Repository navigation
Share x64 EH funclet call helpers between CoreCLR and NativeAOT - #136639
Conversation
- 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: 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. |
|
Tagging subscribers to this area: @agocke |
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).
…re/share-eh-funclet-helpers
The DAC exports the LEAF_ENTRY stubs generated from libredefines.inc, so .hidden broke linking libmscordbi.so against libmscordaccore.so.
…re/share-eh-funclet-helpers
…re/share-eh-funclet-helpers
…t-helpers # Conflicts: # src/coreclr/nativeaot/Runtime/CMakeLists.txt
|
@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
left a comment
There was a problem hiding this comment.
LGTM, a nit - can you please update the PR description that still mentions the EHFuncletRegs?
|
@janvorli thank you, updated the description. |
…n arch-neutral code Co-authored-by: Cursor <[email protected]>
|
/azp run runtime-nativeaot-outerloop |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
…allback Co-authored-by: Cursor <[email protected]>
There was a problem hiding this comment.
🔵 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
|
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. |
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) andCallEHFilterFuncletlive insrc/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'sStackFrameIterator::UnwindFuncletInvokeThunkhard-codes.CallEHFuncletloads (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'sAsmMacros_Shared. CoreCLR passespRD->pCurrentContextas before and the macros load straight from theCONTEXT(including xmm6-xmm15 on Windows), so its code is the same as before. NativeAOT passes itsREGDISPLAYand the macros go through its register pointers.FEATURE_NATIVEAOT: clearingTSF_DoNotTriggerGcaround the funclet, and writing the preserved registers back through theREGDISPLAYafter the funclet returns, then setting the flag again. That is whatRhpCallFinallyFuncletdid.RhpResumeAfterCatch, which does what the tail ofRhpCallCatchFuncletdid: validates the ExInfo pop in debug builds, unhijacks, pops the ExInfos, reloads the preserved registers (and xmm6-xmm15 on Windows), clearsTSF_DoNotTriggerGc, rewinds the CET shadow stack on Windows, and jumps to the resume address. Between the catch funclet returning andRhpResumeAfterCatch, the dispatcher runs withTSF_DoNotTriggerGcset, as during the rest of the second pass, so the thread cannot be hijacked or suspended in that window.CallEHFunclet/CallEHFilterFuncletinvm/amd64, and NativeAOT'sRhpCallCatchFunclet/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=0xCand0x3(DOTNET_GCgen0size=0x100000). The Windows.asmfiles were only assembled with llvm-ml64 for both runtimes and their unwind info checked; I have not run them on Windows.