Commit 5f566f22

5f566f22d197Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

Root freshly-allocated constructor/super arguments across the call

emit_args_filled (the .new / super argument path) emitted each argument expression inline into the call, unrooted. A freshly-allocated heap argument -- the canonical case being the implicit {} for Foo.new reaching def initialize(attrs = {}) -- then sat in an unrooted C temporary while sp_Foo_new ran. sp_Foo_new SP_POOL_NEWs the object (which can trigger a collection) before sp_Foo_initialize roots lv_attrs, so under GC pressure the hash was swept mid-call: a use-after-free that ASAN flags in sp_SymPolyHash_get and that surfaces as SIGSEGV@0x0 on some platforms.

Normal method calls already hoist+root their arguments via emit_dispatch; only this constructor/super path was missing it. Route its argument emission through a new emit_arg_rooted helper that, for a pointer-backed or poly argument, evaluates the expression into a g_pre temp and SP_GC_ROOT(_RBVAL)s it before the call -- the same pattern emit_dispatch uses. Scalar arguments need no root and stay inline.

optcarrot fps is unaffected (the per-pixel hot path constructs no objects); checksum 59662 holds, all gates green.

Fixes #1445

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

Details

Commit
5f566f22d197
Parent
6bee60d084e6

src/codegen_fold.cmodifiedView file

@@ -2400,6 +2400,35 @@
24002400 buf_printf(b, " _t%d; })", t);
24012401 }
24022402
2403+/* Like emit_arg_or_default, but hoists a pointer-backed / poly argument into a
2404+ g_pre temp and roots it before the call. A fresh allocation passed straight
2405+ into a callee that allocates before it roots the parameter -- the canonical
2406+ case being the `{}` for `def initialize(attrs = {})` into sp_<C>_new, which
2407+ SP_POOL_NEWs (can GC) before sp_<C>_initialize roots lv_attrs (#1445) -- would
2408+ otherwise be collected mid-call (use-after-free / SIGSEGV@0x0). This is the
2409+ #1052-deferred "fresh temp passed straight into a call" shape. emit_args_filled
2410+ (the .new / super arg path) emitted args inline; normal method calls already
2411+ hoist+root via emit_dispatch. Rooting in the caller's frame keeps the value
2412+ alive across the whole call. A scalar (int/float/...) arg needs no root and is
2413+ emitted inline. */
2414+static void emit_arg_rooted(Compiler *c, Scope *m, int idx, int provided, Buf *out) {
2415+ LocalVar *p = scope_local(m, m->pnames[idx]);
2416+ TyKind pt = p ? p->type : TY_UNKNOWN;
2417+ int poly = (pt == TY_POLY);
2418+ if (!poly && !needs_root(pt)) { emit_arg_or_default(c, m, idx, provided, out); return; }
2419+ Buf ab; memset(&ab, 0, sizeof ab);
2420+ emit_arg_or_default(c, m, idx, provided, &ab);
2421+ int t = ++g_tmp;
2422+ emit_indent(g_pre, g_indent);
2423+ emit_ctype(c, pt, g_pre);
2424+ buf_printf(g_pre, " _t%d = %s;\n", t, ab.p ? ab.p : default_value(pt));
2425+ emit_indent(g_pre, g_indent);
2426+ if (poly) buf_printf(g_pre, "SP_GC_ROOT_RBVAL(_t%d);\n", t);
2427+ else buf_printf(g_pre, "SP_GC_ROOT(_t%d);\n", t);
2428+ buf_printf(out, "_t%d", t);
2429+ free(ab.p);
2430+}
2431+
24032432 void emit_args_filled(Compiler *c, int callee_idx, int argsNode, const char *lead, Buf *out) {
24042433 Scope *m = &c->scopes[callee_idx];
24052434 const NodeTable *nt = c->nt;
@@ -2423,7 +2452,7 @@
24232452 if (mt == TY_POLY && et != TY_POLY) emit_boxed_text(c, et, txt, out);
24242453 else buf_puts(out, txt);
24252454 }
2426- else emit_arg_or_default(c, m, i, -1, out);
2455+ else emit_arg_rooted(c, m, i, -1, out);
24272456 }
24282457 return;
24292458 }
@@ -2549,9 +2578,9 @@
25492578 int post_j = i - m->rest_idx - 1; /* 0-based index in posts */
25502579 int argv_idx = pos_argc - m->npost_rest + post_j;
25512580 if (argv && argv_idx >= 0 && argv_idx < pos_argc)
2552- emit_arg_or_default(c, m, i, argv[argv_idx], out);
2581+ emit_arg_rooted(c, m, i, argv[argv_idx], out);
25532582 else
2554- emit_arg_or_default(c, m, i, -1, out);
2583+ emit_arg_rooted(c, m, i, -1, out);
25552584 }
25562585 else if (splat_tmp >= 0 && i >= splat_idx) {
25572586 /* this param comes from the splatted array at offset (i - splat_idx) */
@@ -2561,7 +2590,7 @@
25612590 /* Check if this param has a keyword match (lookup by param name in kwh). */
25622591 int kv = kwh >= 0 ? kwh_lookup(nt, kwh, m->pnames[i]) : -1;
25632592 if (kv >= 0) {
2564- emit_arg_or_default(c, m, i, kv, out);
2593+ emit_arg_rooted(c, m, i, kv, out);
25652594 }
25662595 else if (ds_hash_tmp >= 0 && m->pnames[i]) {
25672596 /* Double-splat: extract param by name from the pre-eval'd hash. */
@@ -2619,7 +2648,7 @@
26192648 buf_printf(out, "_t%d", krhash);
26202649 }
26212650 else if (i < pos_argc) {
2622- emit_arg_or_default(c, m, i, argv[i], out);
2651+ emit_arg_rooted(c, m, i, argv[i], out);
26232652 }
26242653 else {
26252654 /* No positional arg and no keyword match. If the param is hash-typed
@@ -2630,7 +2659,7 @@
26302659 LocalVar *p = scope_local(m, m->pnames[i]);
26312660 TyKind pt = p ? p->type : TY_INT;
26322661 int use_kwh = (kwh >= 0 && ty_is_hash(pt));
2633- emit_arg_or_default(c, m, i, use_kwh ? kwh : -1, out);
2662+ emit_arg_rooted(c, m, i, use_kwh ? kwh : -1, out);
26342663 }
26352664 }
26362665 }