Skip to content

Fix IP adjustment for interpreter EH #117055

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions src/coreclr/vm/excep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7360,6 +7360,12 @@ VOID DECLSPEC_NORETURN UnwindAndContinueRethrowHelperAfterCatch(Frame* pEntryFra
}
else
{
#ifdef FEATURE_INTERPRETER
if ((pEntryFrame != FRAME_TOP) && (pEntryFrame->GetFrameIdentifier() == FrameIdentifier::InterpreterFrame))
{
((InterpreterFrame*)pEntryFrame)->SetIsFaulting(true);
}
#endif // FEATURE_INTERPRETER
DispatchManagedException(orThrowable);
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/coreclr/vm/frames.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1894,6 +1894,11 @@ void InterpreterFrame::SetContextToInterpMethodContextFrame(T_CONTEXT * pContext
SetSP(pContext, dac_cast<TADDR>(pFrame));
SetFP(pContext, (TADDR)pFrame->pStack);
SetFirstArgReg(pContext, dac_cast<TADDR>(this));
pContext->ContextFlags = CONTEXT_FULL;
if (m_isFaulting)
{
pContext->ContextFlags |= CONTEXT_EXCEPTION_ACTIVE;
Copy link
Member

@BrzVlad BrzVlad Jun 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does exactly setting this flag end up preventing the ip adjustment ? Seems not obvious from searching in the code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The EH doesn't do adjustment for frames that are marked as faulting. In the JIT/AOT case, these are frames of hardware exceptions where the IP is the IP of the faulting instruction. For the interpreter, the throwing IR opcodes behave the same way. Setting this flag results in setting the m_crawl.isFaulting and m_crawl.isInterrupted to true. The SfiInit and SfiNext perform the IP adjustment only when the isFaulting is not set.
E.g. for the SfiInit, you can see it here:

if (!pThis->m_crawl.HasFaulted() && !pThis->m_crawl.IsIPadjusted())
{
controlPC -= STACKWALK_CONTROLPC_ADJUST_OFFSET;
}

}
}

void InterpreterFrame::UpdateRegDisplay_Impl(const PREGDISPLAY pRD, bool updateFloats)
Expand Down
11 changes: 10 additions & 1 deletion src/coreclr/vm/frames.h
Original file line number Diff line number Diff line change
Expand Up @@ -2417,7 +2417,8 @@ class InterpreterFrame : public FramedMethodFrame
#ifndef DACCESS_COMPILE
InterpreterFrame(TransitionBlock* pTransitionBlock, InterpMethodContextFrame* pContextFrame)
: FramedMethodFrame(FrameIdentifier::InterpreterFrame, pTransitionBlock, NULL),
m_pTopInterpMethodContextFrame(pContextFrame)
m_pTopInterpMethodContextFrame(pContextFrame),
m_isFaulting(false)
#if defined(HOST_AMD64) && defined(HOST_WINDOWS)
, m_SSP(0)
#endif
Expand Down Expand Up @@ -2471,10 +2472,18 @@ class InterpreterFrame : public FramedMethodFrame
}
#endif // HOST_AMD64 && HOST_WINDOWS

void SetIsFaulting(bool isFaulting)
{
LIMITED_METHOD_CONTRACT;
m_isFaulting = isFaulting;
}

private:
// The last known topmost interpreter frame in the InterpExecMethod belonging to
// this InterpreterFrame.
PTR_InterpMethodContextFrame m_pTopInterpMethodContextFrame;
// Set to true to indicate that the topmost interpreted frame has thrown an exception
bool m_isFaulting;
#if defined(HOST_AMD64) && defined(HOST_WINDOWS)
// Saved SSP of the InterpExecMethod for resuming after catch into interpreter frames.
TADDR m_SSP;
Expand Down
4 changes: 4 additions & 0 deletions src/coreclr/vm/interpexec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1737,12 +1737,14 @@ void InterpExecMethod(InterpreterFrame *pInterpreterFrame, InterpMethodContextFr
{
throwable = LOCAL_VAR(ip[1], OBJECTREF);
}
pInterpreterFrame->SetIsFaulting(true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this flag also be set for the other sources of exception throwing in the interp method execution ? Conv ovf opcodes for example

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, these are all handled in the UnwindAndContinueRethrowHelperAfterCatch

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That one is called in the UNINSTALL_UNWIND_AND_CONTINUE_HANDLER macro in the InterpExecMethod.

DispatchManagedException(throwable);
UNREACHABLE();
break;
}
case INTOP_RETHROW:
{
pInterpreterFrame->SetIsFaulting(true);
DispatchRethrownManagedException();
UNREACHABLE();
break;
Expand Down Expand Up @@ -2220,6 +2222,8 @@ do { \
pMethod = pFrame->startIp->Method;
assert(pMethod->CheckIntegrity());
pThreadContext->pStackPointer = pFrame->pStack + pMethod->allocaSize;

pInterpreterFrame->SetIsFaulting(false);
goto MAIN_LOOP;
}

Expand Down
6 changes: 5 additions & 1 deletion src/coreclr/vm/stackwalk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2860,8 +2860,12 @@ void StackFrameIterator::ProcessCurrentFrame(void)
m_interpExecMethodFirstArgReg = (TADDR)GetFirstArgReg(pRD->pCurrentContext);

((PTR_InterpreterFrame)m_crawl.pFrame)->SetContextToInterpMethodContextFrame(pRD->pCurrentContext);
if (pRD->pCurrentContext->ContextFlags & CONTEXT_EXCEPTION_ACTIVE)
{
m_crawl.isInterrupted = true;
m_crawl.hasFaulted = true;
}

pRD->pCurrentContext->ContextFlags = CONTEXT_FULL;
SyncRegDisplayToCurrentContext(pRD);
ProcessIp(GetControlPC(pRD));
m_walkingInterpreterFrames = m_crawl.isFrameless;
Expand Down
Loading