public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/105400] New: g++-11 regression produces -Warray-bounds false positive warning with -O2
@ 2022-04-26 23:43 laurent.pinchart at ideasonboard dot com
  2022-04-27  0:09 ` [Bug middle-end/105400] " pinskia at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: laurent.pinchart at ideasonboard dot com @ 2022-04-26 23:43 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105400

            Bug ID: 105400
           Summary: g++-11 regression produces -Warray-bounds false
                    positive warning with -O2
           Product: gcc
           Version: 11.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: laurent.pinchart at ideasonboard dot com
  Target Milestone: ---

Created attachment 52887
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=52887&action=edit
Test case

Starting with g++ 11, we noticed a -Warray-bounds warning when compiling the
libcamera project. I've narrowed it down to the attached standalone test case
(without any #include).

The warning is produced with the following g++ options:

$ g++-11.2.1 -v -save-temps -W -Wall -std=c++17 -O2 -c -o cast2.o cast2.cpp
Using built-in specs.
COLLECT_GCC=g++-11.2.1
Target: x86_64-pc-linux-gnu
Configured with:
/var/tmp/portage/sys-devel/gcc-11.2.1_p20220115/work/gcc-11-20220115/configure
--host=x86_64-pc-linux-gnu --build=x86_64-pc-linux-gnu --prefix=/usr
--bindir=/usr/x86_64-pc-linux-gnu/gcc-bin/11.2.1
--includedir=/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/include
--datadir=/usr/share/gcc-data/x86_64-pc-linux-gnu/11.2.1
--mandir=/usr/share/gcc-data/x86_64-pc-linux-gnu/11.2.1/man
--infodir=/usr/share/gcc-data/x86_64-pc-linux-gnu/11.2.1/info
--with-gxx-include-dir=/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/include/g++-v11
--with-python-dir=/share/gcc-data/x86_64-pc-linux-gnu/11.2.1/python
--enable-languages=c,c++,fortran --enable-obsolete --enable-secureplt
--disable-werror --with-system-zlib --enable-nls --without-included-gettext
--disable-libunwind-exceptions --enable-checking=yes
--with-bugurl=https://bugs.gentoo.org/ --with-pkgversion='Gentoo
11.2.1_p20220115 p4' --disable-esp --enable-libstdcxx-time
--disable-libstdcxx-pch --enable-shared --enable-threads=posix
--enable-__cxa_atexit --enable-clocale=gnu --enable-multilib
--with-multilib-list=m32,m64 --disable-fixed-point --enable-targets=all
--enable-libgomp --disable-libssp --disable-libada --disable-cet
--disable-systemtap --disable-valgrind-annotations --disable-vtable-verify
--disable-libvtv --without-zstd --enable-lto --with-isl
--disable-isl-version-check --enable-default-pie --enable-default-ssp
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 11.2.1 20220115 (Gentoo 11.2.1_p20220115 p4) 
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wextra' '-Wall' '-std=c++17' '-O2'
'-c' '-o' 'cast2.o' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/libexec/gcc/x86_64-pc-linux-gnu/11.2.1/cc1plus -E -quiet -v -D_GNU_SOURCE
cast2.cpp -mtune=generic -march=x86-64 -std=c++17 -Wextra -Wall -O2
-fpch-preprocess -o cast2.ii
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/../../../../x86_64-pc-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/include/g++-v11
 /usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/include/g++-v11/x86_64-pc-linux-gnu
 /usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/include/g++-v11/backward
 /usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/include
 /usr/local/include
 /usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/include-fixed
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wextra' '-Wall' '-std=c++17' '-O2'
'-c' '-o' 'cast2.o' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/libexec/gcc/x86_64-pc-linux-gnu/11.2.1/cc1plus -fpreprocessed cast2.ii
-quiet -dumpbase cast2.cpp -dumpbase-ext .cpp -mtune=generic -march=x86-64 -O2
-Wextra -Wall -std=c++17 -version -o cast2.s
GNU C++17 (Gentoo 11.2.1_p20220115 p4) version 11.2.1 20220115
(x86_64-pc-linux-gnu)
        compiled by GNU C version 11.2.1 20220115, GMP version 6.2.1, MPFR
version 4.1.0-p13, MPC version 1.2.1, isl version isl-0.24-GMP

GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096
GNU C++17 (Gentoo 11.2.1_p20220115 p4) version 11.2.1 20220115
(x86_64-pc-linux-gnu)
        compiled by GNU C version 11.2.1 20220115, GMP version 6.2.1, MPFR
version 4.1.0-p13, MPC version 1.2.1, isl version isl-0.24-GMP

GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096
Compiler executable checksum: 8c115abe653dbf0f6d87498ebb5d37f4
cast2.cpp: In function ‘void func()’:
cast2.cpp:25:26: warning: array subscript ‘Derived[0]’ is partly outside array
bounds of ‘Base [1]’ [-Warray-bounds]
   25 |                 d->count = 0; 
      |                 ~~~~~~~~~^~~
cast2.cpp:36:14: note: while referencing ‘b’
   36 |         Base b(Base::Type2); 
      |              ^
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wextra' '-Wall' '-std=c++17' '-O2'
'-c' '-o' 'cast2.o' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/../../../../x86_64-pc-linux-gnu/bin/as
-v --64 -o cast2.o cast2.s
GNU assembler version 2.37 (x86_64-pc-linux-gnu) using BFD version (Gentoo
2.37_p1 p2) 2.37
COMPILER_PATH=/usr/libexec/gcc/x86_64-pc-linux-gnu/11.2.1/:/usr/libexec/gcc/x86_64-pc-linux-gnu/11.2.1/:/usr/libexec/gcc/x86_64-pc-linux-gnu/:/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/:/usr/lib/gcc/x86_64-pc-linux-gnu/:/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/../../../../x86_64-pc-linux-gnu/bin/
LIBRARY_PATH=/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/:/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/../../../../x86_64-pc-linux-gnu/lib/:/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wextra' '-Wall' '-std=c++17' '-O2'
'-c' '-o' 'cast2.o' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
'-dumpdir' 'cast2.'


The out of bounds access is prevented by the type() check. The compiler can't
predict the outcome of the check as it doesn't know that type() will return the
type passed to the Base constructor.

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

* [Bug middle-end/105400] g++-11 regression produces -Warray-bounds false positive warning with -O2
  2022-04-26 23:43 [Bug c++/105400] New: g++-11 regression produces -Warray-bounds false positive warning with -O2 laurent.pinchart at ideasonboard dot com
@ 2022-04-27  0:09 ` pinskia at gcc dot gnu.org
  2022-04-27  0:17 ` laurent.pinchart at ideasonboard dot com
  2022-04-29 22:15 ` laurent.pinchart at ideasonboard dot com
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-04-27  0:09 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105400

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This is just inlining and there is no way for GCC to know that the method type
will return Type1 rather than Type2 here for the variable b.

The warning is correct but only because of optimizations and such.

Do you have the original preprocessed source? It might be the case you over
simplified the testcase really.

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

* [Bug middle-end/105400] g++-11 regression produces -Warray-bounds false positive warning with -O2
  2022-04-26 23:43 [Bug c++/105400] New: g++-11 regression produces -Warray-bounds false positive warning with -O2 laurent.pinchart at ideasonboard dot com
  2022-04-27  0:09 ` [Bug middle-end/105400] " pinskia at gcc dot gnu.org
@ 2022-04-27  0:17 ` laurent.pinchart at ideasonboard dot com
  2022-04-29 22:15 ` laurent.pinchart at ideasonboard dot com
  2 siblings, 0 replies; 4+ messages in thread
From: laurent.pinchart at ideasonboard dot com @ 2022-04-27  0:17 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105400

--- Comment #2 from Laurent Pinchart <laurent.pinchart at ideasonboard dot com> ---
Thank you Andrew for the very quick answer!

The original code is available at
https://git.libcamera.org/libcamera/libcamera.git/tree/src/libcamera/base/object.cpp
(the process() function is Object::message(), and the func() function is
Object::notifyThreadMove()), with the corresponding header in
https://git.libcamera.org/libcamera/libcamera.git/tree/include/libcamera/base/object.h.

I agree that gcc can't know that b->type() will return Type1, but is it right
to assume that this code construct deserves a warning ? If so, what would be
the right code construct from gcc's point of view to achieve the same without a
warning ? We strive to have a build-free warning in the libcamera project.

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

* [Bug middle-end/105400] g++-11 regression produces -Warray-bounds false positive warning with -O2
  2022-04-26 23:43 [Bug c++/105400] New: g++-11 regression produces -Warray-bounds false positive warning with -O2 laurent.pinchart at ideasonboard dot com
  2022-04-27  0:09 ` [Bug middle-end/105400] " pinskia at gcc dot gnu.org
  2022-04-27  0:17 ` laurent.pinchart at ideasonboard dot com
@ 2022-04-29 22:15 ` laurent.pinchart at ideasonboard dot com
  2 siblings, 0 replies; 4+ messages in thread
From: laurent.pinchart at ideasonboard dot com @ 2022-04-29 22:15 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105400

--- Comment #3 from Laurent Pinchart <laurent.pinchart at ideasonboard dot com> ---
Created attachment 52911
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=52911&action=edit
Original preprocessed sources

Here's the full preprocessed source, corresponding to
https://git.libcamera.org/libcamera/libcamera.git/tree/src/libcamera/base/object.cpp,
generated with

$ g++-11.2.1 -v -save-temps -Isrc/libcamera/base/libcamera-base.so.0.0.0.p
-Isrc/libcamera/base -I../../src/libcamera/base -Iinclude -I../../include
-fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch
-Wnon-virtual-dtor -Wextra -Werror -std=c++17 -O3 -Wshadow -include config.h
-fPIC -pthread -DLIBCAMERA_BASE_PRIVATE -MD -MQ
src/libcamera/base/libcamera-base.so.0.0.0.p/object.cpp.o -MF
src/libcamera/base/libcamera-base.so.0.0.0.p/object.cpp.o.d -o
src/libcamera/base/libcamera-base.so.0.0.0.p/object.cpp.o -c
../../src/libcamera/base/object.cpp
Using built-in specs.
COLLECT_GCC=g++-11.2.1
Target: x86_64-pc-linux-gnu
Configured with:
/var/tmp/portage/sys-devel/gcc-11.2.1_p20220115/work/gcc-11-20220115/configure
--host=x86_64-pc-linux-gnu --build=x86_64-pc-linux-gnu --prefix=/usr
--bindir=/usr/x86_64-pc-linux-gnu/gcc-bin/11.2.1
--includedir=/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/include
--datadir=/usr/share/gcc-data/x86_64-pc-linux-gnu/11.2.1
--mandir=/usr/share/gcc-data/x86_64-pc-linux-gnu/11.2.1/man
--infodir=/usr/share/gcc-data/x86_64-pc-linux-gnu/11.2.1/info
--with-gxx-include-dir=/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/include/g++-v11
--with-python-dir=/share/gcc-data/x86_64-pc-linux-gnu/11.2.1/python
--enable-languages=c,c++,fortran --enable-obsolete --enable-secureplt
--disable-werror --with-system-zlib --enable-nls --without-included-gettext
--disable-libunwind-exceptions --enable-checking=yes
--with-bugurl=https://bugs.gentoo.org/ --with-pkgversion='Gentoo
11.2.1_p20220115 p4' --disable-esp --enable-libstdcxx-time
--disable-libstdcxx-pch --enable-shared --enable-threads=posix
--enable-__cxa_atexit --enable-clocale=gnu --enable-multilib
--with-multilib-list=m32,m64 --disable-fixed-point --enable-targets=all
--enable-libgomp --disable-libssp --disable-libada --disable-cet
--disable-systemtap --disable-valgrind-annotations --disable-vtable-verify
--disable-libvtv --without-zstd --enable-lto --with-isl
--disable-isl-version-check --enable-default-pie --enable-default-ssp
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 11.2.1 20220115 (Gentoo 11.2.1_p20220115 p4) 
COLLECT_GCC_OPTIONS='-fdiagnostics-color=always' '-v' '-save-temps' '-I'
'src/libcamera/base/libcamera-base.so.0.0.0.p' '-I' 'src/libcamera/base' '-I'
'../../src/libcamera/base' '-I' 'include' '-I' '../../include' '-D'
'_FILE_OFFSET_BITS=64' '-Wall' '-Winvalid-pch' '-Wnon-virtual-dtor' '-Wextra'
'-Werror' '-std=c++17' '-O3' '-Wshadow' '-include' 'config.h' '-fPIC'
'-pthread' '-D' 'LIBCAMERA_BASE_PRIVATE' '-MD' '-MQ'
'src/libcamera/base/libcamera-base.so.0.0.0.p/object.cpp.o' '-MF'
'src/libcamera/base/libcamera-base.so.0.0.0.p/object.cpp.o.d' '-o'
'src/libcamera/base/libcamera-base.so.0.0.0.p/object.cpp.o' '-c'
'-shared-libgcc' '-mtune=generic' '-march=x86-64' '-dumpdir'
'src/libcamera/base/libcamera-base.so.0.0.0.p/'
 /usr/libexec/gcc/x86_64-pc-linux-gnu/11.2.1/cc1plus -E -quiet -v -I
src/libcamera/base/libcamera-base.so.0.0.0.p -I src/libcamera/base -I
../../src/libcamera/base -I include -I ../../include -MD
src/libcamera/base/libcamera-base.so.0.0.0.p/object.cpp.d -MF
src/libcamera/base/libcamera-base.so.0.0.0.p/object.cpp.o.d -MQ
src/libcamera/base/libcamera-base.so.0.0.0.p/object.cpp.o -D_GNU_SOURCE
-D_REENTRANT -D _FILE_OFFSET_BITS=64 -D LIBCAMERA_BASE_PRIVATE -include
config.h ../../src/libcamera/base/object.cpp -mtune=generic -march=x86-64
-std=c++17 -Wall -Winvalid-pch -Wnon-virtual-dtor -Wextra -Werror -Wshadow
-fdiagnostics-color=always -fPIC -O3 -fpch-preprocess -o
src/libcamera/base/libcamera-base.so.0.0.0.p/object.cpp.ii
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/../../../../x86_64-pc-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 src/libcamera/base/libcamera-base.so.0.0.0.p
 src/libcamera/base
 ../../src/libcamera/base
 include
 ../../include
 /usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/include/g++-v11
 /usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/include/g++-v11/x86_64-pc-linux-gnu
 /usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/include/g++-v11/backward
 /usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/include
 /usr/local/include
 /usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/include-fixed
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-fdiagnostics-color=always' '-v' '-save-temps' '-I'
'src/libcamera/base/libcamera-base.so.0.0.0.p' '-I' 'src/libcamera/base' '-I'
'../../src/libcamera/base' '-I' 'include' '-I' '../../include' '-D'
'_FILE_OFFSET_BITS=64' '-Wall' '-Winvalid-pch' '-Wnon-virtual-dtor' '-Wextra'
'-Werror' '-std=c++17' '-O3' '-Wshadow' '-include' 'config.h' '-fPIC'
'-pthread' '-D' 'LIBCAMERA_BASE_PRIVATE' '-MD' '-MQ'
'src/libcamera/base/libcamera-base.so.0.0.0.p/object.cpp.o' '-MF'
'src/libcamera/base/libcamera-base.so.0.0.0.p/object.cpp.o.d' '-o'
'src/libcamera/base/libcamera-base.so.0.0.0.p/object.cpp.o' '-c'
'-shared-libgcc' '-mtune=generic' '-march=x86-64' '-dumpdir'
'src/libcamera/base/libcamera-base.so.0.0.0.p/'
 /usr/libexec/gcc/x86_64-pc-linux-gnu/11.2.1/cc1plus -fpreprocessed
src/libcamera/base/libcamera-base.so.0.0.0.p/object.cpp.ii -quiet -dumpdir
src/libcamera/base/libcamera-base.so.0.0.0.p/ -dumpbase object.cpp.cpp
-dumpbase-ext .cpp -mtune=generic -march=x86-64 -O3 -Wall -Winvalid-pch
-Wnon-virtual-dtor -Wextra -Werror -Wshadow -std=c++17 -version
-fdiagnostics-color=always -fPIC -o
src/libcamera/base/libcamera-base.so.0.0.0.p/object.cpp.s
GNU C++17 (Gentoo 11.2.1_p20220115 p4) version 11.2.1 20220115
(x86_64-pc-linux-gnu)
        compiled by GNU C version 11.2.1 20220115, GMP version 6.2.1, MPFR
version 4.1.0-p13, MPC version 1.2.1, isl version isl-0.24-GMP

GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096
GNU C++17 (Gentoo 11.2.1_p20220115 p4) version 11.2.1 20220115
(x86_64-pc-linux-gnu)
        compiled by GNU C version 11.2.1 20220115, GMP version 6.2.1, MPFR
version 4.1.0-p13, MPC version 1.2.1, isl version isl-0.24-GMP

GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096
Compiler executable checksum: 8c115abe653dbf0f6d87498ebb5d37f4
In file included from ../../src/libcamera/base/object.cpp:13:
../../include/libcamera/base/message.h: In member function ‘void
libcamera::Object::notifyThreadMove()’:
../../include/libcamera/base/message.h:58:47: error: array subscript ‘const
libcamera::InvokeMessage[0]’ is partly outside array bounds of
‘libcamera::Message [1]’ [-Werror=array-bounds]
   58 |         Semaphore *semaphore() const { return semaphore_; }
      |                                               ^~~~~~~~~~
../../src/libcamera/base/object.cpp:280:17: note: while referencing ‘msg’
  280 |         Message msg(Message::ThreadMoveMessage);
      |                 ^~~
cc1plus: all warnings being treated as errors

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

end of thread, other threads:[~2022-04-29 22:15 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-26 23:43 [Bug c++/105400] New: g++-11 regression produces -Warray-bounds false positive warning with -O2 laurent.pinchart at ideasonboard dot com
2022-04-27  0:09 ` [Bug middle-end/105400] " pinskia at gcc dot gnu.org
2022-04-27  0:17 ` laurent.pinchart at ideasonboard dot com
2022-04-29 22:15 ` laurent.pinchart at ideasonboard dot com

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).