forked from dotnet/coreclr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When loading satellite assemblies, we should probe next to the parent assembly and load into the same AssemblyLoadContext as the parent assembly. Disable fallback probing for satellite assemblies. Add AssemblyLoadContext.Resolving handler to probe for satellite assemblies next to parent Fixes #20979
- Loading branch information
Showing
5 changed files
with
173 additions
and
3 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
115 changes: 115 additions & 0 deletions
115
src/System.Private.CoreLib/shared/System/Runtime/Loader/AssemblyLoadContext.Unix.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,115 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
#nullable enable | ||
using System.Buffers; | ||
using System.IO; | ||
using System.Reflection; | ||
|
||
namespace System.Runtime.Loader | ||
{ | ||
public partial class AssemblyLoadContext | ||
{ | ||
// Find satellite path using case insensitive culture name | ||
internal static unsafe string? FindCaseInsensitiveSatellitePath(string parentDirectory, string cultureName, string assembly) | ||
{ | ||
int bufferSize = Interop.Sys.GetReadDirRBufferSize(); | ||
byte[]? dirBuffer = null; | ||
try | ||
{ | ||
dirBuffer = ArrayPool<byte>.Shared.Rent(bufferSize); | ||
|
||
fixed (byte* dirBufferPtr = dirBuffer) | ||
{ | ||
IntPtr dirHandle = Interop.Sys.OpenDir(parentDirectory); | ||
if (dirHandle != IntPtr.Zero) | ||
{ | ||
try | ||
{ | ||
Interop.Sys.DirectoryEntry dirent; | ||
while (Interop.Sys.ReadDirR(dirHandle, dirBufferPtr, bufferSize, out dirent) == 0) | ||
{ | ||
if (dirent.InodeType != Interop.Sys.NodeType.DT_DIR) | ||
continue; | ||
|
||
Span<char> nameBuffer = stackalloc char[Interop.Sys.DirectoryEntry.NameBufferSize]; | ||
ReadOnlySpan<char> entryName = dirent.GetName(nameBuffer); | ||
|
||
if (cultureName.Length != entryName.Length) | ||
continue; | ||
|
||
string entryNameString = entryName.ToString(); | ||
|
||
if (!cultureName.Equals(entryNameString, StringComparison.InvariantCultureIgnoreCase)) | ||
continue; | ||
|
||
string assemblyPath = $"{parentDirectory}/{entryNameString}/{assembly}.dll"; | ||
|
||
if (Internal.IO.File.InternalExists(assemblyPath)) | ||
return assemblyPath; | ||
} | ||
} | ||
finally | ||
{ | ||
if (dirHandle != IntPtr.Zero) | ||
Interop.Sys.CloseDir(dirHandle); | ||
} | ||
} | ||
} | ||
} | ||
finally | ||
{ | ||
if (dirBuffer != null) | ||
ArrayPool<byte>.Shared.Return(dirBuffer); | ||
} | ||
return null; | ||
} | ||
|
||
static private Assembly? ResolveSatelliteAssembly(AssemblyLoadContext alc, AssemblyName assemblyName) | ||
{ | ||
string? cultureName = assemblyName.CultureName; | ||
|
||
if (cultureName == null || cultureName.Length == 0) | ||
return null; | ||
|
||
if (assemblyName.Name == null) | ||
return null; | ||
|
||
AssemblyName parentAssemblyName = new AssemblyName(assemblyName.Name); | ||
|
||
Assembly? parentAssembly = alc.LoadFromAssemblyName(parentAssemblyName); | ||
|
||
if (parentAssembly == null) | ||
return null; | ||
|
||
AssemblyLoadContext? parentAlc = GetLoadContext(parentAssembly); | ||
|
||
if (parentAlc == null) | ||
return null; | ||
|
||
string parentDirectory = Path.GetDirectoryName(parentAssembly.Location)!; | ||
|
||
string assemblyPath = $"{parentDirectory}/{cultureName}/{assemblyName.Name}.dll"; | ||
if (Internal.IO.File.InternalExists(assemblyPath)) | ||
{ | ||
Assembly satelliteAssembly = parentAlc.LoadFromAssemblyPath(assemblyPath); | ||
|
||
if (satelliteAssembly != null) | ||
return satelliteAssembly; | ||
} | ||
else if (Path.IsCaseSensitive) | ||
{ | ||
string? caseInsensitiveAssemblyPath = FindCaseInsensitiveSatellitePath(parentDirectory, cultureName, assemblyName.Name); | ||
|
||
if (caseInsensitiveAssemblyPath != null) | ||
{ | ||
Assembly satelliteAssembly = parentAlc.LoadFromAssemblyPath(caseInsensitiveAssemblyPath); | ||
|
||
return satelliteAssembly; | ||
} | ||
} | ||
return null; | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/System.Private.CoreLib/shared/System/Runtime/Loader/AssemblyLoadContext.Windows.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,49 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
#nullable enable | ||
using System.IO; | ||
using System.Reflection; | ||
|
||
namespace System.Runtime.Loader | ||
{ | ||
public partial class AssemblyLoadContext | ||
{ | ||
static private Assembly? ResolveSatelliteAssembly(AssemblyLoadContext alc, AssemblyName assemblyName) | ||
{ | ||
string? cultureName = assemblyName.CultureName; | ||
|
||
if (cultureName == null || cultureName.Length == 0) | ||
return null; | ||
|
||
if (assemblyName.Name == null) | ||
return null; | ||
|
||
AssemblyName parentAssemblyName = new AssemblyName(assemblyName.Name); | ||
|
||
Assembly? parentAssembly = alc.LoadFromAssemblyName(parentAssemblyName); | ||
|
||
if (parentAssembly == null) | ||
return null; | ||
|
||
AssemblyLoadContext? parentAlc = GetLoadContext(parentAssembly); | ||
|
||
if (parentAlc == null) | ||
return null; | ||
|
||
string parentDirectory = Path.GetDirectoryName(parentAssembly.Location)!; | ||
|
||
string assemblyPath = Path.Combine(parentDirectory, dir, $"{assemblyName.Name}.dll"); | ||
if (Internal.IO.File.InternalExists(assemblyPath)) | ||
{ | ||
Assembly satelliteAssembly = parentAlc.LoadFromAssemblyPath(assemblyPath); | ||
|
||
if (satelliteAssembly != null) | ||
return satelliteAssembly; | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
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