Commit 3f9a2651

3f9a2651fe22Merged

Ori Pekelman <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

spinel-reduce: add --shrink-ints parameter search

Code reduction (ddmin) isolates which code triggers a failure; it can't isolate the numeric boundary of a size-triggered bug ("miscompiles once the dim crosses ~512") -- ddmin removes a literal whole or not at all. --shrink-ints adds a post-reduction pass: for each surviving integer literal, binary-search the smallest value that still reproduces (assuming monotone-in-the-value, which a size threshold is) and report the boundary. A literal whose value is irrelevant (even 0 still reproduces) is zeroed and flagged "not size-dependent".

Regex-free (matches the other tools' style): a manual left-to-right scan finds integer-literal spans, edited right-to-left so an edit never shifts an unprocessed span's offset. Works with any oracle (built-in or --oracle-cmd).

Example: a program reduced to DIM = 600 under a ">= 512" oracle reports 600 -> 512 (threshold; 511 does not trigger) and writes DIM = 512.

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

Details

Commit
3f9a2651fe22
Parent
4058359a8053

tools/reduce.rbmodifiedView file

@@ -2,11 +2,15 @@
22 # input that still reproduces a chosen failure. Re-runs spinel only.
33 #
44 # Usage: spinel-reduce [--oracle build|unsupported|unresolved]
5-# [--oracle-cmd 'CMD {}'] [-o OUT] app.rb
5+# [--oracle-cmd 'CMD {}'] [--shrink-ints] [-o OUT] app.rb
66 #
77 # --oracle <leg> built-in interestingness (default: build = spinel fails)
88 # --oracle-cmd CMD custom test: "{}" is replaced by the candidate file;
99 # the candidate is interesting when CMD exits 0
10+# --shrink-ints after code reduction, binary-search each surviving integer
11+# literal down to the smallest value that still reproduces,
12+# and report the threshold (isolates a size-triggered bug's
13+# numeric boundary, e.g. "fails once the dim crosses ~512")
1014 #
1115 # Tip: flatten multi-file programs first (spinel-flatten) so reduce has one
1216 # input to shrink.
@@ -66,9 +70,101 @@
6670 lines
6771 end
6872
73+# Is `c` a single decimal digit?
74+def is_digit(c)
75+ c >= "0" && c <= "9"
76+end
77+
78+# Does the char left of offset `j` glue a digit there into an identifier or a
79+# larger number (so a digit at `j` is not the start of a fresh integer literal)?
80+def glued_left(line, j)
81+ return false if j == 0
82+ p = line[j - 1, 1]
83+ is_digit(p) || p == "_" || p == "." ||
84+ (p >= "a" && p <= "z") || (p >= "A" && p <= "Z")
85+end
86+
87+# Build a candidate with the [b, e) span of lines[idx] replaced by `v`, and test
88+# whether it is still interesting.
89+def try_int(lines, idx, b, e, v, tmp, sp, oracle, ocmd)
90+ line = lines[idx]
91+ repl = line[0, b] + v.to_s + line[e, line.length - e]
92+ cand = []
93+ k = 0
94+ while k < lines.length
95+ cand.push(k == idx ? repl : lines[k])
96+ k = k + 1
97+ end
98+ interesting(cand, tmp, sp, oracle, ocmd)
99+end
100+
101+# Parameter search: for each surviving integer literal, binary-search the
102+# smallest value that still reproduces the failure (assuming the trigger is
103+# monotone in the value -- what a size threshold is) and report the boundary. A
104+# literal whose value is irrelevant (even 0 still triggers) is zeroed and flagged
105+# "not size-dependent". Edits `lines` in place; returns the report lines.
106+def shrink_ints(lines, src, tmp, sp, oracle, ocmd)
107+ report = []
108+ idx = 0
109+ while idx < lines.length
110+ line = lines[idx]
111+ # Collect [start, stop) spans of integer literals.
112+ starts = []
113+ stops = []
114+ j = 0
115+ while j < line.length
116+ if is_digit(line[j, 1]) && !glued_left(line, j)
117+ k = j
118+ while k < line.length && (is_digit(line[k, 1]) || line[k, 1] == "_")
119+ k = k + 1
120+ end
121+ starts.push(j)
122+ stops.push(k)
123+ j = k
124+ else
125+ j = j + 1
126+ end
127+ end
128+ # Right-to-left, so editing one span never shifts an unprocessed one's offset.
129+ si = starts.length - 1
130+ while si >= 0
131+ b = starts[si]
132+ e = stops[si]
133+ orig = line[b, e - b].delete("_").to_i
134+ if orig != 0
135+ label = base_name(src) + ":" + (idx + 1).to_s
136+ if try_int(lines, idx, b, e, 0, tmp, sp, oracle, ocmd)
137+ line = line[0, b] + "0" + line[e, line.length - e]
138+ lines[idx] = line
139+ report.push(label + ": " + orig.to_s + " -> 0 (not size-dependent)")
140+ else
141+ lo = 1
142+ hi = orig
143+ while lo < hi
144+ mid = (lo + hi) / 2
145+ if try_int(lines, idx, b, e, mid, tmp, sp, oracle, ocmd)
146+ hi = mid
147+ else
148+ lo = mid + 1
149+ end
150+ end
151+ line = line[0, b] + hi.to_s + line[e, line.length - e]
152+ lines[idx] = line
153+ report.push(label + ": " + orig.to_s + " -> " + hi.to_s +
154+ " (threshold; " + (hi - 1).to_s + " does not trigger)")
155+ end
156+ end
157+ si = si - 1
158+ end
159+ idx = idx + 1
160+ end
161+ report
162+end
163+
69164 def main
70165 oracle = "build"
71166 ocmd = ""
167+ shrink = false
72168 outfile = ""
73169 src = ""
74170 i = 0
@@ -80,11 +176,13 @@
80176 elsif a == "--oracle-cmd"
81177 i = i + 1
82178 ocmd = ARGV[i] if i < ARGV.length
179+ elsif a == "--shrink-ints"
180+ shrink = true
83181 elsif a == "-o"
84182 i = i + 1
85183 outfile = ARGV[i] if i < ARGV.length
86184 elsif a == "-h" || a == "--help"
87- puts "usage: spinel-reduce [--oracle build|unsupported|unresolved] [--oracle-cmd 'CMD {}'] [-o OUT] app.rb"
185+ puts "usage: spinel-reduce [--oracle build|unsupported|unresolved] [--oracle-cmd 'CMD {}'] [--shrink-ints] [-o OUT] app.rb"
88186 exit(0)
89187 else
90188 src = a
@@ -105,8 +203,17 @@
105203
106204 $stderr.puts "spinel-reduce: " + lines.length.to_s + " lines, oracle=" + (ocmd.length > 0 ? "cmd" : oracle)
107205 mini = ddmin(lines, tmp, sp, oracle, ocmd)
206+ $stderr.puts "spinel-reduce: reduced to " + mini.length.to_s + " lines"
207+
208+ if shrink
209+ $stderr.puts "spinel-reduce: shrinking integer literals (parameter search)"
210+ report = shrink_ints(mini, src, tmp, sp, oracle, ocmd)
211+ if report.length > 0
212+ $stderr.puts "spinel-reduce: size thresholds:"
213+ report.each { |r| $stderr.puts " " + r }
214+ end
215+ end
108216 text = mini.join("\n") + "\n"
109- $stderr.puts "spinel-reduce: reduced to " + mini.length.to_s + " lines"
110217
111218 if outfile.length > 0
112219 File.write(outfile, text)