102788ebcSKees Cook // SPDX-License-Identifier: GPL-2.0-or-later
202788ebcSKees Cook /*
302788ebcSKees Cook * Test cases for compiler-based stack variable zeroing via
402788ebcSKees Cook * -ftrivial-auto-var-init={zero,pattern} or CONFIG_GCC_PLUGIN_STRUCTLEAK*.
502788ebcSKees Cook * For example, see:
64bba2a04STales Aparecida * "Running tests with kunit_tool" at Documentation/dev-tools/kunit/start.rst
702788ebcSKees Cook * ./tools/testing/kunit/kunit.py run stackinit [--raw_output] \
802788ebcSKees Cook * --make_option LLVM=1 \
902788ebcSKees Cook * --kconfig_add CONFIG_INIT_STACK_ALL_ZERO=y
1002788ebcSKees Cook *
1102788ebcSKees Cook */
1202788ebcSKees Cook #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1302788ebcSKees Cook
1402788ebcSKees Cook #include <kunit/test.h>
1502788ebcSKees Cook #include <linux/init.h>
1602788ebcSKees Cook #include <linux/kernel.h>
1702788ebcSKees Cook #include <linux/module.h>
1802788ebcSKees Cook #include <linux/string.h>
1902788ebcSKees Cook
2002788ebcSKees Cook /* Exfiltration buffer. */
2102788ebcSKees Cook #define MAX_VAR_SIZE 128
2202788ebcSKees Cook static u8 check_buf[MAX_VAR_SIZE];
2302788ebcSKees Cook
2402788ebcSKees Cook /* Character array to trigger stack protector in all functions. */
2502788ebcSKees Cook #define VAR_BUFFER 32
2602788ebcSKees Cook
2702788ebcSKees Cook /* Volatile mask to convince compiler to copy memory with 0xff. */
2802788ebcSKees Cook static volatile u8 forced_mask = 0xff;
2902788ebcSKees Cook
3002788ebcSKees Cook /* Location and size tracking to validate fill and test are colocated. */
3102788ebcSKees Cook static void *fill_start, *target_start;
3202788ebcSKees Cook static size_t fill_size, target_size;
3302788ebcSKees Cook
stackinit_range_contains(char * haystack_start,size_t haystack_size,char * needle_start,size_t needle_size)34*93c177fdSDan Williams static bool stackinit_range_contains(char *haystack_start, size_t haystack_size,
3502788ebcSKees Cook char *needle_start, size_t needle_size)
3602788ebcSKees Cook {
3702788ebcSKees Cook if (needle_start >= haystack_start &&
3802788ebcSKees Cook needle_start + needle_size <= haystack_start + haystack_size)
3902788ebcSKees Cook return true;
4002788ebcSKees Cook return false;
4102788ebcSKees Cook }
4202788ebcSKees Cook
4302788ebcSKees Cook /* Whether the test is expected to fail. */
4402788ebcSKees Cook #define WANT_SUCCESS 0
4502788ebcSKees Cook #define XFAIL 1
4602788ebcSKees Cook
4702788ebcSKees Cook #define DO_NOTHING_TYPE_SCALAR(var_type) var_type
4802788ebcSKees Cook #define DO_NOTHING_TYPE_STRING(var_type) void
4902788ebcSKees Cook #define DO_NOTHING_TYPE_STRUCT(var_type) void
5002788ebcSKees Cook
5102788ebcSKees Cook #define DO_NOTHING_RETURN_SCALAR(ptr) *(ptr)
5202788ebcSKees Cook #define DO_NOTHING_RETURN_STRING(ptr) /**/
5302788ebcSKees Cook #define DO_NOTHING_RETURN_STRUCT(ptr) /**/
5402788ebcSKees Cook
5502788ebcSKees Cook #define DO_NOTHING_CALL_SCALAR(var, name) \
5602788ebcSKees Cook (var) = do_nothing_ ## name(&(var))
5702788ebcSKees Cook #define DO_NOTHING_CALL_STRING(var, name) \
5802788ebcSKees Cook do_nothing_ ## name(var)
5902788ebcSKees Cook #define DO_NOTHING_CALL_STRUCT(var, name) \
6002788ebcSKees Cook do_nothing_ ## name(&(var))
6102788ebcSKees Cook
6202788ebcSKees Cook #define FETCH_ARG_SCALAR(var) &var
6302788ebcSKees Cook #define FETCH_ARG_STRING(var) var
6402788ebcSKees Cook #define FETCH_ARG_STRUCT(var) &var
6502788ebcSKees Cook
6602788ebcSKees Cook #define FILL_SIZE_STRING 16
6702788ebcSKees Cook
6802788ebcSKees Cook #define INIT_CLONE_SCALAR /**/
6902788ebcSKees Cook #define INIT_CLONE_STRING [FILL_SIZE_STRING]
7002788ebcSKees Cook #define INIT_CLONE_STRUCT /**/
7102788ebcSKees Cook
7202788ebcSKees Cook #define ZERO_CLONE_SCALAR(zero) memset(&(zero), 0x00, sizeof(zero))
7302788ebcSKees Cook #define ZERO_CLONE_STRING(zero) memset(&(zero), 0x00, sizeof(zero))
7402788ebcSKees Cook /*
7502788ebcSKees Cook * For the struct, intentionally poison padding to see if it gets
7602788ebcSKees Cook * copied out in direct assignments.
7702788ebcSKees Cook * */
7802788ebcSKees Cook #define ZERO_CLONE_STRUCT(zero) \
7902788ebcSKees Cook do { \
8002788ebcSKees Cook memset(&(zero), 0xFF, sizeof(zero)); \
8102788ebcSKees Cook zero.one = 0; \
8202788ebcSKees Cook zero.two = 0; \
8302788ebcSKees Cook zero.three = 0; \
8402788ebcSKees Cook zero.four = 0; \
8502788ebcSKees Cook } while (0)
8602788ebcSKees Cook
8702788ebcSKees Cook #define INIT_SCALAR_none(var_type) /**/
8802788ebcSKees Cook #define INIT_SCALAR_zero(var_type) = 0
8902788ebcSKees Cook
9002788ebcSKees Cook #define INIT_STRING_none(var_type) [FILL_SIZE_STRING] /**/
9102788ebcSKees Cook #define INIT_STRING_zero(var_type) [FILL_SIZE_STRING] = { }
9202788ebcSKees Cook
9302788ebcSKees Cook #define INIT_STRUCT_none(var_type) /**/
9402788ebcSKees Cook #define INIT_STRUCT_zero(var_type) = { }
9502788ebcSKees Cook
9602788ebcSKees Cook
9702788ebcSKees Cook #define __static_partial { .two = 0, }
9802788ebcSKees Cook #define __static_all { .one = 0, \
9902788ebcSKees Cook .two = 0, \
10002788ebcSKees Cook .three = 0, \
10102788ebcSKees Cook .four = 0, \
10202788ebcSKees Cook }
10302788ebcSKees Cook #define __dynamic_partial { .two = arg->two, }
10402788ebcSKees Cook #define __dynamic_all { .one = arg->one, \
10502788ebcSKees Cook .two = arg->two, \
10602788ebcSKees Cook .three = arg->three, \
10702788ebcSKees Cook .four = arg->four, \
10802788ebcSKees Cook }
10902788ebcSKees Cook #define __runtime_partial var.two = 0
11002788ebcSKees Cook #define __runtime_all var.one = 0; \
11102788ebcSKees Cook var.two = 0; \
11202788ebcSKees Cook var.three = 0; \
11302788ebcSKees Cook var.four = 0
11402788ebcSKees Cook
11502788ebcSKees Cook #define INIT_STRUCT_static_partial(var_type) \
11602788ebcSKees Cook = __static_partial
11702788ebcSKees Cook #define INIT_STRUCT_static_all(var_type) \
11802788ebcSKees Cook = __static_all
11902788ebcSKees Cook #define INIT_STRUCT_dynamic_partial(var_type) \
12002788ebcSKees Cook = __dynamic_partial
12102788ebcSKees Cook #define INIT_STRUCT_dynamic_all(var_type) \
12202788ebcSKees Cook = __dynamic_all
12302788ebcSKees Cook #define INIT_STRUCT_runtime_partial(var_type) \
12402788ebcSKees Cook ; __runtime_partial
12502788ebcSKees Cook #define INIT_STRUCT_runtime_all(var_type) \
12602788ebcSKees Cook ; __runtime_all
12702788ebcSKees Cook
12802788ebcSKees Cook #define INIT_STRUCT_assigned_static_partial(var_type) \
12902788ebcSKees Cook ; var = (var_type)__static_partial
13002788ebcSKees Cook #define INIT_STRUCT_assigned_static_all(var_type) \
13102788ebcSKees Cook ; var = (var_type)__static_all
13202788ebcSKees Cook #define INIT_STRUCT_assigned_dynamic_partial(var_type) \
13302788ebcSKees Cook ; var = (var_type)__dynamic_partial
13402788ebcSKees Cook #define INIT_STRUCT_assigned_dynamic_all(var_type) \
13502788ebcSKees Cook ; var = (var_type)__dynamic_all
13602788ebcSKees Cook
13702788ebcSKees Cook #define INIT_STRUCT_assigned_copy(var_type) \
13802788ebcSKees Cook ; var = *(arg)
13902788ebcSKees Cook
14002788ebcSKees Cook /*
14102788ebcSKees Cook * @name: unique string name for the test
14202788ebcSKees Cook * @var_type: type to be tested for zeroing initialization
14302788ebcSKees Cook * @which: is this a SCALAR, STRING, or STRUCT type?
14402788ebcSKees Cook * @init_level: what kind of initialization is performed
14502788ebcSKees Cook * @xfail: is this test expected to fail?
14602788ebcSKees Cook */
14702788ebcSKees Cook #define DEFINE_TEST_DRIVER(name, var_type, which, xfail) \
14802788ebcSKees Cook /* Returns 0 on success, 1 on failure. */ \
14902788ebcSKees Cook static noinline void test_ ## name (struct kunit *test) \
15002788ebcSKees Cook { \
15102788ebcSKees Cook var_type zero INIT_CLONE_ ## which; \
15202788ebcSKees Cook int ignored; \
15302788ebcSKees Cook u8 sum = 0, i; \
15402788ebcSKees Cook \
15502788ebcSKees Cook /* Notice when a new test is larger than expected. */ \
15602788ebcSKees Cook BUILD_BUG_ON(sizeof(zero) > MAX_VAR_SIZE); \
15702788ebcSKees Cook \
15802788ebcSKees Cook /* Fill clone type with zero for per-field init. */ \
15902788ebcSKees Cook ZERO_CLONE_ ## which(zero); \
16002788ebcSKees Cook /* Clear entire check buffer for 0xFF overlap test. */ \
16102788ebcSKees Cook memset(check_buf, 0x00, sizeof(check_buf)); \
16202788ebcSKees Cook /* Fill stack with 0xFF. */ \
16302788ebcSKees Cook ignored = leaf_ ##name((unsigned long)&ignored, 1, \
16402788ebcSKees Cook FETCH_ARG_ ## which(zero)); \
16502788ebcSKees Cook /* Verify all bytes overwritten with 0xFF. */ \
16602788ebcSKees Cook for (sum = 0, i = 0; i < target_size; i++) \
16702788ebcSKees Cook sum += (check_buf[i] != 0xFF); \
16802788ebcSKees Cook KUNIT_ASSERT_EQ_MSG(test, sum, 0, \
16902788ebcSKees Cook "leaf fill was not 0xFF!?\n"); \
17002788ebcSKees Cook /* Clear entire check buffer for later bit tests. */ \
17102788ebcSKees Cook memset(check_buf, 0x00, sizeof(check_buf)); \
17202788ebcSKees Cook /* Extract stack-defined variable contents. */ \
17302788ebcSKees Cook ignored = leaf_ ##name((unsigned long)&ignored, 0, \
17402788ebcSKees Cook FETCH_ARG_ ## which(zero)); \
17502788ebcSKees Cook \
17602788ebcSKees Cook /* Validate that compiler lined up fill and target. */ \
17702788ebcSKees Cook KUNIT_ASSERT_TRUE_MSG(test, \
178*93c177fdSDan Williams stackinit_range_contains(fill_start, fill_size, \
17902788ebcSKees Cook target_start, target_size), \
18002788ebcSKees Cook "stack fill missed target!? " \
18102788ebcSKees Cook "(fill %zu wide, target offset by %d)\n", \
18202788ebcSKees Cook fill_size, \
18302788ebcSKees Cook (int)((ssize_t)(uintptr_t)fill_start - \
18402788ebcSKees Cook (ssize_t)(uintptr_t)target_start)); \
18502788ebcSKees Cook \
18602788ebcSKees Cook /* Look for any bytes still 0xFF in check region. */ \
18702788ebcSKees Cook for (sum = 0, i = 0; i < target_size; i++) \
18802788ebcSKees Cook sum += (check_buf[i] == 0xFF); \
18902788ebcSKees Cook \
19002788ebcSKees Cook if (sum != 0 && xfail) \
19102788ebcSKees Cook kunit_skip(test, \
19202788ebcSKees Cook "XFAIL uninit bytes: %d\n", \
19302788ebcSKees Cook sum); \
19402788ebcSKees Cook KUNIT_ASSERT_EQ_MSG(test, sum, 0, \
19502788ebcSKees Cook "uninit bytes: %d\n", sum); \
19602788ebcSKees Cook }
19702788ebcSKees Cook #define DEFINE_TEST(name, var_type, which, init_level, xfail) \
19802788ebcSKees Cook /* no-op to force compiler into ignoring "uninitialized" vars */\
19902788ebcSKees Cook static noinline DO_NOTHING_TYPE_ ## which(var_type) \
20002788ebcSKees Cook do_nothing_ ## name(var_type *ptr) \
20102788ebcSKees Cook { \
20202788ebcSKees Cook /* Will always be true, but compiler doesn't know. */ \
20302788ebcSKees Cook if ((unsigned long)ptr > 0x2) \
20402788ebcSKees Cook return DO_NOTHING_RETURN_ ## which(ptr); \
20502788ebcSKees Cook else \
20602788ebcSKees Cook return DO_NOTHING_RETURN_ ## which(ptr + 1); \
20702788ebcSKees Cook } \
20802788ebcSKees Cook static noinline int leaf_ ## name(unsigned long sp, bool fill, \
20902788ebcSKees Cook var_type *arg) \
21002788ebcSKees Cook { \
21102788ebcSKees Cook char buf[VAR_BUFFER]; \
21202788ebcSKees Cook var_type var \
21302788ebcSKees Cook INIT_ ## which ## _ ## init_level(var_type); \
21402788ebcSKees Cook \
21502788ebcSKees Cook target_start = &var; \
21602788ebcSKees Cook target_size = sizeof(var); \
21702788ebcSKees Cook /* \
21802788ebcSKees Cook * Keep this buffer around to make sure we've got a \
21902788ebcSKees Cook * stack frame of SOME kind... \
22002788ebcSKees Cook */ \
22102788ebcSKees Cook memset(buf, (char)(sp & 0xff), sizeof(buf)); \
22202788ebcSKees Cook /* Fill variable with 0xFF. */ \
22302788ebcSKees Cook if (fill) { \
22402788ebcSKees Cook fill_start = &var; \
22502788ebcSKees Cook fill_size = sizeof(var); \
22602788ebcSKees Cook memset(fill_start, \
22702788ebcSKees Cook (char)((sp & 0xff) | forced_mask), \
22802788ebcSKees Cook fill_size); \
22902788ebcSKees Cook } \
23002788ebcSKees Cook \
23102788ebcSKees Cook /* Silence "never initialized" warnings. */ \
23202788ebcSKees Cook DO_NOTHING_CALL_ ## which(var, name); \
23302788ebcSKees Cook \
23402788ebcSKees Cook /* Exfiltrate "var". */ \
23502788ebcSKees Cook memcpy(check_buf, target_start, target_size); \
23602788ebcSKees Cook \
23702788ebcSKees Cook return (int)buf[0] | (int)buf[sizeof(buf) - 1]; \
23802788ebcSKees Cook } \
23902788ebcSKees Cook DEFINE_TEST_DRIVER(name, var_type, which, xfail)
24002788ebcSKees Cook
24102788ebcSKees Cook /* Structure with no padding. */
24202788ebcSKees Cook struct test_packed {
24302788ebcSKees Cook unsigned long one;
24402788ebcSKees Cook unsigned long two;
24502788ebcSKees Cook unsigned long three;
24602788ebcSKees Cook unsigned long four;
24702788ebcSKees Cook };
24802788ebcSKees Cook
24902788ebcSKees Cook /* Simple structure with padding likely to be covered by compiler. */
25002788ebcSKees Cook struct test_small_hole {
25102788ebcSKees Cook size_t one;
25202788ebcSKees Cook char two;
25302788ebcSKees Cook /* 3 byte padding hole here. */
25402788ebcSKees Cook int three;
25502788ebcSKees Cook unsigned long four;
25602788ebcSKees Cook };
25702788ebcSKees Cook
25802788ebcSKees Cook /* Trigger unhandled padding in a structure. */
25902788ebcSKees Cook struct test_big_hole {
26002788ebcSKees Cook u8 one;
26102788ebcSKees Cook u8 two;
26202788ebcSKees Cook u8 three;
26302788ebcSKees Cook /* 61 byte padding hole here. */
26402788ebcSKees Cook u8 four __aligned(64);
26502788ebcSKees Cook } __aligned(64);
26602788ebcSKees Cook
26702788ebcSKees Cook struct test_trailing_hole {
26802788ebcSKees Cook char *one;
26902788ebcSKees Cook char *two;
27002788ebcSKees Cook char *three;
27102788ebcSKees Cook char four;
27202788ebcSKees Cook /* "sizeof(unsigned long) - 1" byte padding hole here. */
27302788ebcSKees Cook };
27402788ebcSKees Cook
27502788ebcSKees Cook /* Test if STRUCTLEAK is clearing structs with __user fields. */
27602788ebcSKees Cook struct test_user {
27702788ebcSKees Cook u8 one;
27802788ebcSKees Cook unsigned long two;
27902788ebcSKees Cook char __user *three;
28002788ebcSKees Cook unsigned long four;
28102788ebcSKees Cook };
28202788ebcSKees Cook
28302788ebcSKees Cook #define ALWAYS_PASS WANT_SUCCESS
28402788ebcSKees Cook #define ALWAYS_FAIL XFAIL
28502788ebcSKees Cook
28602788ebcSKees Cook #ifdef CONFIG_INIT_STACK_NONE
28702788ebcSKees Cook # define USER_PASS XFAIL
28802788ebcSKees Cook # define BYREF_PASS XFAIL
28902788ebcSKees Cook # define STRONG_PASS XFAIL
29002788ebcSKees Cook #elif defined(CONFIG_GCC_PLUGIN_STRUCTLEAK_USER)
29102788ebcSKees Cook # define USER_PASS WANT_SUCCESS
29202788ebcSKees Cook # define BYREF_PASS XFAIL
29302788ebcSKees Cook # define STRONG_PASS XFAIL
29402788ebcSKees Cook #elif defined(CONFIG_GCC_PLUGIN_STRUCTLEAK_BYREF)
29502788ebcSKees Cook # define USER_PASS WANT_SUCCESS
29602788ebcSKees Cook # define BYREF_PASS WANT_SUCCESS
29702788ebcSKees Cook # define STRONG_PASS XFAIL
29802788ebcSKees Cook #else
29902788ebcSKees Cook # define USER_PASS WANT_SUCCESS
30002788ebcSKees Cook # define BYREF_PASS WANT_SUCCESS
30102788ebcSKees Cook # define STRONG_PASS WANT_SUCCESS
30202788ebcSKees Cook #endif
30302788ebcSKees Cook
30402788ebcSKees Cook #define DEFINE_SCALAR_TEST(name, init, xfail) \
30502788ebcSKees Cook DEFINE_TEST(name ## _ ## init, name, SCALAR, \
30602788ebcSKees Cook init, xfail)
30702788ebcSKees Cook
30802788ebcSKees Cook #define DEFINE_SCALAR_TESTS(init, xfail) \
30902788ebcSKees Cook DEFINE_SCALAR_TEST(u8, init, xfail); \
31002788ebcSKees Cook DEFINE_SCALAR_TEST(u16, init, xfail); \
31102788ebcSKees Cook DEFINE_SCALAR_TEST(u32, init, xfail); \
31202788ebcSKees Cook DEFINE_SCALAR_TEST(u64, init, xfail); \
31302788ebcSKees Cook DEFINE_TEST(char_array_ ## init, unsigned char, \
31402788ebcSKees Cook STRING, init, xfail)
31502788ebcSKees Cook
31602788ebcSKees Cook #define DEFINE_STRUCT_TEST(name, init, xfail) \
31702788ebcSKees Cook DEFINE_TEST(name ## _ ## init, \
31802788ebcSKees Cook struct test_ ## name, STRUCT, init, \
31902788ebcSKees Cook xfail)
32002788ebcSKees Cook
32102788ebcSKees Cook #define DEFINE_STRUCT_TESTS(init, xfail) \
32202788ebcSKees Cook DEFINE_STRUCT_TEST(small_hole, init, xfail); \
32302788ebcSKees Cook DEFINE_STRUCT_TEST(big_hole, init, xfail); \
32402788ebcSKees Cook DEFINE_STRUCT_TEST(trailing_hole, init, xfail); \
32502788ebcSKees Cook DEFINE_STRUCT_TEST(packed, init, xfail)
32602788ebcSKees Cook
32702788ebcSKees Cook #define DEFINE_STRUCT_INITIALIZER_TESTS(base, xfail) \
32802788ebcSKees Cook DEFINE_STRUCT_TESTS(base ## _ ## partial, \
32902788ebcSKees Cook xfail); \
33002788ebcSKees Cook DEFINE_STRUCT_TESTS(base ## _ ## all, xfail)
33102788ebcSKees Cook
33202788ebcSKees Cook /* These should be fully initialized all the time! */
33302788ebcSKees Cook DEFINE_SCALAR_TESTS(zero, ALWAYS_PASS);
33402788ebcSKees Cook DEFINE_STRUCT_TESTS(zero, ALWAYS_PASS);
33502788ebcSKees Cook /* Struct initializers: padding may be left uninitialized. */
33602788ebcSKees Cook DEFINE_STRUCT_INITIALIZER_TESTS(static, STRONG_PASS);
33702788ebcSKees Cook DEFINE_STRUCT_INITIALIZER_TESTS(dynamic, STRONG_PASS);
33802788ebcSKees Cook DEFINE_STRUCT_INITIALIZER_TESTS(runtime, STRONG_PASS);
33902788ebcSKees Cook DEFINE_STRUCT_INITIALIZER_TESTS(assigned_static, STRONG_PASS);
34002788ebcSKees Cook DEFINE_STRUCT_INITIALIZER_TESTS(assigned_dynamic, STRONG_PASS);
34102788ebcSKees Cook DEFINE_STRUCT_TESTS(assigned_copy, ALWAYS_FAIL);
34202788ebcSKees Cook /* No initialization without compiler instrumentation. */
34302788ebcSKees Cook DEFINE_SCALAR_TESTS(none, STRONG_PASS);
34402788ebcSKees Cook DEFINE_STRUCT_TESTS(none, BYREF_PASS);
34502788ebcSKees Cook /* Initialization of members with __user attribute. */
34602788ebcSKees Cook DEFINE_TEST(user, struct test_user, STRUCT, none, USER_PASS);
34702788ebcSKees Cook
34802788ebcSKees Cook /*
34902788ebcSKees Cook * Check two uses through a variable declaration outside either path,
35002788ebcSKees Cook * which was noticed as a special case in porting earlier stack init
35102788ebcSKees Cook * compiler logic.
35202788ebcSKees Cook */
__leaf_switch_none(int path,bool fill)35302788ebcSKees Cook static int noinline __leaf_switch_none(int path, bool fill)
35402788ebcSKees Cook {
35502788ebcSKees Cook switch (path) {
35602788ebcSKees Cook /*
35702788ebcSKees Cook * This is intentionally unreachable. To silence the
35802788ebcSKees Cook * warning, build with -Wno-switch-unreachable
35902788ebcSKees Cook */
36002788ebcSKees Cook uint64_t var[10];
36102788ebcSKees Cook
36202788ebcSKees Cook case 1:
36302788ebcSKees Cook target_start = &var;
36402788ebcSKees Cook target_size = sizeof(var);
36502788ebcSKees Cook if (fill) {
36602788ebcSKees Cook fill_start = &var;
36702788ebcSKees Cook fill_size = sizeof(var);
36802788ebcSKees Cook
36902788ebcSKees Cook memset(fill_start, forced_mask | 0x55, fill_size);
37002788ebcSKees Cook }
37102788ebcSKees Cook memcpy(check_buf, target_start, target_size);
37202788ebcSKees Cook break;
37302788ebcSKees Cook case 2:
37402788ebcSKees Cook target_start = &var;
37502788ebcSKees Cook target_size = sizeof(var);
37602788ebcSKees Cook if (fill) {
37702788ebcSKees Cook fill_start = &var;
37802788ebcSKees Cook fill_size = sizeof(var);
37902788ebcSKees Cook
38002788ebcSKees Cook memset(fill_start, forced_mask | 0xaa, fill_size);
38102788ebcSKees Cook }
38202788ebcSKees Cook memcpy(check_buf, target_start, target_size);
38302788ebcSKees Cook break;
38402788ebcSKees Cook default:
38502788ebcSKees Cook var[1] = 5;
38602788ebcSKees Cook return var[1] & forced_mask;
38702788ebcSKees Cook }
38802788ebcSKees Cook return 0;
38902788ebcSKees Cook }
39002788ebcSKees Cook
leaf_switch_1_none(unsigned long sp,bool fill,uint64_t * arg)39102788ebcSKees Cook static noinline int leaf_switch_1_none(unsigned long sp, bool fill,
39202788ebcSKees Cook uint64_t *arg)
39302788ebcSKees Cook {
39402788ebcSKees Cook return __leaf_switch_none(1, fill);
39502788ebcSKees Cook }
39602788ebcSKees Cook
leaf_switch_2_none(unsigned long sp,bool fill,uint64_t * arg)39702788ebcSKees Cook static noinline int leaf_switch_2_none(unsigned long sp, bool fill,
39802788ebcSKees Cook uint64_t *arg)
39902788ebcSKees Cook {
40002788ebcSKees Cook return __leaf_switch_none(2, fill);
40102788ebcSKees Cook }
40202788ebcSKees Cook
40302788ebcSKees Cook /*
40402788ebcSKees Cook * These are expected to fail for most configurations because neither
40502788ebcSKees Cook * GCC nor Clang have a way to perform initialization of variables in
40602788ebcSKees Cook * non-code areas (i.e. in a switch statement before the first "case").
40702788ebcSKees Cook * https://bugs.llvm.org/show_bug.cgi?id=44916
40802788ebcSKees Cook */
40902788ebcSKees Cook DEFINE_TEST_DRIVER(switch_1_none, uint64_t, SCALAR, ALWAYS_FAIL);
41002788ebcSKees Cook DEFINE_TEST_DRIVER(switch_2_none, uint64_t, SCALAR, ALWAYS_FAIL);
41102788ebcSKees Cook
41202788ebcSKees Cook #define KUNIT_test_scalars(init) \
41302788ebcSKees Cook KUNIT_CASE(test_u8_ ## init), \
41402788ebcSKees Cook KUNIT_CASE(test_u16_ ## init), \
41502788ebcSKees Cook KUNIT_CASE(test_u32_ ## init), \
41602788ebcSKees Cook KUNIT_CASE(test_u64_ ## init), \
41702788ebcSKees Cook KUNIT_CASE(test_char_array_ ## init)
41802788ebcSKees Cook
41902788ebcSKees Cook #define KUNIT_test_structs(init) \
42002788ebcSKees Cook KUNIT_CASE(test_small_hole_ ## init), \
42102788ebcSKees Cook KUNIT_CASE(test_big_hole_ ## init), \
42202788ebcSKees Cook KUNIT_CASE(test_trailing_hole_ ## init),\
42302788ebcSKees Cook KUNIT_CASE(test_packed_ ## init) \
42402788ebcSKees Cook
42502788ebcSKees Cook static struct kunit_case stackinit_test_cases[] = {
42602788ebcSKees Cook /* These are explicitly initialized and should always pass. */
42702788ebcSKees Cook KUNIT_test_scalars(zero),
42802788ebcSKees Cook KUNIT_test_structs(zero),
42902788ebcSKees Cook /* Padding here appears to be accidentally always initialized? */
43002788ebcSKees Cook KUNIT_test_structs(dynamic_partial),
43102788ebcSKees Cook KUNIT_test_structs(assigned_dynamic_partial),
43202788ebcSKees Cook /* Padding initialization depends on compiler behaviors. */
43302788ebcSKees Cook KUNIT_test_structs(static_partial),
43402788ebcSKees Cook KUNIT_test_structs(static_all),
43502788ebcSKees Cook KUNIT_test_structs(dynamic_all),
43602788ebcSKees Cook KUNIT_test_structs(runtime_partial),
43702788ebcSKees Cook KUNIT_test_structs(runtime_all),
43802788ebcSKees Cook KUNIT_test_structs(assigned_static_partial),
43902788ebcSKees Cook KUNIT_test_structs(assigned_static_all),
44002788ebcSKees Cook KUNIT_test_structs(assigned_dynamic_all),
44102788ebcSKees Cook /* Everything fails this since it effectively performs a memcpy(). */
44202788ebcSKees Cook KUNIT_test_structs(assigned_copy),
44302788ebcSKees Cook /* STRUCTLEAK_BYREF_ALL should cover everything from here down. */
44402788ebcSKees Cook KUNIT_test_scalars(none),
44502788ebcSKees Cook KUNIT_CASE(test_switch_1_none),
44602788ebcSKees Cook KUNIT_CASE(test_switch_2_none),
44702788ebcSKees Cook /* STRUCTLEAK_BYREF should cover from here down. */
44802788ebcSKees Cook KUNIT_test_structs(none),
44902788ebcSKees Cook /* STRUCTLEAK will only cover this. */
45002788ebcSKees Cook KUNIT_CASE(test_user),
45102788ebcSKees Cook {}
45202788ebcSKees Cook };
45302788ebcSKees Cook
45402788ebcSKees Cook static struct kunit_suite stackinit_test_suite = {
45502788ebcSKees Cook .name = "stackinit",
45602788ebcSKees Cook .test_cases = stackinit_test_cases,
45702788ebcSKees Cook };
45802788ebcSKees Cook
45902788ebcSKees Cook kunit_test_suites(&stackinit_test_suite);
46002788ebcSKees Cook
46102788ebcSKees Cook MODULE_LICENSE("GPL");
462