Commit 8f8d8fc9

8f8d8fc987deMerged

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

Description

Root call-argument temporaries across sibling-arg evaluation (fixes #1057)

A call's earlier argument temporaries were not GC-rooted across the evaluation of its later arguments. When a sibling argument's evaluation allocates and triggers a collection, a fresh-heap String/object arg that was already built is freed, and the call (or sprintf) then reads a dangling pointer -- use-after-free. C leaves argument evaluation order unspecified, so on gcc/x86-64 (right-to-left) the last-listed fresh arg is the most exposed.

Three argument-emission paths missed this (the untyped compile_call_args and constructor paths already rooted):

  • compile_typed_call_args (instance / class method calls)
  • compile_call_args_with_defaults (top-level method calls)
  • compile_interpolated (sp_sprintf for string interpolation -- the roundhouse layout crash: 7 %s helper-call temps)

Each now hoists fresh heap-typed argument temporaries into SP_GC_ROOT'd locals when two or more of the call's arguments may allocate, mirroring the existing root_constructor_args / compile_gc_safe_args mechanism. Hoisting into ordered temps also forces deterministic left-to-right evaluation, so it is correct regardless of the platform's native order. The gate is >= 2 may-allocate args: a lone allocating arg has no sibling to free it, so the common call stays zero-cost (verified: no optcarrot fps regression).

Follow-up: the poly-receiver dispatch inline-arg builders (the arg_strs sites) are not yet covered; tracked for a later pass.

Fixes #1057

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

Details

Commit
8f8d8fc987de
Parent
2b5da1b0fd56

spinel_codegen.rbmodifiedView file

@@ -15498,6 +15498,10 @@
1549815498 end
1549915499 fmt = ""
1550015500 arg_exprs = "".split(",", -1)
15501+ # Source node id per emitted arg, kept aligned with arg_exprs, so the
15502+ # sibling-arg GC rooting below can tell which interpolated values are
15503+ # fresh allocations (#1057).
15504+ arg_inner_ids = []
1550115505 parts.each { |pid|
1550215506 if @nd_type[pid] == "StringNode"
1550315507 fmt = fmt + escape_c_format(@nd_content[pid])
@@ -15515,6 +15519,7 @@
1551515519 k_es2 = k_es2 + 1
1551615520 end
1551715521 inner = stmts[stmts.length - 1]
15522+ arg_inner_ids.push(inner)
1551815523 it = infer_type(inner)
1551915524 if it == "int"
1552015525 fmt = fmt + "%lld"
@@ -15627,6 +15632,42 @@
1562715632 end
1562815633 end
1562915634 }
15635+ # Root fresh heap (`%s` const char*) interpolation values across the
15636+ # evaluation of sibling values when two or more may allocate. Without
15637+ # this, e.g. a 7-`%s` layout sprintf frees an earlier-built helper
15638+ # string when a later one collects, and vsnprintf walks a dangling
15639+ # pointer (#1057). `%lld` args are integers (the "(long long)" prefix)
15640+ # and need no rooting; a lone allocating value has no sibling to free
15641+ # it, so the gate keeps the common case zero-cost.
15642+ gc_n_ip = 0
15643+ jj = 0
15644+ while jj < arg_exprs.length
15645+ rootable_cnt = 1
15646+ if arg_exprs[jj].start_with?("(long long)")
15647+ rootable_cnt = 0
15648+ end
15649+ if rootable_cnt == 1 && jj < arg_inner_ids.length && arg_inner_ids[jj] >= 0 && expr_may_gc(arg_inner_ids[jj]) == 1
15650+ gc_n_ip = gc_n_ip + 1
15651+ end
15652+ jj = jj + 1
15653+ end
15654+ if gc_n_ip >= 2
15655+ @needs_gc = 1
15656+ jj = 0
15657+ while jj < arg_exprs.length
15658+ rootable_ip = 1
15659+ if arg_exprs[jj].start_with?("(long long)")
15660+ rootable_ip = 0
15661+ end
15662+ if rootable_ip == 1 && jj < arg_inner_ids.length && arg_inner_ids[jj] >= 0 && expr_may_gc(arg_inner_ids[jj]) == 1
15663+ t_ip = new_temp
15664+ emit(" const char *" + t_ip + " = " + arg_exprs[jj] + ";")
15665+ emit(" SP_GC_ROOT(" + t_ip + ");")
15666+ arg_exprs[jj] = t_ip
15667+ end
15668+ jj = jj + 1
15669+ end
15670+ end
1563015671 result = "sp_sprintf(\"" + fmt + "\""
1563115672 j = 0
1563215673 while j < arg_exprs.length
@@ -30616,6 +30657,31 @@
3061630657 kwrest_param_idx = -1
3061730658 end
3061830659
30660+ # Root fresh heap arg temporaries across sibling-arg evaluation when
30661+ # two or more args may allocate (the #1057 use-after-free). A lone
30662+ # allocating arg has no sibling to free it, so the common case stays
30663+ # zero-cost. root_constructor_arg_if_needed hoists only the may-gc
30664+ # heap-typed args into rooted temps; literals/locals pass through.
30665+ gc_count_cd = 0
30666+ gi_cd = 0
30667+ while gi_cd < positional_ids.length
30668+ if @nd_type[positional_ids[gi_cd]] != "SplatNode" && expr_may_gc(positional_ids[gi_cd]) == 1
30669+ gc_count_cd = gc_count_cd + 1
30670+ end
30671+ gi_cd = gi_cd + 1
30672+ end
30673+ gi_cd = 0
30674+ while gi_cd < kw_arg_ids.length
30675+ if kw_arg_ids[gi_cd] >= 0 && expr_may_gc(kw_arg_ids[gi_cd]) == 1
30676+ gc_count_cd = gc_count_cd + 1
30677+ end
30678+ gi_cd = gi_cd + 1
30679+ end
30680+ do_root_cd = 0
30681+ if gc_count_cd >= 2
30682+ do_root_cd = 1
30683+ end
30684+
3061930685 result = ""
3062030686 k = 0
3062130687 while k < pnames.length
@@ -30669,7 +30735,11 @@
3066930735 # (e.g. `model:` after call-site widening folds
3067030736 # Article + Comment into poly) is exactly the path
3067130737 # that flows non-string objs through here.
30672- result = result + box_expr_to_poly(kw_arg_ids[ki])
30738+ kw_pv_cd = box_expr_to_poly(kw_arg_ids[ki])
30739+ if do_root_cd == 1
30740+ kw_pv_cd = root_constructor_arg_if_needed(kw_arg_ids[ki], kw_pv_cd, "poly", 0)
30741+ end
30742+ result = result + kw_pv_cd
3067330743 else
3067430744 # promote-mode coerce: kw value's static type vs
3067530745 # param's declared type may need int <-> bigint.
@@ -30682,10 +30752,17 @@
3068230752 @needs_bigint = 1
3068330753 kw_val_pft = "sp_bigint_to_int((sp_Bigint *)" + kw_val_pft + ")"
3068430754 end
30755+ if do_root_cd == 1
30756+ kw_val_pft = root_constructor_arg_if_needed(kw_arg_ids[ki], kw_val_pft, ptypes[k], 0)
30757+ end
3068530758 result = result + kw_val_pft
3068630759 end
3068730760 else
30688- result = result + kw_vals[ki]
30761+ kw_nv_cd = kw_vals[ki]
30762+ if do_root_cd == 1
30763+ kw_nv_cd = root_constructor_arg_if_needed(kw_arg_ids[ki], kw_nv_cd, infer_type(kw_arg_ids[ki]), 0)
30764+ end
30765+ result = result + kw_nv_cd
3068930766 end
3069030767 end
3069130768 ki = ki + 1
@@ -30829,7 +30906,11 @@
3082930906 if k < positional_ids.length
3083030907 if k < ptypes.length
3083130908 if ptypes[k] == "poly"
30832- result = result + box_expr_to_poly(positional_ids[k])
30909+ av_cd = box_expr_to_poly(positional_ids[k])
30910+ if do_root_cd == 1
30911+ av_cd = root_constructor_arg_if_needed(positional_ids[k], av_cd, "poly", 0)
30912+ end
30913+ result = result + av_cd
3083330914 k = k + 1
3083430915 next
3083530916 end
@@ -30926,9 +31007,17 @@
3092631007 # the caller is more general than the callee) is unboxed via
3092731008 # the matching union field. Mirrors the #438 IntStrHash-key
3092831009 # unbox and extends it to user-method args.
30929- result = result + cast_away_volatile_arg(positional_ids[k], compile_expr_for_expected_type(positional_ids[k], ptypes[k]))
31010+ av_cd = cast_away_volatile_arg(positional_ids[k], compile_expr_for_expected_type(positional_ids[k], ptypes[k]))
31011+ if do_root_cd == 1
31012+ av_cd = root_constructor_arg_if_needed(positional_ids[k], av_cd, ptypes[k], 0)
31013+ end
31014+ result = result + av_cd
3093031015 else
30931- result = result + cast_away_volatile_arg(positional_ids[k], compile_expr(positional_ids[k]))
31016+ av_cd = cast_away_volatile_arg(positional_ids[k], compile_expr(positional_ids[k]))
31017+ if do_root_cd == 1
31018+ av_cd = root_constructor_arg_if_needed(positional_ids[k], av_cd, infer_type(positional_ids[k]), 0)
31019+ end
31020+ result = result + av_cd
3093231021 end
3093331022 else
3093431023 # Use default value
@@ -31680,22 +31769,42 @@
3168031769 if total == 0
3168131770 return ""
3168231771 end
31683- later_arg_may_gc = []
31684- if root_constructor_args == 1
31685- gc_expr_ids = []
31686- gk = 0
31687- while gk < total
31688- gc_expr_id = -1
31689- if gk < arg_ids.length
31690- gc_expr_id = arg_ids[gk]
31691- else
31692- if gk < defaults.length
31693- gc_expr_id = defaults[gk].to_i
31694- end
31772+ # Root fresh heap argument temporaries across sibling-arg evaluation.
31773+ # A call like `f(fresh_str, allocating())` can free `fresh_str` when
31774+ # `allocating()` triggers a collection before the call consumes it
31775+ # (use-after-free). Engage the per-arg rooting for constructors
31776+ # (always) and for any call with >= 2 args that may allocate -- a
31777+ # lone allocating arg has no sibling to free, so the common case
31778+ # stays zero-cost. The gate mirrors the untyped compile_call_args /
31779+ # compile_gc_safe_args precedent.
31780+ gc_expr_ids = []
31781+ gk = 0
31782+ while gk < total
31783+ gc_expr_id = -1
31784+ if gk < arg_ids.length
31785+ gc_expr_id = arg_ids[gk]
31786+ else
31787+ if gk < defaults.length
31788+ gc_expr_id = defaults[gk].to_i
3169531789 end
31696- gc_expr_ids.push(gc_expr_id)
31697- gk = gk + 1
3169831790 end
31791+ gc_expr_ids.push(gc_expr_id)
31792+ gk = gk + 1
31793+ end
31794+ gc_count_ta = 0
31795+ gk = 0
31796+ while gk < total
31797+ if gc_expr_ids[gk] >= 0 && expr_may_gc(gc_expr_ids[gk]) == 1
31798+ gc_count_ta = gc_count_ta + 1
31799+ end
31800+ gk = gk + 1
31801+ end
31802+ do_root = root_constructor_args
31803+ if gc_count_ta >= 2
31804+ do_root = 1
31805+ end
31806+ later_arg_may_gc = []
31807+ if do_root == 1
3169931808 seen_gc = 0
3170031809 gk = total - 1
3170131810 while gk >= 0
@@ -31735,7 +31844,7 @@
3173531844 pt_base = base_type(pt)
3173631845 if pt_base == "poly" || pt_base == "string" || is_array_type(pt_base) == 1 || (at == "poly" && is_obj_type(pt_base) == 1) || (at == "poly" && is_hash_type(pt_base) == 1) || (pt_base == "bigint" && at != "bigint") || (at == "bigint" && pt_base != "bigint")
3173731846 aexpr = compile_expr_for_expected_type(arg_ids[k], pt)
31738- if root_constructor_args == 1
31847+ if do_root == 1
3173931848 aexpr = root_constructor_arg_if_needed(arg_ids[k], aexpr, pt, later_arg_may_gc[k])
3174031849 end
3174131850 result = result + aexpr
@@ -31772,7 +31881,7 @@
3177231881 aexpr = compile_expr(arg_ids[k])
3177331882 pt = at
3177431883 end
31775- if root_constructor_args == 1
31884+ if do_root == 1
3177631885 aexpr = root_constructor_arg_if_needed(arg_ids[k], aexpr, pt, later_arg_may_gc[k])
3177731886 end
3177831887 result = result + aexpr
@@ -31814,13 +31923,13 @@
3181431923 end
3181531924 if k < ptypes.length
3181631925 aexpr = compile_expr_for_expected_type(def_id, ptypes[k])
31817- if root_constructor_args == 1
31926+ if do_root == 1
3181831927 aexpr = root_constructor_arg_if_needed(def_id, aexpr, ptypes[k], later_arg_may_gc[k])
3181931928 end
3182031929 result = result + aexpr
3182131930 else
3182231931 aexpr = compile_expr(def_id)
31823- if root_constructor_args == 1
31932+ if do_root == 1
3182431933 aexpr = root_constructor_arg_if_needed(def_id, aexpr, infer_type(def_id), later_arg_may_gc[k])
3182531934 end
3182631935 result = result + aexpr