Skip to content

Commit bcfa5f8

Browse files
authored
Merge pull request #131 from flyingmutant/u8-corpus
fuzz_target: make it possible to return Corpus for byte inputs
2 parents 4b077c2 + a4d967a commit bcfa5f8

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

src/lib.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,27 @@ pub fn initialize(_argc: *const isize, _argv: *const *const *const u8) -> isize
224224
///
225225
#[macro_export]
226226
macro_rules! fuzz_target {
227+
(|$bytes:ident| $body:expr) => {
228+
$crate::fuzz_target!(init: (), |$bytes: &[u8]| -> () { $body });
229+
};
230+
231+
(|$bytes:ident: &[u8]| $body:expr) => {
232+
$crate::fuzz_target!(init: (), |$bytes: &[u8]| -> () { $body });
233+
};
234+
235+
(|$bytes:ident: &[u8]| -> $rty:ty $body:block) => {
236+
$crate::fuzz_target!(init: (), |$bytes: &[u8]| -> $rty { $body });
237+
};
238+
227239
(init: $init:expr, |$bytes:ident| $body:expr) => {
240+
$crate::fuzz_target!(init: $init, |$bytes: &[u8]| -> () { $body });
241+
};
242+
243+
(init: $init:expr, |$bytes:ident: &[u8]| $body:expr) => {
244+
$crate::fuzz_target!(init: $init, |$bytes: &[u8]| -> () { $body });
245+
};
246+
247+
(init: $init:expr, |$bytes:ident: &[u8]| -> $rty:ty $body:block) => {
228248
const _: () = {
229249
/// Auto-generated functions
230250
/// LLVMFuzzerInitialize is called once before the fuzzer starts.
@@ -253,8 +273,8 @@ macro_rules! fuzz_target {
253273
return 0;
254274
}
255275

256-
__libfuzzer_sys_run(bytes);
257-
0
276+
let result = ::libfuzzer_sys::Corpus::from(__libfuzzer_sys_run(bytes));
277+
result.to_libfuzzer_code()
258278
}
259279

260280
// Split out the actual fuzzer into a separate function which is
@@ -270,20 +290,12 @@ macro_rules! fuzz_target {
270290
// ideally help prevent oss-fuzz from deduplicate fuzz bugs across
271291
// distinct targets accidentally.
272292
#[inline(never)]
273-
fn __libfuzzer_sys_run($bytes: &[u8]) {
293+
fn __libfuzzer_sys_run($bytes: &[u8]) -> $rty {
274294
$body
275295
}
276296
};
277297
};
278298

279-
(|$bytes:ident| $body:expr) => {
280-
$crate::fuzz_target!(|$bytes: &[u8]| $body);
281-
};
282-
283-
(|$data:ident: &[u8]| $body:expr) => {
284-
$crate::fuzz_target!(init: (), |$data| $body);
285-
};
286-
287299
(|$data:ident: $dty:ty| $body:expr) => {
288300
$crate::fuzz_target!(init: (), |$data: $dty| -> () { $body });
289301
};
@@ -292,14 +304,6 @@ macro_rules! fuzz_target {
292304
$crate::fuzz_target!(init: (), |$data: $dty| -> $rty { $body });
293305
};
294306

295-
(init: $init:expr, |$data:ident: &[u8]| $body:expr) => {
296-
$crate::fuzz_target!(init: $init, |$data| $body);
297-
};
298-
299-
(init: $init:expr, |$bytes:ident| $body:expr) => {
300-
$crate::fuzz_target!(init: $init, |$bytes: &[u8]| $body);
301-
};
302-
303307
(init: $init:expr, |$data:ident: $dty:ty| $body:expr) => {
304308
$crate::fuzz_target!(init: $init, |$data: $dty| -> () { $body });
305309
};

0 commit comments

Comments
 (0)