-
Notifications
You must be signed in to change notification settings - Fork 746
Csmith tweaks #1035
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Csmith tweaks #1035
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,94 @@ | ||
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 cat(path, title=None): | ||
if not title: | ||
title = path | ||
print("-------------------- {} --------------------".format(title)) | ||
run(["cat", path]) | ||
|
||
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_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(" ".join(cmd), result.returncode)) | ||
cat(stdout.name, title="stdout") | ||
cat(stdout.name, title="stderr") | ||
return result | ||
|
||
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 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") | ||
|
||
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") | ||
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: | ||
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 | ||
|
||
if __name__ == "__main__": | ||
try: | ||
main() | ||
except KeyboardInterrupt: | ||
exit() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should use all of the
--with-derive
to improve coverage?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll file a follow up :)