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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use diem_proptest_helpers::ValueGenerator;
use proptest::{
strategy::{Strategy, ValueTree},
test_runner::{self, RngAlgorithm, TestRunner},
};
use rand::RngCore;
use std::{ffi::CString, fmt, ops::Deref, os::raw::c_char, str::FromStr};
pub mod commands;
#[cfg(test)]
mod coverage;
pub mod fuzz_targets;
pub trait FuzzTargetImpl: Sync + Send + fmt::Debug {
fn name(&self) -> &'static str {
std::any::type_name::<Self>()
.rsplit("::")
.next()
.expect("Implementation struct name must have at least one component")
}
fn description(&self) -> &'static str;
fn generate(&self, _idx: usize, _gen: &mut ValueGenerator) -> Option<Vec<u8>>;
fn fuzz(&self, data: &[u8]);
}
#[derive(Copy, Clone, Debug)]
pub struct FuzzTarget(&'static (dyn FuzzTargetImpl + 'static));
impl Deref for FuzzTarget {
type Target = dyn FuzzTargetImpl + 'static;
fn deref(&self) -> &Self::Target {
self.0
}
}
impl FromStr for FuzzTarget {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
FuzzTarget::by_name(s).ok_or_else(|| format!("Fuzz target '{}' not found (run `list`)", s))
}
}
fn corpus_from_strategy(strategy: impl Strategy) -> Vec<u8> {
let mut seed = [0u8; 32];
let mut rng = rand::thread_rng();
rng.fill_bytes(&mut seed);
let recorder_rng = test_runner::TestRng::from_seed(RngAlgorithm::Recorder, &seed);
let mut runner = TestRunner::new_with_rng(test_runner::Config::default(), recorder_rng);
strategy
.new_tree(&mut runner)
.expect("creating a new value should succeed")
.current();
runner.bytes_used()
}
pub fn fuzz_data_to_value<T: std::fmt::Debug>(
data: &[u8],
strategy: impl Strategy<Value = T>,
) -> T {
let passthrough_rng =
test_runner::TestRng::from_seed(test_runner::RngAlgorithm::PassThrough, data);
let config = test_runner::Config::default();
let mut runner = TestRunner::new_with_rng(config, passthrough_rng);
let strategy_tree = strategy.new_tree(&mut runner).expect("should not happen");
strategy_tree.current()
}
#[no_mangle]
pub extern "C" fn __lsan_default_suppressions() -> *const c_char {
let s = CString::new(include_str!("../lsan_suppressions.txt")).unwrap();
let p = s.as_ptr();
std::mem::forget(s);
p
}