| 1 | <html> |
| 2 | <head> |
| 3 | <title> Minimizing Interesting Files with Delta </title> |
| 4 | </head> |
| 5 | |
| 6 | <body> |
| 7 | <h1> Minimizing Interesting Files with Delta </h1> |
| 8 | |
| 9 | <p>Daniel S. Wilkerson |
| 10 | |
| 11 | <p>Delta assists you in minimizing "interesting" files, subject to a test |
| 12 | of their interestingness. A common such situation is when attempting |
| 13 | to isolate a small failure-inducing substring of a large input that |
| 14 | causes your program to demonstrate a bug. Our implementation is based |
| 15 | on the algorithm found here: http://www.st.cs.uni-sb.de/dd/ |
| 16 | |
| 17 | <p>Three tools are provided: delta, multidelta, and topformflat. Delta |
| 18 | is the script which does the minimizing. Topformflat is a utility |
| 19 | flattens languages with balanced delimiters, such as most common |
| 20 | programming languages, so that all nesting below a specified depth is |
| 21 | one one line. Multidelta is a wrapper that runs delta for you but on |
| 22 | multiple files using delta and topformflat underneath to change |
| 23 | nesting granularity before it is fed into delta. Its UI is arguably |
| 24 | probably more what you actually want, since for example it allows you |
| 25 | to run on multiple input files. |
| 26 | |
| 27 | <h2>Delta</h2> |
| 28 | |
| 29 | <p>Delta is an implementation of the delta debugging algorithm: |
| 30 | http://www.st.cs.uni-sb.de/dd/. In short, you supply delta with <ol> |
| 31 | |
| 32 | <li>a test shell script which decides if its input file is |
| 33 | "interesting" and |
| 34 | |
| 35 | <li>an initial interesting input file. |
| 36 | |
| 37 | </ol> |
| 38 | |
| 39 | <p>Delta uses heuristics to find a sub-file of your input file that is |
| 40 | still "interesting" according to your test. |
| 41 | |
| 42 | <p>Usually "interesting" means a file causing a particular error as input |
| 43 | to a program. For debugging or bug-reporting purposes you would like |
| 44 | to find the smallest such file. An appropriate test here would run |
| 45 | the program on the file and grep for the error message, returning |
| 46 | success (0) if found, and failure (non-zero) otherwise. |
| 47 | |
| 48 | <p>Note that we use C programs as example input because delta was |
| 49 | developed for debugging language tools like compilers, so often the |
| 50 | input was some C program and it was interesting if it crashed _our_ |
| 51 | program, a compiler. |
| 52 | |
| 53 | <h2>An example</h2> |
| 54 | |
| 55 | <p>Here is a rather contrived example minimizing a C input file. In this |
| 56 | case "interesting" means that the file compiles and when run returns |
| 57 | false. This example is in delta/test/delta0. |
| 58 | |
| 59 | <p>The input file (below "in.c"): |
| 60 | |
| 61 | <pre> |
| 62 | #include <stdio.h> |
| 63 | int main(int argc, char **argv) { |
| 64 | int a = 0; |
| 65 | printf("Hello, World!\n"); |
| 66 | a++; |
| 67 | a--; |
| 68 | a+=2; |
| 69 | a++; |
| 70 | a--; |
| 71 | a++; |
| 72 | return a >= 3;} // Brace here prevents main from lacking a return. |
| 73 | </pre> |
| 74 | |
| 75 | <p>The script test that decides interestingness. It is probably best if |
| 76 | your test produces no output, so putting a &>/dev/null at the end of |
| 77 | your grep, as I have here, is recommended. |
| 78 | |
| 79 | <p>Note that delta passes the name of the file to your script, so you can |
| 80 | use it, as I have here; however when testing multiple files, perhaps |
| 81 | it is best to use the Multidelta style and just hard-code in the |
| 82 | filenames. Multidelta minimizes in-place, which makes passing in the |
| 83 | name unnecessary. |
| 84 | |
| 85 | <p>The test script (below "testit"): |
| 86 | |
| 87 | <pre> |
| 88 | #!/bin/sh |
| 89 | # -*-sh-*- |
| 90 | if gcc $1 &> cmp_out; then |
| 91 | if ! ./a.out &> run_out; then |
| 92 | exit 0; # Success. |
| 93 | fi |
| 94 | fi |
| 95 | exit 1; # Failure. |
| 96 | </pre> |
| 97 | |
| 98 | <P>Put both of these files into a new directory and make your test |
| 99 | executable, and run as follows: |
| 100 | |
| 101 | <pre> |
| 102 | delta -test=testit -quiet -cp_minimal=min.c < in.c |
| 103 | </pre> |
| 104 | |
| 105 | <p>When delta returns, you will notice a file "min.c" in the directory |
| 106 | which is smaller than "in.c" and yet still passes the |
| 107 | interestingness test. |
| 108 | |
| 109 | <p>You will also notice a tmp* directory (tmp0 if it is the first run) |
| 110 | that contains all the files that were found to be interesting during |
| 111 | the search. Delta runs quickly on this small example, but for longer |
| 112 | runs you might want to tail -f tmp0/log in another window to watch the |
| 113 | interesting events of the progression of the search. If you remove |
| 114 | the -quiet flag, you will get a very detailed transcript to standard |
| 115 | out of the search process, probably more than you want to know, but |
| 116 | fun to watch. |
| 117 | |
| 118 | <p>There are other features and modes to delta. Below is the usage |
| 119 | message which you can get by typing: delta -help . (Usage messages |
| 120 | reproduced here omit the initial header line and distribution version |
| 121 | number.) |
| 122 | |
| 123 | <pre> |
| 124 | usage: bin/delta [options] start-file |
| 125 | |
| 126 | -test=<testscript> Specify the test script |
| 127 | -suffix=<suffix> Candidate filename suffix [.c] |
| 128 | -dump_input Dump input after reading |
| 129 | -cp_minimal=<filename> Copy the minimal successful test to the |
| 130 | current directory |
| 131 | -granularity=line Use lines as the granularity (default) |
| 132 | -granularity=top_form Use C top-level forms as the granularity |
| 133 | (currently only works with CIL output) |
| 134 | -log=<file> Log file for main events |
| 135 | -quiet Say nothing |
| 136 | -verbose Get more verbose output |
| 137 | -in_place Overwrite start-file with inputs |
| 138 | |
| 139 | -help Get help |
| 140 | |
| 141 | The test program accepts a single argument, the name of the candidate |
| 142 | file to test. It is run within a directory containing only that file, |
| 143 | and it can make temporary files/directories in that directory. It |
| 144 | should return zero for a candidate that exhibits the desired property, |
| 145 | and nonzero for one that does not. |
| 146 | |
| 147 | Example test program (delta will retain a line containing "foo"): |
| 148 | #!/bin/sh |
| 149 | grep 'foo' <"$1" >/dev/null |
| 150 | </pre> |
| 151 | |
| 152 | <h2>Granularity</h2> |
| 153 | |
| 154 | <p>Delta has a notion of the granularity of the file: the smallest atomic |
| 155 | elements of which the file is seen as a sequence. The default is the |
| 156 | line granularity: in this mode, delta will attempt to delete entire |
| 157 | lines, but will never try deleting a smaller element than that. You |
| 158 | can filter a program through topformflat to produce a file where the |
| 159 | line-granularity only goes to a specified nesting depth (if your file |
| 160 | is in a nested language). Multidelta does this for you. |
| 161 | |
| 162 | <p>In general, use semantic granularity appropriate to the input |
| 163 | language. Delta has a much easier time searching the space if the |
| 164 | atomic chunks in the granularity match up with semantically-atomic |
| 165 | chunks in your input language. That is, if you are minimizing C |
| 166 | files, start with the C top-level form granularity. Only after that |
| 167 | minimize with a finer level of granularity. This allows delta to try |
| 168 | removing entire function definitions, rather than removing single |
| 169 | lines which is much less likely to produce a meaningful, and therefore |
| 170 | interesting, C file. If you have some other kind of file, you should |
| 171 | probably write the equivalent of topformflat for it. If your hack is |
| 172 | general and reusable, please send it to us. |
| 173 | |
| 174 | <h2>1-minimality</h2> |
| 175 | |
| 176 | <p>An "interesting" sequence is 1-minimal iff it does not remain |
| 177 | interesting if any one element is removed. Delta is guaranteed to |
| 178 | find a 1-minimal file from your input file, where it is considered a |
| 179 | sequence of elements at the granularity you specified. |
| 180 | |
| 181 | <p>Note that the space of interesting files is usually quite complex, and |
| 182 | delta is doing only a rather simple local walk through it. Therefore |
| 183 | it is easy for delta to get stuck someplace that is locally 1-minimal, |
| 184 | but globally non-minimum. One often therefore gets better results if |
| 185 | delta is run again on its output, with gradually finer granularity, if |
| 186 | that is an option. |
| 187 | |
| 188 | <h2>Topformflat</h2> |
| 189 | |
| 190 | <p>Topformflat is a filter written by Scott McPeak that will print its |
| 191 | input with the line granularity matching a specified nesting depth. |
| 192 | It is useful for making the granularity of an input file start of very |
| 193 | coarse for the first runs of delta and then gradually increasing it. |
| 194 | Multidelta will run topformflat for you with a given nesting depth if |
| 195 | you ask for it on the command line. Here is the usage message which |
| 196 | you can get by typing: topformflat . |
| 197 | |
| 198 | <pre> |
| 199 | usage: bin/topformflat [threshold] <input.c >output.c |
| 200 | The threshold (default: 0) specifies at what nesting level |
| 201 | of braces will line breaks be allowed (or inserted). By |
| 202 | starting with 0, you get all top-level forms, one per line |
| 203 | (roughly). Increasing the threshold leads to finer-grained |
| 204 | structure on each line. The intent is to use the delta |
| 205 | minimizer on each level of granularity. |
| 206 | </pre> |
| 207 | |
| 208 | <h2>Multidelta</h2> |
| 209 | |
| 210 | <p>Multidelta was written as a wrapper for delta by Scott McPeak. It |
| 211 | allows you to easily minimize collections of files by minimizing each |
| 212 | one individually for you using delta underneath. It also |
| 213 | automatically pre-process the input with the C pre-processor (cpp) and |
| 214 | then filters it through topformflat for you before running delta. |
| 215 | (Thus, it is rather oriented towards C programs as input.) Below is |
| 216 | the usage message which you can get by typing: multidelta . |
| 217 | |
| 218 | <pre> |
| 219 | usage: bin/multidelta [options] test-script file [file [...]] |
| 220 | </pre> |
| 221 | |
| 222 | <p>The collection of input files is minimized as much as possible, such |
| 223 | that the test-script continues to return true. |
| 224 | |
| 225 | <p>When the script is run with a source file as an argument, it should do |
| 226 | any single-file integrity checks on that file. Then, it should |
| 227 | proceed to check the entire build, on the assumption that all other |
| 228 | files have already passed their integrity checks. |
| 229 | |
| 230 | <p>When the script is run with no arguments, it should check integrity of |
| 231 | all files and then check the build. |
| 232 | |
| 233 | <p>So that the script does not need to have the list of input files |
| 234 | hard-coded in, the environment variable "multidelta_all_files" is |
| 235 | always set to a space-separated list of the filenames no matter how |
| 236 | the script is run. |
| 237 | |
| 238 | <pre> |
| 239 | Options: |
| 240 | -level=n When topformflattening, flatten to level n [$level]. |
| 241 | -u Undo the last invocation, by copying the *.bak files |
| 242 | onto the original copies. |
| 243 | -cpp Before flattening run through the cpp preprocessor. |
| 244 | </pre> |
| 245 | |
| 246 | <h2>Stopping</h2> |
| 247 | |
| 248 | <p>A new feature of delta and multidelta is that it is possible to |
| 249 | stop it! |
| 250 | |
| 251 | <p>Delta now both distinguishes between a signal and a non-zero result |
| 252 | code from the tests script, halting altogether in the case of a |
| 253 | signal. Multidelta has always stopped if delta returns anything other |
| 254 | than zero and still does. The consequence of this is that you can |
| 255 | just type control-C at the terminal and everything should stop |
| 256 | immediately. However, not sure what state the partially minimized |
| 257 | file will be left in; for a safe way to stop, see the next paragraph. |
| 258 | Thanks to Scott McPeak for pointing out how easy this was to do. |
| 259 | |
| 260 | <p>Delta has always stopped if a file DELTA-STOP appears in the |
| 261 | current directory, however for some unexplained reason, I only checked |
| 262 | for this when it increased the granularity of the search. Now it |
| 263 | checks at every run of the external script. If DELTA-STOP comes into |
| 264 | existence, delta and multidelta should stop before obliterating the |
| 265 | current version of the file to be tested. |
| 266 | |
| 267 | <h2>Notes</h2> |
| 268 | |
| 269 | <p>At some point probably delta should be made into a Perl module that |
| 270 | could be used by any Perl program and any remaining desirable features |
| 271 | of its UI that have not yet been subsumed by multidelta should be; But |
| 272 | it works for now. |
| 273 | |
| 274 | <p>Yes, I wrote a script to re-run delta lots of times, but it seems that |
| 275 | people prefer to do that manually. |
| 276 | |
| 277 | </body> |
| 278 | </html> |