Commit 9f0cb3f0

9f0cb3f03d68Merged

Sam Ruby <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified

Description

Fix two GC use-after-frees in generated view rendering

Both surfaced as intermittent SIGSEGVs serving the roundhouse blog's /articles/new under load, pinpointed with AddressSanitizer to a heap-use-after-free during string/hash work in the layout render.

  1. Chained string concat operands weren't rooted. a + b + c lowers to sp_str_concat3(a, b, c) (and 4 parts to sp_str_concat4). C argument evaluation order is unspecified, so a fresh GC string produced by one operand sits unrooted on the stack while a sibling operand allocates -- if that allocation triggers sp_gc_collect the first operand is freed, and sp_str_concatN then reads it. The

    =5-part path (sp_str_concat_arr) already hoisted each operand into a rooted temp for exactly this reason; the 3- and 4-part paths inlined the operands and were exposed. Hoist uniformly for all N>=3. Triggered in the layout by stylesheet_link_tag("application", ..) + "\n" + stylesheet_link_tag("tailwind", ..): the first tag is built, the second tag's render_attrs allocates and collects it.

  2. The sym-keyed -> str/sym hash converter helpers (sp_StrPolyHash_from_sym_{int,str,poly}hash, sp_SymStrHash_to_sym_poly, sp_SymPolyHash_merge_str) iterate their input hash while calling allocating functions (sp_sym_to_s, spHash_set) without rooting the input or the result being built. When the input is a fresh temporary at the call site (e.g. render_attrs(SymStrHash_merge(...))), a GC mid-loop frees it and the next h->order[i] reads freed memory. Root the input(s) and the result across the loop, matching the existing sp__json helpers that already root their output.

Reproduction: built the blog with AddressSanitizer and a fully instrumented runtime; before, wrk on /articles/new aborts within seconds (heap-use-after-free in sp_str_concat3 / from_sym_str_hash); after, it runs clean (550k+ requests, 0 errors, all paths 200).

Test: concat_chain_operand_gc_root.rb forces a GC inside each concat operand; pre-fix the 4-part chain prints garbage ("[z][z]"), post-fix it matches the CRuby reference. Suite 726 pass / 0 fail.

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

Details

Commit
9f0cb3f03d68
Parent
b29baef903c2

spinel_codegen.rbmodifiedView file

@@ -8893,8 +8893,8 @@
88938893 # are merged in either direction, both result paths return a
88948894 # fresh sym_poly_hash with the str entries boxed via sp_box_str.
88958895 # Issue #515.
8896- emit_raw("static sp_SymPolyHash*sp_SymStrHash_to_sym_poly(sp_SymStrHash*h){sp_SymPolyHash*r=sp_SymPolyHash_new();for(mrb_int i=0;i<h->len;i++)sp_SymPolyHash_set(r,h->order[i],sp_box_str(sp_SymStrHash_get(h,h->order[i])));return r;}")
8897- emit_raw("static sp_SymPolyHash*sp_SymPolyHash_merge_str(sp_SymPolyHash*a,sp_SymStrHash*b){sp_SymPolyHash*r=sp_SymPolyHash_new();for(mrb_int i=0;i<a->len;i++)sp_SymPolyHash_set(r,a->order[i],sp_SymPolyHash_get(a,a->order[i]));for(mrb_int i=0;i<b->len;i++)sp_SymPolyHash_set(r,b->order[i],sp_box_str(sp_SymStrHash_get(b,b->order[i])));return r;}")
8896+ emit_raw("static sp_SymPolyHash*sp_SymStrHash_to_sym_poly(sp_SymStrHash*h){SP_GC_SAVE();SP_GC_ROOT(h);sp_SymPolyHash*r=sp_SymPolyHash_new();SP_GC_ROOT(r);for(mrb_int i=0;i<h->len;i++)sp_SymPolyHash_set(r,h->order[i],sp_box_str(sp_SymStrHash_get(h,h->order[i])));return r;}")
8897+ emit_raw("static sp_SymPolyHash*sp_SymPolyHash_merge_str(sp_SymPolyHash*a,sp_SymStrHash*b){SP_GC_SAVE();SP_GC_ROOT(a);SP_GC_ROOT(b);sp_SymPolyHash*r=sp_SymPolyHash_new();SP_GC_ROOT(r);for(mrb_int i=0;i<a->len;i++)sp_SymPolyHash_set(r,a->order[i],sp_SymPolyHash_get(a,a->order[i]));for(mrb_int i=0;i<b->len;i++)sp_SymPolyHash_set(r,b->order[i],sp_box_str(sp_SymStrHash_get(b,b->order[i])));return r;}")
88988898 emit_raw("")
88998899 end
89008900
@@ -9476,13 +9476,13 @@
94769476 # Sym keys go through sp_sym_to_s; values are boxed to sp_RbVal.
94779477 if @needs_str_poly_hash == 1
94789478 if @needs_sym_int_hash == 1
9479- emit_raw("static sp_StrPolyHash*sp_StrPolyHash_from_sym_int_hash(sp_SymIntHash*h){sp_StrPolyHash*r=sp_StrPolyHash_new();if(!h)return r;r->default_v=sp_box_int(h->default_v);for(mrb_int i=0;i<h->len;i++){sp_sym k=h->order[i];sp_StrPolyHash_set(r,sp_sym_to_s(k),sp_box_int(sp_SymIntHash_get(h,k)));}return r;}")
9479+ emit_raw("static sp_StrPolyHash*sp_StrPolyHash_from_sym_int_hash(sp_SymIntHash*h){SP_GC_SAVE();SP_GC_ROOT(h);sp_StrPolyHash*r=sp_StrPolyHash_new();SP_GC_ROOT(r);if(!h)return r;r->default_v=sp_box_int(h->default_v);for(mrb_int i=0;i<h->len;i++){sp_sym k=h->order[i];sp_StrPolyHash_set(r,sp_sym_to_s(k),sp_box_int(sp_SymIntHash_get(h,k)));}return r;}")
94809480 end
94819481 if @needs_sym_str_hash == 1
9482- emit_raw("static sp_StrPolyHash*sp_StrPolyHash_from_sym_str_hash(sp_SymStrHash*h){sp_StrPolyHash*r=sp_StrPolyHash_new();if(!h)return r;if(h->default_v)r->default_v=sp_box_str(h->default_v);for(mrb_int i=0;i<h->len;i++){sp_sym k=h->order[i];sp_StrPolyHash_set(r,sp_sym_to_s(k),sp_box_str(sp_SymStrHash_get(h,k)));}return r;}")
9482+ emit_raw("static sp_StrPolyHash*sp_StrPolyHash_from_sym_str_hash(sp_SymStrHash*h){SP_GC_SAVE();SP_GC_ROOT(h);sp_StrPolyHash*r=sp_StrPolyHash_new();SP_GC_ROOT(r);if(!h)return r;if(h->default_v)r->default_v=sp_box_str(h->default_v);for(mrb_int i=0;i<h->len;i++){sp_sym k=h->order[i];sp_StrPolyHash_set(r,sp_sym_to_s(k),sp_box_str(sp_SymStrHash_get(h,k)));}return r;}")
94839483 end
94849484 if @needs_sym_poly_hash == 1
9485- emit_raw("static sp_StrPolyHash*sp_StrPolyHash_from_sym_poly_hash(sp_SymPolyHash*h){sp_StrPolyHash*r=sp_StrPolyHash_new();if(!h)return r;r->default_v=h->default_v;for(mrb_int i=0;i<h->len;i++){sp_sym k=h->order[i];sp_StrPolyHash_set(r,sp_sym_to_s(k),sp_SymPolyHash_get(h,k));}return r;}")
9485+ emit_raw("static sp_StrPolyHash*sp_StrPolyHash_from_sym_poly_hash(sp_SymPolyHash*h){SP_GC_SAVE();SP_GC_ROOT(h);sp_StrPolyHash*r=sp_StrPolyHash_new();SP_GC_ROOT(r);if(!h)return r;r->default_v=h->default_v;for(mrb_int i=0;i<h->len;i++){sp_sym k=h->order[i];sp_StrPolyHash_set(r,sp_sym_to_s(k),sp_SymPolyHash_get(h,k));}return r;}")
94869486 end
94879487 end
94889488 end
@@ -19671,21 +19671,21 @@
1967119671 end
1967219672 end
1967319673 # Flatten chained string concat: a + b + c → sp_str_concat3(a,b,c)
19674+ # Hoist every part into a rooted temp before the concat call. The
19675+ # call-argument order (concat3 / concat4) and the compound-literal
19676+ # initializer order (concat_arr) are both unspecified, so a part
19677+ # that is a fresh GC string would otherwise sit unrooted on the C
19678+ # stack while a sibling part evaluates and triggers sp_gc_collect,
19679+ # leaving sp_str_concatN to read freed memory. Observed as a
19680+ # use-after-free in the layout's
19681+ # `stylesheet_link_tag(..) + "\n" + stylesheet_link_tag(..)` under
19682+ # load: the first tag is built, the second tag's render allocates
19683+ # and collects the first. The >=5 (concat_arr) path always hoisted;
19684+ # the 3- and 4-part paths inlined the parts and were exposed.
19685+ # @needs_gc is set in scan_features for any string `+`, so
19686+ # SP_GC_SAVE() is already in the function header.
1967419687 parts = collect_concat_chain(nid)
19675- if parts.length == 3
19676- return "sp_str_concat3(" + parts[0] + ", " + parts[1] + ", " + parts[2] + ")"
19677- end
19678- if parts.length == 4
19679- return "sp_str_concat4(" + parts[0] + ", " + parts[1] + ", " + parts[2] + ", " + parts[3] + ")"
19680- end
19681- if parts.length >= 5
19682- # Variable-length: single malloc for N parts via sp_str_concat_arr.
19683- # Hoist each part into a rooted temp first — the compound-literal
19684- # initializer order is unspecified, and any part that is a fresh
19685- # GC string would otherwise sit unrooted on the C stack while
19686- # later parts evaluate (and may trigger sp_gc_collect).
19687- # @needs_gc is set in scan_features for any string `+`, ensuring
19688- # SP_GC_SAVE() is in the function header before we emit roots here.
19688+ if parts.length >= 3
1968919689 tnames = "".split(",", -1)
1969019690 k = 0
1969119691 while k < parts.length
@@ -19695,6 +19695,13 @@
1969519695 tnames.push(t)
1969619696 k = k + 1
1969719697 end
19698+ if parts.length == 3
19699+ return "sp_str_concat3(" + tnames[0] + ", " + tnames[1] + ", " + tnames[2] + ")"
19700+ end
19701+ if parts.length == 4
19702+ return "sp_str_concat4(" + tnames[0] + ", " + tnames[1] + ", " + tnames[2] + ", " + tnames[3] + ")"
19703+ end
19704+ # Variable-length: single malloc for N parts via sp_str_concat_arr.
1969819705 arr = "(const char *const[]){"
1969919706 k = 0
1970019707 while k < tnames.length