[compiler-rt] [compiler-rt][Fuzzer] implements SetThreadName for darwin. (PR #77014)
David CARLIER via llvm-commits
llvm-commits at lists.llvm.org
Thu Jan 4 14:19:13 PST 2024
https://github.com/devnexen created https://github.com/llvm/llvm-project/pull/77014
Unfortunately, the api makes it impossible to set a id for a thread but only for the current one.
Thus refactoring to set it from the worker callback's standpoint.
>From 999ec39ea3c079f099cb7a984db149505c79faee Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen at gmail.com>
Date: Thu, 4 Jan 2024 22:17:11 +0000
Subject: [PATCH] [compiler-rt][Fuzzer] implements SetThreadName for darwin.
Unfortunately, the api makes it impossible to set a id for a thread
but only for the current one.
Thus refactoring to set it from the worker callback's standpoint.
---
compiler-rt/lib/fuzzer/FuzzerDriver.cpp | 2 +-
compiler-rt/lib/fuzzer/FuzzerUtil.h | 2 +-
compiler-rt/lib/fuzzer/FuzzerUtilDarwin.cpp | 6 +++---
compiler-rt/lib/fuzzer/FuzzerUtilFuchsia.cpp | 2 +-
compiler-rt/lib/fuzzer/FuzzerUtilLinux.cpp | 6 +++---
5 files changed, 9 insertions(+), 9 deletions(-)
diff --git a/compiler-rt/lib/fuzzer/FuzzerDriver.cpp b/compiler-rt/lib/fuzzer/FuzzerDriver.cpp
index 8674d788932f84..c2b5b4307ac9cc 100644
--- a/compiler-rt/lib/fuzzer/FuzzerDriver.cpp
+++ b/compiler-rt/lib/fuzzer/FuzzerDriver.cpp
@@ -229,6 +229,7 @@ static void PulseThread() {
static void WorkerThread(const Command &BaseCmd, std::atomic<unsigned> *Counter,
unsigned NumJobs, std::atomic<bool> *HasErrors) {
+ SetThreadName("FuzzerWorker");
while (true) {
unsigned C = (*Counter)++;
if (C >= NumJobs) break;
@@ -297,7 +298,6 @@ static int RunInMultipleProcesses(const std::vector<std::string> &Args,
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();
diff --git a/compiler-rt/lib/fuzzer/FuzzerUtil.h b/compiler-rt/lib/fuzzer/FuzzerUtil.h
index 554567e1b8fcb3..83bc4b91003078 100644
--- a/compiler-rt/lib/fuzzer/FuzzerUtil.h
+++ b/compiler-rt/lib/fuzzer/FuzzerUtil.h
@@ -59,7 +59,7 @@ 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);
+void SetThreadName(const std::string &name);
// Fuchsia does not have popen/pclose.
FILE *OpenProcessPipe(const char *Command, const char *Mode);
diff --git a/compiler-rt/lib/fuzzer/FuzzerUtilDarwin.cpp b/compiler-rt/lib/fuzzer/FuzzerUtilDarwin.cpp
index 6c3ece30f67bdf..549702bd58f5ab 100644
--- a/compiler-rt/lib/fuzzer/FuzzerUtilDarwin.cpp
+++ b/compiler-rt/lib/fuzzer/FuzzerUtilDarwin.cpp
@@ -12,6 +12,7 @@
#include "FuzzerCommand.h"
#include "FuzzerIO.h"
#include <mutex>
+#include <pthread.h>
#include <signal.h>
#include <spawn.h>
#include <stdlib.h>
@@ -165,9 +166,8 @@ 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
+void SetThreadName(const std::string &name) {
+ pthread_setname_np(name.c_str());
}
} // namespace fuzzer
diff --git a/compiler-rt/lib/fuzzer/FuzzerUtilFuchsia.cpp b/compiler-rt/lib/fuzzer/FuzzerUtilFuchsia.cpp
index cfb81cd3f780bb..3745cced1027ec 100644
--- a/compiler-rt/lib/fuzzer/FuzzerUtilFuchsia.cpp
+++ b/compiler-rt/lib/fuzzer/FuzzerUtilFuchsia.cpp
@@ -605,7 +605,7 @@ size_t PageSize() {
return PageSizeCached;
}
-void SetThreadName(std::thread &thread, const std::string &name) {
+void SetThreadName(const std::string &name) {
// TODO ?
}
diff --git a/compiler-rt/lib/fuzzer/FuzzerUtilLinux.cpp b/compiler-rt/lib/fuzzer/FuzzerUtilLinux.cpp
index 5729448b0beb13..b81094fc5ce5b8 100644
--- a/compiler-rt/lib/fuzzer/FuzzerUtilLinux.cpp
+++ b/compiler-rt/lib/fuzzer/FuzzerUtilLinux.cpp
@@ -40,11 +40,11 @@ void DiscardOutput(int Fd) {
fclose(Temp);
}
-void SetThreadName(std::thread &thread, const std::string &name) {
+void SetThreadName(const std::string &name) {
#if LIBFUZZER_LINUX || LIBFUZZER_FREEBSD
- (void)pthread_setname_np(thread.native_handle(), name.c_str());
+ (void)pthread_setname_np(pthread_self(), name.c_str());
#elif LIBFUZZER_NETBSD
- (void)pthread_set_name_np(thread.native_handle(), "%s", name.c_str());
+ (void)pthread_set_name_np(pthread_self(), "%s", name.c_str());
#endif
}
More information about the llvm-commits
mailing list