Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix exception context leak in GC stress C
The PAL_SEHException had the records allocated on stack, so the
direct context restoration after the EH for GC stress C completed
leaked those.
  • Loading branch information
janvorli committed Jan 20, 2022
commit cc2df9ccf3183ab76fda534822660001a3018853
22 changes: 17 additions & 5 deletions src/coreclr/pal/src/exception/machexception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,19 +373,31 @@ void PAL_DispatchException(PCONTEXT pContext, PEXCEPTION_RECORD pExRecord, MachE
g_hardware_exception_context_locvar_offset = (int)((char*)&contextRecord - (char*)__builtin_frame_address(0));

pContext->ContextFlags |= CONTEXT_EXCEPTION_ACTIVE;
bool continueExecution;
{
PAL_SEHException exception(pExRecord, pContext, true);

PAL_SEHException exception(pExRecord, pContext, true);
TRACE("PAL_DispatchException(EC %08x EA %p)\n", pExRecord->ExceptionCode, pExRecord->ExceptionAddress);

TRACE("PAL_DispatchException(EC %08x EA %p)\n", pExRecord->ExceptionCode, pExRecord->ExceptionAddress);
continueExecution = SEHProcessException(&exception);
if (continueExecution)
{
// Make a copy of the exception records so that we can free them before restoring the context
*pContext = *exception.ExceptionPointers.ContextRecord;
*pExRecord = *exception.ExceptionPointers.ExceptionRecord;
}

// The exception records are destroyed by the PAL_SEHException destructor now.
}

if (SEHProcessException(&exception))
if (continueExecution)
{
#if defined(HOST_ARM64)
// RtlRestoreContext assembly corrupts X16 & X17, so it cannot be
// used for GCStress=C restore
MachSetThreadContext(exception.ExceptionPointers.ContextRecord);
MachSetThreadContext(pContext);
#else
RtlRestoreContext(exception.ExceptionPointers.ContextRecord, pExRecord);
RtlRestoreContext(pContext, pExRecord);
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion src/tests/Regressions/coreclr/GitHub_62058/test62058.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static void CatchRethrow(Action action)
{
Console.Out.WriteLine("catch");
Console.Out.Flush();
throw new Exception("catch", e); // throw; doesn't work either
throw new Exception("catch", e);
}
}

Expand Down