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
@needs_int_array = 1 emit(" sp_IntArray *" + tmp_arrn + " = sp_IntArray_new();") end # Root the accumulator so a GC pass triggered by an # allocation inside the block (e.g. string interpolation) # doesn't free the half-built array. Same reasoning as the # int_array / str_array branches further down. emit(" SP_GC_ROOT(" + tmp_arrn + ");") emit(" for (mrb_int " + tmp_in + " = 0; " + tmp_in + " < " + ncount + "; " + tmp_in + "++) {") if bpn != "" emit(" lv_" + bpn + " = " + tmp_in + ";") else emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();") end # Root the accumulator so a GC pass triggered by an # allocation inside the block (e.g. string interpolation) # doesn't free the half-built array. Same reasoning as the # int_array / str_array branches further down. emit(" SP_GC_ROOT(" + tmp_arr + ");") # Walk through ParenthesesNode wrappers to find a literal # RangeNode (so we can read its exclude_end flag and its raw # endpoint expressions). If the receiver is a variable holdingtest/gc_root_range_and_times_map.rbaddedView file
Loading diff…