From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 9D33D384D144 for ; Wed, 14 Sep 2022 22:04:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9D33D384D144 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1663193094; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=pQuvnAYy1r9kHiMXFiNwnMdq4nGEeCEQUyarqnA+PS8=; b=f/0XsNEGN/knNLtd8ZqIcvTkFnI0jEIpaEch6BUa0DusroRJYvqxU2sgIo3zFNb3i5XGOX pbKtGcPm5KtsyAkDMYsS+/lZICdDJxSAm9aLI4VsKZHz6H9aNsAHu7ePdeiKb8ESkIw6b4 i9zHgYo61UcQJP4ou65sjXGWYSBeb7U= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-533-aCqW7CeSOqio3xqyMnZRHA-1; Wed, 14 Sep 2022 18:04:50 -0400 X-MC-Unique: aCqW7CeSOqio3xqyMnZRHA-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0BBBD811E81; Wed, 14 Sep 2022 22:04:50 +0000 (UTC) Received: from localhost (unknown [10.33.36.214]) by smtp.corp.redhat.com (Postfix) with ESMTP id C71F040C6EC2; Wed, 14 Sep 2022 22:04:49 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Add TSan annotations to std::atomic> Date: Wed, 14 Sep 2022 23:04:49 +0100 Message-Id: <20220914220449.276340-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Tested powerpc64le-linux, pushed to trunk. -- >8 -- This adds annotations to std::atomic> 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. --- 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 d4bd712fc7d..4580807f42c 100644 --- a/libstdc++-v3/include/bits/shared_ptr_atomic.h +++ b/libstdc++-v3/include/bits/shared_ptr_atomic.h @@ -32,6 +32,30 @@ #include +#if defined _GLIBCXX_TSAN && __has_include() +#include +#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 @@ -377,6 +401,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(__val)) { @@ -406,6 +431,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, @@ -416,6 +443,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #endif __current = __current & ~_S_lock_bit; } + _GLIBCXX_TSAN_MUTEX_POST_LOCK(&_M_val); return reinterpret_cast(__current); } @@ -423,7 +451,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. @@ -434,7 +464,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION if (__o != memory_order_seq_cst) __o = memory_order_release; auto __x = reinterpret_cast(__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(__x & ~_S_lock_bit); } @@ -443,20 +475,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 -- 2.37.3