Skip to content
Merged
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
Allow interop resolvers to return self handle
  • Loading branch information
am11 authored and github-actions committed Nov 8, 2022
commit 43da0993e5a0f2f725e5a87fc4bb3a3082644fd2
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