Repository navigation
[interp] Fix wasm-interp stack cleanup for tail calls - #2835
Conversation
a1d3749 to
fee30e3
Compare
|
Yes. Thanks for your advice. Current change: Compute and emit the correct This bug can be reproduced using the function |
1b8220a to
6fe4e91
Compare
6fe4e91 to
e430599
Compare
|
@sbc100 Hi, could you help to take a look. Thanks! |
57ec36c to
e7a2126
Compare
zherczeg
left a comment
There was a problem hiding this comment.
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.
|
|
|
Current code: I don't know the code, but I suspect the problem is the following. The original code called |
|
Btw you could also check |
|
@zherczeg Thanks for the review and questions! Let me clarify what is happening here:
|
|
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:
|
|
Thanks for the feedback! I understand the design principle that validation should ideally precede lowering. However unlike a regular At runtime, the interpreter stack also holds the outgoing Because |
|
Maybe an example could help. Stack below grows left from right, After the validation the type stack changes the following way: 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. |
|
An idea: store the |
| *out_drop_count = | ||
| (orig_type_stack_size >= keep_count ? orig_type_stack_size - keep_count | ||
| : 0) + |
There was a problem hiding this comment.
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.
|
@sbc100 Hi, could you take a look again. Looking forward to a new release, thanks |
Summary
Fix incorrect value-stack cleanup when lowering
return_callandreturn_call_indirectinwasm-interp.In
wasm-interp, all call frames share a single flat value stack. When performing a tail call,InterpDropKeepis emitted to drop the current frame's locals and intermediate stack values while keeping the arguments to be passed to the callee.GetReturnCallDropKeepCountcalculatesdrop_countbased on the validator's current type-stack height. Previously,validator_.OnReturnCall*was invoked beforeGetReturnCallDropKeepCount, which immediately popped the call arguments from the type stack. Consequently,drop_countwas 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
OnReturnCallExpr/OnReturnCallIndirectExpr, if the function/type index is out of bounds, delegate directly tovalidator_.OnReturnCall*to report the standard validation error.GetReturnCallDropKeepCountandGetCatchCountbefore invokingvalidator_.OnReturnCall*, ensuring the call arguments are still present on the validator's type stack during cleanup calculation.Tests
return-call-stack-cleanup.txtandreturn-call-indirect-stack-cleanup.txtcovering both direct and indirect tail calls with unconsumed stack values.