Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit cdc06e2

Browse files
committed
Call ResolveSatelliteAssembly from native
Only call ResolveSatelliteAssembly from native when resolving a satellite assembly
1 parent c7f8827 commit cdc06e2

File tree

7 files changed

+51
-15
lines changed

7 files changed

+51
-15
lines changed

src/System.Private.CoreLib/shared/System/Runtime/Loader/AssemblyLoadContext.Unix.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public partial class AssemblyLoadContext
6666
return null;
6767
}
6868

69-
static private Assembly? ResolveSatelliteAssembly(AssemblyLoadContext alc, AssemblyName assemblyName)
69+
private Assembly? ResolveSatelliteAssembly(AssemblyName assemblyName)
7070
{
7171
string? cultureName = assemblyName.CultureName;
7272

@@ -78,7 +78,7 @@ public partial class AssemblyLoadContext
7878

7979
AssemblyName parentAssemblyName = new AssemblyName(assemblyName.Name);
8080

81-
Assembly? parentAssembly = alc.LoadFromAssemblyName(parentAssemblyName);
81+
Assembly? parentAssembly = LoadFromAssemblyName(parentAssemblyName);
8282

8383
if (parentAssembly == null)
8484
return null;

src/System.Private.CoreLib/shared/System/Runtime/Loader/AssemblyLoadContext.Windows.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace System.Runtime.Loader
1010
{
1111
public partial class AssemblyLoadContext
1212
{
13-
static private Assembly? ResolveSatelliteAssembly(AssemblyLoadContext alc, AssemblyName assemblyName)
13+
private Assembly? ResolveSatelliteAssembly(AssemblyName assemblyName)
1414
{
1515
string? cultureName = assemblyName.CultureName;
1616

@@ -22,7 +22,7 @@ public partial class AssemblyLoadContext
2222

2323
AssemblyName parentAssemblyName = new AssemblyName(assemblyName.Name);
2424

25-
Assembly? parentAssembly = alc.LoadFromAssemblyName(parentAssemblyName);
25+
Assembly? parentAssembly = LoadFromAssemblyName(parentAssemblyName);
2626

2727
if (parentAssembly == null)
2828
return null;

src/System.Private.CoreLib/shared/System/Runtime/Loader/AssemblyLoadContext.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,6 @@ private protected AssemblyLoadContext(bool representsTPALoadContext, bool isColl
100100
_id = s_nextId++;
101101
s_allContexts.Add(_id, new WeakReference<AssemblyLoadContext>(this, true));
102102
}
103-
104-
Resolving += ResolveSatelliteAssembly;
105103
}
106104

107105
~AssemblyLoadContext()

src/System.Private.CoreLib/src/System/Runtime/Loader/AssemblyLoadContext.CoreCLR.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,24 @@ internal Assembly LoadFromInMemoryModule(IntPtr moduleHandle)
9393

9494
// This method is invoked by the VM to resolve an assembly reference using the Resolving event
9595
// after trying assembly resolution via Load override and TPA load context without success.
96-
private static Assembly ResolveUsingResolvingEvent(IntPtr gchManagedAssemblyLoadContext, AssemblyName assemblyName)
96+
private static Assembly? ResolveUsingResolvingEvent(IntPtr gchManagedAssemblyLoadContext, AssemblyName assemblyName)
9797
{
9898
AssemblyLoadContext context = (AssemblyLoadContext)(GCHandle.FromIntPtr(gchManagedAssemblyLoadContext).Target)!;
9999

100100
// Invoke the AssemblyResolve event callbacks if wired up
101101
return context.ResolveUsingEvent(assemblyName);
102102
}
103103

104+
// This method is invoked by the VM to resolve an assembly reference using the Resolving event
105+
// after trying assembly resolution via Load override and TPA load context without success.
106+
private static Assembly? ResolveSatelliteAssembly(IntPtr gchManagedAssemblyLoadContext, AssemblyName assemblyName)
107+
{
108+
AssemblyLoadContext context = (AssemblyLoadContext)(GCHandle.FromIntPtr(gchManagedAssemblyLoadContext).Target)!;
109+
110+
// Invoke the ResolveSatelliteAssembly method
111+
return context.ResolveSatelliteAssembly(assemblyName);
112+
}
113+
104114
private Assembly? GetFirstResolvedAssembly(AssemblyName assemblyName)
105115
{
106116
Assembly? resolvedAssembly = null;

src/binder/clrprivbinderassemblyloadcontext.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ HRESULT CLRPrivBinderAssemblyLoadContext::BindAssemblyByName(IAssemblyName *
6565
//
6666
// 1) Lookup the assembly within the LoadContext itself. If assembly is found, use it.
6767
// 2) Invoke the LoadContext's Load method implementation. If assembly is found, use it.
68-
// 3) Lookup the assembly within TPABinder. If assembly is found, use it.
69-
// 4) Invoke the LoadContext's Resolving event. If assembly is found, use it.
70-
// 5) Raise exception.
68+
// 3) Lookup the assembly within TPABinder (except for ssatellite requests). If assembly is found, use it.
69+
// 4) Invoke the LoadContext's ResolveSatelliteAssembly method (for ssatellite requests). If assembly is found, use it.
70+
// 5) Invoke the LoadContext's Resolving event. If assembly is found, use it.
71+
// 6) Raise exception.
7172
//
7273
// This approach enables a LoadContext to override assemblies that have been loaded in TPA context by loading
7374
// a different (or even the same!) version.

src/vm/appdomain.cpp

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6887,6 +6887,8 @@ HRESULT RuntimeInvokeHostAssemblyResolver(INT_PTR pManagedAssemblyLoadContextToB
68876887
// Initialize the AssemblyName object from the AssemblySpec
68886888
spec.AssemblyNameInit(&_gcRefs.oRefAssemblyName, NULL);
68896889

6890+
bool isSatelliteAssemblyRequest = (_gcRefs.oRefAssemblyName->GetCultureInfo() != NULL);
6891+
68906892
if (!fInvokedForTPABinder)
68916893
{
68926894
// Step 2 (of CLRPrivBinderAssemblyLoadContext::BindUsingAssemblyName) - Invoke Load method
@@ -6910,8 +6912,6 @@ HRESULT RuntimeInvokeHostAssemblyResolver(INT_PTR pManagedAssemblyLoadContextToB
69106912
fResolvedAssembly = true;
69116913
}
69126914

6913-
bool isSatelliteAssemblyRequest = (_gcRefs.oRefAssemblyName->GetCultureInfo() != NULL);
6914-
69156915
// Step 3 (of CLRPrivBinderAssemblyLoadContext::BindUsingAssemblyName)
69166916
if (!fResolvedAssembly && !isSatelliteAssemblyRequest)
69176917
{
@@ -6931,11 +6931,37 @@ HRESULT RuntimeInvokeHostAssemblyResolver(INT_PTR pManagedAssemblyLoadContextToB
69316931
}
69326932
}
69336933

6934-
if (!fResolvedAssembly)
6934+
if (!fResolvedAssembly && isSatelliteAssemblyRequest)
69356935
{
69366936
// Step 4 (of CLRPrivBinderAssemblyLoadContext::BindUsingAssemblyName)
69376937
//
69386938
// If we couldnt resolve the assembly using TPA LoadContext as well, then
6939+
// attempt to resolve it using the ResolveSatelliteAssembly method.
6940+
// Finally, setup arguments for invocation
6941+
BinderMethodID idHAR_ResolveUsingEvent = METHOD__ASSEMBLYLOADCONTEXT__RESOLVESATELLITEASSEMBLY;
6942+
MethodDescCallSite methLoadAssembly(idHAR_ResolveUsingEvent);
6943+
6944+
// Setup the arguments for the call
6945+
ARG_SLOT args[2] =
6946+
{
6947+
PtrToArgSlot(pManagedAssemblyLoadContextToBindWithin), // IntPtr for managed assembly load context instance
6948+
ObjToArgSlot(_gcRefs.oRefAssemblyName), // AssemblyName instance
6949+
};
6950+
6951+
// Make the call
6952+
_gcRefs.oRefLoadedAssembly = (ASSEMBLYREF) methLoadAssembly.Call_RetOBJECTREF(args);
6953+
if (_gcRefs.oRefLoadedAssembly != NULL)
6954+
{
6955+
// Set the flag indicating we found the assembly
6956+
fResolvedAssembly = true;
6957+
}
6958+
}
6959+
6960+
if (!fResolvedAssembly)
6961+
{
6962+
// Step 5 (of CLRPrivBinderAssemblyLoadContext::BindUsingAssemblyName)
6963+
//
6964+
// If we couldnt resolve the assembly using TPA LoadContext as well, then
69396965
// attempt to resolve it using the Resolving event.
69406966
// Finally, setup arguments for invocation
69416967
BinderMethodID idHAR_ResolveUsingEvent = METHOD__ASSEMBLYLOADCONTEXT__RESOLVEUSINGEVENT;

src/vm/mscorlib.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -899,9 +899,10 @@ DEFINE_FIELD_U(_state, AssemblyLoadContextBaseObject, _stat
899899
DEFINE_FIELD_U(_isCollectible, AssemblyLoadContextBaseObject, _isCollectible)
900900
DEFINE_CLASS(ASSEMBLYLOADCONTEXT, Loader, AssemblyLoadContext)
901901
DEFINE_METHOD(ASSEMBLYLOADCONTEXT, RESOLVE, Resolve, SM_IntPtr_AssemblyName_RetAssemblyBase)
902-
DEFINE_METHOD(ASSEMBLYLOADCONTEXT, RESOLVEUNMANAGEDDLL, ResolveUnmanagedDll, SM_Str_IntPtr_RetIntPtr)
902+
DEFINE_METHOD(ASSEMBLYLOADCONTEXT, RESOLVEUNMANAGEDDLL, ResolveUnmanagedDll, SM_Str_IntPtr_RetIntPtr)
903903
DEFINE_METHOD(ASSEMBLYLOADCONTEXT, RESOLVEUNMANAGEDDLLUSINGEVENT, ResolveUnmanagedDllUsingEvent, SM_Str_AssemblyBase_IntPtr_RetIntPtr)
904-
DEFINE_METHOD(ASSEMBLYLOADCONTEXT, RESOLVEUSINGEVENT, ResolveUsingResolvingEvent, SM_IntPtr_AssemblyName_RetAssemblyBase)
904+
DEFINE_METHOD(ASSEMBLYLOADCONTEXT, RESOLVEUSINGEVENT, ResolveUsingResolvingEvent, SM_IntPtr_AssemblyName_RetAssemblyBase)
905+
DEFINE_METHOD(ASSEMBLYLOADCONTEXT, RESOLVESATELLITEASSEMBLY, ResolveSatelliteAssembly, SM_IntPtr_AssemblyName_RetAssemblyBase)
905906
DEFINE_FIELD(ASSEMBLYLOADCONTEXT, ASSEMBLY_LOAD, AssemblyLoad)
906907
DEFINE_METHOD(ASSEMBLYLOADCONTEXT, ON_ASSEMBLY_LOAD, OnAssemblyLoad, SM_Assembly_RetVoid)
907908
DEFINE_METHOD(ASSEMBLYLOADCONTEXT, ON_RESOURCE_RESOLVE, OnResourceResolve, SM_Assembly_Str_RetAssembly)

0 commit comments

Comments
 (0)