From c356875521fd087addb11e7efc04227cd5ea2d6c Mon Sep 17 00:00:00 2001 From: Aleksey Kliger Date: Tue, 27 Jul 2021 11:58:22 -0400 Subject: [PATCH 1/2] [loader] Call managed resolving events for the default ALC Refine the work in https://github.com/dotnet/runtime/commit/f70b5b7aef21a70478e9b86dba7d996a57a9add9 The issue is that even if we know that the ALC gchandle points to null in native, we can't automatically skip the call to the managed resolving event (for native library, for example) because the native event is indirectly responsible for creating the default ALC managed object. Instead, check in native if the gchandle is equal to the default ALC's gchandle (which is initially allocated with a null target) and if so pass IntPtr.Zero to the managed code, which will call `AssemblyLoadContext.GEtAssemblyLoadContext (IntPtr gch)` which in turn will construct the managed object for the default ALC. Fixes https://github.com/dotnet/runtime/issues/55921 --- .../Loader/AssemblyLoadContext.Mono.cs | 14 ++----------- .../mono/metadata/assembly-load-context.c | 12 +++++++---- src/mono/mono/metadata/native-library.c | 21 ++++++++++++++----- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/mono/System.Private.CoreLib/src/System/Runtime/Loader/AssemblyLoadContext.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Runtime/Loader/AssemblyLoadContext.Mono.cs index eb5ef034972a4f..2ec69c7393648d 100644 --- a/src/mono/System.Private.CoreLib/src/System/Runtime/Loader/AssemblyLoadContext.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Runtime/Loader/AssemblyLoadContext.Mono.cs @@ -126,24 +126,14 @@ public void StartProfileOptimization(string? profile) // success. private static Assembly? MonoResolveUsingResolvingEvent(IntPtr gchALC, string assemblyName) { - AssemblyLoadContext context; - // This check exists because the function can be called early in startup, before the default ALC is initialized - if (gchALC == IntPtr.Zero) - context = Default; - else - context = (AssemblyLoadContext)(GCHandle.FromIntPtr(gchALC).Target)!; + AssemblyLoadContext context = GetAssemblyLoadContext(gchALC); return context.ResolveUsingEvent(new AssemblyName(assemblyName)); } // Invoked by Mono to resolve requests to load satellite assemblies. private static Assembly? MonoResolveUsingResolveSatelliteAssembly(IntPtr gchALC, string assemblyName) { - AssemblyLoadContext context; - // This check exists because the function can be called early in startup, before the default ALC is initialized - if (gchALC == IntPtr.Zero) - context = Default; - else - context = (AssemblyLoadContext)(GCHandle.FromIntPtr(gchALC).Target)!; + AssemblyLoadContext context = GetAssemblyLoadContext(gchALC); return context.ResolveSatelliteAssembly(new AssemblyName(assemblyName)); } diff --git a/src/mono/mono/metadata/assembly-load-context.c b/src/mono/mono/metadata/assembly-load-context.c index d3e4df84d2da3c..d324204d69344c 100644 --- a/src/mono/mono/metadata/assembly-load-context.c +++ b/src/mono/mono/metadata/assembly-load-context.c @@ -445,9 +445,6 @@ invoke_resolve_method (MonoMethod *resolve_method, MonoAssemblyLoadContext *alc, if (mono_runtime_get_no_exec ()) return NULL; - if (!mono_gchandle_get_target_internal (alc->gchandle)) - return NULL; - HANDLE_FUNCTION_ENTER (); aname_str = mono_stringify_assembly_name (aname); @@ -457,7 +454,14 @@ invoke_resolve_method (MonoMethod *resolve_method, MonoAssemblyLoadContext *alc, MonoReflectionAssemblyHandle assm; gpointer gchandle; - gchandle = (gpointer)alc->gchandle; + /* for the default ALC, pass NULL to ask for the Default ALC - see + * AssemblyLoadContext.GetAssemblyLoadContext(IntPtr gchManagedAssemblyLoadContext) - which + * will create the managed ALC object if it hasn't been created yet + */ + if (alc->gchandle == default_alc->gchandle) + gchandle = NULL; + else + gchandle = GUINT_TO_POINTER (alc->gchandle); gpointer args [2]; args [0] = &gchandle; args [1] = MONO_HANDLE_RAW (aname_obj); diff --git a/src/mono/mono/metadata/native-library.c b/src/mono/mono/metadata/native-library.c index 6867a07379cd88..cd371139c9224f 100644 --- a/src/mono/mono/metadata/native-library.c +++ b/src/mono/mono/metadata/native-library.c @@ -628,9 +628,6 @@ netcore_resolve_with_load (MonoAssemblyLoadContext *alc, const char *scope, Mono if (mono_runtime_get_no_exec ()) return NULL; - if (!mono_gchandle_get_target_internal (alc->gchandle)) - return NULL; - HANDLE_FUNCTION_ENTER (); MonoStringHandle scope_handle; @@ -638,7 +635,14 @@ netcore_resolve_with_load (MonoAssemblyLoadContext *alc, const char *scope, Mono goto_if_nok (error, leave); gpointer gchandle; - gchandle = GUINT_TO_POINTER (alc->gchandle); + /* for the default ALC, pass NULL to ask for the Default ALC - see + * AssemblyLoadContext.GetAssemblyLoadContext(IntPtr gchManagedAssemblyLoadContext) - which + * will create the managed ALC object if it hasn't been created yet + */ + if (alc->gchandle == mono_alc_get_default ()->gchandle) + gchandle = NULL; + else + gchandle = GUINT_TO_POINTER (alc->gchandle); gpointer args [3]; args [0] = MONO_HANDLE_RAW (scope_handle); args [1] = &gchandle; @@ -708,7 +712,14 @@ netcore_resolve_with_resolving_event (MonoAssemblyLoadContext *alc, MonoAssembly goto_if_nok (error, leave); gpointer gchandle; - gchandle = GUINT_TO_POINTER (alc->gchandle); + /* for the default ALC, pass NULL to ask for the Default ALC - see + * AssemblyLoadContext.GetAssemblyLoadContext(IntPtr gchManagedAssemblyLoadContext) - which + * will create the managed ALC object if it hasn't been created yet + */ + if (alc->gchandle == mono_alc_get_default ()->gchandle) + gchandle = NULL; + else + gchandle = GUINT_TO_POINTER (alc->gchandle); gpointer args [4]; args [0] = MONO_HANDLE_RAW (scope_handle); args [1] = MONO_HANDLE_RAW (assembly_handle); From 70fd153fcecaef13029bc0382f30159b956b2c70 Mon Sep 17 00:00:00 2001 From: Aleksey Kliger Date: Tue, 27 Jul 2021 12:53:22 -0400 Subject: [PATCH 2/2] remove one more early exit --- src/mono/mono/metadata/native-library.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/mono/mono/metadata/native-library.c b/src/mono/mono/metadata/native-library.c index cd371139c9224f..05046b6c79e7cd 100644 --- a/src/mono/mono/metadata/native-library.c +++ b/src/mono/mono/metadata/native-library.c @@ -698,9 +698,6 @@ netcore_resolve_with_resolving_event (MonoAssemblyLoadContext *alc, MonoAssembly if (mono_runtime_get_no_exec ()) return NULL; - if (!mono_gchandle_get_target_internal (alc->gchandle)) - return NULL; - HANDLE_FUNCTION_ENTER (); MonoStringHandle scope_handle;