Skip to content

Commit 8bf8d36

Browse files
authored
[compiler-rt][fuzzer] Reland "SetThreadName windows implementation" (#83562)
Following-up on GH-76761.
1 parent ddf79de commit 8bf8d36

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

compiler-rt/lib/fuzzer/FuzzerUtilWindows.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,15 @@
2121
#include <signal.h>
2222
#include <stdio.h>
2323
#include <sys/types.h>
24+
// clang-format off
2425
#include <windows.h>
25-
26-
// This must be included after windows.h.
26+
// These must be included after windows.h.
27+
// archicture need to be set before including
28+
// libloaderapi
29+
#include <libloaderapi.h>
30+
#include <stringapiset.h>
2731
#include <psapi.h>
32+
// clang-format on
2833

2934
namespace fuzzer {
3035

@@ -234,8 +239,20 @@ size_t PageSize() {
234239
}
235240

236241
void SetThreadName(std::thread &thread, const std::string &name) {
237-
// TODO ?
238-
// to UTF-8 then SetThreadDescription ?
242+
typedef HRESULT(WINAPI * proc)(HANDLE, PCWSTR);
243+
HMODULE kbase = GetModuleHandleA("KernelBase.dll");
244+
proc ThreadNameProc =
245+
reinterpret_cast<proc>(GetProcAddress(kbase, "SetThreadDescription"));
246+
if (proc) {
247+
std::wstring buf;
248+
auto sz = MultiByteToWideChar(CP_UTF8, 0, name.data(), -1, nullptr, 0);
249+
if (sz > 0) {
250+
buf.resize(sz);
251+
if (MultiByteToWideChar(CP_UTF8, 0, name.data(), -1, &buf[0], sz) > 0) {
252+
(void)ThreadNameProc(thread.native_handle(), buf.c_str());
253+
}
254+
}
255+
}
239256
}
240257

241258
} // namespace fuzzer

0 commit comments

Comments
 (0)