[Bug libstdc++/105880] eh_globals_init destructor not setting _M_init to false
redi at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Wed Jun 8 09:19:05 GMT 2022
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.
More information about the Gcc-bugs
mailing list