blob: 5c4855f8284d3177568befdf2f48a05f81f73912 [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>
kccdd391142019-02-14 21:09:32 +000022#include <fstream>
kccedefdf32019-02-16 00:14:16 +000023#include <memory>
kcc6526f1d2019-02-14 00:25:43 +000024#include <mutex>
kcc6526f1d2019-02-14 00:25:43 +000025#include <queue>
kccdd391142019-02-14 21:09:32 +000026#include <sstream>
27#include <thread>
kcc6526f1d2019-02-14 00:25:43 +000028
kcc2e6ca5c2019-02-12 22:48:55 +000029namespace fuzzer {
30
kccdd391142019-02-14 21:09:32 +000031struct Stats {
32 size_t number_of_executed_units = 0;
33 size_t peak_rss_mb = 0;
34 size_t average_exec_per_sec = 0;
35};
36
37static Stats ParseFinalStatsFromLog(const std::string &LogPath) {
38 std::ifstream In(LogPath);
39 std::string Line;
40 Stats Res;
41 struct {
42 const char *Name;
43 size_t *Var;
44 } NameVarPairs[] = {
45 {"stat::number_of_executed_units:", &Res.number_of_executed_units},
46 {"stat::peak_rss_mb:", &Res.peak_rss_mb},
47 {"stat::average_exec_per_sec:", &Res.average_exec_per_sec},
48 {nullptr, nullptr},
49 };
50 while (std::getline(In, Line, '\n')) {
51 if (Line.find("stat::") != 0) continue;
52 std::istringstream ISS(Line);
53 std::string Name;
54 size_t Val;
55 ISS >> Name >> Val;
56 for (size_t i = 0; NameVarPairs[i].Name; i++)
57 if (Name == NameVarPairs[i].Name)
58 *NameVarPairs[i].Var = Val;
59 }
60 return Res;
61}
62
kcc64bcb922019-02-13 04:04:45 +000063struct FuzzJob {
64 // Inputs.
65 Command Cmd;
kcc64bcb922019-02-13 04:04:45 +000066 std::string CorpusDir;
kcc1c5afe22019-04-13 01:57:33 +000067 std::string FeaturesDir;
kcc64bcb922019-02-13 04:04:45 +000068 std::string LogPath;
metzmane9b95bc2019-04-30 20:56:18 +000069 std::string SeedListPath;
kcc64bcb922019-02-13 04:04:45 +000070 std::string CFPath;
kcc64bcb922019-02-13 04:04:45 +000071
72 // Fuzzing Outputs.
73 int ExitCode;
kccedefdf32019-02-16 00:14:16 +000074
75 ~FuzzJob() {
76 RemoveFile(CFPath);
77 RemoveFile(LogPath);
metzmane9b95bc2019-04-30 20:56:18 +000078 RemoveFile(SeedListPath);
kccedefdf32019-02-16 00:14:16 +000079 RmDirRecursive(CorpusDir);
kcc1c5afe22019-04-13 01:57:33 +000080 RmDirRecursive(FeaturesDir);
kccedefdf32019-02-16 00:14:16 +000081 }
kcc64bcb922019-02-13 04:04:45 +000082};
83
84struct GlobalEnv {
kcc6526f1d2019-02-14 00:25:43 +000085 Vector<std::string> Args;
86 Vector<std::string> CorpusDirs;
kcc64bcb922019-02-13 04:04:45 +000087 std::string MainCorpusDir;
kcc6526f1d2019-02-14 00:25:43 +000088 std::string TempDir;
kccd701d9e2019-05-23 00:22:46 +000089 std::string DFTDir;
90 std::string DataFlowBinary;
kcc98a86242019-02-15 00:08:16 +000091 Set<uint32_t> Features, Cov;
kcc0a66b5b2019-06-14 19:54:32 +000092 Set<std::string> FilesWithDFT;
kcc64bcb922019-02-13 04:04:45 +000093 Vector<std::string> Files;
kcc6526f1d2019-02-14 00:25:43 +000094 Random *Rand;
kcc55e54ed2019-02-15 21:51:15 +000095 std::chrono::system_clock::time_point ProcessStartTime;
kcc6526f1d2019-02-14 00:25:43 +000096 int Verbosity = 0;
97
kcc55e54ed2019-02-15 21:51:15 +000098 size_t NumTimeouts = 0;
99 size_t NumOOMs = 0;
100 size_t NumCrashes = 0;
101
102
kccdd391142019-02-14 21:09:32 +0000103 size_t NumRuns = 0;
104
kcc55e54ed2019-02-15 21:51:15 +0000105 size_t secondsSinceProcessStartUp() const {
106 return std::chrono::duration_cast<std::chrono::seconds>(
107 std::chrono::system_clock::now() - ProcessStartTime)
108 .count();
109 }
110
kcc6526f1d2019-02-14 00:25:43 +0000111 FuzzJob *CreateNewJob(size_t JobId) {
112 Command Cmd(Args);
113 Cmd.removeFlag("fork");
kcc18b370a2019-04-12 20:20:57 +0000114 Cmd.removeFlag("runs");
kccd701d9e2019-05-23 00:22:46 +0000115 Cmd.removeFlag("collect_data_flow");
kcc6526f1d2019-02-14 00:25:43 +0000116 for (auto &C : CorpusDirs) // Remove all corpora from the args.
117 Cmd.removeArgument(C);
118 Cmd.addFlag("reload", "0"); // working in an isolated dir, no reload.
kccdd391142019-02-14 21:09:32 +0000119 Cmd.addFlag("print_final_stats", "1");
kcc9c0ed932019-02-15 01:22:00 +0000120 Cmd.addFlag("print_funcs", "0"); // no need to spend time symbolizing.
kcc6526f1d2019-02-14 00:25:43 +0000121 Cmd.addFlag("max_total_time", std::to_string(std::min((size_t)300, JobId)));
kccd701d9e2019-05-23 00:22:46 +0000122 if (!DataFlowBinary.empty()) {
123 Cmd.addFlag("data_flow_trace", DFTDir);
124 if (!Cmd.hasFlag("focus_function"))
125 Cmd.addFlag("focus_function", "auto");
126 }
kcc6526f1d2019-02-14 00:25:43 +0000127 auto Job = new FuzzJob;
128 std::string Seeds;
kcc9c0ed932019-02-15 01:22:00 +0000129 if (size_t CorpusSubsetSize =
kcc0a66b5b2019-06-14 19:54:32 +0000130 std::min(Files.size(), (size_t)sqrt(Files.size() + 2))) {
131 for (size_t i = 0; i < CorpusSubsetSize; i++) {
132 auto &SF = Files[Rand->SkewTowardsLast(Files.size())];
133 Seeds += (Seeds.empty() ? "" : ",") + SF;
134 CollectDFT(SF);
135 }
136 }
metzmane9b95bc2019-04-30 20:56:18 +0000137 if (!Seeds.empty()) {
kccd701d9e2019-05-23 00:22:46 +0000138 Job->SeedListPath =
139 DirPlusFile(TempDir, std::to_string(JobId) + ".seeds");
metzmane9b95bc2019-04-30 20:56:18 +0000140 WriteToFile(Seeds, Job->SeedListPath);
141 Cmd.addFlag("seed_inputs", "@" + Job->SeedListPath);
142 }
kcc6526f1d2019-02-14 00:25:43 +0000143 Job->LogPath = DirPlusFile(TempDir, std::to_string(JobId) + ".log");
144 Job->CorpusDir = DirPlusFile(TempDir, "C" + std::to_string(JobId));
kcc1c5afe22019-04-13 01:57:33 +0000145 Job->FeaturesDir = DirPlusFile(TempDir, "F" + std::to_string(JobId));
kcc6526f1d2019-02-14 00:25:43 +0000146 Job->CFPath = DirPlusFile(TempDir, std::to_string(JobId) + ".merge");
147
148
149 Cmd.addArgument(Job->CorpusDir);
kcc1c5afe22019-04-13 01:57:33 +0000150 Cmd.addFlag("features_dir", Job->FeaturesDir);
151
152 for (auto &D : {Job->CorpusDir, Job->FeaturesDir}) {
153 RmDirRecursive(D);
154 MkDir(D);
155 }
kcc6526f1d2019-02-14 00:25:43 +0000156
157 Cmd.setOutputFile(Job->LogPath);
158 Cmd.combineOutAndErr();
159
160 Job->Cmd = Cmd;
161
162 if (Verbosity >= 2)
163 Printf("Job %zd/%p Created: %s\n", JobId, Job,
164 Job->Cmd.toString().c_str());
165 // Start from very short runs and gradually increase them.
166 return Job;
167 }
168
169 void RunOneMergeJob(FuzzJob *Job) {
kcce5ef7c02019-04-19 01:39:14 +0000170 auto Stats = ParseFinalStatsFromLog(Job->LogPath);
171 NumRuns += Stats.number_of_executed_units;
172
kcc1c5afe22019-04-13 01:57:33 +0000173 Vector<SizedFile> TempFiles, MergeCandidates;
174 // Read all newly created inputs and their feature sets.
175 // Choose only those inputs that have new features.
kcc6526f1d2019-02-14 00:25:43 +0000176 GetSizedFilesFromDir(Job->CorpusDir, &TempFiles);
kcc1c5afe22019-04-13 01:57:33 +0000177 std::sort(TempFiles.begin(), TempFiles.end());
178 for (auto &F : TempFiles) {
179 auto FeatureFile = F.File;
180 FeatureFile.replace(0, Job->CorpusDir.size(), Job->FeaturesDir);
181 auto FeatureBytes = FileToVector(FeatureFile, 0, false);
182 assert((FeatureBytes.size() % sizeof(uint32_t)) == 0);
183 Vector<uint32_t> NewFeatures(FeatureBytes.size() / sizeof(uint32_t));
184 memcpy(NewFeatures.data(), FeatureBytes.data(), FeatureBytes.size());
185 for (auto Ft : NewFeatures) {
186 if (!Features.count(Ft)) {
187 MergeCandidates.push_back(F);
188 break;
189 }
190 }
191 }
192 if (MergeCandidates.empty()) return;
kcc6526f1d2019-02-14 00:25:43 +0000193
194 Vector<std::string> FilesToAdd;
kcc98a86242019-02-15 00:08:16 +0000195 Set<uint32_t> NewFeatures, NewCov;
kcc1c5afe22019-04-13 01:57:33 +0000196 CrashResistantMerge(Args, {}, MergeCandidates, &FilesToAdd, Features,
kcc98a86242019-02-15 00:08:16 +0000197 &NewFeatures, Cov, &NewCov, Job->CFPath, false);
kcc6526f1d2019-02-14 00:25:43 +0000198 for (auto &Path : FilesToAdd) {
199 auto U = FileToVector(Path);
200 auto NewPath = DirPlusFile(MainCorpusDir, Hash(U));
201 WriteToFile(U, NewPath);
202 Files.push_back(NewPath);
203 }
kcc6526f1d2019-02-14 00:25:43 +0000204 Features.insert(NewFeatures.begin(), NewFeatures.end());
kcc98a86242019-02-15 00:08:16 +0000205 Cov.insert(NewCov.begin(), NewCov.end());
kcc9c0ed932019-02-15 01:22:00 +0000206 for (auto Idx : NewCov)
207 if (auto *TE = TPC.PCTableEntryByIdx(Idx))
208 if (TPC.PcIsFuncEntry(TE))
209 PrintPC(" NEW_FUNC: %p %F %L\n", "",
210 TPC.GetNextInstructionPc(TE->PC));
211
kcc55e54ed2019-02-15 21:51:15 +0000212 if (!FilesToAdd.empty() || Job->ExitCode != 0)
213 Printf("#%zd: cov: %zd ft: %zd corp: %zd exec/s %zd "
214 "oom/timeout/crash: %zd/%zd/%zd time: %zds\n", NumRuns,
kcc98a86242019-02-15 00:08:16 +0000215 Cov.size(), Features.size(), Files.size(),
kcc55e54ed2019-02-15 21:51:15 +0000216 Stats.average_exec_per_sec,
217 NumOOMs, NumTimeouts, NumCrashes, secondsSinceProcessStartUp());
kcc6526f1d2019-02-14 00:25:43 +0000218 }
kccd701d9e2019-05-23 00:22:46 +0000219
220
221 void CollectDFT(const std::string &InputPath) {
222 if (DataFlowBinary.empty()) return;
kcc0a66b5b2019-06-14 19:54:32 +0000223 if (!FilesWithDFT.insert(InputPath).second) return;
kccd701d9e2019-05-23 00:22:46 +0000224 Command Cmd(Args);
225 Cmd.removeFlag("fork");
226 Cmd.removeFlag("runs");
227 Cmd.addFlag("data_flow_trace", DFTDir);
228 Cmd.addArgument(InputPath);
229 for (auto &C : CorpusDirs) // Remove all corpora from the args.
230 Cmd.removeArgument(C);
231 Cmd.setOutputFile(DirPlusFile(TempDir, "dft.log"));
232 Cmd.combineOutAndErr();
kcc0a66b5b2019-06-14 19:54:32 +0000233 // Printf("CollectDFT: %s\n", Cmd.toString().c_str());
kccd701d9e2019-05-23 00:22:46 +0000234 ExecuteCommand(Cmd);
235 }
236
kcc64bcb922019-02-13 04:04:45 +0000237};
238
kcc6526f1d2019-02-14 00:25:43 +0000239struct JobQueue {
240 std::queue<FuzzJob *> Qu;
241 std::mutex Mu;
kcc64bcb922019-02-13 04:04:45 +0000242
kcc6526f1d2019-02-14 00:25:43 +0000243 void Push(FuzzJob *Job) {
244 std::lock_guard<std::mutex> Lock(Mu);
245 Qu.push(Job);
kcc64bcb922019-02-13 04:04:45 +0000246 }
kcc6526f1d2019-02-14 00:25:43 +0000247 FuzzJob *Pop() {
248 std::lock_guard<std::mutex> Lock(Mu);
249 if (Qu.empty()) return nullptr;
250 auto Job = Qu.front();
251 Qu.pop();
252 return Job;
253 }
254};
255
256void WorkerThread(std::atomic<bool> *Stop, JobQueue *FuzzQ, JobQueue *MergeQ) {
kccb1fa9e02019-02-14 01:11:29 +0000257 while (!Stop->load()) {
kcc6526f1d2019-02-14 00:25:43 +0000258 auto Job = FuzzQ->Pop();
259 // Printf("WorkerThread: job %p\n", Job);
260 if (!Job) {
261 SleepSeconds(1);
262 continue;
263 }
264 Job->ExitCode = ExecuteCommand(Job->Cmd);
265 MergeQ->Push(Job);
266 }
kcc64bcb922019-02-13 04:04:45 +0000267}
268
kcc2e6ca5c2019-02-12 22:48:55 +0000269// This is just a skeleton of an experimental -fork=1 feature.
270void FuzzWithFork(Random &Rand, const FuzzingOptions &Options,
271 const Vector<std::string> &Args,
kcc6526f1d2019-02-14 00:25:43 +0000272 const Vector<std::string> &CorpusDirs, int NumJobs) {
kcc55e54ed2019-02-15 21:51:15 +0000273 Printf("INFO: -fork=%d: fuzzing in separate process(s)\n", NumJobs);
kcc2e6ca5c2019-02-12 22:48:55 +0000274
kcc64bcb922019-02-13 04:04:45 +0000275 GlobalEnv Env;
kcc6526f1d2019-02-14 00:25:43 +0000276 Env.Args = Args;
277 Env.CorpusDirs = CorpusDirs;
278 Env.Rand = &Rand;
279 Env.Verbosity = Options.Verbosity;
kcc55e54ed2019-02-15 21:51:15 +0000280 Env.ProcessStartTime = std::chrono::system_clock::now();
kccd701d9e2019-05-23 00:22:46 +0000281 Env.DataFlowBinary = Options.CollectDataFlow;
kcc64bcb922019-02-13 04:04:45 +0000282
kcc2e6ca5c2019-02-12 22:48:55 +0000283 Vector<SizedFile> SeedFiles;
284 for (auto &Dir : CorpusDirs)
285 GetSizedFilesFromDir(Dir, &SeedFiles);
286 std::sort(SeedFiles.begin(), SeedFiles.end());
kcc6526f1d2019-02-14 00:25:43 +0000287 Env.TempDir = TempPath(".dir");
kccd701d9e2019-05-23 00:22:46 +0000288 Env.DFTDir = DirPlusFile(Env.TempDir, "DFT");
kcc6526f1d2019-02-14 00:25:43 +0000289 RmDirRecursive(Env.TempDir); // in case there is a leftover from old runs.
290 MkDir(Env.TempDir);
kccd701d9e2019-05-23 00:22:46 +0000291 MkDir(Env.DFTDir);
kcc2e6ca5c2019-02-12 22:48:55 +0000292
kcc2e6ca5c2019-02-12 22:48:55 +0000293
kcc64bcb922019-02-13 04:04:45 +0000294 if (CorpusDirs.empty())
kcc6526f1d2019-02-14 00:25:43 +0000295 MkDir(Env.MainCorpusDir = DirPlusFile(Env.TempDir, "C"));
kcc64bcb922019-02-13 04:04:45 +0000296 else
297 Env.MainCorpusDir = CorpusDirs[0];
298
kcc6526f1d2019-02-14 00:25:43 +0000299 auto CFPath = DirPlusFile(Env.TempDir, "merge.txt");
300 CrashResistantMerge(Env.Args, {}, SeedFiles, &Env.Files, {}, &Env.Features,
kcc98a86242019-02-15 00:08:16 +0000301 {}, &Env.Cov,
kcc64bcb922019-02-13 04:04:45 +0000302 CFPath, false);
303 RemoveFile(CFPath);
kcc55e54ed2019-02-15 21:51:15 +0000304 Printf("INFO: -fork=%d: %zd seed inputs, starting to fuzz in %s\n", NumJobs,
305 Env.Files.size(), Env.TempDir.c_str());
kcc64bcb922019-02-13 04:04:45 +0000306
kcc2e6ca5c2019-02-12 22:48:55 +0000307 int ExitCode = 0;
kcc64bcb922019-02-13 04:04:45 +0000308
kcc6526f1d2019-02-14 00:25:43 +0000309 JobQueue FuzzQ, MergeQ;
310 std::atomic<bool> Stop(false);
kcc64bcb922019-02-13 04:04:45 +0000311
kcc6526f1d2019-02-14 00:25:43 +0000312 size_t JobId = 1;
313 Vector<std::thread> Threads;
314 for (int t = 0; t < NumJobs; t++) {
315 Threads.push_back(std::thread(WorkerThread, &Stop, &FuzzQ, &MergeQ));
316 FuzzQ.Push(Env.CreateNewJob(JobId++));
kcc2e6ca5c2019-02-12 22:48:55 +0000317 }
318
kcc55e54ed2019-02-15 21:51:15 +0000319 while (true) {
kccedefdf32019-02-16 00:14:16 +0000320 std::unique_ptr<FuzzJob> Job(MergeQ.Pop());
kcc6526f1d2019-02-14 00:25:43 +0000321 if (!Job) {
kcc55e54ed2019-02-15 21:51:15 +0000322 if (Stop)
323 break;
kcc6526f1d2019-02-14 00:25:43 +0000324 SleepSeconds(1);
325 continue;
326 }
327 ExitCode = Job->ExitCode;
kccedefdf32019-02-16 00:14:16 +0000328 if (ExitCode == Options.InterruptExitCode) {
329 Printf("==%lu== libFuzzer: a child was interrupted; exiting\n", GetPid());
330 Stop = true;
331 break;
332 }
kcc1c5afe22019-04-13 01:57:33 +0000333 Fuzzer::MaybeExitGracefully();
kccedefdf32019-02-16 00:14:16 +0000334
335 Env.RunOneMergeJob(Job.get());
kcc6526f1d2019-02-14 00:25:43 +0000336
337 // Continue if our crash is one of the ignorred ones.
338 if (Options.IgnoreTimeouts && ExitCode == Options.TimeoutExitCode)
kcc55e54ed2019-02-15 21:51:15 +0000339 Env.NumTimeouts++;
kcc6526f1d2019-02-14 00:25:43 +0000340 else if (Options.IgnoreOOMs && ExitCode == Options.OOMExitCode)
kcc55e54ed2019-02-15 21:51:15 +0000341 Env.NumOOMs++;
kcc6526f1d2019-02-14 00:25:43 +0000342 else if (ExitCode != 0) {
kcc55e54ed2019-02-15 21:51:15 +0000343 Env.NumCrashes++;
344 if (Options.IgnoreCrashes) {
345 std::ifstream In(Job->LogPath);
346 std::string Line;
347 while (std::getline(In, Line, '\n'))
kcc18b370a2019-04-12 20:20:57 +0000348 if (Line.find("ERROR:") != Line.npos ||
349 Line.find("runtime error:") != Line.npos)
kcc55e54ed2019-02-15 21:51:15 +0000350 Printf("%s\n", Line.c_str());
351 } else {
352 // And exit if we don't ignore this crash.
353 Printf("INFO: log from the inner process:\n%s",
354 FileToString(Job->LogPath).c_str());
355 Stop = true;
356 }
kcc6526f1d2019-02-14 00:25:43 +0000357 }
kcc55e54ed2019-02-15 21:51:15 +0000358
359 // Stop if we are over the time budget.
360 // This is not precise, since other threads are still running
361 // and we will wait while joining them.
362 // We also don't stop instantly: other jobs need to finish.
363 if (Options.MaxTotalTimeSec > 0 && !Stop &&
364 Env.secondsSinceProcessStartUp() >= (size_t)Options.MaxTotalTimeSec) {
365 Printf("INFO: fuzzed for %zd seconds, wrapping up soon\n",
366 Env.secondsSinceProcessStartUp());
367 Stop = true;
368 }
morehousea3f53122019-04-16 17:38:19 +0000369 if (!Stop && Env.NumRuns >= Options.MaxNumberOfRuns) {
kcc18b370a2019-04-12 20:20:57 +0000370 Printf("INFO: fuzzed for %zd iterations, wrapping up soon\n",
371 Env.NumRuns);
372 Stop = true;
373 }
kcc55e54ed2019-02-15 21:51:15 +0000374
375 if (!Stop)
376 FuzzQ.Push(Env.CreateNewJob(JobId++));
kcc6526f1d2019-02-14 00:25:43 +0000377 }
378 Stop = true;
379
380 for (auto &T : Threads)
381 T.join();
382
metzmane847d8a2019-02-27 19:27:16 +0000383 // The workers have terminated. Don't try to remove the directory before they
384 // terminate to avoid a race condition preventing cleanup on Windows.
385 RmDirRecursive(Env.TempDir);
386
kcc2e6ca5c2019-02-12 22:48:55 +0000387 // Use the exit code from the last child process.
kcc55e54ed2019-02-15 21:51:15 +0000388 Printf("INFO: exiting: %d time: %zds\n", ExitCode,
389 Env.secondsSinceProcessStartUp());
kcc2e6ca5c2019-02-12 22:48:55 +0000390 exit(ExitCode);
391}
392
393} // namespace fuzzer