cargo-fuzz
cargo-fuzz copied to clipboard
Detect when "cargo fuzz init" is run on a binary package instead of a library package
I was trying out cargo fuzz init on a rust project that only had a main.rs but no lib.rs. As a consequence, the generated file fuzz_target_1.rs failed to build:
$ cargo fuzz run fuzz_target_1
Fresh cc v1.0.8
Fresh arbitrary v0.1.0
Fresh libfuzzer-sys v0.1.0 (https://github.com/rust-fuzz/libfuzzer-sys.git#4594b1f3)
Compiling merge-dags-fuzz v0.0.1 (file:///Users/mstange/code/merge-dags/fuzz)
[...]
error[E0463]: can't find crate for `merge_dags`
--> fuzz_targets/fuzz_target_1.rs:3:1
|
3 | extern crate merge_dags;
| ^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate
error: aborting due to previous error
For more information about this error, try `rustc --explain E0463`.
error: Could not compile `merge-dags-fuzz`.
It would be nice to detect this condition and show a more helpful error message.
Yes! It would be nice :sweat_smile: I had this exact same problem and now I finally know what's up!
The way I'm working around this is by putting all my code in a lib crate and then just invoking the lib from src/main.rs and the specific function I want to fuzz from the fuzz target.
The way I'm working around this is by putting all my code in a lib crate and then just invoking the lib from src/main.rs and the specific function I want to fuzz from the fuzz target.
How can I do that? I assume you're making 2 distinct crates, then.
I'm barely a novice with cargo and the module system, though. Is there a tutorial for this? It would also be nice to be able to have my lib crate in a local directory, because I can't currently publish it.
@felix91gr it's as simple as creating both src/lib.rs and src/main.rs. This can be done within the same crate, you can have a look at an example project (that also uses cargo-fuzz) over here.