From patchwork Thu Jul 21 07:17:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Huber X-Patchwork-Id: 97 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e252:0:0:0:0:0 with SMTP id bl18csp1311584wrb; Thu, 21 Jul 2022 00:17:39 -0700 (PDT) X-Google-Smtp-Source: AGRyM1teqw6tyR6JBjDGftz+sEHdG6rHIOly2Hso48AoufO8fS+KA+GtZeu/LCSjvJC1Kk5tNFAQ X-Received: by 2002:a17:907:7290:b0:72b:3799:624c with SMTP id dt16-20020a170907729000b0072b3799624cmr39219923ejc.108.1658387859313; Thu, 21 Jul 2022 00:17:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658387859; cv=none; d=google.com; s=arc-20160816; b=Bk6uhXOZfhpvhJmh5MpOICcdOy4Proq14OZUjkuYDO54Nm2yPVmqTZ5FyW3x+hybfs ugYzHvqBQjQUDNwtWPTezevjwYSOeED8/LszZyaSA2SpnHIHCfjJKd4F63yHFa53eNbI Oi73Y0sjQIYlJKLBlSK+/PToFOqtJccKjW5QRdDv859opSuUV6nD6TEGyW2b4KPlOL6I mAJW2vE+Kk3c8TRfOf1dwWBxwjo6YoplEmMywXuRBVUm80+98NqoKcFkWWCUdbIA+5Fa u/ebTRBU6x532Q7JaRshVcDG1LAXsixwDT5lUWOHpu6dsKet2ECHs6lhrvrOJqgLnlVm LJ1Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:to:from:dmarc-filter :delivered-to; bh=OR7p090qeZal+48YsbjnfoTuOrPB34IOi7cg7KeT900=; b=lW7yuxMgaMR/81L4sXZaGqmINPTDY7x4Cwg+ZCeaYeOqI4nUE+tFleehP9+F2GiRAv 8FPKUtIyXAm9SEsdmJVVjpGap7Rki/YsKKEXyORmZUwz4nQOBC2W9NsduzfkJ/R7kqSw QcM6i9aBcfWfxFVCw8BTBumCGvz39mCDXdkW+tWxKEa8Bxv2jXKguU63tBELIr/LYU35 t/UEkKfB4w2JpTr14BJTxRXSaoh/bBuGcOb6vve3rAeOXzmEcscWlq0ITz0LjcSLlN0Z 3NiGHmHORm5L6GyDHXlRA5b9cfg+dskZUaeyhG+B8/T39R0rrCWaLW0KgvmazRf3Vw8Z Cvqg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id y13-20020a056402270d00b0043bb67ba75fsi1673622edd.489.2022.07.21.00.17.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Jul 2022 00:17:39 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 48D0B3858D28 for ; Thu, 21 Jul 2022 07:17:35 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from dedi548.your-server.de (dedi548.your-server.de [85.10.215.148]) by sourceware.org (Postfix) with ESMTPS id EB42B3858D28 for ; Thu, 21 Jul 2022 07:17:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org EB42B3858D28 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 1oEQQo-000DvO-A3 for gcc-patches@gcc.gnu.org; Thu, 21 Jul 2022 09:17:10 +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 1oEQQo-000Wvn-GY for gcc-patches@gcc.gnu.org; Thu, 21 Jul 2022 09:17:10 +0200 Received: from localhost (localhost [127.0.0.1]) by mail.embedded-brains.de (Postfix) with ESMTP id 3C528480057 for ; Thu, 21 Jul 2022 09:17:10 +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 ipHlrh0rnuNj for ; Thu, 21 Jul 2022 09:17:09 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by mail.embedded-brains.de (Postfix) with ESMTP id E581E4800C5 for ; Thu, 21 Jul 2022 09:17:09 +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 qp5xytxn-ws0 for ; Thu, 21 Jul 2022 09:17:09 +0200 (CEST) Received: from zimbra.eb.localhost (unknown [192.168.96.242]) by mail.embedded-brains.de (Postfix) with ESMTPSA id D4324480057 for ; Thu, 21 Jul 2022 09:17:09 +0200 (CEST) From: Sebastian Huber To: gcc-patches@gcc.gnu.org Subject: [GCC 12] libstdc++: Fix lifetime bugs for non-TLS eh_globals [PR105880] Date: Thu, 21 Jul 2022 09:17:07 +0200 Message-Id: <20220721071707.31000-1-sebastian.huber@embedded-brains.de> X-Mailer: git-send-email 2.35.3 MIME-Version: 1.0 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: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1738945708079796430?= X-GMAIL-MSGID: =?utf-8?q?1738945708079796430?= 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 #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