Skip to content

Commit 130794d

Browse files
committed
[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
1 parent b181911 commit 130794d

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/mono/mono/metadata/native-library.c

+13-7
Original file line numberDiff line numberDiff line change
@@ -532,15 +532,23 @@ netcore_probe_for_module (MonoImage *image, const char *file_name, int flags, Mo
532532
// If the difference becomes a problem, overhaul this algorithm to match theirs exactly
533533

534534
ERROR_DECL (bad_image_error);
535+
gboolean probe_first_without_prepend = FALSE;
535536

536537
#if defined(HOST_ANDROID)
537538
// On Android, try without any path additions first. It is sensitive to probing that will always miss
538539
// and lookup for some libraries is required to use a relative path
539-
module = netcore_probe_for_module_variations (NULL, file_name, lflags, error);
540-
if (!module && !is_ok (error) && mono_error_get_error_code (error) == MONO_ERROR_BAD_IMAGE)
541-
mono_error_move (bad_image_error, error);
540+
probe_first_without_prepend = TRUE;
541+
#else
542+
if (file_name != NULL && g_path_is_absolute (file_name))
543+
probe_first_without_prepend = TRUE;
542544
#endif
543545

546+
if (module == NULL && probe_first_without_prepend) {
547+
module = netcore_probe_for_module_variations (NULL, file_name, lflags, error);
548+
if (!module && !is_ok (error) && mono_error_get_error_code (error) == MONO_ERROR_BAD_IMAGE)
549+
mono_error_move (bad_image_error, error);
550+
}
551+
544552
// Check the NATIVE_DLL_SEARCH_DIRECTORIES
545553
for (int i = 0; i < pinvoke_search_directories_count && module == NULL; ++i) {
546554
mono_error_cleanup (error);
@@ -563,15 +571,13 @@ netcore_probe_for_module (MonoImage *image, const char *file_name, int flags, Mo
563571
g_free (mdirname);
564572
}
565573

566-
#if !defined(HOST_ANDROID)
567-
// Try without any path additions
568-
if (module == NULL)
574+
// Try without any path additions, if we didn't try it already
575+
if (module == NULL && !probe_first_without_prepend)
569576
{
570577
module = netcore_probe_for_module_variations (NULL, file_name, lflags, error);
571578
if (!module && !is_ok (error) && mono_error_get_error_code (error) == MONO_ERROR_BAD_IMAGE)
572579
mono_error_move (bad_image_error, error);
573580
}
574-
#endif
575581

576582
// 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
577583

0 commit comments

Comments
 (0)