Fix Binder based PInvoke resolution for dynamic assembly (dotnet/coreclr#7770)
authorGaurav Khanna <gkhanna@microsoft.com>
Sat, 22 Oct 2016 16:18:26 +0000 (09:18 -0700)
committerJan Kotas <jkotas@microsoft.com>
Sat, 22 Oct 2016 16:18:26 +0000 (09:18 -0700)
Commit migrated from https://github.com/dotnet/coreclr/commit/5ff10a5b41d5481e21df9bbf5a4e8b419895530d

src/coreclr/src/vm/assembly.cpp
src/coreclr/src/vm/dllimport.cpp

index 481bcea..b3e7611 100644 (file)
@@ -762,7 +762,7 @@ Assembly *Assembly::CreateDynamic(AppDomain *pDomain, CreateDynamicAssemblyArgs
         // using an actual binder. As a result, we will assume the same binding/loadcontext information for the dynamic assembly as its
         // caller/creator to ensure that any assembly loads triggered by the dynamic assembly are resolved using the intended load context.
         //
-        // If the creator assembly has a HostAssembly associated with it, then use it for binding. Otherwise, ithe creator is dynamic
+        // If the creator assembly has a HostAssembly associated with it, then use it for binding. Otherwise, the creator is dynamic
         // and will have a fallback load context binder associated with it.
         ICLRPrivBinder *pFallbackLoadContextBinder = nullptr;
         
index 551e9b2..f724169 100644 (file)
@@ -6832,15 +6832,29 @@ HMODULE NDirect::LoadLibraryModuleViaHost(NDirectMethodDesc * pMD, AppDomain* pD
     CLRPrivBinderCoreCLR *pTPABinder = pDomain->GetTPABinderContext();
     Assembly* pAssembly = pMD->GetMethodTable()->GetAssembly();
    
-    PTR_ICLRPrivBinder pBindingContext = pAssembly->GetManifestFile()->GetBindingContext();
+    PEFile *pManifestFile = pAssembly->GetManifestFile();
+    PTR_ICLRPrivBinder pBindingContext = pManifestFile->GetBindingContext();
 
     //Step 0: Check if  the assembly was bound using TPA. 
     //        The Binding Context can be null or an overridden TPA context
     if (pBindingContext == NULL)
     {
-        return 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)
+    {
+        return NULL;
+    }    
+
     UINT_PTR assemblyBinderID = 0;
     IfFailThrow(pBindingContext->GetBinderID(&assemblyBinderID));