]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Avoid calling undefined __gthread_self weak symbol [PR 95989]
authorJonathan Wakely <jwakely@redhat.com>
Thu, 19 Nov 2020 21:07:06 +0000 (21:07 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Fri, 20 Nov 2020 13:29:04 +0000 (13:29 +0000)
Since glibc 2.27 the pthread_self symbol has been defined in libc rather
than libpthread. Because we only call pthread_self through a weak alias
it's possible for statically linked executables to end up without a
definition of pthread_self. This crashes when trying to call an
undefined weak symbol.

We can use the __GLIBC_PREREQ version check to detect the version of
glibc where pthread_self is no longer in libpthread, and call it
directly rather than through the weak reference.

It would be better to check for pthread_self in libc during configure
instead of hardcoding the __GLIBC_PREREQ check. That would be
complicated by the fact that prior to glibc 2.27 libc.a didn't have the
pthread_self symbol, but libc.so.6 did.  The configure checks would need
to try to link both statically and dynamically, and the result would
depend on whether the static libc.a happens to be installed during
configure (which could vary between different systems using the same
version of glibc). Doing it properly is left for a future date, as that
will be needed anyway after glibc moves all pthread symbols from
libpthread to libc. When that happens we should revisit the whole
approach of using weak symbols for pthread symbols.

For the purposes of std::this_thread::get_id() we call
pthread_self() directly when using glibc 2.27 or later. Otherwise, if
__gthread_active_p() is true then we know the libpthread symbol is
available so we call that. Otherwise, we are single-threaded and just
use ((__gthread_t)1) as the thread ID.

An undesirable consequence of this change is that code compiled prior to
the change might inline the old definition of this_thread::get_id()
which always returns (__gthread_t)1 in a program that isn't linked to
libpthread. Code compiled after the change will use pthread_self() and
so get a real TID. That could result in the main thread having different
thread::id values in different translation units. This seems acceptable,
as there are not expected to be many uses of thread::id in programs
that aren't linked to libpthread.

An earlier version of this patch also changed __gthread_self() to use
__GLIBC_PREREQ(2, 27) and only use the weak symbol for older glibc. Tha
might still make sense to do, but isn't needed by libstdc++ now.

libstdc++-v3/ChangeLog:

PR libstdc++/95989
* config/os/gnu-linux/os_defines.h (_GLIBCXX_NATIVE_THREAD_ID):
Define new macro to get reliable thread ID.
* include/std/stop_token (_Stop_state_t::_M_request_stop):
Use new macro if it's defined.
(_Stop_state_t::_M_remove_callback): Likewise.
* include/std/thread (this_thread::get_id): Likewise.
* testsuite/30_threads/jthread/95989.cc: New test.
* testsuite/30_threads/this_thread/95989.cc: New test.

(cherry picked from commit 08b4d325711d5c6f68ac29443aba3fd7aa173ac8)

libstdc++-v3/config/os/gnu-linux/os_defines.h
libstdc++-v3/include/std/stop_token
libstdc++-v3/include/std/thread
libstdc++-v3/testsuite/30_threads/jthread/95989.cc [new file with mode: 0644]
libstdc++-v3/testsuite/30_threads/this_thread/95989.cc [new file with mode: 0644]

index f821486ec8f5a7cf37ceae3edbe7d239fcf51a02..01bfa9ddd4f28419f41e6106249e3cecb8fe76a6 100644 (file)
 // version dynamically in case it has changed since libstdc++ was configured.
 #define _GLIBCXX_NO_OBSOLETE_ISINF_ISNAN_DYNAMIC __GLIBC_PREREQ(2,23)
 
+#if __GLIBC_PREREQ(2, 27)
+// Since glibc 2.27 pthread_self() is usable without linking to libpthread.
+# define _GLIBCXX_NATIVE_THREAD_ID pthread_self()
+#else
+// Before then it was in libc.so.6 but not libc.a, and always returns 0,
+// which breaks the invariant this_thread::get_id() != thread::id{}.
+// So only use it if we know the libpthread version is available.
+// Otherwise use (__gthread_t)1 as the ID of the main (and only) thread.
+# define _GLIBCXX_NATIVE_THREAD_ID \
+  (__gthread_active_p() ? __gthread_self() : (__gthread_t)1)
+#endif
+
 #endif
index 847d12f7454e6ef0d7c415d1c07c4d5783f02448..76709dd59ebdfa3337501e24db69100d8c5267e5 100644 (file)
@@ -238,7 +238,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        while (!_M_try_lock_and_stop(__old));
 
 #if _GLIBCXX_HAS_GTHREADS
+#ifdef _GLIBCXX_NATIVE_THREAD_ID
+       _M_requester = _GLIBCXX_NATIVE_THREAD_ID;
+#else
        _M_requester = __gthread_self();
+#endif
 #endif
 
        while (_M_head)
@@ -344,10 +348,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        // _M_request_stop.
 
 #if _GLIBCXX_HAS_GTHREADS
+#ifdef _GLIBCXX_NATIVE_THREAD_ID
+       auto __tid = _GLIBCXX_NATIVE_THREAD_ID;
+#else
+       auto __tid = __gthread_self();
+#endif
        // Despite appearances there is no data race on _M_requester. The only
        // write to it happens before the callback is removed from the list,
        // and removing it from the list happens before this read.
-       if (!__gthread_equal(_M_requester, __gthread_self()))
+       if (!__gthread_equal(_M_requester, __tid))
          {
            // Synchronize with completion of callback.
            __cb->_M_done.acquire();
index 96e5d1ed4e21cf9f69f78e526b49f898d6ce994f..b1ed458ba212bf34722aacae19eddeb0692d4c87 100644 (file)
@@ -364,15 +364,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     inline thread::id
     get_id() noexcept
     {
-#ifdef __GLIBC__
-      // For the GNU C library pthread_self() is usable without linking to
-      // libpthread.so but returns 0, so we cannot use it in single-threaded
-      // programs, because this_thread::get_id() != thread::id{} must be true.
-      // We know that pthread_t is an integral type in the GNU C library.
-      if (!__gthread_active_p())
-       return thread::id(1);
-#endif
+#ifdef _GLIBCXX_NATIVE_THREAD_ID
+      return thread::id(_GLIBCXX_NATIVE_THREAD_ID);
+#else
       return thread::id(__gthread_self());
+#endif
     }
 
     /// yield
diff --git a/libstdc++-v3/testsuite/30_threads/jthread/95989.cc b/libstdc++-v3/testsuite/30_threads/jthread/95989.cc
new file mode 100644 (file)
index 0000000..46444b5
--- /dev/null
@@ -0,0 +1,54 @@
+// Copyright (C) 2020 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do run { target c++2a } }
+// { dg-require-gthreads {} }
+// { dg-additional-options "-pthread" { target pthread } }
+// { dg-additional-options "-static" { target static } }
+
+#include <thread>
+
+// PR libstdc++/95989
+// Segfault compiling with static libraries and using jthread::request_stop
+
+void
+test01()
+{
+  std::jthread t{ [] () {} };
+}
+
+void
+test02()
+{
+  std::jthread t{ [] () {} };
+  t.request_stop();
+}
+
+void
+test03()
+{
+  std::jthread t{ [] {} };
+  std::stop_callback cb(t.get_stop_token(), [] () {});
+}
+
+int
+main()
+{
+  test01();
+  test01();
+}
diff --git a/libstdc++-v3/testsuite/30_threads/this_thread/95989.cc b/libstdc++-v3/testsuite/30_threads/this_thread/95989.cc
new file mode 100644 (file)
index 0000000..16535af
--- /dev/null
@@ -0,0 +1,51 @@
+// Copyright (C) 2020 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-do run { target c++11 } }
+// { dg-require-gthreads {} }
+// { dg-additional-options "-pthread" { target pthread } }
+// { dg-additional-options "-static" { target static } }
+
+#include <thread>
+#include <testsuite_hooks.h>
+
+__attribute__((noinline,noipa))
+void
+join(std::thread& t)
+{
+  if (!t.joinable())
+    return;
+
+  // Using thread::join() creates a dependency on libpthread symbols
+  // so that __gthread_active_p is true, and we use pthread_self.
+  t.join();
+}
+
+void
+test01()
+{
+  std::thread t;
+  // PR libstdc++/95989
+  auto id = std::this_thread::get_id();
+  VERIFY (t.get_id() != id );
+}
+
+int
+main()
+{
+  test01();
+}