Fix off-by-one in Select instruction ref tracking#2704
Merged
sbc100 merged 2 commits intoFeb 26, 2026
Conversation
After Pop<u32>() removes the condition value, values_.size() has already decremented. The ref check compares refs_.back() against values_.size(), but at that point values_.size() is one past the index of the value being checked. This causes neither select operand to be detected as a ref, losing GC tracking for the result.
Collaborator
|
The patch looks good. Thank you for finding these and fixing them. |
Exercise typed select with funcref values to verify reference tracking works correctly. Tests cover selecting between two non-null funcrefs, selecting between non-null and null refs, and storing the select result in a global.
Contributor
Author
|
Added a test in
Note that a GC-level test (verifying early collection has visible side effects) would ideally need low-level Thread control to trigger |
sbc100
approved these changes
Feb 26, 2026
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Selectinstruction's ref tracking compares against wrong stack index after popping the condition value, causing reference operands to lose GC tracking.values_.size() - 1instead ofvalues_.size().Details
In the
Selectcase (interp.cc),Pop<u32>()removes the condition from the value stack before the ref checks run. At that point,values_.size()is the index one pastfalse_val, not the index offalse_valitself. Similarly, after poppingfalse_,values_.size()is one pasttrue_val.The
refs_vector stores indices of reference-typed values on the stack. Comparingrefs_.back()againstvalues_.size()(instead ofvalues_.size() - 1) means neither operand is detected as a ref. The result is that when both select operands are references, the pushed result loses its ref tracking, which can cause premature GC collection.The fix changes both comparisons from
values_.size()tovalues_.size() - 1to correctly identify the top-of-stack value.