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



             Bug #: 56407

           Summary: Optimizations (-O2 -O3) make comparison of arrays of

                    ints to fail

    Classification: Unclassified

           Product: gcc

           Version: 4.7.2

            Status: UNCONFIRMED

          Severity: normal

          Priority: P3

         Component: c

        AssignedTo: unassig...@gcc.gnu.org

        ReportedBy: quin...@lix.polytechnique.fr





Created attachment 29509

  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=29509

The .c .i .s .o files



This is my first bug report, so I hope this is really a bug and that I file it

correctly.



Versions of my stuff:

- gcc (Debian 4.7.2-5) 4.7.2

- Linux 3.2.0-4-amd64 #1 SMP Debian 3.2.35-2 x86_64 GNU/Linux

- Intel(R) Core(TM)2 Duo CPU T9600 @ 2.80GHz



gcc compile stuff:

Using built-in specs.

COLLECT_GCC=gcc

COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.7/lto-wrapper

Target: x86_64-linux-gnu

Configured with: ../src/configure -v --with-pkgversion='Debian 4.7.2-5'

--with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs

--enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr

--program-suffix=-4.7 --enable-shared --enable-linker-build-id

--with-system-zlib --libexecdir=/usr/lib --without-included-gettext

--enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7

--libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu

--enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object

--enable-plugin --enable-objc-gc --with-arch-32=i586 --with-tune=generic

--enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu

--target=x86_64-linux-gnu

Thread model: posix

gcc version 4.7.2 (Debian 4.7.2-5) 



The bug appears when I compile the program with -O2 or -O3:

$ gcc -O3 gcc_bug.c -o gcc_bug

$ ./gcc_bug && echo ok || echo fail

fail



But it works fine with -O1:

$ gcc -O1 gcc_bug.c -o gcc_bug

$ ./gcc_bug && echo ok || echo fail

ok



It works fine with an older version of gcc:

$ gcc --version

gcc (GCC) 4.4.6 20120305 (Red Hat 4.4.6-4)

$ gcc -v

Using built-in specs.

Target: x86_64-redhat-linux

Configured with: ../configure --prefix=/usr --mandir=/usr/share/man

--infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla

--enable-bootstrap --enable-shared --enable-threads=posix

--enable-checking=release --with-system-zlib --enable-__cxa_atexit

--disable-libunwind-exceptions --enable-gnu-unique-object

--enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk

--disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre

--enable-libgcj-multifile --enable-java-maintainer-mode

--with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib

--with-ppl --with-cloog --with-tune=generic --with-arch_32=i686

--build=x86_64-redhat-linux

Thread model: posix

gcc version 4.4.6 20120305 (Red Hat 4.4.6-4) (GCC)

$ gcc -O3 gcc_bug.c -o gcc_bug

$ ./gcc_bug && echo ok || echo fail

ok



When the program fails with gcc 4.7.2 valgrind tells me:

$ gcc -O3 -g gcc_bug.c -o gcc_bug

$ valgrind ./gcc_bug

==3383== Memcheck, a memory error detector

==3383== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.

==3383== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info

==3383== Command: ./gcc_bug

==3383== 

==3383== Conditional jump or move depends on uninitialised value(s)

==3383==    at 0x40079E: test (gcc_bug.c:21)

==3383==    by 0x400482: main (gcc_bug.c:41)

==3383== 

==3383== 

==3383== HEAP SUMMARY:

==3383==     in use at exit: 0 bytes in 0 blocks

==3383==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated

==3383== 

==3383== All heap blocks were freed -- no leaks are possible

==3383== 

==3383== For counts of detected and suppressed errors, rerun with: -v

==3383== Use --track-origins=yes to see where uninitialised values come from

==3383== ERROR SUMMARY: 2 errors from 1 contexts (suppressed: 4 from 4)



Whereas all is fine for valgrind with -O1. Also all is fine with -fwrapv:

$ gcc -O3 -fwrapv gcc_bug.c -o gcc_bug

$ ./gcc_bug && echo ok || echo fail

ok



I suppose the bug appears when gcc is doing some optimizations but I do not

know which. Any small modifications to the program make it work. For example if

I remove the ``if ( comm ) {" line or if I replace ``for( j = 1 ; j <= sz" by

``for( j = 1 ; j < sz".



======================================

extern void exit(int);

extern int rand(void);



void copy(int *r,int *a,int na) {

  int i;

  for( i = 0 ; i < na ; i++ ) {

    r[i] = a[i];

  }

}



void random(int *a,int na) {

  int i;

  for( i = 0 ; i < na ; i++ ) {

    a[i] = rand();

  }

}



int cmp(int *a,int *b,int n) {

  int i;

  for( i = 0 ; i < n ; i++ ) {

    if ( a[i] != b[i] ) return -1;

  }

  return 0;

}



void test(int sz,int comm) {

  int j,n;

  int v[64],w[64];

  for( j = 1 ; j <= sz ; j++ ) {

    n = (2 * j - 1) * (2 * j - 1);

    random(w,n);

    copy(v,w,n);

    if ( comm ) {

      if ( cmp(v,w,n) ) exit(-1);

    }

  }

  exit(0);

}



int main(void) {

  test(2,1);

  return 0;

}

======================================

Reply via email to