Commit 8145379a
8145379a9d65Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified
Description
Walk full ancestor chain in generated GC scan functions
A class's generated gc_scan only marked its own ivars plus those of its immediate parent. When a passthrough middle class (introducing no ivars of its own) sat in the hierarchy, ivars introduced two-or-more levels up were dropped from the leaf's scan function: the leaf skips them (inherited) and the immediate-parent walk never sees them because the empty middle class breaks the chain. The GC then freed objects those grandparent ivars held while the instance was still live, a use-after-free (e.g. ArticlesController < ApplicationController < ActionController::Base dropping @params under load).
Replace the single immediate-parent walk with a walk over the full ancestor chain, deduping marked names so an ivar re-listed under a widened child type isn't marked twice.
Also add the missing if(!h) return guard to sp_StrPolyHash_has_key
for parity with its siblings sp_StrPolyHash_get / _keys.
Fixes #1050
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 8145379a9d65
- Parent
- 9a43cb152fd8
lib/sp_runtime.hmodifiedView file
static void sp_StrPolyHash_grow(sp_StrPolyHash*h){mrb_int oc=h->cap;const char**ok=h->keys;sp_RbVal*ov=h->vals;h->cap*=2;h->mask=h->cap-1;h->keys=(const char**)calloc(h->cap,sizeof(const char*));h->vals=(sp_RbVal*)calloc(h->cap,sizeof(sp_RbVal));h->order=(const char**)realloc(h->order,sizeof(const char*)*h->cap);h->len=0;for(mrb_int i=0;i<oc;i++){if(ok[i]){mrb_int idx=(mrb_int)(sp_str_hash(ok[i])&h->mask);while(h->keys[idx])idx=(idx+1)&h->mask;h->keys[idx]=ok[i];h->vals[idx]=ov[i];h->len++;}}free(ok);free(ov);}static sp_RbVal sp_StrPolyHash_get(sp_StrPolyHash*h,const char*k){if(!h)return sp_box_nil();mrb_int idx=(mrb_int)(sp_str_hash(k)&h->mask);while(h->keys[idx]){if(strcmp(h->keys[idx],k)==0)return h->vals[idx];idx=(idx+1)&h->mask;}if(h->dproc)return h->dproc(h,k);return h->default_v;}static void sp_StrPolyHash_set(sp_StrPolyHash*h,const char*k,sp_RbVal v){if(h->len*2>=h->cap)sp_StrPolyHash_grow(h);mrb_int idx=(mrb_int)(sp_str_hash(k)&h->mask);while(h->keys[idx]){if(strcmp(h->keys[idx],k)==0){h->vals[idx]=v;return;}idx=(idx+1)&h->mask;}h->keys[idx]=k;h->vals[idx]=v;h->order[h->len]=k;h->len++;}static mrb_bool sp_StrPolyHash_has_key(sp_StrPolyHash*h,const char*k){mrb_int idx=(mrb_int)(sp_str_hash(k)&h->mask);while(h->keys[idx]){if(strcmp(h->keys[idx],k)==0)return TRUE;idx=(idx+1)&h->mask;}return FALSE;}static mrb_bool sp_StrPolyHash_has_key(sp_StrPolyHash*h,const char*k){if(!h)return FALSE;mrb_int idx=(mrb_int)(sp_str_hash(k)&h->mask);while(h->keys[idx]){if(strcmp(h->keys[idx],k)==0)return TRUE;idx=(idx+1)&h->mask;}return FALSE;}static mrb_int sp_StrPolyHash_length(sp_StrPolyHash*h){return h->len;}static sp_StrArray*sp_StrPolyHash_keys(sp_StrPolyHash*h){sp_StrArray*a=sp_StrArray_new();if(!h)return a;for(mrb_int i=0;i<h->len;i++)sp_StrArray_push(a,h->order[i]);return a;}static sp_PolyArray*sp_StrPolyHash_values(sp_StrPolyHash*h){sp_PolyArray*a=sp_PolyArray_new();for(mrb_int i=0;i<h->len;i++)sp_PolyArray_push(a,sp_StrPolyHash_get(h,h->order[i]));return a;}spinel_codegen.rbmodifiedView file
Loading diff…
test/gc_grandparent_ivar_scan.rbaddedView file
Loading diff…
test/gc_grandparent_ivar_scan.rb.expectedaddedView file
Loading diff…