Fix Binder NI to IL fallback (dotnet/coreclr#11341)
authorSteve MacLean <sdmaclea@qti.qualcomm.com>
Wed, 3 May 2017 18:24:21 +0000 (14:24 -0400)
committerGaurav Khanna <gkhanna@microsoft.com>
Wed, 3 May 2017 18:24:21 +0000 (11:24 -0700)
* Fix Binder NI to IL fallback

Always try NI first

* Fix Windows warning as error

* [Arm64/Unix] Revise PR per feedback

* More cleanup

Commit migrated from https://github.com/dotnet/coreclr/commit/ef8d1522eb15cb0371f31a9392891c942547a91f

src/coreclr/src/binder/assemblybinder.cpp
src/coreclr/src/vm/coreassemblyspec.cpp

index bd5dd18..1015b43 100644 (file)
@@ -689,11 +689,11 @@ namespace BINDER_SPACE
         sCoreLib = sCoreLibDir;
         sCoreLib.Append(CoreLibName_IL_W);
         BOOL fExplicitBindToNativeImage = (fBindToNativeImage == true)? TRUE:FALSE;
-#if defined(FEATURE_PAL) && !defined(_TARGET_AMD64_)      
-        // Non-Amd64 platforms on non-Windows do not support generating the NI image
-        // as CoreLib.dll. For those, we will bind as IL.
+#if defined(FEATURE_PAL) && !defined(_TARGET_AMD64_) && !defined(_TARGET_ARM64_)
+        // Some non-Windows platforms do not automatically generate the NI image as CoreLib.dll.
+        // If those platforms also do not support automatic fallback from NI to IL, bind as IL.
         fExplicitBindToNativeImage = FALSE;
-#endif // defined(FEATURE_PAL) && !defined(_TARGET_AMD64_)
+#endif // defined(FEATURE_PAL) && !defined(_TARGET_AMD64_) && !defined(_TARGET_ARM64_)
         IF_FAIL_GO(AssemblyBinder::GetAssembly(sCoreLib,
                                                    FALSE /* fInspectionOnly */,
                                                    TRUE /* fIsInGAC */,
@@ -1596,8 +1596,13 @@ namespace BINDER_SPACE
                 IF_FAIL_GO(BinderHasNativeHeader(pNativePEImage, &hasHeader));
                 if (!hasHeader)
                 {
-                     pPEImage = pNativePEImage;
-                     pNativePEImage = NULL;
+                    BinderReleasePEImage(pPEImage);
+                    BinderReleasePEImage(pNativePEImage);
+
+                    BINDER_LOG_ENTER(W("BinderAcquirePEImageIL"));
+                    hr = BinderAcquirePEImage(szAssemblyPath, &pPEImage, &pNativePEImage, false);
+                    BINDER_LOG_LEAVE_HR(W("BinderAcquirePEImageIL"), hr);
+                    IF_FAIL_GO(hr);
                 }
             }
 
index 7cb1f56..1d3567e 100644 (file)
@@ -275,8 +275,32 @@ STDAPI BinderAcquirePEImage(LPCWSTR   wszAssemblyPath,
 
 STDAPI BinderHasNativeHeader(PEImage *pPEImage, BOOL* result)
 {
-    *result = pPEImage->HasNativeHeader();
-    return S_OK;
+    HRESULT hr = S_OK;
+
+    _ASSERTE(pPEImage != NULL);
+    _ASSERTE(result != NULL);
+
+    EX_TRY
+    {
+        *result = pPEImage->HasNativeHeader();
+    }
+    EX_CATCH_HRESULT(hr);
+
+    if (FAILED(hr))
+    {
+        *result = false;
+
+#if defined(FEATURE_PAL)
+        // PAL_LOADLoadPEFile may fail while loading IL masquerading as NI.
+        // This will result in a ThrowHR(E_FAIL).  Suppress the error.
+        if(hr == E_FAIL)
+        {
+            hr = S_OK;
+        }
+#endif // defined(FEATURE_PAL)
+    }
+
+    return hr;
 }
 
 STDAPI BinderAcquireImport(PEImage                  *pPEImage,