-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[compiler-rt][Fuzzer] SetThreadName windows implementation new try. #76761
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[compiler-rt][Fuzzer] SetThreadName windows implementation new try. #76761
Conversation
@llvm/pr-subscribers-compiler-rt-sanitizer Author: David CARLIER (devnexen) ChangesSetThreadDescription 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. Full diff: https://github.com/llvm/llvm-project/pull/76761.diff 1 Files Affected:
diff --git a/compiler-rt/lib/fuzzer/FuzzerUtilWindows.cpp b/compiler-rt/lib/fuzzer/FuzzerUtilWindows.cpp
index 71770166805f78..e47494060a0ff5 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,19 @@ 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
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
Can you please include into description # of the prev attempt? |
d99a0f9
to
9db1173
Compare
Note, PR github description, not commit, will be used, if PR is pushed web UI |
9db1173
to
b7f17aa
Compare
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.
b7f17aa
to
5e6e63d
Compare
Can you please include into description the PR# pr commit of the previous attempt? |
I added revert patch into description. Just to help to see improvement vs prev version. |
This interestingly broke our compiler-rt builds downstream:
Interestingly, the contents of winnt.h around that line are:
which suggests |
Then changing the order of the includes should solve it, windows.h before the new added entries ? |
After the include order change, it fails with:
|
oh I see I ll revert and redo a PR, couple of typos here and there. |
…w try. (llvm#76761)" This reverts commit 2cdf611.
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