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