Skip to content

[Wasm RyuJit] Zero init gc shadow stack slots in the prolog#129073

Open
AndyAyersMS wants to merge 3 commits into
dotnet:mainfrom
AndyAyersMS:fix-wasm-r2r-prolog-zero-init
Open

[Wasm RyuJit] Zero init gc shadow stack slots in the prolog#129073
AndyAyersMS wants to merge 3 commits into
dotnet:mainfrom
AndyAyersMS:fix-wasm-r2r-prolog-zero-init

Conversation

@AndyAyersMS
Copy link
Copy Markdown
Member

We must explicitly zero GC ref slots on the shadow stack in the prolog. Do this via a single memory.fill.

Copilot AI review requested due to automatic review settings June 6, 2026 14:42
@github-actions github-actions Bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Jun 6, 2026
@AndyAyersMS
Copy link
Copy Markdown
Member Author

@dotnet/wasm-contrib PTAL
@kg this may explain some of odd GC errors you were seeing
fyi @dotnet/jit-contrib

@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
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 updates the CoreCLR JIT’s WASM backend prolog generation to ensure stack-frame zero-initialization is emitted as a single memory.fill, with the goal of explicitly zeroing GC-ref slots on the WASM shadow stack.

Changes:

  • Add a WASM-specific CodeGen::genZeroInitFrame implementation that emits a single memory.fill.
  • Update genCheckUseBlockInit() so WASM always uses block-init when there are any init slots.
  • Adjust genFnProlog() on WASM to compute the prolog zero-init byte range from memory-resident lvMustInit locals (skipping certain WASM frame-header locals).

Reviewed changes

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

File Description
src/coreclr/jit/codegenwasm.cpp Adds WASM-specific prolog frame zero-init via memory.fill and marks the block-init helper as unreachable.
src/coreclr/jit/codegencommon.cpp Forces block-init selection on WASM and changes prolog range computation to WASM-specific logic.

Comment thread src/coreclr/jit/codegenwasm.cpp
Comment thread src/coreclr/jit/codegencommon.cpp
@kg
Copy link
Copy Markdown
Member

kg commented Jun 6, 2026

With this applied to my local branch, my spilling test scenario works without GC crashes, so I think you're right!

Comment thread src/coreclr/jit/codegencommon.cpp Outdated
Use a single memory.fill to zero the slots.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@AndyAyersMS AndyAyersMS force-pushed the fix-wasm-r2r-prolog-zero-init branch from 45a9808 to 1f9b88e Compare June 6, 2026 15:22
Address PR review feedback: drop the confusing reference to `register-init bookkeeping below`.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 6, 2026 15:36
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 2 out of 2 changed files in this pull request and generated 1 comment.

Comment on lines 3856 to +3866
// Compiler::fgVarNeedsExplicitZeroInit relies on this logic to
// find structs that are guaranteed to be block initialized.
// If this logic changes, Compiler::fgVarNeedsExplicitZeroInit needs
// to be modified.

#ifdef TARGET_64BIT
#ifdef TARGET_WASM

// On WASM we always use a single memory.fill for prolog zeroing.
genUseBlockInit = (genInitStkLclCnt > 0);

#elif defined(TARGET_64BIT)
@AndyAyersMS
Copy link
Copy Markdown
Member Author

Diffs

Seems to be working and showing what we'd expect; memory.fills in the prologs.

Address PR review feedback: genCheckUseBlockInit now uses block-init on
WASM whenever there are any init slots, so fgVarNeedsExplicitZeroInit
should mirror that and skip the per-target size threshold for WASM.

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

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants