Repository navigation
[wasm] Fix null shadow stack pointer in reverse-pinvoke calls into the interpreter - #133894
Merged
pavelsavara merged 1 commit intoSep 15, 2026
Merged
Conversation
…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: 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. |
Contributor
|
Tagging subscribers to this area: @JulieLeeMSFT, @BrzVlad, @janvorli |
Contributor
|
Tagging subscribers to 'arch-wasm': @lewing, @pavelsavara |
|
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. |
Contributor
There was a problem hiding this comment.
🔵 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
pavelsavara
enabled auto-merge (squash)
September 14, 2026 19:51
Member
Author
|
/ba-g CI infra and unrelated failures |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
On WebAssembly (CoreCLR R2R + interpreter), a
[UnmanagedCallersOnly](reverse-pinvoke) method that re-enters managed code can crash withRuntimeError: memory access out of bounds. This is hit by real apps — e.g. Avalonia/SkiaSharp, where native SkiaSharp calls back theSKManagedStream[UnmanagedCallersOnly]proxies, which then call interpreted managed code:Root cause
A reverse-pinvoke method establishes its shadow stack pointer in the prolog by loading the
__stack_pointerglobal intolvaWasmSpArg(genAllocLclFrame). But that local was not markedlvHasExplicitInit. During value numbering, a live-in non-parameter local whose method has.locals initand nolvHasExplicitInitis treated as zero-initialized and value-numbered to0. The optimizer then constant-folds theWasmShadowStackPointerargument of outgoing managed calls (gtNewLclVarNode(lvaWasmSpArg)) toi32.const 0.When such a call targets interpreted code, the R2R→interpreter thunk computes
sp - frameSizeand 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
GCHandlevia a shared generic +castclass+ virtual dispatch). Straight-line reverse-pinvoke methods thread the real$spcorrectly and were unaffected — which is why increasing the stack size did not help.Byte-level, for the reproducing method's outgoing calls:
i32.const 0local.get <sp>Fix
Mark the reverse-pinvoke shadow-SP local
lvHasExplicitInitinlvaAllocWasmStackPtr. This gives it an opaqueVNF_InitValat 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 SkiaSharpSKManagedStreamshape (a[UnmanagedCallersOnly]entry that resolves aGCHandlethrough a shared generic +castclassand calls a virtual method whose override is interpreted), plus a straight-line control. It fails withmemory access out of boundsbefore the fix and passes after.Validation
Built the fixed cross-targeting wasm JIT, re-crossgen'd, and ran
WasmInterpreterTransitionsunder R2R on Node/corerun:PASSED(wasmemory access out of bounds). Confirmed at the wasm level that the outgoing shadow-SP argument changes fromi32.const 0tolocal.get <sp>.Note
This pull request was authored with the assistance of GitHub Copilot.