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

NativeAOT: Mark module constructors in MSR #19097

Merged
merged 1 commit into from
Sep 27, 2023

Conversation

ivanpovazan
Copy link
Contributor

@ivanpovazan ivanpovazan commented Sep 25, 2023

There is an issue with managed static registrar and explicitly rooted assemblies (through linker descriptor files) where the linker does not emit the created module initializer.

This was originally reported in: dotnet/runtime#92106
and a smaller repro can be set up in: tests/dotnet/MySingleView test project with the following changes:

Smaller repro

  1. Add a descriptor xml - Roots.xml
<linker>
  <assembly fullname="MySingleView" preserve="All" />
</linker>
  1. Include the root descriptor in the MySingleView.csproj
<ItemGroup>
     <TrimmerRootDescriptor Condition="'$(_RootSelf)' == 'true'" Include="Roots.xml" />
</ItemGroup>
  1. Publish/run the app on a device
dotnet publish -c Release -r ios-arm64 -p:PublishAot=true -p:PublishAotUsingRuntimePack=true -p:_RootSelf=true -t:Run /bl
  1. The app crashes at start up with:
2023-09-25 18:55:42.562 MySingleView[477:55844] *** Terminating app due to uncaught exception 'ObjCRuntime.RuntimeException', reason: 'Can't register the class MySingleView.AppDelegate when the dynamic registrar has been linked away. (ObjCRuntime.RuntimeException)
   at ObjCRuntime.Class.GetClassHandle(Type, Boolean, Boolean&) + 0x1c4
   at UIKit.UIApplication.Main(String[], Type, Type) + 0x8c
   at MySingleView!<BaseAddress>+0x2fae7c
'

NOTE: This is reported for NativeAOT but seems like a general problem

Additionally, here are the disassembled linked assemblies:

It can be seen that the rooted one is missing:

// ================== GLOBAL METHODS =========================

.method private hidebysig specialname rtspecialname static 
        void  .cctor() cil managed
{
  // Code size       11 (0xb)
  .maxstack  1
  IL_0000:  newobj     instance void ObjCRuntime.__Registrar__::.ctor()
  IL_0005:  call       void [Microsoft.iOS]ObjCRuntime.RegistrarHelper::Register(class [Microsoft.iOS]ObjCRuntime.IManagedRegistrar)
  IL_000a:  ret
} // end of global method .cctor

The fix

This PR fixes this behaviour by explicitly marking module constructor to prevent this from happening.


Fixes dotnet/runtime#92106

@ivanpovazan
Copy link
Contributor Author

/cc: @filipnavara

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [CI Build] Windows Integration Tests passed 💻

All Windows Integration Tests passed.

Pipeline on Agent
Hash: 58cb13adfa16b87144a2551c2276808497df9996 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [PR Build] Tests on macOS M1 - Mac Ventura (13.0) passed 💻

All tests on macOS M1 - Mac Ventura (13.0) passed.

Pipeline on Agent
Hash: [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [PR Build] Tests on macOS M1 - Mac Big Sur (11.5) passed 💻

All tests on macOS M1 - Mac Big Sur (11.5) passed.

Pipeline on Agent
Hash: [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

📚 [PR Build] Artifacts 📚

Packages generated

View packages

Pipeline on Agent
Hash: [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

✅ API diff for current PR / commit

Legacy Xamarin (No breaking changes)
  • iOS (no change detected)
  • tvOS (no change detected)
  • watchOS (no change detected)
  • macOS (no change detected)
NET (empty diffs)
  • iOS: (empty diff detected)
  • tvOS: (empty diff detected)
  • MacCatalyst: (empty diff detected)
  • macOS: (empty diff detected)

✅ API diff vs stable

Legacy Xamarin (No breaking changes)
.NET (No breaking changes)
Legacy Xamarin (stable) vs .NET

ℹ️ Generator diff

Generator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes)

Pipeline on Agent
Hash: 58cb13adfa16b87144a2551c2276808497df9996 [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2
Copy link
Collaborator

🚀 [CI Build] Test results 🚀

Test results

✅ All tests passed on VSTS: simulator tests.

🎉 All 233 tests passed 🎉

Tests counts

✅ bcl: All 69 tests passed. Html Report (VSDrops) Download
✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests: All 1 tests passed. Html Report (VSDrops) Download
✅ fsharp: All 7 tests passed. Html Report (VSDrops) Download
✅ framework: All 8 tests passed. Html Report (VSDrops) Download
✅ generator: All 2 tests passed. Html Report (VSDrops) Download
✅ interdependent_binding_projects: All 7 tests passed. Html Report (VSDrops) Download
✅ install_source: All 1 tests passed. Html Report (VSDrops) Download
✅ introspection: All 8 tests passed. Html Report (VSDrops) Download
✅ linker: All 65 tests passed. Html Report (VSDrops) Download
✅ mac_binding_project: All 1 tests passed. Html Report (VSDrops) Download
✅ mmp: All 2 tests passed. Html Report (VSDrops) Download
✅ mononative: All 6 tests passed. Html Report (VSDrops) Download
✅ monotouch: All 39 tests passed. [attempt 3] Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
✅ mtouch: All 1 tests passed. Html Report (VSDrops) Download
✅ xammac: All 3 tests passed. Html Report (VSDrops) Download
✅ xcframework: All 8 tests passed. Html Report (VSDrops) Download
✅ xtro: All 2 tests passed. Html Report (VSDrops) Download

Pipeline on Agent
Hash: 58cb13adfa16b87144a2551c2276808497df9996 [PR build]

@ivanpovazan ivanpovazan marked this pull request as ready for review September 26, 2023 12:02
@ivanpovazan
Copy link
Contributor Author

@mandel-macaque I think we should add a test that covers this behaviour, although I am not sure what would be the best place. Do you have any suggestions?

Additionally, we should try to get this in for RC2.

@ivanpovazan
Copy link
Contributor Author

Adding a unit test will be added in a separate PR.
I have opened: #19108 to track this

@dalexsoto
Copy link
Member

Hello @ivanpovazan thank you for the fix, unfortunately due to schedule is a little late for RC2, this will be going out with RTM

@dalexsoto dalexsoto merged commit 951e756 into xamarin:net8.0 Sep 27, 2023
78 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants