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

Reply via email to