From 730fd83a1c3a51cc377fe32c603234951fe17ce0 Mon Sep 17 00:00:00 2001 From: Aleksey Kliger Date: Mon, 24 Apr 2023 16:08:36 -0400 Subject: [PATCH] [native-library] If a dllimport is specified with an absolute path, look for it first The unmanaged native library probing documentation says to try absolute paths without variations https://learn.microsoft.com/en-us/dotnet/core/dependency-loading/default-probing#unmanaged-native-library-probing Manual backport of https://github.com/dotnet/runtime/pull/85255 to net6 --- src/mono/mono/metadata/native-library.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/mono/mono/metadata/native-library.c b/src/mono/mono/metadata/native-library.c index f84f2dc8e3061..ab5f643238655 100644 --- a/src/mono/mono/metadata/native-library.c +++ b/src/mono/mono/metadata/native-library.c @@ -518,11 +518,18 @@ netcore_probe_for_module (MonoImage *image, const char *file_name, int flags) // TODO: this algorithm doesn't quite match CoreCLR, so respecting DLLIMPORTSEARCHPATH_LEGACY_BEHAVIOR makes little sense // If the difference becomes a problem, overhaul this algorithm to match theirs exactly + gboolean probe_first_without_prepend = FALSE; + #if defined(HOST_ANDROID) // On Android, try without any path additions first. It is sensitive to probing that will always miss // and lookup for some libraries is required to use a relative path - module = netcore_probe_for_module_variations (NULL, file_name, lflags); + probe_first_without_prepend = TRUE; +#else + if (file_name != NULL && g_path_is_absolute (file_name)) + probe_first_without_prepend = TRUE; #endif + if (module == NULL && probe_first_without_prepend) + module = netcore_probe_for_module_variations (NULL, file_name, lflags); // Check the NATIVE_DLL_SEARCH_DIRECTORIES for (int i = 0; i < pinvoke_search_directories_count && module == NULL; ++i) @@ -537,13 +544,11 @@ netcore_probe_for_module (MonoImage *image, const char *file_name, int flags) g_free (mdirname); } -#if !defined(HOST_ANDROID) - // Try without any path additions - if (module == NULL) + // Try without any path additions, if we didn't try it already + if (module == NULL && !probe_first_without_prepend) { module = netcore_probe_for_module_variations (NULL, file_name, lflags); } -#endif // TODO: Pass remaining flags on to LoadLibraryEx on Windows where appropriate, see https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.dllimportsearchpath?view=netcore-3.1