Skip to content

Commit dc05360

Browse files
committed
Pass command line options rather than env vars
1 parent d302008 commit dc05360

File tree

3 files changed

+100
-38
lines changed

3 files changed

+100
-38
lines changed

afl-rt.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ void init(int argc, const char **argv, char **envp, void *_unused,
209209
/*
210210
* This is a shared library. For this, we set up a dummy area so the
211211
* instrumentation does not crash during program initialization. The
212-
* main executable is repsonsible for setting up AFL proper.
212+
* main executable is responsible for setting up AFL proper.
213213
*/
214214
(void)mmap(AREA_BASE, AREA_SIZE,
215215
PROT_READ | PROT_WRITE,

e9AFLPlugin.cpp

+59-26
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#include <set>
3131
#include <vector>
3232

33+
#include <getopt.h>
34+
3335
#include "e9plugin.h"
3436

3537
using namespace e9tool;
@@ -47,7 +49,6 @@ enum Option
4749
OPTION_ALWAYS
4850
};
4951
static Option option_debug = OPTION_DEFAULT;
50-
static Option option_instrument = OPTION_DEFAULT;
5152
static Option option_Oselect = OPTION_DEFAULT;
5253
static Option option_Oblock = OPTION_DEFAULT;
5354

@@ -113,11 +114,68 @@ typedef std::map<intptr_t, unsigned> Ids;
113114
*/
114115
static std::set<intptr_t> instrument;
115116

117+
/*
118+
* Options.
119+
*/
120+
enum
121+
{
122+
OPTION_COUNTER,
123+
OPTION_OBLOCK,
124+
OPTION_OSELECT,
125+
OPTION_DEBUG,
126+
OPTION_PATH,
127+
};
128+
116129
/*
117130
* Initialization.
118131
*/
119132
extern void *e9_plugin_init_v1(const Context *cxt)
120133
{
134+
static const struct option long_options[] =
135+
{
136+
{"counter", required_argument, nullptr, OPTION_COUNTER},
137+
{"Oblock", required_argument, nullptr, OPTION_OBLOCK},
138+
{"Oselect", required_argument, nullptr, OPTION_OSELECT},
139+
{"debug", no_argument, nullptr, OPTION_DEBUG},
140+
{"path", required_argument, nullptr, OPTION_PATH},
141+
{nullptr, no_argument, nullptr, 0}
142+
};
143+
std::string option_path(".");
144+
Counter option_counter = COUNTER_CLASSIC;
145+
optind = 1;
146+
char * const *argv = cxt->argv->data();
147+
int argc = (int)cxt->argv->size();
148+
while (true)
149+
{
150+
int idx;
151+
int opt = getopt_long_only(argc, argv, "Po:v", long_options, &idx);
152+
if (opt < 0)
153+
break;
154+
switch (opt)
155+
{
156+
case OPTION_COUNTER:
157+
option_counter = parseCounter(optarg);
158+
break;
159+
case OPTION_OBLOCK:
160+
option_Oblock = parseOption(optarg);
161+
break;
162+
case OPTION_OSELECT:
163+
option_Oselect = parseOption(optarg);
164+
break;
165+
case OPTION_DEBUG:
166+
option_debug = OPTION_ALWAYS;
167+
break;
168+
case OPTION_PATH:
169+
option_path = optarg;
170+
break;
171+
default:
172+
error("invalid command-line options for %s", argv[0]);
173+
}
174+
}
175+
if (option_Oblock == OPTION_ALWAYS)
176+
warning("always removing AFL instrumentation for bad blocks; coverage "
177+
"may be incomplete");
178+
121179
// Make seed depend on filename.
122180
unsigned seed = 0;
123181
const char *filename = getELFFilename(cxt->elf);
@@ -132,28 +190,6 @@ extern void *e9_plugin_init_v1(const Context *cxt)
132190
// Reserve memory used by the afl_area_ptr:
133191
sendReserveMessage(cxt->out, afl_area_ptr, AREA_SIZE, /*absolute=*/true);
134192

135-
const char *str = nullptr;
136-
std::string option_path(".");
137-
Counter option_counter = COUNTER_CLASSIC;
138-
if ((str = getenv("E9AFL_COUNTER")) != nullptr)
139-
option_counter = parseCounter(str);
140-
if ((str = getenv("E9AFL_DEBUG")) != nullptr)
141-
option_debug = parseOption(str);
142-
if ((str = getenv("E9AFL_INSTRUMENT")) != nullptr)
143-
option_instrument = parseOption(str);
144-
if ((str = getenv("E9AFL_OBLOCK")) != nullptr)
145-
option_Oblock = parseOption(str);
146-
if ((str = getenv("E9AFL_OSELECT")) != nullptr)
147-
option_Oselect = parseOption(str);
148-
if ((str = getenv("E9AFL_PATH")) != nullptr)
149-
option_path = str;
150-
151-
if (option_instrument == OPTION_NEVER)
152-
return nullptr;
153-
if (option_Oblock == OPTION_ALWAYS)
154-
warning("always removing AFL instrumentation for bad blocks; coverage "
155-
"may be incomplete");
156-
157193
// Send the AFL runtime (if not shared object):
158194
std::string path(option_path);
159195
path += "/afl-rt";
@@ -635,9 +671,6 @@ extern intptr_t e9_plugin_match_v1(const Context *cxt)
635671
*/
636672
extern void e9_plugin_patch_v1(const Context *cxt, Phase phase)
637673
{
638-
if (option_instrument == OPTION_NEVER)
639-
return;
640-
641674
switch (phase)
642675
{
643676
case PHASE_CODE:

e9afl.cpp

+40-11
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,14 @@ int main(int argc, char **argv)
244244
// Setup environment:
245245
std::string path;
246246
getExePath(path);
247-
setenv("E9AFL_COUNTER", getCounter(option_counter), true);
248-
setenv("E9AFL_OBLOCK", getValue(option_Oblock), true);
249-
setenv("E9AFL_OSELECT", getValue(option_Oselect), true);
250-
setenv("E9AFL_DEBUG", (option_debug? "always": "default"), true);
251-
setenv("E9AFL_PATH", path.c_str(), true);
247+
std::string plugin;
248+
plugin += '\"';
249+
plugin += path;
250+
plugin += "/e9AFLPlugin.so\"";
251+
std::string plugin_opt;
252+
plugin_opt += "--plugin=";
253+
plugin_opt += plugin;
254+
plugin_opt += ':';
252255

253256
// Construct command:
254257
std::string command;
@@ -265,14 +268,40 @@ int main(int argc, char **argv)
265268
command += output;
266269
command += "\" ";
267270

268-
command += "-M 'plugin(\"";
269-
command += path;
270-
command += "/e9AFLPlugin.so\").match()' ";
271+
command += "-M 'plugin(";
272+
command += plugin;
273+
command += ").match()' ";
271274

272-
command += "-P 'plugin(\"";
273-
command += path;
274-
command += "/e9AFLPlugin.so\").patch()' ";
275+
command += "-P 'plugin(";
276+
command += plugin;
277+
command += ").patch()' ";
278+
279+
command += plugin_opt;
280+
command += "--counter=";
281+
command += getCounter(option_counter);
282+
command += ' ';
283+
284+
command += plugin_opt;
285+
command += "-Oblock=";
286+
command += getValue(option_Oblock);
287+
command += ' ';
288+
289+
command += plugin_opt;
290+
command += "-Oselect=";
291+
command += getValue(option_Oselect);
292+
command += ' ';
275293

294+
if (option_debug)
295+
{
296+
command += plugin_opt;
297+
command += "--debug ";
298+
}
299+
300+
command += plugin_opt;
301+
command += "--path='";
302+
command += path;
303+
command += "' ";
304+
276305
for (int i = optind+1; i < argc; i++)
277306
{
278307
command += '\'';

0 commit comments

Comments
 (0)