rust-fuzz / libfuzzer

Rust bindings and utilities for LLVM’s libFuzzer
Apache License 2.0
208 stars 44 forks source link

Using custom `Arbitrary`-implementing types doesn't work #42

Closed fitzgen closed 4 years ago

fitzgen commented 4 years ago

First, the Cargo.toml needs to have a dep on arbitrary, which cargo-fuzz doesn't generate (or alternatively, libfuzzer_sys needs to re-export the Arbitrary trait).

Second, the fuzz_target! macro expands to code that has a type error:

// fuzz_target_1.rs

#![no_main]
use libfuzzer_sys::fuzz_target;

fuzz_target!(|data: usize| {
    // NB:          ^^^^^ type that implements `Arbitrary`

    let _ = data;
});

Running cargo fuzz run fuzz_target_1 results in this error:

error[E0308]: mismatched types
 --> fuzz_targets/fuzz_target_1.rs:4:1
  |
4 | / fuzz_target!(|data: usize| {
5 | |     // NB:          ^^^^^ type that implements `Arbitrary`
6 | |
7 | |     let _ = data;
8 | | });
  | |___^ expected enum `arbitrary::BufferError`, found &str
  |
  = note: expected type `std::result::Result<_, arbitrary::BufferError>`
             found type `std::result::Result<_, &str>`
  = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.
error: could not compile `foobar-fuzz`.