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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
use std::io::{self, Read};
use std::panic;
mod common;
extern "C" {
fn __afl_persistent_loop(counter: usize) -> isize;
fn __afl_manual_init();
static __afl_fuzz_len: *const u32;
static __afl_fuzz_ptr: *const u8;
}
#[doc(hidden)]
#[no_mangle]
pub static __afl_sharedmem_fuzzing: i32 = 1;
pub fn fuzz<F>(hook: bool, mut closure: F)
where
F: FnMut(&[u8]) + std::panic::RefUnwindSafe,
{
static PERSIST_MARKER: &str = "##SIG_AFL_PERSISTENT##\0";
static DEFERED_MARKER: &str = "##SIG_AFL_DEFER_FORKSRV##\0";
unsafe { std::ptr::read_volatile(&PERSIST_MARKER) }; unsafe { std::ptr::read_volatile(&DEFERED_MARKER) };
if hook {
std::panic::set_hook(Box::new(|_| {
std::process::abort();
}));
}
let mut input = vec![];
unsafe { __afl_manual_init() };
while unsafe { __afl_persistent_loop(1000) } != 0 {
let input_ref = if unsafe { __afl_fuzz_ptr.is_null() } {
let result = io::stdin().read_to_end(&mut input);
if result.is_err() {
return;
}
&input
} else {
unsafe {
let input_len = *__afl_fuzz_len as usize;
std::slice::from_raw_parts(__afl_fuzz_ptr, input_len)
}
};
let did_panic = std::panic::catch_unwind(panic::AssertUnwindSafe(|| {
closure(input_ref);
}))
.is_err();
if did_panic {
std::process::abort();
}
#[cfg(feature = "reset_lazy_static")]
unsafe {
lazy_static::lazy::reset();
}
input.clear();
}
}
#[macro_export]
macro_rules! fuzz {
( $($x:tt)* ) => { $crate::__fuzz!(true, $($x)*) }
}
#[macro_export]
macro_rules! fuzz_nohook {
( $($x:tt)* ) => { $crate::__fuzz!(false, $($x)*) }
}
#[doc(hidden)]
#[macro_export]
macro_rules! __fuzz {
($hook:expr, |$buf:ident| $body:block) => {
$crate::fuzz($hook, |$buf| $body);
};
($hook:expr, |$buf:ident: &[u8]| $body:block) => {
$crate::fuzz($hook, |$buf| $body);
};
($hook:expr, |$buf:ident: $dty: ty| $body:block) => {
$crate::fuzz($hook, |$buf| {
let $buf: $dty = {
let mut data = ::arbitrary::Unstructured::new($buf);
if let Ok(d) = ::arbitrary::Arbitrary::arbitrary(&mut data).map_err(|_| "") {
d
} else {
return;
}
};
$body
});
};
}
#[cfg(test)]
mod test {
use std::{path, process, thread, time};
fn target_dir_path() -> &'static path::Path {
if path::Path::new("../target/debug/cargo-afl").exists() {
path::Path::new("../target/debug/")
} else if path::Path::new("target/debug/cargo-afl").exists() {
path::Path::new("target/debug/")
} else {
panic!("Could not find cargo-afl binary");
}
}
fn cargo_afl_path() -> path::PathBuf {
target_dir_path().join("cargo-afl")
}
fn examples_hello_path() -> path::PathBuf {
target_dir_path().join("examples").join("hello")
}
#[test]
fn integration() {
let temp_dir = tempfile::TempDir::new().expect("Could not create temporary directory");
let temp_dir_path = temp_dir.path();
let mut child = process::Command::new(cargo_afl_path())
.arg("afl")
.arg("fuzz")
.stdout(process::Stdio::inherit())
.stderr(process::Stdio::inherit())
.arg("-i")
.arg(".")
.arg("-o")
.arg(temp_dir_path)
.arg(examples_hello_path())
.spawn()
.expect("Could not run cargo afl fuzz");
thread::sleep(time::Duration::from_secs(10));
for _ in 0..5 {
thread::sleep(time::Duration::from_secs(1));
child.kill().unwrap_or_default();
}
assert!(temp_dir_path.join("default").join("fuzzer_stats").is_file());
}
}