Bug 59557

Summary: [4.8/4.9 Regression] Miscompilation after libstdc++ change
Product: gcc Reporter: Richard Biener <rguenth>
Component: libstdc++Assignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED DUPLICATE    
Severity: normal CC: chris
Priority: P2 Keywords: wrong-code
Version: 4.8.2   
Target Milestone: 4.8.3   
Host: Target:
Build: Known to work: 4.8.1
Known to fail: 4.8.2 Last reconfirmed: 2013-12-19 00:00:00
Attachments: testcase
preprocessed source

Description Richard Biener 2013-12-19 10:39:37 UTC
Created attachment 31479 [details]
testcase

A libstdc++ change between r202388 and the 4.8.2 release broke Eigen.  Preprocessing the attached testcase with 4.8.2 and then compiling with -O0
gives

> ./a.out                                                  
Brute force distance = 0.00428018, calls = 10000                                
Segmentation fault      

while preprocessing with 4.8.1 gets the correct

> ./a.out                                                  
Brute force distance = 0.00428018, calls = 10000                                
BVH distance         = 0.00428018, calls = 756
Comment 1 Richard Biener 2013-12-19 10:44:54 UTC
The diff of the preprocessed sources points at

+2013-09-30  Chris Jefferson  <chris@bubblescope.net>                           
+                                                                               
+       PR libstdc++/58437                                                      
+       * include/bits/stl_algo.h (__move_median_first): Rename to              
+       __move_median_to_first, change to take an addition argument.            
+       (__unguarded_partition_pivot): Adjust.                                  
+       * testsuite/performance/25_algorithms/sort.cc: New.                     
+       * testsuite/performance/25_algorithms/sort_heap.cc: Likewise.           
+       * testsuite/performance/25_algorithms/stable_sort.cc: Likewise.   
+                                                                               
+2013-09-19  Mitsuru Kariya  <kariya_mitsuru@hotmail.com>                       
+           Chris Jefferson  <chris@bubblescope.net>                            
+                                                                               
+       PR libstdc++/58358
+       * include/bits/stl_algo.h (search_n): Fix to guarantee a number
+       of comparisons <= number of elements in the range.
+       * testsuite/25_algorithms/search_n/58358.cc: New.
+       * testsuite/25_algorithms/search_n/iterator.cc: Extend.
Comment 2 Chris Jefferson 2013-12-19 14:43:27 UTC
Investigating
Comment 3 Chris Jefferson 2013-12-19 15:02:23 UTC
Can I just confirm I am testing this code correctly?

caj@caj-laptop ~/Downloads> g++ BVH_Example.cpp -O0
caj@caj-laptop ~/Downloads> ./a.out
Brute force distance = 0.00428018, calls = 10000
BVH distance         = 0.00428018, calls = 756
caj@caj-laptop ~/Downloads> g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.8.2-10' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libmudflap --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --with-arch-32=i586 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --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.8.2 (Debian 4.8.2-10)
Comment 4 Richard Biener 2013-12-19 15:16:40 UTC
(In reply to Chris Jefferson from comment #3)
> Can I just confirm I am testing this code correctly?
> 
> caj@caj-laptop ~/Downloads> g++ BVH_Example.cpp -O0
> caj@caj-laptop ~/Downloads> ./a.out
> Brute force distance = 0.00428018, calls = 10000
> BVH distance         = 0.00428018, calls = 756
> caj@caj-laptop ~/Downloads> g++ -v
> Using built-in specs.
> COLLECT_GCC=g++
> COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper
> Target: x86_64-linux-gnu
> Configured with: ../src/configure -v --with-pkgversion='Debian 4.8.2-10'
> --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs
> --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr
> --program-suffix=-4.8 --enable-shared --enable-linker-build-id
> --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
> --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls
> --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug
> --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libmudflap
> --enable-plugin --with-system-zlib --disable-browser-plugin
> --enable-java-awt=gtk --enable-gtk-cairo
> --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre
> --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64
> --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64
> --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar
> --enable-objc-gc --enable-multiarch --with-arch-32=i586 --with-abi=m64
> --with-multilib-list=m32,m64,mx32 --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.8.2 (Debian 4.8.2-10)

Looks like so.  Werid, I definitely can reproduce with FSF 4.8.2 and SUSE 4.8.2
on x86_64.  Are the libstdc++ includes those from 4.8.2-10, too?
Comment 5 Chris Jefferson 2013-12-19 15:43:23 UTC
Also tried with svn head, the code works (and passes using valgrind as well)

caj@caj-laptop ~/Downloads> /gcc-svn/bin/g++ -v
Using built-in specs.
COLLECT_GCC=/gcc-svn/bin/g++
COLLECT_LTO_WRAPPER=/gcc-svn/libexec/gcc/x86_64-unknown-linux-gnu/4.9.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../gcc-svn/configure --prefix=/gcc-svn --enable-languages=c,c++
Thread model: posix
gcc version 4.9.0 20131219 (experimental) (GCC)
Comment 6 Chris Jefferson 2013-12-19 15:51:40 UTC
Wait a minute, I am getting my timeline wrong, sorry.

This is http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58800 , which will be fixed for 4.8.3
Comment 7 Richard Biener 2013-12-19 15:54:18 UTC
Created attachment 31481 [details]
preprocessed source

Fully preprocessed testcase (preprocessed with FSF 4.8.2).  Reproduces on the
brach head for me as well as on trunk.
Comment 8 Markus Trippelsdorf 2013-12-19 15:56:41 UTC
markus@x4 tmp % /usr/x86_64-pc-linux-gnu/gcc-bin/4.8.3/c++ -g -O2 BVH_Example.cpp
markus@x4 tmp % gdb ./a.out
Reading symbols from ./a.out...done.
(gdb) run
Starting program: /var/tmp/a.out 
Brute force distance = 0.00428018, calls = 10000

Program received signal SIGSEGV, Segmentation fault.
__unguarded_partition<__gnu_cxx::__normal_iterator<Eigen::internal::vector_int_pair<double, 2>*, std::vector<Eigen::internal::vector_int_pair<double, 2>, Eigen::aligned_allocator_indirection<Eigen::internal::vector_int_pair<double, 2> > > >, Eigen::internal::vector_int_pair<double, 2>, Eigen::KdBVH<double, 2, Eigen::Matrix<double, 2, 1> >::VectorComparator> (__comp=..., __pivot=..., __last=..., __first=<error reading variable: Cannot access memory at address 0x428000>)
    at /usr/lib/gcc/x86_64-pc-linux-gnu/4.8.3/include/g++-v4/bits/stl_algo.h:2263
2263              while (__comp(*__first, __pivot))
(gdb) bt
#0  __unguarded_partition<__gnu_cxx::__normal_iterator<Eigen::internal::vector_int_pair<double, 2>*, std::vector<Eigen::internal::vector_int_pair<double, 2>, Eigen::aligned_allocator_indirection<Eigen::internal::vector_int_pair<double, 2> > > >, Eigen::internal::vector_int_pair<double, 2>, Eigen::KdBVH<double, 2, Eigen::Matrix<double, 2, 1> >::VectorComparator> (__comp=..., __pivot=..., __last=..., __first=<error reading variable: Cannot access memory at address 0x428000>)
    at /usr/lib/gcc/x86_64-pc-linux-gnu/4.8.3/include/g++-v4/bits/stl_algo.h:2263
#1  __unguarded_partition_pivot<__gnu_cxx::__normal_iterator<Eigen::internal::vector_int_pair<double, 2>*, std::vector<Eigen::internal::vector_int_pair<double, 2>, Eigen::aligned_allocator_indirection<Eigen::internal::vector_int_pair<double, 2> > > >, Eigen::KdBVH<double, 2, Eigen::Matrix<double, 2, 1> >::VectorComparator> (__comp=..., 
    __last=..., __first=...) at /usr/lib/gcc/x86_64-pc-linux-gnu/4.8.3/include/g++-v4/bits/stl_algo.h:2296
#2  std::__introselect<__gnu_cxx::__normal_iterator<Eigen::internal::vector_int_pair<double, 2>*, std::vector<Eigen::internal::vector_int_pair<double, 2>, Eigen::aligned_allocator_indirection<Eigen::internal::vector_int_pair<double, 2> > > >, long, Eigen::KdBVH<double, 2, Eigen::Matrix<double, 2, 1, 0, 2, 1> >::VectorComparator> (__first=..., 
    __nth=..., __last=..., __depth_limit=2, __comp=__comp@entry=...) at /usr/lib/gcc/x86_64-pc-linux-gnu/4.8.3/include/g++-v4/bits/stl_algo.h:2394
#3  0x0000000000403d9d in nth_element<__gnu_cxx::__normal_iterator<Eigen::internal::vector_int_pair<double, 2>*, std::vector<Eigen::internal::vector_int_pair<double, 2>, Eigen::aligned_allocator_indirection<Eigen::internal::vector_int_pair<double, 2> > > >, Eigen::KdBVH<double, 2, Eigen::Matrix<double, 2, 1> >::VectorComparator> (__comp=..., 
    __last=..., __nth=..., __first=...) at /usr/lib/gcc/x86_64-pc-linux-gnu/4.8.3/include/g++-v4/bits/stl_algo.h:5417
#4  Eigen::KdBVH<double, 2, Eigen::Matrix<double, 2, 1, 0, 2, 1> >::build (this=this@entry=0x7fffffffe240, objCenters=std::vector of length 100, capacity 100 = {...}, 
    from=from@entry=68, to=to@entry=75, objBoxes=std::vector of length 100, capacity 100 = {...}, dim=dim@entry=0)
    at /home/abuild/rpmbuild/BUILD/eigen-eigen-ffa86ffb5570/unsupported/Eigen/src/BVH/KdBVH.h:203
#5  0x0000000000403df1 in Eigen::KdBVH<double, 2, Eigen::Matrix<double, 2, 1, 0, 2, 1> >::build (this=this@entry=0x7fffffffe240, 
    objCenters=std::vector of length 100, capacity 100 = {...}, from=from@entry=62, to=to@entry=75, objBoxes=std::vector of length 100, capacity 100 = {...}, 
    dim=dim@entry=1) at /home/abuild/rpmbuild/BUILD/eigen-eigen-ffa86ffb5570/unsupported/Eigen/src/BVH/KdBVH.h:207
#6  0x0000000000403df1 in Eigen::KdBVH<double, 2, Eigen::Matrix<double, 2, 1, 0, 2, 1> >::build (this=this@entry=0x7fffffffe240, 
    objCenters=std::vector of length 100, capacity 100 = {...}, from=from@entry=50, to=to@entry=75, objBoxes=std::vector of length 100, capacity 100 = {...}, 
    dim=dim@entry=0) at /home/abuild/rpmbuild/BUILD/eigen-eigen-ffa86ffb5570/unsupported/Eigen/src/BVH/KdBVH.h:207
#7  0x0000000000403dc5 in Eigen::KdBVH<double, 2, Eigen::Matrix<double, 2, 1, 0, 2, 1> >::build (this=this@entry=0x7fffffffe240, 
    objCenters=std::vector of length 100, capacity 100 = {...}, from=from@entry=50, to=to@entry=100, objBoxes=std::vector of length 100, capacity 100 = {...}, 
    dim=dim@entry=1) at /home/abuild/rpmbuild/BUILD/eigen-eigen-ffa86ffb5570/unsupported/Eigen/src/BVH/KdBVH.h:205
#8  0x0000000000403df1 in Eigen::KdBVH<double, 2, Eigen::Matrix<double, 2, 1, 0, 2, 1> >::build (this=this@entry=0x7fffffffe240, 
    objCenters=std::vector of length 100, capacity 100 = {...}, from=from@entry=0, to=to@entry=100, objBoxes=std::vector of length 100, capacity 100 = {...}, 
    dim=dim@entry=0) at /home/abuild/rpmbuild/BUILD/eigen-eigen-ffa86ffb5570/unsupported/Eigen/src/BVH/KdBVH.h:207
#9  0x00000000004044b2 in init<__gnu_cxx::__normal_iterator<Eigen::Matrix<double, 2, 1>*, std::vector<Eigen::Matrix<double, 2, 1>, Eigen::aligned_allocator_indirection<Eigen::Matrix<double, 2, 1> > > >, int> (boxEnd=0, boxBegin=0, end=..., begin=..., this=<optimized out>)
    at /home/abuild/rpmbuild/BUILD/eigen-eigen-ffa86ffb5570/unsupported/Eigen/src/BVH/KdBVH.h:119
#10 Eigen::KdBVH<double, 2, Eigen::Matrix<double, 2, 1, 0, 2, 1> >::KdBVH<__gnu_cxx::__normal_iterator<Eigen::Matrix<double, 2, 1, 0, 2, 1>*, std::vector<Eigen::Matrix<double, 2, 1, 0, 2, 1>, Eigen::aligned_allocator_indirection<Eigen::Matrix<double, 2, 1, 0, 2, 1> > > > > (this=0x7fffffffe240, begin=..., end=...)
    at /home/abuild/rpmbuild/BUILD/eigen-eigen-ffa86ffb5570/unsupported/Eigen/src/BVH/KdBVH.h:83
#11 0x0000000000401068 in main () at /home/abuild/rpmbuild/BUILD/eigen-eigen-ffa86ffb5570/unsupported/doc/examples/BVH_Example.cpp:47
(gdb)
Comment 9 Richard Biener 2013-12-19 16:00:00 UTC
Updating to r205857 on the branch helps.  Either a code-gen bug uncovered and now
fixed or maybe a dup of 58800.
Comment 10 Chris Jefferson 2013-12-19 16:03:54 UTC
It's a dup -- debian have backported the fix to 4.8.2.
Comment 11 Richard Biener 2013-12-19 16:12:35 UTC
Dup.

*** This bug has been marked as a duplicate of bug 58800 ***