Skip to content

[Interpreter] Implement StackSwitch execution - #9323

Merged
tlively merged 2 commits into
mainfrom
fix-interpreter-stack-switch
Oct 29, 2026
Merged

tlively merged 2 commits into
mainfrom
fix-interpreter-stack-switch

Conversation

@tlively

@tlively tlively commented Oct 29, 2026

Copy link
Copy Markdown
Member

ModuleRunnerBase::visitStackSwitch previously returned Flow(NONCONSTANT_FLOW), which caused ModuleRunnerBase::callFunction to throw an uncaught NonconstantException when executing a switch instruction under --fuzz-exec or wasm-shell. Implement full interpreter support for switch instructions and (on $tag switch) handlers in resume and resume_throw, distinguishing label handlers from switch handlers and looping in doResume to transfer control directly to the target continuation.

`ModuleRunnerBase::visitStackSwitch` previously returned `Flow(NONCONSTANT_FLOW)`, which caused `ModuleRunnerBase::callFunction` to throw an uncaught `NonconstantException` when executing a `switch` instruction under `--fuzz-exec` or `wasm-shell`. Implement full interpreter support for `switch` instructions and `(on $tag switch)` handlers in `resume` and `resume_throw`, distinguishing label handlers from switch handlers and looping in `doResume` to transfer control directly to the target continuation.
@tlively
tlively requested a review from a team as a code owner October 29, 2026 03:28
@tlively
tlively requested review from stevenfontanella and removed request for a team October 29, 2026 03:28

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request implements stack switching support in the WebAssembly interpreter, enabling fuzzer support and unblocking several spec tests. Key changes include adding STACK_SWITCH_FLOW control flow handling, refactoring continuation resumption, and implementing the visitStackSwitch and doResume logic to support switching between continuations. Additionally, comprehensive tests have been added to verify behavior under various scenarios, such as null continuation traps and switching back and forth. A potential underflow issue was identified in visitStackSwitch when retrieving the last parameter of the continuation's signature, which could lead to an out-of-bounds access if sig.params is empty.

Comment thread src/wasm-interpreter.h
Comment on lines +5331 to +5333
auto sig =
curr->cont->type.getHeapType().getContinuation().type.getSignature();
new_->type = sig.params[sig.params.size() - 1].getHeapType();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

If sig.params is empty, sig.params.size() - 1 will underflow because size() returns an unsigned size_t. This would lead to an out-of-bounds access and undefined behavior. We should assert that sig.params is not empty before accessing its elements.

    auto sig =
      curr->cont->type.getHeapType().getContinuation().type.getSignature();
    assert(sig.params.size() > 0);
    new_->type = sig.params[sig.params.size() - 1].getHeapType();

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

Looks like this fixes current fuzz errors on main?

@tlively

tlively commented Oct 29, 2026

Copy link
Copy Markdown
Member Author

Looks like this fixes current fuzz errors on main?

Yes :)

@stevenfontanella

Copy link
Copy Markdown
Member

LGTM after ASAN build is resolved

@tlively

tlively commented Oct 29, 2026

Copy link
Copy Markdown
Member Author

ASAN build resolved by 1) ignoring leaks due to cycles, like we already do for GCData, etc. and 2) clearing continuation arguments after they are consumed. This avoids building up unbounded chains of expired continuations when passing continuations to other continuations.

@tlively
tlively enabled auto-merge (squash) October 29, 2026 18:30
@tlively
tlively merged commit 05e368e into main Oct 29, 2026
16 of 17 checks passed
@tlively
tlively deleted the fix-interpreter-stack-switch branch October 29, 2026 18:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants