Skip to content

Commit

Permalink
Fix exception context leak in GC stress C
Browse files Browse the repository at this point in the history
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
1 parent f15b34e commit cc2df9c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
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

0 comments on commit cc2df9c

Please sign in to comment.