Commit 34a54d65

34a54d65de77Merged

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

Description

fix(runtime): sp_gc_mark handle 0xfd tag for sp_String buffer

sp_String_new (and the append / prepend grow paths) stamps the byte before the buffer's data pointer with 0xfd to mark the buffer as wrapper-owned: the sp_String itself carries an sp_gc_hdr and is reached through the regular GC root chain, so the bare lv__buf->data pointer is just a borrow.

sp_gc_mark only short-circuited on 0xfe (heap str), 0xfc (already marked), 0xff (frozen literal). When codegen returned return lv__buf->data; from an ERB-style render whose result later landed in a SP_GC_ROOT slot, the next collect cycle read pm = 0xfd, fell through to the sp_gc_hdr cast path, read garbage as h->scan, and jumped to a junk function pointer (SIGBUS / SIGSEGV).

Add 0xfd to the early-return condition. The wrapper still reaches its own gc_hdr through the root chain; the bare data pointer just isn't GC-walked.

Bug reported and patched by @afomera with a detailed root-cause trace and an HTTP-server repro. Their verification matrix:

  • minimal.rb repro: dead at req 563 → alive 2000+
  • real framework: dead at req 28-139 → alive 2000+
  • framework smoke chain: crashed within handful → all 30 served

Fixes #583.

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

Details

Commit
34a54d65de77
Parent
1611698e9936

lib/sp_runtime.hmodifiedView file

@@ -507,7 +507,21 @@
507507 #define SP_GC_RESTORE() sp_gc_nroots = _gc_saved
508508 #define SP_GC_MARK_STACK_MAX (1024*64)
509509 static void**sp_gc_mark_stack=NULL;static int sp_gc_mark_top=0;
510-static void sp_gc_mark(void*obj){if(!obj)return;unsigned char pm=((unsigned char*)obj)[-1];if(pm==0xfe){((char*)obj)[-1]=(char)0xfc;return;}if(pm==0xfc||pm==0xff)return;sp_gc_hdr*h=(sp_gc_hdr*)((char*)obj-sizeof(sp_gc_hdr));if(h->marked)return;h->marked=1;if(h->scan){if(sp_gc_mark_stack&&sp_gc_mark_top<SP_GC_MARK_STACK_MAX){sp_gc_mark_stack[sp_gc_mark_top++]=obj;}else{h->scan(obj);}}}
510+/* Tag bytes on the byte preceding `obj`:
511+ * 0xfe : heap-allocated string (sp_str_alloc), unmarked -> bump to 0xfc.
512+ * 0xfc : heap string already marked this cycle -> skip.
513+ * 0xff : frozen literal in rodata (sp_str_empty, "\xff"+literal) -> skip.
514+ * 0xfd : data buffer owned by an sp_String wrapper. The wrapper
515+ * itself carries an sp_gc_hdr and is reached as a separate
516+ * root; the bare data pointer is just a borrow. Skip here.
517+ * Issue #583: without this arm, a `return lv__buf->data;`
518+ * from an ERB-style render whose result later lands in a
519+ * SP_GC_ROOT slot would fall through to the sp_gc_hdr cast
520+ * path on the next collect cycle, read garbage as h->scan,
521+ * and crash with SIGBUS / SIGSEGV.
522+ * else : a real GC-allocated object; cast back to sp_gc_hdr and
523+ * recurse through its scan function. */
524+static void sp_gc_mark(void*obj){if(!obj)return;unsigned char pm=((unsigned char*)obj)[-1];if(pm==0xfe){((char*)obj)[-1]=(char)0xfc;return;}if(pm==0xfc||pm==0xff||pm==0xfd)return;sp_gc_hdr*h=(sp_gc_hdr*)((char*)obj-sizeof(sp_gc_hdr));if(h->marked)return;h->marked=1;if(h->scan){if(sp_gc_mark_stack&&sp_gc_mark_top<SP_GC_MARK_STACK_MAX){sp_gc_mark_stack[sp_gc_mark_top++]=obj;}else{h->scan(obj);}}}
511525 /* Forward decl: defined alongside the regex globals it marks. */
512526 static void sp_re_mark_globals(void);
513527 static void sp_gc_mark_all(void){if(!sp_gc_mark_stack)sp_gc_mark_stack=(void**)malloc(sizeof(void*)*SP_GC_MARK_STACK_MAX);sp_gc_mark_top=0;for(int i=0;i<sp_gc_nroots;i++){void*obj=*sp_gc_roots[i];if(obj)sp_gc_mark(obj);}sp_re_mark_globals();while(sp_gc_mark_top>0){void*obj=sp_gc_mark_stack[--sp_gc_mark_top];sp_gc_hdr*h=(sp_gc_hdr*)((char*)obj-sizeof(sp_gc_hdr));if(h->scan)h->scan(obj);}}