Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSLog constructor yields System.TypeInitializationException: handle is null #7959

Closed
t9mike opened this issue Feb 23, 2020 · 6 comments · Fixed by #7962
Closed

OSLog constructor yields System.TypeInitializationException: handle is null #7959

t9mike opened this issue Feb 23, 2020 · 6 comments · Fixed by #7962
Assignees
Labels
bug If an issue is a bug or a pull request a bug fix iOS Issues affecting iOS
Milestone

Comments

@t9mike
Copy link

t9mike commented Feb 23, 2020

Steps to Reproduce

  1. Create iOS phone project
  2. Add var oslog = new OSLog(NSBundle.MainBundle.BundleIdentifier, "Test");
    to controller's ViewDidLoad
  3. Get exception

Expected Behavior

Construct an OSLog

Actual Behavior

System.TypeInitializationException: The type initializer for 'CoreFoundation.OSLog' threw an exception. ---> System.Exception: Could not initialize an instance of the type 'CoreFoundation.OSLog': handle is null.
It is possible to ignore this condition by setting ObjCRuntime.Class.ThrowOnInitFailure to false.
at CoreFoundation.NativeObject.InitializeHandle (System.IntPtr handle) [0x00014] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.14.1.30/src/Xamarin.iOS/CoreFoundation/NativeObject.cs:74
at CoreFoundation.NativeObject.set_Handle (System.IntPtr value) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.14.1.30/src/Xamarin.iOS/CoreFoundation/NativeObject.cs:34
at CoreFoundation.NativeObject..ctor (System.IntPtr handle, System.Boolean owns) [0x00006] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.14.1.30/src/Xamarin.iOS/CoreFoundation/NativeObject.cs:43
at CoreFoundation.OSLog..ctor (System.IntPtr handle, System.Boolean owns) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.14.1.30/src/Xamarin.iOS/CoreFoundation/OSLog.cs:59
at CoreFoundation.OSLog..cctor () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.14.1.30/src/Xamarin.iOS/CoreFoundation/OSLog.cs:32
--- End of inner exception stack trace ---
at OSLogTest.ViewController.ViewDidLoad () [0x00008] in /Users/mmuegel/Projects/OSLogTest/ViewController.cs:19
at at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.14.1.30/src/Xamarin.iOS/UIKit/UIApplication.cs:86
at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0000e] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.14.1.30/src/Xamarin.iOS/UIKit/UIApplication.cs:65
at OSLogTest.Application.Main (System.String[] args) [0x00001] in /Users/mmuegel/Projects/OSLogTest/Main.cs:12

Environment

=== Visual Studio Community 2019 for Mac (Preview) ===

Version 8.5 Preview (8.5 build 2987)
Installation UUID: 49afa5cc-00b9-4c18-a240-552efe177384
	GTK+ 2.24.23 (Raleigh theme)
	Xamarin.Mac 6.14.1.30 (d16-5 / 4cc97e67)

	Package version: 608000106

=== Mono Framework MDK ===

Runtime:
	Mono 6.8.0.106 (2019-10/0e18125d711) (64-bit)
	Package version: 608000106

=== Roslyn (Language Service) ===

3.5.0-beta2-20052-02+04aada103893143c9da8a96bdc465e106c1f0151

=== NuGet ===

Version: 5.4.0.6315

=== .NET Core SDK ===

SDK: /usr/local/share/dotnet/sdk/3.1.101/Sdks
SDK Versions:
	3.1.101
	3.1.100
	3.0.101
	3.0.100
	2.2.203
	2.1.505
	2.1.504
	2.1.503
	2.1.500
	2.1.302
	2.0.0
	1.0.0-preview2-1-003177
MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/6.8.0/lib/mono/msbuild/Current/bin/Sdks

=== .NET Core Runtime ===

Runtime: /usr/local/share/dotnet/dotnet
Runtime Versions:
	3.1.1
	3.1.0
	3.0.1
	3.0.0
	2.2.4
	2.1.15
	2.1.14
	2.1.13
	2.1.9
	2.1.8
	2.1.7
	2.1.6
	2.1.2
	2.0.0
	1.1.0

=== Xamarin.Profiler ===

Version: 1.6.12.26
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Updater ===

Version: 11

=== Xamarin.Android ===

Version: 10.2.0.84 (Visual Studio Community)
Commit: xamarin-android/d16-5/ac3f71f
Android SDK: /Users/mmuegel/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		8.1 (API level 27)

SDK Tools Version: 26.1.1
SDK Platform Tools Version: 28.0.0
SDK Build Tools Version: 27.0.3

Build Information: 
Mono: df42020
Java.Interop: xamarin/java.interop/d16-5@c0cc770
ProGuard: xamarin/proguard/master@905836d
SQLite: xamarin/sqlite/3.28.0@46204c4
Xamarin.Android Tools: xamarin/xamarin-android-tools/master@9f4ed4b

=== Microsoft Mobile OpenJDK ===

Java SDK: /Users/mmuegel/Library/Developer/Xamarin/jdk/microsoft_dist_openjdk_8.0.25
1.8.0-25
Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

=== Android SDK Manager ===

Version: 16.5.0.38
Hash: 5adcbfa
Branch: remotes/origin/d16-5~1
Build date: 2020-02-13 23:02:30 UTC

=== Android Device Manager ===

Version: 16.5.0.65
Hash: 327dcef
Branch: remotes/origin/d16-5-fix/mac-pre-catalina~1
Build date: 2020-02-13 23:02:52 UTC

=== Xamarin Designer ===

Version: 16.5.0.459
Hash: 8aade6148
Branch: remotes/origin/d16-5
Build date: 2020-02-04 08:22:34 UTC

=== Apple Developer Tools ===

Xcode 11.3.1 (15715)
Build 11C505

=== Xamarin.Mac ===

Version: 6.14.1.30 (Visual Studio Community)
Hash: 4cc97e67
Branch: d16-5
Build date: 2020-02-04 08:39:21-0500

=== Xamarin.iOS ===

Version: 13.14.1.30 (Visual Studio Community)
Hash: 4cc97e67
Branch: d16-5
Build date: 2020-02-04 08:39:21-0500

=== Xamarin Inspector ===

Version: 1.4.3
Hash: db27525
Branch: 1.4-release
Build date: Mon, 09 Jul 2018 21:20:18 GMT
Client compatibility: 1

=== Build Information ===

Release ID: 805002987
Git revision: 466e632f6c8e74ecc2efc78bd9ae10c7d3b7ee30
Build date: 2020-02-14 13:34:22-05
Build branch: release-8.5
Xamarin extensions: 466e632f6c8e74ecc2efc78bd9ae10c7d3b7ee30

=== Operating System ===

Mac OS X 10.15.3
Darwin 19.3.0 Darwin Kernel Version 19.3.0
    Thu Jan  9 20:58:23 PST 2020
    root:xnu-6153.81.5~1/RELEASE_X86_64 x86_64

Build Logs

Example Project (If Possible)

OSLogTest.zip #

@spouliot spouliot added bug If an issue is a bug or a pull request a bug fix iOS Issues affecting iOS labels Feb 24, 2020
@spouliot spouliot added this to the Future milestone Feb 24, 2020
@spouliot
Copy link
Contributor

#7141

sadly approved without #7141 (comment)

@spouliot spouliot self-assigned this Feb 24, 2020
@spouliot
Copy link
Contributor

Got an idea for a new introspection tests to run all .cctor :)
I'll fix that while testing the test...

spouliot added a commit to spouliot/xamarin-macios that referenced this issue Feb 24, 2020
`Default` property was using a nil-handle which is incorrect since
* we don't allow that (this is generally a bad sign)
* it does not map to `OS_LOG_DEFAULT`

Since `Default` was assigned in the type (static) constructor then
the whole type became unusable :(

Header `log.h` shows that the right definition requires us to load a
field and use it.

```
define OS_LOG_DEFAULT OS_OBJECT_GLOBAL_OBJECT(os_log_t, _os_log_default)
```

While `NULL` can actually be used for disabled (not exposed) by this
contradicting (nullability-wise) macro

```
define OS_LOG_DISABLED ((os_log_t _Nonnull)NULL)
```

Also adds unit tests. A more general tests for `.cctor` will be added
to introspection tests in a separate PR.

Fixes dotnet#7959
spouliot added a commit that referenced this issue Feb 25, 2020
`Default` property was using a nil-handle which is incorrect since
* we don't allow that (this is generally a bad sign)
* it does not map to `OS_LOG_DEFAULT`

Since `Default` was assigned in the type (static) constructor then
the whole type became unusable :(

Header `log.h` shows that the right definition requires us to load a
field and use it.

```
define OS_LOG_DEFAULT OS_OBJECT_GLOBAL_OBJECT(os_log_t, _os_log_default)
```

While `NULL` can actually be used for disabled (not exposed) by this
contradicting (nullability-wise) macro

```
define OS_LOG_DISABLED ((os_log_t _Nonnull)NULL)
```

Also adds unit tests. A more general tests for `.cctor` will be added
to introspection tests in a separate PR.

Fixes #7959
spouliot added a commit to spouliot/xamarin-macios that referenced this issue Feb 25, 2020
This will spot cases like dotnet#7959
where a type (static) constructor can fail at runtime, leading to
`TypeLoadException`

Ideally it's covered by it's own tests but it's better covered twice
than never :)
@t9mike
Copy link
Author

t9mike commented Feb 25, 2020

Thank you @spouliot. Is there a nightly build or other installer available to try this out?

spouliot added a commit to spouliot/xamarin-macios that referenced this issue Feb 25, 2020
…net#7962)

`Default` property was using a nil-handle which is incorrect since
* we don't allow that (this is generally a bad sign)
* it does not map to `OS_LOG_DEFAULT`

Since `Default` was assigned in the type (static) constructor then
the whole type became unusable :(

Header `log.h` shows that the right definition requires us to load a
field and use it.

```
define OS_LOG_DEFAULT OS_OBJECT_GLOBAL_OBJECT(os_log_t, _os_log_default)
```

While `NULL` can actually be used for disabled (not exposed) by this
contradicting (nullability-wise) macro

```
define OS_LOG_DISABLED ((os_log_t _Nonnull)NULL)
```

Also adds unit tests. A more general tests for `.cctor` will be added
to introspection tests in a separate PR.

Fixes dotnet#7959
@spouliot
Copy link
Contributor

@t9mike soon but not yet. Packages are built after the PR is merged (which happened 2 hours ago). Look at the commit itself, e.g. 9a88551 for master, and (in a few hours) you'll see packages that correspond to the commit.

A bit later (once the back port to d16-6 is merged) you'll have packages for d16-6 (our next public release).

spouliot added a commit that referenced this issue Feb 25, 2020
…) (#7977)

`Default` property was using a nil-handle which is incorrect since
* we don't allow that (this is generally a bad sign)
* it does not map to `OS_LOG_DEFAULT`

Since `Default` was assigned in the type (static) constructor then
the whole type became unusable :(

Header `log.h` shows that the right definition requires us to load a
field and use it.

```
define OS_LOG_DEFAULT OS_OBJECT_GLOBAL_OBJECT(os_log_t, _os_log_default)
```

While `NULL` can actually be used for disabled (not exposed) by this
contradicting (nullability-wise) macro

```
define OS_LOG_DISABLED ((os_log_t _Nonnull)NULL)
```

Also adds unit tests. A more general tests for `.cctor` will be added
to introspection tests in a separate PR.

Fixes #7959
@t9mike
Copy link
Author

t9mike commented Feb 26, 2020

@spouliot I was able to use OSLog against iPhone real device OK and saw expected output in Console. With watchOS real device, I get crash. But this is unrelated to OSLog as once I disabled OSLog I still get crash. OK on simulator.

I used stock Release channel everything else and installed Xamarin.iOS 13.19.0.76 package. Do I need to install other packages to try this out on watchOS?

0   libsystem_kernel.dylib              0x21050c70 __pthread_kill + 8
1   libsystem_pthread.dylib             0x210cd980 pthread_kill + 220
2   libsystem_c.dylib                   0x20fa46f4 abort + 100
3   SundialWatchAppExtension            0x04cec534 print_callback(char const*, int) + 12584244 (runtime.m:1218)
4   SundialWatchAppExtension            0x04cda330 monoeg_g_logv_nofree + 12510000 (goutput.c:150)
5   SundialWatchAppExtension            0x04cda380 monoeg_g_log + 12510080 (goutput.c:165)
6   SundialWatchAppExtension            0x04ccc9b8 mono_threads_transition_do_blocking + 12454328 (mono-threads-state-machine.c:753)
7   SundialWatchAppExtension            0x04cca4a0 mono_threads_enter_gc_safe_region_unbalanced_with_info + 12444832 (mono-threads-coop.c:324)
8   SundialWatchAppExtension            0x04c36a6c mono_gc_pthread_create + 11840108 (sgen-mono.c:2444)
9   SundialWatchAppExtension            0x04ccb784 mono_thread_platform_create_thread + 12449668 (mono-threads-posix.c:97)
10  SundialWatchAppExtension            0x04c59b08 create_thread + 11983624 (threads.c:1385)
11  SundialWatchAppExtension            0x04c595cc mono_thread_create_internal + 11982284 (threads.c:529)
12  SundialWatchAppExtension            0x04bb3ca0 mono_gc_init_finalizer_thread + 11304096 (gc.c:1004)
13  SundialWatchAppExtension            0x04cf3dd8 -[XamarinGCSupport init] + 12615128 (monotouch-main.m:0)
14  SundialWatchAppExtension            0x04cf41cc xamarin_main + 12616140 (monotouch-main.m:463)
15  SundialWatchAppExtension            0x041019dc xamarin_watchextension_main + 88540 (main.m:65)
16  libdyld.dylib                       0x20ed0a78 start + 4

Watch extension:

image

@spouliot
Copy link
Contributor

@t9mike the stack trace looks related to the GC initialization (on XI side). Can you file a operate issue for this and attach the symbolicated crash report from the device (so we can see what other threads were doing).

The same fix, backported to d16-6, is now available from packages inside 514991e#commitcomment-37486792
That's an older mono (and closer to the current on stable - even if the delta is quite large at this time).

monojenkins pushed a commit to monojenkins/xamarin-macios that referenced this issue Feb 26, 2020
This will spot cases like dotnet#7959
where a type (static) constructor can fail at runtime, leading to
`TypeLoadException`

Ideally it's covered by it's own tests but it's better covered twice
than never :)
spouliot added a commit that referenced this issue Feb 26, 2020
This will spot cases like #7959
where a type (static) constructor can fail at runtime, leading to
`TypeLoadException`

Ideally it's covered by it's own tests but it's better covered twice
than never :)

On iOS 64bits [1] simulator we hit some failures [2], later, if the
`.cctor` is executed. It's not a big deal to avoid those types since
we it will be executed on devices later.

[1] API not present on 32bits

[2] Fixing the following triggers similar failures for `DCDevice`

```
ApiClassPtrTest.VerifyClassPtr: class_ptr and RegisterAttribute are different: NFCIso15693CustomCommandConfiguration
Expected: 0
But was: 140735471513712

ApiSelectorTest.StaticMethods: 7 errors found in 2788 static selector validated:
CoreNFC.NFCIso15693ReaderSession : readingAvailable
CoreNFC.NFCNdefMessage : ndefMessageWithData:
CoreNFC.NFCNdefPayload : wellKnownTypeURIPayloadWithString:
CoreNFC.NFCNdefPayload : wellKnownTypeURIPayloadWithURL:
CoreNFC.NFCNdefPayload : wellKnownTypeTextPayloadWithString:locale:
CoreNFC.NFCNdefReaderSession : readingAvailable
CoreNFC.NFCReaderSession : readingAvailable
Expected: 0
But was: 7
```
spouliot added a commit that referenced this issue Feb 26, 2020
This will spot cases like #7959
where a type (static) constructor can fail at runtime, leading to
`TypeLoadException`

Ideally it's covered by it's own tests but it's better covered twice
than never :)

On iOS 64bits [1] simulator we hit some failures [2], later, if the
`.cctor` is executed. It's not a big deal to avoid those types since
we it will be executed on devices later.

[1] API not present on 32bits

[2] Fixing the following triggers similar failures for `DCDevice`

```
ApiClassPtrTest.VerifyClassPtr: class_ptr and RegisterAttribute are different: NFCIso15693CustomCommandConfiguration
Expected: 0
But was: 140735471513712

ApiSelectorTest.StaticMethods: 7 errors found in 2788 static selector validated:
CoreNFC.NFCIso15693ReaderSession : readingAvailable
CoreNFC.NFCNdefMessage : ndefMessageWithData:
CoreNFC.NFCNdefPayload : wellKnownTypeURIPayloadWithString:
CoreNFC.NFCNdefPayload : wellKnownTypeURIPayloadWithURL:
CoreNFC.NFCNdefPayload : wellKnownTypeTextPayloadWithString:locale:
CoreNFC.NFCNdefReaderSession : readingAvailable
CoreNFC.NFCReaderSession : readingAvailable
Expected: 0
But was: 7
```

Co-authored-by: Sebastien Pouliot <[email protected]>
dalexsoto pushed a commit to dalexsoto/xamarin-macios that referenced this issue Mar 3, 2020
`Default` property was using a nil-handle which is incorrect since
* we don't allow that (this is generally a bad sign)
* it does not map to `OS_LOG_DEFAULT`

Since `Default` was assigned in the type (static) constructor then
the whole type became unusable :(

Header `log.h` shows that the right definition requires us to load a
field and use it.

```
define OS_LOG_DEFAULT OS_OBJECT_GLOBAL_OBJECT(os_log_t, _os_log_default)
```

While `NULL` can actually be used for disabled (not exposed) by this
contradicting (nullability-wise) macro

```
define OS_LOG_DISABLED ((os_log_t _Nonnull)NULL)
```

Also adds unit tests. A more general tests for `.cctor` will be added
to introspection tests in a separate PR.

Fixes dotnet#7959
@ghost ghost locked as resolved and limited conversation to collaborators May 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug If an issue is a bug or a pull request a bug fix iOS Issues affecting iOS
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants