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

@@ -971,10 +971,14 @@
971971 # ::C -> C
972972 # M::C -> M_C
973973 # A::B::C -> A_B_C
974- def const_ref_flat_name(nid)
974+ def const_ref_flat_name(nid, depth = 32)
975975 if nid < 0
976976 return ""
977977 end
978+ if depth <= 0
979+ $stderr.puts "Error: const_ref_flat_name exceeded depth 32 -- pathological ConstantPath nesting"
980+ exit(1)
981+ end
978982 t = @nd_type[nid]
979983 if t == "ConstantReadNode"
980984 return @nd_name[nid]
@@ -985,7 +989,7 @@
985989 if parent < 0
986990 return leaf
987991 end
988- base = const_ref_flat_name(parent)
992+ base = const_ref_flat_name(parent, depth - 1)
989993 if base == ""
990994 return ""
991995 end
@@ -1109,10 +1113,14 @@
11091113 name
11101114 end
11111115
1112- def resolve_const_ref_name(nid)
1116+ def resolve_const_ref_name(nid, depth = 32)
11131117 if nid < 0
11141118 return ""
11151119 end
1120+ if depth <= 0
1121+ $stderr.puts "Error: resolve_const_ref_name exceeded depth 32 -- pathological ConstantPath nesting"
1122+ exit(1)
1123+ end
11161124 t = @nd_type[nid]
11171125 if t == "ConstantReadNode"
11181126 return resolve_const_read_name(@nd_name[nid])
@@ -1123,7 +1131,7 @@
11231131 if parent < 0
11241132 return leaf
11251133 end
1126- base = resolve_const_ref_name(parent)
1134+ base = resolve_const_ref_name(parent, depth - 1)
11271135 if base == ""
11281136 return ""
11291137 end
@@ -14699,7 +14707,14 @@
1469914707 # Second pass: scan method bodies for parameters used as lambda receivers
1470014708 # or passed to functions that expect lambda args (transitive closure)
1470114709 changed = 1
14710+ iter = 0
14711+ sig_hist = "".split(",")
1470214712 while changed == 1
14713+ iter = iter + 1
14714+ if iter > 256
14715+ $stderr.puts "Error: infer_lambda_param_types did not converge after 256 iterations"
14716+ exit(1)
14717+ end
1470314718 changed = 0
1470414719 mi = 0
1470514720 while mi < @meth_names.length
@@ -14725,6 +14740,23 @@
1472514740 end
1472614741 mi = mi + 1
1472714742 end
14743+ # Oscillation guard: only run when this iteration claims progress
14744+ # (changed == 1, so the loop will iterate again). A repeated
14745+ # @meth_param_types signature under changed=1 means the lambda
14746+ # flag is flip-flopping -- a bug in param_used_as_lambda, not a
14747+ # "needs more iterations" situation. Fail loudly.
14748+ if changed == 1
14749+ sig = @meth_param_types.join("|")
14750+ sh = 0
14751+ while sh < sig_hist.length
14752+ if sig_hist[sh] == sig
14753+ $stderr.puts "Error: infer_lambda_param_types oscillating (iter=" + iter.to_s + ", cycle len=" + (iter - sh - 1).to_s + ")"
14754+ exit(1)
14755+ end
14756+ sh = sh + 1
14757+ end
14758+ sig_hist.push(sig)
14759+ end
1472814760 end
1472914761 end
1473014762
@@ -18151,11 +18183,37 @@
1815118183
1815218184 def scan_bigint_propagate(stmts, names, types)
1815318185 changed = 1
18186+ iter = 0
18187+ sig_hist = "".split(",")
1815418188 while changed == 1
18189+ iter = iter + 1
18190+ if iter > 256
18191+ $stderr.puts "Error: scan_bigint_propagate did not converge after 256 iterations"
18192+ exit(1)
18193+ end
1815518194 changed = 0
1815618195 stmts.each { |sid|
1815718196 changed = propagate_bigint_node(sid, names, types, changed)
1815818197 }
18198+ # Oscillation guard: only run when this iteration claims progress
18199+ # (changed == 1, so the loop will iterate again). A repeated `types`
18200+ # signature under changed=1 means propagate_bigint_node reported a
18201+ # change it didn't actually make, or a cycle is in flight. Use `|`
18202+ # as the field separator because type tokens can contain commas
18203+ # (e.g. `tuple:int,int`) -- a comma join would alias distinct
18204+ # length-N and length-(N+1) states.
18205+ if changed == 1
18206+ sig = types.join("|")
18207+ sh = 0
18208+ while sh < sig_hist.length
18209+ if sig_hist[sh] == sig
18210+ $stderr.puts "Error: scan_bigint_propagate oscillating (iter=" + iter.to_s + ", cycle len=" + (iter - sh - 1).to_s + ")"
18211+ exit(1)
18212+ end
18213+ sh = sh + 1
18214+ end
18215+ sig_hist.push(sig)
18216+ end
1815918217 end
1816018218 end
1816118219