Commit 54923cf2

54923cf2197dMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

Root the receiver array in poly each codegen

The runtime-dispatch each over a poly or poly_array receiver stored the iterated collection in an unrooted temp. When the receiver is a freshly built array referenced only by that temp and the loop body triggers GC, the collection was freed mid-iteration and the next element fetch read a dangling pointer -- latent on x86_64 Linux but surfacing on macOS arm64 as a truncated loop (only the first element survived). Emit SP_GC_ROOT / SP_GC_ROOT_RBVAL for the receiver temp before the loop.

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

Details

Commit
54923cf2197d
Parent
984cc7dfd23b

src/codegen_call.cmodifiedView file

@@ -9348,6 +9348,9 @@
93489348 int ta = ++g_tmp, tn = ++g_tmp, ti = ++g_tmp;
93499349 Buf rb; memset(&rb, 0, sizeof rb); emit_expr(c, recv, &rb);
93509350 emit_indent(b, indent); buf_printf(b, "sp_RbVal _t%d = %s;\n", ta, rb.p ? rb.p : "sp_box_nil()"); free(rb.p);
9351+ /* Root the boxed receiver so a GC fired by the loop body doesn't free a
9352+ freshly-built collection held only by this temp. */
9353+ emit_indent(b, indent); buf_printf(b, "SP_GC_ROOT_RBVAL(_t%d);\n", ta);
93519354 emit_indent(b, indent); buf_printf(b, "mrb_int _t%d = sp_poly_arr_len_ex(_t%d);\n", tn, ta);
93529355 emit_indent(b, indent);
93539356 buf_printf(b, "for (mrb_int _t%d = 0; _t%d < _t%d; _t%d++) {\n", ti, ti, tn, ti);
@@ -9388,6 +9391,11 @@
93889391 }
93899392 emit_indent(b, indent);
93909393 buf_printf(b, "sp_PolyArray *_t%d = %s;\n", ta, rb.p ? rb.p : ""); free(rb.p);
9394+ /* Root the receiver: a freshly-built array referenced only by this temp
9395+ is otherwise freed if the loop body triggers GC mid-iteration, leaving
9396+ the next element fetch dangling. */
9397+ emit_indent(b, indent);
9398+ buf_printf(b, "SP_GC_ROOT(_t%d);\n", ta);
93919399 emit_indent(b, indent);
93929400 buf_printf(b, "for (mrb_int _t%d = 0; _t%d < sp_PolyArray_length(_t%d); _t%d++) {\n", t, t, ta, t);
93939401 if (p0) {