-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merged PR 29231: [internal/release/7.0] Fix handling of load for msqu…
…ic and search paths for nativeaot and mono Fix loading of msquic in System.Net.Quic.. Fix handling of `DllImportSearchPath` values in `DllImport` and `NativeLibrary`. This affects mono and nativeaot.
- Loading branch information
1 parent
eaaed2a
commit 5a1baeb
Showing
16 changed files
with
237 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/tests/Interop/DllImportSearchPaths/DllImportSearchPathsTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.IO; | ||
using System.Reflection; | ||
using System.Runtime.InteropServices; | ||
using Xunit; | ||
|
||
public class DllImportSearchPathsTest | ||
{ | ||
private static string Subdirectory => Path.Combine(NativeLibraryToLoad.GetDirectory(), "subdirectory"); | ||
|
||
[Fact] | ||
public static void AssemblyDirectory_NotFound() | ||
{ | ||
// Library should not be found in the assembly directory | ||
Assert.Throws<DllNotFoundException>(() => NativeLibraryPInvoke.Sum(1, 2)); | ||
} | ||
|
||
public static bool CanLoadAssemblyInSubdirectory => | ||
!TestLibrary.Utilities.IsNativeAot && !TestLibrary.PlatformDetection.IsMonoLLVMFULLAOT; | ||
|
||
[ConditionalFact(nameof(CanLoadAssemblyInSubdirectory))] | ||
public static void AssemblyDirectory_Found() | ||
{ | ||
// Library should be found in the assembly directory | ||
var assembly = Assembly.LoadFile(Path.Combine(Subdirectory, $"{nameof(DllImportSearchPathsTest)}.dll")); | ||
var type = assembly.GetType(nameof(NativeLibraryPInvoke)); | ||
var method = type.GetMethod(nameof(NativeLibraryPInvoke.Sum)); | ||
|
||
int sum = (int)method.Invoke(null, new object[] { 1, 2 }); | ||
Assert.Equal(3, sum); | ||
} | ||
|
||
[Fact] | ||
[PlatformSpecific(TestPlatforms.Windows)] | ||
public static void AssemblyDirectory_Fallback_Found() | ||
{ | ||
string currentDirectory = Environment.CurrentDirectory; | ||
try | ||
{ | ||
Environment.CurrentDirectory = Subdirectory; | ||
|
||
// Library should not be found in the assembly directory, but should fall back to the default OS search which includes CWD on Windows | ||
int sum = NativeLibraryPInvoke.Sum(1, 2); | ||
Assert.Equal(3, sum); | ||
} | ||
finally | ||
{ | ||
Environment.CurrentDirectory = currentDirectory; | ||
} | ||
} | ||
} | ||
|
||
public class NativeLibraryPInvoke | ||
{ | ||
public static int Sum(int a, int b) | ||
{ | ||
return NativeSum(a, b); | ||
} | ||
|
||
[DllImport(NativeLibraryToLoad.Name)] | ||
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)] | ||
static extern int NativeSum(int arg1, int arg2); | ||
} |
24 changes: 24 additions & 0 deletions
24
src/tests/Interop/DllImportSearchPaths/DllImportSearchPathsTest.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="*.cs" /> | ||
<Compile Include="../NativeLibrary/NativeLibraryToLoad/NativeLibraryToLoad.cs" /> | ||
<CMakeProjectReference Include="../NativeLibrary/NativeLibraryToLoad/CMakeLists.txt" /> | ||
</ItemGroup> | ||
|
||
<Target Name="SetUpSubdirectory" AfterTargets="CopyNativeProjectBinaries"> | ||
<PropertyGroup> | ||
<NativeLibrarySubdirectory>$(OutDir)/subdirectory</NativeLibrarySubdirectory> | ||
<FileNameSuffix>-in-subdirectory</FileNameSuffix> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<_FilesToCopy Include="$(OutDir)/$(TargetName).dll" /> | ||
<_FilesToMove Include="$(OutDir)/libNativeLibrary.*" /> | ||
<_FilesToMove Include="$(OutDir)/NativeLibrary.*" /> | ||
</ItemGroup> | ||
<Copy SourceFiles="@(_FilesToCopy)" DestinationFiles="@(_FilesToCopy -> '$(NativeLibrarySubdirectory)/%(Filename)%(Extension)')" /> | ||
<Move SourceFiles="@(_FilesToMove)" DestinationFiles="@(_FilesToMove -> '$(NativeLibrarySubdirectory)/%(Filename)%(Extension)')" /> | ||
</Target> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.