Skip to content

[Xamarin.Android.Build.Tasks] fix <CilStrip/> for Hybrid AOT - #4850

Merged
jonpryor merged 1 commit into
dotnet:masterfrom
jonathanpeppers:fix-hybrid-aot
Jun 23, 2020
Merged

[Xamarin.Android.Build.Tasks] fix <CilStrip/> for Hybrid AOT#4850
jonpryor merged 1 commit into
dotnet:masterfrom
jonathanpeppers:fix-hybrid-aot

Conversation

@jonathanpeppers

Copy link
Copy Markdown
Member

Fixes: #4818

Use of Hybrid AOT currently results in assemblies that have not been
CIL-stripped.

In 77ab240, a change was made to fix how Mono.Android.dll was
treated during AOT. Mono.Android.dll used to be the only assembly
copied to the shrunk directory, and we had two copies of the this
assembly passed to the AOT compiler. This change unintentionally made
<CilStrip/> strip the wrong set of assemblies...

The initial fix would be:

<CilStrip
    ...
-   ResolvedAssemblies="@(_ResolvedAssemblies)">
+   ResolvedAssemblies="@(_ShrunkAssemblies)">

Unfortunately, this causes a crash:

06-11 16:19:08.557 E/AndroidRuntime(18806): java.lang.UnsatisfiedLinkError: No implementation found for void mono.android.TypeManager.n_activate(java.lang.String, java.lang.String, java.lang.Object, java.lang.Object[]) (tried Java_mono_android_TypeManager_n_1activate and Java_mono_android_TypeManager_n_1activate__Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_Object_2_3Ljava_lang_Object_2)
06-11 16:19:08.557 E/AndroidRuntime(18806): 	at mono.android.TypeManager.n_activate(Native Method)
06-11 16:19:08.557 E/AndroidRuntime(18806): 	at mono.android.TypeManager.Activate(:7)
06-11 16:19:08.557 E/AndroidRuntime(18806): 	at crc64446c24ccf511bf5f.SplashScreenActivity.<init>(:25)
06-11 16:19:08.557 E/AndroidRuntime(18806): 	at java.lang.Class.newInstance(Native Method)
06-11 16:19:08.557 E/AndroidRuntime(18806): 	at android.app.Instrumentation.newActivity(Instrumentation.java:1086)
06-11 16:19:08.557 E/AndroidRuntime(18806): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2809)
06-11 16:19:08.557 E/AndroidRuntime(18806): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
06-11 16:19:08.557 E/AndroidRuntime(18806): 	at android.app.ActivityThread.-wrap14(ActivityThread.java)
06-11 16:19:08.557 E/AndroidRuntime(18806): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
06-11 16:19:08.557 E/AndroidRuntime(18806): 	at android.os.Handler.dispatchMessage(Handler.java:102)
06-11 16:19:08.557 E/AndroidRuntime(18806): 	at android.os.Looper.loop(Looper.java:154)
06-11 16:19:08.557 E/AndroidRuntime(18806): 	at android.app.ActivityThread.main(ActivityThread.java:6682)
06-11 16:19:08.557 E/AndroidRuntime(18806): 	at java.lang.reflect.Method.invoke(Native Method)
06-11 16:19:08.557 E/AndroidRuntime(18806): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
06-11 16:19:08.557 E/AndroidRuntime(18806): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

Thinking about how things "used to work", we weren't stripping the
correct Mono.Android.dll. So we could run <CilStrip/> on every
assembly besides Mono.Android.dll?

I think this could be improved further if we could strip
Mono.Android.dll, but this at least gets things back to working the
way they used to.

I updated the BuildIncrementalAot test that had a //TODO comment,
which works properly now. I also added a new test to check that method
bodies are stripped.

Fixes: dotnet#4818

Use of Hybrid AOT currently results in assemblies that have not been
CIL-stripped.

In 77ab240, a change was made to fix how `Mono.Android.dll` was
treated during AOT. `Mono.Android.dll` used to be the only assembly
copied to the `shrunk` directory, and we had two copies of the this
assembly passed to the AOT compiler. This change unintentionally made
`<CilStrip/>` strip the wrong set of assemblies...

The initial fix would be:

    <CilStrip
        ...
    -   ResolvedAssemblies="@(_ResolvedAssemblies)">
    +   ResolvedAssemblies="@(_ShrunkAssemblies)">

Unfortunately, this causes a crash:

    06-11 16:19:08.557 E/AndroidRuntime(18806): java.lang.UnsatisfiedLinkError: No implementation found for void mono.android.TypeManager.n_activate(java.lang.String, java.lang.String, java.lang.Object, java.lang.Object[]) (tried Java_mono_android_TypeManager_n_1activate and Java_mono_android_TypeManager_n_1activate__Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_Object_2_3Ljava_lang_Object_2)
    06-11 16:19:08.557 E/AndroidRuntime(18806): 	at mono.android.TypeManager.n_activate(Native Method)
    06-11 16:19:08.557 E/AndroidRuntime(18806): 	at mono.android.TypeManager.Activate(:7)
    06-11 16:19:08.557 E/AndroidRuntime(18806): 	at crc64446c24ccf511bf5f.SplashScreenActivity.<init>(:25)
    06-11 16:19:08.557 E/AndroidRuntime(18806): 	at java.lang.Class.newInstance(Native Method)
    06-11 16:19:08.557 E/AndroidRuntime(18806): 	at android.app.Instrumentation.newActivity(Instrumentation.java:1086)
    06-11 16:19:08.557 E/AndroidRuntime(18806): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2809)
    06-11 16:19:08.557 E/AndroidRuntime(18806): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
    06-11 16:19:08.557 E/AndroidRuntime(18806): 	at android.app.ActivityThread.-wrap14(ActivityThread.java)
    06-11 16:19:08.557 E/AndroidRuntime(18806): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
    06-11 16:19:08.557 E/AndroidRuntime(18806): 	at android.os.Handler.dispatchMessage(Handler.java:102)
    06-11 16:19:08.557 E/AndroidRuntime(18806): 	at android.os.Looper.loop(Looper.java:154)
    06-11 16:19:08.557 E/AndroidRuntime(18806): 	at android.app.ActivityThread.main(ActivityThread.java:6682)
    06-11 16:19:08.557 E/AndroidRuntime(18806): 	at java.lang.reflect.Method.invoke(Native Method)
    06-11 16:19:08.557 E/AndroidRuntime(18806): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
    06-11 16:19:08.557 E/AndroidRuntime(18806): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

Thinking about how things "used to work", we weren't stripping the
*correct* `Mono.Android.dll`. So we could run `<CilStrip/>` on every
assembly *besides* `Mono.Android.dll`?

I think this could be improved further if we could strip
`Mono.Android.dll`, but this at least gets things back to working the
way they used to.

I updated the `BuildIncrementalAot` test that had a `//TODO` comment,
which works properly now. I also added a new test to check that method
bodies are stripped.
@radekdoulik

Copy link
Copy Markdown
Member

There is new test failure though:

  [AOT] MONO_PATH="C:\A\vs2019xam00000X-1\_work\2\s\bin\TestRelease\temp\BuildAotApplication_arm64-v8a_Normal_False_True\obj\Release\android\assets\shrunk" MONO_ENV_OPTIONS="" "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Xamarin\Android\cross-arm64" --response=obj\Release\aot\arm64-v8a\System.dll\response.txt (TaskId:183)
  [AOT] response file obj\Release\aot\arm64-v8a\System.dll\response.txt: "--aot=outfile=obj\Release\aot\arm64-v8a\libaot-System.dll.so,asmwriter,mtriple=aarch64-linux-android,tool-prefix=C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Xamarin\Android\ndk\aarch64-linux-android-,llvm-path=C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Xamarin\Android,temp-path=obj\Release\aot\arm64-v8a\System.dll,ld-flags=" C:\A\vs2019xam00000X-1\_work\2\s\bin\TestRelease\temp\BuildAotApplication_arm64-v8a_Normal_False_True\obj\Release\android\assets\shrunk\System.dll
   (TaskId:183)
C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(2132,3): error XA3001: Could not AOT the assembly: System.Core.dll [C:\A\vs2019xam00000X-1\_work\2\s\bin\TestRelease\temp\BuildAotApplication_arm64-v8a_Normal_False_True\UnnamedProject.csproj]
Done executing task "Aot" -- FAILED. (TaskId:183)

@jonathanpeppers

Copy link
Copy Markdown
Member Author

Could not AOT the assembly: System.Core.dll

This looks similar to some of the file locking issues we see on the bots, since it passes for me locally, let me do a retry.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AOT mode Hybrid does not CIL strip the assemblies that end up in the APK

4 participants