Commit 274bec5c
274bec5cc630Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
Merge pull request #1482 from OriPekelman/reduce-shrink-ints
spinel-reduce: add --shrink-ints parameter search
Details
- Commit
- 274bec5cc630
- Parent
- d572a6d7b1fc
- Merge parent
- 3f9a2651fe22
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.rb5+
# [--oracle-cmd 'CMD {}'] [--shrink-ints] [-o OUT] app.rb66
#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 010+
# --shrink-ints after code reduction, binary-search each surviving integer11+
# literal down to the smallest value that still reproduces,12+
# and report the threshold (isolates a size-triggered bug's13+
# numeric boundary, e.g. "fails once the dim crosses ~512")1014
#1115
# Tip: flatten multi-file programs first (spinel-flatten) so reduce has one1216
# input to shrink.@@ -66,9 +70,101 @@
6670
lines6771
end6872
73+
# Is `c` a single decimal digit?74+
def is_digit(c)75+
c >= "0" && c <= "9"76+
end77+
78+
# Does the char left of offset `j` glue a digit there into an identifier or a79+
# 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 == 082+
p = line[j - 1, 1]83+
is_digit(p) || p == "_" || p == "." ||84+
(p >= "a" && p <= "z") || (p >= "A" && p <= "Z")85+
end86+
87+
# Build a candidate with the [b, e) span of lines[idx] replaced by `v`, and test88+
# 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 = 094+
while k < lines.length95+
cand.push(k == idx ? repl : lines[k])96+
k = k + 197+
end98+
interesting(cand, tmp, sp, oracle, ocmd)99+
end100+
101+
# Parameter search: for each surviving integer literal, binary-search the102+
# smallest value that still reproduces the failure (assuming the trigger is103+
# monotone in the value -- what a size threshold is) and report the boundary. A104+
# literal whose value is irrelevant (even 0 still triggers) is zeroed and flagged105+
# "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 = 0109+
while idx < lines.length110+
line = lines[idx]111+
# Collect [start, stop) spans of integer literals.112+
starts = []113+
stops = []114+
j = 0115+
while j < line.length116+
if is_digit(line[j, 1]) && !glued_left(line, j)117+
k = j118+
while k < line.length && (is_digit(line[k, 1]) || line[k, 1] == "_")119+
k = k + 1120+
end121+
starts.push(j)122+
stops.push(k)123+
j = k124+
else125+
j = j + 1126+
end127+
end128+
# Right-to-left, so editing one span never shifts an unprocessed one's offset.129+
si = starts.length - 1130+
while si >= 0131+
b = starts[si]132+
e = stops[si]133+
orig = line[b, e - b].delete("_").to_i134+
if orig != 0135+
label = base_name(src) + ":" + (idx + 1).to_s136+
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] = line139+
report.push(label + ": " + orig.to_s + " -> 0 (not size-dependent)")140+
else141+
lo = 1142+
hi = orig143+
while lo < hi144+
mid = (lo + hi) / 2145+
if try_int(lines, idx, b, e, mid, tmp, sp, oracle, ocmd)146+
hi = mid147+
else148+
lo = mid + 1149+
end150+
end151+
line = line[0, b] + hi.to_s + line[e, line.length - e]152+
lines[idx] = line153+
report.push(label + ": " + orig.to_s + " -> " + hi.to_s +154+
" (threshold; " + (hi - 1).to_s + " does not trigger)")155+
end156+
end157+
si = si - 1158+
end159+
idx = idx + 1160+
end161+
report162+
end163+
69164
def main70165
oracle = "build"71166
ocmd = ""167+
shrink = false72168
outfile = ""73169
src = ""74170
i = 0@@ -80,11 +176,13 @@
80176
elsif a == "--oracle-cmd"81177
i = i + 182178
ocmd = ARGV[i] if i < ARGV.length179+
elsif a == "--shrink-ints"180+
shrink = true83181
elsif a == "-o"84182
i = i + 185183
outfile = ARGV[i] if i < ARGV.length86184
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
else90188
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 shrink209+
$stderr.puts "spinel-reduce: shrinking integer literals (parameter search)"210+
report = shrink_ints(mini, src, tmp, sp, oracle, ocmd)211+
if report.length > 0212+
$stderr.puts "spinel-reduce: size thresholds:"213+
report.each { |r| $stderr.puts " " + r }214+
end215+
end108216
text = mini.join("\n") + "\n"109-
$stderr.puts "spinel-reduce: reduced to " + mini.length.to_s + " lines"110217
111218
if outfile.length > 0112219
File.write(outfile, text)