* [PATCH] libstdc++: Ensure __gthread_self doesn't call undefined weak symbol [PR 95989] @ 2020-11-11 17:24 Jonathan Wakely 2020-11-11 18:08 ` Jakub Jelinek 0 siblings, 1 reply; 7+ messages in thread From: Jonathan Wakely @ 2020-11-11 17:24 UTC (permalink / raw) To: libstdc++, gcc-patches; +Cc: Jakub Jelinek, Florian Weimer [-- Attachment #1: Type: text/plain, Size: 2447 bytes --] 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 somewhat complicated by the fact that prior to glibc 2.27 only libc.so.6 contained the pthread_self symbol. 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 it 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. 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. libgcc/ChangeLog: PR libstdc++/95989 * gthr-posix.h (__gthread_self) [__GLIBC_PREREQ(2, 27)]: Call pthread_self directly rather than using weak alias. libstdc++-v3/ChangeLog: PR libstdc++/95989 * include/std/thread (this_thread::get_id): Add explicit cast from int to __gthread_t. Use __gthread_self for glibc 2.27 and newer. Tested powerpc64le-linux (glibc 2.17) and x86_64-linux (glibc 2.31). I can't approve the libgcc/gthr-posix.h part. OK for trunk? If the libgcc/gthr-posix.h change is not acceptable I will just change the two places libstdc++ uses __gthread_self() so that they call pthread_self() directly instead. But it seems worth fixing gthr-posix.h to avoid the problem. [-- Attachment #2: patch.txt --] [-- Type: text/plain, Size: 4281 bytes --] commit f01eaa49afab0cbd88a7e2d177d6b416ce1b78c6 Author: Jonathan Wakely <jwakely@redhat.com> Date: Thu Jul 9 10:11:57 2020 libstdc++: Ensure __gthread_self doesn't call undefined weak symbol [PR 95989] 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 somewhat complicated by the fact that prior to glibc 2.27 only libc.so.6 contained the pthread_self symbol. 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 it 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. 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. libgcc/ChangeLog: PR libstdc++/95989 * gthr-posix.h (__gthread_self) [__GLIBC_PREREQ(2, 27)]: Call pthread_self directly rather than using weak alias. libstdc++-v3/ChangeLog: PR libstdc++/95989 * include/std/thread (this_thread::get_id): Add explicit cast from int to __gthread_t. Use __gthread_self for glibc 2.27 and newer. diff --git a/libgcc/gthr-posix.h b/libgcc/gthr-posix.h index 965247602acf..5699e091c85b 100644 --- a/libgcc/gthr-posix.h +++ b/libgcc/gthr-posix.h @@ -684,7 +684,14 @@ __gthread_equal (__gthread_t __t1, __gthread_t __t2) static inline __gthread_t __gthread_self (void) { +#if __GLIBC_PREREQ(2, 27) + /* Since Glibc 2.27, pthread_self is defined in libc not libpthread. + * Call it directly so that we get a non-weak reference and won't call + * an undefined weak symbol when linked to the libc.a static lib. */ + return pthread_self (); +#else return __gthrw_(pthread_self) (); +#endif } static inline int diff --git a/libstdc++-v3/include/std/thread b/libstdc++-v3/include/std/thread index 080036e26097..ac50729c41a6 100644 --- a/libstdc++-v3/include/std/thread +++ b/libstdc++-v3/include/std/thread @@ -350,13 +350,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION inline thread::id get_id() noexcept { -#ifdef __GLIBC__ +#if defined __GLIBC__ && ! __GLIBC_PREREQ(2, 27) // 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. + // libpthread, but prior to version 2.27 the version in libc returns 0, + // which breaks the invariant this_thread::get_id() != thread::id{}. + // We know that pthread_t is a scalar type in the GNU C library, + // so just use (__gthread_t)1 as the ID of the main (and only) thread. if (!__gthread_active_p()) - return thread::id(1); + return thread::id((__gthread_t)1); #endif return thread::id(__gthread_self()); } ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] libstdc++: Ensure __gthread_self doesn't call undefined weak symbol [PR 95989] 2020-11-11 17:24 [PATCH] libstdc++: Ensure __gthread_self doesn't call undefined weak symbol [PR 95989] Jonathan Wakely @ 2020-11-11 18:08 ` Jakub Jelinek 2020-11-11 19:24 ` Jonathan Wakely 2020-11-12 17:34 ` Jonathan Wakely 0 siblings, 2 replies; 7+ messages in thread From: Jakub Jelinek @ 2020-11-11 18:08 UTC (permalink / raw) To: Jonathan Wakely; +Cc: libstdc++, gcc-patches, Florian Weimer On Wed, Nov 11, 2020 at 05:24:42PM +0000, Jonathan Wakely wrote: > --- a/libgcc/gthr-posix.h > +++ b/libgcc/gthr-posix.h > @@ -684,7 +684,14 @@ __gthread_equal (__gthread_t __t1, __gthread_t __t2) > static inline __gthread_t > __gthread_self (void) > { > +#if __GLIBC_PREREQ(2, 27) What if it is a non-glibc system where __GLIBC_PREREQ macro isn't defined? I think you'd get then error: missing binary operator before token "(" So I think you want #if defined __GLIBC__ && defined __GLIBC_PREREQ #if __GLIBC_PREREQ(2, 27) return pthread_self (); #else return __gthrw_(pthread_self) (); #else return __gthrw_(pthread_self) (); #endif or similar. Jakub ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] libstdc++: Ensure __gthread_self doesn't call undefined weak symbol [PR 95989] 2020-11-11 18:08 ` Jakub Jelinek @ 2020-11-11 19:24 ` Jonathan Wakely 2020-11-12 17:34 ` Jonathan Wakely 1 sibling, 0 replies; 7+ messages in thread From: Jonathan Wakely @ 2020-11-11 19:24 UTC (permalink / raw) To: Jakub Jelinek; +Cc: Florian Weimer, libstdc++, gcc-patches On 11/11/20 19:08 +0100, Jakub Jelinek via Libstdc++ wrote: >On Wed, Nov 11, 2020 at 05:24:42PM +0000, Jonathan Wakely wrote: >> --- a/libgcc/gthr-posix.h >> +++ b/libgcc/gthr-posix.h >> @@ -684,7 +684,14 @@ __gthread_equal (__gthread_t __t1, __gthread_t __t2) >> static inline __gthread_t >> __gthread_self (void) >> { >> +#if __GLIBC_PREREQ(2, 27) > >What if it is a non-glibc system where __GLIBC_PREREQ macro isn't defined? Ah yes, I forgot non-glibc systems exist :-) Thanks, I'll fix it tomorrow, and test on some more targets. >I think you'd get then >error: missing binary operator before token "(" >So I think you want >#if defined __GLIBC__ && defined __GLIBC_PREREQ >#if __GLIBC_PREREQ(2, 27) > return pthread_self (); >#else > return __gthrw_(pthread_self) (); >#else > return __gthrw_(pthread_self) (); >#endif >or similar. > > Jakub > ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] libstdc++: Ensure __gthread_self doesn't call undefined weak symbol [PR 95989] 2020-11-11 18:08 ` Jakub Jelinek 2020-11-11 19:24 ` Jonathan Wakely @ 2020-11-12 17:34 ` Jonathan Wakely 2020-11-19 21:42 ` Jonathan Wakely 1 sibling, 1 reply; 7+ messages in thread From: Jonathan Wakely @ 2020-11-12 17:34 UTC (permalink / raw) To: Jakub Jelinek; +Cc: Florian Weimer, libstdc++, gcc-patches [-- Attachment #1: Type: text/plain, Size: 1153 bytes --] On 11/11/20 19:08 +0100, Jakub Jelinek via Libstdc++ wrote: >On Wed, Nov 11, 2020 at 05:24:42PM +0000, Jonathan Wakely wrote: >> --- a/libgcc/gthr-posix.h >> +++ b/libgcc/gthr-posix.h >> @@ -684,7 +684,14 @@ __gthread_equal (__gthread_t __t1, __gthread_t __t2) >> static inline __gthread_t >> __gthread_self (void) >> { >> +#if __GLIBC_PREREQ(2, 27) > >What if it is a non-glibc system where __GLIBC_PREREQ macro isn't defined? >I think you'd get then >error: missing binary operator before token "(" >So I think you want >#if defined __GLIBC__ && defined __GLIBC_PREREQ >#if __GLIBC_PREREQ(2, 27) > return pthread_self (); >#else > return __gthrw_(pthread_self) (); >#else > return __gthrw_(pthread_self) (); >#endif >or similar. Here's a fixed version of the patch. I've moved the glibc-specific code in this_thread::get_id() into a new macro defined in config/os/gnu-linux/os_defines.h (where we already know we are dealing with glibc). That means we don't do the __GLIBC_PREREQ check directly in <thread>, it's hidden away in a target-specific header. Tested powerpc64le-linux (glibc 2.17 and 2.32), sparc-solaris2.11 and powerpc-aix. [-- Attachment #2: patch.txt --] [-- Type: text/x-patch, Size: 5416 bytes --] commit 822914f1f1f4710ff252764ee634aa07ac565d53 Author: Jonathan Wakely <jwakely@redhat.com> Date: Wed Nov 11 19:26:00 2020 libstdc++: Ensure __gthread_self doesn't call undefined weak symbol [PR 95989] 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 somewhat complicated by the fact that prior to glibc 2.27 only libc.so.6 contained the pthread_self symbol. 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 it 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 create a fake non-zero thread ID ((__gthread_t)1) when using glibc but not linked to libpthread. When using glibc 2.27 or later pthread_self() never returns zero so we don't need to use (__gthread_t)1 for new glibc. 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. libgcc/ChangeLog: PR libstdc++/95989 * gthr-posix.h (__gthread_self) [__GLIBC_PREREQ(2, 27)]: Call pthread_self directly rather than using weak alias. 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/thread (this_thread::get_id): Use new macro if it's defined. diff --git a/libgcc/gthr-posix.h b/libgcc/gthr-posix.h index 965247602acf..dc34645d1c52 100644 --- a/libgcc/gthr-posix.h +++ b/libgcc/gthr-posix.h @@ -684,7 +684,18 @@ __gthread_equal (__gthread_t __t1, __gthread_t __t2) static inline __gthread_t __gthread_self (void) { +#if defined __GLIBC__ && defined __GLIBC_PREREQ +# if __GLIBC_PREREQ(2, 27) + /* Since Glibc 2.27, pthread_self is defined in libc not libpthread. + * Call it directly so that we get a non-weak reference and won't call + * an undefined weak symbol when linked to the libc.a static lib. */ + return pthread_self (); +# else return __gthrw_(pthread_self) (); +# endif +#else + return __gthrw_(pthread_self) (); +#endif } static inline int diff --git a/libstdc++-v3/config/os/gnu-linux/os_defines.h b/libstdc++-v3/config/os/gnu-linux/os_defines.h index f821486ec8f5..ca61ecf60f62 100644 --- a/libstdc++-v3/config/os/gnu-linux/os_defines.h +++ b/libstdc++-v3/config/os/gnu-linux/os_defines.h @@ -49,4 +49,14 @@ // 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. +// 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 just 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 diff --git a/libstdc++-v3/include/std/thread b/libstdc++-v3/include/std/thread index 080036e26097..66d30901ac5f 100644 --- a/libstdc++-v3/include/std/thread +++ b/libstdc++-v3/include/std/thread @@ -350,15 +350,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 } #endif // _GLIBCXX_HAS_GTHREADS ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] libstdc++: Ensure __gthread_self doesn't call undefined weak symbol [PR 95989] 2020-11-12 17:34 ` Jonathan Wakely @ 2020-11-19 21:42 ` Jonathan Wakely 2020-11-20 13:50 ` Jonathan Wakely 2020-11-24 15:06 ` Jonathan Wakely 0 siblings, 2 replies; 7+ messages in thread From: Jonathan Wakely @ 2020-11-19 21:42 UTC (permalink / raw) To: Jakub Jelinek; +Cc: Florian Weimer, libstdc++, gcc-patches [-- Attachment #1: Type: text/plain, Size: 1576 bytes --] On 12/11/20 17:34 +0000, Jonathan Wakely wrote: >On 11/11/20 19:08 +0100, Jakub Jelinek via Libstdc++ wrote: >>On Wed, Nov 11, 2020 at 05:24:42PM +0000, Jonathan Wakely wrote: >>>--- a/libgcc/gthr-posix.h >>>+++ b/libgcc/gthr-posix.h >>>@@ -684,7 +684,14 @@ __gthread_equal (__gthread_t __t1, __gthread_t __t2) >>> static inline __gthread_t >>> __gthread_self (void) >>> { >>>+#if __GLIBC_PREREQ(2, 27) >> >>What if it is a non-glibc system where __GLIBC_PREREQ macro isn't defined? >>I think you'd get then >>error: missing binary operator before token "(" >>So I think you want >>#if defined __GLIBC__ && defined __GLIBC_PREREQ >>#if __GLIBC_PREREQ(2, 27) >> return pthread_self (); >>#else >> return __gthrw_(pthread_self) (); >>#else >> return __gthrw_(pthread_self) (); >>#endif >>or similar. > > >Here's a fixed version of the patch. > >I've moved the glibc-specific code in this_thread::get_id() into a new >macro defined in config/os/gnu-linux/os_defines.h (where we already >know we are dealing with glibc). That means we don't do the >__GLIBC_PREREQ check directly in <thread>, it's hidden away in a >target-specific header. > >Tested powerpc64le-linux (glibc 2.17 and 2.32), sparc-solaris2.11 and >powerpc-aix. I've committed this version which only fixes this_thread::get_id() in libstdc++, and doesn't change __gthread_self in gthr-posix.h Due to a recent change to replace other uses of __gthread_self with calls to this_thread::get_id(), fixing it there fixes all uses in libstdc++. Tested x86_64-linux, powerpc-aix, sparc-solaris2.11, committed to trunk. [-- Attachment #2: patch.txt --] [-- Type: text/x-patch, Size: 8769 bytes --] commit 08b4d325711d5c6f68ac29443aba3fd7aa173ac8 Author: Jonathan Wakely <jwakely@redhat.com> Date: Thu Nov 19 21:07:06 2020 libstdc++: Avoid calling undefined __gthread_self weak symbol [PR 95989] 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/bits/std_thread.h: (this_thread::get_id): Use new macro if it's defined. * testsuite/30_threads/jthread/95989.cc: New test. * testsuite/30_threads/this_thread/95989.cc: New test. diff --git a/libstdc++-v3/config/os/gnu-linux/os_defines.h b/libstdc++-v3/config/os/gnu-linux/os_defines.h index f821486ec8f5..01bfa9ddd4f2 100644 --- a/libstdc++-v3/config/os/gnu-linux/os_defines.h +++ b/libstdc++-v3/config/os/gnu-linux/os_defines.h @@ -49,4 +49,16 @@ // 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 diff --git a/libstdc++-v3/include/bits/std_thread.h b/libstdc++-v3/include/bits/std_thread.h index 96c8d1bb19ff..24bd5fbd44e8 100644 --- a/libstdc++-v3/include/bits/std_thread.h +++ b/libstdc++-v3/include/bits/std_thread.h @@ -294,26 +294,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION inline thread::id get_id() noexcept { -#ifdef _GLIBCXX_HAS_GTHREADS - -#ifdef __GLIBC__ - // For the GNU C library pthread_self() is usable without linking to - // libpthread, but prior to version 2.27 the version in libc returns 0, - // which breaks the invariant this_thread::get_id() != thread::id{}. - // - // We know that pthread_t is a scalar type in the GNU C library, - // so just use (__gthread_t)1 as the ID of the main (and only) thread. - // - // This uses __gthread_active_p not __gnu_cxx::__is_single_threaded - // because we don't want the thread::id of the main thread to change - // if additional threads are created later. - if (!__gthread_active_p()) - return thread::id((__gthread_t)1); -#endif - - return thread::id(__gthread_self()); -#else +#ifndef _GLIBCXX_HAS_GTHREADS return thread::id(1); +#elif defined _GLIBCXX_NATIVE_THREAD_ID + return thread::id(_GLIBCXX_NATIVE_THREAD_ID); +#else + return thread::id(__gthread_self()); #endif } diff --git a/libstdc++-v3/testsuite/30_threads/jthread/95989.cc b/libstdc++-v3/testsuite/30_threads/jthread/95989.cc new file mode 100644 index 000000000000..46444b5ccabc --- /dev/null +++ b/libstdc++-v3/testsuite/30_threads/jthread/95989.cc @@ -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 index 000000000000..16535af8b743 --- /dev/null +++ b/libstdc++-v3/testsuite/30_threads/this_thread/95989.cc @@ -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(); +} ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] libstdc++: Ensure __gthread_self doesn't call undefined weak symbol [PR 95989] 2020-11-19 21:42 ` Jonathan Wakely @ 2020-11-20 13:50 ` Jonathan Wakely 2020-11-24 15:06 ` Jonathan Wakely 1 sibling, 0 replies; 7+ messages in thread From: Jonathan Wakely @ 2020-11-20 13:50 UTC (permalink / raw) To: Jakub Jelinek; +Cc: Florian Weimer, libstdc++, gcc-patches [-- Attachment #1: Type: text/plain, Size: 1736 bytes --] On 19/11/20 21:42 +0000, Jonathan Wakely wrote: >On 12/11/20 17:34 +0000, Jonathan Wakely wrote: >>On 11/11/20 19:08 +0100, Jakub Jelinek via Libstdc++ wrote: >>>On Wed, Nov 11, 2020 at 05:24:42PM +0000, Jonathan Wakely wrote: >>>>--- a/libgcc/gthr-posix.h >>>>+++ b/libgcc/gthr-posix.h >>>>@@ -684,7 +684,14 @@ __gthread_equal (__gthread_t __t1, __gthread_t __t2) >>>>static inline __gthread_t >>>>__gthread_self (void) >>>>{ >>>>+#if __GLIBC_PREREQ(2, 27) >>> >>>What if it is a non-glibc system where __GLIBC_PREREQ macro isn't defined? >>>I think you'd get then >>>error: missing binary operator before token "(" >>>So I think you want >>>#if defined __GLIBC__ && defined __GLIBC_PREREQ >>>#if __GLIBC_PREREQ(2, 27) >>>return pthread_self (); >>>#else >>>return __gthrw_(pthread_self) (); >>>#else >>>return __gthrw_(pthread_self) (); >>>#endif >>>or similar. >> >> >>Here's a fixed version of the patch. >> >>I've moved the glibc-specific code in this_thread::get_id() into a new >>macro defined in config/os/gnu-linux/os_defines.h (where we already >>know we are dealing with glibc). That means we don't do the >>__GLIBC_PREREQ check directly in <thread>, it's hidden away in a >>target-specific header. >> >>Tested powerpc64le-linux (glibc 2.17 and 2.32), sparc-solaris2.11 and >>powerpc-aix. > >I've committed this version which only fixes this_thread::get_id() in >libstdc++, and doesn't change __gthread_self in gthr-posix.h > >Due to a recent change to replace other uses of __gthread_self with >calls to this_thread::get_id(), fixing it there fixes all uses in >libstdc++. Here's the backport for gcc-10, where we still use __gthread_self in two places in <stop_token>. Tested x86_64-linux, committed to gcc-10 branch. [-- Attachment #2: patch.txt --] [-- Type: text/x-patch, Size: 9620 bytes --] commit b11cbbbb74b0e357652a1f1f0d937455310a6389 Author: Jonathan Wakely <jwakely@redhat.com> Date: Thu Nov 19 21:07:06 2020 libstdc++: Avoid calling undefined __gthread_self weak symbol [PR 95989] 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) diff --git a/libstdc++-v3/config/os/gnu-linux/os_defines.h b/libstdc++-v3/config/os/gnu-linux/os_defines.h index f821486ec8f5..01bfa9ddd4f2 100644 --- a/libstdc++-v3/config/os/gnu-linux/os_defines.h +++ b/libstdc++-v3/config/os/gnu-linux/os_defines.h @@ -49,4 +49,16 @@ // 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 diff --git a/libstdc++-v3/include/std/stop_token b/libstdc++-v3/include/std/stop_token index 847d12f7454e..76709dd59ebd 100644 --- a/libstdc++-v3/include/std/stop_token +++ b/libstdc++-v3/include/std/stop_token @@ -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(); diff --git a/libstdc++-v3/include/std/thread b/libstdc++-v3/include/std/thread index 96e5d1ed4e21..b1ed458ba212 100644 --- a/libstdc++-v3/include/std/thread +++ b/libstdc++-v3/include/std/thread @@ -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 index 000000000000..46444b5ccabc --- /dev/null +++ b/libstdc++-v3/testsuite/30_threads/jthread/95989.cc @@ -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 index 000000000000..16535af8b743 --- /dev/null +++ b/libstdc++-v3/testsuite/30_threads/this_thread/95989.cc @@ -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(); +} ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] libstdc++: Ensure __gthread_self doesn't call undefined weak symbol [PR 95989] 2020-11-19 21:42 ` Jonathan Wakely 2020-11-20 13:50 ` Jonathan Wakely @ 2020-11-24 15:06 ` Jonathan Wakely 1 sibling, 0 replies; 7+ messages in thread From: Jonathan Wakely @ 2020-11-24 15:06 UTC (permalink / raw) To: libstdc++, gcc-patches [-- Attachment #1: Type: text/plain, Size: 3539 bytes --] On 19/11/20 21:42 +0000, Jonathan Wakely wrote: >On 12/11/20 17:34 +0000, Jonathan Wakely wrote: >>On 11/11/20 19:08 +0100, Jakub Jelinek via Libstdc++ wrote: >>>On Wed, Nov 11, 2020 at 05:24:42PM +0000, Jonathan Wakely wrote: >>>>--- a/libgcc/gthr-posix.h >>>>+++ b/libgcc/gthr-posix.h >>>>@@ -684,7 +684,14 @@ __gthread_equal (__gthread_t __t1, __gthread_t __t2) >>>>static inline __gthread_t >>>>__gthread_self (void) >>>>{ >>>>+#if __GLIBC_PREREQ(2, 27) >>> >>>What if it is a non-glibc system where __GLIBC_PREREQ macro isn't defined? >>>I think you'd get then >>>error: missing binary operator before token "(" >>>So I think you want >>>#if defined __GLIBC__ && defined __GLIBC_PREREQ >>>#if __GLIBC_PREREQ(2, 27) >>>return pthread_self (); >>>#else >>>return __gthrw_(pthread_self) (); >>>#else >>>return __gthrw_(pthread_self) (); >>>#endif >>>or similar. >> >> >>Here's a fixed version of the patch. >> >>I've moved the glibc-specific code in this_thread::get_id() into a new >>macro defined in config/os/gnu-linux/os_defines.h (where we already >>know we are dealing with glibc). That means we don't do the >>__GLIBC_PREREQ check directly in <thread>, it's hidden away in a >>target-specific header. >> >>Tested powerpc64le-linux (glibc 2.17 and 2.32), sparc-solaris2.11 and >>powerpc-aix. > >I've committed this version which only fixes this_thread::get_id() in >libstdc++, and doesn't change __gthread_self in gthr-posix.h > >Due to a recent change to replace other uses of __gthread_self with >calls to this_thread::get_id(), fixing it there fixes all uses in >libstdc++. > >Tested x86_64-linux, powerpc-aix, sparc-solaris2.11, committed to >trunk. >diff --git a/libstdc++-v3/testsuite/30_threads/jthread/95989.cc b/libstdc++-v3/testsuite/30_threads/jthread/95989.cc >new file mode 100644 >index 000000000000..46444b5ccabc >--- /dev/null >+++ b/libstdc++-v3/testsuite/30_threads/jthread/95989.cc >@@ -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(); >+} This test runs test01 twice, which isn't what I meant to do. Fixed by this patch (but the test is still failing, see PR 97944). Committed to trunk. [-- Attachment #2: patch.txt --] [-- Type: text/x-patch, Size: 687 bytes --] commit 7e0078f8643f9204777152ed0f915b52072a05c8 Author: Jonathan Wakely <jwakely@redhat.com> Date: Tue Nov 24 13:11:13 2020 libstdc++: Run all tests in file libstdc++-v3/ChangeLog: * testsuite/30_threads/jthread/95989.cc: Run all three test functions, not just the first one twice. diff --git a/libstdc++-v3/testsuite/30_threads/jthread/95989.cc b/libstdc++-v3/testsuite/30_threads/jthread/95989.cc index 46444b5ccabc..c7a9430eee90 100644 --- a/libstdc++-v3/testsuite/30_threads/jthread/95989.cc +++ b/libstdc++-v3/testsuite/30_threads/jthread/95989.cc @@ -50,5 +50,6 @@ int main() { test01(); - test01(); + test02(); + test03(); } ^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2020-11-24 15:06 UTC | newest] Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2020-11-11 17:24 [PATCH] libstdc++: Ensure __gthread_self doesn't call undefined weak symbol [PR 95989] Jonathan Wakely 2020-11-11 18:08 ` Jakub Jelinek 2020-11-11 19:24 ` Jonathan Wakely 2020-11-12 17:34 ` Jonathan Wakely 2020-11-19 21:42 ` Jonathan Wakely 2020-11-20 13:50 ` Jonathan Wakely 2020-11-24 15:06 ` Jonathan Wakely
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).