From 76d2d2dd465ab9beb50786793876608279983885 Mon Sep 17 00:00:00 2001 From: Woongsuk Cho Date: Wed, 19 Dec 2018 08:50:43 +0900 Subject: [PATCH] Add an error log to determine where to throw BadImageFormatException Change-Id: I0231e9c5209ce39acfe37c54cfcda934b4f773db --- src/binder/assembly.cpp | 1 + src/binder/assemblybinder.cpp | 5 +++ src/binder/clrprivbinderassemblyloadcontext.cpp | 1 + src/binder/clrprivbindercoreclr.cpp | 1 + src/utilcode/peinformation.cpp | 4 +++ src/vm/assemblynative.cpp | 1 + src/vm/clrex.cpp | 3 ++ src/vm/dllimport.cpp | 1 + src/vm/managedmdimport.cpp | 20 +++++++++++ src/vm/nativeformatreader.h | 45 +++++++++++++++++++++++++ src/vm/pefile.cpp | 1 + src/vm/runtimehandles.cpp | 3 ++ src/vm/typedesc.cpp | 1 + 13 files changed, 87 insertions(+) diff --git a/src/binder/assembly.cpp b/src/binder/assembly.cpp index 8fcc7cf..a275c85 100644 --- a/src/binder/assembly.cpp +++ b/src/binder/assembly.cpp @@ -191,6 +191,7 @@ Exit: if (!fInspectionOnly && !IsValidArchitecture(kAssemblyArchitecture)) { // Assembly image can't be executed on this platform + fprintf(stderr, "@@[SR] %s:%d, fInspectionOnly(%d), IsValidArchitecture(%d)\n", __FILE__, __LINE__, fInspectionOnly, IsValidArchitecture(kAssemblyArchitecture)); IF_FAIL_GO(HRESULT_FROM_WIN32(ERROR_BAD_FORMAT)); } diff --git a/src/binder/assemblybinder.cpp b/src/binder/assemblybinder.cpp index 7c0aa80..89f4a06 100644 --- a/src/binder/assemblybinder.cpp +++ b/src/binder/assemblybinder.cpp @@ -517,6 +517,7 @@ namespace BINDER_SPACE { // Not a PE. Shouldn't ever get here. BINDER_LOG(W("Not a PE!")); + fprintf(stderr, "@@[SR] %s:%d, Not a PE\n", __FILE__, __LINE__); IF_FAIL_GO(HRESULT_FROM_WIN32(ERROR_BAD_FORMAT)); } else @@ -535,6 +536,7 @@ namespace BINDER_SPACE { // Invalid BINDER_LOG(W("CLRPeKind & pe32BitRequired is true")); + fprintf(stderr, "@@[SR] %s:%d, Not a PE\n", __FILE__, __LINE__); IF_FAIL_GO(HRESULT_FROM_WIN32(ERROR_BAD_FORMAT)); } @@ -548,6 +550,7 @@ namespace BINDER_SPACE { // We don't support other architectures BINDER_LOG(W("Unknown architecture")); + fprintf(stderr, "@@[SR] %s:%d, Not a PE\n", __FILE__, __LINE__); IF_FAIL_GO(HRESULT_FROM_WIN32(ERROR_BAD_FORMAT)); } } @@ -562,6 +565,7 @@ namespace BINDER_SPACE { // Not supported BINDER_LOG(W("32-bit, non-agnostic")); + fprintf(stderr, "@@[SR] %s:%d, Not a PE\n", __FILE__, __LINE__); IF_FAIL_GO(HRESULT_FROM_WIN32(ERROR_BAD_FORMAT)); } } @@ -852,6 +856,7 @@ namespace BINDER_SPACE // Validate architecture if (!fInspectionOnly && !Assembly::IsValidArchitecture(pAssemblyName->GetArchitecture())) { + fprintf(stderr, "@@[SR] %s:%d, Not a PE\n", __FILE__, __LINE__); IF_FAIL_GO(HRESULT_FROM_WIN32(ERROR_BAD_FORMAT)); } diff --git a/src/binder/clrprivbinderassemblyloadcontext.cpp b/src/binder/clrprivbinderassemblyloadcontext.cpp index e6f957a..c6f729d 100644 --- a/src/binder/clrprivbinderassemblyloadcontext.cpp +++ b/src/binder/clrprivbinderassemblyloadcontext.cpp @@ -145,6 +145,7 @@ HRESULT CLRPrivBinderAssemblyLoadContext::BindUsingPEImage( /* in */ PEImage *pP // Validate architecture if (!BINDER_SPACE::Assembly::IsValidArchitecture(pAssemblyName->GetArchitecture())) { + fprintf(stderr, "@@[SR] %s:%d\n"); IF_FAIL_GO(HRESULT_FROM_WIN32(ERROR_BAD_FORMAT)); } diff --git a/src/binder/clrprivbindercoreclr.cpp b/src/binder/clrprivbindercoreclr.cpp index d756454..2c1b9bd 100644 --- a/src/binder/clrprivbindercoreclr.cpp +++ b/src/binder/clrprivbindercoreclr.cpp @@ -136,6 +136,7 @@ HRESULT CLRPrivBinderCoreCLR::BindUsingPEImage( /* in */ PEImage *pPEImage, // Validate architecture if (!BINDER_SPACE::Assembly::IsValidArchitecture(pAssemblyName->GetArchitecture())) { + fprintf(stderr, "@@[SR] %s:%d\n"); IF_FAIL_GO(HRESULT_FROM_WIN32(ERROR_BAD_FORMAT)); } diff --git a/src/utilcode/peinformation.cpp b/src/utilcode/peinformation.cpp index 20a9fe1..de7bbc6 100644 --- a/src/utilcode/peinformation.cpp +++ b/src/utilcode/peinformation.cpp @@ -26,6 +26,7 @@ HRESULT TranslatePEToArchitectureType(CorPEKind CLRPeKind, DWORD dwImageType, DW if (CLRPeKind == peNot) { // Not a PE. Shouldn't ever get here. *pPeKind = peInvalid; + fprintf(stderr, "@@[SR] %s:%d\n"); hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT); goto Exit; } @@ -51,6 +52,7 @@ HRESULT TranslatePEToArchitectureType(CorPEKind CLRPeKind, DWORD dwImageType, DW if (CLRPeKind & pe32BitRequired) { *pPeKind = peInvalid; + fprintf(stderr, "@@[SR] %s:%d\n"); hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT); goto Exit; } @@ -69,6 +71,7 @@ HRESULT TranslatePEToArchitectureType(CorPEKind CLRPeKind, DWORD dwImageType, DW else { // We don't support other architectures *pPeKind = peInvalid; + fprintf(stderr, "@@[SR] %s:%d\n"); hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT); goto Exit; } @@ -88,6 +91,7 @@ HRESULT TranslatePEToArchitectureType(CorPEKind CLRPeKind, DWORD dwImageType, DW else { // Not supported *pPeKind = peInvalid; + fprintf(stderr, "@@[SR] %s:%d\n"); hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT); goto Exit; } diff --git a/src/vm/assemblynative.cpp b/src/vm/assemblynative.cpp index f0cfe93..5929541 100644 --- a/src/vm/assemblynative.cpp +++ b/src/vm/assemblynative.cpp @@ -233,6 +233,7 @@ Assembly* AssemblyNative::LoadFromPEImage(ICLRPrivBinder* pBinderContext, PEImag StackSString name; spec.GetFileOrDisplayName(0, name); + fprintf(stderr, "@@[SR] %s:%d\n"); COMPlusThrowHR(COR_E_FILELOAD, dwMessageID, name); } diff --git a/src/vm/clrex.cpp b/src/vm/clrex.cpp index 930ac5d..a67dbef 100644 --- a/src/vm/clrex.cpp +++ b/src/vm/clrex.cpp @@ -1837,7 +1837,10 @@ RuntimeExceptionKind EEFileLoadException::GetFileLoadKind(HRESULT hr) (hr == COR_E_LOADING_REFERENCE_ASSEMBLY) || (hr == META_E_BAD_SIGNATURE) || (hr == COR_E_LOADING_WINMD_REFERENCE_ASSEMBLY)) + { + fprintf(stderr, "@@[SR] %s:%d, exception number(%ld)\n", __FILE__, __LINE__, hr); return kBadImageFormatException; + } else { if ((hr == E_OUTOFMEMORY) || (hr == NTE_NO_MEMORY)) diff --git a/src/vm/dllimport.cpp b/src/vm/dllimport.cpp index d019631..7cfb5cb 100644 --- a/src/vm/dllimport.cpp +++ b/src/vm/dllimport.cpp @@ -5786,6 +5786,7 @@ public: HRESULT theHRESULT = GetHR(); if (theHRESULT == HRESULT_FROM_WIN32(ERROR_BAD_EXE_FORMAT)) { + fprintf(stderr, "@@[SR] %s:%d, exception number(%ld)\n", __FILE__, __LINE__, theHRESULT); COMPlusThrow(kBadImageFormatException); } else diff --git a/src/vm/managedmdimport.cpp b/src/vm/managedmdimport.cpp index e780275..0bc5ea2 100644 --- a/src/vm/managedmdimport.cpp +++ b/src/vm/managedmdimport.cpp @@ -127,6 +127,7 @@ ErrExit: if (FAILED(hr)) { + fprintf(stderr, "@@[SR] %s:%d, exception number(%ld)\n", __FILE__, __LINE__, hr); FCThrow(kBadImageFormatException); } @@ -149,6 +150,7 @@ ErrExit: if (FAILED(hr)) { + fprintf(stderr, "@@[SR] %s:%d, exception number(%ld)\n", __FILE__, __LINE__, hr); FCThrowVoid(kBadImageFormatException); } } @@ -280,6 +282,7 @@ ErrExit: if (FAILED(hr)) { + fprintf(stderr, "@@[SR] %s:%d, exception number(%ld)\n", __FILE__, __LINE__, hr); FCThrowVoid(kBadImageFormatException); } } @@ -318,6 +321,7 @@ ErrExit: if (FAILED(hr)) { + fprintf(stderr, "@@[SR] %s:%d, exception number(%ld)\n", __FILE__, __LINE__, hr); FCThrow(kBadImageFormatException); } FC_RETURN_BOOL(retVal); @@ -338,6 +342,7 @@ MDImpl3(void, MetaDataImport::GetUserString, mdToken tk, LPCSTR* pszName, ULONG* if (FAILED(hr)) { + fprintf(stderr, "@@[SR] %s:%d, exception number(%ld)\n", __FILE__, __LINE__, hr); FCThrowVoid(kBadImageFormatException); } } @@ -390,6 +395,7 @@ MDImpl2(void, MetaDataImport::GetName, mdToken tk, LPCSTR* pszName) if (FAILED(hr)) { + fprintf(stderr, "@@[SR] %s:%d, exception number(%ld)\n", __FILE__, __LINE__, hr); FCThrowVoid(kBadImageFormatException); } } @@ -409,6 +415,7 @@ MDImpl2(void, MetaDataImport::GetNamespace, mdToken tk, LPCSTR* pszName) if (FAILED(hr)) { + fprintf(stderr, "@@[SR] %s:%d, exception number(%ld)\n", __FILE__, __LINE__, hr); FCThrowVoid(kBadImageFormatException); } } @@ -428,6 +435,7 @@ MDImpl2(void, MetaDataImport::GetGenericParamProps, mdToken tk, DWORD* pAttribut if (FAILED(hr)) { + fprintf(stderr, "@@[SR] %s:%d, exception number(%ld)\n", __FILE__, __LINE__, hr); FCThrowVoid(kBadImageFormatException); } } @@ -446,6 +454,7 @@ MDImpl3(void, MetaDataImport::GetEventProps, mdToken tk, LPCSTR* pszName, INT32 if (FAILED(hr)) { + fprintf(stderr, "@@[SR] %s:%d, exception number(%ld)\n", __FILE__, __LINE__, hr); FCThrowVoid(kBadImageFormatException); } } @@ -476,6 +485,7 @@ MDImpl4(void, MetaDataImport::GetPinvokeMap, mdToken tk, DWORD* pMappingFlags, L if (FAILED(hr)) { + fprintf(stderr, "@@[SR] %s:%d, exception number(%ld)\n", __FILE__, __LINE__, hr); FCThrowVoid(kBadImageFormatException); } } @@ -507,6 +517,7 @@ MDImpl3(void, MetaDataImport::GetParamDefProps, mdToken tk, INT32* pSequence, IN if (FAILED(hr)) { + fprintf(stderr, "@@[SR] %s:%d, exception number(%ld)\n", __FILE__, __LINE__, hr); FCThrowVoid(kBadImageFormatException); } } @@ -525,6 +536,7 @@ MDImpl2(void, MetaDataImport::GetFieldDefProps, mdToken tk, INT32 *pdwFieldFlags if (FAILED(hr)) { + fprintf(stderr, "@@[SR] %s:%d, exception number(%ld)\n", __FILE__, __LINE__, hr); FCThrowVoid(kBadImageFormatException); } } @@ -543,6 +555,7 @@ MDImpl4(void, MetaDataImport::GetPropertyProps, mdToken tk, LPCSTR* pszName, INT if (FAILED(hr)) { + fprintf(stderr, "@@[SR] %s:%d, exception number(%ld)\n", __FILE__, __LINE__, hr); FCThrowVoid(kBadImageFormatException); } } @@ -567,6 +580,7 @@ MDImpl2(void, MetaDataImport::GetFieldMarshal, mdToken tk, ConstArray* ppValue) if (FAILED(hr)) { + fprintf(stderr, "@@[SR] %s:%d, exception number(%ld)\n", __FILE__, __LINE__, hr); FCThrowVoid(kBadImageFormatException); } } @@ -585,6 +599,7 @@ MDImpl2(void, MetaDataImport::GetSigOfMethodDef, mdToken tk, ConstArray* ppValue if (FAILED(hr)) { + fprintf(stderr, "@@[SR] %s:%d, exception number(%ld)\n", __FILE__, __LINE__, hr); FCThrowVoid(kBadImageFormatException); } } @@ -603,6 +618,7 @@ MDImpl2(void, MetaDataImport::GetSignatureFromToken, mdToken tk, ConstArray* ppV if (FAILED(hr)) { + fprintf(stderr, "@@[SR] %s:%d, exception number(%ld)\n", __FILE__, __LINE__, hr); FCThrowVoid(kBadImageFormatException); } } @@ -621,6 +637,7 @@ MDImpl2(void, MetaDataImport::GetSigOfFieldDef, mdToken tk, ConstArray* ppValue) if (FAILED(hr)) { + fprintf(stderr, "@@[SR] %s:%d, exception number(%ld)\n", __FILE__, __LINE__, hr); FCThrowVoid(kBadImageFormatException); } } @@ -670,6 +687,7 @@ MDImpl2(void, MetaDataImport::GetParentToken, mdToken tk, mdToken* ptk) if (FAILED(hr)) { + fprintf(stderr, "@@[SR] %s:%d, exception number(%ld)\n", __FILE__, __LINE__, hr); FCThrowVoid(kBadImageFormatException); } } @@ -689,6 +707,7 @@ MDImpl1(void, MetaDataImport::GetScopeProps, GUID* pmvid) if (FAILED(hr)) { + fprintf(stderr, "@@[SR] %s:%d, exception number(%ld)\n", __FILE__, __LINE__, hr); FCThrowVoid(kBadImageFormatException); } } @@ -711,6 +730,7 @@ MDImpl2(void, MetaDataImport::GetMemberRefProps, if (FAILED(hr)) { + fprintf(stderr, "@@[SR] %s:%d, exception number(%ld)\n", __FILE__, __LINE__, hr); FCThrowVoid(kBadImageFormatException); } } diff --git a/src/vm/nativeformatreader.h b/src/vm/nativeformatreader.h index 4182b03..fdeadfe 100644 --- a/src/vm/nativeformatreader.h +++ b/src/vm/nativeformatreader.h @@ -54,35 +54,50 @@ namespace NativeFormat uint EnsureOffsetInRange(uint offset, uint lookAhead) { if ((int)offset < 0 || offset + lookAhead >= _size) + { + fprintf(stderr, "@@[SR] %s:%d\n", __FILE__, __LINE__); ThrowBadImageFormatException(); + } return offset; } byte ReadUInt8(uint offset) { if (offset >= _size) + { + fprintf(stderr, "@@[SR] %s:%d\n", __FILE__, __LINE__); ThrowBadImageFormatException(); + } return *(_base + offset); // Assumes little endian and unaligned access } UInt16 ReadUInt16(uint offset) { if ((int)offset < 0 || offset + 1 >= _size) + { + fprintf(stderr, "@@[SR] %s:%d\n", __FILE__, __LINE__); ThrowBadImageFormatException(); + } return *dac_cast(_base + offset); // Assumes little endian and unaligned access } UInt32 ReadUInt32(uint offset) { if ((int)offset < 0 || offset + 3 >= _size) + { + fprintf(stderr, "@@[SR] %s:%d\n", __FILE__, __LINE__); ThrowBadImageFormatException(); + } return *dac_cast(_base + offset); // Assumes little endian and unaligned access } uint DecodeUnsigned(uint offset, uint * pValue) { if (offset >= _size) + { + fprintf(stderr, "@@[SR] %s:%d\n", __FILE__, __LINE__); ThrowBadImageFormatException(); + } uint val = *(_base + offset); if ((val & 1) == 0) @@ -94,7 +109,10 @@ namespace NativeFormat if ((val & 2) == 0) { if (offset + 1 >= _size) + { + fprintf(stderr, "@@[SR] %s:%d\n", __FILE__, __LINE__); ThrowBadImageFormatException(); + } *pValue = (val >> 2) | (((uint)*(_base + offset + 1)) << 6); offset += 2; @@ -103,7 +121,10 @@ namespace NativeFormat if ((val & 4) == 0) { if (offset + 2 >= _size) + { + fprintf(stderr, "@@[SR] %s:%d\n", __FILE__, __LINE__); ThrowBadImageFormatException(); + } *pValue = (val >> 3) | (((uint)*(_base + offset + 1)) << 5) | (((uint)*(_base + offset + 2)) << 13); @@ -113,7 +134,10 @@ namespace NativeFormat if ((val & 8) == 0) { if (offset + 3 >= _size) + { + fprintf(stderr, "@@[SR] %s:%d\n", __FILE__, __LINE__); ThrowBadImageFormatException(); + } *pValue = (val >> 4) | (((uint)*(_base + offset + 1)) << 4) | (((uint)*(_base + offset + 2)) << 12) | @@ -128,6 +152,7 @@ namespace NativeFormat } else { + fprintf(stderr, "@@[SR] %s:%d\n", __FILE__, __LINE__); ThrowBadImageFormatException(); } @@ -137,7 +162,10 @@ namespace NativeFormat int DecodeSigned(uint offset, int * pValue) { if (offset >= _size) + { + fprintf(stderr, "@@[SR] %s:%d\n", __FILE__, __LINE__); ThrowBadImageFormatException(); + } int val = *(_base + offset); if ((val & 1) == 0) @@ -148,7 +176,10 @@ namespace NativeFormat else if ((val & 2) == 0) { if (offset + 1 >= _size) + { + fprintf(stderr, "@@[SR] %s:%d\n", __FILE__, __LINE__); ThrowBadImageFormatException(); + } *pValue = (val >> 2) | (((int)*(_base + offset + 1)) << 6); offset += 2; @@ -156,7 +187,10 @@ namespace NativeFormat else if ((val & 4) == 0) { if (offset + 2 >= _size) + { + fprintf(stderr, "@@[SR] %s:%d\n", __FILE__, __LINE__); ThrowBadImageFormatException(); + } *pValue = (val >> 3) | (((int)*(_base + offset + 1)) << 5) | (((int)*(_base + offset + 2)) << 13); @@ -165,7 +199,10 @@ namespace NativeFormat else if ((val & 8) == 0) { if (offset + 3 >= _size) + { + fprintf(stderr, "@@[SR] %s:%d\n", __FILE__, __LINE__); ThrowBadImageFormatException(); + } *pValue = (val >> 4) | (((int)*(_base + offset + 1)) << 4) | (((int)*(_base + offset + 2)) << 12) | @@ -179,6 +216,7 @@ namespace NativeFormat } else { + fprintf(stderr, "@@[SR] %s:%d\n", __FILE__, __LINE__); ThrowBadImageFormatException(); } @@ -220,6 +258,7 @@ namespace NativeFormat } else { + fprintf(stderr, "@@[SR] %s:%d\n", __FILE__, __LINE__); ThrowBadImageFormatException(); return offset; } @@ -450,12 +489,18 @@ namespace NativeFormat int numberOfBucketsShift = (int)(header >> 2); if (numberOfBucketsShift > 31) + { + fprintf(stderr, "@@[SR] %s:%d\n", __FILE__, __LINE__); _pReader->ThrowBadImageFormatException(); + } _bucketMask = (uint)((1 << numberOfBucketsShift) - 1); byte entryIndexSize = (byte)(header & 3); if (entryIndexSize > 2) + { + fprintf(stderr, "@@[SR] %s:%d\n", __FILE__, __LINE__); _pReader->ThrowBadImageFormatException(); + } _entryIndexSize = entryIndexSize; } diff --git a/src/vm/pefile.cpp b/src/vm/pefile.cpp index 77c99f6..a3281c9 100644 --- a/src/vm/pefile.cpp +++ b/src/vm/pefile.cpp @@ -250,6 +250,7 @@ static void ValidatePEFileMachineType(PEFile *peFile) else name = StackSString(SString::Utf8, peFile->GetSimpleName()); + fprintf(stderr, "@@[SR] %s:%d, name(%s)\n", __FILE__, __LINE__, name.GetUnicode()); COMPlusThrow(kBadImageFormatException, IDS_CLASSLOAD_WRONGCPU, name.GetUnicode()); } diff --git a/src/vm/runtimehandles.cpp b/src/vm/runtimehandles.cpp index de9505a..e393c14 100644 --- a/src/vm/runtimehandles.cpp +++ b/src/vm/runtimehandles.cpp @@ -1339,6 +1339,7 @@ FCIMPL1(ReflectClassBaseObject*, RuntimeTypeHandle::GetDeclaringType, ReflectCla if (FAILED(typeHandle.GetModule()->GetMDImport()->GetNestedClassProps(tkTypeDef, &tkTypeDef))) { fThrowException = TRUE; + fprintf(stderr, "@@[SR] %s:%d\n"); reKind = kBadImageFormatException; argName = NULL; goto Exit; @@ -2664,6 +2665,7 @@ FCIMPL1(LPCUTF8, RuntimeFieldHandle::GetUtf8Name, FieldDesc *pField) { if (FAILED(pField->GetName_NoThrow(&szFieldName))) { + fprintf(stderr, "@@[SR] %s:%d\n"); FCThrow(kBadImageFormatException); } return szFieldName; @@ -2813,6 +2815,7 @@ FCIMPL1(INT32, AssemblyHandle::GetToken, AssemblyBaseObject* pAssemblyUNSAFE) { { if (FAILED(mdImport->GetAssemblyFromScope(&token))) { + fprintf(stderr, "@@[SR] %s:%d\n"); FCThrow(kBadImageFormatException); } } diff --git a/src/vm/typedesc.cpp b/src/vm/typedesc.cpp index 7da1c84..9b54e02 100644 --- a/src/vm/typedesc.cpp +++ b/src/vm/typedesc.cpp @@ -1782,6 +1782,7 @@ LoadConstraintOnOpenType: default: { + fprintf(stderr, "@@[SR] %s:%d\n"); COMPlusThrow(kBadImageFormatException); } } -- 2.7.4