Skip to content

Commit 8f313fd

Browse files
authored
Revert "Don't use incompatible LOAD_LIBRARY_SEARCH flags when using LOAD_WITH…" (#114592)
This reverts commit 15d227c.
1 parent b8f318d commit 8f313fd

File tree

9 files changed

+19
-105
lines changed

9 files changed

+19
-105
lines changed

src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/NativeLibrary.NativeAot.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,6 @@ internal static IntPtr LoadBySearch(Assembly callingAssembly, bool searchAssembl
6767
IntPtr ret;
6868

6969
int loadWithAlteredPathFlags = LoadWithAlteredSearchPathFlag;
70-
const int loadLibrarySearchFlags = (int)DllImportSearchPath.UseDllDirectoryForDependencies
71-
| (int)DllImportSearchPath.ApplicationDirectory
72-
| (int)DllImportSearchPath.UserDirectories
73-
| (int)DllImportSearchPath.System32
74-
| (int)DllImportSearchPath.SafeDirectories;
7570
bool libNameIsRelativePath = !Path.IsPathFullyQualified(libraryName);
7671

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

8681
if (!libNameIsRelativePath)
8782
{
88-
// LOAD_WITH_ALTERED_SEARCH_PATH is incompatible with LOAD_LIBRARY_SEARCH flags. Remove those flags if they are set.
89-
int flags = loadWithAlteredPathFlags | (dllImportSearchPathFlags & ~loadLibrarySearchFlags);
83+
int flags = loadWithAlteredPathFlags;
84+
if ((dllImportSearchPathFlags & (int)DllImportSearchPath.UseDllDirectoryForDependencies) != 0)
85+
{
86+
// LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR is the only flag affecting absolute path. Don't OR the flags
87+
// unconditionally as all absolute path P/Invokes could then lose LOAD_WITH_ALTERED_SEARCH_PATH.
88+
flags |= dllImportSearchPathFlags;
89+
}
90+
9091
ret = LoadLibraryHelper(currLibNameVariation, flags, ref errorTracker);
9192
if (ret != IntPtr.Zero)
9293
{
@@ -95,12 +96,9 @@ internal static IntPtr LoadBySearch(Assembly callingAssembly, bool searchAssembl
9596
}
9697
else if ((callingAssembly != null) && searchAssemblyDirectory)
9798
{
98-
// LOAD_WITH_ALTERED_SEARCH_PATH is incompatible with LOAD_LIBRARY_SEARCH flags. Remove those flags if they are set.
99-
int flags = loadWithAlteredPathFlags | (dllImportSearchPathFlags & ~loadLibrarySearchFlags);
100-
10199
// Try to load the module alongside the assembly where the PInvoke was declared.
102100
// For PInvokes where the DllImportSearchPath.AssemblyDirectory is specified, look next to the application.
103-
ret = LoadLibraryHelper(Path.Combine(AppContext.BaseDirectory, currLibNameVariation), flags, ref errorTracker);
101+
ret = LoadLibraryHelper(Path.Combine(AppContext.BaseDirectory, currLibNameVariation), loadWithAlteredPathFlags | dllImportSearchPathFlags, ref errorTracker);
104102
if (ret != IntPtr.Zero)
105103
{
106104
return ret;

src/coreclr/vm/nativelibrary.cpp

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,6 @@ namespace
476476
NATIVE_LIBRARY_HANDLE hmod = NULL;
477477

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

481480
SString::Iterator lastPathSeparatorIter = path.End();
482481
if (PEAssembly::FindLastPathSeparator(path, lastPathSeparatorIter))
@@ -657,14 +656,6 @@ namespace
657656

658657
AppDomain* pDomain = GetAppDomain();
659658
DWORD loadWithAlteredPathFlags = GetLoadWithAlteredSearchPathFlag();
660-
DWORD loadLibrarySearchFlags = 0;
661-
#ifdef TARGET_WINDOWS
662-
loadLibrarySearchFlags = LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR
663-
| LOAD_LIBRARY_SEARCH_APPLICATION_DIR
664-
| LOAD_LIBRARY_SEARCH_USER_DIRS
665-
| LOAD_LIBRARY_SEARCH_SYSTEM32
666-
| LOAD_LIBRARY_SEARCH_DEFAULT_DIRS;
667-
#endif
668659
bool libNameIsRelativePath = Path::IsRelative(wszLibName);
669660

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

699690
if (!libNameIsRelativePath)
700691
{
701-
// LOAD_WITH_ALTERED_SEARCH_PATH is incompatible with LOAD_LIBRARY_SEARCH flags. Remove those flags if they are set.
702-
DWORD flags = loadWithAlteredPathFlags | (dllImportSearchPathFlags & ~loadLibrarySearchFlags);
692+
DWORD flags = loadWithAlteredPathFlags;
693+
if ((dllImportSearchPathFlags & LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR) != 0)
694+
{
695+
// LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR is the only flag affecting absolute path. Don't OR the flags
696+
// unconditionally as all absolute path P/Invokes could then lose LOAD_WITH_ALTERED_SEARCH_PATH.
697+
flags |= dllImportSearchPathFlags;
698+
}
699+
703700
hmod = LocalLoadLibraryHelper(currLibNameVariation, flags, pErrorTracker);
704701
if (hmod != NULL)
705702
{
@@ -708,9 +705,7 @@ namespace
708705
}
709706
else if ((callingAssembly != nullptr) && searchAssemblyDirectory)
710707
{
711-
// LOAD_WITH_ALTERED_SEARCH_PATH is incompatible with LOAD_LIBRARY_SEARCH flags. Remove those flags if they are set.
712-
DWORD flags = loadWithAlteredPathFlags | (dllImportSearchPathFlags & ~loadLibrarySearchFlags);
713-
hmod = LoadFromPInvokeAssemblyDirectory(callingAssembly, currLibNameVariation, flags, pErrorTracker);
708+
hmod = LoadFromPInvokeAssemblyDirectory(callingAssembly, currLibNameVariation, loadWithAlteredPathFlags | dllImportSearchPathFlags, pErrorTracker);
714709
if (hmod != NULL)
715710
{
716711
return hmod;

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

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -275,22 +275,6 @@ convert_dllimport_flags (int flags)
275275
#endif
276276
}
277277

278-
static int
279-
add_load_with_altered_search_path_flags (int flags)
280-
{
281-
#ifdef HOST_WIN32
282-
// LOAD_WITH_ALTERED_SEARCH_PATH is incompatible with LOAD_LIBRARY_SEARCH flags. Remove those flags if they are set.
283-
int load_library_search_flags = LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR
284-
| LOAD_LIBRARY_SEARCH_APPLICATION_DIR
285-
| LOAD_LIBRARY_SEARCH_USER_DIRS
286-
| LOAD_LIBRARY_SEARCH_SYSTEM32
287-
| LOAD_LIBRARY_SEARCH_DEFAULT_DIRS;
288-
return LOAD_WITH_ALTERED_SEARCH_PATH | (flags & ~load_library_search_flags);
289-
#else
290-
return flags;
291-
#endif
292-
}
293-
294278
static MonoDl *
295279
netcore_probe_for_module_variations (const char *mdirname, const char *file_name, int raw_flags, MonoError *error)
296280
{
@@ -368,7 +352,7 @@ netcore_probe_for_module (MonoImage *image, const char *file_name, int flags, Mo
368352
error_init_reuse (error);
369353
char *mdirname = g_path_get_dirname (image->filename);
370354
if (mdirname)
371-
module = netcore_probe_for_module_variations (mdirname, file_name, add_load_with_altered_search_path_flags(lflags), error);
355+
module = netcore_probe_for_module_variations (mdirname, file_name, lflags, error);
372356
g_free (mdirname);
373357
}
374358

src/tests/Interop/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ add_subdirectory(MarshalAPI/FunctionPointer)
5757
add_subdirectory(NativeLibrary/NativeLibraryToLoad)
5858
add_subdirectory(DllImportAttribute/DllImportPath)
5959
add_subdirectory(DllImportAttribute/ExactSpelling)
60-
add_subdirectory(DllImportSearchPaths/NativeLibraryWithDependency)
6160
add_subdirectory(ICustomMarshaler/ConflictingNames)
6261
add_subdirectory(ICustomMarshaler/Primitives)
6362
add_subdirectory(LayoutClass)

src/tests/Interop/DllImportSearchPaths/DllImportSearchPathsTest.cs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,6 @@ public static void AssemblyDirectory_Fallback_Found()
6565
Environment.CurrentDirectory = currentDirectory;
6666
}
6767
}
68-
69-
[ConditionalFact(nameof(CanLoadAssemblyInSubdirectory))]
70-
[PlatformSpecific(TestPlatforms.Windows)]
71-
public static void AssemblyDirectory_SearchFlags_WithDependency_Found()
72-
{
73-
// Library and its dependency should be found in the assembly directory.
74-
var assembly = Assembly.LoadFile(Path.Combine(Subdirectory, $"{nameof(DllImportSearchPathsTest)}.dll"));
75-
var type = assembly.GetType(nameof(NativeLibraryWithDependency));
76-
var method = type.GetMethod(nameof(NativeLibraryWithDependency.Sum));
77-
78-
int sum = (int)method.Invoke(null, new object[] { 1, 2 });
79-
Assert.Equal(3, sum);
80-
Console.WriteLine("NativeLibraryWithDependency.Sum returned {0}", sum);
81-
}
8268
}
8369

8470
public class NativeLibraryPInvoke
@@ -107,18 +93,3 @@ public static int Sum(int a, int b)
10793
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory | DllImportSearchPath.System32)]
10894
static extern int NativeSum(int arg1, int arg2);
10995
}
110-
111-
public class NativeLibraryWithDependency
112-
{
113-
public static int Sum(int a, int b)
114-
{
115-
return CallDependencySum(a, b);
116-
}
117-
118-
// For LoadLibrary on Windows, search flags, like that represented by System32, are incompatible with
119-
// looking at a specific path (per AssemblyDirectory), so we specify both flags to validate that we do
120-
// not incorrectly use both when looking in the assembly directory.
121-
[DllImport(nameof(NativeLibraryWithDependency))]
122-
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory | DllImportSearchPath.System32)]
123-
static extern int CallDependencySum(int a, int b);
124-
}

src/tests/Interop/DllImportSearchPaths/DllImportSearchPathsTest.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
<Compile Include="*.cs" />
1010
<Compile Include="../NativeLibrary/NativeLibraryToLoad/NativeLibraryToLoad.cs" />
1111
<CMakeProjectReference Include="../NativeLibrary/NativeLibraryToLoad/CMakeLists.txt" />
12-
<CMakeProjectReference Include="NativeLibraryWithDependency/CMakeLists.txt" />
1312
</ItemGroup>
1413

1514
<PropertyGroup>
@@ -19,9 +18,7 @@
1918
<Target Name="SetUpSubdirectoryNative" AfterTargets="CopyNativeProjectBinaries">
2019
<ItemGroup>
2120
<NativeLibrariesToMove Include="$(OutDir)/libNativeLibrary.*" />
22-
<NativeLibrariesToMove Include="$(OutDir)/libNativeLibraryWithDependency.*" />
2321
<NativeLibrariesToMove Include="$(OutDir)/NativeLibrary.*" />
24-
<NativeLibrariesToMove Include="$(OutDir)/NativeLibraryWithDependency.*" />
2522
</ItemGroup>
2623
<Move SourceFiles="@(NativeLibrariesToMove)" DestinationFiles="@(NativeLibrariesToMove -> '$(LibrarySubdirectory)/%(Filename)%(Extension)')"/>
2724
</Target>

src/tests/Interop/DllImportSearchPaths/NativeLibraryWithDependency/CMakeLists.txt

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/tests/Interop/DllImportSearchPaths/NativeLibraryWithDependency/Dependency.cpp

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/tests/Interop/DllImportSearchPaths/NativeLibraryWithDependency/NativeLibraryWithDependency.cpp

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)