Skip to content

Commit

Permalink
Fold native AssemblyLoadContext into the base AssemblyBinder
Browse files Browse the repository at this point in the history
  • Loading branch information
VSadov committed Aug 31, 2021
1 parent 01b0567 commit 2de7d84
Show file tree
Hide file tree
Showing 19 changed files with 104 additions and 148 deletions.
14 changes: 7 additions & 7 deletions src/coreclr/binder/customassemblybinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ HRESULT CustomAssemblyBinder::BindUsingAssemblyName(BINDER_SPACE::AssemblyName*
hr = AssemblyBinderCommon::BindUsingHostAssemblyResolver(GetManagedAssemblyLoadContext(), pAssemblyName, m_pTPABinder, &pCoreCLRFoundAssembly);
if (SUCCEEDED(hr))
{
// We maybe returned an assembly that was bound to a different AssemblyLoadContext instance.
// In such a case, we will not overwrite the binding context (which would be wrong since it would not
// We maybe returned an assembly that was bound to a different AssemblyBinder instance.
// In such a case, we will not overwrite the binder (which would be wrong since the assembly would not
// be present in the cache of the current binding context).
if (pCoreCLRFoundAssembly->GetBinder() == NULL)
{
Expand Down Expand Up @@ -159,7 +159,7 @@ AssemblyLoaderAllocator* CustomAssemblyBinder::GetLoaderAllocator()
}

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

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

if (pLoaderAllocator != NULL)
{
Expand Down Expand Up @@ -253,16 +253,16 @@ CustomAssemblyBinder::CustomAssemblyBinder()

void CustomAssemblyBinder::ReleaseLoadContext()
{
VERIFY(m_ptrManagedAssemblyLoadContext != NULL);
VERIFY(GetManagedAssemblyLoadContext() != NULL);
VERIFY(m_ptrManagedStrongAssemblyLoadContext != NULL);

// This method is called to release the weak and strong handles on the managed AssemblyLoadContext
// once the Unloading event has been fired
OBJECTHANDLE handle = reinterpret_cast<OBJECTHANDLE>(m_ptrManagedAssemblyLoadContext);
OBJECTHANDLE handle = reinterpret_cast<OBJECTHANDLE>(GetManagedAssemblyLoadContext());
DestroyLongWeakHandle(handle);
handle = reinterpret_cast<OBJECTHANDLE>(m_ptrManagedStrongAssemblyLoadContext);
DestroyHandle(handle);
m_ptrManagedAssemblyLoadContext = NULL;
SetManagedAssemblyLoadContext(NULL);
}

#endif // !defined(DACCESS_COMPILE)
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/binder/inc/customassemblybinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
class AssemblyLoaderAllocator;
class PEImage;

class CustomAssemblyBinder final : public AssemblyLoadContext
class CustomAssemblyBinder final : public AssemblyBinder
{
public:

Expand Down
4 changes: 2 additions & 2 deletions src/coreclr/binder/inc/defaultassemblybinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
#define __DEFAULT_ASSEMBLY_BINDER_H__

#include "applicationcontext.hpp"
#include "assemblyloadcontext.h"
#include "assemblybinder.h"

class PEAssembly;
class PEImage;

class DefaultAssemblyBinder final : public AssemblyLoadContext
class DefaultAssemblyBinder final : public AssemblyBinder
{
public:
//=========================================================================
Expand Down
4 changes: 2 additions & 2 deletions src/coreclr/debug/daccess/request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4769,9 +4769,9 @@ HRESULT ClrDataAccess::GetAssemblyLoadContext(CLRDATA_ADDRESS methodTable, CLRDA
PTR_Module pModule = pMT->GetModule();

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

INT_PTR managedAssemblyLoadContextHandle = pAssemblyLoadContext->GetManagedAssemblyLoadContext();
INT_PTR managedAssemblyLoadContextHandle = pBinder->GetManagedAssemblyLoadContext();

TADDR managedAssemblyLoadContextAddr = 0;
if (managedAssemblyLoadContextHandle != 0)
Expand Down
2 changes: 0 additions & 2 deletions src/coreclr/vm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ set(VM_SOURCES_DAC_AND_WKS_COMMON
array.cpp
assembly.cpp
assemblybinder.cpp
assemblyloadcontext.cpp
binder.cpp
bundle.cpp
castcache.cpp
Expand Down Expand Up @@ -149,7 +148,6 @@ set(VM_HEADERS_DAC_AND_WKS_COMMON
array.h
assembly.hpp
assemblybinder.h
assemblyloadcontext.h
binder.h
castcache.h
callcounting.h
Expand Down
4 changes: 2 additions & 2 deletions src/coreclr/vm/appdomain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2844,7 +2844,7 @@ DomainAssembly *AppDomain::LoadDomainAssemblyInternal(AssemblySpec* pIdentity,
AssemblyBinder *pFileBinder = pFile->GetBindingContext();
if (pFileBinder != NULL)
{
// Assemblies loaded with AssemblyLoadContext need to use a different LoaderAllocator if
// Assemblies loaded with CustomAssemblyBinder need to use a different LoaderAllocator if
// marked as collectible
pLoaderAllocator = pFileBinder->GetLoaderAllocator();
}
Expand Down Expand Up @@ -2904,7 +2904,7 @@ DomainAssembly *AppDomain::LoadDomainAssemblyInternal(AssemblySpec* pIdentity,

if (registerNewAssembly)
{
pFile->GetAssemblyLoadContext()->AddLoadedAssembly(pDomainAssembly->GetCurrentAssembly());
pFile->GetAssemblyBinder()->AddLoadedAssembly(pDomainAssembly->GetCurrentAssembly());
}
}
else
Expand Down
36 changes: 36 additions & 0 deletions src/coreclr/vm/assemblybinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

#include "assemblybinder.h"
#include "nativeimage.h"
#include "../binder/inc/assemblyname.hpp"

#ifndef DACCESS_COMPILE
Expand All @@ -24,4 +25,39 @@ HRESULT AssemblyBinder::BindAssemblyByName(AssemblyNameData* pAssemblyNameData,
return hr;
}


NativeImage* AssemblyBinder::LoadNativeImage(Module* componentModule, LPCUTF8 nativeImageName)
{
STANDARD_VM_CONTRACT;

BaseDomain::LoadLockHolder lock(AppDomain::GetCurrentDomain());
AssemblyBinder* binder = componentModule->GetFile()->GetAssemblyBinder();
PTR_LoaderAllocator moduleLoaderAllocator = componentModule->GetLoaderAllocator();

bool isNewNativeImage;
NativeImage* nativeImage = NativeImage::Open(componentModule, nativeImageName, binder, moduleLoaderAllocator, &isNewNativeImage);

if (isNewNativeImage && nativeImage != nullptr)
{
m_nativeImages.Append(nativeImage);

for (COUNT_T assemblyIndex = 0; assemblyIndex < m_loadedAssemblies.GetCount(); assemblyIndex++)
{
nativeImage->CheckAssemblyMvid(m_loadedAssemblies[assemblyIndex]);
}
}

return nativeImage;
}

void AssemblyBinder::AddLoadedAssembly(Assembly* loadedAssembly)
{
BaseDomain::LoadLockHolder lock(AppDomain::GetCurrentDomain());
m_loadedAssemblies.Append(loadedAssembly);
for (COUNT_T nativeImageIndex = 0; nativeImageIndex < m_nativeImages.GetCount(); nativeImageIndex++)
{
m_nativeImages[nativeImageIndex]->CheckAssemblyMvid(loadedAssembly);
}
}

#endif //DACCESS_COMPILE
24 changes: 24 additions & 0 deletions src/coreclr/vm/assemblybinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
#ifndef _ASSEMBLYBINDER_H
#define _ASSEMBLYBINDER_H

#include <sarray.h>
#include "../binder/inc/applicationcontext.hpp"

class PEImage;
class NativeImage;
class Assembly;
class Module;
class AssemblyLoaderAllocator;

class AssemblyBinder
Expand All @@ -33,8 +37,28 @@ class AssemblyBinder
return &m_appContext;
}

INT_PTR GetManagedAssemblyLoadContext()
{
return m_ptrManagedAssemblyLoadContext;
}

void SetManagedAssemblyLoadContext(INT_PTR ptrManagedTPABinderInstance)
{
m_ptrManagedAssemblyLoadContext = ptrManagedTPABinderInstance;
}

NativeImage* LoadNativeImage(Module* componentModule, LPCUTF8 nativeImageName);
void AddLoadedAssembly(Assembly* loadedAssembly);

private:
BINDER_SPACE::ApplicationContext m_appContext;

// A GC handle to the managed AssemblyLoadContext.
// It is a long weak handle for collectible AssemblyLoadContexts and strong handle for non-collectible ones.
INT_PTR m_ptrManagedAssemblyLoadContext;

SArray<NativeImage*> m_nativeImages;
SArray<Assembly*> m_loadedAssemblies;
};

#endif
47 changes: 0 additions & 47 deletions src/coreclr/vm/assemblyloadcontext.cpp

This file was deleted.

49 changes: 0 additions & 49 deletions src/coreclr/vm/assemblyloadcontext.h

This file was deleted.

10 changes: 3 additions & 7 deletions src/coreclr/vm/assemblynative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1254,16 +1254,12 @@ INT_PTR QCALLTYPE AssemblyNative::GetLoadContextForAssembly(QCall::AssemblyHandl

_ASSERTE(pAssembly != NULL);

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

if (pAssemblyLoadContext != AppDomain::GetCurrentDomain()->GetTPABinderContext())
if (pAssemblyBinder != AppDomain::GetCurrentDomain()->GetTPABinderContext())
{
// Only CustomAssemblyBinder instance contains the reference to its
// corresponding managed instance.
CustomAssemblyBinder* pBinder = (CustomAssemblyBinder*)(pAssemblyLoadContext);

// Fetch the managed binder reference from the native binder instance
ptrManagedAssemblyLoadContext = pBinder->GetManagedAssemblyLoadContext();
ptrManagedAssemblyLoadContext = pAssemblyBinder->GetManagedAssemblyLoadContext();
_ASSERTE(ptrManagedAssemblyLoadContext != NULL);
}

Expand Down
4 changes: 2 additions & 2 deletions src/coreclr/vm/assemblyspec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -727,10 +727,10 @@ AssemblyBinder* AssemblySpec::GetBindingContextFromParentAssembly(AppDomain *pDo
DefaultAssemblyBinder *pTPABinder = pDomain->GetTPABinderContext();
if (pTPABinder == pParentAssemblyBinder)
{
// If the parent assembly is a platform (TPA) assembly, then its binding context will always be the TPABinder context. In
// If the parent assembly is a platform (TPA) assembly, then its binding context will always be the DefaultBinder context. In
// such case, we will return the default context for binding to allow the bind to go
// via the custom binder context, if it was overridden. If it was not overridden, then we will get the expected
// TPABinder context anyways.
// DefaultBinder context anyways.
//
// Get the reference to the default binding context (this could be the TPABinder context or custom AssemblyLoadContext)
pParentAssemblyBinder = static_cast<AssemblyBinder*>(pDomain->GetTPABinderContext());
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/vm/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ typedef DPTR(class ArrayBase) PTR_ArrayBase;
typedef DPTR(class Assembly) PTR_Assembly;
typedef DPTR(class AssemblyBaseObject) PTR_AssemblyBaseObject;
typedef DPTR(class AssemblyLoadContextBaseObject) PTR_AssemblyLoadContextBaseObject;
typedef DPTR(class AssemblyLoadContext) PTR_AssemblyLoadContext;
typedef DPTR(class AssemblyBinder) PTR_AssemblyBinder;
typedef DPTR(class AssemblyNameBaseObject) PTR_AssemblyNameBaseObject;
typedef VPTR(class BaseDomain) PTR_BaseDomain;
typedef DPTR(class ClassLoader) PTR_ClassLoader;
Expand Down
Loading

0 comments on commit 2de7d84

Please sign in to comment.