blob: 6cdbe4f34d622657f040cc241c5f8150b5f98ae4 [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 Serebryany52a788e2015-03-31 20:13:20 +000043#define FUZZER_FLAG_INT(Name, Default, Description) int Name;
Mike Aizatskya1a5c692015-12-10 20:41:53 +000044#define FUZZER_FLAG_UNSIGNED(Name, Default, Description) unsigned int Name;
Kostya Serebryany52a788e2015-03-31 20:13:20 +000045#define FUZZER_FLAG_STRING(Name, Description) const char *Name;
Kostya Serebryany016852c2015-02-19 18:45:37 +000046#include "FuzzerFlags.def"
Kostya Serebryany52a788e2015-03-31 20:13:20 +000047#undef FUZZER_FLAG_INT
Mike Aizatskya1a5c692015-12-10 20:41:53 +000048#undef FUZZER_FLAG_UNSIGNED
Kostya Serebryany52a788e2015-03-31 20:13:20 +000049#undef FUZZER_FLAG_STRING
Kostya Serebryany016852c2015-02-19 18:45:37 +000050} Flags;
51
Craig Topper26260942015-10-18 05:15:34 +000052static const FlagDescription FlagDescriptions [] {
Kostya Serebryany52a788e2015-03-31 20:13:20 +000053#define FUZZER_FLAG_INT(Name, Default, Description) \
Mike Aizatskya1a5c692015-12-10 20:41:53 +000054 {#Name, Description, Default, &Flags.Name, nullptr, nullptr},
55#define FUZZER_FLAG_UNSIGNED(Name, Default, Description) \
56 {#Name, Description, static_cast<int>(Default), \
57 nullptr, nullptr, &Flags.Name},
Kostya Serebryany52a788e2015-03-31 20:13:20 +000058#define FUZZER_FLAG_STRING(Name, Description) \
Mike Aizatskya1a5c692015-12-10 20:41:53 +000059 {#Name, Description, 0, nullptr, &Flags.Name, nullptr},
Kostya Serebryany016852c2015-02-19 18:45:37 +000060#include "FuzzerFlags.def"
Kostya Serebryany52a788e2015-03-31 20:13:20 +000061#undef FUZZER_FLAG_INT
Mike Aizatskya1a5c692015-12-10 20:41:53 +000062#undef FUZZER_FLAG_UNSIGNED
Kostya Serebryany52a788e2015-03-31 20:13:20 +000063#undef FUZZER_FLAG_STRING
Kostya Serebryany016852c2015-02-19 18:45:37 +000064};
65
66static const size_t kNumFlags =
67 sizeof(FlagDescriptions) / sizeof(FlagDescriptions[0]);
68
Kostya Serebryanya938bcb2015-09-10 16:57:57 +000069static std::vector<std::string> *Inputs;
70static std::string *ProgName;
Kostya Serebryany016852c2015-02-19 18:45:37 +000071
72static void PrintHelp() {
Kostya Serebryanybfbe7fc2016-02-02 03:03:47 +000073 Printf("Usage:\n");
74 auto Prog = ProgName->c_str();
75 Printf("\nTo run fuzzing pass 0 or more directories.\n");
76 Printf("%s [-flag1=val1 [-flag2=val2 ...] ] [dir1 [dir2 ...] ]\n", Prog);
77
78 Printf("\nTo run individual tests without fuzzing pass 1 or more files:\n");
79 Printf("%s [-flag1=val1 [-flag2=val2 ...] ] file1 [file2 ...]\n", Prog);
80
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000081 Printf("\nFlags: (strictly in form -flag=value)\n");
Kostya Serebryany016852c2015-02-19 18:45:37 +000082 size_t MaxFlagLen = 0;
83 for (size_t F = 0; F < kNumFlags; F++)
84 MaxFlagLen = std::max(strlen(FlagDescriptions[F].Name), MaxFlagLen);
85
86 for (size_t F = 0; F < kNumFlags; F++) {
87 const auto &D = FlagDescriptions[F];
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000088 Printf(" %s", D.Name);
Kostya Serebryany016852c2015-02-19 18:45:37 +000089 for (size_t i = 0, n = MaxFlagLen - strlen(D.Name); i < n; i++)
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000090 Printf(" ");
91 Printf("\t");
92 Printf("%d\t%s\n", D.Default, D.Description);
Kostya Serebryany016852c2015-02-19 18:45:37 +000093 }
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000094 Printf("\nFlags starting with '--' will be ignored and "
95 "will be passed verbatim to subprocesses.\n");
Kostya Serebryany016852c2015-02-19 18:45:37 +000096}
97
98static const char *FlagValue(const char *Param, const char *Name) {
99 size_t Len = strlen(Name);
100 if (Param[0] == '-' && strstr(Param + 1, Name) == Param + 1 &&
101 Param[Len + 1] == '=')
102 return &Param[Len + 2];
103 return nullptr;
104}
105
Kostya Serebryany4282d302016-01-15 00:17:37 +0000106// Avoid calling stol as it triggers a bug in clang/glibc build.
107static long MyStol(const char *Str) {
108 long Res = 0;
Kostya Serebryany311f27c2016-01-19 20:33:57 +0000109 long Sign = 1;
110 if (*Str == '-') {
111 Str++;
112 Sign = -1;
113 }
Kostya Serebryany4282d302016-01-15 00:17:37 +0000114 for (size_t i = 0; Str[i]; i++) {
115 char Ch = Str[i];
116 if (Ch < '0' || Ch > '9')
117 return Res;
118 Res = Res * 10 + (Ch - '0');
119 }
Kostya Serebryany311f27c2016-01-19 20:33:57 +0000120 return Res * Sign;
Kostya Serebryany4282d302016-01-15 00:17:37 +0000121}
122
Kostya Serebryany016852c2015-02-19 18:45:37 +0000123static bool ParseOneFlag(const char *Param) {
124 if (Param[0] != '-') return false;
Kostya Serebryany71e0feb2015-05-21 20:39:13 +0000125 if (Param[1] == '-') {
126 static bool PrintedWarning = false;
127 if (!PrintedWarning) {
128 PrintedWarning = true;
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000129 Printf("WARNING: libFuzzer ignores flags that start with '--'\n");
Kostya Serebryany71e0feb2015-05-21 20:39:13 +0000130 }
131 return true;
132 }
Kostya Serebryany016852c2015-02-19 18:45:37 +0000133 for (size_t F = 0; F < kNumFlags; F++) {
134 const char *Name = FlagDescriptions[F].Name;
135 const char *Str = FlagValue(Param, Name);
136 if (Str) {
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000137 if (FlagDescriptions[F].IntFlag) {
Kostya Serebryany4282d302016-01-15 00:17:37 +0000138 int Val = MyStol(Str);
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000139 *FlagDescriptions[F].IntFlag = Val;
140 if (Flags.verbosity >= 2)
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000141 Printf("Flag: %s %d\n", Name, Val);;
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000142 return true;
Mike Aizatskya1a5c692015-12-10 20:41:53 +0000143 } else if (FlagDescriptions[F].UIntFlag) {
144 unsigned int Val = std::stoul(Str);
145 *FlagDescriptions[F].UIntFlag = Val;
146 if (Flags.verbosity >= 2)
147 Printf("Flag: %s %u\n", Name, Val);
148 return true;
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000149 } else if (FlagDescriptions[F].StrFlag) {
150 *FlagDescriptions[F].StrFlag = Str;
151 if (Flags.verbosity >= 2)
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000152 Printf("Flag: %s %s\n", Name, Str);
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000153 return true;
154 }
Kostya Serebryany016852c2015-02-19 18:45:37 +0000155 }
156 }
157 PrintHelp();
158 exit(1);
159}
160
161// We don't use any library to minimize dependencies.
Kostya Serebryanya938bcb2015-09-10 16:57:57 +0000162static void ParseFlags(const std::vector<std::string> &Args) {
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000163 for (size_t F = 0; F < kNumFlags; F++) {
164 if (FlagDescriptions[F].IntFlag)
165 *FlagDescriptions[F].IntFlag = FlagDescriptions[F].Default;
Mike Aizatskya1a5c692015-12-10 20:41:53 +0000166 if (FlagDescriptions[F].UIntFlag)
167 *FlagDescriptions[F].UIntFlag =
168 static_cast<unsigned int>(FlagDescriptions[F].Default);
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000169 if (FlagDescriptions[F].StrFlag)
170 *FlagDescriptions[F].StrFlag = nullptr;
171 }
Kostya Serebryanya938bcb2015-09-10 16:57:57 +0000172 Inputs = new std::vector<std::string>;
173 for (size_t A = 1; A < Args.size(); A++) {
174 if (ParseOneFlag(Args[A].c_str())) continue;
175 Inputs->push_back(Args[A]);
Kostya Serebryany016852c2015-02-19 18:45:37 +0000176 }
177}
178
Kostya Serebryany83fd4862015-05-11 21:31:51 +0000179static std::mutex Mu;
180
181static void PulseThread() {
182 while (true) {
183 std::this_thread::sleep_for(std::chrono::seconds(600));
184 std::lock_guard<std::mutex> Lock(Mu);
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000185 Printf("pulse...\n");
Kostya Serebryany83fd4862015-05-11 21:31:51 +0000186 }
187}
188
Kostya Serebryany016852c2015-02-19 18:45:37 +0000189static void WorkerThread(const std::string &Cmd, std::atomic<int> *Counter,
190 int NumJobs, std::atomic<bool> *HasErrors) {
Kostya Serebryany016852c2015-02-19 18:45:37 +0000191 while (true) {
192 int C = (*Counter)++;
193 if (C >= NumJobs) break;
194 std::string Log = "fuzz-" + std::to_string(C) + ".log";
195 std::string ToRun = Cmd + " > " + Log + " 2>&1\n";
196 if (Flags.verbosity)
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000197 Printf("%s", ToRun.c_str());
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000198 int ExitCode = ExecuteCommand(ToRun.c_str());
Kostya Serebryany016852c2015-02-19 18:45:37 +0000199 if (ExitCode != 0)
200 *HasErrors = true;
Kostya Serebryany83fd4862015-05-11 21:31:51 +0000201 std::lock_guard<std::mutex> Lock(Mu);
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000202 Printf("================== Job %d exited with exit code %d ============\n",
203 C, ExitCode);
Kostya Serebryany016852c2015-02-19 18:45:37 +0000204 fuzzer::CopyFileToErr(Log);
205 }
206}
207
Kostya Serebryanya938bcb2015-09-10 16:57:57 +0000208static int RunInMultipleProcesses(const std::vector<std::string> &Args,
209 int NumWorkers, int NumJobs) {
Kostya Serebryany016852c2015-02-19 18:45:37 +0000210 std::atomic<int> Counter(0);
211 std::atomic<bool> HasErrors(false);
212 std::string Cmd;
Kostya Serebryanya938bcb2015-09-10 16:57:57 +0000213 for (auto &S : Args) {
214 if (FlagValue(S.c_str(), "jobs") || FlagValue(S.c_str(), "workers"))
215 continue;
216 Cmd += S + " ";
Kostya Serebryany016852c2015-02-19 18:45:37 +0000217 }
218 std::vector<std::thread> V;
Kostya Serebryany83fd4862015-05-11 21:31:51 +0000219 std::thread Pulse(PulseThread);
Kostya Serebryanycd7629c2015-05-12 01:43:20 +0000220 Pulse.detach();
Kostya Serebryany016852c2015-02-19 18:45:37 +0000221 for (int i = 0; i < NumWorkers; i++)
222 V.push_back(std::thread(WorkerThread, Cmd, &Counter, NumJobs, &HasErrors));
223 for (auto &T : V)
224 T.join();
225 return HasErrors ? 1 : 0;
226}
227
Ivan Krasin95e82d52015-10-01 23:23:06 +0000228int RunOneTest(Fuzzer *F, const char *InputFilePath) {
229 Unit U = FileToVector(InputFilePath);
Kostya Serebryany856b7af2015-11-03 18:57:25 +0000230 Unit PreciseSizedU(U);
231 assert(PreciseSizedU.size() == PreciseSizedU.capacity());
232 F->ExecuteCallback(PreciseSizedU);
Ivan Krasin95e82d52015-10-01 23:23:06 +0000233 return 0;
234}
235
Kostya Serebryanybfbe7fc2016-02-02 03:03:47 +0000236static bool AllInputsAreFiles() {
237 if (Inputs->empty()) return false;
238 for (auto &Path : *Inputs)
239 if (!IsFile(Path))
240 return false;
241 return true;
242}
243
Kostya Serebryany016852c2015-02-19 18:45:37 +0000244int FuzzerDriver(int argc, char **argv, UserCallback Callback) {
Kostya Serebryany311f27c2016-01-19 20:33:57 +0000245 FuzzerRandom_mt19937 Rand(0);
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000246 SimpleUserSuppliedFuzzer SUSF(&Rand, Callback);
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000247 return FuzzerDriver(argc, argv, SUSF);
248}
249
250int FuzzerDriver(int argc, char **argv, UserSuppliedFuzzer &USF) {
Kostya Serebryanya938bcb2015-09-10 16:57:57 +0000251 std::vector<std::string> Args(argv, argv + argc);
252 return FuzzerDriver(Args, USF);
253}
Kostya Serebryany016852c2015-02-19 18:45:37 +0000254
Kostya Serebryanya938bcb2015-09-10 16:57:57 +0000255int FuzzerDriver(const std::vector<std::string> &Args, UserCallback Callback) {
Kostya Serebryany311f27c2016-01-19 20:33:57 +0000256 FuzzerRandom_mt19937 Rand(0);
Kostya Serebryanya938bcb2015-09-10 16:57:57 +0000257 SimpleUserSuppliedFuzzer SUSF(&Rand, Callback);
258 return FuzzerDriver(Args, SUSF);
259}
260
261int FuzzerDriver(const std::vector<std::string> &Args,
262 UserSuppliedFuzzer &USF) {
263 using namespace fuzzer;
264 assert(!Args.empty());
265 ProgName = new std::string(Args[0]);
266 ParseFlags(Args);
Kostya Serebryany016852c2015-02-19 18:45:37 +0000267 if (Flags.help) {
268 PrintHelp();
269 return 0;
270 }
271
Kostya Serebryany9690fcf2015-05-12 18:51:57 +0000272 if (Flags.jobs > 0 && Flags.workers == 0) {
273 Flags.workers = std::min(NumberOfCpuCores() / 2, Flags.jobs);
274 if (Flags.workers > 1)
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000275 Printf("Running %d workers\n", Flags.workers);
Kostya Serebryany9690fcf2015-05-12 18:51:57 +0000276 }
277
Kostya Serebryany016852c2015-02-19 18:45:37 +0000278 if (Flags.workers > 0 && Flags.jobs > 0)
Kostya Serebryanya938bcb2015-09-10 16:57:57 +0000279 return RunInMultipleProcesses(Args, Flags.workers, Flags.jobs);
Kostya Serebryany016852c2015-02-19 18:45:37 +0000280
281 Fuzzer::FuzzingOptions Options;
282 Options.Verbosity = Flags.verbosity;
283 Options.MaxLen = Flags.max_len;
Kostya Serebryany490bbd62015-05-19 22:12:57 +0000284 Options.UnitTimeoutSec = Flags.timeout;
Kostya Serebryany9768e7f2016-01-23 19:34:19 +0000285 Options.AbortOnTimeout = Flags.abort_on_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;
290 Options.ExitOnFirst = Flags.exit_on_first;
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +0000291 Options.UseCounters = Flags.use_counters;
Kostya Serebryany2e9fca92015-10-22 23:55:39 +0000292 Options.UseIndirCalls = Flags.use_indir_calls;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000293 Options.UseTraces = Flags.use_traces;
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000294 Options.UseMemcmp = Flags.use_memcmp;
Kostya Serebryanyfed509e2015-10-17 04:38:26 +0000295 Options.ShuffleAtStartUp = Flags.shuffle;
Kostya Serebryany016852c2015-02-19 18:45:37 +0000296 Options.PreferSmallDuringInitialShuffle =
297 Flags.prefer_small_during_initial_shuffle;
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000298 Options.Reload = Flags.reload;
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +0000299 Options.OnlyASCII = Flags.only_ascii;
Mike Aizatskya9c23872015-11-12 04:38:40 +0000300 Options.OutputCSV = Flags.output_csv;
Kostya Serebryany016852c2015-02-19 18:45:37 +0000301 if (Flags.runs >= 0)
302 Options.MaxNumberOfRuns = Flags.runs;
Kostya Serebryanya938bcb2015-09-10 16:57:57 +0000303 if (!Inputs->empty())
304 Options.OutputCorpus = (*Inputs)[0];
Kostya Serebryany2da7b842015-05-18 21:34:20 +0000305 if (Flags.sync_command)
306 Options.SyncCommand = Flags.sync_command;
307 Options.SyncTimeout = Flags.sync_timeout;
Kostya Serebryany70926ae2015-08-05 21:43:48 +0000308 Options.ReportSlowUnits = Flags.report_slow_units;
Kostya Serebryanybd5d1cd2015-10-09 03:57:59 +0000309 if (Flags.artifact_prefix)
310 Options.ArtifactPrefix = Flags.artifact_prefix;
Kostya Serebryany2d0ef142015-11-25 21:40:46 +0000311 if (Flags.exact_artifact_path)
312 Options.ExactArtifactPath = Flags.exact_artifact_path;
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000313 std::vector<Unit> Dictionary;
Kostya Serebryany7d211662015-09-04 00:12:11 +0000314 if (Flags.dict)
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000315 if (!ParseDictionaryFile(FileToString(Flags.dict), &Dictionary))
Kostya Serebryany7d211662015-09-04 00:12:11 +0000316 return 1;
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000317 if (Flags.verbosity > 0 && !Dictionary.empty())
318 Printf("Dictionary: %zd entries\n", Dictionary.size());
Kostya Serebryanyb91c62b2015-10-16 22:41:47 +0000319 Options.SaveArtifacts = !Flags.test_single_input;
Mike Aizatsky8b11f872016-01-06 00:21:22 +0000320 Options.PrintNewCovPcs = Flags.print_new_cov_pcs;
Kostya Serebryany7d211662015-09-04 00:12:11 +0000321
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000322 Fuzzer F(USF, Options);
Kostya Serebryany016852c2015-02-19 18:45:37 +0000323
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000324 for (auto &U: Dictionary)
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000325 if (U.size() <= Word::GetMaxSize())
326 USF.GetMD().AddWordToManualDictionary(Word(U.data(), U.size()));
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000327
Kostya Serebryanyb91c62b2015-10-16 22:41:47 +0000328 // Timer
329 if (Flags.timeout > 0)
330 SetTimer(Flags.timeout / 2 + 1);
331
Kostya Serebryanyb5693682015-11-21 03:46:43 +0000332 if (Flags.test_single_input) {
333 RunOneTest(&F, Flags.test_single_input);
334 exit(0);
335 }
Ivan Krasin95e82d52015-10-01 23:23:06 +0000336
Kostya Serebryanybfbe7fc2016-02-02 03:03:47 +0000337 if (AllInputsAreFiles()) {
Kostya Serebryany9d14e4b2016-02-12 02:32:03 +0000338 int Runs = std::max(1, Flags.runs);
339 Printf("%s: Running %zd inputs %d time(s) each.\n", ProgName->c_str(),
340 Inputs->size(), Runs);
Kostya Serebryanybfbe7fc2016-02-02 03:03:47 +0000341 for (auto &Path : *Inputs) {
342 auto StartTime = system_clock::now();
Kostya Serebryany9d14e4b2016-02-12 02:32:03 +0000343 while (Runs-- > 0)
344 RunOneTest(&F, Path.c_str());
Kostya Serebryanybfbe7fc2016-02-02 03:03:47 +0000345 auto StopTime = system_clock::now();
346 auto MS = duration_cast<milliseconds>(StopTime - StartTime).count();
347 Printf("%s: %zd ms\n", Path.c_str(), (long)MS);
348 }
349 exit(0);
350 }
351
Kostya Serebryany550e9c82015-12-19 03:42:16 +0000352 if (Flags.save_minimized_corpus) {
353 Printf("The flag -save_minimized_corpus is deprecated; use -merge=1\n");
354 exit(1);
355 }
356
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000357 if (Flags.merge) {
358 F.Merge(*Inputs);
359 exit(0);
360 }
361
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000362 unsigned Seed = Flags.seed;
363 // Initialize Seed.
364 if (Seed == 0)
Kostya Serebryany311f27c2016-01-19 20:33:57 +0000365 Seed = (std::chrono::system_clock::now().time_since_epoch().count() << 10) +
366 getpid();
Kostya Serebryany016852c2015-02-19 18:45:37 +0000367 if (Flags.verbosity)
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000368 Printf("Seed: %u\n", Seed);
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000369 USF.GetRand().ResetSeed(Seed);
Kostya Serebryany016852c2015-02-19 18:45:37 +0000370
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000371 F.RereadOutputCorpus();
Kostya Serebryanya938bcb2015-09-10 16:57:57 +0000372 for (auto &inp : *Inputs)
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000373 if (inp != Options.OutputCorpus)
374 F.ReadDir(inp, nullptr);
Kostya Serebryany016852c2015-02-19 18:45:37 +0000375
376 if (F.CorpusSize() == 0)
377 F.AddToCorpus(Unit()); // Can't fuzz empty corpus, so add an empty input.
378 F.ShuffleAndMinimize();
Kostya Serebryany550e9c82015-12-19 03:42:16 +0000379 if (Flags.drill)
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000380 F.Drill();
381 else
382 F.Loop();
383
Kostya Serebryany016852c2015-02-19 18:45:37 +0000384 if (Flags.verbosity)
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000385 Printf("Done %d runs in %zd second(s)\n", F.getTotalNumberOfRuns(),
386 F.secondsSinceProcessStartUp());
387
Kostya Serebryanyc8cd29f2015-10-03 07:02:05 +0000388 exit(0); // Don't let F destroy itself.
Kostya Serebryany016852c2015-02-19 18:45:37 +0000389}
390
391} // namespace fuzzer