blob: 256a0a773a1aee98d9f2f5809e3464f95e29270b [file] [log] [blame]
Kostya Serebryany016852c2015-02-19 18:45:37 +00001//===- FuzzerDriver.cpp - FuzzerDriver function and flags -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9// FuzzerDriver and flag parsing.
10//===----------------------------------------------------------------------===//
11
12#include "FuzzerInterface.h"
13#include "FuzzerInternal.h"
14
15#include <cstring>
Kostya Serebryany83fd4862015-05-11 21:31:51 +000016#include <chrono>
Kostya Serebryany016852c2015-02-19 18:45:37 +000017#include <unistd.h>
Kostya Serebryany016852c2015-02-19 18:45:37 +000018#include <thread>
19#include <atomic>
20#include <mutex>
Kostya Serebryany52a788e2015-03-31 20:13:20 +000021#include <string>
22#include <sstream>
23#include <algorithm>
24#include <iterator>
Kostya Serebryany016852c2015-02-19 18:45:37 +000025
Kostya Serebryany4282d302016-01-15 00:17:37 +000026// This function should be present in the libFuzzer so that the client
27// binary can test for its existence.
28extern "C" __attribute__((used)) void __libfuzzer_is_present() {}
29
Kostya Serebryany016852c2015-02-19 18:45:37 +000030namespace fuzzer {
31
32// Program arguments.
33struct FlagDescription {
34 const char *Name;
35 const char *Description;
36 int Default;
Kostya Serebryany52a788e2015-03-31 20:13:20 +000037 int *IntFlag;
38 const char **StrFlag;
Mike Aizatskya1a5c692015-12-10 20:41:53 +000039 unsigned int *UIntFlag;
Kostya Serebryany016852c2015-02-19 18:45:37 +000040};
41
42struct {
Kostya Serebryany3d95dd92016-03-01 22:33:14 +000043#define FUZZER_DEPRECATED_FLAG(Name)
Kostya Serebryany52a788e2015-03-31 20:13:20 +000044#define FUZZER_FLAG_INT(Name, Default, Description) int Name;
Mike Aizatskya1a5c692015-12-10 20:41:53 +000045#define FUZZER_FLAG_UNSIGNED(Name, Default, Description) unsigned int Name;
Kostya Serebryany52a788e2015-03-31 20:13:20 +000046#define FUZZER_FLAG_STRING(Name, Description) const char *Name;
Kostya Serebryany016852c2015-02-19 18:45:37 +000047#include "FuzzerFlags.def"
Kostya Serebryany3d95dd92016-03-01 22:33:14 +000048#undef FUZZER_DEPRECATED_FLAG
Kostya Serebryany52a788e2015-03-31 20:13:20 +000049#undef FUZZER_FLAG_INT
Mike Aizatskya1a5c692015-12-10 20:41:53 +000050#undef FUZZER_FLAG_UNSIGNED
Kostya Serebryany52a788e2015-03-31 20:13:20 +000051#undef FUZZER_FLAG_STRING
Kostya Serebryany016852c2015-02-19 18:45:37 +000052} Flags;
53
Craig Topper26260942015-10-18 05:15:34 +000054static const FlagDescription FlagDescriptions [] {
Kostya Serebryany3d95dd92016-03-01 22:33:14 +000055#define FUZZER_DEPRECATED_FLAG(Name) \
56 {#Name, "Deprecated; don't use", 0, nullptr, nullptr, nullptr},
Kostya Serebryany52a788e2015-03-31 20:13:20 +000057#define FUZZER_FLAG_INT(Name, Default, Description) \
Mike Aizatskya1a5c692015-12-10 20:41:53 +000058 {#Name, Description, Default, &Flags.Name, nullptr, nullptr},
59#define FUZZER_FLAG_UNSIGNED(Name, Default, Description) \
60 {#Name, Description, static_cast<int>(Default), \
61 nullptr, nullptr, &Flags.Name},
Kostya Serebryany52a788e2015-03-31 20:13:20 +000062#define FUZZER_FLAG_STRING(Name, Description) \
Mike Aizatskya1a5c692015-12-10 20:41:53 +000063 {#Name, Description, 0, nullptr, &Flags.Name, nullptr},
Kostya Serebryany016852c2015-02-19 18:45:37 +000064#include "FuzzerFlags.def"
Kostya Serebryany3d95dd92016-03-01 22:33:14 +000065#undef FUZZER_DEPRECATED_FLAG
Kostya Serebryany52a788e2015-03-31 20:13:20 +000066#undef FUZZER_FLAG_INT
Mike Aizatskya1a5c692015-12-10 20:41:53 +000067#undef FUZZER_FLAG_UNSIGNED
Kostya Serebryany52a788e2015-03-31 20:13:20 +000068#undef FUZZER_FLAG_STRING
Kostya Serebryany016852c2015-02-19 18:45:37 +000069};
70
71static const size_t kNumFlags =
72 sizeof(FlagDescriptions) / sizeof(FlagDescriptions[0]);
73
Kostya Serebryanya938bcb2015-09-10 16:57:57 +000074static std::vector<std::string> *Inputs;
75static std::string *ProgName;
Kostya Serebryany016852c2015-02-19 18:45:37 +000076
77static void PrintHelp() {
Kostya Serebryanybfbe7fc2016-02-02 03:03:47 +000078 Printf("Usage:\n");
79 auto Prog = ProgName->c_str();
80 Printf("\nTo run fuzzing pass 0 or more directories.\n");
81 Printf("%s [-flag1=val1 [-flag2=val2 ...] ] [dir1 [dir2 ...] ]\n", Prog);
82
83 Printf("\nTo run individual tests without fuzzing pass 1 or more files:\n");
84 Printf("%s [-flag1=val1 [-flag2=val2 ...] ] file1 [file2 ...]\n", Prog);
85
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000086 Printf("\nFlags: (strictly in form -flag=value)\n");
Kostya Serebryany016852c2015-02-19 18:45:37 +000087 size_t MaxFlagLen = 0;
88 for (size_t F = 0; F < kNumFlags; F++)
89 MaxFlagLen = std::max(strlen(FlagDescriptions[F].Name), MaxFlagLen);
90
91 for (size_t F = 0; F < kNumFlags; F++) {
92 const auto &D = FlagDescriptions[F];
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000093 Printf(" %s", D.Name);
Kostya Serebryany016852c2015-02-19 18:45:37 +000094 for (size_t i = 0, n = MaxFlagLen - strlen(D.Name); i < n; i++)
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000095 Printf(" ");
96 Printf("\t");
97 Printf("%d\t%s\n", D.Default, D.Description);
Kostya Serebryany016852c2015-02-19 18:45:37 +000098 }
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000099 Printf("\nFlags starting with '--' will be ignored and "
100 "will be passed verbatim to subprocesses.\n");
Kostya Serebryany016852c2015-02-19 18:45:37 +0000101}
102
103static const char *FlagValue(const char *Param, const char *Name) {
104 size_t Len = strlen(Name);
105 if (Param[0] == '-' && strstr(Param + 1, Name) == Param + 1 &&
106 Param[Len + 1] == '=')
107 return &Param[Len + 2];
108 return nullptr;
109}
110
Kostya Serebryany4282d302016-01-15 00:17:37 +0000111// Avoid calling stol as it triggers a bug in clang/glibc build.
112static long MyStol(const char *Str) {
113 long Res = 0;
Kostya Serebryany311f27c2016-01-19 20:33:57 +0000114 long Sign = 1;
115 if (*Str == '-') {
116 Str++;
117 Sign = -1;
118 }
Kostya Serebryany4282d302016-01-15 00:17:37 +0000119 for (size_t i = 0; Str[i]; i++) {
120 char Ch = Str[i];
121 if (Ch < '0' || Ch > '9')
122 return Res;
123 Res = Res * 10 + (Ch - '0');
124 }
Kostya Serebryany311f27c2016-01-19 20:33:57 +0000125 return Res * Sign;
Kostya Serebryany4282d302016-01-15 00:17:37 +0000126}
127
Kostya Serebryany016852c2015-02-19 18:45:37 +0000128static bool ParseOneFlag(const char *Param) {
129 if (Param[0] != '-') return false;
Kostya Serebryany71e0feb2015-05-21 20:39:13 +0000130 if (Param[1] == '-') {
131 static bool PrintedWarning = false;
132 if (!PrintedWarning) {
133 PrintedWarning = true;
Kostya Serebryany64d24572016-03-12 01:57:04 +0000134 Printf("INFO: libFuzzer ignores flags that start with '--'\n");
Kostya Serebryany71e0feb2015-05-21 20:39:13 +0000135 }
Kostya Serebryanyb60397f2016-04-15 21:56:29 +0000136 for (size_t F = 0; F < kNumFlags; F++)
137 if (FlagValue(Param + 1, FlagDescriptions[F].Name))
138 Printf("WARNING: did you mean '%s' (single dash)?\n", Param + 1);
Kostya Serebryany71e0feb2015-05-21 20:39:13 +0000139 return true;
140 }
Kostya Serebryany016852c2015-02-19 18:45:37 +0000141 for (size_t F = 0; F < kNumFlags; F++) {
142 const char *Name = FlagDescriptions[F].Name;
143 const char *Str = FlagValue(Param, Name);
144 if (Str) {
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000145 if (FlagDescriptions[F].IntFlag) {
Kostya Serebryany4282d302016-01-15 00:17:37 +0000146 int Val = MyStol(Str);
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000147 *FlagDescriptions[F].IntFlag = Val;
148 if (Flags.verbosity >= 2)
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000149 Printf("Flag: %s %d\n", Name, Val);;
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000150 return true;
Mike Aizatskya1a5c692015-12-10 20:41:53 +0000151 } else if (FlagDescriptions[F].UIntFlag) {
152 unsigned int Val = std::stoul(Str);
153 *FlagDescriptions[F].UIntFlag = Val;
154 if (Flags.verbosity >= 2)
155 Printf("Flag: %s %u\n", Name, Val);
156 return true;
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000157 } else if (FlagDescriptions[F].StrFlag) {
158 *FlagDescriptions[F].StrFlag = Str;
159 if (Flags.verbosity >= 2)
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000160 Printf("Flag: %s %s\n", Name, Str);
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000161 return true;
Kostya Serebryany3d95dd92016-03-01 22:33:14 +0000162 } else { // Deprecated flag.
163 Printf("Flag: %s: deprecated, don't use\n", Name);
164 return true;
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000165 }
Kostya Serebryany016852c2015-02-19 18:45:37 +0000166 }
167 }
168 PrintHelp();
169 exit(1);
170}
171
172// We don't use any library to minimize dependencies.
Kostya Serebryanya938bcb2015-09-10 16:57:57 +0000173static void ParseFlags(const std::vector<std::string> &Args) {
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000174 for (size_t F = 0; F < kNumFlags; F++) {
175 if (FlagDescriptions[F].IntFlag)
176 *FlagDescriptions[F].IntFlag = FlagDescriptions[F].Default;
Mike Aizatskya1a5c692015-12-10 20:41:53 +0000177 if (FlagDescriptions[F].UIntFlag)
178 *FlagDescriptions[F].UIntFlag =
179 static_cast<unsigned int>(FlagDescriptions[F].Default);
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000180 if (FlagDescriptions[F].StrFlag)
181 *FlagDescriptions[F].StrFlag = nullptr;
182 }
Kostya Serebryanya938bcb2015-09-10 16:57:57 +0000183 Inputs = new std::vector<std::string>;
184 for (size_t A = 1; A < Args.size(); A++) {
185 if (ParseOneFlag(Args[A].c_str())) continue;
186 Inputs->push_back(Args[A]);
Kostya Serebryany016852c2015-02-19 18:45:37 +0000187 }
188}
189
Kostya Serebryany83fd4862015-05-11 21:31:51 +0000190static std::mutex Mu;
191
192static void PulseThread() {
193 while (true) {
194 std::this_thread::sleep_for(std::chrono::seconds(600));
195 std::lock_guard<std::mutex> Lock(Mu);
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000196 Printf("pulse...\n");
Kostya Serebryany83fd4862015-05-11 21:31:51 +0000197 }
198}
199
Kostya Serebryany016852c2015-02-19 18:45:37 +0000200static void WorkerThread(const std::string &Cmd, std::atomic<int> *Counter,
201 int NumJobs, std::atomic<bool> *HasErrors) {
Kostya Serebryany016852c2015-02-19 18:45:37 +0000202 while (true) {
203 int C = (*Counter)++;
204 if (C >= NumJobs) break;
205 std::string Log = "fuzz-" + std::to_string(C) + ".log";
206 std::string ToRun = Cmd + " > " + Log + " 2>&1\n";
207 if (Flags.verbosity)
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000208 Printf("%s", ToRun.c_str());
Hans Wennborge6319962016-04-11 20:35:17 +0000209 int ExitCode = ExecuteCommand(ToRun);
Kostya Serebryany016852c2015-02-19 18:45:37 +0000210 if (ExitCode != 0)
211 *HasErrors = true;
Kostya Serebryany83fd4862015-05-11 21:31:51 +0000212 std::lock_guard<std::mutex> Lock(Mu);
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000213 Printf("================== Job %d exited with exit code %d ============\n",
214 C, ExitCode);
Kostya Serebryany016852c2015-02-19 18:45:37 +0000215 fuzzer::CopyFileToErr(Log);
216 }
217}
218
Kostya Serebryanya938bcb2015-09-10 16:57:57 +0000219static int RunInMultipleProcesses(const std::vector<std::string> &Args,
220 int NumWorkers, int NumJobs) {
Kostya Serebryany016852c2015-02-19 18:45:37 +0000221 std::atomic<int> Counter(0);
222 std::atomic<bool> HasErrors(false);
223 std::string Cmd;
Kostya Serebryanya938bcb2015-09-10 16:57:57 +0000224 for (auto &S : Args) {
225 if (FlagValue(S.c_str(), "jobs") || FlagValue(S.c_str(), "workers"))
226 continue;
227 Cmd += S + " ";
Kostya Serebryany016852c2015-02-19 18:45:37 +0000228 }
229 std::vector<std::thread> V;
Kostya Serebryany83fd4862015-05-11 21:31:51 +0000230 std::thread Pulse(PulseThread);
Kostya Serebryanycd7629c2015-05-12 01:43:20 +0000231 Pulse.detach();
Kostya Serebryany016852c2015-02-19 18:45:37 +0000232 for (int i = 0; i < NumWorkers; i++)
233 V.push_back(std::thread(WorkerThread, Cmd, &Counter, NumJobs, &HasErrors));
234 for (auto &T : V)
235 T.join();
236 return HasErrors ? 1 : 0;
237}
238
Ivan Krasin95e82d52015-10-01 23:23:06 +0000239int RunOneTest(Fuzzer *F, const char *InputFilePath) {
240 Unit U = FileToVector(InputFilePath);
Kostya Serebryany856b7af2015-11-03 18:57:25 +0000241 Unit PreciseSizedU(U);
242 assert(PreciseSizedU.size() == PreciseSizedU.capacity());
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000243 F->ExecuteCallback(PreciseSizedU.data(), PreciseSizedU.size());
Ivan Krasin95e82d52015-10-01 23:23:06 +0000244 return 0;
245}
246
Kostya Serebryanybfbe7fc2016-02-02 03:03:47 +0000247static bool AllInputsAreFiles() {
248 if (Inputs->empty()) return false;
249 for (auto &Path : *Inputs)
250 if (!IsFile(Path))
251 return false;
252 return true;
253}
254
Kostya Serebryany29bcb9f2016-02-13 03:59:26 +0000255static int FuzzerDriver(const std::vector<std::string> &Args,
256 UserCallback Callback) {
Kostya Serebryanya938bcb2015-09-10 16:57:57 +0000257 using namespace fuzzer;
258 assert(!Args.empty());
259 ProgName = new std::string(Args[0]);
260 ParseFlags(Args);
Kostya Serebryany016852c2015-02-19 18:45:37 +0000261 if (Flags.help) {
262 PrintHelp();
263 return 0;
264 }
265
Kostya Serebryany49e40902016-03-18 20:58:29 +0000266 if (Flags.close_fd_mask & 2)
267 DupAndCloseStderr();
268 if (Flags.close_fd_mask & 1)
269 CloseStdout();
270
Kostya Serebryany9690fcf2015-05-12 18:51:57 +0000271 if (Flags.jobs > 0 && Flags.workers == 0) {
272 Flags.workers = std::min(NumberOfCpuCores() / 2, Flags.jobs);
273 if (Flags.workers > 1)
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000274 Printf("Running %d workers\n", Flags.workers);
Kostya Serebryany9690fcf2015-05-12 18:51:57 +0000275 }
276
Kostya Serebryany016852c2015-02-19 18:45:37 +0000277 if (Flags.workers > 0 && Flags.jobs > 0)
Kostya Serebryanya938bcb2015-09-10 16:57:57 +0000278 return RunInMultipleProcesses(Args, Flags.workers, Flags.jobs);
Kostya Serebryany016852c2015-02-19 18:45:37 +0000279
Kostya Serebryany64d24572016-03-12 01:57:04 +0000280 const size_t kMaxSaneLen = 1 << 20;
Kostya Serebryany0c5e3af2016-03-15 01:28:00 +0000281 const size_t kMinDefaultLen = 64;
Kostya Serebryany016852c2015-02-19 18:45:37 +0000282 Fuzzer::FuzzingOptions Options;
283 Options.Verbosity = Flags.verbosity;
284 Options.MaxLen = Flags.max_len;
Kostya Serebryany490bbd62015-05-19 22:12:57 +0000285 Options.UnitTimeoutSec = Flags.timeout;
Kostya Serebryany54a63632016-01-29 23:30:07 +0000286 Options.TimeoutExitCode = Flags.timeout_exitcode;
Kostya Serebryanyb85db172015-10-02 20:47:55 +0000287 Options.MaxTotalTimeSec = Flags.max_total_time;
Kostya Serebryany016852c2015-02-19 18:45:37 +0000288 Options.DoCrossOver = Flags.cross_over;
289 Options.MutateDepth = Flags.mutate_depth;
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +0000290 Options.UseCounters = Flags.use_counters;
Kostya Serebryany2e9fca92015-10-22 23:55:39 +0000291 Options.UseIndirCalls = Flags.use_indir_calls;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000292 Options.UseTraces = Flags.use_traces;
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000293 Options.UseMemcmp = Flags.use_memcmp;
Kostya Serebryanyfed509e2015-10-17 04:38:26 +0000294 Options.ShuffleAtStartUp = Flags.shuffle;
Kostya Serebryany945761b2016-03-18 00:23:29 +0000295 Options.PreferSmall = Flags.prefer_small;
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000296 Options.Reload = Flags.reload;
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +0000297 Options.OnlyASCII = Flags.only_ascii;
Mike Aizatskya9c23872015-11-12 04:38:40 +0000298 Options.OutputCSV = Flags.output_csv;
Kostya Serebryany016852c2015-02-19 18:45:37 +0000299 if (Flags.runs >= 0)
300 Options.MaxNumberOfRuns = Flags.runs;
Kostya Serebryanya938bcb2015-09-10 16:57:57 +0000301 if (!Inputs->empty())
302 Options.OutputCorpus = (*Inputs)[0];
Kostya Serebryany70926ae2015-08-05 21:43:48 +0000303 Options.ReportSlowUnits = Flags.report_slow_units;
Kostya Serebryanybd5d1cd2015-10-09 03:57:59 +0000304 if (Flags.artifact_prefix)
305 Options.ArtifactPrefix = Flags.artifact_prefix;
Kostya Serebryany2d0ef142015-11-25 21:40:46 +0000306 if (Flags.exact_artifact_path)
307 Options.ExactArtifactPath = Flags.exact_artifact_path;
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000308 std::vector<Unit> Dictionary;
Kostya Serebryany7d211662015-09-04 00:12:11 +0000309 if (Flags.dict)
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000310 if (!ParseDictionaryFile(FileToString(Flags.dict), &Dictionary))
Kostya Serebryany7d211662015-09-04 00:12:11 +0000311 return 1;
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000312 if (Flags.verbosity > 0 && !Dictionary.empty())
313 Printf("Dictionary: %zd entries\n", Dictionary.size());
Kostya Serebryanyc5575aa2016-03-17 19:59:39 +0000314 bool DoPlainRun = AllInputsAreFiles();
315 Options.SaveArtifacts = !DoPlainRun;
Mike Aizatsky8b11f872016-01-06 00:21:22 +0000316 Options.PrintNewCovPcs = Flags.print_new_cov_pcs;
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000317 Options.PrintFinalStats = Flags.print_final_stats;
Kostya Serebryany7d211662015-09-04 00:12:11 +0000318
Kostya Serebryanya3992212016-02-13 03:00:53 +0000319 unsigned Seed = Flags.seed;
320 // Initialize Seed.
321 if (Seed == 0)
322 Seed = (std::chrono::system_clock::now().time_since_epoch().count() << 10) +
323 getpid();
324 if (Flags.verbosity)
Kostya Serebryany64d24572016-03-12 01:57:04 +0000325 Printf("INFO: Seed: %u\n", Seed);
Kostya Serebryanya3992212016-02-13 03:00:53 +0000326
327 Random Rand(Seed);
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000328 MutationDispatcher MD(Rand);
329 Fuzzer F(Callback, MD, Options);
Kostya Serebryany016852c2015-02-19 18:45:37 +0000330
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000331 for (auto &U: Dictionary)
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000332 if (U.size() <= Word::GetMaxSize())
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000333 MD.AddWordToManualDictionary(Word(U.data(), U.size()));
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000334
Kostya Serebryanyb91c62b2015-10-16 22:41:47 +0000335 // Timer
336 if (Flags.timeout > 0)
337 SetTimer(Flags.timeout / 2 + 1);
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000338 if (Flags.handle_segv) SetSigSegvHandler();
339 if (Flags.handle_bus) SetSigBusHandler();
340 if (Flags.handle_abrt) SetSigAbrtHandler();
341 if (Flags.handle_ill) SetSigIllHandler();
342 if (Flags.handle_fpe) SetSigFpeHandler();
343 if (Flags.handle_int) SetSigIntHandler();
Kostya Serebryanyf389ae12016-03-24 21:03:58 +0000344 if (Flags.handle_term) SetSigTermHandler();
Kostya Serebryanyb91c62b2015-10-16 22:41:47 +0000345
Kostya Serebryanyc5575aa2016-03-17 19:59:39 +0000346 if (DoPlainRun) {
347 Options.SaveArtifacts = false;
Kostya Serebryany9d14e4b2016-02-12 02:32:03 +0000348 int Runs = std::max(1, Flags.runs);
349 Printf("%s: Running %zd inputs %d time(s) each.\n", ProgName->c_str(),
350 Inputs->size(), Runs);
Kostya Serebryanybfbe7fc2016-02-02 03:03:47 +0000351 for (auto &Path : *Inputs) {
352 auto StartTime = system_clock::now();
Kostya Serebryany5c3701c2016-03-04 22:35:40 +0000353 for (int Iter = 0; Iter < Runs; Iter++)
Kostya Serebryany9d14e4b2016-02-12 02:32:03 +0000354 RunOneTest(&F, Path.c_str());
Kostya Serebryanybfbe7fc2016-02-02 03:03:47 +0000355 auto StopTime = system_clock::now();
356 auto MS = duration_cast<milliseconds>(StopTime - StartTime).count();
357 Printf("%s: %zd ms\n", Path.c_str(), (long)MS);
358 }
359 exit(0);
360 }
361
Kostya Serebryany64d24572016-03-12 01:57:04 +0000362
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000363 if (Flags.merge) {
Kostya Serebryany64d24572016-03-12 01:57:04 +0000364 if (Options.MaxLen == 0)
365 F.SetMaxLen(kMaxSaneLen);
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000366 F.Merge(*Inputs);
367 exit(0);
368 }
369
Kostya Serebryany64d24572016-03-12 01:57:04 +0000370 size_t TemporaryMaxLen = Options.MaxLen ? Options.MaxLen : kMaxSaneLen;
Kostya Serebryany016852c2015-02-19 18:45:37 +0000371
Kostya Serebryany64d24572016-03-12 01:57:04 +0000372 F.RereadOutputCorpus(TemporaryMaxLen);
Kostya Serebryanya938bcb2015-09-10 16:57:57 +0000373 for (auto &inp : *Inputs)
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000374 if (inp != Options.OutputCorpus)
Kostya Serebryany64d24572016-03-12 01:57:04 +0000375 F.ReadDir(inp, nullptr, TemporaryMaxLen);
376
377 if (Options.MaxLen == 0)
378 F.SetMaxLen(
Kostya Serebryany0c5e3af2016-03-15 01:28:00 +0000379 std::min(std::max(kMinDefaultLen, F.MaxUnitSizeInCorpus()), kMaxSaneLen));
Kostya Serebryany016852c2015-02-19 18:45:37 +0000380
381 if (F.CorpusSize() == 0)
382 F.AddToCorpus(Unit()); // Can't fuzz empty corpus, so add an empty input.
383 F.ShuffleAndMinimize();
Kostya Serebryany550e9c82015-12-19 03:42:16 +0000384 if (Flags.drill)
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000385 F.Drill();
386 else
387 F.Loop();
388
Kostya Serebryany016852c2015-02-19 18:45:37 +0000389 if (Flags.verbosity)
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000390 Printf("Done %d runs in %zd second(s)\n", F.getTotalNumberOfRuns(),
391 F.secondsSinceProcessStartUp());
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000392 F.PrintFinalStats();
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000393
Kostya Serebryanyc8cd29f2015-10-03 07:02:05 +0000394 exit(0); // Don't let F destroy itself.
Kostya Serebryany016852c2015-02-19 18:45:37 +0000395}
396
Kostya Serebryany29bcb9f2016-02-13 03:59:26 +0000397int FuzzerDriver(int argc, char **argv, UserCallback Callback) {
398 std::vector<std::string> Args(argv, argv + argc);
399 return FuzzerDriver(Args, Callback);
400}
401
Kostya Serebryany016852c2015-02-19 18:45:37 +0000402} // namespace fuzzer