Skip to content

[interp] Fix wasm-interp stack cleanup for tail calls - #2835

Merged
sbc100 merged 5 commits into
WebAssembly:mainfrom
KKiiim:fix-interp-return-call
Sep 14, 2026
Merged

sbc100 merged 5 commits into
WebAssembly:mainfrom
KKiiim:fix-interp-return-call

Conversation

@KKiiim

@KKiiim KKiiim commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fix incorrect value-stack cleanup when lowering return_call and return_call_indirect in wasm-interp.

In wasm-interp, all call frames share a single flat value stack. When performing a tail call, InterpDropKeep is emitted to drop the current frame's locals and intermediate stack values while keeping the arguments to be passed to the callee.

GetReturnCallDropKeepCount calculates drop_count based on the validator's current type-stack height. Previously, validator_.OnReturnCall* was invoked before GetReturnCallDropKeepCount, which immediately popped the call arguments from the type stack. Consequently, drop_count was undercounted by the number of call arguments, leaving dead stack values behind that could corrupt subsequent operations (e.g., being incorrectly observed as the caller's return value).

Fix

  1. In OnReturnCallExpr / OnReturnCallIndirectExpr, if the function/type index is out of bounds, delegate directly to validator_.OnReturnCall* to report the standard validation error.
  2. On valid paths, compute GetReturnCallDropKeepCount and GetCatchCount before invoking validator_.OnReturnCall*, ensuring the call arguments are still present on the validator's type stack during cleanup calculation.

Tests

  • Added return-call-stack-cleanup.txt and return-call-indirect-stack-cleanup.txt covering both direct and indirect tail calls with unconsumed stack values.
  • Verified against existing interpreter, validation, and spec test suites.

@KKiiim
KKiiim force-pushed the fix-interp-return-call branch 2 times, most recently from a1d3749 to fee30e3 Compare August 25, 2026 09:29
Comment thread src/interp/binary-reader-interp.cc Resolved
Comment thread src/interp/binary-reader-interp.cc Resolved
@KKiiim

KKiiim commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor Author

Yes. Thanks for your advice.

Current change: Compute and emit the correct drop_keep cleanup before validation consumes the tail-call arguments.

This bug can be reproduced using the function f. f should return 0, not 777.

(module
  (type $i_i (func (param i32) (result i32)))
  (import "host" "print" (func $imported (type $i_i)))

  (func $tail (result i32)
    i32.const 777
    i32.const 0
    return_call $imported)

  (func (export "f") (result i32)
    (local i32)
    call $tail
    local.get 0
    return)
)

@KKiiim
KKiiim force-pushed the fix-interp-return-call branch 2 times, most recently from 1b8220a to 6fe4e91 Compare August 26, 2026 05:52
@KKiiim
KKiiim force-pushed the fix-interp-return-call branch from 6fe4e91 to e430599 Compare August 26, 2026 06:23
@KKiiim
KKiiim requested a review from sbc100 August 26, 2026 06:33
@KKiiim

KKiiim commented Aug 28, 2026

Copy link
Copy Markdown
Contributor Author

@sbc100 Hi, could you help to take a look. Thanks!

Comment thread src/interp/binary-reader-interp.cc Resolved
Comment thread src/interp/binary-reader-interp.cc Resolved
@KKiiim
KKiiim force-pushed the fix-interp-return-call branch from 57ec36c to e7a2126 Compare August 31, 2026 06:09
@KKiiim
KKiiim requested a review from sbc100 August 31, 2026 06:20
Comment thread src/interp/binary-reader-interp.cc Resolved
Comment thread src/interp/binary-reader-interp.cc Resolved
@sbc100
sbc100 requested a review from zherczeg August 31, 2026 18:56

@zherczeg zherczeg left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I don't understand the description. These are return calls, there is nothing on the stack after they are executed. Is this a call frame problem? I would like a bit more information what is happening here.

Comment thread src/interp/binary-reader-interp.cc Resolved
Comment thread src/interp/binary-reader-interp.cc Resolved
@zherczeg

Copy link
Copy Markdown
Collaborator

This bug can be reproduced using the function f. f should return 0, not 777. - this is also unclear to me. Normally, neither 0 nor 777 should be returned. The imported function should return with something, and the rest should be dropped.

@zherczeg

zherczeg commented Sep 1, 2026

Copy link
Copy Markdown
Collaborator

Current code:
https://github.com/WebAssembly/wabt/blob/main/src/interp/binary-reader-interp.cc#L1239
The OnReturnCallExpr calls validator_.OnReturnCall twice. The first one was added to avoid some fuzzer bugs. Both modify the type stack. I don't understand why this is correct.

I don't know the code, but I suspect the problem is the following. The original code called validator_.OnReturnCall after calling GetReturnCallDropKeepCount. However, this depends on a validated func_index. A fuzzer found this, then the first validator_.OnReturnCall was added at the beginning of the function, as it should be. I think the solution is removing the second validator_.OnReturnCall, and GetReturnCallDropKeepCount should use the results of the called function, not the params.

@zherczeg

zherczeg commented Sep 1, 2026

Copy link
Copy Markdown
Collaborator

Btw you could also check OnReturnCallRefExpr. The catch update is missing there, so the code is probably just wrong.

@KKiiim

KKiiim commented Sep 1, 2026

Copy link
Copy Markdown
Contributor Author

@zherczeg Thanks for the review and questions! Let me clarify what is happening here:

  1. Why InterpDropKeep is needed for return_call (stack / call frame cleanup):
    In wasm-interp, all call frames share a single flat value stack (values_). When a function performs a tail call, caller-allocated locals and unconsumed temporary operands on the stack below the outgoing arguments need to be discarded, while keeping the callee arguments on top of the stack. InterpDropKeep shifts the kept arguments down to overwrite the current frame's locals/temporaries before transferring control.

  2. Why keep_count must be the callee's params, not results:
    At the point of executing a return_call, the callee has not been executed yet. The values currently on top of the value stack are the arguments/parameters to be passed into the callee (plus the table index for indirect calls). If we kept results instead, any arguments beyond the result count would be dropped before entering the callee.

  3. Why validator_.OnReturnCall cannot be called before computing drop_count:
    validator_.OnReturnCall mutates the validator's type_stack_ by popping the call parameters and marking the block unreachable. If we validate first, type_stack_size() is already decremented by param_count, causing GetReturnCallDropKeepCount to undercount drop_count and leave dead stack values behind (which was the bug fixed in this PR).
    Therefore, we must compute GetReturnCallDropKeepCount while the operands are still on the type stack, followed by validator_.OnReturnCall.

  4. Regarding OnReturnCallRefExpr:
    Good catch. OnReturnCallRefExpr is currently missing full drop_keep and catch_drop handling in the interpreter. But that's not the purpose of the current bug fix.

@KKiiim
KKiiim requested review from sbc100 and zherczeg September 1, 2026 08:04
@zherczeg

zherczeg commented Sep 1, 2026

Copy link
Copy Markdown
Collaborator

In general, the validator should run first, because everything is assumed to be correct after it. Please don't forget the validator is not only used by this project, and changing this general rule can cause confusion.

Ideally, something like this should happen:

  1. Run the validator. Then it consumes the params of the called function from the type stack, and replace them with the results.
  2. If validation passes, you know the results are on the type stack, and you can compute the number of unnecessary values using the result size (that was my recommended change).
  3. The number of unnecessary values are the same before the call, so remove them. Also drop the catches.

@KKiiim

KKiiim commented Sep 1, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback! I understand the design principle that validation should ideally precede lowering. However unlike a regular call, return_call does not push results onto the type stack. Instead, TypeChecker::OnReturnCall marks the block unreachable and immediately clears the type stack via SetUnreachable().

At runtime, the interpreter stack also holds the outgoing params (not results), which InterpDropKeep needs to keep while dropping values below them.

Because OnReturnCall pops those params and resets the type stack, we must compute the drop/keep count before OnReturnCall mutates the stack. If the index is invalid, we bail directly into OnReturnCall so validation still handles the error.

@zherczeg

zherczeg commented Sep 1, 2026

Copy link
Copy Markdown
Collaborator

Maybe an example could help. Stack below grows left from right, t(i) represents removable values, p(i) represents parameters, r(i) represents results. Lets assume the called function has 3 parameters, and two results.

After the validation the type stack changes the following way:
[t0, t1, t2, p0, p1,p2] -> [t0, t1, t2, r0, r1]

Since you know the function has two results, and the type stack size is 5, you can compute that 3 (=5-2) values will never be used (and should be discarded). You also know, this is also true, before the call is executed. Copy the values back, and the stack now contains [p0, p1,p2]. Then do the call.

@zherczeg

zherczeg commented Sep 1, 2026

Copy link
Copy Markdown
Collaborator

An idea: store the validator_.GetLocalCount() in a local variable before the validation. This operation does not need any checks. Then perform the validation. Then the function is validated, so the dropped values can be computed. Needs some code refactor.

Comment on lines +467 to +469
*out_drop_count =
(orig_type_stack_size >= keep_count ? orig_type_stack_size - keep_count
: 0) +

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Same to GetDropCount.
// The keep_count may be larger than the type_stack_count if the typechecker
// is currently unreachable. In that case, it doesn't matter what value we
// drop, but 0 is a reasonable choice.

@zherczeg zherczeg left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@KKiiim

KKiiim commented Sep 7, 2026

Copy link
Copy Markdown
Contributor Author

@sbc100 Hi, could you take a look again. Looking forward to a new release, thanks

@sbc100
sbc100 merged commit a32dcf8 into WebAssembly:main Sep 14, 2026
17 checks passed
@KKiiim

KKiiim commented Sep 15, 2026

Copy link
Copy Markdown
Contributor Author

@sbc100 @zherczeg Hi maintainers. Could you release a new version? Thanks.

@sbc100

sbc100 commented Sep 15, 2026

Copy link
Copy Markdown
Member

#2859

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants