Commit aeb3b363

aeb3b363b43fMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

Merge pull request #198 from mame/pr/gc-root-map-accumulators

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

Details

Commit
aeb3b363b43f
Parent
0a66fa7dafef
Merge parent
38981d09ef80

spinel_codegen.rbmodifiedView file

@@ -27040,6 +27040,12 @@
2704027040 if block_ret == "string"
2704127041 @needs_str_array = 1
2704227042 emit(" sp_StrArray *" + tmp_arr + " = sp_StrArray_new();")
27043+ # Root the accumulator: a nested map block (e.g. each
27044+ # iteration allocates many inner objects via sp_*_new) can
27045+ # trigger GC between pushes; without rooting, the outer
27046+ # accumulator is collected mid-loop and the next push
27047+ # corrupts the freed buffer.
27048+ emit(" SP_GC_ROOT(" + tmp_arr + ");")
2704327049 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_IntArray_length(" + rc + "); " + tmp_i + "++) {")
2704427050 if bp_is_lambda == 1
2704527051 emit(" sp_Val * lv_" + bp1 + " = (sp_Val *)sp_IntArray_get(" + rc + ", " + tmp_i + ");")
@@ -27065,6 +27071,7 @@
2706527071 return tmp_arr
2706627072 else
2706727073 emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")
27074+ emit(" SP_GC_ROOT(" + tmp_arr + ");")
2706827075 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_IntArray_length(" + rc + "); " + tmp_i + "++) {")
2706927076 if bp_is_lambda == 1
2707027077 emit(" sp_Val * lv_" + bp1 + " = (sp_Val *)sp_IntArray_get(" + rc + ", " + tmp_i + ");")
@@ -27111,6 +27118,7 @@
2711127118 if block_ret == "int"
2711227119 @needs_int_array = 1
2711327120 emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")
27121+ emit(" SP_GC_ROOT(" + tmp_arr + ");")
2711427122 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_StrArray_length(" + rc + "); " + tmp_i + "++) {")
2711527123 # Declare lv_<bp> with the block-local type inside the for body
2711627124 # so it C-shadows any outer same-named local (Ruby block-local
@@ -27140,6 +27148,7 @@
2714027148 end
2714127149 @needs_str_array = 1
2714227150 emit(" sp_StrArray *" + tmp_arr + " = sp_StrArray_new();")
27151+ emit(" SP_GC_ROOT(" + tmp_arr + ");")
2714327152 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_StrArray_length(" + rc + "); " + tmp_i + "++) {")
2714427153 # Block-local C decl, see note in the int-block branch above.
2714527154 emit(" const char *lv_" + bp1 + " = sp_StrArray_get(" + rc + ", " + tmp_i + ");")