Skip to content

Commit

Permalink
Fix PEFile::GetAssemblyLoadContext during early runtime stages (dotne…
Browse files Browse the repository at this point in the history
…t#38222)

* Fix PEFile::GetAssemblyLoadContext during early runtime stages

The PEFile::GetAssemblyLoadContext was asserting if it was called before
the m_pTPABinderContext was created in AppDomain::CreateBinderContext.
That turned out to be a problem for a check performed in
ReadyToRunInfo::Initialize.

This change calls AppDomain::CreateBinderContext from the
PEFile::SetupAssemblyLoadContext instead of calling
AppDomain::GetTPABinderContext. That ensures that the
PEFile::GetAssemblyLoadContext can always return valid
AssemblyLoadContext.
  • Loading branch information
janvorli authored Jun 22, 2020
1 parent b217441 commit 8552b6d
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 13 deletions.
9 changes: 2 additions & 7 deletions src/coreclr/src/vm/appdomain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4472,9 +4472,9 @@ AppDomain::RaiseUnhandledExceptionEvent(OBJECTREF *pThrowable, BOOL isTerminatin

#endif // CROSSGEN_COMPILE

IUnknown *AppDomain::CreateBinderContext()
CLRPrivBinderCoreCLR *AppDomain::CreateBinderContext()
{
CONTRACT(IUnknown *)
CONTRACT(CLRPrivBinderCoreCLR *)
{
GC_TRIGGERS;
THROWS;
Expand All @@ -4492,11 +4492,6 @@ IUnknown *AppDomain::CreateBinderContext()

// Initialize the assembly binder for the default context loads for CoreCLR.
IfFailThrow(CCoreCLRBinderHelper::DefaultBinderSetupContext(DefaultADID, &m_pTPABinderContext));

// Since the PEAssembly for the System.Private.CoreLib.dll is created before the binder (the AssemblyLoadContext),
// we need to update the AssemblyLoadContext pointer in that PEAssembly now. For other assemblies, it is
// set in the PEFile constructor.
SystemDomain::SystemFile()->SetupAssemblyLoadContext();
}

RETURN m_pTPABinderContext;
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/src/vm/appdomain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2036,7 +2036,7 @@ class AppDomain : public BaseDomain
return m_tpIndex;
}

IUnknown *CreateBinderContext();
CLRPrivBinderCoreCLR *CreateBinderContext();

void SetIgnoreUnhandledExceptions()
{
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/src/vm/pefile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2349,7 +2349,7 @@ void PEFile::SetupAssemblyLoadContext()

m_pAssemblyLoadContext = (pOpaqueBinder != NULL) ?
(AssemblyLoadContext*)pOpaqueBinder :
AppDomain::GetCurrentDomain()->GetTPABinderContext();
AppDomain::GetCurrentDomain()->CreateBinderContext();
}

#endif // #ifndef DACCESS_COMPILE
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/src/vm/pefile.h
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,7 @@ class PEFile

#endif //!DACCESS_COMPILE

// Returns AssemblyLoadContext into which the current PEFile was loaded.
PTR_AssemblyLoadContext GetAssemblyLoadContext()
{
LIMITED_METHOD_CONTRACT;
Expand Down
4 changes: 0 additions & 4 deletions src/coreclr/src/vm/readytoruninfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,10 +504,6 @@ static NativeImage *AcquireCompositeImage(Module * pModule, PEImageLayout * pLay
if (ownerCompositeExecutableName != NULL)
{
AssemblyLoadContext *loadContext = pModule->GetFile()->GetAssemblyLoadContext();
if (loadContext == nullptr)
{
loadContext = (AssemblyLoadContext *)AppDomain::GetCurrentDomain()->CreateBinderContext();
}
return loadContext->LoadNativeImage(pModule, ownerCompositeExecutableName);
}

Expand Down

0 comments on commit 8552b6d

Please sign in to comment.