linux-stable/tools
Willy Tarreau e8f1633d2d tools/nolibc: Fix missing strlen() definition and infinite loop with gcc-12
[ Upstream commit bfc3b0f056 ]

When built at -Os, gcc-12 recognizes an strlen() pattern in nolibc_strlen()
and replaces it with a jump to strlen(), which is not defined as a symbol
and breaks compilation. Worse, when the function is called strlen(), the
function is simply replaced with a jump to itself, hence becomes an
infinite loop.

One way to avoid this is to always set -ffreestanding, but the calling
code doesn't know this and there's no way (either via attributes or
pragmas) to globally enable it from include files, effectively leaving
a painful situation for the caller.

Alexey suggested to place an empty asm() statement inside the loop to
stop gcc from recognizing a well-known pattern, which happens to work
pretty fine. At least it allows us to make sure our local definition
is not replaced with a self jump.

The function only needs to be renamed back to strlen() so that the symbol
exists, which implies that nolibc_strlen() which is used on variable
strings has to be declared as a macro that points back to it before the
strlen() macro is redifined.

It was verified to produce valid code with gcc 3.4 to 12.1 at different
optimization levels, and both with constant and variable strings.

In case this problem surfaces again in the future, an alternate approach
consisting in adding an optimize("no-tree-loop-distribute-patterns")
function attribute for gcc>=12 worked as well but is less pretty.

Reported-by: kernel test robot <yujie.liu@intel.com>
Link: https://lore.kernel.org/r/202210081618.754a77db-yujie.liu@intel.com
Fixes: 66b6f755ad ("rcutorture: Import a copy of nolibc")
Fixes: 96980b833a ("tools/nolibc/string: do not use __builtin_strlen() at -O0")
Cc: "Paul E. McKenney" <paulmck@kernel.org>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Signed-off-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
2022-11-10 18:17:16 +01:00
..
accounting delayacct: remove some unused variables 2022-06-16 19:58:21 -07:00
arch tools headers cpufeatures: Sync with the kernel sources 2022-09-21 16:08:00 -03:00
bootconfig
bpf bpftool: Clear errno after libcap's checks 2022-10-21 12:39:05 +02:00
build tools build: Switch to new openssl API for test-libcrypto 2022-08-10 10:44:02 -03:00
certs
cgroup tools: add memcg_shrinker.py 2022-07-03 18:08:40 -07:00
counter
debugging tools: Add new "test" taint to kernel-chktaint 2022-09-07 14:51:12 -06:00
edid
firewire
firmware
gpio
hv tools: hv: kvp: remove unnecessary (void*) conversions 2022-09-05 16:55:20 +00:00
iio tools: iio: iio_utils: fix digit calculation 2022-11-04 00:00:20 +09:00
include tools/nolibc: Fix missing strlen() definition and infinite loop with gcc-12 2022-11-10 18:17:16 +01:00
io_uring
kvm/kvm_stat tools/kvm_stat: fix display of error when multiple processes are found 2022-06-15 08:14:20 -04:00
laptop
leds
lib libbpf: Fix overrun in netlink attribute iteration 2022-10-21 12:39:11 +02:00
memory-model
objtool objtool: Preserve special st_shndx indexes in elf_update_symbol 2022-10-21 12:38:00 +02:00
pci
pcmcia
perf perf vendor events arm64: Fix incorrect Hisi hip08 L3 metrics 2022-11-04 00:00:32 +09:00
power tools/power turbostat: Use standard Energy Unit for SPR Dram RAPL domain 2022-10-21 12:39:02 +02:00
rcu
scripts
spi spi: spidev_test: Warn when the mode is not the requested mode 2022-06-13 15:56:03 +01:00
testing selftests: net: Fix netdev name mismatch in cleanup 2022-11-04 00:00:29 +09:00
thermal tools/thermal: Fix possible path truncations 2022-08-03 19:28:46 +02:00
time
tracing rtla: Consolidate and show all necessary libraries that failed for building 2022-08-10 12:03:02 -04:00
usb tools: usb: testusb: Add super-plus speed reporting 2022-07-08 14:54:49 +02:00
verification rv/dot2c: Make automaton definition static 2022-10-29 10:08:35 +02:00
virtio virtio_test: fixup for vq reset 2022-09-27 18:30:49 -04:00
vm - The usual batches of cleanups from Baoquan He, Muchun Song, Miaohe 2022-08-05 16:32:45 -07:00
wmi
Makefile tools/nolibc: make the default target build the headers 2022-06-20 09:43:19 -07:00