Skip to content

Use hidden arguments for NativeAOT thunks - #133222

Draft
jkotas wants to merge 5 commits into
dotnet:mainfrom
jkotas:jkotas-nativeaot-hidden-thunk-arguments
Draft

Use hidden arguments for NativeAOT thunks#133222
jkotas wants to merge 5 commits into
dotnet:mainfrom
jkotas:jkotas-nativeaot-hidden-thunk-arguments

Conversation

@jkotas

@jkotasjkotas commented Sep 4, 2026

Copy link
Copy Markdown
Member

Replace the TLS-based thunk context handoff with SecretStubArgument across supported architectures and remove the common-stub helpers.

RyuJIT must support the special hidden arguments for regular CoreCLR with JIT, so we can take advantage of it in NativeAOT to reduce amount of arch-specific asm code.

Depends on #132534

Replace the TLS-based thunk context handoff with SecretStubArgument across supported architectures and remove the obsolete common-stub helpers.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 6 pipeline(s).
10 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: @agocke, @dotnet/ilc-contrib
See info in area-owners.md if you want to be subscribed.

CopilotAI 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 generated AMD64 and x86 thunk encodings use misaligned typed stores, introducing C++ undefined behavior.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Replaces NativeAOT’s TLS-based thunk context handoff with the hidden SecretStubArgument, building on #132534.

Changes:

  • Adds hidden context parameters to delegate and interface thunks.
  • Emits direct architecture-specific context loads and removes common-stub/TLS helpers.
  • Preserves the x86 hidden argument across stack probing.
File summaries
FileDescription
src/coreclr/tools/Common/TypeSystem/IL/Stubs/PInvokeILEmitter.csPasses hidden context to delegate helpers.
src/coreclr/tools/Common/TypeSystem/IL/Stubs/ILEmitter.csAdds a hidden-argument signature helper.
src/coreclr/tools/Common/TypeSystem/IL/Stubs/DelegateMarshallingMethodThunk.csExtends reverse delegate signatures.
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/CompilerTypeSystemContext.InterfaceThunks.csUses hidden context for interface thunks.
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/ThunkPool.csRemoves common-stub configuration.
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/RuntimeImports.csRemoves obsolete runtime imports.
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/PInvokeMarshal.csAccepts explicit thunk context.
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/IDynamicInterfaceCastable.csSimplifies thunk-pool creation.
src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/CompilerHelpers/SharedCodeHelpers.csRemoves TLS context retrieval.
src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/CompilerHelpers/InteropHelpers.csForwards explicit context arguments.
src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/Augments/RuntimeAugments.csSimplifies thunk-heap API.
src/coreclr/nativeaot/Runtime/unix/unixasmmacrosarm64.incRemoves obsolete TLS helper macro.
src/coreclr/nativeaot/Runtime/unix/PalUnix.cppRemoves thunk TLS storage and exports.
src/coreclr/nativeaot/Runtime/ThunksMapping.cppGenerates direct context/target thunk code.
src/coreclr/nativeaot/Runtime/riscv64/InteropThunksHelpers.SRemoves the RISC-V common stub.
src/coreclr/nativeaot/Runtime/portable.cppRemoves obsolete portable stubs.
src/coreclr/nativeaot/Runtime/loongarch64/InteropThunksHelpers.SRemoves the LoongArch common stub.
src/coreclr/nativeaot/Runtime/i386/InteropThunksHelpers.asmRemoves the Windows x86 common stub.
src/coreclr/nativeaot/Runtime/CMakeLists.txtStops compiling common-stub assembly.
src/coreclr/nativeaot/Runtime/arm64/ThunkPoolThunks.SLoads context directly on Unix ARM64.
src/coreclr/nativeaot/Runtime/arm64/ThunkPoolThunks.asmLoads context directly on Windows ARM64.
src/coreclr/nativeaot/Runtime/arm64/InteropThunksHelpers.SRemoves the Unix ARM64 common stub.
src/coreclr/nativeaot/Runtime/arm64/InteropThunksHelpers.asmRemoves the Windows ARM64 common stub.
src/coreclr/nativeaot/Runtime/arm/InteropThunksHelpers.SRemoves the ARM common stub.
src/coreclr/nativeaot/Runtime/amd64/ThunkPoolThunks.SLoads context directly on Unix AMD64.
src/coreclr/nativeaot/Runtime/amd64/ThunkPoolThunks.asmLoads context directly on Windows AMD64.
src/coreclr/nativeaot/Runtime/amd64/InteropThunksHelpers.SRemoves the Unix AMD64 common stub.
src/coreclr/nativeaot/Runtime/amd64/InteropThunksHelpers.asmRemoves the Windows AMD64 common stub.
src/coreclr/jit/codegenxarch.cppPreserves the x86 hidden argument during stack probes.
Review details

Suppressed comments (1)

src/coreclr/nativeaot/Runtime/ThunksMapping.cpp:182

  • This target-address field starts at byte offset 7, so writing through void** is an unaligned typed store and undefined behavior in C++. Use an unaligned-safe write for both absolute-address fields in this x86 thunk.
 *((void **)pCurrentThunkAddress) = pCurrentDataAddress + POINTER_SIZE;
  • Files reviewed: 29/29 changed files
  • Comments generated: 1
  • Review effort level: Balanced

Comment threadsrc/coreclr/nativeaot/Runtime/ThunksMapping.cpp Outdated
Comment threadsrc/coreclr/nativeaot/Runtime/ThunksMapping.cpp Outdated
Comment threadsrc/coreclr/nativeaot/Runtime/ThunksMapping.cpp Outdated
Co-authored-by: Jan Kotas <jkotas@microsoft.com>
Comment threadsrc/coreclr/nativeaot/Runtime/ThunksMapping.cpp Outdated
Comment threadsrc/coreclr/nativeaot/Runtime/ThunksMapping.cpp Outdated
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@jkotas