Skip to content

Commit

Permalink
[release/9.0.1xx] [ObjCRuntime] Ensure all assemblies' module constru…
Browse files Browse the repository at this point in the history
…ctors have executed before failing registrar lookup. (#21891)

The managed static registrar depends on each assembly registering itself
to
find types from that assembly. This is done using a module constructor,
but
the module constructor isn't necessarily executed before the managed
static
registrar needs the assembly registered (in particular for app
extensions,
which don't have a managed entry point).

That would lead to exceptions like this:

*** Terminating app due to uncaught exception
'ObjCRuntime.RuntimeException', reason: 'Could not find the type
'ObjCRuntime.__Registrar__' in the assembly '...'.
(ObjCRuntime.RuntimeException)
		at ObjCRuntime.RegistrarHelper.GetMapEntry(String assemblyName)
		at ObjCRuntime.RegistrarHelper.GetMapEntry(Assembly assembly)
		[...]

Fix this by manually calling the module constructor for every loaded
assembly
if we can't find it in the map of registered assemblies.


Backport of #21885

Co-authored-by: Rolf Bjarne Kvinge <[email protected]>
  • Loading branch information
1 parent c29b554 commit b3ee0f2
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/ObjCRuntime/RegistrarHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,25 @@ static MapInfo GetMapEntry (string assemblyName)
{
if (TryGetMapEntry (assemblyName, out var rv))
return rv;

#if TRACE
Runtime.NSLog ($"RegistrarHelper.GetMapEntry ({assemblyName}) => failed to find entry, will ensure module constructors are called for all loaded assemblies.");
#endif
// An assembly is only registered if we've (tried to) execute code from it, which is not guaranteed to
// happen before we get here (in particular for app extensions, which don't have a managed Main method).
// So here we loop over all the assemblies in the current domain, make sure the module constructor
// has been called for all of them, and then we try again.
var assemblies = AppDomain.CurrentDomain.GetAssemblies ();
foreach (var asm in assemblies)
RuntimeHelpers.RunModuleConstructor (asm.ManifestModule.ModuleHandle);

if (TryGetMapEntry (assemblyName, out rv))
return rv;

#if TRACE
Runtime.NSLog ($"RegistrarHelper.GetMapEntry ({assemblyName}) => failed to find entry for the second time.");
#endif

throw ErrorHelper.CreateError (8055, Errors.MX8055 /* Could not find the type 'ObjCRuntime.__Registrar__' in the assembly '{0}' */, assemblyName);
}

Expand Down

16 comments on commit b3ee0f2

@vs-mobiletools-engineering-service2
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ [CI Build] Build passed (Detect API changes) ✅

Pipeline on Agent
Hash: b3ee0f2422cd950a0c0c676fb8ac9e7dc5f98290 [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 Unable to find the contents for the comment: D:\a\1\s\change-detection\results\gh-comment.md does not exist :fire

Pipeline on Agent
Hash: b3ee0f2422cd950a0c0c676fb8ac9e7dc5f98290 [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ [CI Build] Build passed (Detect API changes) ✅

Pipeline on Agent
Hash: b3ee0f2422cd950a0c0c676fb8ac9e7dc5f98290 [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ [CI Build] Build passed (Build packages) ✅

Pipeline on Agent
Hash: b3ee0f2422cd950a0c0c676fb8ac9e7dc5f98290 [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 Unable to find the contents for the comment: D:\a\1\s\change-detection\results\gh-comment.md does not exist :fire

Pipeline on Agent
Hash: b3ee0f2422cd950a0c0c676fb8ac9e7dc5f98290 [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ [CI Build] Build passed (Build macOS tests) ✅

Pipeline on Agent
Hash: b3ee0f2422cd950a0c0c676fb8ac9e7dc5f98290 [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💻 [CI Build] Tests on macOS arm64 - Mac Sequoia (15) passed 💻

All tests on macOS arm64 - Mac Sequoia (15) passed.

Pipeline on Agent
Hash: b3ee0f2422cd950a0c0c676fb8ac9e7dc5f98290 [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💻 [CI Build] Tests on macOS M1 - Mac Ventura (13) passed 💻

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

Pipeline on Agent
Hash: b3ee0f2422cd950a0c0c676fb8ac9e7dc5f98290 [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ [CI Build] Build passed (Detect API changes) ✅

Pipeline on Agent
Hash: b3ee0f2422cd950a0c0c676fb8ac9e7dc5f98290 [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💻 [CI Build] Windows Integration Tests passed 💻

All Windows Integration Tests passed.

Pipeline on Agent
Hash: b3ee0f2422cd950a0c0c676fb8ac9e7dc5f98290 [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💻 [CI Build] Tests on macOS M1 - Mac Monterey (12) passed 💻

All tests on macOS M1 - Mac Monterey (12) passed.

Pipeline on Agent
Hash: b3ee0f2422cd950a0c0c676fb8ac9e7dc5f98290 [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 Unable to find the contents for the comment: D:\a\1\s\change-detection\results\gh-comment.md does not exist :fire

Pipeline on Agent
Hash: b3ee0f2422cd950a0c0c676fb8ac9e7dc5f98290 [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💻 [CI Build] Tests on macOS X64 - Mac Sonoma (14) passed 💻

All tests on macOS X64 - Mac Sonoma (14) passed.

Pipeline on Agent
Hash: b3ee0f2422cd950a0c0c676fb8ac9e7dc5f98290 [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ [CI Build] Build passed (Detect API changes) ✅

Pipeline on Agent
Hash: b3ee0f2422cd950a0c0c676fb8ac9e7dc5f98290 [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 Unable to find the contents for the comment: D:\a\1\s\change-detection\results\gh-comment.md does not exist :fire

Pipeline on Agent
Hash: b3ee0f2422cd950a0c0c676fb8ac9e7dc5f98290 [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 [CI Build] Test results 🚀

Test results

✅ All tests passed on VSTS: test results.

🎉 All 103 tests passed 🎉

Tests counts

✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (iOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (MacCatalyst): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (macOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (Multiple platforms): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (tvOS): All 1 tests passed. Html Report (VSDrops) Download
✅ framework: All 2 tests passed. Html Report (VSDrops) Download
✅ fsharp: All 4 tests passed. Html Report (VSDrops) Download
✅ generator: All 5 tests passed. Html Report (VSDrops) Download
✅ interdependent-binding-projects: All 4 tests passed. Html Report (VSDrops) Download
✅ introspection: All 4 tests passed. Html Report (VSDrops) Download
✅ linker: All 40 tests passed. Html Report (VSDrops) Download
✅ monotouch (iOS): All 7 tests passed. Html Report (VSDrops) Download
✅ monotouch (MacCatalyst): All 8 tests passed. Html Report (VSDrops) Download
✅ monotouch (macOS): All 9 tests passed. Html Report (VSDrops) Download
✅ monotouch (tvOS): All 7 tests passed. Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
✅ xcframework: All 4 tests passed. Html Report (VSDrops) Download
✅ xtro: All 1 tests passed. Html Report (VSDrops) Download

Pipeline on Agent
Hash: b3ee0f2422cd950a0c0c676fb8ac9e7dc5f98290 [CI build]

Please sign in to comment.