blob: 95ed36551463bf81b1216012c64f348aa06d1a52 [file] [log] [blame]
kcc2e6ca5c2019-02-12 22:48:55 +00001//===- FuzzerFork.cpp - run fuzzing in separate subprocesses --------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
kcc64bcb922019-02-13 04:04:45 +00008// Spawn and orchestrate separate fuzzing processes.
kcc2e6ca5c2019-02-12 22:48:55 +00009//===----------------------------------------------------------------------===//
10
11#include "FuzzerCommand.h"
12#include "FuzzerFork.h"
13#include "FuzzerIO.h"
kcc1c5afe22019-04-13 01:57:33 +000014#include "FuzzerInternal.h"
kcc2e6ca5c2019-02-12 22:48:55 +000015#include "FuzzerMerge.h"
16#include "FuzzerSHA1.h"
kcc9c0ed932019-02-15 01:22:00 +000017#include "FuzzerTracePC.h"
kcc2e6ca5c2019-02-12 22:48:55 +000018#include "FuzzerUtil.h"
19
kccb1fa9e02019-02-14 01:11:29 +000020#include <atomic>
kcc55e54ed2019-02-15 21:51:15 +000021#include <chrono>
kcc1ee7f412019-06-14 22:56:50 +000022#include <condition_variable>
kccdd391142019-02-14 21:09:32 +000023#include <fstream>
kccedefdf32019-02-16 00:14:16 +000024#include <memory>
kcc6526f1d2019-02-14 00:25:43 +000025#include <mutex>
kcc6526f1d2019-02-14 00:25:43 +000026#include <queue>
kccdd391142019-02-14 21:09:32 +000027#include <sstream>
28#include <thread>
kcc6526f1d2019-02-14 00:25:43 +000029
kcc2e6ca5c2019-02-12 22:48:55 +000030namespace fuzzer {
31
kccdd391142019-02-14 21:09:32 +000032struct Stats {
33 size_t number_of_executed_units = 0;
34 size_t peak_rss_mb = 0;
35 size_t average_exec_per_sec = 0;
36};
37
38static Stats ParseFinalStatsFromLog(const std::string &LogPath) {
39 std::ifstream In(LogPath);
40 std::string Line;
41 Stats Res;
42 struct {
43 const char *Name;
44 size_t *Var;
45 } NameVarPairs[] = {
46 {"stat::number_of_executed_units:", &Res.number_of_executed_units},
47 {"stat::peak_rss_mb:", &Res.peak_rss_mb},
48 {"stat::average_exec_per_sec:", &Res.average_exec_per_sec},
49 {nullptr, nullptr},
50 };
51 while (std::getline(In, Line, '\n')) {
52 if (Line.find("stat::") != 0) continue;
53 std::istringstream ISS(Line);
54 std::string Name;
55 size_t Val;
56 ISS >> Name >> Val;
57 for (size_t i = 0; NameVarPairs[i].Name; i++)
58 if (Name == NameVarPairs[i].Name)
59 *NameVarPairs[i].Var = Val;
60 }
61 return Res;
62}
63
kcc64bcb922019-02-13 04:04:45 +000064struct FuzzJob {
65 // Inputs.
66 Command Cmd;
kcc64bcb922019-02-13 04:04:45 +000067 std::string CorpusDir;
kcc1c5afe22019-04-13 01:57:33 +000068 std::string FeaturesDir;
kcc64bcb922019-02-13 04:04:45 +000069 std::string LogPath;
metzmane9b95bc2019-04-30 20:56:18 +000070 std::string SeedListPath;
kcc64bcb922019-02-13 04:04:45 +000071 std::string CFPath;
kcc1ee7f412019-06-14 22:56:50 +000072 size_t JobId;
73
74 int DftTimeInSeconds = 0;
kcc64bcb922019-02-13 04:04:45 +000075
76 // Fuzzing Outputs.
77 int ExitCode;
kccedefdf32019-02-16 00:14:16 +000078
79 ~FuzzJob() {
80 RemoveFile(CFPath);
81 RemoveFile(LogPath);
metzmane9b95bc2019-04-30 20:56:18 +000082 RemoveFile(SeedListPath);
kccedefdf32019-02-16 00:14:16 +000083 RmDirRecursive(CorpusDir);
kcc1c5afe22019-04-13 01:57:33 +000084 RmDirRecursive(FeaturesDir);
kccedefdf32019-02-16 00:14:16 +000085 }
kcc64bcb922019-02-13 04:04:45 +000086};
87
88struct GlobalEnv {
kcc6526f1d2019-02-14 00:25:43 +000089 Vector<std::string> Args;
90 Vector<std::string> CorpusDirs;
kcc64bcb922019-02-13 04:04:45 +000091 std::string MainCorpusDir;
kcc6526f1d2019-02-14 00:25:43 +000092 std::string TempDir;
kccd701d9e2019-05-23 00:22:46 +000093 std::string DFTDir;
94 std::string DataFlowBinary;
kcc98a86242019-02-15 00:08:16 +000095 Set<uint32_t> Features, Cov;
kcc0a66b5b2019-06-14 19:54:32 +000096 Set<std::string> FilesWithDFT;
kcc64bcb922019-02-13 04:04:45 +000097 Vector<std::string> Files;
kcc6526f1d2019-02-14 00:25:43 +000098 Random *Rand;
kcc55e54ed2019-02-15 21:51:15 +000099 std::chrono::system_clock::time_point ProcessStartTime;
kcc6526f1d2019-02-14 00:25:43 +0000100 int Verbosity = 0;
101
kcc55e54ed2019-02-15 21:51:15 +0000102 size_t NumTimeouts = 0;
103 size_t NumOOMs = 0;
104 size_t NumCrashes = 0;
105
106
kccdd391142019-02-14 21:09:32 +0000107 size_t NumRuns = 0;
108
kcc1ee7f412019-06-14 22:56:50 +0000109 std::string StopFile() { return DirPlusFile(TempDir, "STOP"); }
110
kcc55e54ed2019-02-15 21:51:15 +0000111 size_t secondsSinceProcessStartUp() const {
112 return std::chrono::duration_cast<std::chrono::seconds>(
113 std::chrono::system_clock::now() - ProcessStartTime)
114 .count();
115 }
116
kcc6526f1d2019-02-14 00:25:43 +0000117 FuzzJob *CreateNewJob(size_t JobId) {
118 Command Cmd(Args);
119 Cmd.removeFlag("fork");
kcc18b370a2019-04-12 20:20:57 +0000120 Cmd.removeFlag("runs");
kccd701d9e2019-05-23 00:22:46 +0000121 Cmd.removeFlag("collect_data_flow");
kcc6526f1d2019-02-14 00:25:43 +0000122 for (auto &C : CorpusDirs) // Remove all corpora from the args.
123 Cmd.removeArgument(C);
124 Cmd.addFlag("reload", "0"); // working in an isolated dir, no reload.
kccdd391142019-02-14 21:09:32 +0000125 Cmd.addFlag("print_final_stats", "1");
kcc9c0ed932019-02-15 01:22:00 +0000126 Cmd.addFlag("print_funcs", "0"); // no need to spend time symbolizing.
kcc6526f1d2019-02-14 00:25:43 +0000127 Cmd.addFlag("max_total_time", std::to_string(std::min((size_t)300, JobId)));
kcc1ee7f412019-06-14 22:56:50 +0000128 Cmd.addFlag("stop_file", StopFile());
kccd701d9e2019-05-23 00:22:46 +0000129 if (!DataFlowBinary.empty()) {
130 Cmd.addFlag("data_flow_trace", DFTDir);
131 if (!Cmd.hasFlag("focus_function"))
132 Cmd.addFlag("focus_function", "auto");
133 }
kcc6526f1d2019-02-14 00:25:43 +0000134 auto Job = new FuzzJob;
135 std::string Seeds;
kcc9c0ed932019-02-15 01:22:00 +0000136 if (size_t CorpusSubsetSize =
kcc0a66b5b2019-06-14 19:54:32 +0000137 std::min(Files.size(), (size_t)sqrt(Files.size() + 2))) {
kcc1ee7f412019-06-14 22:56:50 +0000138 auto Time1 = std::chrono::system_clock::now();
kcc0a66b5b2019-06-14 19:54:32 +0000139 for (size_t i = 0; i < CorpusSubsetSize; i++) {
140 auto &SF = Files[Rand->SkewTowardsLast(Files.size())];
141 Seeds += (Seeds.empty() ? "" : ",") + SF;
142 CollectDFT(SF);
143 }
kcc1ee7f412019-06-14 22:56:50 +0000144 auto Time2 = std::chrono::system_clock::now();
145 Job->DftTimeInSeconds = duration_cast<seconds>(Time2 - Time1).count();
kcc0a66b5b2019-06-14 19:54:32 +0000146 }
metzmane9b95bc2019-04-30 20:56:18 +0000147 if (!Seeds.empty()) {
kccd701d9e2019-05-23 00:22:46 +0000148 Job->SeedListPath =
149 DirPlusFile(TempDir, std::to_string(JobId) + ".seeds");
metzmane9b95bc2019-04-30 20:56:18 +0000150 WriteToFile(Seeds, Job->SeedListPath);
151 Cmd.addFlag("seed_inputs", "@" + Job->SeedListPath);
152 }
kcc6526f1d2019-02-14 00:25:43 +0000153 Job->LogPath = DirPlusFile(TempDir, std::to_string(JobId) + ".log");
154 Job->CorpusDir = DirPlusFile(TempDir, "C" + std::to_string(JobId));
kcc1c5afe22019-04-13 01:57:33 +0000155 Job->FeaturesDir = DirPlusFile(TempDir, "F" + std::to_string(JobId));
kcc6526f1d2019-02-14 00:25:43 +0000156 Job->CFPath = DirPlusFile(TempDir, std::to_string(JobId) + ".merge");
kcc1ee7f412019-06-14 22:56:50 +0000157 Job->JobId = JobId;
kcc6526f1d2019-02-14 00:25:43 +0000158
159
160 Cmd.addArgument(Job->CorpusDir);
kcc1c5afe22019-04-13 01:57:33 +0000161 Cmd.addFlag("features_dir", Job->FeaturesDir);
162
163 for (auto &D : {Job->CorpusDir, Job->FeaturesDir}) {
164 RmDirRecursive(D);
165 MkDir(D);
166 }
kcc6526f1d2019-02-14 00:25:43 +0000167
168 Cmd.setOutputFile(Job->LogPath);
169 Cmd.combineOutAndErr();
170
171 Job->Cmd = Cmd;
172
173 if (Verbosity >= 2)
174 Printf("Job %zd/%p Created: %s\n", JobId, Job,
175 Job->Cmd.toString().c_str());
176 // Start from very short runs and gradually increase them.
177 return Job;
178 }
179
180 void RunOneMergeJob(FuzzJob *Job) {
kcce5ef7c02019-04-19 01:39:14 +0000181 auto Stats = ParseFinalStatsFromLog(Job->LogPath);
182 NumRuns += Stats.number_of_executed_units;
183
kcc1c5afe22019-04-13 01:57:33 +0000184 Vector<SizedFile> TempFiles, MergeCandidates;
185 // Read all newly created inputs and their feature sets.
186 // Choose only those inputs that have new features.
kcc6526f1d2019-02-14 00:25:43 +0000187 GetSizedFilesFromDir(Job->CorpusDir, &TempFiles);
kcc1c5afe22019-04-13 01:57:33 +0000188 std::sort(TempFiles.begin(), TempFiles.end());
189 for (auto &F : TempFiles) {
190 auto FeatureFile = F.File;
191 FeatureFile.replace(0, Job->CorpusDir.size(), Job->FeaturesDir);
192 auto FeatureBytes = FileToVector(FeatureFile, 0, false);
193 assert((FeatureBytes.size() % sizeof(uint32_t)) == 0);
194 Vector<uint32_t> NewFeatures(FeatureBytes.size() / sizeof(uint32_t));
195 memcpy(NewFeatures.data(), FeatureBytes.data(), FeatureBytes.size());
196 for (auto Ft : NewFeatures) {
197 if (!Features.count(Ft)) {
198 MergeCandidates.push_back(F);
199 break;
200 }
201 }
202 }
kcc1ee7f412019-06-14 22:56:50 +0000203 // if (!FilesToAdd.empty() || Job->ExitCode != 0)
204 Printf("#%zd: cov: %zd ft: %zd corp: %zd exec/s %zd "
205 "oom/timeout/crash: %zd/%zd/%zd time: %zds job: %zd dft_time: %d\n",
206 NumRuns, Cov.size(), Features.size(), Files.size(),
207 Stats.average_exec_per_sec, NumOOMs, NumTimeouts, NumCrashes,
208 secondsSinceProcessStartUp(), Job->JobId, Job->DftTimeInSeconds);
209
kcc1c5afe22019-04-13 01:57:33 +0000210 if (MergeCandidates.empty()) return;
kcc6526f1d2019-02-14 00:25:43 +0000211
212 Vector<std::string> FilesToAdd;
kcc98a86242019-02-15 00:08:16 +0000213 Set<uint32_t> NewFeatures, NewCov;
kcc1c5afe22019-04-13 01:57:33 +0000214 CrashResistantMerge(Args, {}, MergeCandidates, &FilesToAdd, Features,
kcc98a86242019-02-15 00:08:16 +0000215 &NewFeatures, Cov, &NewCov, Job->CFPath, false);
kcc6526f1d2019-02-14 00:25:43 +0000216 for (auto &Path : FilesToAdd) {
217 auto U = FileToVector(Path);
218 auto NewPath = DirPlusFile(MainCorpusDir, Hash(U));
219 WriteToFile(U, NewPath);
220 Files.push_back(NewPath);
221 }
kcc6526f1d2019-02-14 00:25:43 +0000222 Features.insert(NewFeatures.begin(), NewFeatures.end());
kcc98a86242019-02-15 00:08:16 +0000223 Cov.insert(NewCov.begin(), NewCov.end());
kcc9c0ed932019-02-15 01:22:00 +0000224 for (auto Idx : NewCov)
225 if (auto *TE = TPC.PCTableEntryByIdx(Idx))
226 if (TPC.PcIsFuncEntry(TE))
227 PrintPC(" NEW_FUNC: %p %F %L\n", "",
228 TPC.GetNextInstructionPc(TE->PC));
229
kcc6526f1d2019-02-14 00:25:43 +0000230 }
kccd701d9e2019-05-23 00:22:46 +0000231
232
233 void CollectDFT(const std::string &InputPath) {
234 if (DataFlowBinary.empty()) return;
kcc0a66b5b2019-06-14 19:54:32 +0000235 if (!FilesWithDFT.insert(InputPath).second) return;
kccd701d9e2019-05-23 00:22:46 +0000236 Command Cmd(Args);
237 Cmd.removeFlag("fork");
238 Cmd.removeFlag("runs");
239 Cmd.addFlag("data_flow_trace", DFTDir);
240 Cmd.addArgument(InputPath);
241 for (auto &C : CorpusDirs) // Remove all corpora from the args.
242 Cmd.removeArgument(C);
243 Cmd.setOutputFile(DirPlusFile(TempDir, "dft.log"));
244 Cmd.combineOutAndErr();
kcc0a66b5b2019-06-14 19:54:32 +0000245 // Printf("CollectDFT: %s\n", Cmd.toString().c_str());
kccd701d9e2019-05-23 00:22:46 +0000246 ExecuteCommand(Cmd);
247 }
248
kcc64bcb922019-02-13 04:04:45 +0000249};
250
kcc6526f1d2019-02-14 00:25:43 +0000251struct JobQueue {
252 std::queue<FuzzJob *> Qu;
253 std::mutex Mu;
kcc1ee7f412019-06-14 22:56:50 +0000254 std::condition_variable Cv;
kcc64bcb922019-02-13 04:04:45 +0000255
kcc6526f1d2019-02-14 00:25:43 +0000256 void Push(FuzzJob *Job) {
kcc1ee7f412019-06-14 22:56:50 +0000257 {
258 std::lock_guard<std::mutex> Lock(Mu);
259 Qu.push(Job);
260 }
261 Cv.notify_one();
kcc64bcb922019-02-13 04:04:45 +0000262 }
kcc6526f1d2019-02-14 00:25:43 +0000263 FuzzJob *Pop() {
kcc1ee7f412019-06-14 22:56:50 +0000264 std::unique_lock<std::mutex> Lk(Mu);
265 // std::lock_guard<std::mutex> Lock(Mu);
266 Cv.wait(Lk, [&]{return !Qu.empty();});
267 assert(!Qu.empty());
kcc6526f1d2019-02-14 00:25:43 +0000268 auto Job = Qu.front();
269 Qu.pop();
270 return Job;
271 }
272};
273
kcc1ee7f412019-06-14 22:56:50 +0000274void WorkerThread(JobQueue *FuzzQ, JobQueue *MergeQ) {
275 while (auto Job = FuzzQ->Pop()) {
kcc6526f1d2019-02-14 00:25:43 +0000276 // Printf("WorkerThread: job %p\n", Job);
kcc6526f1d2019-02-14 00:25:43 +0000277 Job->ExitCode = ExecuteCommand(Job->Cmd);
278 MergeQ->Push(Job);
279 }
kcc64bcb922019-02-13 04:04:45 +0000280}
281
kcc2e6ca5c2019-02-12 22:48:55 +0000282// This is just a skeleton of an experimental -fork=1 feature.
283void FuzzWithFork(Random &Rand, const FuzzingOptions &Options,
284 const Vector<std::string> &Args,
kcc6526f1d2019-02-14 00:25:43 +0000285 const Vector<std::string> &CorpusDirs, int NumJobs) {
kcc55e54ed2019-02-15 21:51:15 +0000286 Printf("INFO: -fork=%d: fuzzing in separate process(s)\n", NumJobs);
kcc2e6ca5c2019-02-12 22:48:55 +0000287
kcc64bcb922019-02-13 04:04:45 +0000288 GlobalEnv Env;
kcc6526f1d2019-02-14 00:25:43 +0000289 Env.Args = Args;
290 Env.CorpusDirs = CorpusDirs;
291 Env.Rand = &Rand;
292 Env.Verbosity = Options.Verbosity;
kcc55e54ed2019-02-15 21:51:15 +0000293 Env.ProcessStartTime = std::chrono::system_clock::now();
kccd701d9e2019-05-23 00:22:46 +0000294 Env.DataFlowBinary = Options.CollectDataFlow;
kcc64bcb922019-02-13 04:04:45 +0000295
kcc2e6ca5c2019-02-12 22:48:55 +0000296 Vector<SizedFile> SeedFiles;
297 for (auto &Dir : CorpusDirs)
298 GetSizedFilesFromDir(Dir, &SeedFiles);
299 std::sort(SeedFiles.begin(), SeedFiles.end());
kcc6526f1d2019-02-14 00:25:43 +0000300 Env.TempDir = TempPath(".dir");
kccd701d9e2019-05-23 00:22:46 +0000301 Env.DFTDir = DirPlusFile(Env.TempDir, "DFT");
kcc6526f1d2019-02-14 00:25:43 +0000302 RmDirRecursive(Env.TempDir); // in case there is a leftover from old runs.
303 MkDir(Env.TempDir);
kccd701d9e2019-05-23 00:22:46 +0000304 MkDir(Env.DFTDir);
kcc2e6ca5c2019-02-12 22:48:55 +0000305
kcc2e6ca5c2019-02-12 22:48:55 +0000306
kcc64bcb922019-02-13 04:04:45 +0000307 if (CorpusDirs.empty())
kcc6526f1d2019-02-14 00:25:43 +0000308 MkDir(Env.MainCorpusDir = DirPlusFile(Env.TempDir, "C"));
kcc64bcb922019-02-13 04:04:45 +0000309 else
310 Env.MainCorpusDir = CorpusDirs[0];
311
kcc6526f1d2019-02-14 00:25:43 +0000312 auto CFPath = DirPlusFile(Env.TempDir, "merge.txt");
313 CrashResistantMerge(Env.Args, {}, SeedFiles, &Env.Files, {}, &Env.Features,
kcc98a86242019-02-15 00:08:16 +0000314 {}, &Env.Cov,
kcc64bcb922019-02-13 04:04:45 +0000315 CFPath, false);
316 RemoveFile(CFPath);
kcc55e54ed2019-02-15 21:51:15 +0000317 Printf("INFO: -fork=%d: %zd seed inputs, starting to fuzz in %s\n", NumJobs,
318 Env.Files.size(), Env.TempDir.c_str());
kcc64bcb922019-02-13 04:04:45 +0000319
kcc2e6ca5c2019-02-12 22:48:55 +0000320 int ExitCode = 0;
kcc64bcb922019-02-13 04:04:45 +0000321
kcc6526f1d2019-02-14 00:25:43 +0000322 JobQueue FuzzQ, MergeQ;
kcc1ee7f412019-06-14 22:56:50 +0000323
324 auto StopJobs = [&]() {
325 for (int i = 0; i < NumJobs; i++)
326 FuzzQ.Push(nullptr);
327 MergeQ.Push(nullptr);
328 WriteToFile(Unit({1}), Env.StopFile());
329 };
kcc64bcb922019-02-13 04:04:45 +0000330
kcc6526f1d2019-02-14 00:25:43 +0000331 size_t JobId = 1;
332 Vector<std::thread> Threads;
333 for (int t = 0; t < NumJobs; t++) {
kcc1ee7f412019-06-14 22:56:50 +0000334 Threads.push_back(std::thread(WorkerThread, &FuzzQ, &MergeQ));
kcc6526f1d2019-02-14 00:25:43 +0000335 FuzzQ.Push(Env.CreateNewJob(JobId++));
kcc2e6ca5c2019-02-12 22:48:55 +0000336 }
337
kcc55e54ed2019-02-15 21:51:15 +0000338 while (true) {
kccedefdf32019-02-16 00:14:16 +0000339 std::unique_ptr<FuzzJob> Job(MergeQ.Pop());
kcc1ee7f412019-06-14 22:56:50 +0000340 if (!Job)
341 break;
kcc6526f1d2019-02-14 00:25:43 +0000342 ExitCode = Job->ExitCode;
kccedefdf32019-02-16 00:14:16 +0000343 if (ExitCode == Options.InterruptExitCode) {
344 Printf("==%lu== libFuzzer: a child was interrupted; exiting\n", GetPid());
kcc1ee7f412019-06-14 22:56:50 +0000345 StopJobs();
kccedefdf32019-02-16 00:14:16 +0000346 break;
347 }
kcc1c5afe22019-04-13 01:57:33 +0000348 Fuzzer::MaybeExitGracefully();
kccedefdf32019-02-16 00:14:16 +0000349
350 Env.RunOneMergeJob(Job.get());
kcc6526f1d2019-02-14 00:25:43 +0000351
352 // Continue if our crash is one of the ignorred ones.
353 if (Options.IgnoreTimeouts && ExitCode == Options.TimeoutExitCode)
kcc55e54ed2019-02-15 21:51:15 +0000354 Env.NumTimeouts++;
kcc6526f1d2019-02-14 00:25:43 +0000355 else if (Options.IgnoreOOMs && ExitCode == Options.OOMExitCode)
kcc55e54ed2019-02-15 21:51:15 +0000356 Env.NumOOMs++;
kcc6526f1d2019-02-14 00:25:43 +0000357 else if (ExitCode != 0) {
kcc55e54ed2019-02-15 21:51:15 +0000358 Env.NumCrashes++;
359 if (Options.IgnoreCrashes) {
360 std::ifstream In(Job->LogPath);
361 std::string Line;
362 while (std::getline(In, Line, '\n'))
kcc18b370a2019-04-12 20:20:57 +0000363 if (Line.find("ERROR:") != Line.npos ||
364 Line.find("runtime error:") != Line.npos)
kcc55e54ed2019-02-15 21:51:15 +0000365 Printf("%s\n", Line.c_str());
366 } else {
367 // And exit if we don't ignore this crash.
368 Printf("INFO: log from the inner process:\n%s",
369 FileToString(Job->LogPath).c_str());
kcc1ee7f412019-06-14 22:56:50 +0000370 StopJobs();
371 break;
kcc55e54ed2019-02-15 21:51:15 +0000372 }
kcc6526f1d2019-02-14 00:25:43 +0000373 }
kcc55e54ed2019-02-15 21:51:15 +0000374
375 // Stop if we are over the time budget.
376 // This is not precise, since other threads are still running
377 // and we will wait while joining them.
378 // We also don't stop instantly: other jobs need to finish.
kcc1ee7f412019-06-14 22:56:50 +0000379 if (Options.MaxTotalTimeSec > 0 &&
kcc55e54ed2019-02-15 21:51:15 +0000380 Env.secondsSinceProcessStartUp() >= (size_t)Options.MaxTotalTimeSec) {
381 Printf("INFO: fuzzed for %zd seconds, wrapping up soon\n",
382 Env.secondsSinceProcessStartUp());
kcc1ee7f412019-06-14 22:56:50 +0000383 StopJobs();
384 break;
kcc55e54ed2019-02-15 21:51:15 +0000385 }
kcc1ee7f412019-06-14 22:56:50 +0000386 if (Env.NumRuns >= Options.MaxNumberOfRuns) {
kcc18b370a2019-04-12 20:20:57 +0000387 Printf("INFO: fuzzed for %zd iterations, wrapping up soon\n",
388 Env.NumRuns);
kcc1ee7f412019-06-14 22:56:50 +0000389 StopJobs();
390 break;
kcc18b370a2019-04-12 20:20:57 +0000391 }
kcc55e54ed2019-02-15 21:51:15 +0000392
kcc1ee7f412019-06-14 22:56:50 +0000393 FuzzQ.Push(Env.CreateNewJob(JobId++));
kcc6526f1d2019-02-14 00:25:43 +0000394 }
kcc6526f1d2019-02-14 00:25:43 +0000395
396 for (auto &T : Threads)
397 T.join();
398
metzmane847d8a2019-02-27 19:27:16 +0000399 // The workers have terminated. Don't try to remove the directory before they
400 // terminate to avoid a race condition preventing cleanup on Windows.
401 RmDirRecursive(Env.TempDir);
402
kcc2e6ca5c2019-02-12 22:48:55 +0000403 // Use the exit code from the last child process.
kcc55e54ed2019-02-15 21:51:15 +0000404 Printf("INFO: exiting: %d time: %zds\n", ExitCode,
405 Env.secondsSinceProcessStartUp());
kcc2e6ca5c2019-02-12 22:48:55 +0000406 exit(ExitCode);
407}
408
409} // namespace fuzzer