Skip to content

Commit b68d4af

Browse files
Fuzz: Replace honggfuzz harness with cargo-fuzz
1 parent 9296011 commit b68d4af

4 files changed

Lines changed: 38 additions & 62 deletions

File tree

‎docs/fuzzing.md‎

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,39 +19,12 @@
1919

2020
# Fuzzing
2121

22-
## Installing `honggfuzz`
23-
24-
```
25-
cargo install honggfuzz
26-
```
27-
28-
Install [dependencies](https://github.com/rust-fuzz/honggfuzz-rs#dependencies) for your system.
29-
30-
## Running the fuzzer
31-
32-
Running the fuzzer is as easy as running in the `fuzz` directory.
33-
34-
Choose a target:
35-
36-
These are `[[bin]]` entries in `Cargo.toml`.
37-
List them with `cargo read-manifest | jq '.targets[].name'` from the `fuzz` directory.
38-
39-
Run the fuzzer:
22+
`cargo-fuzz` needs the nightly toolchain. Install it with `rustup toolchain install nightly`, then:
4023

4124
```shell
25+
cargo install cargo-fuzz
4226
cd fuzz
43-
cargo hfuzz run <target>
44-
```
45-
46-
After a panic is found, get a stack trace with:
47-
48-
```shell
49-
cargo hfuzz run-debug <target> hfuzz_workspace/<target>/*.fuzz
27+
cargo +nightly fuzz run fuzz_parse_sql -- -max_total_time=600
5028
```
5129

52-
For example, with the `fuzz_parse_sql` target:
53-
54-
```shell
55-
cargo hfuzz run fuzz_parse_sql
56-
cargo hfuzz run-debug fuzz_parse_sql hfuzz_workspace/fuzz_parse_sql/*.fuzz
57-
```
30+
Crashes land in `artifacts/<target>/` and replay with `cargo fuzz run <target> <crash-file>`.

‎fuzz/.gitignore‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
corpus
2-
hfuzz_target
3-
hfuzz_workspace
1+
/artifacts
2+
/corpus
3+
/target

‎fuzz/Cargo.toml‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818
[package]
1919
name = "fuzz"
2020
version = "0.1.0"
21-
edition = "2018"
21+
edition = "2021"
2222
publish = false
2323

24+
[package.metadata]
25+
cargo-fuzz = true
26+
2427
[dependencies]
25-
honggfuzz = "0.5.54"
28+
libfuzzer-sys = "0.4"
2629
sqlparser = { path = ".." }
2730

2831
# Prevent this from interfering with workspaces
@@ -32,3 +35,6 @@ members = ["."]
3235
[[bin]]
3336
name = "fuzz_parse_sql"
3437
path = "fuzz_targets/fuzz_parse_sql.rs"
38+
test = false
39+
doc = false
40+
bench = false

‎fuzz/fuzz_targets/fuzz_parse_sql.rs‎

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,33 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
use honggfuzz::fuzz;
18+
#![no_main]
19+
20+
use libfuzzer_sys::fuzz_target;
1921
use sqlparser::dialect::{
20-
AnsiDialect, BigQueryDialect, ClickHouseDialect, DatabricksDialect, DuckDbDialect,
22+
AnsiDialect, BigQueryDialect, ClickHouseDialect, DatabricksDialect, Dialect, DuckDbDialect,
2123
GenericDialect, HiveDialect, MsSqlDialect, MySqlDialect, OracleDialect, PostgreSqlDialect,
2224
RedshiftSqlDialect, SQLiteDialect, SnowflakeDialect,
2325
};
2426
use sqlparser::parser::Parser;
25-
26-
fn main() {
27-
let dialects: Vec<Box<dyn sqlparser::dialect::Dialect>> = vec![
28-
Box::new(AnsiDialect::default()),
29-
Box::new(BigQueryDialect::default()),
30-
Box::new(ClickHouseDialect::default()),
31-
Box::new(DatabricksDialect::default()),
32-
Box::new(DuckDbDialect::default()),
33-
Box::new(GenericDialect::default()),
34-
Box::new(HiveDialect::default()),
35-
Box::new(MsSqlDialect::default()),
36-
Box::new(MySqlDialect::default()),
37-
Box::new(OracleDialect::default()),
38-
Box::new(PostgreSqlDialect::default()),
39-
Box::new(RedshiftSqlDialect::default()),
40-
Box::new(SQLiteDialect::default()),
41-
Box::new(SnowflakeDialect::default()),
27+
fuzz_target!(|sql: &str| {
28+
let dialects: [&dyn Dialect; 14] = [
29+
&AnsiDialect {},
30+
&BigQueryDialect {},
31+
&ClickHouseDialect {},
32+
&DatabricksDialect {},
33+
&DuckDbDialect {},
34+
&GenericDialect {},
35+
&HiveDialect {},
36+
&MsSqlDialect {},
37+
&MySqlDialect {},
38+
&OracleDialect {},
39+
&PostgreSqlDialect {},
40+
&RedshiftSqlDialect {},
41+
&SQLiteDialect {},
42+
&SnowflakeDialect {},
4243
];
43-
loop {
44-
fuzz!(|data: String| {
45-
for dialect in &dialects {
46-
let _ = Parser::parse_sql(dialect.as_ref(), &data);
47-
}
48-
});
44+
for dialect in dialects {
45+
let _ = Parser::parse_sql(dialect, sql);
4946
}
50-
}
47+
});

0 commit comments

Comments
 (0)