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 "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 "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, "Unexpected " #op " macro side-effect!\n"); \ 269 KUNIT_EXPECT_EQ_MSG(test, _b_orig, _b_bump, "Unexpected " #op " macro side-effect!\n"); \ 270 } while (0) 271 272 #define DEFINE_TEST_FUNC_TYPED(n, t, fmt) \ 273 static void do_test_ ## n(struct kunit *test, const struct test_ ## n *p) \ 274 { \ 275 check_one_op(t, fmt, add, "+", p->a, p->b, p->sum, p->s_of); \ 276 check_one_op(t, fmt, add, "+", p->b, p->a, p->sum, p->s_of); \ 277 check_one_op(t, fmt, sub, "-", p->a, p->b, p->diff, p->d_of); \ 278 check_one_op(t, fmt, mul, "*", p->a, p->b, p->prod, p->p_of); \ 279 check_one_op(t, fmt, mul, "*", p->b, p->a, p->prod, p->p_of); \ 280 } \ 281 \ 282 static void n ## _overflow_test(struct kunit *test) { \ 283 unsigned i; \ 284 \ 285 SKIP_64_ON_32(__same_type(t, u64)); \ 286 SKIP_64_ON_32(__same_type(t, s64)); \ 287 SKIP_SIGN_MISMATCH(__same_type(n ## _tests[0].a, u32) && \ 288 __same_type(n ## _tests[0].b, u32) && \ 289 __same_type(n ## _tests[0].sum, int)); \ 290 \ 291 for (i = 0; i < ARRAY_SIZE(n ## _tests); ++i) \ 292 do_test_ ## n(test, &n ## _tests[i]); \ 293 kunit_info(test, "%zu %s arithmetic tests finished\n", \ 294 ARRAY_SIZE(n ## _tests), #n); \ 295 } 296 297 #define DEFINE_TEST_FUNC(t, fmt) \ 298 DEFINE_TEST_FUNC_TYPED(t ## _ ## t ## __ ## t, t, fmt) 299 300 DEFINE_TEST_FUNC(u8, "%d"); 301 DEFINE_TEST_FUNC(s8, "%d"); 302 DEFINE_TEST_FUNC(u16, "%d"); 303 DEFINE_TEST_FUNC(s16, "%d"); 304 DEFINE_TEST_FUNC(u32, "%u"); 305 DEFINE_TEST_FUNC(s32, "%d"); 306 DEFINE_TEST_FUNC(u64, "%llu"); 307 DEFINE_TEST_FUNC(s64, "%lld"); 308 309 DEFINE_TEST_ARRAY_TYPED(u32, u32, u8) = { 310 {0, 0, 0, 0, 0, false, false, false}, 311 {U8_MAX, 2, 1, U8_MAX - 2, U8_MAX - 1, true, false, true}, 312 {U8_MAX + 1, 0, 0, 0, 0, true, true, false}, 313 }; 314 DEFINE_TEST_FUNC_TYPED(u32_u32__u8, u8, "%d"); 315 316 DEFINE_TEST_ARRAY_TYPED(u32, u32, int) = { 317 {0, 0, 0, 0, 0, false, false, false}, 318 {U32_MAX, 0, -1, -1, 0, true, true, false}, 319 }; 320 DEFINE_TEST_FUNC_TYPED(u32_u32__int, int, "%d"); 321 322 DEFINE_TEST_ARRAY_TYPED(u8, u8, int) = { 323 {0, 0, 0, 0, 0, false, false, false}, 324 {U8_MAX, U8_MAX, 2 * U8_MAX, 0, U8_MAX * U8_MAX, false, false, false}, 325 {1, 2, 3, -1, 2, false, false, false}, 326 }; 327 DEFINE_TEST_FUNC_TYPED(u8_u8__int, int, "%d"); 328 329 DEFINE_TEST_ARRAY_TYPED(int, int, u8) = { 330 {0, 0, 0, 0, 0, false, false, false}, 331 {1, 2, 3, U8_MAX, 2, false, true, false}, 332 {-1, 0, U8_MAX, U8_MAX, 0, true, true, false}, 333 }; 334 DEFINE_TEST_FUNC_TYPED(int_int__u8, u8, "%d"); 335 336 /* Args are: value, shift, type, expected result, overflow expected */ 337 #define TEST_ONE_SHIFT(a, s, t, expect, of) do { \ 338 typeof(a) __a = (a); \ 339 typeof(s) __s = (s); \ 340 t __e = (expect); \ 341 t __d; \ 342 bool __of = check_shl_overflow(__a, __s, &__d); \ 343 if (__of != of) { \ 344 KUNIT_EXPECT_EQ_MSG(test, __of, of, \ 345 "expected (%s)(%s << %s) to%s overflow\n", \ 346 #t, #a, #s, of ? "" : " not"); \ 347 } else if (!__of && __d != __e) { \ 348 KUNIT_EXPECT_EQ_MSG(test, __d, __e, \ 349 "expected (%s)(%s << %s) == %s\n", \ 350 #t, #a, #s, #expect); \ 351 if ((t)-1 < 0) \ 352 kunit_info(test, "got %lld\n", (s64)__d); \ 353 else \ 354 kunit_info(test, "got %llu\n", (u64)__d); \ 355 } \ 356 count++; \ 357 } while (0) 358 359 static void shift_sane_test(struct kunit *test) 360 { 361 int count = 0; 362 363 /* Sane shifts. */ 364 TEST_ONE_SHIFT(1, 0, u8, 1 << 0, false); 365 TEST_ONE_SHIFT(1, 4, u8, 1 << 4, false); 366 TEST_ONE_SHIFT(1, 7, u8, 1 << 7, false); 367 TEST_ONE_SHIFT(0xF, 4, u8, 0xF << 4, false); 368 TEST_ONE_SHIFT(1, 0, u16, 1 << 0, false); 369 TEST_ONE_SHIFT(1, 10, u16, 1 << 10, false); 370 TEST_ONE_SHIFT(1, 15, u16, 1 << 15, false); 371 TEST_ONE_SHIFT(0xFF, 8, u16, 0xFF << 8, false); 372 TEST_ONE_SHIFT(1, 0, int, 1 << 0, false); 373 TEST_ONE_SHIFT(1, 16, int, 1 << 16, false); 374 TEST_ONE_SHIFT(1, 30, int, 1 << 30, false); 375 TEST_ONE_SHIFT(1, 0, s32, 1 << 0, false); 376 TEST_ONE_SHIFT(1, 16, s32, 1 << 16, false); 377 TEST_ONE_SHIFT(1, 30, s32, 1 << 30, false); 378 TEST_ONE_SHIFT(1, 0, unsigned int, 1U << 0, false); 379 TEST_ONE_SHIFT(1, 20, unsigned int, 1U << 20, false); 380 TEST_ONE_SHIFT(1, 31, unsigned int, 1U << 31, false); 381 TEST_ONE_SHIFT(0xFFFFU, 16, unsigned int, 0xFFFFU << 16, false); 382 TEST_ONE_SHIFT(1, 0, u32, 1U << 0, false); 383 TEST_ONE_SHIFT(1, 20, u32, 1U << 20, false); 384 TEST_ONE_SHIFT(1, 31, u32, 1U << 31, false); 385 TEST_ONE_SHIFT(0xFFFFU, 16, u32, 0xFFFFU << 16, false); 386 TEST_ONE_SHIFT(1, 0, u64, 1ULL << 0, false); 387 TEST_ONE_SHIFT(1, 40, u64, 1ULL << 40, false); 388 TEST_ONE_SHIFT(1, 63, u64, 1ULL << 63, false); 389 TEST_ONE_SHIFT(0xFFFFFFFFULL, 32, u64, 0xFFFFFFFFULL << 32, false); 390 391 /* Sane shift: start and end with 0, without a too-wide shift. */ 392 TEST_ONE_SHIFT(0, 7, u8, 0, false); 393 TEST_ONE_SHIFT(0, 15, u16, 0, false); 394 TEST_ONE_SHIFT(0, 31, unsigned int, 0, false); 395 TEST_ONE_SHIFT(0, 31, u32, 0, false); 396 TEST_ONE_SHIFT(0, 63, u64, 0, false); 397 398 /* Sane shift: start and end with 0, without reaching signed bit. */ 399 TEST_ONE_SHIFT(0, 6, s8, 0, false); 400 TEST_ONE_SHIFT(0, 14, s16, 0, false); 401 TEST_ONE_SHIFT(0, 30, int, 0, false); 402 TEST_ONE_SHIFT(0, 30, s32, 0, false); 403 TEST_ONE_SHIFT(0, 62, s64, 0, false); 404 405 kunit_info(test, "%d sane shift tests finished\n", count); 406 } 407 408 static void shift_overflow_test(struct kunit *test) 409 { 410 int count = 0; 411 412 /* Overflow: shifted the bit off the end. */ 413 TEST_ONE_SHIFT(1, 8, u8, 0, true); 414 TEST_ONE_SHIFT(1, 16, u16, 0, true); 415 TEST_ONE_SHIFT(1, 32, unsigned int, 0, true); 416 TEST_ONE_SHIFT(1, 32, u32, 0, true); 417 TEST_ONE_SHIFT(1, 64, u64, 0, true); 418 419 /* Overflow: shifted into the signed bit. */ 420 TEST_ONE_SHIFT(1, 7, s8, 0, true); 421 TEST_ONE_SHIFT(1, 15, s16, 0, true); 422 TEST_ONE_SHIFT(1, 31, int, 0, true); 423 TEST_ONE_SHIFT(1, 31, s32, 0, true); 424 TEST_ONE_SHIFT(1, 63, s64, 0, true); 425 426 /* Overflow: high bit falls off unsigned types. */ 427 /* 10010110 */ 428 TEST_ONE_SHIFT(150, 1, u8, 0, true); 429 /* 1000100010010110 */ 430 TEST_ONE_SHIFT(34966, 1, u16, 0, true); 431 /* 10000100000010001000100010010110 */ 432 TEST_ONE_SHIFT(2215151766U, 1, u32, 0, true); 433 TEST_ONE_SHIFT(2215151766U, 1, unsigned int, 0, true); 434 /* 1000001000010000010000000100000010000100000010001000100010010110 */ 435 TEST_ONE_SHIFT(9372061470395238550ULL, 1, u64, 0, true); 436 437 /* Overflow: bit shifted into signed bit on signed types. */ 438 /* 01001011 */ 439 TEST_ONE_SHIFT(75, 1, s8, 0, true); 440 /* 0100010001001011 */ 441 TEST_ONE_SHIFT(17483, 1, s16, 0, true); 442 /* 01000010000001000100010001001011 */ 443 TEST_ONE_SHIFT(1107575883, 1, s32, 0, true); 444 TEST_ONE_SHIFT(1107575883, 1, int, 0, true); 445 /* 0100000100001000001000000010000001000010000001000100010001001011 */ 446 TEST_ONE_SHIFT(4686030735197619275LL, 1, s64, 0, true); 447 448 /* Overflow: bit shifted past signed bit on signed types. */ 449 /* 01001011 */ 450 TEST_ONE_SHIFT(75, 2, s8, 0, true); 451 /* 0100010001001011 */ 452 TEST_ONE_SHIFT(17483, 2, s16, 0, true); 453 /* 01000010000001000100010001001011 */ 454 TEST_ONE_SHIFT(1107575883, 2, s32, 0, true); 455 TEST_ONE_SHIFT(1107575883, 2, int, 0, true); 456 /* 0100000100001000001000000010000001000010000001000100010001001011 */ 457 TEST_ONE_SHIFT(4686030735197619275LL, 2, s64, 0, true); 458 459 kunit_info(test, "%d overflow shift tests finished\n", count); 460 } 461 462 static void shift_truncate_test(struct kunit *test) 463 { 464 int count = 0; 465 466 /* Overflow: values larger than destination type. */ 467 TEST_ONE_SHIFT(0x100, 0, u8, 0, true); 468 TEST_ONE_SHIFT(0xFF, 0, s8, 0, true); 469 TEST_ONE_SHIFT(0x10000U, 0, u16, 0, true); 470 TEST_ONE_SHIFT(0xFFFFU, 0, s16, 0, true); 471 TEST_ONE_SHIFT(0x100000000ULL, 0, u32, 0, true); 472 TEST_ONE_SHIFT(0x100000000ULL, 0, unsigned int, 0, true); 473 TEST_ONE_SHIFT(0xFFFFFFFFUL, 0, s32, 0, true); 474 TEST_ONE_SHIFT(0xFFFFFFFFUL, 0, int, 0, true); 475 TEST_ONE_SHIFT(0xFFFFFFFFFFFFFFFFULL, 0, s64, 0, true); 476 477 /* Overflow: shifted at or beyond entire type's bit width. */ 478 TEST_ONE_SHIFT(0, 8, u8, 0, true); 479 TEST_ONE_SHIFT(0, 9, u8, 0, true); 480 TEST_ONE_SHIFT(0, 8, s8, 0, true); 481 TEST_ONE_SHIFT(0, 9, s8, 0, true); 482 TEST_ONE_SHIFT(0, 16, u16, 0, true); 483 TEST_ONE_SHIFT(0, 17, u16, 0, true); 484 TEST_ONE_SHIFT(0, 16, s16, 0, true); 485 TEST_ONE_SHIFT(0, 17, s16, 0, true); 486 TEST_ONE_SHIFT(0, 32, u32, 0, true); 487 TEST_ONE_SHIFT(0, 33, u32, 0, true); 488 TEST_ONE_SHIFT(0, 32, int, 0, true); 489 TEST_ONE_SHIFT(0, 33, int, 0, true); 490 TEST_ONE_SHIFT(0, 32, s32, 0, true); 491 TEST_ONE_SHIFT(0, 33, s32, 0, true); 492 TEST_ONE_SHIFT(0, 64, u64, 0, true); 493 TEST_ONE_SHIFT(0, 65, u64, 0, true); 494 TEST_ONE_SHIFT(0, 64, s64, 0, true); 495 TEST_ONE_SHIFT(0, 65, s64, 0, true); 496 497 kunit_info(test, "%d truncate shift tests finished\n", count); 498 } 499 500 static void shift_nonsense_test(struct kunit *test) 501 { 502 int count = 0; 503 504 /* Nonsense: negative initial value. */ 505 TEST_ONE_SHIFT(-1, 0, s8, 0, true); 506 TEST_ONE_SHIFT(-1, 0, u8, 0, true); 507 TEST_ONE_SHIFT(-5, 0, s16, 0, true); 508 TEST_ONE_SHIFT(-5, 0, u16, 0, true); 509 TEST_ONE_SHIFT(-10, 0, int, 0, true); 510 TEST_ONE_SHIFT(-10, 0, unsigned int, 0, true); 511 TEST_ONE_SHIFT(-100, 0, s32, 0, true); 512 TEST_ONE_SHIFT(-100, 0, u32, 0, true); 513 TEST_ONE_SHIFT(-10000, 0, s64, 0, true); 514 TEST_ONE_SHIFT(-10000, 0, u64, 0, true); 515 516 /* Nonsense: negative shift values. */ 517 TEST_ONE_SHIFT(0, -5, s8, 0, true); 518 TEST_ONE_SHIFT(0, -5, u8, 0, true); 519 TEST_ONE_SHIFT(0, -10, s16, 0, true); 520 TEST_ONE_SHIFT(0, -10, u16, 0, true); 521 TEST_ONE_SHIFT(0, -15, int, 0, true); 522 TEST_ONE_SHIFT(0, -15, unsigned int, 0, true); 523 TEST_ONE_SHIFT(0, -20, s32, 0, true); 524 TEST_ONE_SHIFT(0, -20, u32, 0, true); 525 TEST_ONE_SHIFT(0, -30, s64, 0, true); 526 TEST_ONE_SHIFT(0, -30, u64, 0, true); 527 528 /* 529 * Corner case: for unsigned types, we fail when we've shifted 530 * through the entire width of bits. For signed types, we might 531 * want to match this behavior, but that would mean noticing if 532 * we shift through all but the signed bit, and this is not 533 * currently detected (but we'll notice an overflow into the 534 * signed bit). So, for now, we will test this condition but 535 * mark it as not expected to overflow. 536 */ 537 TEST_ONE_SHIFT(0, 7, s8, 0, false); 538 TEST_ONE_SHIFT(0, 15, s16, 0, false); 539 TEST_ONE_SHIFT(0, 31, int, 0, false); 540 TEST_ONE_SHIFT(0, 31, s32, 0, false); 541 TEST_ONE_SHIFT(0, 63, s64, 0, false); 542 543 kunit_info(test, "%d nonsense shift tests finished\n", count); 544 } 545 #undef TEST_ONE_SHIFT 546 547 /* 548 * Deal with the various forms of allocator arguments. See comments above 549 * the DEFINE_TEST_ALLOC() instances for mapping of the "bits". 550 */ 551 #define alloc_GFP (GFP_KERNEL | __GFP_NOWARN) 552 #define alloc010(alloc, arg, sz) alloc(sz, alloc_GFP) 553 #define alloc011(alloc, arg, sz) alloc(sz, alloc_GFP, NUMA_NO_NODE) 554 #define alloc000(alloc, arg, sz) alloc(sz) 555 #define alloc001(alloc, arg, sz) alloc(sz, NUMA_NO_NODE) 556 #define alloc110(alloc, arg, sz) alloc(arg, sz, alloc_GFP) 557 #define free0(free, arg, ptr) free(ptr) 558 #define free1(free, arg, ptr) free(arg, ptr) 559 560 /* Wrap around to 16K */ 561 #define TEST_SIZE (5 * 4096) 562 563 #define DEFINE_TEST_ALLOC(func, free_func, want_arg, want_gfp, want_node)\ 564 static void test_ ## func (struct kunit *test, void *arg) \ 565 { \ 566 volatile size_t a = TEST_SIZE; \ 567 volatile size_t b = (SIZE_MAX / TEST_SIZE) + 1; \ 568 void *ptr; \ 569 \ 570 /* Tiny allocation test. */ \ 571 ptr = alloc ## want_arg ## want_gfp ## want_node (func, arg, 1);\ 572 KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, \ 573 #func " failed regular allocation?!\n"); \ 574 free ## want_arg (free_func, arg, ptr); \ 575 \ 576 /* Wrapped allocation test. */ \ 577 ptr = alloc ## want_arg ## want_gfp ## want_node (func, arg, \ 578 a * b); \ 579 KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, \ 580 #func " unexpectedly failed bad wrapping?!\n"); \ 581 free ## want_arg (free_func, arg, ptr); \ 582 \ 583 /* Saturated allocation test. */ \ 584 ptr = alloc ## want_arg ## want_gfp ## want_node (func, arg, \ 585 array_size(a, b)); \ 586 if (ptr) { \ 587 KUNIT_FAIL(test, #func " missed saturation!\n"); \ 588 free ## want_arg (free_func, arg, ptr); \ 589 } \ 590 } 591 592 /* 593 * Allocator uses a trailing node argument --------+ (e.g. kmalloc_node()) 594 * Allocator uses the gfp_t argument -----------+ | (e.g. kmalloc()) 595 * Allocator uses a special leading argument + | | (e.g. devm_kmalloc()) 596 * | | | 597 */ 598 DEFINE_TEST_ALLOC(kmalloc, kfree, 0, 1, 0); 599 DEFINE_TEST_ALLOC(kmalloc_node, kfree, 0, 1, 1); 600 DEFINE_TEST_ALLOC(kzalloc, kfree, 0, 1, 0); 601 DEFINE_TEST_ALLOC(kzalloc_node, kfree, 0, 1, 1); 602 DEFINE_TEST_ALLOC(__vmalloc, vfree, 0, 1, 0); 603 DEFINE_TEST_ALLOC(kvmalloc, kvfree, 0, 1, 0); 604 DEFINE_TEST_ALLOC(kvmalloc_node, kvfree, 0, 1, 1); 605 DEFINE_TEST_ALLOC(kvzalloc, kvfree, 0, 1, 0); 606 DEFINE_TEST_ALLOC(kvzalloc_node, kvfree, 0, 1, 1); 607 DEFINE_TEST_ALLOC(devm_kmalloc, devm_kfree, 1, 1, 0); 608 DEFINE_TEST_ALLOC(devm_kzalloc, devm_kfree, 1, 1, 0); 609 610 static void overflow_allocation_test(struct kunit *test) 611 { 612 const char device_name[] = "overflow-test"; 613 struct device *dev; 614 int count = 0; 615 616 #define check_allocation_overflow(alloc) do { \ 617 count++; \ 618 test_ ## alloc(test, dev); \ 619 } while (0) 620 621 /* Create dummy device for devm_kmalloc()-family tests. */ 622 dev = kunit_device_register(test, device_name); 623 KUNIT_ASSERT_FALSE_MSG(test, IS_ERR(dev), 624 "Cannot register test device\n"); 625 626 check_allocation_overflow(kmalloc); 627 check_allocation_overflow(kmalloc_node); 628 check_allocation_overflow(kzalloc); 629 check_allocation_overflow(kzalloc_node); 630 check_allocation_overflow(__vmalloc); 631 check_allocation_overflow(kvmalloc); 632 check_allocation_overflow(kvmalloc_node); 633 check_allocation_overflow(kvzalloc); 634 check_allocation_overflow(kvzalloc_node); 635 check_allocation_overflow(devm_kmalloc); 636 check_allocation_overflow(devm_kzalloc); 637 638 kunit_info(test, "%d allocation overflow tests finished\n", count); 639 #undef check_allocation_overflow 640 } 641 642 struct __test_flex_array { 643 unsigned long flags; 644 size_t count; 645 unsigned long data[]; 646 }; 647 648 static void overflow_size_helpers_test(struct kunit *test) 649 { 650 /* Make sure struct_size() can be used in a constant expression. */ 651 u8 ce_array[struct_size_t(struct __test_flex_array, data, 55)]; 652 struct __test_flex_array *obj; 653 int count = 0; 654 int var; 655 volatile int unconst = 0; 656 657 /* Verify constant expression against runtime version. */ 658 var = 55; 659 OPTIMIZER_HIDE_VAR(var); 660 KUNIT_EXPECT_EQ(test, sizeof(ce_array), struct_size(obj, data, var)); 661 662 #define check_one_size_helper(expected, func, args...) do { \ 663 size_t _r = func(args); \ 664 KUNIT_EXPECT_EQ_MSG(test, _r, expected, \ 665 "expected " #func "(" #args ") to return %zu but got %zu instead\n", \ 666 (size_t)(expected), _r); \ 667 count++; \ 668 } while (0) 669 670 var = 4; 671 check_one_size_helper(20, size_mul, var++, 5); 672 check_one_size_helper(20, size_mul, 4, var++); 673 check_one_size_helper(0, size_mul, 0, 3); 674 check_one_size_helper(0, size_mul, 3, 0); 675 check_one_size_helper(6, size_mul, 2, 3); 676 check_one_size_helper(SIZE_MAX, size_mul, SIZE_MAX, 1); 677 check_one_size_helper(SIZE_MAX, size_mul, SIZE_MAX, 3); 678 check_one_size_helper(SIZE_MAX, size_mul, SIZE_MAX, -3); 679 680 var = 4; 681 check_one_size_helper(9, size_add, var++, 5); 682 check_one_size_helper(9, size_add, 4, var++); 683 check_one_size_helper(9, size_add, 9, 0); 684 check_one_size_helper(9, size_add, 0, 9); 685 check_one_size_helper(5, size_add, 2, 3); 686 check_one_size_helper(SIZE_MAX, size_add, SIZE_MAX, 1); 687 check_one_size_helper(SIZE_MAX, size_add, SIZE_MAX, 3); 688 check_one_size_helper(SIZE_MAX, size_add, SIZE_MAX, -3); 689 690 var = 4; 691 check_one_size_helper(1, size_sub, var--, 3); 692 check_one_size_helper(1, size_sub, 4, var--); 693 check_one_size_helper(1, size_sub, 3, 2); 694 check_one_size_helper(9, size_sub, 9, 0); 695 check_one_size_helper(SIZE_MAX, size_sub, 9, -3); 696 check_one_size_helper(SIZE_MAX, size_sub, 0, 9); 697 check_one_size_helper(SIZE_MAX, size_sub, 2, 3); 698 check_one_size_helper(SIZE_MAX, size_sub, SIZE_MAX, 0); 699 check_one_size_helper(SIZE_MAX, size_sub, SIZE_MAX, 10); 700 check_one_size_helper(SIZE_MAX, size_sub, 0, SIZE_MAX); 701 check_one_size_helper(SIZE_MAX, size_sub, 14, SIZE_MAX); 702 check_one_size_helper(SIZE_MAX - 2, size_sub, SIZE_MAX - 1, 1); 703 check_one_size_helper(SIZE_MAX - 4, size_sub, SIZE_MAX - 1, 3); 704 check_one_size_helper(1, size_sub, SIZE_MAX - 1, -3); 705 706 var = 4; 707 check_one_size_helper(4 * sizeof(*obj->data), 708 flex_array_size, obj, data, var++); 709 check_one_size_helper(5 * sizeof(*obj->data), 710 flex_array_size, obj, data, var++); 711 check_one_size_helper(0, flex_array_size, obj, data, 0 + unconst); 712 check_one_size_helper(sizeof(*obj->data), 713 flex_array_size, obj, data, 1 + unconst); 714 check_one_size_helper(7 * sizeof(*obj->data), 715 flex_array_size, obj, data, 7 + unconst); 716 check_one_size_helper(SIZE_MAX, 717 flex_array_size, obj, data, -1 + unconst); 718 check_one_size_helper(SIZE_MAX, 719 flex_array_size, obj, data, SIZE_MAX - 4 + unconst); 720 721 var = 4; 722 check_one_size_helper(sizeof(*obj) + (4 * sizeof(*obj->data)), 723 struct_size, obj, data, var++); 724 check_one_size_helper(sizeof(*obj) + (5 * sizeof(*obj->data)), 725 struct_size, obj, data, var++); 726 check_one_size_helper(sizeof(*obj), struct_size, obj, data, 0 + unconst); 727 check_one_size_helper(sizeof(*obj) + sizeof(*obj->data), 728 struct_size, obj, data, 1 + unconst); 729 check_one_size_helper(SIZE_MAX, 730 struct_size, obj, data, -3 + unconst); 731 check_one_size_helper(SIZE_MAX, 732 struct_size, obj, data, SIZE_MAX - 3 + unconst); 733 734 kunit_info(test, "%d overflow size helper tests finished\n", count); 735 #undef check_one_size_helper 736 } 737 738 static void overflows_type_test(struct kunit *test) 739 { 740 int count = 0; 741 unsigned int var; 742 743 #define __TEST_OVERFLOWS_TYPE(func, arg1, arg2, of) do { \ 744 bool __of = func(arg1, arg2); \ 745 KUNIT_EXPECT_EQ_MSG(test, __of, of, \ 746 "expected " #func "(" #arg1 ", " #arg2 " to%s overflow\n",\ 747 of ? "" : " not"); \ 748 count++; \ 749 } while (0) 750 751 /* Args are: first type, second type, value, overflow expected */ 752 #define TEST_OVERFLOWS_TYPE(__t1, __t2, v, of) do { \ 753 __t1 t1 = (v); \ 754 __t2 t2; \ 755 __TEST_OVERFLOWS_TYPE(__overflows_type, t1, t2, of); \ 756 __TEST_OVERFLOWS_TYPE(__overflows_type, t1, __t2, of); \ 757 __TEST_OVERFLOWS_TYPE(__overflows_type_constexpr, t1, t2, of); \ 758 __TEST_OVERFLOWS_TYPE(__overflows_type_constexpr, t1, __t2, of);\ 759 } while (0) 760 761 TEST_OVERFLOWS_TYPE(u8, u8, U8_MAX, false); 762 TEST_OVERFLOWS_TYPE(u8, u16, U8_MAX, false); 763 TEST_OVERFLOWS_TYPE(u8, s8, U8_MAX, true); 764 TEST_OVERFLOWS_TYPE(u8, s8, S8_MAX, false); 765 TEST_OVERFLOWS_TYPE(u8, s8, (u8)S8_MAX + 1, true); 766 TEST_OVERFLOWS_TYPE(u8, s16, U8_MAX, false); 767 TEST_OVERFLOWS_TYPE(s8, u8, S8_MAX, false); 768 TEST_OVERFLOWS_TYPE(s8, u8, -1, true); 769 TEST_OVERFLOWS_TYPE(s8, u8, S8_MIN, true); 770 TEST_OVERFLOWS_TYPE(s8, u16, S8_MAX, false); 771 TEST_OVERFLOWS_TYPE(s8, u16, -1, true); 772 TEST_OVERFLOWS_TYPE(s8, u16, S8_MIN, true); 773 TEST_OVERFLOWS_TYPE(s8, u32, S8_MAX, false); 774 TEST_OVERFLOWS_TYPE(s8, u32, -1, true); 775 TEST_OVERFLOWS_TYPE(s8, u32, S8_MIN, true); 776 #if BITS_PER_LONG == 64 777 TEST_OVERFLOWS_TYPE(s8, u64, S8_MAX, false); 778 TEST_OVERFLOWS_TYPE(s8, u64, -1, true); 779 TEST_OVERFLOWS_TYPE(s8, u64, S8_MIN, true); 780 #endif 781 TEST_OVERFLOWS_TYPE(s8, s8, S8_MAX, false); 782 TEST_OVERFLOWS_TYPE(s8, s8, S8_MIN, false); 783 TEST_OVERFLOWS_TYPE(s8, s16, S8_MAX, false); 784 TEST_OVERFLOWS_TYPE(s8, s16, S8_MIN, false); 785 TEST_OVERFLOWS_TYPE(u16, u8, U8_MAX, false); 786 TEST_OVERFLOWS_TYPE(u16, u8, (u16)U8_MAX + 1, true); 787 TEST_OVERFLOWS_TYPE(u16, u8, U16_MAX, true); 788 TEST_OVERFLOWS_TYPE(u16, s8, S8_MAX, false); 789 TEST_OVERFLOWS_TYPE(u16, s8, (u16)S8_MAX + 1, true); 790 TEST_OVERFLOWS_TYPE(u16, s8, U16_MAX, true); 791 TEST_OVERFLOWS_TYPE(u16, s16, S16_MAX, false); 792 TEST_OVERFLOWS_TYPE(u16, s16, (u16)S16_MAX + 1, true); 793 TEST_OVERFLOWS_TYPE(u16, s16, U16_MAX, true); 794 TEST_OVERFLOWS_TYPE(u16, u32, U16_MAX, false); 795 TEST_OVERFLOWS_TYPE(u16, s32, U16_MAX, false); 796 TEST_OVERFLOWS_TYPE(s16, u8, U8_MAX, false); 797 TEST_OVERFLOWS_TYPE(s16, u8, (s16)U8_MAX + 1, true); 798 TEST_OVERFLOWS_TYPE(s16, u8, -1, true); 799 TEST_OVERFLOWS_TYPE(s16, u8, S16_MIN, true); 800 TEST_OVERFLOWS_TYPE(s16, u16, S16_MAX, false); 801 TEST_OVERFLOWS_TYPE(s16, u16, -1, true); 802 TEST_OVERFLOWS_TYPE(s16, u16, S16_MIN, true); 803 TEST_OVERFLOWS_TYPE(s16, u32, S16_MAX, false); 804 TEST_OVERFLOWS_TYPE(s16, u32, -1, true); 805 TEST_OVERFLOWS_TYPE(s16, u32, S16_MIN, true); 806 #if BITS_PER_LONG == 64 807 TEST_OVERFLOWS_TYPE(s16, u64, S16_MAX, false); 808 TEST_OVERFLOWS_TYPE(s16, u64, -1, true); 809 TEST_OVERFLOWS_TYPE(s16, u64, S16_MIN, true); 810 #endif 811 TEST_OVERFLOWS_TYPE(s16, s8, S8_MAX, false); 812 TEST_OVERFLOWS_TYPE(s16, s8, S8_MIN, false); 813 TEST_OVERFLOWS_TYPE(s16, s8, (s16)S8_MAX + 1, true); 814 TEST_OVERFLOWS_TYPE(s16, s8, (s16)S8_MIN - 1, true); 815 TEST_OVERFLOWS_TYPE(s16, s8, S16_MAX, true); 816 TEST_OVERFLOWS_TYPE(s16, s8, S16_MIN, true); 817 TEST_OVERFLOWS_TYPE(s16, s16, S16_MAX, false); 818 TEST_OVERFLOWS_TYPE(s16, s16, S16_MIN, false); 819 TEST_OVERFLOWS_TYPE(s16, s32, S16_MAX, false); 820 TEST_OVERFLOWS_TYPE(s16, s32, S16_MIN, false); 821 TEST_OVERFLOWS_TYPE(u32, u8, U8_MAX, false); 822 TEST_OVERFLOWS_TYPE(u32, u8, (u32)U8_MAX + 1, true); 823 TEST_OVERFLOWS_TYPE(u32, u8, U32_MAX, true); 824 TEST_OVERFLOWS_TYPE(u32, s8, S8_MAX, false); 825 TEST_OVERFLOWS_TYPE(u32, s8, (u32)S8_MAX + 1, true); 826 TEST_OVERFLOWS_TYPE(u32, s8, U32_MAX, true); 827 TEST_OVERFLOWS_TYPE(u32, u16, U16_MAX, false); 828 TEST_OVERFLOWS_TYPE(u32, u16, U16_MAX + 1, true); 829 TEST_OVERFLOWS_TYPE(u32, u16, U32_MAX, true); 830 TEST_OVERFLOWS_TYPE(u32, s16, S16_MAX, false); 831 TEST_OVERFLOWS_TYPE(u32, s16, (u32)S16_MAX + 1, true); 832 TEST_OVERFLOWS_TYPE(u32, s16, U32_MAX, true); 833 TEST_OVERFLOWS_TYPE(u32, u32, U32_MAX, false); 834 TEST_OVERFLOWS_TYPE(u32, s32, S32_MAX, false); 835 TEST_OVERFLOWS_TYPE(u32, s32, U32_MAX, true); 836 TEST_OVERFLOWS_TYPE(u32, s32, (u32)S32_MAX + 1, true); 837 #if BITS_PER_LONG == 64 838 TEST_OVERFLOWS_TYPE(u32, u64, U32_MAX, false); 839 TEST_OVERFLOWS_TYPE(u32, s64, U32_MAX, false); 840 #endif 841 TEST_OVERFLOWS_TYPE(s32, u8, U8_MAX, false); 842 TEST_OVERFLOWS_TYPE(s32, u8, (s32)U8_MAX + 1, true); 843 TEST_OVERFLOWS_TYPE(s32, u16, S32_MAX, true); 844 TEST_OVERFLOWS_TYPE(s32, u8, -1, true); 845 TEST_OVERFLOWS_TYPE(s32, u8, S32_MIN, true); 846 TEST_OVERFLOWS_TYPE(s32, u16, U16_MAX, false); 847 TEST_OVERFLOWS_TYPE(s32, u16, (s32)U16_MAX + 1, true); 848 TEST_OVERFLOWS_TYPE(s32, u16, S32_MAX, true); 849 TEST_OVERFLOWS_TYPE(s32, u16, -1, true); 850 TEST_OVERFLOWS_TYPE(s32, u16, S32_MIN, true); 851 TEST_OVERFLOWS_TYPE(s32, u32, S32_MAX, false); 852 TEST_OVERFLOWS_TYPE(s32, u32, -1, true); 853 TEST_OVERFLOWS_TYPE(s32, u32, S32_MIN, true); 854 #if BITS_PER_LONG == 64 855 TEST_OVERFLOWS_TYPE(s32, u64, S32_MAX, false); 856 TEST_OVERFLOWS_TYPE(s32, u64, -1, true); 857 TEST_OVERFLOWS_TYPE(s32, u64, S32_MIN, true); 858 #endif 859 TEST_OVERFLOWS_TYPE(s32, s8, S8_MAX, false); 860 TEST_OVERFLOWS_TYPE(s32, s8, S8_MIN, false); 861 TEST_OVERFLOWS_TYPE(s32, s8, (s32)S8_MAX + 1, true); 862 TEST_OVERFLOWS_TYPE(s32, s8, (s32)S8_MIN - 1, true); 863 TEST_OVERFLOWS_TYPE(s32, s8, S32_MAX, true); 864 TEST_OVERFLOWS_TYPE(s32, s8, S32_MIN, true); 865 TEST_OVERFLOWS_TYPE(s32, s16, S16_MAX, false); 866 TEST_OVERFLOWS_TYPE(s32, s16, S16_MIN, false); 867 TEST_OVERFLOWS_TYPE(s32, s16, (s32)S16_MAX + 1, true); 868 TEST_OVERFLOWS_TYPE(s32, s16, (s32)S16_MIN - 1, true); 869 TEST_OVERFLOWS_TYPE(s32, s16, S32_MAX, true); 870 TEST_OVERFLOWS_TYPE(s32, s16, S32_MIN, true); 871 TEST_OVERFLOWS_TYPE(s32, s32, S32_MAX, false); 872 TEST_OVERFLOWS_TYPE(s32, s32, S32_MIN, false); 873 #if BITS_PER_LONG == 64 874 TEST_OVERFLOWS_TYPE(s32, s64, S32_MAX, false); 875 TEST_OVERFLOWS_TYPE(s32, s64, S32_MIN, false); 876 TEST_OVERFLOWS_TYPE(u64, u8, U64_MAX, true); 877 TEST_OVERFLOWS_TYPE(u64, u8, U8_MAX, false); 878 TEST_OVERFLOWS_TYPE(u64, u8, (u64)U8_MAX + 1, true); 879 TEST_OVERFLOWS_TYPE(u64, u16, U64_MAX, true); 880 TEST_OVERFLOWS_TYPE(u64, u16, U16_MAX, false); 881 TEST_OVERFLOWS_TYPE(u64, u16, (u64)U16_MAX + 1, true); 882 TEST_OVERFLOWS_TYPE(u64, u32, U64_MAX, true); 883 TEST_OVERFLOWS_TYPE(u64, u32, U32_MAX, false); 884 TEST_OVERFLOWS_TYPE(u64, u32, (u64)U32_MAX + 1, true); 885 TEST_OVERFLOWS_TYPE(u64, u64, U64_MAX, false); 886 TEST_OVERFLOWS_TYPE(u64, s8, S8_MAX, false); 887 TEST_OVERFLOWS_TYPE(u64, s8, (u64)S8_MAX + 1, true); 888 TEST_OVERFLOWS_TYPE(u64, s8, U64_MAX, true); 889 TEST_OVERFLOWS_TYPE(u64, s16, S16_MAX, false); 890 TEST_OVERFLOWS_TYPE(u64, s16, (u64)S16_MAX + 1, true); 891 TEST_OVERFLOWS_TYPE(u64, s16, U64_MAX, true); 892 TEST_OVERFLOWS_TYPE(u64, s32, S32_MAX, false); 893 TEST_OVERFLOWS_TYPE(u64, s32, (u64)S32_MAX + 1, true); 894 TEST_OVERFLOWS_TYPE(u64, s32, U64_MAX, true); 895 TEST_OVERFLOWS_TYPE(u64, s64, S64_MAX, false); 896 TEST_OVERFLOWS_TYPE(u64, s64, U64_MAX, true); 897 TEST_OVERFLOWS_TYPE(u64, s64, (u64)S64_MAX + 1, true); 898 TEST_OVERFLOWS_TYPE(s64, u8, S64_MAX, true); 899 TEST_OVERFLOWS_TYPE(s64, u8, S64_MIN, true); 900 TEST_OVERFLOWS_TYPE(s64, u8, -1, true); 901 TEST_OVERFLOWS_TYPE(s64, u8, U8_MAX, false); 902 TEST_OVERFLOWS_TYPE(s64, u8, (s64)U8_MAX + 1, true); 903 TEST_OVERFLOWS_TYPE(s64, u16, S64_MAX, true); 904 TEST_OVERFLOWS_TYPE(s64, u16, S64_MIN, true); 905 TEST_OVERFLOWS_TYPE(s64, u16, -1, true); 906 TEST_OVERFLOWS_TYPE(s64, u16, U16_MAX, false); 907 TEST_OVERFLOWS_TYPE(s64, u16, (s64)U16_MAX + 1, true); 908 TEST_OVERFLOWS_TYPE(s64, u32, S64_MAX, true); 909 TEST_OVERFLOWS_TYPE(s64, u32, S64_MIN, true); 910 TEST_OVERFLOWS_TYPE(s64, u32, -1, true); 911 TEST_OVERFLOWS_TYPE(s64, u32, U32_MAX, false); 912 TEST_OVERFLOWS_TYPE(s64, u32, (s64)U32_MAX + 1, true); 913 TEST_OVERFLOWS_TYPE(s64, u64, S64_MAX, false); 914 TEST_OVERFLOWS_TYPE(s64, u64, S64_MIN, true); 915 TEST_OVERFLOWS_TYPE(s64, u64, -1, true); 916 TEST_OVERFLOWS_TYPE(s64, s8, S8_MAX, false); 917 TEST_OVERFLOWS_TYPE(s64, s8, S8_MIN, false); 918 TEST_OVERFLOWS_TYPE(s64, s8, (s64)S8_MAX + 1, true); 919 TEST_OVERFLOWS_TYPE(s64, s8, (s64)S8_MIN - 1, true); 920 TEST_OVERFLOWS_TYPE(s64, s8, S64_MAX, true); 921 TEST_OVERFLOWS_TYPE(s64, s16, S16_MAX, false); 922 TEST_OVERFLOWS_TYPE(s64, s16, S16_MIN, false); 923 TEST_OVERFLOWS_TYPE(s64, s16, (s64)S16_MAX + 1, true); 924 TEST_OVERFLOWS_TYPE(s64, s16, (s64)S16_MIN - 1, true); 925 TEST_OVERFLOWS_TYPE(s64, s16, S64_MAX, true); 926 TEST_OVERFLOWS_TYPE(s64, s32, S32_MAX, false); 927 TEST_OVERFLOWS_TYPE(s64, s32, S32_MIN, false); 928 TEST_OVERFLOWS_TYPE(s64, s32, (s64)S32_MAX + 1, true); 929 TEST_OVERFLOWS_TYPE(s64, s32, (s64)S32_MIN - 1, true); 930 TEST_OVERFLOWS_TYPE(s64, s32, S64_MAX, true); 931 TEST_OVERFLOWS_TYPE(s64, s64, S64_MAX, false); 932 TEST_OVERFLOWS_TYPE(s64, s64, S64_MIN, false); 933 #endif 934 935 /* Check for macro side-effects. */ 936 var = INT_MAX - 1; 937 __TEST_OVERFLOWS_TYPE(__overflows_type, var++, int, false); 938 __TEST_OVERFLOWS_TYPE(__overflows_type, var++, int, false); 939 __TEST_OVERFLOWS_TYPE(__overflows_type, var++, int, true); 940 var = INT_MAX - 1; 941 __TEST_OVERFLOWS_TYPE(overflows_type, var++, int, false); 942 __TEST_OVERFLOWS_TYPE(overflows_type, var++, int, false); 943 __TEST_OVERFLOWS_TYPE(overflows_type, var++, int, true); 944 945 kunit_info(test, "%d overflows_type() tests finished\n", count); 946 #undef TEST_OVERFLOWS_TYPE 947 #undef __TEST_OVERFLOWS_TYPE 948 } 949 950 static void same_type_test(struct kunit *test) 951 { 952 int count = 0; 953 int var; 954 955 #define TEST_SAME_TYPE(t1, t2, same) do { \ 956 typeof(t1) __t1h = type_max(t1); \ 957 typeof(t1) __t1l = type_min(t1); \ 958 typeof(t2) __t2h = type_max(t2); \ 959 typeof(t2) __t2l = type_min(t2); \ 960 KUNIT_EXPECT_EQ(test, true, __same_type(t1, __t1h)); \ 961 KUNIT_EXPECT_EQ(test, true, __same_type(t1, __t1l)); \ 962 KUNIT_EXPECT_EQ(test, true, __same_type(__t1h, t1)); \ 963 KUNIT_EXPECT_EQ(test, true, __same_type(__t1l, t1)); \ 964 KUNIT_EXPECT_EQ(test, true, __same_type(t2, __t2h)); \ 965 KUNIT_EXPECT_EQ(test, true, __same_type(t2, __t2l)); \ 966 KUNIT_EXPECT_EQ(test, true, __same_type(__t2h, t2)); \ 967 KUNIT_EXPECT_EQ(test, true, __same_type(__t2l, t2)); \ 968 KUNIT_EXPECT_EQ(test, same, __same_type(t1, t2)); \ 969 KUNIT_EXPECT_EQ(test, same, __same_type(t2, __t1h)); \ 970 KUNIT_EXPECT_EQ(test, same, __same_type(t2, __t1l)); \ 971 KUNIT_EXPECT_EQ(test, same, __same_type(__t1h, t2)); \ 972 KUNIT_EXPECT_EQ(test, same, __same_type(__t1l, t2)); \ 973 KUNIT_EXPECT_EQ(test, same, __same_type(t1, __t2h)); \ 974 KUNIT_EXPECT_EQ(test, same, __same_type(t1, __t2l)); \ 975 KUNIT_EXPECT_EQ(test, same, __same_type(__t2h, t1)); \ 976 KUNIT_EXPECT_EQ(test, same, __same_type(__t2l, t1)); \ 977 } while (0) 978 979 #if BITS_PER_LONG == 64 980 # define TEST_SAME_TYPE64(base, t, m) TEST_SAME_TYPE(base, t, m) 981 #else 982 # define TEST_SAME_TYPE64(base, t, m) do { } while (0) 983 #endif 984 985 #define TEST_TYPE_SETS(base, mu8, mu16, mu32, ms8, ms16, ms32, mu64, ms64) \ 986 do { \ 987 TEST_SAME_TYPE(base, u8, mu8); \ 988 TEST_SAME_TYPE(base, u16, mu16); \ 989 TEST_SAME_TYPE(base, u32, mu32); \ 990 TEST_SAME_TYPE(base, s8, ms8); \ 991 TEST_SAME_TYPE(base, s16, ms16); \ 992 TEST_SAME_TYPE(base, s32, ms32); \ 993 TEST_SAME_TYPE64(base, u64, mu64); \ 994 TEST_SAME_TYPE64(base, s64, ms64); \ 995 } while (0) 996 997 TEST_TYPE_SETS(u8, true, false, false, false, false, false, false, false); 998 TEST_TYPE_SETS(u16, false, true, false, false, false, false, false, false); 999 TEST_TYPE_SETS(u32, false, false, true, false, false, false, false, false); 1000 TEST_TYPE_SETS(s8, false, false, false, true, false, false, false, false); 1001 TEST_TYPE_SETS(s16, false, false, false, false, true, false, false, false); 1002 TEST_TYPE_SETS(s32, false, false, false, false, false, true, false, false); 1003 #if BITS_PER_LONG == 64 1004 TEST_TYPE_SETS(u64, false, false, false, false, false, false, true, false); 1005 TEST_TYPE_SETS(s64, false, false, false, false, false, false, false, true); 1006 #endif 1007 1008 /* Check for macro side-effects. */ 1009 var = 4; 1010 KUNIT_EXPECT_EQ(test, var, 4); 1011 KUNIT_EXPECT_TRUE(test, __same_type(var++, int)); 1012 KUNIT_EXPECT_EQ(test, var, 4); 1013 KUNIT_EXPECT_TRUE(test, __same_type(int, var++)); 1014 KUNIT_EXPECT_EQ(test, var, 4); 1015 KUNIT_EXPECT_TRUE(test, __same_type(var++, var++)); 1016 KUNIT_EXPECT_EQ(test, var, 4); 1017 1018 kunit_info(test, "%d __same_type() tests finished\n", count); 1019 1020 #undef TEST_TYPE_SETS 1021 #undef TEST_SAME_TYPE64 1022 #undef TEST_SAME_TYPE 1023 } 1024 1025 static void castable_to_type_test(struct kunit *test) 1026 { 1027 int count = 0; 1028 1029 #define TEST_CASTABLE_TO_TYPE(arg1, arg2, pass) do { \ 1030 bool __pass = castable_to_type(arg1, arg2); \ 1031 KUNIT_EXPECT_EQ_MSG(test, __pass, pass, \ 1032 "expected castable_to_type(" #arg1 ", " #arg2 ") to%s pass\n",\ 1033 pass ? "" : " not"); \ 1034 count++; \ 1035 } while (0) 1036 1037 TEST_CASTABLE_TO_TYPE(16, u8, true); 1038 TEST_CASTABLE_TO_TYPE(16, u16, true); 1039 TEST_CASTABLE_TO_TYPE(16, u32, true); 1040 TEST_CASTABLE_TO_TYPE(16, s8, true); 1041 TEST_CASTABLE_TO_TYPE(16, s16, true); 1042 TEST_CASTABLE_TO_TYPE(16, s32, true); 1043 TEST_CASTABLE_TO_TYPE(-16, s8, true); 1044 TEST_CASTABLE_TO_TYPE(-16, s16, true); 1045 TEST_CASTABLE_TO_TYPE(-16, s32, true); 1046 #if BITS_PER_LONG == 64 1047 TEST_CASTABLE_TO_TYPE(16, u64, true); 1048 TEST_CASTABLE_TO_TYPE(-16, s64, true); 1049 #endif 1050 1051 #define TEST_CASTABLE_TO_TYPE_VAR(width) do { \ 1052 u ## width u ## width ## var = 0; \ 1053 s ## width s ## width ## var = 0; \ 1054 \ 1055 /* Constant expressions that fit types. */ \ 1056 TEST_CASTABLE_TO_TYPE(type_max(u ## width), u ## width, true); \ 1057 TEST_CASTABLE_TO_TYPE(type_min(u ## width), u ## width, true); \ 1058 TEST_CASTABLE_TO_TYPE(type_max(u ## width), u ## width ## var, true); \ 1059 TEST_CASTABLE_TO_TYPE(type_min(u ## width), u ## width ## var, true); \ 1060 TEST_CASTABLE_TO_TYPE(type_max(s ## width), s ## width, true); \ 1061 TEST_CASTABLE_TO_TYPE(type_min(s ## width), s ## width, true); \ 1062 TEST_CASTABLE_TO_TYPE(type_max(s ## width), s ## width ## var, true); \ 1063 TEST_CASTABLE_TO_TYPE(type_min(u ## width), s ## width ## var, true); \ 1064 /* Constant expressions that do not fit types. */ \ 1065 TEST_CASTABLE_TO_TYPE(type_max(u ## width), s ## width, false); \ 1066 TEST_CASTABLE_TO_TYPE(type_max(u ## width), s ## width ## var, false); \ 1067 TEST_CASTABLE_TO_TYPE(type_min(s ## width), u ## width, false); \ 1068 TEST_CASTABLE_TO_TYPE(type_min(s ## width), u ## width ## var, false); \ 1069 /* Non-constant expression with mismatched type. */ \ 1070 TEST_CASTABLE_TO_TYPE(s ## width ## var, u ## width, false); \ 1071 TEST_CASTABLE_TO_TYPE(u ## width ## var, s ## width, false); \ 1072 } while (0) 1073 1074 #define TEST_CASTABLE_TO_TYPE_RANGE(width) do { \ 1075 unsigned long big = U ## width ## _MAX; \ 1076 signed long small = S ## width ## _MIN; \ 1077 u ## width u ## width ## var = 0; \ 1078 s ## width s ## width ## var = 0; \ 1079 \ 1080 /* Constant expression in range. */ \ 1081 TEST_CASTABLE_TO_TYPE(U ## width ## _MAX, u ## width, true); \ 1082 TEST_CASTABLE_TO_TYPE(U ## width ## _MAX, u ## width ## var, true); \ 1083 TEST_CASTABLE_TO_TYPE(S ## width ## _MIN, s ## width, true); \ 1084 TEST_CASTABLE_TO_TYPE(S ## width ## _MIN, s ## width ## var, true); \ 1085 /* Constant expression out of range. */ \ 1086 TEST_CASTABLE_TO_TYPE((unsigned long)U ## width ## _MAX + 1, u ## width, false); \ 1087 TEST_CASTABLE_TO_TYPE((unsigned long)U ## width ## _MAX + 1, u ## width ## var, false); \ 1088 TEST_CASTABLE_TO_TYPE((signed long)S ## width ## _MIN - 1, s ## width, false); \ 1089 TEST_CASTABLE_TO_TYPE((signed long)S ## width ## _MIN - 1, s ## width ## var, false); \ 1090 /* Non-constant expression with mismatched type. */ \ 1091 TEST_CASTABLE_TO_TYPE(big, u ## width, false); \ 1092 TEST_CASTABLE_TO_TYPE(big, u ## width ## var, false); \ 1093 TEST_CASTABLE_TO_TYPE(small, s ## width, false); \ 1094 TEST_CASTABLE_TO_TYPE(small, s ## width ## var, false); \ 1095 } while (0) 1096 1097 TEST_CASTABLE_TO_TYPE_VAR(8); 1098 TEST_CASTABLE_TO_TYPE_VAR(16); 1099 TEST_CASTABLE_TO_TYPE_VAR(32); 1100 #if BITS_PER_LONG == 64 1101 TEST_CASTABLE_TO_TYPE_VAR(64); 1102 #endif 1103 1104 TEST_CASTABLE_TO_TYPE_RANGE(8); 1105 TEST_CASTABLE_TO_TYPE_RANGE(16); 1106 #if BITS_PER_LONG == 64 1107 TEST_CASTABLE_TO_TYPE_RANGE(32); 1108 #endif 1109 kunit_info(test, "%d castable_to_type() tests finished\n", count); 1110 1111 #undef TEST_CASTABLE_TO_TYPE_RANGE 1112 #undef TEST_CASTABLE_TO_TYPE_VAR 1113 #undef TEST_CASTABLE_TO_TYPE 1114 } 1115 1116 static struct kunit_case overflow_test_cases[] = { 1117 KUNIT_CASE(u8_u8__u8_overflow_test), 1118 KUNIT_CASE(s8_s8__s8_overflow_test), 1119 KUNIT_CASE(u16_u16__u16_overflow_test), 1120 KUNIT_CASE(s16_s16__s16_overflow_test), 1121 KUNIT_CASE(u32_u32__u32_overflow_test), 1122 KUNIT_CASE(s32_s32__s32_overflow_test), 1123 KUNIT_CASE(u64_u64__u64_overflow_test), 1124 KUNIT_CASE(s64_s64__s64_overflow_test), 1125 KUNIT_CASE(u32_u32__int_overflow_test), 1126 KUNIT_CASE(u32_u32__u8_overflow_test), 1127 KUNIT_CASE(u8_u8__int_overflow_test), 1128 KUNIT_CASE(int_int__u8_overflow_test), 1129 KUNIT_CASE(shift_sane_test), 1130 KUNIT_CASE(shift_overflow_test), 1131 KUNIT_CASE(shift_truncate_test), 1132 KUNIT_CASE(shift_nonsense_test), 1133 KUNIT_CASE(overflow_allocation_test), 1134 KUNIT_CASE(overflow_size_helpers_test), 1135 KUNIT_CASE(overflows_type_test), 1136 KUNIT_CASE(same_type_test), 1137 KUNIT_CASE(castable_to_type_test), 1138 {} 1139 }; 1140 1141 static struct kunit_suite overflow_test_suite = { 1142 .name = "overflow", 1143 .test_cases = overflow_test_cases, 1144 }; 1145 1146 kunit_test_suite(overflow_test_suite); 1147 1148 MODULE_LICENSE("Dual MIT/GPL"); 1149