Skip to content

Commit c7fe6b6

Browse files
author
bors-servo
authored
Auto merge of #1061 - fitzgen:add-csmith-instructions-to-contributing, r=pepyakin
Point to `csmith` instructions from CONTRIBUTING.md r? @pepyakin
2 parents 7bbfb44 + 29fff6a commit c7fe6b6

File tree

4 files changed

+76
-7
lines changed

4 files changed

+76
-7
lines changed

CONTRIBUTING.md

+9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ out to us in a GitHub issue, or stop by
1919
- [Testing a Single Header's Bindings Generation and Compiling its Bindings](#testing-a-single-headers-bindings-generation-and-compiling-its-bindings)
2020
- [Authoring New Tests](#authoring-new-tests)
2121
- [Test Expectations and `libclang` Versions](#test-expectations-and-libclang-versions)
22+
- [Fuzzing `bindgen` with `csmith`](#fuzzing-bindgen-with-csmith)
2223
- [Code Overview](#code-overview)
2324
- [Pull Requests and Code Reviews](#pull-requests-and-code-reviews)
2425
- [Generating Graphviz Dot Files](#generating-graphviz-dot-files)
@@ -193,6 +194,14 @@ Where `$VERSION` is one of:
193194

194195
depending on which version of `libclang` you have installed.
195196

197+
### Fuzzing `bindgen` with `csmith`
198+
199+
We <3 finding hidden bugs and the people who help us find them! One way to help
200+
uncover hidden bugs is by running `csmith` to generate random headers to test
201+
`bindgen` against.
202+
203+
See [./csmith-fuzzing/README.md](./csmith-fuzzing/README.md) for details.
204+
196205
## Code Overview
197206

198207
`bindgen` takes C and C++ header files as input and generates corresponding Rust

csmith-fuzzing/README

-7
This file was deleted.

csmith-fuzzing/README.md

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Fuzzing `bindgen` with `csmith`
2+
3+
[`csmith`][csmith] generates random C and C++ programs that can be used as test
4+
cases for compilers. When testing `bindgen` with `csmith`, we interpret the
5+
generated programs as header files, and emit Rust bindings to them. If `bindgen`
6+
panics, the emitted bindings won't compile with `rustc`, or the generated layout
7+
tests in the bindings fail, then we report an issue containing the test case!
8+
9+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
10+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
11+
12+
13+
- [Prerequisites](#prerequisites)
14+
- [Running the Fuzzer](#running-the-fuzzer)
15+
- [Reporting Issues](#reporting-issues)
16+
17+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
18+
19+
## Prerequisites
20+
21+
Requires `python3`, `csmith` and `bindgen` to be in `$PATH`.
22+
23+
Many OS package managers have a `csmith` package:
24+
25+
```
26+
$ sudo apt install csmith
27+
$ brew install csmith
28+
$ # Etc...
29+
```
30+
31+
## Running the Fuzzer
32+
33+
Run `csmith` and test `bindgen` on the generated test cases with this command:
34+
35+
```
36+
$ ./driver.py
37+
```
38+
39+
The driver will keep running until it encounters an error in `bindgen`.
40+
41+
Each invocation of `./driver.py` will use its own temporary directories, so
42+
running it in multiple terminals in parallel is supported.
43+
44+
`csmith` is run with `--no-checksum --nomain --max-block-size 1
45+
--max-block-depth 1` which disables the `main` function, and makes function
46+
bodies as simple as possible as `bindgen` does not care about them, but they
47+
cannot be completely disabled in `csmith`. Run `csmith --help` to see what
48+
exactly those options do.
49+
50+
## Reporting Issues
51+
52+
Once the fuzz driver finds a test case that causes some kind of error in
53+
`bindgen` or its emitted bindings, it is helpful to
54+
[run C-Reduce on the test case][creducing] to remove the parts that are
55+
irrelevant to reproducing the error. This is ***very*** helpful for the folks
56+
who further investigate the issue and come up with a fix!
57+
58+
Additionally, mention that you discovered the issue via `csmith` and we will add
59+
the `A-csmith` label. You can find all the issues discovered with `csmith`, and
60+
related to fuzzing with `csmith`, by looking up
61+
[all issues tagged with the `A-csmith` label][csmith-issues].
62+
63+
[csmith]: https://github.com/csmith-project/csmith
64+
[creducing]: ../CONTRIBUTING.md#using-creduce-to-minimize-test-cases
65+
[csmith-issues]: https://github.com/rust-lang-nursery/rust-bindgen/issues?q=label%3AA-csmith

csmith-fuzzing/driver.py

100644100755
+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env python3
2+
13
import os, sys
24
from subprocess import run, SubprocessError, DEVNULL, PIPE
35
from tempfile import NamedTemporaryFile

0 commit comments

Comments
 (0)