[clang] [Clang] Warn when `std::atomic_thread_fence` is used with `fsanitize=thread` (PR #166542)
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 6 08:12:58 PST 2025
================
@@ -0,0 +1,44 @@
+// No warnings in regular compile
+// RUN: %clang_cc1 -verify=no-tsan %s
+
+// Emits warning with `-fsanitize=thread`
+// RUN: %clang_cc1 -verify=with-tsan -fsanitize=thread %s
+
+// No warnings if `-Wno-tsan` is passed
+// RUN: %clang_cc1 -verify=no-tsan -fsanitize=thread -Wno-tsan %s
+
+// Ignoring function
+// RUN: echo "fun:main" > %t
+// RUN: %clang_cc1 -verify=no-tsan -fsanitize=thread -fsanitize-ignorelist=%t %s
+
+// Ignoring source file
+// RUN: echo "src:%s" > %t
+// RUN: %clang_cc1 -verify=no-tsan -fsanitize=thread -fsanitize-ignorelist=%t %s
+
+// no-tsan-no-diagnostics
+
+namespace std {
+ enum memory_order {
+ memory_order_relaxed,
+ memory_order_consume,
+ memory_order_acquire,
+ memory_order_release,
+ memory_order_acq_rel,
+ memory_order_seq_cst,
+ };
+ void atomic_thread_fence(memory_order) {}
+};
+
+__attribute__((no_sanitize("thread")))
+void ignore_1() {
+ std::atomic_thread_fence(std::memory_order_relaxed);
+}
+
+__attribute__((no_sanitize_thread))
+void ignore_2() {
+ std::atomic_thread_fence(std::memory_order_relaxed);
+}
+
+int main() {
+ std::atomic_thread_fence(std::memory_order_relaxed); // with-tsan-warning {{'std::atomic_thread_fence' is not supported with '-fsanitize=thread'}}
+}
----------------
AaronBallman wrote:
I think other tests that would be interesting to try out would be:
```
__attribute__((no_sanitize("thread")))
void func() {
auto lam = [](){
std::atomic_thread_fence(std::memory_order_relaxed); // This should still diagnose, right?
};
}
void other() {
auto lam = []() __attribute__((no_sanitize("thread"))) {
std::atomic_thread_fence(std::memory_order_relaxed); // This should not diagnose, right?
};
}
```
and we probably should document that there can be false positives, e.g.,
```
inline void inline_func() {
std::atomic_thread_fence(std::memory_order_relaxed); // Still diagnosed even though it's an inline function
}
__attribute__((no_sanitize("thread"))) void caller() {
inline_func();
if (0) {
std::atomic_thread_fence(std::memory_order_relaxed); // Still diagnosed even though it's unreachable
}
}
```
https://github.com/llvm/llvm-project/pull/166542
More information about the cfe-commits
mailing list