Skip to content

Commit

Permalink
[compiler-rt][Fuzzer] SetThreadName windows implementation new try. (#…
Browse files Browse the repository at this point in the history
…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 dd3aa26
  • Loading branch information
devnexen committed Mar 1, 2024
1 parent e7c3cd2 commit 2cdf611
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions compiler-rt/lib/fuzzer/FuzzerUtilWindows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2cdf611

Please sign in to comment.