Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make RUST_LIBFUZZER_DEBUG_PATH "free" for non-users #81

Merged
merged 2 commits into from
May 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ version = "0.4.1"

[dependencies]
arbitrary = "1"
once_cell = "1"

[build-dependencies]
cc = "1.0"
Expand Down
22 changes: 19 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#![deny(missing_docs, missing_debug_implementations)]

pub use arbitrary;
use once_cell::sync::OnceCell;

extern "C" {
// We do not actually cross the FFI bound here.
Expand All @@ -36,6 +37,9 @@ pub fn test_input_wrap(data: *const u8, size: usize) -> i32 {
0
}

#[doc(hidden)]
pub static RUST_LIBFUZZER_DEBUG_PATH: OnceCell<String> = OnceCell::new();

#[doc(hidden)]
#[export_name = "LLVMFuzzerInitialize"]
pub fn initialize(_argc: *const isize, _argv: *const *const *const u8) -> isize {
Expand All @@ -52,6 +56,14 @@ pub fn initialize(_argc: *const isize, _argv: *const *const *const u8) -> isize
default_hook(panic_info);
::std::process::abort();
}));

// Initialize the `RUST_LIBFUZZER_DEBUG_PATH` cell with the path so it can be
// reused with little overhead.
if let Ok(path) = std::env::var("RUST_LIBFUZZER_DEBUG_PATH") {
RUST_LIBFUZZER_DEBUG_PATH
.set(path)
.expect("Since this is initialize it is only called once so can never fail");
}
0
}

Expand Down Expand Up @@ -130,7 +142,9 @@ macro_rules! fuzz_target {
// When `RUST_LIBFUZZER_DEBUG_PATH` is set, write the debug
// formatting of the input to that file. This is only intended for
// `cargo fuzz`'s use!
if let Ok(path) = std::env::var("RUST_LIBFUZZER_DEBUG_PATH") {

// `RUST_LIBFUZZER_DEBUG_PATH` is set in initialization.
if let Some(path) = $crate::RUST_LIBFUZZER_DEBUG_PATH.get() {
use std::io::Write;
let mut file = std::fs::File::create(path)
.expect("failed to create `RUST_LIBFUZZER_DEBUG_PATH` file");
Expand All @@ -151,7 +165,7 @@ macro_rules! fuzz_target {
/// Auto-generated function
#[no_mangle]
pub extern "C" fn rust_fuzzer_test_input(bytes: &[u8]) {
use libfuzzer_sys::arbitrary::{Arbitrary, Unstructured};
use $crate::arbitrary::{Arbitrary, Unstructured};

// Early exit if we don't have enough bytes for the `Arbitrary`
// implementation. This helps the fuzzer avoid exploring all the
Expand All @@ -169,7 +183,9 @@ macro_rules! fuzz_target {
// When `RUST_LIBFUZZER_DEBUG_PATH` is set, write the debug
// formatting of the input to that file. This is only intended for
// `cargo fuzz`'s use!
if let Ok(path) = std::env::var("RUST_LIBFUZZER_DEBUG_PATH") {

// `RUST_LIBFUZZER_DEBUG_PATH` is set in initialization.
if let Some(path) = $crate::RUST_LIBFUZZER_DEBUG_PATH.get() {
use std::io::Write;
let mut file = std::fs::File::create(path)
.expect("failed to create `RUST_LIBFUZZER_DEBUG_PATH` file");
Expand Down