Skip to content

Commit

Permalink
[Fuzzer] Assign names to workers
Browse files Browse the repository at this point in the history
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
  • Loading branch information
devnexen committed Jul 26, 2023
1 parent b3fec10 commit b2a2538
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions compiler-rt/lib/fuzzer/FuzzerCommand.h
Expand Up @@ -19,6 +19,7 @@
#include <sstream>
#include <string>
#include <vector>
#include <thread>

namespace fuzzer {

Expand Down
9 changes: 6 additions & 3 deletions compiler-rt/lib/fuzzer/FuzzerDriver.cpp
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions compiler-rt/lib/fuzzer/FuzzerUtil.h
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions compiler-rt/lib/fuzzer/FuzzerUtilDarwin.cpp
Expand Up @@ -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
4 changes: 4 additions & 0 deletions compiler-rt/lib/fuzzer/FuzzerUtilFuchsia.cpp
Expand Up @@ -605,6 +605,10 @@ size_t PageSize() {
return PageSizeCached;
}

void SetThreadName(std::thread &thread, const std::string &name) {
// TODO ?
}

} // namespace fuzzer

#endif // LIBFUZZER_FUCHSIA
8 changes: 8 additions & 0 deletions compiler-rt/lib/fuzzer/FuzzerUtilLinux.cpp
Expand Up @@ -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
5 changes: 5 additions & 0 deletions compiler-rt/lib/fuzzer/FuzzerUtilWindows.cpp
Expand Up @@ -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

0 comments on commit b2a2538

Please sign in to comment.