Bug 105400 - g++-11 regression produces -Warray-bounds false positive warning with -O2
Summary: g++-11 regression produces -Warray-bounds false positive warning with -O2
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 11.2.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: Warray-bounds
  Show dependency treegraph
 
Reported: 2022-04-26 23:43 UTC by Laurent Pinchart
Modified: 2022-04-29 22:15 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
Test case (1.64 KB, text/x-modelica)
2022-04-26 23:43 UTC, Laurent Pinchart
Details
Original preprocessed sources (236.49 KB, application/gzip)
2022-04-29 22:15 UTC, Laurent Pinchart
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Laurent Pinchart 2022-04-26 23:43:09 UTC
Created attachment 52887 [details]
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.
Comment 1 Andrew Pinski 2022-04-27 00:09:27 UTC
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.
Comment 2 Laurent Pinchart 2022-04-27 00:17:41 UTC
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.
Comment 3 Laurent Pinchart 2022-04-29 22:15:27 UTC
Created attachment 52911 [details]
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