Commit d727f8e3

d727f8e34e6bMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

fix(runtime): scope-track GC roots via cleanup-attribute sentinel

SP_GC_ROOT(v) previously pushed &v onto a global roots array and relied on a single SP_GC_SAVE() at function entry to restore the nroots stack pointer on function exit (via gcc/clang's __attribute__((cleanup)) on _gc_saved). For variables declared at function scope this works. For temporaries declared inside nested if/while/for blocks — which spinel codegen emits liberally for return values that need to survive a subsequent allocating call — SP_GC_ROOT(_tmp) registered a stack address that became invalid when its declaring inner block ended, but stayed in the roots array until the function's outer SP_GC_SAVE cleanup fired. Subsequent GC collects walked the stale entry and read freed stack memory.

GCC's stack-frame layout happened to mask the bug (the same offset held a still-valid value left over from the prior live local), so gcc-built spinel_codegen ran fine. Clang's stricter layout exposed it as a hard segfault on test/block2.rb (deeply nested scan_locals/scan_locals_children mutual recursion); ASan caught it as stack-use-after-scope at sp_gc_mark_all.

Switch SP_GC_ROOT to a declaration with its own per-root cleanup sentinel:

int attribute((cleanup(_sp_gc_root_pop))) sp_gcr = _sp_gc_root_push((void**)&(v));

The sentinel auto-pops the root when its declaring scope ends — matching the variable's actual lifetime, including each iteration of a for-loop body. SP_GC_SAVE / SP_GC_RESTORE remain for backward compatibility with codegen that emits them; their cleanup of _gc_saved still fires, but it's now redundant since each root cleans itself up.

After: gcc-built and clang-built spinel_codegen both pass 221 tests and 56 benches. ASan reports clean. block2 is no longer the canary for the latent race.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
d727f8e34e6b
Parent
c27a063c7115

lib/sp_runtime.hmodifiedView file

@@ -179,8 +179,24 @@
179179
180180 #define SP_GC_STACK_MAX 65536
181181 static void **sp_gc_roots[SP_GC_STACK_MAX]; static int sp_gc_nroots = 0;
182+/* GC root tracking. SP_GC_ROOT registers a stack-resident root with a
183+ cleanup-attribute sentinel so it's auto-popped when its declaring
184+ scope ends — matches the variable's actual lifetime, including for
185+ temporaries declared inside nested if/while/for blocks. The previous
186+ form (push to global array, paired with SP_GC_SAVE/RESTORE at
187+ function entry) leaked roots whose stack memory was reclaimed when
188+ inner blocks returned, which clang's stricter stack layout exposed
189+ as use-after-scope on inputs that nest scan_locals deeply (issue
190+ surfaced on test/block2.rb under clang-built spinel_codegen). */
191+static inline int _sp_gc_root_push(void **p) {
192+ if (sp_gc_nroots < SP_GC_STACK_MAX) { sp_gc_roots[sp_gc_nroots++] = p; return 1; }
193+ return 0;
194+}
195+static inline void _sp_gc_root_pop(int *added) { if (*added) sp_gc_nroots--; }
196+#define _SP_GC_CONCAT2(a,b) a##b
197+#define _SP_GC_CONCAT(a,b) _SP_GC_CONCAT2(a,b)
182198 #define SP_GC_SAVE() int __attribute__((cleanup(sp_gc_cleanup))) _gc_saved = sp_gc_nroots
183-#define SP_GC_ROOT(v) do{if(sp_gc_nroots<SP_GC_STACK_MAX)sp_gc_roots[sp_gc_nroots++]=(void**)&(v);}while(0)
199+#define SP_GC_ROOT(v) int __attribute__((cleanup(_sp_gc_root_pop))) _SP_GC_CONCAT(_sp_gcr_, __COUNTER__) = _sp_gc_root_push((void**)&(v))
184200 #define SP_GC_RESTORE() sp_gc_nroots = _gc_saved
185201 #define SP_GC_MARK_STACK_MAX (1024*64)
186202 static void**sp_gc_mark_stack=NULL;static int sp_gc_mark_top=0;