Commit 09252cc4

09252cc49467Merged

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

Description

fix(codegen): SP_GC_ROOT Range#map / N.times.map accumulators

Follow-up to #198 (gc-root-map-accumulators), which rooted the fresh accumulator in the int_array / sym_array / str_array recv branches of compile_map_expr. The Range#map and N.times.map branches build the same shape (sp_*Array_new + push loop) but were missed.

Same scenario: a GC pass triggered mid-loop by an allocation inside the block (e.g. string interpolation) frees the unrooted accumulator; 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.

Emit SP_GC_ROOT(<accum>) immediately after the sp_*Array_new in both branches.

Test: test/gc_root_range_and_times_map.rb exercises Range#map (with both string-allocating and int-with-scratch blocks) and N.times.map (same two shapes).

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

Details

Commit
09252cc49467
Parent
5a321da268b9

spinel_codegen.rbmodifiedView file

@@ -26983,6 +26983,11 @@
2698326983 @needs_int_array = 1
2698426984 emit(" sp_IntArray *" + tmp_arrn + " = sp_IntArray_new();")
2698526985 end
26986+ # Root the accumulator so a GC pass triggered by an
26987+ # allocation inside the block (e.g. string interpolation)
26988+ # doesn't free the half-built array. Same reasoning as the
26989+ # int_array / str_array branches further down.
26990+ emit(" SP_GC_ROOT(" + tmp_arrn + ");")
2698626991 emit(" for (mrb_int " + tmp_in + " = 0; " + tmp_in + " < " + ncount + "; " + tmp_in + "++) {")
2698726992 if bpn != ""
2698826993 emit(" lv_" + bpn + " = " + tmp_in + ";")
@@ -27064,6 +27069,11 @@
2706427069 else
2706527070 emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")
2706627071 end
27072+ # Root the accumulator so a GC pass triggered by an
27073+ # allocation inside the block (e.g. string interpolation)
27074+ # doesn't free the half-built array. Same reasoning as the
27075+ # int_array / str_array branches further down.
27076+ emit(" SP_GC_ROOT(" + tmp_arr + ");")
2706727077 # Walk through ParenthesesNode wrappers to find a literal
2706827078 # RangeNode (so we can read its exclude_end flag and its raw
2706927079 # endpoint expressions). If the receiver is a variable holding