30
30
#include < set>
31
31
#include < vector>
32
32
33
+ #include < getopt.h>
34
+
33
35
#include " e9plugin.h"
34
36
35
37
using namespace e9tool ;
@@ -47,7 +49,6 @@ enum Option
47
49
OPTION_ALWAYS
48
50
};
49
51
static Option option_debug = OPTION_DEFAULT;
50
- static Option option_instrument = OPTION_DEFAULT;
51
52
static Option option_Oselect = OPTION_DEFAULT;
52
53
static Option option_Oblock = OPTION_DEFAULT;
53
54
@@ -113,11 +114,68 @@ typedef std::map<intptr_t, unsigned> Ids;
113
114
*/
114
115
static std::set<intptr_t > instrument;
115
116
117
+ /*
118
+ * Options.
119
+ */
120
+ enum
121
+ {
122
+ OPTION_COUNTER,
123
+ OPTION_OBLOCK,
124
+ OPTION_OSELECT,
125
+ OPTION_DEBUG,
126
+ OPTION_PATH,
127
+ };
128
+
116
129
/*
117
130
* Initialization.
118
131
*/
119
132
extern void *e9_plugin_init_v1 (const Context *cxt)
120
133
{
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
+
121
179
// Make seed depend on filename.
122
180
unsigned seed = 0 ;
123
181
const char *filename = getELFFilename (cxt->elf );
@@ -132,28 +190,6 @@ extern void *e9_plugin_init_v1(const Context *cxt)
132
190
// Reserve memory used by the afl_area_ptr:
133
191
sendReserveMessage (cxt->out , afl_area_ptr, AREA_SIZE, /* absolute=*/ true );
134
192
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
-
157
193
// Send the AFL runtime (if not shared object):
158
194
std::string path (option_path);
159
195
path += " /afl-rt" ;
@@ -635,9 +671,6 @@ extern intptr_t e9_plugin_match_v1(const Context *cxt)
635
671
*/
636
672
extern void e9_plugin_patch_v1 (const Context *cxt, Phase phase)
637
673
{
638
- if (option_instrument == OPTION_NEVER)
639
- return ;
640
-
641
674
switch (phase)
642
675
{
643
676
case PHASE_CODE:
0 commit comments