stackinit: Add union initialization to selftests
authorKees Cook <kees@kernel.org>
Mon, 27 Jan 2025 19:10:27 +0000 (11:10 -0800)
committerKees Cook <kees@kernel.org>
Thu, 30 Jan 2025 16:48:28 +0000 (08:48 -0800)
commite71a29db79da194678630ebfcc53ff2aecc9d441
treeb8be8a9a4184e46d33ace117a83bef853b6a3f8f
parentad9f265c7328d9d73a9d1edbd52f4415cc764296
stackinit: Add union initialization to selftests

The stack initialization selftests were checking scalars, strings,
and structs, but not unions. Add union tests (which are mostly identical
setup to structs). This catches the recent union initialization behavioral
changes seen in GCC 15. Before GCC 15, this new test passes:

    ok 18 test_small_start_old_zero

With GCC 15, it fails:

    not ok 18 test_small_start_old_zero

Specifically, a union with a larger member where a smaller member is
initialized with the older "= { 0 }" syntax:

union test_small_start {
     char one:1;
     char two;
     short three;
     unsigned long four;
     struct big_struct {
             unsigned long array[8];
     } big;
};

This is a regression in compiler behavior that Linux has depended on.
GCC does not seem likely to fix it, instead suggesting that affected
projects start using -fzero-init-padding-bits=unions:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118403

Link: https://lore.kernel.org/r/20250127191031.245214-2-kees@kernel.org
Signed-off-by: Kees Cook <kees@kernel.org>
lib/stackinit_kunit.c