Skip to content

[interp] Reverse p/invoke - #119133

Merged
kg merged 15 commits into
dotnet:mainfrom
kg:interp-reverse-pinvoke
Sep 2, 2025
Merged

[interp] Reverse p/invoke#119133
kg merged 15 commits into
dotnet:mainfrom
kg:interp-reverse-pinvoke

Conversation

@kg

@kgkg commented Aug 27, 2025

Copy link
Copy Markdown
Contributor
  • 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.

Comment threadsrc/coreclr/vm/dllimportcallback.h Outdated
Comment threadsrc/coreclr/vm/dllimportcallback.h Outdated
Comment threadsrc/coreclr/vm/prestub.cpp Outdated
Comment threadsrc/coreclr/vm/dllimportcallback.cpp
@kg
kgforce-pushed the interp-reverse-pinvoke branch from b1b10b0 to 8fcd3d1CompareAugust 27, 2025 21:54
@kg

kg commented Aug 27, 2025

Copy link
Copy Markdown
ContributorAuthor

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.

Comment threadsrc/coreclr/vm/dllimportcallback.h Outdated
Comment threadsrc/coreclr/vm/dllimportcallback.h Outdated
Comment threadsrc/coreclr/vm/dllimportcallback.cpp Outdated
Comment threadsrc/coreclr/vm/dllimportcallback.cpp
Comment threadsrc/coreclr/vm/dllimportcallback.h Outdated
Comment threadsrc/coreclr/vm/dllimportcallback.h Outdated
Comment threadsrc/coreclr/vm/dllimportcallback.cpp
Comment threadsrc/coreclr/vm/interpexec.h Outdated

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.

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
FileDescription
src/coreclr/vm/prestub.cppAdds hidden argument capture logic to ExecuteInterpretedMethod
src/coreclr/vm/interpexec.hExtends InterpMethodContextFrame with hiddenArgument field
src/coreclr/vm/interpexec.cppImplements INTOP_GETSTUBCONTEXT instruction handler
src/coreclr/vm/dllimportcallback.hAdds interpreter target support to UMEntryThunkData
src/coreclr/vm/dllimportcallback.cppImplements thread-local storage for UMEntryThunkData
src/coreclr/interpreter/intrinsics.cppAdds GetStubContext to named intrinsics mapping
src/coreclr/interpreter/intops.defDefines INTOP_GETSTUBCONTEXT instruction
src/coreclr/interpreter/interpretershared.hAdds hasHiddenArgument flag to InterpMethod
src/coreclr/interpreter/compiler.cppImplements GetStubContext intrinsic compilation and adds hasHiddenArgument detection

Comment threadsrc/coreclr/vm/dllimportcallback.cpp Outdated
@janvorlijanvorli 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
kgforce-pushed the interp-reverse-pinvoke branch from 14de1a8 to 2191010CompareAugust 30, 2025 04:44
@kg

kg commented Aug 30, 2025

Copy link
Copy Markdown
ContributorAuthor

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
kg requested a review from jkotasSeptember 2, 2025 00:55

@jkotasjkotas 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!

@kg
kg merged commit e3467fc into dotnet:mainSep 2, 2025
99 checks passed
@github-actionsgithub-actionsBot locked and limited conversation to collaborators Oct 2, 2025
Sign up for freeto 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.

4 participants

@kg@jkotas@janvorli