Skip to content

Share x64 EH funclet call helpers between CoreCLR and NativeAOT - #136639

Merged
jkotas merged 28 commits into
dotnet:mainfrom
am11:chore/share-eh-funclet-helpers
Nov 8, 2026
Merged

jkotas merged 28 commits into
dotnet:mainfrom
am11:chore/share-eh-funclet-helpers

Conversation

@am11

@am11 am11 commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

This shares the x64 EH funclet call helpers between CoreCLR and NativeAOT. Per review, this PR covers x64 only (Unix and Windows); the other architectures will follow in separate PRs using the same template.

  • CallEHFunclet (catch and finally) and CallEHFilterFunclet live in src/coreclr/runtime/amd64/EHHelpers.{S,asm} and are used by both runtimes. The prolog/epilog macros are local to these files and keep the frame layout that NativeAOT's StackFrameIterator::UnwindFuncletInvokeThunk hard-codes.
  • CallEHFunclet loads (and on NativeAOT writes back) the preserved registers through per-runtime macros (LOAD_FUNCLET_REGISTER, LOAD_FUNCLET_XMM_REGISTER, STORE_FUNCLET_REGISTER) defined in each runtime's AsmMacros_Shared. CoreCLR passes pRD->pCurrentContext as before and the macros load straight from the CONTEXT (including xmm6-xmm15 on Windows), so its code is the same as before. NativeAOT passes its REGDISPLAY and the macros go through its register pointers.
  • NativeAOT-only parts stay in the shared helper under FEATURE_NATIVEAOT: clearing TSF_DoNotTriggerGc around the funclet, and writing the preserved registers back through the REGDISPLAY after the funclet returns, then setting the flag again. That is what RhpCallFinallyFunclet did.
  • NativeAOT resumes after a catch through a new RhpResumeAfterCatch, which does what the tail of RhpCallCatchFunclet did: validates the ExInfo pop in debug builds, unhijacks, pops the ExInfos, reloads the preserved registers (and xmm6-xmm15 on Windows), clears TSF_DoNotTriggerGc, rewinds the CET shadow stack on Windows, and jumps to the resume address. Between the catch funclet returning and RhpResumeAfterCatch, the dispatcher runs with TSF_DoNotTriggerGc set, as during the rest of the second pass, so the thread cannot be hijacked or suspended in that window.
  • Removed on x64: CoreCLR's CallEHFunclet/CallEHFilterFunclet in vm/amd64, and NativeAOT's RhpCallCatchFunclet/RhpCallFinallyFunclet/RhpCallFilterFunclet. RhpCallPropagateExceptionCallback (Apple only) is unchanged.

Validation, on a linux-x64 Debug runtime: the build is clean. I used an EH stress app with catch, finally and filter funclets that allocate and collect while callers hold objects in callee-saved registers, nested finallys in one frame, a throw from a finally, a rethrow from a catch, throwing filters, and a background thread forcing GCs. It passed 20 runs on CoreCLR and 20 on NativeAOT, plus CoreCLR runs with DOTNET_GCStress=0xC and 0x3 (DOTNET_GCgen0size=0x100000). The Windows .asm files were only assembled with llvm-ml64 for both runtimes and their unwind info checked; I have not run them on Windows.

am11 added 2 commits October 29, 2026 17:39
- Move the common Unix macros into runtime/<arch>/UnixAsmMacros.inc.
- Move the common Windows macros into runtime/amd64/AsmMacros_Common.inc,
  runtime/arm64/AsmMacros_Common.h and runtime/i386/AsmMacros_Common.inc.
- Use the shared argument register save/restore macros in NativeAOT GcProbe
  and UniversalTransition.
- Delete the unused runtime/<arch>/StubDispatch files.
- Move the Unix amd64 funclet call prologue/epilogue into
  runtime/amd64/FuncletCall.inc and the Windows x64 FUNCLET_CALL_PROLOGUE/
  FUNCLET_CALL_EPILOGUE into runtime/amd64/AsmMacros_Common.inc.
- Add a shared CallEHFilterFunclet in runtime/<arch>/EHHelpers and use it from
  both runtimes, replacing RhpCallFilterFunclet and the per-arch CoreCLR copies.
@azure-pipelines

azure-pipelines Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 4 pipeline(s).
11 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 29, 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.

am11 added 10 commits October 29, 2026 19:50
Move the general-purpose macros that only one runtime defined (prolog and
epilog helpers, register save and restore helpers, entry/end/label macros,
barriers, PREPARE_EXTERNAL_VAR variants, nop and return encodings) into the
shared per-architecture macro files, including the identical MOVL64,
PREPARE_EXTERNAL_VAR_INDIRECT(_W) and InterlockedOperationBarrier copies in
CoreCLR's AsmMacros_Shared.h.

The shared Windows arm64 INLINE_GET_TLS_VAR declares the TLS variable as
EXTERN itself again, so its callers no longer need to.
…pers

# Conflicts:
#	src/coreclr/runtime/amd64/AsmMacros_Common.inc
Restore the arm, arm64, loongarch64 and riscv64 CallEHFilterFunclet and
RhpCallFilterFunclet helpers in their runtime-specific files. The shared
EH helpers are limited to x64 for now; the other architectures will move
to the same template in follow-up changes.
Move CallEHFunclet next to CallEHFilterFunclet in runtime/amd64/EHHelpers
and use it for catch and finally funclets in both runtimes. The funclet
call prolog/epilog macros are local to that file, with the frame layout
that NativeAOT's StackFrameIterator expects.

The helper loads the preserved registers through a block of register
pointers: CoreCLR passes pointers into its CONTEXT (EHFuncletRegs), and
NativeAOT passes its REGDISPLAY. On NativeAOT, the helper also clears
TSF_DoNotTriggerGc around the funclet and writes the preserved registers
back after it returns, as RhpCallFinallyFunclet did.

NativeAOT resumes after a catch with the new RhpResumeAfterCatch, which
does what the tail of RhpCallCatchFunclet did. RhpCallCatchFunclet and
RhpCallFinallyFunclet are removed on x64.
Drop the unused NativeAOT-only '.global _Name' on amd64, and use .hidden for LEAF_ENTRY in both runtimes on arm64 and loongarch64 (matching ALTERNATE_ENTRY).
@am11 am11 changed the title Share EH funclet call helpers between CoreCLR and NativeAOT Share x64 EH funclet call helpers between CoreCLR and NativeAOT Oct 29, 2026
@am11
am11 marked this pull request as ready for review October 30, 2026 18:02
Comment thread src/coreclr/vm/eetwain.cpp Resolved
@jkotas

jkotas commented Oct 30, 2026

Copy link
Copy Markdown
Member

@janvorli Do you have thoughts about this refactoring or how to make it cleaner? (Note this PR is x64 only - the idea is to iterate on the shape on x64 and then apply it to other arches once we are happy with it.)

@janvorli janvorli left 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.

LGTM, a nit - can you please update the PR description that still mentions the EHFuncletRegs?

@am11

am11 commented Nov 2, 2026

Copy link
Copy Markdown
Member Author

@janvorli thank you, updated the description.

@am11
am11 requested a review from jkotas November 3, 2026 23:36
@jkotas

jkotas commented Nov 8, 2026

Copy link
Copy Markdown
Member

/azp run runtime-nativeaot-outerloop

@azure-pipelines

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

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.

🔵 Needs a closer look

Windows execution and GC-stress validation are still needed for the x64 EH and NativeAOT catch-resume changes, as assembly/unwind inspection alone cannot establish GC, collided-unwind, and CET shadow-stack correctness.

No open issues 🎉

What changed in this PR

Consolidates x64 exception-handling funclet call helpers into shared assembly used by CoreCLR and NativeAOT on Unix and Windows, reducing duplicate implementations while retaining runtime-specific register access.

Changes:

  • Adds shared catch, finally, and filter call helpers with per-runtime register-loading macros.
  • Separates NativeAOT catch resumption and exception-record cleanup from funclet invocation, and updates stack walking and managed dispatch.
  • Updates both runtimes’ build inputs and removes superseded x64 helpers.

The PR reports Linux x64 runtime validation; Windows validation was limited to assembly and unwind-info checks.

File Description
src/​coreclr/​vm/​eetwain.cpp Passes the full context to the x64 filter helper.
src/​coreclr/​vm/​CMakeLists.txt Includes shared EH assembly in CoreCLR builds.
src/​coreclr/​vm/​amd64/​AsmMacros_Shared.inc Adds Windows context-based register-loading macros.
src/​coreclr/​vm/​amd64/​AsmMacros_Shared.h Adds the Unix context-based register-loading macro.
src/​coreclr/​vm/​amd64/​asmhelpers.S Removes duplicate Unix funclet call helpers.
src/​coreclr/​vm/​amd64/​AsmHelpers.asm Removes duplicate Windows funclet call helpers.
src/​coreclr/​runtime/​amd64/​EHHelpers.S Adds shared Unix x64 funclet call helpers.
src/​coreclr/​runtime/​amd64/​EHHelpers.asm Adds shared Windows x64 funclet call helpers.
src/​coreclr/​nativeaot/​Runtime/​thread.h Makes PopExInfo available beyond portable helpers.
src/​coreclr/​nativeaot/​Runtime/​thread.cpp Exposes exception-record cleanup through RhpPopExInfo.
src/​coreclr/​nativeaot/​Runtime/​StackFrameIterator.cpp Updates helper recognition and hard-coded frame layouts.
src/​coreclr/​nativeaot/​Runtime/​CMakeLists.txt Includes shared EH assembly in NativeAOT x64 builds.
src/​coreclr/​nativeaot/​Runtime/​amd64/​ExceptionHandling.S Adds catch resumption and adjusts Apple propagation cleanup.
src/​coreclr/​nativeaot/​Runtime/​amd64/​ExceptionHandling.asm Replaces Windows funclet callers with catch-resumption logic.
src/​coreclr/​nativeaot/​Runtime/​amd64/​AsmMacros.inc Removes the unused validation-helper declaration.
src/​coreclr/​nativeaot/​Runtime/​amd64/​AsmMacros_Shared.inc Adds Windows register-display load and write-back macros.
src/​coreclr/​nativeaot/​Runtime/​amd64/​AsmMacros_Shared.h Adds Unix register-display load and write-back macros.
src/​coreclr/​nativeaot/​Runtime.Base/​src/​System/​Runtime/​InternalCalls.cs Declares shared helpers and revised x64 runtime imports.
src/​coreclr/​nativeaot/​Runtime.Base/​src/​System/​Runtime/​ExceptionHandling.cs Routes x64 dispatch through shared helpers and explicit cleanup.

Review effort: Balanced

@am11

am11 commented Nov 8, 2026

Copy link
Copy Markdown
Member Author

Thanks. I will try to follow up on other archs after this.

@jkotas

jkotas commented Nov 8, 2026

Copy link
Copy Markdown
Member

Thanks. I will try to follow up on other archs after this.

For other arches, do you plan to align the calling convention produced by the JIT with https://github.com/dotnet/runtime/blob/main/docs/design/coreclr/botr/clr-abi.md#registers-on-entry-to-a-funclet together with the asm refactoring? If yes, it would be best to do arm64 only next to make it easier to get the change merged.

@jkotas jkotas left 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.

Thank you!

@jkotas
jkotas merged commit 7d0aafa into dotnet:main Nov 8, 2026
102 checks passed
@am11
am11 deleted the chore/share-eh-funclet-helpers branch November 8, 2026 23:48
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-VM-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.

5 participants