blob: bbeb34a6f142540493d4c7ae47f3e1c4fe67c582 [file] [log] [blame]
Robert Swieckia88f96f2015-10-09 16:47:39 +02001/*
2
3 honggfuzz - cmdline parsing
4
5 -----------------------------------------
6
7 Copyright 2014 Google Inc. All Rights Reserved.
8
9 Licensed under the Apache License, Version 2.0 (the "License");
10 you may not use this file except in compliance with the License.
11 You may obtain a copy of the License at
12
13 http://www.apache.org/licenses/LICENSE-2.0
14
15 Unless required by applicable law or agreed to in writing, software
16 distributed under the License is distributed on an "AS IS" BASIS,
17 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 See the License for the specific language governing permissions and
19 limitations under the License.
20
21*/
22
23#include "cmdline.h"
24
25#include <ctype.h>
26#include <errno.h>
27#include <getopt.h>
Robert Swieckia88f96f2015-10-09 16:47:39 +020028#include <inttypes.h>
29#include <limits.h>
Robert Swieckia88f96f2015-10-09 16:47:39 +020030#include <stdlib.h>
31#include <stdio.h>
32#include <string.h>
Robert Swiecki3bfc33c2016-03-14 18:12:41 +010033#include <sys/queue.h>
Robert Swieckia88f96f2015-10-09 16:47:39 +020034#include <unistd.h>
35
36#include "common.h"
37#include "log.h"
Anestis Bechtsoudisbe0ac7b2015-12-26 15:38:47 +020038#include "files.h"
Robert Swiecki72d2bef2016-01-19 14:39:26 +010039#include "util.h"
Robert Swieckia88f96f2015-10-09 16:47:39 +020040
41struct custom_option {
42 struct option opt;
43 const char *descr;
44};
45
46static bool checkFor_FILE_PLACEHOLDER(char **args)
47{
48 for (int x = 0; args[x]; x++) {
49 if (strstr(args[x], _HF_FILE_PLACEHOLDER))
50 return true;
51 }
52 return false;
53}
54
55static const char *cmdlineYesNo(bool yes)
56{
57 return (yes ? "true" : "false");
58}
59
60static void cmdlineHelp(const char *pname, struct custom_option *opts)
61{
62 LOG_HELP_BOLD("Usage: %s [options] -- path_to_command [args]", pname);
63 LOG_HELP_BOLD("Options:");
64 for (int i = 0; opts[i].opt.name; i++) {
Robert Swieckif3a5f6a2016-03-16 14:47:30 +010065 if (isprint(opts[i].opt.val) && opts[i].opt.val < 0x80) {
Robert Swieckia88f96f2015-10-09 16:47:39 +020066 LOG_HELP_BOLD(" --%s%s%c %s", opts[i].opt.name,
67 "|-", opts[i].opt.val,
Robert Swieckibf063db2016-02-16 18:42:00 +010068 opts[i].opt.has_arg == required_argument ? "VALUE" : "");
Robert Swieckia88f96f2015-10-09 16:47:39 +020069 } else {
70 LOG_HELP_BOLD(" --%s %s", opts[i].opt.name,
Robert Swieckibf063db2016-02-16 18:42:00 +010071 opts[i].opt.has_arg == required_argument ? "VALUE" : "");
Robert Swieckia88f96f2015-10-09 16:47:39 +020072 }
73 LOG_HELP("\t%s", opts[i].descr);
74 }
Jagger32127372015-10-09 23:07:38 +020075 LOG_HELP_BOLD("\nExamples:");
76 LOG_HELP(" Run the binary over a mutated file chosen from the directory");
77 LOG_HELP_BOLD(" " PROG_NAME " -f input_dir -- /usr/bin/tiffinfo -D " _HF_FILE_PLACEHOLDER);
78 LOG_HELP(" As above, provide input over STDIN:");
Robert Swiecki72d2bef2016-01-19 14:39:26 +010079 LOG_HELP_BOLD(" " PROG_NAME " -f input_dir -s -- /usr/bin/djpeg");
Jaggerba92b4b2016-03-16 02:24:17 +010080 LOG_HELP(" Use SANCOV to maximize code coverage:");
81 LOG_HELP_BOLD(" " PROG_NAME " -f input_dir -C -- /usr/bin/tiffinfo -D " _HF_FILE_PLACEHOLDER);
Jaggere848cc72016-09-19 02:28:52 +020082 LOG_HELP(" Use compile-time instrumentation (libhfuzz/instrument.c):");
83 LOG_HELP_BOLD(" " PROG_NAME " -f input_dir -z -- /usr/bin/tiffinfo -D " _HF_FILE_PLACEHOLDER);
Robert Swieckia2d5be32016-10-11 22:52:42 +020084 LOG_HELP(" Use persistent mode (libhfuzz/persistent.c):");
Jaggere848cc72016-09-19 02:28:52 +020085 LOG_HELP_BOLD(" " PROG_NAME " -f input_dir -P -- /usr/bin/tiffinfo_persistent");
Robert Swiecki43f0b282016-11-01 18:06:40 +010086 LOG_HELP
87 (" Use persistent mode (libhfuzz/persistent.c) and compile-time instrumentation (libhfuzz/instrument.c):");
Robert Swiecki14129492016-11-01 18:04:32 +010088 LOG_HELP_BOLD(" " PROG_NAME " -f input_dir -P -z -- /usr/bin/tiffinfo_persistent");
Robert Swieckia88f96f2015-10-09 16:47:39 +020089#if defined(_HF_ARCH_LINUX)
Jagger32127372015-10-09 23:07:38 +020090 LOG_HELP(" Run the binary over a dynamic file, maximize total no. of instructions:");
Jagger72f258b2015-10-09 23:09:01 +020091 LOG_HELP_BOLD(" " PROG_NAME " --linux_perf_instr -- /usr/bin/tiffinfo -D "
92 _HF_FILE_PLACEHOLDER);
Jagger32127372015-10-09 23:07:38 +020093 LOG_HELP(" Run the binary over a dynamic file, maximize total no. of branches:");
Jagger72f258b2015-10-09 23:09:01 +020094 LOG_HELP_BOLD(" " PROG_NAME " --linux_perf_branch -- /usr/bin/tiffinfo -D "
95 _HF_FILE_PLACEHOLDER);
Robert Swiecki349cb9e2016-10-14 21:36:42 +020096 LOG_HELP(" Run the binary over a dynamic file, maximize unique code blocks via BTS:");
97 LOG_HELP_BOLD(" " PROG_NAME " --linux_perf_bts_block -- /usr/bin/tiffinfo -D "
98 _HF_FILE_PLACEHOLDER);
99 LOG_HELP(" Run the binary over a dynamic file, maximize unique branches (edges) via BTS:");
Robert Swiecki43f0b282016-11-01 18:06:40 +0100100 LOG_HELP_BOLD(" " PROG_NAME " --linux_perf_bts_edge -- /usr/bin/tiffinfo -D "
101 _HF_FILE_PLACEHOLDER);
Robert Swiecki349cb9e2016-10-14 21:36:42 +0200102 LOG_HELP
Robert Swiecki8499c662016-11-01 18:02:52 +0100103 (" Run the binary over a dynamic file, maximize unique code blocks via Intel Processor Trace (requires libipt.so):");
Robert Swiecki349cb9e2016-10-14 21:36:42 +0200104 LOG_HELP_BOLD(" " PROG_NAME " --linux_perf_ipt_block -- /usr/bin/tiffinfo -D "
Jagger72f258b2015-10-09 23:09:01 +0200105 _HF_FILE_PLACEHOLDER);
Robert Swieckia88f96f2015-10-09 16:47:39 +0200106#endif /* defined(_HF_ARCH_LINUX) */
Robert Swieckia88f96f2015-10-09 16:47:39 +0200107}
108
109static void cmdlineUsage(const char *pname, struct custom_option *opts)
110{
111 cmdlineHelp(pname, opts);
112 exit(0);
113}
114
Robert Swieckia88f96f2015-10-09 16:47:39 +0200115rlim_t cmdlineParseRLimit(int res, const char *optarg, unsigned long mul)
116{
117 struct rlimit cur;
118 if (getrlimit(res, &cur) == -1) {
119 PLOG_F("getrlimit(%d)", res);
120 }
121 if (strcasecmp(optarg, "max") == 0) {
122 return cur.rlim_max;
123 }
124 if (strcasecmp(optarg, "def") == 0) {
125 return cur.rlim_cur;
126 }
Anestis Bechtsoudis413cb132016-02-07 12:59:00 +0200127 if (util_isANumber(optarg) == false) {
Robert Swieckia88f96f2015-10-09 16:47:39 +0200128 LOG_F("RLIMIT %d needs a numeric or 'max'/'def' value ('%s' provided)", res, optarg);
129 }
130 rlim_t val = strtoul(optarg, NULL, 0) * mul;
Jagger2bd61b72015-10-10 05:23:32 +0200131 if ((unsigned long)val == ULONG_MAX && errno != 0) {
Robert Swieckia88f96f2015-10-09 16:47:39 +0200132 PLOG_F("strtoul('%s', 0)", optarg);
133 }
134 return val;
135}
136
137bool cmdlineParse(int argc, char *argv[], honggfuzz_t * hfuzz)
138{
139 /* *INDENT-OFF* */
140 (*hfuzz) = (honggfuzz_t) {
141 .cmdline = NULL,
Robert Swiecki72d2bef2016-01-19 14:39:26 +0100142 .cmdline_txt[0] = '\0',
Jagger1b2d4822016-09-25 16:19:45 +0200143 .inputDir = NULL,
Robert Swieckia88f96f2015-10-09 16:47:39 +0200144 .nullifyStdio = false,
Robert Swieckia88f96f2015-10-09 16:47:39 +0200145 .fuzzStdin = false,
146 .saveUnique = true,
Robert Swiecki6c9f6822016-03-14 16:12:27 +0100147 .useScreen = true,
148 .useVerifier = false,
149 .timeStart = time(NULL),
Robert Swieckia88f96f2015-10-09 16:47:39 +0200150 .fileExtn = "fuzz",
151 .workDir = ".",
Jagger1b2d4822016-09-25 16:19:45 +0200152 .covDir = NULL,
Robert Swieckia96d78d2016-03-14 16:50:50 +0100153 .origFlipRate = 0.001f,
Robert Swieckia88f96f2015-10-09 16:47:39 +0200154 .externalCommand = NULL,
Robert Swieckiee266ac2016-10-03 02:25:59 +0200155 .postExternalCommand = NULL,
Robert Swieckia88f96f2015-10-09 16:47:39 +0200156 .blacklistFile = NULL,
157 .blacklistCnt = 0,
158 .blacklist = NULL,
Jaggerf4a60562016-09-25 15:40:23 +0200159 .maxFileSz = 0UL,
Jaggerba92b4b2016-03-16 02:24:17 +0100160 .tmOut = 10,
Robert Swieckia88f96f2015-10-09 16:47:39 +0200161 .mutationsMax = 0,
162 .threadsFinished = 0,
Jagger2664f542016-09-28 14:37:00 +0200163 .threadsMax = (sysconf(_SC_NPROCESSORS_ONLN) <= 1) ? 1 : sysconf(_SC_NPROCESSORS_ONLN) / 2,
Robert Swieckia88f96f2015-10-09 16:47:39 +0200164 .reportFile = NULL,
165 .asLimit = 0ULL,
Robert Swieckia88f96f2015-10-09 16:47:39 +0200166 .fileCnt = 0,
Robert Swiecki05354ca2016-03-15 19:10:23 +0100167 .lastFileIndex = 0,
168 .doneFileIndex = 0,
Jagger80041fe2016-03-10 21:32:35 +0100169 .clearEnv = false,
Jaggerab26e702016-03-22 04:28:00 +0100170 .envs = {
171 [0 ... (ARRAYSIZE(hfuzz->envs) - 1)] = NULL,
172 },
Robert Swiecki0f937af2016-03-30 18:19:16 +0200173 .persistent = false,
Robert Swieckia88f96f2015-10-09 16:47:39 +0200174
Robert Swiecki531438a2016-09-13 19:05:11 +0200175 .dictionaryFile = NULL,
176 .dictionaryCnt = 0,
177
Robert Swiecki6c9f6822016-03-14 16:12:27 +0100178 .state = _HF_STATE_UNSET,
Jaggerb7fa3ee2016-08-21 19:46:26 +0200179 .feedback = NULL,
Robert Swieckibc7532e2016-08-20 00:34:17 +0200180 .bbFd = -1,
Robert Swieckie586c1f2016-03-14 18:46:03 +0100181 .dynfileq_mutex = PTHREAD_MUTEX_INITIALIZER,
Robert Swiecki3bfc33c2016-03-14 18:12:41 +0100182 .dynfileqCnt = 0U,
Robert Swiecki6c9f6822016-03-14 16:12:27 +0100183
Robert Swieckia88f96f2015-10-09 16:47:39 +0200184 .mutationsCnt = 0,
185 .crashesCnt = 0,
186 .uniqueCrashesCnt = 0,
Anestis Bechtsoudis79b799e2015-11-01 00:02:25 +0200187 .verifiedCrashesCnt = 0,
Robert Swieckia88f96f2015-10-09 16:47:39 +0200188 .blCrashesCnt = 0,
189 .timeoutedCnt = 0,
190
Robert Swiecki6c9f6822016-03-14 16:12:27 +0100191 .dynFileMethod = _HF_DYNFILE_NONE,
Anestis Bechtsoudisbe0ac7b2015-12-26 15:38:47 +0200192 .sanCovCnts = {
Jaggerab26e702016-03-22 04:28:00 +0100193 .hitBBCnt = 0ULL,
194 .totalBBCnt = 0ULL,
195 .dsoCnt = 0ULL,
196 .iDsoCnt = 0ULL,
197 .newBBCnt = 0ULL,
198 .crashesCnt = 0ULL,
199 },
Robert Swieckia88f96f2015-10-09 16:47:39 +0200200
Anestis Bechtsoudisa16f70f2016-01-03 13:03:21 +0200201 .sanCov_mutex = PTHREAD_MUTEX_INITIALIZER,
Robert Swiecki23ec02a2016-01-19 18:47:45 +0100202 .sanOpts = {
Jaggerab26e702016-03-22 04:28:00 +0100203 .asanOpts = NULL,
204 .msanOpts = NULL,
205 .ubsanOpts = NULL,
Anestis Bechtsoudis61b5ab12016-01-08 16:07:02 +0200206 },
Robert Swiecki6c9f6822016-03-14 16:12:27 +0100207 .useSanCov = false,
Robert Swiecki6c9f6822016-03-14 16:12:27 +0100208 .covMetadata = NULL,
Jagger247c3b42016-03-21 23:24:05 +0100209 .msanReportUMRS = false,
Robert Swiecki6c9f6822016-03-14 16:12:27 +0100210
Haris Andrianakisc9a71332016-05-09 21:56:30 -0700211 .report_mutex = PTHREAD_MUTEX_INITIALIZER,
212
Robert Swiecki6c9f6822016-03-14 16:12:27 +0100213 /* Linux code */
Robert Swieckifab69162016-03-31 15:41:36 +0200214 .linux = {
Jaggerab26e702016-03-22 04:28:00 +0100215 .hwCnts = {
216 .cpuInstrCnt = 0ULL,
217 .cpuBranchCnt = 0ULL,
Jaggerab26e702016-03-22 04:28:00 +0100218 .bbCnt = 0ULL,
219 .newBBCnt = 0ULL,
Jagger34789a72016-09-08 00:36:09 +0200220 .softCntPc = 0ULL,
221 .softCntCmp = 0ULL,
Jaggerab26e702016-03-22 04:28:00 +0100222 },
223 .dynamicCutOffAddr = ~(0ULL),
224 .disableRandomization = true,
225 .ignoreAddr = NULL,
226 .numMajorFrames = 7,
227 .pid = 0,
228 .pidFile = NULL,
229 .pidCmd = NULL,
Anestis Bechtsoudisba68b382016-10-29 20:44:15 +0300230 .symsBlFile = NULL,
231 .symsBlCnt = 0,
232 .symsBl = NULL,
233 .symsWlFile = NULL,
234 .symsWlCnt = 0,
235 .symsWl = NULL,
Jaggerab26e702016-03-22 04:28:00 +0100236 },
Robert Swieckia88f96f2015-10-09 16:47:39 +0200237 };
238 /* *INDENT-ON* */
239
Robert Swiecki3bfc33c2016-03-14 18:12:41 +0100240 TAILQ_INIT(&hfuzz->dynfileq);
Robert Swiecki3a572262016-10-04 01:48:34 +0200241 TAILQ_INIT(&hfuzz->dictq);
Robert Swieckie8f8e8d2016-10-03 23:51:32 +0200242 TAILQ_INIT(&hfuzz->fileq);
Robert Swiecki3bfc33c2016-03-14 18:12:41 +0100243
Robert Swieckia88f96f2015-10-09 16:47:39 +0200244 /* *INDENT-OFF* */
245 struct custom_option custom_opts[] = {
246 {{"help", no_argument, NULL, 'h'}, "Help plz.."},
Jagger1b2d4822016-09-25 16:19:45 +0200247 {{"input", required_argument, NULL, 'f'}, "Path to a directory containing initial file corpus"},
Robert Swieckia88f96f2015-10-09 16:47:39 +0200248 {{"nullify_stdio", no_argument, NULL, 'q'}, "Null-ify children's stdin, stdout, stderr; make them quiet"},
Jagger54c96a22016-03-30 23:13:30 +0200249 {{"timeout", required_argument, NULL, 't'}, "Timeout in seconds (default: '10')"},
Jagger2664f542016-09-28 14:37:00 +0200250 {{"threads", required_argument, NULL, 'n'}, "Number of concurrent fuzzing threads (default: number of CPUs / 2)"},
Robert Swieckia88f96f2015-10-09 16:47:39 +0200251 {{"stdin_input", no_argument, NULL, 's'}, "Provide fuzzing input on STDIN, instead of ___FILE___"},
Jagger54c96a22016-03-30 23:13:30 +0200252 {{"mutation_rate", required_argument, NULL, 'r'}, "Maximal mutation rate in relation to the file size, (default: '0.001')"},
Robert Swiecki03ef5312015-10-09 18:25:40 +0200253 {{"logfile", required_argument, NULL, 'l'}, "Log file"},
Robert Swieckia88f96f2015-10-09 16:47:39 +0200254 {{"verbose", no_argument, NULL, 'v'}, "Disable ANSI console; use simple log output"},
Jagger5ff08ac2016-02-09 22:15:58 +0100255 {{"verifier", no_argument, NULL, 'V'}, "Enable crashes verifier"},
Robert Swieckia88f96f2015-10-09 16:47:39 +0200256 {{"debug_level", required_argument, NULL, 'd'}, "Debug level (0 - FATAL ... 4 - DEBUG), (default: '3' [INFO])"},
257 {{"extension", required_argument, NULL, 'e'}, "Input file extension (e.g. 'swf'), (default: 'fuzz')"},
Anestis Bechtsoudisbfcba122016-04-28 10:55:20 +0300258 {{"workspace", required_argument, NULL, 'W'}, "Workspace directory to save crashes & runtime files (default: '.')"},
Robert Swieckid32018f2016-10-11 22:58:22 +0200259 {{"covdir", required_argument, NULL, 0x103}, "New coverage is written to a separate directory (default: use the input directory)"},
Robert Swieckia88f96f2015-10-09 16:47:39 +0200260 {{"wordlist", required_argument, NULL, 'w'}, "Wordlist file (tokens delimited by NUL-bytes)"},
261 {{"stackhash_bl", required_argument, NULL, 'B'}, "Stackhashes blacklist file (one entry per line)"},
Robert Swieckiee266ac2016-10-03 02:25:59 +0200262 {{"mutate_cmd", required_argument, NULL, 'c'}, "External command producing fuzz files (instead of internal mutators)"},
263 {{"pprocess_cmd", required_argument, NULL, 0x104}, "External command postprocessing files produced by internal mutators"},
Robert Swieckia88f96f2015-10-09 16:47:39 +0200264 {{"iterations", required_argument, NULL, 'N'}, "Number of fuzzing iterations (default: '0' [no limit])"},
Robert Swiecki03ef5312015-10-09 18:25:40 +0200265 {{"rlimit_as", required_argument, NULL, 0x100}, "Per process memory limit in MiB (default: '0' [no limit])"},
dyjakanebfd24e2015-10-16 19:24:32 +0100266 {{"report", required_argument, NULL, 'R'}, "Write report to this file (default: '" _HF_REPORT_FILE "')"},
Robert Swieckia88f96f2015-10-09 16:47:39 +0200267 {{"max_file_size", required_argument, NULL, 'F'}, "Maximal size of files processed by the fuzzer in bytes (default: '1048576')"},
Jagger80041fe2016-03-10 21:32:35 +0100268 {{"clear_env", no_argument, NULL, 0x101}, "Clear all environment variables before executing the binary"},
Robert Swieckia88f96f2015-10-09 16:47:39 +0200269 {{"env", required_argument, NULL, 'E'}, "Pass this environment variable, can be used multiple times"},
Jagger54c96a22016-03-30 23:13:30 +0200270 {{"save_all", no_argument, NULL, 'u'}, "Save all test-cases (not only the unique ones) by appending the current time-stamp to the filenames"},
Jagger3db1d952016-03-10 02:02:46 +0100271 {{"sancov", no_argument, NULL, 'C'}, "Enable sanitizer coverage feedback"},
Robert Swieckif05b1cd2016-11-01 18:05:45 +0100272 {{"instrument", no_argument, NULL, 'z'}, "Enable compile-time instrumentation (link with libhfuzz/libhfuzz.a)"},
Jagger7ff92fa2016-03-22 04:13:50 +0100273 {{"msan_report_umrs", no_argument, NULL, 0x102}, "Report MSAN's UMRS (uninitialized memory access)"},
Robert Swieckif05b1cd2016-11-01 18:05:45 +0100274 {{"persistent", no_argument, NULL, 'P'}, "Enable persistent fuzzing (link with libhfuzz/libhfuzz.a)"},
Robert Swieckia88f96f2015-10-09 16:47:39 +0200275
276#if defined(_HF_ARCH_LINUX)
Anestis Bechtsoudisfafb3332016-10-31 08:57:16 +0200277 {{"linux_symbols_bl", required_argument, NULL, 0x504}, "Symbols blacklist filter file (one entry per line)"},
278 {{"linux_symbols_wl", required_argument, NULL, 0x505}, "Symbols whitelist filter file (one entry per line)"},
Anestis Bechtsoudis413cb132016-02-07 12:59:00 +0200279 {{"linux_pid", required_argument, NULL, 'p'}, "Attach to a pid (and its thread group)"},
Robert Swieckifab69162016-03-31 15:41:36 +0200280 {{"linux_file_pid", required_argument, NULL, 0x502}, "Attach to pid (and its thread group) read from file"},
Robert Swieckia88f96f2015-10-09 16:47:39 +0200281 {{"linux_addr_low_limit", required_argument, NULL, 0x500}, "Address limit (from si.si_addr) below which crashes are not reported, (default: '0')"},
282 {{"linux_keep_aslr", no_argument, NULL, 0x501}, "Don't disable ASLR randomization, might be useful with MSAN"},
Jaggerae6a4452015-10-14 17:34:43 +0200283 {{"linux_perf_ignore_above", required_argument, NULL, 0x503}, "Ignore perf events which report IPs above this address"},
Robert Swieckia88f96f2015-10-09 16:47:39 +0200284 {{"linux_perf_instr", no_argument, NULL, 0x510}, "Use PERF_COUNT_HW_INSTRUCTIONS perf"},
285 {{"linux_perf_branch", no_argument, NULL, 0x511}, "Use PERF_COUNT_HW_BRANCH_INSTRUCTIONS perf"},
Jagger39bd2b02016-02-04 01:16:15 +0100286 {{"linux_perf_bts_block", no_argument, NULL, 0x512}, "Use Intel BTS to count unique blocks"},
287 {{"linux_perf_bts_edge", no_argument, NULL, 0x513}, "Use Intel BTS to count unique edges"},
Robert Swiecki8499c662016-11-01 18:02:52 +0100288 {{"linux_perf_ipt_block", no_argument, NULL, 0x514}, "Use Intel Processor Trace to count unique blocks (requires libipt.so)"},
Robert Swieckia88f96f2015-10-09 16:47:39 +0200289#endif // defined(_HF_ARCH_LINUX)
290 {{0, 0, 0, 0}, NULL},
291 };
292 /* *INDENT-ON* */
293
294 struct option opts[ARRAYSIZE(custom_opts)];
295 for (unsigned i = 0; i < ARRAYSIZE(custom_opts); i++) {
296 opts[i] = custom_opts[i].opt;
297 }
298
299 enum llevel_t ll = INFO;
300 const char *logfile = NULL;
301 int opt_index = 0;
302 for (;;) {
Jagger4aac9fe2016-08-28 17:35:48 +0200303 int c = getopt_long(argc, argv, "-?hqvVsuPf:d:e:W:r:c:F:t:R:n:N:l:p:g:E:w:B:Cz", opts,
Robert Swieckia88f96f2015-10-09 16:47:39 +0200304 &opt_index);
305 if (c < 0)
306 break;
307
308 switch (c) {
309 case 'h':
310 case '?':
311 cmdlineUsage(argv[0], custom_opts);
312 break;
313 case 'f':
Jagger1b2d4822016-09-25 16:19:45 +0200314 hfuzz->inputDir = optarg;
Robert Swieckia88f96f2015-10-09 16:47:39 +0200315 break;
316 case 'q':
317 hfuzz->nullifyStdio = true;
318 break;
319 case 'v':
320 hfuzz->useScreen = false;
321 break;
Anestis Bechtsoudis0cde66f2015-10-11 19:37:11 -0700322 case 'V':
323 hfuzz->useVerifier = true;
324 break;
Robert Swieckia88f96f2015-10-09 16:47:39 +0200325 case 's':
326 hfuzz->fuzzStdin = true;
327 break;
328 case 'u':
329 hfuzz->saveUnique = false;
330 break;
Robert Swiecki03ef5312015-10-09 18:25:40 +0200331 case 'l':
332 logfile = optarg;
333 break;
Robert Swieckia88f96f2015-10-09 16:47:39 +0200334 case 'd':
335 ll = atoi(optarg);
336 break;
337 case 'e':
338 hfuzz->fileExtn = optarg;
339 break;
340 case 'W':
341 hfuzz->workDir = optarg;
342 break;
343 case 'r':
Robert Swieckia96d78d2016-03-14 16:50:50 +0100344 hfuzz->origFlipRate = strtod(optarg, NULL);
Robert Swieckia88f96f2015-10-09 16:47:39 +0200345 break;
346 case 'c':
347 hfuzz->externalCommand = optarg;
348 break;
Anestis Bechtsoudisbe0ac7b2015-12-26 15:38:47 +0200349 case 'C':
350 hfuzz->useSanCov = true;
351 break;
Jagger4aac9fe2016-08-28 17:35:48 +0200352 case 'z':
353 hfuzz->dynFileMethod |= _HF_DYNFILE_SOFT;
354 break;
Robert Swieckia88f96f2015-10-09 16:47:39 +0200355 case 'F':
356 hfuzz->maxFileSz = strtoul(optarg, NULL, 0);
357 break;
358 case 't':
359 hfuzz->tmOut = atol(optarg);
360 break;
361 case 'R':
362 hfuzz->reportFile = optarg;
363 break;
364 case 'n':
365 hfuzz->threadsMax = atol(optarg);
366 break;
367 case 'N':
368 hfuzz->mutationsMax = atol(optarg);
369 break;
Robert Swiecki03ef5312015-10-09 18:25:40 +0200370 case 0x100:
Robert Swieckia88f96f2015-10-09 16:47:39 +0200371 hfuzz->asLimit = strtoull(optarg, NULL, 0);
372 break;
Jagger80041fe2016-03-10 21:32:35 +0100373 case 0x101:
374 hfuzz->clearEnv = true;
375 break;
Jagger7ff92fa2016-03-22 04:13:50 +0100376 case 0x102:
377 hfuzz->msanReportUMRS = true;
378 break;
Robert Swieckidac8cf12016-09-22 15:36:28 +0200379 case 0x103:
Jagger1b2d4822016-09-25 16:19:45 +0200380 hfuzz->covDir = optarg;
Robert Swieckidac8cf12016-09-22 15:36:28 +0200381 break;
Robert Swieckiee266ac2016-10-03 02:25:59 +0200382 case 0x104:
383 hfuzz->postExternalCommand = optarg;
384 break;
Robert Swieckifab69162016-03-31 15:41:36 +0200385 case 'P':
Robert Swiecki0f937af2016-03-30 18:19:16 +0200386 hfuzz->persistent = true;
387 break;
Robert Swieckia88f96f2015-10-09 16:47:39 +0200388 case 'p':
Anestis Bechtsoudis413cb132016-02-07 12:59:00 +0200389 if (util_isANumber(optarg) == false) {
Jagger9c4d1622015-10-16 01:40:17 +0200390 LOG_E("-p '%s' is not a number", optarg);
391 return false;
392 }
Jagger247c3b42016-03-21 23:24:05 +0100393 hfuzz->linux.pid = atoi(optarg);
394 if (hfuzz->linux.pid < 1) {
395 LOG_E("-p '%d' is invalid", hfuzz->linux.pid);
Jagger9c4d1622015-10-16 01:40:17 +0200396 return false;
397 }
Robert Swieckia88f96f2015-10-09 16:47:39 +0200398 break;
Robert Swieckifab69162016-03-31 15:41:36 +0200399 case 0x502:
Jagger247c3b42016-03-21 23:24:05 +0100400 hfuzz->linux.pidFile = optarg;
Anestis Bechtsoudis413cb132016-02-07 12:59:00 +0200401 break;
Robert Swieckia88f96f2015-10-09 16:47:39 +0200402 case 'E':
403 for (size_t i = 0; i < ARRAYSIZE(hfuzz->envs); i++) {
404 if (hfuzz->envs[i] == NULL) {
405 hfuzz->envs[i] = optarg;
406 break;
407 }
408 }
409 break;
410 case 'w':
411 hfuzz->dictionaryFile = optarg;
412 break;
413 case 'B':
414 hfuzz->blacklistFile = optarg;
415 break;
416 case 0x500:
Jagger247c3b42016-03-21 23:24:05 +0100417 hfuzz->linux.ignoreAddr = (void *)strtoul(optarg, NULL, 0);
Robert Swieckia88f96f2015-10-09 16:47:39 +0200418 break;
419 case 0x501:
Jagger247c3b42016-03-21 23:24:05 +0100420 hfuzz->linux.disableRandomization = false;
Robert Swieckia88f96f2015-10-09 16:47:39 +0200421 break;
Robert Swieckia88f96f2015-10-09 16:47:39 +0200422 case 0x503:
Jagger247c3b42016-03-21 23:24:05 +0100423 hfuzz->linux.dynamicCutOffAddr = strtoull(optarg, NULL, 0);
Robert Swieckia88f96f2015-10-09 16:47:39 +0200424 break;
Anestis Bechtsoudisba68b382016-10-29 20:44:15 +0300425 case 0x504:
426 hfuzz->linux.symsBlFile = optarg;
427 break;
428 case 0x505:
429 hfuzz->linux.symsWlFile = optarg;
430 break;
Robert Swieckia88f96f2015-10-09 16:47:39 +0200431 case 0x510:
432 hfuzz->dynFileMethod |= _HF_DYNFILE_INSTR_COUNT;
433 break;
434 case 0x511:
435 hfuzz->dynFileMethod |= _HF_DYNFILE_BRANCH_COUNT;
436 break;
437 case 0x512:
Jagger3abc5602016-02-04 00:53:43 +0100438 hfuzz->dynFileMethod |= _HF_DYNFILE_BTS_BLOCK;
Robert Swieckia88f96f2015-10-09 16:47:39 +0200439 break;
440 case 0x513:
Jagger3abc5602016-02-04 00:53:43 +0100441 hfuzz->dynFileMethod |= _HF_DYNFILE_BTS_EDGE;
Robert Swieckia88f96f2015-10-09 16:47:39 +0200442 break;
443 case 0x514:
Jagger39bd2b02016-02-04 01:16:15 +0100444 hfuzz->dynFileMethod |= _HF_DYNFILE_IPT_BLOCK;
445 break;
Robert Swieckia88f96f2015-10-09 16:47:39 +0200446 default:
447 cmdlineUsage(argv[0], custom_opts);
448 return false;
449 break;
450 }
451 }
Jagger72f258b2015-10-09 23:09:01 +0200452
Robert Swieckia88f96f2015-10-09 16:47:39 +0200453 if (logInitLogFile(logfile, ll) == false) {
454 return false;
455 }
456
457 hfuzz->cmdline = &argv[optind];
458 if (hfuzz->cmdline[0] == NULL) {
459 LOG_E("No fuzz command provided");
460 cmdlineUsage(argv[0], custom_opts);
461 return false;
462 }
463
Robert Swiecki0f937af2016-03-30 18:19:16 +0200464 if (!hfuzz->fuzzStdin && !hfuzz->persistent && !checkFor_FILE_PLACEHOLDER(hfuzz->cmdline)) {
Robert Swieckia88f96f2015-10-09 16:47:39 +0200465 LOG_E("You must specify '" _HF_FILE_PLACEHOLDER
Robert Swiecki0f937af2016-03-30 18:19:16 +0200466 "' when the -s (stdin fuzzing) or --persistent options are not set");
Robert Swieckia88f96f2015-10-09 16:47:39 +0200467 return false;
468 }
469
Robert Swiecki71b73722016-09-05 15:18:25 +0200470 if (hfuzz->threadsMax >= _HF_THREAD_MAX) {
471 LOG_E("Too many fuzzing threads specified %zu (>= _HF_THREAD_MAX (%u))", hfuzz->threadsMax,
472 _HF_THREAD_MAX);
473 return false;
474 }
475
Robert Swieckia88f96f2015-10-09 16:47:39 +0200476 if (strchr(hfuzz->fileExtn, '/')) {
477 LOG_E("The file extension contains the '/' character: '%s'", hfuzz->fileExtn);
478 return false;
479 }
Anestis Bechtsoudisbe0ac7b2015-12-26 15:38:47 +0200480
Anestis Bechtsoudis8f4aa612015-12-27 12:06:19 +0200481 if (hfuzz->workDir[0] != '.' || strlen(hfuzz->workDir) > 2) {
Anestis Bechtsoudisc8e7f6e2015-12-26 14:48:48 +0200482 if (!files_exists(hfuzz->workDir)) {
483 LOG_E("Provided workspace directory '%s' doesn't exist", hfuzz->workDir);
484 return false;
485 }
486 }
Robert Swieckia88f96f2015-10-09 16:47:39 +0200487
Jagger247c3b42016-03-21 23:24:05 +0100488 if (hfuzz->linux.pid > 0 || hfuzz->linux.pidFile) {
489 LOG_I("PID=%d specified, lowering maximum number of concurrent threads to 1",
490 hfuzz->linux.pid);
Robert Swieckia88f96f2015-10-09 16:47:39 +0200491 hfuzz->threadsMax = 1;
492 }
493
Robert Swieckia96d78d2016-03-14 16:50:50 +0100494 if (hfuzz->origFlipRate == 0.0L && hfuzz->useVerifier) {
Anestis Bechtsoudisc8e7f6e2015-12-26 14:48:48 +0200495 LOG_I("Verifier enabled with 0.0 flipRate, activating dry run mode");
Anestis Bechtsoudis46ea10e2015-11-07 18:16:25 +0200496 }
497
Jagger1b2d4822016-09-25 16:19:45 +0200498 LOG_I("inputDir '%s', nullifyStdio: %s, fuzzStdin: %s, saveUnique: %s, flipRate: %lf, "
Jagger247c3b42016-03-21 23:24:05 +0100499 "externalCommand: '%s', tmOut: %ld, mutationsMax: %zu, threadsMax: %zu, fileExtn '%s', "
Robert Swieckia88f96f2015-10-09 16:47:39 +0200500 "memoryLimit: 0x%" PRIx64 "(MiB), fuzzExe: '%s', fuzzedPid: %d",
Jagger1b2d4822016-09-25 16:19:45 +0200501 hfuzz->inputDir,
Robert Swieckia88f96f2015-10-09 16:47:39 +0200502 cmdlineYesNo(hfuzz->nullifyStdio), cmdlineYesNo(hfuzz->fuzzStdin),
Robert Swieckia96d78d2016-03-14 16:50:50 +0100503 cmdlineYesNo(hfuzz->saveUnique), hfuzz->origFlipRate,
Robert Swieckia88f96f2015-10-09 16:47:39 +0200504 hfuzz->externalCommand == NULL ? "NULL" : hfuzz->externalCommand, hfuzz->tmOut,
Jagger247c3b42016-03-21 23:24:05 +0100505 hfuzz->mutationsMax, hfuzz->threadsMax, hfuzz->fileExtn,
506 hfuzz->asLimit, hfuzz->cmdline[0], hfuzz->linux.pid);
Robert Swieckia88f96f2015-10-09 16:47:39 +0200507
Robert Swiecki2aaa52b2016-01-19 14:40:47 +0100508 snprintf(hfuzz->cmdline_txt, sizeof(hfuzz->cmdline_txt), "%s", hfuzz->cmdline[0]);
Robert Swiecki72d2bef2016-01-19 14:39:26 +0100509 for (size_t i = 1; hfuzz->cmdline[i]; i++) {
510 util_ssnprintf(hfuzz->cmdline_txt, sizeof(hfuzz->cmdline_txt), " %s", hfuzz->cmdline[i]);
Robert Swieckif2d9c3a2016-11-03 02:13:54 +0100511 if (strlen(hfuzz->cmdline_txt) == (sizeof(hfuzz->cmdline_txt) - 1)) {
512 hfuzz->cmdline_txt[sizeof(hfuzz->cmdline_txt) - 3] = '.';
513 hfuzz->cmdline_txt[sizeof(hfuzz->cmdline_txt) - 2] = '.';
514 hfuzz->cmdline_txt[sizeof(hfuzz->cmdline_txt) - 1] = '.';
515 }
Robert Swiecki72d2bef2016-01-19 14:39:26 +0100516 }
517
Robert Swieckia88f96f2015-10-09 16:47:39 +0200518 return true;
519}