From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ed1-x535.google.com (mail-ed1-x535.google.com [IPv6:2a00:1450:4864:20::535]) by sourceware.org (Postfix) with ESMTPS id 7BE2838582B9 for ; Thu, 21 Jul 2022 07:48:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7BE2838582B9 Received: by mail-ed1-x535.google.com with SMTP id k30so1106245edk.8 for ; Thu, 21 Jul 2022 00:48:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=EOebtZfbPMaibBQa5eETN3+Kg09FmfIB8r8loLRsbxI=; b=VHQbXGV9A83FPhORt6AC9ZfM2raqwncXBfdQ/2BwvSYE2ZZCgUVocmW1KzqlZAQ3PB U709UnkbYXpfr4zjtFigZLjXw5NBVIKPJn50yVUQAubrM0fbxks0Ay6DjR352p33XUcx RE/1dvRepgxC4PesWRAlNIl411N788AVwMI0WHl2a6XpCFcypV1hWE4L9HhGam218ppk e7HYaqkTVKXZGVM/KJPppy9/HG51JgMracmfC2xng+Ym63xdhL93I6i9qKxeVwvt8g87 n0MKep+3yxx9euWDqmVYrdnaPzDlwMnAhjGWCIkPUK4UHp2ODHeaLHo1eU4mWFQjDPX2 evoQ== X-Gm-Message-State: AJIora+AX21PPyLoRRiovs7XbOpZGESi6RKrS0CYUsNsNKEPWJ6ki9Ap 3VFdk4XvndaJFAKpgp7coh2cIoA8PwHd9ldEnak= X-Google-Smtp-Source: AGRyM1tYkHZ+XzNyW9xyw32XZNdEoTL9d0GG7cYDVEpCKpQUUmLuw153ELRZRKG/iPIJ4ow0D1Mg09McL73hhoJdKdg= X-Received: by 2002:a05:6402:254c:b0:43a:9e77:3b29 with SMTP id l12-20020a056402254c00b0043a9e773b29mr54898082edb.356.1658389720159; Thu, 21 Jul 2022 00:48:40 -0700 (PDT) MIME-Version: 1.0 References: <20220721071621.111353-1-sebastian.huber@embedded-brains.de> In-Reply-To: <20220721071621.111353-1-sebastian.huber@embedded-brains.de> From: Jonathan Wakely Date: Thu, 21 Jul 2022 08:48:27 +0100 Message-ID: Subject: Re: [GCC 12] libstdc++: Fix lifetime bugs for non-TLS eh_globals [PR105880] To: Sebastian Huber Cc: "libstdc++" , Jonathan Wakely X-Spam-Status: No, score=-6.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, HTML_MESSAGE, RCVD_IN_DNSWL_NONE, 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 Content-Type: text/plain; charset="UTF-8" X-Content-Filtered-By: Mailman/MimeDel 2.1.29 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:48:43 -0000 On Thu, 21 Jul 2022, 08:16 Sebastian Huber, < sebastian.huber@embedded-brains.de> wrote: > 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? > Yes, it's ok for 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 > > #else > > -// 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; > +} > > #if __GTHREADS > > @@ -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; > > - __eh_globals_init() : _M_init(false) > - { > + __eh_globals_init() > + { > if (__gthread_active_p()) > - _M_init = __gthread_key_create(&_M_key, eh_globals_dtor) == 0; > + _S_init = __gthread_key_create(&_M_key, eh_globals_dtor) == 0; > } > > ~__eh_globals_init() > { > - if (_M_init) > + if (_S_init) > __gthread_key_delete(_M_key); > - _M_init = false; > + _S_init = false; > } > + > + __eh_globals_init(const __eh_globals_init&) = delete; > + __eh_globals_init& operator=(const __eh_globals_init&) = delete; > }; > > +bool __eh_globals_init::_S_init = false; > + > static __eh_globals_init init; > > 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 = > static_cast<__cxa_eh_globals*>(__gthread_getspecific(init._M_key)); > else > - g = &eh_globals; > + g = &eh_globals.obj; > return g; > } > > @@ -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 = > static_cast<__cxa_eh_globals*>(__gthread_getspecific(init._M_key)); > if (!g) > @@ -140,7 +163,7 @@ __cxxabiv1::__cxa_get_globals() _GLIBCXX_NOTHROW > } > } > else > - g = &eh_globals; > + g = &eh_globals.obj; > return g; > } > > @@ -148,11 +171,11 @@ __cxxabiv1::__cxa_get_globals() _GLIBCXX_NOTHROW > > extern "C" __cxa_eh_globals* > __cxxabiv1::__cxa_get_globals_fast() _GLIBCXX_NOTHROW > -{ return &eh_globals; } > +{ return &eh_globals.obj; } > > extern "C" __cxa_eh_globals* > __cxxabiv1::__cxa_get_globals() _GLIBCXX_NOTHROW > -{ return &eh_globals; } > +{ return &eh_globals.obj; } > > #endif > > -- > 2.35.3 > >