-
Notifications
You must be signed in to change notification settings - Fork 105
/
cargo-afl.rs
160 lines (152 loc) Β· 7.28 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
#[macro_use]
extern crate clap;
extern crate rustc_version;
extern crate xdg;
use std::env;
use std::ffi::OsStr;
use std::process::{self, Command};
#[path = "../dirs.rs"]
mod dirs;
fn main() {
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").help("known bug! rewrite as '-- --help' to see AFL help"))
.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").help("known bug! rewrite as '-- --help' to see AFL help"))
.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").help("known bug! rewrite as '-- --help' to see AFL help"))
.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").help("known bug! rewrite as '-- --help' to see AFL help"))
.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").help("known bug! rewrite as '-- --help' to see AFL help"))
.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").help("known bug! rewrite as '-- --help' to see AFL help"))
.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").help("known bug! rewrite as '-- --help' to see AFL help"))
.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").help("known bug! rewrite as '-- --help' to see AFL help"))
.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 = dirs::afl().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");
let rustflags = &format!(
"-C llvm-args=-sanitizer-coverage-level=3 \
-C llvm-args=-sanitizer-coverage-trace-pc-guard \
-C passes=sancov \
-C panic=abort \
-l afl-llvm-rt \
-L {}",
dirs::afl_llvm_rt().display()
);
let status = Command::new(cargo_path)
.args(args) // skip `cargo` and `afl`
.env("RUSTFLAGS", rustflags)
.status()
.unwrap();
process::exit(status.code().unwrap_or(1));
}