aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid CARLIER <devnexen@gmail.com>2024-03-07 23:53:27 +0000
committerGitHub <noreply@github.com>2024-03-07 23:53:27 +0000
commit8bf8d36f8e82a1e2d32f33dbe7369d9cecd57f46 (patch)
treef0f61d80252ff251efc53ad206685bffaab128cb
parentddf79deb42d901fbb732e56464efbf93bc444070 (diff)
downloadllvm-8bf8d36f8e82a1e2d32f33dbe7369d9cecd57f46.zip
llvm-8bf8d36f8e82a1e2d32f33dbe7369d9cecd57f46.tar.gz
llvm-8bf8d36f8e82a1e2d32f33dbe7369d9cecd57f46.tar.bz2
[compiler-rt][fuzzer] Reland "SetThreadName windows implementation" (#83562)
Following-up on GH-76761.
-rw-r--r--compiler-rt/lib/fuzzer/FuzzerUtilWindows.cpp25
1 files changed, 21 insertions, 4 deletions
diff --git a/compiler-rt/lib/fuzzer/FuzzerUtilWindows.cpp b/compiler-rt/lib/fuzzer/FuzzerUtilWindows.cpp
index 7177016..0dbcec8 100644
--- a/compiler-rt/lib/fuzzer/FuzzerUtilWindows.cpp
+++ b/compiler-rt/lib/fuzzer/FuzzerUtilWindows.cpp
@@ -21,10 +21,15 @@
#include <signal.h>
#include <stdio.h>
#include <sys/types.h>
+// clang-format off
#include <windows.h>
-
-// This must be included after windows.h.
+// These must be included after windows.h.
+// archicture need to be set before including
+// libloaderapi
+#include <libloaderapi.h>
+#include <stringapiset.h>
#include <psapi.h>
+// clang-format on
namespace fuzzer {
@@ -234,8 +239,20 @@ size_t PageSize() {
}
void SetThreadName(std::thread &thread, const std::string &name) {
- // TODO ?
- // to UTF-8 then SetThreadDescription ?
+ typedef HRESULT(WINAPI * proc)(HANDLE, PCWSTR);
+ HMODULE kbase = GetModuleHandleA("KernelBase.dll");
+ proc ThreadNameProc =
+ reinterpret_cast<proc>(GetProcAddress(kbase, "SetThreadDescription"));
+ if (proc) {
+ std::wstring buf;
+ auto sz = MultiByteToWideChar(CP_UTF8, 0, name.data(), -1, nullptr, 0);
+ if (sz > 0) {
+ buf.resize(sz);
+ if (MultiByteToWideChar(CP_UTF8, 0, name.data(), -1, &buf[0], sz) > 0) {
+ (void)ThreadNameProc(thread.native_handle(), buf.c_str());
+ }
+ }
+ }
}
} // namespace fuzzer