Commit e94b60bc

e94b60bc2cc6Merged

Ori Pekelman <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): root PtrArray temp in array-of-objects literal

@x = [Klass.new(...)] lowered to:

sp_PtrArray *_t = sp_PtrArray_new();
sp_PtrArray_push(_t, sp_Klass_new(...));
self->iv_x = _t;

_t was not in the GC root set. sp_Klass_new (and any element expression that allocates) can trigger a collect, which frees the half-built array; the subsequent push and ivar assignment then write through a dangling pointer.

Emit SP_GC_ROOT(tmp) immediately after sp_PtrArray_new() so the temp survives element evaluation. Float/Int array literals are unaffected — their elements are unboxed and never trigger GC.

Add gc_root_ptr_array_literal.rb: 1000 Containers each holding three PtrArray ivars of four Tile objects (~6 KB / Container) — crosses the 256 KB GC threshold mid-construction. Reports 0 corruption errors with the fix; without it, it reports several on darwin/arm64.

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
e94b60bc2cc6
Parent
5f895ce7fc3d

spinel_codegen.rbmodifiedView file

@@ -24259,6 +24259,12 @@
2425924259 if is_ptr_array_type(arr_type) == 1
2426024260 tmp = new_temp
2426124261 emit(" sp_PtrArray *" + tmp + " = sp_PtrArray_new();")
24262+ # Root the freshly-allocated PtrArray before compiling element
24263+ # expressions. Each element is typically Klass.new(...), and that
24264+ # call allocates → may trigger GC. Without this, GC sees `tmp` as
24265+ # unreachable and frees it, leaving a dangling pointer that the
24266+ # subsequent push (and the eventual ivar assignment) writes through.
24267+ emit(" SP_GC_ROOT(" + tmp + ");")
2426224268 k = 0
2426324269 while k < elems.length
2426424270 emit(" sp_PtrArray_push(" + tmp + ", " + compile_expr(elems[k]) + ");")