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
end j = j + 1 end if has_gc_locals == 1 has_gc_params = (param_needs_gc_root_count(params) > 0) ? 1 : 0 if has_gc_locals == 1 || has_gc_params == 1 if @needs_gc == 1 && @in_gc_scope == 0 emit(" SP_GC_SAVE();") @in_gc_scope = 1 emit(" " + c_type(ltypes[j]) + " lv_" + lnames[j] + " = " + c_default_val(ltypes[j]) + ";") j = j + 1 end if has_gc_locals == 1 if @needs_gc == 1 if @needs_gc == 1 if has_gc_locals == 1 emit_gc_roots(lnames, ltypes) end if has_gc_params == 1 emit_param_gc_roots(params) end end end end j = j + 1 end if has_gc_locals == 1 has_gc_params = (param_needs_gc_root_count(params) > 0) ? 1 : 0 if has_gc_locals == 1 || has_gc_params == 1 if @needs_gc == 1 && @in_gc_scope == 0 emit(" SP_GC_SAVE();") @in_gc_scope = 1 emit(" " + vol + c_type(ltypes[j]) + " lv_" + lnames[j] + " = " + c_default_val(ltypes[j]) + ";") j = j + 1 end if has_gc_locals == 1 if @needs_gc == 1 if @needs_gc == 1 if has_gc_locals == 1 emit_gc_roots(lnames, ltypes) end if has_gc_params == 1 emit_param_gc_roots(params) end end end end end # Root heap-typed (string/object/array/poly/value-type) method # parameters at function entry, alongside the local roots. A param is # a live value the caller passed but that nothing else roots; if the # method allocates internally, a collection would free the object a # string/poly param points into (e.g. `sp_poly_to_s(lv_body)` reads a # freed String). Rooting the param keeps it reachable for every read # in the body, regardless of argument evaluation order. The params are # already declare_var'd by the prologue, so the type comes from the # scope. Issue #1068 (follow-up to #1057). def param_needs_gc_root_count(params) c = 0 j = 0 while j < params.length if gc_trace_kind(find_var_declared_type(params[j])) != "none" c = c + 1 end j = j + 1 end c end def emit_param_gc_roots(params) j = 0 while j < params.length pt = find_var_declared_type(params[j]) if gc_trace_kind(pt) != "none" emit_gc_root_for_expr("lv_" + params[j], pt) end j = j + 1 end end # Returns 1 if `nid` is an explicit literal value (not a placeholder or # inferred fallback). Used by scan_locals to distinguish a genuine int # write like `x = 1` from a defaulted "int" from an unresolved read.test/gc_root_heap_param.rbaddedView file
Loading diff…
test/gc_root_heap_param.rb.expectedaddedView file
Loading diff…