blob: a6db9fa338b6ee444e68836fc6af01ac7a899364 [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
Mehdi Aminib550cb12016-04-18 09:17:29 +000015#include <algorithm>
Kostya Serebryany016852c2015-02-19 18:45:37 +000016#include <atomic>
Mehdi Aminib550cb12016-04-18 09:17:29 +000017#include <chrono>
18#include <cstring>
Kostya Serebryany016852c2015-02-19 18:45:37 +000019#include <mutex>
Kostya Serebryany52a788e2015-03-31 20:13:20 +000020#include <string>
Mehdi Aminib550cb12016-04-18 09:17:29 +000021#include <thread>
22#include <unistd.h>
Kostya Serebryany016852c2015-02-19 18:45:37 +000023
Kostya Serebryany4282d302016-01-15 00:17:37 +000024// This function should be present in the libFuzzer so that the client
25// binary can test for its existence.
26extern "C" __attribute__((used)) void __libfuzzer_is_present() {}
27
Kostya Serebryany016852c2015-02-19 18:45:37 +000028namespace fuzzer {
29
30// Program arguments.
31struct FlagDescription {
32 const char *Name;
33 const char *Description;
34 int Default;
Kostya Serebryany52a788e2015-03-31 20:13:20 +000035 int *IntFlag;
36 const char **StrFlag;
Mike Aizatskya1a5c692015-12-10 20:41:53 +000037 unsigned int *UIntFlag;
Kostya Serebryany016852c2015-02-19 18:45:37 +000038};
39
40struct {
Kostya Serebryany3d95dd92016-03-01 22:33:14 +000041#define FUZZER_DEPRECATED_FLAG(Name)
Kostya Serebryany52a788e2015-03-31 20:13:20 +000042#define FUZZER_FLAG_INT(Name, Default, Description) int Name;
Mike Aizatskya1a5c692015-12-10 20:41:53 +000043#define FUZZER_FLAG_UNSIGNED(Name, Default, Description) unsigned int Name;
Kostya Serebryany52a788e2015-03-31 20:13:20 +000044#define FUZZER_FLAG_STRING(Name, Description) const char *Name;
Kostya Serebryany016852c2015-02-19 18:45:37 +000045#include "FuzzerFlags.def"
Kostya Serebryany3d95dd92016-03-01 22:33:14 +000046#undef FUZZER_DEPRECATED_FLAG
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 Serebryany3d95dd92016-03-01 22:33:14 +000053#define FUZZER_DEPRECATED_FLAG(Name) \
54 {#Name, "Deprecated; don't use", 0, nullptr, nullptr, nullptr},
Kostya Serebryany52a788e2015-03-31 20:13:20 +000055#define FUZZER_FLAG_INT(Name, Default, Description) \
Mike Aizatskya1a5c692015-12-10 20:41:53 +000056 {#Name, Description, Default, &Flags.Name, nullptr, nullptr},
57#define FUZZER_FLAG_UNSIGNED(Name, Default, Description) \
58 {#Name, Description, static_cast<int>(Default), \
59 nullptr, nullptr, &Flags.Name},
Kostya Serebryany52a788e2015-03-31 20:13:20 +000060#define FUZZER_FLAG_STRING(Name, Description) \
Mike Aizatskya1a5c692015-12-10 20:41:53 +000061 {#Name, Description, 0, nullptr, &Flags.Name, nullptr},
Kostya Serebryany016852c2015-02-19 18:45:37 +000062#include "FuzzerFlags.def"
Kostya Serebryany3d95dd92016-03-01 22:33:14 +000063#undef FUZZER_DEPRECATED_FLAG
Kostya Serebryany52a788e2015-03-31 20:13:20 +000064#undef FUZZER_FLAG_INT
Mike Aizatskya1a5c692015-12-10 20:41:53 +000065#undef FUZZER_FLAG_UNSIGNED
Kostya Serebryany52a788e2015-03-31 20:13:20 +000066#undef FUZZER_FLAG_STRING
Kostya Serebryany016852c2015-02-19 18:45:37 +000067};
68
69static const size_t kNumFlags =
70 sizeof(FlagDescriptions) / sizeof(FlagDescriptions[0]);
71
Kostya Serebryanya938bcb2015-09-10 16:57:57 +000072static std::vector<std::string> *Inputs;
73static std::string *ProgName;
Kostya Serebryany016852c2015-02-19 18:45:37 +000074
75static void PrintHelp() {
Kostya Serebryanybfbe7fc2016-02-02 03:03:47 +000076 Printf("Usage:\n");
77 auto Prog = ProgName->c_str();
78 Printf("\nTo run fuzzing pass 0 or more directories.\n");
79 Printf("%s [-flag1=val1 [-flag2=val2 ...] ] [dir1 [dir2 ...] ]\n", Prog);
80
81 Printf("\nTo run individual tests without fuzzing pass 1 or more files:\n");
82 Printf("%s [-flag1=val1 [-flag2=val2 ...] ] file1 [file2 ...]\n", Prog);
83
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000084 Printf("\nFlags: (strictly in form -flag=value)\n");
Kostya Serebryany016852c2015-02-19 18:45:37 +000085 size_t MaxFlagLen = 0;
86 for (size_t F = 0; F < kNumFlags; F++)
87 MaxFlagLen = std::max(strlen(FlagDescriptions[F].Name), MaxFlagLen);
88
89 for (size_t F = 0; F < kNumFlags; F++) {
90 const auto &D = FlagDescriptions[F];
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000091 Printf(" %s", D.Name);
Kostya Serebryany016852c2015-02-19 18:45:37 +000092 for (size_t i = 0, n = MaxFlagLen - strlen(D.Name); i < n; i++)
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000093 Printf(" ");
94 Printf("\t");
95 Printf("%d\t%s\n", D.Default, D.Description);
Kostya Serebryany016852c2015-02-19 18:45:37 +000096 }
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000097 Printf("\nFlags starting with '--' will be ignored and "
98 "will be passed verbatim to subprocesses.\n");
Kostya Serebryany016852c2015-02-19 18:45:37 +000099}
100
101static const char *FlagValue(const char *Param, const char *Name) {
102 size_t Len = strlen(Name);
103 if (Param[0] == '-' && strstr(Param + 1, Name) == Param + 1 &&
104 Param[Len + 1] == '=')
105 return &Param[Len + 2];
106 return nullptr;
107}
108
Kostya Serebryany4282d302016-01-15 00:17:37 +0000109// Avoid calling stol as it triggers a bug in clang/glibc build.
110static long MyStol(const char *Str) {
111 long Res = 0;
Kostya Serebryany311f27c2016-01-19 20:33:57 +0000112 long Sign = 1;
113 if (*Str == '-') {
114 Str++;
115 Sign = -1;
116 }
Kostya Serebryany4282d302016-01-15 00:17:37 +0000117 for (size_t i = 0; Str[i]; i++) {
118 char Ch = Str[i];
119 if (Ch < '0' || Ch > '9')
120 return Res;
121 Res = Res * 10 + (Ch - '0');
122 }
Kostya Serebryany311f27c2016-01-19 20:33:57 +0000123 return Res * Sign;
Kostya Serebryany4282d302016-01-15 00:17:37 +0000124}
125
Kostya Serebryany016852c2015-02-19 18:45:37 +0000126static bool ParseOneFlag(const char *Param) {
127 if (Param[0] != '-') return false;
Kostya Serebryany71e0feb2015-05-21 20:39:13 +0000128 if (Param[1] == '-') {
129 static bool PrintedWarning = false;
130 if (!PrintedWarning) {
131 PrintedWarning = true;
Kostya Serebryany64d24572016-03-12 01:57:04 +0000132 Printf("INFO: libFuzzer ignores flags that start with '--'\n");
Kostya Serebryany71e0feb2015-05-21 20:39:13 +0000133 }
Kostya Serebryanyb60397f2016-04-15 21:56:29 +0000134 for (size_t F = 0; F < kNumFlags; F++)
135 if (FlagValue(Param + 1, FlagDescriptions[F].Name))
136 Printf("WARNING: did you mean '%s' (single dash)?\n", Param + 1);
Kostya Serebryany71e0feb2015-05-21 20:39:13 +0000137 return true;
138 }
Kostya Serebryany016852c2015-02-19 18:45:37 +0000139 for (size_t F = 0; F < kNumFlags; F++) {
140 const char *Name = FlagDescriptions[F].Name;
141 const char *Str = FlagValue(Param, Name);
142 if (Str) {
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000143 if (FlagDescriptions[F].IntFlag) {
Kostya Serebryany4282d302016-01-15 00:17:37 +0000144 int Val = MyStol(Str);
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000145 *FlagDescriptions[F].IntFlag = Val;
146 if (Flags.verbosity >= 2)
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000147 Printf("Flag: %s %d\n", Name, Val);;
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000148 return true;
Mike Aizatskya1a5c692015-12-10 20:41:53 +0000149 } else if (FlagDescriptions[F].UIntFlag) {
150 unsigned int Val = std::stoul(Str);
151 *FlagDescriptions[F].UIntFlag = Val;
152 if (Flags.verbosity >= 2)
153 Printf("Flag: %s %u\n", Name, Val);
154 return true;
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000155 } else if (FlagDescriptions[F].StrFlag) {
156 *FlagDescriptions[F].StrFlag = Str;
157 if (Flags.verbosity >= 2)
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000158 Printf("Flag: %s %s\n", Name, Str);
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000159 return true;
Kostya Serebryany3d95dd92016-03-01 22:33:14 +0000160 } else { // Deprecated flag.
161 Printf("Flag: %s: deprecated, don't use\n", Name);
162 return true;
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000163 }
Kostya Serebryany016852c2015-02-19 18:45:37 +0000164 }
165 }
Kostya Serebryanyf8177312016-06-01 16:41:12 +0000166 Printf("\n\nWARNING: unrecognized flag '%s'; "
167 "use -help=1 to list all flags\n\n", Param);
168 return true;
Kostya Serebryany016852c2015-02-19 18:45:37 +0000169}
170
171// We don't use any library to minimize dependencies.
Kostya Serebryanya938bcb2015-09-10 16:57:57 +0000172static void ParseFlags(const std::vector<std::string> &Args) {
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000173 for (size_t F = 0; F < kNumFlags; F++) {
174 if (FlagDescriptions[F].IntFlag)
175 *FlagDescriptions[F].IntFlag = FlagDescriptions[F].Default;
Mike Aizatskya1a5c692015-12-10 20:41:53 +0000176 if (FlagDescriptions[F].UIntFlag)
177 *FlagDescriptions[F].UIntFlag =
178 static_cast<unsigned int>(FlagDescriptions[F].Default);
Kostya Serebryany52a788e2015-03-31 20:13:20 +0000179 if (FlagDescriptions[F].StrFlag)
180 *FlagDescriptions[F].StrFlag = nullptr;
181 }
Kostya Serebryanya938bcb2015-09-10 16:57:57 +0000182 Inputs = new std::vector<std::string>;
183 for (size_t A = 1; A < Args.size(); A++) {
184 if (ParseOneFlag(Args[A].c_str())) continue;
185 Inputs->push_back(Args[A]);
Kostya Serebryany016852c2015-02-19 18:45:37 +0000186 }
187}
188
Kostya Serebryany83fd4862015-05-11 21:31:51 +0000189static std::mutex Mu;
190
191static void PulseThread() {
192 while (true) {
Kostya Serebryany8b8f7a32016-05-06 23:38:07 +0000193 SleepSeconds(600);
Kostya Serebryany83fd4862015-05-11 21:31:51 +0000194 std::lock_guard<std::mutex> Lock(Mu);
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000195 Printf("pulse...\n");
Kostya Serebryany83fd4862015-05-11 21:31:51 +0000196 }
197}
198
Kostya Serebryany016852c2015-02-19 18:45:37 +0000199static void WorkerThread(const std::string &Cmd, std::atomic<int> *Counter,
200 int NumJobs, std::atomic<bool> *HasErrors) {
Kostya Serebryany016852c2015-02-19 18:45:37 +0000201 while (true) {
202 int C = (*Counter)++;
203 if (C >= NumJobs) break;
204 std::string Log = "fuzz-" + std::to_string(C) + ".log";
205 std::string ToRun = Cmd + " > " + Log + " 2>&1\n";
206 if (Flags.verbosity)
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000207 Printf("%s", ToRun.c_str());
Hans Wennborge6319962016-04-11 20:35:17 +0000208 int ExitCode = ExecuteCommand(ToRun);
Kostya Serebryany016852c2015-02-19 18:45:37 +0000209 if (ExitCode != 0)
210 *HasErrors = true;
Kostya Serebryany83fd4862015-05-11 21:31:51 +0000211 std::lock_guard<std::mutex> Lock(Mu);
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000212 Printf("================== Job %d exited with exit code %d ============\n",
213 C, ExitCode);
Kostya Serebryany016852c2015-02-19 18:45:37 +0000214 fuzzer::CopyFileToErr(Log);
215 }
216}
217
Kostya Serebryanya938bcb2015-09-10 16:57:57 +0000218static int RunInMultipleProcesses(const std::vector<std::string> &Args,
219 int NumWorkers, int NumJobs) {
Kostya Serebryany016852c2015-02-19 18:45:37 +0000220 std::atomic<int> Counter(0);
221 std::atomic<bool> HasErrors(false);
222 std::string Cmd;
Kostya Serebryanya938bcb2015-09-10 16:57:57 +0000223 for (auto &S : Args) {
224 if (FlagValue(S.c_str(), "jobs") || FlagValue(S.c_str(), "workers"))
225 continue;
226 Cmd += S + " ";
Kostya Serebryany016852c2015-02-19 18:45:37 +0000227 }
228 std::vector<std::thread> V;
Kostya Serebryany83fd4862015-05-11 21:31:51 +0000229 std::thread Pulse(PulseThread);
Kostya Serebryanycd7629c2015-05-12 01:43:20 +0000230 Pulse.detach();
Kostya Serebryany016852c2015-02-19 18:45:37 +0000231 for (int i = 0; i < NumWorkers; i++)
232 V.push_back(std::thread(WorkerThread, Cmd, &Counter, NumJobs, &HasErrors));
233 for (auto &T : V)
234 T.join();
235 return HasErrors ? 1 : 0;
236}
237
Kostya Serebryany52b394e2016-05-06 21:58:35 +0000238static void RssThread(Fuzzer *F, size_t RssLimitMb) {
239 while (true) {
Kostya Serebryany8b8f7a32016-05-06 23:38:07 +0000240 SleepSeconds(1);
Kostya Serebryany52b394e2016-05-06 21:58:35 +0000241 size_t Peak = GetPeakRSSMb();
242 if (Peak > RssLimitMb)
Kostya Serebryany8b8f7a32016-05-06 23:38:07 +0000243 F->RssLimitCallback();
Kostya Serebryany52b394e2016-05-06 21:58:35 +0000244 }
245}
246
247static void StartRssThread(Fuzzer *F, size_t RssLimitMb) {
248 if (!RssLimitMb) return;
249 std::thread T(RssThread, F, RssLimitMb);
250 T.detach();
251}
252
Ivan Krasin95e82d52015-10-01 23:23:06 +0000253int RunOneTest(Fuzzer *F, const char *InputFilePath) {
254 Unit U = FileToVector(InputFilePath);
Kostya Serebryany856b7af2015-11-03 18:57:25 +0000255 Unit PreciseSizedU(U);
256 assert(PreciseSizedU.size() == PreciseSizedU.capacity());
Kostya Serebryanybaf7fd02016-05-04 20:44:50 +0000257 F->RunOne(PreciseSizedU.data(), PreciseSizedU.size());
Ivan Krasin95e82d52015-10-01 23:23:06 +0000258 return 0;
259}
260
Kostya Serebryanybfbe7fc2016-02-02 03:03:47 +0000261static bool AllInputsAreFiles() {
262 if (Inputs->empty()) return false;
263 for (auto &Path : *Inputs)
264 if (!IsFile(Path))
265 return false;
266 return true;
267}
268
Dan Liewd3c33112016-06-02 05:48:02 +0000269int FuzzerDriver(int *argc, char ***argv, UserCallback Callback) {
Kostya Serebryanya938bcb2015-09-10 16:57:57 +0000270 using namespace fuzzer;
Dan Liewd3c33112016-06-02 05:48:02 +0000271 assert(argc && argv && "Argument pointers cannot be nullptr");
Dan Liew1873a492016-06-07 23:32:50 +0000272 EF = new ExternalFunctions();
273 if (EF->LLVMFuzzerInitialize)
274 EF->LLVMFuzzerInitialize(argc, argv);
Dan Liewd3c33112016-06-02 05:48:02 +0000275 const std::vector<std::string> Args(*argv, *argv + *argc);
Kostya Serebryanya938bcb2015-09-10 16:57:57 +0000276 assert(!Args.empty());
277 ProgName = new std::string(Args[0]);
278 ParseFlags(Args);
Kostya Serebryany016852c2015-02-19 18:45:37 +0000279 if (Flags.help) {
280 PrintHelp();
281 return 0;
282 }
283
Kostya Serebryany49e40902016-03-18 20:58:29 +0000284 if (Flags.close_fd_mask & 2)
285 DupAndCloseStderr();
286 if (Flags.close_fd_mask & 1)
287 CloseStdout();
288
Kostya Serebryany9690fcf2015-05-12 18:51:57 +0000289 if (Flags.jobs > 0 && Flags.workers == 0) {
290 Flags.workers = std::min(NumberOfCpuCores() / 2, Flags.jobs);
291 if (Flags.workers > 1)
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000292 Printf("Running %d workers\n", Flags.workers);
Kostya Serebryany9690fcf2015-05-12 18:51:57 +0000293 }
294
Kostya Serebryany016852c2015-02-19 18:45:37 +0000295 if (Flags.workers > 0 && Flags.jobs > 0)
Kostya Serebryanya938bcb2015-09-10 16:57:57 +0000296 return RunInMultipleProcesses(Args, Flags.workers, Flags.jobs);
Kostya Serebryany016852c2015-02-19 18:45:37 +0000297
Kostya Serebryany64d24572016-03-12 01:57:04 +0000298 const size_t kMaxSaneLen = 1 << 20;
Kostya Serebryany0c5e3af2016-03-15 01:28:00 +0000299 const size_t kMinDefaultLen = 64;
Mike Aizatskyf0b3e852016-06-23 20:44:48 +0000300 FuzzingOptions Options;
Kostya Serebryany016852c2015-02-19 18:45:37 +0000301 Options.Verbosity = Flags.verbosity;
302 Options.MaxLen = Flags.max_len;
Kostya Serebryany490bbd62015-05-19 22:12:57 +0000303 Options.UnitTimeoutSec = Flags.timeout;
Kostya Serebryany54a63632016-01-29 23:30:07 +0000304 Options.TimeoutExitCode = Flags.timeout_exitcode;
Kostya Serebryanyb85db172015-10-02 20:47:55 +0000305 Options.MaxTotalTimeSec = Flags.max_total_time;
Kostya Serebryany016852c2015-02-19 18:45:37 +0000306 Options.DoCrossOver = Flags.cross_over;
307 Options.MutateDepth = Flags.mutate_depth;
Kostya Serebryanybe5e0ed2015-03-03 23:27:02 +0000308 Options.UseCounters = Flags.use_counters;
Kostya Serebryany2e9fca92015-10-22 23:55:39 +0000309 Options.UseIndirCalls = Flags.use_indir_calls;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000310 Options.UseTraces = Flags.use_traces;
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000311 Options.UseMemcmp = Flags.use_memcmp;
Kostya Serebryanyc135b552016-07-15 23:27:19 +0000312 Options.UseMemmem = Flags.use_memmem;
Kostya Serebryanyfed509e2015-10-17 04:38:26 +0000313 Options.ShuffleAtStartUp = Flags.shuffle;
Kostya Serebryany945761b2016-03-18 00:23:29 +0000314 Options.PreferSmall = Flags.prefer_small;
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000315 Options.Reload = Flags.reload;
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +0000316 Options.OnlyASCII = Flags.only_ascii;
Mike Aizatskya9c23872015-11-12 04:38:40 +0000317 Options.OutputCSV = Flags.output_csv;
Kostya Serebryany1bfd5832016-04-20 00:24:21 +0000318 Options.DetectLeaks = Flags.detect_leaks;
Kostya Serebryany8b8f7a32016-05-06 23:38:07 +0000319 Options.RssLimitMb = Flags.rss_limit_mb;
Kostya Serebryany016852c2015-02-19 18:45:37 +0000320 if (Flags.runs >= 0)
321 Options.MaxNumberOfRuns = Flags.runs;
Kostya Serebryanya938bcb2015-09-10 16:57:57 +0000322 if (!Inputs->empty())
323 Options.OutputCorpus = (*Inputs)[0];
Kostya Serebryany70926ae2015-08-05 21:43:48 +0000324 Options.ReportSlowUnits = Flags.report_slow_units;
Kostya Serebryanybd5d1cd2015-10-09 03:57:59 +0000325 if (Flags.artifact_prefix)
326 Options.ArtifactPrefix = Flags.artifact_prefix;
Kostya Serebryany2d0ef142015-11-25 21:40:46 +0000327 if (Flags.exact_artifact_path)
328 Options.ExactArtifactPath = Flags.exact_artifact_path;
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000329 std::vector<Unit> Dictionary;
Kostya Serebryany7d211662015-09-04 00:12:11 +0000330 if (Flags.dict)
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000331 if (!ParseDictionaryFile(FileToString(Flags.dict), &Dictionary))
Kostya Serebryany7d211662015-09-04 00:12:11 +0000332 return 1;
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000333 if (Flags.verbosity > 0 && !Dictionary.empty())
334 Printf("Dictionary: %zd entries\n", Dictionary.size());
Kostya Serebryanyc5575aa2016-03-17 19:59:39 +0000335 bool DoPlainRun = AllInputsAreFiles();
336 Options.SaveArtifacts = !DoPlainRun;
Mike Aizatsky8b11f872016-01-06 00:21:22 +0000337 Options.PrintNewCovPcs = Flags.print_new_cov_pcs;
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000338 Options.PrintFinalStats = Flags.print_final_stats;
Mike Aizatskyaf432a42016-05-24 23:14:29 +0000339 Options.TruncateUnits = Flags.truncate_units;
Mike Aizatsky1f88b122016-06-07 18:16:32 +0000340 Options.PruneCorpus = Flags.prune_corpus;
Kostya Serebryany7d211662015-09-04 00:12:11 +0000341
Kostya Serebryanya3992212016-02-13 03:00:53 +0000342 unsigned Seed = Flags.seed;
343 // Initialize Seed.
344 if (Seed == 0)
345 Seed = (std::chrono::system_clock::now().time_since_epoch().count() << 10) +
346 getpid();
347 if (Flags.verbosity)
Kostya Serebryany64d24572016-03-12 01:57:04 +0000348 Printf("INFO: Seed: %u\n", Seed);
Kostya Serebryanya3992212016-02-13 03:00:53 +0000349
350 Random Rand(Seed);
Mike Aizatskyf0b3e852016-06-23 20:44:48 +0000351 MutationDispatcher MD(Rand, Options);
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000352 Fuzzer F(Callback, MD, Options);
Kostya Serebryany016852c2015-02-19 18:45:37 +0000353
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000354 for (auto &U: Dictionary)
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000355 if (U.size() <= Word::GetMaxSize())
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000356 MD.AddWordToManualDictionary(Word(U.data(), U.size()));
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000357
Kostya Serebryany52b394e2016-05-06 21:58:35 +0000358 StartRssThread(&F, Flags.rss_limit_mb);
359
Kostya Serebryanyb91c62b2015-10-16 22:41:47 +0000360 // Timer
361 if (Flags.timeout > 0)
362 SetTimer(Flags.timeout / 2 + 1);
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000363 if (Flags.handle_segv) SetSigSegvHandler();
364 if (Flags.handle_bus) SetSigBusHandler();
365 if (Flags.handle_abrt) SetSigAbrtHandler();
366 if (Flags.handle_ill) SetSigIllHandler();
367 if (Flags.handle_fpe) SetSigFpeHandler();
368 if (Flags.handle_int) SetSigIntHandler();
Kostya Serebryanyf389ae12016-03-24 21:03:58 +0000369 if (Flags.handle_term) SetSigTermHandler();
Kostya Serebryanyb91c62b2015-10-16 22:41:47 +0000370
Kostya Serebryanyc5575aa2016-03-17 19:59:39 +0000371 if (DoPlainRun) {
372 Options.SaveArtifacts = false;
Kostya Serebryany9d14e4b2016-02-12 02:32:03 +0000373 int Runs = std::max(1, Flags.runs);
374 Printf("%s: Running %zd inputs %d time(s) each.\n", ProgName->c_str(),
375 Inputs->size(), Runs);
Kostya Serebryanybfbe7fc2016-02-02 03:03:47 +0000376 for (auto &Path : *Inputs) {
377 auto StartTime = system_clock::now();
Kostya Serebryany042d1a72016-06-17 13:07:06 +0000378 Printf("Running: %s\n", Path.c_str());
Kostya Serebryany5c3701c2016-03-04 22:35:40 +0000379 for (int Iter = 0; Iter < Runs; Iter++)
Kostya Serebryany9d14e4b2016-02-12 02:32:03 +0000380 RunOneTest(&F, Path.c_str());
Kostya Serebryanybfbe7fc2016-02-02 03:03:47 +0000381 auto StopTime = system_clock::now();
382 auto MS = duration_cast<milliseconds>(StopTime - StartTime).count();
Kostya Serebryany042d1a72016-06-17 13:07:06 +0000383 Printf("Executed %s in %zd ms\n", Path.c_str(), (long)MS);
Kostya Serebryanybfbe7fc2016-02-02 03:03:47 +0000384 }
Kostya Serebryanybdb220c2016-08-15 19:44:04 +0000385 Printf("***\n"
386 "*** NOTE: fuzzing was not performed, you have only\n"
387 "*** executed the target code on a fixed set of inputs.\n"
388 "***\n");
Kostya Serebryanybaf7fd02016-05-04 20:44:50 +0000389 F.PrintFinalStats();
Kostya Serebryanybfbe7fc2016-02-02 03:03:47 +0000390 exit(0);
391 }
392
Kostya Serebryany64d24572016-03-12 01:57:04 +0000393
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000394 if (Flags.merge) {
Kostya Serebryany64d24572016-03-12 01:57:04 +0000395 if (Options.MaxLen == 0)
396 F.SetMaxLen(kMaxSaneLen);
Kostya Serebryany9cc3b0d2015-10-24 01:16:40 +0000397 F.Merge(*Inputs);
398 exit(0);
399 }
400
Kostya Serebryany64d24572016-03-12 01:57:04 +0000401 size_t TemporaryMaxLen = Options.MaxLen ? Options.MaxLen : kMaxSaneLen;
Kostya Serebryany016852c2015-02-19 18:45:37 +0000402
Kostya Serebryany64d24572016-03-12 01:57:04 +0000403 F.RereadOutputCorpus(TemporaryMaxLen);
Kostya Serebryanya938bcb2015-09-10 16:57:57 +0000404 for (auto &inp : *Inputs)
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000405 if (inp != Options.OutputCorpus)
Kostya Serebryany64d24572016-03-12 01:57:04 +0000406 F.ReadDir(inp, nullptr, TemporaryMaxLen);
407
408 if (Options.MaxLen == 0)
409 F.SetMaxLen(
Kostya Serebryany0c5e3af2016-03-15 01:28:00 +0000410 std::min(std::max(kMinDefaultLen, F.MaxUnitSizeInCorpus()), kMaxSaneLen));
Kostya Serebryany016852c2015-02-19 18:45:37 +0000411
Kostya Serebryany09087bb2016-04-18 21:14:11 +0000412 if (F.CorpusSize() == 0) {
Kostya Serebryany016852c2015-02-19 18:45:37 +0000413 F.AddToCorpus(Unit()); // Can't fuzz empty corpus, so add an empty input.
Kostya Serebryany09087bb2016-04-18 21:14:11 +0000414 if (Options.Verbosity)
415 Printf("INFO: A corpus is not provided, starting from an empty corpus\n");
416 }
Kostya Serebryany016852c2015-02-19 18:45:37 +0000417 F.ShuffleAndMinimize();
Kostya Serebryany550e9c82015-12-19 03:42:16 +0000418 if (Flags.drill)
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000419 F.Drill();
420 else
421 F.Loop();
422
Kostya Serebryany016852c2015-02-19 18:45:37 +0000423 if (Flags.verbosity)
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000424 Printf("Done %d runs in %zd second(s)\n", F.getTotalNumberOfRuns(),
425 F.secondsSinceProcessStartUp());
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000426 F.PrintFinalStats();
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000427
Kostya Serebryanyc8cd29f2015-10-03 07:02:05 +0000428 exit(0); // Don't let F destroy itself.
Kostya Serebryany016852c2015-02-19 18:45:37 +0000429}
Dan Liew1873a492016-06-07 23:32:50 +0000430
431// Storage for global ExternalFunctions object.
432ExternalFunctions *EF = nullptr;
433
Kostya Serebryany016852c2015-02-19 18:45:37 +0000434} // namespace fuzzer