-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.rs
308 lines (293 loc) · 8.28 KB
/
main.rs
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
extern crate regex;
extern crate structopt;
#[macro_use]
extern crate clap;
#[macro_use]
extern crate failure;
// Strum contains all the trait definitions
extern crate strum;
#[macro_use]
extern crate strum_macros;
use crate::strum::IntoEnumIterator;
use failure::Error;
use structopt::StructOpt;
mod debug;
mod env;
mod exec_all;
mod fuzzers;
mod rust_fuzzers;
mod targets;
mod utils;
/// WARF - WebAssembly Runtimes Fuzzing project
#[derive(StructOpt, Debug)]
enum Cli {
/// Run all fuzz targets
#[structopt(name = "continuously")]
Continuous {
/// Only run target containing this string
#[structopt(short = "q", long = "filter")]
filter: Option<String>,
/// Which fuzzer to run
#[structopt(
short = "f",
long = "fuzzer",
default_value = "Honggfuzz",
raw(
possible_values = "&fuzzers::Fuzzer::variants()",
case_insensitive = "true"
)
)]
fuzzer: fuzzers::Fuzzer,
/// Set timeout per target
#[structopt(short = "t", long = "timeout", default_value = "10")]
timeout: i32,
/// Set number of thread
#[structopt(short = "n", long = "thread")]
thread: Option<i32>,
/// Set seed
#[structopt(short = "s", long = "seed")]
seed: Option<i32>,
/// Set a compilation Sanitizer (advanced)
#[structopt(
long = "sanitizer",
raw(
possible_values = "&fuzzers::Sanitizer::variants()",
case_insensitive = "true"
)
)]
sanitizer: Option<fuzzers::Sanitizer>,
// Run until the end of time (or Ctrl+C)
#[structopt(short = "i", long = "infinite")]
infinite: bool,
},
/// Run one target with specific fuzzer
#[structopt(name = "target")]
Run {
/// Which target to run
target: String,
/// Which fuzzer to run
#[structopt(
short = "f",
long = "fuzzer",
default_value = "Honggfuzz",
raw(
possible_values = "&fuzzers::Fuzzer::variants()",
case_insensitive = "true"
)
)]
fuzzer: fuzzers::Fuzzer,
/// Set timeout
#[structopt(short = "t", long = "timeout")]
timeout: Option<i32>,
/// Set number of thread (only for hfuzz)
#[structopt(short = "n", long = "thread")]
thread: Option<i32>,
/// Set seed
#[structopt(short = "s", long = "seed")]
seed: Option<i32>,
/// Set dictionary file
#[structopt(short = "d", long = "dict")]
dict: Option<String>,
/// Set a compilation Sanitizer (advanced)
#[structopt(
long = "sanitizer",
raw(
possible_values = "&fuzzers::Sanitizer::variants()",
case_insensitive = "true"
)
)]
sanitizer: Option<fuzzers::Sanitizer>,
},
/// Debug one target
#[structopt(name = "debug")]
Debug {
/// Which target to debug
target: String,
},
/// List all available targets
#[structopt(name = "list")]
ListTargets,
/// Run WebAssembly module on all targets
#[structopt(name = "execute-all")]
ExecuteAll {
/// Which wasm to execute
wasm: String,
},
/// Run WebAssembly module on all targets with benchmark
#[structopt(name = "benchmark-all")]
BenchmarkAll {
/// Which wasm to execute
wasm: String,
},
}
/// Main function catching errors
fn main() {
if let Err(e) = run() {
eprintln!("{}", e);
for cause in e.iter_chain().skip(1) {
eprintln!("caused by: {}", cause);
}
::std::process::exit(1);
}
}
/// Parsing of CLI arguments
fn run() -> Result<(), Error> {
use Cli::*;
let cli = Cli::from_args();
match cli {
ExecuteAll { wasm } => {
exec_all::run_exec_all(wasm, false)?;
}
BenchmarkAll { wasm } => {
exec_all::run_exec_all(wasm, true)?;
}
// list all targets
ListTargets => {
list_targets()?;
}
// Fuzz one target
Run {
target,
fuzzer,
timeout,
thread,
seed,
dict,
sanitizer,
} => {
let config = fuzzers::FuzzerConfig {
timeout,
thread,
sanitizer,
seed,
dict,
};
run_target(target, fuzzer, config)?;
}
// Debug one target
Debug { target } => {
debug::run_debug(target)?;
}
// Fuzz multiple targets
Continuous {
filter,
timeout,
fuzzer,
thread,
seed,
sanitizer,
infinite,
} => {
let config = fuzzers::FuzzerConfig {
timeout: Some(timeout),
thread,
sanitizer,
seed,
dict: None,
};
run_continuously(filter, fuzzer, config, infinite)?;
}
}
Ok(())
}
/// List all targets available
fn list_targets() -> Result<(), Error> {
for target in targets::get_targets() {
println!("{}", target);
}
Ok(())
}
/// Run fuzzing on only one target
fn run_target(
target: String,
fuzzer: fuzzers::Fuzzer,
config: fuzzers::FuzzerConfig,
) -> Result<(), Error> {
let target = match targets::Targets::iter().find(|x| x.name() == target) {
None => bail!(
"Don't know target `{}`. {}",
target,
if let Some(alt) = utils::did_you_mean(&target, &targets::get_targets()) {
format!("Did you mean `{}`?", alt)
} else {
"".into()
}
),
Some(t) => t,
};
use fuzzers::Fuzzer::*;
match fuzzer {
Afl => {
let afl = rust_fuzzers::FuzzerAfl::new(config)?;
afl.run(target)?;
}
Honggfuzz => {
let hfuzz = rust_fuzzers::FuzzerHfuzz::new(config)?;
hfuzz.run(target)?;
}
Libfuzzer => {
let lfuzz = rust_fuzzers::FuzzerLibfuzzer::new(config)?;
lfuzz.run(target)?;
}
}
Ok(())
}
/// Run fuzzing on multiple target matching the filter option
fn run_continuously(
filter: Option<String>,
fuzzer: fuzzers::Fuzzer,
config: fuzzers::FuzzerConfig,
infinite: bool,
) -> Result<(), Error> {
let run = |target: &str| -> Result<(), Error> {
let target = match targets::Targets::iter().find(|x| x.name() == target) {
None => bail!(
"Don't know target `{}`. {}",
target,
if let Some(alt) = utils::did_you_mean(&target, &targets::get_targets()) {
format!("Did you mean `{}`?", alt)
} else {
"".into()
}
),
Some(t) => t,
};
use fuzzers::Fuzzer::*;
match fuzzer {
Afl => {
let hfuzz = rust_fuzzers::FuzzerAfl::new(config.clone())?;
hfuzz.run(target)?;
}
Honggfuzz => {
let hfuzz = rust_fuzzers::FuzzerHfuzz::new(config.clone())?;
hfuzz.run(target)?;
}
Libfuzzer => {
let hfuzz = rust_fuzzers::FuzzerLibfuzzer::new(config.clone())?;
hfuzz.run(target)?;
}
}
Ok(())
};
let targets = targets::get_targets();
let targets = targets
.iter()
.filter(|x| filter.as_ref().map(|f| x.contains(f)).unwrap_or(true));
'cycle: loop {
'targets_pass: for target in targets.clone() {
if let Err(e) = run(target) {
match e.downcast::<fuzzers::FuzzerQuit>() {
Ok(_) => {
println!("Fuzzer failed so we'll continue with the next one");
continue 'targets_pass;
}
Err(other_error) => return Err(other_error),
}
}
}
if !infinite {
break 'cycle;
}
}
Ok(())
}