public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <gcc-bugzilla@gcc.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	[thread overview]
Message-ID: <bug-105880-4-bL1ke34vso@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-105880-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105880

--- Comment #8 from Jonathan Wakely <redi at gcc dot gnu.org> ---
As Andrew says, the problem here is that __cxa_get_globals is being used after
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 destructor
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)
-  { 
+  __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;
@@ -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 = static_cast<__cxa_eh_globals*>(__gthread_getspecific(init._M_key));
       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 need to
make eh_globals immortal.

  parent reply	other threads:[~2022-06-08  9:19 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-08  5:19 [Bug libstdc++/105880] New: " chrisj at rtems dot org
2022-06-08  5:50 ` [Bug libstdc++/105880] " sebastian.huber@embedded-brains.de
2022-06-08  5:56 ` pinskia at gcc dot gnu.org
2022-06-08  5:58 ` pinskia at gcc dot gnu.org
2022-06-08  6:00 ` chrisj at rtems dot org
2022-06-08  6:04 ` pinskia at gcc dot gnu.org
2022-06-08  6:12 ` chrisj at rtems dot org
2022-06-08  9:00 ` redi at gcc dot gnu.org
2022-06-08  9:19 ` redi at gcc dot gnu.org [this message]
2022-06-08  9:49 ` redi at gcc dot gnu.org
2022-06-09  0:19 ` chrisj at rtems dot org
2022-06-09  9:07 ` redi at gcc dot gnu.org
2022-06-10 14:24 ` cvs-commit at gcc dot gnu.org
2022-06-10 14:27 ` redi at gcc dot gnu.org
2022-07-21  7:55 ` cvs-commit at gcc dot gnu.org
2022-07-21  8:58 ` redi at gcc dot gnu.org
2022-11-02 14:14 ` redi at gcc dot gnu.org
2022-11-02 14:55 ` rdiezmail-gcc at yahoo dot de

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-105880-4-bL1ke34vso@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).