[compiler-rt] 2cdf611 - [compiler-rt][Fuzzer] SetThreadName windows implementation new try. (#76761)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 29 17:47:09 PST 2024
Author: David CARLIER
Date: 2024-02-29T17:47:05-08:00
New Revision: 2cdf611c02392112860e661e8251efa8b1335cc2
URL: https://github.com/llvm/llvm-project/commit/2cdf611c02392112860e661e8251efa8b1335cc2
DIFF: https://github.com/llvm/llvm-project/commit/2cdf611c02392112860e661e8251efa8b1335cc2.diff
LOG: [compiler-rt][Fuzzer] SetThreadName windows implementation new try. (#76761)
SetThreadDescription symbol needs to be dynamically loaded before usage.
Then using a wide string buffer, since we re using a null terminated
string, we can use MultiByteToWideChar -1 as 4th argument to finally set
the thread name.
Previously `SetThreadDescription` was called directly causing crash.
It was reverted in dd3aa26fc8e9de37a39611f7a6a602bcb4153784
Added:
Modified:
compiler-rt/lib/fuzzer/FuzzerUtilWindows.cpp
Removed:
################################################################################
diff --git a/compiler-rt/lib/fuzzer/FuzzerUtilWindows.cpp b/compiler-rt/lib/fuzzer/FuzzerUtilWindows.cpp
index 71770166805f78..13f9a67a2f0fe3 100644
--- a/compiler-rt/lib/fuzzer/FuzzerUtilWindows.cpp
+++ b/compiler-rt/lib/fuzzer/FuzzerUtilWindows.cpp
@@ -18,8 +18,10 @@
#include <errno.h>
#include <io.h>
#include <iomanip>
+#include <libloaderapi.h>
#include <signal.h>
#include <stdio.h>
+#include <stringapiset.h>
#include <sys/types.h>
#include <windows.h>
@@ -234,8 +236,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, "SetThreadDescription");
+ if (proc) {
+ std::wstring buf;
+ auto sz = MultiByteToWideChar(CP_UTF8, 0, name.data(), -1, nullptr, 0);
+ if (sz > 0) {
+ buf.resize(sz);
+ if (MultyByteToWideChar(CP_UTF8, 0, name.data(), -1, &buf[0], sz) > 0) {
+ (void)ThreadNameProc(thread.native_handle(), buf.c_str());
+ }
+ }
+ }
}
} // namespace fuzzer
More information about the llvm-commits
mailing list