Skip to content

Commit

Permalink
Correctly identify when an unmanaged exception hijack is occurring an…
Browse files Browse the repository at this point in the history
…d display unmanaged frames in callstack (#105536)
  • Loading branch information
tommcdon authored and pull[bot] committed Sep 20, 2024
1 parent a7ba43a commit 8065507
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
34 changes: 34 additions & 0 deletions src/coreclr/debug/di/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13503,6 +13503,40 @@ bool CordbProcess::IsSpecialStackOverflowCase(CordbUnmanagedThread *pUThread, co
return true;
}

#ifdef FEATURE_INTEROP_DEBUGGING
bool CordbProcess::IsUnmanagedThreadHijacked(ICorDebugThread * pICorDebugThread)
{
PUBLIC_REENTRANT_API_ENTRY_FOR_SHIM(this);

if (GetShim() == NULL || !IsInteropDebugging())
{
return false;
}

{
RSLockHolder lockHolder(GetProcessLock());
CordbThread * pCordbThread = static_cast<CordbThread *> (pICorDebugThread);
HRESULT hr = pCordbThread->EnsureThreadIsAlive();
if (FAILED(hr))
{
return false;
}

// And only if we have a CordbUnmanagedThread and we are hijacked to code:Debugger::GenericHijackFunc
CordbUnmanagedThread * pUT = GetUnmanagedThread(pCordbThread->GetVolatileOSThreadID());
if (pUT != NULL)
{
if (pUT->IsFirstChanceHijacked() || pUT->IsGenericHijacked())
{
return true;
}
}
}
return false;
}
#endif // FEATURE_INTEROP_DEBUGGING


//-----------------------------------------------------------------------------
// Longhorn broke ContinueDebugEvent.
// In previous OS releases, DBG_CONTINUE would continue a non-continuable exception.
Expand Down
6 changes: 6 additions & 0 deletions src/coreclr/debug/di/rspriv.h
Original file line number Diff line number Diff line change
Expand Up @@ -2879,6 +2879,10 @@ class IProcessShimHooks
virtual void RequestSyncAtEvent()= 0;

virtual bool IsThreadSuspendedOrHijacked(ICorDebugThread * pThread) = 0;

#ifdef FEATURE_INTEROP_DEBUGGING
virtual bool IsUnmanagedThreadHijacked(ICorDebugThread * pICorDebugThread) = 0;
#endif
};


Expand Down Expand Up @@ -3462,6 +3466,8 @@ class CordbProcess :
_ASSERTE(ThreadHoldsProcessLock());
return m_unmanagedThreads.GetBase(dwThreadId);
}

virtual bool IsUnmanagedThreadHijacked(ICorDebugThread * pICorDebugThread);
#endif // FEATURE_INTEROP_DEBUGGING

/*
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/debug/di/shimpriv.h
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,8 @@ class ShimProcess

bool IsThreadSuspendedOrHijacked(ICorDebugThread * pThread);

bool IsUnmanagedThreadHijacked(ICorDebugThread * pThread);

// Expose m_attached to CordbProcess.
bool GetAttached();

Expand Down
9 changes: 9 additions & 0 deletions src/coreclr/debug/di/shimprocess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1654,3 +1654,12 @@ bool ShimProcess::IsThreadSuspendedOrHijacked(ICorDebugThread * pThread)
{
return m_pProcess->IsThreadSuspendedOrHijacked(pThread);
}

bool ShimProcess::IsUnmanagedThreadHijacked(ICorDebugThread * pThread)
{
#ifdef FEATURE_INTEROP_DEBUGGING
return m_pProcess->IsUnmanagedThreadHijacked(pThread);
#else
return false;
#endif
}
3 changes: 2 additions & 1 deletion src/coreclr/debug/di/shimstackwalk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ BOOL ShimStackWalk::ShouldTrackUMChain(StackWalkInfo * pswInfo)
// returning false above. We need to check the exception state to make sure we don't
// track the chain in this case. Since we know the type of Frame we are dealing with,
// we can make a more accurate determination of whether we should track the chain.
if (GetInternalFrameType(pswInfo->GetCurrentInternalFrame()) == STUBFRAME_EXCEPTION)
// However if we are interop debugging and the thread is hijacked, we should track the chain.
if (!m_pProcess->IsUnmanagedThreadHijacked(m_pThread) && GetInternalFrameType(pswInfo->GetCurrentInternalFrame()) == STUBFRAME_EXCEPTION)
return FALSE;

return TRUE;
Expand Down

0 comments on commit 8065507

Please sign in to comment.