Commit 93638f21
93638f216ef6Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
fix(runtime,codegen): String#setbyte mutates heap strings, no-ops on literals
Issue #504 disabled s.setbyte(i, v) because the prior naive
emit cast rc to char * and wrote through; for a literal-
sourced receiver (rodata, marker 0xff) that SEGV'd. The fix
silenced the write entirely, which broke benchmarks like
bm_ruby_xor (result = a.dup; result.setbyte(i, b)) — dup
returns a heap buffer that's safe to mutate, but the static
codegen couldn't distinguish at the call site.
Resolve with a runtime gate. Spinel's string-heap already tags
every buffer with a marker byte at s[-1]:
0xfe / 0xfc -> sp_str_alloc heap (writable; GC unmarked / marked) 0xfd -> sp_String wrapper buffer (writable) 0xff -> rodata literal (writing here would SEGV) other -> FFI return, unknown provenance
New runtime helper sp_str_setbyte consults the marker; writes
only when whitelisted. Codegen routes setbyte through the
helper. Literal recvs stay safe (no-op, matches pre-#504 fix);
dup'd / +-built / sp_String buffers mutate; FFI strings get
the conservative no-op.
Benchmark: bm_ruby_xor FAIL -> PASS. All 57 benchmarks green. Default raise: 628 / 0 / 0 (one new regression test). Optcarrot checksum 59662 unchanged.
Closes #504-followup (setbyte support for heap-allocated strings).
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 93638f216ef6
- Parent
- 00a23f9868f7
lib/sp_runtime.hmodifiedView file
static const char*sp_str_rstrip(const char*s){size_t l=strlen(s);while(l>0&&isspace((unsigned char)s[l-1]))l--;char*r=sp_str_alloc_raw(l+1);memcpy(r,s,l);r[l]=0;return r;}static const char*sp_str_dup(const char*s){char*r=sp_str_alloc_raw(strlen(s)+1);strcpy(r,s);return r;}/* String#setbyte: mutate s[i] = v in place. Spinel strings are stored as `const char *` so the compile-time path can't tell writable buffers from rodata literals. The runtime marker byte at s[-1] resolves it: 0xfe / 0xfc -> sp_str_alloc heap (writable, GC unmarked / marked) 0xfd -> sp_String wrapper buffer (writable) 0xff -> rodata literal (writing here would SEGV) other -> FFI / unknown provenance, conservative no-op Whitelisted markers get the write; everything else falls through. Returns the byte value (matches CRuby's setbyte return). Issue #504 originally disabled the mutating emit because the compile- time path couldn't avoid the literal-SEGV; this runtime gate plugs the gap so dup'd strings work (bm_ruby_xor). */static inline mrb_int sp_str_setbyte(const char *s, mrb_int i, mrb_int v) { if (!s) return v; unsigned char m = ((const unsigned char *)s)[-1]; if (m == 0xfe || m == 0xfc || m == 0xfd) { ((char *)s)[i] = (char)v; } return v;}typedef struct{char*data;int64_t len;int64_t cap;}sp_String;static void sp_String_fin(void*p){free(((sp_String*)p)->data-1);}static sp_String*sp_String_new(const char*s){spinel_codegen.rbmodifiedView file
Loading diff…
test/str_setbyte_dup_mutates.rbaddedView file
Loading diff…
test/str_setbyte_dup_mutates.rb.expectedaddedView file
Loading diff…