public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/36960]  New: Reference variable in virtually inherited base corrupted under optimization
@ 2008-07-28 23:58 raymond at corvil dot com
  2008-07-29  0:02 ` [Bug c++/36960] " pinskia at gcc dot gnu dot org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: raymond at corvil dot com @ 2008-07-28 23:58 UTC (permalink / raw)
  To: gcc-bugs

Apologies if this is a duplicate:  I searched existing reports and found tons
of hits on "virtual inheritance" and "reference", but couldn't find anything
like this.  The following program illustrates the problem:

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

struct Lower {
        const int&      ref;

        Lower(const int& ref) : ref(ref) { }
};


struct Middle : public virtual Lower {

        Middle(const int& ref) : Lower(ref) { }
};


struct Upper : public Middle {

        Upper(const int& ref) : Lower(ref), Middle(ref) { }

        int     get()
        {
                return ref;
        }
};


int     main()
{
        int i = 0;
        Upper upper(i);

        return upper.get();
}


>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Compiling this using 4.2.2 or 4.2.3 with -O2 or higher causes the resulting
binary to return a non-zero value;  3.4.2 produces a bug-free binary.  I've
seen the same effect in more complex code, where gdb gives an obviously wrong
address for the "ref" member.  Dropping to -O fixes the problem, as do several
other changes:

* Making "ref" an int (as opposed to an int&)
* Making Middle inherit non-virtually from Lower
* Accessing "ref" from Middle instead of Upper

The constness of the reference has no effect.


Issued command-line:
    g++ -Wall -W -Wundef -Wpointer-arith -g -O2 VirtuallyInheritedReference.cpp

Full output from running with "-v --save-temps" is as follows:
    g++ -v -save-temps -Wall -W -Wundef -Wpointer-arith -g -O2 -c -o /dev/null
VirtuallyInheritedReference.cpp

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: /mnt/taw/usr/Taw/tmp/gcc-4.2.2/configure --prefix=/usr
--disable-nls --libexecdir=/usr/lib --enable-shared --enable-threads=posix
--enable-__cxa_atexit --enable-clocale=gnu --enable-languages=c,c++
--disable-bootstrap
Thread model: posix
gcc version 4.2.2
 /usr/lib/gcc/i686-pc-linux-gnu/4.2.2/cc1plus -E -quiet -v -D_GNU_SOURCE
VirtuallyInheritedReference.cpp -mtune=generic -Wall -W -Wundef -Wpointer-arith
-fworking-directory -O2 -fpch-preprocess -o VirtuallyInheritedReference.ii
ignoring nonexistent directory
"/usr/lib/gcc/i686-pc-linux-gnu/4.2.2/../../../../i686-pc-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/i686-pc-linux-gnu/4.2.2/../../../../include/c++/4.2.2

/usr/lib/gcc/i686-pc-linux-gnu/4.2.2/../../../../include/c++/4.2.2/i686-pc-linux-gnu
 /usr/lib/gcc/i686-pc-linux-gnu/4.2.2/../../../../include/c++/4.2.2/backward
 /usr/local/include
 /usr/lib/gcc/i686-pc-linux-gnu/4.2.2/include
 /usr/include
End of search list.
 /usr/lib/gcc/i686-pc-linux-gnu/4.2.2/cc1plus -fpreprocessed
VirtuallyInheritedReference.ii -quiet -dumpbase VirtuallyInheritedReference.cpp
-mtune=generic -auxbase-strip /dev/null -g -O2 -Wall -W -Wundef -Wpointer-arith
-version -o VirtuallyInheritedReference.s
GNU C++ version 4.2.2 (i686-pc-linux-gnu)
        compiled by GNU C version 4.2.2.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 6ba594205d388e98f3b46dee442d61ac
 as -V -Qy -o /dev/null VirtuallyInheritedReference.s
GNU assembler version 2.18 (i686-pc-linux-gnu) using BFD version (GNU Binutils)
2.18
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>



Contents of VirtuallyInheritedReference.ii:

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
# 1 "VirtuallyInheritedReference.cpp"
# 1 "/home/raymond/src/C++/gcc-bugs//"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "VirtuallyInheritedReference.cpp"

struct Lower {
 int& ref;

 virtual ~Lower() { }

 Lower(int& ref) : ref(ref) { }
};


struct Middle : public virtual Lower {

 Middle(int& ref) : Lower(ref) { }
};


struct Upper : public Middle {

 Upper(int& ref) : Lower(ref), Middle(ref) { }

 int get()
 {
  return ref;
 }
};


int main()
{
 int i = 0;
 Upper upper(i);

 return upper.get();
}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


-- 
           Summary: Reference variable in virtually inherited base corrupted
                    under optimization
           Product: gcc
           Version: 4.2.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: raymond at corvil dot com
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36960


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

* [Bug c++/36960] Reference variable in virtually inherited base corrupted under optimization
  2008-07-28 23:58 [Bug c++/36960] New: Reference variable in virtually inherited base corrupted under optimization raymond at corvil dot com
@ 2008-07-29  0:02 ` pinskia at gcc dot gnu dot org
  2008-07-29  0:03 ` pinskia at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2008-07-29  0:02 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2008-07-29 00:01 -------
  i = 0;
  upper.D.2127.ref = &i;
  upper.D.2125._vptr$Middle = &_ZTV5Upper + 12;
  return *((struct Lower *) &upper + (long unsigned int) *(int *)
&_ZTV5Upper)->ref;

This works on the trunk correctly.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36960


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

* [Bug c++/36960] Reference variable in virtually inherited base corrupted under optimization
  2008-07-28 23:58 [Bug c++/36960] New: Reference variable in virtually inherited base corrupted under optimization raymond at corvil dot com
  2008-07-29  0:02 ` [Bug c++/36960] " pinskia at gcc dot gnu dot org
@ 2008-07-29  0:03 ` pinskia at gcc dot gnu dot org
  2010-07-13  9:41 ` steven at gcc dot gnu dot org
  2010-07-13 13:31 ` rguenth at gcc dot gnu dot org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2008-07-29  0:03 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from pinskia at gcc dot gnu dot org  2008-07-29 00:02 -------
Likewise for 4.3.0.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36960


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

* [Bug c++/36960] Reference variable in virtually inherited base corrupted under optimization
  2008-07-28 23:58 [Bug c++/36960] New: Reference variable in virtually inherited base corrupted under optimization raymond at corvil dot com
  2008-07-29  0:02 ` [Bug c++/36960] " pinskia at gcc dot gnu dot org
  2008-07-29  0:03 ` pinskia at gcc dot gnu dot org
@ 2010-07-13  9:41 ` steven at gcc dot gnu dot org
  2010-07-13 13:31 ` rguenth at gcc dot gnu dot org
  3 siblings, 0 replies; 5+ messages in thread
From: steven at gcc dot gnu dot org @ 2010-07-13  9:41 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from steven at gcc dot gnu dot org  2010-07-13 09:41 -------
Works with GCC 4.3, 4.4, 4.5, and trunk.

GCC 4.2 is no longer maintained, so this bug will not be fixed.

Richi, perhaps you can use the test case, put it in the test suite?


-- 

steven at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |rguenth at gcc dot gnu dot
                   |                            |org
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |WONTFIX


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36960


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

* [Bug c++/36960] Reference variable in virtually inherited base corrupted under optimization
  2008-07-28 23:58 [Bug c++/36960] New: Reference variable in virtually inherited base corrupted under optimization raymond at corvil dot com
                   ` (2 preceding siblings ...)
  2010-07-13  9:41 ` steven at gcc dot gnu dot org
@ 2010-07-13 13:31 ` rguenth at gcc dot gnu dot org
  3 siblings, 0 replies; 5+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2010-07-13 13:31 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from rguenth at gcc dot gnu dot org  2010-07-13 13:31 -------
Subject: Bug 36960

Author: rguenth
Date: Tue Jul 13 13:31:26 2010
New Revision: 162141

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=162141
Log:
2010-07-13  Richard Guenther  <rguenther@suse.de>

        PR tree-optimization/36960
        * g++.dg/torture/pr36960.C: New testcase.

Added:
    trunk/gcc/testsuite/g++.dg/torture/pr36960.C
Modified:
    trunk/gcc/testsuite/ChangeLog


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36960


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

end of thread, other threads:[~2010-07-13 13:31 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-07-28 23:58 [Bug c++/36960] New: Reference variable in virtually inherited base corrupted under optimization raymond at corvil dot com
2008-07-29  0:02 ` [Bug c++/36960] " pinskia at gcc dot gnu dot org
2008-07-29  0:03 ` pinskia at gcc dot gnu dot org
2010-07-13  9:41 ` steven at gcc dot gnu dot org
2010-07-13 13:31 ` rguenth at gcc dot gnu dot 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).