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
use libc;
use std::env;
use std::ffi::CString;
use std::path::PathBuf;
extern "C" {
fn afl_fuzz_main(argc: libc::c_int,
argv: *const *const libc::c_char) -> libc::c_int;
}
pub struct AflFuzzConfig {
pub in_dir: PathBuf,
pub out_dir: PathBuf,
pub target: PathBuf,
}
impl AflFuzzConfig {
fn into_args(self) -> Vec<CString> {
let mut args = vec![];
args.push("-i");
args.push(self.in_dir.to_str().unwrap());
args.push("-o");
args.push(self.out_dir.to_str().unwrap());
args.push(self.target.to_str().unwrap());
args.into_iter()
.map(|s| CString::new(s).unwrap())
.collect()
}
}
pub fn afl_fuzz_env() -> Result<(), libc::c_int> {
let args = env::args();
let c_args = args.map(|arg| CString::new(arg).unwrap())
.map(|arg| arg.as_ptr())
.collect::<Vec<_>>();
let ret = unsafe {
afl_fuzz_main(c_args.len() as libc::c_int, c_args.as_ptr())
};
match ret {
0 => Ok(()),
n => Err(n),
}
}
pub fn afl_fuzz(config: AflFuzzConfig) -> Result<(), ()> {
let mut args = vec![];
let arg0 = CString::new("afl-fuzz").unwrap();
args.push(arg0);
args.extend(config.into_args());
let c_args = args.iter()
.map(|arg| arg.as_ptr())
.collect::<Vec<_>>();
let ret = unsafe {
afl_fuzz_main(c_args.len() as i32, c_args.as_ptr())
};
match ret {
0 => Ok(()),
_ => Err(()),
}
}
#[test]
fn testsomething() {
let config = AflFuzzConfig {
in_dir: ".".into(),
out_dir: "testout".into(),
target: "../target/debug/examples/hello".into(),
};
afl_fuzz(config);
}