1 // SPDX-License-Identifier: GPL-2.0 OR MIT
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]
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 #include <kunit/test.h>
10 #include <linux/device.h>
11 #include <linux/kernel.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>
19 #define DEFINE_TEST_ARRAY(t) \
20 static const struct test_ ## t { \
23 bool s_of, d_of, p_of; \
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},
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},
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},
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},
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},
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},
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},
68 {123, 234, 357, 65425, 28782, false, true, false},
69 {1234, 2345, 3579, 64425, 10146, false, true, true},
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},
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},
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},
90 {-2U, 1U, -1U, -3U, -2U, false, false, false},
91 {-4U, 5U, 1U, -9U, -20U, true, false, true},
94 #if BITS_PER_LONG == 64
95 DEFINE_TEST_ARRAY(u64) = {
96 {0, 0, 0, 0, 0, false, false, false},
97 {1, 1, 2, 0, 1, false, false, false},
98 {0, 1, 1, U64_MAX, 0, false, true, false},
99 {1, 0, 1, 1, 0, false, false, false},
100 {0, U64_MAX, U64_MAX, 1, 0, false, true, false},
101 {U64_MAX, 0, U64_MAX, U64_MAX, 0, false, false, false},
102 {1, U64_MAX, 0, 2, U64_MAX, true, true, false},
103 {U64_MAX, 1, 0, U64_MAX-1, U64_MAX, true, false, false},
104 {U64_MAX, U64_MAX, U64_MAX-1, 0, 1, true, false, true},
106 {U64_MAX, U64_MAX-1, U64_MAX-2, 1, 2, true, false, true},
107 {U64_MAX-1, U64_MAX, U64_MAX-2, U64_MAX, 2, true, true, true},
109 {1ULL << 31, 1ULL << 31, 1ULL << 32, 0, 1ULL << 62, false, false, false},
110 {1ULL << 32, 1ULL << 32, 1ULL << 33, 0, 0, false, false, true},
111 {1ULL << 32, 1ULL << 31, 3*(1ULL << 31), 1ULL << 31, 1ULL << 63, false, false, false},
112 {1ULL << 63, 1ULL << 63, 0, 0, 0, true, false, true},
113 {1000000000ULL /* 10^9 */, 10000000000ULL /* 10^10 */,
114 11000000000ULL, 18446744064709551616ULL, 10000000000000000000ULL,
116 {-15ULL, 10ULL, -5ULL, -25ULL, -150ULL, false, false, true},
120 DEFINE_TEST_ARRAY(s8) = {
121 {0, 0, 0, 0, 0, false, false, false},
123 {0, S8_MAX, S8_MAX, -S8_MAX, 0, false, false, false},
124 {S8_MAX, 0, S8_MAX, S8_MAX, 0, false, false, false},
125 {0, S8_MIN, S8_MIN, S8_MIN, 0, false, true, false},
126 {S8_MIN, 0, S8_MIN, S8_MIN, 0, false, false, false},
128 {-1, S8_MIN, S8_MAX, S8_MAX, S8_MIN, true, false, true},
129 {S8_MIN, -1, S8_MAX, -S8_MAX, S8_MIN, true, false, true},
130 {-1, S8_MAX, S8_MAX-1, S8_MIN, -S8_MAX, false, false, false},
131 {S8_MAX, -1, S8_MAX-1, S8_MIN, -S8_MAX, false, true, false},
132 {-1, -S8_MAX, S8_MIN, S8_MAX-1, S8_MAX, false, false, false},
133 {-S8_MAX, -1, S8_MIN, S8_MIN+2, S8_MAX, false, false, false},
135 {1, S8_MIN, -S8_MAX, -S8_MAX, S8_MIN, false, true, false},
136 {S8_MIN, 1, -S8_MAX, S8_MAX, S8_MIN, false, true, false},
137 {1, S8_MAX, S8_MIN, S8_MIN+2, S8_MAX, true, false, false},
138 {S8_MAX, 1, S8_MIN, S8_MAX-1, S8_MAX, true, false, false},
140 {S8_MIN, S8_MIN, 0, 0, 0, true, false, true},
141 {S8_MAX, S8_MAX, -2, 0, 1, true, false, true},
143 {-4, -32, -36, 28, -128, false, false, true},
144 {-4, 32, 28, -36, -128, false, false, false},
147 DEFINE_TEST_ARRAY(s16) = {
148 {0, 0, 0, 0, 0, false, false, false},
150 {0, S16_MAX, S16_MAX, -S16_MAX, 0, false, false, false},
151 {S16_MAX, 0, S16_MAX, S16_MAX, 0, false, false, false},
152 {0, S16_MIN, S16_MIN, S16_MIN, 0, false, true, false},
153 {S16_MIN, 0, S16_MIN, S16_MIN, 0, false, false, false},
155 {-1, S16_MIN, S16_MAX, S16_MAX, S16_MIN, true, false, true},
156 {S16_MIN, -1, S16_MAX, -S16_MAX, S16_MIN, true, false, true},
157 {-1, S16_MAX, S16_MAX-1, S16_MIN, -S16_MAX, false, false, false},
158 {S16_MAX, -1, S16_MAX-1, S16_MIN, -S16_MAX, false, true, false},
159 {-1, -S16_MAX, S16_MIN, S16_MAX-1, S16_MAX, false, false, false},
160 {-S16_MAX, -1, S16_MIN, S16_MIN+2, S16_MAX, false, false, false},
162 {1, S16_MIN, -S16_MAX, -S16_MAX, S16_MIN, false, true, false},
163 {S16_MIN, 1, -S16_MAX, S16_MAX, S16_MIN, false, true, false},
164 {1, S16_MAX, S16_MIN, S16_MIN+2, S16_MAX, true, false, false},
165 {S16_MAX, 1, S16_MIN, S16_MAX-1, S16_MAX, true, false, false},
167 {S16_MIN, S16_MIN, 0, 0, 0, true, false, true},
168 {S16_MAX, S16_MAX, -2, 0, 1, true, false, true},
170 DEFINE_TEST_ARRAY(s32) = {
171 {0, 0, 0, 0, 0, false, false, false},
173 {0, S32_MAX, S32_MAX, -S32_MAX, 0, false, false, false},
174 {S32_MAX, 0, S32_MAX, S32_MAX, 0, false, false, false},
175 {0, S32_MIN, S32_MIN, S32_MIN, 0, false, true, false},
176 {S32_MIN, 0, S32_MIN, S32_MIN, 0, false, false, false},
178 {-1, S32_MIN, S32_MAX, S32_MAX, S32_MIN, true, false, true},
179 {S32_MIN, -1, S32_MAX, -S32_MAX, S32_MIN, true, false, true},
180 {-1, S32_MAX, S32_MAX-1, S32_MIN, -S32_MAX, false, false, false},
181 {S32_MAX, -1, S32_MAX-1, S32_MIN, -S32_MAX, false, true, false},
182 {-1, -S32_MAX, S32_MIN, S32_MAX-1, S32_MAX, false, false, false},
183 {-S32_MAX, -1, S32_MIN, S32_MIN+2, S32_MAX, false, false, false},
185 {1, S32_MIN, -S32_MAX, -S32_MAX, S32_MIN, false, true, false},
186 {S32_MIN, 1, -S32_MAX, S32_MAX, S32_MIN, false, true, false},
187 {1, S32_MAX, S32_MIN, S32_MIN+2, S32_MAX, true, false, false},
188 {S32_MAX, 1, S32_MIN, S32_MAX-1, S32_MAX, true, false, false},
190 {S32_MIN, S32_MIN, 0, 0, 0, true, false, true},
191 {S32_MAX, S32_MAX, -2, 0, 1, true, false, true},
194 #if BITS_PER_LONG == 64
195 DEFINE_TEST_ARRAY(s64) = {
196 {0, 0, 0, 0, 0, false, false, false},
198 {0, S64_MAX, S64_MAX, -S64_MAX, 0, false, false, false},
199 {S64_MAX, 0, S64_MAX, S64_MAX, 0, false, false, false},
200 {0, S64_MIN, S64_MIN, S64_MIN, 0, false, true, false},
201 {S64_MIN, 0, S64_MIN, S64_MIN, 0, false, false, false},
203 {-1, S64_MIN, S64_MAX, S64_MAX, S64_MIN, true, false, true},
204 {S64_MIN, -1, S64_MAX, -S64_MAX, S64_MIN, true, false, true},
205 {-1, S64_MAX, S64_MAX-1, S64_MIN, -S64_MAX, false, false, false},
206 {S64_MAX, -1, S64_MAX-1, S64_MIN, -S64_MAX, false, true, false},
207 {-1, -S64_MAX, S64_MIN, S64_MAX-1, S64_MAX, false, false, false},
208 {-S64_MAX, -1, S64_MIN, S64_MIN+2, S64_MAX, false, false, false},
210 {1, S64_MIN, -S64_MAX, -S64_MAX, S64_MIN, false, true, false},
211 {S64_MIN, 1, -S64_MAX, S64_MAX, S64_MIN, false, true, false},
212 {1, S64_MAX, S64_MIN, S64_MIN+2, S64_MAX, true, false, false},
213 {S64_MAX, 1, S64_MIN, S64_MAX-1, S64_MAX, true, false, false},
215 {S64_MIN, S64_MIN, 0, 0, 0, true, false, true},
216 {S64_MAX, S64_MAX, -2, 0, 1, true, false, true},
218 {-1, -1, -2, 0, 1, false, false, false},
219 {-1, -128, -129, 127, 128, false, false, false},
220 {-128, -1, -129, -127, 128, false, false, false},
221 {0, -S64_MAX, -S64_MAX, S64_MAX, 0, false, false, false},
225 #define check_one_op(t, fmt, op, sym, a, b, r, of) do { \
229 _of = check_ ## op ## _overflow(a, b, &_r); \
230 KUNIT_EXPECT_EQ_MSG(test, _of, of, \
231 "expected "fmt" "sym" "fmt" to%s overflow (type %s)\n", \
232 a, b, of ? "" : " not", #t); \
233 KUNIT_EXPECT_EQ_MSG(test, _r, r, \
234 "expected "fmt" "sym" "fmt" == "fmt", got "fmt" (type %s)\n", \
238 #define DEFINE_TEST_FUNC(t, fmt) \
239 static void do_test_ ## t(struct kunit *test, const struct test_ ## t *p) \
241 check_one_op(t, fmt, add, "+", p->a, p->b, p->sum, p->s_of); \
242 check_one_op(t, fmt, add, "+", p->b, p->a, p->sum, p->s_of); \
243 check_one_op(t, fmt, sub, "-", p->a, p->b, p->diff, p->d_of); \
244 check_one_op(t, fmt, mul, "*", p->a, p->b, p->prod, p->p_of); \
245 check_one_op(t, fmt, mul, "*", p->b, p->a, p->prod, p->p_of); \
248 static void t ## _overflow_test(struct kunit *test) { \
251 for (i = 0; i < ARRAY_SIZE(t ## _tests); ++i) \
252 do_test_ ## t(test, &t ## _tests[i]); \
253 kunit_info(test, "%zu %s arithmetic tests finished\n", \
254 ARRAY_SIZE(t ## _tests), #t); \
257 DEFINE_TEST_FUNC(u8, "%d");
258 DEFINE_TEST_FUNC(s8, "%d");
259 DEFINE_TEST_FUNC(u16, "%d");
260 DEFINE_TEST_FUNC(s16, "%d");
261 DEFINE_TEST_FUNC(u32, "%u");
262 DEFINE_TEST_FUNC(s32, "%d");
263 #if BITS_PER_LONG == 64
264 DEFINE_TEST_FUNC(u64, "%llu");
265 DEFINE_TEST_FUNC(s64, "%lld");
268 static void overflow_shift_test(struct kunit *test)
272 /* Args are: value, shift, type, expected result, overflow expected */
273 #define TEST_ONE_SHIFT(a, s, t, expect, of) do { \
274 typeof(a) __a = (a); \
275 typeof(s) __s = (s); \
278 bool __of = check_shl_overflow(__a, __s, &__d); \
280 KUNIT_EXPECT_EQ_MSG(test, __of, of, \
281 "expected (%s)(%s << %s) to%s overflow\n", \
282 #t, #a, #s, of ? "" : " not"); \
283 } else if (!__of && __d != __e) { \
284 KUNIT_EXPECT_EQ_MSG(test, __d, __e, \
285 "expected (%s)(%s << %s) == %s\n", \
286 #t, #a, #s, #expect); \
288 kunit_info(test, "got %lld\n", (s64)__d); \
290 kunit_info(test, "got %llu\n", (u64)__d); \
296 TEST_ONE_SHIFT(1, 0, u8, 1 << 0, false);
297 TEST_ONE_SHIFT(1, 4, u8, 1 << 4, false);
298 TEST_ONE_SHIFT(1, 7, u8, 1 << 7, false);
299 TEST_ONE_SHIFT(0xF, 4, u8, 0xF << 4, false);
300 TEST_ONE_SHIFT(1, 0, u16, 1 << 0, false);
301 TEST_ONE_SHIFT(1, 10, u16, 1 << 10, false);
302 TEST_ONE_SHIFT(1, 15, u16, 1 << 15, false);
303 TEST_ONE_SHIFT(0xFF, 8, u16, 0xFF << 8, false);
304 TEST_ONE_SHIFT(1, 0, int, 1 << 0, false);
305 TEST_ONE_SHIFT(1, 16, int, 1 << 16, false);
306 TEST_ONE_SHIFT(1, 30, int, 1 << 30, false);
307 TEST_ONE_SHIFT(1, 0, s32, 1 << 0, false);
308 TEST_ONE_SHIFT(1, 16, s32, 1 << 16, false);
309 TEST_ONE_SHIFT(1, 30, s32, 1 << 30, false);
310 TEST_ONE_SHIFT(1, 0, unsigned int, 1U << 0, false);
311 TEST_ONE_SHIFT(1, 20, unsigned int, 1U << 20, false);
312 TEST_ONE_SHIFT(1, 31, unsigned int, 1U << 31, false);
313 TEST_ONE_SHIFT(0xFFFFU, 16, unsigned int, 0xFFFFU << 16, false);
314 TEST_ONE_SHIFT(1, 0, u32, 1U << 0, false);
315 TEST_ONE_SHIFT(1, 20, u32, 1U << 20, false);
316 TEST_ONE_SHIFT(1, 31, u32, 1U << 31, false);
317 TEST_ONE_SHIFT(0xFFFFU, 16, u32, 0xFFFFU << 16, false);
318 TEST_ONE_SHIFT(1, 0, u64, 1ULL << 0, false);
319 TEST_ONE_SHIFT(1, 40, u64, 1ULL << 40, false);
320 TEST_ONE_SHIFT(1, 63, u64, 1ULL << 63, false);
321 TEST_ONE_SHIFT(0xFFFFFFFFULL, 32, u64, 0xFFFFFFFFULL << 32, false);
323 /* Sane shift: start and end with 0, without a too-wide shift. */
324 TEST_ONE_SHIFT(0, 7, u8, 0, false);
325 TEST_ONE_SHIFT(0, 15, u16, 0, false);
326 TEST_ONE_SHIFT(0, 31, unsigned int, 0, false);
327 TEST_ONE_SHIFT(0, 31, u32, 0, false);
328 TEST_ONE_SHIFT(0, 63, u64, 0, false);
330 /* Sane shift: start and end with 0, without reaching signed bit. */
331 TEST_ONE_SHIFT(0, 6, s8, 0, false);
332 TEST_ONE_SHIFT(0, 14, s16, 0, false);
333 TEST_ONE_SHIFT(0, 30, int, 0, false);
334 TEST_ONE_SHIFT(0, 30, s32, 0, false);
335 TEST_ONE_SHIFT(0, 62, s64, 0, false);
337 /* Overflow: shifted the bit off the end. */
338 TEST_ONE_SHIFT(1, 8, u8, 0, true);
339 TEST_ONE_SHIFT(1, 16, u16, 0, true);
340 TEST_ONE_SHIFT(1, 32, unsigned int, 0, true);
341 TEST_ONE_SHIFT(1, 32, u32, 0, true);
342 TEST_ONE_SHIFT(1, 64, u64, 0, true);
344 /* Overflow: shifted into the signed bit. */
345 TEST_ONE_SHIFT(1, 7, s8, 0, true);
346 TEST_ONE_SHIFT(1, 15, s16, 0, true);
347 TEST_ONE_SHIFT(1, 31, int, 0, true);
348 TEST_ONE_SHIFT(1, 31, s32, 0, true);
349 TEST_ONE_SHIFT(1, 63, s64, 0, true);
351 /* Overflow: high bit falls off unsigned types. */
353 TEST_ONE_SHIFT(150, 1, u8, 0, true);
354 /* 1000100010010110 */
355 TEST_ONE_SHIFT(34966, 1, u16, 0, true);
356 /* 10000100000010001000100010010110 */
357 TEST_ONE_SHIFT(2215151766U, 1, u32, 0, true);
358 TEST_ONE_SHIFT(2215151766U, 1, unsigned int, 0, true);
359 /* 1000001000010000010000000100000010000100000010001000100010010110 */
360 TEST_ONE_SHIFT(9372061470395238550ULL, 1, u64, 0, true);
362 /* Overflow: bit shifted into signed bit on signed types. */
364 TEST_ONE_SHIFT(75, 1, s8, 0, true);
365 /* 0100010001001011 */
366 TEST_ONE_SHIFT(17483, 1, s16, 0, true);
367 /* 01000010000001000100010001001011 */
368 TEST_ONE_SHIFT(1107575883, 1, s32, 0, true);
369 TEST_ONE_SHIFT(1107575883, 1, int, 0, true);
370 /* 0100000100001000001000000010000001000010000001000100010001001011 */
371 TEST_ONE_SHIFT(4686030735197619275LL, 1, s64, 0, true);
373 /* Overflow: bit shifted past signed bit on signed types. */
375 TEST_ONE_SHIFT(75, 2, s8, 0, true);
376 /* 0100010001001011 */
377 TEST_ONE_SHIFT(17483, 2, s16, 0, true);
378 /* 01000010000001000100010001001011 */
379 TEST_ONE_SHIFT(1107575883, 2, s32, 0, true);
380 TEST_ONE_SHIFT(1107575883, 2, int, 0, true);
381 /* 0100000100001000001000000010000001000010000001000100010001001011 */
382 TEST_ONE_SHIFT(4686030735197619275LL, 2, s64, 0, true);
384 /* Overflow: values larger than destination type. */
385 TEST_ONE_SHIFT(0x100, 0, u8, 0, true);
386 TEST_ONE_SHIFT(0xFF, 0, s8, 0, true);
387 TEST_ONE_SHIFT(0x10000U, 0, u16, 0, true);
388 TEST_ONE_SHIFT(0xFFFFU, 0, s16, 0, true);
389 TEST_ONE_SHIFT(0x100000000ULL, 0, u32, 0, true);
390 TEST_ONE_SHIFT(0x100000000ULL, 0, unsigned int, 0, true);
391 TEST_ONE_SHIFT(0xFFFFFFFFUL, 0, s32, 0, true);
392 TEST_ONE_SHIFT(0xFFFFFFFFUL, 0, int, 0, true);
393 TEST_ONE_SHIFT(0xFFFFFFFFFFFFFFFFULL, 0, s64, 0, true);
395 /* Nonsense: negative initial value. */
396 TEST_ONE_SHIFT(-1, 0, s8, 0, true);
397 TEST_ONE_SHIFT(-1, 0, u8, 0, true);
398 TEST_ONE_SHIFT(-5, 0, s16, 0, true);
399 TEST_ONE_SHIFT(-5, 0, u16, 0, true);
400 TEST_ONE_SHIFT(-10, 0, int, 0, true);
401 TEST_ONE_SHIFT(-10, 0, unsigned int, 0, true);
402 TEST_ONE_SHIFT(-100, 0, s32, 0, true);
403 TEST_ONE_SHIFT(-100, 0, u32, 0, true);
404 TEST_ONE_SHIFT(-10000, 0, s64, 0, true);
405 TEST_ONE_SHIFT(-10000, 0, u64, 0, true);
407 /* Nonsense: negative shift values. */
408 TEST_ONE_SHIFT(0, -5, s8, 0, true);
409 TEST_ONE_SHIFT(0, -5, u8, 0, true);
410 TEST_ONE_SHIFT(0, -10, s16, 0, true);
411 TEST_ONE_SHIFT(0, -10, u16, 0, true);
412 TEST_ONE_SHIFT(0, -15, int, 0, true);
413 TEST_ONE_SHIFT(0, -15, unsigned int, 0, true);
414 TEST_ONE_SHIFT(0, -20, s32, 0, true);
415 TEST_ONE_SHIFT(0, -20, u32, 0, true);
416 TEST_ONE_SHIFT(0, -30, s64, 0, true);
417 TEST_ONE_SHIFT(0, -30, u64, 0, true);
419 /* Overflow: shifted at or beyond entire type's bit width. */
420 TEST_ONE_SHIFT(0, 8, u8, 0, true);
421 TEST_ONE_SHIFT(0, 9, u8, 0, true);
422 TEST_ONE_SHIFT(0, 8, s8, 0, true);
423 TEST_ONE_SHIFT(0, 9, s8, 0, true);
424 TEST_ONE_SHIFT(0, 16, u16, 0, true);
425 TEST_ONE_SHIFT(0, 17, u16, 0, true);
426 TEST_ONE_SHIFT(0, 16, s16, 0, true);
427 TEST_ONE_SHIFT(0, 17, s16, 0, true);
428 TEST_ONE_SHIFT(0, 32, u32, 0, true);
429 TEST_ONE_SHIFT(0, 33, u32, 0, true);
430 TEST_ONE_SHIFT(0, 32, int, 0, true);
431 TEST_ONE_SHIFT(0, 33, int, 0, true);
432 TEST_ONE_SHIFT(0, 32, s32, 0, true);
433 TEST_ONE_SHIFT(0, 33, s32, 0, true);
434 TEST_ONE_SHIFT(0, 64, u64, 0, true);
435 TEST_ONE_SHIFT(0, 65, u64, 0, true);
436 TEST_ONE_SHIFT(0, 64, s64, 0, true);
437 TEST_ONE_SHIFT(0, 65, s64, 0, true);
440 * Corner case: for unsigned types, we fail when we've shifted
441 * through the entire width of bits. For signed types, we might
442 * want to match this behavior, but that would mean noticing if
443 * we shift through all but the signed bit, and this is not
444 * currently detected (but we'll notice an overflow into the
445 * signed bit). So, for now, we will test this condition but
446 * mark it as not expected to overflow.
448 TEST_ONE_SHIFT(0, 7, s8, 0, false);
449 TEST_ONE_SHIFT(0, 15, s16, 0, false);
450 TEST_ONE_SHIFT(0, 31, int, 0, false);
451 TEST_ONE_SHIFT(0, 31, s32, 0, false);
452 TEST_ONE_SHIFT(0, 63, s64, 0, false);
454 kunit_info(test, "%d shift tests finished\n", count);
455 #undef TEST_ONE_SHIFT
459 * Deal with the various forms of allocator arguments. See comments above
460 * the DEFINE_TEST_ALLOC() instances for mapping of the "bits".
462 #define alloc_GFP (GFP_KERNEL | __GFP_NOWARN)
463 #define alloc010(alloc, arg, sz) alloc(sz, alloc_GFP)
464 #define alloc011(alloc, arg, sz) alloc(sz, alloc_GFP, NUMA_NO_NODE)
465 #define alloc000(alloc, arg, sz) alloc(sz)
466 #define alloc001(alloc, arg, sz) alloc(sz, NUMA_NO_NODE)
467 #define alloc110(alloc, arg, sz) alloc(arg, sz, alloc_GFP)
468 #define free0(free, arg, ptr) free(ptr)
469 #define free1(free, arg, ptr) free(arg, ptr)
471 /* Wrap around to 16K */
472 #define TEST_SIZE (5 * 4096)
474 #define DEFINE_TEST_ALLOC(func, free_func, want_arg, want_gfp, want_node)\
475 static void test_ ## func (struct kunit *test, void *arg) \
477 volatile size_t a = TEST_SIZE; \
478 volatile size_t b = (SIZE_MAX / TEST_SIZE) + 1; \
481 /* Tiny allocation test. */ \
482 ptr = alloc ## want_arg ## want_gfp ## want_node (func, arg, 1);\
483 KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, \
484 #func " failed regular allocation?!\n"); \
485 free ## want_arg (free_func, arg, ptr); \
487 /* Wrapped allocation test. */ \
488 ptr = alloc ## want_arg ## want_gfp ## want_node (func, arg, \
490 KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, \
491 #func " unexpectedly failed bad wrapping?!\n"); \
492 free ## want_arg (free_func, arg, ptr); \
494 /* Saturated allocation test. */ \
495 ptr = alloc ## want_arg ## want_gfp ## want_node (func, arg, \
498 KUNIT_FAIL(test, #func " missed saturation!\n"); \
499 free ## want_arg (free_func, arg, ptr); \
504 * Allocator uses a trailing node argument --------+ (e.g. kmalloc_node())
505 * Allocator uses the gfp_t argument -----------+ | (e.g. kmalloc())
506 * Allocator uses a special leading argument + | | (e.g. devm_kmalloc())
509 DEFINE_TEST_ALLOC(kmalloc, kfree, 0, 1, 0);
510 DEFINE_TEST_ALLOC(kmalloc_node, kfree, 0, 1, 1);
511 DEFINE_TEST_ALLOC(kzalloc, kfree, 0, 1, 0);
512 DEFINE_TEST_ALLOC(kzalloc_node, kfree, 0, 1, 1);
513 DEFINE_TEST_ALLOC(__vmalloc, vfree, 0, 1, 0);
514 DEFINE_TEST_ALLOC(kvmalloc, kvfree, 0, 1, 0);
515 DEFINE_TEST_ALLOC(kvmalloc_node, kvfree, 0, 1, 1);
516 DEFINE_TEST_ALLOC(kvzalloc, kvfree, 0, 1, 0);
517 DEFINE_TEST_ALLOC(kvzalloc_node, kvfree, 0, 1, 1);
518 DEFINE_TEST_ALLOC(devm_kmalloc, devm_kfree, 1, 1, 0);
519 DEFINE_TEST_ALLOC(devm_kzalloc, devm_kfree, 1, 1, 0);
521 static void overflow_allocation_test(struct kunit *test)
523 const char device_name[] = "overflow-test";
527 #define check_allocation_overflow(alloc) do { \
529 test_ ## alloc(test, dev); \
532 /* Create dummy device for devm_kmalloc()-family tests. */
533 dev = root_device_register(device_name);
534 KUNIT_ASSERT_FALSE_MSG(test, IS_ERR(dev),
535 "Cannot register test device\n");
537 check_allocation_overflow(kmalloc);
538 check_allocation_overflow(kmalloc_node);
539 check_allocation_overflow(kzalloc);
540 check_allocation_overflow(kzalloc_node);
541 check_allocation_overflow(__vmalloc);
542 check_allocation_overflow(kvmalloc);
543 check_allocation_overflow(kvmalloc_node);
544 check_allocation_overflow(kvzalloc);
545 check_allocation_overflow(kvzalloc_node);
546 check_allocation_overflow(devm_kmalloc);
547 check_allocation_overflow(devm_kzalloc);
549 device_unregister(dev);
551 kunit_info(test, "%d allocation overflow tests finished\n", count);
552 #undef check_allocation_overflow
555 struct __test_flex_array {
558 unsigned long data[];
561 static void overflow_size_helpers_test(struct kunit *test)
563 /* Make sure struct_size() can be used in a constant expression. */
564 u8 ce_array[struct_size((struct __test_flex_array *)0, data, 55)];
565 struct __test_flex_array *obj;
568 volatile int unconst = 0;
570 /* Verify constant expression against runtime version. */
572 OPTIMIZER_HIDE_VAR(var);
573 KUNIT_EXPECT_EQ(test, sizeof(ce_array), struct_size(obj, data, var));
575 #define check_one_size_helper(expected, func, args...) do { \
576 size_t _r = func(args); \
577 KUNIT_EXPECT_EQ_MSG(test, _r, expected, \
578 "expected " #func "(" #args ") to return %zu but got %zu instead\n", \
579 (size_t)(expected), _r); \
584 check_one_size_helper(20, size_mul, var++, 5);
585 check_one_size_helper(20, size_mul, 4, var++);
586 check_one_size_helper(0, size_mul, 0, 3);
587 check_one_size_helper(0, size_mul, 3, 0);
588 check_one_size_helper(6, size_mul, 2, 3);
589 check_one_size_helper(SIZE_MAX, size_mul, SIZE_MAX, 1);
590 check_one_size_helper(SIZE_MAX, size_mul, SIZE_MAX, 3);
591 check_one_size_helper(SIZE_MAX, size_mul, SIZE_MAX, -3);
594 check_one_size_helper(9, size_add, var++, 5);
595 check_one_size_helper(9, size_add, 4, var++);
596 check_one_size_helper(9, size_add, 9, 0);
597 check_one_size_helper(9, size_add, 0, 9);
598 check_one_size_helper(5, size_add, 2, 3);
599 check_one_size_helper(SIZE_MAX, size_add, SIZE_MAX, 1);
600 check_one_size_helper(SIZE_MAX, size_add, SIZE_MAX, 3);
601 check_one_size_helper(SIZE_MAX, size_add, SIZE_MAX, -3);
604 check_one_size_helper(1, size_sub, var--, 3);
605 check_one_size_helper(1, size_sub, 4, var--);
606 check_one_size_helper(1, size_sub, 3, 2);
607 check_one_size_helper(9, size_sub, 9, 0);
608 check_one_size_helper(SIZE_MAX, size_sub, 9, -3);
609 check_one_size_helper(SIZE_MAX, size_sub, 0, 9);
610 check_one_size_helper(SIZE_MAX, size_sub, 2, 3);
611 check_one_size_helper(SIZE_MAX, size_sub, SIZE_MAX, 0);
612 check_one_size_helper(SIZE_MAX, size_sub, SIZE_MAX, 10);
613 check_one_size_helper(SIZE_MAX, size_sub, 0, SIZE_MAX);
614 check_one_size_helper(SIZE_MAX, size_sub, 14, SIZE_MAX);
615 check_one_size_helper(SIZE_MAX - 2, size_sub, SIZE_MAX - 1, 1);
616 check_one_size_helper(SIZE_MAX - 4, size_sub, SIZE_MAX - 1, 3);
617 check_one_size_helper(1, size_sub, SIZE_MAX - 1, -3);
620 check_one_size_helper(4 * sizeof(*obj->data),
621 flex_array_size, obj, data, var++);
622 check_one_size_helper(5 * sizeof(*obj->data),
623 flex_array_size, obj, data, var++);
624 check_one_size_helper(0, flex_array_size, obj, data, 0 + unconst);
625 check_one_size_helper(sizeof(*obj->data),
626 flex_array_size, obj, data, 1 + unconst);
627 check_one_size_helper(7 * sizeof(*obj->data),
628 flex_array_size, obj, data, 7 + unconst);
629 check_one_size_helper(SIZE_MAX,
630 flex_array_size, obj, data, -1 + unconst);
631 check_one_size_helper(SIZE_MAX,
632 flex_array_size, obj, data, SIZE_MAX - 4 + unconst);
635 check_one_size_helper(sizeof(*obj) + (4 * sizeof(*obj->data)),
636 struct_size, obj, data, var++);
637 check_one_size_helper(sizeof(*obj) + (5 * sizeof(*obj->data)),
638 struct_size, obj, data, var++);
639 check_one_size_helper(sizeof(*obj), struct_size, obj, data, 0 + unconst);
640 check_one_size_helper(sizeof(*obj) + sizeof(*obj->data),
641 struct_size, obj, data, 1 + unconst);
642 check_one_size_helper(SIZE_MAX,
643 struct_size, obj, data, -3 + unconst);
644 check_one_size_helper(SIZE_MAX,
645 struct_size, obj, data, SIZE_MAX - 3 + unconst);
647 kunit_info(test, "%d overflow size helper tests finished\n", count);
648 #undef check_one_size_helper
651 static struct kunit_case overflow_test_cases[] = {
652 KUNIT_CASE(u8_overflow_test),
653 KUNIT_CASE(s8_overflow_test),
654 KUNIT_CASE(u16_overflow_test),
655 KUNIT_CASE(s16_overflow_test),
656 KUNIT_CASE(u32_overflow_test),
657 KUNIT_CASE(s32_overflow_test),
658 /* Clang 13 and earlier generate unwanted libcalls on 32-bit. */
659 #if BITS_PER_LONG == 64
660 KUNIT_CASE(u64_overflow_test),
661 KUNIT_CASE(s64_overflow_test),
663 KUNIT_CASE(overflow_shift_test),
664 KUNIT_CASE(overflow_allocation_test),
665 KUNIT_CASE(overflow_size_helpers_test),
669 static struct kunit_suite overflow_test_suite = {
671 .test_cases = overflow_test_cases,
674 kunit_test_suite(overflow_test_suite);
676 MODULE_LICENSE("Dual MIT/GPL");