Skip to content

Commit 2542ae4

Browse files
authored
Merge pull request #43 from fitzgen/fix-arbitrary
Fix `fuzz_target!` with `Arbitrary`-implementing types
2 parents 4ad88ec + 5e92615 commit 2542ae4

File tree

8 files changed

+16
-18
lines changed

8 files changed

+16
-18
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ version = "0.1.0"
44
authors = ["The cargo-fuzz Project Developers"]
55
repository = "https://github.com/rust-fuzz/libfuzzer-sys"
66
license = "MIT/Apache-2.0/NCSA"
7+
edition = "2018"
78

89
# cargo-fuzz puts this in a crate subdirectory,
910
# which causes problems if the crate uses workspaces.

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ and change the `src/main.rs` to fuzz your code:
3232
```rust
3333
#![no_main]
3434

35-
#[macro_use]
36-
extern crate libfuzzer_sys;
37-
extern crate your_crate;
35+
use libfuzzer_sys::fuzz_target;
3836

3937
fuzz_target!(|data: &[u8]| {
4038
// code to fuzz goes here

build.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
extern crate cc;
2-
31
fn main() {
42
if let Ok(custom) = ::std::env::var("CUSTOM_LIBFUZZER_PATH") {
53
let custom_lib_path = ::std::path::PathBuf::from(&custom);

example/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
name = "example"
33
version = "0.1.0"
44
authors = ["Simonas Kazlauskas <[email protected]>"]
5+
edition = "2018"
56

67
[workspace]
78
members = ["."]

example/src/main.rs

100644100755
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#![no_main]
22

3-
#[macro_use]
4-
extern crate libfuzzer_sys;
3+
use libfuzzer_sys::fuzz_target;
54

65
fuzz_target!(|data: &[u8]| {
76
if data == b"banana!" {

example_arbitrary/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
name = "example"
33
version = "0.1.0"
44
authors = ["Simonas Kazlauskas <[email protected]>"]
5+
edition = "2018"
56

67
[workspace]
78
members = ["."]

example_arbitrary/src/main.rs

100644100755
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#![no_main]
22

3-
#[macro_use]
4-
extern crate libfuzzer_sys;
3+
use libfuzzer_sys::fuzz_target;
54

65
fuzz_target!(|data: u16| {
76
if data == 0xba7 { // ba[nana]

src/lib.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub fn initialize(_argc: *const isize, _argv: *const *const *const u8) -> isize
2323
// Registers a panic hook that aborts the process before unwinding.
2424
// It is useful to abort before unwinding so that the fuzzer will then be
2525
// able to analyse the process stack frames to tell different bugs appart.
26-
//
26+
//
2727
// HACK / FIXME: it would be better to use `-C panic=abort` but it's currently
2828
// impossible to build code using compiler plugins with this flag.
2929
// We will be able to remove this code when
@@ -46,19 +46,20 @@ macro_rules! fuzz_target {
4646
fuzz_target!(|$data| $body);
4747
};
4848
(|$data:ident: $dty: ty| $body:block) => {
49-
extern crate arbitrary;
50-
5149
#[no_mangle]
5250
pub extern fn rust_fuzzer_test_input(bytes: &[u8]) {
5351
use arbitrary::{Arbitrary, RingBuffer};
5452

55-
let $data: $dty = if let Ok(d) = RingBuffer::new(bytes, bytes.len()).and_then(|mut b|{
56-
Arbitrary::arbitrary(&mut b).map_err(|_| "")
57-
}) {
58-
d
59-
} else {
60-
return
53+
let mut buf = match RingBuffer::new(bytes, bytes.len()) {
54+
Ok(b) => b,
55+
Err(_) => return,
56+
};
57+
58+
let $data: $dty = match Arbitrary::arbitrary(&mut buf) {
59+
Ok(d) => d,
60+
Err(_) => return,
6161
};
62+
6263
$body
6364
}
6465
};

0 commit comments

Comments
 (0)