Commit d9c6efb1
d9c6efb1494dMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 8 files · +6 added · ~2 modified
Description
Root poly (sp_RbVal) locals so they survive GC
A local whose type unified to poly (sp_RbVal) and was assigned the
result of a call was never registered as a GC root. A collection that
fired while the local was still live (e.g. from allocation later in
the same method) freed the object it held, and the next use read
freed memory -- a use-after-free. Directly-typed object locals were
already rooted; only the poly case had the gap. This crashed a
roundhouse-transpiled Rails app whose lv_controller = instantiate_controller(...) (poly return) was collected mid-request.
The root stack stores void** and marks *slot as one GC pointer, but an sp_RbVal carries its referent inside a union (nonzero offset) and only when its tag is STR/OBJ. So poly roots are stored with the low bit of the slot set (sp_RbVal addresses are aligned, so the bit is free) and the mark walker routes a tagged entry through sp_mark_rbval, which marks only a STR/OBJ payload and ignores an int/float/nil one. The untagged fast path in the mark loops is left byte-identical, and the tag bit is opaque to SP_GC_SAVE/RESTORE and the fiber root save/restore, so none of that machinery changes.
Codegen now roots poly locals (the new "poly" arm in emit_gc_root_for_expr_impl, plus including poly in the has_gc_locals gate and an explicit root at the top-level-locals site), keeping the volatile-under-setjmp qualifier orthogonally. Poly params are deliberately not rooted (emit_gc_root_for_param) -- their referent is reachable through the caller's own root, so re-rooting in every callee is pure churn on hot paths; the one uncovered shape, a fresh poly temporary passed straight into a call, is left for separate work.
Fixes #1052
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- d9c6efb1494d
- Parent
- 567104ce028e
lib/sp_runtime.hmodifiedView file
#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) int __attribute__((cleanup(_sp_gc_root_pop))) _SP_GC_CONCAT(_sp_gcr_, __COUNTER__) = _sp_gc_root_push((void**)&(v))/* Root a poly (sp_RbVal) local/global. The root stack stores void** and marks *slot as a single GC pointer, but an sp_RbVal carries its object pointer inside a union (offset != 0) and only when its tag is STR/OBJ -- a blind *slot deref would read the tag word as a pointer. So we tag the stored slot with the low bit (sp_RbVal addresses are >=4-byte aligned, so the bit is free) and the mark walker, seeing the tag, routes the entry through sp_mark_rbval instead. Same auto-pop cleanup as SP_GC_ROOT, so SP_GC_SAVE/RESTORE and the fiber root save/restore (which treat entries as opaque) need no changes. */#define SP_GC_ROOT_RBVAL(v) int __attribute__((cleanup(_sp_gc_root_pop))) _SP_GC_CONCAT(_sp_gcr_, __COUNTER__) = _sp_gc_root_push((void**)((uintptr_t)&(v) | (uintptr_t)1))#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; pointer stays NULL and sp_gc_mark_all skips the walk. Issue #636. */static void (*sp_gc_mark_suspended_fibers_hook)(void) = NULL;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);}if(sp_gc_mark_suspended_fibers_hook)sp_gc_mark_suspended_fibers_hook();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);}}/* Mark one root-stack entry. A low-bit-tagged entry is an sp_RbVal* root (see SP_GC_ROOT_RBVAL) and routes through sp_mark_rbval, which marks only its embedded STR/OBJ pointer; an untagged entry is a plain void** whose target is a direct GC pointer. Defined after sp_RbVal / sp_mark_rbval; forward-declared here. */static inline void sp_gc_mark_root_entry(void**e);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**e=sp_gc_roots[i];if((uintptr_t)e&(uintptr_t)1){sp_gc_mark_root_entry(e);}else{void*obj=*e;if(obj)sp_gc_mark(obj);}}if(sp_gc_mark_suspended_fibers_hook)sp_gc_mark_suspended_fibers_hook();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);}}static void sp_gc_cleanup(int*p){sp_gc_nroots=*p;}#define SP_GC_NBUCKETS 32static sp_gc_hdr*sp_gc_buckets[SP_GC_NBUCKETS];/* PolyArray: array of sp_RbVal */typedef struct { sp_RbVal *data; mrb_int len; mrb_int cap; mrb_int frozen; } sp_PolyArray;static inline void sp_mark_rbval(sp_RbVal v);/* Definition of the root-entry marker forward-declared near sp_gc_mark_all: a low-bit-tagged slot is an sp_RbVal* root. */static inline void sp_gc_mark_root_entry(void**e){uintptr_t u=(uintptr_t)e;if(u&(uintptr_t)1){sp_mark_rbval(*(sp_RbVal*)(u&~(uintptr_t)1));}else{void*o=*e;if(o)sp_gc_mark(o);}}static void sp_PolyArray_scan(void *p) { sp_PolyArray *a = (sp_PolyArray *)p; for (mrb_int i = 0; i < a->len; i++) sp_mark_rbval(a->data[i]); }static void sp_PolyArray_fin(void *p) { sp_PolyArray *a = (sp_PolyArray *)p; sp_gc_hdr *h = (sp_gc_hdr *)((char *)a - sizeof(sp_gc_hdr)); sp_gc_bytes -= sizeof(sp_RbVal) * a->cap; h->size -= sizeof(sp_RbVal) * a->cap; free(a->data); }static sp_PolyArray *sp_PolyArray_new(void) { sp_PolyArray *a = (sp_PolyArray *)sp_gc_alloc(sizeof(sp_PolyArray), sp_PolyArray_fin, sp_PolyArray_scan); a->cap = 16; a->data = (sp_RbVal *)malloc(sizeof(sp_RbVal) * a->cap); if (!a->data) sp_oom_die(); a->len = 0; { sp_gc_hdr *h = (sp_gc_hdr *)((char *)a - sizeof(sp_gc_hdr)); h->size += sizeof(sp_RbVal) * a->cap; sp_gc_bytes += sizeof(sp_RbVal) * a->cap; } return a; } sp_fiber_root.saved_roots (the resume side stashed them there) but sp_fiber_root is NOT in sp_fiber_list_head, so walking the list alone would miss them and free main's live locals. */static void sp_mark_fiber_roots(sp_Fiber*f){if(f==sp_fiber_current)return;int i;for(i=0;i<f->saved_nroots;i++){void*obj=*f->saved_roots[i];if(obj)sp_gc_mark(obj);}}static void sp_mark_fiber_roots(sp_Fiber*f){if(f==sp_fiber_current)return;int i;for(i=0;i<f->saved_nroots;i++){void**e=f->saved_roots[i];if((uintptr_t)e&(uintptr_t)1){sp_gc_mark_root_entry(e);}else{void*obj=*e;if(obj)sp_gc_mark(obj);}}}static void sp_mark_suspended_fibers(void){sp_mark_fiber_roots(&sp_fiber_root);sp_Fiber*f=sp_fiber_list_head;while(f){sp_mark_fiber_roots(f);f=f->fiber_next;}}static void sp_fiber_install_gc_hook(void){if(!sp_gc_mark_suspended_fibers_hook)sp_gc_mark_suspended_fibers_hook=sp_mark_suspended_fibers;}static void sp_Fiber_fin(void*p){sp_Fiber*f=(sp_Fiber*)p;if(f->ctx)DeleteFiber(f->ctx);if(f->saved_roots)free(f->saved_roots);sp_fiber_list_remove(f);} sp_fiber_root.saved_roots (the resume side stashed them there) but sp_fiber_root is NOT in sp_fiber_list_head, so walking the list alone would miss them and free main's live locals. */static void sp_mark_fiber_roots(sp_Fiber*f){if(f==sp_fiber_current)return;int i;for(i=0;i<f->saved_nroots;i++){void*obj=*f->saved_roots[i];if(obj)sp_gc_mark(obj);}}static void sp_mark_fiber_roots(sp_Fiber*f){if(f==sp_fiber_current)return;int i;for(i=0;i<f->saved_nroots;i++){void**e=f->saved_roots[i];if((uintptr_t)e&(uintptr_t)1){sp_gc_mark_root_entry(e);}else{void*obj=*e;if(obj)sp_gc_mark(obj);}}}static void sp_mark_suspended_fibers(void){sp_mark_fiber_roots(&sp_fiber_root);sp_Fiber*f=sp_fiber_list_head;while(f){sp_mark_fiber_roots(f);f=f->fiber_next;}}static void sp_fiber_install_gc_hook(void){if(!sp_gc_mark_suspended_fibers_hook)sp_gc_mark_suspended_fibers_hook=sp_mark_suspended_fibers;}static void sp_Fiber_fin(void*p){sp_Fiber*f=(sp_Fiber*)p;if(f->stack)munmap(f->stack,SP_FIBER_STACK_SIZE);if(f->saved_roots)free(f->saved_roots);sp_fiber_list_remove(f);}spinel_codegen.rbmodifiedView file
Loading diff…
test/gc_poly_local_int.rbaddedView file
Loading diff…
test/gc_poly_local_int.rb.expectedaddedView file
Loading diff…
test/gc_poly_local_root.rbaddedView file
Loading diff…
test/gc_poly_local_root.rb.expectedaddedView file
Loading diff…
test/gc_poly_local_setjmp.rbaddedView file
Loading diff…
test/gc_poly_local_setjmp.rb.expectedaddedView file
Loading diff…