12
12
#![ deny( missing_docs, missing_debug_implementations) ]
13
13
14
14
pub use arbitrary;
15
+ use once_cell:: sync:: OnceCell ;
15
16
16
17
extern "C" {
17
18
// We do not actually cross the FFI bound here.
@@ -36,6 +37,9 @@ pub fn test_input_wrap(data: *const u8, size: usize) -> i32 {
36
37
0
37
38
}
38
39
40
+ #[ doc( hidden) ]
41
+ pub static RUST_LIBFUZZER_DEBUG_PATH : OnceCell < String > = OnceCell :: new ( ) ;
42
+
39
43
#[ doc( hidden) ]
40
44
#[ export_name = "LLVMFuzzerInitialize" ]
41
45
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
52
56
default_hook ( panic_info) ;
53
57
:: std:: process:: abort ( ) ;
54
58
} ) ) ;
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
+ }
55
67
0
56
68
}
57
69
@@ -130,7 +142,9 @@ macro_rules! fuzz_target {
130
142
// When `RUST_LIBFUZZER_DEBUG_PATH` is set, write the debug
131
143
// formatting of the input to that file. This is only intended for
132
144
// `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( ) {
134
148
use std:: io:: Write ;
135
149
let mut file = std:: fs:: File :: create( path)
136
150
. expect( "failed to create `RUST_LIBFUZZER_DEBUG_PATH` file" ) ;
@@ -169,7 +183,9 @@ macro_rules! fuzz_target {
169
183
// When `RUST_LIBFUZZER_DEBUG_PATH` is set, write the debug
170
184
// formatting of the input to that file. This is only intended for
171
185
// `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( ) {
173
189
use std:: io:: Write ;
174
190
let mut file = std:: fs:: File :: create( path)
175
191
. expect( "failed to create `RUST_LIBFUZZER_DEBUG_PATH` file" ) ;
0 commit comments