Skip to content

Commit

Permalink
Fix VS issue with unhandled exception on secondary threads (#103425)
Browse files Browse the repository at this point in the history
* Fix VS issue with unhandled exception on secondary threads

When "just my code" is disabled, unhandled exceptions on secondary
threads cause the VS to stop without any stack trace shown. This is
similar to the recently fixed problem that was happening with user
unhandled exception treatment.
The exception is rethrown as native exception after leaving the last
managed frame and it propagates to the `ManagedThreadBase_DispatchOuter`
where it is caught. The debugger gets notified from there, but all of
the managed stack frames are gone at that point, so the debugger cannot
show them.
The fix is to report exception on a secondary thread as unhandled
earlier, right in the SfiNext where we report it for the primary
threads. The secondary thread is different in having the
DebuggerU2MCatchHandlerFrame on stack while in the primary thread case,
there is no explicit frame.

Close #103385

* Modify the check for DebuggerU2MCatchHandlerFrame

We need to check that it is also the topmost frame

---------

Co-authored-by: Jan Kotas <[email protected]>
  • Loading branch information
janvorli and jkotas authored Jun 15, 2024
1 parent ae71602 commit 69a475a
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/coreclr/vm/exceptionhandling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8213,6 +8213,11 @@ void FailFastIfCorruptingStateException(ExInfo *pExInfo)
}
}

static bool IsTopmostDebuggerU2MCatchHandlerFrame(Frame *pFrame)
{
return (pFrame->GetVTablePtr() == DebuggerU2MCatchHandlerFrame::GetMethodFrameVPtr()) && (pFrame->PtrNextFrame() == FRAME_TOP);
}

static void NotifyExceptionPassStarted(StackFrameIterator *pThis, Thread *pThread, ExInfo *pExInfo)
{
if (pExInfo->m_passNumber == 1)
Expand Down Expand Up @@ -8289,7 +8294,7 @@ static void NotifyExceptionPassStarted(StackFrameIterator *pThis, Thread *pThrea
pFrame = pFrame->PtrNextFrame();
_ASSERTE(pFrame != FRAME_TOP);
}
if ((pFrame->GetVTablePtr() == FuncEvalFrame::GetMethodFrameVPtr()) || (pFrame->GetVTablePtr() == DebuggerU2MCatchHandlerFrame::GetMethodFrameVPtr()))
if ((pFrame->GetVTablePtr() == FuncEvalFrame::GetMethodFrameVPtr()) || IsTopmostDebuggerU2MCatchHandlerFrame(pFrame))
{
EEToDebuggerExceptionInterfaceWrapper::NotifyOfCHFFilter((EXCEPTION_POINTERS *)&pExInfo->m_ptrs, pFrame);
}
Expand Down Expand Up @@ -8518,9 +8523,6 @@ extern "C" bool QCALLTYPE SfiNext(StackFrameIterator* pThis, uint* uExCollideCla
}
else
{
// TODO-NewEH: Currently there is one case of internal VM->managed transitions: COMToCLRDispatchHelperWithStack
// Either add handling here as well or rewrite it in C#, so that CallDescrWorker is the only path that
// needs to be handled here.
size_t CallDescrWorkerInternalReturnAddress = (size_t)CallDescrWorkerInternal + CallDescrWorkerInternalReturnAddressOffset;
if (GetIP(pThis->m_crawl.GetRegisterSet()->pCallerContext) == CallDescrWorkerInternalReturnAddress)
{
Expand All @@ -8544,7 +8546,9 @@ extern "C" bool QCALLTYPE SfiNext(StackFrameIterator* pThis, uint* uExCollideCla
_ASSERTE(retVal != SWA_FAILED);
_ASSERTE(pThis->GetFrameState() != StackFrameIterator::SFITER_SKIPPED_FRAME_FUNCTION);

if (pThis->m_crawl.GetFrame() == FRAME_TOP)
pFrame = pThis->m_crawl.GetFrame();
// Check if there are any further managed frames on the stack, if not, the exception is unhandled.
if ((pFrame == FRAME_TOP) || IsTopmostDebuggerU2MCatchHandlerFrame(pFrame))
{
if (pTopExInfo->m_passNumber == 1)
{
Expand Down

0 comments on commit 69a475a

Please sign in to comment.