Commit f4203243

f420324356d8Merged

Sam Ruby <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified

Description

Address review: preserve GC_STRESS threshold, NULL-check delete temps, +test

Review feedback on #1463 (gemini-code-assist, Copilot):

  • sp_str_threshold_init (mirrors sp_gc_threshold_init): the recompute clamped the string threshold back to 256*1024 after the first collection, so SPINEL_GC_STRESS=1 silently relaxed string-heap stressing to the normal floor after one cycle. Thread an init floor (2048 under stress) through the recompute so stress mode persists. Normal-mode behavior is unchanged (init stays 256KB).
  • sp_str_delete / sp_str_delete_n: NULL-check the new malloc temp and sp_oom_die on failure, consistent with the rest of the runtime (was an unchecked deref).
  • test/string_plus_heap_gc.rb: regression for #1450 itself — a String#+ loop with no object allocation must still collect (asserts GC.stat["cycle"] advances; the only thing that can advance it here is the string-heap trigger).

Re-validated: normal make test 932/932 (+1, the new test). SPINEL_GC_STRESS=1 make test 925 pass / 7 fail — one MORE than the prior 6 because persistent stress now genuinely fires every alloc and surfaces one further pre-existing unrooted-temp gap (gsub_sub_block; the gap is latent, not introduced here). The other 6 are unchanged. This is the intended diagnostic effect; closing those gaps is the incremental rooting work the PR proposes gating in CI.

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

Details

Commit
f420324356d8
Parent
a5aded520b5b

lib/sp_runtime.hmodifiedView file

@@ -378,6 +378,7 @@
378378 comment in sp_str_alloc). */
379379 static __thread size_t sp_str_heap_bytes = 0; /* live string-heap bytes */
380380 static __thread size_t sp_str_threshold = 256*1024; /* mirrors sp_gc_threshold init */
381+static __thread size_t sp_str_threshold_init = 256*1024; /* recompute floor; lowered to 2048 under SPINEL_GC_STRESS so stress mode survives a collection (mirrors sp_gc_threshold_init) */
381382 static __thread int sp_str_stress_checked = 0;
382383 #define SPL(s) (&("\xff" s)[1])
383384 static const char sp_str_empty_data[] = "\xff";
@@ -446,14 +447,14 @@
446447 arguments to sp_str_concat) must be reachable across this point -- they are
447448 for rooted locals; the codegen's SP_GC_ROOT discipline is what keeps them
448449 so. Threshold recompute mirrors sp_gc_alloc's. */
449- if (!sp_str_stress_checked) { sp_str_stress_checked = 1; const char *e = getenv("SPINEL_GC_STRESS"); if (e && *e && *e != '0') sp_str_threshold = 2048; }
450+ if (!sp_str_stress_checked) { sp_str_stress_checked = 1; const char *e = getenv("SPINEL_GC_STRESS"); if (e && *e && *e != '0') { sp_str_threshold = 2048; sp_str_threshold_init = 2048; } }
450451 if (sp_str_heap_bytes > sp_str_threshold) {
451452 size_t before = sp_str_heap_bytes;
452453 sp_gc_collect(); /* runs sp_str_sweep, which decrements sp_str_heap_bytes */
453454 size_t freed = before - sp_str_heap_bytes;
454455 if (freed < before/4) sp_str_threshold = before*2;
455- else if (sp_str_heap_bytes > 0) { sp_str_threshold = sp_str_heap_bytes*4; if (sp_str_threshold < 256*1024) sp_str_threshold = 256*1024; }
456- else sp_str_threshold = 256*1024;
456+ else if (sp_str_heap_bytes > 0) { sp_str_threshold = sp_str_heap_bytes*4; if (sp_str_threshold < sp_str_threshold_init) sp_str_threshold = sp_str_threshold_init; }
457+ else sp_str_threshold = sp_str_threshold_init;
457458 }
458459 sp_str_hdr *h = (sp_str_hdr *)malloc(total);
459460 if (!h) sp_oom_die();
@@ -1848,7 +1849,7 @@
18481849 /* Build into a malloc temp and read all of `s` BEFORE the result sp_str_alloc:
18491850 that allocation can now trigger a string-heap collection, which would sweep
18501851 an unrooted `s` mid-copy (the read-first pattern sp_str_tr/sp_str_format use). */
1851-static const char*sp_str_delete(const char*s,const char*chars){if(!s)return sp_str_empty;if(!chars)return s;int negate=0;const char*csp=chars;if(*csp=='^'&&*(csp+1)){negate=1;csp++;}size_t setn;uint32_t*set=sp_utf8_decode_charset(csp,&setn);size_t bl=strlen(s);char*buf=(char*)malloc(bl+1);size_t n=0;const char*p=s;while(*p){uint32_t cp;int cn=sp_utf8_decode(p,&cp);int in_set=sp_utf8_set_has(set,setn,cp);if(negate)in_set=!in_set;if(!in_set){memcpy(buf+n,p,cn);n+=cn;}p+=cn;}buf[n]=0;free(set);char*r=sp_str_alloc(n);memcpy(r,buf,n+1);free(buf);return r;}
1852+static const char*sp_str_delete(const char*s,const char*chars){if(!s)return sp_str_empty;if(!chars)return s;int negate=0;const char*csp=chars;if(*csp=='^'&&*(csp+1)){negate=1;csp++;}size_t setn;uint32_t*set=sp_utf8_decode_charset(csp,&setn);size_t bl=strlen(s);char*buf=(char*)malloc(bl+1);if(!buf)sp_oom_die();size_t n=0;const char*p=s;while(*p){uint32_t cp;int cn=sp_utf8_decode(p,&cp);int in_set=sp_utf8_set_has(set,setn,cp);if(negate)in_set=!in_set;if(!in_set){memcpy(buf+n,p,cn);n+=cn;}p+=cn;}buf[n]=0;free(set);char*r=sp_str_alloc(n);memcpy(r,buf,n+1);free(buf);return r;}
18521853 /* Issue #921: shrink the heap-string header length to match the
18531854 squeezed payload — the alloc gives bl+1 bytes, the squeezed
18541855 write fills n<=bl, leaving the header's stored length stale.
@@ -1862,7 +1863,7 @@
18621863 /* Multi-arg delete/squeeze: delete (or squeeze runs of) characters that
18631864 are in the INTERSECTION of all n charset args, mirroring
18641865 sp_str_count_n. Each arg is a charset spec (^negation, a-z ranges). */
1865-static const char*sp_str_delete_n(const char*s,const char**chars,mrb_int n){if(!s)return sp_str_empty;if(n<=0)return s;size_t*setns=(size_t*)malloc(n*sizeof(size_t));uint32_t**sets=(uint32_t**)malloc(n*sizeof(uint32_t*));int*negs=(int*)malloc(n*sizeof(int));for(mrb_int i=0;i<n;i++){if(!chars[i])sp_raise_cls("TypeError","no implicit conversion of nil into String");const char*cs=chars[i];negs[i]=0;if(*cs=='^'&&*(cs+1)){negs[i]=1;cs++;}sets[i]=sp_utf8_decode_charset(cs,&setns[i]);}size_t bl=strlen(s);char*buf=(char*)malloc(bl+1);size_t m=0;const char*p=s;while(*p){uint32_t cp;int cn=sp_utf8_decode(p,&cp);int all=1;for(mrb_int i=0;i<n;i++){int in_set=sp_utf8_set_has(sets[i],setns[i],cp);if(negs[i])in_set=!in_set;if(!in_set){all=0;break;}}if(!all){memcpy(buf+m,p,cn);m+=cn;}p+=cn;}buf[m]=0;for(mrb_int i=0;i<n;i++)free(sets[i]);free(sets);free(setns);free(negs);char*r=sp_str_alloc(m);memcpy(r,buf,m+1);free(buf);return r;}
1866+static const char*sp_str_delete_n(const char*s,const char**chars,mrb_int n){if(!s)return sp_str_empty;if(n<=0)return s;size_t*setns=(size_t*)malloc(n*sizeof(size_t));uint32_t**sets=(uint32_t**)malloc(n*sizeof(uint32_t*));int*negs=(int*)malloc(n*sizeof(int));for(mrb_int i=0;i<n;i++){if(!chars[i])sp_raise_cls("TypeError","no implicit conversion of nil into String");const char*cs=chars[i];negs[i]=0;if(*cs=='^'&&*(cs+1)){negs[i]=1;cs++;}sets[i]=sp_utf8_decode_charset(cs,&setns[i]);}size_t bl=strlen(s);char*buf=(char*)malloc(bl+1);if(!buf)sp_oom_die();size_t m=0;const char*p=s;while(*p){uint32_t cp;int cn=sp_utf8_decode(p,&cp);int all=1;for(mrb_int i=0;i<n;i++){int in_set=sp_utf8_set_has(sets[i],setns[i],cp);if(negs[i])in_set=!in_set;if(!in_set){all=0;break;}}if(!all){memcpy(buf+m,p,cn);m+=cn;}p+=cn;}buf[m]=0;for(mrb_int i=0;i<n;i++)free(sets[i]);free(sets);free(setns);free(negs);char*r=sp_str_alloc(m);memcpy(r,buf,m+1);free(buf);return r;}
18661867 static const char*sp_str_squeeze_n(const char*s,const char**chars,mrb_int n){if(!s)return sp_str_empty;if(n<=0)return sp_str_squeeze(s);size_t*setns=(size_t*)malloc(n*sizeof(size_t));uint32_t**sets=(uint32_t**)malloc(n*sizeof(uint32_t*));int*negs=(int*)malloc(n*sizeof(int));for(mrb_int i=0;i<n;i++){if(!chars[i])sp_raise_cls("TypeError","no implicit conversion of nil into String");const char*cs=chars[i];negs[i]=0;if(*cs=='^'&&*(cs+1)){negs[i]=1;cs++;}sets[i]=sp_utf8_decode_charset(cs,&setns[i]);}size_t bl=strlen(s);char*r=sp_str_alloc_raw(bl+1);size_t m=0;uint32_t prev=0xFFFFFFFFu;const char*p=s;while(*p){uint32_t cp;int cn=sp_utf8_decode(p,&cp);int all=1;for(mrb_int i=0;i<n;i++){int in_set=sp_utf8_set_has(sets[i],setns[i],cp);if(negs[i])in_set=!in_set;if(!in_set){all=0;break;}}if(!all){memcpy(r+m,p,cn);m+=cn;prev=0xFFFFFFFFu;}else if(cp!=prev){memcpy(r+m,p,cn);m+=cn;prev=cp;}p+=cn;}r[m]=0;sp_str_set_len(r,m);for(mrb_int i=0;i<n;i++)free(sets[i]);free(sets);free(setns);free(negs);return r;}
18671868
18681869 /* Forward decl from sp_crypto.h (libspinel_rt.a). Used by