xref: /openbmc/linux/lib/overflow_kunit.c (revision c059ee9d)
1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /*
3  * Test cases for arithmetic overflow checks. See:
4  * https://www.kernel.org/doc/html/latest/dev-tools/kunit/kunit-tool.html#configuring-building-and-running-tests
5  *	./tools/testing/kunit/kunit.py run overflow [--raw_output]
6  */
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 
9 #include <kunit/test.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/mm.h>
13 #include <linux/module.h>
14 #include <linux/overflow.h>
15 #include <linux/slab.h>
16 #include <linux/types.h>
17 #include <linux/vmalloc.h>
18 
19 #define DEFINE_TEST_ARRAY(t)			\
20 	static const struct test_ ## t {	\
21 		t a, b;				\
22 		t sum, diff, prod;		\
23 		bool s_of, d_of, p_of;		\
24 	} t ## _tests[]
25 
26 DEFINE_TEST_ARRAY(u8) = {
27 	{0, 0, 0, 0, 0, false, false, false},
28 	{1, 1, 2, 0, 1, false, false, false},
29 	{0, 1, 1, U8_MAX, 0, false, true, false},
30 	{1, 0, 1, 1, 0, false, false, false},
31 	{0, U8_MAX, U8_MAX, 1, 0, false, true, false},
32 	{U8_MAX, 0, U8_MAX, U8_MAX, 0, false, false, false},
33 	{1, U8_MAX, 0, 2, U8_MAX, true, true, false},
34 	{U8_MAX, 1, 0, U8_MAX-1, U8_MAX, true, false, false},
35 	{U8_MAX, U8_MAX, U8_MAX-1, 0, 1, true, false, true},
36 
37 	{U8_MAX, U8_MAX-1, U8_MAX-2, 1, 2, true, false, true},
38 	{U8_MAX-1, U8_MAX, U8_MAX-2, U8_MAX, 2, true, true, true},
39 
40 	{1U << 3, 1U << 3, 1U << 4, 0, 1U << 6, false, false, false},
41 	{1U << 4, 1U << 4, 1U << 5, 0, 0, false, false, true},
42 	{1U << 4, 1U << 3, 3*(1U << 3), 1U << 3, 1U << 7, false, false, false},
43 	{1U << 7, 1U << 7, 0, 0, 0, true, false, true},
44 
45 	{48, 32, 80, 16, 0, false, false, true},
46 	{128, 128, 0, 0, 0, true, false, true},
47 	{123, 234, 101, 145, 110, true, true, true},
48 };
49 DEFINE_TEST_ARRAY(u16) = {
50 	{0, 0, 0, 0, 0, false, false, false},
51 	{1, 1, 2, 0, 1, false, false, false},
52 	{0, 1, 1, U16_MAX, 0, false, true, false},
53 	{1, 0, 1, 1, 0, false, false, false},
54 	{0, U16_MAX, U16_MAX, 1, 0, false, true, false},
55 	{U16_MAX, 0, U16_MAX, U16_MAX, 0, false, false, false},
56 	{1, U16_MAX, 0, 2, U16_MAX, true, true, false},
57 	{U16_MAX, 1, 0, U16_MAX-1, U16_MAX, true, false, false},
58 	{U16_MAX, U16_MAX, U16_MAX-1, 0, 1, true, false, true},
59 
60 	{U16_MAX, U16_MAX-1, U16_MAX-2, 1, 2, true, false, true},
61 	{U16_MAX-1, U16_MAX, U16_MAX-2, U16_MAX, 2, true, true, true},
62 
63 	{1U << 7, 1U << 7, 1U << 8, 0, 1U << 14, false, false, false},
64 	{1U << 8, 1U << 8, 1U << 9, 0, 0, false, false, true},
65 	{1U << 8, 1U << 7, 3*(1U << 7), 1U << 7, 1U << 15, false, false, false},
66 	{1U << 15, 1U << 15, 0, 0, 0, true, false, true},
67 
68 	{123, 234, 357, 65425, 28782, false, true, false},
69 	{1234, 2345, 3579, 64425, 10146, false, true, true},
70 };
71 DEFINE_TEST_ARRAY(u32) = {
72 	{0, 0, 0, 0, 0, false, false, false},
73 	{1, 1, 2, 0, 1, false, false, false},
74 	{0, 1, 1, U32_MAX, 0, false, true, false},
75 	{1, 0, 1, 1, 0, false, false, false},
76 	{0, U32_MAX, U32_MAX, 1, 0, false, true, false},
77 	{U32_MAX, 0, U32_MAX, U32_MAX, 0, false, false, false},
78 	{1, U32_MAX, 0, 2, U32_MAX, true, true, false},
79 	{U32_MAX, 1, 0, U32_MAX-1, U32_MAX, true, false, false},
80 	{U32_MAX, U32_MAX, U32_MAX-1, 0, 1, true, false, true},
81 
82 	{U32_MAX, U32_MAX-1, U32_MAX-2, 1, 2, true, false, true},
83 	{U32_MAX-1, U32_MAX, U32_MAX-2, U32_MAX, 2, true, true, true},
84 
85 	{1U << 15, 1U << 15, 1U << 16, 0, 1U << 30, false, false, false},
86 	{1U << 16, 1U << 16, 1U << 17, 0, 0, false, false, true},
87 	{1U << 16, 1U << 15, 3*(1U << 15), 1U << 15, 1U << 31, false, false, false},
88 	{1U << 31, 1U << 31, 0, 0, 0, true, false, true},
89 
90 	{-2U, 1U, -1U, -3U, -2U, false, false, false},
91 	{-4U, 5U, 1U, -9U, -20U, true, false, true},
92 };
93 
94 DEFINE_TEST_ARRAY(u64) = {
95 	{0, 0, 0, 0, 0, false, false, false},
96 	{1, 1, 2, 0, 1, false, false, false},
97 	{0, 1, 1, U64_MAX, 0, false, true, false},
98 	{1, 0, 1, 1, 0, false, false, false},
99 	{0, U64_MAX, U64_MAX, 1, 0, false, true, false},
100 	{U64_MAX, 0, U64_MAX, U64_MAX, 0, false, false, false},
101 	{1, U64_MAX, 0, 2, U64_MAX, true, true, false},
102 	{U64_MAX, 1, 0, U64_MAX-1, U64_MAX, true, false, false},
103 	{U64_MAX, U64_MAX, U64_MAX-1, 0, 1, true, false, true},
104 
105 	{U64_MAX, U64_MAX-1, U64_MAX-2, 1, 2, true, false, true},
106 	{U64_MAX-1, U64_MAX, U64_MAX-2, U64_MAX, 2, true, true, true},
107 
108 	{1ULL << 31, 1ULL << 31, 1ULL << 32, 0, 1ULL << 62, false, false, false},
109 	{1ULL << 32, 1ULL << 32, 1ULL << 33, 0, 0, false, false, true},
110 	{1ULL << 32, 1ULL << 31, 3*(1ULL << 31), 1ULL << 31, 1ULL << 63, false, false, false},
111 	{1ULL << 63, 1ULL << 63, 0, 0, 0, true, false, true},
112 	{1000000000ULL /* 10^9 */, 10000000000ULL /* 10^10 */,
113 	 11000000000ULL, 18446744064709551616ULL, 10000000000000000000ULL,
114 	 false, true, false},
115 	{-15ULL, 10ULL, -5ULL, -25ULL, -150ULL, false, false, true},
116 };
117 
118 DEFINE_TEST_ARRAY(s8) = {
119 	{0, 0, 0, 0, 0, false, false, false},
120 
121 	{0, S8_MAX, S8_MAX, -S8_MAX, 0, false, false, false},
122 	{S8_MAX, 0, S8_MAX, S8_MAX, 0, false, false, false},
123 	{0, S8_MIN, S8_MIN, S8_MIN, 0, false, true, false},
124 	{S8_MIN, 0, S8_MIN, S8_MIN, 0, false, false, false},
125 
126 	{-1, S8_MIN, S8_MAX, S8_MAX, S8_MIN, true, false, true},
127 	{S8_MIN, -1, S8_MAX, -S8_MAX, S8_MIN, true, false, true},
128 	{-1, S8_MAX, S8_MAX-1, S8_MIN, -S8_MAX, false, false, false},
129 	{S8_MAX, -1, S8_MAX-1, S8_MIN, -S8_MAX, false, true, false},
130 	{-1, -S8_MAX, S8_MIN, S8_MAX-1, S8_MAX, false, false, false},
131 	{-S8_MAX, -1, S8_MIN, S8_MIN+2, S8_MAX, false, false, false},
132 
133 	{1, S8_MIN, -S8_MAX, -S8_MAX, S8_MIN, false, true, false},
134 	{S8_MIN, 1, -S8_MAX, S8_MAX, S8_MIN, false, true, false},
135 	{1, S8_MAX, S8_MIN, S8_MIN+2, S8_MAX, true, false, false},
136 	{S8_MAX, 1, S8_MIN, S8_MAX-1, S8_MAX, true, false, false},
137 
138 	{S8_MIN, S8_MIN, 0, 0, 0, true, false, true},
139 	{S8_MAX, S8_MAX, -2, 0, 1, true, false, true},
140 
141 	{-4, -32, -36, 28, -128, false, false, true},
142 	{-4, 32, 28, -36, -128, false, false, false},
143 };
144 
145 DEFINE_TEST_ARRAY(s16) = {
146 	{0, 0, 0, 0, 0, false, false, false},
147 
148 	{0, S16_MAX, S16_MAX, -S16_MAX, 0, false, false, false},
149 	{S16_MAX, 0, S16_MAX, S16_MAX, 0, false, false, false},
150 	{0, S16_MIN, S16_MIN, S16_MIN, 0, false, true, false},
151 	{S16_MIN, 0, S16_MIN, S16_MIN, 0, false, false, false},
152 
153 	{-1, S16_MIN, S16_MAX, S16_MAX, S16_MIN, true, false, true},
154 	{S16_MIN, -1, S16_MAX, -S16_MAX, S16_MIN, true, false, true},
155 	{-1, S16_MAX, S16_MAX-1, S16_MIN, -S16_MAX, false, false, false},
156 	{S16_MAX, -1, S16_MAX-1, S16_MIN, -S16_MAX, false, true, false},
157 	{-1, -S16_MAX, S16_MIN, S16_MAX-1, S16_MAX, false, false, false},
158 	{-S16_MAX, -1, S16_MIN, S16_MIN+2, S16_MAX, false, false, false},
159 
160 	{1, S16_MIN, -S16_MAX, -S16_MAX, S16_MIN, false, true, false},
161 	{S16_MIN, 1, -S16_MAX, S16_MAX, S16_MIN, false, true, false},
162 	{1, S16_MAX, S16_MIN, S16_MIN+2, S16_MAX, true, false, false},
163 	{S16_MAX, 1, S16_MIN, S16_MAX-1, S16_MAX, true, false, false},
164 
165 	{S16_MIN, S16_MIN, 0, 0, 0, true, false, true},
166 	{S16_MAX, S16_MAX, -2, 0, 1, true, false, true},
167 };
168 DEFINE_TEST_ARRAY(s32) = {
169 	{0, 0, 0, 0, 0, false, false, false},
170 
171 	{0, S32_MAX, S32_MAX, -S32_MAX, 0, false, false, false},
172 	{S32_MAX, 0, S32_MAX, S32_MAX, 0, false, false, false},
173 	{0, S32_MIN, S32_MIN, S32_MIN, 0, false, true, false},
174 	{S32_MIN, 0, S32_MIN, S32_MIN, 0, false, false, false},
175 
176 	{-1, S32_MIN, S32_MAX, S32_MAX, S32_MIN, true, false, true},
177 	{S32_MIN, -1, S32_MAX, -S32_MAX, S32_MIN, true, false, true},
178 	{-1, S32_MAX, S32_MAX-1, S32_MIN, -S32_MAX, false, false, false},
179 	{S32_MAX, -1, S32_MAX-1, S32_MIN, -S32_MAX, false, true, false},
180 	{-1, -S32_MAX, S32_MIN, S32_MAX-1, S32_MAX, false, false, false},
181 	{-S32_MAX, -1, S32_MIN, S32_MIN+2, S32_MAX, false, false, false},
182 
183 	{1, S32_MIN, -S32_MAX, -S32_MAX, S32_MIN, false, true, false},
184 	{S32_MIN, 1, -S32_MAX, S32_MAX, S32_MIN, false, true, false},
185 	{1, S32_MAX, S32_MIN, S32_MIN+2, S32_MAX, true, false, false},
186 	{S32_MAX, 1, S32_MIN, S32_MAX-1, S32_MAX, true, false, false},
187 
188 	{S32_MIN, S32_MIN, 0, 0, 0, true, false, true},
189 	{S32_MAX, S32_MAX, -2, 0, 1, true, false, true},
190 };
191 DEFINE_TEST_ARRAY(s64) = {
192 	{0, 0, 0, 0, 0, false, false, false},
193 
194 	{0, S64_MAX, S64_MAX, -S64_MAX, 0, false, false, false},
195 	{S64_MAX, 0, S64_MAX, S64_MAX, 0, false, false, false},
196 	{0, S64_MIN, S64_MIN, S64_MIN, 0, false, true, false},
197 	{S64_MIN, 0, S64_MIN, S64_MIN, 0, false, false, false},
198 
199 	{-1, S64_MIN, S64_MAX, S64_MAX, S64_MIN, true, false, true},
200 	{S64_MIN, -1, S64_MAX, -S64_MAX, S64_MIN, true, false, true},
201 	{-1, S64_MAX, S64_MAX-1, S64_MIN, -S64_MAX, false, false, false},
202 	{S64_MAX, -1, S64_MAX-1, S64_MIN, -S64_MAX, false, true, false},
203 	{-1, -S64_MAX, S64_MIN, S64_MAX-1, S64_MAX, false, false, false},
204 	{-S64_MAX, -1, S64_MIN, S64_MIN+2, S64_MAX, false, false, false},
205 
206 	{1, S64_MIN, -S64_MAX, -S64_MAX, S64_MIN, false, true, false},
207 	{S64_MIN, 1, -S64_MAX, S64_MAX, S64_MIN, false, true, false},
208 	{1, S64_MAX, S64_MIN, S64_MIN+2, S64_MAX, true, false, false},
209 	{S64_MAX, 1, S64_MIN, S64_MAX-1, S64_MAX, true, false, false},
210 
211 	{S64_MIN, S64_MIN, 0, 0, 0, true, false, true},
212 	{S64_MAX, S64_MAX, -2, 0, 1, true, false, true},
213 
214 	{-1, -1, -2, 0, 1, false, false, false},
215 	{-1, -128, -129, 127, 128, false, false, false},
216 	{-128, -1, -129, -127, 128, false, false, false},
217 	{0, -S64_MAX, -S64_MAX, S64_MAX, 0, false, false, false},
218 };
219 
220 #define check_one_op(t, fmt, op, sym, a, b, r, of) do {		\
221 	t _r;							\
222 	bool _of;						\
223 								\
224 	_of = check_ ## op ## _overflow(a, b, &_r);		\
225 	KUNIT_EXPECT_EQ_MSG(test, _of, of,			\
226 		"expected "fmt" "sym" "fmt" to%s overflow (type %s)\n",	\
227 		a, b, of ? "" : " not", #t);			\
228 	KUNIT_EXPECT_EQ_MSG(test, _r, r,			\
229 		"expected "fmt" "sym" "fmt" == "fmt", got "fmt" (type %s)\n", \
230 		a, b, r, _r, #t);				\
231 } while (0)
232 
233 #define DEFINE_TEST_FUNC(t, fmt)					\
234 static void do_test_ ## t(struct kunit *test, const struct test_ ## t *p) \
235 {							   		\
236 	check_one_op(t, fmt, add, "+", p->a, p->b, p->sum, p->s_of);	\
237 	check_one_op(t, fmt, add, "+", p->b, p->a, p->sum, p->s_of);	\
238 	check_one_op(t, fmt, sub, "-", p->a, p->b, p->diff, p->d_of);	\
239 	check_one_op(t, fmt, mul, "*", p->a, p->b, p->prod, p->p_of);	\
240 	check_one_op(t, fmt, mul, "*", p->b, p->a, p->prod, p->p_of);	\
241 }									\
242 									\
243 static void t ## _overflow_test(struct kunit *test) {			\
244 	unsigned i;							\
245 									\
246 	for (i = 0; i < ARRAY_SIZE(t ## _tests); ++i)			\
247 		do_test_ ## t(test, &t ## _tests[i]);			\
248 	kunit_info(test, "%zu %s arithmetic tests finished\n",		\
249 		ARRAY_SIZE(t ## _tests), #t);				\
250 }
251 
252 DEFINE_TEST_FUNC(u8, "%d");
253 DEFINE_TEST_FUNC(s8, "%d");
254 DEFINE_TEST_FUNC(u16, "%d");
255 DEFINE_TEST_FUNC(s16, "%d");
256 DEFINE_TEST_FUNC(u32, "%u");
257 DEFINE_TEST_FUNC(s32, "%d");
258 #if BITS_PER_LONG == 64
259 DEFINE_TEST_FUNC(u64, "%llu");
260 DEFINE_TEST_FUNC(s64, "%lld");
261 #endif
262 
263 static void overflow_shift_test(struct kunit *test)
264 {
265 	int count = 0;
266 
267 /* Args are: value, shift, type, expected result, overflow expected */
268 #define TEST_ONE_SHIFT(a, s, t, expect, of)	do {			\
269 	typeof(a) __a = (a);						\
270 	typeof(s) __s = (s);						\
271 	t __e = (expect);						\
272 	t __d;								\
273 	bool __of = check_shl_overflow(__a, __s, &__d);			\
274 	if (__of != of) {						\
275 		KUNIT_EXPECT_EQ_MSG(test, __of, of,			\
276 			"expected (%s)(%s << %s) to%s overflow\n",	\
277 			#t, #a, #s, of ? "" : " not");			\
278 	} else if (!__of && __d != __e) {				\
279 		KUNIT_EXPECT_EQ_MSG(test, __d, __e,			\
280 			"expected (%s)(%s << %s) == %s\n",		\
281 			#t, #a, #s, #expect);				\
282 		if ((t)-1 < 0)						\
283 			kunit_info(test, "got %lld\n", (s64)__d);	\
284 		else							\
285 			kunit_info(test, "got %llu\n", (u64)__d);	\
286 	}								\
287 	count++;							\
288 } while (0)
289 
290 	/* Sane shifts. */
291 	TEST_ONE_SHIFT(1, 0, u8, 1 << 0, false);
292 	TEST_ONE_SHIFT(1, 4, u8, 1 << 4, false);
293 	TEST_ONE_SHIFT(1, 7, u8, 1 << 7, false);
294 	TEST_ONE_SHIFT(0xF, 4, u8, 0xF << 4, false);
295 	TEST_ONE_SHIFT(1, 0, u16, 1 << 0, false);
296 	TEST_ONE_SHIFT(1, 10, u16, 1 << 10, false);
297 	TEST_ONE_SHIFT(1, 15, u16, 1 << 15, false);
298 	TEST_ONE_SHIFT(0xFF, 8, u16, 0xFF << 8, false);
299 	TEST_ONE_SHIFT(1, 0, int, 1 << 0, false);
300 	TEST_ONE_SHIFT(1, 16, int, 1 << 16, false);
301 	TEST_ONE_SHIFT(1, 30, int, 1 << 30, false);
302 	TEST_ONE_SHIFT(1, 0, s32, 1 << 0, false);
303 	TEST_ONE_SHIFT(1, 16, s32, 1 << 16, false);
304 	TEST_ONE_SHIFT(1, 30, s32, 1 << 30, false);
305 	TEST_ONE_SHIFT(1, 0, unsigned int, 1U << 0, false);
306 	TEST_ONE_SHIFT(1, 20, unsigned int, 1U << 20, false);
307 	TEST_ONE_SHIFT(1, 31, unsigned int, 1U << 31, false);
308 	TEST_ONE_SHIFT(0xFFFFU, 16, unsigned int, 0xFFFFU << 16, false);
309 	TEST_ONE_SHIFT(1, 0, u32, 1U << 0, false);
310 	TEST_ONE_SHIFT(1, 20, u32, 1U << 20, false);
311 	TEST_ONE_SHIFT(1, 31, u32, 1U << 31, false);
312 	TEST_ONE_SHIFT(0xFFFFU, 16, u32, 0xFFFFU << 16, false);
313 	TEST_ONE_SHIFT(1, 0, u64, 1ULL << 0, false);
314 	TEST_ONE_SHIFT(1, 40, u64, 1ULL << 40, false);
315 	TEST_ONE_SHIFT(1, 63, u64, 1ULL << 63, false);
316 	TEST_ONE_SHIFT(0xFFFFFFFFULL, 32, u64, 0xFFFFFFFFULL << 32, false);
317 
318 	/* Sane shift: start and end with 0, without a too-wide shift. */
319 	TEST_ONE_SHIFT(0, 7, u8, 0, false);
320 	TEST_ONE_SHIFT(0, 15, u16, 0, false);
321 	TEST_ONE_SHIFT(0, 31, unsigned int, 0, false);
322 	TEST_ONE_SHIFT(0, 31, u32, 0, false);
323 	TEST_ONE_SHIFT(0, 63, u64, 0, false);
324 
325 	/* Sane shift: start and end with 0, without reaching signed bit. */
326 	TEST_ONE_SHIFT(0, 6, s8, 0, false);
327 	TEST_ONE_SHIFT(0, 14, s16, 0, false);
328 	TEST_ONE_SHIFT(0, 30, int, 0, false);
329 	TEST_ONE_SHIFT(0, 30, s32, 0, false);
330 	TEST_ONE_SHIFT(0, 62, s64, 0, false);
331 
332 	/* Overflow: shifted the bit off the end. */
333 	TEST_ONE_SHIFT(1, 8, u8, 0, true);
334 	TEST_ONE_SHIFT(1, 16, u16, 0, true);
335 	TEST_ONE_SHIFT(1, 32, unsigned int, 0, true);
336 	TEST_ONE_SHIFT(1, 32, u32, 0, true);
337 	TEST_ONE_SHIFT(1, 64, u64, 0, true);
338 
339 	/* Overflow: shifted into the signed bit. */
340 	TEST_ONE_SHIFT(1, 7, s8, 0, true);
341 	TEST_ONE_SHIFT(1, 15, s16, 0, true);
342 	TEST_ONE_SHIFT(1, 31, int, 0, true);
343 	TEST_ONE_SHIFT(1, 31, s32, 0, true);
344 	TEST_ONE_SHIFT(1, 63, s64, 0, true);
345 
346 	/* Overflow: high bit falls off unsigned types. */
347 	/* 10010110 */
348 	TEST_ONE_SHIFT(150, 1, u8, 0, true);
349 	/* 1000100010010110 */
350 	TEST_ONE_SHIFT(34966, 1, u16, 0, true);
351 	/* 10000100000010001000100010010110 */
352 	TEST_ONE_SHIFT(2215151766U, 1, u32, 0, true);
353 	TEST_ONE_SHIFT(2215151766U, 1, unsigned int, 0, true);
354 	/* 1000001000010000010000000100000010000100000010001000100010010110 */
355 	TEST_ONE_SHIFT(9372061470395238550ULL, 1, u64, 0, true);
356 
357 	/* Overflow: bit shifted into signed bit on signed types. */
358 	/* 01001011 */
359 	TEST_ONE_SHIFT(75, 1, s8, 0, true);
360 	/* 0100010001001011 */
361 	TEST_ONE_SHIFT(17483, 1, s16, 0, true);
362 	/* 01000010000001000100010001001011 */
363 	TEST_ONE_SHIFT(1107575883, 1, s32, 0, true);
364 	TEST_ONE_SHIFT(1107575883, 1, int, 0, true);
365 	/* 0100000100001000001000000010000001000010000001000100010001001011 */
366 	TEST_ONE_SHIFT(4686030735197619275LL, 1, s64, 0, true);
367 
368 	/* Overflow: bit shifted past signed bit on signed types. */
369 	/* 01001011 */
370 	TEST_ONE_SHIFT(75, 2, s8, 0, true);
371 	/* 0100010001001011 */
372 	TEST_ONE_SHIFT(17483, 2, s16, 0, true);
373 	/* 01000010000001000100010001001011 */
374 	TEST_ONE_SHIFT(1107575883, 2, s32, 0, true);
375 	TEST_ONE_SHIFT(1107575883, 2, int, 0, true);
376 	/* 0100000100001000001000000010000001000010000001000100010001001011 */
377 	TEST_ONE_SHIFT(4686030735197619275LL, 2, s64, 0, true);
378 
379 	/* Overflow: values larger than destination type. */
380 	TEST_ONE_SHIFT(0x100, 0, u8, 0, true);
381 	TEST_ONE_SHIFT(0xFF, 0, s8, 0, true);
382 	TEST_ONE_SHIFT(0x10000U, 0, u16, 0, true);
383 	TEST_ONE_SHIFT(0xFFFFU, 0, s16, 0, true);
384 	TEST_ONE_SHIFT(0x100000000ULL, 0, u32, 0, true);
385 	TEST_ONE_SHIFT(0x100000000ULL, 0, unsigned int, 0, true);
386 	TEST_ONE_SHIFT(0xFFFFFFFFUL, 0, s32, 0, true);
387 	TEST_ONE_SHIFT(0xFFFFFFFFUL, 0, int, 0, true);
388 	TEST_ONE_SHIFT(0xFFFFFFFFFFFFFFFFULL, 0, s64, 0, true);
389 
390 	/* Nonsense: negative initial value. */
391 	TEST_ONE_SHIFT(-1, 0, s8, 0, true);
392 	TEST_ONE_SHIFT(-1, 0, u8, 0, true);
393 	TEST_ONE_SHIFT(-5, 0, s16, 0, true);
394 	TEST_ONE_SHIFT(-5, 0, u16, 0, true);
395 	TEST_ONE_SHIFT(-10, 0, int, 0, true);
396 	TEST_ONE_SHIFT(-10, 0, unsigned int, 0, true);
397 	TEST_ONE_SHIFT(-100, 0, s32, 0, true);
398 	TEST_ONE_SHIFT(-100, 0, u32, 0, true);
399 	TEST_ONE_SHIFT(-10000, 0, s64, 0, true);
400 	TEST_ONE_SHIFT(-10000, 0, u64, 0, true);
401 
402 	/* Nonsense: negative shift values. */
403 	TEST_ONE_SHIFT(0, -5, s8, 0, true);
404 	TEST_ONE_SHIFT(0, -5, u8, 0, true);
405 	TEST_ONE_SHIFT(0, -10, s16, 0, true);
406 	TEST_ONE_SHIFT(0, -10, u16, 0, true);
407 	TEST_ONE_SHIFT(0, -15, int, 0, true);
408 	TEST_ONE_SHIFT(0, -15, unsigned int, 0, true);
409 	TEST_ONE_SHIFT(0, -20, s32, 0, true);
410 	TEST_ONE_SHIFT(0, -20, u32, 0, true);
411 	TEST_ONE_SHIFT(0, -30, s64, 0, true);
412 	TEST_ONE_SHIFT(0, -30, u64, 0, true);
413 
414 	/* Overflow: shifted at or beyond entire type's bit width. */
415 	TEST_ONE_SHIFT(0, 8, u8, 0, true);
416 	TEST_ONE_SHIFT(0, 9, u8, 0, true);
417 	TEST_ONE_SHIFT(0, 8, s8, 0, true);
418 	TEST_ONE_SHIFT(0, 9, s8, 0, true);
419 	TEST_ONE_SHIFT(0, 16, u16, 0, true);
420 	TEST_ONE_SHIFT(0, 17, u16, 0, true);
421 	TEST_ONE_SHIFT(0, 16, s16, 0, true);
422 	TEST_ONE_SHIFT(0, 17, s16, 0, true);
423 	TEST_ONE_SHIFT(0, 32, u32, 0, true);
424 	TEST_ONE_SHIFT(0, 33, u32, 0, true);
425 	TEST_ONE_SHIFT(0, 32, int, 0, true);
426 	TEST_ONE_SHIFT(0, 33, int, 0, true);
427 	TEST_ONE_SHIFT(0, 32, s32, 0, true);
428 	TEST_ONE_SHIFT(0, 33, s32, 0, true);
429 	TEST_ONE_SHIFT(0, 64, u64, 0, true);
430 	TEST_ONE_SHIFT(0, 65, u64, 0, true);
431 	TEST_ONE_SHIFT(0, 64, s64, 0, true);
432 	TEST_ONE_SHIFT(0, 65, s64, 0, true);
433 
434 	/*
435 	 * Corner case: for unsigned types, we fail when we've shifted
436 	 * through the entire width of bits. For signed types, we might
437 	 * want to match this behavior, but that would mean noticing if
438 	 * we shift through all but the signed bit, and this is not
439 	 * currently detected (but we'll notice an overflow into the
440 	 * signed bit). So, for now, we will test this condition but
441 	 * mark it as not expected to overflow.
442 	 */
443 	TEST_ONE_SHIFT(0, 7, s8, 0, false);
444 	TEST_ONE_SHIFT(0, 15, s16, 0, false);
445 	TEST_ONE_SHIFT(0, 31, int, 0, false);
446 	TEST_ONE_SHIFT(0, 31, s32, 0, false);
447 	TEST_ONE_SHIFT(0, 63, s64, 0, false);
448 
449 	kunit_info(test, "%d shift tests finished\n", count);
450 #undef TEST_ONE_SHIFT
451 }
452 
453 /*
454  * Deal with the various forms of allocator arguments. See comments above
455  * the DEFINE_TEST_ALLOC() instances for mapping of the "bits".
456  */
457 #define alloc_GFP		 (GFP_KERNEL | __GFP_NOWARN)
458 #define alloc010(alloc, arg, sz) alloc(sz, alloc_GFP)
459 #define alloc011(alloc, arg, sz) alloc(sz, alloc_GFP, NUMA_NO_NODE)
460 #define alloc000(alloc, arg, sz) alloc(sz)
461 #define alloc001(alloc, arg, sz) alloc(sz, NUMA_NO_NODE)
462 #define alloc110(alloc, arg, sz) alloc(arg, sz, alloc_GFP)
463 #define free0(free, arg, ptr)	 free(ptr)
464 #define free1(free, arg, ptr)	 free(arg, ptr)
465 
466 /* Wrap around to 16K */
467 #define TEST_SIZE		(5 * 4096)
468 
469 #define DEFINE_TEST_ALLOC(func, free_func, want_arg, want_gfp, want_node)\
470 static void test_ ## func (struct kunit *test, void *arg)		\
471 {									\
472 	volatile size_t a = TEST_SIZE;					\
473 	volatile size_t b = (SIZE_MAX / TEST_SIZE) + 1;			\
474 	void *ptr;							\
475 									\
476 	/* Tiny allocation test. */					\
477 	ptr = alloc ## want_arg ## want_gfp ## want_node (func, arg, 1);\
478 	KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr,			\
479 			    #func " failed regular allocation?!\n");	\
480 	free ## want_arg (free_func, arg, ptr);				\
481 									\
482 	/* Wrapped allocation test. */					\
483 	ptr = alloc ## want_arg ## want_gfp ## want_node (func, arg,	\
484 							  a * b);	\
485 	KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr,			\
486 			    #func " unexpectedly failed bad wrapping?!\n"); \
487 	free ## want_arg (free_func, arg, ptr);				\
488 									\
489 	/* Saturated allocation test. */				\
490 	ptr = alloc ## want_arg ## want_gfp ## want_node (func, arg,	\
491 						   array_size(a, b));	\
492 	if (ptr) {							\
493 		KUNIT_FAIL(test, #func " missed saturation!\n");	\
494 		free ## want_arg (free_func, arg, ptr);			\
495 	}								\
496 }
497 
498 /*
499  * Allocator uses a trailing node argument --------+  (e.g. kmalloc_node())
500  * Allocator uses the gfp_t argument -----------+  |  (e.g. kmalloc())
501  * Allocator uses a special leading argument +  |  |  (e.g. devm_kmalloc())
502  *                                           |  |  |
503  */
504 DEFINE_TEST_ALLOC(kmalloc,	 kfree,	     0, 1, 0);
505 DEFINE_TEST_ALLOC(kmalloc_node,	 kfree,	     0, 1, 1);
506 DEFINE_TEST_ALLOC(kzalloc,	 kfree,	     0, 1, 0);
507 DEFINE_TEST_ALLOC(kzalloc_node,  kfree,	     0, 1, 1);
508 DEFINE_TEST_ALLOC(__vmalloc,	 vfree,	     0, 1, 0);
509 DEFINE_TEST_ALLOC(kvmalloc,	 kvfree,     0, 1, 0);
510 DEFINE_TEST_ALLOC(kvmalloc_node, kvfree,     0, 1, 1);
511 DEFINE_TEST_ALLOC(kvzalloc,	 kvfree,     0, 1, 0);
512 DEFINE_TEST_ALLOC(kvzalloc_node, kvfree,     0, 1, 1);
513 DEFINE_TEST_ALLOC(devm_kmalloc,  devm_kfree, 1, 1, 0);
514 DEFINE_TEST_ALLOC(devm_kzalloc,  devm_kfree, 1, 1, 0);
515 
516 static void overflow_allocation_test(struct kunit *test)
517 {
518 	const char device_name[] = "overflow-test";
519 	struct device *dev;
520 	int count = 0;
521 
522 #define check_allocation_overflow(alloc)	do {	\
523 	count++;					\
524 	test_ ## alloc(test, dev);			\
525 } while (0)
526 
527 	/* Create dummy device for devm_kmalloc()-family tests. */
528 	dev = root_device_register(device_name);
529 	KUNIT_ASSERT_FALSE_MSG(test, IS_ERR(dev),
530 			       "Cannot register test device\n");
531 
532 	check_allocation_overflow(kmalloc);
533 	check_allocation_overflow(kmalloc_node);
534 	check_allocation_overflow(kzalloc);
535 	check_allocation_overflow(kzalloc_node);
536 	check_allocation_overflow(__vmalloc);
537 	check_allocation_overflow(kvmalloc);
538 	check_allocation_overflow(kvmalloc_node);
539 	check_allocation_overflow(kvzalloc);
540 	check_allocation_overflow(kvzalloc_node);
541 	check_allocation_overflow(devm_kmalloc);
542 	check_allocation_overflow(devm_kzalloc);
543 
544 	device_unregister(dev);
545 
546 	kunit_info(test, "%d allocation overflow tests finished\n", count);
547 #undef check_allocation_overflow
548 }
549 
550 struct __test_flex_array {
551 	unsigned long flags;
552 	size_t count;
553 	unsigned long data[];
554 };
555 
556 static void overflow_size_helpers_test(struct kunit *test)
557 {
558 	/* Make sure struct_size() can be used in a constant expression. */
559 	u8 ce_array[struct_size((struct __test_flex_array *)0, data, 55)];
560 	struct __test_flex_array *obj;
561 	int count = 0;
562 	int var;
563 	volatile int unconst = 0;
564 
565 	/* Verify constant expression against runtime version. */
566 	var = 55;
567 	OPTIMIZER_HIDE_VAR(var);
568 	KUNIT_EXPECT_EQ(test, sizeof(ce_array), struct_size(obj, data, var));
569 
570 #define check_one_size_helper(expected, func, args...)	do {	\
571 	size_t _r = func(args);					\
572 	KUNIT_EXPECT_EQ_MSG(test, _r, expected,			\
573 		"expected " #func "(" #args ") to return %zu but got %zu instead\n", \
574 		(size_t)(expected), _r);			\
575 	count++;						\
576 } while (0)
577 
578 	var = 4;
579 	check_one_size_helper(20,	size_mul, var++, 5);
580 	check_one_size_helper(20,	size_mul, 4, var++);
581 	check_one_size_helper(0,	size_mul, 0, 3);
582 	check_one_size_helper(0,	size_mul, 3, 0);
583 	check_one_size_helper(6,	size_mul, 2, 3);
584 	check_one_size_helper(SIZE_MAX,	size_mul, SIZE_MAX,  1);
585 	check_one_size_helper(SIZE_MAX,	size_mul, SIZE_MAX,  3);
586 	check_one_size_helper(SIZE_MAX,	size_mul, SIZE_MAX, -3);
587 
588 	var = 4;
589 	check_one_size_helper(9,	size_add, var++, 5);
590 	check_one_size_helper(9,	size_add, 4, var++);
591 	check_one_size_helper(9,	size_add, 9, 0);
592 	check_one_size_helper(9,	size_add, 0, 9);
593 	check_one_size_helper(5,	size_add, 2, 3);
594 	check_one_size_helper(SIZE_MAX, size_add, SIZE_MAX,  1);
595 	check_one_size_helper(SIZE_MAX, size_add, SIZE_MAX,  3);
596 	check_one_size_helper(SIZE_MAX, size_add, SIZE_MAX, -3);
597 
598 	var = 4;
599 	check_one_size_helper(1,	size_sub, var--, 3);
600 	check_one_size_helper(1,	size_sub, 4, var--);
601 	check_one_size_helper(1,	size_sub, 3, 2);
602 	check_one_size_helper(9,	size_sub, 9, 0);
603 	check_one_size_helper(SIZE_MAX, size_sub, 9, -3);
604 	check_one_size_helper(SIZE_MAX, size_sub, 0, 9);
605 	check_one_size_helper(SIZE_MAX, size_sub, 2, 3);
606 	check_one_size_helper(SIZE_MAX, size_sub, SIZE_MAX,  0);
607 	check_one_size_helper(SIZE_MAX, size_sub, SIZE_MAX, 10);
608 	check_one_size_helper(SIZE_MAX, size_sub, 0,  SIZE_MAX);
609 	check_one_size_helper(SIZE_MAX, size_sub, 14, SIZE_MAX);
610 	check_one_size_helper(SIZE_MAX - 2, size_sub, SIZE_MAX - 1,  1);
611 	check_one_size_helper(SIZE_MAX - 4, size_sub, SIZE_MAX - 1,  3);
612 	check_one_size_helper(1,		size_sub, SIZE_MAX - 1, -3);
613 
614 	var = 4;
615 	check_one_size_helper(4 * sizeof(*obj->data),
616 			      flex_array_size, obj, data, var++);
617 	check_one_size_helper(5 * sizeof(*obj->data),
618 			      flex_array_size, obj, data, var++);
619 	check_one_size_helper(0, flex_array_size, obj, data, 0 + unconst);
620 	check_one_size_helper(sizeof(*obj->data),
621 			      flex_array_size, obj, data, 1 + unconst);
622 	check_one_size_helper(7 * sizeof(*obj->data),
623 			      flex_array_size, obj, data, 7 + unconst);
624 	check_one_size_helper(SIZE_MAX,
625 			      flex_array_size, obj, data, -1 + unconst);
626 	check_one_size_helper(SIZE_MAX,
627 			      flex_array_size, obj, data, SIZE_MAX - 4 + unconst);
628 
629 	var = 4;
630 	check_one_size_helper(sizeof(*obj) + (4 * sizeof(*obj->data)),
631 			      struct_size, obj, data, var++);
632 	check_one_size_helper(sizeof(*obj) + (5 * sizeof(*obj->data)),
633 			      struct_size, obj, data, var++);
634 	check_one_size_helper(sizeof(*obj), struct_size, obj, data, 0 + unconst);
635 	check_one_size_helper(sizeof(*obj) + sizeof(*obj->data),
636 			      struct_size, obj, data, 1 + unconst);
637 	check_one_size_helper(SIZE_MAX,
638 			      struct_size, obj, data, -3 + unconst);
639 	check_one_size_helper(SIZE_MAX,
640 			      struct_size, obj, data, SIZE_MAX - 3 + unconst);
641 
642 	kunit_info(test, "%d overflow size helper tests finished\n", count);
643 #undef check_one_size_helper
644 }
645 
646 static struct kunit_case overflow_test_cases[] = {
647 	KUNIT_CASE(u8_overflow_test),
648 	KUNIT_CASE(s8_overflow_test),
649 	KUNIT_CASE(u16_overflow_test),
650 	KUNIT_CASE(s16_overflow_test),
651 	KUNIT_CASE(u32_overflow_test),
652 	KUNIT_CASE(s32_overflow_test),
653 #if BITS_PER_LONG == 64
654 	KUNIT_CASE(u64_overflow_test),
655 	KUNIT_CASE(s64_overflow_test),
656 #endif
657 	KUNIT_CASE(overflow_shift_test),
658 	KUNIT_CASE(overflow_allocation_test),
659 	KUNIT_CASE(overflow_size_helpers_test),
660 	{}
661 };
662 
663 static struct kunit_suite overflow_test_suite = {
664 	.name = "overflow",
665 	.test_cases = overflow_test_cases,
666 };
667 
668 kunit_test_suite(overflow_test_suite);
669 
670 MODULE_LICENSE("Dual MIT/GPL");
671