Skip to content

Commit

Permalink
Allow interop resolvers to return self handle (#78018)
Browse files Browse the repository at this point in the history
Co-authored-by: Adeel <[email protected]>
  • Loading branch information
github-actions[bot] and am11 authored Nov 11, 2022
1 parent 12b1873 commit 6ab036f
Showing 1 changed file with 38 additions and 25 deletions.
63 changes: 38 additions & 25 deletions src/mono/mono/metadata/native-library.c
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,41 @@ netcore_probe_for_module_nofail (MonoImage *image, const char *file_name, int fl
return result;
}

static MonoDl*
netcore_lookup_self_native_handle (void)
{
ERROR_DECL (load_error);
if (!internal_module)
internal_module = mono_dl_open_self (load_error);

if (!internal_module)
mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT, "DllImport error loading library '__Internal': '%s'.", mono_error_get_message_without_fields (load_error));

mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_DLLIMPORT, "Native library found via __Internal.");
mono_error_cleanup (load_error);

return internal_module;
}

static MonoDl* native_handle_lookup_wrapper (gpointer handle)
{
MonoDl *result = NULL;

if (!internal_module)
netcore_lookup_self_native_handle ();

if (internal_module->handle == handle) {
result = internal_module;
}
else {
native_library_lock ();
result = netcore_handle_lookup (handle);
native_library_unlock ();
}

return result;
}

static MonoDl *
netcore_resolve_with_dll_import_resolver (MonoAssemblyLoadContext *alc, MonoAssembly *assembly, const char *scope, guint32 flags, MonoError *error)
{
Expand Down Expand Up @@ -631,9 +666,7 @@ netcore_resolve_with_dll_import_resolver (MonoAssemblyLoadContext *alc, MonoAsse
mono_runtime_invoke_checked (resolve, NULL, args, error);
goto_if_nok (error, leave);

native_library_lock ();
result = netcore_handle_lookup (lib);
native_library_unlock ();
result = native_handle_lookup_wrapper (lib);

leave:
HANDLE_FUNCTION_RETURN_VAL (result);
Expand Down Expand Up @@ -688,9 +721,7 @@ netcore_resolve_with_load (MonoAssemblyLoadContext *alc, const char *scope, Mono
mono_runtime_invoke_checked (resolve, NULL, args, error);
goto_if_nok (error, leave);

native_library_lock ();
result = netcore_handle_lookup (lib);
native_library_unlock ();
result = native_handle_lookup_wrapper (lib);

leave:
HANDLE_FUNCTION_RETURN_VAL (result);
Expand Down Expand Up @@ -755,9 +786,7 @@ netcore_resolve_with_resolving_event (MonoAssemblyLoadContext *alc, MonoAssembly
mono_runtime_invoke_checked (resolve, NULL, args, error);
goto_if_nok (error, leave);

native_library_lock ();
result = netcore_handle_lookup (lib);
native_library_unlock ();
result = native_handle_lookup_wrapper (lib);

leave:
HANDLE_FUNCTION_RETURN_VAL (result);
Expand Down Expand Up @@ -802,22 +831,6 @@ netcore_check_alc_cache (MonoAssemblyLoadContext *alc, const char *scope)
return result;
}

static MonoDl*
netcore_lookup_self_native_handle (void)
{
ERROR_DECL (load_error);
if (!internal_module)
internal_module = mono_dl_open_self (load_error);

if (!internal_module)
mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT, "DllImport error loading library '__Internal': '%s'.", mono_error_get_message_without_fields (load_error));

mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_DLLIMPORT, "Native library found via __Internal.");
mono_error_cleanup (load_error);

return internal_module;
}

static MonoDl *
netcore_lookup_native_library (MonoAssemblyLoadContext *alc, MonoImage *image, const char *scope, guint32 flags)
{
Expand Down

0 comments on commit 6ab036f

Please sign in to comment.