Commit f19990f8

f19990f805efMerged

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

Description

Root heap-typed method parameters across internal GC (fixes #1068)

The #1057 fix roots a call's argument temporaries that themselves allocate, but a %s arg can be a live pointer into a GC object without allocating -- sp_poly_to_s(poly_param) returns the existing String's char*. With the param itself unrooted, a sibling arg's allocation (or any collection inside the method) frees the object the param points into, and the read dangles -> use-after-free.

Argument-level rooting can't fix this: in wrap(body) -> "#{gc_then}-#{body}", gc_then is source-first, so its collection runs before sp_poly_to_s(lv_body) is even evaluated -- the String is already dead. The fix is to root the parameter itself at function entry, the way poly/heap locals are already rooted (9d5bfc8). Then the param is reachable for the whole body regardless of argument order.

Root every heap-typed parameter (gc_trace_kind != "none": string, object, array, poly, value-type) in both method-prologue emitters (declare_method_locals and declare_method_locals_from), gated on the existing @needs_gc so leaf functions that never allocate pay nothing. No optcarrot regression (checksum 59662, fps steady); self-host bootstrap fixpoint holds.

Not covered: a %s arg whose value points into a temporary poly (not a param/local), e.g. "#{poly_call()}#{alloc}" -- there is no param/local to root. Distinct from this issue; tracked separately.

Fixes #1068

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

Details

Commit
f19990f805ef
Parent
01f2ec72af04

spinel_codegen.rbmodifiedView file

@@ -12138,7 +12138,8 @@
1213812138 end
1213912139 j = j + 1
1214012140 end
12141- if has_gc_locals == 1
12141+ has_gc_params = (param_needs_gc_root_count(params) > 0) ? 1 : 0
12142+ if has_gc_locals == 1 || has_gc_params == 1
1214212143 if @needs_gc == 1 && @in_gc_scope == 0
1214312144 emit(" SP_GC_SAVE();")
1214412145 @in_gc_scope = 1
@@ -12149,10 +12150,13 @@
1214912150 emit(" " + c_type(ltypes[j]) + " lv_" + lnames[j] + " = " + c_default_val(ltypes[j]) + ";")
1215012151 j = j + 1
1215112152 end
12152- if has_gc_locals == 1
12153- if @needs_gc == 1
12153+ if @needs_gc == 1
12154+ if has_gc_locals == 1
1215412155 emit_gc_roots(lnames, ltypes)
1215512156 end
12157+ if has_gc_params == 1
12158+ emit_param_gc_roots(params)
12159+ end
1215612160 end
1215712161 end
1215812162
@@ -12423,7 +12427,8 @@
1242312427 end
1242412428 j = j + 1
1242512429 end
12426- if has_gc_locals == 1
12430+ has_gc_params = (param_needs_gc_root_count(params) > 0) ? 1 : 0
12431+ if has_gc_locals == 1 || has_gc_params == 1
1242712432 if @needs_gc == 1 && @in_gc_scope == 0
1242812433 emit(" SP_GC_SAVE();")
1242912434 @in_gc_scope = 1
@@ -12461,10 +12466,13 @@
1246112466 emit(" " + vol + c_type(ltypes[j]) + " lv_" + lnames[j] + " = " + c_default_val(ltypes[j]) + ";")
1246212467 j = j + 1
1246312468 end
12464- if has_gc_locals == 1
12465- if @needs_gc == 1
12469+ if @needs_gc == 1
12470+ if has_gc_locals == 1
1246612471 emit_gc_roots(lnames, ltypes)
1246712472 end
12473+ if has_gc_params == 1
12474+ emit_param_gc_roots(params)
12475+ end
1246812476 end
1246912477 end
1247012478
@@ -12516,6 +12524,38 @@
1251612524 end
1251712525 end
1251812526
12527+ # Root heap-typed (string/object/array/poly/value-type) method
12528+ # parameters at function entry, alongside the local roots. A param is
12529+ # a live value the caller passed but that nothing else roots; if the
12530+ # method allocates internally, a collection would free the object a
12531+ # string/poly param points into (e.g. `sp_poly_to_s(lv_body)` reads a
12532+ # freed String). Rooting the param keeps it reachable for every read
12533+ # in the body, regardless of argument evaluation order. The params are
12534+ # already declare_var'd by the prologue, so the type comes from the
12535+ # scope. Issue #1068 (follow-up to #1057).
12536+ def param_needs_gc_root_count(params)
12537+ c = 0
12538+ j = 0
12539+ while j < params.length
12540+ if gc_trace_kind(find_var_declared_type(params[j])) != "none"
12541+ c = c + 1
12542+ end
12543+ j = j + 1
12544+ end
12545+ c
12546+ end
12547+
12548+ def emit_param_gc_roots(params)
12549+ j = 0
12550+ while j < params.length
12551+ pt = find_var_declared_type(params[j])
12552+ if gc_trace_kind(pt) != "none"
12553+ emit_gc_root_for_expr("lv_" + params[j], pt)
12554+ end
12555+ j = j + 1
12556+ end
12557+ end
12558+
1251912559 # Returns 1 if `nid` is an explicit literal value (not a placeholder or
1252012560 # inferred fallback). Used by scan_locals to distinguish a genuine int
1252112561 # write like `x = 1` from a defaulted "int" from an unresolved read.