Skip to content

Commit e9e5eb8

Browse files
authored
Merge pull request dtolnay#375 from dtolnay/afl
Add AFL++ support to fuzz target
2 parents 9f9328b + 93f8ee7 commit e9e5eb8

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ jobs:
9797
- uses: dtolnay/rust-toolchain@nightly
9898
- uses: dtolnay/install@cargo-fuzz
9999
- run: cargo fuzz check
100+
- run: cargo check --no-default-features --features afl
101+
working-directory: fuzz
102+
100103

101104
clippy:
102105
name: Clippy

fuzz/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
artifacts/
22
corpus/
33
coverage/
4+
in/
5+
out/
46
target/

fuzz/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@ publish = false
99
cargo-fuzz = true
1010

1111
[dependencies]
12-
libfuzzer-sys = "0.4"
12+
afl = { version = "0.12", optional = true }
13+
libfuzzer-sys = { version = "0.4", optional = true }
1314
proc-macro2 = { path = "..", default-features = false }
1415

1516
[features]
17+
default = ["libfuzzer"]
18+
afl = ["dep:afl"]
19+
libfuzzer = ["dep:libfuzzer-sys"]
1620
span-locations = ["proc-macro2/span-locations"]
1721

1822
[[bin]]
Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,41 @@
1-
#![no_main]
1+
// libfuzzer:
2+
//
3+
// cargo install cargo-fuzz
4+
// cargo fuzz run parse_token_stream -j $(nproc) -- -max_len=200 -timeout=1
5+
//
6+
// afl++:
7+
//
8+
// cargo install afl
9+
// cargo afl build --no-default-features --features afl --release
10+
// cargo afl fuzz -i in -o out target/release/parse_token_stream
11+
12+
#![cfg_attr(feature = "libfuzzer", no_main)]
213

3-
use libfuzzer_sys::fuzz_target;
414
use std::str;
515

6-
fuzz_target!(|bytes: &[u8]| {
16+
#[cfg(not(any(
17+
all(feature = "libfuzzer", not(feature = "afl")),
18+
all(not(feature = "libfuzzer"), feature = "afl"),
19+
)))]
20+
fn main() {
21+
compile_error! {
22+
r#"exactly one of feature="libfuzzer" and feature="afl" must be enabled"#
23+
}
24+
}
25+
26+
#[cfg(feature = "libfuzzer")]
27+
libfuzzer_sys::fuzz_target!(|bytes: &[u8]| { do_fuzz(bytes) });
28+
29+
#[cfg(feature = "afl")]
30+
fn main() {
31+
let hook = true; // turn panic into crashes
32+
afl::fuzz(hook, do_fuzz);
33+
}
34+
35+
fn do_fuzz(bytes: &[u8]) {
736
if bytes.len() < 200 {
837
if let Ok(string) = str::from_utf8(bytes) {
938
_ = string.parse::<proc_macro2::TokenStream>();
1039
}
1140
}
12-
});
41+
}

0 commit comments

Comments
 (0)