Commit dc1d5adb
dc1d5adba605Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
fix(codegen): clear ptr ivars after SP_POOL_NEW before init body runs
#473. SP_POOL_NEW (added in 4a7a678 to recycle user-class instances through a per-class free-list) hands back a slot whose pointer ivars still carry the previous tenant's values. The initialize body's first sub-allocation can trigger GC before the body has overwritten every pointer ivar — at which point gc_scan walks those stale pointers and crashes inside sp_gc_mark.
The transformer LM repro (Mat#initialize -> @flat = Array.new(n, 0.0) -> sp_FloatArray_new -> sp_gc_alloc -> sp_gc_mark_all ->
sp_Mat_gc_scan reading the recycled slot's old iv_flat) hits this
during the backward pass once enough Mats have cycled through
the pool that recycled slots dominate. Symptoms vary by platform:
SIGBUS on aarch64 (alignment of the stale pointer), SIGSEGV on
x86_64 in some memory layouts, silent success in others. Tests
and bench all happen to slip through because their per-class
allocation counts stay below the recycle threshold.
Fix: codegen-side. After SP_POOL_NEW + SP_GC_ROOT(self), emit
self->iv_X = NULL (or sp_box_nil() for poly ivars) for every
GC-traced ivar declared on the class chain, walking parents
first. The gc_scan callbacks already short-circuit on if (self->iv_X), so the NULL is benign for the rest of the init
body's normal execution but makes the recycled-slot reads safe.
Validation:
- train_minimal.rb (from OriPekelman/toy_ruby_neural_network) runs cleanly 5/5 with stdout redirected; loss converges ~2.2 -> ~0.035 over 40 SGD steps as expected.
- valgrind clean on the same workload.
- make test: 439 pass, 0 fail; make bench: 55/55 pass; bootstrap 4-way fixpoint OK.
Closes #473.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- dc1d5adba605
- Parent
- 825d51aa5375
spinel_codegen.rbmodifiedView file
0 end # Emit `self->iv_<name> = NULL;` / `sp_box_nil();` for each # GC-traced ivar on the class chain. Called right after # SP_POOL_NEW + SP_GC_ROOT(self) to defuse the recycled-slot # stale-pointer hazard: if a sub-allocation in the initialize # body triggers GC before the body writes every pointer ivar, # the gc_scan callback would otherwise mark the prior tenant's # freed pointers and crash inside sp_gc_mark. def emit_clear_ptr_ivars_for_new(ci) emit_clear_ptr_ivars_chain(ci) end def emit_clear_ptr_ivars_chain(ci) if @cls_parents[ci] != "" pi = find_class_idx(@cls_parents[ci]) if pi >= 0 emit_clear_ptr_ivars_chain(pi) end end names = @cls_ivar_names[ci].split(";") types = @cls_ivar_types[ci].split(";") j = 0 while j < names.length if j < types.length if ivar_is_gc_ptr(types[j]) == 1 field = "self->" + sanitize_ivar(names[j]) if types[j] == "poly" emit_raw(" " + field + " = sp_box_nil();") else emit_raw(" " + field + " = NULL;") end end end j = j + 1 end end def emit_gc_mark_ivar(field, t) if t == "poly" emit_raw(" sp_mark_rbval(" + field + ");") # BC + internal_ci so it sits past the built-in prefix. emit_raw(" self->cls_id = " + cls_id_for_user_internal(ci).to_s + "LL;") emit_raw(" SP_GC_ROOT(self);") # Pool-recycled slots keep the previous tenant's pointer ivars. # If the initialize body's RHS triggers GC (e.g. `@flat = []` -> # sp_FloatArray_new -> sp_gc_alloc) before each pointer ivar is # assigned, the gc_scan callback walks a stale pointer from the # prior tenant and crashes. Clear every pointer ivar declared on # this class (incl. inherited ones) up front so gc_scan's # `if (self->iv_X)` guard short-circuits until the body fills the # slot. The poly ivar case is symmetric: a stale sp_RbVal carries # a stale .v.p, so reset it to a tagged nil that sp_mark_rbval # treats as inert. emit_clear_ptr_ivars_for_new(ci) end # Root pointer-type constructor parameters