stackinit_kunit.c 18 KB

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