1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#[cfg(fuzzing_libfuzzer)]
pub mod fuzzer {
use std::{
ffi::CString,
os::raw::{c_char, c_int},
};
extern "C" {
#[allow(improper_ctypes)]
fn __BOLERO__test(input: &[u8]);
pub fn LLVMFuzzerStartTest(a: c_int, b: *const *const c_char) -> c_int;
}
#[no_mangle]
pub unsafe extern "C" fn LLVMFuzzerTestOneInput(data: *const u8, size: usize) -> i32 {
let exec = || {
let data_slice = std::slice::from_raw_parts(data, size);
__BOLERO__test(data_slice);
};
if std::panic::catch_unwind(exec).is_err() {
1
} else {
0
}
}
#[no_mangle]
pub unsafe extern "C" fn LLVMFuzzerInitialize(
_argc: *const isize,
_argv: *const *const *const u8,
) -> isize {
std::panic::set_hook(Box::new(|_| {
std::process::abort();
}));
0
}
pub unsafe fn fuzz() {
let args = std::env::args()
.map(|arg| CString::new(arg).unwrap())
.collect::<Vec<_>>();
let c_args = args
.iter()
.map(|arg| arg.as_ptr())
.chain(Some(0 as *const _))
.collect::<Vec<_>>();
LLVMFuzzerStartTest(args.len() as c_int, c_args.as_ptr());
}
}
#[cfg(fuzzing_libfuzzer)]
pub use fuzzer::*;