@@ -53,10 +53,11 @@ extern "C" {
53
53
fn LLVMFuzzerMutate ( data : * mut u8 , size : usize , max_size : usize ) -> usize ;
54
54
}
55
55
56
+ /// Do not use; only for LibFuzzer's consumption.
56
57
#[ doc( hidden) ]
57
58
#[ 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 ( || {
60
61
let data_slice = :: std:: slice:: from_raw_parts ( data, size) ;
61
62
rust_fuzzer_test_input ( data_slice)
62
63
} ) ;
@@ -459,9 +460,11 @@ macro_rules! fuzz_mutator {
459
460
|
460
461
$body: block
461
462
) => {
462
- /// Auto-generated function.
463
+ /// Auto-generated function. Do not use; only for LibFuzzer's
464
+ /// consumption.
463
465
#[ export_name = "LLVMFuzzerCustomMutator" ]
464
- pub fn rust_fuzzer_custom_mutator(
466
+ #[ doc( hidden) ]
467
+ pub unsafe fn rust_fuzzer_custom_mutator(
465
468
$data: * mut u8 ,
466
469
$size: usize ,
467
470
$max_size: usize ,
@@ -471,15 +474,26 @@ macro_rules! fuzz_mutator {
471
474
// might be larger or smaller than `max_size`. The `data`'s capacity
472
475
// is the maximum of the two.
473
476
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) ;
475
478
476
479
// `unsigned int` is generally a `u32`, but not on all targets. Do
477
480
// an infallible (and potentially lossy, but that's okay because it
478
481
// preserves determinism) conversion.
479
482
let $seed = $seed as u32 ;
480
483
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
+
481
496
// Truncate the new size if it is larger than the max.
482
- let new_size = { $body } ;
483
497
std:: cmp:: min( new_size, $max_size)
484
498
}
485
499
} ;
0 commit comments