@@ -118,6 +118,18 @@ macro_rules! fuzz_target {
118
118
( |$bytes: ident| $body: block) => {
119
119
#[ no_mangle]
120
120
pub extern "C" fn rust_fuzzer_test_input( $bytes: & [ u8 ] ) {
121
+ // When `RUST_LIBFUZZER_DEBUG_PATH` is set, write the debug
122
+ // formatting of the input to that file. This is only intended for
123
+ // `cargo fuzz`'s use!
124
+ if let Ok ( path) = std:: env:: var( "RUST_LIBFUZZER_DEBUG_PATH" ) {
125
+ use std:: io:: Write ;
126
+ let mut file = std:: fs:: File :: create( path)
127
+ . expect( "failed to create `RUST_LIBFUZZER_DEBUG_PATH` file" ) ;
128
+ writeln!( & mut file, "{:?}" , $bytes)
129
+ . expect( "failed to write to `RUST_LIBFUZZER_DEBUG_PATH` file" ) ;
130
+ return ;
131
+ }
132
+
121
133
$body
122
134
}
123
135
} ;
@@ -132,8 +144,24 @@ macro_rules! fuzz_target {
132
144
use libfuzzer_sys:: arbitrary:: { Arbitrary , Unstructured } ;
133
145
134
146
let mut u = Unstructured :: new( bytes) ;
147
+ let data = <$dty as Arbitrary >:: arbitrary_take_rest( u) ;
148
+
149
+ // When `RUST_LIBFUZZER_DEBUG_PATH` is set, write the debug
150
+ // formatting of the input to that file. This is only intended for
151
+ // `cargo fuzz`'s use!
152
+ if let Ok ( path) = std:: env:: var( "RUST_LIBFUZZER_DEBUG_PATH" ) {
153
+ use std:: io:: Write ;
154
+ let mut file = std:: fs:: File :: create( path)
155
+ . expect( "failed to create `RUST_LIBFUZZER_DEBUG_PATH` file" ) ;
156
+ ( match data {
157
+ Ok ( data) => writeln!( & mut file, "{:#?}" , data) ,
158
+ Err ( err) => writeln!( & mut file, "Arbitrary Error: {}" , err) ,
159
+ } )
160
+ . expect( "failed to write to `RUST_LIBFUZZER_DEBUG_PATH` file" ) ;
161
+ return ;
162
+ }
135
163
136
- let $data: $dty = match Arbitrary :: arbitrary_take_rest ( u ) {
164
+ let $data = match data {
137
165
Ok ( d) => d,
138
166
Err ( _) => return ,
139
167
} ;
0 commit comments