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

@@ -7440,6 +7440,42 @@
74407440 0
74417441 end
74427442
7443+ # Emit `self->iv_<name> = NULL;` / `sp_box_nil();` for each
7444+ # GC-traced ivar on the class chain. Called right after
7445+ # SP_POOL_NEW + SP_GC_ROOT(self) to defuse the recycled-slot
7446+ # stale-pointer hazard: if a sub-allocation in the initialize
7447+ # body triggers GC before the body writes every pointer ivar,
7448+ # the gc_scan callback would otherwise mark the prior tenant's
7449+ # freed pointers and crash inside sp_gc_mark.
7450+ def emit_clear_ptr_ivars_for_new(ci)
7451+ emit_clear_ptr_ivars_chain(ci)
7452+ end
7453+
7454+ def emit_clear_ptr_ivars_chain(ci)
7455+ if @cls_parents[ci] != ""
7456+ pi = find_class_idx(@cls_parents[ci])
7457+ if pi >= 0
7458+ emit_clear_ptr_ivars_chain(pi)
7459+ end
7460+ end
7461+ names = @cls_ivar_names[ci].split(";")
7462+ types = @cls_ivar_types[ci].split(";")
7463+ j = 0
7464+ while j < names.length
7465+ if j < types.length
7466+ if ivar_is_gc_ptr(types[j]) == 1
7467+ field = "self->" + sanitize_ivar(names[j])
7468+ if types[j] == "poly"
7469+ emit_raw(" " + field + " = sp_box_nil();")
7470+ else
7471+ emit_raw(" " + field + " = NULL;")
7472+ end
7473+ end
7474+ end
7475+ j = j + 1
7476+ end
7477+ end
7478+
74437479 def emit_gc_mark_ivar(field, t)
74447480 if t == "poly"
74457481 emit_raw(" sp_mark_rbval(" + field + ");")
@@ -8371,6 +8407,17 @@
83718407 # BC + internal_ci so it sits past the built-in prefix.
83728408 emit_raw(" self->cls_id = " + cls_id_for_user_internal(ci).to_s + "LL;")
83738409 emit_raw(" SP_GC_ROOT(self);")
8410+ # Pool-recycled slots keep the previous tenant's pointer ivars.
8411+ # If the initialize body's RHS triggers GC (e.g. `@flat = []` ->
8412+ # sp_FloatArray_new -> sp_gc_alloc) before each pointer ivar is
8413+ # assigned, the gc_scan callback walks a stale pointer from the
8414+ # prior tenant and crashes. Clear every pointer ivar declared on
8415+ # this class (incl. inherited ones) up front so gc_scan's
8416+ # `if (self->iv_X)` guard short-circuits until the body fills the
8417+ # slot. The poly ivar case is symmetric: a stale sp_RbVal carries
8418+ # a stale .v.p, so reset it to a tagged nil that sp_mark_rbval
8419+ # treats as inert.
8420+ emit_clear_ptr_ivars_for_new(ci)
83748421 end
83758422
83768423 # Root pointer-type constructor parameters