Skip to content

Commit a8ab59b

Browse files
authored
Merge pull request #84610 from dotnet-maestro-bot/merge/release/6.0-to-release/6.0-staging
[automated] Merge branch 'release/6.0' => 'release/6.0-staging'
2 parents f548a84 + 75ce69d commit a8ab59b

11 files changed

Lines changed: 195 additions & 16 deletions

File tree

eng/pipelines/runtime-official.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ stages:
140140
# - windows_arm
141141
# - windows_arm64
142142
jobParameters:
143-
buildArgs: -s mono+libs+host+packs+mono.mscordbi -c $(_BuildConfig)
143+
buildArgs: -s mono+libs+host+packs -c $(_BuildConfig)
144144
nameSuffix: AllSubsets_Mono
145145
isOfficialBuild: ${{ variables.isOfficialBuild }}
146146
extraStepsTemplate: /eng/pipelines/common/upload-intermediate-artifacts-step.yml

eng/pipelines/runtime-staging.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ jobs:
288288
jobParameters:
289289
testScope: innerloop
290290
nameSuffix: AllSubsets_Mono
291-
buildArgs: -s mono+libs+host+packs+libs.tests -c $(_BuildConfig) /p:ArchiveTests=true
291+
buildArgs: -s mono+mono.mscordbi+libs+host+packs+libs.tests -c $(_BuildConfig) /p:ArchiveTests=true
292292
timeoutInMinutes: 120
293293
condition: >-
294294
or(

src/libraries/System.Net.Quic/src/System/Net/Quic/Implementations/MsQuic/Internal/MsQuicApi.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ static MsQuicApi()
141141
return;
142142
}
143143

144-
if (!NativeLibrary.TryLoad(Interop.Libraries.MsQuic, typeof(MsQuicApi).Assembly, DllImportSearchPath.AssemblyDirectory, out IntPtr msQuicHandle))
144+
// Windows ships msquic in the assembly directory. Non-Windows relies an the package being installed on the system.
145+
DllImportSearchPath? searchPath = OperatingSystem.IsWindows() ? DllImportSearchPath.AssemblyDirectory : null;
146+
if (!NativeLibrary.TryLoad(Interop.Libraries.MsQuic, typeof(MsQuicApi).Assembly, searchPath, out IntPtr msQuicHandle))
145147
{
146148
// MsQuic library not loaded
147149
return;

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ mono_dllmap_lookup_list (MonoDllMap *dll_map, const char *dll, const char* func,
9292
if (!dll_map)
9393
goto exit;
9494

95-
/*
95+
/*
9696
* we use the first entry we find that matches, since entries from
9797
* the config file are prepended to the list and we document that the
9898
* later entries win.
@@ -254,7 +254,7 @@ mono_global_dllmap_cleanup (void)
254254
* This function is used to programatically add \c DllImport remapping in either
255255
* a specific assembly, or as a global remapping. This is done by remapping
256256
* references in a \c DllImport attribute from the \p dll library name into the \p tdll
257-
* name. If the \p dll name contains the prefix <code>i:</code>, the comparison of the
257+
* name. If the \p dll name contains the prefix <code>i:</code>, the comparison of the
258258
* library name is done without case sensitivity.
259259
*
260260
* If you pass \p func, this is the name of the \c EntryPoint in a \c DllImport if specified
@@ -518,21 +518,33 @@ netcore_probe_for_module (MonoImage *image, const char *file_name, int flags)
518518
// TODO: this algorithm doesn't quite match CoreCLR, so respecting DLLIMPORTSEARCHPATH_LEGACY_BEHAVIOR makes little sense
519519
// If the difference becomes a problem, overhaul this algorithm to match theirs exactly
520520

521-
// Try without any path additions
521+
#if defined(HOST_ANDROID)
522+
// On Android, try without any path additions first. It is sensitive to probing that will always miss
523+
// and lookup for some libraries is required to use a relative path
522524
module = netcore_probe_for_module_variations (NULL, file_name, lflags);
525+
#endif
523526

524527
// Check the NATIVE_DLL_SEARCH_DIRECTORIES
525528
for (int i = 0; i < pinvoke_search_directories_count && module == NULL; ++i)
526529
module = netcore_probe_for_module_variations (pinvoke_search_directories[i], file_name, lflags);
527530

528531
// Check the assembly directory if the search flag is set and the image exists
529-
if (flags & DLLIMPORTSEARCHPATH_ASSEMBLY_DIRECTORY && image != NULL && module == NULL) {
532+
if ((flags & DLLIMPORTSEARCHPATH_ASSEMBLY_DIRECTORY) != 0 && image != NULL &&
533+
module == NULL && (image->filename != NULL)) {
530534
char *mdirname = g_path_get_dirname (image->filename);
531535
if (mdirname)
532536
module = netcore_probe_for_module_variations (mdirname, file_name, lflags);
533537
g_free (mdirname);
534538
}
535539

540+
#if !defined(HOST_ANDROID)
541+
// Try without any path additions
542+
if (module == NULL)
543+
{
544+
module = netcore_probe_for_module_variations (NULL, file_name, lflags);
545+
}
546+
#endif
547+
536548
// 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
537549

538550
return module;
@@ -1048,7 +1060,7 @@ lookup_pinvoke_call_impl (MonoMethod *method, MonoLookupPInvokeStatus *status_ou
10481060
mono_custom_attrs_free (cinfo);
10491061
}
10501062
if (flags < 0)
1051-
flags = 0;
1063+
flags = DLLIMPORTSEARCHPATH_ASSEMBLY_DIRECTORY;
10521064
module = netcore_lookup_native_library (alc, image, new_scope, flags);
10531065

10541066
if (!module) {
@@ -1154,7 +1166,7 @@ pinvoke_probe_for_symbol (MonoDl *module, MonoMethodPInvoke *piinfo, const char
11541166

11551167
#if HOST_WIN32 && HOST_X86
11561168
/* Try the stdcall mangled name */
1157-
/*
1169+
/*
11581170
* gcc under windows creates mangled names without the underscore, but MS.NET
11591171
* doesn't support it, so we doesn't support it either.
11601172
*/
@@ -1394,7 +1406,7 @@ mono_loader_save_bundled_library (int fd, uint64_t offset, uint64_t size, const
13941406
char *file, *buffer, *err, *internal_path;
13951407
if (!bundle_save_library_initialized)
13961408
bundle_save_library_initialize ();
1397-
1409+
13981410
file = g_build_filename (bundled_dylibrary_directory, destfname, (const char*)NULL);
13991411
buffer = g_str_from_file_region (fd, offset, size);
14001412
g_file_set_contents (file, buffer, size, NULL);
@@ -1409,7 +1421,7 @@ mono_loader_save_bundled_library (int fd, uint64_t offset, uint64_t size, const
14091421
mono_loader_register_module (internal_path, lib);
14101422
g_free (internal_path);
14111423
bundle_library_paths = g_slist_append (bundle_library_paths, file);
1412-
1424+
14131425
g_free (buffer);
14141426
}
14151427

src/tests/Common/CoreCLRTestLibrary/Utilities.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ public static bool Verbose
6868
public static bool IsWindows7 => IsWindows && Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor == 1;
6969
public static bool IsWindowsNanoServer => (!IsWindowsIoTCore && GetInstallationType().Equals("Nano Server", StringComparison.OrdinalIgnoreCase));
7070

71+
private static string _variant = Environment.GetEnvironmentVariable("DOTNET_RUNTIME_VARIANT");
72+
public static bool IsMonoLLVMFULLAOT => _variant == "llvmfullaot";
73+
7174
// Windows 10 October 2018 Update
7275
public static bool IsWindows10Version1809OrGreater =>
7376
IsWindows && GetWindowsVersion() == 10 && GetWindowsMinorVersion() == 0 && GetWindowsBuildNumber() >= 17763;

src/tests/Common/testenvironment.proj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@
201201
<!-- Mono interpreter -->
202202
<_TestEnvFileLine Condition="'$(RuntimeVariant)' == 'monointerpreter'" Include="set MONO_ENV_OPTIONS=--interpreter" />
203203

204+
<_TestEnvFileLine Condition="'$(RuntimeVariant)' != ''" Include="set DOTNET_RUNTIME_VARIANT=$(RuntimeVariant)" />
205+
204206
<!-- CLR interpreter -->
205207
<_TestEnvFileLine Condition="'$(Scenario)' == 'clrinterpreter'" Include="set COMPlus_Interpret=%2A" /> <!-- %2A is asterisk / wildcard -->
206208
<_TestEnvFileLine Condition="'$(Scenario)' == 'clrinterpreter'" Include="set COMPlus_InterpreterHWIntrinsicsIsSupportedFalse=1" />
@@ -216,6 +218,8 @@
216218
<!-- Mono interpreter -->
217219
<_TestEnvFileLine Condition="'$(RuntimeVariant)' == 'monointerpreter'" Include="export MONO_ENV_OPTIONS=--interpreter" />
218220

221+
<_TestEnvFileLine Condition="'$(RuntimeVariant)' != ''" Include="export DOTNET_RUNTIME_VARIANT=$(RuntimeVariant)" />
222+
219223
<!-- Hack: Use Mono LLVM JIT when JIT-compiling the non-AOT-compiled parts of the runtime tests -->
220224
<_TestEnvFileLine Condition="'$(RuntimeVariant)' == 'llvmaot'" Include="export MONO_ENV_OPTIONS=--llvm" />
221225

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.IO;
6+
using System.Reflection;
7+
using System.Runtime.InteropServices;
8+
using Xunit;
9+
10+
public class DllImportSearchPathsTest
11+
{
12+
private static string Subdirectory => Path.Combine(NativeLibraryToLoad.GetDirectory(), "subdirectory");
13+
14+
static int Main(string[] args)
15+
{
16+
try
17+
{
18+
AssemblyDirectory_NotFound();
19+
if (!TestLibrary.Utilities.IsMonoLLVMFULLAOT)
20+
AssemblyDirectory_Found();
21+
22+
if (OperatingSystem.IsWindows())
23+
AssemblyDirectory_Fallback_Found();
24+
}
25+
catch (Exception e)
26+
{
27+
Console.WriteLine($"Test Failure: {e}");
28+
return 101;
29+
}
30+
31+
return 100;
32+
}
33+
34+
public static void AssemblyDirectory_NotFound()
35+
{
36+
// Library should not be found in the assembly directory
37+
Assert.Throws<DllNotFoundException>(() => NativeLibraryPInvoke.Sum(1, 2));
38+
}
39+
40+
public static void AssemblyDirectory_Found()
41+
{
42+
// Library should be found in the assembly directory
43+
var assembly = Assembly.LoadFile(Path.Combine(Subdirectory, $"{nameof(DllImportSearchPathsTest)}.dll"));
44+
var type = assembly.GetType(nameof(NativeLibraryPInvoke));
45+
var method = type.GetMethod(nameof(NativeLibraryPInvoke.Sum));
46+
47+
int sum = (int)method.Invoke(null, new object[] { 1, 2 });
48+
Assert.Equal(3, sum);
49+
}
50+
51+
public static void AssemblyDirectory_Fallback_Found()
52+
{
53+
string currentDirectory = Environment.CurrentDirectory;
54+
try
55+
{
56+
Environment.CurrentDirectory = Subdirectory;
57+
58+
// Library should not be found in the assembly directory, but should fall back to the default OS search which includes CWD on Windows
59+
int sum = NativeLibraryPInvoke.Sum(1, 2);
60+
Assert.Equal(3, sum);
61+
}
62+
finally
63+
{
64+
Environment.CurrentDirectory = currentDirectory;
65+
}
66+
}
67+
}
68+
69+
public class NativeLibraryPInvoke
70+
{
71+
public static int Sum(int a, int b)
72+
{
73+
return NativeSum(a, b);
74+
}
75+
76+
[DllImport(NativeLibraryToLoad.Name)]
77+
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
78+
static extern int NativeSum(int arg1, int arg2);
79+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="*.cs" />
8+
<Compile Include="../NativeLibrary/NativeLibraryToLoad/NativeLibraryToLoad.cs" />
9+
<ProjectReference Include="../NativeLibrary/NativeLibraryToLoad/CMakeLists.txt" />
10+
</ItemGroup>
11+
12+
<Target Name="SetUpSubdirectory" AfterTargets="CopyNativeProjectBinaries">
13+
<PropertyGroup>
14+
<NativeLibrarySubdirectory>$(OutDir)/subdirectory</NativeLibrarySubdirectory>
15+
<FileNameSuffix>-in-subdirectory</FileNameSuffix>
16+
</PropertyGroup>
17+
<ItemGroup>
18+
<_FilesToCopy Include="$(OutDir)/$(TargetName).dll" />
19+
<_FilesToMove Include="$(OutDir)/libNativeLibrary.*" />
20+
<_FilesToMove Include="$(OutDir)/NativeLibrary.*" />
21+
</ItemGroup>
22+
<Copy SourceFiles="@(_FilesToCopy)" DestinationFiles="@(_FilesToCopy -> '$(NativeLibrarySubdirectory)/%(Filename)%(Extension)')" />
23+
<Move SourceFiles="@(_FilesToMove)" DestinationFiles="@(_FilesToMove -> '$(NativeLibrarySubdirectory)/%(Filename)%(Extension)')" />
24+
</Target>
25+
</Project>

src/tests/Interop/NativeLibrary/API/NativeLibraryTests.cs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,22 @@ public static int Main()
110110
if (TestLibrary.Utilities.IsWindows &&
111111
File.Exists(Path.Combine(Environment.SystemDirectory, libName)))
112112
{
113-
// Calls on a valid library from System32 directory
113+
// Library should be found in the system directory
114114
success &= EXPECT(LoadLibraryAdvanced(libName, assembly, DllImportSearchPath.System32));
115115
success &= EXPECT(TryLoadLibraryAdvanced(libName, assembly, DllImportSearchPath.System32));
116116

117-
// Calls on a valid library from application directory
117+
// Library should not be found in the assembly directory and should be found in the system directory
118+
success &= EXPECT(LoadLibraryAdvanced(libName, assembly, DllImportSearchPath.AssemblyDirectory | DllImportSearchPath.System32));
119+
success &= EXPECT(TryLoadLibraryAdvanced(libName, assembly, DllImportSearchPath.AssemblyDirectory | DllImportSearchPath.System32));
120+
121+
// Library should not be found in the assembly directory, but should fall back to the default OS search which includes CWD on Windows
122+
success &= EXPECT(LoadLibraryAdvanced(libName, assembly, DllImportSearchPath.AssemblyDirectory));
123+
success &= EXPECT(TryLoadLibraryAdvanced(libName, assembly, DllImportSearchPath.AssemblyDirectory));
124+
125+
// Library should not be found in application directory
118126
success &= EXPECT(LoadLibraryAdvanced(libName, assembly, DllImportSearchPath.ApplicationDirectory), TestResult.DllNotFound);
119127
success &= EXPECT(TryLoadLibraryAdvanced(libName, assembly, DllImportSearchPath.ApplicationDirectory), TestResult.ReturnFailure);
128+
120129
}
121130

122131
// Calls with null libName input
@@ -134,6 +143,35 @@ public static int Main()
134143
success &= EXPECT(LoadLibraryAdvanced(libName, assembly, DllImportSearchPath.AssemblyDirectory), TestResult.DllNotFound);
135144
success &= EXPECT(TryLoadLibraryAdvanced(libName, assembly, DllImportSearchPath.AssemblyDirectory), TestResult.ReturnFailure);
136145

146+
string suffix = "-in-subdirectory";
147+
libName = $"{NativeLibraryToLoad.Name}{suffix}";
148+
string subdirectory = Path.Combine(testBinDir, "subdirectory");
149+
150+
if (!TestLibrary.Utilities.IsMonoLLVMFULLAOT)
151+
{
152+
// Library should be found in the assembly directory
153+
Assembly assemblyInSubdirectory = Assembly.LoadFile(Path.Combine(subdirectory, $"{Path.GetFileNameWithoutExtension(assembly.Location)}{suffix}.dll"));
154+
EXPECT(LoadLibraryAdvanced(libName, assemblyInSubdirectory, DllImportSearchPath.AssemblyDirectory));
155+
EXPECT(TryLoadLibraryAdvanced(libName, assemblyInSubdirectory, DllImportSearchPath.AssemblyDirectory));
156+
}
157+
158+
if (OperatingSystem.IsWindows())
159+
{
160+
string currentDirectory = Environment.CurrentDirectory;
161+
try
162+
{
163+
Environment.CurrentDirectory = subdirectory;
164+
165+
// Library should not be found in the assembly directory, but should fall back to the default OS search which includes CWD on Windows
166+
EXPECT(LoadLibraryAdvanced(libName, assembly, DllImportSearchPath.AssemblyDirectory));
167+
EXPECT(TryLoadLibraryAdvanced(libName, assembly, DllImportSearchPath.AssemblyDirectory));
168+
}
169+
finally
170+
{
171+
Environment.CurrentDirectory = currentDirectory;
172+
}
173+
}
174+
137175
// -----------------------------------------------
138176
// FreeLibrary Tests
139177
// -----------------------------------------------

src/tests/Interop/NativeLibrary/API/NativeLibraryTests.csproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,17 @@
1616
<ProjectReference Include="$(TestSourceDir)Common/CoreCLRTestLibrary/CoreCLRTestLibrary.csproj" />
1717
<ProjectReference Include="../NativeLibraryToLoad/CMakeLists.txt" />
1818
</ItemGroup>
19+
20+
<Target Name="SetUpSubdirectory" AfterTargets="CopyNativeProjectBinaries">
21+
<PropertyGroup>
22+
<NativeLibrarySubdirectory>$(OutDir)/subdirectory</NativeLibrarySubdirectory>
23+
<FileNameSuffix>-in-subdirectory</FileNameSuffix>
24+
</PropertyGroup>
25+
<ItemGroup>
26+
<AssembliesToCopy Include="$(OutDir)/libNativeLibrary.*" />
27+
<AssembliesToCopy Include="$(OutDir)/NativeLibrary.*" />
28+
<AssembliesToCopy Include="$(OutDir)/$(TargetName).dll" />
29+
</ItemGroup>
30+
<Copy SourceFiles="@(AssembliesToCopy)" DestinationFiles="@(AssembliesToCopy -> '$(NativeLibrarySubdirectory)/%(Filename)$(FileNameSuffix)%(Extension)')" />
31+
</Target>
1932
</Project>

0 commit comments

Comments
 (0)