public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: Jonathan Wakely <jwakely@redhat.com>
Cc: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: Re: [committed] libstdc++: Add TSan annotations to std::atomic<shared_ptr<T>>
Date: Wed, 14 Sep 2022 23:28:34 +0100	[thread overview]
Message-ID: <CACb0b4ky_GqCniMZC=NQezofzNkekUtkp6kcF_a3KcVQPpQPJw@mail.gmail.com> (raw)
In-Reply-To: <CACb0b4n6NzrwKvpT9GzhTSvLfh6pkhj_1gDNAXYCVmaUbTpKLA@mail.gmail.com>

On Wed, 14 Sept 2022 at 23:25, Jonathan Wakely wrote:
>
> On Wed, 14 Sept 2022 at 23:05, Jonathan Wakely via Libstdc++
> <libstdc++@gcc.gnu.org> wrote:
> >
> > Tested powerpc64le-linux, pushed to trunk.
> >
> > -- >8 --
> >
> > 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.
> > ---
> >  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 <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
> > @@ -377,6 +401,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> >         ~_Atomic_count()
> >         {
> >           auto __val = _M_val.load(memory_order_relaxed);
> > +         _GLIBCXX_TSAN_MUTEX_DESTROY(&_M_val);
>
> After further thought, I'm not sure this is right. This tells tsan
> that the "mutex" at &_M_val cannot be locked or unlocked again after
> this. But what happens if the address is reused by a different
> atomic<shared_ptr<T>> which happens to be at the same memory address?
> Will tsan think that's an invalid use of the original "mutex" after
> its destruction?

We can't easily add a call to __tsan_mutex_create, which would begin
the lifetime of a new object at that address, because the default
constructor is constexpr, and the create function isn't.

>
> I will investigate.
>
> We might need to stop using the __tsan_mutex_destroy call, and if so,
> we can stop using the __tsan_mutex_not_static flag too. The pre/post
> lock/unlock/signal pairs are still valuable without the lifetime
> checking.


  reply	other threads:[~2022-09-14 22:28 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-14 22:04 Jonathan Wakely
2022-09-14 22:25 ` Jonathan Wakely
2022-09-14 22:28   ` Jonathan Wakely [this message]
2022-09-15 20:46     ` [committed] libstdc++: Tweak TSan annotations for std::atomic<shared_ptr<T>> Jonathan Wakely

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='CACb0b4ky_GqCniMZC=NQezofzNkekUtkp6kcF_a3KcVQPpQPJw@mail.gmail.com' \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@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: link
Be 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).