Skip to content
Open
Show file tree
Hide file tree
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
10 changes: 5 additions & 5 deletions src/mono/mono/metadata/native-library.c
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ static MonoDl* native_handle_lookup_wrapper (gpointer handle)
}

static MonoDl *
netcore_resolve_with_dll_import_resolver (MonoAssemblyLoadContext *alc, MonoAssembly *assembly, const char *scope, guint32 flags, MonoError *error)
netcore_resolve_with_dll_import_resolver (MonoAssemblyLoadContext *alc, MonoAssembly *assembly, const char *scope, gboolean user_specified_flags, guint32 flags, MonoError *error)
{
MonoDl *result = NULL;
gpointer lib = NULL;
Expand Down Expand Up @@ -476,7 +476,7 @@ netcore_resolve_with_dll_import_resolver (MonoAssemblyLoadContext *alc, MonoAsse
goto_if_nok (error, leave);

gboolean has_search_flags;
has_search_flags = flags != 0 ? TRUE : FALSE;
has_search_flags = user_specified_flags;
gpointer args [5];
args [0] = MONO_HANDLE_RAW (scope_handle);
args [1] = MONO_HANDLE_RAW (assembly_handle);
Expand All @@ -493,12 +493,12 @@ netcore_resolve_with_dll_import_resolver (MonoAssemblyLoadContext *alc, MonoAsse
}

static MonoDl *
netcore_resolve_with_dll_import_resolver_nofail (MonoAssemblyLoadContext *alc, MonoAssembly *assembly, const char *scope, guint32 flags)
netcore_resolve_with_dll_import_resolver_nofail (MonoAssemblyLoadContext *alc, MonoAssembly *assembly, const char *scope, gboolean user_specified_flags, guint32 flags)
{
MonoDl *result = NULL;
ERROR_DECL (error);

result = netcore_resolve_with_dll_import_resolver (alc, assembly, scope, flags, error);
result = netcore_resolve_with_dll_import_resolver (alc, assembly, scope, user_specified_flags, flags, error);
if (!is_ok (error))
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_DLLIMPORT, "Error while invoking ALC DllImportResolver(\"%s\") delegate: '%s'", scope, mono_error_get_message (error));

Expand Down Expand Up @@ -703,7 +703,7 @@ netcore_lookup_native_library (MonoAssemblyLoadContext *alc, MonoImage *image, c
goto leave;
}

module = (MonoDl *)netcore_resolve_with_dll_import_resolver_nofail (alc, assembly, scope, flags);
module = (MonoDl *)netcore_resolve_with_dll_import_resolver_nofail (alc, assembly, scope, user_specified_flags, flags);
if (module) {
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_DLLIMPORT, "Native library found via DllImportResolver: '%s'.", scope);
goto add_to_alc_cache;
Expand Down
46 changes: 46 additions & 0 deletions src/tests/Interop/DllImportSearchPaths/DllImportSearchPathsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,31 @@ public static void AssemblyDirectory_SearchFlags_WithDependency_Found()
Assert.Equal(3, sum);
Console.WriteLine("NativeLibraryWithDependency.Sum returned {0}", sum);
}

[Fact]
public static void DllImportResolver_SearchPathMatchesAttributes()
{
DllImportSearchPath? observedSearchPath = null;
NativeLibrary.SetDllImportResolver(Assembly.GetExecutingAssembly(), (libraryName, assembly, searchPath) =>
{
if (libraryName == SearchPathPInvoke.LibraryName)
observedSearchPath = searchPath;

return IntPtr.Zero;
});

Assert.Throws<DllNotFoundException>(() => SearchPathPInvoke.AssemblyDirectory());
Assert.Equal(DllImportSearchPath.AssemblyDirectory, observedSearchPath);

Assert.Throws<DllNotFoundException>(() => SearchPathPInvoke.System32());
Assert.Equal(DllImportSearchPath.System32, observedSearchPath);

Assert.Throws<DllNotFoundException>(() => SearchPathPInvoke.LegacyBehavior());
Assert.Equal(DllImportSearchPath.LegacyBehavior, observedSearchPath);

Assert.Throws<DllNotFoundException>(() => SearchPathPInvoke.NoFlags());
Assert.Null(observedSearchPath);
}
Comment thread
elinor-fung marked this conversation as resolved.
}

public class NativeLibraryPInvoke
Expand Down Expand Up @@ -219,4 +244,25 @@ public static int Sum(int a, int b)
[DllImport(nameof(NativeLibraryWithDependency))]
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory | DllImportSearchPath.System32)]
static extern int CallDependencySum(int a, int b);
}

public class SearchPathPInvoke
{
// Routed through the registered DllImportResolver, then fails the built-in search.
internal const string LibraryName = "DoesNotExist";

[DllImport(LibraryName)]
public static extern void NoFlags();

[DllImport(LibraryName)]
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
public static extern void AssemblyDirectory();

[DllImport(LibraryName)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
public static extern void System32();

[DllImport(LibraryName)]
[DefaultDllImportSearchPaths(DllImportSearchPath.LegacyBehavior)]
public static extern void LegacyBehavior();
}
Comment thread
elinor-fung marked this conversation as resolved.
Loading