|
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 run_logged(cmd): |
| 14 | + with NamedTemporaryFile() as stdout, NamedTemporaryFile() as stderr: |
| 15 | + result = run(cmd, stdin=DEVNULL, stdout=stdout, stderr=stderr) |
| 16 | + if result.returncode != 0: |
| 17 | + print() |
| 18 | + print("Error: {} exited with code {}".format(str(cmd), result.returncode)) |
| 19 | + print("-------------------- stdout --------------------") |
| 20 | + run(["cat", stdout.name]) |
| 21 | + print("-------------------- stderr --------------------") |
| 22 | + run(["cat", stderr.name]) |
| 23 | + return result |
12 | 24 |
|
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.") |
| 25 | +def run_bindgen(input, output): |
| 26 | + return run_logged([ |
| 27 | + "bindgen", |
| 28 | + "--with-derive-partialeq", |
| 29 | + "--with-derive-eq", |
| 30 | + "-o", output.name, |
| 31 | + input.name, |
| 32 | + "--", |
| 33 | + "-I", os.path.abspath(os.path.dirname(sys.argv[0])), |
| 34 | + ]) |
| 35 | + |
| 36 | +def main(): |
| 37 | + print("Fuzzing `bindgen` with C-Smith...\n") |
18 | 38 |
|
19 | 39 | iterations = 0
|
20 | 40 | while True:
|
21 | 41 | print("\rIteration: {}".format(iterations), end="", flush=True)
|
22 | 42 |
|
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() |
| 43 | + input = NamedTemporaryFile(delete=False, prefix="input-", suffix=".h") |
| 44 | + result = run_logged(csmith_command + ["-o", input.name]) |
| 45 | + if result.returncode != 0: |
| 46 | + exit(1) |
| 47 | + |
| 48 | + output = NamedTemporaryFile(delete=False, prefix="output-", suffix=".rs") |
| 49 | + result = run_bindgen(input, output) |
| 50 | + if result.returncode != 0: |
| 51 | + print("-------------------- {} --------------------".format(input.name)) |
| 52 | + run(["cat", input.name]) |
| 53 | + print("-------------------- {} --------------------".format(output.name)) |
| 54 | + run(["cat", output.name]) |
| 55 | + exit(1) |
| 56 | + |
| 57 | + os.remove(input.name) |
| 58 | + os.remove(output.name) |
| 59 | + |
33 | 60 | iterations += 1
|
| 61 | + |
| 62 | +if __name__ == "__main__": |
| 63 | + try: |
| 64 | + main() |
| 65 | + except KeyboardInterrupt: |
| 66 | + exit() |
0 commit comments