Commit 3091bd3b
3091bd3b9dcdMerged
Ryan Seys <[email protected]> · 6 months ago · 7 files · +6 added · ~1 modified
Description
fix(analyze): bound fixed-point loops and const-path recursion
Two fixed-point loops in spinel_analyze.rb -- infer_lambda_param_types
and scan_bigint_propagate -- ran while changed == 1 with no
iteration cap, inconsistent with the established iter < N idiom at
infer_param_array_type_from_body, infer_all_returns, and
compute_live_cls_methods. Both update logically-monotonic state
(lambda flag, bigint flag), so a healthy run converges in O(chain-
depth) iterations; an oscillation indicates a real bug in the helper
(param_used_as_lambda / propagate_bigint_node returning
non-deterministic answers), not "needs more iterations". A silent
cap would hide such bugs and emit subtly miscompiled output.
Both loops now snapshot a state signature each iteration and bail
loudly when the signature repeats (oscillation detected) or when
256 iterations elapse (runaway -- never expected on monotonic
state). The signature check only runs when changed == 1 at the
end of the iteration, so legitimate convergence (where iter N
verifies "nothing flipped" and exits with the same signature as
iter N-1) does not false-positive.
The recursive const-path walkers const_ref_flat_name and
resolve_const_ref_name gain a default depth = 32 parameter and
exit(1) when exhausted. The AST is acyclic so the walk always
terminates, but a pathological 1000-deep A::B::... input would
blow the C stack instead of giving a clear error.
Three stress tests exercise the new guards on realistic inputs that must converge well below the caps: bigint propagation across an 8-deep dependency chain, 6-level ConstantPath nesting, and lambda- type inference across a 5-method call chain. None trips the new guards.
Details
- Commit
- 3091bd3b9dcd
- Parent
- 992c82eada39
spinel_analyze.rbmodifiedView file
# ::C -> C # M::C -> M_C # A::B::C -> A_B_C def const_ref_flat_name(nid) def const_ref_flat_name(nid, depth = 32) if nid < 0 return "" end if depth <= 0 $stderr.puts "Error: const_ref_flat_name exceeded depth 32 -- pathological ConstantPath nesting" exit(1) end t = @nd_type[nid] if t == "ConstantReadNode" return @nd_name[nid] if parent < 0 return leaf end base = const_ref_flat_name(parent) base = const_ref_flat_name(parent, depth - 1) if base == "" return "" end name end def resolve_const_ref_name(nid) def resolve_const_ref_name(nid, depth = 32) if nid < 0 return "" end if depth <= 0 $stderr.puts "Error: resolve_const_ref_name exceeded depth 32 -- pathological ConstantPath nesting" exit(1) end t = @nd_type[nid] if t == "ConstantReadNode" return resolve_const_read_name(@nd_name[nid]) if parent < 0 return leaf end base = resolve_const_ref_name(parent) base = resolve_const_ref_name(parent, depth - 1) if base == "" return "" end # Second pass: scan method bodies for parameters used as lambda receivers # or passed to functions that expect lambda args (transitive closure) changed = 1 iter = 0 sig_hist = "".split(",") while changed == 1 iter = iter + 1 if iter > 256 $stderr.puts "Error: infer_lambda_param_types did not converge after 256 iterations" exit(1) end changed = 0 mi = 0 while mi < @meth_names.length end mi = mi + 1 end # Oscillation guard: only run when this iteration claims progress # (changed == 1, so the loop will iterate again). A repeated # @meth_param_types signature under changed=1 means the lambda # flag is flip-flopping -- a bug in param_used_as_lambda, not a # "needs more iterations" situation. Fail loudly. if changed == 1 sig = @meth_param_types.join("|") sh = 0 while sh < sig_hist.length if sig_hist[sh] == sig $stderr.puts "Error: infer_lambda_param_types oscillating (iter=" + iter.to_s + ", cycle len=" + (iter - sh - 1).to_s + ")" exit(1) end sh = sh + 1 end sig_hist.push(sig) end end end def scan_bigint_propagate(stmts, names, types) changed = 1 iter = 0 sig_hist = "".split(",") while changed == 1 iter = iter + 1 if iter > 256 $stderr.puts "Error: scan_bigint_propagate did not converge after 256 iterations" exit(1) end changed = 0 stmts.each { |sid| changed = propagate_bigint_node(sid, names, types, changed) } # Oscillation guard: only run when this iteration claims progress # (changed == 1, so the loop will iterate again). A repeated `types` # signature under changed=1 means propagate_bigint_node reported a # change it didn't actually make, or a cycle is in flight. Use `|` # as the field separator because type tokens can contain commas # (e.g. `tuple:int,int`) -- a comma join would alias distinct # length-N and length-(N+1) states. if changed == 1 sig = types.join("|") sh = 0 while sh < sig_hist.length if sig_hist[sh] == sig $stderr.puts "Error: scan_bigint_propagate oscillating (iter=" + iter.to_s + ", cycle len=" + (iter - sh - 1).to_s + ")" exit(1) end sh = sh + 1 end sig_hist.push(sig) end end endtest/bigint_propagation_chain.rbaddedView file
Loading diff…
test/bigint_propagation_chain.rb.expectedaddedView file
Loading diff…
test/const_path_deep_nesting.rbaddedView file
Loading diff…
test/const_path_deep_nesting.rb.expectedaddedView file
Loading diff…
test/lambda_inference_chain.rbaddedView file
Loading diff…
test/lambda_inference_chain.rb.expectedaddedView file
Loading diff…