From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id D7AD738983AA for ; Wed, 12 Oct 2022 10:55:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D7AD738983AA Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1665572135; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=6AR7/KX0QhAeihf4w4h4tJCf6QBG9nAZbi/cAjd4h88=; b=YgQLLq0rLI4qWiDnsO8Ogq4n/wfIbk5K8rEd1DZDK+gWvPrwUksRuzImFbMPdVoui2oEQ5 xvuLPWNjyE86GUHGradceC1BerjZdCdxbb+oz15P46IikObSwtVrfZb/Vm7YlOyFSuhRXY N1XtTxf8PikdhqPLygV27oPLJzxueN8= Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-116-lVM9pQP2NFiPdXdKn5O2nw-1; Wed, 12 Oct 2022 06:55:34 -0400 X-MC-Unique: lVM9pQP2NFiPdXdKn5O2nw-1 Received: by mail-qt1-f199.google.com with SMTP id a19-20020a05622a02d300b0039a3711179dso5368476qtx.12 for ; Wed, 12 Oct 2022 03:55:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=6AR7/KX0QhAeihf4w4h4tJCf6QBG9nAZbi/cAjd4h88=; b=DUR/K505EFTgV1hGppJmjRPMyL14ofU7Y9ijCUsBbhLdcl117qWM9HwQecNs9ga9jy eUbPBFNW3AoHH4Bl3rneNOe2qK2Rd3C5uzxYFGJ84IUJYDollImifjFWcjePRwMHSaxj 2RGyOYrRqrlJnQ833CPa762VxCGVw+YdzN6peJz7YojoZrxUElHmoGKs+nmQy2J9ZRt9 5QLqkJmc8DPWL9Ltkm1Xd4EX9pm8cTWeCHBRBOZV2lBqsFND+Icnep+nZkxlJsbHIyYR X0vuZDQXsxV5HeiqVC7ln7tcR/yD8gMaErda732TQOmrsaJu2RkRabpshmCwIIgJ5t7A yOnw== X-Gm-Message-State: ACrzQf3XbcN02hlXWnKfmzb+0jsLKrpOJqKVdUsTxpMMXrVHdIZkzJcz 0yxJly50aq3j1DRDJMHnRTZMDNJFR4QGyxM9TVAAghHngVr2HagmHDdysN5Lw1JqnVScQCpY6oP 9ZANEtDGzplAj2mQBgOx5qd5hntbCuvw= X-Received: by 2002:a05:622a:653:b0:39c:289c:6dec with SMTP id a19-20020a05622a065300b0039c289c6decmr6687762qtb.165.1665572134028; Wed, 12 Oct 2022 03:55:34 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4dRwgoSVh5wEankUOwKsUTvyLot0bZtrAnFtKnQpn/n/DckvriF42ILygQ9lE8kEf+geP07e0tOqN+cY00Rjg= X-Received: by 2002:a05:622a:653:b0:39c:289c:6dec with SMTP id a19-20020a05622a065300b0039c289c6decmr6687756qtb.165.1665572133797; Wed, 12 Oct 2022 03:55:33 -0700 (PDT) MIME-Version: 1.0 References: <20220721071621.111353-1-sebastian.huber@embedded-brains.de> In-Reply-To: From: Jonathan Wakely Date: Wed, 12 Oct 2022 11:55:23 +0100 Message-ID: Subject: Re: [GCC 12] libstdc++: Fix lifetime bugs for non-TLS eh_globals [PR105880] To: Jonathan Wakely Cc: Sebastian Huber , "libstdc++" X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE,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 List-Id: On Thu, 21 Jul 2022 at 08:48, Jonathan Wakely wrote: > > > > On Thu, 21 Jul 2022, 08:16 Sebastian Huber, 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 But the a33dda016e5acf9c6325ce8a72a1b0238130374e follow-up is also needed on the branch. I'll take care of that. > > > >> >> 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 >>