Skip to content

Fix Windows interpreter calli native exception handling - #136163

Open
BrzVlad wants to merge 8 commits into
dotnet:mainfrom
BrzVlad:fix-clrinterp-seh
Open

BrzVlad wants to merge 8 commits into
dotnet:mainfrom
BrzVlad:fix-clrinterp-seh

Conversation

@BrzVlad

@BrzVlad BrzVlad commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

A managed calli can enter the prestub and fail during compilation. The C++ exception is converted to managed SEH, but this call path was missing the SEH wrapper needed to reach interpreter catches.

Adding the wrapper exposed missing cleanup of explicit frames in this call path. Adding destructors for InlinedCallFrame, InterpreterFrame and PrestubMethodFrame that pop the frames in case they are on the explicit frames list fixes the problem.

Fixes #136993
Fixes #136908
Fixes #137173

@BrzVlad

BrzVlad commented Oct 20, 2026

Copy link
Copy Markdown
Member Author

/azp-run runtime-interpreter

@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

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.

@BrzVlad

BrzVlad commented Oct 21, 2026

Copy link
Copy Markdown
Member Author

/azp run runtime-interpreter

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@BrzVlad

BrzVlad commented Oct 21, 2026

Copy link
Copy Markdown
Member Author

/azp run runtime-interpreter

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@janvorli

Copy link
Copy Markdown
Member

/azp run runtime-interpreter

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

BrzVlad and others added 3 commits October 30, 2026 11:22
A managed calli can enter the prestub and fail during compilation. The C++ exception is converted to managed SEH, but this call path
was missing the SEH wrapper needed to reach interpreter catches.

Adding the wrapper exposed missing personality routines on the interpreter call stubs: native unwinding left stale entries in the runtime frame chain (on windows, frames are popped explicitly in the SEH unwind handler, rather than relying on a destructor). Register the existing frame-chain cleanup handler on the Windows x64 and ARM64 call stubs.
Avoid using personality routines, pop frame explicitly in the prestub exception scenario

Fix bug where we were clobbering callee saved register RSI and RDI.
* PreStubWorker needs to pop all explicit frames upto and including the
  PrestubMethodFrame
* When propagating unhandled exception to the host, prevent it being
  caught and processed in the interpreter code.
@BrzVlad

BrzVlad commented Oct 30, 2026

Copy link
Copy Markdown
Member Author

/azp run runtime-interpreter

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@BrzVlad

BrzVlad commented Nov 2, 2026

Copy link
Copy Markdown
Member Author

/azp run runtime-interpreter

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@janvorli

janvorli commented Nov 4, 2026

Copy link
Copy Markdown
Member

/azp run runtime-interpreter

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@jkotas

This comment was marked as resolved.

@janvorli

This comment was marked as resolved.

Comment thread src/coreclr/vm/frames.h
#if defined(FEATURE_INTERPRETER) && !defined(DACCESS_COMPILE)
~PrestubMethodFrame() noexcept
{
PopIfChained();

@jkotas jkotas Nov 5, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would it be better to just enable ~Frame() noexcept { PopIfChained(); } for interpreter instead of enabling it on individual Frames?

If this is necessary on PrestubMethodFrame, I would expect it to be needed on number of other similar Frames - ExternalMethodFrame, etc.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Enabling it for all frames would require a number of methods to be split because the destructor would now occur in code that has SEH handling. That's why I have opted for changing only the ones that require it. It is possible that the ExternalMethodFrame may need it too, but I think that would be it.
Essentially, these are places where managed exception (SEH) can flow into the interpreter code.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@jkotas this issue got introduced by #133662

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There used to be a CallDescrWorker whose personality routine popped explicit frames. Now it is gone.
The path before Jeremy's change was:

19 0000003e`c3f7b6c0 00007ffa`3fa96e25     coreclr!PreStubWorker+0x650 [D:\git\runtime2\src\coreclr\vm\prestub.cpp @ 1982] 
1a 0000003e`c3f7b950 00007ffa`3fa959b3     coreclr!ThePreStub+0x55 [D:\git\runtime2\src\coreclr\vm\amd64\ThePreStubAMD64.asm @ 20] 
1b 0000003e`c3f7ba00 00007ffa`3f90fae2     coreclr!CallDescrWorkerInternal+0x83 [D:\git\runtime2\src\coreclr\vm\amd64\CallDescrWorkerAMD64.asm @ 74] 
1c 0000003e`c3f7ba40 00007ffa`3fa95244     coreclr!RuntimeMethodHandle_InvokeMethod+0xe92 [D:\git\runtime2\src\coreclr\vm\reflectioninvocation.cpp @ 645] 
1d 0000003e`c3f7beb0 00007ffa`3f8439e4     coreclr!CallJittedMethodRetVoid+0x14 [D:\git\runtime2\src\coreclr\vm\amd64\AsmHelpers.asm @ 1089] 
1e 0000003e`c3f7bef0 00007ffa`3f84503d     coreclr!InvokeManagedMethod+0x554 [D:\git\runtime2\src\coreclr\vm\interpexec.cpp @ 487] 
1f 0000003e`c3f7c0a0 00007ffa`3f844836     coreclr!`InvokeUnmanagedMethodWithTransition'::`3'::__Body::Run+0xcd [D:\git\runtime2\src\coreclr\vm\interpexec.cpp @ 294] 
20 0000003e`c3f7c1b0 00007ffa`3f83eaeb     coreclr!InvokeUnmanagedMethodWithTransition+0xe6 [D:\git\runtime2\src\coreclr\vm\interpexec.cpp @ 296] 
21 0000003e`c3f7c310 00007ffa`3f664672     coreclr!InterpExecMethod+0x3f1b [D:\git\runtime2\src\coreclr\vm\interpexec.cpp @ 3489] 

Now it is

19 0000000a`a0d7b0f0 00007ffa`e6f748a5     coreclr!PreStubWorker+0x650 [D:\git\runtime2\src\coreclr\vm\prestub.cpp @ 1982] 
1a 0000000a`a0d7b380 00007ffa`e6f72cc4     coreclr!ThePreStub+0x55 [D:\git\runtime2\src\coreclr\vm\amd64\ThePreStubAMD64.asm @ 20] 
1b 0000000a`a0d7b430 00007ffa`e6d22ac9     coreclr!CallJittedMethodRetVoid+0x14 [D:\git\runtime2\src\coreclr\vm\amd64\AsmHelpers.asm @ 1089] 
1c 0000000a`a0d7b460 00007ffa`e6d1ea08     coreclr!InvokeCalliStub+0x409 [D:\git\runtime2\src\coreclr\vm\interpexec.cpp @ 597] 
1d 0000000a`a0d7b610 00007ffa`e6b44622     coreclr!InterpExecMethod+0x3e88 [D:\git\runtime2\src\coreclr\vm\interpexec.cpp @ 3454] 

@jkotas jkotas Nov 5, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Introduced or exposed?

If I am understanding this correctly, this bug was there since forever, but it was not causing any tests to fail.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah, I should have said exposed. I meant it started to happen after that PR.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@jkotas, I don't think the code that uses ExternalMethodFrame is invoked directly from the interpreter like the prestub. it would be called by R2R compiled code only, wouldn't it?

@jkotas jkotas Nov 10, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ExternalMethodFrame is not invoked directly, but I believe it can end up as the next direct frame in the presence of tailcalls:

  1. Interpreted managed calli can enter R2R method
  2. R2R method calls unresolved import that ends up inside ExternalMethodFrame
  3. Resolution of the unresolved import throws an exception

The stack layout at the time of the exception is going to be exactly same as if the managed calli called the ExternalMethodFrame directly.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You are right, copilot was able to construct a test that hit the same issue with ExternalMethodFrame using your comment.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Added the same treatment and a test

@jkotas

This comment was marked as resolved.

Comment thread src/coreclr/vm/interpexec.cpp Outdated
@@ -267,16 +267,8 @@ std::invoke_result_t<Function> CallWithSEHWrapper(Function function)

// Use the NOINLINE to ensure that the InlinedCallFrame in this method is a lower stack address than any InterpMethodContextFrame values.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Move this comment and the NOINLINE annotation to InvokeUnmanagedMethodWithTransition?

I do not see a reason for marking this method with NOINLINE

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why was it necessary to split the original method into two? If there is a reason, it can use a comment.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Since the InlinedCallFrame now has a destructor, it cannot be in the same function where we do SEH handling.

@jkotas jkotas Nov 11, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

SEH handling

Should this change be Windows-only so that we are not regressing non-Windows platforms that we ship?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The NOINLINE on the internal method was an accidental leftover of a copy and paste split of the original method. It should not be there. I've fixed it and added FORCEINLINE to ensure that on non-windows, the Inner method is inlined into the outer to preserve the previous state.

* Add similar treatment to ExternalMethodFrame and related test
* Add missing skip of handling of propagation of unhandled exception to
  host / external caller
@janvorli

Copy link
Copy Markdown
Member

/azp run runtime-interpreter

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

<CLRTestTargetUnsupported Condition="'$(EnableNativeSanitizers)' != ''">true</CLRTestTargetUnsupported>
<CLRTestTargetUnsupported Condition="'$(TargetArchitecture)' == 'wasm'">true</CLRTestTargetUnsupported>
<CLRTestTargetUnsupported Condition="'$(TargetsAppleMobile)' == 'true' or '$(TargetsAndroid)' == 'true' or '$(TargetsBrowser)' == 'true' or '$(TargetsWasi)' == 'true'">true</CLRTestTargetUnsupported>
<!-- Compile only the helper: the calli caller must remain JITted or interpreted. -->

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We can achieve this by [BypassReadyToRun] and then enable the test for all CI configurations and just use the default tests harness.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Updated

Comment thread src/coreclr/vm/frames.h
ExternalMethodFrame(TransitionBlock * pTransitionBlock);

#if defined(FEATURE_INTERPRETER) && !defined(DACCESS_COMPILE)
~ExternalMethodFrame() noexcept

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How about StubDispatchFrame - can it hit the same problem? (Tailcall of an interface method.)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, it seems that is another one.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Fixed and added test

@janvorli

Copy link
Copy Markdown
Member

/azp run runtime-interpreter

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@jkotas

jkotas commented Nov 16, 2026

Copy link
Copy Markdown
Member

/azp run runtime-coreclr outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@jkotas
jkotas requested a balanced review from Copilot November 16, 2026 07:23
@jkotas

jkotas commented Nov 16, 2026

Copy link
Copy Markdown
Member

stackoverflowtester is falling on interpreter

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The test project does not force ReadyToRun compilation, so the import scenario can pass without exercising the new ExternalMethodFrame cleanup.

1 open issue
What changed in this PR

This PR fixes Windows CoreCLR interpreter exception handling for managed calli calls that fail during compilation or native resolution, addressing the failures reported in #136993, #136908, and #137173.

Changes:

  • Wraps interpreter calli execution in structured exception handling while allowing unhandled exceptions to propagate outward.
  • Adds explicit-frame cleanup during unwinding and fixes register usage in the Windows x64 stack helper.
  • Adds subprocess regression tests for tail-call import and ambiguous interface-dispatch failures.
File Description
src/​tests/​readytorun/​TailCallImportException/​TailCallImportException.csproj Configures the isolated regression executable and platform exclusions.
src/​tests/​readytorun/​TailCallImportException/​TailCallImportException.cs Runs child scenarios and checks exception diagnostics and exit codes.
src/​tests/​readytorun/​TailCallImportException/​TailCallHelper.ilproj Builds the IL helper assembly.
src/​tests/​readytorun/​TailCallImportException/​TailCallHelper.il Defines failing tail-call import and interface-dispatch scenarios.
src/​coreclr/​vm/​threads.h Renames the unhandled-exception propagation flag.
src/​coreclr/​vm/​interpexec.cpp Adds the exception wrapper and updates filtering and transition handling.
src/​coreclr/​vm/​frames.h Adds destructors that remove chained explicit frames.
src/​coreclr/​vm/​frames.cpp Makes conditional frame cleanup available on Windows and in either GC mode.
src/​coreclr/​vm/​exceptionhandling.cpp Updates references to the renamed propagation flag.
src/​coreclr/​vm/​amd64/​AsmHelpers.asm Avoids modifying nonvolatile registers in Load_Stack_Ref.

Review effort: Balanced

<RequiresProcessIsolation>true</RequiresProcessIsolation>
<ReferenceXUnitWrapperGenerator>false</ReferenceXUnitWrapperGenerator>
<CLRTestPriority>1</CLRTestPriority>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
@BrzVlad

BrzVlad commented Nov 19, 2026

Copy link
Copy Markdown
Member Author

stackoverflowtester is falling on interpreter

This is not related to this PR, it shows on normal scheduled runs as well. Seems to have regressed after #135807

@jkotas

jkotas commented Nov 19, 2026

Copy link
Copy Markdown
Member

This is not related to this PR, it shows on normal scheduled runs as well. Seems to have regressed after #135807

Could you please open a known issue for this failure then?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

4 participants