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
if is_ptr_array_type(arr_type) == 1 tmp = new_temp emit(" sp_PtrArray *" + tmp + " = sp_PtrArray_new();") # Root the freshly-allocated PtrArray before compiling element # expressions. Each element is typically Klass.new(...), and that # call allocates → may trigger GC. Without this, GC sees `tmp` as # unreachable and frees it, leaving a dangling pointer that the # subsequent push (and the eventual ivar assignment) writes through. emit(" SP_GC_ROOT(" + tmp + ");") k = 0 while k < elems.length emit(" sp_PtrArray_push(" + tmp + ", " + compile_expr(elems[k]) + ");")test/gc_root_ptr_array_literal.rbaddedView file
Loading diff…
test/gc_root_ptr_array_literal.rb.expectedaddedView file
Loading diff…