Skip to content

Commit d5f8722

Browse files
author
e
committed
csmith fuzzing
1 parent f23b118 commit d5f8722

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

csmith-fuzzing/README

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Fuzz bindgen with `csmith` https://github.com/csmith-project/csmith .
2+
3+
Run with `python3 driver.py`. It will run until until it encounters an error in `bindgen`.
4+
5+
Requires `python3`, `csmith` and `bindgen` to be in `$PATH`.
6+
7+
csmith is run with `--no-checksum --nomain --max-block-size 1 --max-block-depth 1` which disables the `main` function and makes function bodies as simple as possible as bindgen does not care about them but they cannot be completely disabled in csmith. Run `csmith --help` to see what exactly those options do.

csmith-fuzzing/csmith.h

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Type definitions from csmith's csmith_minimal.h included in csmith.h .
2+
// Since other header contents are not needed we put them in here
3+
// so the other original header is not needed anymore.
4+
5+
#define int8_t signed char
6+
#define uint8_t unsigned char
7+
8+
#define int16_t short
9+
#define uint16_t unsigned short
10+
11+
#define int32_t int
12+
#define uint32_t unsigned
13+
14+
#define int64_t long long
15+
#define uint64_t unsigned long long

csmith-fuzzing/driver.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from subprocess import run, DEVNULL, PIPE
2+
3+
csmith_command = [
4+
"csmith",
5+
"--no-checksum",
6+
"--nomain",
7+
"--max-block-size", "1",
8+
"--max-block-depth", "1",
9+
"--output", "generated.h"]
10+
11+
bindgen_command = ["bindgen", "generated.h"]
12+
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.")
18+
19+
iterations = 0
20+
while True:
21+
print("\rIteration: {}".format(iterations), end="", flush=True)
22+
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()
33+
iterations += 1

0 commit comments

Comments
 (0)