Skip to content

Conversation

@AaronRobinsonMSFT
Copy link
Member

Fixes #121006

This provides an entry point, similar to P/Invokes thunks, for generated reverse P/Invoke stubs. A commented out example of the generated code was provided.

@AaronRobinsonMSFT AaronRobinsonMSFT added this to the 11.0.0 milestone Nov 4, 2025
Copilot AI review requested due to automatic review settings November 4, 2025 23:31
@AaronRobinsonMSFT AaronRobinsonMSFT added arch-wasm WebAssembly architecture area-VM-coreclr labels Nov 4, 2025
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @mangod9
See info in area-owners.md if you want to be subscribed.

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 adds support for UnmanagedCallersOnly methods in the WASM interpreter environment. It implements a thunk lookup mechanism for reverse P/Invoke scenarios and refactors some existing code for better organization.

Key changes:

  • Implements GetUnmanagedCallersOnlyThunk function to look up thunks for methods with the UnmanagedCallersOnly attribute
  • Adds caching infrastructure for reverse thunks using hash-based lookup
  • Refactors ExecuteInterpretedMethodFromUnmanaged to consolidate interpreter method execution from unmanaged code
  • Enables UnmanagedCallersOnly support in CreateDelegate for WASM by removing the blanket E_NOTIMPL error

Reviewed Changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/coreclr/vm/wasm/helpers.cpp Adds reverse thunk lookup infrastructure and GetUnmanagedCallersOnlyThunk implementation
src/coreclr/vm/wasm/callhelpers.hpp Updates header guards and adds reverse thunk data structures
src/coreclr/vm/wasm/callhelpers.cpp Adds reverse thunk map array definition with placeholder example
src/coreclr/vm/wasm/calldescrworkerwasm.cpp Simplifies to use new ExecuteInterpretedMethodFromUnmanaged function
src/coreclr/vm/prestub.cpp Refactors and renames function to ExecuteInterpretedMethodFromUnmanaged
src/coreclr/vm/precode_portable.hpp Adds flags for tracking UnmanagedCallersOnly attribute
src/coreclr/vm/precode_portable.cpp Implements HasUnmanagedCallersOnlyAttribute method with caching
src/coreclr/vm/method.cpp Updates TryGetMultiCallableAddrOfCode to handle UnmanagedCallersOnly
src/coreclr/vm/interpexec.h Adds forward declaration for ExecuteInterpretedMethodFromUnmanaged
src/coreclr/vm/corhost.cpp Enables UnmanagedCallersOnly for WASM in CreateDelegate
src/coreclr/interpreter/compiler.cpp Uses aggregate initialization instead of memset
src/coreclr/hosts/corerun/CMakeLists.txt Conditionalizes WASM_BIGINT flag for browser-only scenarios

@AaronRobinsonMSFT AaronRobinsonMSFT marked this pull request as draft November 5, 2025 00:19
@AaronRobinsonMSFT
Copy link
Member Author

Converting this to draft. The approach in this PR doesn't permit linking against exports. I will need to modify it.

@radekdoulik
Copy link
Member

radekdoulik commented Nov 6, 2025

I have the reversed generator part nearly ported, current output looks like https://gist.github.com/radekdoulik/049236003eca75bc34fed6ee727d89fc

Take it just as a preview, didn't try to build the output yet. It also shouldn't have zero sized args arrays, etc.

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.

Thanks

@AaronRobinsonMSFT
Copy link
Member Author

I have the reversed generator part nearly ported, current output looks like https://gist.github.com/radekdoulik/049236003eca75bc34fed6ee727d89fc

Take it just as a preview, didn't try to build the output yet. It also shouldn't have zero sized args arrays, etc.

Thanks @radekdoulik. I had to tweak them a bit, but they were close. See 0bf48e9. Note that the hashing logic doesn't match what is in this PR. That means this will only work for exports and not through ldftn calls using C# function pointers.

@pavelsavara Please checkout the above commit so you can see how to enable this for more.

@radekdoulik
Copy link
Member

/ba-g unrelated timeouts

@radekdoulik radekdoulik merged commit 6e1e6b1 into dotnet:main Nov 7, 2025
132 of 156 checks passed
@radekdoulik
Copy link
Member

I have the reversed generator part nearly ported, current output looks like https://gist.github.com/radekdoulik/049236003eca75bc34fed6ee727d89fc
Take it just as a preview, didn't try to build the output yet. It also shouldn't have zero sized args arrays, etc.

Thanks @radekdoulik. I had to tweak them a bit, but they were close. See 0bf48e9. Note that the hashing logic doesn't match what is in this PR. That means this will only work for exports and not through ldftn calls using C# function pointers.

That's great, thank you. I will finish the generator changes.

@pavelsavara
Copy link
Member

pavelsavara commented Nov 7, 2025

So now, we don't need those anymore

DEFINE_CLASS(TIMER_QUEUE, Threading, TimerQueue)
DEFINE_METHOD(TIMER_QUEUE, TIMER_HANDLER, TimerHandler, SM_RetVoid)
DEFINE_CLASS(THREAD_POOL, Threading, ThreadPool)
DEFINE_METHOD(THREAD_POOL, BACKGROUND_JOB_HANDLER, BackgroundJobHandler, SM_RetVoid)

except they are keeping the IL from trimming.

Because #101434

@AaronRobinsonMSFT AaronRobinsonMSFT deleted the wasm_umco branch November 7, 2025 16:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch-wasm WebAssembly architecture area-VM-coreclr

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[browser][coreCLR] enable [UnmanagedCallersOnly]

5 participants