Heads up: gcc miscompiling initramfs zlib decompression code at -O3

From: Vineet Gupta
Date: Fri Apr 30 2021 - 16:46:26 EST


Hi,

I've hit a mainline gcc 10.2 (also gcc 9.3) bug which triggers at -O3
causing wrong codegen.

Config needs to have initramfs + gzip compressed.

CONFIG_HAVE_KERNEL_GZIP=y
CONFIG_KERNEL_GZIP=y
CONFIG_DECOMPRESS_GZIP=y
CONFIG_INITRAMFS_COMPRESSION_GZIP=y

lib/zlib_inflate/inffast.c

if (dist > 2) {
unsigned short *sfrom;

sfrom = (unsigned short *)(from);
loops = len >> 1;
do
*sout++ = *sfrom++;
^^^^^^ ^^^^^^^^
while (--loops);
out = (unsigned char *)sout;
from = (unsigned char *)sfrom;
}
...

The gist of issue is that despite use of unsigned short pointers, gcc is
generating wider load/stores (8-byte LDD/STD on arcv2 and 16-byte on
aarch64) causing extraneous bytes to copied into inflated gzip binaries
manifesting later as corrupted fragments in the binaries.

I've opened a gcc bug at:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100363

The workaround is to build lib/zlib_inflate/inffast.c with -O2, although
I reckon not many arches build with -O3 as default. I'll be proposing an
ARC only patch to build this file with -O2, unless people think it needs
to be generalized.

Also problem originally seen on 5.6 kernel, although I confirm it shows
on latest mainline as well.

Unraveling this pretty fun, gory details for those interested at:


https://github.com/foss-for-synopsys-dwc-arc-processors/toolchain/issues/372


Thx,
-Vineet