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
sp_str_hdr *h = (sp_str_hdr *)malloc(total); if (!h) sp_oom_die(); h->next = sp_str_heap; h->size = (uint32_t)total; h->len = (uint32_t)len; h->hash = 0; h->size = total; h->len = len; sp_str_heap = h; /* Don't fold string-heap pressure into sp_gc_bytes : the threshold heuristic in sp_gc_alloc is keyed on heap survivors, and if (!s) return; unsigned char marker = ((unsigned char *)s)[-1]; if (marker == 0xfe || marker == 0xfc) { sp_str_hdr *hd = ((sp_str_hdr *)(s - 1)) - 1; hd->len = (uint32_t)len; hd->hash = 0; /* length change implies content change: invalidate cached hash */ (((sp_str_hdr *)(s - 1)) - 1)->len = len; }}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;}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)];}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;}/* Cold path: compute (and, for a heap/heap-frozen string, cache) the FNV hash. Kept out-of-line so sp_str_hash's inline fast path -- a cached-hash read -- stays tiny and doesn't bloat every call site's code layout. */static SP_NOINLINE uint64_t sp_str_hash_miss(const char*s,unsigned char m){ if(m==0xfe||m==0xfc||m==0xf1){ sp_str_hdr*hd=((sp_str_hdr*)(s-1))-1; uint64_t h=sp_str_hash_compute(s); hd->hash=h?h:1; return hd->hash; } return sp_str_hash_compute(s);}static inline uint64_t sp_str_hash(const char*s){ unsigned char m=((const unsigned char*)s)[-1]; if(m==0xfe||m==0xfc||m==0xf1){ uint64_t cached=(((sp_str_hdr*)(s-1))-1)->hash; if(cached)return cached; } return sp_str_hash_miss(s,m);}static inline uint64_t sp_str_hash(const char*s){uint64_t h=14695981039346656037ULL;while(*s){h^=(unsigned char)*s++;h*=1099511628211ULL;}return h;}static void sp_StrIntHash_fin(void*p){sp_StrIntHash*h=(sp_StrIntHash*)p;free(h->keys);free(h->vals);free(h->order);}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]);}}/* default_v is SP_INT_NIL for a hash with no explicit default ({} / {k=>v}), return v; } unsigned char m = ((const unsigned char *)s)[-1]; if (m == 0xfe || m == 0xfc) { (((sp_str_hdr *)(s - 1)) - 1)->hash = 0; /* invalidate cached key hash */ ((char *)s)[i] = (char)v; return v; } if (m == 0xfd) { if (m == 0xfe || m == 0xfc || m == 0xfd) { ((char *)s)[i] = (char)v; return v; }lib/sp_types.hmodifiedView file
Loading diff…