dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.95k stars 4.65k forks source link

Android API 23, Runtime Crash in .NET 9 #106025

Open jonathanpeppers opened 1 month ago

jonathanpeppers commented 1 month ago

Description

We have a nightly test pipeline that runs our test suite on older Android emulators.

The API 23 emulator crashes at runtime with:

08-06 03:33:12.480  2745  2745 F mono-rt : [ERROR] FATAL UNHANDLED EXCEPTION: Nested exception detected.
08-06 03:33:12.480  2745  2745 F mono-rt : Original Exception: at System.Threading.Thread.get_CurrentThread () [0x00009] in <d7679677bb2b4cc291070c60be6bf246>:0
08-06 03:33:12.480  2745  2745 F mono-rt : at System.Threading.Monitor.Exit (object) [0x0000e] in <d7679677bb2b4cc291070c60be6bf246>:0
08-06 03:33:12.480  2745  2745 F mono-rt : at System.AppContext.GetData (string) [0x00037] in <d7679677bb2b4cc291070c60be6bf246>:0
08-06 03:33:12.480  2745  2745 F mono-rt : at System.AppContext.TryGetSwitch (string,bool&) [0x00040] in <d7679677bb2b4cc291070c60be6bf246>:0
08-06 03:33:12.480  2745  2745 F mono-rt : at System.AppContextConfigHelper.GetBooleanConfig (string,bool) [0x00000] in <d7679677bb2b4cc291070c60be6bf246>:0
08-06 03:33:12.480  2745  2745 F mono-rt : at System.LocalAppContextSwitches.GetDefaultShowILOffsetSetting () [0x00014] in <d7679677bb2b4cc291070c60be6bf246>:0
08-06 03:33:12.480  2745  2745 F mono-rt : at System.Diagnostics.StackTrace.ToString (System.Diagnostics.StackTrace/TraceFormat,System.Text.StringBuilder) [0x002ee] in <d7679677bb2b4cc291070c60be6bf246>:0
08-06 03:33:12.480  2745  2745 F mono-rt : at System.Diagnostics.StackTrace.ToString (System.Diagnostics.StackTrace/TraceFormat) [0x0000b] in <d7679677bb2b4cc291070c60be6bf246>:0
08-06 03:33:12.480  2745  2745 F mono-rt : at System.Exception.GetStackTrace () [0x00007] in <d7679677bb2b4cc291070c60be6bf246>:0
08-06 03:33:12.480  2745  2745 F mono-rt : at System.Exception.get_StackTrace () [0x00023] in <d7679677bb2b4cc291070c60be6bf246>:0
08-06 03:33:12.480  2745  2745 F mono-rt : at System.Exception.ToString () [0x0001f] in <d7679677bb2b4cc291070c60be6bf246>:0
08-06 03:33:12.480  2745  2745 F mono-rt : at System.Exception.ToString () [0x0000c] in <d7679677bb2b4cc291070c60be6bf246>:0
08-06 03:33:12.480  2745  2745 F mono-rt : at System.Exception.ToString () [0x0000c] in <d7679677bb2b4cc291070c60be6bf246>:0
08-06 03:33:12.480  2745  2745 F mono-rt : 
08-06 03:33:12.480  2745  2745 F mono-rt : Nested exception:at System.Threading.Thread.get_CurrentThread () [0x00009] in <d7679677bb2b4cc291070c60be6bf246>:0
08-06 03:33:12.480  2745  2745 F mono-rt : at System.Threading.Monitor.Exit (object) [0x0000e] in <d7679677bb2b4cc291070c60be6bf246>:0
08-06 03:33:12.480  2745  2745 F mono-rt : at System.AppContext.GetData (string) [0x00037] in <d7679677bb2b4cc291070c60be6bf246>:0
08-06 03:33:12.480  2745  2745 F mono-rt : at System.AppContext.TryGetSwitch (string,bool&) [0x00040] in <d7679677bb2b4cc291070c60be6bf246>:0
08-06 03:33:12.480  2745  2745 F mono-rt : at System.AppContextConfigHelper.GetBooleanConfig (string,bool) [0x00000] in <d7679677bb2b4cc291070c60be6bf246>:0
08-06 03:33:12.480  2745  2745 F mono-rt : at System.LocalAppContextSwitches.GetDefaultShowILOffsetSetting () [0x00014] in <d7679677bb2b4cc291070c60be6bf246>:0
08-06 03:33:12.480  2745  2745 F mono-rt : at System.Diagnostics.StackTrace.ToString (System.Diagnostics.StackTrace/TraceFormat,System.Text.StringBuilder) [0x002ee] in <d7679677bb2b4cc291070c60be6bf246>:0
08-06 03:33:12.480  2745  2745 F mono-rt : at System.Diagnostics.StackTrace.ToString (System.Diagnostics.StackTrace/TraceFormat) [0x0000b] in <d7679677bb2b4cc291070c60be6bf246>:0
08-06 03:33:12.480  2745  2745 F mono-rt : at System.Exception.GetStackTrace () [0x00007] in <d7679677bb2b4cc291070c60be6bf246>:0
08-06 03:33:12.480  2745  2745 F mono-rt : at System.Exception.get_StackTrace () [0x00023] in <d7679677bb2b4cc291070c60be6bf246>:0
08-06 03:33:12.480  2745  2745 F mono-rt : at System.Exception.ToString () [0x0001f] in <d7679677bb2b4cc291070c60be6bf246>:0
08-06 03:33:12.480  2745  2745 F mono-rt : at System.Exception.ToString () [0x0000c] in <d7679677bb2b4cc291070c60be6bf246>:0
08-06 03:33:12.480  2745  2745 F mono-rt : at System.Exception.ToString () [0x0000c] in <d7679677bb2b4cc291070c60be6bf246>:0

This appears to be working fine on newer API levels.

Reproduction Steps

Run this test project on an API 23 emulator:

Expected behavior

Our test suite passes on API 23 emulators.

Actual behavior

Our test suite crashes on API 23 emulators.

Regression?

Yes, this is working in .NET 8.

Known Workarounds

n/a

Configuration

Noticed with:

I think this was also happening in .NET 9 Preview 7 builds.

Other information

logcat-Release23-Mono.Android.NET_Tests.txt

dotnet-policy-service[bot] commented 1 month ago

Tagging subscribers to this area: @dotnet/area-system-runtime See info in area-owners.md if you want to be subscribed.

akoeplinger commented 1 month ago

This looks like two issues in one: it's trying to throw an exception but then crashes because of the nested exception.

dotnet-policy-service[bot] commented 1 month ago

Tagging subscribers to this area: @dotnet/area-system-runtime See info in area-owners.md if you want to be subscribed.

dotnet-policy-service[bot] commented 1 month ago

Tagging subscribers to 'arch-android': @vitek-karas, @simonrozsival, @steveisok, @akoeplinger See info in area-owners.md if you want to be subscribed.

steveisok commented 1 month ago

@jonathanpeppers from the logcat, it appears libSystem.Native could not be loaded. Is there something wrong with your setup?

jonathanpeppers commented 1 month ago

@grendello do you know what might cause:

08-06 03:33:11.720  2745  2745 D monodroid-assembly: monodroid_dlopen: hash for name 'libSystem.Native' is 0x5b9ade60
08-06 03:33:11.721  2745  2745 D monodroid-assembly: monodroid_dlopen: hash match found, DSO name is 'libSystem.Native.so'
08-06 03:33:11.723  2745  2745 I monodroid-assembly: Trying to load shared library '/data/app/Mono.Android.NET_Tests-1/base.apk!/lib/x86/libSystem.Native.so'
08-06 03:33:11.732  2745  2745 I monodroid-assembly: Failed to load shared library '/data/app/Mono.Android.NET_Tests-1/base.apk!/lib/x86/libSystem.Native.so'. dlopen failed: library "/data/app/Mono.Android.NET_Tests-1/base.apk!/lib/x86/libSystem.Native.so" not found
08-06 03:33:11.732  2745  2745 I monodroid-assembly: Trying to load shared library '/data/app/Mono.Android.NET_Tests-1/split_config.x86.apk!/lib/x86/libSystem.Native.so'
08-06 03:33:11.735  2745  2745 I monodroid-assembly: Failed to load shared library '/data/app/Mono.Android.NET_Tests-1/split_config.x86.apk!/lib/x86/libSystem.Native.so'. dlopen failed: cannot find "libc.so" from verneed[1] in DT_NEEDED list for "/data/app/Mono.Android.NET_Tests-1/split_config.x86.apk"
08-06 03:33:11.736  2745  2745 I monodroid-assembly: Trying to load shared library '/data/app/Mono.Android.NET_Tests-1/base.apk!/lib/x86/libSystem.Native'
08-06 03:33:11.736  2745  2745 I monodroid-assembly: Failed to load shared library '/data/app/Mono.Android.NET_Tests-1/base.apk!/lib/x86/libSystem.Native'. dlopen failed: library "/data/app/Mono.Android.NET_Tests-1/base.apk!/lib/x86/libSystem.Native" not found
08-06 03:33:11.737  2745  2745 I monodroid-assembly: Trying to load shared library '/data/app/Mono.Android.NET_Tests-1/split_config.x86.apk!/lib/x86/libSystem.Native'
08-06 03:33:11.737  2745  2745 I monodroid-assembly: Failed to load shared library '/data/app/Mono.Android.NET_Tests-1/split_config.x86.apk!/lib/x86/libSystem.Native'. dlopen failed: library "/data/app/Mono.Android.NET_Tests-1/split_config.x86.apk!/lib/x86/libSystem.Native" not found
08-06 03:33:11.738  2745  2745 W monodroid-assembly: Shared library 'libSystem.Native' not loaded, p/invoke 'SystemNative_SchedGetCpu' may fail
08-06 03:33:11.738  2745  2745 F monodroid-assembly: Failed to load symbol 'SystemNative_SchedGetCpu' from shared library 'libSystem.Native'

But earlier in the log:

08-06 03:33:11.008  2745  2745 D monodroid-assembly: /data/app/Mono.Android.NET_Tests-1/split_config.x86.apk entry: lib/x86/libSystem.Native.so
08-06 03:33:11.008  2745  2745 D monodroid-assembly:     ZIP: local header offset: 921492; data offset: 933888; file size: 96760
08-06 03:33:11.008  2745  2745 D monodroid-assembly: Found a shared library entry lib/x86/libSystem.Native.so (index: 2; name: libSystem.Native.so; hash: 0x79d6a0ba; apk offset: 0)

This is a 32-bit emulator, could this be fallout of the 16k alignment changes?

grendello commented 3 weeks ago

@jonathanpeppers 32-bit shared libraries are aligned to 4k, 16k applies only to 64-bit targets. That's what NDK r27 does, so I followed suit, but it might be still a system error. We store the dlerror message when loading the shared library and pass it back to Mono via the dlopen hook here.

@steveisok @akoeplinger does Mono log the err when it sees one?

akoeplinger commented 3 weeks ago

No, looks like we don't do anything with the error: https://github.com/dotnet/runtime/blob/dec456802525f73944de05a59c4b7db7d67b4a8a/src/mono/mono/utils/mono-dl.c#L269-L271

akoeplinger commented 3 weeks ago

@grendello isn't that the real error? sounds weird though

dlopen failed: cannot find "libc.so" from verneed[1] in DT_NEEDED list for "/data/app/Mono.Android.NET_Tests-1/split_config.x86.apk"

grendello commented 3 weeks ago

@akoeplinger on one hand yes, it is, on the other... how do we even run the app if libc.so cannot be found? :) Just doesn't make sense