Bug 486093

Summary: -Wstrict-aliasing in tests
Product: [Developer tools] valgrind Reporter: Sam James <sam>
Component: generalAssignee: Paul Floyd <pjfloyd>
Status: REPORTED ---    
Severity: normal CC: pjfloyd
Priority: NOR    
Version First Reported In: 3.22 GIT   
Target Milestone: ---   
Platform: Other   
OS: Other   
Latest Commit: Version Fixed In:
Sentry Crash Report:

Description Sam James 2024-04-24 23:00:16 UTC
THere's a lot of -Wstrict-aliasing instances in the testsuite. In Gentoo, we try to recommend people to use -Werror=strict-aliasing -Werror=lto-type-mismatch -Werror=some-other-bits when using LTO to help find likely runtime problems (Valgrind isn't LTO-compaitble for other reasons but that's neither here nor there for this).

When building Valgrind with such, we get:
```
x86_64-pc-linux-gnu-gcc -DHAVE_CONFIG_H -I. -I../../..  -I../../.. -I../../../include -I../../../coregrind -I../../../include -I../../../VEX/pub -I../../../VEX/pub -DVGA_amd64=1 -DVGO_l
inux=1 -DVGP_amd64_linux=1 -DVGPV_amd64_linux_vanilla=1 -DVGA_SEC_x86=1 -DVGP_SEC_amd64_linux=1   -Winline -Wall -Wshadow -Wno-long-long -g -fno-stack-protector   -m64  -O -ffast-math -
mfpmath=387 -mfancy-math-387 -O3 -march=native -mtls-dialect=gnu2 -fno-semantic-interposition -pipe -fcf-protection=none -fdiagnostics-color=always -fdiagnostics-urls=never -frecord-gcc
-switches -Wa,-O2 -Wa,-mtune=znver2 -Wstrict-aliasing -Wfree-nonheap-object -Werror=lto-type-mismatch -Werror=strict-aliasing -Werror=odr -Wstrict-aliasing -Wfree-nonheap-object -Werror
=lto-type-mismatch -Werror=strict-aliasing -Werror=odr -Wbuiltin-declaration-mismatch -ggdb3 -Wformat -Wformat-security -Waddress -Warray-bounds -Wfree-nonheap-object -Wint-to-pointer-c
ast -Wmain -Wnonnull -Wodr -Wreturn-type -Wsizeof-pointer-memaccess -Wstrict-aliasing -Wstring-compare -Wuninitialized -Wvarargs -fno-stack-protector -fno-harden-control-flow-redundancy
 -c -o more_x87_fp-more_x87_fp.o `test -f 'more_x87_fp.c' || echo './'`more_x87_fp.c
make[5]: 'shr_edx' is up to date.
more_x87_fp.c: In function ‘test_fcvt’:
more_x87_fp.c:108:28: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing]
  108 |     printf("a=%016llx\n", *(long long *)&a);
      |                            ^~~~~~~~~~~~~~~
more_x87_fp.c:109:34: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing]
  109 |     printf("la=%016llx %04x\n", *(long long *)&la,
      |                                  ^~~~~~~~~~~~~~~~
more_x87_fp.c:110:12: warning: ‘la’ is used uninitialized [-Wuninitialized]
  110 |            *(unsigned short *)((char *)(&la) + 8));
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
more_x87_fp.c:96:17: note: ‘la’ declared here
   96 |     long double la;
      |                 ^~
cc1: some warnings being treated as errors
x86_64-pc-linux-gnu-gcc -DHAVE_CONFIG_H -I. -I../../..  -I../../.. -I../../../include -I../../../coregrind -I../../../include -I../../../VEX/pub -I../../../VEX/pub -DVGA_amd64=1 -DVGO_l
inux=1 -DVGP_amd64_linux=1 -DVGPV_amd64_linux_vanilla=1 -DVGA_SEC_x86=1 -DVGP_SEC_amd64_linux=1   -Winline -Wall -Wshadow -Wno-long-long -g -fno-stack-protector   -m32 -mmmx -msse -O3 -
march=native -mtls-dialect=gnu2 -fno-semantic-interposition -pipe -fcf-protection=none -fdiagnostics-color=always -fdiagnostics-urls=never -frecord-gcc-switches -Wa,-O2 -Wa,-mtune=znver
2 -Wstrict-aliasing -Wfree-nonheap-object -Werror=lto-type-mismatch -Werror=strict-aliasing -Werror=odr -Wstrict-aliasing -Wfree-nonheap-object -Werror=lto-type-mismatch -Werror=strict-
aliasing -Werror=odr -Wbuiltin-declaration-mismatch -ggdb3 -Wformat -Wformat-security -Waddress -Warray-bounds -Wfree-nonheap-object -Wint-to-pointer-cast -Wmain -Wnonnull -Wodr -Wretur
n-type -Wsizeof-pointer-memaccess -Wstrict-aliasing -Wstring-compare -Wuninitialized -Wvarargs -fno-stack-protector -fno-harden-control-flow-redundancy -c -o more_x86_fp.o more_x86_fp.c
more_x86_fp.c: In function ‘test_fcvt’:
more_x86_fp.c:98:28: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing]
   98 |     printf("a=%016llx\n", *(long long *)&a);
      |                            ^~~~~~~~~~~~~~~
more_x86_fp.c:99:34: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing]
   99 |     printf("la=%016llx %04x\n", *(long long *)&la,
      |                                  ^~~~~~~~~~~~~~~~
more_x86_fp.c:100:12: warning: ‘la’ is used uninitialized [-Wuninitialized]
  100 |            *(unsigned short *)((char *)(&la) + 8));
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
more_x86_fp.c:86:17: note: ‘la’ declared here
   86 |     long double la;
      |                 ^~
cc1: some warnings being treated as errors
```

This is with `gcc version 14.0.1 20240423 (experimental) 0c8e99e5c32be9f2604f3c330814993f29818037 (Gentoo Hardened 14.0.9999 p, commit 257336021ebdc63cdd390487506f23d83f04ab91)` but I've been seeing this for a while and just slacked on reporting it.
Comment 1 Sam James 2024-04-24 23:01:41 UTC
As a workaround, we're building tests with:
```
emake CFLAGS="${CFLAGS} -fno-strict-aliasing" LDFLAGS="${LDFLAGS} -Wl,-z,notext" check
```

for now.
Comment 2 Paul Floyd 2024-04-25 05:09:05 UTC
There is of course a load of intentional UB in the test cases. But this looks unintentional. The risk is that compilers start to exploit the UB for optimization and the code then breaks.

Rather than using illegal type casts the code should use memcpy (or even better std::bit_cast but that requires C++20).