The following program crashes after an unaligned memory access.
--------------------------------------------------
extern "C" int printf(const char*,...);

struct Base
{
   double a;
   int b;
   Base() { a = 0; }
};

struct Left: public virtual Base { };

class Right : public virtual Base
{
   public:
      int a;
      int b;
      int c;
      Right();
};

Right::Right()
   : a(0), b(0), c(0)
{
   printf("this=0x%x\n",(int)this);
}

class Join: public virtual Left, public virtual Right
{
};

int main()
{
   Join p;
   return 0;
}

--------------------------------------------------

To reproduce the problem:
   # g++ bug.cc -o bug -g -O2
   # ./bug
   Bus Error (core dumped)

gcc -v output:

Using built-in specs.
Target: sparc-sun-solaris2.8
Configured with: /home/mringe/rebuild/gcc-4.3.2/configure
--prefix=/home/mringe/gcc43 --enable-languages=c,c++ --disable-nls
--with-gmp=/home/mringe/gcc43 --with-mpfr=/home/mringe/gcc43
--with-libiconv-prefix=/usr --with-as=/usr/ccs/bin/as --with-ld=/usr/ccs/bin/ld
Thread model: posix
gcc version 4.3.2 (GCC) 

--------------------------------------------------
Some additional remarks:
- The same problem exists with Solaris 10.
- Using -O1 or -fno-peephole2 solves the problem
- The crash occurs in Right() when initializing the members. 
  Here is the relevant portion of the generated code:
          ...
          ! bug.cc:22
          sethi   %hi(_ZTV5Right+12), %g1
          or      %g1, %lo(_ZTV5Right+12), %g1
          st      %g0, [%o0+4]
          stx     %g0, [%o0+8]    <----- CRASH
          ...

Obviously, the compiler assumes that %o0 (which I assume is the "this"
pointer) is a multiple of 8 and combines b(0) and c(0) into a single
stx instruction. However, analyzing the core file with gdb shows that 
%o8 is 0xffbef7cc. Indeed, when I remove the member initializers,I get
"this=0xffbef7cc" from the printf.

This bug seems to be linked to multiple inheritance. With single inheritance,
objects are always 64-bit aligned as far as I can tell.


-- 
           Summary: Unaligned memory access with multiple inheritance
           Product: gcc
           Version: 4.3.2
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: grxnprzn at gmx dot net
 GCC build triplet: sparc-sun-solaris2.8
  GCC host triplet: sparc-sun-solaris2.8
GCC target triplet: sparc-sun-solaris2.8


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

Reply via email to