From aacefcc82b86bf1aa24333b3c2536cc0a56432af Mon Sep 17 00:00:00 2001 From: Gaurav Khanna Date: Thu, 27 Apr 2017 15:36:24 -0700 Subject: [PATCH] Unify fetching LoadContext for Dynamic Assemblies (dotnet/coreclr#11264) Commit migrated from https://github.com/dotnet/coreclr/commit/d2c5fed03744206122b074a766adaa82634d17ba --- src/coreclr/src/vm/assemblyspec.cpp | 14 ++------------ src/coreclr/src/vm/dllimport.cpp | 14 +------------- src/coreclr/src/vm/pefile.cpp | 14 ++++++++++++-- src/coreclr/src/vm/pefile.h | 6 +++--- 4 files changed, 18 insertions(+), 30 deletions(-) diff --git a/src/coreclr/src/vm/assemblyspec.cpp b/src/coreclr/src/vm/assemblyspec.cpp index e278c00..e5952c2 100644 --- a/src/coreclr/src/vm/assemblyspec.cpp +++ b/src/coreclr/src/vm/assemblyspec.cpp @@ -785,15 +785,6 @@ ICLRPrivBinder* AssemblySpec::GetBindingContextFromParentAssembly(AppDomain *pDo // ICLRPrivAssembly implements ICLRPrivBinder and thus, "is a" binder in a manner of semantics. pParentAssemblyBinder = pParentPEAssembly->GetBindingContext(); - if (pParentAssemblyBinder == NULL) - { - if (pParentPEAssembly->IsDynamic()) - { - // If the parent assembly is dynamically generated, then use its fallback load context - // as the binder. - pParentAssemblyBinder = pParentPEAssembly->GetFallbackLoadContextBinder(); - } - } } if (GetPreferFallbackLoadContextBinder()) @@ -811,13 +802,12 @@ ICLRPrivBinder* AssemblySpec::GetBindingContextFromParentAssembly(AppDomain *pDo // // 1) Domain Neutral assembly // 2) Entrypoint assembly - // 3) RefEmitted assembly - // 4) AssemblyLoadContext.LoadFromAssemblyName + // 3) AssemblyLoadContext.LoadFromAssemblyName // // For (1) and (2), we will need to bind against the DefaultContext binder (aka TPA Binder). This happens // below if we do not find the parent assembly binder. // - // For (3) and (4), fetch the fallback load context binder reference. + // For (3), fetch the fallback load context binder reference. pParentAssemblyBinder = GetFallbackLoadContextBinderForRequestingAssembly(); } diff --git a/src/coreclr/src/vm/dllimport.cpp b/src/coreclr/src/vm/dllimport.cpp index 3abe9cb..b58ac56 100644 --- a/src/coreclr/src/vm/dllimport.cpp +++ b/src/coreclr/src/vm/dllimport.cpp @@ -5938,19 +5938,7 @@ HMODULE NDirect::LoadLibraryModuleViaHost(NDirectMethodDesc * pMD, AppDomain* pD // The Binding Context can be null or an overridden TPA context if (pBindingContext == NULL) { - pBindingContext = nullptr; - - // If the assembly does not have a binder associated with it explicitly, then check if it is - // a dynamic assembly, or not, since they can have a fallback load context associated with them. - if (pManifestFile->IsDynamic()) - { - pBindingContext = pManifestFile->GetFallbackLoadContextBinder(); - } - } - - // If we do not have any binder associated, then return to the default resolution mechanism. - if (pBindingContext == nullptr) - { + // If we do not have any binder associated, then return to the default resolution mechanism. return NULL; } diff --git a/src/coreclr/src/vm/pefile.cpp b/src/coreclr/src/vm/pefile.cpp index 16c66b5..c7870e6 100644 --- a/src/coreclr/src/vm/pefile.cpp +++ b/src/coreclr/src/vm/pefile.cpp @@ -2849,12 +2849,22 @@ PTR_ICLRPrivBinder PEFile::GetBindingContext() PTR_ICLRPrivBinder pBindingContext = NULL; - // Mscorlib is always bound in context of the TPA Binder. However, since it gets loaded and published - // during EEStartup *before* TPAbinder is initialized, we dont have a binding context to publish against. + // CoreLibrary is always bound in context of the TPA Binder. However, since it gets loaded and published + // during EEStartup *before* DefaultContext Binder (aka TPAbinder) is initialized, we dont have a binding context to publish against. // Thus, we will always return NULL for its binding context. if (!IsSystem()) { pBindingContext = dac_cast(GetHostAssembly()); + if (!pBindingContext) + { + // If we do not have any binding context, check if we are dealing with + // a dynamically emitted assembly and if so, use its fallback load context + // binder reference. + if (IsDynamic()) + { + pBindingContext = GetFallbackLoadContextBinder(); + } + } } return pBindingContext; diff --git a/src/coreclr/src/vm/pefile.h b/src/coreclr/src/vm/pefile.h index b7c7bd3..2856083 100644 --- a/src/coreclr/src/vm/pefile.h +++ b/src/coreclr/src/vm/pefile.h @@ -631,7 +631,7 @@ protected: // To enable this, we maintain a concept of "Fallback LoadContext", which will be set to the Binder of the // assembly that created the dynamic assembly. If the creator assembly is dynamic itself, then its fallback // load context would be propagated to the assembly being dynamically generated. - ICLRPrivBinder *m_pFallbackLoadContextBinder; + PTR_ICLRPrivBinder m_pFallbackLoadContextBinder; protected: @@ -657,13 +657,13 @@ public: bool CanUseWithBindingCache() { LIMITED_METHOD_CONTRACT; return !HasHostAssembly(); } - void SetFallbackLoadContextBinder(ICLRPrivBinder *pFallbackLoadContextBinder) + void SetFallbackLoadContextBinder(PTR_ICLRPrivBinder pFallbackLoadContextBinder) { LIMITED_METHOD_CONTRACT; m_pFallbackLoadContextBinder = pFallbackLoadContextBinder; } - ICLRPrivBinder *GetFallbackLoadContextBinder() + PTR_ICLRPrivBinder GetFallbackLoadContextBinder() { LIMITED_METHOD_CONTRACT; -- 2.7.4