Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 5bfb741

Browse files
authored
Fix x86 dumps from HandleFatalError showing misleading callstack (#23289)
1 parent fe85174 commit 5bfb741

File tree

6 files changed

+35
-5
lines changed

6 files changed

+35
-5
lines changed

src/vm/crossgencompile.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,10 +363,14 @@ extern "C" UINT_PTR STDCALL GetCurrentIP()
363363
return 0;
364364
}
365365

366-
void EEPolicy::HandleFatalError(UINT exitCode, UINT_PTR address, LPCWSTR pszMessage, PEXCEPTION_POINTERS pExceptionInfo, LPCWSTR errorSource, LPCWSTR argExceptionString)
366+
// This method must return a value to avoid getting non-actionable dumps on x86.
367+
// If this method were a DECLSPEC_NORETURN then dumps would not provide the necessary
368+
// context at the point of the failure
369+
int NOINLINE EEPolicy::HandleFatalError(UINT exitCode, UINT_PTR address, LPCWSTR pszMessage, PEXCEPTION_POINTERS pExceptionInfo, LPCWSTR errorSource, LPCWSTR argExceptionString)
367370
{
368371
fprintf(stderr, "Fatal error: %08x\n", exitCode);
369372
ExitProcess(exitCode);
373+
return -1;
370374
}
371375

372376
//---------------------------------------------------------------------------------------

src/vm/eepolicy.cpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,10 +1194,26 @@ void DECLSPEC_NORETURN EEPolicy::HandleFatalStackOverflow(EXCEPTION_POINTERS *pE
11941194
UNREACHABLE();
11951195
}
11961196

1197+
#if defined(_TARGET_X86_) && defined(PLATFORM_WINDOWS)
1198+
// This noinline method is required to ensure that RtlCaptureContext captures
1199+
// the context of HandleFatalError. On x86 RtlCaptureContext will not capture
1200+
// the current method's context
1201+
// NOTE: explicitly turning off optimizations to force the compiler to spill to the
1202+
// stack and establish a stack frame. This is required to ensure that
1203+
// RtlCaptureContext captures the context of HandleFatalError
1204+
#pragma optimize("", off)
1205+
int NOINLINE WrapperClrCaptureContext(CONTEXT* context)
1206+
{
1207+
ClrCaptureContext(context);
1208+
return 0;
1209+
}
1210+
#pragma optimize("", on)
1211+
#endif // defined(_TARGET_X86_) && defined(PLATFORM_WINDOWS)
11971212

1198-
1199-
1200-
void DECLSPEC_NORETURN EEPolicy::HandleFatalError(UINT exitCode, UINT_PTR address, LPCWSTR pszMessage /* = NULL */, PEXCEPTION_POINTERS pExceptionInfo /* = NULL */, LPCWSTR errorSource /* = NULL */, LPCWSTR argExceptionString /* = NULL */)
1213+
// This method must return a value to avoid getting non-actionable dumps on x86.
1214+
// If this method were a DECLSPEC_NORETURN then dumps would not provide the necessary
1215+
// context at the point of the failure
1216+
int NOINLINE EEPolicy::HandleFatalError(UINT exitCode, UINT_PTR address, LPCWSTR pszMessage /* = NULL */, PEXCEPTION_POINTERS pExceptionInfo /* = NULL */, LPCWSTR errorSource /* = NULL */, LPCWSTR argExceptionString /* = NULL */)
12011217
{
12021218
WRAPPER_NO_CONTRACT;
12031219

@@ -1215,7 +1231,12 @@ void DECLSPEC_NORETURN EEPolicy::HandleFatalError(UINT exitCode, UINT_PTR addres
12151231
ZeroMemory(&context, sizeof(context));
12161232

12171233
context.ContextFlags = CONTEXT_CONTROL;
1234+
#if defined(_TARGET_X86_) && defined(PLATFORM_WINDOWS)
1235+
// Add a frame to ensure that the context captured is this method and not the caller
1236+
WrapperClrCaptureContext(&context);
1237+
#else // defined(_TARGET_X86_) && defined(PLATFORM_WINDOWS)
12181238
ClrCaptureContext(&context);
1239+
#endif
12191240

12201241
exceptionRecord.ExceptionCode = exitCode;
12211242
exceptionRecord.ExceptionAddress = reinterpret_cast< PVOID >(address);
@@ -1269,6 +1290,7 @@ void DECLSPEC_NORETURN EEPolicy::HandleFatalError(UINT exitCode, UINT_PTR addres
12691290
}
12701291

12711292
UNREACHABLE();
1293+
return -1;
12721294
}
12731295

12741296
void EEPolicy::HandleExitProcessFromEscalation(EPolicyAction action, UINT exitCode)

src/vm/eepolicy.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class EEPolicy
120120

121121
static void HandleExitProcess(ShutdownCompleteAction sca = SCA_ExitProcessWhenShutdownComplete);
122122

123-
static void DECLSPEC_NORETURN HandleFatalError(UINT exitCode, UINT_PTR address, LPCWSTR pMessage=NULL, PEXCEPTION_POINTERS pExceptionInfo= NULL, LPCWSTR errorSource=NULL, LPCWSTR argExceptionString=NULL);
123+
static int NOINLINE HandleFatalError(UINT exitCode, UINT_PTR address, LPCWSTR pMessage=NULL, PEXCEPTION_POINTERS pExceptionInfo= NULL, LPCWSTR errorSource=NULL, LPCWSTR argExceptionString=NULL);
124124

125125
static void DECLSPEC_NORETURN HandleFatalStackOverflow(EXCEPTION_POINTERS *pException, BOOL fSkipDebugger = FALSE);
126126

src/vm/encee.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,7 @@ HRESULT EditAndContinueModule::ResumeInUpdatedFunction(
683683
// If we fail for any reason we have already potentially trashed with new locals and we have also unwound any
684684
// Win32 handlers on the stack so cannot ever return from this function.
685685
EEPOLICY_HANDLE_FATAL_ERROR(CORDBG_E_ENC_INTERNAL_ERROR);
686+
return hr;
686687
}
687688

688689
//---------------------------------------------------------------------------------------

src/vm/excep.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2983,6 +2983,7 @@ VOID DECLSPEC_NORETURN RaiseTheExceptionInternalOnly(OBJECTREF throwable, BOOL r
29832983
// User hits 'g'
29842984
// Then debugger can bring us here.
29852985
EEPOLICY_HANDLE_FATAL_ERROR(COR_E_EXECUTIONENGINE);
2986+
UNREACHABLE();
29862987
}
29872988

29882989

@@ -11945,6 +11946,7 @@ void ExceptionNotifications::GetEventArgsForNotification(ExceptionNotificationHa
1194511946
static LONG ExceptionNotificationFilter(PEXCEPTION_POINTERS pExceptionInfo, LPVOID pParam)
1194611947
{
1194711948
EEPOLICY_HANDLE_FATAL_ERROR(COR_E_EXECUTIONENGINE);
11949+
return -1;
1194811950
}
1194911951

1195011952
#ifdef FEATURE_CORRUPTING_EXCEPTIONS

src/vm/exceptionhandling.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4693,6 +4693,7 @@ VOID DECLSPEC_NORETURN UnwindManagedExceptionPass1(PAL_SEHException& ex, CONTEXT
46934693

46944694
_ASSERTE(!"UnwindManagedExceptionPass1: Failed to find a handler. Reached the end of the stack");
46954695
EEPOLICY_HANDLE_FATAL_ERROR(COR_E_EXECUTIONENGINE);
4696+
UNREACHABLE();
46964697
}
46974698

46984699
VOID DECLSPEC_NORETURN DispatchManagedException(PAL_SEHException& ex, bool isHardwareException)

0 commit comments

Comments
 (0)