Commit 38981d09

38981d09ef80Merged

Yusuke Endoh <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

fix(codegen): SP_GC_ROOT map accumulators against deep-nested allocation

A nested map block — outer iteration allocates many inner objects via sp_*new — can trigger a GC pass between pushes into the outer accumulator. Without rooting the accumulator, GC frees it as unreachable; the next sp*Array_push then writes into freed memory and corrupts malloc bookkeeping, surfacing as a SIGSEGV in _int_malloc on the next allocation. Originally hit while generating optcarrot's TILE_LUT (each outer iteration allocates ~64K inner IntArrays).

Emit SP_GC_ROOT immediately after sp_*Array_new in every compile_map_expr branch that builds an accumulator on a master- supported recv:

  • int_array / sym_array recv, string-return block (StrArray accum)
  • int_array / sym_array recv, int-return block (IntArray accum)
  • str_array recv, int-return block (IntArray accum)
  • str_array recv, string-return block (StrArray accum)

Future map branches (Range, ptr_array, poly_array) added by other PRs should apply the same one-liner.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
38981d09ef80
Parent
d920667f4cc7

spinel_codegen.rbmodifiedView file

@@ -25867,6 +25867,12 @@
2586725867 if block_ret == "string"
2586825868 @needs_str_array = 1
2586925869 emit(" sp_StrArray *" + tmp_arr + " = sp_StrArray_new();")
25870+ # Root the accumulator: a nested map block (e.g. each
25871+ # iteration allocates many inner objects via sp_*_new) can
25872+ # trigger GC between pushes; without rooting, the outer
25873+ # accumulator is collected mid-loop and the next push
25874+ # corrupts the freed buffer.
25875+ emit(" SP_GC_ROOT(" + tmp_arr + ");")
2587025876 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_IntArray_length(" + rc + "); " + tmp_i + "++) {")
2587125877 if bp_is_lambda == 1
2587225878 emit(" sp_Val * lv_" + bp1 + " = (sp_Val *)sp_IntArray_get(" + rc + ", " + tmp_i + ");")
@@ -25892,6 +25898,7 @@
2589225898 return tmp_arr
2589325899 else
2589425900 emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")
25901+ emit(" SP_GC_ROOT(" + tmp_arr + ");")
2589525902 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_IntArray_length(" + rc + "); " + tmp_i + "++) {")
2589625903 if bp_is_lambda == 1
2589725904 emit(" sp_Val * lv_" + bp1 + " = (sp_Val *)sp_IntArray_get(" + rc + ", " + tmp_i + ");")
@@ -25938,6 +25945,7 @@
2593825945 if block_ret == "int"
2593925946 @needs_int_array = 1
2594025947 emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")
25948+ emit(" SP_GC_ROOT(" + tmp_arr + ");")
2594125949 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_StrArray_length(" + rc + "); " + tmp_i + "++) {")
2594225950 # Declare lv_<bp> with the block-local type inside the for body
2594325951 # so it C-shadows any outer same-named local (Ruby block-local
@@ -25967,6 +25975,7 @@
2596725975 end
2596825976 @needs_str_array = 1
2596925977 emit(" sp_StrArray *" + tmp_arr + " = sp_StrArray_new();")
25978+ emit(" SP_GC_ROOT(" + tmp_arr + ");")
2597025979 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_StrArray_length(" + rc + "); " + tmp_i + "++) {")
2597125980 # Block-local C decl, see note in the int-block branch above.
2597225981 emit(" const char *lv_" + bp1 + " = sp_StrArray_get(" + rc + ", " + tmp_i + ");")