public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/94810] New: std::cout segmentation fault in __attribute__((constructor)) function
@ 2020-04-27 21:44 gcasper42 at gmail dot com
  2020-04-27 22:32 ` [Bug libstdc++/94810] " redi at gcc dot gnu.org
                   ` (12 more replies)
  0 siblings, 13 replies; 14+ messages in thread
From: gcasper42 at gmail dot com @ 2020-04-27 21:44 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 94810
           Summary: std::cout segmentation fault in
                    __attribute__((constructor)) function
           Product: gcc
           Version: 9.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gcasper42 at gmail dot com
  Target Milestone: ---

Created attachment 48389
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=48389&action=edit
Code to reproduce bug

Exact Version of GCC:
g++ (Arch Linux 9.3.0-1) 9.3.0

System Type:
Arch Linux (under Windows Subsystem for Linux 1)

The attached code compiles and runs, but runs into a seg fault. The seg fault
occurs when trying to print using std::cout in my function marked with the
__attribute__((constructor)), but when I instead use printf to print there is
no seg fault. 

See end for successful build environment.

GDB Stacktrace/Useful Output:

Program received signal SIGSEGV, Segmentation fault.
0x00007fffff6e661a in std::ostream::sentry::sentry (this=0x7ffffffee460,
    __os=...)
    at
/build/gcc/src/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/ostream.tcc:46
46     
/build/gcc/src/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/ostream.tcc:
No such file or directory.

(gdb) bt
#0  0x00007fffff6e661a in std::ostream::sentry::sentry (this=0x7ffffffee460,
    __os=...)
    at
/build/gcc/src/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/ostream.tcc:46
#1  0x00007fffff6e6d4c in std::__ostream_insert<char, std::char_traits<char> >
(__out=..., __s=__s@entry=0x8002005 "Starting", __n=8)
    at
/build/gcc/src/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/ostream_insert.h:76
#2  0x00007fffff6e722e in std::operator<< <std::char_traits<char> > (
    __out=..., __s=0x8002005 "Starting")
    at
/build/gcc/src/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/char_traits.h:335
#3  0x0000000008001180 in constructor () at test.cpp:4
#4  0x00000000080012ad in __libc_csu_init ()
#5  0x00007fffff2b6fb0 in __libc_start_main () from /usr/lib/libc.so.6
#6  0x000000000800109e in _start ()

Something weird to mention (other build environment):

While using the below build environment, the attached code runs as expected.

Exact Version of GCC:
g++.exe (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0

System Type:
MinGW-W64 Windows 10

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

* [Bug libstdc++/94810] std::cout segmentation fault in __attribute__((constructor)) function
  2020-04-27 21:44 [Bug libstdc++/94810] New: std::cout segmentation fault in __attribute__((constructor)) function gcasper42 at gmail dot com
@ 2020-04-27 22:32 ` redi at gcc dot gnu.org
  2020-04-27 23:41 ` gcasper42 at gmail dot com
                   ` (11 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: redi at gcc dot gnu.org @ 2020-04-27 22:32 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Why can't you just use a global object with a constructor and destructor? That
would get correctly ordered with respect to the std::ios_base::Init object that
needs to be created before std::cout can be used.

The constructor and destructor attributes not necessary in C++, and are
documented as having an unspecified order relative to global objects (like the
std::ios_base::Init one).

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

* [Bug libstdc++/94810] std::cout segmentation fault in __attribute__((constructor)) function
  2020-04-27 21:44 [Bug libstdc++/94810] New: std::cout segmentation fault in __attribute__((constructor)) function gcasper42 at gmail dot com
  2020-04-27 22:32 ` [Bug libstdc++/94810] " redi at gcc dot gnu.org
@ 2020-04-27 23:41 ` gcasper42 at gmail dot com
  2020-04-28  8:51 ` redi at gcc dot gnu.org
                   ` (10 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: gcasper42 at gmail dot com @ 2020-04-27 23:41 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Geoffrey Casper <gcasper42 at gmail dot com> ---
I suppose that's a workaround, but there shouldn't be a seg fault in a
libstdc++ header file.

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

* [Bug libstdc++/94810] std::cout segmentation fault in __attribute__((constructor)) function
  2020-04-27 21:44 [Bug libstdc++/94810] New: std::cout segmentation fault in __attribute__((constructor)) function gcasper42 at gmail dot com
  2020-04-27 22:32 ` [Bug libstdc++/94810] " redi at gcc dot gnu.org
  2020-04-27 23:41 ` gcasper42 at gmail dot com
@ 2020-04-28  8:51 ` redi at gcc dot gnu.org
  2020-04-28 13:16 ` gcasper42 at gmail dot com
                   ` (9 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: redi at gcc dot gnu.org @ 2020-04-28  8:51 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
There isn't, there's a segfault in your program. That is caused by assuming
that the global streams have been constructed already, when it is clearly
documented that the construction order is unspecified.

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

* [Bug libstdc++/94810] std::cout segmentation fault in __attribute__((constructor)) function
  2020-04-27 21:44 [Bug libstdc++/94810] New: std::cout segmentation fault in __attribute__((constructor)) function gcasper42 at gmail dot com
                   ` (2 preceding siblings ...)
  2020-04-28  8:51 ` redi at gcc dot gnu.org
@ 2020-04-28 13:16 ` gcasper42 at gmail dot com
  2020-04-28 16:07 ` redi at gcc dot gnu.org
                   ` (8 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: gcasper42 at gmail dot com @ 2020-04-28 13:16 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Geoffrey Casper <gcasper42 at gmail dot com> ---
Wouldn't it make more sense to initialize global objects on a per need basis?
So the constructors of unused global objects would never be called and there is
no ambiguity of when constructors are called. I'd gladly work on implementing
this, but I'd need some more information on the brod design of GCC.

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

* [Bug libstdc++/94810] std::cout segmentation fault in __attribute__((constructor)) function
  2020-04-27 21:44 [Bug libstdc++/94810] New: std::cout segmentation fault in __attribute__((constructor)) function gcasper42 at gmail dot com
                   ` (3 preceding siblings ...)
  2020-04-28 13:16 ` gcasper42 at gmail dot com
@ 2020-04-28 16:07 ` redi at gcc dot gnu.org
  2020-04-28 16:09 ` redi at gcc dot gnu.org
                   ` (7 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: redi at gcc dot gnu.org @ 2020-04-28 16:07 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
I don't see how that could conform to the standard's requirements.

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

* [Bug libstdc++/94810] std::cout segmentation fault in __attribute__((constructor)) function
  2020-04-27 21:44 [Bug libstdc++/94810] New: std::cout segmentation fault in __attribute__((constructor)) function gcasper42 at gmail dot com
                   ` (4 preceding siblings ...)
  2020-04-28 16:07 ` redi at gcc dot gnu.org
@ 2020-04-28 16:09 ` redi at gcc dot gnu.org
  2020-04-28 17:39 ` gcasper42 at gmail dot com
                   ` (6 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: redi at gcc dot gnu.org @ 2020-04-28 16:09 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Maybe you misunderstood comment 3, the construction order is only unspecified
when you use __attribute__((constructor)). If you just create normal global
objects using normal C++ the order within a single translation unit is
completely specified, and your program would work as expected.

So again, why can't you just use standard C++ instead of attributes that aren't
needed in C++, and proposing non-conforming changes to the compiler?

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

* [Bug libstdc++/94810] std::cout segmentation fault in __attribute__((constructor)) function
  2020-04-27 21:44 [Bug libstdc++/94810] New: std::cout segmentation fault in __attribute__((constructor)) function gcasper42 at gmail dot com
                   ` (5 preceding siblings ...)
  2020-04-28 16:09 ` redi at gcc dot gnu.org
@ 2020-04-28 17:39 ` gcasper42 at gmail dot com
  2020-04-28 20:52 ` redi at gcc dot gnu.org
                   ` (5 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: gcasper42 at gmail dot com @ 2020-04-28 17:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Geoffrey Casper <gcasper42 at gmail dot com> ---
I believe I did misunderstand comment 3. Thanks for your clarification. Do all
global objects get marked with the constructor attribute, which leads to the
ambiguity?

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

* [Bug libstdc++/94810] std::cout segmentation fault in __attribute__((constructor)) function
  2020-04-27 21:44 [Bug libstdc++/94810] New: std::cout segmentation fault in __attribute__((constructor)) function gcasper42 at gmail dot com
                   ` (6 preceding siblings ...)
  2020-04-28 17:39 ` gcasper42 at gmail dot com
@ 2020-04-28 20:52 ` redi at gcc dot gnu.org
  2023-02-03 19:29 ` murugesandins at gmail dot com
                   ` (4 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: redi at gcc dot gnu.org @ 2020-04-28 20:52 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #8 from Jonathan Wakely <redi at gcc dot gnu.org> ---
No. The ambiguity only happens if you use the attribute explicitly, as you are
doing.

Just create a global object with a constructor and a destructor, instead of
using functions with __attribute__((constructor)) and
__attribute__((destructor)).

#include <iostream>

struct X {
  X() {
    std::cout << "Starting" << std::endl;
  }
  ~X() {
    std::cout << "Finishing" << std::endl;
  }
};

int main() {
    std::cout << "Hello, World!" << std::endl;
}

I'm closing this as invalid, since the attribute is documented to have
unspecified construction order relative to normal globals, which means your
original example is not expected to work.

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

* [Bug libstdc++/94810] std::cout segmentation fault in __attribute__((constructor)) function
  2020-04-27 21:44 [Bug libstdc++/94810] New: std::cout segmentation fault in __attribute__((constructor)) function gcasper42 at gmail dot com
                   ` (7 preceding siblings ...)
  2020-04-28 20:52 ` redi at gcc dot gnu.org
@ 2023-02-03 19:29 ` murugesandins at gmail dot com
  2023-02-03 19:47 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: murugesandins at gmail dot com @ 2023-02-03 19:29 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #9 from Murugesan Nagarajan <murugesandins at gmail dot com> ---
I'll update my comment today(Sat 04-Feb-2023 IST) after 09:00 AM IST. Right now
I'm facing network issue due to travelling.

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

* [Bug libstdc++/94810] std::cout segmentation fault in __attribute__((constructor)) function
  2020-04-27 21:44 [Bug libstdc++/94810] New: std::cout segmentation fault in __attribute__((constructor)) function gcasper42 at gmail dot com
                   ` (8 preceding siblings ...)
  2023-02-03 19:29 ` murugesandins at gmail dot com
@ 2023-02-03 19:47 ` pinskia at gcc dot gnu.org
  2023-02-03 22:04 ` murugesandins at gmail dot com
                   ` (2 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-02-03 19:47 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

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

--- Comment #10 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note in GCC 13+, this should work now after r13-3707-g4e4e3ffd10f53e .

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

* [Bug libstdc++/94810] std::cout segmentation fault in __attribute__((constructor)) function
  2020-04-27 21:44 [Bug libstdc++/94810] New: std::cout segmentation fault in __attribute__((constructor)) function gcasper42 at gmail dot com
                   ` (9 preceding siblings ...)
  2023-02-03 19:47 ` pinskia at gcc dot gnu.org
@ 2023-02-03 22:04 ` murugesandins at gmail dot com
  2023-02-04  3:22 ` murugesandins at gmail dot com
  2023-02-06  4:41 ` murugesandins at gmail dot com
  12 siblings, 0 replies; 14+ messages in thread
From: murugesandins at gmail dot com @ 2023-02-03 22:04 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #11 from Murugesan Nagarajan <murugesandins at gmail dot com> ---
Hi Andrew,
Thank you for your comment. I'll check this after 09:00 AM.
Regards,
N.Murugesan
Google:
murugesan openssl

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

* [Bug libstdc++/94810] std::cout segmentation fault in __attribute__((constructor)) function
  2020-04-27 21:44 [Bug libstdc++/94810] New: std::cout segmentation fault in __attribute__((constructor)) function gcasper42 at gmail dot com
                   ` (10 preceding siblings ...)
  2023-02-03 22:04 ` murugesandins at gmail dot com
@ 2023-02-04  3:22 ` murugesandins at gmail dot com
  2023-02-06  4:41 ` murugesandins at gmail dot com
  12 siblings, 0 replies; 14+ messages in thread
From: murugesandins at gmail dot com @ 2023-02-04  3:22 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #12 from Murugesan Nagarajan <murugesandins at gmail dot com> ---
Thank you for sharing comment at:
>> https://gcc.gnu.org/git/gitweb.cgi?p=gcc.git;h=4e4e3ffd10f53e
Move stream initialization into compiled library
I am facing my issue to have my proper environment:
01. My laptop has gone before 6 months(it was having RHEL and ubuntu) at
vmplayer iso files.
02. Current acer system do not have any .iso files.
03. My working place not providing sudo access at Linux.
I have verified at cygwin using g++.exe which is working fine.
:)

$ g++.exe --version 2>&1 | grep GCC
g++ (GCC) 11.3.0

However I can verify your g++ related changes and I will update this page(in
future) when I have related OS using relative admin privileges.
Have a nice weekend.

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

* [Bug libstdc++/94810] std::cout segmentation fault in __attribute__((constructor)) function
  2020-04-27 21:44 [Bug libstdc++/94810] New: std::cout segmentation fault in __attribute__((constructor)) function gcasper42 at gmail dot com
                   ` (11 preceding siblings ...)
  2023-02-04  3:22 ` murugesandins at gmail dot com
@ 2023-02-06  4:41 ` murugesandins at gmail dot com
  12 siblings, 0 replies; 14+ messages in thread
From: murugesandins at gmail dot com @ 2023-02-06  4:41 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #13 from Murugesan Nagarajan <murugesandins at gmail dot com> ---
@Andrew,
        Thank you for updating current bug id: 94810
Old bug reported:
        On:                     Mon 27-Apr-2020 09:44:52 PM UTC
        Old Bug URL:           
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94810
        Old Status:             RESOLVED INVALID
@Andrew's comment on 2023-02-03 07:47:43 PM UTC
        Related linked bug/url:
https://gcc.gnu.org/git/gitweb.cgi?p=gcc.git;h=4e4e3ffd10f53e
        Updated bug on:         Sun 06-Nov-2022 04:16:00 PM UTC
        Updated status:         Bug fix completed using "__attribute__" and new
bug using "__has_attribute".

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

end of thread, other threads:[~2023-02-06  4:41 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-27 21:44 [Bug libstdc++/94810] New: std::cout segmentation fault in __attribute__((constructor)) function gcasper42 at gmail dot com
2020-04-27 22:32 ` [Bug libstdc++/94810] " redi at gcc dot gnu.org
2020-04-27 23:41 ` gcasper42 at gmail dot com
2020-04-28  8:51 ` redi at gcc dot gnu.org
2020-04-28 13:16 ` gcasper42 at gmail dot com
2020-04-28 16:07 ` redi at gcc dot gnu.org
2020-04-28 16:09 ` redi at gcc dot gnu.org
2020-04-28 17:39 ` gcasper42 at gmail dot com
2020-04-28 20:52 ` redi at gcc dot gnu.org
2023-02-03 19:29 ` murugesandins at gmail dot com
2023-02-03 19:47 ` pinskia at gcc dot gnu.org
2023-02-03 22:04 ` murugesandins at gmail dot com
2023-02-04  3:22 ` murugesandins at gmail dot com
2023-02-06  4:41 ` murugesandins at gmail dot com

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