Commit ed5a888b
ed5a888bac65Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
fix(codegen): hash/array length stop reusing strlen hoist cache (#730)
The string-side strlen hoisting cache (@hoisted_strlen_var /
@hoisted_strlen_recv) is keyed on the receiver's C expression
("rc") only -- it stores the integer result of strlen() on a
string. Eight non-string arms in compile_call also consulted the
same cache for .length / .size / .count: int_array,
float_array, ptr_array, str_array (twice), sym_str_hash,
str_int_hash, sym_int_hash, str_str_hash. When the same C
expression names appear in both string and non-string contexts
within a single emit (rare but reproducible -- shared param names,
narrowed locals), the non-string arm reused the cached strlen
result and returned the wrong length.
Fix: drop the hoist-cache check from each non-string arm; they now always compute the answer through the type-specific runtime helper (sp_IntArray_length, sp_StrIntHash_length, etc.). The cache is now only consulted in the three legitimate string-recv sites.
472 tests pass; optcarrot checksum 59662. The pre-existing reproducer for this issue is hard to trigger without specific shared-name shapes; the surface fix is in any case the right correction.
Closes #730
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- ed5a888bac65
- Parent
- f78c5792301d
spinel_codegen.rbmodifiedView file
# Array methods if recv_type == "int_array" || recv_type == "sym_array" if mname == "length" || mname == "size" if @hoisted_strlen_var != "" && @hoisted_strlen_recv == rc return @hoisted_strlen_var end return "sp_IntArray_length(" + rc + ")" end if mname == "[]" # Float array methods if recv_type == "float_array" if mname == "length" if @hoisted_strlen_var != "" && @hoisted_strlen_recv == rc return @hoisted_strlen_var end return "sp_FloatArray_length(" + rc + ")" end if mname == "[]" elem_type = ptr_array_elem_type(recv_type) ct = c_type(elem_type) if mname == "length" || mname == "size" if @hoisted_strlen_var != "" && @hoisted_strlen_recv == rc return @hoisted_strlen_var end return "sp_PtrArray_length(" + rc + ")" end if mname == "[]" end if recv_type == "str_array" if mname == "length" if @hoisted_strlen_var != "" && @hoisted_strlen_recv == rc return @hoisted_strlen_var end return "sp_StrArray_length(" + rc + ")" end if mname == "size" if @hoisted_strlen_var != "" && @hoisted_strlen_recv == rc return @hoisted_strlen_var end return "sp_StrArray_length(" + rc + ")" end if mname == "[]" return "sp_SymIntHash_has_key((sp_SymIntHash *)(" + rc + "), " + compile_arg0_as_sym(nid) + ")" end if mname == "length" || mname == "size" || (mname == "count" && @nd_block[nid] < 0 && @nd_arguments[nid] < 0) if @hoisted_strlen_var != "" && @hoisted_strlen_recv == rc return @hoisted_strlen_var end # Don't reuse @hoisted_strlen_var for a hash receiver -- the # cache was computed via strlen() on a string and reading # `h->len` field through that cache value would be junk. # Issue #730. return "sp_SymIntHash_length((sp_SymIntHash *)(" + rc + "))" end if mname == "empty?" return "sp_SymStrHash_has_key((sp_SymStrHash *)(" + rc + "), " + compile_arg0_as_sym(nid) + ")" end if mname == "length" || mname == "size" || (mname == "count" && @nd_block[nid] < 0 && @nd_arguments[nid] < 0) if @hoisted_strlen_var != "" && @hoisted_strlen_recv == rc return @hoisted_strlen_var end return "sp_SymStrHash_length((sp_SymStrHash *)(" + rc + "))" end if mname == "empty?" return "sp_StrIntHash_has_key(" + rc + ", " + compile_str_arg0(nid) + ")" end if mname == "length" || mname == "size" || (mname == "count" && @nd_block[nid] < 0 && @nd_arguments[nid] < 0) if @hoisted_strlen_var != "" && @hoisted_strlen_recv == rc return @hoisted_strlen_var end return "sp_StrIntHash_length(" + rc + ")" end if mname == "empty?" return "sp_StrStrHash_has_key(" + rc + ", " + compile_str_arg0(nid) + ")" end if mname == "length" || mname == "size" || (mname == "count" && @nd_block[nid] < 0 && @nd_arguments[nid] < 0) if @hoisted_strlen_var != "" && @hoisted_strlen_recv == rc return @hoisted_strlen_var end return "sp_StrStrHash_length(" + rc + ")" end if mname == "empty?"