|
1 |
| -from subprocess import run, DEVNULL, PIPE |
| 1 | +import os, sys |
| 2 | +from subprocess import run, SubprocessError, DEVNULL, PIPE |
| 3 | +from tempfile import NamedTemporaryFile |
2 | 4 |
|
3 | 5 | csmith_command = [
|
4 |
| - "csmith", |
5 |
| - "--no-checksum", |
6 |
| - "--nomain", |
7 |
| - "--max-block-size", "1", |
8 |
| - "--max-block-depth", "1", |
9 |
| - "--output", "generated.h"] |
| 6 | + "csmith", |
| 7 | + "--no-checksum", |
| 8 | + "--nomain", |
| 9 | + "--max-block-size", "1", |
| 10 | + "--max-block-depth", "1", |
| 11 | +] |
10 | 12 |
|
11 |
| -bindgen_command = ["bindgen", "generated.h"] |
| 13 | +def cat(path, title=None): |
| 14 | + if not title: |
| 15 | + title = path |
| 16 | + print("-------------------- {} --------------------".format(title)) |
| 17 | + run(["cat", path]) |
12 | 18 |
|
13 |
| -if __name__ == "__main__": |
14 |
| - print("Bindgen fuzzing with csmith.") |
15 |
| - print( |
16 |
| - "This script will write to generated.h, bindgen_stdout, bindgen_stderr and platform.info . " |
17 |
| - "These files can be deleted after running.") |
| 19 | +def run_logged(cmd): |
| 20 | + with NamedTemporaryFile() as stdout, NamedTemporaryFile() as stderr: |
| 21 | + result = run(cmd, stdin=DEVNULL, stdout=stdout, stderr=stderr) |
| 22 | + if result.returncode != 0: |
| 23 | + print() |
| 24 | + print("Error: '{}' exited with code {}".format(" ".join(cmd), result.returncode)) |
| 25 | + cat(stdout.name, title="stdout") |
| 26 | + cat(stdout.name, title="stderr") |
| 27 | + return result |
| 28 | + |
| 29 | +def run_bindgen(input, output): |
| 30 | + return run_logged([ |
| 31 | + "bindgen", |
| 32 | + "--with-derive-partialeq", |
| 33 | + "--with-derive-eq", |
| 34 | + "-o", output.name, |
| 35 | + input.name, |
| 36 | + "--", |
| 37 | + "-I", os.path.abspath(os.path.dirname(sys.argv[0])), |
| 38 | + ]) |
| 39 | + |
| 40 | +def run_rustc(output, test): |
| 41 | + return run_logged([ |
| 42 | + "rustc", |
| 43 | + "--crate-type", "lib", |
| 44 | + "--test", |
| 45 | + output.name, |
| 46 | + "-o", test.name, |
| 47 | + ]) |
| 48 | + |
| 49 | +def main(): |
| 50 | + print("Fuzzing `bindgen` with C-Smith...\n") |
18 | 51 |
|
19 | 52 | iterations = 0
|
20 | 53 | while True:
|
21 | 54 | print("\rIteration: {}".format(iterations), end="", flush=True)
|
22 | 55 |
|
23 |
| - run(csmith_command, stdin=DEVNULL, stdout=DEVNULL, stderr=DEVNULL) |
24 |
| - with open("bindgen_stdout", "wb") as stdout, open("bindgen_stdout", "wb") as stderr: |
25 |
| - result = run(bindgen_command, stdin=DEVNULL, stdout=stdout, stderr=stderr) |
26 |
| - if result.returncode != 0: |
27 |
| - print() |
28 |
| - print( |
29 |
| - "Error: bindgen existed with non zero exit code {} when ran on generated.h . " |
30 |
| - "You can find its output in bindgen_stoud and bindgen_stderr." |
31 |
| - .format(result.returncode)) |
32 |
| - exit() |
| 56 | + input = NamedTemporaryFile(delete=False, prefix="input-", suffix=".h") |
| 57 | + input.close() |
| 58 | + result = run_logged(csmith_command + ["-o", input.name]) |
| 59 | + if result.returncode != 0: |
| 60 | + exit(1) |
| 61 | + |
| 62 | + output = NamedTemporaryFile(delete=False, prefix="output-", suffix=".rs") |
| 63 | + output.close() |
| 64 | + result = run_bindgen(input, output) |
| 65 | + if result.returncode != 0: |
| 66 | + cat(input.name) |
| 67 | + cat(output.name) |
| 68 | + exit(1) |
| 69 | + |
| 70 | + test = NamedTemporaryFile(delete=False, prefix="test-") |
| 71 | + test.close() |
| 72 | + result = run_rustc(output, test) |
| 73 | + if result.returncode != 0: |
| 74 | + cat(input.name) |
| 75 | + cat(output.name) |
| 76 | + exit(1) |
| 77 | + |
| 78 | + result = run_logged([test.name]) |
| 79 | + if result.returncode != 0: |
| 80 | + cat(input.name) |
| 81 | + cat(output.name) |
| 82 | + exit(1) |
| 83 | + |
| 84 | + os.remove(input.name) |
| 85 | + os.remove(output.name) |
| 86 | + os.remove(test.name) |
| 87 | + |
33 | 88 | iterations += 1
|
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + try: |
| 92 | + main() |
| 93 | + except KeyboardInterrupt: |
| 94 | + exit() |
0 commit comments