| 1 | # spinel-reduce: delta-debug (ddmin) a degrading program down to a minimal
|
| 2 | # input that still reproduces a chosen failure. Re-runs spinel only.
|
| 3 | #
|
| 4 | # Usage: spinel-reduce [--oracle build|unsupported|unresolved]
|
| 5 | # [--oracle-cmd 'CMD {}'] [--shrink-ints] [-o OUT] app.rb
|
| 6 | #
|
| 7 | # --oracle <leg> built-in interestingness (default: build = spinel fails)
|
| 8 | # --oracle-cmd CMD custom test: "{}" is replaced by the candidate file;
|
| 9 | # 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")
|
| 14 | #
|
| 15 | # Tip: flatten multi-file programs first (spinel-flatten) so reduce has one
|
| 16 | # input to shrink.
|
| 17 |
|
| 18 | require_relative "tool_common"
|
| 19 |
|
| 20 | # Is the candidate (an array of source lines) still interesting?
|
| 21 | def interesting(lines, tmp, sp, oracle, ocmd)
|
| 22 | File.write(tmp, lines.join("\n") + "\n")
|
| 23 | if ocmd.length > 0
|
| 24 | sh(ocmd.gsub("{}", tmp))
|
| 25 | return $sh_status == 0
|
| 26 | end
|
| 27 | if oracle == "build"
|
| 28 | sh(sp + " " + tmp + " -o " + tmp + ".bin")
|
| 29 | return $sh_status != 0
|
| 30 | end
|
| 31 | needle = "unsupported "
|
| 32 | needle = "warning: unresolved" if oracle == "unresolved"
|
| 33 | out = sh("SPINEL_WARN_UNRESOLVED=1 " + sp + " " + tmp + " -c -o " + tmp + ".c")
|
| 34 | out.include?(needle)
|
| 35 | end
|
| 36 |
|
| 37 | # Classic ddmin: shrink `lines` while `interesting` holds, increasing
|
| 38 | # granularity when no single chunk-complement reproduces.
|
| 39 | def ddmin(lines, tmp, sp, oracle, ocmd)
|
| 40 | n = 2
|
| 41 | while lines.length >= 2
|
| 42 | size = lines.length / n
|
| 43 | size = 1 if size < 1
|
| 44 | start = 0
|
| 45 | reduced = false
|
| 46 | while start < lines.length
|
| 47 | stop = start + size
|
| 48 | comp = []
|
| 49 | k = 0
|
| 50 | while k < lines.length
|
| 51 | comp.push(lines[k]) if k < start || k >= stop
|
| 52 | k = k + 1
|
| 53 | end
|
| 54 | if comp.length > 0 && comp.length < lines.length && interesting(comp, tmp, sp, oracle, ocmd)
|
| 55 | lines = comp
|
| 56 | n = n - 1
|
| 57 | n = 2 if n < 2
|
| 58 | reduced = true
|
| 59 | start = 0
|
| 60 | else
|
| 61 | start = start + size
|
| 62 | end
|
| 63 | end
|
| 64 | if !reduced
|
| 65 | break if n >= lines.length
|
| 66 | n = n * 2
|
| 67 | n = lines.length if n > lines.length
|
| 68 | end
|
| 69 | end
|
| 70 | lines
|
| 71 | end
|
| 72 |
|
| 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 |
|
| 164 | def main
|
| 165 | oracle = "build"
|
| 166 | ocmd = ""
|
| 167 | shrink = false
|
| 168 | outfile = ""
|
| 169 | src = ""
|
| 170 | i = 0
|
| 171 | while i < ARGV.length
|
| 172 | a = ARGV[i]
|
| 173 | if a == "--oracle"
|
| 174 | i = i + 1
|
| 175 | oracle = ARGV[i] if i < ARGV.length
|
| 176 | elsif a == "--oracle-cmd"
|
| 177 | i = i + 1
|
| 178 | ocmd = ARGV[i] if i < ARGV.length
|
| 179 | elsif a == "--shrink-ints"
|
| 180 | shrink = true
|
| 181 | elsif a == "-o"
|
| 182 | i = i + 1
|
| 183 | outfile = ARGV[i] if i < ARGV.length
|
| 184 | elsif a == "-h" || a == "--help"
|
| 185 | puts "usage: spinel-reduce [--oracle build|unsupported|unresolved] [--oracle-cmd 'CMD {}'] [--shrink-ints] [-o OUT] app.rb"
|
| 186 | exit(0)
|
| 187 | else
|
| 188 | src = a
|
| 189 | end
|
| 190 | i = i + 1
|
| 191 | end
|
| 192 | die("usage: spinel-reduce [options] app.rb", 2) if src.length == 0
|
| 193 | die("spinel-reduce: no such file: " + src, 2) if !File.exist?(src)
|
| 194 |
|
| 195 | sp = find_spinel
|
| 196 | tmp = tmp_path("reduce", src, ".rb")
|
| 197 | lines = []
|
| 198 | File.read(src).split("\n").each { |l| lines.push(l) }
|
| 199 |
|
| 200 | if !interesting(lines, tmp, sp, oracle, ocmd)
|
| 201 | die("spinel-reduce: the input is not interesting under this oracle (nothing to reduce).", 1)
|
| 202 | end
|
| 203 |
|
| 204 | $stderr.puts "spinel-reduce: " + lines.length.to_s + " lines, oracle=" + (ocmd.length > 0 ? "cmd" : oracle)
|
| 205 | 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
|
| 216 | text = mini.join("\n") + "\n"
|
| 217 |
|
| 218 | if outfile.length > 0
|
| 219 | File.write(outfile, text)
|
| 220 | $stderr.puts "spinel-reduce: wrote " + outfile
|
| 221 | else
|
| 222 | print text
|
| 223 | end
|
| 224 | exit(0)
|
| 225 | end
|
| 226 |
|
| 227 | main
|