Skip to content

Commit 2de7d84

Browse files
committed
Fold native AssemblyLoadContext into the base AssemblyBinder
1 parent 01b0567 commit 2de7d84

19 files changed

+104
-148
lines changed

src/coreclr/binder/customassemblybinder.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ HRESULT CustomAssemblyBinder::BindUsingAssemblyName(BINDER_SPACE::AssemblyName*
7777
hr = AssemblyBinderCommon::BindUsingHostAssemblyResolver(GetManagedAssemblyLoadContext(), pAssemblyName, m_pTPABinder, &pCoreCLRFoundAssembly);
7878
if (SUCCEEDED(hr))
7979
{
80-
// We maybe returned an assembly that was bound to a different AssemblyLoadContext instance.
81-
// In such a case, we will not overwrite the binding context (which would be wrong since it would not
80+
// We maybe returned an assembly that was bound to a different AssemblyBinder instance.
81+
// In such a case, we will not overwrite the binder (which would be wrong since the assembly would not
8282
// be present in the cache of the current binding context).
8383
if (pCoreCLRFoundAssembly->GetBinder() == NULL)
8484
{
@@ -159,7 +159,7 @@ AssemblyLoaderAllocator* CustomAssemblyBinder::GetLoaderAllocator()
159159
}
160160

161161
//=============================================================================
162-
// Creates an instance of the AssemblyLoadContext Binder
162+
// Creates an instance of the CustomAssemblyBinder
163163
//
164164
// This method does not take a lock since it is invoked from the ctor of the
165165
// managed AssemblyLoadContext type.
@@ -188,7 +188,7 @@ HRESULT CustomAssemblyBinder::SetupContext(DefaultAssemblyBinder *pTPABinder,
188188

189189
// Save the reference to the IntPtr for GCHandle for the managed
190190
// AssemblyLoadContext instance
191-
pBinder->m_ptrManagedAssemblyLoadContext = ptrAssemblyLoadContext;
191+
pBinder->SetManagedAssemblyLoadContext(ptrAssemblyLoadContext);
192192

193193
if (pLoaderAllocator != NULL)
194194
{
@@ -253,16 +253,16 @@ CustomAssemblyBinder::CustomAssemblyBinder()
253253

254254
void CustomAssemblyBinder::ReleaseLoadContext()
255255
{
256-
VERIFY(m_ptrManagedAssemblyLoadContext != NULL);
256+
VERIFY(GetManagedAssemblyLoadContext() != NULL);
257257
VERIFY(m_ptrManagedStrongAssemblyLoadContext != NULL);
258258

259259
// This method is called to release the weak and strong handles on the managed AssemblyLoadContext
260260
// once the Unloading event has been fired
261-
OBJECTHANDLE handle = reinterpret_cast<OBJECTHANDLE>(m_ptrManagedAssemblyLoadContext);
261+
OBJECTHANDLE handle = reinterpret_cast<OBJECTHANDLE>(GetManagedAssemblyLoadContext());
262262
DestroyLongWeakHandle(handle);
263263
handle = reinterpret_cast<OBJECTHANDLE>(m_ptrManagedStrongAssemblyLoadContext);
264264
DestroyHandle(handle);
265-
m_ptrManagedAssemblyLoadContext = NULL;
265+
SetManagedAssemblyLoadContext(NULL);
266266
}
267267

268268
#endif // !defined(DACCESS_COMPILE)

src/coreclr/binder/inc/customassemblybinder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
class AssemblyLoaderAllocator;
1414
class PEImage;
1515

16-
class CustomAssemblyBinder final : public AssemblyLoadContext
16+
class CustomAssemblyBinder final : public AssemblyBinder
1717
{
1818
public:
1919

src/coreclr/binder/inc/defaultassemblybinder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
#define __DEFAULT_ASSEMBLY_BINDER_H__
77

88
#include "applicationcontext.hpp"
9-
#include "assemblyloadcontext.h"
9+
#include "assemblybinder.h"
1010

1111
class PEAssembly;
1212
class PEImage;
1313

14-
class DefaultAssemblyBinder final : public AssemblyLoadContext
14+
class DefaultAssemblyBinder final : public AssemblyBinder
1515
{
1616
public:
1717
//=========================================================================

src/coreclr/debug/daccess/request.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4769,9 +4769,9 @@ HRESULT ClrDataAccess::GetAssemblyLoadContext(CLRDATA_ADDRESS methodTable, CLRDA
47694769
PTR_Module pModule = pMT->GetModule();
47704770

47714771
PTR_PEFile pPEFile = pModule->GetFile();
4772-
PTR_AssemblyLoadContext pAssemblyLoadContext = pPEFile->GetAssemblyLoadContext();
4772+
PTR_AssemblyBinder pBinder = pPEFile->GetAssemblyBinder();
47734773

4774-
INT_PTR managedAssemblyLoadContextHandle = pAssemblyLoadContext->GetManagedAssemblyLoadContext();
4774+
INT_PTR managedAssemblyLoadContextHandle = pBinder->GetManagedAssemblyLoadContext();
47754775

47764776
TADDR managedAssemblyLoadContextAddr = 0;
47774777
if (managedAssemblyLoadContextHandle != 0)

src/coreclr/vm/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ set(VM_SOURCES_DAC_AND_WKS_COMMON
5050
array.cpp
5151
assembly.cpp
5252
assemblybinder.cpp
53-
assemblyloadcontext.cpp
5453
binder.cpp
5554
bundle.cpp
5655
castcache.cpp
@@ -149,7 +148,6 @@ set(VM_HEADERS_DAC_AND_WKS_COMMON
149148
array.h
150149
assembly.hpp
151150
assemblybinder.h
152-
assemblyloadcontext.h
153151
binder.h
154152
castcache.h
155153
callcounting.h

src/coreclr/vm/appdomain.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2844,7 +2844,7 @@ DomainAssembly *AppDomain::LoadDomainAssemblyInternal(AssemblySpec* pIdentity,
28442844
AssemblyBinder *pFileBinder = pFile->GetBindingContext();
28452845
if (pFileBinder != NULL)
28462846
{
2847-
// Assemblies loaded with AssemblyLoadContext need to use a different LoaderAllocator if
2847+
// Assemblies loaded with CustomAssemblyBinder need to use a different LoaderAllocator if
28482848
// marked as collectible
28492849
pLoaderAllocator = pFileBinder->GetLoaderAllocator();
28502850
}
@@ -2904,7 +2904,7 @@ DomainAssembly *AppDomain::LoadDomainAssemblyInternal(AssemblySpec* pIdentity,
29042904

29052905
if (registerNewAssembly)
29062906
{
2907-
pFile->GetAssemblyLoadContext()->AddLoadedAssembly(pDomainAssembly->GetCurrentAssembly());
2907+
pFile->GetAssemblyBinder()->AddLoadedAssembly(pDomainAssembly->GetCurrentAssembly());
29082908
}
29092909
}
29102910
else

src/coreclr/vm/assemblybinder.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
#include "assemblybinder.h"
5+
#include "nativeimage.h"
56
#include "../binder/inc/assemblyname.hpp"
67

78
#ifndef DACCESS_COMPILE
@@ -24,4 +25,39 @@ HRESULT AssemblyBinder::BindAssemblyByName(AssemblyNameData* pAssemblyNameData,
2425
return hr;
2526
}
2627

28+
29+
NativeImage* AssemblyBinder::LoadNativeImage(Module* componentModule, LPCUTF8 nativeImageName)
30+
{
31+
STANDARD_VM_CONTRACT;
32+
33+
BaseDomain::LoadLockHolder lock(AppDomain::GetCurrentDomain());
34+
AssemblyBinder* binder = componentModule->GetFile()->GetAssemblyBinder();
35+
PTR_LoaderAllocator moduleLoaderAllocator = componentModule->GetLoaderAllocator();
36+
37+
bool isNewNativeImage;
38+
NativeImage* nativeImage = NativeImage::Open(componentModule, nativeImageName, binder, moduleLoaderAllocator, &isNewNativeImage);
39+
40+
if (isNewNativeImage && nativeImage != nullptr)
41+
{
42+
m_nativeImages.Append(nativeImage);
43+
44+
for (COUNT_T assemblyIndex = 0; assemblyIndex < m_loadedAssemblies.GetCount(); assemblyIndex++)
45+
{
46+
nativeImage->CheckAssemblyMvid(m_loadedAssemblies[assemblyIndex]);
47+
}
48+
}
49+
50+
return nativeImage;
51+
}
52+
53+
void AssemblyBinder::AddLoadedAssembly(Assembly* loadedAssembly)
54+
{
55+
BaseDomain::LoadLockHolder lock(AppDomain::GetCurrentDomain());
56+
m_loadedAssemblies.Append(loadedAssembly);
57+
for (COUNT_T nativeImageIndex = 0; nativeImageIndex < m_nativeImages.GetCount(); nativeImageIndex++)
58+
{
59+
m_nativeImages[nativeImageIndex]->CheckAssemblyMvid(loadedAssembly);
60+
}
61+
}
62+
2763
#endif //DACCESS_COMPILE

src/coreclr/vm/assemblybinder.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
#ifndef _ASSEMBLYBINDER_H
55
#define _ASSEMBLYBINDER_H
66

7+
#include <sarray.h>
78
#include "../binder/inc/applicationcontext.hpp"
89

910
class PEImage;
11+
class NativeImage;
12+
class Assembly;
13+
class Module;
1014
class AssemblyLoaderAllocator;
1115

1216
class AssemblyBinder
@@ -33,8 +37,28 @@ class AssemblyBinder
3337
return &m_appContext;
3438
}
3539

40+
INT_PTR GetManagedAssemblyLoadContext()
41+
{
42+
return m_ptrManagedAssemblyLoadContext;
43+
}
44+
45+
void SetManagedAssemblyLoadContext(INT_PTR ptrManagedTPABinderInstance)
46+
{
47+
m_ptrManagedAssemblyLoadContext = ptrManagedTPABinderInstance;
48+
}
49+
50+
NativeImage* LoadNativeImage(Module* componentModule, LPCUTF8 nativeImageName);
51+
void AddLoadedAssembly(Assembly* loadedAssembly);
52+
3653
private:
3754
BINDER_SPACE::ApplicationContext m_appContext;
55+
56+
// A GC handle to the managed AssemblyLoadContext.
57+
// It is a long weak handle for collectible AssemblyLoadContexts and strong handle for non-collectible ones.
58+
INT_PTR m_ptrManagedAssemblyLoadContext;
59+
60+
SArray<NativeImage*> m_nativeImages;
61+
SArray<Assembly*> m_loadedAssemblies;
3862
};
3963

4064
#endif

src/coreclr/vm/assemblyloadcontext.cpp

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

src/coreclr/vm/assemblyloadcontext.h

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

src/coreclr/vm/assemblynative.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,16 +1254,12 @@ INT_PTR QCALLTYPE AssemblyNative::GetLoadContextForAssembly(QCall::AssemblyHandl
12541254

12551255
_ASSERTE(pAssembly != NULL);
12561256

1257-
AssemblyLoadContext* pAssemblyLoadContext = pAssembly->GetFile()->GetAssemblyLoadContext();
1257+
AssemblyBinder* pAssemblyBinder = pAssembly->GetFile()->GetAssemblyBinder();
12581258

1259-
if (pAssemblyLoadContext != AppDomain::GetCurrentDomain()->GetTPABinderContext())
1259+
if (pAssemblyBinder != AppDomain::GetCurrentDomain()->GetTPABinderContext())
12601260
{
1261-
// Only CustomAssemblyBinder instance contains the reference to its
1262-
// corresponding managed instance.
1263-
CustomAssemblyBinder* pBinder = (CustomAssemblyBinder*)(pAssemblyLoadContext);
1264-
12651261
// Fetch the managed binder reference from the native binder instance
1266-
ptrManagedAssemblyLoadContext = pBinder->GetManagedAssemblyLoadContext();
1262+
ptrManagedAssemblyLoadContext = pAssemblyBinder->GetManagedAssemblyLoadContext();
12671263
_ASSERTE(ptrManagedAssemblyLoadContext != NULL);
12681264
}
12691265

src/coreclr/vm/assemblyspec.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -727,10 +727,10 @@ AssemblyBinder* AssemblySpec::GetBindingContextFromParentAssembly(AppDomain *pDo
727727
DefaultAssemblyBinder *pTPABinder = pDomain->GetTPABinderContext();
728728
if (pTPABinder == pParentAssemblyBinder)
729729
{
730-
// If the parent assembly is a platform (TPA) assembly, then its binding context will always be the TPABinder context. In
730+
// If the parent assembly is a platform (TPA) assembly, then its binding context will always be the DefaultBinder context. In
731731
// such case, we will return the default context for binding to allow the bind to go
732732
// via the custom binder context, if it was overridden. If it was not overridden, then we will get the expected
733-
// TPABinder context anyways.
733+
// DefaultBinder context anyways.
734734
//
735735
// Get the reference to the default binding context (this could be the TPABinder context or custom AssemblyLoadContext)
736736
pParentAssemblyBinder = static_cast<AssemblyBinder*>(pDomain->GetTPABinderContext());

src/coreclr/vm/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ typedef DPTR(class ArrayBase) PTR_ArrayBase;
111111
typedef DPTR(class Assembly) PTR_Assembly;
112112
typedef DPTR(class AssemblyBaseObject) PTR_AssemblyBaseObject;
113113
typedef DPTR(class AssemblyLoadContextBaseObject) PTR_AssemblyLoadContextBaseObject;
114-
typedef DPTR(class AssemblyLoadContext) PTR_AssemblyLoadContext;
114+
typedef DPTR(class AssemblyBinder) PTR_AssemblyBinder;
115115
typedef DPTR(class AssemblyNameBaseObject) PTR_AssemblyNameBaseObject;
116116
typedef VPTR(class BaseDomain) PTR_BaseDomain;
117117
typedef DPTR(class ClassLoader) PTR_ClassLoader;

0 commit comments

Comments
 (0)