Commit e3e748ce
e3e748ce6d76Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
fix(runtime): char_len cache gates on spinel string markers only
Regression from fe0d123. The 32-slot pointer-keyed char_len cache
keyed off raw const char * and only excluded 0xfd (sp_String
wrapper). FFI returns from a static C buffer (e.g. the static char buf[N] pattern OriPekelman hits at scale via tep's
sphttp_request_buf) have no spinel marker byte, a stable pointer
across calls, and mutating content/length between calls — perfect
shape to trip a pointer-keyed cache. The first call's length stuck;
subsequent calls served the stale value.
Adds sp_str_cacheable(s) predicate that only returns true for the
three managed markers: 0xfe (heap, unmarked), 0xfc (heap, marked),
0xff (rodata literal). Heap strings still invalidate via
sp_str_sweep; rodata is immutable so the entry is correct for the
program's lifetime. FFI-returned bare buffers fall through to the
direct sp_str_count_chars walk every call — same correctness as
pre-fe0d123.
json_parse benchmark stays at the post-fe0d123 41-42ms (rodata
literals still hit the cache); the regression-only path is FFI
:str returns, where the speedup never applied anyway.
Note: a 1.2% theoretical risk remains where an unmanaged buffer's preceding byte randomly matches a spinel marker. In practice the byte before a static-allocated C array is typically a length, flag, or padding byte, not 0xfe/0xfc/0xff. If this materializes, the proper fix is per-allocator address-range bounds rather than a marker check.
Closes #611. 587 tests pass / bootstrap fixpoint clean.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- e3e748ce6d76
- Parent
- 22863fac2e07
lib/sp_runtime.hmodifiedView file
return n;}/* True when `s` carries one of spinel's own string markers in the preceding byte (0xfe / 0xfc heap, 0xff rodata literal). FFI returns a bare `const char *` whose preceding byte is whatever C variable sits before the buffer in memory — using the pointer as a cache key without this gate aliased subsequent FFI calls into the prior call's cached length (#611). 0xfd (sp_String wrapper) is excluded too because its buffer can move on append. */static inline int sp_str_cacheable(const char *s) { unsigned char m = ((const unsigned char *)s)[-1]; return m == 0xfe || m == 0xfc || m == 0xff;}static mrb_int sp_str_length(const char*s){ if (!s) return 0; unsigned char m = ((const unsigned char *)s)[-1]; if (m == 0xfd) return sp_str_count_chars(s, sp_str_byte_len(s)); if (!sp_str_cacheable(s)) return sp_str_count_chars(s, sp_str_byte_len(s)); unsigned h = sp_str_lcache_hash(s); if (sp_str_lcache[h].s == s) return sp_str_lcache[h].char_len; size_t bl = sp_str_byte_len(s);static inline int sp_str_eq(const char*a,const char*b){if(!a||!b)return a==b;return strcmp(a,b)==0;}static size_t sp_utf8_byte_offset(const char*s,mrb_int char_idx){ if (!s || char_idx <= 0) return 0; unsigned char m = ((const unsigned char *)s)[-1]; if (m != 0xfd) { if (sp_str_cacheable(s)) { unsigned h = sp_str_lcache_hash(s); if (sp_str_lcache[h].s == s && (size_t)sp_str_lcache[h].char_len == sp_str_lcache[h].byte_len) {