Skip to content

Commit 9cf016e

Browse files
keestorvalds
authored andcommitted
lib: test_stackinit.c: XFAIL switch variable init tests
The tests for initializing a variable defined between a switch statement's test and its first "case" statement are currently not initialized in Clang[1] nor the proposed auto-initialization feature in GCC. We should retain the test (so that we can evaluate compiler fixes), but mark it as an "expected fail". The rest of the kernel source will be adjusted to avoid this corner case. Also disable -Wswitch-unreachable for the test so that the intentionally broken code won't trigger warnings for GCC (nor future Clang) when initialization happens this unhandled place. [1] https://bugs.llvm.org/show_bug.cgi?id=44916 Suggested-by: Alexander Potapenko <glider@google.com> Signed-off-by: Kees Cook <keescook@chromium.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Cc: Jann Horn <jannh@google.com> Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org> Link: http://lkml.kernel.org/r/202002191358.2897A07C6@keescook Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 6e85318 commit 9cf016e

2 files changed

Lines changed: 19 additions & 10 deletions

File tree

‎lib/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ obj-$(CONFIG_TEST_KMOD) += test_kmod.o
8787
obj-$(CONFIG_TEST_DEBUG_VIRTUAL) += test_debug_virtual.o
8888
obj-$(CONFIG_TEST_MEMCAT_P) += test_memcat_p.o
8989
obj-$(CONFIG_TEST_OBJAGG) += test_objagg.o
90+
CFLAGS_test_stackinit.o += $(call cc-disable-warning, switch-unreachable)
9091
obj-$(CONFIG_TEST_STACKINIT) += test_stackinit.o
9192
obj-$(CONFIG_TEST_BLACKHOLE_DEV) += test_blackhole_dev.o
9293
obj-$(CONFIG_TEST_MEMINIT) += test_meminit.o

‎lib/test_stackinit.c‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,9 @@ static bool range_contains(char *haystack_start, size_t haystack_size,
9292
* @var_type: type to be tested for zeroing initialization
9393
* @which: is this a SCALAR, STRING, or STRUCT type?
9494
* @init_level: what kind of initialization is performed
95+
* @xfail: is this test expected to fail?
9596
*/
96-
#define DEFINE_TEST_DRIVER(name, var_type, which) \
97+
#define DEFINE_TEST_DRIVER(name, var_type, which, xfail) \
9798
/* Returns 0 on success, 1 on failure. */ \
9899
static noinline __init int test_ ## name (void) \
99100
{ \
@@ -139,13 +140,14 @@ static noinline __init int test_ ## name (void) \
139140
for (sum = 0, i = 0; i < target_size; i++) \
140141
sum += (check_buf[i] == 0xFF); \
141142
\
142-
if (sum == 0) \
143+
if (sum == 0) { \
143144
pr_info(#name " ok\n"); \
144-
else \
145-
pr_warn(#name " FAIL (uninit bytes: %d)\n", \
146-
sum); \
147-
\
148-
return (sum != 0); \
145+
return 0; \
146+
} else { \
147+
pr_warn(#name " %sFAIL (uninit bytes: %d)\n", \
148+
(xfail) ? "X" : "", sum); \
149+
return (xfail) ? 0 : 1; \
150+
} \
149151
}
150152
#define DEFINE_TEST(name, var_type, which, init_level) \
151153
/* no-op to force compiler into ignoring "uninitialized" vars */\
@@ -189,7 +191,7 @@ static noinline __init int leaf_ ## name(unsigned long sp, \
189191
\
190192
return (int)buf[0] | (int)buf[sizeof(buf) - 1]; \
191193
} \
192-
DEFINE_TEST_DRIVER(name, var_type, which)
194+
DEFINE_TEST_DRIVER(name, var_type, which, 0)
193195

194196
/* Structure with no padding. */
195197
struct test_packed {
@@ -326,8 +328,14 @@ static noinline __init int leaf_switch_2_none(unsigned long sp, bool fill,
326328
return __leaf_switch_none(2, fill);
327329
}
328330

329-
DEFINE_TEST_DRIVER(switch_1_none, uint64_t, SCALAR);
330-
DEFINE_TEST_DRIVER(switch_2_none, uint64_t, SCALAR);
331+
/*
332+
* These are expected to fail for most configurations because neither
333+
* GCC nor Clang have a way to perform initialization of variables in
334+
* non-code areas (i.e. in a switch statement before the first "case").
335+
* https://bugs.llvm.org/show_bug.cgi?id=44916
336+
*/
337+
DEFINE_TEST_DRIVER(switch_1_none, uint64_t, SCALAR, 1);
338+
DEFINE_TEST_DRIVER(switch_2_none, uint64_t, SCALAR, 1);
331339

332340
static int __init test_stackinit_init(void)
333341
{

0 commit comments

Comments
 (0)