Skip to content

[clr-interp] Fix Swift error handling for marshaled P/Invokes in interpreter#125177

Merged
kotlarmilos merged 2 commits intodotnet:mainfrom
kotlarmilos:clr-ios/swift-callconv-marshalled-pinvoke
Mar 12, 2026
Merged

[clr-interp] Fix Swift error handling for marshaled P/Invokes in interpreter#125177
kotlarmilos merged 2 commits intodotnet:mainfrom
kotlarmilos:clr-ios/swift-callconv-marshalled-pinvoke

Conversation

@kotlarmilos
Copy link
Copy Markdown
Member

@kotlarmilos kotlarmilos commented Mar 4, 2026

Description

When a Swift P/Invoke requires marshaling (e.g. ref SwiftError), the interpreter creates an IL stub with a calli to the native function. Previously, the P/Invoke MethodDesc had no interpreter code, so INTOP_CALL fell through to InvokeManagedMethod, creating an outer native transition via InterpreterStub. The inner calli created a second transition. The inner Load_SwiftError captured x21 correctly, but ExecuteInterpretedMethod could save/restore x21 as a callee-saved register, causing the outer epilog to read a stale value.

Debug actually also had two native transitions but it just passed by coincidence because the unoptimized C compiler didn't use x21 as a scratch register, so the SwiftError value survived through ExecuteInterpretedMethod.

Copilot AI review requested due to automatic review settings March 4, 2026 13:51
@kotlarmilos kotlarmilos self-assigned this Mar 4, 2026
@kotlarmilos kotlarmilos modified the milestones: 10.0.x, 11.0.0 Mar 4, 2026
@kotlarmilos kotlarmilos added the os-ios Apple iOS label Mar 4, 2026
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @BrzVlad, @janvorli, @kg
See info in area-owners.md if you want to be subscribed.

This comment was marked as outdated.

Copilot AI review requested due to automatic review settings March 11, 2026 09:27
@kotlarmilos kotlarmilos force-pushed the clr-ios/swift-callconv-marshalled-pinvoke branch from 72a25d1 to fb5fadb Compare March 11, 2026 09:27
Copy link
Copy Markdown
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

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Comments suppressed due to low confidence (1)

src/coreclr/vm/wasm/helpers.cpp:732

  • pContextMD is unused in the WASM implementation of GetCookieForCalliSig, which can trigger -Wunused-parameter//WX build breaks. Either remove the parameter name (keep the type for signature compatibility) or explicitly mark it unused (e.g., UNREFERENCED_PARAMETER(pContextMD)).
void* GetCookieForCalliSig(MetaSig metaSig, MethodDesc *pContextMD)
{
    STANDARD_VM_CONTRACT;

    void* thunk = ComputeCalliSigThunk(metaSig);
    if (thunk == NULL)
    {
        PORTABILITY_ASSERT("GetCookieForCalliSig: unknown thunk signature");

You can also share your feedback on Copilot code review. Take the survey.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@kotlarmilos kotlarmilos force-pushed the clr-ios/swift-callconv-marshalled-pinvoke branch from fb5fadb to 8c9b943 Compare March 11, 2026 09:37
Copilot AI review requested due to automatic review settings March 11, 2026 13:42
Copy link
Copy Markdown
Member

@janvorli janvorli left a comment

Choose a reason for hiding this comment

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

LGTM, thank you!

Copy link
Copy Markdown
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

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


You can also share your feedback on Copilot code review. Take the survey.

@kotlarmilos kotlarmilos merged commit e7daed6 into dotnet:main Mar 12, 2026
126 checks passed
Copilot AI pushed a commit that referenced this pull request Mar 13, 2026
…rpreter (#125177)

## Description

When a Swift P/Invoke requires marshaling (e.g. ref SwiftError), the
interpreter creates an IL stub with a calli to the native function.
Previously, the P/Invoke MethodDesc had no interpreter code, so
INTOP_CALL fell through to InvokeManagedMethod, creating an outer native
transition via InterpreterStub. The inner calli created a second
transition. The inner Load_SwiftError captured x21 correctly, but
ExecuteInterpretedMethod could save/restore x21 as a callee-saved
register, causing the outer epilog to read a stale value.

Debug actually also had two native transitions but it just passed by
coincidence because the unoptimized C compiler didn't use x21 as a
scratch register, so the SwiftError value survived through
ExecuteInterpretedMethod.

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions github-actions bot locked and limited conversation to collaborators Apr 12, 2026
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