Skip to content

[compiler-rt] [fuzzer] Skip trying to set the thread name on MinGW #115167

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

Merged
merged 1 commit into from
Nov 6, 2024

Conversation

mstorsjo
Copy link
Member

@mstorsjo mstorsjo commented Nov 6, 2024

Since b4130be, we check for _LIBCPP_HAS_THREAD_API_PTHREAD to decide between using SetThreadDescription or pthread_setname_np for setting the thread name.

c6f3b7b changed how libcxx defines their configuration macros - now they are always defined, but defined to 0 or 1, while they previously were either defined or undefined.

As these libcxx defines used to be defined to an empty string (rather than expanding to 1) if enabled, we can't easily produce an expression that works both with older and newer libcxx. Additionally, these defines are libcxx internal config macros that aren't a detail that isn't supported and isn't meant to be relied upon.

Simply skip trying to set thread name on MinGW as we can't easily know which kind of thread native handle we have. Setting the thread name is only a nice to have, quality of life improvement - things should work the same even without it.

Additionally, libfuzzer isn't generally usable on MinGW targets yet (Clang doesn't include it in the getSupportedSanitizers() method for the MinGW target), so this shouldn't make any difference in practice anyway.

Since b4130be, we check for
_LIBCPP_HAS_THREAD_API_PTHREAD to decide between using
SetThreadDescription or pthread_setname_np for setting the thread
name.

c6f3b7b changed how libcxx
defines their configuration macros - now they are always defined,
but defined to 0 or 1, while they previously were either defined
or undefined.

As these libcxx defines used to be defined to an empty string
(rather than expanding to 1) if enabled, we can't easily produce
an expression that works both with older and newer libcxx.
Additionally, these defines are libcxx internal config macros
that aren't a detail that isn't supported and isn't meant to be
relied upon.

Simply skip trying to set thread name on MinGW as we can't easily
know which kind of thread native handle we have. Setting the thread
name is only a nice to have, quality of life improvement - things
should work the same even without it.

Additionally, libfuzzer isn't generally usable on MinGW targets
yet (Clang doesn't include it in the getSupportedSanitizers()
method for the MinGW target), so this shouldn't make any difference
in practice anyway.
@llvmbot
Copy link
Member

llvmbot commented Nov 6, 2024

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: Martin Storsjö (mstorsjo)

Changes

Since b4130be, we check for _LIBCPP_HAS_THREAD_API_PTHREAD to decide between using SetThreadDescription or pthread_setname_np for setting the thread name.

c6f3b7b changed how libcxx defines their configuration macros - now they are always defined, but defined to 0 or 1, while they previously were either defined or undefined.

As these libcxx defines used to be defined to an empty string (rather than expanding to 1) if enabled, we can't easily produce an expression that works both with older and newer libcxx. Additionally, these defines are libcxx internal config macros that aren't a detail that isn't supported and isn't meant to be relied upon.

Simply skip trying to set thread name on MinGW as we can't easily know which kind of thread native handle we have. Setting the thread name is only a nice to have, quality of life improvement - things should work the same even without it.

Additionally, libfuzzer isn't generally usable on MinGW targets yet (Clang doesn't include it in the getSupportedSanitizers() method for the MinGW target), so this shouldn't make any difference in practice anyway.


Full diff: https://github.com/llvm/llvm-project/pull/115167.diff

1 Files Affected:

  • (modified) compiler-rt/lib/fuzzer/FuzzerUtilWindows.cpp (+5-4)
diff --git a/compiler-rt/lib/fuzzer/FuzzerUtilWindows.cpp b/compiler-rt/lib/fuzzer/FuzzerUtilWindows.cpp
index 37aecae7237ae9..2db2ea98d5c4f9 100644
--- a/compiler-rt/lib/fuzzer/FuzzerUtilWindows.cpp
+++ b/compiler-rt/lib/fuzzer/FuzzerUtilWindows.cpp
@@ -239,10 +239,11 @@ size_t PageSize() {
 }
 
 void SetThreadName(std::thread &thread, const std::string &name) {
-#if defined(_LIBCPP_HAS_THREAD_API_PTHREAD) ||                                 \
-    defined(_GLIBCXX_GCC_GTHR_POSIX_H)
-  (void)pthread_setname_np(thread.native_handle(), name.c_str());
-#else
+#ifndef __MINGW32__
+  // Not setting the thread name in MinGW environments. MinGW C++ standard
+  // libraries can either use native Windows threads or pthreads, so we
+  // don't know with certainty what kind of thread handle we're getting
+  // from thread.native_handle() here.
   typedef HRESULT(WINAPI * proc)(HANDLE, PCWSTR);
   HMODULE kbase = GetModuleHandleA("KernelBase.dll");
   proc ThreadNameProc = reinterpret_cast<proc>(

@mstorsjo
Copy link
Member Author

mstorsjo commented Nov 6, 2024

CC @Zentrik

See #112094 (comment) for discussion on how we can't easily check the thread type, and for why we shouldn't be checking libcxx internal macros.

Copy link
Member

@devnexen devnexen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess no other choice atm.

@Zentrik
Copy link
Contributor

Zentrik commented Nov 6, 2024

Thank you for fixing this.

@mstorsjo mstorsjo merged commit 87f4bc0 into llvm:main Nov 6, 2024
11 checks passed
@mstorsjo mstorsjo deleted the compiler-rt-thread-name branch November 6, 2024 22:19
@mstorsjo mstorsjo added this to the LLVM 19.X Release milestone Nov 7, 2024
@mstorsjo
Copy link
Member Author

mstorsjo commented Nov 7, 2024

/cherry-pick 87f4bc0

@llvmbot
Copy link
Member

llvmbot commented Nov 7, 2024

/pull-request #115287

@mstorsjo
Copy link
Member Author

mstorsjo commented Nov 7, 2024

FYI, based on advice from @ldionne, I did manage to implement a way to properly select automatically between the two kinds of thread handles - see mstorsjo@compiler-rt-thread-name-select.

I don't think it's worth merging this for now, though, as this code isn't really used on mingw targets right now anyway, but I'm putting it out there for future reference.

tru pushed a commit to llvmbot/llvm-project that referenced this pull request Nov 12, 2024
…lvm#115167)

Since b4130be, we check for
_LIBCPP_HAS_THREAD_API_PTHREAD to decide between using
SetThreadDescription or pthread_setname_np for setting the thread name.

c6f3b7b changed how libcxx defines
their configuration macros - now they are always defined, but defined to
0 or 1, while they previously were either defined or undefined.

As these libcxx defines used to be defined to an empty string (rather
than expanding to 1) if enabled, we can't easily produce an expression
that works both with older and newer libcxx. Additionally, these defines
are libcxx internal config macros that aren't a detail that isn't
supported and isn't meant to be relied upon.

Simply skip trying to set thread name on MinGW as we can't easily know
which kind of thread native handle we have. Setting the thread name is
only a nice to have, quality of life improvement - things should work
the same even without it.

Additionally, libfuzzer isn't generally usable on MinGW targets yet
(Clang doesn't include it in the getSupportedSanitizers() method for the
MinGW target), so this shouldn't make any difference in practice anyway.

(cherry picked from commit 87f4bc0)
nikic pushed a commit to rust-lang/llvm-project that referenced this pull request Nov 20, 2024
…lvm#115167)

Since b4130be, we check for
_LIBCPP_HAS_THREAD_API_PTHREAD to decide between using
SetThreadDescription or pthread_setname_np for setting the thread name.

c6f3b7b changed how libcxx defines
their configuration macros - now they are always defined, but defined to
0 or 1, while they previously were either defined or undefined.

As these libcxx defines used to be defined to an empty string (rather
than expanding to 1) if enabled, we can't easily produce an expression
that works both with older and newer libcxx. Additionally, these defines
are libcxx internal config macros that aren't a detail that isn't
supported and isn't meant to be relied upon.

Simply skip trying to set thread name on MinGW as we can't easily know
which kind of thread native handle we have. Setting the thread name is
only a nice to have, quality of life improvement - things should work
the same even without it.

Additionally, libfuzzer isn't generally usable on MinGW targets yet
(Clang doesn't include it in the getSupportedSanitizers() method for the
MinGW target), so this shouldn't make any difference in practice anyway.

(cherry picked from commit 87f4bc0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

4 participants