From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id DEAC2385E037; Wed, 8 Jun 2022 09:19:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DEAC2385E037 From: "redi at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/105880] eh_globals_init destructor not setting _M_init to false Date: Wed, 08 Jun 2022 09:19:05 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: 12.1.1 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: redi at gcc dot gnu.org X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Jun 2022 09:19:06 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D105880 --- Comment #8 from Jonathan Wakely --- As Andrew says, the problem here is that __cxa_get_globals is being used af= ter the global has been destroyed. Nothing done to the _M_init member in the destructor can be changed to fix that, accessing an object after its destru= ctor runs is undefined, period. What might "work" is to make the _M_init member a static data member, which outlives the singleton instance of the class. Something like this: --- a/libstdc++-v3/libsupc++/eh_globals.cc +++ b/libstdc++-v3/libsupc++/eh_globals.cc @@ -90,29 +90,34 @@ 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) - {=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; } ~__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; }; +bool __eh_globals_init::_S_init =3D 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 =3D static_cast<__cxa_eh_globals*>(__gthread_getspecific(init._M_key= )); else g =3D &eh_globals; @@ -123,7 +128,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_k= ey)); if (!g) This doesn't really seem correct though, as it just means we revert to the eh_globals single-threaded fallback buffer after _S_init becomes false. But that fallback buffer has also been destroyed at that point. So maybe we nee= d to make eh_globals immortal.=