xref: /linux/lib/tests/stackinit_kunit.c (revision 0f82b48209e85a1f049df20455703a2767f65cfd)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Test cases for compiler-based stack variable zeroing via
4  * -ftrivial-auto-var-init={zero,pattern}.
5  * For example, see:
6  * "Running tests with kunit_tool" at Documentation/dev-tools/kunit/start.rst
7  *	./tools/testing/kunit/kunit.py run stackinit [--raw_output] \
8  *		--make_option LLVM=1 \
9  *		--kconfig_add CONFIG_INIT_STACK_ALL_ZERO=y
10  *
11  */
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 
14 #include <kunit/test.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/string.h>
19 
20 /* Exfiltration buffer. */
21 #define MAX_VAR_SIZE	128
22 static u8 check_buf[MAX_VAR_SIZE];
23 
24 /* Character array to trigger stack protector in all functions. */
25 #define VAR_BUFFER	 32
26 
27 /* Volatile mask to convince compiler to copy memory with 0xff. */
28 static volatile u8 forced_mask = 0xff;
29 
30 /* Location and size tracking to validate fill and test are colocated. */
31 static void *fill_start, *target_start;
32 static size_t fill_size, target_size;
33 
34 static bool stackinit_range_contains(char *haystack_start, size_t haystack_size,
35 				     char *needle_start, size_t needle_size)
36 {
37 	if (needle_start >= haystack_start &&
38 	    needle_start + needle_size <= haystack_start + haystack_size)
39 		return true;
40 	return false;
41 }
42 
43 /* Whether the test is expected to fail. */
44 #define WANT_SUCCESS				0
45 #define XFAIL					1
46 
47 #define DO_NOTHING_TYPE_SCALAR(var_type)	var_type
48 #define DO_NOTHING_TYPE_STRING(var_type)	void
49 #define DO_NOTHING_TYPE_STRUCT(var_type)	void
50 #define DO_NOTHING_TYPE_UNION(var_type)		void
51 
52 #define DO_NOTHING_RETURN_SCALAR(ptr)		*(ptr)
53 #define DO_NOTHING_RETURN_STRING(ptr)		/**/
54 #define DO_NOTHING_RETURN_STRUCT(ptr)		/**/
55 #define DO_NOTHING_RETURN_UNION(ptr)		/**/
56 
57 #define DO_NOTHING_CALL_SCALAR(var, name)			\
58 		(var) = do_nothing_ ## name(&(var))
59 #define DO_NOTHING_CALL_STRING(var, name)			\
60 		do_nothing_ ## name(var)
61 #define DO_NOTHING_CALL_STRUCT(var, name)			\
62 		do_nothing_ ## name(&(var))
63 #define DO_NOTHING_CALL_UNION(var, name)			\
64 		do_nothing_ ## name(&(var))
65 
66 #define FETCH_ARG_SCALAR(var)		&var
67 #define FETCH_ARG_STRING(var)		var
68 #define FETCH_ARG_STRUCT(var)		&var
69 #define FETCH_ARG_UNION(var)		&var
70 
71 /*
72  * On m68k, if the leaf function test variable is longer than 8 bytes,
73  * the start of the stack frame moves. 8 is sufficiently large to
74  * test m68k char arrays, but leave it at 16 for other architectures.
75  */
76 #ifdef CONFIG_M68K
77 #define FILL_SIZE_STRING		8
78 #define FILL_SIZE_ARRAY			2
79 #else
80 #define FILL_SIZE_STRING		16
81 #define FILL_SIZE_ARRAY			8
82 #endif
83 
84 #define INIT_CLONE_SCALAR		/**/
85 #define INIT_CLONE_STRING		[FILL_SIZE_STRING]
86 #define INIT_CLONE_STRUCT		/**/
87 #define INIT_CLONE_UNION		/**/
88 
89 #define ZERO_CLONE_SCALAR(zero)		memset(&(zero), 0x00, sizeof(zero))
90 #define ZERO_CLONE_STRING(zero)		memset(&(zero), 0x00, sizeof(zero))
91 /*
92  * For the struct, intentionally poison padding to see if it gets
93  * copied out in direct assignments.
94  * */
95 #define ZERO_CLONE_STRUCT(zero)				\
96 	do {						\
97 		memset(&(zero), 0xFF, sizeof(zero));	\
98 		zero.one = 0;				\
99 		zero.two = 0;				\
100 		zero.three = 0;				\
101 		zero.four = 0;				\
102 	} while (0)
103 #define ZERO_CLONE_UNION(zero)		ZERO_CLONE_STRUCT(zero)
104 
105 #define INIT_SCALAR_none(var_type)	/**/
106 #define INIT_SCALAR_zero(var_type)	= 0
107 
108 #define INIT_STRING_none(var_type)	[FILL_SIZE_STRING] /**/
109 #define INIT_STRING_zero(var_type)	[FILL_SIZE_STRING] = { }
110 
111 #define INIT_STRUCT_none(var_type)	/**/
112 #define INIT_STRUCT_zero(var_type)	= { }
113 #define INIT_STRUCT_old_zero(var_type)	= { 0 }
114 
115 
116 #define __static_partial		{ .two = 0, }
117 #define __static_all			{ .one = 0,			\
118 					  .two = 0,			\
119 					  .three = 0,			\
120 					  .four = 0,			\
121 					}
122 #define __dynamic_partial		{ .two = arg->two, }
123 #define __dynamic_all			{ .one = arg->one,		\
124 					  .two = arg->two,		\
125 					  .three = arg->three,		\
126 					  .four = arg->four,		\
127 					}
128 #define __runtime_partial		var.two = 0
129 #define __runtime_all			var.one = 0;			\
130 					var.two = 0;			\
131 					var.three = 0;			\
132 					var.four = 0
133 
134 #define INIT_STRUCT_static_partial(var_type)				\
135 					= __static_partial
136 #define INIT_STRUCT_static_all(var_type)				\
137 					= __static_all
138 #define INIT_STRUCT_dynamic_partial(var_type)				\
139 					= __dynamic_partial
140 #define INIT_STRUCT_dynamic_all(var_type)				\
141 					= __dynamic_all
142 #define INIT_STRUCT_runtime_partial(var_type)				\
143 					; __runtime_partial
144 #define INIT_STRUCT_runtime_all(var_type)				\
145 					; __runtime_all
146 
147 #define INIT_STRUCT_assigned_static_partial(var_type)			\
148 					; var = (var_type)__static_partial
149 #define INIT_STRUCT_assigned_static_all(var_type)			\
150 					; var = (var_type)__static_all
151 #define INIT_STRUCT_assigned_dynamic_partial(var_type)			\
152 					; var = (var_type)__dynamic_partial
153 #define INIT_STRUCT_assigned_dynamic_all(var_type)			\
154 					; var = (var_type)__dynamic_all
155 
156 #define INIT_STRUCT_assigned_copy(var_type)				\
157 					; var = *(arg)
158 
159 /* Union initialization is the same as structs. */
160 #define INIT_UNION_none(var_type)	INIT_STRUCT_none(var_type)
161 #define INIT_UNION_zero(var_type)	INIT_STRUCT_zero(var_type)
162 #define INIT_UNION_old_zero(var_type)	INIT_STRUCT_old_zero(var_type)
163 
164 #define INIT_UNION_static_partial(var_type)		\
165 	INIT_STRUCT_static_partial(var_type)
166 #define INIT_UNION_static_all(var_type)			\
167 	INIT_STRUCT_static_all(var_type)
168 #define INIT_UNION_dynamic_partial(var_type)		\
169 	INIT_STRUCT_dynamic_partial(var_type)
170 #define INIT_UNION_dynamic_all(var_type)		\
171 	INIT_STRUCT_dynamic_all(var_type)
172 #define INIT_UNION_runtime_partial(var_type)		\
173 	INIT_STRUCT_runtime_partial(var_type)
174 #define INIT_UNION_runtime_all(var_type)		\
175 	INIT_STRUCT_runtime_all(var_type)
176 #define INIT_UNION_assigned_static_partial(var_type)	\
177 	INIT_STRUCT_assigned_static_partial(var_type)
178 #define INIT_UNION_assigned_static_all(var_type)	\
179 	INIT_STRUCT_assigned_static_all(var_type)
180 #define INIT_UNION_assigned_dynamic_partial(var_type)	\
181 	INIT_STRUCT_assigned_dynamic_partial(var_type)
182 #define INIT_UNION_assigned_dynamic_all(var_type)	\
183 	INIT_STRUCT_assigned_dynamic_all(var_type)
184 #define INIT_UNION_assigned_copy(var_type)		\
185 	INIT_STRUCT_assigned_copy(var_type)
186 
187 /*
188  * The "did we actually fill the stack?" check value needs
189  * to be neither 0 nor any of the "pattern" bytes. The
190  * pattern bytes are compiler, architecture, and type based,
191  * so we have to pick a value that never appears for those
192  * combinations. Use 0x99 which is not 0xFF, 0xFE, nor 0xAA.
193  */
194 #define FILL_BYTE	0x99
195 
196 /*
197  * @name: unique string name for the test
198  * @var_type: type to be tested for zeroing initialization
199  * @which: is this a SCALAR, STRING, or STRUCT type?
200  * @init_level: what kind of initialization is performed
201  * @xfail: is this test expected to fail?
202  */
203 #define DEFINE_TEST_DRIVER(name, var_type, which, xfail)	\
204 /* Returns 0 on success, 1 on failure. */			\
205 static noinline void test_ ## name (struct kunit *test)		\
206 {								\
207 	var_type zero INIT_CLONE_ ## which;			\
208 	int ignored;						\
209 	u8 sum = 0, i;						\
210 								\
211 	/* Notice when a new test is larger than expected. */	\
212 	BUILD_BUG_ON(sizeof(zero) > MAX_VAR_SIZE);		\
213 								\
214 	/* Fill clone type with zero for per-field init. */	\
215 	ZERO_CLONE_ ## which(zero);				\
216 	/* Clear entire check buffer for 0xFF overlap test. */	\
217 	memset(check_buf, 0x00, sizeof(check_buf));		\
218 	/* Fill stack with FILL_BYTE. */			\
219 	ignored = leaf_ ##name((unsigned long)&ignored, 1,	\
220 				FETCH_ARG_ ## which(zero));	\
221 	/* Verify all bytes overwritten with FILL_BYTE. */	\
222 	for (sum = 0, i = 0; i < target_size; i++)		\
223 		sum += (check_buf[i] != FILL_BYTE);		\
224 	/* Clear entire check buffer for later bit tests. */	\
225 	memset(check_buf, 0x00, sizeof(check_buf));		\
226 	/* Extract stack-defined variable contents. */		\
227 	ignored = leaf_ ##name((unsigned long)&ignored, 0,	\
228 				FETCH_ARG_ ## which(zero));	\
229 	/*							\
230 	 * Delay the sum test to here to do as little as	\
231 	 * possible between the two leaf function calls.	\
232 	 */							\
233 	KUNIT_ASSERT_EQ_MSG(test, sum, 0,			\
234 			    "leaf fill was not 0x%02X!?\n",	\
235 			    FILL_BYTE);				\
236 								\
237 	/* Validate that compiler lined up fill and target. */	\
238 	KUNIT_ASSERT_TRUE_MSG(test,				\
239 		stackinit_range_contains(fill_start, fill_size,	\
240 			    target_start, target_size),		\
241 		"stackframe was not the same between calls!? "	\
242 		"(fill %zu wide, target offset by %d)\n",	\
243 		fill_size,					\
244 		(int)((ssize_t)(uintptr_t)fill_start -		\
245 		      (ssize_t)(uintptr_t)target_start));	\
246 								\
247 	/* Validate check region has no FILL_BYTE bytes. */	\
248 	for (sum = 0, i = 0; i < target_size; i++)		\
249 		sum += (check_buf[i] == FILL_BYTE);		\
250 								\
251 	if (sum != 0 && xfail)					\
252 		kunit_skip(test,				\
253 			   "XFAIL uninit bytes: %d\n",		\
254 			   sum);				\
255 	KUNIT_ASSERT_EQ_MSG(test, sum, 0,			\
256 		"uninit bytes: %d\n", sum);			\
257 }
258 #define DEFINE_TEST(name, var_type, which, init_level, xfail)	\
259 /* no-op to force compiler into ignoring "uninitialized" vars */\
260 static noinline DO_NOTHING_TYPE_ ## which(var_type)		\
261 do_nothing_ ## name(var_type *ptr)				\
262 {								\
263 	OPTIMIZER_HIDE_VAR(ptr);				\
264 	/* Will always be true, but compiler doesn't know. */	\
265 	if ((unsigned long)ptr > 0x2)				\
266 		return DO_NOTHING_RETURN_ ## which(ptr);	\
267 	else							\
268 		return DO_NOTHING_RETURN_ ## which(ptr + 1);	\
269 }								\
270 static noinline int leaf_ ## name(unsigned long sp, bool fill,	\
271 				  var_type *arg)		\
272 {								\
273 	char buf[VAR_BUFFER];					\
274 	var_type var						\
275 		INIT_ ## which ## _ ## init_level(var_type);	\
276 								\
277 	target_start = &var;					\
278 	target_size = sizeof(var);				\
279 	/*							\
280 	 * Keep this buffer around to make sure we've got a	\
281 	 * stack frame of SOME kind...				\
282 	 */							\
283 	memset(buf, (char)(sp & 0xff), sizeof(buf));		\
284 	/* Fill variable with FILL_BYTE. */			\
285 	if (fill) {						\
286 		fill_start = &var;				\
287 		fill_size = sizeof(var);			\
288 		memset(fill_start,				\
289 		       FILL_BYTE & forced_mask,			\
290 		       fill_size);				\
291 	}							\
292 								\
293 	/* Silence "never initialized" warnings. */		\
294 	DO_NOTHING_CALL_ ## which(var, name);			\
295 								\
296 	/* Exfiltrate "var". */					\
297 	memcpy(check_buf, target_start, target_size);		\
298 								\
299 	return (int)buf[0] | (int)buf[sizeof(buf) - 1];		\
300 }								\
301 DEFINE_TEST_DRIVER(name, var_type, which, xfail)
302 
303 /* Structure with no padding. */
304 struct test_packed {
305 	unsigned long one;
306 	unsigned long two;
307 	unsigned long three;
308 	unsigned long four;
309 };
310 
311 /* Simple structure with padding likely to be covered by compiler. */
312 struct test_small_hole {
313 	size_t one;
314 	char two;
315 	/* 3 byte padding hole here. */
316 	int three;
317 	unsigned long four;
318 };
319 
320 /* Trigger unhandled padding in a structure. */
321 struct test_big_hole {
322 	u8 one;
323 	u8 two;
324 	u8 three;
325 	/* 61 byte padding hole here. */
326 	u8 four __aligned(64);
327 } __aligned(64);
328 
329 struct test_trailing_hole {
330 	char *one;
331 	char *two;
332 	char *three;
333 	char four;
334 	/* "sizeof(unsigned long) - 1" byte padding hole here. */
335 };
336 
337 /* Test if STRUCTLEAK is clearing structs with __user fields. */
338 struct test_user {
339 	u8 one;
340 	unsigned long two;
341 	char __user *three;
342 	unsigned long four;
343 };
344 
345 /* No padding: all members are the same size. */
346 union test_same_sizes {
347 	unsigned long one;
348 	unsigned long two;
349 	unsigned long three;
350 	unsigned long four;
351 };
352 
353 /* Mismatched sizes, with one and two being small */
354 union test_small_start {
355 	char one:1;
356 	char two;
357 	short three;
358 	unsigned long four;
359 	struct big_struct {
360 		unsigned long array[FILL_SIZE_ARRAY];
361 	} big;
362 };
363 
364 /* Mismatched sizes, with three and four being small */
365 union test_small_end {
366 	short one;
367 	unsigned long two;
368 	char three:1;
369 	char four;
370 };
371 
372 #define ALWAYS_PASS		WANT_SUCCESS
373 #define ALWAYS_FAIL		XFAIL
374 
375 #ifdef CONFIG_INIT_STACK_NONE
376 # define INSTRUMENTED_PASS	XFAIL
377 #else
378 # define INSTRUMENTED_PASS	WANT_SUCCESS
379 #endif
380 
381 /*
382  * Clang 24 and later emit initialization on the jumps that bypass the
383  * declaration. GCC still initializes only at the declaration. Track
384  * test expectation based on CONFIG_INIT_STACK_NONE above.
385  */
386 #if defined(CONFIG_CC_IS_CLANG) && CONFIG_CLANG_VERSION >= 240000
387 # define BYPASS_PASS	INSTRUMENTED_PASS
388 #else
389 # define BYPASS_PASS	XFAIL
390 #endif
391 
392 #define DEFINE_SCALAR_TEST(name, init, xfail)			\
393 		DEFINE_TEST(name ## _ ## init, name, SCALAR,	\
394 			    init, xfail)
395 
396 #define DEFINE_SCALAR_TESTS(init, xfail)			\
397 		DEFINE_SCALAR_TEST(u8, init, xfail);		\
398 		DEFINE_SCALAR_TEST(u16, init, xfail);		\
399 		DEFINE_SCALAR_TEST(u32, init, xfail);		\
400 		DEFINE_SCALAR_TEST(u64, init, xfail);		\
401 		DEFINE_TEST(char_array_ ## init, unsigned char,	\
402 			    STRING, init, xfail)
403 
404 #define DEFINE_STRUCT_TEST(name, init, xfail)			\
405 		DEFINE_TEST(name ## _ ## init,			\
406 			    struct test_ ## name, STRUCT, init, \
407 			    xfail)
408 
409 #define DEFINE_UNION_TEST(name, init, xfail)			\
410 		DEFINE_TEST(name ## _ ## init,			\
411 			    union test_ ## name, STRUCT, init,	\
412 			    xfail)
413 
414 #define DEFINE_STRUCT_TESTS(init, xfail)			\
415 		DEFINE_STRUCT_TEST(small_hole, init, xfail);	\
416 		DEFINE_STRUCT_TEST(big_hole, init, xfail);	\
417 		DEFINE_STRUCT_TEST(trailing_hole, init, xfail);	\
418 		DEFINE_STRUCT_TEST(packed, init, xfail)
419 
420 #define DEFINE_STRUCT_INITIALIZER_TESTS(base, xfail)		\
421 		DEFINE_STRUCT_TESTS(base ## _ ## partial,	\
422 				    xfail);			\
423 		DEFINE_STRUCT_TESTS(base ## _ ## all, xfail)
424 
425 #define DEFINE_UNION_INITIALIZER_TESTS(base, xfail)		\
426 		DEFINE_UNION_TESTS(base ## _ ## partial,	\
427 				    xfail);			\
428 		DEFINE_UNION_TESTS(base ## _ ## all, xfail)
429 
430 #define DEFINE_UNION_TESTS(init, xfail)				\
431 		DEFINE_UNION_TEST(same_sizes, init, xfail);	\
432 		DEFINE_UNION_TEST(small_start, init, xfail);	\
433 		DEFINE_UNION_TEST(small_end, init, xfail);
434 
435 /* These should be fully initialized all the time! */
436 DEFINE_SCALAR_TESTS(zero, ALWAYS_PASS);
437 DEFINE_STRUCT_TESTS(zero, ALWAYS_PASS);
438 DEFINE_STRUCT_TESTS(old_zero, ALWAYS_PASS);
439 DEFINE_UNION_TESTS(zero, ALWAYS_PASS);
440 DEFINE_UNION_TESTS(old_zero, ALWAYS_PASS);
441 /* Struct initializers: padding may be left uninitialized. */
442 DEFINE_STRUCT_INITIALIZER_TESTS(static, INSTRUMENTED_PASS);
443 DEFINE_STRUCT_INITIALIZER_TESTS(dynamic, INSTRUMENTED_PASS);
444 DEFINE_STRUCT_INITIALIZER_TESTS(runtime, INSTRUMENTED_PASS);
445 DEFINE_STRUCT_INITIALIZER_TESTS(assigned_static, INSTRUMENTED_PASS);
446 DEFINE_STRUCT_INITIALIZER_TESTS(assigned_dynamic, INSTRUMENTED_PASS);
447 DEFINE_STRUCT_TESTS(assigned_copy, ALWAYS_FAIL);
448 DEFINE_UNION_INITIALIZER_TESTS(static, INSTRUMENTED_PASS);
449 DEFINE_UNION_INITIALIZER_TESTS(dynamic, INSTRUMENTED_PASS);
450 DEFINE_UNION_INITIALIZER_TESTS(runtime, INSTRUMENTED_PASS);
451 DEFINE_UNION_INITIALIZER_TESTS(assigned_static, INSTRUMENTED_PASS);
452 DEFINE_UNION_INITIALIZER_TESTS(assigned_dynamic, INSTRUMENTED_PASS);
453 DEFINE_UNION_TESTS(assigned_copy, ALWAYS_FAIL);
454 /* No initialization without compiler instrumentation. */
455 DEFINE_SCALAR_TESTS(none, INSTRUMENTED_PASS);
456 DEFINE_STRUCT_TESTS(none, INSTRUMENTED_PASS);
457 /* Initialization of members with __user attribute. */
458 DEFINE_TEST(user, struct test_user, STRUCT, none, INSTRUMENTED_PASS);
459 
460 /*
461  * Check two uses through a variable declaration outside either path,
462  * which was noticed as a special case in porting earlier stack init
463  * compiler logic.
464  */
465 static int noinline __leaf_switch_none(int path, bool fill)
466 {
467 	switch (path) {
468 		/*
469 		 * This is intentionally unreachable. To silence the
470 		 * warning, build with -Wno-switch-unreachable
471 		 */
472 		uint64_t var[10];
473 
474 	case 1:
475 		target_start = &var;
476 		target_size = sizeof(var);
477 		if (fill) {
478 			fill_start = &var;
479 			fill_size = sizeof(var);
480 
481 			memset(fill_start, (forced_mask | 0x55) & FILL_BYTE, fill_size);
482 		}
483 		memcpy(check_buf, target_start, target_size);
484 		break;
485 	case 2:
486 		target_start = &var;
487 		target_size = sizeof(var);
488 		if (fill) {
489 			fill_start = &var;
490 			fill_size = sizeof(var);
491 
492 			memset(fill_start, (forced_mask | 0xaa) & FILL_BYTE, fill_size);
493 		}
494 		memcpy(check_buf, target_start, target_size);
495 		break;
496 	default:
497 		var[1] = 5;
498 		return var[1] & forced_mask;
499 	}
500 	return 0;
501 }
502 
503 static noinline int __leaf_goto_none(bool fill)
504 {
505 	goto bypass;
506 	/*
507 	 * This declaration is jumped over by the goto above, so it is
508 	 * never reached. Compilers that initialize only at the point of
509 	 * declaration leave this variable untouched.
510 	 */
511 	uint64_t var[10];
512 
513 bypass:
514 	target_start = &var;
515 	target_size = sizeof(var);
516 	if (fill) {
517 		fill_start = &var;
518 		fill_size = sizeof(var);
519 
520 		memset(fill_start, (forced_mask | 0x55) & FILL_BYTE, fill_size);
521 	}
522 	memcpy(check_buf, target_start, target_size);
523 
524 	return 0;
525 }
526 
527 static noinline int leaf_goto_none(unsigned long sp, bool fill,
528 					  uint64_t *arg)
529 {
530 	return __leaf_goto_none(fill);
531 }
532 
533 static noinline int leaf_switch_1_none(unsigned long sp, bool fill,
534 					      uint64_t *arg)
535 {
536 	return __leaf_switch_none(1, fill);
537 }
538 
539 static noinline int leaf_switch_2_none(unsigned long sp, bool fill,
540 					      uint64_t *arg)
541 {
542 	return __leaf_switch_none(2, fill);
543 }
544 
545 /*
546  * A declaration whose definition point is jumped over, either by a switch
547  * dispatch reaching a "case" label below it or by a goto, used to be left
548  * uninitialized: the initialization is emitted where the variable is
549  * declared, and that point is never reached.
550  */
551 DEFINE_TEST_DRIVER(switch_1_none, uint64_t, SCALAR, BYPASS_PASS);
552 DEFINE_TEST_DRIVER(switch_2_none, uint64_t, SCALAR, BYPASS_PASS);
553 DEFINE_TEST_DRIVER(goto_none, uint64_t, SCALAR, BYPASS_PASS);
554 
555 #define KUNIT_test_scalars(init)			\
556 		KUNIT_CASE(test_u8_ ## init),		\
557 		KUNIT_CASE(test_u16_ ## init),		\
558 		KUNIT_CASE(test_u32_ ## init),		\
559 		KUNIT_CASE(test_u64_ ## init),		\
560 		KUNIT_CASE(test_char_array_ ## init)
561 
562 #define KUNIT_test_structs(init)			\
563 		KUNIT_CASE(test_small_hole_ ## init),	\
564 		KUNIT_CASE(test_big_hole_ ## init),	\
565 		KUNIT_CASE(test_trailing_hole_ ## init),\
566 		KUNIT_CASE(test_packed_ ## init)	\
567 
568 #define KUNIT_test_unions(init)				\
569 		KUNIT_CASE(test_same_sizes_ ## init),	\
570 		KUNIT_CASE(test_small_start_ ## init),	\
571 		KUNIT_CASE(test_small_end_ ## init)	\
572 
573 static struct kunit_case stackinit_test_cases[] = {
574 	/* These are explicitly initialized and should always pass. */
575 	KUNIT_test_scalars(zero),
576 	KUNIT_test_structs(zero),
577 	KUNIT_test_structs(old_zero),
578 	KUNIT_test_unions(zero),
579 	KUNIT_test_unions(old_zero),
580 	/* Padding here appears to be accidentally always initialized? */
581 	KUNIT_test_structs(dynamic_partial),
582 	KUNIT_test_structs(assigned_dynamic_partial),
583 	KUNIT_test_unions(dynamic_partial),
584 	KUNIT_test_unions(assigned_dynamic_partial),
585 	/* Padding initialization depends on compiler behaviors. */
586 	KUNIT_test_structs(static_partial),
587 	KUNIT_test_structs(static_all),
588 	KUNIT_test_structs(dynamic_all),
589 	KUNIT_test_structs(runtime_partial),
590 	KUNIT_test_structs(runtime_all),
591 	KUNIT_test_structs(assigned_static_partial),
592 	KUNIT_test_structs(assigned_static_all),
593 	KUNIT_test_structs(assigned_dynamic_all),
594 	KUNIT_test_unions(static_partial),
595 	KUNIT_test_unions(static_all),
596 	KUNIT_test_unions(dynamic_all),
597 	KUNIT_test_unions(runtime_partial),
598 	KUNIT_test_unions(runtime_all),
599 	KUNIT_test_unions(assigned_static_partial),
600 	KUNIT_test_unions(assigned_static_all),
601 	KUNIT_test_unions(assigned_dynamic_all),
602 	/* Everything fails this since it effectively performs a memcpy(). */
603 	KUNIT_test_structs(assigned_copy),
604 	KUNIT_test_unions(assigned_copy),
605 	/* STRUCTLEAK_BYREF_ALL should cover everything from here down. */
606 	KUNIT_test_scalars(none),
607 	KUNIT_CASE(test_switch_1_none),
608 	KUNIT_CASE(test_switch_2_none),
609 	KUNIT_CASE(test_goto_none),
610 	/* STRUCTLEAK_BYREF should cover from here down. */
611 	KUNIT_test_structs(none),
612 	/* STRUCTLEAK will only cover this. */
613 	KUNIT_CASE(test_user),
614 	{}
615 };
616 
617 static struct kunit_suite stackinit_test_suite = {
618 	.name = "stackinit",
619 	.test_cases = stackinit_test_cases,
620 };
621 
622 kunit_test_suites(&stackinit_test_suite);
623 
624 MODULE_DESCRIPTION("Test cases for compiler-based stack variable zeroing");
625 MODULE_LICENSE("GPL");
626