Skip to content

Commit 6ab036f

Browse files
Allow interop resolvers to return self handle (#78018)
Co-authored-by: Adeel <[email protected]>
1 parent 12b1873 commit 6ab036f

File tree

1 file changed

+38
-25
lines changed

1 file changed

+38
-25
lines changed

Diff for: src/mono/mono/metadata/native-library.c

+38-25
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,41 @@ netcore_probe_for_module_nofail (MonoImage *image, const char *file_name, int fl
584584
return result;
585585
}
586586

587+
static MonoDl*
588+
netcore_lookup_self_native_handle (void)
589+
{
590+
ERROR_DECL (load_error);
591+
if (!internal_module)
592+
internal_module = mono_dl_open_self (load_error);
593+
594+
if (!internal_module)
595+
mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT, "DllImport error loading library '__Internal': '%s'.", mono_error_get_message_without_fields (load_error));
596+
597+
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_DLLIMPORT, "Native library found via __Internal.");
598+
mono_error_cleanup (load_error);
599+
600+
return internal_module;
601+
}
602+
603+
static MonoDl* native_handle_lookup_wrapper (gpointer handle)
604+
{
605+
MonoDl *result = NULL;
606+
607+
if (!internal_module)
608+
netcore_lookup_self_native_handle ();
609+
610+
if (internal_module->handle == handle) {
611+
result = internal_module;
612+
}
613+
else {
614+
native_library_lock ();
615+
result = netcore_handle_lookup (handle);
616+
native_library_unlock ();
617+
}
618+
619+
return result;
620+
}
621+
587622
static MonoDl *
588623
netcore_resolve_with_dll_import_resolver (MonoAssemblyLoadContext *alc, MonoAssembly *assembly, const char *scope, guint32 flags, MonoError *error)
589624
{
@@ -631,9 +666,7 @@ netcore_resolve_with_dll_import_resolver (MonoAssemblyLoadContext *alc, MonoAsse
631666
mono_runtime_invoke_checked (resolve, NULL, args, error);
632667
goto_if_nok (error, leave);
633668

634-
native_library_lock ();
635-
result = netcore_handle_lookup (lib);
636-
native_library_unlock ();
669+
result = native_handle_lookup_wrapper (lib);
637670

638671
leave:
639672
HANDLE_FUNCTION_RETURN_VAL (result);
@@ -688,9 +721,7 @@ netcore_resolve_with_load (MonoAssemblyLoadContext *alc, const char *scope, Mono
688721
mono_runtime_invoke_checked (resolve, NULL, args, error);
689722
goto_if_nok (error, leave);
690723

691-
native_library_lock ();
692-
result = netcore_handle_lookup (lib);
693-
native_library_unlock ();
724+
result = native_handle_lookup_wrapper (lib);
694725

695726
leave:
696727
HANDLE_FUNCTION_RETURN_VAL (result);
@@ -755,9 +786,7 @@ netcore_resolve_with_resolving_event (MonoAssemblyLoadContext *alc, MonoAssembly
755786
mono_runtime_invoke_checked (resolve, NULL, args, error);
756787
goto_if_nok (error, leave);
757788

758-
native_library_lock ();
759-
result = netcore_handle_lookup (lib);
760-
native_library_unlock ();
789+
result = native_handle_lookup_wrapper (lib);
761790

762791
leave:
763792
HANDLE_FUNCTION_RETURN_VAL (result);
@@ -802,22 +831,6 @@ netcore_check_alc_cache (MonoAssemblyLoadContext *alc, const char *scope)
802831
return result;
803832
}
804833

805-
static MonoDl*
806-
netcore_lookup_self_native_handle (void)
807-
{
808-
ERROR_DECL (load_error);
809-
if (!internal_module)
810-
internal_module = mono_dl_open_self (load_error);
811-
812-
if (!internal_module)
813-
mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT, "DllImport error loading library '__Internal': '%s'.", mono_error_get_message_without_fields (load_error));
814-
815-
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_DLLIMPORT, "Native library found via __Internal.");
816-
mono_error_cleanup (load_error);
817-
818-
return internal_module;
819-
}
820-
821834
static MonoDl *
822835
netcore_lookup_native_library (MonoAssemblyLoadContext *alc, MonoImage *image, const char *scope, guint32 flags)
823836
{

0 commit comments

Comments
 (0)