at commit 709ba1c5
| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Test cases for compiler-based stack variable zeroing via |
| 4 | * -ftrivial-auto-var-init={zero,pattern}. |
| 5 | * For example, see: |
| 6 | * "Running tests with kunit_tool" at Documentation/dev-tools/kunit/start.rst |
| 7 | * ./tools/testing/kunit/kunit.py run stackinit [--raw_output] \ |
| 8 | * --make_option LLVM=1 \ |
| 9 | * --kconfig_add CONFIG_INIT_STACK_ALL_ZERO=y |
| 10 | * |
| 11 | */ |
| 12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 13 | |
| 14 | #include <kunit/test.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/string.h> |
| 19 | |
| 20 | /* Exfiltration buffer. */ |
| 21 | #define MAX_VAR_SIZE 128 |
| 22 | static u8 check_buf[MAX_VAR_SIZE]; |
| 23 | |
| 24 | /* Character array to trigger stack protector in all functions. */ |
| 25 | #define VAR_BUFFER 32 |
| 26 | |
| 27 | /* Volatile mask to convince compiler to copy memory with 0xff. */ |
| 28 | static volatile u8 forced_mask = 0xff; |
| 29 | |
| 30 | /* Location and size tracking to validate fill and test are colocated. */ |
| 31 | static void *fill_start, *target_start; |
| 32 | static size_t fill_size, target_size; |
| 33 | |
| 34 | static bool stackinit_range_contains(char *haystack_start, size_t haystack_size, |
| 35 | char *needle_start, size_t needle_size) |
| 36 | { |
| 37 | if (needle_start >= haystack_start && |
| 38 | needle_start + needle_size <= haystack_start + haystack_size) |
| 39 | return true; |
| 40 | return false; |
| 41 | } |
| 42 | |
| 43 | /* Whether the test is expected to fail. */ |
| 44 | #define WANT_SUCCESS 0 |
| 45 | #define XFAIL 1 |
| 46 | |
| 47 | #define DO_NOTHING_TYPE_SCALAR(var_type) var_type |
| 48 | #define DO_NOTHING_TYPE_STRING(var_type) void |
| 49 | #define DO_NOTHING_TYPE_STRUCT(var_type) void |
| 50 | #define DO_NOTHING_TYPE_UNION(var_type) void |
| 51 | |
| 52 | #define DO_NOTHING_RETURN_SCALAR(ptr) *(ptr) |
| 53 | #define DO_NOTHING_RETURN_STRING(ptr) /**/ |
| 54 | #define DO_NOTHING_RETURN_STRUCT(ptr) /**/ |
| 55 | #define DO_NOTHING_RETURN_UNION(ptr) /**/ |
| 56 | |
| 57 | #define DO_NOTHING_CALL_SCALAR(var, name) \ |
| 58 | (var) = do_nothing_ ## name(&(var)) |
| 59 | #define DO_NOTHING_CALL_STRING(var, name) \ |
| 60 | do_nothing_ ## name(var) |
| 61 | #define DO_NOTHING_CALL_STRUCT(var, name) \ |
| 62 | do_nothing_ ## name(&(var)) |
| 63 | #define DO_NOTHING_CALL_UNION(var, name) \ |
| 64 | do_nothing_ ## name(&(var)) |
| 65 | |
| 66 | #define FETCH_ARG_SCALAR(var) &var |
| 67 | #define FETCH_ARG_STRING(var) var |
| 68 | #define FETCH_ARG_STRUCT(var) &var |
| 69 | #define FETCH_ARG_UNION(var) &var |
| 70 | |
| 71 | /* |
| 72 | * On m68k, if the leaf function test variable is longer than 8 bytes, |
| 73 | * the start of the stack frame moves. 8 is sufficiently large to |
| 74 | * test m68k char arrays, but leave it at 16 for other architectures. |
| 75 | */ |
| 76 | #ifdef CONFIG_M68K |
| 77 | #define FILL_SIZE_STRING 8 |
| 78 | #define FILL_SIZE_ARRAY 2 |
| 79 | #else |
| 80 | #define FILL_SIZE_STRING 16 |
| 81 | #define FILL_SIZE_ARRAY 8 |
| 82 | #endif |
| 83 | |
| 84 | #define INIT_CLONE_SCALAR /**/ |
| 85 | #define INIT_CLONE_STRING [FILL_SIZE_STRING] |
| 86 | #define INIT_CLONE_STRUCT /**/ |
| 87 | #define INIT_CLONE_UNION /**/ |
| 88 | |
| 89 | #define ZERO_CLONE_SCALAR(zero) memset(&(zero), 0x00, sizeof(zero)) |
| 90 | #define ZERO_CLONE_STRING(zero) memset(&(zero), 0x00, sizeof(zero)) |
| 91 | /* |
| 92 | * For the struct, intentionally poison padding to see if it gets |
| 93 | * copied out in direct assignments. |
| 94 | * */ |
| 95 | #define ZERO_CLONE_STRUCT(zero) \ |
| 96 | do { \ |
| 97 | memset(&(zero), 0xFF, sizeof(zero)); \ |
| 98 | zero.one = 0; \ |
| 99 | zero.two = 0; \ |
| 100 | zero.three = 0; \ |
| 101 | zero.four = 0; \ |
| 102 | } while (0) |
| 103 | #define ZERO_CLONE_UNION(zero) ZERO_CLONE_STRUCT(zero) |
| 104 | |
| 105 | #define INIT_SCALAR_none(var_type) /**/ |
| 106 | #define INIT_SCALAR_zero(var_type) = 0 |
| 107 | |
| 108 | #define INIT_STRING_none(var_type) [FILL_SIZE_STRING] /**/ |
| 109 | #define INIT_STRING_zero(var_type) [FILL_SIZE_STRING] = { } |
| 110 | |
| 111 | #define INIT_STRUCT_none(var_type) /**/ |
| 112 | #define INIT_STRUCT_zero(var_type) = { } |
| 113 | #define INIT_STRUCT_old_zero(var_type) = { 0 } |
| 114 | |
| 115 | |
| 116 | #define __static_partial { .two = 0, } |
| 117 | #define __static_all { .one = 0, \ |
| 118 | .two = 0, \ |
| 119 | .three = 0, \ |
| 120 | .four = 0, \ |
| 121 | } |
| 122 | #define __dynamic_partial { .two = arg->two, } |
| 123 | #define __dynamic_all { .one = arg->one, \ |
| 124 | .two = arg->two, \ |
| 125 | .three = arg->three, \ |
| 126 | .four = arg->four, \ |
| 127 | } |
| 128 | #define __runtime_partial var.two = 0 |
| 129 | #define __runtime_all var.one = 0; \ |
| 130 | var.two = 0; \ |
| 131 | var.three = 0; \ |
| 132 | var.four = 0 |
| 133 | |
| 134 | #define INIT_STRUCT_static_partial(var_type) \ |
| 135 | = __static_partial |
| 136 | #define INIT_STRUCT_static_all(var_type) \ |
| 137 | = __static_all |
| 138 | #define INIT_STRUCT_dynamic_partial(var_type) \ |
| 139 | = __dynamic_partial |
| 140 | #define INIT_STRUCT_dynamic_all(var_type) \ |
| 141 | = __dynamic_all |
| 142 | #define INIT_STRUCT_runtime_partial(var_type) \ |
| 143 | ; __runtime_partial |
| 144 | #define INIT_STRUCT_runtime_all(var_type) \ |
| 145 | ; __runtime_all |
| 146 | |
| 147 | #define INIT_STRUCT_assigned_static_partial(var_type) \ |
| 148 | ; var = (var_type)__static_partial |
| 149 | #define INIT_STRUCT_assigned_static_all(var_type) \ |
| 150 | ; var = (var_type)__static_all |
| 151 | #define INIT_STRUCT_assigned_dynamic_partial(var_type) \ |
| 152 | ; var = (var_type)__dynamic_partial |
| 153 | #define INIT_STRUCT_assigned_dynamic_all(var_type) \ |
| 154 | ; var = (var_type)__dynamic_all |
| 155 | |
| 156 | #define INIT_STRUCT_assigned_copy(var_type) \ |
| 157 | ; var = *(arg) |
| 158 | |
| 159 | /* Union initialization is the same as structs. */ |
| 160 | #define INIT_UNION_none(var_type) INIT_STRUCT_none(var_type) |
| 161 | #define INIT_UNION_zero(var_type) INIT_STRUCT_zero(var_type) |
| 162 | #define INIT_UNION_old_zero(var_type) INIT_STRUCT_old_zero(var_type) |
| 163 | |
| 164 | #define INIT_UNION_static_partial(var_type) \ |
| 165 | INIT_STRUCT_static_partial(var_type) |
| 166 | #define INIT_UNION_static_all(var_type) \ |
| 167 | INIT_STRUCT_static_all(var_type) |
| 168 | #define INIT_UNION_dynamic_partial(var_type) \ |
| 169 | INIT_STRUCT_dynamic_partial(var_type) |
| 170 | #define INIT_UNION_dynamic_all(var_type) \ |
| 171 | INIT_STRUCT_dynamic_all(var_type) |
| 172 | #define INIT_UNION_runtime_partial(var_type) \ |
| 173 | INIT_STRUCT_runtime_partial(var_type) |
| 174 | #define INIT_UNION_runtime_all(var_type) \ |
| 175 | INIT_STRUCT_runtime_all(var_type) |
| 176 | #define INIT_UNION_assigned_static_partial(var_type) \ |
| 177 | INIT_STRUCT_assigned_static_partial(var_type) |
| 178 | #define INIT_UNION_assigned_static_all(var_type) \ |
| 179 | INIT_STRUCT_assigned_static_all(var_type) |
| 180 | #define INIT_UNION_assigned_dynamic_partial(var_type) \ |
| 181 | INIT_STRUCT_assigned_dynamic_partial(var_type) |
| 182 | #define INIT_UNION_assigned_dynamic_all(var_type) \ |
| 183 | INIT_STRUCT_assigned_dynamic_all(var_type) |
| 184 | #define INIT_UNION_assigned_copy(var_type) \ |
| 185 | INIT_STRUCT_assigned_copy(var_type) |
| 186 | |
| 187 | /* |
| 188 | * The "did we actually fill the stack?" check value needs |
| 189 | * to be neither 0 nor any of the "pattern" bytes. The |
| 190 | * pattern bytes are compiler, architecture, and type based, |
| 191 | * so we have to pick a value that never appears for those |
| 192 | * combinations. Use 0x99 which is not 0xFF, 0xFE, nor 0xAA. |
| 193 | */ |
| 194 | #define FILL_BYTE 0x99 |
| 195 | |
| 196 | /* |
| 197 | * @name: unique string name for the test |
| 198 | * @var_type: type to be tested for zeroing initialization |
| 199 | * @which: is this a SCALAR, STRING, or STRUCT type? |
| 200 | * @init_level: what kind of initialization is performed |
| 201 | * @xfail: is this test expected to fail? |
| 202 | */ |
| 203 | #define DEFINE_TEST_DRIVER(name, var_type, which, xfail) \ |
| 204 | /* Returns 0 on success, 1 on failure. */ \ |
| 205 | static noinline void test_ ## name (struct kunit *test) \ |
| 206 | { \ |
| 207 | var_type zero INIT_CLONE_ ## which; \ |
| 208 | int ignored; \ |
| 209 | u8 sum = 0, i; \ |
| 210 | \ |
| 211 | /* Notice when a new test is larger than expected. */ \ |
| 212 | BUILD_BUG_ON(sizeof(zero) > MAX_VAR_SIZE); \ |
| 213 | \ |
| 214 | /* Fill clone type with zero for per-field init. */ \ |
| 215 | ZERO_CLONE_ ## which(zero); \ |
| 216 | /* Clear entire check buffer for 0xFF overlap test. */ \ |
| 217 | memset(check_buf, 0x00, sizeof(check_buf)); \ |
| 218 | /* Fill stack with FILL_BYTE. */ \ |
| 219 | ignored = leaf_ ##name((unsigned long)&ignored, 1, \ |
| 220 | FETCH_ARG_ ## which(zero)); \ |
| 221 | /* Verify all bytes overwritten with FILL_BYTE. */ \ |
| 222 | for (sum = 0, i = 0; i < target_size; i++) \ |
| 223 | sum += (check_buf[i] != FILL_BYTE); \ |
| 224 | /* Clear entire check buffer for later bit tests. */ \ |
| 225 | memset(check_buf, 0x00, sizeof(check_buf)); \ |
| 226 | /* Extract stack-defined variable contents. */ \ |
| 227 | ignored = leaf_ ##name((unsigned long)&ignored, 0, \ |
| 228 | FETCH_ARG_ ## which(zero)); \ |
| 229 | /* \ |
| 230 | * Delay the sum test to here to do as little as \ |
| 231 | * possible between the two leaf function calls. \ |
| 232 | */ \ |
| 233 | KUNIT_ASSERT_EQ_MSG(test, sum, 0, \ |
| 234 | "leaf fill was not 0x%02X!?\n", \ |
| 235 | FILL_BYTE); \ |
| 236 | \ |
| 237 | /* Validate that compiler lined up fill and target. */ \ |
| 238 | KUNIT_ASSERT_TRUE_MSG(test, \ |
| 239 | stackinit_range_contains(fill_start, fill_size, \ |
| 240 | target_start, target_size), \ |
| 241 | "stackframe was not the same between calls!? " \ |
| 242 | "(fill %zu wide, target offset by %d)\n", \ |
| 243 | fill_size, \ |
| 244 | (int)((ssize_t)(uintptr_t)fill_start - \ |
| 245 | (ssize_t)(uintptr_t)target_start)); \ |
| 246 | \ |
| 247 | /* Validate check region has no FILL_BYTE bytes. */ \ |
| 248 | for (sum = 0, i = 0; i < target_size; i++) \ |
| 249 | sum += (check_buf[i] == FILL_BYTE); \ |
| 250 | \ |
| 251 | if (sum != 0 && xfail) \ |
| 252 | kunit_skip(test, \ |
| 253 | "XFAIL uninit bytes: %d\n", \ |
| 254 | sum); \ |
| 255 | KUNIT_ASSERT_EQ_MSG(test, sum, 0, \ |
| 256 | "uninit bytes: %d\n", sum); \ |
| 257 | } |
| 258 | #define DEFINE_TEST(name, var_type, which, init_level, xfail) \ |
| 259 | /* no-op to force compiler into ignoring "uninitialized" vars */\ |
| 260 | static noinline DO_NOTHING_TYPE_ ## which(var_type) \ |
| 261 | do_nothing_ ## name(var_type *ptr) \ |
| 262 | { \ |
| 263 | OPTIMIZER_HIDE_VAR(ptr); \ |
| 264 | /* Will always be true, but compiler doesn't know. */ \ |
| 265 | if ((unsigned long)ptr > 0x2) \ |
| 266 | return DO_NOTHING_RETURN_ ## which(ptr); \ |
| 267 | else \ |
| 268 | return DO_NOTHING_RETURN_ ## which(ptr + 1); \ |
| 269 | } \ |
| 270 | static noinline int leaf_ ## name(unsigned long sp, bool fill, \ |
| 271 | var_type *arg) \ |
| 272 | { \ |
| 273 | char buf[VAR_BUFFER]; \ |
| 274 | var_type var \ |
| 275 | INIT_ ## which ## _ ## init_level(var_type); \ |
| 276 | \ |
| 277 | target_start = &var; \ |
| 278 | target_size = sizeof(var); \ |
| 279 | /* \ |
| 280 | * Keep this buffer around to make sure we've got a \ |
| 281 | * stack frame of SOME kind... \ |
| 282 | */ \ |
| 283 | memset(buf, (char)(sp & 0xff), sizeof(buf)); \ |
| 284 | /* Fill variable with FILL_BYTE. */ \ |
| 285 | if (fill) { \ |
| 286 | fill_start = &var; \ |
| 287 | fill_size = sizeof(var); \ |
| 288 | memset(fill_start, \ |
| 289 | FILL_BYTE & forced_mask, \ |
| 290 | fill_size); \ |
| 291 | } \ |
| 292 | \ |
| 293 | /* Silence "never initialized" warnings. */ \ |
| 294 | DO_NOTHING_CALL_ ## which(var, name); \ |
| 295 | \ |
| 296 | /* Exfiltrate "var". */ \ |
| 297 | memcpy(check_buf, target_start, target_size); \ |
| 298 | \ |
| 299 | return (int)buf[0] | (int)buf[sizeof(buf) - 1]; \ |
| 300 | } \ |
| 301 | DEFINE_TEST_DRIVER(name, var_type, which, xfail) |
| 302 | |
| 303 | /* Structure with no padding. */ |
| 304 | struct test_packed { |
| 305 | unsigned long one; |
| 306 | unsigned long two; |
| 307 | unsigned long three; |
| 308 | unsigned long four; |
| 309 | }; |
| 310 | |
| 311 | /* Simple structure with padding likely to be covered by compiler. */ |
| 312 | struct test_small_hole { |
| 313 | size_t one; |
| 314 | char two; |
| 315 | /* 3 byte padding hole here. */ |
| 316 | int three; |
| 317 | unsigned long four; |
| 318 | }; |
| 319 | |
| 320 | /* Trigger unhandled padding in a structure. */ |
| 321 | struct test_big_hole { |
| 322 | u8 one; |
| 323 | u8 two; |
| 324 | u8 three; |
| 325 | /* 61 byte padding hole here. */ |
| 326 | u8 four __aligned(64); |
| 327 | } __aligned(64); |
| 328 | |
| 329 | struct test_trailing_hole { |
| 330 | char *one; |
| 331 | char *two; |
| 332 | char *three; |
| 333 | char four; |
| 334 | /* "sizeof(unsigned long) - 1" byte padding hole here. */ |
| 335 | }; |
| 336 | |
| 337 | /* Test if STRUCTLEAK is clearing structs with __user fields. */ |
| 338 | struct test_user { |
| 339 | u8 one; |
| 340 | unsigned long two; |
| 341 | char __user *three; |
| 342 | unsigned long four; |
| 343 | }; |
| 344 | |
| 345 | /* No padding: all members are the same size. */ |
| 346 | union test_same_sizes { |
| 347 | unsigned long one; |
| 348 | unsigned long two; |
| 349 | unsigned long three; |
| 350 | unsigned long four; |
| 351 | }; |
| 352 | |
| 353 | /* Mismatched sizes, with one and two being small */ |
| 354 | union test_small_start { |
| 355 | char one:1; |
| 356 | char two; |
| 357 | short three; |
| 358 | unsigned long four; |
| 359 | struct big_struct { |
| 360 | unsigned long array[FILL_SIZE_ARRAY]; |
| 361 | } big; |
| 362 | }; |
| 363 | |
| 364 | /* Mismatched sizes, with three and four being small */ |
| 365 | union test_small_end { |
| 366 | short one; |
| 367 | unsigned long two; |
| 368 | char three:1; |
| 369 | char four; |
| 370 | }; |
| 371 | |
| 372 | #define ALWAYS_PASS WANT_SUCCESS |
| 373 | #define ALWAYS_FAIL XFAIL |
| 374 | |
| 375 | #ifdef CONFIG_INIT_STACK_NONE |
| 376 | # define USER_PASS XFAIL |
| 377 | # define BYREF_PASS XFAIL |
| 378 | # define STRONG_PASS XFAIL |
| 379 | #else |
| 380 | # define USER_PASS WANT_SUCCESS |
| 381 | # define BYREF_PASS WANT_SUCCESS |
| 382 | # define STRONG_PASS WANT_SUCCESS |
| 383 | #endif |
| 384 | |
| 385 | #define DEFINE_SCALAR_TEST(name, init, xfail) \ |
| 386 | DEFINE_TEST(name ## _ ## init, name, SCALAR, \ |
| 387 | init, xfail) |
| 388 | |
| 389 | #define DEFINE_SCALAR_TESTS(init, xfail) \ |
| 390 | DEFINE_SCALAR_TEST(u8, init, xfail); \ |
| 391 | DEFINE_SCALAR_TEST(u16, init, xfail); \ |
| 392 | DEFINE_SCALAR_TEST(u32, init, xfail); \ |
| 393 | DEFINE_SCALAR_TEST(u64, init, xfail); \ |
| 394 | DEFINE_TEST(char_array_ ## init, unsigned char, \ |
| 395 | STRING, init, xfail) |
| 396 | |
| 397 | #define DEFINE_STRUCT_TEST(name, init, xfail) \ |
| 398 | DEFINE_TEST(name ## _ ## init, \ |
| 399 | struct test_ ## name, STRUCT, init, \ |
| 400 | xfail) |
| 401 | |
| 402 | #define DEFINE_UNION_TEST(name, init, xfail) \ |
| 403 | DEFINE_TEST(name ## _ ## init, \ |
| 404 | union test_ ## name, STRUCT, init, \ |
| 405 | xfail) |
| 406 | |
| 407 | #define DEFINE_STRUCT_TESTS(init, xfail) \ |
| 408 | DEFINE_STRUCT_TEST(small_hole, init, xfail); \ |
| 409 | DEFINE_STRUCT_TEST(big_hole, init, xfail); \ |
| 410 | DEFINE_STRUCT_TEST(trailing_hole, init, xfail); \ |
| 411 | DEFINE_STRUCT_TEST(packed, init, xfail) |
| 412 | |
| 413 | #define DEFINE_STRUCT_INITIALIZER_TESTS(base, xfail) \ |
| 414 | DEFINE_STRUCT_TESTS(base ## _ ## partial, \ |
| 415 | xfail); \ |
| 416 | DEFINE_STRUCT_TESTS(base ## _ ## all, xfail) |
| 417 | |
| 418 | #define DEFINE_UNION_INITIALIZER_TESTS(base, xfail) \ |
| 419 | DEFINE_UNION_TESTS(base ## _ ## partial, \ |
| 420 | xfail); \ |
| 421 | DEFINE_UNION_TESTS(base ## _ ## all, xfail) |
| 422 | |
| 423 | #define DEFINE_UNION_TESTS(init, xfail) \ |
| 424 | DEFINE_UNION_TEST(same_sizes, init, xfail); \ |
| 425 | DEFINE_UNION_TEST(small_start, init, xfail); \ |
| 426 | DEFINE_UNION_TEST(small_end, init, xfail); |
| 427 | |
| 428 | /* These should be fully initialized all the time! */ |
| 429 | DEFINE_SCALAR_TESTS(zero, ALWAYS_PASS); |
| 430 | DEFINE_STRUCT_TESTS(zero, ALWAYS_PASS); |
| 431 | DEFINE_STRUCT_TESTS(old_zero, ALWAYS_PASS); |
| 432 | DEFINE_UNION_TESTS(zero, ALWAYS_PASS); |
| 433 | DEFINE_UNION_TESTS(old_zero, ALWAYS_PASS); |
| 434 | /* Struct initializers: padding may be left uninitialized. */ |
| 435 | DEFINE_STRUCT_INITIALIZER_TESTS(static, STRONG_PASS); |
| 436 | DEFINE_STRUCT_INITIALIZER_TESTS(dynamic, STRONG_PASS); |
| 437 | DEFINE_STRUCT_INITIALIZER_TESTS(runtime, STRONG_PASS); |
| 438 | DEFINE_STRUCT_INITIALIZER_TESTS(assigned_static, STRONG_PASS); |
| 439 | DEFINE_STRUCT_INITIALIZER_TESTS(assigned_dynamic, STRONG_PASS); |
| 440 | DEFINE_STRUCT_TESTS(assigned_copy, ALWAYS_FAIL); |
| 441 | DEFINE_UNION_INITIALIZER_TESTS(static, STRONG_PASS); |
| 442 | DEFINE_UNION_INITIALIZER_TESTS(dynamic, STRONG_PASS); |
| 443 | DEFINE_UNION_INITIALIZER_TESTS(runtime, STRONG_PASS); |
| 444 | DEFINE_UNION_INITIALIZER_TESTS(assigned_static, STRONG_PASS); |
| 445 | DEFINE_UNION_INITIALIZER_TESTS(assigned_dynamic, STRONG_PASS); |
| 446 | DEFINE_UNION_TESTS(assigned_copy, ALWAYS_FAIL); |
| 447 | /* No initialization without compiler instrumentation. */ |
| 448 | DEFINE_SCALAR_TESTS(none, STRONG_PASS); |
| 449 | DEFINE_STRUCT_TESTS(none, BYREF_PASS); |
| 450 | /* Initialization of members with __user attribute. */ |
| 451 | DEFINE_TEST(user, struct test_user, STRUCT, none, USER_PASS); |
| 452 | |
| 453 | /* |
| 454 | * Check two uses through a variable declaration outside either path, |
| 455 | * which was noticed as a special case in porting earlier stack init |
| 456 | * compiler logic. |
| 457 | */ |
| 458 | static int noinline __leaf_switch_none(int path, bool fill) |
| 459 | { |
| 460 | switch (path) { |
| 461 | /* |
| 462 | * This is intentionally unreachable. To silence the |
| 463 | * warning, build with -Wno-switch-unreachable |
| 464 | */ |
| 465 | uint64_t var[10]; |
| 466 | |
| 467 | case 1: |
| 468 | target_start = &var; |
| 469 | target_size = sizeof(var); |
| 470 | if (fill) { |
| 471 | fill_start = &var; |
| 472 | fill_size = sizeof(var); |
| 473 | |
| 474 | memset(fill_start, (forced_mask | 0x55) & FILL_BYTE, fill_size); |
| 475 | } |
| 476 | memcpy(check_buf, target_start, target_size); |
| 477 | break; |
| 478 | case 2: |
| 479 | target_start = &var; |
| 480 | target_size = sizeof(var); |
| 481 | if (fill) { |
| 482 | fill_start = &var; |
| 483 | fill_size = sizeof(var); |
| 484 | |
| 485 | memset(fill_start, (forced_mask | 0xaa) & FILL_BYTE, fill_size); |
| 486 | } |
| 487 | memcpy(check_buf, target_start, target_size); |
| 488 | break; |
| 489 | default: |
| 490 | var[1] = 5; |
| 491 | return var[1] & forced_mask; |
| 492 | } |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | static noinline int leaf_switch_1_none(unsigned long sp, bool fill, |
| 497 | uint64_t *arg) |
| 498 | { |
| 499 | return __leaf_switch_none(1, fill); |
| 500 | } |
| 501 | |
| 502 | static noinline int leaf_switch_2_none(unsigned long sp, bool fill, |
| 503 | uint64_t *arg) |
| 504 | { |
| 505 | return __leaf_switch_none(2, fill); |
| 506 | } |
| 507 | |
| 508 | /* |
| 509 | * These are expected to fail for most configurations because neither |
| 510 | * GCC nor Clang have a way to perform initialization of variables in |
| 511 | * non-code areas (i.e. in a switch statement before the first "case"). |
| 512 | * https://llvm.org/pr44916 |
| 513 | */ |
| 514 | DEFINE_TEST_DRIVER(switch_1_none, uint64_t, SCALAR, ALWAYS_FAIL); |
| 515 | DEFINE_TEST_DRIVER(switch_2_none, uint64_t, SCALAR, ALWAYS_FAIL); |
| 516 | |
| 517 | #define KUNIT_test_scalars(init) \ |
| 518 | KUNIT_CASE(test_u8_ ## init), \ |
| 519 | KUNIT_CASE(test_u16_ ## init), \ |
| 520 | KUNIT_CASE(test_u32_ ## init), \ |
| 521 | KUNIT_CASE(test_u64_ ## init), \ |
| 522 | KUNIT_CASE(test_char_array_ ## init) |
| 523 | |
| 524 | #define KUNIT_test_structs(init) \ |
| 525 | KUNIT_CASE(test_small_hole_ ## init), \ |
| 526 | KUNIT_CASE(test_big_hole_ ## init), \ |
| 527 | KUNIT_CASE(test_trailing_hole_ ## init),\ |
| 528 | KUNIT_CASE(test_packed_ ## init) \ |
| 529 | |
| 530 | #define KUNIT_test_unions(init) \ |
| 531 | KUNIT_CASE(test_same_sizes_ ## init), \ |
| 532 | KUNIT_CASE(test_small_start_ ## init), \ |
| 533 | KUNIT_CASE(test_small_end_ ## init) \ |
| 534 | |
| 535 | static struct kunit_case stackinit_test_cases[] = { |
| 536 | /* These are explicitly initialized and should always pass. */ |
| 537 | KUNIT_test_scalars(zero), |
| 538 | KUNIT_test_structs(zero), |
| 539 | KUNIT_test_structs(old_zero), |
| 540 | KUNIT_test_unions(zero), |
| 541 | KUNIT_test_unions(old_zero), |
| 542 | /* Padding here appears to be accidentally always initialized? */ |
| 543 | KUNIT_test_structs(dynamic_partial), |
| 544 | KUNIT_test_structs(assigned_dynamic_partial), |
| 545 | KUNIT_test_unions(dynamic_partial), |
| 546 | KUNIT_test_unions(assigned_dynamic_partial), |
| 547 | /* Padding initialization depends on compiler behaviors. */ |
| 548 | KUNIT_test_structs(static_partial), |
| 549 | KUNIT_test_structs(static_all), |
| 550 | KUNIT_test_structs(dynamic_all), |
| 551 | KUNIT_test_structs(runtime_partial), |
| 552 | KUNIT_test_structs(runtime_all), |
| 553 | KUNIT_test_structs(assigned_static_partial), |
| 554 | KUNIT_test_structs(assigned_static_all), |
| 555 | KUNIT_test_structs(assigned_dynamic_all), |
| 556 | KUNIT_test_unions(static_partial), |
| 557 | KUNIT_test_unions(static_all), |
| 558 | KUNIT_test_unions(dynamic_all), |
| 559 | KUNIT_test_unions(runtime_partial), |
| 560 | KUNIT_test_unions(runtime_all), |
| 561 | KUNIT_test_unions(assigned_static_partial), |
| 562 | KUNIT_test_unions(assigned_static_all), |
| 563 | KUNIT_test_unions(assigned_dynamic_all), |
| 564 | /* Everything fails this since it effectively performs a memcpy(). */ |
| 565 | KUNIT_test_structs(assigned_copy), |
| 566 | KUNIT_test_unions(assigned_copy), |
| 567 | /* STRUCTLEAK_BYREF_ALL should cover everything from here down. */ |
| 568 | KUNIT_test_scalars(none), |
| 569 | KUNIT_CASE(test_switch_1_none), |
| 570 | KUNIT_CASE(test_switch_2_none), |
| 571 | /* STRUCTLEAK_BYREF should cover from here down. */ |
| 572 | KUNIT_test_structs(none), |
| 573 | /* STRUCTLEAK will only cover this. */ |
| 574 | KUNIT_CASE(test_user), |
| 575 | {} |
| 576 | }; |
| 577 | |
| 578 | static struct kunit_suite stackinit_test_suite = { |
| 579 | .name = "stackinit", |
| 580 | .test_cases = stackinit_test_cases, |
| 581 | }; |
| 582 | |
| 583 | kunit_test_suites(&stackinit_test_suite); |
| 584 | |
| 585 | MODULE_DESCRIPTION("Test cases for compiler-based stack variable zeroing"); |
| 586 | MODULE_LICENSE("GPL"); |