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.

  1. 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] = v writes 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.

  2. []= expression typing (Gemini, high). Extended the analyzer's Fiber [] arm to also match []=, so x = (Fiber[:k] = 42) types x as poly instead of the int default that would mismatch sp_RbVal in the generated C.

  3. []= expression codegen (follow-on from fix 2). The analyzer-only change introduced a regression: with x typed as sp_RbVal but codegen emitting the default lv_x = 0 for 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) => v semantics.

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 chained Fiber[: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

@@ -1091,6 +1091,13 @@
10911091 pointers in $1..$9, $&, $`, $'. sp_mark_string is null-safe and
10921092 no-ops on non-heap strings (the empty-string default of
10931093 sp_re_last_str), so it's safe to call unconditionally. */
1094+/* sp_fiber_root.storage needs marking here because sp_fiber_root is
1095+ a static (not sp_gc_alloc'd), so its normal sp_Fiber_scan never
1096+ runs via the heap walker. Without this, top-level `Fiber[:k] = v`
1097+ writes get prematurely collected. The forward declaration is
1098+ needed because sp_fiber_root is defined further down in the
1099+ Fiber runtime block. */
1100+static void sp_mark_fiber_root_storage(void);
10941101 static void sp_re_mark_globals(void) {
10951102 sp_mark_string(sp_re_last_str);
10961103 for (int i = 0; i < 10; i++) sp_mark_string(sp_re_captures[i]);
@@ -1099,6 +1106,7 @@
10991106 sp_mark_string(sp_re_match_post);
11001107 for (mrb_int i = 0; i < sp_argv.len; i++) sp_mark_string(sp_argv.data[i]);
11011108 sp_mark_in_flight_exceptions();
1109+ sp_mark_fiber_root_storage();
11021110 }
11031111
11041112 static void sp_re_set_captures(const char *str, int *caps, int ncaps) {
@@ -1949,6 +1957,7 @@
19491957 static mrb_bool sp_Fiber_alive(sp_Fiber*f){return f->state!=3;}
19501958 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;}
19511959 #endif
1960+static void sp_mark_fiber_root_storage(void){if(sp_fiber_root.storage)sp_gc_mark(sp_fiber_root.storage);}
19521961
19531962 /* Bigint (linked from sp_bigint.o) */
19541963 typedef struct sp_Bigint sp_Bigint;