Skip to content

[Interpreter] Record the next instruction's ip before calling a managed helper - #135464

Merged
BrzVlad merged 4 commits into
dotnet:mainfrom
pcshrosbree:interp-helper-call-ip
Oct 13, 2026
Merged

BrzVlad merged 4 commits into
dotnet:mainfrom
pcshrosbree:interp-helper-call-ip

Conversation

@pcshrosbree

@pcshrosbree pcshrosbree commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #135455.

The native-helper exits of INTOP_CALL_HELPER_* and INTOP_UNBOX_ANY[_GENERIC] left pFrame->ip at the start of the instruction while the helper ran. The helpers that can throw there are managed code, so an exception from one reaches the interpreter frame through managed dispatch: the frame is not marked faulting, and SfiInitWorker/SfiNextWorker treat its ip as a return address and subtract STACKWALK_CONTROLPC_ADJUST_OFFSET. When the helper call is the first instruction of a try region, as the static-base helper is for a static field access on a type with a .cctor, the adjusted ip falls before the region and its handlers do not run.

INTOP_CALL, and the IL-target exits of the same opcodes (ip += N; goto CALL_INTERP_METHOD), already record the next instruction's ip. The native helper exits now do the same: they advance ip and store it before the helper call.

The pFrame->ip = ip at the top of each case is gone where only GetPossiblyIndirectHelper runs before the helper. It stays in the five opcodes that call DoGenericLookup first (INTOP_CALL_HELPER_P_G, P_GS, P_GA, V_AGS and INTOP_UNBOX_ANY_GENERIC): that lookup can throw and can trigger a GC, and its exceptions are C++ exceptions, which the unwind-and-continue handler in InterpExecMethod catches and marks the frame faulting, so the instruction start is right for them.

Test: src/tests/Regressions/coreclr/GitHub_135455 puts the static access first in both a try/finally and an enclosing try/catch. It covers the first access, the access that rethrows the recorded failure, and a shared-generic instantiation, which takes the INTOP_CALL_HELPER_P_G path. The project sets Optimize so that the access stays the first instruction of the region in every build configuration.

Validation (Checked runtime, at this PR's base), with the test run under the JIT and under DOTNET_InterpMode 1, 2 and 3:

without the fix with the fix
linux-x64: the test fails under the interpreter (TypeInitializationException escapes) passes
linux-x64: issue repro, cases 1 and 2 handlers skipped handlers run
osx-arm64: the test fails under the interpreter (TypeInitializationException escapes) passes
osx-arm64: issue repro, cases 1 and 2 handlers skipped handlers run
windows-x64: the test fails under the interpreter (TypeInitializationException escapes) passes
windows-x64: issue repro, cases 1 and 2 handlers skipped handlers run

Storing the instruction start again in INTOP_CALL_HELPER_P_P makes the test fail under the interpreter on all three platforms. Under the JIT the test passes with and without the fix.

The test ran through the merged Regressions runner, as it does in CI. The existing Regressions/coreclr/GitHub_135234 test (from #135290) hits the same bug: on osx-arm64 it fails under DOTNET_InterpMode 1 and 3 at this PR's base and passes with the fix.

With the fix, JIT/interpreter passes on linux-x64 and osx-arm64. On windows-x64 it passes up to TestPInvoke, which I could not run there because the native test assets did not build on that machine; the test assemblies used there were the ones built on macOS.

Other places that store the start of an instruction and then call out, and why they are not changed here:

  • INTOP_NEWARR[_GENERIC]: the allocation failure is raised from the helper's own DynamicHelperFrame, so the frame is not faulting there either. A newarr cannot be the first instruction of a try region, though: its length is always loaded by a separate instruction inside the region, and nothing folds that load away today. It would become reachable with copy propagation.
  • The continuation allocation in the async suspend path always directly follows the awaited call, in the same region, so the adjusted ip stays in that call.
  • The thread-abort dispatch after resuming from a catch has a separate, larger problem: [Interpreter] Thread abort re-raised at the end of an interpreted catch: Thread::m_OSContext is not set up #135463.

Note

This PR was prepared with AI assistance (Anthropic Claude, reviewed by OpenAI Codex). The builds and runs it cites were done at the PR's base commit.

…ed helper

The native-helper exits of INTOP_CALL_HELPER_* and INTOP_UNBOX_ANY[_GENERIC]
left pFrame->ip at the start of the instruction while the helper ran. An
exception thrown by a managed helper reaches the interpreter frame through
managed dispatch, which treats the frame's ip as a return address and moves it
back by STACKWALK_CONTROLPC_ADJUST_OFFSET. When the helper call started a try
region, as the static-base helper does for a static field access on a type with
a .cctor, the adjusted ip fell before the region and its handlers did not run.

Record the next instruction's ip before the native helper call, as INTOP_CALL
and the IL-target exits of the same opcodes already do.

Fixes dotnet#135455
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 9, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @agocke
See info in area-owners.md if you want to be subscribed.

Comment thread src/coreclr/vm/interpexec.cpp Resolved
Replace SAVE_IP_BEFORE_HELPER_CALL with explicit `pFrame->ip = ip + N`, and
remove the store of the instruction start at the top of the helper opcodes
that call nothing but GetPossiblyIndirectHelper before the helper. Keep it in
the opcodes that call DoGenericLookup first: that lookup can throw and trigger
a GC, and its exceptions mark the frame faulting, so the ip has to be the
start of the instruction for them.
Take TestCctorExceptionAtTryStart out of the interpreter bring-up suite and
add it as src/tests/Regressions/coreclr/GitHub_135455, with the same cases.
Comment thread src/coreclr/vm/interpexec.cpp Resolved
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @BrzVlad, @janvorli
See info in area-owners.md if you want to be subscribed.

Instead of storing ip + N before the helper call and adding N again after
it, advance ip first and store that, as INTOP_CALL does.
@BrzVlad

BrzVlad commented Oct 13, 2026

Copy link
Copy Markdown
Member

/ba-g diagnostics failure unrelated

@BrzVlad
BrzVlad merged commit 7462dfd into dotnet:main Oct 13, 2026
118 of 120 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 14, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 14, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-CodeGen-Interpreter-coreclr community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[clr-interp] Handlers of a try region are skipped when its first instruction is a static field load whose type initializer throws

4 participants