public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/115096] New: -fstrict-aliasing miscompilation or missing warning
@ 2024-05-14 21:25 Philip.Taff at itron dot com
  2024-05-14 21:54 ` [Bug c/115096] " pinskia at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Philip.Taff at itron dot com @ 2024-05-14 21:25 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 115096
           Summary: -fstrict-aliasing miscompilation or missing warning
           Product: gcc
           Version: 12.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: Philip.Taff at itron dot com
  Target Milestone: ---

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

I'm not sure whether this code is a violation of strict-aliasing, in which case
a warning would be great, or it is valid, in which case it is being
miscompiled, removing some of the increments. According to Bug #29901 a similar
cast does break strict-aliasing rules, but the warning described in that bug is
not occurring. 

Built with:
$ gcc -O1 -fstrict-aliasing -Wall -Wextra test.c

No compiler warnings or errors.

Executed with: ("ab." is the test data)
$ ./a.out ab.

GCC/system information:
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc-12
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/12/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu
12.3.0-1ubuntu1~22.04' --with-bugurl=file:///usr/share/doc/gcc-12/README.Bugs
--enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++,m2 --prefix=/usr
--with-gcc-major-version-only --program-suffix=-12
--program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug
--enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new
--enable-gnu-unique-object --disable-vtable-verify --enable-plugin
--enable-default-pie --with-system-zlib --enable-libphobos-checking=release
--with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch
--disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64
--with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic
--enable-offload-targets=nvptx-none=/build/gcc-12-ALHxjy/gcc-12-12.3.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-12-ALHxjy/gcc-12-12.3.0/debian/tmp-gcn/usr
--enable-offload-defaulted --without-cuda-driver --enable-checking=release
--build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.3.0 (Ubuntu 12.3.0-1ubuntu1~22.04)
COLLECT_GCC_OPTIONS='-O1' '-fstrict-aliasing' '-Wall' '-Wextra' '-v'
'-save-temps' '-mtune=generic' '-march=x86-64' '-dumpdir' 'a-'
/usr/lib/gcc/x86_64-linux-gnu/12/cc1 -E -quiet -v -imultiarch x86_64-linux-gnu
test.c -mtune=generic -march=x86-64 -Wall -Wextra -fstrict-aliasing -O1
-fpch-preprocess -fasynchronous-unwind-tables -fstack-protector-strong
-Wformat-security -fstack-clash-protection -fcf-protection -o a-test.i
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/12/include-fixed"
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-linux-gnu/12/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/lib/gcc/x86_64-linux-gnu/12/include
/usr/local/include
/usr/include/x86_64-linux-gnu
/usr/include
End of search list.
COLLECT_GCC_OPTIONS='-O1' '-fstrict-aliasing' '-Wall' '-Wextra' '-v'
'-save-temps' '-mtune=generic' '-march=x86-64' '-dumpdir' 'a-'
/usr/lib/gcc/x86_64-linux-gnu/12/cc1 -fpreprocessed a-test.i -quiet -dumpdir a-
-dumpbase test.c -dumpbase-ext .c -mtune=generic -march=x86-64 -O1 -Wall
-Wextra -version -fstrict-aliasing -fasynchronous-unwind-tables
-fstack-protector-strong -Wformat-security -fstack-clash-protection
-fcf-protection -o a-test.s
GNU C17 (Ubuntu 12.3.0-1ubuntu1~22.04) version 12.3.0 (x86_64-linux-gnu)
        compiled by GNU C version 12.3.0, GMP version 6.2.1, MPFR version
4.1.0, MPC version 1.2.1, isl version isl-0.24-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU C17 (Ubuntu 12.3.0-1ubuntu1~22.04) version 12.3.0 (x86_64-linux-gnu)
        compiled by GNU C version 12.3.0, GMP version 6.2.1, MPFR version
4.1.0, MPC version 1.2.1, isl version isl-0.24-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: d93534411cccf908c6f3926ba41cb359
COLLECT_GCC_OPTIONS='-O1' '-fstrict-aliasing' '-Wall' '-Wextra' '-v'
'-save-temps' '-mtune=generic' '-march=x86-64' '-dumpdir' 'a-'
as -v --64 -o a-test.o a-test.s
GNU assembler version 2.38 (x86_64-linux-gnu) using BFD version (GNU Binutils
for Ubuntu) 2.38
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/12/:/usr/lib/gcc/x86_64-linux-gnu/12/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/12/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/12/:/usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/12/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/12/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-O1' '-fstrict-aliasing' '-Wall' '-Wextra' '-v'
'-save-temps' '-mtune=generic' '-march=x86-64' '-dumpdir' 'a.'
/usr/lib/gcc/x86_64-linux-gnu/12/collect2 -plugin
/usr/lib/gcc/x86_64-linux-gnu/12/liblto_plugin.so
-plugin-opt=/usr/lib/gcc/x86_64-linux-gnu/12/lto-wrapper
-plugin-opt=-fresolution=a.res -plugin-opt=-pass-through=-lgcc
-plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc
-plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s --build-id
--eh-frame-hdr -m elf_x86_64 --hash-style=gnu --as-needed -dynamic-linker
/lib64/ld-linux-x86-64.so.2 -pie -z now -z relro
/usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu/Scrt1.o
/usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu/crti.o
/usr/lib/gcc/x86_64-linux-gnu/12/crtbeginS.o -L/usr/lib/gcc/x86_64-linux-gnu/12
-L/usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu
-L/usr/lib/gcc/x86_64-linux-gnu/12/../../../../lib -L/lib/x86_64-linux-gnu
-L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib
-L/usr/lib/gcc/x86_64-linux-gnu/12/../../.. a-test.o -lgcc --push-state
--as-needed -lgcc_s --pop-state -lc -lgcc --push-state --as-needed -lgcc_s
--pop-state /usr/lib/gcc/x86_64-linux-gnu/12/crtendS.o
/usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu/crtn.o
COLLECT_GCC_OPTIONS='-O1' '-fstrict-aliasing' '-Wall' '-Wextra' '-v'
'-save-temps' '-mtune=generic' '-march=x86-64' '-dumpdir' 'a.'

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

* [Bug c/115096] -fstrict-aliasing miscompilation or missing warning
  2024-05-14 21:25 [Bug c/115096] New: -fstrict-aliasing miscompilation or missing warning Philip.Taff at itron dot com
@ 2024-05-14 21:54 ` pinskia at gcc dot gnu.org
  2024-05-14 21:57 ` pinskia at gcc dot gnu.org
  2024-06-11 15:29 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-05-14 21:54 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
So this is not a miscompiling as the pointer types "char*" and "unsigned char*"
are two distant pointer types and are not considered to be aliasing and you are
not accessing the pointer via a character type (GCC has an exception for void*
types though which you could do the store in and get the correct result).

There is no warning here because the casting between pointer types is not the
issue but rather the access.

Since test is passed a pointer to a "char*" type and only in test you do the
cast to a pointer to a "unsigned char*" type. a warning in this case would 100%
require full path analysis of the code.

Note you could rewrite the code to be:
```
static int helper(void **cursor)
{
    // first character ('a') is OK
    if (*(unsigned char*)*cursor > 'z')
    {
        return -11;
    }
    *cursor = ((unsigned char*)*cursor) + 1;
    // second character ('b') is OK
    if (*(unsigned char*)*cursor > 'z')
    {
        return -12;
    }
    // increment to third character ('.')...
    *cursor = ((unsigned char*)*cursor) + 1;
    return 0;
}
```

And it would work correctly as I mentioned GCC has an exception on the `void*`
type which is allowed to alias all other pointer types. (Note I am not 100%
sure if C standard says that is still undefined code or not but GCC makes it as
such).

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

* [Bug c/115096] -fstrict-aliasing miscompilation or missing warning
  2024-05-14 21:25 [Bug c/115096] New: -fstrict-aliasing miscompilation or missing warning Philip.Taff at itron dot com
  2024-05-14 21:54 ` [Bug c/115096] " pinskia at gcc dot gnu.org
@ 2024-05-14 21:57 ` pinskia at gcc dot gnu.org
  2024-06-11 15:29 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-05-14 21:57 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
> the warning described in that bug is not occurring. 

That is because in that case it had literally `(unsigned char**)&var` while in
this case it is spread across function calls and that would require a lot of
work to get the warning happening.

Note there are a few open bug reports specifically asking for a runtime
catching of this too; but that is also hard as you need to record the aliasing
set of when stores and loads happen.

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

* [Bug c/115096] -fstrict-aliasing miscompilation or missing warning
  2024-05-14 21:25 [Bug c/115096] New: -fstrict-aliasing miscompilation or missing warning Philip.Taff at itron dot com
  2024-05-14 21:54 ` [Bug c/115096] " pinskia at gcc dot gnu.org
  2024-05-14 21:57 ` pinskia at gcc dot gnu.org
@ 2024-06-11 15:29 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-11 15:29 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |Explorer09 at gmail dot com

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 115437 has been marked as a duplicate of this bug. ***

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

end of thread, other threads:[~2024-06-11 15:29 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-14 21:25 [Bug c/115096] New: -fstrict-aliasing miscompilation or missing warning Philip.Taff at itron dot com
2024-05-14 21:54 ` [Bug c/115096] " pinskia at gcc dot gnu.org
2024-05-14 21:57 ` pinskia at gcc dot gnu.org
2024-06-11 15:29 ` pinskia at gcc dot gnu.org

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