Skip to content

Commit

Permalink
Make AssemblyBaseObject store Assembly instead of DomainAssembly (dot…
Browse files Browse the repository at this point in the history
  • Loading branch information
elinor-fung authored and jtschuster committed Sep 17, 2024
1 parent a66b683 commit 93049b7
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 77 deletions.
29 changes: 7 additions & 22 deletions src/coreclr/vm/appdomain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4448,26 +4448,11 @@ HRESULT RuntimeInvokeHostAssemblyResolver(INT_PTR pManagedAssemblyLoadContextToB

// We were able to get the assembly loaded. Now, get its name since the host could have
// performed the resolution using an assembly with different name.
DomainAssembly *pDomainAssembly = _gcRefs.oRefLoadedAssembly->GetDomainAssembly();
PEAssembly *pLoadedPEAssembly = NULL;
bool fFailLoad = false;
if (!pDomainAssembly)
{
// Reflection emitted assemblies will not have a domain assembly.
fFailLoad = true;
}
else
{
pLoadedPEAssembly = pDomainAssembly->GetPEAssembly();
if (!pLoadedPEAssembly->HasHostAssembly())
{
// Reflection emitted assemblies will not have a domain assembly.
fFailLoad = true;
}
}
Assembly *pAssembly = _gcRefs.oRefLoadedAssembly->GetAssembly();
_ASSERTE(pAssembly != NULL);

// The loaded assembly's BINDER_SPACE::Assembly* is saved as HostAssembly in PEAssembly
if (fFailLoad)
// Disallow reflection emitted assemblies returned in assembly resolution extension points
if (pAssembly->IsDynamic())
{
PathString name;
pAssemblyName->GetDisplayName(name, BINDER_SPACE::AssemblyName::INCLUDE_ALL);
Expand All @@ -4476,9 +4461,9 @@ HRESULT RuntimeInvokeHostAssemblyResolver(INT_PTR pManagedAssemblyLoadContextToB

// For collectible assemblies, ensure that the parent loader allocator keeps the assembly's loader allocator
// alive for all its lifetime.
if (pDomainAssembly->IsCollectible())
if (pAssembly->IsCollectible())
{
LoaderAllocator *pResultAssemblyLoaderAllocator = pDomainAssembly->GetLoaderAllocator();
LoaderAllocator *pResultAssemblyLoaderAllocator = pAssembly->GetLoaderAllocator();
LoaderAllocator *pParentLoaderAllocator = pBinder->GetLoaderAllocator();
if (pParentLoaderAllocator == NULL)
{
Expand All @@ -4490,7 +4475,7 @@ HRESULT RuntimeInvokeHostAssemblyResolver(INT_PTR pManagedAssemblyLoadContextToB
pParentLoaderAllocator->EnsureReference(pResultAssemblyLoaderAllocator);
}

pResolvedAssembly = pLoadedPEAssembly->GetHostAssembly();
pResolvedAssembly = pAssembly->GetPEAssembly()->GetHostAssembly();
}

if (fResolvedAssembly)
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/vm/assembly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1590,7 +1590,7 @@ OBJECTREF Assembly::GetExposedObject()
// Create the assembly object
GCPROTECT_BEGIN(assemblyObj);
assemblyObj = (ASSEMBLYREF)AllocateObject(pMT);
assemblyObj->SetAssembly(GetDomainAssembly());
assemblyObj->SetAssembly(this);

// Attach the reference to the assembly to keep the LoaderAllocator for this collectible type
// alive as long as a reference to the assembly is kept alive.
Expand Down
47 changes: 17 additions & 30 deletions src/coreclr/vm/assemblynative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ extern "C" void QCALLTYPE AssemblyNative_GetLocation(QCall::AssemblyHandle pAsse
END_QCALL;
}

extern "C" void QCALLTYPE AssemblyNative_GetTypeCore(QCall::AssemblyHandle assemblyHandle,
extern "C" void QCALLTYPE AssemblyNative_GetTypeCore(QCall::AssemblyHandle pAssembly,
LPCSTR szTypeName,
LPCSTR * rgszNestedTypeNames,
int32_t cNestedTypeNamesLength,
Expand All @@ -351,8 +351,6 @@ extern "C" void QCALLTYPE AssemblyNative_GetTypeCore(QCall::AssemblyHandle assem

BEGIN_QCALL;

Assembly* pAssembly = assemblyHandle->GetAssembly();

TypeHandle th = TypeHandle();
Module* pManifestModule = pAssembly->GetModule();
ClassLoader* pClassLoader = pAssembly->GetLoader();
Expand Down Expand Up @@ -421,7 +419,7 @@ extern "C" void QCALLTYPE AssemblyNative_GetTypeCoreIgnoreCase(QCall::AssemblyHa

BEGIN_QCALL;

Assembly* pAssembly = assemblyHandle->GetAssembly();
Assembly* pAssembly = assemblyHandle;

TypeHandle th = TypeHandle();
Module* pManifestModule = pAssembly->GetModule();
Expand Down Expand Up @@ -499,14 +497,13 @@ extern "C" void QCALLTYPE AssemblyNative_GetForwardedType(QCall::AssemblyHandle
LPCSTR pszClassName;
mdToken mdImpl;

Assembly * pAsm = pAssembly->GetAssembly();
Module *pManifestModule = pAsm->GetModule();
Module *pManifestModule = pAssembly->GetModule();
IfFailThrow(pManifestModule->GetMDImport()->GetExportedTypeProps(mdtExternalType, &pszNameSpace, &pszClassName, &mdImpl, NULL, NULL));
if (TypeFromToken(mdImpl) == mdtAssemblyRef)
{
NameHandle typeName(pszNameSpace, pszClassName);
typeName.SetTypeToken(pManifestModule, mdtExternalType);
TypeHandle typeHnd = pAsm->GetLoader()->LoadTypeHandleThrowIfFailed(&typeName);
TypeHandle typeHnd = pAssembly->GetLoader()->LoadTypeHandleThrowIfFailed(&typeName);
{
GCX_COOP();
retType.Set(typeHnd.GetManagedClassObject());
Expand All @@ -525,7 +522,7 @@ FCIMPL1(FC_BOOL_RET, AssemblyNative::IsDynamic, AssemblyBaseObject* pAssemblyUNS
if (refAssembly == NULL)
FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));

FC_RETURN_BOOL(refAssembly->GetDomainAssembly()->GetPEAssembly()->IsReflectionEmit());
FC_RETURN_BOOL(refAssembly->GetAssembly()->GetPEAssembly()->IsReflectionEmit());
}
FCIMPLEND

Expand Down Expand Up @@ -648,7 +645,7 @@ extern "C" BYTE * QCALLTYPE AssemblyNative_GetResource(QCall::AssemblyHandle pAs

pAssembly->GetPEAssembly()->GetResource(pNameUTF8, length,
&pbInMemoryResource, NULL, NULL,
NULL, pAssembly->GetAssembly());
NULL, pAssembly);

END_QCALL;

Expand Down Expand Up @@ -680,7 +677,7 @@ extern "C" INT32 QCALLTYPE AssemblyNative_GetManifestResourceInfo(QCall::Assembl
DWORD dwLocation = 0;

if (pAssembly->GetPEAssembly()->GetResource(pNameUTF8, NULL, NULL, &pReferencedAssembly, &pFileName,
&dwLocation, pAssembly->GetAssembly()))
&dwLocation, pAssembly))
{
if (pFileName)
retFileName.Set(pFileName);
Expand Down Expand Up @@ -815,10 +812,7 @@ extern "C" void QCALLTYPE AssemblyNative_GetExportedTypes(QCall::AssemblyHandle
BEGIN_QCALL;

InlineSArray<TypeHandle, 20> types;

Assembly * pAsm = pAssembly->GetAssembly();

IMDInternalImport *pImport = pAsm->GetMDImport();
IMDInternalImport *pImport = pAssembly->GetMDImport();

{
HENUMTypeDefInternalHolder phTDEnum(pImport);
Expand Down Expand Up @@ -846,7 +840,7 @@ extern "C" void QCALLTYPE AssemblyNative_GetExportedTypes(QCall::AssemblyHandle

if (IsTdPublic(dwFlags))
{
TypeHandle typeHnd = ClassLoader::LoadTypeDefThrowing(pAsm->GetModule(), mdTD,
TypeHandle typeHnd = ClassLoader::LoadTypeDefThrowing(pAssembly->GetModule(), mdTD,
ClassLoader::ThrowIfNotFound,
ClassLoader::PermitUninstDefOrRef);
types.Append(typeHnd);
Expand Down Expand Up @@ -894,8 +888,8 @@ extern "C" void QCALLTYPE AssemblyNative_GetExportedTypes(QCall::AssemblyHandle
IsTdPublic(dwFlags))
{
NameHandle typeName(pszNameSpace, pszClassName);
typeName.SetTypeToken(pAsm->GetModule(), mdCT);
TypeHandle typeHnd = pAsm->GetLoader()->LoadTypeHandleThrowIfFailed(&typeName);
typeName.SetTypeToken(pAssembly->GetModule(), mdCT);
TypeHandle typeHnd = pAssembly->GetLoader()->LoadTypeHandleThrowIfFailed(&typeName);

types.Append(typeHnd);
}
Expand Down Expand Up @@ -935,10 +929,7 @@ extern "C" void QCALLTYPE AssemblyNative_GetForwardedTypes(QCall::AssemblyHandle
BEGIN_QCALL;

InlineSArray<TypeHandle, 8> types;

Assembly * pAsm = pAssembly->GetAssembly();

IMDInternalImport *pImport = pAsm->GetMDImport();
IMDInternalImport *pImport = pAssembly->GetMDImport();

// enumerate the ExportedTypes table
{
Expand All @@ -964,8 +955,8 @@ extern "C" void QCALLTYPE AssemblyNative_GetForwardedTypes(QCall::AssemblyHandle
if ((TypeFromToken(mdImpl) == mdtAssemblyRef) && (mdImpl != mdAssemblyRefNil))
{
NameHandle typeName(pszNameSpace, pszClassName);
typeName.SetTypeToken(pAsm->GetModule(), mdCT);
TypeHandle typeHnd = pAsm->GetLoader()->LoadTypeHandleThrowIfFailed(&typeName);
typeName.SetTypeToken(pAssembly->GetModule(), mdCT);
TypeHandle typeHnd = pAssembly->GetLoader()->LoadTypeHandleThrowIfFailed(&typeName);

types.Append(typeHnd);
}
Expand Down Expand Up @@ -1005,9 +996,7 @@ extern "C" void QCALLTYPE AssemblyNative_GetManifestResourceNames(QCall::Assembl

BEGIN_QCALL;

Assembly * pAsm = pAssembly->GetAssembly();

IMDInternalImport *pImport = pAsm->GetMDImport();
IMDInternalImport *pImport = pAssembly->GetMDImport();

HENUMInternalHolder phEnum(pImport);
phEnum.EnumInit(mdtManifestResource, mdTokenNil);
Expand Down Expand Up @@ -1047,9 +1036,7 @@ extern "C" void QCALLTYPE AssemblyNative_GetReferencedAssemblies(QCall::Assembly
{
BEGIN_QCALL;

Assembly * pAsm = pAssembly->GetAssembly();

IMDInternalImport *pImport = pAsm->GetMDImport();
IMDInternalImport *pImport = pAssembly->GetMDImport();

HENUMInternalHolder phEnum(pImport);
phEnum.EnumInit(mdtAssemblyRef, mdTokenNil);
Expand Down Expand Up @@ -1099,7 +1086,7 @@ extern "C" void QCALLTYPE AssemblyNative_GetEntryPoint(QCall::AssemblyHandle pAs

BEGIN_QCALL;

pMeth = pAssembly->GetAssembly()->GetEntryPoint();
pMeth = pAssembly->GetEntryPoint();
if (pMeth != NULL)
{
GCX_COOP();
Expand Down
3 changes: 1 addition & 2 deletions src/coreclr/vm/nativelibrarynative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ extern "C" INT_PTR QCALLTYPE NativeLibrary_LoadByName(LPCWSTR name, QCall::Assem
QCALL_CONTRACT;

NATIVE_LIBRARY_HANDLE handle = nullptr;
Assembly *pAssembly = callingAssembly->GetAssembly();

BEGIN_QCALL;

handle = NativeLibrary::LoadLibraryByName(name, pAssembly, hasDllImportSearchPathFlag, dllImportSearchPathFlag, throwOnError);
handle = NativeLibrary::LoadLibraryByName(name, callingAssembly, hasDllImportSearchPathFlag, dllImportSearchPathFlag, throwOnError);

END_QCALL;

Expand Down
12 changes: 6 additions & 6 deletions src/coreclr/vm/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ TypeHandle Object::GetGCSafeTypeHandleIfPossible() const
Assembly *AssemblyBaseObject::GetAssembly()
{
WRAPPER_NO_CONTRACT;
return m_pAssembly->GetAssembly();
return m_pAssembly;
}

STRINGREF AllocateString(SString sstr)
Expand Down Expand Up @@ -1489,7 +1489,7 @@ void StackTraceArray::Allocate(size_t size)
{
EX_THROW(EEMessageException, (kOverflowException, IDS_EE_ARRAY_DIMENSIONS_EXCEEDED));
}

SetArray(I1ARRAYREF(AllocatePrimitiveArray(ELEMENT_TYPE_I1, static_cast<DWORD>(raw_size.Value()))));
SetSize(0);
SetKeepAliveItemsCount(0);
Expand Down Expand Up @@ -1915,7 +1915,7 @@ void ExceptionObject::SetStackTrace(OBJECTREF stackTrace)
// - if the stack trace was created by the current thread, the arrays are returned as is.
// - if it was created by another thread, deep copies of the arrays are returned. It is ensured
// that both of these arrays are consistent. That means that the stack trace doesn't contain
// frames that need keep alive objects and that are not protected by entries in the keep alive
// frames that need keep alive objects and that are not protected by entries in the keep alive
// array.
void ExceptionObject::GetStackTrace(StackTraceArray & stackTrace, PTRARRAYREF * outKeepAliveArray /*= NULL*/) const
{
Expand Down Expand Up @@ -1956,7 +1956,7 @@ void ExceptionObject::GetStackTrace(StackTraceArray & stackTrace, PTRARRAYREF *

uint32_t keepAliveArrayCapacity = ((*outKeepAliveArray) == NULL) ? 0 : (*outKeepAliveArray)->GetNumComponents();

// It is possible that another thread was modifying the stack trace array and keep alive array while we were making the copies.
// It is possible that another thread was modifying the stack trace array and keep alive array while we were making the copies.
// The following sequence of events could have happened:
// Case 1:
// * The current thread gets the stack trace array and the keep alive array references using the ExceptionObject::GetStackTraceParts above
Expand Down Expand Up @@ -2014,10 +2014,10 @@ void ExceptionObject::GetStackTrace(StackTraceArray & stackTrace, PTRARRAYREF *
}
GCPROTECT_END();
}
#endif // DACCESS_COMPILE
#endif // DACCESS_COMPILE
}

// Get the stack trace and the dynamic method array from the stack trace object.
// Get the stack trace and the dynamic method array from the stack trace object.
// If the stack trace was created by another thread, it returns clones of both arrays.
/* static */
void ExceptionObject::GetStackTraceParts(OBJECTREF stackTraceObj, StackTraceArray & stackTrace, PTRARRAYREF * outKeepAliveArray /*= NULL*/)
Expand Down
10 changes: 2 additions & 8 deletions src/coreclr/vm/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -1408,26 +1408,20 @@ class AssemblyBaseObject : public Object
OBJECTREF m_pModuleEventHandler; // Delegate for 'resolve module' event
STRINGREF m_fullname; // Slot for storing assemblies fullname
OBJECTREF m_pSyncRoot; // Pointer to loader allocator to keep collectible types alive, and to serve as the syncroot for assembly building in ref.emit
DomainAssembly* m_pAssembly; // Pointer to the Assembly Structure
Assembly* m_pAssembly; // Pointer to the Assembly Structure

protected:
AssemblyBaseObject() { LIMITED_METHOD_CONTRACT; }
~AssemblyBaseObject() { LIMITED_METHOD_CONTRACT; }

public:

void SetAssembly(DomainAssembly* p)
void SetAssembly(Assembly* p)
{
LIMITED_METHOD_CONTRACT;
m_pAssembly = p;
}

DomainAssembly* GetDomainAssembly()
{
LIMITED_METHOD_CONTRACT;
return m_pAssembly;
}

Assembly* GetAssembly();

void SetSyncRoot(OBJECTREF pSyncRoot)
Expand Down
6 changes: 3 additions & 3 deletions src/coreclr/vm/qcall.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,15 +248,15 @@ class QCall
struct AssemblyHandle
{
Object ** m_ppObject;
DomainAssembly * m_pAssembly;
Assembly * m_pAssembly;

operator DomainAssembly * ()
operator Assembly * ()
{
LIMITED_METHOD_CONTRACT;
return m_pAssembly;
}

DomainAssembly * operator->() const
Assembly * operator->() const
{
LIMITED_METHOD_CONTRACT;
return m_pAssembly;
Expand Down
7 changes: 2 additions & 5 deletions src/coreclr/vm/runtimehandles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2687,8 +2687,7 @@ FCIMPL1(ReflectModuleBaseObject*, AssemblyHandle::GetManifestModule, AssemblyBas
if (refAssembly == NULL)
FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));

DomainAssembly *pAssembly = refAssembly->GetDomainAssembly();
Assembly* currentAssembly = pAssembly->GetAssembly();
Assembly* currentAssembly = refAssembly->GetAssembly();

FC_RETURN_MODULE_OBJECT(currentAssembly->GetModule(), refAssembly);
}
Expand All @@ -2702,10 +2701,8 @@ FCIMPL1(INT32, AssemblyHandle::GetToken, AssemblyBaseObject* pAssemblyUNSAFE) {
if (refAssembly == NULL)
FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));

DomainAssembly *pAssembly = refAssembly->GetDomainAssembly();
mdAssembly token = mdAssemblyNil;

IMDInternalImport *mdImport = pAssembly->GetAssembly()->GetMDImport();
IMDInternalImport *mdImport = refAssembly->GetAssembly()->GetMDImport();

if (mdImport != 0)
{
Expand Down

0 comments on commit 93049b7

Please sign in to comment.