Skip to content

Commit 0417c4f

Browse files
authored
Merge pull request #81 from elichai/no-env-check
Make RUST_LIBFUZZER_DEBUG_PATH "free" for non-users
2 parents 062f36b + 4ac2b47 commit 0417c4f

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ version = "0.4.1"
1010

1111
[dependencies]
1212
arbitrary = "1"
13+
once_cell = "1"
1314

1415
[build-dependencies]
1516
cc = "1.0"

src/lib.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#![deny(missing_docs, missing_debug_implementations)]
1313

1414
pub use arbitrary;
15+
use once_cell::sync::OnceCell;
1516

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

40+
#[doc(hidden)]
41+
pub static RUST_LIBFUZZER_DEBUG_PATH: OnceCell<String> = OnceCell::new();
42+
3943
#[doc(hidden)]
4044
#[export_name = "LLVMFuzzerInitialize"]
4145
pub fn initialize(_argc: *const isize, _argv: *const *const *const u8) -> isize {
@@ -52,6 +56,14 @@ pub fn initialize(_argc: *const isize, _argv: *const *const *const u8) -> isize
5256
default_hook(panic_info);
5357
::std::process::abort();
5458
}));
59+
60+
// Initialize the `RUST_LIBFUZZER_DEBUG_PATH` cell with the path so it can be
61+
// reused with little overhead.
62+
if let Ok(path) = std::env::var("RUST_LIBFUZZER_DEBUG_PATH") {
63+
RUST_LIBFUZZER_DEBUG_PATH
64+
.set(path)
65+
.expect("Since this is initialize it is only called once so can never fail");
66+
}
5567
0
5668
}
5769

@@ -130,7 +142,9 @@ macro_rules! fuzz_target {
130142
// When `RUST_LIBFUZZER_DEBUG_PATH` is set, write the debug
131143
// formatting of the input to that file. This is only intended for
132144
// `cargo fuzz`'s use!
133-
if let Ok(path) = std::env::var("RUST_LIBFUZZER_DEBUG_PATH") {
145+
146+
// `RUST_LIBFUZZER_DEBUG_PATH` is set in initialization.
147+
if let Some(path) = $crate::RUST_LIBFUZZER_DEBUG_PATH.get() {
134148
use std::io::Write;
135149
let mut file = std::fs::File::create(path)
136150
.expect("failed to create `RUST_LIBFUZZER_DEBUG_PATH` file");
@@ -151,7 +165,7 @@ macro_rules! fuzz_target {
151165
/// Auto-generated function
152166
#[no_mangle]
153167
pub extern "C" fn rust_fuzzer_test_input(bytes: &[u8]) {
154-
use libfuzzer_sys::arbitrary::{Arbitrary, Unstructured};
168+
use $crate::arbitrary::{Arbitrary, Unstructured};
155169

156170
// Early exit if we don't have enough bytes for the `Arbitrary`
157171
// implementation. This helps the fuzzer avoid exploring all the
@@ -169,7 +183,9 @@ macro_rules! fuzz_target {
169183
// When `RUST_LIBFUZZER_DEBUG_PATH` is set, write the debug
170184
// formatting of the input to that file. This is only intended for
171185
// `cargo fuzz`'s use!
172-
if let Ok(path) = std::env::var("RUST_LIBFUZZER_DEBUG_PATH") {
186+
187+
// `RUST_LIBFUZZER_DEBUG_PATH` is set in initialization.
188+
if let Some(path) = $crate::RUST_LIBFUZZER_DEBUG_PATH.get() {
173189
use std::io::Write;
174190
let mut file = std::fs::File::create(path)
175191
.expect("failed to create `RUST_LIBFUZZER_DEBUG_PATH` file");

0 commit comments

Comments
 (0)