xref: /linux/lib/overflow_kunit.c (revision 705c09bb3cdffb141986598ad4ff9c9b0a66c3bd)
1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /*
3  * Test cases for arithmetic overflow checks. See:
4  * "Running tests with kunit_tool" at Documentation/dev-tools/kunit/start.rst
5  *	./tools/testing/kunit/kunit.py run overflow [--raw_output]
6  */
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 
9 #include <kunit/device.h>
10 #include <kunit/test.h>
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/mm.h>
14 #include <linux/module.h>
15 #include <linux/overflow.h>
16 #include <linux/slab.h>
17 #include <linux/types.h>
18 #include <linux/vmalloc.h>
19 
20 #define SKIP(cond, reason)		do {			\
21 	if (cond) {						\
22 		kunit_skip(test, reason);			\
23 		return;						\
24 	}							\
25 } while (0)
26 
27 /*
28  * Clang 11 and earlier generate unwanted libcalls for signed output
29  * on unsigned input.
30  */
31 #if defined(CONFIG_CC_IS_CLANG) && __clang_major__ <= 11
32 # define SKIP_SIGN_MISMATCH(t)	SKIP(t, "Clang 11 unwanted libcalls")
33 #else
34 # define SKIP_SIGN_MISMATCH(t)	do { } while (0)
35 #endif
36 
37 /*
38  * Clang 13 and earlier generate unwanted libcalls for 64-bit tests on
39  * 32-bit hosts.
40  */
41 #if defined(CONFIG_CC_IS_CLANG) && __clang_major__ <= 13 &&	\
42     BITS_PER_LONG != 64
43 # define SKIP_64_ON_32(t)	SKIP(t, "Clang 13 unwanted libcalls")
44 #else
45 # define SKIP_64_ON_32(t)	do { } while (0)
46 #endif
47 
48 #define DEFINE_TEST_ARRAY_TYPED(t1, t2, t)			\
49 	static const struct test_ ## t1 ## _ ## t2 ## __ ## t {	\
50 		t1 a;						\
51 		t2 b;						\
52 		t sum, diff, prod;				\
53 		bool s_of, d_of, p_of;				\
54 	} t1 ## _ ## t2 ## __ ## t ## _tests[]
55 
56 #define DEFINE_TEST_ARRAY(t)	DEFINE_TEST_ARRAY_TYPED(t, t, t)
57 
58 DEFINE_TEST_ARRAY(u8) = {
59 	{0, 0, 0, 0, 0, false, false, false},
60 	{1, 1, 2, 0, 1, false, false, false},
61 	{0, 1, 1, U8_MAX, 0, false, true, false},
62 	{1, 0, 1, 1, 0, false, false, false},
63 	{0, U8_MAX, U8_MAX, 1, 0, false, true, false},
64 	{U8_MAX, 0, U8_MAX, U8_MAX, 0, false, false, false},
65 	{1, U8_MAX, 0, 2, U8_MAX, true, true, false},
66 	{U8_MAX, 1, 0, U8_MAX-1, U8_MAX, true, false, false},
67 	{U8_MAX, U8_MAX, U8_MAX-1, 0, 1, true, false, true},
68 
69 	{U8_MAX, U8_MAX-1, U8_MAX-2, 1, 2, true, false, true},
70 	{U8_MAX-1, U8_MAX, U8_MAX-2, U8_MAX, 2, true, true, true},
71 
72 	{1U << 3, 1U << 3, 1U << 4, 0, 1U << 6, false, false, false},
73 	{1U << 4, 1U << 4, 1U << 5, 0, 0, false, false, true},
74 	{1U << 4, 1U << 3, 3*(1U << 3), 1U << 3, 1U << 7, false, false, false},
75 	{1U << 7, 1U << 7, 0, 0, 0, true, false, true},
76 
77 	{48, 32, 80, 16, 0, false, false, true},
78 	{128, 128, 0, 0, 0, true, false, true},
79 	{123, 234, 101, 145, 110, true, true, true},
80 };
81 DEFINE_TEST_ARRAY(u16) = {
82 	{0, 0, 0, 0, 0, false, false, false},
83 	{1, 1, 2, 0, 1, false, false, false},
84 	{0, 1, 1, U16_MAX, 0, false, true, false},
85 	{1, 0, 1, 1, 0, false, false, false},
86 	{0, U16_MAX, U16_MAX, 1, 0, false, true, false},
87 	{U16_MAX, 0, U16_MAX, U16_MAX, 0, false, false, false},
88 	{1, U16_MAX, 0, 2, U16_MAX, true, true, false},
89 	{U16_MAX, 1, 0, U16_MAX-1, U16_MAX, true, false, false},
90 	{U16_MAX, U16_MAX, U16_MAX-1, 0, 1, true, false, true},
91 
92 	{U16_MAX, U16_MAX-1, U16_MAX-2, 1, 2, true, false, true},
93 	{U16_MAX-1, U16_MAX, U16_MAX-2, U16_MAX, 2, true, true, true},
94 
95 	{1U << 7, 1U << 7, 1U << 8, 0, 1U << 14, false, false, false},
96 	{1U << 8, 1U << 8, 1U << 9, 0, 0, false, false, true},
97 	{1U << 8, 1U << 7, 3*(1U << 7), 1U << 7, 1U << 15, false, false, false},
98 	{1U << 15, 1U << 15, 0, 0, 0, true, false, true},
99 
100 	{123, 234, 357, 65425, 28782, false, true, false},
101 	{1234, 2345, 3579, 64425, 10146, false, true, true},
102 };
103 DEFINE_TEST_ARRAY(u32) = {
104 	{0, 0, 0, 0, 0, false, false, false},
105 	{1, 1, 2, 0, 1, false, false, false},
106 	{0, 1, 1, U32_MAX, 0, false, true, false},
107 	{1, 0, 1, 1, 0, false, false, false},
108 	{0, U32_MAX, U32_MAX, 1, 0, false, true, false},
109 	{U32_MAX, 0, U32_MAX, U32_MAX, 0, false, false, false},
110 	{1, U32_MAX, 0, 2, U32_MAX, true, true, false},
111 	{U32_MAX, 1, 0, U32_MAX-1, U32_MAX, true, false, false},
112 	{U32_MAX, U32_MAX, U32_MAX-1, 0, 1, true, false, true},
113 
114 	{U32_MAX, U32_MAX-1, U32_MAX-2, 1, 2, true, false, true},
115 	{U32_MAX-1, U32_MAX, U32_MAX-2, U32_MAX, 2, true, true, true},
116 
117 	{1U << 15, 1U << 15, 1U << 16, 0, 1U << 30, false, false, false},
118 	{1U << 16, 1U << 16, 1U << 17, 0, 0, false, false, true},
119 	{1U << 16, 1U << 15, 3*(1U << 15), 1U << 15, 1U << 31, false, false, false},
120 	{1U << 31, 1U << 31, 0, 0, 0, true, false, true},
121 
122 	{-2U, 1U, -1U, -3U, -2U, false, false, false},
123 	{-4U, 5U, 1U, -9U, -20U, true, false, true},
124 };
125 
126 DEFINE_TEST_ARRAY(u64) = {
127 	{0, 0, 0, 0, 0, false, false, false},
128 	{1, 1, 2, 0, 1, false, false, false},
129 	{0, 1, 1, U64_MAX, 0, false, true, false},
130 	{1, 0, 1, 1, 0, false, false, false},
131 	{0, U64_MAX, U64_MAX, 1, 0, false, true, false},
132 	{U64_MAX, 0, U64_MAX, U64_MAX, 0, false, false, false},
133 	{1, U64_MAX, 0, 2, U64_MAX, true, true, false},
134 	{U64_MAX, 1, 0, U64_MAX-1, U64_MAX, true, false, false},
135 	{U64_MAX, U64_MAX, U64_MAX-1, 0, 1, true, false, true},
136 
137 	{U64_MAX, U64_MAX-1, U64_MAX-2, 1, 2, true, false, true},
138 	{U64_MAX-1, U64_MAX, U64_MAX-2, U64_MAX, 2, true, true, true},
139 
140 	{1ULL << 31, 1ULL << 31, 1ULL << 32, 0, 1ULL << 62, false, false, false},
141 	{1ULL << 32, 1ULL << 32, 1ULL << 33, 0, 0, false, false, true},
142 	{1ULL << 32, 1ULL << 31, 3*(1ULL << 31), 1ULL << 31, 1ULL << 63, false, false, false},
143 	{1ULL << 63, 1ULL << 63, 0, 0, 0, true, false, true},
144 	{1000000000ULL /* 10^9 */, 10000000000ULL /* 10^10 */,
145 	 11000000000ULL, 18446744064709551616ULL, 10000000000000000000ULL,
146 	 false, true, false},
147 	{-15ULL, 10ULL, -5ULL, -25ULL, -150ULL, false, false, true},
148 };
149 
150 DEFINE_TEST_ARRAY(s8) = {
151 	{0, 0, 0, 0, 0, false, false, false},
152 
153 	{0, S8_MAX, S8_MAX, -S8_MAX, 0, false, false, false},
154 	{S8_MAX, 0, S8_MAX, S8_MAX, 0, false, false, false},
155 	{0, S8_MIN, S8_MIN, S8_MIN, 0, false, true, false},
156 	{S8_MIN, 0, S8_MIN, S8_MIN, 0, false, false, false},
157 
158 	{-1, S8_MIN, S8_MAX, S8_MAX, S8_MIN, true, false, true},
159 	{S8_MIN, -1, S8_MAX, -S8_MAX, S8_MIN, true, false, true},
160 	{-1, S8_MAX, S8_MAX-1, S8_MIN, -S8_MAX, false, false, false},
161 	{S8_MAX, -1, S8_MAX-1, S8_MIN, -S8_MAX, false, true, false},
162 	{-1, -S8_MAX, S8_MIN, S8_MAX-1, S8_MAX, false, false, false},
163 	{-S8_MAX, -1, S8_MIN, S8_MIN+2, S8_MAX, false, false, false},
164 
165 	{1, S8_MIN, -S8_MAX, -S8_MAX, S8_MIN, false, true, false},
166 	{S8_MIN, 1, -S8_MAX, S8_MAX, S8_MIN, false, true, false},
167 	{1, S8_MAX, S8_MIN, S8_MIN+2, S8_MAX, true, false, false},
168 	{S8_MAX, 1, S8_MIN, S8_MAX-1, S8_MAX, true, false, false},
169 
170 	{S8_MIN, S8_MIN, 0, 0, 0, true, false, true},
171 	{S8_MAX, S8_MAX, -2, 0, 1, true, false, true},
172 
173 	{-4, -32, -36, 28, -128, false, false, true},
174 	{-4, 32, 28, -36, -128, false, false, false},
175 };
176 
177 DEFINE_TEST_ARRAY(s16) = {
178 	{0, 0, 0, 0, 0, false, false, false},
179 
180 	{0, S16_MAX, S16_MAX, -S16_MAX, 0, false, false, false},
181 	{S16_MAX, 0, S16_MAX, S16_MAX, 0, false, false, false},
182 	{0, S16_MIN, S16_MIN, S16_MIN, 0, false, true, false},
183 	{S16_MIN, 0, S16_MIN, S16_MIN, 0, false, false, false},
184 
185 	{-1, S16_MIN, S16_MAX, S16_MAX, S16_MIN, true, false, true},
186 	{S16_MIN, -1, S16_MAX, -S16_MAX, S16_MIN, true, false, true},
187 	{-1, S16_MAX, S16_MAX-1, S16_MIN, -S16_MAX, false, false, false},
188 	{S16_MAX, -1, S16_MAX-1, S16_MIN, -S16_MAX, false, true, false},
189 	{-1, -S16_MAX, S16_MIN, S16_MAX-1, S16_MAX, false, false, false},
190 	{-S16_MAX, -1, S16_MIN, S16_MIN+2, S16_MAX, false, false, false},
191 
192 	{1, S16_MIN, -S16_MAX, -S16_MAX, S16_MIN, false, true, false},
193 	{S16_MIN, 1, -S16_MAX, S16_MAX, S16_MIN, false, true, false},
194 	{1, S16_MAX, S16_MIN, S16_MIN+2, S16_MAX, true, false, false},
195 	{S16_MAX, 1, S16_MIN, S16_MAX-1, S16_MAX, true, false, false},
196 
197 	{S16_MIN, S16_MIN, 0, 0, 0, true, false, true},
198 	{S16_MAX, S16_MAX, -2, 0, 1, true, false, true},
199 };
200 DEFINE_TEST_ARRAY(s32) = {
201 	{0, 0, 0, 0, 0, false, false, false},
202 
203 	{0, S32_MAX, S32_MAX, -S32_MAX, 0, false, false, false},
204 	{S32_MAX, 0, S32_MAX, S32_MAX, 0, false, false, false},
205 	{0, S32_MIN, S32_MIN, S32_MIN, 0, false, true, false},
206 	{S32_MIN, 0, S32_MIN, S32_MIN, 0, false, false, false},
207 
208 	{-1, S32_MIN, S32_MAX, S32_MAX, S32_MIN, true, false, true},
209 	{S32_MIN, -1, S32_MAX, -S32_MAX, S32_MIN, true, false, true},
210 	{-1, S32_MAX, S32_MAX-1, S32_MIN, -S32_MAX, false, false, false},
211 	{S32_MAX, -1, S32_MAX-1, S32_MIN, -S32_MAX, false, true, false},
212 	{-1, -S32_MAX, S32_MIN, S32_MAX-1, S32_MAX, false, false, false},
213 	{-S32_MAX, -1, S32_MIN, S32_MIN+2, S32_MAX, false, false, false},
214 
215 	{1, S32_MIN, -S32_MAX, -S32_MAX, S32_MIN, false, true, false},
216 	{S32_MIN, 1, -S32_MAX, S32_MAX, S32_MIN, false, true, false},
217 	{1, S32_MAX, S32_MIN, S32_MIN+2, S32_MAX, true, false, false},
218 	{S32_MAX, 1, S32_MIN, S32_MAX-1, S32_MAX, true, false, false},
219 
220 	{S32_MIN, S32_MIN, 0, 0, 0, true, false, true},
221 	{S32_MAX, S32_MAX, -2, 0, 1, true, false, true},
222 };
223 
224 DEFINE_TEST_ARRAY(s64) = {
225 	{0, 0, 0, 0, 0, false, false, false},
226 
227 	{0, S64_MAX, S64_MAX, -S64_MAX, 0, false, false, false},
228 	{S64_MAX, 0, S64_MAX, S64_MAX, 0, false, false, false},
229 	{0, S64_MIN, S64_MIN, S64_MIN, 0, false, true, false},
230 	{S64_MIN, 0, S64_MIN, S64_MIN, 0, false, false, false},
231 
232 	{-1, S64_MIN, S64_MAX, S64_MAX, S64_MIN, true, false, true},
233 	{S64_MIN, -1, S64_MAX, -S64_MAX, S64_MIN, true, false, true},
234 	{-1, S64_MAX, S64_MAX-1, S64_MIN, -S64_MAX, false, false, false},
235 	{S64_MAX, -1, S64_MAX-1, S64_MIN, -S64_MAX, false, true, false},
236 	{-1, -S64_MAX, S64_MIN, S64_MAX-1, S64_MAX, false, false, false},
237 	{-S64_MAX, -1, S64_MIN, S64_MIN+2, S64_MAX, false, false, false},
238 
239 	{1, S64_MIN, -S64_MAX, -S64_MAX, S64_MIN, false, true, false},
240 	{S64_MIN, 1, -S64_MAX, S64_MAX, S64_MIN, false, true, false},
241 	{1, S64_MAX, S64_MIN, S64_MIN+2, S64_MAX, true, false, false},
242 	{S64_MAX, 1, S64_MIN, S64_MAX-1, S64_MAX, true, false, false},
243 
244 	{S64_MIN, S64_MIN, 0, 0, 0, true, false, true},
245 	{S64_MAX, S64_MAX, -2, 0, 1, true, false, true},
246 
247 	{-1, -1, -2, 0, 1, false, false, false},
248 	{-1, -128, -129, 127, 128, false, false, false},
249 	{-128, -1, -129, -127, 128, false, false, false},
250 	{0, -S64_MAX, -S64_MAX, S64_MAX, 0, false, false, false},
251 };
252 
253 #define check_one_op(t, fmt, op, sym, a, b, r, of) do {			\
254 	int _a_orig = a, _a_bump = a + 1;				\
255 	int _b_orig = b, _b_bump = b + 1;				\
256 	bool _of;							\
257 	t _r;								\
258 									\
259 	_of = check_ ## op ## _overflow(a, b, &_r);			\
260 	KUNIT_EXPECT_EQ_MSG(test, _of, of,				\
261 		"expected check "fmt" "sym" "fmt" to%s overflow (type %s)\n",	\
262 		a, b, of ? "" : " not", #t);				\
263 	KUNIT_EXPECT_EQ_MSG(test, _r, r,				\
264 		"expected check "fmt" "sym" "fmt" == "fmt", got "fmt" (type %s)\n", \
265 		a, b, r, _r, #t);					\
266 	/* Check for internal macro side-effects. */			\
267 	_of = check_ ## op ## _overflow(_a_orig++, _b_orig++, &_r);	\
268 	KUNIT_EXPECT_EQ_MSG(test, _a_orig, _a_bump,			\
269 		"Unexpected check " #op " macro side-effect!\n");	\
270 	KUNIT_EXPECT_EQ_MSG(test, _b_orig, _b_bump,			\
271 		"Unexpected check " #op " macro side-effect!\n");	\
272 									\
273 	_r = wrapping_ ## op(t, a, b);					\
274 	KUNIT_EXPECT_TRUE_MSG(test, _r == r,				\
275 		"expected wrap "fmt" "sym" "fmt" == "fmt", got "fmt" (type %s)\n", \
276 		a, b, r, _r, #t);					\
277 	/* Check for internal macro side-effects. */			\
278 	_a_orig = a;							\
279 	_b_orig = b;							\
280 	_r = wrapping_ ## op(t, _a_orig++, _b_orig++);			\
281 	KUNIT_EXPECT_EQ_MSG(test, _a_orig, _a_bump,			\
282 		"Unexpected wrap " #op " macro side-effect!\n");	\
283 	KUNIT_EXPECT_EQ_MSG(test, _b_orig, _b_bump,			\
284 		"Unexpected wrap " #op " macro side-effect!\n");	\
285 } while (0)
286 
287 static int global_counter;
288 static void bump_counter(void)
289 {
290 	global_counter++;
291 }
292 
293 static int get_index(void)
294 {
295 	volatile int index = 0;
296 	bump_counter();
297 	return index;
298 }
299 
300 #define check_self_op(fmt, op, sym, a, b) do {				\
301 	typeof(a + 0) _a = a;						\
302 	typeof(b + 0) _b = b;						\
303 	typeof(a + 0) _a_sym = a;					\
304 	typeof(a + 0) _a_orig[1] = { a };				\
305 	typeof(b + 0) _b_orig = b;					\
306 	typeof(b + 0) _b_bump = b + 1;					\
307 	typeof(a + 0) _r;						\
308 									\
309 	_a_sym sym _b;							\
310 	_r = wrapping_ ## op(_a, _b);					\
311 	KUNIT_EXPECT_TRUE_MSG(test, _r == _a_sym,			\
312 		"expected "fmt" "#op" "fmt" == "fmt", got "fmt"\n",	\
313 		a, b, _a_sym, _r);					\
314 	KUNIT_EXPECT_TRUE_MSG(test, _a == _a_sym,			\
315 		"expected "fmt" "#op" "fmt" == "fmt", got "fmt"\n",	\
316 		a, b, _a_sym, _a);					\
317 	/* Check for internal macro side-effects. */			\
318 	global_counter = 0;						\
319 	wrapping_ ## op(_a_orig[get_index()], _b_orig++);		\
320 	KUNIT_EXPECT_EQ_MSG(test, global_counter, 1,			\
321 		"Unexpected wrapping_" #op " macro side-effect on arg1!\n"); \
322 	KUNIT_EXPECT_EQ_MSG(test, _b_orig, _b_bump,			\
323 		"Unexpected wrapping_" #op " macro side-effect on arg2!\n"); \
324 } while (0)
325 
326 #define DEFINE_TEST_FUNC_TYPED(n, t, fmt)				\
327 static void do_test_ ## n(struct kunit *test, const struct test_ ## n *p) \
328 {									\
329 	/* check_{add,sub,mul}_overflow() and wrapping_{add,sub,mul} */	\
330 	check_one_op(t, fmt, add, "+", p->a, p->b, p->sum, p->s_of);	\
331 	check_one_op(t, fmt, add, "+", p->b, p->a, p->sum, p->s_of);	\
332 	check_one_op(t, fmt, sub, "-", p->a, p->b, p->diff, p->d_of);	\
333 	check_one_op(t, fmt, mul, "*", p->a, p->b, p->prod, p->p_of);	\
334 	check_one_op(t, fmt, mul, "*", p->b, p->a, p->prod, p->p_of);	\
335 	/* wrapping_assign_{add,sub}() */				\
336 	check_self_op(fmt, assign_add, +=, p->a, p->b);			\
337 	check_self_op(fmt, assign_add, +=, p->b, p->a);			\
338 	check_self_op(fmt, assign_sub, -=, p->a, p->b);			\
339 }									\
340 									\
341 static void n ## _overflow_test(struct kunit *test) {			\
342 	unsigned i;							\
343 									\
344 	SKIP_64_ON_32(__same_type(t, u64));				\
345 	SKIP_64_ON_32(__same_type(t, s64));				\
346 	SKIP_SIGN_MISMATCH(__same_type(n ## _tests[0].a, u32) &&	\
347 			   __same_type(n ## _tests[0].b, u32) &&	\
348 			   __same_type(n ## _tests[0].sum, int));	\
349 									\
350 	for (i = 0; i < ARRAY_SIZE(n ## _tests); ++i)			\
351 		do_test_ ## n(test, &n ## _tests[i]);			\
352 	kunit_info(test, "%zu %s arithmetic tests finished\n",		\
353 		ARRAY_SIZE(n ## _tests), #n);				\
354 }
355 
356 #define DEFINE_TEST_FUNC(t, fmt)					\
357 	DEFINE_TEST_FUNC_TYPED(t ## _ ## t ## __ ## t, t, fmt)
358 
359 DEFINE_TEST_FUNC(u8, "%d");
360 DEFINE_TEST_FUNC(s8, "%d");
361 DEFINE_TEST_FUNC(u16, "%d");
362 DEFINE_TEST_FUNC(s16, "%d");
363 DEFINE_TEST_FUNC(u32, "%u");
364 DEFINE_TEST_FUNC(s32, "%d");
365 DEFINE_TEST_FUNC(u64, "%llu");
366 DEFINE_TEST_FUNC(s64, "%lld");
367 
368 DEFINE_TEST_ARRAY_TYPED(u32, u32, u8) = {
369 	{0, 0, 0, 0, 0, false, false, false},
370 	{U8_MAX, 2, 1, U8_MAX - 2, U8_MAX - 1, true, false, true},
371 	{U8_MAX + 1, 0, 0, 0, 0, true, true, false},
372 };
373 DEFINE_TEST_FUNC_TYPED(u32_u32__u8, u8, "%d");
374 
375 DEFINE_TEST_ARRAY_TYPED(u32, u32, int) = {
376 	{0, 0, 0, 0, 0, false, false, false},
377 	{U32_MAX, 0, -1, -1, 0, true, true, false},
378 };
379 DEFINE_TEST_FUNC_TYPED(u32_u32__int, int, "%d");
380 
381 DEFINE_TEST_ARRAY_TYPED(u8, u8, int) = {
382 	{0, 0, 0, 0, 0, false, false, false},
383 	{U8_MAX, U8_MAX, 2 * U8_MAX, 0, U8_MAX * U8_MAX, false, false, false},
384 	{1, 2, 3, -1, 2, false, false, false},
385 };
386 DEFINE_TEST_FUNC_TYPED(u8_u8__int, int, "%d");
387 
388 DEFINE_TEST_ARRAY_TYPED(int, int, u8) = {
389 	{0, 0, 0, 0, 0, false, false, false},
390 	{1, 2, 3, U8_MAX, 2, false, true, false},
391 	{-1, 0, U8_MAX, U8_MAX, 0, true, true, false},
392 };
393 DEFINE_TEST_FUNC_TYPED(int_int__u8, u8, "%d");
394 
395 /* Args are: value, shift, type, expected result, overflow expected */
396 #define TEST_ONE_SHIFT(a, s, t, expect, of)	do {			\
397 	typeof(a) __a = (a);						\
398 	typeof(s) __s = (s);						\
399 	t __e = (expect);						\
400 	t __d;								\
401 	bool __of = check_shl_overflow(__a, __s, &__d);			\
402 	if (__of != of) {						\
403 		KUNIT_EXPECT_EQ_MSG(test, __of, of,			\
404 			"expected (%s)(%s << %s) to%s overflow\n",	\
405 			#t, #a, #s, of ? "" : " not");			\
406 	} else if (!__of && __d != __e) {				\
407 		KUNIT_EXPECT_EQ_MSG(test, __d, __e,			\
408 			"expected (%s)(%s << %s) == %s\n",		\
409 			#t, #a, #s, #expect);				\
410 		if ((t)-1 < 0)						\
411 			kunit_info(test, "got %lld\n", (s64)__d);	\
412 		else							\
413 			kunit_info(test, "got %llu\n", (u64)__d);	\
414 	}								\
415 	count++;							\
416 } while (0)
417 
418 static void shift_sane_test(struct kunit *test)
419 {
420 	int count = 0;
421 
422 	/* Sane shifts. */
423 	TEST_ONE_SHIFT(1, 0, u8, 1 << 0, false);
424 	TEST_ONE_SHIFT(1, 4, u8, 1 << 4, false);
425 	TEST_ONE_SHIFT(1, 7, u8, 1 << 7, false);
426 	TEST_ONE_SHIFT(0xF, 4, u8, 0xF << 4, false);
427 	TEST_ONE_SHIFT(1, 0, u16, 1 << 0, false);
428 	TEST_ONE_SHIFT(1, 10, u16, 1 << 10, false);
429 	TEST_ONE_SHIFT(1, 15, u16, 1 << 15, false);
430 	TEST_ONE_SHIFT(0xFF, 8, u16, 0xFF << 8, false);
431 	TEST_ONE_SHIFT(1, 0, int, 1 << 0, false);
432 	TEST_ONE_SHIFT(1, 16, int, 1 << 16, false);
433 	TEST_ONE_SHIFT(1, 30, int, 1 << 30, false);
434 	TEST_ONE_SHIFT(1, 0, s32, 1 << 0, false);
435 	TEST_ONE_SHIFT(1, 16, s32, 1 << 16, false);
436 	TEST_ONE_SHIFT(1, 30, s32, 1 << 30, false);
437 	TEST_ONE_SHIFT(1, 0, unsigned int, 1U << 0, false);
438 	TEST_ONE_SHIFT(1, 20, unsigned int, 1U << 20, false);
439 	TEST_ONE_SHIFT(1, 31, unsigned int, 1U << 31, false);
440 	TEST_ONE_SHIFT(0xFFFFU, 16, unsigned int, 0xFFFFU << 16, false);
441 	TEST_ONE_SHIFT(1, 0, u32, 1U << 0, false);
442 	TEST_ONE_SHIFT(1, 20, u32, 1U << 20, false);
443 	TEST_ONE_SHIFT(1, 31, u32, 1U << 31, false);
444 	TEST_ONE_SHIFT(0xFFFFU, 16, u32, 0xFFFFU << 16, false);
445 	TEST_ONE_SHIFT(1, 0, u64, 1ULL << 0, false);
446 	TEST_ONE_SHIFT(1, 40, u64, 1ULL << 40, false);
447 	TEST_ONE_SHIFT(1, 63, u64, 1ULL << 63, false);
448 	TEST_ONE_SHIFT(0xFFFFFFFFULL, 32, u64, 0xFFFFFFFFULL << 32, false);
449 
450 	/* Sane shift: start and end with 0, without a too-wide shift. */
451 	TEST_ONE_SHIFT(0, 7, u8, 0, false);
452 	TEST_ONE_SHIFT(0, 15, u16, 0, false);
453 	TEST_ONE_SHIFT(0, 31, unsigned int, 0, false);
454 	TEST_ONE_SHIFT(0, 31, u32, 0, false);
455 	TEST_ONE_SHIFT(0, 63, u64, 0, false);
456 
457 	/* Sane shift: start and end with 0, without reaching signed bit. */
458 	TEST_ONE_SHIFT(0, 6, s8, 0, false);
459 	TEST_ONE_SHIFT(0, 14, s16, 0, false);
460 	TEST_ONE_SHIFT(0, 30, int, 0, false);
461 	TEST_ONE_SHIFT(0, 30, s32, 0, false);
462 	TEST_ONE_SHIFT(0, 62, s64, 0, false);
463 
464 	kunit_info(test, "%d sane shift tests finished\n", count);
465 }
466 
467 static void shift_overflow_test(struct kunit *test)
468 {
469 	int count = 0;
470 
471 	/* Overflow: shifted the bit off the end. */
472 	TEST_ONE_SHIFT(1, 8, u8, 0, true);
473 	TEST_ONE_SHIFT(1, 16, u16, 0, true);
474 	TEST_ONE_SHIFT(1, 32, unsigned int, 0, true);
475 	TEST_ONE_SHIFT(1, 32, u32, 0, true);
476 	TEST_ONE_SHIFT(1, 64, u64, 0, true);
477 
478 	/* Overflow: shifted into the signed bit. */
479 	TEST_ONE_SHIFT(1, 7, s8, 0, true);
480 	TEST_ONE_SHIFT(1, 15, s16, 0, true);
481 	TEST_ONE_SHIFT(1, 31, int, 0, true);
482 	TEST_ONE_SHIFT(1, 31, s32, 0, true);
483 	TEST_ONE_SHIFT(1, 63, s64, 0, true);
484 
485 	/* Overflow: high bit falls off unsigned types. */
486 	/* 10010110 */
487 	TEST_ONE_SHIFT(150, 1, u8, 0, true);
488 	/* 1000100010010110 */
489 	TEST_ONE_SHIFT(34966, 1, u16, 0, true);
490 	/* 10000100000010001000100010010110 */
491 	TEST_ONE_SHIFT(2215151766U, 1, u32, 0, true);
492 	TEST_ONE_SHIFT(2215151766U, 1, unsigned int, 0, true);
493 	/* 1000001000010000010000000100000010000100000010001000100010010110 */
494 	TEST_ONE_SHIFT(9372061470395238550ULL, 1, u64, 0, true);
495 
496 	/* Overflow: bit shifted into signed bit on signed types. */
497 	/* 01001011 */
498 	TEST_ONE_SHIFT(75, 1, s8, 0, true);
499 	/* 0100010001001011 */
500 	TEST_ONE_SHIFT(17483, 1, s16, 0, true);
501 	/* 01000010000001000100010001001011 */
502 	TEST_ONE_SHIFT(1107575883, 1, s32, 0, true);
503 	TEST_ONE_SHIFT(1107575883, 1, int, 0, true);
504 	/* 0100000100001000001000000010000001000010000001000100010001001011 */
505 	TEST_ONE_SHIFT(4686030735197619275LL, 1, s64, 0, true);
506 
507 	/* Overflow: bit shifted past signed bit on signed types. */
508 	/* 01001011 */
509 	TEST_ONE_SHIFT(75, 2, s8, 0, true);
510 	/* 0100010001001011 */
511 	TEST_ONE_SHIFT(17483, 2, s16, 0, true);
512 	/* 01000010000001000100010001001011 */
513 	TEST_ONE_SHIFT(1107575883, 2, s32, 0, true);
514 	TEST_ONE_SHIFT(1107575883, 2, int, 0, true);
515 	/* 0100000100001000001000000010000001000010000001000100010001001011 */
516 	TEST_ONE_SHIFT(4686030735197619275LL, 2, s64, 0, true);
517 
518 	kunit_info(test, "%d overflow shift tests finished\n", count);
519 }
520 
521 static void shift_truncate_test(struct kunit *test)
522 {
523 	int count = 0;
524 
525 	/* Overflow: values larger than destination type. */
526 	TEST_ONE_SHIFT(0x100, 0, u8, 0, true);
527 	TEST_ONE_SHIFT(0xFF, 0, s8, 0, true);
528 	TEST_ONE_SHIFT(0x10000U, 0, u16, 0, true);
529 	TEST_ONE_SHIFT(0xFFFFU, 0, s16, 0, true);
530 	TEST_ONE_SHIFT(0x100000000ULL, 0, u32, 0, true);
531 	TEST_ONE_SHIFT(0x100000000ULL, 0, unsigned int, 0, true);
532 	TEST_ONE_SHIFT(0xFFFFFFFFUL, 0, s32, 0, true);
533 	TEST_ONE_SHIFT(0xFFFFFFFFUL, 0, int, 0, true);
534 	TEST_ONE_SHIFT(0xFFFFFFFFFFFFFFFFULL, 0, s64, 0, true);
535 
536 	/* Overflow: shifted at or beyond entire type's bit width. */
537 	TEST_ONE_SHIFT(0, 8, u8, 0, true);
538 	TEST_ONE_SHIFT(0, 9, u8, 0, true);
539 	TEST_ONE_SHIFT(0, 8, s8, 0, true);
540 	TEST_ONE_SHIFT(0, 9, s8, 0, true);
541 	TEST_ONE_SHIFT(0, 16, u16, 0, true);
542 	TEST_ONE_SHIFT(0, 17, u16, 0, true);
543 	TEST_ONE_SHIFT(0, 16, s16, 0, true);
544 	TEST_ONE_SHIFT(0, 17, s16, 0, true);
545 	TEST_ONE_SHIFT(0, 32, u32, 0, true);
546 	TEST_ONE_SHIFT(0, 33, u32, 0, true);
547 	TEST_ONE_SHIFT(0, 32, int, 0, true);
548 	TEST_ONE_SHIFT(0, 33, int, 0, true);
549 	TEST_ONE_SHIFT(0, 32, s32, 0, true);
550 	TEST_ONE_SHIFT(0, 33, s32, 0, true);
551 	TEST_ONE_SHIFT(0, 64, u64, 0, true);
552 	TEST_ONE_SHIFT(0, 65, u64, 0, true);
553 	TEST_ONE_SHIFT(0, 64, s64, 0, true);
554 	TEST_ONE_SHIFT(0, 65, s64, 0, true);
555 
556 	kunit_info(test, "%d truncate shift tests finished\n", count);
557 }
558 
559 static void shift_nonsense_test(struct kunit *test)
560 {
561 	int count = 0;
562 
563 	/* Nonsense: negative initial value. */
564 	TEST_ONE_SHIFT(-1, 0, s8, 0, true);
565 	TEST_ONE_SHIFT(-1, 0, u8, 0, true);
566 	TEST_ONE_SHIFT(-5, 0, s16, 0, true);
567 	TEST_ONE_SHIFT(-5, 0, u16, 0, true);
568 	TEST_ONE_SHIFT(-10, 0, int, 0, true);
569 	TEST_ONE_SHIFT(-10, 0, unsigned int, 0, true);
570 	TEST_ONE_SHIFT(-100, 0, s32, 0, true);
571 	TEST_ONE_SHIFT(-100, 0, u32, 0, true);
572 	TEST_ONE_SHIFT(-10000, 0, s64, 0, true);
573 	TEST_ONE_SHIFT(-10000, 0, u64, 0, true);
574 
575 	/* Nonsense: negative shift values. */
576 	TEST_ONE_SHIFT(0, -5, s8, 0, true);
577 	TEST_ONE_SHIFT(0, -5, u8, 0, true);
578 	TEST_ONE_SHIFT(0, -10, s16, 0, true);
579 	TEST_ONE_SHIFT(0, -10, u16, 0, true);
580 	TEST_ONE_SHIFT(0, -15, int, 0, true);
581 	TEST_ONE_SHIFT(0, -15, unsigned int, 0, true);
582 	TEST_ONE_SHIFT(0, -20, s32, 0, true);
583 	TEST_ONE_SHIFT(0, -20, u32, 0, true);
584 	TEST_ONE_SHIFT(0, -30, s64, 0, true);
585 	TEST_ONE_SHIFT(0, -30, u64, 0, true);
586 
587 	/*
588 	 * Corner case: for unsigned types, we fail when we've shifted
589 	 * through the entire width of bits. For signed types, we might
590 	 * want to match this behavior, but that would mean noticing if
591 	 * we shift through all but the signed bit, and this is not
592 	 * currently detected (but we'll notice an overflow into the
593 	 * signed bit). So, for now, we will test this condition but
594 	 * mark it as not expected to overflow.
595 	 */
596 	TEST_ONE_SHIFT(0, 7, s8, 0, false);
597 	TEST_ONE_SHIFT(0, 15, s16, 0, false);
598 	TEST_ONE_SHIFT(0, 31, int, 0, false);
599 	TEST_ONE_SHIFT(0, 31, s32, 0, false);
600 	TEST_ONE_SHIFT(0, 63, s64, 0, false);
601 
602 	kunit_info(test, "%d nonsense shift tests finished\n", count);
603 }
604 #undef TEST_ONE_SHIFT
605 
606 /*
607  * Deal with the various forms of allocator arguments. See comments above
608  * the DEFINE_TEST_ALLOC() instances for mapping of the "bits".
609  */
610 #define alloc_GFP		 (GFP_KERNEL | __GFP_NOWARN)
611 #define alloc010(alloc, arg, sz) alloc(sz, alloc_GFP)
612 #define alloc011(alloc, arg, sz) alloc(sz, alloc_GFP, NUMA_NO_NODE)
613 #define alloc000(alloc, arg, sz) alloc(sz)
614 #define alloc001(alloc, arg, sz) alloc(sz, NUMA_NO_NODE)
615 #define alloc110(alloc, arg, sz) alloc(arg, sz, alloc_GFP)
616 #define free0(free, arg, ptr)	 free(ptr)
617 #define free1(free, arg, ptr)	 free(arg, ptr)
618 
619 /* Wrap around to 16K */
620 #define TEST_SIZE		(5 * 4096)
621 
622 #define DEFINE_TEST_ALLOC(func, free_func, want_arg, want_gfp, want_node)\
623 static void test_ ## func (struct kunit *test, void *arg)		\
624 {									\
625 	volatile size_t a = TEST_SIZE;					\
626 	volatile size_t b = (SIZE_MAX / TEST_SIZE) + 1;			\
627 	void *ptr;							\
628 									\
629 	/* Tiny allocation test. */					\
630 	ptr = alloc ## want_arg ## want_gfp ## want_node (func, arg, 1);\
631 	KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr,			\
632 			    #func " failed regular allocation?!\n");	\
633 	free ## want_arg (free_func, arg, ptr);				\
634 									\
635 	/* Wrapped allocation test. */					\
636 	ptr = alloc ## want_arg ## want_gfp ## want_node (func, arg,	\
637 							  a * b);	\
638 	KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr,			\
639 			    #func " unexpectedly failed bad wrapping?!\n"); \
640 	free ## want_arg (free_func, arg, ptr);				\
641 									\
642 	/* Saturated allocation test. */				\
643 	ptr = alloc ## want_arg ## want_gfp ## want_node (func, arg,	\
644 						   array_size(a, b));	\
645 	if (ptr) {							\
646 		KUNIT_FAIL(test, #func " missed saturation!\n");	\
647 		free ## want_arg (free_func, arg, ptr);			\
648 	}								\
649 }
650 
651 /*
652  * Allocator uses a trailing node argument --------+  (e.g. kmalloc_node())
653  * Allocator uses the gfp_t argument -----------+  |  (e.g. kmalloc())
654  * Allocator uses a special leading argument +  |  |  (e.g. devm_kmalloc())
655  *                                           |  |  |
656  */
657 DEFINE_TEST_ALLOC(kmalloc,	 kfree,	     0, 1, 0);
658 DEFINE_TEST_ALLOC(kmalloc_node,	 kfree,	     0, 1, 1);
659 DEFINE_TEST_ALLOC(kzalloc,	 kfree,	     0, 1, 0);
660 DEFINE_TEST_ALLOC(kzalloc_node,  kfree,	     0, 1, 1);
661 DEFINE_TEST_ALLOC(__vmalloc,	 vfree,	     0, 1, 0);
662 DEFINE_TEST_ALLOC(kvmalloc,	 kvfree,     0, 1, 0);
663 DEFINE_TEST_ALLOC(kvmalloc_node, kvfree,     0, 1, 1);
664 DEFINE_TEST_ALLOC(kvzalloc,	 kvfree,     0, 1, 0);
665 DEFINE_TEST_ALLOC(kvzalloc_node, kvfree,     0, 1, 1);
666 DEFINE_TEST_ALLOC(devm_kmalloc,  devm_kfree, 1, 1, 0);
667 DEFINE_TEST_ALLOC(devm_kzalloc,  devm_kfree, 1, 1, 0);
668 
669 static void overflow_allocation_test(struct kunit *test)
670 {
671 	const char device_name[] = "overflow-test";
672 	struct device *dev;
673 	int count = 0;
674 
675 #define check_allocation_overflow(alloc)	do {	\
676 	count++;					\
677 	test_ ## alloc(test, dev);			\
678 } while (0)
679 
680 	/* Create dummy device for devm_kmalloc()-family tests. */
681 	dev = kunit_device_register(test, device_name);
682 	KUNIT_ASSERT_FALSE_MSG(test, IS_ERR(dev),
683 			       "Cannot register test device\n");
684 
685 	check_allocation_overflow(kmalloc);
686 	check_allocation_overflow(kmalloc_node);
687 	check_allocation_overflow(kzalloc);
688 	check_allocation_overflow(kzalloc_node);
689 	check_allocation_overflow(__vmalloc);
690 	check_allocation_overflow(kvmalloc);
691 	check_allocation_overflow(kvmalloc_node);
692 	check_allocation_overflow(kvzalloc);
693 	check_allocation_overflow(kvzalloc_node);
694 	check_allocation_overflow(devm_kmalloc);
695 	check_allocation_overflow(devm_kzalloc);
696 
697 	kunit_info(test, "%d allocation overflow tests finished\n", count);
698 #undef check_allocation_overflow
699 }
700 
701 struct __test_flex_array {
702 	unsigned long flags;
703 	size_t count;
704 	unsigned long data[];
705 };
706 
707 static void overflow_size_helpers_test(struct kunit *test)
708 {
709 	/* Make sure struct_size() can be used in a constant expression. */
710 	u8 ce_array[struct_size_t(struct __test_flex_array, data, 55)];
711 	struct __test_flex_array *obj;
712 	int count = 0;
713 	int var;
714 	volatile int unconst = 0;
715 
716 	/* Verify constant expression against runtime version. */
717 	var = 55;
718 	OPTIMIZER_HIDE_VAR(var);
719 	KUNIT_EXPECT_EQ(test, sizeof(ce_array), struct_size(obj, data, var));
720 
721 #define check_one_size_helper(expected, func, args...)	do {	\
722 	size_t _r = func(args);					\
723 	KUNIT_EXPECT_EQ_MSG(test, _r, expected,			\
724 		"expected " #func "(" #args ") to return %zu but got %zu instead\n", \
725 		(size_t)(expected), _r);			\
726 	count++;						\
727 } while (0)
728 
729 	var = 4;
730 	check_one_size_helper(20,	size_mul, var++, 5);
731 	check_one_size_helper(20,	size_mul, 4, var++);
732 	check_one_size_helper(0,	size_mul, 0, 3);
733 	check_one_size_helper(0,	size_mul, 3, 0);
734 	check_one_size_helper(6,	size_mul, 2, 3);
735 	check_one_size_helper(SIZE_MAX,	size_mul, SIZE_MAX,  1);
736 	check_one_size_helper(SIZE_MAX,	size_mul, SIZE_MAX,  3);
737 	check_one_size_helper(SIZE_MAX,	size_mul, SIZE_MAX, -3);
738 
739 	var = 4;
740 	check_one_size_helper(9,	size_add, var++, 5);
741 	check_one_size_helper(9,	size_add, 4, var++);
742 	check_one_size_helper(9,	size_add, 9, 0);
743 	check_one_size_helper(9,	size_add, 0, 9);
744 	check_one_size_helper(5,	size_add, 2, 3);
745 	check_one_size_helper(SIZE_MAX, size_add, SIZE_MAX,  1);
746 	check_one_size_helper(SIZE_MAX, size_add, SIZE_MAX,  3);
747 	check_one_size_helper(SIZE_MAX, size_add, SIZE_MAX, -3);
748 
749 	var = 4;
750 	check_one_size_helper(1,	size_sub, var--, 3);
751 	check_one_size_helper(1,	size_sub, 4, var--);
752 	check_one_size_helper(1,	size_sub, 3, 2);
753 	check_one_size_helper(9,	size_sub, 9, 0);
754 	check_one_size_helper(SIZE_MAX, size_sub, 9, -3);
755 	check_one_size_helper(SIZE_MAX, size_sub, 0, 9);
756 	check_one_size_helper(SIZE_MAX, size_sub, 2, 3);
757 	check_one_size_helper(SIZE_MAX, size_sub, SIZE_MAX,  0);
758 	check_one_size_helper(SIZE_MAX, size_sub, SIZE_MAX, 10);
759 	check_one_size_helper(SIZE_MAX, size_sub, 0,  SIZE_MAX);
760 	check_one_size_helper(SIZE_MAX, size_sub, 14, SIZE_MAX);
761 	check_one_size_helper(SIZE_MAX - 2, size_sub, SIZE_MAX - 1,  1);
762 	check_one_size_helper(SIZE_MAX - 4, size_sub, SIZE_MAX - 1,  3);
763 	check_one_size_helper(1,		size_sub, SIZE_MAX - 1, -3);
764 
765 	var = 4;
766 	check_one_size_helper(4 * sizeof(*obj->data),
767 			      flex_array_size, obj, data, var++);
768 	check_one_size_helper(5 * sizeof(*obj->data),
769 			      flex_array_size, obj, data, var++);
770 	check_one_size_helper(0, flex_array_size, obj, data, 0 + unconst);
771 	check_one_size_helper(sizeof(*obj->data),
772 			      flex_array_size, obj, data, 1 + unconst);
773 	check_one_size_helper(7 * sizeof(*obj->data),
774 			      flex_array_size, obj, data, 7 + unconst);
775 	check_one_size_helper(SIZE_MAX,
776 			      flex_array_size, obj, data, -1 + unconst);
777 	check_one_size_helper(SIZE_MAX,
778 			      flex_array_size, obj, data, SIZE_MAX - 4 + unconst);
779 
780 	var = 4;
781 	check_one_size_helper(sizeof(*obj) + (4 * sizeof(*obj->data)),
782 			      struct_size, obj, data, var++);
783 	check_one_size_helper(sizeof(*obj) + (5 * sizeof(*obj->data)),
784 			      struct_size, obj, data, var++);
785 	check_one_size_helper(sizeof(*obj), struct_size, obj, data, 0 + unconst);
786 	check_one_size_helper(sizeof(*obj) + sizeof(*obj->data),
787 			      struct_size, obj, data, 1 + unconst);
788 	check_one_size_helper(SIZE_MAX,
789 			      struct_size, obj, data, -3 + unconst);
790 	check_one_size_helper(SIZE_MAX,
791 			      struct_size, obj, data, SIZE_MAX - 3 + unconst);
792 
793 	kunit_info(test, "%d overflow size helper tests finished\n", count);
794 #undef check_one_size_helper
795 }
796 
797 static void overflows_type_test(struct kunit *test)
798 {
799 	int count = 0;
800 	unsigned int var;
801 
802 #define __TEST_OVERFLOWS_TYPE(func, arg1, arg2, of)	do {		\
803 	bool __of = func(arg1, arg2);					\
804 	KUNIT_EXPECT_EQ_MSG(test, __of, of,				\
805 		"expected " #func "(" #arg1 ", " #arg2 " to%s overflow\n",\
806 		of ? "" : " not");					\
807 	count++;							\
808 } while (0)
809 
810 /* Args are: first type, second type, value, overflow expected */
811 #define TEST_OVERFLOWS_TYPE(__t1, __t2, v, of) do {			\
812 	__t1 t1 = (v);							\
813 	__t2 t2;							\
814 	__TEST_OVERFLOWS_TYPE(__overflows_type, t1, t2, of);		\
815 	__TEST_OVERFLOWS_TYPE(__overflows_type, t1, __t2, of);		\
816 	__TEST_OVERFLOWS_TYPE(__overflows_type_constexpr, t1, t2, of);	\
817 	__TEST_OVERFLOWS_TYPE(__overflows_type_constexpr, t1, __t2, of);\
818 } while (0)
819 
820 	TEST_OVERFLOWS_TYPE(u8, u8, U8_MAX, false);
821 	TEST_OVERFLOWS_TYPE(u8, u16, U8_MAX, false);
822 	TEST_OVERFLOWS_TYPE(u8, s8, U8_MAX, true);
823 	TEST_OVERFLOWS_TYPE(u8, s8, S8_MAX, false);
824 	TEST_OVERFLOWS_TYPE(u8, s8, (u8)S8_MAX + 1, true);
825 	TEST_OVERFLOWS_TYPE(u8, s16, U8_MAX, false);
826 	TEST_OVERFLOWS_TYPE(s8, u8, S8_MAX, false);
827 	TEST_OVERFLOWS_TYPE(s8, u8, -1, true);
828 	TEST_OVERFLOWS_TYPE(s8, u8, S8_MIN, true);
829 	TEST_OVERFLOWS_TYPE(s8, u16, S8_MAX, false);
830 	TEST_OVERFLOWS_TYPE(s8, u16, -1, true);
831 	TEST_OVERFLOWS_TYPE(s8, u16, S8_MIN, true);
832 	TEST_OVERFLOWS_TYPE(s8, u32, S8_MAX, false);
833 	TEST_OVERFLOWS_TYPE(s8, u32, -1, true);
834 	TEST_OVERFLOWS_TYPE(s8, u32, S8_MIN, true);
835 #if BITS_PER_LONG == 64
836 	TEST_OVERFLOWS_TYPE(s8, u64, S8_MAX, false);
837 	TEST_OVERFLOWS_TYPE(s8, u64, -1, true);
838 	TEST_OVERFLOWS_TYPE(s8, u64, S8_MIN, true);
839 #endif
840 	TEST_OVERFLOWS_TYPE(s8, s8, S8_MAX, false);
841 	TEST_OVERFLOWS_TYPE(s8, s8, S8_MIN, false);
842 	TEST_OVERFLOWS_TYPE(s8, s16, S8_MAX, false);
843 	TEST_OVERFLOWS_TYPE(s8, s16, S8_MIN, false);
844 	TEST_OVERFLOWS_TYPE(u16, u8, U8_MAX, false);
845 	TEST_OVERFLOWS_TYPE(u16, u8, (u16)U8_MAX + 1, true);
846 	TEST_OVERFLOWS_TYPE(u16, u8, U16_MAX, true);
847 	TEST_OVERFLOWS_TYPE(u16, s8, S8_MAX, false);
848 	TEST_OVERFLOWS_TYPE(u16, s8, (u16)S8_MAX + 1, true);
849 	TEST_OVERFLOWS_TYPE(u16, s8, U16_MAX, true);
850 	TEST_OVERFLOWS_TYPE(u16, s16, S16_MAX, false);
851 	TEST_OVERFLOWS_TYPE(u16, s16, (u16)S16_MAX + 1, true);
852 	TEST_OVERFLOWS_TYPE(u16, s16, U16_MAX, true);
853 	TEST_OVERFLOWS_TYPE(u16, u32, U16_MAX, false);
854 	TEST_OVERFLOWS_TYPE(u16, s32, U16_MAX, false);
855 	TEST_OVERFLOWS_TYPE(s16, u8, U8_MAX, false);
856 	TEST_OVERFLOWS_TYPE(s16, u8, (s16)U8_MAX + 1, true);
857 	TEST_OVERFLOWS_TYPE(s16, u8, -1, true);
858 	TEST_OVERFLOWS_TYPE(s16, u8, S16_MIN, true);
859 	TEST_OVERFLOWS_TYPE(s16, u16, S16_MAX, false);
860 	TEST_OVERFLOWS_TYPE(s16, u16, -1, true);
861 	TEST_OVERFLOWS_TYPE(s16, u16, S16_MIN, true);
862 	TEST_OVERFLOWS_TYPE(s16, u32, S16_MAX, false);
863 	TEST_OVERFLOWS_TYPE(s16, u32, -1, true);
864 	TEST_OVERFLOWS_TYPE(s16, u32, S16_MIN, true);
865 #if BITS_PER_LONG == 64
866 	TEST_OVERFLOWS_TYPE(s16, u64, S16_MAX, false);
867 	TEST_OVERFLOWS_TYPE(s16, u64, -1, true);
868 	TEST_OVERFLOWS_TYPE(s16, u64, S16_MIN, true);
869 #endif
870 	TEST_OVERFLOWS_TYPE(s16, s8, S8_MAX, false);
871 	TEST_OVERFLOWS_TYPE(s16, s8, S8_MIN, false);
872 	TEST_OVERFLOWS_TYPE(s16, s8, (s16)S8_MAX + 1, true);
873 	TEST_OVERFLOWS_TYPE(s16, s8, (s16)S8_MIN - 1, true);
874 	TEST_OVERFLOWS_TYPE(s16, s8, S16_MAX, true);
875 	TEST_OVERFLOWS_TYPE(s16, s8, S16_MIN, true);
876 	TEST_OVERFLOWS_TYPE(s16, s16, S16_MAX, false);
877 	TEST_OVERFLOWS_TYPE(s16, s16, S16_MIN, false);
878 	TEST_OVERFLOWS_TYPE(s16, s32, S16_MAX, false);
879 	TEST_OVERFLOWS_TYPE(s16, s32, S16_MIN, false);
880 	TEST_OVERFLOWS_TYPE(u32, u8, U8_MAX, false);
881 	TEST_OVERFLOWS_TYPE(u32, u8, (u32)U8_MAX + 1, true);
882 	TEST_OVERFLOWS_TYPE(u32, u8, U32_MAX, true);
883 	TEST_OVERFLOWS_TYPE(u32, s8, S8_MAX, false);
884 	TEST_OVERFLOWS_TYPE(u32, s8, (u32)S8_MAX + 1, true);
885 	TEST_OVERFLOWS_TYPE(u32, s8, U32_MAX, true);
886 	TEST_OVERFLOWS_TYPE(u32, u16, U16_MAX, false);
887 	TEST_OVERFLOWS_TYPE(u32, u16, U16_MAX + 1, true);
888 	TEST_OVERFLOWS_TYPE(u32, u16, U32_MAX, true);
889 	TEST_OVERFLOWS_TYPE(u32, s16, S16_MAX, false);
890 	TEST_OVERFLOWS_TYPE(u32, s16, (u32)S16_MAX + 1, true);
891 	TEST_OVERFLOWS_TYPE(u32, s16, U32_MAX, true);
892 	TEST_OVERFLOWS_TYPE(u32, u32, U32_MAX, false);
893 	TEST_OVERFLOWS_TYPE(u32, s32, S32_MAX, false);
894 	TEST_OVERFLOWS_TYPE(u32, s32, U32_MAX, true);
895 	TEST_OVERFLOWS_TYPE(u32, s32, (u32)S32_MAX + 1, true);
896 #if BITS_PER_LONG == 64
897 	TEST_OVERFLOWS_TYPE(u32, u64, U32_MAX, false);
898 	TEST_OVERFLOWS_TYPE(u32, s64, U32_MAX, false);
899 #endif
900 	TEST_OVERFLOWS_TYPE(s32, u8, U8_MAX, false);
901 	TEST_OVERFLOWS_TYPE(s32, u8, (s32)U8_MAX + 1, true);
902 	TEST_OVERFLOWS_TYPE(s32, u16, S32_MAX, true);
903 	TEST_OVERFLOWS_TYPE(s32, u8, -1, true);
904 	TEST_OVERFLOWS_TYPE(s32, u8, S32_MIN, true);
905 	TEST_OVERFLOWS_TYPE(s32, u16, U16_MAX, false);
906 	TEST_OVERFLOWS_TYPE(s32, u16, (s32)U16_MAX + 1, true);
907 	TEST_OVERFLOWS_TYPE(s32, u16, S32_MAX, true);
908 	TEST_OVERFLOWS_TYPE(s32, u16, -1, true);
909 	TEST_OVERFLOWS_TYPE(s32, u16, S32_MIN, true);
910 	TEST_OVERFLOWS_TYPE(s32, u32, S32_MAX, false);
911 	TEST_OVERFLOWS_TYPE(s32, u32, -1, true);
912 	TEST_OVERFLOWS_TYPE(s32, u32, S32_MIN, true);
913 #if BITS_PER_LONG == 64
914 	TEST_OVERFLOWS_TYPE(s32, u64, S32_MAX, false);
915 	TEST_OVERFLOWS_TYPE(s32, u64, -1, true);
916 	TEST_OVERFLOWS_TYPE(s32, u64, S32_MIN, true);
917 #endif
918 	TEST_OVERFLOWS_TYPE(s32, s8, S8_MAX, false);
919 	TEST_OVERFLOWS_TYPE(s32, s8, S8_MIN, false);
920 	TEST_OVERFLOWS_TYPE(s32, s8, (s32)S8_MAX + 1, true);
921 	TEST_OVERFLOWS_TYPE(s32, s8, (s32)S8_MIN - 1, true);
922 	TEST_OVERFLOWS_TYPE(s32, s8, S32_MAX, true);
923 	TEST_OVERFLOWS_TYPE(s32, s8, S32_MIN, true);
924 	TEST_OVERFLOWS_TYPE(s32, s16, S16_MAX, false);
925 	TEST_OVERFLOWS_TYPE(s32, s16, S16_MIN, false);
926 	TEST_OVERFLOWS_TYPE(s32, s16, (s32)S16_MAX + 1, true);
927 	TEST_OVERFLOWS_TYPE(s32, s16, (s32)S16_MIN - 1, true);
928 	TEST_OVERFLOWS_TYPE(s32, s16, S32_MAX, true);
929 	TEST_OVERFLOWS_TYPE(s32, s16, S32_MIN, true);
930 	TEST_OVERFLOWS_TYPE(s32, s32, S32_MAX, false);
931 	TEST_OVERFLOWS_TYPE(s32, s32, S32_MIN, false);
932 #if BITS_PER_LONG == 64
933 	TEST_OVERFLOWS_TYPE(s32, s64, S32_MAX, false);
934 	TEST_OVERFLOWS_TYPE(s32, s64, S32_MIN, false);
935 	TEST_OVERFLOWS_TYPE(u64, u8, U64_MAX, true);
936 	TEST_OVERFLOWS_TYPE(u64, u8, U8_MAX, false);
937 	TEST_OVERFLOWS_TYPE(u64, u8, (u64)U8_MAX + 1, true);
938 	TEST_OVERFLOWS_TYPE(u64, u16, U64_MAX, true);
939 	TEST_OVERFLOWS_TYPE(u64, u16, U16_MAX, false);
940 	TEST_OVERFLOWS_TYPE(u64, u16, (u64)U16_MAX + 1, true);
941 	TEST_OVERFLOWS_TYPE(u64, u32, U64_MAX, true);
942 	TEST_OVERFLOWS_TYPE(u64, u32, U32_MAX, false);
943 	TEST_OVERFLOWS_TYPE(u64, u32, (u64)U32_MAX + 1, true);
944 	TEST_OVERFLOWS_TYPE(u64, u64, U64_MAX, false);
945 	TEST_OVERFLOWS_TYPE(u64, s8, S8_MAX, false);
946 	TEST_OVERFLOWS_TYPE(u64, s8, (u64)S8_MAX + 1, true);
947 	TEST_OVERFLOWS_TYPE(u64, s8, U64_MAX, true);
948 	TEST_OVERFLOWS_TYPE(u64, s16, S16_MAX, false);
949 	TEST_OVERFLOWS_TYPE(u64, s16, (u64)S16_MAX + 1, true);
950 	TEST_OVERFLOWS_TYPE(u64, s16, U64_MAX, true);
951 	TEST_OVERFLOWS_TYPE(u64, s32, S32_MAX, false);
952 	TEST_OVERFLOWS_TYPE(u64, s32, (u64)S32_MAX + 1, true);
953 	TEST_OVERFLOWS_TYPE(u64, s32, U64_MAX, true);
954 	TEST_OVERFLOWS_TYPE(u64, s64, S64_MAX, false);
955 	TEST_OVERFLOWS_TYPE(u64, s64, U64_MAX, true);
956 	TEST_OVERFLOWS_TYPE(u64, s64, (u64)S64_MAX + 1, true);
957 	TEST_OVERFLOWS_TYPE(s64, u8, S64_MAX, true);
958 	TEST_OVERFLOWS_TYPE(s64, u8, S64_MIN, true);
959 	TEST_OVERFLOWS_TYPE(s64, u8, -1, true);
960 	TEST_OVERFLOWS_TYPE(s64, u8, U8_MAX, false);
961 	TEST_OVERFLOWS_TYPE(s64, u8, (s64)U8_MAX + 1, true);
962 	TEST_OVERFLOWS_TYPE(s64, u16, S64_MAX, true);
963 	TEST_OVERFLOWS_TYPE(s64, u16, S64_MIN, true);
964 	TEST_OVERFLOWS_TYPE(s64, u16, -1, true);
965 	TEST_OVERFLOWS_TYPE(s64, u16, U16_MAX, false);
966 	TEST_OVERFLOWS_TYPE(s64, u16, (s64)U16_MAX + 1, true);
967 	TEST_OVERFLOWS_TYPE(s64, u32, S64_MAX, true);
968 	TEST_OVERFLOWS_TYPE(s64, u32, S64_MIN, true);
969 	TEST_OVERFLOWS_TYPE(s64, u32, -1, true);
970 	TEST_OVERFLOWS_TYPE(s64, u32, U32_MAX, false);
971 	TEST_OVERFLOWS_TYPE(s64, u32, (s64)U32_MAX + 1, true);
972 	TEST_OVERFLOWS_TYPE(s64, u64, S64_MAX, false);
973 	TEST_OVERFLOWS_TYPE(s64, u64, S64_MIN, true);
974 	TEST_OVERFLOWS_TYPE(s64, u64, -1, true);
975 	TEST_OVERFLOWS_TYPE(s64, s8, S8_MAX, false);
976 	TEST_OVERFLOWS_TYPE(s64, s8, S8_MIN, false);
977 	TEST_OVERFLOWS_TYPE(s64, s8, (s64)S8_MAX + 1, true);
978 	TEST_OVERFLOWS_TYPE(s64, s8, (s64)S8_MIN - 1, true);
979 	TEST_OVERFLOWS_TYPE(s64, s8, S64_MAX, true);
980 	TEST_OVERFLOWS_TYPE(s64, s16, S16_MAX, false);
981 	TEST_OVERFLOWS_TYPE(s64, s16, S16_MIN, false);
982 	TEST_OVERFLOWS_TYPE(s64, s16, (s64)S16_MAX + 1, true);
983 	TEST_OVERFLOWS_TYPE(s64, s16, (s64)S16_MIN - 1, true);
984 	TEST_OVERFLOWS_TYPE(s64, s16, S64_MAX, true);
985 	TEST_OVERFLOWS_TYPE(s64, s32, S32_MAX, false);
986 	TEST_OVERFLOWS_TYPE(s64, s32, S32_MIN, false);
987 	TEST_OVERFLOWS_TYPE(s64, s32, (s64)S32_MAX + 1, true);
988 	TEST_OVERFLOWS_TYPE(s64, s32, (s64)S32_MIN - 1, true);
989 	TEST_OVERFLOWS_TYPE(s64, s32, S64_MAX, true);
990 	TEST_OVERFLOWS_TYPE(s64, s64, S64_MAX, false);
991 	TEST_OVERFLOWS_TYPE(s64, s64, S64_MIN, false);
992 #endif
993 
994 	/* Check for macro side-effects. */
995 	var = INT_MAX - 1;
996 	__TEST_OVERFLOWS_TYPE(__overflows_type, var++, int, false);
997 	__TEST_OVERFLOWS_TYPE(__overflows_type, var++, int, false);
998 	__TEST_OVERFLOWS_TYPE(__overflows_type, var++, int, true);
999 	var = INT_MAX - 1;
1000 	__TEST_OVERFLOWS_TYPE(overflows_type, var++, int, false);
1001 	__TEST_OVERFLOWS_TYPE(overflows_type, var++, int, false);
1002 	__TEST_OVERFLOWS_TYPE(overflows_type, var++, int, true);
1003 
1004 	kunit_info(test, "%d overflows_type() tests finished\n", count);
1005 #undef TEST_OVERFLOWS_TYPE
1006 #undef __TEST_OVERFLOWS_TYPE
1007 }
1008 
1009 static void same_type_test(struct kunit *test)
1010 {
1011 	int count = 0;
1012 	int var;
1013 
1014 #define TEST_SAME_TYPE(t1, t2, same)			do {	\
1015 	typeof(t1) __t1h = type_max(t1);			\
1016 	typeof(t1) __t1l = type_min(t1);			\
1017 	typeof(t2) __t2h = type_max(t2);			\
1018 	typeof(t2) __t2l = type_min(t2);			\
1019 	KUNIT_EXPECT_EQ(test, true, __same_type(t1, __t1h));	\
1020 	KUNIT_EXPECT_EQ(test, true, __same_type(t1, __t1l));	\
1021 	KUNIT_EXPECT_EQ(test, true, __same_type(__t1h, t1));	\
1022 	KUNIT_EXPECT_EQ(test, true, __same_type(__t1l, t1));	\
1023 	KUNIT_EXPECT_EQ(test, true, __same_type(t2, __t2h));	\
1024 	KUNIT_EXPECT_EQ(test, true, __same_type(t2, __t2l));	\
1025 	KUNIT_EXPECT_EQ(test, true, __same_type(__t2h, t2));	\
1026 	KUNIT_EXPECT_EQ(test, true, __same_type(__t2l, t2));	\
1027 	KUNIT_EXPECT_EQ(test, same, __same_type(t1, t2));	\
1028 	KUNIT_EXPECT_EQ(test, same, __same_type(t2, __t1h));	\
1029 	KUNIT_EXPECT_EQ(test, same, __same_type(t2, __t1l));	\
1030 	KUNIT_EXPECT_EQ(test, same, __same_type(__t1h, t2));	\
1031 	KUNIT_EXPECT_EQ(test, same, __same_type(__t1l, t2));	\
1032 	KUNIT_EXPECT_EQ(test, same, __same_type(t1, __t2h));	\
1033 	KUNIT_EXPECT_EQ(test, same, __same_type(t1, __t2l));	\
1034 	KUNIT_EXPECT_EQ(test, same, __same_type(__t2h, t1));	\
1035 	KUNIT_EXPECT_EQ(test, same, __same_type(__t2l, t1));	\
1036 } while (0)
1037 
1038 #if BITS_PER_LONG == 64
1039 # define TEST_SAME_TYPE64(base, t, m)	TEST_SAME_TYPE(base, t, m)
1040 #else
1041 # define TEST_SAME_TYPE64(base, t, m)	do { } while (0)
1042 #endif
1043 
1044 #define TEST_TYPE_SETS(base, mu8, mu16, mu32, ms8, ms16, ms32, mu64, ms64) \
1045 do {									\
1046 	TEST_SAME_TYPE(base,  u8,  mu8);				\
1047 	TEST_SAME_TYPE(base, u16, mu16);				\
1048 	TEST_SAME_TYPE(base, u32, mu32);				\
1049 	TEST_SAME_TYPE(base,  s8,  ms8);				\
1050 	TEST_SAME_TYPE(base, s16, ms16);				\
1051 	TEST_SAME_TYPE(base, s32, ms32);				\
1052 	TEST_SAME_TYPE64(base, u64, mu64);				\
1053 	TEST_SAME_TYPE64(base, s64, ms64);				\
1054 } while (0)
1055 
1056 	TEST_TYPE_SETS(u8,   true, false, false, false, false, false, false, false);
1057 	TEST_TYPE_SETS(u16, false,  true, false, false, false, false, false, false);
1058 	TEST_TYPE_SETS(u32, false, false,  true, false, false, false, false, false);
1059 	TEST_TYPE_SETS(s8,  false, false, false,  true, false, false, false, false);
1060 	TEST_TYPE_SETS(s16, false, false, false, false,  true, false, false, false);
1061 	TEST_TYPE_SETS(s32, false, false, false, false, false,  true, false, false);
1062 #if BITS_PER_LONG == 64
1063 	TEST_TYPE_SETS(u64, false, false, false, false, false, false,  true, false);
1064 	TEST_TYPE_SETS(s64, false, false, false, false, false, false, false,  true);
1065 #endif
1066 
1067 	/* Check for macro side-effects. */
1068 	var = 4;
1069 	KUNIT_EXPECT_EQ(test, var, 4);
1070 	KUNIT_EXPECT_TRUE(test, __same_type(var++, int));
1071 	KUNIT_EXPECT_EQ(test, var, 4);
1072 	KUNIT_EXPECT_TRUE(test, __same_type(int, var++));
1073 	KUNIT_EXPECT_EQ(test, var, 4);
1074 	KUNIT_EXPECT_TRUE(test, __same_type(var++, var++));
1075 	KUNIT_EXPECT_EQ(test, var, 4);
1076 
1077 	kunit_info(test, "%d __same_type() tests finished\n", count);
1078 
1079 #undef TEST_TYPE_SETS
1080 #undef TEST_SAME_TYPE64
1081 #undef TEST_SAME_TYPE
1082 }
1083 
1084 static void castable_to_type_test(struct kunit *test)
1085 {
1086 	int count = 0;
1087 
1088 #define TEST_CASTABLE_TO_TYPE(arg1, arg2, pass)	do {	\
1089 	bool __pass = castable_to_type(arg1, arg2);		\
1090 	KUNIT_EXPECT_EQ_MSG(test, __pass, pass,			\
1091 		"expected castable_to_type(" #arg1 ", " #arg2 ") to%s pass\n",\
1092 		pass ? "" : " not");				\
1093 	count++;						\
1094 } while (0)
1095 
1096 	TEST_CASTABLE_TO_TYPE(16, u8, true);
1097 	TEST_CASTABLE_TO_TYPE(16, u16, true);
1098 	TEST_CASTABLE_TO_TYPE(16, u32, true);
1099 	TEST_CASTABLE_TO_TYPE(16, s8, true);
1100 	TEST_CASTABLE_TO_TYPE(16, s16, true);
1101 	TEST_CASTABLE_TO_TYPE(16, s32, true);
1102 	TEST_CASTABLE_TO_TYPE(-16, s8, true);
1103 	TEST_CASTABLE_TO_TYPE(-16, s16, true);
1104 	TEST_CASTABLE_TO_TYPE(-16, s32, true);
1105 #if BITS_PER_LONG == 64
1106 	TEST_CASTABLE_TO_TYPE(16, u64, true);
1107 	TEST_CASTABLE_TO_TYPE(-16, s64, true);
1108 #endif
1109 
1110 #define TEST_CASTABLE_TO_TYPE_VAR(width)	do {				\
1111 	u ## width u ## width ## var = 0;					\
1112 	s ## width s ## width ## var = 0;					\
1113 										\
1114 	/* Constant expressions that fit types. */				\
1115 	TEST_CASTABLE_TO_TYPE(type_max(u ## width), u ## width, true);		\
1116 	TEST_CASTABLE_TO_TYPE(type_min(u ## width), u ## width, true);		\
1117 	TEST_CASTABLE_TO_TYPE(type_max(u ## width), u ## width ## var, true);	\
1118 	TEST_CASTABLE_TO_TYPE(type_min(u ## width), u ## width ## var, true);	\
1119 	TEST_CASTABLE_TO_TYPE(type_max(s ## width), s ## width, true);		\
1120 	TEST_CASTABLE_TO_TYPE(type_min(s ## width), s ## width, true);		\
1121 	TEST_CASTABLE_TO_TYPE(type_max(s ## width), s ## width ## var, true);	\
1122 	TEST_CASTABLE_TO_TYPE(type_min(u ## width), s ## width ## var, true);	\
1123 	/* Constant expressions that do not fit types. */			\
1124 	TEST_CASTABLE_TO_TYPE(type_max(u ## width), s ## width, false);		\
1125 	TEST_CASTABLE_TO_TYPE(type_max(u ## width), s ## width ## var, false);	\
1126 	TEST_CASTABLE_TO_TYPE(type_min(s ## width), u ## width, false);		\
1127 	TEST_CASTABLE_TO_TYPE(type_min(s ## width), u ## width ## var, false);	\
1128 	/* Non-constant expression with mismatched type. */			\
1129 	TEST_CASTABLE_TO_TYPE(s ## width ## var, u ## width, false);		\
1130 	TEST_CASTABLE_TO_TYPE(u ## width ## var, s ## width, false);		\
1131 } while (0)
1132 
1133 #define TEST_CASTABLE_TO_TYPE_RANGE(width)	do {				\
1134 	unsigned long big = U ## width ## _MAX;					\
1135 	signed long small = S ## width ## _MIN;					\
1136 	u ## width u ## width ## var = 0;					\
1137 	s ## width s ## width ## var = 0;					\
1138 										\
1139 	/* Constant expression in range. */					\
1140 	TEST_CASTABLE_TO_TYPE(U ## width ## _MAX, u ## width, true);		\
1141 	TEST_CASTABLE_TO_TYPE(U ## width ## _MAX, u ## width ## var, true);	\
1142 	TEST_CASTABLE_TO_TYPE(S ## width ## _MIN, s ## width, true);		\
1143 	TEST_CASTABLE_TO_TYPE(S ## width ## _MIN, s ## width ## var, true);	\
1144 	/* Constant expression out of range. */					\
1145 	TEST_CASTABLE_TO_TYPE((unsigned long)U ## width ## _MAX + 1, u ## width, false); \
1146 	TEST_CASTABLE_TO_TYPE((unsigned long)U ## width ## _MAX + 1, u ## width ## var, false); \
1147 	TEST_CASTABLE_TO_TYPE((signed long)S ## width ## _MIN - 1, s ## width, false); \
1148 	TEST_CASTABLE_TO_TYPE((signed long)S ## width ## _MIN - 1, s ## width ## var, false); \
1149 	/* Non-constant expression with mismatched type. */			\
1150 	TEST_CASTABLE_TO_TYPE(big, u ## width, false);				\
1151 	TEST_CASTABLE_TO_TYPE(big, u ## width ## var, false);			\
1152 	TEST_CASTABLE_TO_TYPE(small, s ## width, false);			\
1153 	TEST_CASTABLE_TO_TYPE(small, s ## width ## var, false);			\
1154 } while (0)
1155 
1156 	TEST_CASTABLE_TO_TYPE_VAR(8);
1157 	TEST_CASTABLE_TO_TYPE_VAR(16);
1158 	TEST_CASTABLE_TO_TYPE_VAR(32);
1159 #if BITS_PER_LONG == 64
1160 	TEST_CASTABLE_TO_TYPE_VAR(64);
1161 #endif
1162 
1163 	TEST_CASTABLE_TO_TYPE_RANGE(8);
1164 	TEST_CASTABLE_TO_TYPE_RANGE(16);
1165 #if BITS_PER_LONG == 64
1166 	TEST_CASTABLE_TO_TYPE_RANGE(32);
1167 #endif
1168 	kunit_info(test, "%d castable_to_type() tests finished\n", count);
1169 
1170 #undef TEST_CASTABLE_TO_TYPE_RANGE
1171 #undef TEST_CASTABLE_TO_TYPE_VAR
1172 #undef TEST_CASTABLE_TO_TYPE
1173 }
1174 
1175 static struct kunit_case overflow_test_cases[] = {
1176 	KUNIT_CASE(u8_u8__u8_overflow_test),
1177 	KUNIT_CASE(s8_s8__s8_overflow_test),
1178 	KUNIT_CASE(u16_u16__u16_overflow_test),
1179 	KUNIT_CASE(s16_s16__s16_overflow_test),
1180 	KUNIT_CASE(u32_u32__u32_overflow_test),
1181 	KUNIT_CASE(s32_s32__s32_overflow_test),
1182 	KUNIT_CASE(u64_u64__u64_overflow_test),
1183 	KUNIT_CASE(s64_s64__s64_overflow_test),
1184 	KUNIT_CASE(u32_u32__int_overflow_test),
1185 	KUNIT_CASE(u32_u32__u8_overflow_test),
1186 	KUNIT_CASE(u8_u8__int_overflow_test),
1187 	KUNIT_CASE(int_int__u8_overflow_test),
1188 	KUNIT_CASE(shift_sane_test),
1189 	KUNIT_CASE(shift_overflow_test),
1190 	KUNIT_CASE(shift_truncate_test),
1191 	KUNIT_CASE(shift_nonsense_test),
1192 	KUNIT_CASE(overflow_allocation_test),
1193 	KUNIT_CASE(overflow_size_helpers_test),
1194 	KUNIT_CASE(overflows_type_test),
1195 	KUNIT_CASE(same_type_test),
1196 	KUNIT_CASE(castable_to_type_test),
1197 	{}
1198 };
1199 
1200 static struct kunit_suite overflow_test_suite = {
1201 	.name = "overflow",
1202 	.test_cases = overflow_test_cases,
1203 };
1204 
1205 kunit_test_suite(overflow_test_suite);
1206 
1207 MODULE_LICENSE("Dual MIT/GPL");
1208