Skip to content

Commit c797010

Browse files
author
bors-servo
authored
Auto merge of rust-lang#1035 - fitzgen:csmith-tweaks, r=pepyakin
Csmith tweaks r? @e00E or @emilio or @pepyakin whoever wants to get here first ;)
2 parents 9113b7b + ba2db57 commit c797010

File tree

2 files changed

+87
-23
lines changed

2 files changed

+87
-23
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ ir.png
1111

1212
# Output of the --dump-preprocessed-input flag.
1313
__bindgen.*
14+
15+
# Generated by C-Smith
16+
csmith-fuzzing/platform.info

csmith-fuzzing/driver.py

+84-23
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,94 @@
1-
from subprocess import run, DEVNULL, PIPE
1+
import os, sys
2+
from subprocess import run, SubprocessError, DEVNULL, PIPE
3+
from tempfile import NamedTemporaryFile
24

35
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+
]
1012

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])
1218

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")
1851

1952
iterations = 0
2053
while True:
2154
print("\rIteration: {}".format(iterations), end="", flush=True)
2255

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+
3388
iterations += 1
89+
90+
if __name__ == "__main__":
91+
try:
92+
main()
93+
except KeyboardInterrupt:
94+
exit()

0 commit comments

Comments
 (0)