Commit bbddb420
bbddb420f8c8Merged
Sam Ruby <[email protected]> · 6 months ago · 7 files · +4 added · ~3 modified
Description
review: GC root for sp_fiber_root.storage; []= expression form
Addresses both Gemini review findings on #578, plus a follow-on that the analyzer-only fix exposed.
-
GC root for sp_fiber_root.storage (Gemini, high). sp_fiber_root is a static, not sp_gc_alloc'd, so its sp_Fiber_scan never runs via the heap walker. Top-level
Fiber[:k] = vwrites allocated a SymPolyHash that the GC then prematurely collected. Verified the bug: new test/fiber_storage_gc_root.rb segfaults on master without the fix, passes with it. Fix: add a sp_mark_fiber_root_ storage helper (declared forward, defined after sp_fiber_root) called from sp_re_mark_globals. -
[]=expression typing (Gemini, high). Extended the analyzer's Fiber[]arm to also match[]=, sox = (Fiber[:k] = 42)typesxas poly instead of the int default that would mismatch sp_RbVal in the generated C. -
[]=expression codegen (follow-on from fix 2). The analyzer-only change introduced a regression: withxtyped as sp_RbVal but codegen emitting the defaultlv_x = 0for the call's value, C compile fails with type mismatch. Added a compile_call_method arm for Fiber[]=that emits a gcc statement-expression({ sp_RbVal tmp = <rhs>; sp_Fiber_storage_set(...); tmp; })so the assignment's value reaches the outer expression context, matching MRI's(h[k]=v) => vsemantics.
Two new tests:
- fiber_storage_gc_root.rb: writes :answer = 42, allocates 10k arrays of strings to push past the 256KB GC threshold, verifies read still returns 42 (segfaults without fix 1).
- fiber_storage_assign_expr.rb:
(Fiber[:n] = 42)as expression, plus chainedFiber[:a] = Fiber[:b] = 7(exercises fix 3).
Full suite: 547 pass / 0 fail / 0 error.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- bbddb420f8c8
- Parent
- b0d4fe90f9e9
lib/sp_runtime.hmodifiedView file
pointers in $1..$9, $&, $`, $'. sp_mark_string is null-safe and no-ops on non-heap strings (the empty-string default of sp_re_last_str), so it's safe to call unconditionally. *//* sp_fiber_root.storage needs marking here because sp_fiber_root is a static (not sp_gc_alloc'd), so its normal sp_Fiber_scan never runs via the heap walker. Without this, top-level `Fiber[:k] = v` writes get prematurely collected. The forward declaration is needed because sp_fiber_root is defined further down in the Fiber runtime block. */static void sp_mark_fiber_root_storage(void);static void sp_re_mark_globals(void) { sp_mark_string(sp_re_last_str); for (int i = 0; i < 10; i++) sp_mark_string(sp_re_captures[i]); sp_mark_string(sp_re_match_post); for (mrb_int i = 0; i < sp_argv.len; i++) sp_mark_string(sp_argv.data[i]); sp_mark_in_flight_exceptions(); sp_mark_fiber_root_storage();}static void sp_re_set_captures(const char *str, int *caps, int ncaps) {static mrb_bool sp_Fiber_alive(sp_Fiber*f){return f->state!=3;}static sp_RbVal sp_Fiber_transfer(sp_Fiber*f,sp_RbVal val){f->resumed_value=val;sp_Fiber*prev=sp_fiber_current;sp_fiber_current=f;if(f->state==0){f->state=1;f->transferred=1;getcontext(&f->ctx);f->ctx.uc_stack.ss_sp=f->stack;f->ctx.uc_stack.ss_size=SP_FIBER_STACK_SIZE;f->ctx.uc_link=&prev->ctx;makecontext(&f->ctx,(void(*)(void))sp_fiber_trampoline,0);swapcontext(&prev->ctx,&f->ctx);}else{f->state=1;swapcontext(&prev->ctx,&f->ctx);}sp_fiber_current=prev;return prev->resumed_value;}#endifstatic void sp_mark_fiber_root_storage(void){if(sp_fiber_root.storage)sp_gc_mark(sp_fiber_root.storage);}/* Bigint (linked from sp_bigint.o) */typedef struct sp_Bigint sp_Bigint;spinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/fiber_storage_assign_expr.rbaddedView file
Loading diff…
test/fiber_storage_assign_expr.rb.expectedaddedView file
Loading diff…
test/fiber_storage_gc_root.rbaddedView file
Loading diff…
test/fiber_storage_gc_root.rb.expectedaddedView file
Loading diff…