From a0f6fbbc37b273162cc4aa9a97ce8af2dbd9575f Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Mon, 25 Sep 2017 13:09:18 -0700 Subject: C-Smith: use temp files instead of local files This allows us to have multiple processes generating C files with C-Smith and testing them on bindgen without stepping on each others' toes. --- csmith-fuzzing/driver.py | 79 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 56 insertions(+), 23 deletions(-) diff --git a/csmith-fuzzing/driver.py b/csmith-fuzzing/driver.py index ee42203d..fe3d7879 100644 --- a/csmith-fuzzing/driver.py +++ b/csmith-fuzzing/driver.py @@ -1,33 +1,66 @@ -from subprocess import run, DEVNULL, PIPE +import os, sys +from subprocess import run, SubprocessError, DEVNULL, PIPE +from tempfile import NamedTemporaryFile csmith_command = [ - "csmith", - "--no-checksum", - "--nomain", - "--max-block-size", "1", - "--max-block-depth", "1", - "--output", "generated.h"] + "csmith", + "--no-checksum", + "--nomain", + "--max-block-size", "1", + "--max-block-depth", "1", +] -bindgen_command = ["bindgen", "generated.h"] +def run_logged(cmd): + with NamedTemporaryFile() as stdout, NamedTemporaryFile() as stderr: + result = run(cmd, stdin=DEVNULL, stdout=stdout, stderr=stderr) + if result.returncode != 0: + print() + print("Error: {} exited with code {}".format(str(cmd), result.returncode)) + print("-------------------- stdout --------------------") + run(["cat", stdout.name]) + print("-------------------- stderr --------------------") + run(["cat", stderr.name]) + return result -if __name__ == "__main__": - print("Bindgen fuzzing with csmith.") - print( - "This script will write to generated.h, bindgen_stdout, bindgen_stderr and platform.info . " - "These files can be deleted after running.") +def run_bindgen(input, output): + return run_logged([ + "bindgen", + "--with-derive-partialeq", + "--with-derive-eq", + "-o", output.name, + input.name, + "--", + "-I", os.path.abspath(os.path.dirname(sys.argv[0])), + ]) + +def main(): + print("Fuzzing `bindgen` with C-Smith...\n") iterations = 0 while True: print("\rIteration: {}".format(iterations), end="", flush=True) - run(csmith_command, stdin=DEVNULL, stdout=DEVNULL, stderr=DEVNULL) - with open("bindgen_stdout", "wb") as stdout, open("bindgen_stdout", "wb") as stderr: - result = run(bindgen_command, stdin=DEVNULL, stdout=stdout, stderr=stderr) - if result.returncode != 0: - print() - print( - "Error: bindgen existed with non zero exit code {} when ran on generated.h . " - "You can find its output in bindgen_stoud and bindgen_stderr." - .format(result.returncode)) - exit() + input = NamedTemporaryFile(delete=False, prefix="input-", suffix=".h") + result = run_logged(csmith_command + ["-o", input.name]) + if result.returncode != 0: + exit(1) + + output = NamedTemporaryFile(delete=False, prefix="output-", suffix=".rs") + result = run_bindgen(input, output) + if result.returncode != 0: + print("-------------------- {} --------------------".format(input.name)) + run(["cat", input.name]) + print("-------------------- {} --------------------".format(output.name)) + run(["cat", output.name]) + exit(1) + + os.remove(input.name) + os.remove(output.name) + iterations += 1 + +if __name__ == "__main__": + try: + main() + except KeyboardInterrupt: + exit() -- cgit v1.2.3 From ba2db57c78df73cea85d227001d4b505b3a1fb9a Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Mon, 25 Sep 2017 13:30:04 -0700 Subject: C-Smith: compile bindings and execute layout tests This makes us fall over flat on our faces almost immediately... --- .gitignore | 3 +++ csmith-fuzzing/driver.py | 46 +++++++++++++++++++++++++++++++++++++--------- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index b2bfe5aa..77cea935 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,6 @@ ir.png # Output of the --dump-preprocessed-input flag. __bindgen.* + +# Generated by C-Smith +csmith-fuzzing/platform.info diff --git a/csmith-fuzzing/driver.py b/csmith-fuzzing/driver.py index fe3d7879..e2816813 100644 --- a/csmith-fuzzing/driver.py +++ b/csmith-fuzzing/driver.py @@ -10,16 +10,20 @@ csmith_command = [ "--max-block-depth", "1", ] +def cat(path, title=None): + if not title: + title = path + print("-------------------- {} --------------------".format(title)) + run(["cat", path]) + def run_logged(cmd): with NamedTemporaryFile() as stdout, NamedTemporaryFile() as stderr: result = run(cmd, stdin=DEVNULL, stdout=stdout, stderr=stderr) if result.returncode != 0: print() - print("Error: {} exited with code {}".format(str(cmd), result.returncode)) - print("-------------------- stdout --------------------") - run(["cat", stdout.name]) - print("-------------------- stderr --------------------") - run(["cat", stderr.name]) + print("Error: '{}' exited with code {}".format(" ".join(cmd), result.returncode)) + cat(stdout.name, title="stdout") + cat(stdout.name, title="stderr") return result def run_bindgen(input, output): @@ -33,6 +37,15 @@ def run_bindgen(input, output): "-I", os.path.abspath(os.path.dirname(sys.argv[0])), ]) +def run_rustc(output, test): + return run_logged([ + "rustc", + "--crate-type", "lib", + "--test", + output.name, + "-o", test.name, + ]) + def main(): print("Fuzzing `bindgen` with C-Smith...\n") @@ -41,21 +54,36 @@ def main(): print("\rIteration: {}".format(iterations), end="", flush=True) input = NamedTemporaryFile(delete=False, prefix="input-", suffix=".h") + input.close() result = run_logged(csmith_command + ["-o", input.name]) if result.returncode != 0: exit(1) output = NamedTemporaryFile(delete=False, prefix="output-", suffix=".rs") + output.close() result = run_bindgen(input, output) if result.returncode != 0: - print("-------------------- {} --------------------".format(input.name)) - run(["cat", input.name]) - print("-------------------- {} --------------------".format(output.name)) - run(["cat", output.name]) + cat(input.name) + cat(output.name) + exit(1) + + test = NamedTemporaryFile(delete=False, prefix="test-") + test.close() + result = run_rustc(output, test) + if result.returncode != 0: + cat(input.name) + cat(output.name) + exit(1) + + result = run_logged([test.name]) + if result.returncode != 0: + cat(input.name) + cat(output.name) exit(1) os.remove(input.name) os.remove(output.name) + os.remove(test.name) iterations += 1 -- cgit v1.2.3