Commit 155ec59a

155ec59a2269Merged

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

Description

Copy a mutable String's buffer when converting it to an immutable string

A mutable String (String.new + <<) is an sp_String object that owns its data buffer; the object's finalizer frees it, and the buffer carries a 0xfd marker byte that sp_gc_mark deliberately ignores. Converting that buffer to a const char* by aliasing ->data therefore produces a string whose lifetime the GC cannot track independently: once the unreferenced sp_String is collected the alias dangles. This is the use-after-free that crashes a roundhouse Rails app under load, where every render boxes the view builder's buffer (sp_box_str(view_result->data)) into iv_body and a GC between render and the layout sprintf frees it.

Copy into a sweep-owned (0xfe) buffer via sp_str_dup_external at every site where the alias escapes the builder's rooted scope, centralized in mutable_str_to_cstr:

  • the poly box (mutable_str -> SP_TAG_STR), the reported crash;
  • compile_expr_for_expected_type's mutable_str -> string coercion;
  • Kernel#String(builder);
  • builder.to_s;
  • the general mutable-string-method fallthrough, whose receiver- returning methods (itself, encode, each_char/each_byte, and gsub/sub/chomp/tr_s on a no-op) would otherwise hand back the alias. The fallthrough copy is GC-rooted so it also survives a GC inside an allocating method (split, chars, scan, ...) that reads the receiver.

Transient reads where the builder stays rooted across the expression (method receivers passed to allocating helpers, sprintf args, iteration) are left aliasing; they were never the bug.

This makes mutable_str -> string a snapshot, which diverges from CRuby's shared-mutable String(s) / s.to_s-returns-self, but Spinel already types those results as immutable strings, so it is consistent with the subset.

Fixes #1071

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
155ec59a2269
Parent
b55b1483270b

spinel_codegen.rbmodifiedView file

@@ -7382,7 +7382,7 @@
73827382 # return type was inferred too narrowly upstream, but the call site
73837383 # can recover at the boundary.
73847384 if at == "mutable_str"
7385- return "(" + val + ")->data"
7385+ return mutable_str_to_cstr(val)
73867386 end
73877387 end
73887388 if is_obj_type(expected_base) == 1 && at == "poly"
@@ -15816,6 +15816,19 @@
1581615816 tmp
1581715817 end
1581815818
15819+ # A mutable_str (sp_String *) owns its `->data` buffer, freed by the
15820+ # object's finalizer. That buffer carries a 0xfd marker byte which
15821+ # sp_gc_mark deliberately ignores, so a `const char *` aliasing it
15822+ # cannot be kept alive independently: once the unreferenced sp_String
15823+ # is collected the alias dangles. Whenever the alias would escape the
15824+ # object's rooted scope -- boxed into a poly slot, returned, stored --
15825+ # copy into a sweep-owned (0xfe) buffer via sp_str_dup_external so the
15826+ # resulting immutable string carries its own lifetime. `expr` is
15827+ # emitted exactly once.
15828+ def mutable_str_to_cstr(expr)
15829+ "sp_str_dup_external((" + expr + ")->data)"
15830+ end
15831+
1581915832 def compile_arg0(nid)
1582015833 args_id = @nd_arguments[nid]
1582115834 if args_id >= 0
@@ -17490,7 +17503,7 @@
1749017503 return "sp_String_dup(" + rc + ")"
1749117504 end
1749217505 if mname == "to_s"
17493- return rc + "->data"
17506+ return mutable_str_to_cstr(rc)
1749417507 end
1749517508 # Issues #740, #741: mutating methods on mutable_str.
1749617509 if mname == "prepend" || mname == "<<" || mname == "concat"
@@ -17541,8 +17554,18 @@
1754117554 if mname == "frozen?"
1754217555 return "sp_String_is_frozen(" + rc + ")"
1754317556 end
17544- # For all other string methods, convert via ->data
17545- r = compile_string_method_expr(nid, mname, rc + "->data")
17557+ # For all other string methods, operate on a sweep-owned copy of the
17558+ # buffer instead of the object-owned alias. Methods that return the
17559+ # receiver unchanged (itself, encode, each_char/each_byte, and
17560+ # gsub/sub/chomp/tr_s on a no-op) would otherwise hand back a
17561+ # `const char *` aliasing `->data`, which dangles once the sp_String
17562+ # is collected. The copy is rooted so it also survives a GC inside an
17563+ # allocating method (split, chars, scan, ...) that reads the receiver.
17564+ cptmp = new_temp
17565+ @needs_gc = 1
17566+ emit(" const char *" + cptmp + " = " + mutable_str_to_cstr(rc) + ";")
17567+ emit_gc_root_for_expr(cptmp, "string")
17568+ r = compile_string_method_expr(nid, mname, cptmp)
1754617569 if r != ""
1754717570 return r
1754817571 end
@@ -18260,7 +18283,7 @@
1826018283 return "sp_bigint_to_s((sp_Bigint *)" + a_e_S + ")"
1826118284 end
1826218285 if at_S == "mutable_str"
18263- return "(" + a_e_S + ")->data"
18286+ return mutable_str_to_cstr(a_e_S)
1826418287 end
1826518288 if at_S == "poly"
1826618289 @needs_rb_value = 1
@@ -30033,7 +30056,7 @@
3003330056 # a statement-expression so side effects fire exactly once.
3003430057 if at == "mutable_str"
3003530058 mtmp = new_temp
30036- return "({ sp_String *" + mtmp + " = " + val + "; " + mtmp + " ? sp_box_str(" + mtmp + "->data) : sp_box_nil(); })"
30059+ return "({ sp_String *" + mtmp + " = " + val + "; " + mtmp + " ? sp_box_str(" + mutable_str_to_cstr(mtmp) + ") : sp_box_nil(); })"
3003730060 end
3003830061 # Other pointer types — box with a neutral cls_id of 0 rather
3003930062 # than truncating the pointer to int.