From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 8DEF5388A425; Fri, 20 Nov 2020 13:49:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8DEF5388A425 From: "cvs-commit at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/95989] Segmentation fault compiling with static libraries and using jthread::request_stop Date: Fri, 20 Nov 2020 13:49:07 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: 10.0 X-Bugzilla-Keywords: patch X-Bugzilla-Severity: normal X-Bugzilla-Who: cvs-commit at gcc dot gnu.org X-Bugzilla-Status: ASSIGNED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: redi at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 20 Nov 2020 13:49:07 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D95989 --- Comment #17 from CVS Commits --- The releases/gcc-10 branch has been updated by Jonathan Wakely : https://gcc.gnu.org/g:b11cbbbb74b0e357652a1f1f0d937455310a6389 commit r10-9055-gb11cbbbb74b0e357652a1f1f0d937455310a6389 Author: Jonathan Wakely Date: Thu Nov 19 21:07:06 2020 +0000 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)=