public inbox for libstdc++-cvs@sourceware.org help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-9342] libstdc++: Add TSan annotations to std::atomic<shared_ptr<T>> Date: Tue, 28 Mar 2023 23:34:55 +0000 (GMT) [thread overview] Message-ID: <20230328233455.5C4F438708C6@sourceware.org> (raw) https://gcc.gnu.org/g:0cec4e9cd44e257b159a28c44d3c215fe51dbbe9 commit r12-9342-g0cec4e9cd44e257b159a28c44d3c215fe51dbbe9 Author: Jonathan Wakely <jwakely@redhat.com> Date: Wed Sep 14 19:11:22 2022 +0100 libstdc++: Add TSan annotations to std::atomic<shared_ptr<T>> This adds annotations to std::atomic<shared_ptr<T>> to enable TSan to understand the custom locking. Without this, TSan reports data races for accesses to the _M_ptr member, even though those are correctly synchronized using atomic operations on the tagged pointer. libstdc++-v3/ChangeLog: * include/bits/shared_ptr_atomic.h (_GLIBCXX_TSAN_MUTEX_DESTROY) (_GLIBCXX_TSAN_MUTEX_PRE_LOCK, _GLIBCXX_TSAN_MUTEX_POST_LOCK) (_GLIBCXX_TSAN_MUTEX_PRE_UNLOCK, _GLIBCXX_TSAN_MUTEX_POST_UNLOCK) (_GLIBCXX_TSAN_MUTEX_PRE_SIGNAL, _GLIBCXX_TSAN_MUTEX_POST_SIGNAL): Define macros for TSan annotation functions. (_Sp_atomic::_Atomic_count): Add annotations. (cherry picked from commit 0abc63a5ea4550c9e75e81b05a153dfb4b234446) Diff: --- libstdc++-v3/include/bits/shared_ptr_atomic.h | 38 +++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/libstdc++-v3/include/bits/shared_ptr_atomic.h b/libstdc++-v3/include/bits/shared_ptr_atomic.h index ff86432f0b4..385dbbea609 100644 --- a/libstdc++-v3/include/bits/shared_ptr_atomic.h +++ b/libstdc++-v3/include/bits/shared_ptr_atomic.h @@ -32,6 +32,30 @@ #include <bits/atomic_base.h> +#if defined _GLIBCXX_TSAN && __has_include(<sanitizer/tsan_interface.h>) +#include <sanitizer/tsan_interface.h> +#define _GLIBCXX_TSAN_MUTEX_DESTROY(X) \ + __tsan_mutex_destroy(X, __tsan_mutex_not_static) +#define _GLIBCXX_TSAN_MUTEX_PRE_LOCK(X) \ + __tsan_mutex_pre_lock(X, __tsan_mutex_not_static) +#define _GLIBCXX_TSAN_MUTEX_POST_LOCK(X) \ + __tsan_mutex_post_lock(X, __tsan_mutex_not_static, 0) +#define _GLIBCXX_TSAN_MUTEX_PRE_UNLOCK(X) \ + __tsan_mutex_pre_unlock(X, __tsan_mutex_not_static) +#define _GLIBCXX_TSAN_MUTEX_POST_UNLOCK(X) \ + __tsan_mutex_post_unlock(X, __tsan_mutex_not_static) +#define _GLIBCXX_TSAN_MUTEX_PRE_SIGNAL(X) __tsan_mutex_pre_signal(X, 0) +#define _GLIBCXX_TSAN_MUTEX_POST_SIGNAL(X) __tsan_mutex_post_signal(X, 0) +#else +#define _GLIBCXX_TSAN_MUTEX_DESTROY(X) +#define _GLIBCXX_TSAN_MUTEX_PRE_LOCK(X) +#define _GLIBCXX_TSAN_MUTEX_POST_LOCK(X) +#define _GLIBCXX_TSAN_MUTEX_PRE_UNLOCK(X) +#define _GLIBCXX_TSAN_MUTEX_POST_UNLOCK(X) +#define _GLIBCXX_TSAN_MUTEX_PRE_SIGNAL(X) +#define _GLIBCXX_TSAN_MUTEX_POST_SIGNAL(X) +#endif + namespace std _GLIBCXX_VISIBILITY(default) { _GLIBCXX_BEGIN_NAMESPACE_VERSION @@ -369,6 +393,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ~_Atomic_count() { auto __val = _M_val.load(memory_order_relaxed); + _GLIBCXX_TSAN_MUTEX_DESTROY(&_M_val); __glibcxx_assert(!(__val & _S_lock_bit)); if (auto __pi = reinterpret_cast<pointer>(__val)) { @@ -398,6 +423,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __current = _M_val.load(memory_order_relaxed); } + _GLIBCXX_TSAN_MUTEX_PRE_LOCK(&_M_val); + while (!_M_val.compare_exchange_strong(__current, __current | _S_lock_bit, __o, @@ -408,6 +435,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #endif __current = __current & ~_S_lock_bit; } + _GLIBCXX_TSAN_MUTEX_POST_LOCK(&_M_val); return reinterpret_cast<pointer>(__current); } @@ -415,7 +443,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION void unlock(memory_order __o) const noexcept { + _GLIBCXX_TSAN_MUTEX_PRE_UNLOCK(&_M_val); _M_val.fetch_sub(1, __o); + _GLIBCXX_TSAN_MUTEX_POST_UNLOCK(&_M_val); } // Swaps the values of *this and __c, and unlocks *this. @@ -426,7 +456,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION if (__o != memory_order_seq_cst) __o = memory_order_release; auto __x = reinterpret_cast<uintptr_t>(__c._M_pi); + _GLIBCXX_TSAN_MUTEX_PRE_UNLOCK(&_M_val); __x = _M_val.exchange(__x, __o); + _GLIBCXX_TSAN_MUTEX_POST_UNLOCK(&_M_val); __c._M_pi = reinterpret_cast<pointer>(__x & ~_S_lock_bit); } @@ -435,20 +467,26 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION void _M_wait_unlock(memory_order __o) const noexcept { + _GLIBCXX_TSAN_MUTEX_PRE_UNLOCK(&_M_val); auto __v = _M_val.fetch_sub(1, memory_order_relaxed); + _GLIBCXX_TSAN_MUTEX_POST_UNLOCK(&_M_val); _M_val.wait(__v & ~_S_lock_bit, __o); } void notify_one() noexcept { + _GLIBCXX_TSAN_MUTEX_PRE_SIGNAL(&_M_val); _M_val.notify_one(); + _GLIBCXX_TSAN_MUTEX_POST_SIGNAL(&_M_val); } void notify_all() noexcept { + _GLIBCXX_TSAN_MUTEX_PRE_SIGNAL(&_M_val); _M_val.notify_all(); + _GLIBCXX_TSAN_MUTEX_POST_SIGNAL(&_M_val); } #endif
reply other threads:[~2023-03-28 23:34 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20230328233455.5C4F438708C6@sourceware.org \ --to=redi@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ --cc=libstdc++-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).