Commit b54d5d50
b54d5d5020f3Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): GC-root hash and array temps before subsequent allocations
Hash literal temps (sp_SymStrHash *_t586 = sp_SymStrHash_new();)
emitted from inline {} literals weren't SP_GC_ROOT'd. When a
following allocation (a second hash literal, a sp_str_concat call,
another helper) triggered GC before the temp was consumed, the
collector reclaimed the temp's heap memory. The next access read
NULL+offset and segfaulted under sustained HTTP load.
Add SP_GC_ROOT(_tN) immediately after every sp_<Hash>_new() in
compile_hash_literal (all six variants -- StrInt, StrStr, StrPoly,
SymInt, SymStr, SymPoly, PolyPoly) and after sp_<Array>_new() in
compile_array_literal_from_ids (str_array, poly_array, float_array).
Rooting at allocation-time matches the pattern named locals already
use (SP_GC_ROOT(lv_foo)); SP_GC_ROOT's cleanup attribute pops the
root when the enclosing scope exits, so per-temp rooting is correct
even when several temps are live simultaneously.
Tests: default 458/0/0, promote 458/0/0, optcarrot checksum 59662. New regression in test/hash_temp_gc_root.rb iterates the failing pattern (two hash literals + filler string allocation, 5000×) to ensure GC pressure doesn't reclaim the un-consumed temp.
Closes #689
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- b54d5d5020f3
- Parent
- 30461528d72c
spinel_codegen.rbmodifiedView file
@needs_str_array = 1 tmp = new_temp emit(" sp_StrArray *" + tmp + " = sp_StrArray_new();") emit(" SP_GC_ROOT(" + tmp + ");") k = 0 while k < elems.length emit(" sp_StrArray_push(" + tmp + ", " + compile_expr(elems[k]) + ");") @needs_rb_value = 1 tmp = new_temp emit(" sp_PolyArray *" + tmp + " = sp_PolyArray_new();") emit(" SP_GC_ROOT(" + tmp + ");") k = 0 while k < elems.length et = infer_type(elems[k]) @needs_float_array = 1 tmp = new_temp emit(" sp_FloatArray *" + tmp + " = sp_FloatArray_new();") emit(" SP_GC_ROOT(" + tmp + ");") k = 0 while k < elems.length emit(" sp_FloatArray_push(" + tmp + ", " + compile_expr(elems[k]) + ");") @needs_int_str_hash = 1 tmp = new_temp emit(" sp_IntStrHash *" + tmp + " = sp_IntStrHash_new();") emit(" SP_GC_ROOT(" + tmp + ");") elems.each { |el| if @nd_type[el] == "AssocNode" emit(" sp_IntStrHash_set(" + tmp + ", " + compile_expr(@nd_key[el]) + ", " + compile_expr(@nd_expression[el]) + ");") @needs_str_str_hash = 1 tmp = new_temp emit(" sp_StrStrHash *" + tmp + " = sp_StrStrHash_new();") emit(" SP_GC_ROOT(" + tmp + ");") elems.each { |el| if @nd_type[el] == "AssocNode" vt = infer_type(@nd_expression[el]) @needs_sym_int_hash = 1 tmp = new_temp emit(" sp_SymIntHash *" + tmp + " = sp_SymIntHash_new();") emit(" SP_GC_ROOT(" + tmp + ");") elems.each { |el| if @nd_type[el] == "AssocNode" v_syh = compile_expr(@nd_expression[el]) @needs_sym_str_hash = 1 tmp = new_temp emit(" sp_SymStrHash *" + tmp + " = sp_SymStrHash_new();") emit(" SP_GC_ROOT(" + tmp + ");") elems.each { |el| if @nd_type[el] == "AssocNode" emit(" sp_SymStrHash_set(" + tmp + ", " + compile_expr(@nd_key[el]) + ", " + compile_expr(@nd_expression[el]) + ");") @needs_rb_value = 1 tmp = new_temp emit(" sp_SymPolyHash *" + tmp + " = sp_SymPolyHash_new();") emit(" SP_GC_ROOT(" + tmp + ");") elems.each { |el| if @nd_type[el] == "AssocNode" emit(" sp_SymPolyHash_set(" + tmp + ", " + compile_expr(@nd_key[el]) + ", " + box_expr_to_poly(@nd_expression[el]) + ");") @needs_rb_value = 1 tmp = new_temp emit(" sp_StrPolyHash *" + tmp + " = sp_StrPolyHash_new();") emit(" SP_GC_ROOT(" + tmp + ");") elems.each { |el| if @nd_type[el] == "AssocNode" emit(" sp_StrPolyHash_set(" + tmp + ", " + compile_expr_as_string(@nd_key[el]) + ", " + box_expr_to_poly(@nd_expression[el]) + ");") @needs_poly_poly_hash = 1 tmp = new_temp emit(" sp_PolyPolyHash *" + tmp + " = sp_PolyPolyHash_new();") emit(" SP_GC_ROOT(" + tmp + ");") elems.each { |el| if @nd_type[el] == "AssocNode" emit(" sp_PolyPolyHash_set(" + tmp + ", " + box_expr_to_poly(@nd_key[el]) + ", " + box_expr_to_poly(@nd_expression[el]) + ");") @needs_str_int_hash = 1 tmp = new_temp emit(" sp_StrIntHash *" + tmp + " = sp_StrIntHash_new();") emit(" SP_GC_ROOT(" + tmp + ");") elems.each { |el| if @nd_type[el] == "AssocNode" v_sih = compile_expr(@nd_expression[el])test/hash_temp_gc_root.rbaddedView file
Loading diff…
test/hash_temp_gc_root.rb.expectedaddedView file
Loading diff…