Skip to content

Conversation

@kg
Copy link
Member

@kg kg commented Aug 27, 2025

  • Implements GetStubContext intrinsic in the interpreter
  • For interpreted reverse p/invoke stubs, we ensure that TheUMEntryPrestubWorker runs for every invocation so that it can capture the hidden argument in a thread local. The interpreter can then recover the hidden argument from the thread local in order to offer it via GetStubContext later.
  • Upon entry to interpreted methods that accept a hidden argument, we store the hidden argument (that was saved by the um entry prestub worker) into a special local that can be read from later by calls to GetStubContext.

Fixes baseservices\threading\regressions\115178 and probably others.

@kg kg force-pushed the interp-reverse-pinvoke branch from b1b10b0 to 8fcd3d1 Compare August 27, 2025 21:54
@kg
Copy link
Member Author

kg commented Aug 27, 2025

I redid part of this from scratch to try and make it less messy and take into account @jkotas 's feedback. Still need to apply the optimization he suggested based on CORJIT_FLAG_PUBLISH_SECRET_PARAM.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR implements reverse P/Invoke support in the CoreCLR interpreter by adding the GetStubContext intrinsic and ensuring proper hidden argument handling for interpreted code. The implementation allows the interpreter to capture and retrieve hidden arguments needed for reverse P/Invoke scenarios.

Key changes:

  • Adds GetStubContext intrinsic support to the interpreter instruction set
  • Implements thread-local storage for capturing hidden arguments during reverse P/Invoke transitions
  • Modifies interpreter frame structure to store hidden arguments for later retrieval

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/coreclr/vm/prestub.cpp Adds hidden argument capture logic to ExecuteInterpretedMethod
src/coreclr/vm/interpexec.h Extends InterpMethodContextFrame with hiddenArgument field
src/coreclr/vm/interpexec.cpp Implements INTOP_GETSTUBCONTEXT instruction handler
src/coreclr/vm/dllimportcallback.h Adds interpreter target support to UMEntryThunkData
src/coreclr/vm/dllimportcallback.cpp Implements thread-local storage for UMEntryThunkData
src/coreclr/interpreter/intrinsics.cpp Adds GetStubContext to named intrinsics mapping
src/coreclr/interpreter/intops.def Defines INTOP_GETSTUBCONTEXT instruction
src/coreclr/interpreter/interpretershared.h Adds hasHiddenArgument flag to InterpMethod
src/coreclr/interpreter/compiler.cpp Implements GetStubContext intrinsic compilation and adds hasHiddenArgument detection

@janvorli janvorli mentioned this pull request Aug 29, 2025
66 tasks
kg added 10 commits August 29, 2025 19:06
…d reverse p/invoke stubs and ensure that the worker runs on every reverse p/invoke, so that the interpreter can recover the hidden argument and make it available via GetStubContext.

Add INTOP_GETSTUBCONTEXT and always generate it for the GetStubContext intrinsic, even if other intrinsics are disabled

Address PR feedback

Revert most changes
Add missing ifdef
@kg kg force-pushed the interp-reverse-pinvoke branch from 14de1a8 to 2191010 Compare August 30, 2025 04:44
@kg
Copy link
Member Author

kg commented Aug 30, 2025

GetInterpreterTarget had STANDARD_VM_CONTRACT which was causing spurious contract failures in JIT\SIMD\Vector3Interop_r\Vector3Interop_r. I've updated it to LIMITED_METHOD_CONTRACT since it's just a field getter.

kg added 3 commits September 1, 2025 17:46
… on demand instead of always having a field for it on InterpreterFrame
@kg kg requested a review from jkotas September 2, 2025 00:55
Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

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

Thank you!

@kg kg merged commit e3467fc into dotnet:main Sep 2, 2025
99 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Oct 2, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants