Skip to content

Commit 910a31a

Browse files
authored
Merge pull request #114 from fitzgen/mark-internal-fuzzer-hook-functions-as-unsafe
Mark internal, fuzzer hook functions as `unsafe`
2 parents c9c43f3 + f267b63 commit 910a31a

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/lib.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,11 @@ extern "C" {
5353
fn LLVMFuzzerMutate(data: *mut u8, size: usize, max_size: usize) -> usize;
5454
}
5555

56+
/// Do not use; only for LibFuzzer's consumption.
5657
#[doc(hidden)]
5758
#[export_name = "LLVMFuzzerTestOneInput"]
58-
pub fn test_input_wrap(data: *const u8, size: usize) -> i32 {
59-
let test_input = ::std::panic::catch_unwind(|| unsafe {
59+
pub unsafe fn test_input_wrap(data: *const u8, size: usize) -> i32 {
60+
let test_input = ::std::panic::catch_unwind(|| {
6061
let data_slice = ::std::slice::from_raw_parts(data, size);
6162
rust_fuzzer_test_input(data_slice)
6263
});
@@ -459,9 +460,11 @@ macro_rules! fuzz_mutator {
459460
|
460461
$body:block
461462
) => {
462-
/// Auto-generated function.
463+
/// Auto-generated function. Do not use; only for LibFuzzer's
464+
/// consumption.
463465
#[export_name = "LLVMFuzzerCustomMutator"]
464-
pub fn rust_fuzzer_custom_mutator(
466+
#[doc(hidden)]
467+
pub unsafe fn rust_fuzzer_custom_mutator(
465468
$data: *mut u8,
466469
$size: usize,
467470
$max_size: usize,
@@ -471,15 +474,26 @@ macro_rules! fuzz_mutator {
471474
// might be larger or smaller than `max_size`. The `data`'s capacity
472475
// is the maximum of the two.
473476
let len = std::cmp::max($max_size, $size);
474-
let $data: &mut [u8] = unsafe { std::slice::from_raw_parts_mut($data, len) };
477+
let $data: &mut [u8] = std::slice::from_raw_parts_mut($data, len);
475478

476479
// `unsigned int` is generally a `u32`, but not on all targets. Do
477480
// an infallible (and potentially lossy, but that's okay because it
478481
// preserves determinism) conversion.
479482
let $seed = $seed as u32;
480483

484+
// Define and invoke a new, safe function so that the body doesn't
485+
// inherit `unsafe`.
486+
fn custom_mutator(
487+
$data: &mut [u8],
488+
$size: usize,
489+
$max_size: usize,
490+
$seed: u32,
491+
) -> usize {
492+
$body
493+
}
494+
let new_size = custom_mutator($data, $size, $max_size, $seed);
495+
481496
// Truncate the new size if it is larger than the max.
482-
let new_size = { $body };
483497
std::cmp::min(new_size, $max_size)
484498
}
485499
};

0 commit comments

Comments
 (0)