Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of hardware exceptions in native code on Unix #69685

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/coreclr/pal/inc/pal.h
Original file line number Diff line number Diff line change
Expand Up @@ -4523,6 +4523,7 @@ struct PAL_SEHException
ExceptionPointers.ContextRecord = ex.ExceptionPointers.ContextRecord;
TargetFrameSp = ex.TargetFrameSp;
RecordsOnStack = ex.RecordsOnStack;
IsExternal = ex.IsExternal;
ManagedToNativeExceptionCallback = ex.ManagedToNativeExceptionCallback;
ManagedToNativeExceptionCallbackContext = ex.ManagedToNativeExceptionCallbackContext;

Expand All @@ -4544,6 +4545,9 @@ struct PAL_SEHException
// Target frame stack pointer set before the 2nd pass.
SIZE_T TargetFrameSp;
bool RecordsOnStack;
// The exception is a hardware exception coming from a native code out of
// the well known runtime helpers
bool IsExternal;

void(*ManagedToNativeExceptionCallback)(void* context);
void* ManagedToNativeExceptionCallbackContext;
Expand All @@ -4554,6 +4558,7 @@ struct PAL_SEHException
ExceptionPointers.ContextRecord = pContextRecord;
TargetFrameSp = NoTargetFrameSp;
RecordsOnStack = onStack;
IsExternal = false;
ManagedToNativeExceptionCallback = NULL;
ManagedToNativeExceptionCallbackContext = NULL;
}
Expand Down Expand Up @@ -4592,6 +4597,7 @@ struct PAL_SEHException
ExceptionPointers.ContextRecord = NULL;
TargetFrameSp = NoTargetFrameSp;
RecordsOnStack = false;
IsExternal = false;
ManagedToNativeExceptionCallback = NULL;
ManagedToNativeExceptionCallbackContext = NULL;
}
Expand Down Expand Up @@ -4898,6 +4904,7 @@ class NativeExceptionHolderFactory
{ \
try \
{ \
HardwareExceptionHolder \
tryBlock(__param); \
} \
catch (...) \
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/pal/src/exception/seh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ SEHProcessException(PAL_SEHException* exception)
if (CatchHardwareExceptionHolder::IsEnabled())
{
EnsureExceptionRecordsOnHeap(exception);
exception->IsExternal = true;
PAL_ThrowExceptionFromContext(exception->GetContextRecord(), exception);
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/coreclr/vm/exceptionhandling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4569,7 +4569,13 @@ VOID DECLSPEC_NORETURN UnwindManagedExceptionPass1(PAL_SEHException& ex, CONTEXT
// This is the first time we see the managed exception, set its context to the managed frame that has caused
// the exception to be thrown
*ex.GetContextRecord() = *frameContext;
ex.GetExceptionRecord()->ExceptionAddress = (VOID*)controlPc;

// Move the exception address to the first managed frame on the stack except for the hardware exceptions
// stemming from from a native code out of the well known runtime helpers
if (!ex.IsExternal)
{
ex.GetExceptionRecord()->ExceptionAddress = (VOID*)controlPc;
}
}

ex.GetExceptionRecord()->ExceptionFlags = 0;
Expand Down