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

Add GCHandle in native default ALC at creation #53308

Merged
merged 1 commit into from
Jun 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/mono/mono/metadata/assembly-load-context.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ mono_alcs_init (void)
mono_coop_mutex_init (&alc_list_lock);

default_alc = mono_alc_create (FALSE);
default_alc->gchandle = mono_gchandle_new_internal (NULL, FALSE);
}

MonoAssemblyLoadContext *
Expand Down Expand Up @@ -237,8 +238,10 @@ ves_icall_System_Runtime_Loader_AssemblyLoadContext_InternalInitializeNativeALC
if (is_default_alc) {
alc = default_alc;
g_assert (alc);
if (!alc->gchandle)
alc->gchandle = this_gchandle;

// Change target of the existing GCHandle
mono_gchandle_set_target (alc->gchandle, mono_gchandle_get_target_internal (this_gchandle));
mono_gchandle_free_internal (this_gchandle);
} else {
alc = mono_alc_create_individual (this_gchandle, collectible, error);
}
Expand Down Expand Up @@ -416,6 +419,9 @@ mono_alc_is_default (MonoAssemblyLoadContext *alc)
MonoAssemblyLoadContext *
mono_alc_from_gchandle (MonoGCHandle alc_gchandle)
{
if (alc_gchandle == default_alc->gchandle)
return default_alc;

HANDLE_FUNCTION_ENTER ();
MonoManagedAssemblyLoadContextHandle managed_alc = MONO_HANDLE_CAST (MonoManagedAssemblyLoadContext, mono_gchandle_get_target_handle (alc_gchandle));
MonoAssemblyLoadContext *alc = MONO_HANDLE_GETVAL (managed_alc, native_assembly_load_context);
Expand All @@ -438,6 +444,9 @@ 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);
Expand Down
1 change: 1 addition & 0 deletions src/mono/mono/metadata/mono-private-unstable.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ typedef MonoAssembly * (*MonoAssemblyPreLoadFuncV3) (MonoAssemblyLoadContextGCHa
MONO_API MONO_RT_EXTERNAL_ONLY void
mono_install_assembly_preload_hook_v3 (MonoAssemblyPreLoadFuncV3 func, void *user_data, mono_bool append);

// This can point at NULL before the default ALC is initialized
MONO_API MONO_RT_EXTERNAL_ONLY MonoAssemblyLoadContextGCHandle
mono_alc_get_default_gchandle (void);

Expand Down
6 changes: 6 additions & 0 deletions src/mono/mono/metadata/native-library.c
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,9 @@ 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;
Expand Down Expand Up @@ -693,6 +696,9 @@ 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;
Expand Down