public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/101695] New: calling incorrect destructor of same-name class in anonymous namespaces in separate translation units
@ 2021-07-30 14:40 tom_maly at volny dot cz
  2021-08-06  9:44 ` [Bug c++/101695] " redi at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: tom_maly at volny dot cz @ 2021-07-30 14:40 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 101695
           Summary: calling incorrect destructor of same-name class in
                    anonymous namespaces in separate translation units
           Product: gcc
           Version: 11.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: tom_maly at volny dot cz
  Target Milestone: ---

Created attachment 51225
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51225&action=edit
commit 079009b17cac42fb7c46adcee6055b4776b1360f

I have two cpp files, each with anonymous namespace with a class with same
name.
I store member function pointer to method that call the destructor.
Destructor from same translation unit is used in both cases.

Tested with g++ 9, 10, and 11 - all incorrect.
Also tested with clang 10 and msvc - both correct.

All source code to reproduce the bug (in addition to the attached archive) is
available at:
https://github.com/malytomas/gppBugAnonDestr

The github actions show results for all mentioned compilers.



More detailed info:

At least two translation units are required to separate the anonymous
namespaces.

Example command line used for compilation:
/usr/bin/g++-11   -Wall -Wextra -g -std=gnu++17 -MD -MT
CMakeFiles/gppBugAnonDestr.dir/sources/aaa.cpp.o -MF
CMakeFiles/gppBugAnonDestr.dir/sources/aaa.cpp.o.d -o
CMakeFiles/gppBugAnonDestr.dir/sources/aaa.cpp.o -c
/home/runner/work/gppBugAnonDestr/gppBugAnonDestr/sources/aaa.cpp

Example g++ version:
g++-11 (Ubuntu 11.1.0-1ubuntu1~20.04) 11.1.0

In the test code, a counter is used to detect which constructors/destructors
are called and an exception is thrown when number of constructors does not
match number of destructors.

Structure MemoryArena is used to create and destroy the Tester structures.
I could not reproduce the bug without it (using just free functions).

The struct Holder is very simplified std::unique_ptr.

Struct Delegate is simpler std::function. Simplifying it even further (making
it non template) also partially fixes the bug, therefore I keep it as is.



Feel free to contact me if you need any further information.
Thanks

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

* [Bug c++/101695] calling incorrect destructor of same-name class in anonymous namespaces in separate translation units
  2021-07-30 14:40 [Bug c++/101695] New: calling incorrect destructor of same-name class in anonymous namespaces in separate translation units tom_maly at volny dot cz
@ 2021-08-06  9:44 ` redi at gcc dot gnu.org
  2021-08-06  9:51 ` redi at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2021-08-06  9:44 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2021-08-06

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

* [Bug c++/101695] calling incorrect destructor of same-name class in anonymous namespaces in separate translation units
  2021-07-30 14:40 [Bug c++/101695] New: calling incorrect destructor of same-name class in anonymous namespaces in separate translation units tom_maly at volny dot cz
  2021-08-06  9:44 ` [Bug c++/101695] " redi at gcc dot gnu.org
@ 2021-08-06  9:51 ` redi at gcc dot gnu.org
  2021-08-06 10:14 ` redi at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2021-08-06  9:51 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
  Attachment #51225|0                           |1
        is obsolete|                            |

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Created attachment 51268
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51268&action=edit
Tar file with reproducer

This is a slightly reduced version of the repro, containing only the source
files. It can be built using g++ *.cpp with any version from 4.7.0 onwards to
demonstrate the bug.

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

* [Bug c++/101695] calling incorrect destructor of same-name class in anonymous namespaces in separate translation units
  2021-07-30 14:40 [Bug c++/101695] New: calling incorrect destructor of same-name class in anonymous namespaces in separate translation units tom_maly at volny dot cz
  2021-08-06  9:44 ` [Bug c++/101695] " redi at gcc dot gnu.org
  2021-08-06  9:51 ` redi at gcc dot gnu.org
@ 2021-08-06 10:14 ` redi at gcc dot gnu.org
  2021-08-06 10:21 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2021-08-06 10:14 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
  Attachment #51268|0                           |1
        is obsolete|                            |

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Created attachment 51269
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51269&action=edit
Tar file with reproducer

Slightly further reduced.

The bug is the visibility of this symbol:

0000000000000000 W
_ZN8DelegateIFvPvEE4bindI11MemoryArenaXadL_ZNS4_7destroyIN12_GLOBAL__N_16TesterEEEvS0_EEEERS2_PT_


That should have internal linkage. Because both aaa.o and bbb.o contain that as
a weak symbol, the linker merges them and only keeps the first one, which runs
the destroy<{aaa.cpp anonymous namespace}::Tester> specialization, which casts
the void* to that type and so deletes it as the wrong type, which runs the
wrong destructor, and decrements the wrong counter.

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

* [Bug c++/101695] calling incorrect destructor of same-name class in anonymous namespaces in separate translation units
  2021-07-30 14:40 [Bug c++/101695] New: calling incorrect destructor of same-name class in anonymous namespaces in separate translation units tom_maly at volny dot cz
                   ` (2 preceding siblings ...)
  2021-08-06 10:14 ` redi at gcc dot gnu.org
@ 2021-08-06 10:21 ` redi at gcc dot gnu.org
  2021-08-06 10:23 ` redi at gcc dot gnu.org
  2021-08-06 10:32 ` redi at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2021-08-06 10:21 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=70413

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Possibly a dup of PR 70413

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

* [Bug c++/101695] calling incorrect destructor of same-name class in anonymous namespaces in separate translation units
  2021-07-30 14:40 [Bug c++/101695] New: calling incorrect destructor of same-name class in anonymous namespaces in separate translation units tom_maly at volny dot cz
                   ` (3 preceding siblings ...)
  2021-08-06 10:21 ` redi at gcc dot gnu.org
@ 2021-08-06 10:23 ` redi at gcc dot gnu.org
  2021-08-06 10:32 ` redi at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2021-08-06 10:23 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
PR 70413 comment 3 has a suggested fix

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

* [Bug c++/101695] calling incorrect destructor of same-name class in anonymous namespaces in separate translation units
  2021-07-30 14:40 [Bug c++/101695] New: calling incorrect destructor of same-name class in anonymous namespaces in separate translation units tom_maly at volny dot cz
                   ` (4 preceding siblings ...)
  2021-08-06 10:23 ` redi at gcc dot gnu.org
@ 2021-08-06 10:32 ` redi at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2021-08-06 10:32 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |DUPLICATE
             Status|NEW                         |RESOLVED
           See Also|https://gcc.gnu.org/bugzill |
                   |a/show_bug.cgi?id=70413     |

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Definitely a dup

*** This bug has been marked as a duplicate of bug 70413 ***

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

end of thread, other threads:[~2021-08-06 10:32 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-30 14:40 [Bug c++/101695] New: calling incorrect destructor of same-name class in anonymous namespaces in separate translation units tom_maly at volny dot cz
2021-08-06  9:44 ` [Bug c++/101695] " redi at gcc dot gnu.org
2021-08-06  9:51 ` redi at gcc dot gnu.org
2021-08-06 10:14 ` redi at gcc dot gnu.org
2021-08-06 10:21 ` redi at gcc dot gnu.org
2021-08-06 10:23 ` redi at gcc dot gnu.org
2021-08-06 10:32 ` 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).