Skip to content

[wasm][coreclr] Cache calli cookies#127016

Merged
radekdoulik merged 24 commits intodotnet:mainfrom
radekdoulik:pe-cookie-cache
May 5, 2026
Merged

[wasm][coreclr] Cache calli cookies#127016
radekdoulik merged 24 commits intodotnet:mainfrom
radekdoulik:pe-cookie-cache

Conversation

@radekdoulik
Copy link
Copy Markdown
Member

@radekdoulik radekdoulik commented Apr 16, 2026

Avoid repeated expensive calls to get calli cookie by caching it

Checked in HelloWorld

CalliCookie cache: 18 hits, 17 misses, 35 total (51.4% hit rate)

Avoid repeated expensive calls to get calli cookie by caching it
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

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

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

This PR reduces overhead in the WASM interpreter call path by caching the computed calli cookie on the MethodDesc, avoiding repeated (and potentially expensive) cookie computation for repeated invocations of the same managed method.

Changes:

  • Cache and reuse a per-MethodDesc calli cookie in InvokeManagedMethod on WASM.
  • Extend MethodDescCodeData (portable entrypoints + interpreter builds) to store a CalliCookie.
  • Add MethodDesc::{GetCalliCookie, SetCalliCookie} APIs to manage the cached value thread-safely.

Reviewed changes

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

File Description
src/coreclr/vm/wasm/helpers.cpp Uses MethodDesc-cached cookie to avoid repeated GetCookieForCalliSig calls.
src/coreclr/vm/method.hpp Adds CalliCookie storage in MethodDescCodeData and declares accessor APIs under the relevant feature flags.
src/coreclr/vm/method.cpp Implements thread-safe cookie get/set using EnsureCodeDataExists + interlocked/volatile operations.

Comment thread src/coreclr/vm/wasm/helpers.cpp
Comment thread src/coreclr/vm/wasm/helpers.cpp
Comment thread src/coreclr/vm/method.cpp Outdated
…Cookie typedef

- Define InterpreterCalliCookie typedef: function pointer on WASM,
  CallStubHeader* on non-WASM (Jan's feedback)
- Unify SetCalliCookie/GetCalliCookie API, removing the separate
  SetCallStub/GetCallStub and FEATURE_PORTABLE_ENTRYPOINTS branching
  in MethodDesc
- Cache calli cookie on targetMethod in the WASM calli path in
  interpexec.cpp (Jan's feedback)
- Re-read cookie after SetCalliCookie in wasm/helpers.cpp to use
  the race-winning value (Aaron's feedback)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 26, 2026 20:29
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 5 out of 5 changed files in this pull request and generated 3 comments.

Comment thread src/coreclr/vm/wasm/helpers.cpp
Comment thread src/coreclr/vm/jitinterface.cpp Outdated
Comment thread src/coreclr/vm/interpexec.cpp Outdated
Comment thread src/coreclr/vm/interpexec.cpp Outdated
Copilot AI review requested due to automatic review settings April 27, 2026 13:09
Copilot AI review requested due to automatic review settings May 3, 2026 19:59
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 10 out of 10 changed files in this pull request and generated 2 comments.

Comment thread src/coreclr/vm/method.hpp
Comment thread src/mono/browser/build/BrowserWasmApp.CoreCLR.targets Outdated
Comment thread src/coreclr/vm/precode_portable.hpp Outdated
Comment thread src/coreclr/vm/wasm/callhelpers-interp-to-managed.cpp Outdated
Comment thread src/coreclr/vm/wasm/callhelpers-interp-to-managed.cpp Outdated
Co-authored-by: Jan Kotas <jkotas@microsoft.com>
Copilot AI review requested due to automatic review settings May 4, 2026 10:54
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 10 out of 10 changed files in this pull request and generated 1 comment.

Comment thread src/coreclr/vm/jitinterface.cpp
Copilot AI review requested due to automatic review settings May 4, 2026 12:35
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 10 out of 10 changed files in this pull request and generated 3 comments.

Comment thread src/coreclr/vm/jitinterface.cpp
Comment thread src/tasks/WasmAppBuilder/coreclr/InterpToNativeGenerator.cs
Comment thread src/coreclr/vm/wasm/callhelpers-pinvoke.cpp
@radekdoulik
Copy link
Copy Markdown
Member Author

/ba-g infra issues

@radekdoulik radekdoulik merged commit 7018450 into dotnet:main May 5, 2026
122 of 126 checks passed
kotlarmilos added a commit that referenced this pull request May 7, 2026
…127876)

## Problem

PR #127016 updated `MethodDescCodeData::CallStub` added a new writer in
`CInterpreterJitInfo::GetCookieForInterpreterCalliSig` that caches a
calli signature `CallStubHeader*` on the P/Invoke target `MethodDesc`.

PR #127016 added a small "calli cookie" cache. Each managed method has
one slot where this cookie is stored. The problem is that two different
parts of the runtime now write to that one slot:
- The interpreter-to-native call path stores a helper that's set up for
the method's own signature
- The new code in #127016 stores a helper that's set up for the
signature of a `calli` instruction inside an IL stub.
 
On iOS, when an app tries to execute address 0 the kernel does not give
us a normal crash. It assumes the page is unsigned/tampered and kills
the process with `SIGKILL` and a `CODESIGNING / Invalid Page` reason.

## Proposed fix

Drop the new `pContextMD->{Set,Get}CalliCookie` cache reads/writes in
`CInterpreterJitInfo::GetCookieForInterpreterCalliSig`. The function now
always computes the cookie via `GetCookieForCalliSig(sig, pContextMD)`,
matching pre-#127016 behavior.

Fixes #127869 #127863 #127867

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

4 participants