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

@@ -1254,6 +1254,28 @@
12541254 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;}
12551255 static const char*sp_str_dup(const char*s){char*r=sp_str_alloc_raw(strlen(s)+1);strcpy(r,s);return r;}
12561256
1257+/* String#setbyte: mutate s[i] = v in place. Spinel strings are
1258+ stored as `const char *` so the compile-time path can't tell
1259+ writable buffers from rodata literals. The runtime marker byte
1260+ at s[-1] resolves it:
1261+ 0xfe / 0xfc -> sp_str_alloc heap (writable, GC unmarked / marked)
1262+ 0xfd -> sp_String wrapper buffer (writable)
1263+ 0xff -> rodata literal (writing here would SEGV)
1264+ other -> FFI / unknown provenance, conservative no-op
1265+ Whitelisted markers get the write; everything else falls through.
1266+ Returns the byte value (matches CRuby's setbyte return). Issue
1267+ #504 originally disabled the mutating emit because the compile-
1268+ time path couldn't avoid the literal-SEGV; this runtime gate
1269+ plugs the gap so dup'd strings work (bm_ruby_xor). */
1270+static inline mrb_int sp_str_setbyte(const char *s, mrb_int i, mrb_int v) {
1271+ if (!s) return v;
1272+ unsigned char m = ((const unsigned char *)s)[-1];
1273+ if (m == 0xfe || m == 0xfc || m == 0xfd) {
1274+ ((char *)s)[i] = (char)v;
1275+ }
1276+ return v;
1277+}
1278+
12571279 typedef struct{char*data;int64_t len;int64_t cap;}sp_String;
12581280 static void sp_String_fin(void*p){free(((sp_String*)p)->data-1);}
12591281 static sp_String*sp_String_new(const char*s){