Skip to content

Commit 1ee7f41

Browse files
author
kcc
committed
[libFuzzer] implement a better queue for the fork mode. Add an internal flag -stop_file to allow graceful shutdown of fuzzing. Enhance the logging in the fork mode
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk/lib/fuzzer@363470 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 69e0205 commit 1ee7f41

File tree

5 files changed

+57
-34
lines changed

5 files changed

+57
-34
lines changed

FuzzerDriver.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,8 @@ int FuzzerDriver(int *argc, char ***argv, UserCallback Callback) {
709709
if (Flags.collect_data_flow)
710710
Options.CollectDataFlow = Flags.collect_data_flow;
711711
Options.LazyCounters = Flags.lazy_counters;
712+
if (Flags.stop_file)
713+
Options.StopFile = Flags.stop_file;
712714

713715
unsigned Seed = Flags.seed;
714716
// Initialize Seed.

FuzzerFlags.def

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ FUZZER_FLAG_INT(ignore_crashes, 0, "Ignore crashes in fork mode")
5050
FUZZER_FLAG_INT(merge, 0, "If 1, the 2-nd, 3-rd, etc corpora will be "
5151
"merged into the 1-st corpus. Only interesting units will be taken. "
5252
"This flag can be used to minimize a corpus.")
53+
FUZZER_FLAG_STRING(stop_file, "Stop fuzzing ASAP if this file exists")
5354
FUZZER_FLAG_STRING(merge_inner, "internal flag")
5455
FUZZER_FLAG_STRING(merge_control_file,
5556
"Specify a control file used for the merge process. "

FuzzerFork.cpp

+50-34
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include <atomic>
2121
#include <chrono>
22+
#include <condition_variable>
2223
#include <fstream>
2324
#include <memory>
2425
#include <mutex>
@@ -68,6 +69,9 @@ struct FuzzJob {
6869
std::string LogPath;
6970
std::string SeedListPath;
7071
std::string CFPath;
72+
size_t JobId;
73+
74+
int DftTimeInSeconds = 0;
7175

7276
// Fuzzing Outputs.
7377
int ExitCode;
@@ -102,6 +106,8 @@ struct GlobalEnv {
102106

103107
size_t NumRuns = 0;
104108

109+
std::string StopFile() { return DirPlusFile(TempDir, "STOP"); }
110+
105111
size_t secondsSinceProcessStartUp() const {
106112
return std::chrono::duration_cast<std::chrono::seconds>(
107113
std::chrono::system_clock::now() - ProcessStartTime)
@@ -119,6 +125,7 @@ struct GlobalEnv {
119125
Cmd.addFlag("print_final_stats", "1");
120126
Cmd.addFlag("print_funcs", "0"); // no need to spend time symbolizing.
121127
Cmd.addFlag("max_total_time", std::to_string(std::min((size_t)300, JobId)));
128+
Cmd.addFlag("stop_file", StopFile());
122129
if (!DataFlowBinary.empty()) {
123130
Cmd.addFlag("data_flow_trace", DFTDir);
124131
if (!Cmd.hasFlag("focus_function"))
@@ -128,11 +135,14 @@ struct GlobalEnv {
128135
std::string Seeds;
129136
if (size_t CorpusSubsetSize =
130137
std::min(Files.size(), (size_t)sqrt(Files.size() + 2))) {
138+
auto Time1 = std::chrono::system_clock::now();
131139
for (size_t i = 0; i < CorpusSubsetSize; i++) {
132140
auto &SF = Files[Rand->SkewTowardsLast(Files.size())];
133141
Seeds += (Seeds.empty() ? "" : ",") + SF;
134142
CollectDFT(SF);
135143
}
144+
auto Time2 = std::chrono::system_clock::now();
145+
Job->DftTimeInSeconds = duration_cast<seconds>(Time2 - Time1).count();
136146
}
137147
if (!Seeds.empty()) {
138148
Job->SeedListPath =
@@ -144,6 +154,7 @@ struct GlobalEnv {
144154
Job->CorpusDir = DirPlusFile(TempDir, "C" + std::to_string(JobId));
145155
Job->FeaturesDir = DirPlusFile(TempDir, "F" + std::to_string(JobId));
146156
Job->CFPath = DirPlusFile(TempDir, std::to_string(JobId) + ".merge");
157+
Job->JobId = JobId;
147158

148159

149160
Cmd.addArgument(Job->CorpusDir);
@@ -189,6 +200,13 @@ struct GlobalEnv {
189200
}
190201
}
191202
}
203+
// 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+
192210
if (MergeCandidates.empty()) return;
193211

194212
Vector<std::string> FilesToAdd;
@@ -209,12 +227,6 @@ struct GlobalEnv {
209227
PrintPC(" NEW_FUNC: %p %F %L\n", "",
210228
TPC.GetNextInstructionPc(TE->PC));
211229

212-
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,
215-
Cov.size(), Features.size(), Files.size(),
216-
Stats.average_exec_per_sec,
217-
NumOOMs, NumTimeouts, NumCrashes, secondsSinceProcessStartUp());
218230
}
219231

220232

@@ -239,28 +251,29 @@ struct GlobalEnv {
239251
struct JobQueue {
240252
std::queue<FuzzJob *> Qu;
241253
std::mutex Mu;
254+
std::condition_variable Cv;
242255

243256
void Push(FuzzJob *Job) {
244-
std::lock_guard<std::mutex> Lock(Mu);
245-
Qu.push(Job);
257+
{
258+
std::lock_guard<std::mutex> Lock(Mu);
259+
Qu.push(Job);
260+
}
261+
Cv.notify_one();
246262
}
247263
FuzzJob *Pop() {
248-
std::lock_guard<std::mutex> Lock(Mu);
249-
if (Qu.empty()) return nullptr;
264+
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());
250268
auto Job = Qu.front();
251269
Qu.pop();
252270
return Job;
253271
}
254272
};
255273

256-
void WorkerThread(std::atomic<bool> *Stop, JobQueue *FuzzQ, JobQueue *MergeQ) {
257-
while (!Stop->load()) {
258-
auto Job = FuzzQ->Pop();
274+
void WorkerThread(JobQueue *FuzzQ, JobQueue *MergeQ) {
275+
while (auto Job = FuzzQ->Pop()) {
259276
// Printf("WorkerThread: job %p\n", Job);
260-
if (!Job) {
261-
SleepSeconds(1);
262-
continue;
263-
}
264277
Job->ExitCode = ExecuteCommand(Job->Cmd);
265278
MergeQ->Push(Job);
266279
}
@@ -307,27 +320,29 @@ void FuzzWithFork(Random &Rand, const FuzzingOptions &Options,
307320
int ExitCode = 0;
308321

309322
JobQueue FuzzQ, MergeQ;
310-
std::atomic<bool> Stop(false);
323+
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+
};
311330

312331
size_t JobId = 1;
313332
Vector<std::thread> Threads;
314333
for (int t = 0; t < NumJobs; t++) {
315-
Threads.push_back(std::thread(WorkerThread, &Stop, &FuzzQ, &MergeQ));
334+
Threads.push_back(std::thread(WorkerThread, &FuzzQ, &MergeQ));
316335
FuzzQ.Push(Env.CreateNewJob(JobId++));
317336
}
318337

319338
while (true) {
320339
std::unique_ptr<FuzzJob> Job(MergeQ.Pop());
321-
if (!Job) {
322-
if (Stop)
323-
break;
324-
SleepSeconds(1);
325-
continue;
326-
}
340+
if (!Job)
341+
break;
327342
ExitCode = Job->ExitCode;
328343
if (ExitCode == Options.InterruptExitCode) {
329344
Printf("==%lu== libFuzzer: a child was interrupted; exiting\n", GetPid());
330-
Stop = true;
345+
StopJobs();
331346
break;
332347
}
333348
Fuzzer::MaybeExitGracefully();
@@ -352,30 +367,31 @@ void FuzzWithFork(Random &Rand, const FuzzingOptions &Options,
352367
// And exit if we don't ignore this crash.
353368
Printf("INFO: log from the inner process:\n%s",
354369
FileToString(Job->LogPath).c_str());
355-
Stop = true;
370+
StopJobs();
371+
break;
356372
}
357373
}
358374

359375
// Stop if we are over the time budget.
360376
// This is not precise, since other threads are still running
361377
// and we will wait while joining them.
362378
// We also don't stop instantly: other jobs need to finish.
363-
if (Options.MaxTotalTimeSec > 0 && !Stop &&
379+
if (Options.MaxTotalTimeSec > 0 &&
364380
Env.secondsSinceProcessStartUp() >= (size_t)Options.MaxTotalTimeSec) {
365381
Printf("INFO: fuzzed for %zd seconds, wrapping up soon\n",
366382
Env.secondsSinceProcessStartUp());
367-
Stop = true;
383+
StopJobs();
384+
break;
368385
}
369-
if (!Stop && Env.NumRuns >= Options.MaxNumberOfRuns) {
386+
if (Env.NumRuns >= Options.MaxNumberOfRuns) {
370387
Printf("INFO: fuzzed for %zd iterations, wrapping up soon\n",
371388
Env.NumRuns);
372-
Stop = true;
389+
StopJobs();
390+
break;
373391
}
374392

375-
if (!Stop)
376-
FuzzQ.Push(Env.CreateNewJob(JobId++));
393+
FuzzQ.Push(Env.CreateNewJob(JobId++));
377394
}
378-
Stop = true;
379395

380396
for (auto &T : Threads)
381397
T.join();

FuzzerLoop.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,9 @@ void Fuzzer::Loop(Vector<SizedFile> &CorporaFiles) {
801801

802802
while (true) {
803803
auto Now = system_clock::now();
804+
if (!Options.StopFile.empty() &&
805+
!FileToVector(Options.StopFile, 1, false).empty())
806+
break;
804807
if (duration_cast<seconds>(Now - LastCorpusReload).count() >=
805808
Options.ReloadIntervalSec) {
806809
RereadOutputCorpus(MaxInputLen);

FuzzerOptions.h

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ struct FuzzingOptions {
5353
std::string DataFlowTrace;
5454
std::string CollectDataFlow;
5555
std::string FeaturesDir;
56+
std::string StopFile;
5657
bool SaveArtifacts = true;
5758
bool PrintNEW = true; // Print a status line when new units are found;
5859
bool PrintNewCovPcs = false;

0 commit comments

Comments
 (0)