public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/65343] New: unexpected exception thrown during destruction of static object in debug mode
@ 2015-03-07 13:29 frankhb1989 at gmail dot com
  2015-03-10 12:25 ` [Bug libstdc++/65343] " frankhb1989 at gmail dot com
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: frankhb1989 at gmail dot com @ 2015-03-07 13:29 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 65343
           Summary: unexpected exception thrown during destruction of
                    static object in debug mode
           Product: gcc
           Version: 4.9.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: frankhb1989 at gmail dot com

Recently I found one of my program unexpected aborted by unhandled exception.
The program is compliled with '-std=c++11 -D_GLIBCXX_DEBUG
-D_GLIBCXX_DEBUG_PEDANTIC'. The corresponding release build (i.e. "no
-D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC") does not behave like this.

GDB tells me there is something wrong happened during destruction of some
global container objects:

#2  0x00454b09 in __cxxabiv1::__terminate(void (*)()) ()
#3  0x00635d99 in __cxa_call_terminate ()
#4  0x006366df in __gxx_personality_v0 ()
#5  0x6e952cd2 in ?? () from F:\msys\mingw\bin\libgcc_s_dw2-1.dll
#6  0x6e95332b in ?? () from F:\msys\mingw\bin\libgcc_s_dw2-1.dll
#7  0x00636435 in __cxa_throw ()
#8  0x005021ea in __gnu_cxx::__throw_concurrence_lock_error ()
    at F:/msys/mingw/include/c++/4.9.1/ext/concurrence.h:102
#9  0x00470a15 in __gnu_debug::_Safe_sequence_base::_M_detach_all() ()

I searched the source of libstdc++, then I found, in "C++11/debug.cc", there
were mutexes used by the debug mode containers, which was some static
__gnu_cxx::__mutex objects. This was probably the direct reason: the order
destruction of static objects among translation units are unspecified, so the
mutexes could be destroyed before the sequences contained by some other static
objects (certainly in different translation units). Then, during the
desturction of the sequence, "_M_detach_all" was called. In that function,
"__gnu_cxx::__scoped_lock sentry(_M_get_mutex());" tried using the (already
destroyed) mutex to lock the resources, which failed and threw.

Since the documentation says, "all functional and exception-handling guarantees
made by the normal library also hold for the debug mode library, with one
exception: performance guarantees made by the normal library may not hold in
the debug mode library", and ISO C++ effectively forbids destructors in
standard library to throw, this should be a bug.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug libstdc++/65343] unexpected exception thrown during destruction of static object in debug mode
  2015-03-07 13:29 [Bug libstdc++/65343] New: unexpected exception thrown during destruction of static object in debug mode frankhb1989 at gmail dot com
@ 2015-03-10 12:25 ` frankhb1989 at gmail dot com
  2022-01-25 14:52 ` poulhies at adacore dot com
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: frankhb1989 at gmail dot com @ 2015-03-10 12:25 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from frankhb1989 at gmail dot com ---
(In reply to Jonathan Wakely from comment #1)
> Maybe we want to placement-new the mutexes into a buffer so they are never
> destroyed, although on mingw that will show up as leaked resources at
> program shutdown (and this is only really a problem on mingw as we don't
> need to run any destructor on the mutexes for most targets).

This could be an acceptable workaround for MinGW, but I am not sure if there
would be some unexpected side effects. For other targets in future, this can
still be a problem.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug libstdc++/65343] unexpected exception thrown during destruction of static object in debug mode
  2015-03-07 13:29 [Bug libstdc++/65343] New: unexpected exception thrown during destruction of static object in debug mode frankhb1989 at gmail dot com
  2015-03-10 12:25 ` [Bug libstdc++/65343] " frankhb1989 at gmail dot com
@ 2022-01-25 14:52 ` poulhies at adacore dot com
  2022-01-25 17:30 ` ebotcazou at gcc dot gnu.org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: poulhies at adacore dot com @ 2022-01-25 14:52 UTC (permalink / raw)
  To: gcc-bugs

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

Marc Poulhiès <poulhies at adacore dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |poulhies at adacore dot com

--- Comment #3 from Marc Poulhiès <poulhies at adacore dot com> ---
Hi !

It looks like we are seeing this issue using vxworks7 on gcc-11.

The test "/testsuite/21_strings/basic_string/requirements/citerators.cc" has
been updated since gcc-10 to use __gnu_debug::string:

8<----8<-----8<-----8<-----8<-----8<------
commit 50bb46e4d2543f2a78f97feddcde27e42639dae1
Author: François Dumont <fdumont@gcc.gnu.org>
Date:   Fri Mar 5 18:50:22 2021 +0100

    libstdc++: Fix and complete __gnu_debug::basic_string implementation
8<----8<-----8<-----8<-----8<-----8<------

Currently, the mutexes are created when  __gnu_internal::get_mutex() is first
called, during the construction of " __gnu_test::citerator<__gnu_debug::string>
dtest1;" in main().

#0  __gnu_cxx::__mutex::__mutex (this=0x801a80c0
<__gnu_internal::get_mutex(unsigned char)::m>) at
...libstdc++-v3/include/ext/concurrence.h:132
#1  0x0000000080043188 in __gnu_internal::M::M (this=0x801a80c0
<__gnu_internal::get_mutex(unsigned char)::m>) at
.../libstdc++-v3/src/c++11/shared_ptr.cc:38
#2  0x0000000080043268 in __gnu_internal::get_mutex (i=5 '\005') at
.../libstdc++-v3/src/c++11/shared_ptr.cc:39
...
#14 0x00000000800005b8 in main () at
.../libstdc++-v3/testsuite/21_strings/basic_string/requirements/citerators.cc:29

On vxworks, __GTHREAD_MUTEX_INIT is not defined, so a destructor is defined for
the __mutex object (include/ext/concurrence.h:136) and registered with
__cxa_atexit.

#0  __cxa_atexit (fn=0x800431ac <__tcf_0(void*)>, data=0x0,
dso_handle=0x801a5d40) at cxa_atexit.c:215
#1  0x0000000080043290 in __gnu_internal::get_mutex (i=5 '\005') at
.../libstdc++-v3/src/c++11/shared_ptr.cc:39
#2  0x0000000080010710 in (anonymous namespace)::get_safe_base_mutex
(address=0x801a7f28 <__gnu_test::citerator<__gnu_debug::basic_string<char,
std::char_traits<char>, std::allocator<char> > >::_S_container>)
    at .../libstdc++-v3/src/c++11/debug.cc:66

Then, when main() returns, these mutexes' destructors are called, which on
vxworks call semDestroy() that invalidate the mutexes.

#0  __gnu_internal::M::~M (this=0x801a8480 <__gnu_internal::get_mutex(unsigned
char)::m+960>, __in_chrg=<optimized out>) at
.../libstdc++-v3/src/c++11/shared_ptr.cc:38
#1  0x00000000800431e0 in __tcf_0 () at
.../libstdc++-v3/src/c++11/shared_ptr.cc:39
#2  0x00000000800f4ae4 in __cxa_finalize (dso_handle=0x0) at cxa_atexit.c:222
#3  0x00000000800f4cac in __do_atexit_work () at cxa_atexit.c:229
#4  0x00000000800ff274 in exit (status=0) at vxexit.c:49
#5  0x00000000800001d8 in _start ()

A later destructor for the _S_container object then tries to lock the destroyed
and now invalid mutexes, leading to an error.

#0  __gnu_cxx::__mutex::lock (this=0x801a8200
<__gnu_internal::get_mutex(unsigned char)::m+320>)
    at .../libstdc++-v3/include/ext/concurrence.h:149
#1  0x0000000080000b1c in __gnu_cxx::__scoped_lock::__scoped_lock
(this=0x600e68, __name=...) at ...p/libstdc++-v3/include/ext/concurrence.h:241
#2  0x00000000800109c4 in __gnu_debug::_Safe_sequence_base::_M_detach_all
(this=0x801a7f28 <__gnu_test::citerator<__gnu_debug::basic_string<char,
std::char_traits<char>, std::allocator<char> > >::_S_container>)
    at .../libstdc++-v3/src/c++11/debug.cc:255
...
#7  0x00000000800f4ae4 in __cxa_finalize (dso_handle=0x0) at cxa_atexit.c:222
#8  0x00000000800f4cac in __do_atexit_work () at cxa_atexit.c:229
#9  0x00000000800ff274 in exit (status=0) at vxexit.c:49
#10 0x00000000800001d8 in _start ()

Forcing a call to get_mutex() earlier during the _Safe_sequence_base ctor
forces the mutex destructor to be called in the correct order, but does not
seem like a very robust workaround.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug libstdc++/65343] unexpected exception thrown during destruction of static object in debug mode
  2015-03-07 13:29 [Bug libstdc++/65343] New: unexpected exception thrown during destruction of static object in debug mode frankhb1989 at gmail dot com
  2015-03-10 12:25 ` [Bug libstdc++/65343] " frankhb1989 at gmail dot com
  2022-01-25 14:52 ` poulhies at adacore dot com
@ 2022-01-25 17:30 ` ebotcazou at gcc dot gnu.org
  2022-01-25 20:58 ` redi at gcc dot gnu.org
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: ebotcazou at gcc dot gnu.org @ 2022-01-25 17:30 UTC (permalink / raw)
  To: gcc-bugs

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

Eric Botcazou <ebotcazou at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
                 CC|                            |ebotcazou at gcc dot gnu.org
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2022-01-25

--- Comment #4 from Eric Botcazou <ebotcazou at gcc dot gnu.org> ---
Right, this is problematic on VxWorks and Windows at least.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug libstdc++/65343] unexpected exception thrown during destruction of static object in debug mode
  2015-03-07 13:29 [Bug libstdc++/65343] New: unexpected exception thrown during destruction of static object in debug mode frankhb1989 at gmail dot com
                   ` (2 preceding siblings ...)
  2022-01-25 17:30 ` ebotcazou at gcc dot gnu.org
@ 2022-01-25 20:58 ` redi at gcc dot gnu.org
  2022-01-25 21:35 ` poulhies at adacore dot com
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2022-01-25 20:58 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Patch for stage 1:

--- a/libstdc++-v3/src/c++11/shared_ptr.cc
+++ b/libstdc++-v3/src/c++11/shared_ptr.cc
@@ -28,6 +28,11 @@

 namespace __gnu_internal _GLIBCXX_VISIBILITY(hidden)
 {
+  namespace
+  {
+    alignas(64) unsigned char __mutex_storage[64 * (mask + 1)];
+  }
+
   /* Returns different instances of __mutex depending on the passed index
    * in order to limit contention.
    */
@@ -36,7 +41,7 @@ namespace __gnu_internal _GLIBCXX_VISIBILITY(hidden)
   {
     // increase alignment to put each lock on a separate cache line
     struct alignas(64) M : __gnu_cxx::__mutex { };
-    static M m[mask + 1];
+    static M* m = ::new(__mutex_storage) M[mask + 1];
     return m[i];
   }
 }

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug libstdc++/65343] unexpected exception thrown during destruction of static object in debug mode
  2015-03-07 13:29 [Bug libstdc++/65343] New: unexpected exception thrown during destruction of static object in debug mode frankhb1989 at gmail dot com
                   ` (3 preceding siblings ...)
  2022-01-25 20:58 ` redi at gcc dot gnu.org
@ 2022-01-25 21:35 ` poulhies at adacore dot com
  2022-01-25 21:36 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: poulhies at adacore dot com @ 2022-01-25 21:35 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Marc Poulhiès <poulhies at adacore dot com> ---
(In reply to Jonathan Wakely from comment #5)
> Patch for stage 1:

Thank you ! I can confirm this patch fixes the issue we have on vxworks.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug libstdc++/65343] unexpected exception thrown during destruction of static object in debug mode
  2015-03-07 13:29 [Bug libstdc++/65343] New: unexpected exception thrown during destruction of static object in debug mode frankhb1989 at gmail dot com
                   ` (4 preceding siblings ...)
  2022-01-25 21:35 ` poulhies at adacore dot com
@ 2022-01-25 21:36 ` redi at gcc dot gnu.org
  2022-01-25 21:38 ` redi at gcc dot gnu.org
  2024-02-08 12:40 ` redi at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2022-01-25 21:36 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Great, thanks for checking it.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug libstdc++/65343] unexpected exception thrown during destruction of static object in debug mode
  2015-03-07 13:29 [Bug libstdc++/65343] New: unexpected exception thrown during destruction of static object in debug mode frankhb1989 at gmail dot com
                   ` (5 preceding siblings ...)
  2022-01-25 21:36 ` redi at gcc dot gnu.org
@ 2022-01-25 21:38 ` redi at gcc dot gnu.org
  2024-02-08 12:40 ` redi at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2022-01-25 21:38 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from Jonathan Wakely <redi at gcc dot gnu.org> ---
We should do something similar in src/c++11/mutex.cc

  mutex&
  __get_once_mutex()
  {
    static mutex once_mutex;
    return once_mutex;
  }

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug libstdc++/65343] unexpected exception thrown during destruction of static object in debug mode
  2015-03-07 13:29 [Bug libstdc++/65343] New: unexpected exception thrown during destruction of static object in debug mode frankhb1989 at gmail dot com
                   ` (6 preceding siblings ...)
  2022-01-25 21:38 ` redi at gcc dot gnu.org
@ 2024-02-08 12:40 ` redi at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2024-02-08 12:40 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #9 from Jonathan Wakely <redi at gcc dot gnu.org> ---
The original bug should be fixed by r13-6282-gd70f49e98245f8

Comment 8 isn't done though.

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2024-02-08 12:40 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-03-07 13:29 [Bug libstdc++/65343] New: unexpected exception thrown during destruction of static object in debug mode frankhb1989 at gmail dot com
2015-03-10 12:25 ` [Bug libstdc++/65343] " frankhb1989 at gmail dot com
2022-01-25 14:52 ` poulhies at adacore dot com
2022-01-25 17:30 ` ebotcazou at gcc dot gnu.org
2022-01-25 20:58 ` redi at gcc dot gnu.org
2022-01-25 21:35 ` poulhies at adacore dot com
2022-01-25 21:36 ` redi at gcc dot gnu.org
2022-01-25 21:38 ` redi at gcc dot gnu.org
2024-02-08 12:40 ` redi at gcc dot gnu.org

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).