1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-26 04:32:44 +01:00

[libFuzzer] Properly use unsigned for workers, jobs and NumberOfCpuCores.

std:🧵:hardware_concurrency() returns an unsigned, so I modify
NumberOfCpuCores() to return unsigned too.
The number of cpus is used to define the number of workers, so I decided
to update the worker and jobs flags to be declared as unsigned too.

Differential Revision: https://reviews.llvm.org/D27685

llvm-svn: 289559
This commit is contained in:
Marcos Pividori 2016-12-13 17:45:53 +00:00
parent f6db228eaa
commit 79b26fd29b
4 changed files with 12 additions and 12 deletions

View File

@ -200,10 +200,10 @@ static void PulseThread() {
} }
} }
static void WorkerThread(const std::string &Cmd, std::atomic<int> *Counter, static void WorkerThread(const std::string &Cmd, std::atomic<unsigned> *Counter,
int NumJobs, std::atomic<bool> *HasErrors) { unsigned NumJobs, std::atomic<bool> *HasErrors) {
while (true) { while (true) {
int C = (*Counter)++; unsigned C = (*Counter)++;
if (C >= NumJobs) break; if (C >= NumJobs) break;
std::string Log = "fuzz-" + std::to_string(C) + ".log"; std::string Log = "fuzz-" + std::to_string(C) + ".log";
std::string ToRun = Cmd + " > " + Log + " 2>&1\n"; std::string ToRun = Cmd + " > " + Log + " 2>&1\n";
@ -213,7 +213,7 @@ static void WorkerThread(const std::string &Cmd, std::atomic<int> *Counter,
if (ExitCode != 0) if (ExitCode != 0)
*HasErrors = true; *HasErrors = true;
std::lock_guard<std::mutex> Lock(Mu); std::lock_guard<std::mutex> Lock(Mu);
Printf("================== Job %d exited with exit code %d ============\n", Printf("================== Job %u exited with exit code %d ============\n",
C, ExitCode); C, ExitCode);
fuzzer::CopyFileToErr(Log); fuzzer::CopyFileToErr(Log);
} }
@ -231,14 +231,14 @@ std::string CloneArgsWithoutX(const std::vector<std::string> &Args,
} }
static int RunInMultipleProcesses(const std::vector<std::string> &Args, static int RunInMultipleProcesses(const std::vector<std::string> &Args,
int NumWorkers, int NumJobs) { unsigned NumWorkers, unsigned NumJobs) {
std::atomic<int> Counter(0); std::atomic<unsigned> Counter(0);
std::atomic<bool> HasErrors(false); std::atomic<bool> HasErrors(false);
std::string Cmd = CloneArgsWithoutX(Args, "jobs", "workers"); std::string Cmd = CloneArgsWithoutX(Args, "jobs", "workers");
std::vector<std::thread> V; std::vector<std::thread> V;
std::thread Pulse(PulseThread); std::thread Pulse(PulseThread);
Pulse.detach(); Pulse.detach();
for (int i = 0; i < NumWorkers; i++) for (unsigned i = 0; i < NumWorkers; i++)
V.push_back(std::thread(WorkerThread, Cmd, &Counter, NumJobs, &HasErrors)); V.push_back(std::thread(WorkerThread, Cmd, &Counter, NumJobs, &HasErrors));
for (auto &T : V) for (auto &T : V)
T.join(); T.join();
@ -379,7 +379,7 @@ int FuzzerDriver(int *argc, char ***argv, UserCallback Callback) {
if (Flags.jobs > 0 && Flags.workers == 0) { if (Flags.jobs > 0 && Flags.workers == 0) {
Flags.workers = std::min(NumberOfCpuCores() / 2, Flags.jobs); Flags.workers = std::min(NumberOfCpuCores() / 2, Flags.jobs);
if (Flags.workers > 1) if (Flags.workers > 1)
Printf("Running %d workers\n", Flags.workers); Printf("Running %u workers\n", Flags.workers);
} }
if (Flags.workers > 0 && Flags.jobs > 0) if (Flags.workers > 0 && Flags.jobs > 0)

View File

@ -54,10 +54,10 @@ FUZZER_FLAG_INT(use_value_profile, 0,
"Experimental. Use value profile to guide fuzzing.") "Experimental. Use value profile to guide fuzzing.")
FUZZER_FLAG_INT(use_cmp, 1, "Use CMP traces to guide mutations") FUZZER_FLAG_INT(use_cmp, 1, "Use CMP traces to guide mutations")
FUZZER_FLAG_INT(shrink, 0, "Experimental. Try to shrink corpus elements.") FUZZER_FLAG_INT(shrink, 0, "Experimental. Try to shrink corpus elements.")
FUZZER_FLAG_INT(jobs, 0, "Number of jobs to run. If jobs >= 1 we spawn" FUZZER_FLAG_UNSIGNED(jobs, 0, "Number of jobs to run. If jobs >= 1 we spawn"
" this number of jobs in separate worker processes" " this number of jobs in separate worker processes"
" with stdout/stderr redirected to fuzz-JOB.log.") " with stdout/stderr redirected to fuzz-JOB.log.")
FUZZER_FLAG_INT(workers, 0, FUZZER_FLAG_UNSIGNED(workers, 0,
"Number of simultaneous worker processes to run the jobs." "Number of simultaneous worker processes to run the jobs."
" If zero, \"min(jobs,NumberOfCpuCores()/2)\" is used.") " If zero, \"min(jobs,NumberOfCpuCores()/2)\" is used.")
FUZZER_FLAG_INT(reload, 1, FUZZER_FLAG_INT(reload, 1,

View File

@ -195,7 +195,7 @@ void PrintPC(const char *SymbolizedFMT, const char *FallbackFMT, uintptr_t PC) {
Printf(FallbackFMT, PC); Printf(FallbackFMT, PC);
} }
int NumberOfCpuCores() { unsigned NumberOfCpuCores() {
unsigned N = std::thread::hardware_concurrency(); unsigned N = std::thread::hardware_concurrency();
if (!N) { if (!N) {
Printf("WARNING: std::thread::hardware_concurrency not well defined for " Printf("WARNING: std::thread::hardware_concurrency not well defined for "

View File

@ -38,7 +38,7 @@ void PrintPC(const char *SymbolizedFMT, const char *FallbackFMT, uintptr_t PC);
std::string DescribePC(const char *SymbolizedFMT, uintptr_t PC); std::string DescribePC(const char *SymbolizedFMT, uintptr_t PC);
int NumberOfCpuCores(); unsigned NumberOfCpuCores();
bool ExecuteCommandAndReadOutput(const std::string &Command, std::string *Out); bool ExecuteCommandAndReadOutput(const std::string &Command, std::string *Out);