optimization/10877: GCC 3.3 miscompiles code with -O3 -fPIC on x86

[email protected]
Newsgroups gmane.comp.gcc.prs
Message-ID <[email protected]>
>Number:         10877
>Category:       optimization
>Synopsis:       GCC 3.3 miscompiles code with -O3 -fPIC on x86
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          wrong-code
>Submitter-Id:   net
>Arrival-Date:   Tue May 20 01:06:00 UTC 2003
>Closed-Date:
>Last-Modified:
>Originator:     Jack Lloyd
>Release:        3.3
>Organization:
>Environment:
RedHat 7.3, AMD Athlon, glibc 2.2.5, kernel 2.4.19, binutils 2.11.93.0.2
>Description:
GCC 3.3 miscompiles what is (AFAIK) valid C++ code if it is compiled with -O3 -fPIC
on x86. The code in question is attached. I have the following information:

$ g++-3.3 -v
Reading specs from /usr/local/gcc-3.3/lib/gcc-lib/i686-pc-linux-gnu/3.3/specs
Configured with: ../gcc-3.3/configure --prefix=/usr/local/gcc-3.3 --enable-threads
Thread model: posix
gcc version 3.3

The code is accepted by GCC 2.95.3, 3.0.4, 3.1, and 3.2. With all of these
versions, the code works correctly with -O3 -fPIC (and various other
combinations of -O and -fPIC). The code works with 3.3 if -O2 or lower is
specified, or if -O3 without -fPIC/-fpic is used.

One interesting thing is that if the variable local_foo is declared as:

   static foo* local_foo;

then it works. It also doesn't work if local_foo is declared as a non-static
global (rather than in an anonymous namespace), presumably because anonymous
namespace members aren't (IIRC) linked as static in GCC.

I only have 3.2, so it's possible this was introduced in later 3.2 versions
rather than the 3.3 branch. BTW, I've checked, and the resulting binary does
use the gcc 3.3 versions of libgcc and libstdc++, so it's not that.
>How-To-Repeat:
Here is exactly what I'm seeing. This is with a printf that runs right before we exit from main; the version attached has the include of <stdio.h> and the printf call commented out.

     $ g++-3.3 -O3 -fPIC gccbug.cpp 
     $ ./a.out 
     Segmentation fault
     $ g++-3.3 -O2 -fPIC gccbug.cpp 
     $ ./a.out 
     I guess we didn't crash
     $ g++-3.3 -O3 gccbug.cpp 
     $ ./a.out 
     I guess we didn't crash
     $ g++-2.95.3 -O3 -fPIC gccbug.cpp 
     $ ./a.out 
     I guess we didn't crash
     $ g++-3.0.4 -O3 -fPIC gccbug.cpp 
     $ ./a.out 
     I guess we didn't crash
     $ g++-3.1 -O3 -fPIC gccbug.cpp 
     $ ./a.out 
     I guess we didn't crash
     $ g++-3.2 -O3 -fPIC gccbug.cpp 
     $ ./a.out 
     I guess we didn't crash
>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: application/octet-stream; name="gccbug.cpp"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="gccbug.cpp"

c3RydWN0IGZvbyB7IH07CgpuYW1lc3BhY2UgewogICBmb28qIGxvY2FsX2ZvbzsKfQoKY29uc3Qg
Zm9vJiBnZXRfeCgpCiAgIHsKICAgcmV0dXJuICgqbG9jYWxfZm9vKTsKICAgfQoKLy8jaW5jbHVk
ZSA8c3RkaW8uaD4KCmludCBtYWluKCkKICAgewogICBsb2NhbF9mb28gPSBuZXcgZm9vOwogICBm
b28geCA9IGdldF94KCk7CiAgIGRlbGV0ZSBsb2NhbF9mb287CgogICAvL3ByaW50ZigiSSBndWVz
cyB3ZSBkaWRuJ3QgY3Jhc2hcbiIpOwogICByZXR1cm4gMDsKICAgfQo=
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.