blob: 604122a74be53a40211691209a6b3b8c31174ff4 [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 Swiecki846ccd72017-01-12 17:52:23 +010030#if defined(_HF_ARCH_LINUX)
31#include <sched.h>
32#endif /* defined(_HF_ARCH_LINUX) */
33#include <signal.h>
Robert Swieckia88f96f2015-10-09 16:47:39 +020034#include <stdlib.h>
35#include <stdio.h>
36#include <string.h>
Robert Swiecki3bfc33c2016-03-14 18:12:41 +010037#include <sys/queue.h>
Robert Swieckia88f96f2015-10-09 16:47:39 +020038#include <unistd.h>
39
40#include "common.h"
41#include "log.h"
Anestis Bechtsoudisbe0ac7b2015-12-26 15:38:47 +020042#include "files.h"
Robert Swiecki72d2bef2016-01-19 14:39:26 +010043#include "util.h"
Robert Swieckia88f96f2015-10-09 16:47:39 +020044
45struct custom_option {
46 struct option opt;
47 const char *descr;
48};
49
50static bool checkFor_FILE_PLACEHOLDER(char **args)
51{
52 for (int x = 0; args[x]; x++) {
53 if (strstr(args[x], _HF_FILE_PLACEHOLDER))
54 return true;
55 }
56 return false;
57}
58
59static const char *cmdlineYesNo(bool yes)
60{
61 return (yes ? "true" : "false");
62}
63
64static void cmdlineHelp(const char *pname, struct custom_option *opts)
65{
66 LOG_HELP_BOLD("Usage: %s [options] -- path_to_command [args]", pname);
67 LOG_HELP_BOLD("Options:");
68 for (int i = 0; opts[i].opt.name; i++) {
Robert Swieckif3a5f6a2016-03-16 14:47:30 +010069 if (isprint(opts[i].opt.val) && opts[i].opt.val < 0x80) {
Robert Swieckia88f96f2015-10-09 16:47:39 +020070 LOG_HELP_BOLD(" --%s%s%c %s", opts[i].opt.name,
71 "|-", opts[i].opt.val,
Robert Swieckibf063db2016-02-16 18:42:00 +010072 opts[i].opt.has_arg == required_argument ? "VALUE" : "");
Robert Swieckia88f96f2015-10-09 16:47:39 +020073 } else {
74 LOG_HELP_BOLD(" --%s %s", opts[i].opt.name,
Robert Swieckibf063db2016-02-16 18:42:00 +010075 opts[i].opt.has_arg == required_argument ? "VALUE" : "");
Robert Swieckia88f96f2015-10-09 16:47:39 +020076 }
77 LOG_HELP("\t%s", opts[i].descr);
78 }
Jagger32127372015-10-09 23:07:38 +020079 LOG_HELP_BOLD("\nExamples:");
80 LOG_HELP(" Run the binary over a mutated file chosen from the directory");
81 LOG_HELP_BOLD(" " PROG_NAME " -f input_dir -- /usr/bin/tiffinfo -D " _HF_FILE_PLACEHOLDER);
82 LOG_HELP(" As above, provide input over STDIN:");
Robert Swiecki72d2bef2016-01-19 14:39:26 +010083 LOG_HELP_BOLD(" " PROG_NAME " -f input_dir -s -- /usr/bin/djpeg");
Jaggerba92b4b2016-03-16 02:24:17 +010084 LOG_HELP(" Use SANCOV to maximize code coverage:");
85 LOG_HELP_BOLD(" " PROG_NAME " -f input_dir -C -- /usr/bin/tiffinfo -D " _HF_FILE_PLACEHOLDER);
Jaggere848cc72016-09-19 02:28:52 +020086 LOG_HELP(" Use compile-time instrumentation (libhfuzz/instrument.c):");
87 LOG_HELP_BOLD(" " PROG_NAME " -f input_dir -z -- /usr/bin/tiffinfo -D " _HF_FILE_PLACEHOLDER);
Robert Swieckia2d5be32016-10-11 22:52:42 +020088 LOG_HELP(" Use persistent mode (libhfuzz/persistent.c):");
Jaggere848cc72016-09-19 02:28:52 +020089 LOG_HELP_BOLD(" " PROG_NAME " -f input_dir -P -- /usr/bin/tiffinfo_persistent");
Robert Swiecki43f0b282016-11-01 18:06:40 +010090 LOG_HELP
91 (" Use persistent mode (libhfuzz/persistent.c) and compile-time instrumentation (libhfuzz/instrument.c):");
Robert Swiecki14129492016-11-01 18:04:32 +010092 LOG_HELP_BOLD(" " PROG_NAME " -f input_dir -P -z -- /usr/bin/tiffinfo_persistent");
Robert Swieckia88f96f2015-10-09 16:47:39 +020093#if defined(_HF_ARCH_LINUX)
Jagger32127372015-10-09 23:07:38 +020094 LOG_HELP(" Run the binary over a dynamic file, maximize total no. of instructions:");
Jagger72f258b2015-10-09 23:09:01 +020095 LOG_HELP_BOLD(" " PROG_NAME " --linux_perf_instr -- /usr/bin/tiffinfo -D "
96 _HF_FILE_PLACEHOLDER);
Jagger32127372015-10-09 23:07:38 +020097 LOG_HELP(" Run the binary over a dynamic file, maximize total no. of branches:");
Jagger72f258b2015-10-09 23:09:01 +020098 LOG_HELP_BOLD(" " PROG_NAME " --linux_perf_branch -- /usr/bin/tiffinfo -D "
99 _HF_FILE_PLACEHOLDER);
Robert Swiecki349cb9e2016-10-14 21:36:42 +0200100 LOG_HELP(" Run the binary over a dynamic file, maximize unique code blocks via BTS:");
101 LOG_HELP_BOLD(" " PROG_NAME " --linux_perf_bts_block -- /usr/bin/tiffinfo -D "
102 _HF_FILE_PLACEHOLDER);
103 LOG_HELP(" Run the binary over a dynamic file, maximize unique branches (edges) via BTS:");
Robert Swiecki43f0b282016-11-01 18:06:40 +0100104 LOG_HELP_BOLD(" " PROG_NAME " --linux_perf_bts_edge -- /usr/bin/tiffinfo -D "
105 _HF_FILE_PLACEHOLDER);
Robert Swiecki349cb9e2016-10-14 21:36:42 +0200106 LOG_HELP
Robert Swiecki8499c662016-11-01 18:02:52 +0100107 (" 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 +0200108 LOG_HELP_BOLD(" " PROG_NAME " --linux_perf_ipt_block -- /usr/bin/tiffinfo -D "
Jagger72f258b2015-10-09 23:09:01 +0200109 _HF_FILE_PLACEHOLDER);
Robert Swieckia88f96f2015-10-09 16:47:39 +0200110#endif /* defined(_HF_ARCH_LINUX) */
Robert Swieckia88f96f2015-10-09 16:47:39 +0200111}
112
113static void cmdlineUsage(const char *pname, struct custom_option *opts)
114{
115 cmdlineHelp(pname, opts);
116 exit(0);
117}
118
Robert Swieckia88f96f2015-10-09 16:47:39 +0200119rlim_t cmdlineParseRLimit(int res, const char *optarg, unsigned long mul)
120{
121 struct rlimit cur;
122 if (getrlimit(res, &cur) == -1) {
123 PLOG_F("getrlimit(%d)", res);
124 }
125 if (strcasecmp(optarg, "max") == 0) {
126 return cur.rlim_max;
127 }
128 if (strcasecmp(optarg, "def") == 0) {
129 return cur.rlim_cur;
130 }
Anestis Bechtsoudis413cb132016-02-07 12:59:00 +0200131 if (util_isANumber(optarg) == false) {
Robert Swieckia88f96f2015-10-09 16:47:39 +0200132 LOG_F("RLIMIT %d needs a numeric or 'max'/'def' value ('%s' provided)", res, optarg);
133 }
134 rlim_t val = strtoul(optarg, NULL, 0) * mul;
Jagger2bd61b72015-10-10 05:23:32 +0200135 if ((unsigned long)val == ULONG_MAX && errno != 0) {
Robert Swieckia88f96f2015-10-09 16:47:39 +0200136 PLOG_F("strtoul('%s', 0)", optarg);
137 }
138 return val;
139}
140
141bool cmdlineParse(int argc, char *argv[], honggfuzz_t * hfuzz)
142{
143 /* *INDENT-OFF* */
144 (*hfuzz) = (honggfuzz_t) {
145 .cmdline = NULL,
Robert Swiecki72d2bef2016-01-19 14:39:26 +0100146 .cmdline_txt[0] = '\0',
Jagger1b2d4822016-09-25 16:19:45 +0200147 .inputDir = NULL,
Robert Swieckia88f96f2015-10-09 16:47:39 +0200148 .nullifyStdio = false,
Robert Swieckia88f96f2015-10-09 16:47:39 +0200149 .fuzzStdin = false,
150 .saveUnique = true,
Robert Swiecki6c9f6822016-03-14 16:12:27 +0100151 .useScreen = true,
152 .useVerifier = false,
153 .timeStart = time(NULL),
Robert Swieckia88f96f2015-10-09 16:47:39 +0200154 .fileExtn = "fuzz",
155 .workDir = ".",
Jagger1b2d4822016-09-25 16:19:45 +0200156 .covDir = NULL,
Robert Swieckia96d78d2016-03-14 16:50:50 +0100157 .origFlipRate = 0.001f,
Robert Swieckia88f96f2015-10-09 16:47:39 +0200158 .externalCommand = NULL,
Robert Swieckiee266ac2016-10-03 02:25:59 +0200159 .postExternalCommand = NULL,
Robert Swieckia88f96f2015-10-09 16:47:39 +0200160 .blacklistFile = NULL,
161 .blacklistCnt = 0,
162 .blacklist = NULL,
Jaggerf4a60562016-09-25 15:40:23 +0200163 .maxFileSz = 0UL,
Jaggerba92b4b2016-03-16 02:24:17 +0100164 .tmOut = 10,
Robert Swieckia88f96f2015-10-09 16:47:39 +0200165 .mutationsMax = 0,
166 .threadsFinished = 0,
Jagger2664f542016-09-28 14:37:00 +0200167 .threadsMax = (sysconf(_SC_NPROCESSORS_ONLN) <= 1) ? 1 : sysconf(_SC_NPROCESSORS_ONLN) / 2,
Robert Swieckia88f96f2015-10-09 16:47:39 +0200168 .reportFile = NULL,
169 .asLimit = 0ULL,
Robert Swieckia88f96f2015-10-09 16:47:39 +0200170 .fileCnt = 0,
Robert Swiecki05354ca2016-03-15 19:10:23 +0100171 .lastFileIndex = 0,
172 .doneFileIndex = 0,
Jagger80041fe2016-03-10 21:32:35 +0100173 .clearEnv = false,
Jaggerab26e702016-03-22 04:28:00 +0100174 .envs = {
175 [0 ... (ARRAYSIZE(hfuzz->envs) - 1)] = NULL,
176 },
Robert Swiecki0f937af2016-03-30 18:19:16 +0200177 .persistent = false,
Robert Swieckie84b6452016-12-12 12:42:04 +0100178 .tmout_vtalrm = false,
Anestis Bechtsoudise5f09f82016-12-27 16:06:05 +0200179 .enableSanitizers = false,
180#if defined(__ANDROID__)
181 .monitorSIGABRT = false,
182#else
183 .monitorSIGABRT = true,
184#endif
Robert Swiecki0ec98112017-02-03 02:08:14 +0100185 .threadsActiveCnt = 0,
Robert Swieckia88f96f2015-10-09 16:47:39 +0200186
Robert Swiecki531438a2016-09-13 19:05:11 +0200187 .dictionaryFile = NULL,
188 .dictionaryCnt = 0,
189
Robert Swiecki6c9f6822016-03-14 16:12:27 +0100190 .state = _HF_STATE_UNSET,
Jaggerb7fa3ee2016-08-21 19:46:26 +0200191 .feedback = NULL,
Robert Swieckibc7532e2016-08-20 00:34:17 +0200192 .bbFd = -1,
Robert Swieckie586c1f2016-03-14 18:46:03 +0100193 .dynfileq_mutex = PTHREAD_MUTEX_INITIALIZER,
Robert Swiecki3bfc33c2016-03-14 18:12:41 +0100194 .dynfileqCnt = 0U,
Robert Swiecki6c9f6822016-03-14 16:12:27 +0100195
Robert Swieckia88f96f2015-10-09 16:47:39 +0200196 .mutationsCnt = 0,
197 .crashesCnt = 0,
198 .uniqueCrashesCnt = 0,
Anestis Bechtsoudis79b799e2015-11-01 00:02:25 +0200199 .verifiedCrashesCnt = 0,
Robert Swieckia88f96f2015-10-09 16:47:39 +0200200 .blCrashesCnt = 0,
201 .timeoutedCnt = 0,
202
Robert Swiecki6c9f6822016-03-14 16:12:27 +0100203 .dynFileMethod = _HF_DYNFILE_NONE,
Anestis Bechtsoudisbe0ac7b2015-12-26 15:38:47 +0200204 .sanCovCnts = {
Jaggerab26e702016-03-22 04:28:00 +0100205 .hitBBCnt = 0ULL,
206 .totalBBCnt = 0ULL,
207 .dsoCnt = 0ULL,
208 .iDsoCnt = 0ULL,
209 .newBBCnt = 0ULL,
210 .crashesCnt = 0ULL,
211 },
Robert Swieckia88f96f2015-10-09 16:47:39 +0200212
Anestis Bechtsoudisa16f70f2016-01-03 13:03:21 +0200213 .sanCov_mutex = PTHREAD_MUTEX_INITIALIZER,
Robert Swiecki23ec02a2016-01-19 18:47:45 +0100214 .sanOpts = {
Jaggerab26e702016-03-22 04:28:00 +0100215 .asanOpts = NULL,
216 .msanOpts = NULL,
217 .ubsanOpts = NULL,
Anestis Bechtsoudis61b5ab12016-01-08 16:07:02 +0200218 },
Robert Swiecki6c9f6822016-03-14 16:12:27 +0100219 .useSanCov = false,
Robert Swiecki6c9f6822016-03-14 16:12:27 +0100220 .covMetadata = NULL,
Jagger247c3b42016-03-21 23:24:05 +0100221 .msanReportUMRS = false,
Robert Swiecki6c9f6822016-03-14 16:12:27 +0100222
Haris Andrianakisc9a71332016-05-09 21:56:30 -0700223 .report_mutex = PTHREAD_MUTEX_INITIALIZER,
224
Robert Swiecki6c9f6822016-03-14 16:12:27 +0100225 /* Linux code */
Robert Swieckifab69162016-03-31 15:41:36 +0200226 .linux = {
Jaggerab26e702016-03-22 04:28:00 +0100227 .hwCnts = {
228 .cpuInstrCnt = 0ULL,
229 .cpuBranchCnt = 0ULL,
Jaggerab26e702016-03-22 04:28:00 +0100230 .bbCnt = 0ULL,
231 .newBBCnt = 0ULL,
Jagger34789a72016-09-08 00:36:09 +0200232 .softCntPc = 0ULL,
233 .softCntCmp = 0ULL,
Jaggerab26e702016-03-22 04:28:00 +0100234 },
235 .dynamicCutOffAddr = ~(0ULL),
236 .disableRandomization = true,
237 .ignoreAddr = NULL,
238 .numMajorFrames = 7,
239 .pid = 0,
240 .pidFile = NULL,
241 .pidCmd = NULL,
Anestis Bechtsoudisba68b382016-10-29 20:44:15 +0300242 .symsBlFile = NULL,
243 .symsBlCnt = 0,
244 .symsBl = NULL,
245 .symsWlFile = NULL,
246 .symsWlCnt = 0,
247 .symsWl = NULL,
Robert Swiecki846ccd72017-01-12 17:52:23 +0100248 .cloneFlags = 0,
Jaggerab26e702016-03-22 04:28:00 +0100249 },
Robert Swieckia88f96f2015-10-09 16:47:39 +0200250 };
251 /* *INDENT-ON* */
252
Robert Swiecki3bfc33c2016-03-14 18:12:41 +0100253 TAILQ_INIT(&hfuzz->dynfileq);
Robert Swiecki3a572262016-10-04 01:48:34 +0200254 TAILQ_INIT(&hfuzz->dictq);
Robert Swieckie8f8e8d2016-10-03 23:51:32 +0200255 TAILQ_INIT(&hfuzz->fileq);
Robert Swiecki3bfc33c2016-03-14 18:12:41 +0100256
Robert Swieckia88f96f2015-10-09 16:47:39 +0200257 /* *INDENT-OFF* */
258 struct custom_option custom_opts[] = {
259 {{"help", no_argument, NULL, 'h'}, "Help plz.."},
Jagger1b2d4822016-09-25 16:19:45 +0200260 {{"input", required_argument, NULL, 'f'}, "Path to a directory containing initial file corpus"},
Robert Swieckia88f96f2015-10-09 16:47:39 +0200261 {{"nullify_stdio", no_argument, NULL, 'q'}, "Null-ify children's stdin, stdout, stderr; make them quiet"},
Jagger54c96a22016-03-30 23:13:30 +0200262 {{"timeout", required_argument, NULL, 't'}, "Timeout in seconds (default: '10')"},
Jagger2664f542016-09-28 14:37:00 +0200263 {{"threads", required_argument, NULL, 'n'}, "Number of concurrent fuzzing threads (default: number of CPUs / 2)"},
Robert Swieckia88f96f2015-10-09 16:47:39 +0200264 {{"stdin_input", no_argument, NULL, 's'}, "Provide fuzzing input on STDIN, instead of ___FILE___"},
Jagger54c96a22016-03-30 23:13:30 +0200265 {{"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 +0200266 {{"logfile", required_argument, NULL, 'l'}, "Log file"},
Robert Swieckia88f96f2015-10-09 16:47:39 +0200267 {{"verbose", no_argument, NULL, 'v'}, "Disable ANSI console; use simple log output"},
Jagger5ff08ac2016-02-09 22:15:58 +0100268 {{"verifier", no_argument, NULL, 'V'}, "Enable crashes verifier"},
Robert Swieckia88f96f2015-10-09 16:47:39 +0200269 {{"debug_level", required_argument, NULL, 'd'}, "Debug level (0 - FATAL ... 4 - DEBUG), (default: '3' [INFO])"},
270 {{"extension", required_argument, NULL, 'e'}, "Input file extension (e.g. 'swf'), (default: 'fuzz')"},
Anestis Bechtsoudisbfcba122016-04-28 10:55:20 +0300271 {{"workspace", required_argument, NULL, 'W'}, "Workspace directory to save crashes & runtime files (default: '.')"},
Robert Swieckid32018f2016-10-11 22:58:22 +0200272 {{"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 +0200273 {{"wordlist", required_argument, NULL, 'w'}, "Wordlist file (tokens delimited by NUL-bytes)"},
274 {{"stackhash_bl", required_argument, NULL, 'B'}, "Stackhashes blacklist file (one entry per line)"},
Robert Swieckiee266ac2016-10-03 02:25:59 +0200275 {{"mutate_cmd", required_argument, NULL, 'c'}, "External command producing fuzz files (instead of internal mutators)"},
276 {{"pprocess_cmd", required_argument, NULL, 0x104}, "External command postprocessing files produced by internal mutators"},
Robert Swieckia88f96f2015-10-09 16:47:39 +0200277 {{"iterations", required_argument, NULL, 'N'}, "Number of fuzzing iterations (default: '0' [no limit])"},
Robert Swiecki03ef5312015-10-09 18:25:40 +0200278 {{"rlimit_as", required_argument, NULL, 0x100}, "Per process memory limit in MiB (default: '0' [no limit])"},
dyjakanebfd24e2015-10-16 19:24:32 +0100279 {{"report", required_argument, NULL, 'R'}, "Write report to this file (default: '" _HF_REPORT_FILE "')"},
Robert Swieckia88f96f2015-10-09 16:47:39 +0200280 {{"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 +0100281 {{"clear_env", no_argument, NULL, 0x101}, "Clear all environment variables before executing the binary"},
Robert Swieckia88f96f2015-10-09 16:47:39 +0200282 {{"env", required_argument, NULL, 'E'}, "Pass this environment variable, can be used multiple times"},
Jagger54c96a22016-03-30 23:13:30 +0200283 {{"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 +0100284 {{"sancov", no_argument, NULL, 'C'}, "Enable sanitizer coverage feedback"},
Robert Swieckif05b1cd2016-11-01 18:05:45 +0100285 {{"instrument", no_argument, NULL, 'z'}, "Enable compile-time instrumentation (link with libhfuzz/libhfuzz.a)"},
Jagger7ff92fa2016-03-22 04:13:50 +0100286 {{"msan_report_umrs", no_argument, NULL, 0x102}, "Report MSAN's UMRS (uninitialized memory access)"},
Robert Swieckif05b1cd2016-11-01 18:05:45 +0100287 {{"persistent", no_argument, NULL, 'P'}, "Enable persistent fuzzing (link with libhfuzz/libhfuzz.a)"},
Gergely Nagy5d47c732016-12-12 23:51:51 +0100288 {{"tmout_sigvtalrm", no_argument, NULL, 'T'}, "Use SIGVTALRM to kill timeouting processes (default: use SIGKILL)"},
Anestis Bechtsoudise5f09f82016-12-27 16:06:05 +0200289 {{"sanitizers", no_argument, NULL, 'S'}, "Enable sanitizers settings (default: false)"},
290 {{"monitor_sigabrt", required_argument, NULL, 0x105}, "Monitor SIGABRT (default: 'false for Android - 'true for other platforms)"},
Robert Swieckia88f96f2015-10-09 16:47:39 +0200291
292#if defined(_HF_ARCH_LINUX)
Anestis Bechtsoudisfafb3332016-10-31 08:57:16 +0200293 {{"linux_symbols_bl", required_argument, NULL, 0x504}, "Symbols blacklist filter file (one entry per line)"},
294 {{"linux_symbols_wl", required_argument, NULL, 0x505}, "Symbols whitelist filter file (one entry per line)"},
Anestis Bechtsoudis413cb132016-02-07 12:59:00 +0200295 {{"linux_pid", required_argument, NULL, 'p'}, "Attach to a pid (and its thread group)"},
Robert Swieckifab69162016-03-31 15:41:36 +0200296 {{"linux_file_pid", required_argument, NULL, 0x502}, "Attach to pid (and its thread group) read from file"},
Robert Swieckia88f96f2015-10-09 16:47:39 +0200297 {{"linux_addr_low_limit", required_argument, NULL, 0x500}, "Address limit (from si.si_addr) below which crashes are not reported, (default: '0')"},
298 {{"linux_keep_aslr", no_argument, NULL, 0x501}, "Don't disable ASLR randomization, might be useful with MSAN"},
Jaggerae6a4452015-10-14 17:34:43 +0200299 {{"linux_perf_ignore_above", required_argument, NULL, 0x503}, "Ignore perf events which report IPs above this address"},
Robert Swieckia88f96f2015-10-09 16:47:39 +0200300 {{"linux_perf_instr", no_argument, NULL, 0x510}, "Use PERF_COUNT_HW_INSTRUCTIONS perf"},
301 {{"linux_perf_branch", no_argument, NULL, 0x511}, "Use PERF_COUNT_HW_BRANCH_INSTRUCTIONS perf"},
Jagger39bd2b02016-02-04 01:16:15 +0100302 {{"linux_perf_bts_block", no_argument, NULL, 0x512}, "Use Intel BTS to count unique blocks"},
303 {{"linux_perf_bts_edge", no_argument, NULL, 0x513}, "Use Intel BTS to count unique edges"},
Robert Swiecki8499c662016-11-01 18:02:52 +0100304 {{"linux_perf_ipt_block", no_argument, NULL, 0x514}, "Use Intel Processor Trace to count unique blocks (requires libipt.so)"},
Robert Swiecki846ccd72017-01-12 17:52:23 +0100305 {{"linux_ns_net", no_argument, NULL, 0x0530}, "Use Linux NET namespace isolation"},
306 {{"linux_ns_pid", no_argument, NULL, 0x0531}, "Use Linux PID namespace isolation"},
307 {{"linux_ns_ipc", no_argument, NULL, 0x0532}, "Use Linux IPC namespace isolation"},
Robert Swieckia88f96f2015-10-09 16:47:39 +0200308#endif // defined(_HF_ARCH_LINUX)
309 {{0, 0, 0, 0}, NULL},
310 };
311 /* *INDENT-ON* */
312
313 struct option opts[ARRAYSIZE(custom_opts)];
314 for (unsigned i = 0; i < ARRAYSIZE(custom_opts); i++) {
315 opts[i] = custom_opts[i].opt;
316 }
317
318 enum llevel_t ll = INFO;
319 const char *logfile = NULL;
320 int opt_index = 0;
321 for (;;) {
Anestis Bechtsoudise5f09f82016-12-27 16:06:05 +0200322 int c = getopt_long(argc, argv, "-?hqvVsuPf:d:e:W:r:c:F:t:R:n:N:l:p:g:E:w:B:CzTS", opts,
Robert Swieckia88f96f2015-10-09 16:47:39 +0200323 &opt_index);
324 if (c < 0)
325 break;
326
327 switch (c) {
328 case 'h':
329 case '?':
330 cmdlineUsage(argv[0], custom_opts);
331 break;
332 case 'f':
Jagger1b2d4822016-09-25 16:19:45 +0200333 hfuzz->inputDir = optarg;
Robert Swieckia88f96f2015-10-09 16:47:39 +0200334 break;
335 case 'q':
336 hfuzz->nullifyStdio = true;
337 break;
338 case 'v':
339 hfuzz->useScreen = false;
340 break;
Anestis Bechtsoudis0cde66f2015-10-11 19:37:11 -0700341 case 'V':
342 hfuzz->useVerifier = true;
343 break;
Robert Swieckia88f96f2015-10-09 16:47:39 +0200344 case 's':
345 hfuzz->fuzzStdin = true;
346 break;
347 case 'u':
348 hfuzz->saveUnique = false;
349 break;
Robert Swiecki03ef5312015-10-09 18:25:40 +0200350 case 'l':
351 logfile = optarg;
352 break;
Robert Swieckia88f96f2015-10-09 16:47:39 +0200353 case 'd':
354 ll = atoi(optarg);
355 break;
356 case 'e':
357 hfuzz->fileExtn = optarg;
358 break;
359 case 'W':
360 hfuzz->workDir = optarg;
361 break;
362 case 'r':
Robert Swieckia96d78d2016-03-14 16:50:50 +0100363 hfuzz->origFlipRate = strtod(optarg, NULL);
Robert Swieckia88f96f2015-10-09 16:47:39 +0200364 break;
365 case 'c':
366 hfuzz->externalCommand = optarg;
367 break;
Anestis Bechtsoudisbe0ac7b2015-12-26 15:38:47 +0200368 case 'C':
369 hfuzz->useSanCov = true;
370 break;
Anestis Bechtsoudise5f09f82016-12-27 16:06:05 +0200371 case 'S':
372 hfuzz->enableSanitizers = true;
373 break;
Jagger4aac9fe2016-08-28 17:35:48 +0200374 case 'z':
375 hfuzz->dynFileMethod |= _HF_DYNFILE_SOFT;
376 break;
Robert Swieckia88f96f2015-10-09 16:47:39 +0200377 case 'F':
378 hfuzz->maxFileSz = strtoul(optarg, NULL, 0);
379 break;
380 case 't':
381 hfuzz->tmOut = atol(optarg);
382 break;
383 case 'R':
384 hfuzz->reportFile = optarg;
385 break;
386 case 'n':
387 hfuzz->threadsMax = atol(optarg);
388 break;
389 case 'N':
390 hfuzz->mutationsMax = atol(optarg);
391 break;
Robert Swiecki03ef5312015-10-09 18:25:40 +0200392 case 0x100:
Robert Swieckia88f96f2015-10-09 16:47:39 +0200393 hfuzz->asLimit = strtoull(optarg, NULL, 0);
394 break;
Jagger80041fe2016-03-10 21:32:35 +0100395 case 0x101:
396 hfuzz->clearEnv = true;
397 break;
Jagger7ff92fa2016-03-22 04:13:50 +0100398 case 0x102:
399 hfuzz->msanReportUMRS = true;
400 break;
Robert Swieckidac8cf12016-09-22 15:36:28 +0200401 case 0x103:
Jagger1b2d4822016-09-25 16:19:45 +0200402 hfuzz->covDir = optarg;
Robert Swieckidac8cf12016-09-22 15:36:28 +0200403 break;
Robert Swieckiee266ac2016-10-03 02:25:59 +0200404 case 0x104:
405 hfuzz->postExternalCommand = optarg;
406 break;
Anestis Bechtsoudise5f09f82016-12-27 16:06:05 +0200407 case 0x105:
408 if ((strcasecmp(optarg, "0") == 0) || (strcasecmp(optarg, "false") == 0)) {
409 hfuzz->monitorSIGABRT = false;
410 } else {
411 hfuzz->monitorSIGABRT = true;
412 }
413 break;
Robert Swieckifab69162016-03-31 15:41:36 +0200414 case 'P':
Robert Swiecki0f937af2016-03-30 18:19:16 +0200415 hfuzz->persistent = true;
416 break;
Gergely Nagy5d47c732016-12-12 23:51:51 +0100417 case 'T':
Robert Swieckie84b6452016-12-12 12:42:04 +0100418 hfuzz->tmout_vtalrm = true;
419 break;
Robert Swieckia88f96f2015-10-09 16:47:39 +0200420 case 'p':
Anestis Bechtsoudis413cb132016-02-07 12:59:00 +0200421 if (util_isANumber(optarg) == false) {
Jagger9c4d1622015-10-16 01:40:17 +0200422 LOG_E("-p '%s' is not a number", optarg);
423 return false;
424 }
Jagger247c3b42016-03-21 23:24:05 +0100425 hfuzz->linux.pid = atoi(optarg);
426 if (hfuzz->linux.pid < 1) {
427 LOG_E("-p '%d' is invalid", hfuzz->linux.pid);
Jagger9c4d1622015-10-16 01:40:17 +0200428 return false;
429 }
Robert Swieckia88f96f2015-10-09 16:47:39 +0200430 break;
Robert Swieckifab69162016-03-31 15:41:36 +0200431 case 0x502:
Jagger247c3b42016-03-21 23:24:05 +0100432 hfuzz->linux.pidFile = optarg;
Anestis Bechtsoudis413cb132016-02-07 12:59:00 +0200433 break;
Robert Swieckia88f96f2015-10-09 16:47:39 +0200434 case 'E':
435 for (size_t i = 0; i < ARRAYSIZE(hfuzz->envs); i++) {
436 if (hfuzz->envs[i] == NULL) {
437 hfuzz->envs[i] = optarg;
438 break;
439 }
440 }
441 break;
442 case 'w':
443 hfuzz->dictionaryFile = optarg;
444 break;
445 case 'B':
446 hfuzz->blacklistFile = optarg;
447 break;
Robert Swiecki846ccd72017-01-12 17:52:23 +0100448#if defined(_HF_ARCH_LINUX)
Robert Swieckia88f96f2015-10-09 16:47:39 +0200449 case 0x500:
Jagger247c3b42016-03-21 23:24:05 +0100450 hfuzz->linux.ignoreAddr = (void *)strtoul(optarg, NULL, 0);
Robert Swieckia88f96f2015-10-09 16:47:39 +0200451 break;
452 case 0x501:
Jagger247c3b42016-03-21 23:24:05 +0100453 hfuzz->linux.disableRandomization = false;
Robert Swieckia88f96f2015-10-09 16:47:39 +0200454 break;
Robert Swieckia88f96f2015-10-09 16:47:39 +0200455 case 0x503:
Jagger247c3b42016-03-21 23:24:05 +0100456 hfuzz->linux.dynamicCutOffAddr = strtoull(optarg, NULL, 0);
Robert Swieckia88f96f2015-10-09 16:47:39 +0200457 break;
Anestis Bechtsoudisba68b382016-10-29 20:44:15 +0300458 case 0x504:
459 hfuzz->linux.symsBlFile = optarg;
460 break;
461 case 0x505:
462 hfuzz->linux.symsWlFile = optarg;
463 break;
Robert Swieckia88f96f2015-10-09 16:47:39 +0200464 case 0x510:
465 hfuzz->dynFileMethod |= _HF_DYNFILE_INSTR_COUNT;
466 break;
467 case 0x511:
468 hfuzz->dynFileMethod |= _HF_DYNFILE_BRANCH_COUNT;
469 break;
470 case 0x512:
Jagger3abc5602016-02-04 00:53:43 +0100471 hfuzz->dynFileMethod |= _HF_DYNFILE_BTS_BLOCK;
Robert Swieckia88f96f2015-10-09 16:47:39 +0200472 break;
473 case 0x513:
Jagger3abc5602016-02-04 00:53:43 +0100474 hfuzz->dynFileMethod |= _HF_DYNFILE_BTS_EDGE;
Robert Swieckia88f96f2015-10-09 16:47:39 +0200475 break;
476 case 0x514:
Jagger39bd2b02016-02-04 01:16:15 +0100477 hfuzz->dynFileMethod |= _HF_DYNFILE_IPT_BLOCK;
478 break;
Robert Swiecki846ccd72017-01-12 17:52:23 +0100479 case 0x530:
480 hfuzz->linux.cloneFlags |= (CLONE_NEWUSER | CLONE_NEWNET);
481 break;
482 case 0x531:
483 hfuzz->linux.cloneFlags |= (CLONE_NEWUSER | CLONE_NEWPID);
484 break;
485 case 0x532:
486 hfuzz->linux.cloneFlags |= (CLONE_NEWUSER | CLONE_NEWIPC);
487 break;
488#endif /* defined(_HF_ARCH_LINUX) */
Robert Swieckia88f96f2015-10-09 16:47:39 +0200489 default:
490 cmdlineUsage(argv[0], custom_opts);
491 return false;
492 break;
493 }
494 }
Jagger72f258b2015-10-09 23:09:01 +0200495
Robert Swieckia88f96f2015-10-09 16:47:39 +0200496 if (logInitLogFile(logfile, ll) == false) {
497 return false;
498 }
499
500 hfuzz->cmdline = &argv[optind];
501 if (hfuzz->cmdline[0] == NULL) {
502 LOG_E("No fuzz command provided");
503 cmdlineUsage(argv[0], custom_opts);
504 return false;
505 }
506
Robert Swiecki0f937af2016-03-30 18:19:16 +0200507 if (!hfuzz->fuzzStdin && !hfuzz->persistent && !checkFor_FILE_PLACEHOLDER(hfuzz->cmdline)) {
Robert Swieckia88f96f2015-10-09 16:47:39 +0200508 LOG_E("You must specify '" _HF_FILE_PLACEHOLDER
Robert Swiecki0f937af2016-03-30 18:19:16 +0200509 "' when the -s (stdin fuzzing) or --persistent options are not set");
Robert Swieckia88f96f2015-10-09 16:47:39 +0200510 return false;
511 }
512
Robert Swiecki71b73722016-09-05 15:18:25 +0200513 if (hfuzz->threadsMax >= _HF_THREAD_MAX) {
514 LOG_E("Too many fuzzing threads specified %zu (>= _HF_THREAD_MAX (%u))", hfuzz->threadsMax,
515 _HF_THREAD_MAX);
516 return false;
517 }
518
Robert Swieckia88f96f2015-10-09 16:47:39 +0200519 if (strchr(hfuzz->fileExtn, '/')) {
520 LOG_E("The file extension contains the '/' character: '%s'", hfuzz->fileExtn);
521 return false;
522 }
Anestis Bechtsoudisbe0ac7b2015-12-26 15:38:47 +0200523
Anestis Bechtsoudis8f4aa612015-12-27 12:06:19 +0200524 if (hfuzz->workDir[0] != '.' || strlen(hfuzz->workDir) > 2) {
Anestis Bechtsoudisc8e7f6e2015-12-26 14:48:48 +0200525 if (!files_exists(hfuzz->workDir)) {
526 LOG_E("Provided workspace directory '%s' doesn't exist", hfuzz->workDir);
527 return false;
528 }
529 }
Robert Swieckia88f96f2015-10-09 16:47:39 +0200530
Jagger247c3b42016-03-21 23:24:05 +0100531 if (hfuzz->linux.pid > 0 || hfuzz->linux.pidFile) {
532 LOG_I("PID=%d specified, lowering maximum number of concurrent threads to 1",
533 hfuzz->linux.pid);
Robert Swieckia88f96f2015-10-09 16:47:39 +0200534 hfuzz->threadsMax = 1;
535 }
536
Robert Swieckia96d78d2016-03-14 16:50:50 +0100537 if (hfuzz->origFlipRate == 0.0L && hfuzz->useVerifier) {
Anestis Bechtsoudisc8e7f6e2015-12-26 14:48:48 +0200538 LOG_I("Verifier enabled with 0.0 flipRate, activating dry run mode");
Anestis Bechtsoudis46ea10e2015-11-07 18:16:25 +0200539 }
540
Anestis Bechtsoudisc1a0d9f2016-12-29 11:34:10 +0200541 /*
542 * 'enableSanitizers' can be auto enabled when 'useSanCov', although it's probably
543 * better to let user know about the features that each flag control.
544 */
545 if (hfuzz->useSanCov == true && hfuzz->enableSanitizers == false) {
546 LOG_E("Sanitizer coverage cannot be used without enabling sanitizers '-S/--sanitizers'");
547 return false;
548 }
549
Jagger1b2d4822016-09-25 16:19:45 +0200550 LOG_I("inputDir '%s', nullifyStdio: %s, fuzzStdin: %s, saveUnique: %s, flipRate: %lf, "
Anestis Bechtsoudisecab7762016-12-27 18:27:30 +0200551 "externalCommand: '%s', tmOut: %ld, mutationsMax: %zu, threadsMax: %zu, fileExtn: '%s', "
552 "memoryLimit: 0x%" PRIx64 "(MiB), fuzzExe: '%s', fuzzedPid: %d, monitorSIGABRT: '%s'",
Jagger1b2d4822016-09-25 16:19:45 +0200553 hfuzz->inputDir,
Robert Swieckia88f96f2015-10-09 16:47:39 +0200554 cmdlineYesNo(hfuzz->nullifyStdio), cmdlineYesNo(hfuzz->fuzzStdin),
Robert Swieckia96d78d2016-03-14 16:50:50 +0100555 cmdlineYesNo(hfuzz->saveUnique), hfuzz->origFlipRate,
Robert Swieckia88f96f2015-10-09 16:47:39 +0200556 hfuzz->externalCommand == NULL ? "NULL" : hfuzz->externalCommand, hfuzz->tmOut,
Jagger247c3b42016-03-21 23:24:05 +0100557 hfuzz->mutationsMax, hfuzz->threadsMax, hfuzz->fileExtn,
Anestis Bechtsoudisac366a12016-12-28 12:38:44 +0200558 hfuzz->asLimit, hfuzz->cmdline[0], hfuzz->linux.pid, cmdlineYesNo(hfuzz->monitorSIGABRT));
Robert Swieckia88f96f2015-10-09 16:47:39 +0200559
Robert Swiecki2aaa52b2016-01-19 14:40:47 +0100560 snprintf(hfuzz->cmdline_txt, sizeof(hfuzz->cmdline_txt), "%s", hfuzz->cmdline[0]);
Robert Swiecki72d2bef2016-01-19 14:39:26 +0100561 for (size_t i = 1; hfuzz->cmdline[i]; i++) {
562 util_ssnprintf(hfuzz->cmdline_txt, sizeof(hfuzz->cmdline_txt), " %s", hfuzz->cmdline[i]);
Robert Swieckif2d9c3a2016-11-03 02:13:54 +0100563 if (strlen(hfuzz->cmdline_txt) == (sizeof(hfuzz->cmdline_txt) - 1)) {
564 hfuzz->cmdline_txt[sizeof(hfuzz->cmdline_txt) - 3] = '.';
565 hfuzz->cmdline_txt[sizeof(hfuzz->cmdline_txt) - 2] = '.';
566 hfuzz->cmdline_txt[sizeof(hfuzz->cmdline_txt) - 1] = '.';
567 }
Robert Swiecki72d2bef2016-01-19 14:39:26 +0100568 }
569
Robert Swieckia88f96f2015-10-09 16:47:39 +0200570 return true;
571}