Commit e47608ac

e47608acb9d6Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 7 files · +6 added · ~1 modified

Description

Merge pull request #535 from ryanseys/rs-bound-analyze-loops

fix(analyze): bound fixed-point loops and const-path recursion

Details

Commit
e47608acb9d6
Parent
585bee04368f
Merge parent
3091bd3b9dcd

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
@@ -1136,10 +1140,14 @@
11361140 name
11371141 end
11381142
1139- def resolve_const_ref_name(nid)
1143+ def resolve_const_ref_name(nid, depth = 32)
11401144 if nid < 0
11411145 return ""
11421146 end
1147+ if depth <= 0
1148+ $stderr.puts "Error: resolve_const_ref_name exceeded depth 32 -- pathological ConstantPath nesting"
1149+ exit(1)
1150+ end
11431151 t = @nd_type[nid]
11441152 if t == "ConstantReadNode"
11451153 return resolve_const_read_name(@nd_name[nid])
@@ -1150,7 +1158,7 @@
11501158 if parent < 0
11511159 return leaf
11521160 end
1153- base = resolve_const_ref_name(parent)
1161+ base = resolve_const_ref_name(parent, depth - 1)
11541162 if base == ""
11551163 return ""
11561164 end
@@ -14737,7 +14745,14 @@
1473714745 # Second pass: scan method bodies for parameters used as lambda receivers
1473814746 # or passed to functions that expect lambda args (transitive closure)
1473914747 changed = 1
14748+ iter = 0
14749+ sig_hist = "".split(",")
1474014750 while changed == 1
14751+ iter = iter + 1
14752+ if iter > 256
14753+ $stderr.puts "Error: infer_lambda_param_types did not converge after 256 iterations"
14754+ exit(1)
14755+ end
1474114756 changed = 0
1474214757 mi = 0
1474314758 while mi < @meth_names.length
@@ -14763,6 +14778,23 @@
1476314778 end
1476414779 mi = mi + 1
1476514780 end
14781+ # Oscillation guard: only run when this iteration claims progress
14782+ # (changed == 1, so the loop will iterate again). A repeated
14783+ # @meth_param_types signature under changed=1 means the lambda
14784+ # flag is flip-flopping -- a bug in param_used_as_lambda, not a
14785+ # "needs more iterations" situation. Fail loudly.
14786+ if changed == 1
14787+ sig = @meth_param_types.join("|")
14788+ sh = 0
14789+ while sh < sig_hist.length
14790+ if sig_hist[sh] == sig
14791+ $stderr.puts "Error: infer_lambda_param_types oscillating (iter=" + iter.to_s + ", cycle len=" + (iter - sh - 1).to_s + ")"
14792+ exit(1)
14793+ end
14794+ sh = sh + 1
14795+ end
14796+ sig_hist.push(sig)
14797+ end
1476614798 end
1476714799 end
1476814800
@@ -18189,11 +18221,37 @@
1818918221
1819018222 def scan_bigint_propagate(stmts, names, types)
1819118223 changed = 1
18224+ iter = 0
18225+ sig_hist = "".split(",")
1819218226 while changed == 1
18227+ iter = iter + 1
18228+ if iter > 256
18229+ $stderr.puts "Error: scan_bigint_propagate did not converge after 256 iterations"
18230+ exit(1)
18231+ end
1819318232 changed = 0
1819418233 stmts.each { |sid|
1819518234 changed = propagate_bigint_node(sid, names, types, changed)
1819618235 }
18236+ # Oscillation guard: only run when this iteration claims progress
18237+ # (changed == 1, so the loop will iterate again). A repeated `types`
18238+ # signature under changed=1 means propagate_bigint_node reported a
18239+ # change it didn't actually make, or a cycle is in flight. Use `|`
18240+ # as the field separator because type tokens can contain commas
18241+ # (e.g. `tuple:int,int`) -- a comma join would alias distinct
18242+ # length-N and length-(N+1) states.
18243+ if changed == 1
18244+ sig = types.join("|")
18245+ sh = 0
18246+ while sh < sig_hist.length
18247+ if sig_hist[sh] == sig
18248+ $stderr.puts "Error: scan_bigint_propagate oscillating (iter=" + iter.to_s + ", cycle len=" + (iter - sh - 1).to_s + ")"
18249+ exit(1)
18250+ end
18251+ sh = sh + 1
18252+ end
18253+ sig_hist.push(sig)
18254+ end
1819718255 end
1819818256 end
1819918257