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

@@ -330,10 +330,21 @@
330330 return n;
331331 }
332332
333+/* True when `s` carries one of spinel's own string markers in the
334+ preceding byte (0xfe / 0xfc heap, 0xff rodata literal). FFI returns
335+ a bare `const char *` whose preceding byte is whatever C variable
336+ sits before the buffer in memory — using the pointer as a cache
337+ key without this gate aliased subsequent FFI calls into the prior
338+ call's cached length (#611). 0xfd (sp_String wrapper) is excluded
339+ too because its buffer can move on append. */
340+static inline int sp_str_cacheable(const char *s) {
341+ unsigned char m = ((const unsigned char *)s)[-1];
342+ return m == 0xfe || m == 0xfc || m == 0xff;
343+}
344+
333345 static mrb_int sp_str_length(const char*s){
334346 if (!s) return 0;
335- unsigned char m = ((const unsigned char *)s)[-1];
336- if (m == 0xfd) return sp_str_count_chars(s, sp_str_byte_len(s));
347+ if (!sp_str_cacheable(s)) return sp_str_count_chars(s, sp_str_byte_len(s));
337348 unsigned h = sp_str_lcache_hash(s);
338349 if (sp_str_lcache[h].s == s) return sp_str_lcache[h].char_len;
339350 size_t bl = sp_str_byte_len(s);
@@ -353,8 +364,7 @@
353364 static inline int sp_str_eq(const char*a,const char*b){if(!a||!b)return a==b;return strcmp(a,b)==0;}
354365 static size_t sp_utf8_byte_offset(const char*s,mrb_int char_idx){
355366 if (!s || char_idx <= 0) return 0;
356- unsigned char m = ((const unsigned char *)s)[-1];
357- if (m != 0xfd) {
367+ if (sp_str_cacheable(s)) {
358368 unsigned h = sp_str_lcache_hash(s);
359369 if (sp_str_lcache[h].s == s
360370 && (size_t)sp_str_lcache[h].char_len == sp_str_lcache[h].byte_len) {