Commit 96c8b057
96c8b057fb19Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
codegen: GC-root array-concat destination + eval rhs once
[x] + foo() lowered to dst = dup(lhs); for (i < length(foo())) push(dst, get(foo(), i)).
Two bugs: the dup'd destination was never SP_GC_ROOT'd, and the rhs
was emitted inline in the loop header so a side-effecting/allocating
rhs (a method call) re-ran every iteration — and any allocation freed
the unrooted destination mid-loop, corrupting its length
(non-deterministically 1/2/11/12 instead of 10).
Evaluate the rhs once into a rooted temp and root the destination before the push loop. SP_GC_ROOT is scope-cleanup'd so it pops at the end of each loop iteration (no root leak).
Fixes #1020
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 96c8b057fb19
- Parent
- 4416079b19ce
spinel_codegen.rbmodifiedView file
emit(" { for (mrb_int _i = 0; _i < sp_" + rt_pfx_p + "_length(" + arg + "); _i++) sp_PolyArray_push(" + tmp + ", " + rt_box + "); }") return tmp end # Array `lhs + rhs` (same element type). Issue #1020: the dup'd # destination must be GC-rooted across the push loop, and the rhs # must be evaluated ONCE into a rooted temp — emitting it inline in # the loop header re-ran a side-effecting/allocating rhs (e.g. # `[x] + foo()`) every iteration, and any allocation freed the # unrooted destination mid-loop, corrupting the result length. @needs_gc = 1 rc = compile_expr_gc_rooted(recv) arg = compile_arg0(nid) pfx = array_c_prefix(lt) tmp = new_temp argt = new_temp itmp = new_temp emit(" " + c_type(lt) + argt + " = " + compile_arg0(nid) + ";") emit(" SP_GC_ROOT(" + argt + ");") emit(" " + c_type(lt) + tmp + " = sp_" + pfx + "_dup(" + rc + ");") emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < sp_" + pfx + "_length(" + arg + "); " + itmp + "++)") emit(" sp_" + pfx + "_push(" + tmp + ", sp_" + pfx + "_get(" + arg + ", " + itmp + "));") emit(" SP_GC_ROOT(" + tmp + ");") emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < sp_" + pfx + "_length(" + argt + "); " + itmp + "++)") emit(" sp_" + pfx + "_push(" + tmp + ", sp_" + pfx + "_get(" + argt + ", " + itmp + "));") return tmp end if lt == "complex"test/i1020.rbaddedView file
Loading diff…
test/i1020.rb.expectedaddedView file
Loading diff…