Commit 89e3aee0

89e3aee06af0Merged

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

Description

Drop the str-hash header cache; it overread on non-spinel string keys

The #282 cache stored each key's FNV hash in its sp_str_hdr, gated on the marker byte at s[-1]. That read is out of bounds for a string value that isn't a spinel allocation -- the runtime's own bare C string literals used as hash keys (Encoding names, GC.stat keys, gsub/sub replacement-hash keys). An ASAN sweep flagged the s[-1] read as a global-buffer-overflow in seven tests, and on a clang rodata layout where that byte happened to be a heap marker the cache returned a garbage hash, failing source_encoding on the clang CI.

Revert sp_str_hash to the plain FNV-1a it was before #282 (no s[-1] read, correct for any pointer), restore the 24-byte size_t header, and drop the now-unneeded mutation invalidation. The always-safe half of #282 -- the sp_str_eq pointer-equality fast path in the hash probes -- is kept.

(Pre-existing s[-1] reads in sp_str_byte_len / sp_str_cacheable have the same latent issue on bare-literal string values but predate #282 and do not write; tracked separately.)

Refs #282

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

Details

Commit
89e3aee06af0
Parent
f3609e20cb4e

lib/sp_runtime.hmodifiedView file

@@ -432,9 +432,8 @@
432432 sp_str_hdr *h = (sp_str_hdr *)malloc(total);
433433 if (!h) sp_oom_die();
434434 h->next = sp_str_heap;
435- h->size = (uint32_t)total;
436- h->len = (uint32_t)len;
437- h->hash = 0;
435+ h->size = total;
436+ h->len = len;
438437 sp_str_heap = h;
439438 /* Don't fold string-heap pressure into sp_gc_bytes : the
440439 threshold heuristic in sp_gc_alloc is keyed on heap survivors, and
@@ -474,9 +473,7 @@
474473 if (!s) return;
475474 unsigned char marker = ((unsigned char *)s)[-1];
476475 if (marker == 0xfe || marker == 0xfc) {
477- sp_str_hdr *hd = ((sp_str_hdr *)(s - 1)) - 1;
478- hd->len = (uint32_t)len;
479- hd->hash = 0; /* length change implies content change: invalidate cached hash */
476+ (((sp_str_hdr *)(s - 1)) - 1)->len = len;
480477 }
481478 }
482479
@@ -1172,27 +1169,7 @@
11721169 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;}
11731170 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)];}
11741171
1175-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;}
1176-/* Cold path: compute (and, for a heap/heap-frozen string, cache) the FNV
1177- hash. Kept out-of-line so sp_str_hash's inline fast path -- a cached-hash
1178- read -- stays tiny and doesn't bloat every call site's code layout. */
1179-static SP_NOINLINE uint64_t sp_str_hash_miss(const char*s,unsigned char m){
1180- if(m==0xfe||m==0xfc||m==0xf1){
1181- sp_str_hdr*hd=((sp_str_hdr*)(s-1))-1;
1182- uint64_t h=sp_str_hash_compute(s);
1183- hd->hash=h?h:1;
1184- return hd->hash;
1185- }
1186- return sp_str_hash_compute(s);
1187-}
1188-static inline uint64_t sp_str_hash(const char*s){
1189- unsigned char m=((const unsigned char*)s)[-1];
1190- if(m==0xfe||m==0xfc||m==0xf1){
1191- uint64_t cached=(((sp_str_hdr*)(s-1))-1)->hash;
1192- if(cached)return cached;
1193- }
1194- return sp_str_hash_miss(s,m);
1195-}
1172+static inline uint64_t sp_str_hash(const char*s){uint64_t h=14695981039346656037ULL;while(*s){h^=(unsigned char)*s++;h*=1099511628211ULL;}return h;}
11961173 static void sp_StrIntHash_fin(void*p){sp_StrIntHash*h=(sp_StrIntHash*)p;free(h->keys);free(h->vals);free(h->order);}
11971174 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]);}}
11981175 /* default_v is SP_INT_NIL for a hash with no explicit default ({} / {k=>v}),
@@ -1939,12 +1916,7 @@
19391916 return v;
19401917 }
19411918 unsigned char m = ((const unsigned char *)s)[-1];
1942- if (m == 0xfe || m == 0xfc) {
1943- (((sp_str_hdr *)(s - 1)) - 1)->hash = 0; /* invalidate cached key hash */
1944- ((char *)s)[i] = (char)v;
1945- return v;
1946- }
1947- if (m == 0xfd) {
1919+ if (m == 0xfe || m == 0xfc || m == 0xfd) {
19481920 ((char *)s)[i] = (char)v;
19491921 return v;
19501922 }