Skip to content

Commit 5cea3d6

Browse files
authored
[mono] Invoke AssemblyLoad hooks from RuntimeAssemblyBuilder:.ctor () instead from native code. (#85307)
The call from native code is made before the manifest module was created, causing errors if the hook tried to access Assembly.ManifestModule. Fixes #84771.
1 parent e6924ee commit 5cea3d6

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

src/libraries/System.Private.CoreLib/src/System/Runtime/Loader/AssemblyLoadContext.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,11 @@ private static void OnAssemblyLoad(RuntimeAssembly assembly)
722722
return InvokeResolveEvent(AssemblyResolve, assembly, assemblyFullName);
723723
}
724724

725+
internal static void InvokeAssemblyLoadEvent(Assembly assembly)
726+
{
727+
AssemblyLoad?.Invoke(AppDomain.CurrentDomain, new AssemblyLoadEventArgs(assembly));
728+
}
729+
725730
[UnconditionalSuppressMessage("SingleFile", "IL3000: Avoid accessing Assembly file path when publishing as a single file",
726731
Justification = "The code handles the Assembly.Location equals null")]
727732
private static RuntimeAssembly? InvokeResolveEvent(ResolveEventHandler? eventHandler, RuntimeAssembly? assembly, string name)

src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeAssemblyBuilder.Mono.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ internal RuntimeAssemblyBuilder(AssemblyName n, AssemblyBuilderAccess access)
256256
// Netcore only allows one module per assembly
257257
manifest_module = new RuntimeModuleBuilder(this, "RefEmit_InMemoryManifestModule");
258258
modules = new RuntimeModuleBuilder[] { manifest_module };
259+
260+
AssemblyLoadContext.InvokeAssemblyLoadEvent (this);
259261
}
260262

261263
public override bool ReflectionOnly

src/mono/mono/metadata/appdomain.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,10 @@ mono_domain_fire_assembly_load_event (MonoDomain *domain, MonoAssembly *assembly
571571
if (!method)
572572
goto exit;
573573

574+
if (assembly->dynamic)
575+
/* Called by RuntimeAssemblyBuilder:.ctor () after the manifest module has been created */
576+
goto exit;
577+
574578
MonoReflectionAssemblyHandle assembly_handle;
575579
assembly_handle = mono_assembly_get_object_handle (assembly, error);
576580
goto_if_nok (error, exit);

0 commit comments

Comments
 (0)