Skip to content

Revert "Don't use incompatible LOAD_LIBRARY_SEARCH flags when using LOAD_WITH_ALTERED_SEARCH_PATH" #114592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@ internal static IntPtr LoadBySearch(Assembly callingAssembly, bool searchAssembl
IntPtr ret;

int loadWithAlteredPathFlags = LoadWithAlteredSearchPathFlag;
const int loadLibrarySearchFlags = (int)DllImportSearchPath.UseDllDirectoryForDependencies
| (int)DllImportSearchPath.ApplicationDirectory
| (int)DllImportSearchPath.UserDirectories
| (int)DllImportSearchPath.System32
| (int)DllImportSearchPath.SafeDirectories;
bool libNameIsRelativePath = !Path.IsPathFullyQualified(libraryName);

// P/Invokes are often declared with variations on the actual library name.
Expand All @@ -85,8 +80,14 @@ internal static IntPtr LoadBySearch(Assembly callingAssembly, bool searchAssembl

if (!libNameIsRelativePath)
{
// LOAD_WITH_ALTERED_SEARCH_PATH is incompatible with LOAD_LIBRARY_SEARCH flags. Remove those flags if they are set.
int flags = loadWithAlteredPathFlags | (dllImportSearchPathFlags & ~loadLibrarySearchFlags);
int flags = loadWithAlteredPathFlags;
if ((dllImportSearchPathFlags & (int)DllImportSearchPath.UseDllDirectoryForDependencies) != 0)
{
// LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR is the only flag affecting absolute path. Don't OR the flags
// unconditionally as all absolute path P/Invokes could then lose LOAD_WITH_ALTERED_SEARCH_PATH.
flags |= dllImportSearchPathFlags;
}

ret = LoadLibraryHelper(currLibNameVariation, flags, ref errorTracker);
if (ret != IntPtr.Zero)
{
Expand All @@ -95,12 +96,9 @@ internal static IntPtr LoadBySearch(Assembly callingAssembly, bool searchAssembl
}
else if ((callingAssembly != null) && searchAssemblyDirectory)
{
// LOAD_WITH_ALTERED_SEARCH_PATH is incompatible with LOAD_LIBRARY_SEARCH flags. Remove those flags if they are set.
int flags = loadWithAlteredPathFlags | (dllImportSearchPathFlags & ~loadLibrarySearchFlags);

// Try to load the module alongside the assembly where the PInvoke was declared.
// For PInvokes where the DllImportSearchPath.AssemblyDirectory is specified, look next to the application.
ret = LoadLibraryHelper(Path.Combine(AppContext.BaseDirectory, currLibNameVariation), flags, ref errorTracker);
ret = LoadLibraryHelper(Path.Combine(AppContext.BaseDirectory, currLibNameVariation), loadWithAlteredPathFlags | dllImportSearchPathFlags, ref errorTracker);
if (ret != IntPtr.Zero)
{
return ret;
Expand Down
23 changes: 9 additions & 14 deletions src/coreclr/vm/nativelibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,6 @@ namespace
NATIVE_LIBRARY_HANDLE hmod = NULL;

SString path{ pAssembly->GetPEAssembly()->GetPath() };
_ASSERTE(!Path::IsRelative(path));

SString::Iterator lastPathSeparatorIter = path.End();
if (PEAssembly::FindLastPathSeparator(path, lastPathSeparatorIter))
Expand Down Expand Up @@ -657,14 +656,6 @@ namespace

AppDomain* pDomain = GetAppDomain();
DWORD loadWithAlteredPathFlags = GetLoadWithAlteredSearchPathFlag();
DWORD loadLibrarySearchFlags = 0;
#ifdef TARGET_WINDOWS
loadLibrarySearchFlags = LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR
| LOAD_LIBRARY_SEARCH_APPLICATION_DIR
| LOAD_LIBRARY_SEARCH_USER_DIRS
| LOAD_LIBRARY_SEARCH_SYSTEM32
| LOAD_LIBRARY_SEARCH_DEFAULT_DIRS;
#endif
bool libNameIsRelativePath = Path::IsRelative(wszLibName);

// P/Invokes are often declared with variations on the actual library name.
Expand Down Expand Up @@ -698,8 +689,14 @@ namespace

if (!libNameIsRelativePath)
{
// LOAD_WITH_ALTERED_SEARCH_PATH is incompatible with LOAD_LIBRARY_SEARCH flags. Remove those flags if they are set.
DWORD flags = loadWithAlteredPathFlags | (dllImportSearchPathFlags & ~loadLibrarySearchFlags);
DWORD flags = loadWithAlteredPathFlags;
if ((dllImportSearchPathFlags & LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR) != 0)
{
// LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR is the only flag affecting absolute path. Don't OR the flags
// unconditionally as all absolute path P/Invokes could then lose LOAD_WITH_ALTERED_SEARCH_PATH.
flags |= dllImportSearchPathFlags;
}

hmod = LocalLoadLibraryHelper(currLibNameVariation, flags, pErrorTracker);
if (hmod != NULL)
{
Expand All @@ -708,9 +705,7 @@ namespace
}
else if ((callingAssembly != nullptr) && searchAssemblyDirectory)
{
// LOAD_WITH_ALTERED_SEARCH_PATH is incompatible with LOAD_LIBRARY_SEARCH flags. Remove those flags if they are set.
DWORD flags = loadWithAlteredPathFlags | (dllImportSearchPathFlags & ~loadLibrarySearchFlags);
hmod = LoadFromPInvokeAssemblyDirectory(callingAssembly, currLibNameVariation, flags, pErrorTracker);
hmod = LoadFromPInvokeAssemblyDirectory(callingAssembly, currLibNameVariation, loadWithAlteredPathFlags | dllImportSearchPathFlags, pErrorTracker);
if (hmod != NULL)
{
return hmod;
Expand Down
18 changes: 1 addition & 17 deletions src/mono/mono/metadata/native-library.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,22 +275,6 @@ convert_dllimport_flags (int flags)
#endif
}

static int
add_load_with_altered_search_path_flags (int flags)
{
#ifdef HOST_WIN32
// LOAD_WITH_ALTERED_SEARCH_PATH is incompatible with LOAD_LIBRARY_SEARCH flags. Remove those flags if they are set.
int load_library_search_flags = LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR
| LOAD_LIBRARY_SEARCH_APPLICATION_DIR
| LOAD_LIBRARY_SEARCH_USER_DIRS
| LOAD_LIBRARY_SEARCH_SYSTEM32
| LOAD_LIBRARY_SEARCH_DEFAULT_DIRS;
return LOAD_WITH_ALTERED_SEARCH_PATH | (flags & ~load_library_search_flags);
#else
return flags;
#endif
}

static MonoDl *
netcore_probe_for_module_variations (const char *mdirname, const char *file_name, int raw_flags, MonoError *error)
{
Expand Down Expand Up @@ -368,7 +352,7 @@ netcore_probe_for_module (MonoImage *image, const char *file_name, int flags, Mo
error_init_reuse (error);
char *mdirname = g_path_get_dirname (image->filename);
if (mdirname)
module = netcore_probe_for_module_variations (mdirname, file_name, add_load_with_altered_search_path_flags(lflags), error);
module = netcore_probe_for_module_variations (mdirname, file_name, lflags, error);
g_free (mdirname);
}

Expand Down
1 change: 0 additions & 1 deletion src/tests/Interop/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ add_subdirectory(MarshalAPI/FunctionPointer)
add_subdirectory(NativeLibrary/NativeLibraryToLoad)
add_subdirectory(DllImportAttribute/DllImportPath)
add_subdirectory(DllImportAttribute/ExactSpelling)
add_subdirectory(DllImportSearchPaths/NativeLibraryWithDependency)
add_subdirectory(ICustomMarshaler/ConflictingNames)
add_subdirectory(ICustomMarshaler/Primitives)
add_subdirectory(LayoutClass)
Expand Down
29 changes: 0 additions & 29 deletions src/tests/Interop/DllImportSearchPaths/DllImportSearchPathsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,6 @@ public static void AssemblyDirectory_Fallback_Found()
Environment.CurrentDirectory = currentDirectory;
}
}

[ConditionalFact(nameof(CanLoadAssemblyInSubdirectory))]
[PlatformSpecific(TestPlatforms.Windows)]
public static void AssemblyDirectory_SearchFlags_WithDependency_Found()
{
// Library and its dependency should be found in the assembly directory.
var assembly = Assembly.LoadFile(Path.Combine(Subdirectory, $"{nameof(DllImportSearchPathsTest)}.dll"));
var type = assembly.GetType(nameof(NativeLibraryWithDependency));
var method = type.GetMethod(nameof(NativeLibraryWithDependency.Sum));

int sum = (int)method.Invoke(null, new object[] { 1, 2 });
Assert.Equal(3, sum);
Console.WriteLine("NativeLibraryWithDependency.Sum returned {0}", sum);
}
}

public class NativeLibraryPInvoke
Expand Down Expand Up @@ -107,18 +93,3 @@ public static int Sum(int a, int b)
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory | DllImportSearchPath.System32)]
static extern int NativeSum(int arg1, int arg2);
}

public class NativeLibraryWithDependency
{
public static int Sum(int a, int b)
{
return CallDependencySum(a, b);
}

// For LoadLibrary on Windows, search flags, like that represented by System32, are incompatible with
// looking at a specific path (per AssemblyDirectory), so we specify both flags to validate that we do
// not incorrectly use both when looking in the assembly directory.
[DllImport(nameof(NativeLibraryWithDependency))]
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory | DllImportSearchPath.System32)]
static extern int CallDependencySum(int a, int b);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<Compile Include="*.cs" />
<Compile Include="../NativeLibrary/NativeLibraryToLoad/NativeLibraryToLoad.cs" />
<CMakeProjectReference Include="../NativeLibrary/NativeLibraryToLoad/CMakeLists.txt" />
<CMakeProjectReference Include="NativeLibraryWithDependency/CMakeLists.txt" />
</ItemGroup>

<PropertyGroup>
Expand All @@ -19,9 +18,7 @@
<Target Name="SetUpSubdirectoryNative" AfterTargets="CopyNativeProjectBinaries">
<ItemGroup>
<NativeLibrariesToMove Include="$(OutDir)/libNativeLibrary.*" />
<NativeLibrariesToMove Include="$(OutDir)/libNativeLibraryWithDependency.*" />
<NativeLibrariesToMove Include="$(OutDir)/NativeLibrary.*" />
<NativeLibrariesToMove Include="$(OutDir)/NativeLibraryWithDependency.*" />
</ItemGroup>
<Move SourceFiles="@(NativeLibrariesToMove)" DestinationFiles="@(NativeLibrariesToMove -> '$(LibrarySubdirectory)/%(Filename)%(Extension)')"/>
</Target>
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Loading