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

@@ -27004,6 +27004,7 @@
2700427004 @needs_str_array = 1
2700527005 tmp = new_temp
2700627006 emit(" sp_StrArray *" + tmp + " = sp_StrArray_new();")
27007+ emit(" SP_GC_ROOT(" + tmp + ");")
2700727008 k = 0
2700827009 while k < elems.length
2700927010 emit(" sp_StrArray_push(" + tmp + ", " + compile_expr(elems[k]) + ");")
@@ -27015,6 +27016,7 @@
2701527016 @needs_rb_value = 1
2701627017 tmp = new_temp
2701727018 emit(" sp_PolyArray *" + tmp + " = sp_PolyArray_new();")
27019+ emit(" SP_GC_ROOT(" + tmp + ");")
2701827020 k = 0
2701927021 while k < elems.length
2702027022 et = infer_type(elems[k])
@@ -27085,6 +27087,7 @@
2708527087 @needs_float_array = 1
2708627088 tmp = new_temp
2708727089 emit(" sp_FloatArray *" + tmp + " = sp_FloatArray_new();")
27090+ emit(" SP_GC_ROOT(" + tmp + ");")
2708827091 k = 0
2708927092 while k < elems.length
2709027093 emit(" sp_FloatArray_push(" + tmp + ", " + compile_expr(elems[k]) + ");")
@@ -27228,6 +27231,7 @@
2722827231 @needs_int_str_hash = 1
2722927232 tmp = new_temp
2723027233 emit(" sp_IntStrHash *" + tmp + " = sp_IntStrHash_new();")
27234+ emit(" SP_GC_ROOT(" + tmp + ");")
2723127235 elems.each { |el|
2723227236 if @nd_type[el] == "AssocNode"
2723327237 emit(" sp_IntStrHash_set(" + tmp + ", " + compile_expr(@nd_key[el]) + ", " + compile_expr(@nd_expression[el]) + ");")
@@ -27239,6 +27243,7 @@
2723927243 @needs_str_str_hash = 1
2724027244 tmp = new_temp
2724127245 emit(" sp_StrStrHash *" + tmp + " = sp_StrStrHash_new();")
27246+ emit(" SP_GC_ROOT(" + tmp + ");")
2724227247 elems.each { |el|
2724327248 if @nd_type[el] == "AssocNode"
2724427249 vt = infer_type(@nd_expression[el])
@@ -27263,6 +27268,7 @@
2726327268 @needs_sym_int_hash = 1
2726427269 tmp = new_temp
2726527270 emit(" sp_SymIntHash *" + tmp + " = sp_SymIntHash_new();")
27271+ emit(" SP_GC_ROOT(" + tmp + ");")
2726627272 elems.each { |el|
2726727273 if @nd_type[el] == "AssocNode"
2726827274 v_syh = compile_expr(@nd_expression[el])
@@ -27279,6 +27285,7 @@
2727927285 @needs_sym_str_hash = 1
2728027286 tmp = new_temp
2728127287 emit(" sp_SymStrHash *" + tmp + " = sp_SymStrHash_new();")
27288+ emit(" SP_GC_ROOT(" + tmp + ");")
2728227289 elems.each { |el|
2728327290 if @nd_type[el] == "AssocNode"
2728427291 emit(" sp_SymStrHash_set(" + tmp + ", " + compile_expr(@nd_key[el]) + ", " + compile_expr(@nd_expression[el]) + ");")
@@ -27290,6 +27297,7 @@
2729027297 @needs_rb_value = 1
2729127298 tmp = new_temp
2729227299 emit(" sp_SymPolyHash *" + tmp + " = sp_SymPolyHash_new();")
27300+ emit(" SP_GC_ROOT(" + tmp + ");")
2729327301 elems.each { |el|
2729427302 if @nd_type[el] == "AssocNode"
2729527303 emit(" sp_SymPolyHash_set(" + tmp + ", " + compile_expr(@nd_key[el]) + ", " + box_expr_to_poly(@nd_expression[el]) + ");")
@@ -27301,6 +27309,7 @@
2730127309 @needs_rb_value = 1
2730227310 tmp = new_temp
2730327311 emit(" sp_StrPolyHash *" + tmp + " = sp_StrPolyHash_new();")
27312+ emit(" SP_GC_ROOT(" + tmp + ");")
2730427313 elems.each { |el|
2730527314 if @nd_type[el] == "AssocNode"
2730627315 emit(" sp_StrPolyHash_set(" + tmp + ", " + compile_expr_as_string(@nd_key[el]) + ", " + box_expr_to_poly(@nd_expression[el]) + ");")
@@ -27316,6 +27325,7 @@
2731627325 @needs_poly_poly_hash = 1
2731727326 tmp = new_temp
2731827327 emit(" sp_PolyPolyHash *" + tmp + " = sp_PolyPolyHash_new();")
27328+ emit(" SP_GC_ROOT(" + tmp + ");")
2731927329 elems.each { |el|
2732027330 if @nd_type[el] == "AssocNode"
2732127331 emit(" sp_PolyPolyHash_set(" + tmp + ", " + box_expr_to_poly(@nd_key[el]) + ", " + box_expr_to_poly(@nd_expression[el]) + ");")
@@ -27326,6 +27336,7 @@
2732627336 @needs_str_int_hash = 1
2732727337 tmp = new_temp
2732827338 emit(" sp_StrIntHash *" + tmp + " = sp_StrIntHash_new();")
27339+ emit(" SP_GC_ROOT(" + tmp + ");")
2732927340 elems.each { |el|
2733027341 if @nd_type[el] == "AssocNode"
2733127342 v_sih = compile_expr(@nd_expression[el])