Commit f3609e20
f3609e20cb4eMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
Make the UTF-8 encoding name a spinel literal so str-hash can't overread
The string-hash header cache reads the marker byte at s[-1] to decide
whether a key is a heap string carrying a cached hash. sp_encoding_utf8
returned a bare C literal "UTF-8", which has no controlled preceding
byte, so hashing an Encoding key (enc_hash[__ENCODING__]) read one byte
before the rodata object -- a global-buffer-overflow (ASAN-confirmed) and,
on layouts where that byte happened to be a heap marker, a spurious cache
hit returning a garbage hash. clang's rodata layout triggered it, failing
source_encoding on the clang CI (gcc happened to lay it out benignly).
Give the name a 0xff-marked spinel rodata literal, matching every other
string in the runtime: now s[-1] is an in-bounds literal marker and the
hash takes the correct (non-cached) FNV path. The same invariant already
holds for sp_str_byte_len / sp_str_length, which also read s[-1].
Refs #282
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- f3609e20cb4e
- Parent
- 024156fdcf70
lib/sp_runtime.hmodifiedView file
as a tiny value type so `__ENCODING__` and `String#encoding` can answer Ruby's Encoding-shaped protocol without carrying full transcoding state. */static inline sp_Encoding sp_encoding_utf8(void){return(sp_Encoding){"UTF-8"};}/* The name is a spinel rodata-literal string (0xff marker prefix), not a bare C literal: it flows into sp_str_hash / sp_str_byte_len etc. as a hash key and String value, all of which read the marker byte at s[-1]. A bare literal has no controlled preceding byte, so that read is a global-buffer-overflow (ASAN-caught) and could spuriously hit the heap-header cache path (#282). */static inline sp_Encoding sp_encoding_utf8(void){return(sp_Encoding){&("\xff" "UTF-8")[1]};}static inline const char*sp_encoding_name(sp_Encoding e){return e.name?e.name:sp_str_empty;}static inline const char*sp_encoding_inspect(sp_Encoding e){return sp_sprintf("#<Encoding:%s>",sp_encoding_name(e));}static inline mrb_bool sp_encoding_eq(sp_Encoding a,sp_Encoding b){const char*an=sp_encoding_name(a);const char*bn=sp_encoding_name(b);return strcmp(an,bn)==0;}