-
Notifications
You must be signed in to change notification settings - Fork 103
/
cargo-afl.rs
211 lines (196 loc) Β· 9.01 KB
/
cargo-afl.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
use clap::crate_version;
use std::env;
use std::ffi::OsStr;
use std::process::{self, Command};
#[path = "../common.rs"]
mod common;
fn main() {
if !common::archive_file_path().exists() {
let version = common::afl_rustc_version();
eprintln!("AFL LLVM runtime is not built with Rust {}, run `cargo \
install --force afl` to build it.", version);
process::exit(1);
}
let _ = clap_app().get_matches();
let mut args = env::args().skip(2).peekable(); // skip `cargo` and `afl`
match args.peek().map(|s| &**s) {
Some("analyze") => run_afl(args, "afl-analyze"),
Some("cmin") => run_afl(args, "afl-cmin"),
Some("fuzz") => run_afl(args, "afl-fuzz"),
Some("gotcpu") => run_afl(args, "afl-got-cpu"),
Some("plot") => run_afl(args, "afl-plot"),
Some("showmap") => run_afl(args, "afl-showmap"),
Some("tmin") => run_afl(args, "afl-tmin"),
Some("whatsup") => run_afl(args, "afl-whatsup"),
_ => run_cargo(args),
}
}
fn clap_app() -> clap::App<'static, 'static> {
clap::App::new("cargo afl").bin_name("cargo").subcommand(
clap::SubCommand::with_name("afl")
.version(crate_version!())
.setting(clap::AppSettings::ArgRequiredElseHelp)
.setting(clap::AppSettings::TrailingVarArg)
.setting(clap::AppSettings::AllowExternalSubcommands)
.usage("cargo afl [SUBCOMMAND or Cargo SUBCOMMAND]")
.after_help(
"In addition to the subcommands above, Cargo subcommands are also \
supported (see `cargo help` for a list of all Cargo subcommands).",
)
.subcommand(
clap::SubCommand::with_name("analyze")
.about("Invoke afl-analyze")
.setting(clap::AppSettings::AllowLeadingHyphen)
.setting(clap::AppSettings::DisableHelpSubcommand)
.setting(clap::AppSettings::DisableVersion)
.arg(clap::Arg::with_name("h").short("h").hidden(true))
.arg(clap::Arg::with_name("help").long("help").hidden(true))
.arg(clap::Arg::with_name("afl-analyze args").multiple(true)),
)
.subcommand(
clap::SubCommand::with_name("cmin")
.about("Invoke afl-cmin")
.setting(clap::AppSettings::AllowLeadingHyphen)
.setting(clap::AppSettings::DisableHelpSubcommand)
.setting(clap::AppSettings::DisableVersion)
.arg(clap::Arg::with_name("h").short("h").hidden(true))
.arg(clap::Arg::with_name("help").long("help").hidden(true))
.arg(clap::Arg::with_name("afl-cmin args").multiple(true)),
)
.subcommand(
clap::SubCommand::with_name("fuzz")
.about("Invoke afl-fuzz")
.setting(clap::AppSettings::AllowLeadingHyphen)
.setting(clap::AppSettings::DisableHelpSubcommand)
.setting(clap::AppSettings::DisableVersion)
.arg(clap::Arg::with_name("h").short("h").hidden(true))
.arg(clap::Arg::with_name("help").long("help").hidden(true))
.arg(clap::Arg::with_name("afl-fuzz args").multiple(true)),
)
.subcommand(
clap::SubCommand::with_name("gotcpu")
.about("Invoke afl-gotcpu")
.setting(clap::AppSettings::AllowLeadingHyphen)
.setting(clap::AppSettings::DisableHelpSubcommand)
.setting(clap::AppSettings::DisableVersion)
.arg(clap::Arg::with_name("h").short("h").hidden(true))
.arg(clap::Arg::with_name("help").long("help").hidden(true))
.arg(clap::Arg::with_name("afl-gotcpu args").multiple(true)),
)
.subcommand(
clap::SubCommand::with_name("plot")
.about("Invoke afl-plot")
.setting(clap::AppSettings::AllowLeadingHyphen)
.setting(clap::AppSettings::DisableHelpSubcommand)
.setting(clap::AppSettings::DisableVersion)
.arg(clap::Arg::with_name("h").short("h").hidden(true))
.arg(clap::Arg::with_name("help").long("help").hidden(true))
.arg(clap::Arg::with_name("afl-plot args").multiple(true)),
)
.subcommand(
clap::SubCommand::with_name("showmap")
.about("Invoke afl-showmap")
.setting(clap::AppSettings::AllowLeadingHyphen)
.setting(clap::AppSettings::DisableHelpSubcommand)
.setting(clap::AppSettings::DisableVersion)
.arg(clap::Arg::with_name("h").short("h").hidden(true))
.arg(clap::Arg::with_name("help").long("help").hidden(true))
.arg(clap::Arg::with_name("afl-showmap args").multiple(true)),
)
.subcommand(
clap::SubCommand::with_name("tmin")
.about("Invoke afl-tmin")
.setting(clap::AppSettings::AllowLeadingHyphen)
.setting(clap::AppSettings::DisableHelpSubcommand)
.setting(clap::AppSettings::DisableVersion)
.arg(clap::Arg::with_name("h").short("h").hidden(true))
.arg(clap::Arg::with_name("help").long("help").hidden(true))
.arg(clap::Arg::with_name("afl-tmin args").multiple(true)),
)
.subcommand(
clap::SubCommand::with_name("whatsup")
.about("Invoke afl-whatsup")
.setting(clap::AppSettings::AllowLeadingHyphen)
.setting(clap::AppSettings::DisableHelpSubcommand)
.setting(clap::AppSettings::DisableVersion)
.arg(clap::Arg::with_name("h").short("h").hidden(true))
.arg(clap::Arg::with_name("help").long("help").hidden(true))
.arg(clap::Arg::with_name("afl-whatsup args").multiple(true)),
),
)
}
fn run_afl<I, S>(args: I, cmd: &str)
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
{
let cmd_path = common::afl_dir().join("bin").join(cmd);
let status = Command::new(cmd_path)
.args(args.into_iter().skip(1)) // skip afl sub-command
.status()
.unwrap();
process::exit(status.code().unwrap_or(1));
}
fn run_cargo<I, S>(args: I)
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
{
let cargo_path = env!("CARGO");
// add some flags to sanitizers to make them work with Rust code
let asan_options = env::var("ASAN_OPTIONS").unwrap_or_default();
let asan_options = format!("detect_odr_violation=0:{}", asan_options);
let tsan_options = env::var("TSAN_OPTIONS").unwrap_or_default();
let tsan_options = format!("report_signal_unsafe=0:{}", tsan_options);
let mut rustflags = format!(
"--cfg fuzzing \
-C debug-assertions \
-C overflow_checks \
-C passes=sancov \
-C llvm-args=-sanitizer-coverage-level=3 \
-C llvm-args=-sanitizer-coverage-trace-pc-guard \
-C llvm-args=-sanitizer-coverage-prune-blocks=0 \
-C opt-level=3 \
-C target-cpu=native \
-C debuginfo=0 \
-l afl-llvm-rt \
-L {} ",
common::afl_llvm_rt_dir().display()
);
// RUSTFLAGS are not used by rustdoc, instead RUSTDOCFLAGS are used. Since
// doctests will try to link against afl-llvm-rt, set up RUSTDOCFLAGS to
// have doctests built the same as other code to avoid issues with doctests.
let mut rustdocflags = format!(
"--cfg fuzzing \
-C debug-assertions \
-C overflow_checks \
-C passes=sancov \
-C llvm-args=-sanitizer-coverage-level=3 \
-C llvm-args=-sanitizer-coverage-trace-pc-guard \
-C llvm-args=-sanitizer-coverage-prune-blocks=0 \
-C opt-level=3 \
-C target-cpu=native \
-C debuginfo=0 \
-L {} ",
common::afl_llvm_rt_dir().display()
);
if cfg!(linux) {
// work around https://github.com/rust-fuzz/afl.rs/issues/141 /
// https://github.com/rust-lang/rust/issues/53945, can be removed once
// those are fixed.
rustflags.push_str("-Clink-arg=-fuse-ld=gold");
rustdocflags.push_str("-Clink-arg=-fuse-ld=gold");
}
// add user provided flags
rustflags.push_str(&env::var("RUSTFLAGS").unwrap_or_default());
rustdocflags.push_str(&env::var("RUSTDOCFLAGS").unwrap_or_default());
let status = Command::new(cargo_path)
.args(args) // skip `cargo` and `afl`
.env("RUSTFLAGS", &rustflags)
.env("RUSTDOCFLAGS", &rustdocflags)
.env("ASAN_OPTIONS", asan_options)
.env("TSAN_OPTIONS", tsan_options)
.status()
.unwrap();
process::exit(status.code().unwrap_or(1));
}