linux/lib/tests
Kees Cook 95719dfa32 KUnit: ffs: Validate all the __attribute_const__ annotations
While tracking down a problem where constant expressions used by
BUILD_BUG_ON() suddenly stopped working[1], we found that an added static
initializer was convincing the compiler that it couldn't track the state
of the prior statically initialized value. Tracing this down found that
ffs() was used in the initializer macro, but since it wasn't marked with
__attribute_const__, the compiler had to assume the function might
change variable states as a side-effect (which is not true for ffs(),
which provides deterministic math results).

Validate all the __attibute_const__ annotations were found for all
architectures by reproducing the specific problem encountered in the
original bug report.

Build and run tested with everything I could reach with KUnit:

$ ./tools/testing/kunit/kunit.py run --arch=x86_64 ffs
$ ./tools/testing/kunit/kunit.py run --arch=i386 ffs
$ ./tools/testing/kunit/kunit.py run --arch=arm64 --make_options "CROSS_COMPILE=aarch64-linux-gnu-" ffs
$ ./tools/testing/kunit/kunit.py run --arch=arm --make_options "CROSS_COMPILE=arm-linux-gnueabi-" ffs
$ ./tools/testing/kunit/kunit.py run --arch=powerpc ffs
$ ./tools/testing/kunit/kunit.py run --arch=powerpc32 ffs
$ ./tools/testing/kunit/kunit.py run --arch=powerpcle ffs
$ ./tools/testing/kunit/kunit.py run --arch=m68k ffs
$ ./tools/testing/kunit/kunit.py run --arch=loongarch ffs
$ ./tools/testing/kunit/kunit.py run --arch=s390 --make_options "CROSS_COMPILE=s390x-linux-gnu-" ffs
$ ./tools/testing/kunit/kunit.py run --arch=riscv --make_options "CROSS_COMPILE=riscv64-linux-gnu-" ffs
$ ./tools/testing/kunit/kunit.py run --arch=riscv32 --make_options "CROSS_COMPILE=riscv64-linux-gnu-" ffs
$ ./tools/testing/kunit/kunit.py run --arch=sparc --make_options "CROSS_COMPILE=sparc64-linux-gnu-" ffs
$ ./tools/testing/kunit/kunit.py run --arch=sparc64 --make_options "CROSS_COMPILE=sparc64-linux-gnu-" ffs
$ ./tools/testing/kunit/kunit.py run --arch=alpha --make_options "CROSS_COMPILE=alpha-linux-gnu-" ffs
$ ./tools/testing/kunit/kunit.py run --arch=sh --make_options "CROSS_COMPILE=sh4-linux-gnu-" ffs

Closes: https://github.com/KSPP/linux/issues/364
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lore.kernel.org/r/20250804164417.1612371-17-kees@kernel.org
Signed-off-by: Kees Cook <kees@kernel.org>
2025-09-08 14:58:52 -07:00
..
module tests/module: nix-ify 2025-03-10 11:54:13 +01:00
Makefile KUnit: Introduce ffs()-family tests 2025-09-08 14:58:50 -07:00
bitfield_kunit.c lib: Move KUnit tests into tests/ subdirectory 2025-02-10 18:25:39 -08:00
blackhole_dev_kunit.c Networking changes for 6.15. 2025-03-26 21:48:21 -07:00
checksum_kunit.c lib: Move KUnit tests into tests/ subdirectory 2025-02-10 18:25:39 -08:00
cmdline_kunit.c lib: Move KUnit tests into tests/ subdirectory 2025-02-10 18:25:39 -08:00
cpumask_kunit.c lib: Move KUnit tests into tests/ subdirectory 2025-02-10 18:25:39 -08:00
ffs_kunit.c KUnit: ffs: Validate all the __attribute_const__ annotations 2025-09-08 14:58:52 -07:00
fortify_kunit.c kunit/fortify: Add back "volatile" for sizeof() constants 2025-07-14 22:43:52 -07:00
hashtable_test.c lib: Move KUnit tests into tests/ subdirectory 2025-02-10 18:25:39 -08:00
is_signed_type_kunit.c lib: Move KUnit tests into tests/ subdirectory 2025-02-10 18:25:39 -08:00
kfifo_kunit.c lib/tests/kfifo_kunit.c: add tests for the kfifo structure 2025-02-10 18:25:39 -08:00
kunit_iov_iter.c lib: Move KUnit tests into tests/ subdirectory 2025-02-10 18:25:39 -08:00
list-test.c lib: Move KUnit tests into tests/ subdirectory 2025-02-10 18:25:39 -08:00
longest_symbol_kunit.c kunit: fix longest symbol length test 2025-07-10 14:02:07 -06:00
memcpy_kunit.c lib: Move KUnit tests into tests/ subdirectory 2025-02-10 18:25:39 -08:00
overflow_kunit.c kunit/overflow: Add tests for STACK_FLEX_ARRAY_SIZE() helper 2025-05-08 09:42:05 -07:00
printf_kunit.c vsprintf: Use %p4chR instead of %p4cn for reading data in reversed host ordering 2025-04-29 09:29:33 -04:00
randstruct_kunit.c lib/tests: randstruct: Add deep function pointer layout test 2025-05-08 09:42:40 -07:00
scanf_kunit.c scanf: break kunit into test cases 2025-03-14 13:56:15 -07:00
seq_buf_kunit.c seq_buf: Introduce KUnit tests 2025-07-19 23:03:24 -07:00
siphash_kunit.c lib: Move KUnit tests into tests/ subdirectory 2025-02-10 18:25:39 -08:00
slub_kunit.c kunit: slub: add module description 2025-04-11 17:32:39 -07:00
stackinit_kunit.c Kbuild: remove structleak gcc plugin 2025-04-30 21:57:09 +02:00
string_helpers_kunit.c lib: Move KUnit tests into tests/ subdirectory 2025-02-10 18:25:39 -08:00
string_kunit.c hardening updates for v6.15-rc1 2025-03-24 15:18:08 -07:00
test_bits.c test_bits: add tests for __GENMASK() and __GENMASK_ULL() 2025-07-31 11:28:03 -04:00
test_fprobe.c lib: Move KUnit tests into tests/ subdirectory 2025-02-10 18:25:39 -08:00
test_hash.c lib: Move KUnit tests into tests/ subdirectory 2025-02-10 18:25:39 -08:00
test_kprobes.c lib: Move KUnit tests into tests/ subdirectory 2025-02-10 18:25:39 -08:00
test_linear_ranges.c lib: Move KUnit tests into tests/ subdirectory 2025-02-10 18:25:39 -08:00
test_list_sort.c lib: Move KUnit tests into tests/ subdirectory 2025-02-10 18:25:39 -08:00
test_ratelimit.c lib: Add stress test for ratelimit 2025-06-24 05:47:35 -07:00
test_sort.c lib: Move KUnit tests into tests/ subdirectory 2025-02-10 18:25:39 -08:00
usercopy_kunit.c kunit/usercopy: Disable u64 test on 32-bit SPARC 2025-04-28 10:32:43 -07:00
util_macros_kunit.c lib: Move KUnit tests into tests/ subdirectory 2025-02-10 18:25:39 -08:00