Fix filename display in exception messages (#11238)
authorGaurav Khanna <gkhanna@microsoft.com>
Wed, 26 Apr 2017 23:53:58 +0000 (16:53 -0700)
committerGitHub <noreply@github.com>
Wed, 26 Apr 2017 23:53:58 +0000 (16:53 -0700)
src/mscorlib/src/System/IO/FileLoadException.CoreCLR.cs
src/mscorlib/src/System/__HResults.cs
src/vm/clrex.cpp
src/vm/peimage.cpp
src/vm/peimagelayout.cpp

index f641567..d4ce9a9 100644 (file)
@@ -27,7 +27,10 @@ namespace System.IO
             GetFileLoadExceptionMessage(hResult, JitHelpers.GetStringHandleOnStack(ref format));
 
             string message = null;
-            GetMessageForHR(hResult, JitHelpers.GetStringHandleOnStack(ref message));
+            if (hResult == System.__HResults.COR_E_BADEXEFORMAT)
+                message = SR.Arg_BadImageFormatException;
+            else 
+                GetMessageForHR(hResult, JitHelpers.GetStringHandleOnStack(ref message));
 
             return string.Format(CultureInfo.CurrentCulture, format, fileName, message);
         }
index e4183f6..0592d81 100644 (file)
@@ -44,6 +44,7 @@ namespace System
         internal const int COR_E_ARITHMETIC = unchecked((int)0x80070216);
         internal const int COR_E_ARRAYTYPEMISMATCH = unchecked((int)0x80131503);
         internal const int COR_E_BADIMAGEFORMAT = unchecked((int)0x8007000B);
+        internal const int COR_E_BADEXEFORMAT = unchecked((int)0x800700C1);
         internal const int COR_E_TYPEUNLOADED = unchecked((int)0x80131013);
         internal const int COR_E_CANNOTUNLOADAPPDOMAIN = unchecked((int)0x80131015);
         internal const int COR_E_COMEMULATE = unchecked((int)0x80131535);
index 1c1501e..ba040b7 100644 (file)
@@ -2002,18 +2002,7 @@ void DECLSPEC_NORETURN EEFileLoadException::Throw(LPCWSTR path, HRESULT hr, Exce
     if (hr == E_OUTOFMEMORY)
         COMPlusThrowOM();
 
-#ifndef CROSSGEN_COMPILE
-    // Remove path - location must be hidden for security purposes
-
-    LPCWSTR pStart = wcsrchr(path, '\\');
-    if (pStart != NULL)
-        pStart++;
-    else
-        pStart = path;
-#else
-    LPCWSTR pStart = path;
-#endif
-    EX_THROW_WITH_INNER(EEFileLoadException, (StackSString(pStart), hr), pInnerException);
+    EX_THROW_WITH_INNER(EEFileLoadException, (StackSString(path), hr), pInnerException);
 }
 
 /* static */
index 39b71ff..3367ef9 100644 (file)
@@ -1189,7 +1189,13 @@ HANDLE PEImage::GetFileHandle()
     }
 
     if (m_hFile == INVALID_HANDLE_VALUE)
+    {
+#if !defined(DACCESS_COMPILE)
+        EEFileLoadException::Throw(m_path, HRESULT_FROM_WIN32(GetLastError()));
+#else // defined(DACCESS_COMPILE)
         ThrowLastError();
+#endif // !defined(DACCESS_COMPILE)
+    }
 
     return m_hFile;
 }
index 2416681..34ba4d8 100644 (file)
@@ -392,10 +392,21 @@ MappedImageLayout::MappedImageLayout(HANDLE hFile, PEImage* pOwner)
     {
 #ifndef CROSSGEN_COMPILE
 
+        // Capture last error as it may get reset below.
+        
+        DWORD dwLastError = GetLastError();
         // There is no reflection-only load on CoreCLR and so we can always throw an error here.
         // It is important on Windows Phone. All assemblies that we load must have SEC_IMAGE set
         // so that the OS can perform signature verification.
-        ThrowLastError();
+        if (pOwner->IsFile())
+        {
+            EEFileLoadException::Throw(pOwner->GetPathForErrorMessages(), HRESULT_FROM_WIN32(dwLastError));
+        }
+        else
+        {
+            // Throw generic exception.
+            ThrowWin32(dwLastError);
+        }
 
 #endif // CROSSGEN_COMPILE