From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dedi548.your-server.de (dedi548.your-server.de [85.10.215.148]) by sourceware.org (Postfix) with ESMTPS id 0AD533858D28 for ; Thu, 21 Jul 2022 07:16:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 0AD533858D28 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embedded-brains.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embedded-brains.de Received: from sslproxy03.your-server.de ([88.198.220.132]) by dedi548.your-server.de with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1oEQQ4-000Dlh-QU; Thu, 21 Jul 2022 09:16:25 +0200 Received: from [82.100.198.138] (helo=mail.embedded-brains.de) by sslproxy03.your-server.de with esmtpsa (TLSv1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oEQQ5-000Q0A-0V; Thu, 21 Jul 2022 09:16:25 +0200 Received: from localhost (localhost [127.0.0.1]) by mail.embedded-brains.de (Postfix) with ESMTP id B368B480057; Thu, 21 Jul 2022 09:16:24 +0200 (CEST) Received: from mail.embedded-brains.de ([127.0.0.1]) by localhost (zimbra.eb.localhost [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id jBOSPeJkE_oZ; Thu, 21 Jul 2022 09:16:24 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by mail.embedded-brains.de (Postfix) with ESMTP id 661DE4800C5; Thu, 21 Jul 2022 09:16:24 +0200 (CEST) X-Virus-Scanned: amavisd-new at zimbra.eb.localhost Received: from mail.embedded-brains.de ([127.0.0.1]) by localhost (zimbra.eb.localhost [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id PuMTtez7AtBq; Thu, 21 Jul 2022 09:16:24 +0200 (CEST) Received: from zimbra.eb.localhost (unknown [192.168.96.242]) by mail.embedded-brains.de (Postfix) with ESMTPSA id 50A9E480057; Thu, 21 Jul 2022 09:16:24 +0200 (CEST) From: Sebastian Huber To: libstdc++@gcc.gnu.org Cc: Jonathan Wakely Subject: [GCC 12] libstdc++: Fix lifetime bugs for non-TLS eh_globals [PR105880] Date: Thu, 21 Jul 2022 09:16:21 +0200 Message-Id: <20220721071621.111353-1-sebastian.huber@embedded-brains.de> X-Mailer: git-send-email 2.35.3 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Authenticated-Sender: smtp-embedded@poldinet.de X-Virus-Scanned: Clear (ClamAV 0.103.6/26607/Wed Jul 20 10:04:34 2022) X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 21 Jul 2022 07:16:28 -0000 From: Jonathan Wakely This ensures that the single-threaded fallback buffer eh_globals is not destroyed during program termination, using the same immortalization technique used for error category objects. Also ensure that init._M_init can still be read after init has been destroyed, by making it a static data member. libstdc++-v3/ChangeLog: PR libstdc++/105880 * libsupc++/eh_globals.cc (eh_globals): Ensure constant init and prevent destruction during termination. (__eh_globals_init::_M_init): Replace with static member _S_init. (__cxxabiv1::__cxa_get_globals_fast): Update. (__cxxabiv1::__cxa_get_globals): Likewise. (cherry picked from commit 1e65f2ed99024f23c56f7b6a961898bcaa882a92) --- Would it be acceptable to back port this fix to GCC 12? libstdc++-v3/libsupc++/eh_globals.cc | 51 ++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/libstdc++-v3/libsupc++/eh_globals.cc b/libstdc++-v3/libsupc+= +/eh_globals.cc index 3a003b89edf..768425c0f40 100644 --- a/libstdc++-v3/libsupc++/eh_globals.cc +++ b/libstdc++-v3/libsupc++/eh_globals.cc @@ -64,8 +64,26 @@ __cxxabiv1::__cxa_get_globals() _GLIBCXX_NOTHROW =20 #else =20 -// Single-threaded fallback buffer. -static __cxa_eh_globals eh_globals; +#if __has_cpp_attribute(clang::require_constant_initialization) +# define __constinit [[clang::require_constant_initialization]] +#endif + +namespace +{ + struct constant_init + { + union { + unsigned char unused; + __cxa_eh_globals obj; + }; + constexpr constant_init() : obj() { } + + ~constant_init() { /* do nothing, union member is not destroyed */ } + }; + + // Single-threaded fallback buffer. + __constinit constant_init eh_globals; +} =20 #if __GTHREADS =20 @@ -90,32 +108,37 @@ eh_globals_dtor(void* ptr) struct __eh_globals_init { __gthread_key_t _M_key; - bool _M_init; + static bool _S_init; =20 - __eh_globals_init() : _M_init(false) - {=20 + __eh_globals_init() + { if (__gthread_active_p()) - _M_init =3D __gthread_key_create(&_M_key, eh_globals_dtor) =3D=3D = 0;=20 + _S_init =3D __gthread_key_create(&_M_key, eh_globals_dtor) =3D=3D = 0; } =20 ~__eh_globals_init() { - if (_M_init) + if (_S_init) __gthread_key_delete(_M_key); - _M_init =3D false; + _S_init =3D false; } + + __eh_globals_init(const __eh_globals_init&) =3D delete; + __eh_globals_init& operator=3D(const __eh_globals_init&) =3D delete; }; =20 +bool __eh_globals_init::_S_init =3D false; + static __eh_globals_init init; =20 extern "C" __cxa_eh_globals* __cxxabiv1::__cxa_get_globals_fast() _GLIBCXX_NOTHROW { __cxa_eh_globals* g; - if (init._M_init) + if (init._S_init) g =3D static_cast<__cxa_eh_globals*>(__gthread_getspecific(init._M_k= ey)); else - g =3D &eh_globals; + g =3D &eh_globals.obj; return g; } =20 @@ -123,7 +146,7 @@ extern "C" __cxa_eh_globals* __cxxabiv1::__cxa_get_globals() _GLIBCXX_NOTHROW { __cxa_eh_globals* g; - if (init._M_init) + if (init._S_init) { g =3D static_cast<__cxa_eh_globals*>(__gthread_getspecific(init._M= _key)); if (!g) @@ -140,7 +163,7 @@ __cxxabiv1::__cxa_get_globals() _GLIBCXX_NOTHROW } } else - g =3D &eh_globals; + g =3D &eh_globals.obj; return g; } =20 @@ -148,11 +171,11 @@ __cxxabiv1::__cxa_get_globals() _GLIBCXX_NOTHROW =20 extern "C" __cxa_eh_globals* __cxxabiv1::__cxa_get_globals_fast() _GLIBCXX_NOTHROW -{ return &eh_globals; } +{ return &eh_globals.obj; } =20 extern "C" __cxa_eh_globals* __cxxabiv1::__cxa_get_globals() _GLIBCXX_NOTHROW -{ return &eh_globals; } +{ return &eh_globals.obj; } =20 #endif =20 --=20 2.35.3