blob: 95ed36551463bf81b1216012c64f348aa06d1a52 [file] [log] [blame]
Kostya Serebryany5c08e812019-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//===----------------------------------------------------------------------===//
Kostya Serebryanyd0857482019-02-13 04:04:45 +00008// Spawn and orchestrate separate fuzzing processes.
Kostya Serebryany5c08e812019-02-12 22:48:55 +00009//===----------------------------------------------------------------------===//
10
11#include "FuzzerCommand.h"
12#include "FuzzerFork.h"
13#include "FuzzerIO.h"
Kostya Serebryany5e67abd2019-04-13 01:57:33 +000014#include "FuzzerInternal.h"
Kostya Serebryany5c08e812019-02-12 22:48:55 +000015#include "FuzzerMerge.h"
16#include "FuzzerSHA1.h"
Kostya Serebryany77cbc622019-02-15 01:22:00 +000017#include "FuzzerTracePC.h"
Kostya Serebryany5c08e812019-02-12 22:48:55 +000018#include "FuzzerUtil.h"
19
Kostya Serebryanydebf0af2019-02-14 01:11:29 +000020#include <atomic>
Kostya Serebryany9982ee52019-02-15 21:51:15 +000021#include <chrono>
Kostya Serebryanydb88fc52019-06-14 22:56:50 +000022#include <condition_variable>
Kostya Serebryany8e918d62019-02-14 21:09:32 +000023#include <fstream>
Kostya Serebryany312af152019-02-16 00:14:16 +000024#include <memory>
Kostya Serebryany60c5ded2019-02-14 00:25:43 +000025#include <mutex>
Kostya Serebryany60c5ded2019-02-14 00:25:43 +000026#include <queue>
Kostya Serebryany8e918d62019-02-14 21:09:32 +000027#include <sstream>
28#include <thread>
Kostya Serebryany60c5ded2019-02-14 00:25:43 +000029
Kostya Serebryany5c08e812019-02-12 22:48:55 +000030namespace fuzzer {
31
Kostya Serebryany8e918d62019-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
Kostya Serebryanyd0857482019-02-13 04:04:45 +000064struct FuzzJob {
65 // Inputs.
66 Command Cmd;
Kostya Serebryanyd0857482019-02-13 04:04:45 +000067 std::string CorpusDir;
Kostya Serebryany5e67abd2019-04-13 01:57:33 +000068 std::string FeaturesDir;
Kostya Serebryanyd0857482019-02-13 04:04:45 +000069 std::string LogPath;
Jonathan Metzmanf3ee9772019-04-30 20:56:18 +000070 std::string SeedListPath;
Kostya Serebryanyd0857482019-02-13 04:04:45 +000071 std::string CFPath;
Kostya Serebryanydb88fc52019-06-14 22:56:50 +000072 size_t JobId;
73
74 int DftTimeInSeconds = 0;
Kostya Serebryanyd0857482019-02-13 04:04:45 +000075
76 // Fuzzing Outputs.
77 int ExitCode;
Kostya Serebryany312af152019-02-16 00:14:16 +000078
79 ~FuzzJob() {
80 RemoveFile(CFPath);
81 RemoveFile(LogPath);
Jonathan Metzmanf3ee9772019-04-30 20:56:18 +000082 RemoveFile(SeedListPath);
Kostya Serebryany312af152019-02-16 00:14:16 +000083 RmDirRecursive(CorpusDir);
Kostya Serebryany5e67abd2019-04-13 01:57:33 +000084 RmDirRecursive(FeaturesDir);
Kostya Serebryany312af152019-02-16 00:14:16 +000085 }
Kostya Serebryanyd0857482019-02-13 04:04:45 +000086};
87
88struct GlobalEnv {
Kostya Serebryany60c5ded2019-02-14 00:25:43 +000089 Vector<std::string> Args;
90 Vector<std::string> CorpusDirs;
Kostya Serebryanyd0857482019-02-13 04:04:45 +000091 std::string MainCorpusDir;
Kostya Serebryany60c5ded2019-02-14 00:25:43 +000092 std::string TempDir;
Kostya Serebryanyb7cc3d92019-05-23 00:22:46 +000093 std::string DFTDir;
94 std::string DataFlowBinary;
Kostya Serebryany8c279142019-02-15 00:08:16 +000095 Set<uint32_t> Features, Cov;
Kostya Serebryany3f391232019-06-14 19:54:32 +000096 Set<std::string> FilesWithDFT;
Kostya Serebryanyd0857482019-02-13 04:04:45 +000097 Vector<std::string> Files;
Kostya Serebryany60c5ded2019-02-14 00:25:43 +000098 Random *Rand;
Kostya Serebryany9982ee52019-02-15 21:51:15 +000099 std::chrono::system_clock::time_point ProcessStartTime;
Kostya Serebryany60c5ded2019-02-14 00:25:43 +0000100 int Verbosity = 0;
101
Kostya Serebryany9982ee52019-02-15 21:51:15 +0000102 size_t NumTimeouts = 0;
103 size_t NumOOMs = 0;
104 size_t NumCrashes = 0;
105
106
Kostya Serebryany8e918d62019-02-14 21:09:32 +0000107 size_t NumRuns = 0;
108
Kostya Serebryanydb88fc52019-06-14 22:56:50 +0000109 std::string StopFile() { return DirPlusFile(TempDir, "STOP"); }
110
Kostya Serebryany9982ee52019-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
Kostya Serebryany60c5ded2019-02-14 00:25:43 +0000117 FuzzJob *CreateNewJob(size_t JobId) {
118 Command Cmd(Args);
119 Cmd.removeFlag("fork");
Kostya Serebryany65132e22019-04-12 20:20:57 +0000120 Cmd.removeFlag("runs");
Kostya Serebryanyb7cc3d92019-05-23 00:22:46 +0000121 Cmd.removeFlag("collect_data_flow");
Kostya Serebryany60c5ded2019-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.
Kostya Serebryany8e918d62019-02-14 21:09:32 +0000125 Cmd.addFlag("print_final_stats", "1");
Kostya Serebryany77cbc622019-02-15 01:22:00 +0000126 Cmd.addFlag("print_funcs", "0"); // no need to spend time symbolizing.
Kostya Serebryany60c5ded2019-02-14 00:25:43 +0000127 Cmd.addFlag("max_total_time", std::to_string(std::min((size_t)300, JobId)));
Kostya Serebryanydb88fc52019-06-14 22:56:50 +0000128 Cmd.addFlag("stop_file", StopFile());
Kostya Serebryanyb7cc3d92019-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 }
Kostya Serebryany60c5ded2019-02-14 00:25:43 +0000134 auto Job = new FuzzJob;
135 std::string Seeds;
Kostya Serebryany77cbc622019-02-15 01:22:00 +0000136 if (size_t CorpusSubsetSize =
Kostya Serebryany3f391232019-06-14 19:54:32 +0000137 std::min(Files.size(), (size_t)sqrt(Files.size() + 2))) {
Kostya Serebryanydb88fc52019-06-14 22:56:50 +0000138 auto Time1 = std::chrono::system_clock::now();
Kostya Serebryany3f391232019-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 }
Kostya Serebryanydb88fc52019-06-14 22:56:50 +0000144 auto Time2 = std::chrono::system_clock::now();
145 Job->DftTimeInSeconds = duration_cast<seconds>(Time2 - Time1).count();
Kostya Serebryany3f391232019-06-14 19:54:32 +0000146 }
Jonathan Metzmanf3ee9772019-04-30 20:56:18 +0000147 if (!Seeds.empty()) {
Kostya Serebryanyb7cc3d92019-05-23 00:22:46 +0000148 Job->SeedListPath =
149 DirPlusFile(TempDir, std::to_string(JobId) + ".seeds");
Jonathan Metzmanf3ee9772019-04-30 20:56:18 +0000150 WriteToFile(Seeds, Job->SeedListPath);
151 Cmd.addFlag("seed_inputs", "@" + Job->SeedListPath);
152 }
Kostya Serebryany60c5ded2019-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));
Kostya Serebryany5e67abd2019-04-13 01:57:33 +0000155 Job->FeaturesDir = DirPlusFile(TempDir, "F" + std::to_string(JobId));
Kostya Serebryany60c5ded2019-02-14 00:25:43 +0000156 Job->CFPath = DirPlusFile(TempDir, std::to_string(JobId) + ".merge");
Kostya Serebryanydb88fc52019-06-14 22:56:50 +0000157 Job->JobId = JobId;
Kostya Serebryany60c5ded2019-02-14 00:25:43 +0000158
159
160 Cmd.addArgument(Job->CorpusDir);
Kostya Serebryany5e67abd2019-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 }
Kostya Serebryany60c5ded2019-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) {
Kostya Serebryanybbb85222019-04-19 01:39:14 +0000181 auto Stats = ParseFinalStatsFromLog(Job->LogPath);
182 NumRuns += Stats.number_of_executed_units;
183
Kostya Serebryany5e67abd2019-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.
Kostya Serebryany60c5ded2019-02-14 00:25:43 +0000187 GetSizedFilesFromDir(Job->CorpusDir, &TempFiles);
Kostya Serebryany5e67abd2019-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 }
Kostya Serebryanydb88fc52019-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
Kostya Serebryany5e67abd2019-04-13 01:57:33 +0000210 if (MergeCandidates.empty()) return;
Kostya Serebryany60c5ded2019-02-14 00:25:43 +0000211
212 Vector<std::string> FilesToAdd;
Kostya Serebryany8c279142019-02-15 00:08:16 +0000213 Set<uint32_t> NewFeatures, NewCov;
Kostya Serebryany5e67abd2019-04-13 01:57:33 +0000214 CrashResistantMerge(Args, {}, MergeCandidates, &FilesToAdd, Features,
Kostya Serebryany8c279142019-02-15 00:08:16 +0000215 &NewFeatures, Cov, &NewCov, Job->CFPath, false);
Kostya Serebryany60c5ded2019-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 }
Kostya Serebryany60c5ded2019-02-14 00:25:43 +0000222 Features.insert(NewFeatures.begin(), NewFeatures.end());
Kostya Serebryany8c279142019-02-15 00:08:16 +0000223 Cov.insert(NewCov.begin(), NewCov.end());
Kostya Serebryany77cbc622019-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
Kostya Serebryany60c5ded2019-02-14 00:25:43 +0000230 }
Kostya Serebryanyb7cc3d92019-05-23 00:22:46 +0000231
232
233 void CollectDFT(const std::string &InputPath) {
234 if (DataFlowBinary.empty()) return;
Kostya Serebryany3f391232019-06-14 19:54:32 +0000235 if (!FilesWithDFT.insert(InputPath).second) return;
Kostya Serebryanyb7cc3d92019-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();
Kostya Serebryany3f391232019-06-14 19:54:32 +0000245 // Printf("CollectDFT: %s\n", Cmd.toString().c_str());
Kostya Serebryanyb7cc3d92019-05-23 00:22:46 +0000246 ExecuteCommand(Cmd);
247 }
248
Kostya Serebryanyd0857482019-02-13 04:04:45 +0000249};
250
Kostya Serebryany60c5ded2019-02-14 00:25:43 +0000251struct JobQueue {
252 std::queue<FuzzJob *> Qu;
253 std::mutex Mu;
Kostya Serebryanydb88fc52019-06-14 22:56:50 +0000254 std::condition_variable Cv;
Kostya Serebryanyd0857482019-02-13 04:04:45 +0000255
Kostya Serebryany60c5ded2019-02-14 00:25:43 +0000256 void Push(FuzzJob *Job) {
Kostya Serebryanydb88fc52019-06-14 22:56:50 +0000257 {
258 std::lock_guard<std::mutex> Lock(Mu);
259 Qu.push(Job);
260 }
261 Cv.notify_one();
Kostya Serebryanyd0857482019-02-13 04:04:45 +0000262 }
Kostya Serebryany60c5ded2019-02-14 00:25:43 +0000263 FuzzJob *Pop() {
Kostya Serebryanydb88fc52019-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());
Kostya Serebryany60c5ded2019-02-14 00:25:43 +0000268 auto Job = Qu.front();
269 Qu.pop();
270 return Job;
271 }
272};
273
Kostya Serebryanydb88fc52019-06-14 22:56:50 +0000274void WorkerThread(JobQueue *FuzzQ, JobQueue *MergeQ) {
275 while (auto Job = FuzzQ->Pop()) {
Kostya Serebryany60c5ded2019-02-14 00:25:43 +0000276 // Printf("WorkerThread: job %p\n", Job);
Kostya Serebryany60c5ded2019-02-14 00:25:43 +0000277 Job->ExitCode = ExecuteCommand(Job->Cmd);
278 MergeQ->Push(Job);
279 }
Kostya Serebryanyd0857482019-02-13 04:04:45 +0000280}
281
Kostya Serebryany5c08e812019-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,
Kostya Serebryany60c5ded2019-02-14 00:25:43 +0000285 const Vector<std::string> &CorpusDirs, int NumJobs) {
Kostya Serebryany9982ee52019-02-15 21:51:15 +0000286 Printf("INFO: -fork=%d: fuzzing in separate process(s)\n", NumJobs);
Kostya Serebryany5c08e812019-02-12 22:48:55 +0000287
Kostya Serebryanyd0857482019-02-13 04:04:45 +0000288 GlobalEnv Env;
Kostya Serebryany60c5ded2019-02-14 00:25:43 +0000289 Env.Args = Args;
290 Env.CorpusDirs = CorpusDirs;
291 Env.Rand = &Rand;
292 Env.Verbosity = Options.Verbosity;
Kostya Serebryany9982ee52019-02-15 21:51:15 +0000293 Env.ProcessStartTime = std::chrono::system_clock::now();
Kostya Serebryanyb7cc3d92019-05-23 00:22:46 +0000294 Env.DataFlowBinary = Options.CollectDataFlow;
Kostya Serebryanyd0857482019-02-13 04:04:45 +0000295
Kostya Serebryany5c08e812019-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());
Kostya Serebryany60c5ded2019-02-14 00:25:43 +0000300 Env.TempDir = TempPath(".dir");
Kostya Serebryanyb7cc3d92019-05-23 00:22:46 +0000301 Env.DFTDir = DirPlusFile(Env.TempDir, "DFT");
Kostya Serebryany60c5ded2019-02-14 00:25:43 +0000302 RmDirRecursive(Env.TempDir); // in case there is a leftover from old runs.
303 MkDir(Env.TempDir);
Kostya Serebryanyb7cc3d92019-05-23 00:22:46 +0000304 MkDir(Env.DFTDir);
Kostya Serebryany5c08e812019-02-12 22:48:55 +0000305
Kostya Serebryany5c08e812019-02-12 22:48:55 +0000306
Kostya Serebryanyd0857482019-02-13 04:04:45 +0000307 if (CorpusDirs.empty())
Kostya Serebryany60c5ded2019-02-14 00:25:43 +0000308 MkDir(Env.MainCorpusDir = DirPlusFile(Env.TempDir, "C"));
Kostya Serebryanyd0857482019-02-13 04:04:45 +0000309 else
310 Env.MainCorpusDir = CorpusDirs[0];
311
Kostya Serebryany60c5ded2019-02-14 00:25:43 +0000312 auto CFPath = DirPlusFile(Env.TempDir, "merge.txt");
313 CrashResistantMerge(Env.Args, {}, SeedFiles, &Env.Files, {}, &Env.Features,
Kostya Serebryany8c279142019-02-15 00:08:16 +0000314 {}, &Env.Cov,
Kostya Serebryanyd0857482019-02-13 04:04:45 +0000315 CFPath, false);
316 RemoveFile(CFPath);
Kostya Serebryany9982ee52019-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());
Kostya Serebryanyd0857482019-02-13 04:04:45 +0000319
Kostya Serebryany5c08e812019-02-12 22:48:55 +0000320 int ExitCode = 0;
Kostya Serebryanyd0857482019-02-13 04:04:45 +0000321
Kostya Serebryany60c5ded2019-02-14 00:25:43 +0000322 JobQueue FuzzQ, MergeQ;
Kostya Serebryanydb88fc52019-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 };
Kostya Serebryanyd0857482019-02-13 04:04:45 +0000330
Kostya Serebryany60c5ded2019-02-14 00:25:43 +0000331 size_t JobId = 1;
332 Vector<std::thread> Threads;
333 for (int t = 0; t < NumJobs; t++) {
Kostya Serebryanydb88fc52019-06-14 22:56:50 +0000334 Threads.push_back(std::thread(WorkerThread, &FuzzQ, &MergeQ));
Kostya Serebryany60c5ded2019-02-14 00:25:43 +0000335 FuzzQ.Push(Env.CreateNewJob(JobId++));
Kostya Serebryany5c08e812019-02-12 22:48:55 +0000336 }
337
Kostya Serebryany9982ee52019-02-15 21:51:15 +0000338 while (true) {
Kostya Serebryany312af152019-02-16 00:14:16 +0000339 std::unique_ptr<FuzzJob> Job(MergeQ.Pop());
Kostya Serebryanydb88fc52019-06-14 22:56:50 +0000340 if (!Job)
341 break;
Kostya Serebryany60c5ded2019-02-14 00:25:43 +0000342 ExitCode = Job->ExitCode;
Kostya Serebryany312af152019-02-16 00:14:16 +0000343 if (ExitCode == Options.InterruptExitCode) {
344 Printf("==%lu== libFuzzer: a child was interrupted; exiting\n", GetPid());
Kostya Serebryanydb88fc52019-06-14 22:56:50 +0000345 StopJobs();
Kostya Serebryany312af152019-02-16 00:14:16 +0000346 break;
347 }
Kostya Serebryany5e67abd2019-04-13 01:57:33 +0000348 Fuzzer::MaybeExitGracefully();
Kostya Serebryany312af152019-02-16 00:14:16 +0000349
350 Env.RunOneMergeJob(Job.get());
Kostya Serebryany60c5ded2019-02-14 00:25:43 +0000351
352 // Continue if our crash is one of the ignorred ones.
353 if (Options.IgnoreTimeouts && ExitCode == Options.TimeoutExitCode)
Kostya Serebryany9982ee52019-02-15 21:51:15 +0000354 Env.NumTimeouts++;
Kostya Serebryany60c5ded2019-02-14 00:25:43 +0000355 else if (Options.IgnoreOOMs && ExitCode == Options.OOMExitCode)
Kostya Serebryany9982ee52019-02-15 21:51:15 +0000356 Env.NumOOMs++;
Kostya Serebryany60c5ded2019-02-14 00:25:43 +0000357 else if (ExitCode != 0) {
Kostya Serebryany9982ee52019-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'))
Kostya Serebryany65132e22019-04-12 20:20:57 +0000363 if (Line.find("ERROR:") != Line.npos ||
364 Line.find("runtime error:") != Line.npos)
Kostya Serebryany9982ee52019-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());
Kostya Serebryanydb88fc52019-06-14 22:56:50 +0000370 StopJobs();
371 break;
Kostya Serebryany9982ee52019-02-15 21:51:15 +0000372 }
Kostya Serebryany60c5ded2019-02-14 00:25:43 +0000373 }
Kostya Serebryany9982ee52019-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.
Kostya Serebryanydb88fc52019-06-14 22:56:50 +0000379 if (Options.MaxTotalTimeSec > 0 &&
Kostya Serebryany9982ee52019-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());
Kostya Serebryanydb88fc52019-06-14 22:56:50 +0000383 StopJobs();
384 break;
Kostya Serebryany9982ee52019-02-15 21:51:15 +0000385 }
Kostya Serebryanydb88fc52019-06-14 22:56:50 +0000386 if (Env.NumRuns >= Options.MaxNumberOfRuns) {
Kostya Serebryany65132e22019-04-12 20:20:57 +0000387 Printf("INFO: fuzzed for %zd iterations, wrapping up soon\n",
388 Env.NumRuns);
Kostya Serebryanydb88fc52019-06-14 22:56:50 +0000389 StopJobs();
390 break;
Kostya Serebryany65132e22019-04-12 20:20:57 +0000391 }
Kostya Serebryany9982ee52019-02-15 21:51:15 +0000392
Kostya Serebryanydb88fc52019-06-14 22:56:50 +0000393 FuzzQ.Push(Env.CreateNewJob(JobId++));
Kostya Serebryany60c5ded2019-02-14 00:25:43 +0000394 }
Kostya Serebryany60c5ded2019-02-14 00:25:43 +0000395
396 for (auto &T : Threads)
397 T.join();
398
Jonathan Metzman518514e2019-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
Kostya Serebryany5c08e812019-02-12 22:48:55 +0000403 // Use the exit code from the last child process.
Kostya Serebryany9982ee52019-02-15 21:51:15 +0000404 Printf("INFO: exiting: %d time: %zds\n", ExitCode,
405 Env.secondsSinceProcessStartUp());
Kostya Serebryany5c08e812019-02-12 22:48:55 +0000406 exit(ExitCode);
407}
408
409} // namespace fuzzer