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
#define SP_GC_STACK_MAX 65536static void **sp_gc_roots[SP_GC_STACK_MAX]; static int sp_gc_nroots = 0;/* GC root tracking. SP_GC_ROOT registers a stack-resident root with a cleanup-attribute sentinel so it's auto-popped when its declaring scope ends — matches the variable's actual lifetime, including for temporaries declared inside nested if/while/for blocks. The previous form (push to global array, paired with SP_GC_SAVE/RESTORE at function entry) leaked roots whose stack memory was reclaimed when inner blocks returned, which clang's stricter stack layout exposed as use-after-scope on inputs that nest scan_locals deeply (issue surfaced on test/block2.rb under clang-built spinel_codegen). */static inline int _sp_gc_root_push(void **p) { if (sp_gc_nroots < SP_GC_STACK_MAX) { sp_gc_roots[sp_gc_nroots++] = p; return 1; } return 0;}static inline void _sp_gc_root_pop(int *added) { if (*added) sp_gc_nroots--; }#define _SP_GC_CONCAT2(a,b) a##b#define _SP_GC_CONCAT(a,b) _SP_GC_CONCAT2(a,b)#define SP_GC_SAVE() int __attribute__((cleanup(sp_gc_cleanup))) _gc_saved = sp_gc_nroots#define SP_GC_ROOT(v) do{if(sp_gc_nroots<SP_GC_STACK_MAX)sp_gc_roots[sp_gc_nroots++]=(void**)&(v);}while(0)#define SP_GC_ROOT(v) int __attribute__((cleanup(_sp_gc_root_pop))) _SP_GC_CONCAT(_sp_gcr_, __COUNTER__) = _sp_gc_root_push((void**)&(v))#define SP_GC_RESTORE() sp_gc_nroots = _gc_saved#define SP_GC_MARK_STACK_MAX (1024*64)static void**sp_gc_mark_stack=NULL;static int sp_gc_mark_top=0;