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

@@ -20129,9 +20129,6 @@
2012920129 # Array methods
2013020130 if recv_type == "int_array" || recv_type == "sym_array"
2013120131 if mname == "length" || mname == "size"
20132- if @hoisted_strlen_var != "" && @hoisted_strlen_recv == rc
20133- return @hoisted_strlen_var
20134- end
2013520132 return "sp_IntArray_length(" + rc + ")"
2013620133 end
2013720134 if mname == "[]"
@@ -20513,9 +20510,6 @@
2051320510 # Float array methods
2051420511 if recv_type == "float_array"
2051520512 if mname == "length"
20516- if @hoisted_strlen_var != "" && @hoisted_strlen_recv == rc
20517- return @hoisted_strlen_var
20518- end
2051920513 return "sp_FloatArray_length(" + rc + ")"
2052020514 end
2052120515 if mname == "[]"
@@ -20587,9 +20581,6 @@
2058720581 elem_type = ptr_array_elem_type(recv_type)
2058820582 ct = c_type(elem_type)
2058920583 if mname == "length" || mname == "size"
20590- if @hoisted_strlen_var != "" && @hoisted_strlen_recv == rc
20591- return @hoisted_strlen_var
20592- end
2059320584 return "sp_PtrArray_length(" + rc + ")"
2059420585 end
2059520586 if mname == "[]"
@@ -20686,15 +20677,9 @@
2068620677 end
2068720678 if recv_type == "str_array"
2068820679 if mname == "length"
20689- if @hoisted_strlen_var != "" && @hoisted_strlen_recv == rc
20690- return @hoisted_strlen_var
20691- end
2069220680 return "sp_StrArray_length(" + rc + ")"
2069320681 end
2069420682 if mname == "size"
20695- if @hoisted_strlen_var != "" && @hoisted_strlen_recv == rc
20696- return @hoisted_strlen_var
20697- end
2069820683 return "sp_StrArray_length(" + rc + ")"
2069920684 end
2070020685 if mname == "[]"
@@ -20889,9 +20874,10 @@
2088920874 return "sp_SymIntHash_has_key((sp_SymIntHash *)(" + rc + "), " + compile_arg0_as_sym(nid) + ")"
2089020875 end
2089120876 if mname == "length" || mname == "size" || (mname == "count" && @nd_block[nid] < 0 && @nd_arguments[nid] < 0)
20892- if @hoisted_strlen_var != "" && @hoisted_strlen_recv == rc
20893- return @hoisted_strlen_var
20894- end
20877+ # Don't reuse @hoisted_strlen_var for a hash receiver -- the
20878+ # cache was computed via strlen() on a string and reading
20879+ # `h->len` field through that cache value would be junk.
20880+ # Issue #730.
2089520881 return "sp_SymIntHash_length((sp_SymIntHash *)(" + rc + "))"
2089620882 end
2089720883 if mname == "empty?"
@@ -21068,9 +21054,6 @@
2106821054 return "sp_SymStrHash_has_key((sp_SymStrHash *)(" + rc + "), " + compile_arg0_as_sym(nid) + ")"
2106921055 end
2107021056 if mname == "length" || mname == "size" || (mname == "count" && @nd_block[nid] < 0 && @nd_arguments[nid] < 0)
21071- if @hoisted_strlen_var != "" && @hoisted_strlen_recv == rc
21072- return @hoisted_strlen_var
21073- end
2107421057 return "sp_SymStrHash_length((sp_SymStrHash *)(" + rc + "))"
2107521058 end
2107621059 if mname == "empty?"
@@ -21431,9 +21414,6 @@
2143121414 return "sp_StrIntHash_has_key(" + rc + ", " + compile_str_arg0(nid) + ")"
2143221415 end
2143321416 if mname == "length" || mname == "size" || (mname == "count" && @nd_block[nid] < 0 && @nd_arguments[nid] < 0)
21434- if @hoisted_strlen_var != "" && @hoisted_strlen_recv == rc
21435- return @hoisted_strlen_var
21436- end
2143721417 return "sp_StrIntHash_length(" + rc + ")"
2143821418 end
2143921419 if mname == "empty?"
@@ -21679,9 +21659,6 @@
2167921659 return "sp_StrStrHash_has_key(" + rc + ", " + compile_str_arg0(nid) + ")"
2168021660 end
2168121661 if mname == "length" || mname == "size" || (mname == "count" && @nd_block[nid] < 0 && @nd_arguments[nid] < 0)
21682- if @hoisted_strlen_var != "" && @hoisted_strlen_recv == rc
21683- return @hoisted_strlen_var
21684- end
2168521662 return "sp_StrStrHash_length(" + rc + ")"
2168621663 end
2168721664 if mname == "empty?"