From patchwork Fri Jun 10 14:24:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1641999 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=r/xecsoy; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LKNYz2TSgz9s09 for ; Sat, 11 Jun 2022 00:26:30 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5DE3D381F705 for ; Fri, 10 Jun 2022 14:26:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5DE3D381F705 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1654871187; bh=TMr9uXDCLt/J5losWBEBvQkMdw/dIMHXojnNqtSykqo=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=r/xecsoyOW+fqSzJMU45Bg9aaWLYbmxbg+B6Rorqj68ZxBBbNGJRyKrTL8bfYU3FL UwxEIA9RpSz+D3rjg05l+vlSBjCpZAICQfZT3z2epAPJVrm6GSS4B1sqIV+D6ZTk7c BTQqxMVRYvM+0M56EwL3/Y5cWm4ZuYKClny91Sk8= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 82D75388EC3A for ; Fri, 10 Jun 2022 14:25:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 82D75388EC3A Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-183-kUyCT6GMOrKZ_j2vvQlE0Q-1; Fri, 10 Jun 2022 10:25:05 -0400 X-MC-Unique: kUyCT6GMOrKZ_j2vvQlE0Q-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 7117A10AF7ED; Fri, 10 Jun 2022 14:24:59 +0000 (UTC) Received: from localhost (unknown [10.33.36.159]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3A359C44AFB; Fri, 10 Jun 2022 14:24:59 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix lifetime bugs for non-TLS eh_globals [PR105880] Date: Fri, 10 Jun 2022 15:24:58 +0100 Message-Id: <20220610142458.665820-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.5 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, T_SCC_BODY_TEXT_LINE 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: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Tested powerpc64le-linux, pushed to trunk. -- >8 -- 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. --- 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