[Fuzzer] Assign names to workers

Allow to have a name for workers in case the fuzzed code is itself using threads.

Reviewers: vitalybuka

Reviewed-By: vitalybuka

Differential Revision: https://reviews.llvm.org/D155754
This commit is contained in:
David CARLIER 2023-07-26 05:49:35 +01:00
parent b3fec1067a
commit b2a253855f
7 changed files with 31 additions and 3 deletions

View File

@ -19,6 +19,7 @@
#include <sstream>
#include <string>
#include <vector>
#include <thread>
namespace fuzzer {

View File

@ -293,9 +293,12 @@ static int RunInMultipleProcesses(const std::vector<std::string> &Args,
std::vector<std::thread> V;
std::thread Pulse(PulseThread);
Pulse.detach();
for (unsigned i = 0; i < NumWorkers; i++)
V.push_back(std::thread(WorkerThread, std::ref(Cmd), &Counter, NumJobs,
&HasErrors));
V.resize(NumWorkers);
for (unsigned i = 0; i < NumWorkers; i++) {
V[i] = std::thread(WorkerThread, std::ref(Cmd), &Counter, NumJobs,
&HasErrors);
SetThreadName(V[i], "FuzzerWorker");
}
for (auto &T : V)
T.join();
return HasErrors ? 1 : 0;

View File

@ -59,6 +59,8 @@ size_t GetPeakRSSMb();
int ExecuteCommand(const Command &Cmd);
bool ExecuteCommand(const Command &Cmd, std::string *CmdOutput);
void SetThreadName(std::thread &thread, const std::string &name);
// Fuchsia does not have popen/pclose.
FILE *OpenProcessPipe(const char *Command, const char *Mode);
int CloseProcessPipe(FILE *F);

View File

@ -165,6 +165,11 @@ void DiscardOutput(int Fd) {
fclose(Temp);
}
void SetThreadName(std::thread &thread, const std::string &name) {
// TODO ?
// Darwin allows to set the name only on the current thread it seems
}
} // namespace fuzzer
#endif // LIBFUZZER_APPLE

View File

@ -605,6 +605,10 @@ size_t PageSize() {
return PageSizeCached;
}
void SetThreadName(std::thread &thread, const std::string &name) {
// TODO ?
}
} // namespace fuzzer
#endif // LIBFUZZER_FUCHSIA

View File

@ -40,6 +40,14 @@ void DiscardOutput(int Fd) {
fclose(Temp);
}
void SetThreadName(std::thread &thread, const std::string &name) {
#if LIBFUZZER_LINUX || LIBFUZZER_FREEBSD
(void)pthread_setname_np(thread.native_handle(), name.c_str());
#elif LIBFUZZER_NETBSD
(void)pthread_set_name_np(thread.native_handle(), "%s", name.c_str());
#endif
}
} // namespace fuzzer
#endif

View File

@ -233,6 +233,11 @@ size_t PageSize() {
return PageSizeCached;
}
void SetThreadName(std::thread &thread, const std::string &name) {
// TODO ?
// to UTF-8 then SetThreadDescription ?
}
} // namespace fuzzer
#endif // LIBFUZZER_WINDOWS