Skip to content

[wasm] Fix null shadow stack pointer in reverse-pinvoke calls into the interpreter - #133894

Merged
pavelsavara merged 1 commit into
dotnet:mainfrom
pavelsavara:wasm-reverse-pinvoke-shadow-sp
Sep 15, 2026
Merged

pavelsavara merged 1 commit into
dotnet:mainfrom
pavelsavara:wasm-reverse-pinvoke-shadow-sp

Conversation

@pavelsavara

Copy link
Copy Markdown
Member

Problem

On WebAssembly (CoreCLR R2R + interpreter), a [UnmanagedCallersOnly] (reverse-pinvoke) method that re-enters managed code can crash with RuntimeError: memory access out of bounds. This is hit by real apps — e.g. Avalonia/SkiaSharp, where native SkiaSharp calls back the SKManagedStream [UnmanagedCallersOnly] proxies, which then call interpreted managed code:

RuntimeError: memory access out of bounds
    at WasmR2RToInterpreterThunk(iip)
    at SkiaSharp_..._SKManagedStreamGetLengthProxyImplementation
    ...

Root cause

A reverse-pinvoke method establishes its shadow stack pointer in the prolog by loading the __stack_pointer global into lvaWasmSpArg (genAllocLclFrame). But that local was not marked lvHasExplicitInit. During value numbering, a live-in non-parameter local whose method has .locals init and no lvHasExplicitInit is treated as zero-initialized and value-numbered to 0. The optimizer then constant-folds the WasmShadowStackPointer argument of outgoing managed calls (gtNewLclVarNode(lvaWasmSpArg)) to i32.const 0.

When such a call targets interpreted code, the R2R→interpreter thunk computes sp - frameSize and stores the transition block below a null base → out-of-bounds trap.

This only manifested when the SP local entered SSA (generic/complex methods, e.g. the SkiaSharp proxies that resolve a GCHandle via a shared generic + castclass + virtual dispatch). Straight-line reverse-pinvoke methods thread the real $sp correctly and were unaffected — which is why increasing the stack size did not help.

Byte-level, for the reproducing method's outgoing calls:

shadow-SP argument before after
i32.const 0 local.get <sp>

Fix

Mark the reverse-pinvoke shadow-SP local lvHasExplicitInit in lvaAllocWasmStackPtr. This gives it an opaque VNF_InitVal at entry (like a parameter) so it is never folded to 0, and skips a needless prolog zero-init. Normal (non-reverse-pinvoke) methods are unaffected — there the shadow SP is already a parameter.

Test

Adds a reverse-pinvoke case to src/tests/readytorun/wasm/WasmInterpreterTransitions, mirroring the SkiaSharp SKManagedStream shape (a [UnmanagedCallersOnly] entry that resolves a GCHandle through a shared generic + castclass and calls a virtual method whose override is interpreted), plus a straight-line control. It fails with memory access out of bounds before the fix and passes after.

Validation

Built the fixed cross-targeting wasm JIT, re-crossgen'd, and ran WasmInterpreterTransitions under R2R on Node/corerun: PASSED (was memory access out of bounds). Confirmed at the wasm level that the outgoing shadow-SP argument changes from i32.const 0 to local.get <sp>.

Note

This pull request was authored with the assistance of GitHub Copilot.

…e interpreter

A [UnmanagedCallersOnly] (reverse-pinvoke) method establishes its shadow stack
pointer in the prolog by loading the __stack_pointer global, but the local
holding it (lvaWasmSpArg) was not marked lvHasExplicitInit. The optimizer
therefore treats its use-before-def as zero-initialized and value-numbers it to
0, folding the WasmShadowStackPointer argument of outgoing managed calls to a
null base. When such a call targets interpreted code, the R2R-to-interpreter
thunk stores the transition block below a null base and traps with "memory
access out of bounds".

This only manifested when the SP local entered SSA (e.g. generic/complex methods
such as the SkiaSharp SKManagedStream UnmanagedCallersOnly callbacks), which is
why straight-line reverse-pinvoke methods were unaffected.

Mark the reverse-pinvoke SP local lvHasExplicitInit so it gets an opaque initial
value number (like a parameter) and skips a needless prolog zero-init.

Also adds a reverse-pinvoke case to the WasmInterpreterTransitions R2R test.
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
11 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@pavelsavara pavelsavara added the arch-wasm WebAssembly architecture label Sep 14, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to 'arch-wasm': @lewing, @pavelsavara
See info in area-owners.md if you want to be subscribed.

@pavelsavara
pavelsavara marked this pull request as ready for review September 14, 2026 19:22
Copilot AI lite review requested due to automatic review settings September 14, 2026 19:22
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
11 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI 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.

🔵 Needs a closer look

The JIT/runtime change affects WebAssembly reverse-P/Invoke transitions and warrants final human review.

Pull request overview

Fixes WebAssembly reverse-P/Invoke calls into interpreted managed code by preserving the shadow stack pointer.

Changes:

  • Marks the reverse-P/Invoke shadow-stack local as explicitly initialized.
  • Adds regression tests for callback transition paths.
  • Enables unsafe function pointers in the test project.
File summaries
File Description
src/tests/readytorun/wasm/WasmInterpreterTransitions/WasmInterpreterTransitions.csproj Enables unsafe callback function pointers.
src/tests/readytorun/wasm/WasmInterpreterTransitions/WasmInterpreterTransitions.cs Adds reverse-P/Invoke interpreter transition tests.
src/coreclr/jit/lclvars.cpp Preserves the WebAssembly shadow-stack pointer during optimization.
Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 0
  • Review effort level: Lite

@AndyAyersMS AndyAyersMS 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 for the fix!

Comment thread src/coreclr/jit/lclvars.cpp Resolved
@pavelsavara

Copy link
Copy Markdown
Member Author

/ba-g CI infra and unrelated failures

@pavelsavara
pavelsavara merged commit 8de75c5 into dotnet:main Sep 15, 2026
150 of 153 checks passed
@pavelsavara
pavelsavara deleted the wasm-reverse-pinvoke-shadow-sp branch September 15, 2026 07:48
@pavelsavara pavelsavara added this to the 12.0.0 milestone Sep 15, 2026
jtschuster pushed a commit to jtschuster/runtime that referenced this pull request Sep 18, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 15, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants