Commit a0c358af

a0c358afb94eMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified

Description

runtime: re-add the str-hash header cache, safely this time

The #282 FNV-hash-in-header cache (worth ~16 fps on optcarrot) was dropped in 4620034 because sp_str_hash's s[-1] marker read overread on string values that aren't spinel allocations -- the runtime's own bare C-literal hash keys (GC.stat keys, gsub/sub match buffers, the encoding name). ASAN flagged the read and a clang rodata layout where that byte aliased a heap marker made the cache return a garbage hash (source_encoding failed on clang CI).

Re-add the cache, but first make every key that reaches sp_str_hash carry a valid marker byte at s[-1], so the read is always in-bounds and the marker is trustworthy:

  • GC.stat keys -> SPL() rodata literals (0xff prefix).
  • gsub/sub transient match buffers -> a leading 0xff byte before the key, so s[-1] is in-bounds and routes to the plain (non-caching) FNV path (these stack/heap buffers have no sp_str_hdr to cache into). The encoding name was already converted (7fdb436). User-code keys are always spinel strings (heap, or 0xff-prefixed SPL literals), so they were never the problem.

Verified: an ASAN sweep of 309 string/hash/regex tests shows zero overflows in sp_str_hash; the full suite passes under both gcc and clang (928 tests, incl. source_encoding); optcarrot checksum 59662, ~16 fps recovered. The pre-existing s[-1] reads in sp_str_byte_len / sp_str_cacheable still overread on bare-literal values (present on master, do not write) and are left untouched -- tracked separately.

Refs #282

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

Details

Commit
a0c358afb94e
Parent
0fe595c54332

lib/sp_runtime.hmodifiedView file

@@ -431,8 +431,9 @@
431431 sp_str_hdr *h = (sp_str_hdr *)malloc(total);
432432 if (!h) sp_oom_die();
433433 h->next = sp_str_heap;
434- h->size = total;
435- h->len = len;
434+ h->size = (uint32_t)total;
435+ h->len = (uint32_t)len;
436+ h->hash = 0;
436437 sp_str_heap = h;
437438 /* Don't fold string-heap pressure into sp_gc_bytes : the
438439 threshold heuristic in sp_gc_alloc is keyed on heap survivors, and
@@ -472,7 +473,9 @@
472473 if (!s) return;
473474 unsigned char marker = ((unsigned char *)s)[-1];
474475 if (marker == 0xfe || marker == 0xfc) {
475- (((sp_str_hdr *)(s - 1)) - 1)->len = len;
476+ sp_str_hdr *hd = ((sp_str_hdr *)(s - 1)) - 1;
477+ hd->len = (uint32_t)len;
478+ hd->hash = 0; /* length change implies content change: invalidate cached hash */
476479 }
477480 }
478481
@@ -1182,7 +1185,27 @@
11821185 static sp_StrArray*sp_StrArray_shuffle(sp_StrArray*a){sp_StrArray*r=sp_StrArray_new();sp_StrArray_replace(r,a);sp_StrArray_shuffle_bang(r);return r;}
11831186 static const char *sp_StrArray_sample(sp_StrArray*a){if(a->len<=0)return sp_str_empty;return a->data[(mrb_int)(rand()%a->len)];}
11841187
1185-static inline uint64_t sp_str_hash(const char*s){uint64_t h=14695981039346656037ULL;while(*s){h^=(unsigned char)*s++;h*=1099511628211ULL;}return h;}
1188+static inline uint64_t sp_str_hash_compute(const char*s){uint64_t h=14695981039346656037ULL;while(*s){h^=(unsigned char)*s++;h*=1099511628211ULL;}return h;}
1189+/* Cold path: compute (and, for a heap/heap-frozen string, cache) the FNV
1190+ hash. Kept out-of-line so sp_str_hash's inline fast path -- a cached-hash
1191+ read -- stays tiny and doesn't bloat every call site's code layout. */
1192+static SP_NOINLINE uint64_t sp_str_hash_miss(const char*s,unsigned char m){
1193+ if(m==0xfe||m==0xfc||m==0xf1){
1194+ sp_str_hdr*hd=((sp_str_hdr*)(s-1))-1;
1195+ uint64_t h=sp_str_hash_compute(s);
1196+ hd->hash=h?h:1;
1197+ return hd->hash;
1198+ }
1199+ return sp_str_hash_compute(s);
1200+}
1201+static inline uint64_t sp_str_hash(const char*s){
1202+ unsigned char m=((const unsigned char*)s)[-1];
1203+ if(m==0xfe||m==0xfc||m==0xf1){
1204+ uint64_t cached=(((sp_str_hdr*)(s-1))-1)->hash;
1205+ if(cached)return cached;
1206+ }
1207+ return sp_str_hash_miss(s,m);
1208+}
11861209 static void sp_StrIntHash_fin(void*p){sp_StrIntHash*h=(sp_StrIntHash*)p;free(h->keys);free(h->vals);free(h->order);}
11871210 static void sp_StrIntHash_scan(void*p){sp_StrIntHash*h=(sp_StrIntHash*)p;for(mrb_int i=0;i<h->cap;i++){if(h->keys[i])sp_mark_string(h->keys[i]);}}
11881211 /* default_v is SP_INT_NIL for a hash with no explicit default ({} / {k=>v}),
@@ -1216,7 +1239,10 @@
12161239 /* GC.stat snapshot: String=>Integer hash over the collector globals.
12171240 full_runs derives from sp_gc_cycle / SP_GC_FULL_INTERVAL (the major
12181241 collection cadence). */
1219-static sp_StrIntHash*sp_gc_stat(void){sp_StrIntHash*h=sp_StrIntHash_new();sp_StrIntHash_set(h,"bytes",(mrb_int)sp_gc_bytes);sp_StrIntHash_set(h,"old_bytes",(mrb_int)sp_gc_old_bytes);sp_StrIntHash_set(h,"threshold",(mrb_int)sp_gc_threshold);sp_StrIntHash_set(h,"cycle",(mrb_int)sp_gc_cycle);sp_StrIntHash_set(h,"full_runs",(mrb_int)(sp_gc_cycle/SP_GC_FULL_INTERVAL));return h;}
1242+/* Keys are spinel rodata literals (SPL: 0xff marker prefix) so the str-hash
1243+ header cache's s[-1] read is in-bounds -- a bare C literal here would
1244+ overread (and could alias a heap marker on some rodata layouts). */
1245+static sp_StrIntHash*sp_gc_stat(void){sp_StrIntHash*h=sp_StrIntHash_new();sp_StrIntHash_set(h,SPL("bytes"),(mrb_int)sp_gc_bytes);sp_StrIntHash_set(h,SPL("old_bytes"),(mrb_int)sp_gc_old_bytes);sp_StrIntHash_set(h,SPL("threshold"),(mrb_int)sp_gc_threshold);sp_StrIntHash_set(h,SPL("cycle"),(mrb_int)sp_gc_cycle);sp_StrIntHash_set(h,SPL("full_runs"),(mrb_int)(sp_gc_cycle/SP_GC_FULL_INTERVAL));return h;}
12201246
12211247 static void sp_StrStrHash_fin(void*p){sp_StrStrHash*h=(sp_StrStrHash*)p;free(h->keys);free(h->vals);free(h->order);}
12221248 static void sp_StrStrHash_scan(void*p){sp_StrStrHash*h=(sp_StrStrHash*)p;for(mrb_int i=0;i<h->cap;i++){if(h->keys[i]){sp_mark_string(h->keys[i]);sp_mark_string(h->vals[i]);}}if(h->default_v)sp_mark_string(h->default_v);}
@@ -1929,7 +1955,12 @@
19291955 return v;
19301956 }
19311957 unsigned char m = ((const unsigned char *)s)[-1];
1932- if (m == 0xfe || m == 0xfc || m == 0xfd) {
1958+ if (m == 0xfe || m == 0xfc) {
1959+ (((sp_str_hdr *)(s - 1)) - 1)->hash = 0; /* invalidate cached key hash */
1960+ ((char *)s)[i] = (char)v;
1961+ return v;
1962+ }
1963+ if (m == 0xfd) {
19331964 ((char *)s)[i] = (char)v;
19341965 return v;
19351966 }
@@ -2640,8 +2671,13 @@
26402671 if (n <= 0 || caps[0] < 0) break;
26412672 size_t before = caps[0] - pos;
26422673 int mlen = caps[1] - caps[0];
2643- char keybuf[64];
2644- char *key = mlen < (int)sizeof(keybuf) ? keybuf : (char *)malloc(mlen + 1);
2674+ /* Lay a 0xff (rodata-literal) marker byte right before the transient key
2675+ so sp_str_hash's s[-1] read is in-bounds and routes to the plain
2676+ (non-caching) path -- this buffer has no sp_str_hdr to cache into. */
2677+ char keybuf[65]; keybuf[0] = (char)0xff;
2678+ char *kbuf = mlen + 1 < (int)sizeof(keybuf) ? keybuf : (char *)malloc(mlen + 2);
2679+ if (kbuf != keybuf) kbuf[0] = (char)0xff;
2680+ char *key = kbuf + 1;
26452681 memcpy(key, str + caps[0], mlen);
26462682 key[mlen] = 0;
26472683 const char *rep = sp_StrStrHash_has_key(h, key) ? sp_StrStrHash_get(h, key) : "";
@@ -2649,7 +2685,7 @@
26492685 if (olen + before + rlen >= cap) { cap = (olen + before + rlen) * 2 + 64; out = (char *)realloc(out, cap); }
26502686 memcpy(out + olen, str + pos, before); olen += before;
26512687 memcpy(out + olen, rep, rlen); olen += rlen;
2652- if (key != keybuf) free(key);
2688+ if (kbuf != keybuf) free(kbuf);
26532689 if (caps[0] == caps[1]) {
26542690 /* Zero-width match: keep the source char at this position and step
26552691 past it (see sp_re_gsub for the rationale). */
@@ -2682,8 +2718,12 @@
26822718 int n = re_exec(pat, str, slen, 0, caps, 64);
26832719 if (n <= 0 || caps[0] < 0) return str;
26842720 int mlen = caps[1] - caps[0];
2685- char keybuf[64];
2686- char *key = mlen < (int)sizeof(keybuf) ? keybuf : (char *)malloc(mlen + 1);
2721+ /* 0xff marker before the transient key: keeps sp_str_hash's s[-1] read
2722+ in-bounds and on the non-caching path (no sp_str_hdr behind this buffer). */
2723+ char keybuf[65]; keybuf[0] = (char)0xff;
2724+ char *kbuf = mlen + 1 < (int)sizeof(keybuf) ? keybuf : (char *)malloc(mlen + 2);
2725+ if (kbuf != keybuf) kbuf[0] = (char)0xff;
2726+ char *key = kbuf + 1;
26872727 memcpy(key, str + caps[0], mlen);
26882728 key[mlen] = 0;
26892729 const char *rep = (h && sp_StrStrHash_has_key(h, key)) ? sp_StrStrHash_get(h, key) : "";
@@ -2695,7 +2735,7 @@
26952735 memcpy(out + caps[0], rep, rlen);
26962736 memcpy(out + caps[0] + rlen, str + caps[1], rest);
26972737 out[total] = 0;
2698- if (key != keybuf) free(key);
2738+ if (kbuf != keybuf) free(kbuf);
26992739 return out;
27002740 }
27012741