Commit 913a8331

913a83318df6Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified

Description

Make String#<< binary-safe (preserve embedded NUL), clang-safe

Reapply the matz/spinel#1479 fix correctly after the first attempt (b2e92ea8, reverted in 12384eed) corrupted the heap under clang.

Two compounding bugs, fixed without the earlier landmine:

  • Read side: a mutable buffer (marker 0xfd) carried its length only in the sp_String struct, so once it escaped as a const char* (concat, inspect, FFI byte-writes) sp_str_byte_len fell back to strlen and truncated at the first embedded NUL. Now the 0xfd buffer carries the same length-bearing sp_str_hdr that 0xfe/0xfc heap strings use ([hdr][0xfd][data][NUL]); sp_str_byte_len reads it, and the mutators publish s->len into the header on each change.

  • Write side: String#<< sized the appended operand with strlen, so an operand beginning with 0x00 appended nothing. A new binary-safe sp_String_append_bin sizes it with sp_str_byte_len; codegen emits it for Ruby <</concat, where the operand is a known marked string.

The first attempt routed the in-place mutators' operand/source length through sp_str_byte_len unconditionally. But the runtime's own inspect helpers call sp_String_new / sp_String_append with bare C string literals (116 sites) that have no marker byte; sp_str_byte_len reads operand[-1] as a marker, and under clang's rodata layout it coincided with a heap-string marker, reading a bogus header off unrelated rodata -> heap corruption (gcc's layout avoided the coincidence). So the bare-literal mutators keep strlen; only the dedicated _bin entry point, reached solely from codegen with marked operands, uses sp_str_byte_len.

954 test (gcc + clang) / 57 bench / optcarrot 59662 (752 fps); the repro and the failing cases verified clean under clang -fsanitize=address,undefined.

Closes #1479

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
913a83318df6
Parent
ec5b82d62e47

lib/sp_runtime.hmodifiedView file

@@ -485,7 +485,7 @@
485485 static inline size_t sp_str_byte_len(const char *s) {
486486 if (!s) return 0;
487487 unsigned char marker = ((const unsigned char *)s)[-1];
488- if (marker == 0xfe || marker == 0xfc) {
488+ if (marker == 0xfe || marker == 0xfc || marker == 0xfd) {
489489 return (((const sp_str_hdr *)(s - 1)) - 1)->len;
490490 }
491491 return strlen(s);
@@ -494,7 +494,7 @@
494494 static inline void sp_str_set_len(char *s, size_t len) {
495495 if (!s) return;
496496 unsigned char marker = ((unsigned char *)s)[-1];
497- if (marker == 0xfe || marker == 0xfc) {
497+ if (marker == 0xfe || marker == 0xfc || marker == 0xfd) {
498498 sp_str_hdr *hd = ((sp_str_hdr *)(s - 1)) - 1;
499499 hd->len = (uint32_t)len;
500500 hd->hash = 0; /* length change implies content change: invalidate cached hash */
@@ -2024,7 +2024,49 @@
20242024 strings stay frozen via the 0xff marker byte (see sp_str_setbyte). */
20252025 static inline mrb_bool sp_String_is_frozen(sp_String*s){if(!s)return TRUE;sp_gc_hdr*h=(sp_gc_hdr*)((char*)s-sizeof(sp_gc_hdr));return h->frozen;}
20262026 static inline sp_String*sp_String_freeze(sp_String*s){if(s){sp_gc_hdr*h=(sp_gc_hdr*)((char*)s-sizeof(sp_gc_hdr));h->frozen=1;}return s;}
2027-static void sp_String_fin(void*p){free(((sp_String*)p)->data-1);}
2027+/* A mutable String's payload buffer carries the same length-bearing sp_str_hdr
2028+ that 0xfe/0xfc heap strings use, so an escaped const char* (sp_String_cstr,
2029+ inspect) is binary-safe: sp_str_byte_len reads the header rather than strlen,
2030+ which truncates at an embedded NUL (matz/spinel#1479). Raw block layout:
2031+ [sp_str_hdr][0xfd marker][data ...][NUL]. s->data points at `data`, so
2032+ s->data[-1] == 0xfd (the GC-skip + mutable-provenance marker) and the header
2033+ sits just below it. The ctor and in-place mutators are also reached from the
2034+ runtime's own inspect helpers with *bare C string literals* (no marker byte),
2035+ so they must size their source/operand with strlen, NOT a [-1]-marker-reading
2036+ function. Binary `String#<<` uses sp_String_append_bin, emitted only at the
2037+ generated call site where the operand is a known marked spinel string. */
2038+#define SP_FD_HDR (sizeof(sp_str_hdr)+1) /* header + marker byte */
2039+#define SP_FD_OVH (sizeof(sp_str_hdr)+2) /* header + marker + NUL terminator */
2040+static inline char *sp_fd_base(const char *data){return (char*)data-SP_FD_HDR;}
2041+/* Lay out header + 0xfd marker in a fresh (re)allocated raw block; return data.
2042+ Leaves the data bytes untouched (realloc preserves them); len is published
2043+ separately by sp_fd_publish once s->len is settled. */
2044+static inline char *sp_fd_setup(char *raw){
2045+ sp_str_hdr *h = (sp_str_hdr *)raw;
2046+ h->next = NULL; h->size = 0; h->len = 0; h->hash = 0;
2047+ char *body = (char *)(h + 1);
2048+ body[0] = (char)0xfd;
2049+ return body + 1;
2050+}
2051+/* Publish s->len into the buffer header for readers of the escaped pointer. */
2052+static inline void sp_fd_publish(sp_String *s){
2053+ sp_str_hdr *h = (sp_str_hdr *)sp_fd_base(s->data);
2054+ h->len = (uint32_t)s->len; h->hash = 0;
2055+}
2056+/* Ensure the buffer can hold `need` data bytes (+NUL); realloc preserving the
2057+ header layout. Returns 0 only on realloc failure (string left unchanged). */
2058+static inline int sp_fd_grow(sp_String *s, int64_t need){
2059+ if (need < s->cap) return 1;
2060+ sp_gc_hdr *h = (sp_gc_hdr *)((char *)s - sizeof(sp_gc_hdr));
2061+ int64_t new_cap = need * 2 + 16;
2062+ char *raw = (char *)realloc(sp_fd_base(s->data), SP_FD_OVH + new_cap);
2063+ if (!raw) return 0;
2064+ sp_gc_bytes -= s->cap + SP_FD_OVH; h->size -= s->cap + SP_FD_OVH;
2065+ s->cap = new_cap; s->data = sp_fd_setup(raw);
2066+ h->size += s->cap + SP_FD_OVH; sp_gc_bytes += s->cap + SP_FD_OVH;
2067+ return 1;
2068+}
2069+static void sp_String_fin(void*p){free(sp_fd_base(((sp_String*)p)->data));}
20282070 static sp_String*sp_String_new(const char*s){
20292071 /* Copy s's payload into a raw-malloc'd buffer BEFORE sp_gc_alloc.
20302072 If s is a heap string (sp_str_alloc) whose only liveness anchor
@@ -2037,25 +2079,32 @@
20372079 any GC inside sp_gc_alloc. */
20382080 int64_t len=(int64_t)strlen(s);
20392081 int64_t cap=len*2+16;
2040- char*raw=(char*)malloc(cap+2);
2041- raw[0]=(char)0xfd;
2042- char*data=raw+1;
2043- memcpy(data,s,len+1);
2082+ char*raw=(char*)malloc(SP_FD_OVH+cap);
2083+ char*data=sp_fd_setup(raw);
2084+ memcpy(data,s,len);data[len]=0;
20442085 sp_String*r=(sp_String*)sp_gc_alloc(sizeof(sp_String),sp_String_fin,NULL);
20452086 r->len=len;r->cap=cap;r->data=data;
2046- {sp_gc_hdr*h=(sp_gc_hdr*)((char*)r-sizeof(sp_gc_hdr));h->size+=r->cap+2;sp_gc_bytes+=r->cap+2;}
2087+ {sp_gc_hdr*h=(sp_gc_hdr*)((char*)r-sizeof(sp_gc_hdr));h->size+=r->cap+SP_FD_OVH;sp_gc_bytes+=r->cap+SP_FD_OVH;}
2088+ sp_fd_publish(r);
20472089 return r;
20482090 }
20492091 /* Issue #757: realloc on growth used to overwrite s->data unconditionally,
20502092 leaking the old buffer + null-dereferencing if realloc fails. Now we
20512093 check the result and bail without mutating on failure. */
2052-static inline void sp_String_append(sp_String*s,const char*t){if(!s||!t)return;if(sp_String_is_frozen(s)){sp_raise_cls("FrozenError","can't modify frozen String");return;}int64_t tl=(int64_t)strlen(t);if(s->len+tl>=s->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)s-sizeof(sp_gc_hdr));int64_t new_cap=(s->len+tl)*2+16;char*raw=(char*)realloc(s->data-1,new_cap+2);if(!raw)return;sp_gc_bytes-=s->cap+2;h->size-=s->cap+2;s->cap=new_cap;raw[0]=(char)0xfd;s->data=raw+1;h->size+=s->cap+2;sp_gc_bytes+=s->cap+2;}memcpy(s->data+s->len,t,tl+1);s->len+=tl;}
2053-static inline void sp_String_prepend(sp_String*s,const char*t){if(!s||!t)return;if(sp_String_is_frozen(s)){sp_raise_cls("FrozenError","can't modify frozen String");return;}int64_t tl=(int64_t)strlen(t);if(s->len+tl>=s->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)s-sizeof(sp_gc_hdr));int64_t new_cap=(s->len+tl)*2+16;char*raw=(char*)realloc(s->data-1,new_cap+2);if(!raw)return;sp_gc_bytes-=s->cap+2;h->size-=s->cap+2;s->cap=new_cap;raw[0]=(char)0xfd;s->data=raw+1;h->size+=s->cap+2;sp_gc_bytes+=s->cap+2;}memmove(s->data+tl,s->data,s->len+1);memcpy(s->data,t,tl);s->len+=tl;}
2094+/* Shared append core: `tl` is the operand byte length, computed by the caller
2095+ (strlen for the bare-literal-safe entry, sp_str_byte_len for the binary one). */
2096+static inline void sp_fd_append_len(sp_String*s,const char*t,int64_t tl){if(!sp_fd_grow(s,s->len+tl))return;memcpy(s->data+s->len,t,tl);s->len+=tl;s->data[s->len]=0;sp_fd_publish(s);}
2097+static inline void sp_String_append(sp_String*s,const char*t){if(!s||!t)return;if(sp_String_is_frozen(s)){sp_raise_cls("FrozenError","can't modify frozen String");return;}sp_fd_append_len(s,t,(int64_t)strlen(t));}
2098+/* Binary-safe append: sizes the operand with the header length so an embedded
2099+ NUL is preserved. Only emitted by codegen for Ruby `String#<<` / `concat`,
2100+ where the operand is a marked spinel string (never a bare C literal). */
2101+static inline void sp_String_append_bin(sp_String*s,const char*t){if(!s||!t)return;if(sp_String_is_frozen(s)){sp_raise_cls("FrozenError","can't modify frozen String");return;}sp_fd_append_len(s,t,(int64_t)sp_str_byte_len(t));}
2102+static inline void sp_String_prepend(sp_String*s,const char*t){if(!s||!t)return;if(sp_String_is_frozen(s)){sp_raise_cls("FrozenError","can't modify frozen String");return;}int64_t tl=(int64_t)strlen(t);if(!sp_fd_grow(s,s->len+tl))return;memmove(s->data+tl,s->data,s->len+1);memcpy(s->data,t,tl);s->len+=tl;sp_fd_publish(s);}
20542103 /* Issue #741: String#insert(idx, str) -- insert str at idx. Negative
20552104 idx is relative to len+1 (insert before tail). */
2056-static inline void sp_String_insert(sp_String*s,int64_t idx,const char*t){if(!s||!t)return;if(sp_String_is_frozen(s)){sp_raise_cls("FrozenError","can't modify frozen String");return;}int64_t tl=(int64_t)strlen(t);if(tl==0)return;if(idx<0)idx+=s->len+1;if(idx<0)idx=0;if(idx>s->len)idx=s->len;if(s->len+tl>=s->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)s-sizeof(sp_gc_hdr));int64_t new_cap=(s->len+tl)*2+16;char*raw=(char*)realloc(s->data-1,new_cap+2);if(!raw)return;sp_gc_bytes-=s->cap+2;h->size-=s->cap+2;s->cap=new_cap;raw[0]=(char)0xfd;s->data=raw+1;h->size+=s->cap+2;sp_gc_bytes+=s->cap+2;}memmove(s->data+idx+tl,s->data+idx,s->len-idx+1);memcpy(s->data+idx,t,tl);s->len+=tl;}
2105+static inline void sp_String_insert(sp_String*s,int64_t idx,const char*t){if(!s||!t)return;if(sp_String_is_frozen(s)){sp_raise_cls("FrozenError","can't modify frozen String");return;}int64_t tl=(int64_t)strlen(t);if(tl==0)return;if(idx<0)idx+=s->len+1;if(idx<0)idx=0;if(idx>s->len)idx=s->len;if(!sp_fd_grow(s,s->len+tl))return;memmove(s->data+idx+tl,s->data+idx,s->len-idx+1);memcpy(s->data+idx,t,tl);s->len+=tl;sp_fd_publish(s);}
20572106 /* Issue #740/#741 sibling: String#replace(s) -- replace entire content. */
2058-static inline void sp_String_replace(sp_String*s,const char*t){if(!s||!t)return;if(sp_String_is_frozen(s)){sp_raise_cls("FrozenError","can't modify frozen String");return;}int64_t tl=(int64_t)strlen(t);if(tl>=s->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)s-sizeof(sp_gc_hdr));int64_t new_cap=tl*2+16;char*raw=(char*)realloc(s->data-1,new_cap+2);if(!raw)return;sp_gc_bytes-=s->cap+2;h->size-=s->cap+2;s->cap=new_cap;raw[0]=(char)0xfd;s->data=raw+1;h->size+=s->cap+2;sp_gc_bytes+=s->cap+2;}memcpy(s->data,t,tl);s->data[tl]='\0';s->len=tl;}
2107+static inline void sp_String_replace(sp_String*s,const char*t){if(!s||!t)return;if(sp_String_is_frozen(s)){sp_raise_cls("FrozenError","can't modify frozen String");return;}int64_t tl=(int64_t)strlen(t);if(!sp_fd_grow(s,tl))return;memcpy(s->data,t,tl);s->data[tl]='\0';s->len=tl;sp_fd_publish(s);}
20592108 static inline const char*sp_String_cstr(sp_String*s){return s->data;}
20602109 static inline int64_t sp_String_length(sp_String*s){return s->len;}
20612110 static sp_String*sp_String_dup(sp_String*s){return sp_String_new(s->data);}