Skip to content

Commit f5b6e00

Browse files
author
bors-servo
authored
Auto merge of #1033 - e00E:csmith-fuzz, r=fitzgen
csmith fuzzing ref #969 An initial version of a script that fuzzes bindgen with csmith. I ran it for maybe 1000 iterations and it did not find something wrong. The programs generated by csmith are probably too simple type wise. Here is an example output of what csmith generates: ``` C /* --- Struct/Union Declarations --- */ union U2 { uint64_t f0; const signed f1 : 18; }; union U4 { const volatile signed f0 : 1; volatile int16_t f1; int32_t f2; int8_t * const f3; volatile int64_t f4; }; union U5 { const int8_t * f0; volatile int8_t f1; uint16_t f2; unsigned f3 : 22; }; /* --- GLOBAL VARIABLES --- */ static int8_t g_3[8] = {0x47L,0xE8L,0x47L,0x47L,0xE8L,0x47L,0x47L,0xE8L}; static int32_t g_25 = 0x3421AD7BL; static union U5 g_40 = {0};/* VOLATILE GLOBAL g_40 */ static int32_t g_43[4][3] = {{(-10L),(-10L),(-10L)},{(-10L),(-10L),(-10L)},{(-10L),(-10L),(-10L)},{(-10L),($static int32_t * volatile g_42 = &g_43[0][0];/* VOLATILE GLOBAL g_42 */ static int32_t * volatile g_50 = &g_43[2][0];/* VOLATILE GLOBAL g_50 */ static int32_t g_53 = (-9L); static union U4 g_57 = {0x9C113E7BL};/* VOLATILE GLOBAL g_57 */ /* --- FORWARD DECLARATIONS --- */ static union U4 func_1(void); static int16_t func_4(int32_t p_5); static int32_t func_6(int32_t p_7, union U2 p_8, int8_t * p_9); static int32_t func_10(uint32_t p_11, int8_t * p_12, int8_t * p_13); static int8_t * func_14(int32_t p_15, union U2 p_16); static union U2 func_28(const uint64_t p_29, int8_t * p_30, uint32_t p_31); static union U5 func_34(uint32_t p_35); ```
2 parents af2164d + d5f8722 commit f5b6e00

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)