Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More cleanups in Assembly/Loader/Binder area #58462

Merged
merged 11 commits into from
Sep 8, 2021
63 changes: 31 additions & 32 deletions src/coreclr/binder/assemblybindercommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -619,10 +619,10 @@ namespace BINDER_SPACE

/* static */
HRESULT AssemblyBinderCommon::BindLocked(ApplicationContext *pApplicationContext,
AssemblyName *pAssemblyName,
bool skipVersionCompatibilityCheck,
bool excludeAppPaths,
BindResult *pBindResult)
AssemblyName *pAssemblyName,
bool skipVersionCompatibilityCheck,
bool excludeAppPaths,
BindResult *pBindResult)
{
HRESULT hr = S_OK;

Expand Down Expand Up @@ -688,8 +688,8 @@ namespace BINDER_SPACE

/* static */
HRESULT AssemblyBinderCommon::FindInExecutionContext(ApplicationContext *pApplicationContext,
AssemblyName *pAssemblyName,
ContextEntry **ppContextEntry)
AssemblyName *pAssemblyName,
ContextEntry **ppContextEntry)
{
_ASSERTE(pApplicationContext != NULL);
_ASSERTE(pAssemblyName != NULL);
Expand Down Expand Up @@ -759,10 +759,10 @@ namespace BINDER_SPACE

ReleaseHolder<Assembly> pAssembly;
hr = AssemblyBinderCommon::GetAssembly(relativePath,
FALSE /* fIsInTPA */,
&pAssembly,
NULL, // szMDAssemblyPath
bundleFileLocation);
FALSE /* fIsInTPA */,
&pAssembly,
NULL, // szMDAssemblyPath
bundleFileLocation);

BinderTracing::PathProbed(relativePath, BinderTracing::PathSource::Bundle, hr);

Expand Down Expand Up @@ -808,8 +808,8 @@ namespace BINDER_SPACE
CombinePath(fileName, relativePath, fileName);

hr = AssemblyBinderCommon::GetAssembly(fileName,
FALSE /* fIsInTPA */,
&pAssembly);
FALSE /* fIsInTPA */,
&pAssembly);
BinderTracing::PathProbed(fileName, pathSource, hr);

// Missing files are okay and expected when probing
Expand Down Expand Up @@ -979,9 +979,9 @@ namespace BINDER_SPACE
*/
/* static */
HRESULT AssemblyBinderCommon::BindByTpaList(ApplicationContext *pApplicationContext,
AssemblyName *pRequestedAssemblyName,
bool excludeAppPaths,
BindResult *pBindResult)
AssemblyName *pRequestedAssemblyName,
bool excludeAppPaths,
BindResult *pBindResult)
{
HRESULT hr = S_OK;

Expand Down Expand Up @@ -1152,7 +1152,6 @@ namespace BINDER_SPACE
HRESULT AssemblyBinderCommon::GetAssembly(SString &assemblyPath,
BOOL fIsInTPA,
Assembly **ppAssembly,

// If assemblyPath refers to a native image without metadata,
// szMDAssemblyPath gives the alternative file to get metadata.
LPCTSTR szMDAssemblyPath,
Expand Down Expand Up @@ -1210,7 +1209,7 @@ namespace BINDER_SPACE

/* static */
HRESULT AssemblyBinderCommon::Register(ApplicationContext *pApplicationContext,
BindResult *pBindResult)
BindResult *pBindResult)
{
HRESULT hr = S_OK;

Expand Down Expand Up @@ -1242,9 +1241,9 @@ namespace BINDER_SPACE

/* static */
HRESULT AssemblyBinderCommon::RegisterAndGetHostChosen(ApplicationContext *pApplicationContext,
LONG kContextVersion,
BindResult *pBindResult,
BindResult *pHostBindResult)
LONG kContextVersion,
BindResult *pBindResult,
BindResult *pHostBindResult)
{
HRESULT hr = S_OK;

Expand All @@ -1264,7 +1263,7 @@ namespace BINDER_SPACE
if (kContextVersion != pApplicationContext->GetVersion())
{
IF_FAIL_GO(AssemblyBinderCommon::OtherBindInterfered(pApplicationContext,
pBindResult));
pBindResult));

if (hr == S_FALSE)
{
Expand Down Expand Up @@ -1325,9 +1324,9 @@ namespace BINDER_SPACE

#if !defined(DACCESS_COMPILE)
HRESULT AssemblyBinderCommon::BindUsingHostAssemblyResolver(/* in */ INT_PTR pManagedAssemblyLoadContextToBindWithin,
/* in */ AssemblyName *pAssemblyName,
/* in */ DefaultAssemblyBinder *pTPABinder,
/* out */ Assembly **ppAssembly)
/* in */ AssemblyName *pAssemblyName,
/* in */ DefaultAssemblyBinder *pTPABinder,
/* out */ Assembly **ppAssembly)
{
HRESULT hr = E_FAIL;

Expand All @@ -1348,11 +1347,11 @@ HRESULT AssemblyBinderCommon::BindUsingHostAssemblyResolver(/* in */ INT_PTR pMa

/* static */
HRESULT AssemblyBinderCommon::BindUsingPEImage(/* in */ AssemblyBinder* pBinder,
/* in */ BINDER_SPACE::AssemblyName *pAssemblyName,
/* in */ PEImage *pPEImage,
/* in */ PEKIND peKind,
/* in */ IMDInternalImport *pIMetaDataAssemblyImport,
/* [retval] [out] */ Assembly **ppAssembly)
/* in */ BINDER_SPACE::AssemblyName *pAssemblyName,
/* in */ PEImage *pPEImage,
/* in */ PEKIND peKind,
/* in */ IMDInternalImport *pIMetaDataAssemblyImport,
/* [retval] [out] */ Assembly **ppAssembly)
{
HRESULT hr = E_FAIL;

Expand Down Expand Up @@ -1485,9 +1484,9 @@ HRESULT AssemblyBinderCommon::DefaultBinderSetupContext(DefaultAssemblyBinder**
return hr;
}

HRESULT AssemblyBinderCommon::GetAssemblyIdentity(LPCSTR szTextualIdentity,
BINDER_SPACE::ApplicationContext* pApplicationContext,
NewHolder<AssemblyIdentityUTF8>& assemblyIdentityHolder)
HRESULT AssemblyBinderCommon::GetAssemblyIdentity(LPCSTR szTextualIdentity,
BINDER_SPACE::ApplicationContext* pApplicationContext,
NewHolder<AssemblyIdentityUTF8>& assemblyIdentityHolder)
{
HRESULT hr = S_OK;
_ASSERTE(szTextualIdentity != NULL);
Expand Down
24 changes: 12 additions & 12 deletions src/coreclr/binder/customassemblybinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ using namespace BINDER_SPACE;
// CustomAssemblyBinder implementation
// ============================================================================
HRESULT CustomAssemblyBinder::BindAssemblyByNameWorker(BINDER_SPACE::AssemblyName *pAssemblyName,
BINDER_SPACE::Assembly **ppCoreCLRFoundAssembly)
BINDER_SPACE::Assembly **ppCoreCLRFoundAssembly)
{
VALIDATE_ARG_RET(pAssemblyName != nullptr && ppCoreCLRFoundAssembly != nullptr);
HRESULT hr = S_OK;
Expand Down 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,17 +159,17 @@ 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.
//=============================================================================
/* static */
HRESULT CustomAssemblyBinder::SetupContext(DefaultAssemblyBinder *pTPABinder,
AssemblyLoaderAllocator* pLoaderAllocator,
void* loaderAllocatorHandle,
UINT_PTR ptrAssemblyLoadContext,
CustomAssemblyBinder **ppBindContext)
AssemblyLoaderAllocator* pLoaderAllocator,
void* loaderAllocatorHandle,
UINT_PTR ptrAssemblyLoadContext,
CustomAssemblyBinder **ppBindContext)
{
HRESULT hr = E_FAIL;
EX_TRY
Expand All @@ -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
4 changes: 2 additions & 2 deletions src/coreclr/binder/defaultassemblybinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ HRESULT DefaultAssemblyBinder::BindAssemblyByNameWorker(BINDER_SPACE::AssemblyNa
// DefaultAssemblyBinder implementation
// ============================================================================
HRESULT DefaultAssemblyBinder::BindUsingAssemblyName(BINDER_SPACE::AssemblyName *pAssemblyName,
BINDER_SPACE::Assembly **ppAssembly)
BINDER_SPACE::Assembly **ppAssembly)
{
HRESULT hr = S_OK;
VALIDATE_ARG_RET(pAssemblyName != nullptr && ppAssembly != nullptr);
Expand Down Expand Up @@ -87,7 +87,7 @@ HRESULT DefaultAssemblyBinder::BindUsingAssemblyName(BINDER_SPACE::AssemblyName
if (pManagedAssemblyLoadContext != NULL)
{
hr = AssemblyBinderCommon::BindUsingHostAssemblyResolver(pManagedAssemblyLoadContext, pAssemblyName,
NULL, &pCoreCLRFoundAssembly);
NULL, &pCoreCLRFoundAssembly);
if (SUCCEEDED(hr))
{
// We maybe returned an assembly that was bound to a different AssemblyLoadContext instance.
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
Loading