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..5e805701055b78 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); @@ -456,8 +453,7 @@ invoke_resolve_method (MonoMethod *resolve_method, MonoAssemblyLoadContext *alc, goto_if_nok (error, leave); MonoReflectionAssemblyHandle assm; - gpointer gchandle; - gchandle = (gpointer)alc->gchandle; + gpointer gchandle = mono_alc_get_gchandle_for_resolving (alc); gpointer args [2]; args [0] = &gchandle; args [1] = MONO_HANDLE_RAW (aname_obj); diff --git a/src/mono/mono/metadata/loader-internals.h b/src/mono/mono/metadata/loader-internals.h index 0e1934455d1dc5..bc79d38d4eb5bf 100644 --- a/src/mono/mono/metadata/loader-internals.h +++ b/src/mono/mono/metadata/loader-internals.h @@ -269,6 +269,19 @@ mono_alc_get_ambient (void) return mono_alc_get_default (); } +static inline MonoGCHandle +mono_alc_get_gchandle_for_resolving (MonoAssemblyLoadContext *alc) +{ + /* 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) + return NULL; + else + return GUINT_TO_POINTER (alc->gchandle); +} + MonoAssemblyLoadContext * mono_alc_from_gchandle (MonoGCHandle alc_gchandle); diff --git a/src/mono/mono/metadata/native-library.c b/src/mono/mono/metadata/native-library.c index 6867a07379cd88..dd67477d092d4c 100644 --- a/src/mono/mono/metadata/native-library.c +++ b/src/mono/mono/metadata/native-library.c @@ -628,17 +628,13 @@ 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; scope_handle = mono_string_new_handle (scope, error); goto_if_nok (error, leave); - gpointer gchandle; - gchandle = GUINT_TO_POINTER (alc->gchandle); + gpointer gchandle = mono_alc_get_gchandle_for_resolving (alc); gpointer args [3]; args [0] = MONO_HANDLE_RAW (scope_handle); args [1] = &gchandle; @@ -694,9 +690,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; @@ -707,8 +700,7 @@ netcore_resolve_with_resolving_event (MonoAssemblyLoadContext *alc, MonoAssembly assembly_handle = mono_assembly_get_object_handle (assembly, error); goto_if_nok (error, leave); - gpointer gchandle; - gchandle = GUINT_TO_POINTER (alc->gchandle); + gpointer gchandle = mono_alc_get_gchandle_for_resolving (alc); gpointer args [4]; args [0] = MONO_HANDLE_RAW (scope_handle); args [1] = MONO_HANDLE_RAW (assembly_handle);