Skip to content

Commit 534423d

Browse files
[Mono.Android] fix crash on startup with EnableLLVM
`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. We can also enable the LLVM `Mono.Android-Tests` again.
1 parent 3e4686c commit 534423d

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

build-tools/automation/azure-pipelines.yaml

Lines changed: 2 additions & 4 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,11 @@ stages:
691691

692692
- template: yaml-templates/apk-instrumentation.yaml
693693
parameters:
694-
# TODO: disable LLVM test, see: https://github.com/dotnet/runtime/issues/68914
695-
condition: false
696694
configuration: $(XA.Build.Configuration)
697695
testName: Mono.Android.NET_Tests-AotLlvm
698696
project: tests/Mono.Android-Tests/Runtime-Microsoft.Android.Sdk/Mono.Android.NET-Tests.csproj
699697
testResultsFiles: TestResult-Mono.Android.NET_Tests-$(XA.Build.Configuration)AotLlvm.xml
700-
extraBuildArgs: -p:TestsFlavor=AotLlvm -p:EnableLlvm=true
698+
extraBuildArgs: -p:TestsFlavor=AotLlvm -p:EnableLLVM=true -p:AndroidEnableProfiledAot=false
701699
artifactSource: bin/Test$(XA.Build.Configuration)/$(DotNetTargetFramework)-android/Mono.Android.NET_Tests-Signed.aab
702700
artifactFolder: $(DotNetTargetFramework)-AotLlvm
703701
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)