Skip to content

Commit b6c22f3

Browse files
[Mono.Android] fix crash on startup with EnableLLVM (dotnet#7188)
`dotnet new android` apps would crash on startup when built with `-c Release -p:EnableLLVM=true`: 07-20 08:55:44.642 2983 2983 F monodroid-assembly: Internal p/invoke symbol 'java-interop @ java_interop_jvm_list' (hash: 0x58c48fc8b89cb484) not found in compile-time map. ... 07-20 08:55:44.834 3004 3004 F DEBUG : signal 6 (SIGABRT), code -1 (SI_QUEUE), fault addr -------- 07-20 08:55:44.834 3004 3004 F DEBUG : Abort message: 'Internal p/invoke symbol 'java-interop @ java_interop_jvm_list' (hash: 0x58c48fc8b89cb484) not found in compile-time map.' `java_interop_jvm_list` should *never* be called on Android, it is the result of `new AndroidRuntime()` having not been called yet? The problem being that the static `Android.App.Application.cctor` was somehow running *before* `JNIEnv.Initialize()` was complete? And this only happens with LLVM? Reviewing the code: [UnmanagedCallersOnly] internal static unsafe void Initialize (JnienvInitializeArgs* args) { //... SynchronizationContext.SetSynchronizationContext (Android.App.Application.SynchronizationContext); } We do indeed access `Android.App.Application`... To fix this, we can move this to a new method decorated with `MethodImplOptions.NoInlining`. Apps built with LLVM now launch for me. However, we can't really enable the LLVM `Mono.Android-Tests` quite yet. Read on! ~~ Known Issues ~~ * dotnet/runtime#73003 Using Profiled AOT and LLVM at the same time can hit: [ERROR] FATAL UNHANDLED EXCEPTION: System.PlatformNotSupportedException: Arg_PlatformNotSupported at System.Text.ASCIIUtility.GetIndexOfFirstNonAsciiByte_Intrinsified(Byte* , UIntPtr ) at System.Text.Unicode.Utf8Utility.GetPointerToFirstInvalidByte(Byte* , Int32 , Int32& , Int32& ) at System.Text.UTF8Encoding.GetCharCount(Byte* , Int32 ) at System.String.CreateStringFromEncoding(Byte* , Int32 , Encoding ) at System.Text.Encoding.GetString(Byte* , Int32 ) at System.String.CreateStringForSByteConstructor(Byte* , Int32 ) at System.String.Ctor(SByte* value) at Java.Interop.TypeManager.GetClassName(IntPtr ) at Android.Runtime.JNIEnv.RegisterJniNatives(IntPtr , Int32 , IntPtr , IntPtr , Int32 ) For now we can set `AndroidEnableProfiledAot=false` for the LLVM tests. * dotnet/runtime#73304 Some Java.Interop unit tests fail with: System.InvalidOperationException : InvalidOperation_EnumEnded at System.ArrayEnumerator.get_Current() at Cadenza.Collections.Tests.CollectionContract`1[[System.Int16, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].DisposeCollection(IEnumerable c) at Cadenza.Collections.Tests.CollectionContract`1[[System.Int16, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].CopyTo() at System.Reflection.MethodInvoker.InterpretedInvoke(Object , Span`1 , BindingFlags ) * https://github.com/xamarin/java.interop/blob/a3de91efdaec04b3927b755a07018e8d26cfdc71/tests/Java.Interop-Tests/Cadenza.Collections/CollectionContract.cs#L205-L221 * https://github.com/xamarin/java.interop/blob/a3de91efdaec04b3927b755a07018e8d26cfdc71/tests/Java.Interop-Tests/Cadenza.Collections/CollectionContract.cs#L85-L86 The `InetAccess` category causes the `Mono.Android-Tests` suite to crash with: 08-02 21:26:57.581 5154 5173 I NUnit : System.NetTests.ProxyTest : 646.269 ms 08-02 21:26:57.593 5154 5173 I droid.NET_Test: Explicit concurrent copying GC freed 20(95KB) AllocSpace objects, 0(0B) LOS objects, 59% free, 1054KB/2590KB, paused 1.469ms total 7.180ms 08-02 21:26:57.594 5154 5173 I NUnit : SslTest 08-02 21:26:57.594 5154 5173 I NUnit : HttpsShouldWork 08-02 21:26:57.858 5154 5181 D NetworkSecurityConfig: No Network Security Config specified, using platform default 08-02 21:26:58.585 5154 5164 I droid.NET_Test: Background young concurrent copying GC freed 39658(2920KB) AllocSpace objects, 0(0B) LOS objects, 22% free, 1999KB/2590KB, paused 69.607ms total 194.902ms 08-02 21:26:58.922 5154 5164 I droid.NET_Test: Background concurrent copying GC freed 12412(836KB) AllocSpace objects, 0(0B) LOS objects, 46% free, 6987KB/12MB, paused 2.127ms total 327.448ms 08-02 21:26:58.953 5154 5181 I monodroid-timing: [1/0] LZ4 decompression time for <assembly_store>; elapsed: 0:0::75000 08-02 21:26:58.954 5154 5181 W linker : Warning: "/data/app/Mono.Android.NET_Tests-QcXozuJGB3EpfK6bvDvvMQ==/split_config.x86_64.apk!/lib/x86_64/libaot-System.Formats.Asn1.dll.so" has no DT_SONAME (will use libaot-System.Formats.Asn1.dll.so instead) and will not work when the app moves to API level 23 or later (https://android.googlesource.com/platform/bionic/+/master/missing-soname-enforced-for-api-level-23) (allowing for now because this app's target API level is still 21) 08-02 21:26:59.037 5154 5181 I monodroid-timing: [1/0] LZ4 decompression time for <assembly_store>; elapsed: 0:0::129000 08-02 21:26:59.038 5154 5181 W linker : Warning: "/data/app/Mono.Android.NET_Tests-QcXozuJGB3EpfK6bvDvvMQ==/split_config.x86_64.apk!/lib/x86_64/libaot-System.Runtime.Numerics.dll.so" has no DT_SONAME (will use libaot-System.Runtime.Numerics.dll.so instead) and will not work when the app moves to API level 23 or later (https://android.googlesource.com/platform/bionic/+/master/missing-soname-enforced-for-api-level-23) (allowing for now because this app's target API level is still 21) 08-02 21:26:56.680 5154 5154 W Instrumentation: type=1400 audit(0.0:432): avc: denied { search } for name="run" dev="vdc" ino=32774 scontext=u:r:untrusted_app_25:s0:c512,c768 tcontext=u:object_r:varrun_file:s0 tclass=dir permissive=0 app=Mono.Android.NET_Tests 08-02 21:26:56.770 5154 5154 W Instrumentation: type=1400 audit(0.0:433): avc: denied { ioctl } for path="socket:[49468]" dev="sockfs" ino=49468 ioctlcmd=0x8946 scontext=u:r:untrusted_app_25:s0:c512,c768 tcontext=u:r:untrusted_app_25:s0:c512,c768 tclass=udp_socket permissive=0 app=Mono.Android.NET_Tests 08-02 21:26:56.780 5154 5154 W Instrumentation: type=1400 audit(0.0:434): avc: denied { ioctl } for path="socket:[49468]" dev="sockfs" ino=49468 ioctlcmd=0x8946 scontext=u:r:untrusted_app_25:s0:c512,c768 tcontext=u:r:untrusted_app_25:s0:c512,c768 tclass=udp_socket permissive=0 app=Mono.Android.NET_Tests 08-02 21:27:00.029 1540 1540 I Zygote : Process 5154 exited due to signal 11 (Segmentation fault) Not really any useful info here... Disabled this category for now. We can file an issue for this one, assuming it is different than the other issues.
1 parent 887e0a6 commit b6c22f3

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

build-tools/automation/azure-pipelines.yaml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ stages:
379379
testName: Mono.Android_Tests-Aot
380380
project: tests/Mono.Android-Tests/Mono.Android-Tests.csproj
381381
testResultsFiles: TestResult-Mono.Android_Tests-$(ApkTestConfiguration)-Aot.xml
382-
extraBuildArgs: /p:AotAssemblies=True /p:EnableLlvm=True
382+
extraBuildArgs: /p:AotAssemblies=True /p:EnableLLVM=True
383383
artifactSource: bin/Test$(ApkTestConfiguration)/Mono.Android_Tests-Signed.apk
384384
artifactFolder: AotLlvm
385385

@@ -691,13 +691,15 @@ stages:
691691

692692
- template: yaml-templates/apk-instrumentation.yaml
693693
parameters:
694-
# TODO: disable LLVM test, see: https://github.com/dotnet/runtime/issues/68914
694+
# TODO: disable LLVM test, see:
695+
# https://github.com/dotnet/runtime/issues/68914
696+
# https://github.com/dotnet/runtime/issues/73304
695697
condition: false
696698
configuration: $(XA.Build.Configuration)
697699
testName: Mono.Android.NET_Tests-AotLlvm
698700
project: tests/Mono.Android-Tests/Runtime-Microsoft.Android.Sdk/Mono.Android.NET-Tests.csproj
699701
testResultsFiles: TestResult-Mono.Android.NET_Tests-$(XA.Build.Configuration)AotLlvm.xml
700-
extraBuildArgs: -p:TestsFlavor=AotLlvm -p:EnableLlvm=true
702+
extraBuildArgs: -p:TestsFlavor=AotLlvm -p:EnableLLVM=true -p:AndroidEnableProfiledAot=false
701703
artifactSource: bin/Test$(XA.Build.Configuration)/$(DotNetTargetFramework)-android/Mono.Android.NET_Tests-Signed.aab
702704
artifactFolder: $(DotNetTargetFramework)-AotLlvm
703705
useDotNet: true

src/Mono.Android/Android.Runtime/JNIEnv.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,17 @@ internal static unsafe void Initialize (JnienvInitializeArgs* args)
196196
}
197197

198198
#if !MONOANDROID1_0
199-
SynchronizationContext.SetSynchronizationContext (Android.App.Application.SynchronizationContext);
199+
SetSynchronizationContext ();
200200
#endif
201201
}
202202

203+
#if !MONOANDROID1_0
204+
// NOTE: prevents Android.App.Application static ctor from running
205+
[MethodImpl (MethodImplOptions.NoInlining)]
206+
static void SetSynchronizationContext () =>
207+
SynchronizationContext.SetSynchronizationContext (Android.App.Application.SynchronizationContext);
208+
#endif
209+
203210
internal static void Exit ()
204211
{
205212
/* Manually dispose surfaced objects and close the current JniEnvironment to

tests/Mono.Android-Tests/Runtime-Microsoft.Android.Sdk/Mono.Android.NET-Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
<AndroidUseNegotiateAuthentication>true</AndroidUseNegotiateAuthentication>
2323
<!--
2424
TODO: Fix excluded tests
25-
For AOT, InetAccess excluded due to: https://github.com/dotnet/runtime/issues/56315
25+
For $(EnableLLVM), InetAccess excluded due to: https://github.com/dotnet/runtime/issues/56315
2626
-->
2727
<ExcludeCategories>DotNetIgnore</ExcludeCategories>
28-
<ExcludeCategories Condition=" '$(RunAOTCompilation)' == 'true' ">$(ExcludeCategories):InetAccess</ExcludeCategories>
28+
<ExcludeCategories Condition=" '$(EnableLLVM)' == 'true' ">$(ExcludeCategories):InetAccess</ExcludeCategories>
2929
<ExcludeCategories Condition=" '$(UseInterpreter)' == 'true' ">$(ExcludeCategories):IgnoreInterpreter</ExcludeCategories>
3030
</PropertyGroup>
3131

0 commit comments

Comments
 (0)