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
if block_ret == "string" @needs_str_array = 1 emit(" sp_StrArray *" + tmp_arr + " = sp_StrArray_new();") # Root the accumulator: a nested map block (e.g. each # iteration allocates many inner objects via sp_*_new) can # trigger GC between pushes; without rooting, the outer # accumulator is collected mid-loop and the next push # corrupts the freed buffer. emit(" SP_GC_ROOT(" + tmp_arr + ");") emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_IntArray_length(" + rc + "); " + tmp_i + "++) {") if bp_is_lambda == 1 emit(" sp_Val * lv_" + bp1 + " = (sp_Val *)sp_IntArray_get(" + rc + ", " + tmp_i + ");") return tmp_arr else emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();") emit(" SP_GC_ROOT(" + tmp_arr + ");") emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_IntArray_length(" + rc + "); " + tmp_i + "++) {") if bp_is_lambda == 1 emit(" sp_Val * lv_" + bp1 + " = (sp_Val *)sp_IntArray_get(" + rc + ", " + tmp_i + ");") if block_ret == "int" @needs_int_array = 1 emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();") emit(" SP_GC_ROOT(" + tmp_arr + ");") emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_StrArray_length(" + rc + "); " + tmp_i + "++) {") # Declare lv_<bp> with the block-local type inside the for body # so it C-shadows any outer same-named local (Ruby block-local end @needs_str_array = 1 emit(" sp_StrArray *" + tmp_arr + " = sp_StrArray_new();") emit(" SP_GC_ROOT(" + tmp_arr + ");") emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_StrArray_length(" + rc + "); " + tmp_i + "++) {") # Block-local C decl, see note in the int-block branch above. emit(" const char *lv_" + bp1 + " = sp_StrArray_get(" + rc + ", " + tmp_i + ");")test/gc_root_map_accumulator.rbaddedView file
Loading diff…