public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/96817] New: __cxa_guard_acquire unsafe against dynamically loaded pthread
@ 2020-08-27 12:32 yshuiv7 at gmail dot com
  2020-08-27 12:48 ` [Bug libstdc++/96817] " yshuiv7 at gmail dot com
                   ` (20 more replies)
  0 siblings, 21 replies; 22+ messages in thread
From: yshuiv7 at gmail dot com @ 2020-08-27 12:32 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96817

            Bug ID: 96817
           Summary: __cxa_guard_acquire unsafe against dynamically loaded
                    pthread
           Product: gcc
           Version: 10.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: yshuiv7 at gmail dot com
  Target Milestone: ---

If pthread is loaded dynamically via dlopen() after libstdc++ is loaded,
__gthread_active_p() would return wrong result, cause guard acquire to be
thread unsafe.

An example of this problem:
https://gitlab.freedesktop.org/mesa/mesa/-/issues/3199

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [Bug libstdc++/96817] __cxa_guard_acquire unsafe against dynamically loaded pthread
  2020-08-27 12:32 [Bug libstdc++/96817] New: __cxa_guard_acquire unsafe against dynamically loaded pthread yshuiv7 at gmail dot com
@ 2020-08-27 12:48 ` yshuiv7 at gmail dot com
  2020-08-27 12:48 ` redi at gcc dot gnu.org
                   ` (19 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: yshuiv7 at gmail dot com @ 2020-08-27 12:48 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96817

--- Comment #1 from Yuxuan Shui <yshuiv7 at gmail dot com> ---
Example:

This program normally deadlocks when using linked pthread:

https://godbolt.org/z/Yrza4e

But it will throw recursive_init_error when using dlopen'd pthread:

https://godbolt.org/z/afMe5d

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [Bug libstdc++/96817] __cxa_guard_acquire unsafe against dynamically loaded pthread
  2020-08-27 12:32 [Bug libstdc++/96817] New: __cxa_guard_acquire unsafe against dynamically loaded pthread yshuiv7 at gmail dot com
  2020-08-27 12:48 ` [Bug libstdc++/96817] " yshuiv7 at gmail dot com
@ 2020-08-27 12:48 ` redi at gcc dot gnu.org
  2020-08-27 12:53 ` yshuiv7 at gmail dot com
                   ` (18 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: redi at gcc dot gnu.org @ 2020-08-27 12:48 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96817

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Dynamically loading libpthread breaks everything in libstdc++, it just isn't
supported.

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [Bug libstdc++/96817] __cxa_guard_acquire unsafe against dynamically loaded pthread
  2020-08-27 12:32 [Bug libstdc++/96817] New: __cxa_guard_acquire unsafe against dynamically loaded pthread yshuiv7 at gmail dot com
  2020-08-27 12:48 ` [Bug libstdc++/96817] " yshuiv7 at gmail dot com
  2020-08-27 12:48 ` redi at gcc dot gnu.org
@ 2020-08-27 12:53 ` yshuiv7 at gmail dot com
  2020-08-27 12:58 ` rguenth at gcc dot gnu.org
                   ` (17 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: yshuiv7 at gmail dot com @ 2020-08-27 12:53 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96817

--- Comment #3 from Yuxuan Shui <yshuiv7 at gmail dot com> ---
(In reply to Jonathan Wakely from comment #2)
> Dynamically loading libpthread breaks everything in libstdc++, it just isn't
> supported.

But it has real use cases, e.g. writing a C++ OpenGL program that doesn't use
thread itself.

This is such an easy hole for the end user to fall into. How would they know
that they have to link against pthread even though their program doesn't use
any threading?

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [Bug libstdc++/96817] __cxa_guard_acquire unsafe against dynamically loaded pthread
  2020-08-27 12:32 [Bug libstdc++/96817] New: __cxa_guard_acquire unsafe against dynamically loaded pthread yshuiv7 at gmail dot com
                   ` (2 preceding siblings ...)
  2020-08-27 12:53 ` yshuiv7 at gmail dot com
@ 2020-08-27 12:58 ` rguenth at gcc dot gnu.org
  2020-08-27 13:05 ` yshuiv7 at gmail dot com
                   ` (16 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-08-27 12:58 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96817

--- Comment #4 from Richard Biener <rguenth at gcc dot gnu.org> ---
it's been always that way - but yes, people repeatedly fall into this trap

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [Bug libstdc++/96817] __cxa_guard_acquire unsafe against dynamically loaded pthread
  2020-08-27 12:32 [Bug libstdc++/96817] New: __cxa_guard_acquire unsafe against dynamically loaded pthread yshuiv7 at gmail dot com
                   ` (3 preceding siblings ...)
  2020-08-27 12:58 ` rguenth at gcc dot gnu.org
@ 2020-08-27 13:05 ` yshuiv7 at gmail dot com
  2020-08-27 13:06 ` yshuiv7 at gmail dot com
                   ` (15 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: yshuiv7 at gmail dot com @ 2020-08-27 13:05 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96817

--- Comment #5 from Yuxuan Shui <yshuiv7 at gmail dot com> ---
(In reply to Richard Biener from comment #4)
> it's been always that way - but yes, people repeatedly fall into this trap

If there anything libstdc++, or maybe the middle man (Mesa in my case) can do,
to improve the situation.

Maybe if there is an API Mesa can call to "refresh" libstdc++'s memory about
pthread?

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [Bug libstdc++/96817] __cxa_guard_acquire unsafe against dynamically loaded pthread
  2020-08-27 12:32 [Bug libstdc++/96817] New: __cxa_guard_acquire unsafe against dynamically loaded pthread yshuiv7 at gmail dot com
                   ` (4 preceding siblings ...)
  2020-08-27 13:05 ` yshuiv7 at gmail dot com
@ 2020-08-27 13:06 ` yshuiv7 at gmail dot com
  2020-08-27 13:15 ` redi at gcc dot gnu.org
                   ` (14 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: yshuiv7 at gmail dot com @ 2020-08-27 13:06 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96817

--- Comment #6 from Yuxuan Shui <yshuiv7 at gmail dot com> ---
I meant to say "Is there anything ... can do, to improve the situation?"

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [Bug libstdc++/96817] __cxa_guard_acquire unsafe against dynamically loaded pthread
  2020-08-27 12:32 [Bug libstdc++/96817] New: __cxa_guard_acquire unsafe against dynamically loaded pthread yshuiv7 at gmail dot com
                   ` (5 preceding siblings ...)
  2020-08-27 13:06 ` yshuiv7 at gmail dot com
@ 2020-08-27 13:15 ` redi at gcc dot gnu.org
  2020-08-27 13:23 ` redi at gcc dot gnu.org
                   ` (13 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: redi at gcc dot gnu.org @ 2020-08-27 13:15 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96817

--- Comment #7 from Jonathan Wakely <redi at gcc dot gnu.org> ---
It will work when Glibc defines all the pthread functions in libc.so and
libpthread.so becomes empty. There's not really any way to make it work until
then that wouldn't pessimize programs that are entirely single-threaded.

You can use LD_PRELOAD=libpthread.so to "fix" the program by ensuring the
pthread symbols are present early, before any __cxa_guard_acquire calls happen.

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [Bug libstdc++/96817] __cxa_guard_acquire unsafe against dynamically loaded pthread
  2020-08-27 12:32 [Bug libstdc++/96817] New: __cxa_guard_acquire unsafe against dynamically loaded pthread yshuiv7 at gmail dot com
                   ` (6 preceding siblings ...)
  2020-08-27 13:15 ` redi at gcc dot gnu.org
@ 2020-08-27 13:23 ` redi at gcc dot gnu.org
  2020-08-27 13:26 ` yshuiv7 at gmail dot com
                   ` (12 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: redi at gcc dot gnu.org @ 2020-08-27 13:23 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96817

--- Comment #8 from Jonathan Wakely <redi at gcc dot gnu.org> ---
I'm also going to start using Glibc's __libc_single_threaded instead of
__gthread_active_p() for deciding whether to use atomics for reference counting
in shared_ptr and the old std::string.

Once reference counting no longer depends on whether we're linked to libpthread
it won't be so costly to just link libstdc++.so to libpthread.so
unconditionally, so that std::thread Just Works without needing to use -pthread
and __gthread_active_p() is always true.

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [Bug libstdc++/96817] __cxa_guard_acquire unsafe against dynamically loaded pthread
  2020-08-27 12:32 [Bug libstdc++/96817] New: __cxa_guard_acquire unsafe against dynamically loaded pthread yshuiv7 at gmail dot com
                   ` (7 preceding siblings ...)
  2020-08-27 13:23 ` redi at gcc dot gnu.org
@ 2020-08-27 13:26 ` yshuiv7 at gmail dot com
  2020-08-27 13:34 ` redi at gcc dot gnu.org
                   ` (11 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: yshuiv7 at gmail dot com @ 2020-08-27 13:26 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96817

--- Comment #9 from Yuxuan Shui <yshuiv7 at gmail dot com> ---
(In reply to Jonathan Wakely from comment #7)
> It will work when Glibc defines all the pthread functions in libc.so and
> libpthread.so becomes empty.

Won't that mean libstdc++ will always use the thread-safe behavior? If that's
the goal, why don't we just remove the __gthread_active_p check?

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [Bug libstdc++/96817] __cxa_guard_acquire unsafe against dynamically loaded pthread
  2020-08-27 12:32 [Bug libstdc++/96817] New: __cxa_guard_acquire unsafe against dynamically loaded pthread yshuiv7 at gmail dot com
                   ` (8 preceding siblings ...)
  2020-08-27 13:26 ` yshuiv7 at gmail dot com
@ 2020-08-27 13:34 ` redi at gcc dot gnu.org
  2020-08-27 14:02 ` redi at gcc dot gnu.org
                   ` (10 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: redi at gcc dot gnu.org @ 2020-08-27 13:34 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96817

--- Comment #10 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Because it pessimizes single threaded programs. After we've made some other
changes that depend on a recent glibc, then we can avoid that pessimization.

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [Bug libstdc++/96817] __cxa_guard_acquire unsafe against dynamically loaded pthread
  2020-08-27 12:32 [Bug libstdc++/96817] New: __cxa_guard_acquire unsafe against dynamically loaded pthread yshuiv7 at gmail dot com
                   ` (9 preceding siblings ...)
  2020-08-27 13:34 ` redi at gcc dot gnu.org
@ 2020-08-27 14:02 ` redi at gcc dot gnu.org
  2020-08-27 14:04 ` redi at gcc dot gnu.org
                   ` (9 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: redi at gcc dot gnu.org @ 2020-08-27 14:02 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96817

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2020-08-27
             Status|UNCONFIRMED                 |NEW

--- Comment #11 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Something like this should work with the latest glibc:

--- a/libstdc++-v3/libsupc++/guard.cc
+++ b/libstdc++-v3/libsupc++/guard.cc
@@ -252,7 +252,22 @@ namespace __cxxabiv1
 # ifdef _GLIBCXX_USE_FUTEX
     // If __atomic_* and futex syscall are supported, don't use any global
     // mutex.
+#if __has_include(<sys/single_threaded.h>)
+    // Prefer to rely on __libc_single_threaded instead of __gthread_active_p
+    if (__libc_single_threaded)
+      {
+       int *gi = (int *) (void *) g;
+       if (*gi == 0)
+         {
+           *gi = _GLIBCXX_GUARD_PENDING_BIT;
+           return 1;
+         }
+       else
+         throw_recursive_init_exception();
+      }
+#else
     if (__gthread_active_p ())
+#endif
       {
        int *gi = (int *) (void *) g;
        const int guard_bit = _GLIBCXX_GUARD_BIT;
@@ -390,7 +405,17 @@ namespace __cxxabiv1
 #ifdef _GLIBCXX_USE_FUTEX
     // If __atomic_* and futex syscall are supported, don't use any global
     // mutex.
+#if __has_include(<sys/single_threaded.h>)
+    // Prefer to rely on __libc_single_threaded instead of __gthread_active_p
+    if (__libc_single_threaded)
+      {
+       int *gi = (int *) (void *) g;
+       *gi = _GLIBCXX_GUARD_BIT;
+       return;
+      }
+#else
     if (__gthread_active_p ())
+#endif
       {
        int *gi = (int *) (void *) g;
        const int guard_bit = _GLIBCXX_GUARD_BIT;

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [Bug libstdc++/96817] __cxa_guard_acquire unsafe against dynamically loaded pthread
  2020-08-27 12:32 [Bug libstdc++/96817] New: __cxa_guard_acquire unsafe against dynamically loaded pthread yshuiv7 at gmail dot com
                   ` (10 preceding siblings ...)
  2020-08-27 14:02 ` redi at gcc dot gnu.org
@ 2020-08-27 14:04 ` redi at gcc dot gnu.org
  2020-09-25 18:11 ` redi at gcc dot gnu.org
                   ` (8 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: redi at gcc dot gnu.org @ 2020-08-27 14:04 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96817

--- Comment #12 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Oh plus this for the aborted init case:

@@ -351,7 +366,17 @@ namespace __cxxabiv1
 #ifdef _GLIBCXX_USE_FUTEX
     // If __atomic_* and futex syscall are supported, don't use any global
     // mutex.
+#if __has_include(<sys/single_threaded.h>)
+    // Prefer to rely on __libc_single_threaded instead of __gthread_active_p
+    if (__libc_single_threaded)
+      {
+       int *gi = (int *) (void *) g;
+       *gi = 0;
+       return;
+      }
+#else
     if (__gthread_active_p ())
+#endif
       {
        int *gi = (int *) (void *) g;
        const int waiting_bit = _GLIBCXX_GUARD_WAITING_BIT;

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [Bug libstdc++/96817] __cxa_guard_acquire unsafe against dynamically loaded pthread
  2020-08-27 12:32 [Bug libstdc++/96817] New: __cxa_guard_acquire unsafe against dynamically loaded pthread yshuiv7 at gmail dot com
                   ` (11 preceding siblings ...)
  2020-08-27 14:04 ` redi at gcc dot gnu.org
@ 2020-09-25 18:11 ` redi at gcc dot gnu.org
  2020-09-25 19:55 ` redi at gcc dot gnu.org
                   ` (7 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: redi at gcc dot gnu.org @ 2020-09-25 18:11 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96817

--- Comment #13 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Yuxuan Shui from comment #1)
> Example:
> 
> This program normally deadlocks when using linked pthread:
> 
> https://godbolt.org/z/Yrza4e
> 
> But it will throw recursive_init_error when using dlopen'd pthread:
> 
> https://godbolt.org/z/afMe5d

I'm not sure what these examples are very helpful. Throwing
recursive_init_error is the correct behaviour here, it's a bug that we fail to
detect the recursion when it happens in the same thread.

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [Bug libstdc++/96817] __cxa_guard_acquire unsafe against dynamically loaded pthread
  2020-08-27 12:32 [Bug libstdc++/96817] New: __cxa_guard_acquire unsafe against dynamically loaded pthread yshuiv7 at gmail dot com
                   ` (12 preceding siblings ...)
  2020-09-25 18:11 ` redi at gcc dot gnu.org
@ 2020-09-25 19:55 ` redi at gcc dot gnu.org
  2020-09-25 20:37 ` yshuiv7 at gmail dot com
                   ` (6 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: redi at gcc dot gnu.org @ 2020-09-25 19:55 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96817

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=97211

--- Comment #14 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Jonathan Wakely from comment #13)
> I'm not sure what these examples are very helpful. Throwing
> recursive_init_error is the correct behaviour here, it's a bug that we fail
> to detect the recursion when it happens in the same thread.

I created PR 97211 for that.

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [Bug libstdc++/96817] __cxa_guard_acquire unsafe against dynamically loaded pthread
  2020-08-27 12:32 [Bug libstdc++/96817] New: __cxa_guard_acquire unsafe against dynamically loaded pthread yshuiv7 at gmail dot com
                   ` (13 preceding siblings ...)
  2020-09-25 19:55 ` redi at gcc dot gnu.org
@ 2020-09-25 20:37 ` yshuiv7 at gmail dot com
  2020-09-25 20:40 ` yshuiv7 at gmail dot com
                   ` (5 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: yshuiv7 at gmail dot com @ 2020-09-25 20:37 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96817

--- Comment #15 from Yuxuan Shui <yshuiv7 at gmail dot com> ---
(In reply to Jonathan Wakely from comment #13)
> (In reply to Yuxuan Shui from comment #1)
> > Example:
> > 
> > This program normally deadlocks when using linked pthread:
> > 
> > https://godbolt.org/z/Yrza4e
> > 
> > But it will throw recursive_init_error when using dlopen'd pthread:
> > 
> > https://godbolt.org/z/afMe5d
> 
> I'm not sure what these examples are very helpful. Throwing
> recursive_init_error is the correct behaviour here, it's a bug that we fail
> to detect the recursion when it happens in the same thread.

This is just to demonstrate the difference in the behavior. It's easy to
imagine this happen in a legitimate use case (and we indeed have one).

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [Bug libstdc++/96817] __cxa_guard_acquire unsafe against dynamically loaded pthread
  2020-08-27 12:32 [Bug libstdc++/96817] New: __cxa_guard_acquire unsafe against dynamically loaded pthread yshuiv7 at gmail dot com
                   ` (14 preceding siblings ...)
  2020-09-25 20:37 ` yshuiv7 at gmail dot com
@ 2020-09-25 20:40 ` yshuiv7 at gmail dot com
  2020-09-26 19:41 ` cvs-commit at gcc dot gnu.org
                   ` (4 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: yshuiv7 at gmail dot com @ 2020-09-25 20:40 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96817

--- Comment #16 from Yuxuan Shui <yshuiv7 at gmail dot com> ---
But yeah, that's definitely a bug in itself as well.

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [Bug libstdc++/96817] __cxa_guard_acquire unsafe against dynamically loaded pthread
  2020-08-27 12:32 [Bug libstdc++/96817] New: __cxa_guard_acquire unsafe against dynamically loaded pthread yshuiv7 at gmail dot com
                   ` (15 preceding siblings ...)
  2020-09-25 20:40 ` yshuiv7 at gmail dot com
@ 2020-09-26 19:41 ` cvs-commit at gcc dot gnu.org
  2020-10-25  9:27 ` rimvydas.jas at gmail dot com
                   ` (3 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-09-26 19:41 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96817

--- Comment #17 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:

https://gcc.gnu.org/g:e6923541fae5081b646f240d54de2a32e17a0382

commit r11-3484-ge6923541fae5081b646f240d54de2a32e17a0382
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Sat Sep 26 20:32:36 2020 +0100

    libstdc++: Use __libc_single_threaded to optimise atomics [PR 96817]

    Glibc 2.32 adds a global variable that says whether the process is
    single-threaded. We can use this to decide whether to elide atomic
    operations, as a more precise and reliable indicator than
    __gthread_active_p.

    This means that guard variables for statics and reference counting in
    shared_ptr can use less expensive, non-atomic ops even in processes that
    are linked to libpthread, as long as no threads have been created yet.
    It also means that we switch to using atomics if libpthread gets loaded
    later via dlopen (this still isn't supported in general, for other
    reasons).

    We can't use __libc_single_threaded to replace __gthread_active_p
    everywhere. If we replaced the uses of __gthread_active_p in std::mutex
    then we would elide the pthread_mutex_lock in the code below, but not
    the pthread_mutex_unlock:

      std::mutex m;
      m.lock();            // pthread_mutex_lock
      std::thread t([]{}); // __libc_single_threaded = false
      t.join();
      m.unlock();          // pthread_mutex_unlock

    We need the lock and unlock to use the same "is threading enabled"
    predicate, and similarly for init/destroy pairs for mutexes and
    condition variables, so that we don't try to release resources that were
    never acquired.

    There are other places that could use __libc_single_threaded, such as
    _Sp_locker in src/c++11/shared_ptr.cc and locale init functions, but
    they can be changed later.

    libstdc++-v3/ChangeLog:

            PR libstdc++/96817
            * include/ext/atomicity.h (__gnu_cxx::__is_single_threaded()):
            New function wrapping __libc_single_threaded if available.
            (__exchange_and_add_dispatch, __atomic_add_dispatch): Use it.
            * libsupc++/guard.cc (__cxa_guard_acquire, __cxa_guard_abort)
            (__cxa_guard_release): Likewise.
            * testsuite/18_support/96817.cc: New test.

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [Bug libstdc++/96817] __cxa_guard_acquire unsafe against dynamically loaded pthread
  2020-08-27 12:32 [Bug libstdc++/96817] New: __cxa_guard_acquire unsafe against dynamically loaded pthread yshuiv7 at gmail dot com
                   ` (16 preceding siblings ...)
  2020-09-26 19:41 ` cvs-commit at gcc dot gnu.org
@ 2020-10-25  9:27 ` rimvydas.jas at gmail dot com
  2020-10-25 22:08 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: rimvydas.jas at gmail dot com @ 2020-10-25  9:27 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96817

Rimvydas (RJ) <rimvydas.jas at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |rimvydas.jas at gmail dot com

--- Comment #18 from Rimvydas (RJ) <rimvydas.jas at gmail dot com> ---
When libstdc++ is configured with --disable-linux-futex for use in valgrind,
the g:e6923541fae5081b646f240d54de2a32e17a0382 on x86_64-linux gives

WARNING: 18_support/96817.cc execution test program timed out.
FAIL: 18_support/96817.cc execution test

Also, disabling linux-futex fails libstdc++-abi/abi_check for symbols?
std::__atomic_futex_unsigned_base::_M_futex_wait_until()
std::__atomic_futex_unsigned_base::_M_futex_notify_all()

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [Bug libstdc++/96817] __cxa_guard_acquire unsafe against dynamically loaded pthread
  2020-08-27 12:32 [Bug libstdc++/96817] New: __cxa_guard_acquire unsafe against dynamically loaded pthread yshuiv7 at gmail dot com
                   ` (17 preceding siblings ...)
  2020-10-25  9:27 ` rimvydas.jas at gmail dot com
@ 2020-10-25 22:08 ` redi at gcc dot gnu.org
  2020-11-04 15:34 ` cvs-commit at gcc dot gnu.org
  2020-11-04 15:36 ` redi at gcc dot gnu.org
  20 siblings, 0 replies; 22+ messages in thread
From: redi at gcc dot gnu.org @ 2020-10-25 22:08 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96817

--- Comment #19 from Jonathan Wakely <redi at gcc dot gnu.org> ---
I don't really care about test failures for non-standard configurations like
that.

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [Bug libstdc++/96817] __cxa_guard_acquire unsafe against dynamically loaded pthread
  2020-08-27 12:32 [Bug libstdc++/96817] New: __cxa_guard_acquire unsafe against dynamically loaded pthread yshuiv7 at gmail dot com
                   ` (18 preceding siblings ...)
  2020-10-25 22:08 ` redi at gcc dot gnu.org
@ 2020-11-04 15:34 ` cvs-commit at gcc dot gnu.org
  2020-11-04 15:36 ` redi at gcc dot gnu.org
  20 siblings, 0 replies; 22+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-11-04 15:34 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96817

--- Comment #20 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:

https://gcc.gnu.org/g:9c1125c121423a9948fa39e71ef89ba4059a2fad

commit r11-4723-g9c1125c121423a9948fa39e71ef89ba4059a2fad
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed Nov 4 15:24:47 2020 +0000

    libstdc++: Fix test failure with --disable-linux-futex

    As noted in PR 96817 this new test fails if the library is built without
    futexes. That's expected of course, but we might as well fail more
    obviously than a deadlock that eventually times out.

    libstdc++-v3/ChangeLog:

            * testsuite/18_support/96817.cc: Fail fail if the library is
            configured to not use futexes.

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [Bug libstdc++/96817] __cxa_guard_acquire unsafe against dynamically loaded pthread
  2020-08-27 12:32 [Bug libstdc++/96817] New: __cxa_guard_acquire unsafe against dynamically loaded pthread yshuiv7 at gmail dot com
                   ` (19 preceding siblings ...)
  2020-11-04 15:34 ` cvs-commit at gcc dot gnu.org
@ 2020-11-04 15:36 ` redi at gcc dot gnu.org
  20 siblings, 0 replies; 22+ messages in thread
From: redi at gcc dot gnu.org @ 2020-11-04 15:36 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96817

--- Comment #21 from Jonathan Wakely <redi at gcc dot gnu.org> ---
The abi_check failures are of course expected if you configure with an option
that alters the library ABI.

^ permalink raw reply	[flat|nested] 22+ messages in thread

end of thread, other threads:[~2020-11-04 15:36 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-27 12:32 [Bug libstdc++/96817] New: __cxa_guard_acquire unsafe against dynamically loaded pthread yshuiv7 at gmail dot com
2020-08-27 12:48 ` [Bug libstdc++/96817] " yshuiv7 at gmail dot com
2020-08-27 12:48 ` redi at gcc dot gnu.org
2020-08-27 12:53 ` yshuiv7 at gmail dot com
2020-08-27 12:58 ` rguenth at gcc dot gnu.org
2020-08-27 13:05 ` yshuiv7 at gmail dot com
2020-08-27 13:06 ` yshuiv7 at gmail dot com
2020-08-27 13:15 ` redi at gcc dot gnu.org
2020-08-27 13:23 ` redi at gcc dot gnu.org
2020-08-27 13:26 ` yshuiv7 at gmail dot com
2020-08-27 13:34 ` redi at gcc dot gnu.org
2020-08-27 14:02 ` redi at gcc dot gnu.org
2020-08-27 14:04 ` redi at gcc dot gnu.org
2020-09-25 18:11 ` redi at gcc dot gnu.org
2020-09-25 19:55 ` redi at gcc dot gnu.org
2020-09-25 20:37 ` yshuiv7 at gmail dot com
2020-09-25 20:40 ` yshuiv7 at gmail dot com
2020-09-26 19:41 ` cvs-commit at gcc dot gnu.org
2020-10-25  9:27 ` rimvydas.jas at gmail dot com
2020-10-25 22:08 ` redi at gcc dot gnu.org
2020-11-04 15:34 ` cvs-commit at gcc dot gnu.org
2020-11-04 15:36 ` redi at gcc dot gnu.org

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).