From 0afaf64d63d27ea5eaa3878b2e638d0c62028195 Mon Sep 17 00:00:00 2001 From: Gaurav Khanna Date: Wed, 26 Apr 2017 16:53:58 -0700 Subject: [PATCH] Fix filename display in exception messages (#11238) --- src/mscorlib/src/System/IO/FileLoadException.CoreCLR.cs | 5 ++++- src/mscorlib/src/System/__HResults.cs | 1 + src/vm/clrex.cpp | 13 +------------ src/vm/peimage.cpp | 6 ++++++ src/vm/peimagelayout.cpp | 13 ++++++++++++- 5 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/mscorlib/src/System/IO/FileLoadException.CoreCLR.cs b/src/mscorlib/src/System/IO/FileLoadException.CoreCLR.cs index f641567..d4ce9a9 100644 --- a/src/mscorlib/src/System/IO/FileLoadException.CoreCLR.cs +++ b/src/mscorlib/src/System/IO/FileLoadException.CoreCLR.cs @@ -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); } diff --git a/src/mscorlib/src/System/__HResults.cs b/src/mscorlib/src/System/__HResults.cs index e4183f6..0592d81 100644 --- a/src/mscorlib/src/System/__HResults.cs +++ b/src/mscorlib/src/System/__HResults.cs @@ -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); diff --git a/src/vm/clrex.cpp b/src/vm/clrex.cpp index 1c1501e..ba040b7 100644 --- a/src/vm/clrex.cpp +++ b/src/vm/clrex.cpp @@ -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 */ diff --git a/src/vm/peimage.cpp b/src/vm/peimage.cpp index 39b71ff..3367ef9 100644 --- a/src/vm/peimage.cpp +++ b/src/vm/peimage.cpp @@ -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; } diff --git a/src/vm/peimagelayout.cpp b/src/vm/peimagelayout.cpp index 2416681..34ba4d8 100644 --- a/src/vm/peimagelayout.cpp +++ b/src/vm/peimagelayout.cpp @@ -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 -- 2.7.4