Skip to content

Add support in WASM for calling UnmanagedCallersOnly functions - #121359

Merged
radekdoulik merged 16 commits into
dotnet:mainfrom
AaronRobinsonMSFT:wasm_umco
Nov 7, 2025
Merged

Add support in WASM for calling UnmanagedCallersOnly functions#121359
radekdoulik merged 16 commits into
dotnet:mainfrom
AaronRobinsonMSFT:wasm_umco

Conversation

@AaronRobinsonMSFT

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

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @mangod9
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.

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
FileDescription
src/coreclr/vm/wasm/helpers.cppAdds reverse thunk lookup infrastructure and GetUnmanagedCallersOnlyThunk implementation
src/coreclr/vm/wasm/callhelpers.hppUpdates header guards and adds reverse thunk data structures
src/coreclr/vm/wasm/callhelpers.cppAdds reverse thunk map array definition with placeholder example
src/coreclr/vm/wasm/calldescrworkerwasm.cppSimplifies to use new ExecuteInterpretedMethodFromUnmanaged function
src/coreclr/vm/prestub.cppRefactors and renames function to ExecuteInterpretedMethodFromUnmanaged
src/coreclr/vm/precode_portable.hppAdds flags for tracking UnmanagedCallersOnly attribute
src/coreclr/vm/precode_portable.cppImplements HasUnmanagedCallersOnlyAttribute method with caching
src/coreclr/vm/method.cppUpdates TryGetMultiCallableAddrOfCode to handle UnmanagedCallersOnly
src/coreclr/vm/interpexec.hAdds forward declaration for ExecuteInterpretedMethodFromUnmanaged
src/coreclr/vm/corhost.cppEnables UnmanagedCallersOnly for WASM in CreateDelegate
src/coreclr/interpreter/compiler.cppUses aggregate initialization instead of memset
src/coreclr/hosts/corerun/CMakeLists.txtConditionalizes WASM_BIGINT flag for browser-only scenarios

Comment threadsrc/coreclr/vm/wasm/helpers.cpp
Comment threadsrc/coreclr/vm/wasm/callhelpers.cpp Outdated
Comment threadsrc/coreclr/vm/precode_portable.cpp Outdated
@AaronRobinsonMSFT
AaronRobinsonMSFT marked this pull request as draft November 5, 2025 00:19
@AaronRobinsonMSFT

Copy link
Copy Markdown
MemberAuthor

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

Comment threadsrc/coreclr/interpreter/compiler.cpp Outdated
Comment threadsrc/coreclr/vm/wasm/helpers.cpp Outdated
Comment threadsrc/coreclr/vm/method.cpp
@radekdoulik

radekdoulik commented Nov 6, 2025

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

Comment threadsrc/coreclr/vm/corhost.cpp
Comment threadsrc/coreclr/vm/method.cpp Outdated
Comment threadsrc/coreclr/vm/method.cpp
Comment threadsrc/coreclr/vm/precode_portable.hpp Outdated
Comment threadsrc/coreclr/vm/prestub.cpp
Comment threadsrc/coreclr/vm/comdelegate.cpp
Comment threadsrc/coreclr/vm/wasm/calldescrworkerwasm.cpp

@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.

Thanks

@AaronRobinsonMSFT

Copy link
Copy Markdown
MemberAuthor

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

/ba-g unrelated timeouts

@radekdoulik
radekdoulik merged commit 6e1e6b1 into dotnet:mainNov 7, 2025
132 of 156 checks passed
@radekdoulik

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

pavelsavara commented Nov 7, 2025

Copy link
Copy Markdown
Member

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

Comment threadsrc/coreclr/hosts/corerun/CMakeLists.txt
@AaronRobinsonMSFT
AaronRobinsonMSFT deleted the wasm_umco branch November 7, 2025 16:47
Comment threadsrc/coreclr/vm/dllimport.cpp
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

arch-wasmWebAssembly architecturearea-VM-coreclr

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[browser][coreCLR] enable [UnmanagedCallersOnly]

6 participants

@AaronRobinsonMSFT@radekdoulik@pavelsavara@am11@jkotas