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