Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 6 additions & 20 deletions src/coreclr/vm/assembly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -705,24 +705,10 @@ Module *Assembly::FindModuleByExportedType(mdExportedType mdType,
// We should never get here in the GC case - the above should have succeeded.
CONSISTENCY_CHECK(!FORBIDGC_LOADER_USE_ENABLED());

DomainAssembly* pDomainModule = NULL;
if (loadFlag == Loader::Load)
{
pDomainModule = GetModule()->LoadModule(mdLinkRef);
}

if (pDomainModule == NULL)
RETURN NULL;
else
{
pModule = pDomainModule->GetModule();
if (pModule == NULL)
{
_ASSERTE(loadFlag!=Loader::Load);
}
if (loadFlag != Loader::Load)
return NULL;

RETURN pModule;
}
return GetModule()->LoadModule(mdLinkRef);
#endif // DACCESS_COMPILE
}

Expand Down Expand Up @@ -849,8 +835,8 @@ Module * Assembly::FindModuleByTypeRef(
#ifndef DACCESS_COMPILE
if (loadFlag == Loader::Load)
{
DomainAssembly* pActualDomainAssembly = pModule->LoadModule(tkType);
RETURN(pActualDomainAssembly->GetModule());
Module* pActualModule = pModule->LoadModule(tkType);
RETURN(pActualModule);
}
else
{
Expand Down Expand Up @@ -1493,7 +1479,7 @@ MethodDesc* Assembly::GetEntryPoint()
Module *pModule = NULL;
switch(TypeFromToken(mdEntry)) {
case mdtFile:
pModule = m_pModule->LoadModule(mdEntry)->GetModule();
pModule = m_pModule->LoadModule(mdEntry);

mdEntry = pModule->GetEntryPointToken();
if ( (TypeFromToken(mdEntry) != mdtMethodDef) ||
Expand Down
10 changes: 5 additions & 5 deletions src/coreclr/vm/assemblynative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -707,16 +707,16 @@ extern "C" void QCALLTYPE AssemblyNative_GetModules(QCall::AssemblyHandle pAssem
HENUMInternalHolder phEnum(pAssembly->GetMDImport());
phEnum.EnumInit(mdtFile, mdTokenNil);

InlineSArray<DomainAssembly *, 8> modules;
InlineSArray<Module *, 8> modules;

modules.Append(pAssembly);
modules.Append(pAssembly->GetModule());

mdFile mdFile;
while (pAssembly->GetMDImport()->EnumNext(&phEnum, &mdFile))
{
if (fLoadIfNotFound)
{
DomainAssembly* pModule = pAssembly->GetModule()->LoadModule(mdFile);
Module* pModule = pAssembly->GetModule()->LoadModule(mdFile);
modules.Append(pModule);
}
}
Expand All @@ -733,9 +733,9 @@ extern "C" void QCALLTYPE AssemblyNative_GetModules(QCall::AssemblyHandle pAssem

for(COUNT_T i = 0; i < modules.GetCount(); i++)
{
DomainAssembly * pModule = modules[i];
Module * pModule = modules[i];

OBJECTREF o = pModule->GetExposedModuleObject();
OBJECTREF o = pModule->GetExposedObject();
orModules->SetAt(i, o);
}

Expand Down
85 changes: 72 additions & 13 deletions src/coreclr/vm/ceeload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,10 @@ void Module::NotifyEtwLoadFinished(HRESULT hr)
// It cannot throw or fail.
//
Module::Module(Assembly *pAssembly, PEAssembly *pPEAssembly)
: m_pPEAssembly{pPEAssembly}
, m_dwTransientFlags{CLASSES_FREED}
, m_pAssembly{pAssembly}
, m_hExposedObject{}
{
CONTRACTL
{
Expand All @@ -338,15 +342,12 @@ Module::Module(Assembly *pAssembly, PEAssembly *pPEAssembly)
}
CONTRACTL_END

PREFIX_ASSUME(pAssembly != NULL);
PREFIX_ASSUME(m_pAssembly != NULL);

m_loaderAllocator = NULL;
m_pAssembly = pAssembly;
m_pPEAssembly = pPEAssembly;
m_dwTransientFlags = CLASSES_FREED;
m_pDynamicMetadata = (TADDR)NULL;

pPEAssembly->AddRef();
m_pPEAssembly->AddRef();
}

uint32_t Module::GetNativeMetadataAssemblyCount()
Expand Down Expand Up @@ -1147,6 +1148,11 @@ void Module::SetDomainAssembly(DomainAssembly *pDomainAssembly)
m_pDomainAssembly = pDomainAssembly;
}

//---------------------------------------------------------------------------------------
//
// Returns managed representation of the module (Module or ModuleBuilder).
// Returns NULL if the managed scout was already collected (see code:LoaderAllocator#AssemblyPhases).
//
OBJECTREF Module::GetExposedObject()
{
CONTRACT(OBJECTREF)
Expand All @@ -1159,7 +1165,62 @@ OBJECTREF Module::GetExposedObject()
}
CONTRACT_END;

RETURN GetDomainAssembly()->GetExposedModuleObject();
LoaderAllocator * pLoaderAllocator = GetLoaderAllocator();

if (m_hExposedObject == (LOADERHANDLE)NULL)
{
// Atomically create a handle
LOADERHANDLE handle = pLoaderAllocator->AllocateHandle(NULL);

InterlockedCompareExchangeT(&m_hExposedObject, handle, static_cast<LOADERHANDLE>(0));
}

if (pLoaderAllocator->GetHandleValue(m_hExposedObject) == NULL)
{
REFLECTMODULEBASEREF refClass = NULL;

// Will be true only if LoaderAllocator managed object was already collected and therefore we should
// return NULL
bool fIsLoaderAllocatorCollected = false;

GCPROTECT_BEGIN(refClass);

refClass = (REFLECTMODULEBASEREF) AllocateObject(CoreLibBinder::GetClass(CLASS__MODULE));
refClass->SetModule(this);

// Attach the reference to the assembly to keep the LoaderAllocator for this collectible type
// alive as long as a reference to the module is kept alive.
if (GetAssembly() != NULL)
{
OBJECTREF refAssembly = GetAssembly()->GetExposedObject();
if ((refAssembly == NULL) && GetAssembly()->IsCollectible())
{
fIsLoaderAllocatorCollected = true;
}
refClass->SetAssembly(refAssembly);
}

pLoaderAllocator->CompareExchangeValueInHandle(m_hExposedObject, (OBJECTREF)refClass, NULL);
GCPROTECT_END();

if (fIsLoaderAllocatorCollected)
{ // The LoaderAllocator managed object was already collected, we cannot re-create it
// Note: We did not publish the allocated Module/ModuleBuilder object, it will get collected
// by GC
return NULL;
}
}

RETURN pLoaderAllocator->GetHandleValue(m_hExposedObject);
}

OBJECTREF Module::GetExposedObjectIfExists()
{
LIMITED_METHOD_CONTRACT;

OBJECTREF objRet = NULL;
GET_LOADERHANDLE_VALUE_FAST(GetLoaderAllocator(), m_hExposedObject, &objRet);
return objRet;
}

//
Expand Down Expand Up @@ -2411,6 +2472,9 @@ Module *Module::GetModuleIfLoaded(mdFile kFile)

ENABLE_FORBID_GC_LOADER_USE_IN_THIS_SCOPE();

if (kFile == mdFileNil)
return this;

// Handle the module ref case
if (TypeFromToken(kFile) == mdtModuleRef)
{
Expand All @@ -2423,19 +2487,14 @@ Module *Module::GetModuleIfLoaded(mdFile kFile)
RETURN GetAssembly()->GetModule()->GetModuleIfLoaded(mdFileNil);
}

if (kFile == mdFileNil)
{
return this;
}

RETURN NULL;
}

#ifndef DACCESS_COMPILE

DomainAssembly *ModuleBase::LoadModule(mdFile kFile)
Module *ModuleBase::LoadModule(mdFile kFile)
{
CONTRACT(DomainAssembly *)
CONTRACT(Module *)
{
INSTANCE_CHECK;
THROWS;
Expand Down
5 changes: 4 additions & 1 deletion src/coreclr/vm/ceeload.h
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ class ModuleBase
virtual PTR_Module LookupModule(mdToken kFile) { return NULL; }; //wrapper over GetModuleIfLoaded, takes modulerefs as well
virtual Module *GetModuleIfLoaded(mdFile kFile) { return NULL; };
#ifndef DACCESS_COMPILE
virtual DomainAssembly *LoadModule(mdFile kFile);
virtual Module *LoadModule(mdFile kFile);
#endif
DWORD GetAssemblyRefFlags(mdAssemblyRef tkAssemblyRef);

Expand Down Expand Up @@ -897,6 +897,7 @@ class Module : public ModuleBase
void SetDomainAssembly(DomainAssembly *pDomainAssembly);

OBJECTREF GetExposedObject();
OBJECTREF GetExposedObjectIfExists();

ClassLoader *GetClassLoader();
#ifdef FEATURE_CODE_VERSIONING
Expand Down Expand Up @@ -1601,6 +1602,8 @@ class Module : public ModuleBase

PTR_Assembly *m_NativeMetadataAssemblyRefMap;

LOADERHANDLE m_hExposedObject;

// Buffer of Metadata storage for dynamic modules. May be NULL. This provides a reasonable way for
// the debugger to get metadata of dynamic modules from out of process.
// A dynamic module will eagerly serialize its metadata to this buffer.
Expand Down
66 changes: 0 additions & 66 deletions src/coreclr/vm/domainassembly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ DomainAssembly::DomainAssembly(PEAssembly* pPEAssembly, LoaderAllocator* pLoader
m_pLoaderAllocator(pLoaderAllocator),
m_level(FILE_LOAD_CREATE),
m_loading(TRUE),
m_hExposedModuleObject{},
m_hExposedAssemblyObject{},
m_pError(NULL),
m_bDisableActivationCheck(FALSE),
Expand Down Expand Up @@ -310,71 +309,6 @@ BOOL DomainAssembly::IsVisibleToDebugger()

#ifndef DACCESS_COMPILE

//---------------------------------------------------------------------------------------
//
// Returns managed representation of the module (Module or ModuleBuilder).
// Returns NULL if the managed scout was already collected (see code:LoaderAllocator#AssemblyPhases).
//
OBJECTREF DomainAssembly::GetExposedModuleObject()
{
CONTRACTL
{
INSTANCE_CHECK;
THROWS;
MODE_COOPERATIVE;
GC_TRIGGERS;
}
CONTRACTL_END;

LoaderAllocator * pLoaderAllocator = GetLoaderAllocator();

if (m_hExposedModuleObject == (LOADERHANDLE)NULL)
{
// Atomically create a handle
LOADERHANDLE handle = pLoaderAllocator->AllocateHandle(NULL);

InterlockedCompareExchangeT(&m_hExposedModuleObject, handle, static_cast<LOADERHANDLE>(0));
}

if (pLoaderAllocator->GetHandleValue(m_hExposedModuleObject) == NULL)
{
REFLECTMODULEBASEREF refClass = NULL;

// Will be true only if LoaderAllocator managed object was already collected and therefore we should
// return NULL
bool fIsLoaderAllocatorCollected = false;

GCPROTECT_BEGIN(refClass);

refClass = (REFLECTMODULEBASEREF) AllocateObject(CoreLibBinder::GetClass(CLASS__MODULE));
refClass->SetModule(GetModule());

// Attach the reference to the assembly to keep the LoaderAllocator for this collectible type
// alive as long as a reference to the module is kept alive.
if (GetModule()->GetAssembly() != NULL)
{
OBJECTREF refAssembly = GetModule()->GetAssembly()->GetExposedObject();
if ((refAssembly == NULL) && GetModule()->GetAssembly()->IsCollectible())
{
fIsLoaderAllocatorCollected = true;
}
refClass->SetAssembly(refAssembly);
}

pLoaderAllocator->CompareExchangeValueInHandle(m_hExposedModuleObject, (OBJECTREF)refClass, NULL);
GCPROTECT_END();

if (fIsLoaderAllocatorCollected)
{ // The LoaderAllocator managed object was already collected, we cannot re-create it
// Note: We did not publish the allocated Module/ModuleBuilder object, it will get collected
// by GC
return NULL;
}
}

return pLoaderAllocator->GetHandleValue(m_hExposedModuleObject);
}

BOOL DomainAssembly::DoIncrementalLoad(FileLoadLevel level)
{
STANDARD_VM_CONTRACT;
Expand Down
12 changes: 0 additions & 12 deletions src/coreclr/vm/domainassembly.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,6 @@ class DomainAssembly final
// Returns NULL if the managed scout was already collected (see code:LoaderAllocator#AssemblyPhases).
OBJECTREF GetExposedAssemblyObject();

OBJECTREF GetExposedModuleObjectIfExists()
{
LIMITED_METHOD_CONTRACT;

OBJECTREF objRet = NULL;
GET_LOADERHANDLE_VALUE_FAST(GetLoaderAllocator(), m_hExposedModuleObject, &objRet);
return objRet;
}

OBJECTREF GetExposedModuleObject();

BOOL IsSystem()
{
WRAPPER_NO_CONTRACT;
Expand Down Expand Up @@ -409,7 +398,6 @@ class DomainAssembly final
FileLoadLevel m_level;
BOOL m_loading;

LOADERHANDLE m_hExposedModuleObject;
LOADERHANDLE m_hExposedAssemblyObject;

ExInfo* m_pError;
Expand Down
6 changes: 3 additions & 3 deletions src/coreclr/vm/jitinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -959,10 +959,10 @@ void CEEInfo::resolveToken(/* IN, OUT */ CORINFO_RESOLVED_TOKEN * pResolvedToken
ThrowBadTokenException(pResolvedToken);

{
DomainAssembly *pTargetModule = pModule->LoadModule(metaTOK);
Module *pTargetModule = pModule->LoadModule(metaTOK);
if (pTargetModule == NULL)
COMPlusThrowHR(COR_E_BADIMAGEFORMAT);
th = TypeHandle(pTargetModule->GetModule()->GetGlobalMethodTable());
th = TypeHandle(pTargetModule->GetGlobalMethodTable());
if (th.IsNull())
COMPlusThrowHR(COR_E_BADIMAGEFORMAT);
}
Expand Down Expand Up @@ -6157,7 +6157,7 @@ CORINFO_CLASS_HANDLE CEEInfo::getTypeForBoxOnStack(CORINFO_CLASS_HANDLE cls)
JIT_TO_EE_TRANSITION();

TypeHandle VMClsHnd(cls);
if (Nullable::IsNullableType(VMClsHnd))
if (Nullable::IsNullableType(VMClsHnd))
{
VMClsHnd = VMClsHnd.AsMethodTable()->GetInstantiation()[0];
}
Expand Down
4 changes: 2 additions & 2 deletions src/coreclr/vm/memberload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,10 @@ void MemberLoader::GetDescFromMemberRef(ModuleBase * pModule,
{
case mdtModuleRef:
{
DomainAssembly *pTargetModule = pModule->LoadModule(parent);
Module *pTargetModule = pModule->LoadModule(parent);
if (pTargetModule == NULL)
COMPlusThrowHR(COR_E_BADIMAGEFORMAT);
typeHnd = TypeHandle(pTargetModule->GetModule()->GetGlobalMethodTable());
typeHnd = TypeHandle(pTargetModule->GetGlobalMethodTable());
if (typeHnd.IsNull())
COMPlusThrowHR(COR_E_BADIMAGEFORMAT);
}
Expand Down
Loading