kmsan_test.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Test cases for KMSAN.
  4. * For each test case checks the presence (or absence) of generated reports.
  5. * Relies on 'console' tracepoint to capture reports as they appear in the
  6. * kernel log.
  7. *
  8. * Copyright (C) 2021-2022, Google LLC.
  9. * Author: Alexander Potapenko <glider@google.com>
  10. *
  11. */
  12. #include <kunit/test.h>
  13. #include "kmsan.h"
  14. #include <linux/jiffies.h>
  15. #include <linux/kernel.h>
  16. #include <linux/kmsan.h>
  17. #include <linux/mm.h>
  18. #include <linux/random.h>
  19. #include <linux/slab.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/string.h>
  22. #include <linux/tracepoint.h>
  23. #include <linux/vmalloc.h>
  24. #include <trace/events/printk.h>
  25. static DEFINE_PER_CPU(int, per_cpu_var);
  26. /* Report as observed from console. */
  27. static struct {
  28. spinlock_t lock;
  29. bool available;
  30. bool ignore; /* Stop console output collection. */
  31. char header[256];
  32. } observed = {
  33. .lock = __SPIN_LOCK_UNLOCKED(observed.lock),
  34. };
  35. /* Probe for console output: obtains observed lines of interest. */
  36. static void probe_console(void *ignore, const char *buf, size_t len)
  37. {
  38. unsigned long flags;
  39. if (observed.ignore)
  40. return;
  41. spin_lock_irqsave(&observed.lock, flags);
  42. if (strnstr(buf, "BUG: KMSAN: ", len)) {
  43. /*
  44. * KMSAN report and related to the test.
  45. *
  46. * The provided @buf is not NUL-terminated; copy no more than
  47. * @len bytes and let strscpy() add the missing NUL-terminator.
  48. */
  49. strscpy(observed.header, buf,
  50. min(len + 1, sizeof(observed.header)));
  51. WRITE_ONCE(observed.available, true);
  52. observed.ignore = true;
  53. }
  54. spin_unlock_irqrestore(&observed.lock, flags);
  55. }
  56. /* Check if a report related to the test exists. */
  57. static bool report_available(void)
  58. {
  59. return READ_ONCE(observed.available);
  60. }
  61. /* Reset observed.available, so that the test can trigger another report. */
  62. static void report_reset(void)
  63. {
  64. unsigned long flags;
  65. spin_lock_irqsave(&observed.lock, flags);
  66. WRITE_ONCE(observed.available, false);
  67. observed.ignore = false;
  68. spin_unlock_irqrestore(&observed.lock, flags);
  69. }
  70. /* Information we expect in a report. */
  71. struct expect_report {
  72. const char *error_type; /* Error type. */
  73. /*
  74. * Kernel symbol from the error header, or NULL if no report is
  75. * expected.
  76. */
  77. const char *symbol;
  78. };
  79. /* Check observed report matches information in @r. */
  80. static bool report_matches(const struct expect_report *r)
  81. {
  82. typeof(observed.header) expected_header;
  83. unsigned long flags;
  84. bool ret = false;
  85. const char *end;
  86. char *cur;
  87. /* Doubled-checked locking. */
  88. if (!report_available() || !r->symbol)
  89. return (!report_available() && !r->symbol);
  90. /* Generate expected report contents. */
  91. /* Title */
  92. cur = expected_header;
  93. end = &expected_header[sizeof(expected_header) - 1];
  94. cur += scnprintf(cur, end - cur, "BUG: KMSAN: %s", r->error_type);
  95. scnprintf(cur, end - cur, " in %s", r->symbol);
  96. /* The exact offset won't match, remove it; also strip module name. */
  97. cur = strchr(expected_header, '+');
  98. if (cur)
  99. *cur = '\0';
  100. spin_lock_irqsave(&observed.lock, flags);
  101. if (!report_available())
  102. goto out; /* A new report is being captured. */
  103. /* Finally match expected output to what we actually observed. */
  104. ret = strstr(observed.header, expected_header);
  105. out:
  106. spin_unlock_irqrestore(&observed.lock, flags);
  107. return ret;
  108. }
  109. /* ===== Test cases ===== */
  110. /* Prevent replacing branch with select in LLVM. */
  111. static noinline void check_true(char *arg)
  112. {
  113. pr_info("%s is true\n", arg);
  114. }
  115. static noinline void check_false(char *arg)
  116. {
  117. pr_info("%s is false\n", arg);
  118. }
  119. #define USE(x) \
  120. do { \
  121. if (x) \
  122. check_true(#x); \
  123. else \
  124. check_false(#x); \
  125. } while (0)
  126. #define EXPECTATION_ETYPE_FN(e, reason, fn) \
  127. struct expect_report e = { \
  128. .error_type = reason, \
  129. .symbol = fn, \
  130. }
  131. #define EXPECTATION_NO_REPORT(e) EXPECTATION_ETYPE_FN(e, NULL, NULL)
  132. #define EXPECTATION_UNINIT_VALUE_FN(e, fn) \
  133. EXPECTATION_ETYPE_FN(e, "uninit-value", fn)
  134. #define EXPECTATION_UNINIT_VALUE(e) EXPECTATION_UNINIT_VALUE_FN(e, __func__)
  135. #define EXPECTATION_USE_AFTER_FREE(e) \
  136. EXPECTATION_ETYPE_FN(e, "use-after-free", __func__)
  137. /* Test case: ensure that kmalloc() returns uninitialized memory. */
  138. static void test_uninit_kmalloc(struct kunit *test)
  139. {
  140. EXPECTATION_UNINIT_VALUE(expect);
  141. int *ptr;
  142. kunit_info(test, "uninitialized kmalloc test (UMR report)\n");
  143. ptr = kmalloc(sizeof(*ptr), GFP_KERNEL);
  144. USE(*ptr);
  145. KUNIT_EXPECT_TRUE(test, report_matches(&expect));
  146. }
  147. /*
  148. * Test case: ensure that kmalloc'ed memory becomes initialized after memset().
  149. */
  150. static void test_init_kmalloc(struct kunit *test)
  151. {
  152. EXPECTATION_NO_REPORT(expect);
  153. int *ptr;
  154. kunit_info(test, "initialized kmalloc test (no reports)\n");
  155. ptr = kmalloc(sizeof(*ptr), GFP_KERNEL);
  156. memset(ptr, 0, sizeof(*ptr));
  157. USE(*ptr);
  158. KUNIT_EXPECT_TRUE(test, report_matches(&expect));
  159. }
  160. /* Test case: ensure that kzalloc() returns initialized memory. */
  161. static void test_init_kzalloc(struct kunit *test)
  162. {
  163. EXPECTATION_NO_REPORT(expect);
  164. int *ptr;
  165. kunit_info(test, "initialized kzalloc test (no reports)\n");
  166. ptr = kzalloc(sizeof(*ptr), GFP_KERNEL);
  167. USE(*ptr);
  168. KUNIT_EXPECT_TRUE(test, report_matches(&expect));
  169. }
  170. /* Test case: ensure that local variables are uninitialized by default. */
  171. static void test_uninit_stack_var(struct kunit *test)
  172. {
  173. EXPECTATION_UNINIT_VALUE(expect);
  174. volatile int cond;
  175. kunit_info(test, "uninitialized stack variable (UMR report)\n");
  176. USE(cond);
  177. KUNIT_EXPECT_TRUE(test, report_matches(&expect));
  178. }
  179. /* Test case: ensure that local variables with initializers are initialized. */
  180. static void test_init_stack_var(struct kunit *test)
  181. {
  182. EXPECTATION_NO_REPORT(expect);
  183. volatile int cond = 1;
  184. kunit_info(test, "initialized stack variable (no reports)\n");
  185. USE(cond);
  186. KUNIT_EXPECT_TRUE(test, report_matches(&expect));
  187. }
  188. static noinline void two_param_fn_2(int arg1, int arg2)
  189. {
  190. USE(arg1);
  191. USE(arg2);
  192. }
  193. static noinline void one_param_fn(int arg)
  194. {
  195. two_param_fn_2(arg, arg);
  196. USE(arg);
  197. }
  198. static noinline void two_param_fn(int arg1, int arg2)
  199. {
  200. int init = 0;
  201. one_param_fn(init);
  202. USE(arg1);
  203. USE(arg2);
  204. }
  205. static void test_params(struct kunit *test)
  206. {
  207. #ifdef CONFIG_KMSAN_CHECK_PARAM_RETVAL
  208. /*
  209. * With eager param/retval checking enabled, KMSAN will report an error
  210. * before the call to two_param_fn().
  211. */
  212. EXPECTATION_UNINIT_VALUE_FN(expect, "test_params");
  213. #else
  214. EXPECTATION_UNINIT_VALUE_FN(expect, "two_param_fn");
  215. #endif
  216. volatile int uninit, init = 1;
  217. kunit_info(test,
  218. "uninit passed through a function parameter (UMR report)\n");
  219. two_param_fn(uninit, init);
  220. KUNIT_EXPECT_TRUE(test, report_matches(&expect));
  221. }
  222. static int signed_sum3(int a, int b, int c)
  223. {
  224. return a + b + c;
  225. }
  226. /*
  227. * Test case: ensure that uninitialized values are tracked through function
  228. * arguments.
  229. */
  230. static void test_uninit_multiple_params(struct kunit *test)
  231. {
  232. EXPECTATION_UNINIT_VALUE(expect);
  233. volatile char b = 3, c;
  234. volatile int a;
  235. kunit_info(test, "uninitialized local passed to fn (UMR report)\n");
  236. USE(signed_sum3(a, b, c));
  237. KUNIT_EXPECT_TRUE(test, report_matches(&expect));
  238. }
  239. /* Helper function to make an array uninitialized. */
  240. static noinline void do_uninit_local_array(char *array, int start, int stop)
  241. {
  242. volatile char uninit;
  243. for (int i = start; i < stop; i++)
  244. array[i] = uninit;
  245. }
  246. /*
  247. * Test case: ensure kmsan_check_memory() reports an error when checking
  248. * uninitialized memory.
  249. */
  250. static void test_uninit_kmsan_check_memory(struct kunit *test)
  251. {
  252. EXPECTATION_UNINIT_VALUE_FN(expect, "test_uninit_kmsan_check_memory");
  253. volatile char local_array[8];
  254. kunit_info(
  255. test,
  256. "kmsan_check_memory() called on uninit local (UMR report)\n");
  257. do_uninit_local_array((char *)local_array, 5, 7);
  258. kmsan_check_memory((char *)local_array, 8);
  259. KUNIT_EXPECT_TRUE(test, report_matches(&expect));
  260. }
  261. /*
  262. * Test case: check that a virtual memory range created with vmap() from
  263. * initialized pages is still considered as initialized.
  264. */
  265. static void test_init_kmsan_vmap_vunmap(struct kunit *test)
  266. {
  267. EXPECTATION_NO_REPORT(expect);
  268. const int npages = 2;
  269. struct page **pages;
  270. void *vbuf;
  271. kunit_info(test, "pages initialized via vmap (no reports)\n");
  272. pages = kmalloc_array(npages, sizeof(*pages), GFP_KERNEL);
  273. for (int i = 0; i < npages; i++)
  274. pages[i] = alloc_page(GFP_KERNEL);
  275. vbuf = vmap(pages, npages, VM_MAP, PAGE_KERNEL);
  276. memset(vbuf, 0xfe, npages * PAGE_SIZE);
  277. for (int i = 0; i < npages; i++)
  278. kmsan_check_memory(page_address(pages[i]), PAGE_SIZE);
  279. if (vbuf)
  280. vunmap(vbuf);
  281. for (int i = 0; i < npages; i++) {
  282. if (pages[i])
  283. __free_page(pages[i]);
  284. }
  285. kfree(pages);
  286. KUNIT_EXPECT_TRUE(test, report_matches(&expect));
  287. }
  288. /*
  289. * Test case: ensure that memset() can initialize a buffer allocated via
  290. * vmalloc().
  291. */
  292. static void test_init_vmalloc(struct kunit *test)
  293. {
  294. EXPECTATION_NO_REPORT(expect);
  295. int npages = 8;
  296. char *buf;
  297. kunit_info(test, "vmalloc buffer can be initialized (no reports)\n");
  298. buf = vmalloc(PAGE_SIZE * npages);
  299. buf[0] = 1;
  300. memset(buf, 0xfe, PAGE_SIZE * npages);
  301. USE(buf[0]);
  302. for (int i = 0; i < npages; i++)
  303. kmsan_check_memory(&buf[PAGE_SIZE * i], PAGE_SIZE);
  304. vfree(buf);
  305. KUNIT_EXPECT_TRUE(test, report_matches(&expect));
  306. }
  307. /* Test case: ensure that use-after-free reporting works. */
  308. static void test_uaf(struct kunit *test)
  309. {
  310. EXPECTATION_USE_AFTER_FREE(expect);
  311. volatile int value;
  312. volatile int *var;
  313. kunit_info(test, "use-after-free in kmalloc-ed buffer (UMR report)\n");
  314. var = kmalloc(80, GFP_KERNEL);
  315. var[3] = 0xfeedface;
  316. kfree((int *)var);
  317. /* Copy the invalid value before checking it. */
  318. value = var[3];
  319. USE(value);
  320. KUNIT_EXPECT_TRUE(test, report_matches(&expect));
  321. }
  322. /*
  323. * Test case: ensure that uninitialized values are propagated through per-CPU
  324. * memory.
  325. */
  326. static void test_percpu_propagate(struct kunit *test)
  327. {
  328. EXPECTATION_UNINIT_VALUE(expect);
  329. volatile int uninit, check;
  330. kunit_info(test,
  331. "uninit local stored to per_cpu memory (UMR report)\n");
  332. this_cpu_write(per_cpu_var, uninit);
  333. check = this_cpu_read(per_cpu_var);
  334. USE(check);
  335. KUNIT_EXPECT_TRUE(test, report_matches(&expect));
  336. }
  337. /*
  338. * Test case: ensure that passing uninitialized values to printk() leads to an
  339. * error report.
  340. */
  341. static void test_printk(struct kunit *test)
  342. {
  343. #ifdef CONFIG_KMSAN_CHECK_PARAM_RETVAL
  344. /*
  345. * With eager param/retval checking enabled, KMSAN will report an error
  346. * before the call to pr_info().
  347. */
  348. EXPECTATION_UNINIT_VALUE_FN(expect, "test_printk");
  349. #else
  350. EXPECTATION_UNINIT_VALUE_FN(expect, "number");
  351. #endif
  352. volatile int uninit;
  353. kunit_info(test, "uninit local passed to pr_info() (UMR report)\n");
  354. pr_info("%px contains %d\n", &uninit, uninit);
  355. KUNIT_EXPECT_TRUE(test, report_matches(&expect));
  356. }
  357. /* Prevent the compiler from inlining a memcpy() call. */
  358. static noinline void *memcpy_noinline(volatile void *dst,
  359. const volatile void *src, size_t size)
  360. {
  361. return memcpy((void *)dst, (const void *)src, size);
  362. }
  363. /* Test case: ensure that memcpy() correctly copies initialized values. */
  364. static void test_init_memcpy(struct kunit *test)
  365. {
  366. EXPECTATION_NO_REPORT(expect);
  367. volatile long long src;
  368. volatile long long dst = 0;
  369. src = 1;
  370. kunit_info(
  371. test,
  372. "memcpy()ing aligned initialized src to aligned dst (no reports)\n");
  373. memcpy_noinline((void *)&dst, (void *)&src, sizeof(src));
  374. kmsan_check_memory((void *)&dst, sizeof(dst));
  375. KUNIT_EXPECT_TRUE(test, report_matches(&expect));
  376. }
  377. /*
  378. * Test case: ensure that memcpy() correctly copies uninitialized values between
  379. * aligned `src` and `dst`.
  380. */
  381. static void test_memcpy_aligned_to_aligned(struct kunit *test)
  382. {
  383. EXPECTATION_UNINIT_VALUE_FN(expect, "test_memcpy_aligned_to_aligned");
  384. volatile int uninit_src;
  385. volatile int dst = 0;
  386. kunit_info(
  387. test,
  388. "memcpy()ing aligned uninit src to aligned dst (UMR report)\n");
  389. memcpy_noinline((void *)&dst, (void *)&uninit_src, sizeof(uninit_src));
  390. kmsan_check_memory((void *)&dst, sizeof(dst));
  391. KUNIT_EXPECT_TRUE(test, report_matches(&expect));
  392. }
  393. /*
  394. * Test case: ensure that memcpy() correctly copies uninitialized values between
  395. * aligned `src` and unaligned `dst`.
  396. *
  397. * Copying aligned 4-byte value to an unaligned one leads to touching two
  398. * aligned 4-byte values. This test case checks that KMSAN correctly reports an
  399. * error on the mentioned two values.
  400. */
  401. static void test_memcpy_aligned_to_unaligned(struct kunit *test)
  402. {
  403. EXPECTATION_UNINIT_VALUE_FN(expect, "test_memcpy_aligned_to_unaligned");
  404. volatile int uninit_src;
  405. volatile char dst[8] = { 0 };
  406. kunit_info(
  407. test,
  408. "memcpy()ing aligned uninit src to unaligned dst (UMR report)\n");
  409. kmsan_check_memory((void *)&uninit_src, sizeof(uninit_src));
  410. memcpy_noinline((void *)&dst[1], (void *)&uninit_src,
  411. sizeof(uninit_src));
  412. kmsan_check_memory((void *)dst, 4);
  413. KUNIT_EXPECT_TRUE(test, report_matches(&expect));
  414. report_reset();
  415. kmsan_check_memory((void *)&dst[4], sizeof(uninit_src));
  416. KUNIT_EXPECT_TRUE(test, report_matches(&expect));
  417. }
  418. /*
  419. * Test case: ensure that origin slots do not accidentally get overwritten with
  420. * zeroes during memcpy().
  421. *
  422. * Previously, when copying memory from an aligned buffer to an unaligned one,
  423. * if there were zero origins corresponding to zero shadow values in the source
  424. * buffer, they could have ended up being copied to nonzero shadow values in the
  425. * destination buffer:
  426. *
  427. * memcpy(0xffff888080a00000, 0xffff888080900002, 8)
  428. *
  429. * src (0xffff888080900002): ..xx .... xx..
  430. * src origins: o111 0000 o222
  431. * dst (0xffff888080a00000): xx.. ..xx
  432. * dst origins: o111 0000
  433. * (or 0000 o222)
  434. *
  435. * (here . stands for an initialized byte, and x for an uninitialized one.
  436. *
  437. * Ensure that this does not happen anymore, and for both destination bytes
  438. * the origin is nonzero (i.e. KMSAN reports an error).
  439. */
  440. static void test_memcpy_initialized_gap(struct kunit *test)
  441. {
  442. EXPECTATION_UNINIT_VALUE_FN(expect, "test_memcpy_initialized_gap");
  443. volatile char uninit_src[12];
  444. volatile char dst[8] = { 0 };
  445. kunit_info(
  446. test,
  447. "unaligned 4-byte initialized value gets a nonzero origin after memcpy() - (2 UMR reports)\n");
  448. uninit_src[0] = 42;
  449. uninit_src[1] = 42;
  450. uninit_src[4] = 42;
  451. uninit_src[5] = 42;
  452. uninit_src[6] = 42;
  453. uninit_src[7] = 42;
  454. uninit_src[10] = 42;
  455. uninit_src[11] = 42;
  456. memcpy_noinline((void *)&dst[0], (void *)&uninit_src[2], 8);
  457. kmsan_check_memory((void *)&dst[0], 4);
  458. KUNIT_EXPECT_TRUE(test, report_matches(&expect));
  459. report_reset();
  460. kmsan_check_memory((void *)&dst[2], 4);
  461. KUNIT_EXPECT_FALSE(test, report_matches(&expect));
  462. report_reset();
  463. kmsan_check_memory((void *)&dst[4], 4);
  464. KUNIT_EXPECT_TRUE(test, report_matches(&expect));
  465. }
  466. /* Generate test cases for memset16(), memset32(), memset64(). */
  467. #define DEFINE_TEST_MEMSETXX(size) \
  468. static void test_memset##size(struct kunit *test) \
  469. { \
  470. EXPECTATION_NO_REPORT(expect); \
  471. volatile uint##size##_t uninit; \
  472. \
  473. kunit_info(test, \
  474. "memset" #size "() should initialize memory\n"); \
  475. memset##size((uint##size##_t *)&uninit, 0, 1); \
  476. kmsan_check_memory((void *)&uninit, sizeof(uninit)); \
  477. KUNIT_EXPECT_TRUE(test, report_matches(&expect)); \
  478. }
  479. DEFINE_TEST_MEMSETXX(16)
  480. DEFINE_TEST_MEMSETXX(32)
  481. DEFINE_TEST_MEMSETXX(64)
  482. static noinline void fibonacci(int *array, int size, int start)
  483. {
  484. if (start < 2 || (start == size))
  485. return;
  486. array[start] = array[start - 1] + array[start - 2];
  487. fibonacci(array, size, start + 1);
  488. }
  489. static void test_long_origin_chain(struct kunit *test)
  490. {
  491. EXPECTATION_UNINIT_VALUE_FN(expect, "test_long_origin_chain");
  492. /* (KMSAN_MAX_ORIGIN_DEPTH * 2) recursive calls to fibonacci(). */
  493. volatile int accum[KMSAN_MAX_ORIGIN_DEPTH * 2 + 2];
  494. int last = ARRAY_SIZE(accum) - 1;
  495. kunit_info(
  496. test,
  497. "origin chain exceeding KMSAN_MAX_ORIGIN_DEPTH (UMR report)\n");
  498. /*
  499. * We do not set accum[1] to 0, so the uninitializedness will be carried
  500. * over to accum[2..last].
  501. */
  502. accum[0] = 1;
  503. fibonacci((int *)accum, ARRAY_SIZE(accum), 2);
  504. kmsan_check_memory((void *)&accum[last], sizeof(int));
  505. KUNIT_EXPECT_TRUE(test, report_matches(&expect));
  506. }
  507. /*
  508. * Test case: ensure that saving/restoring/printing stacks to/from stackdepot
  509. * does not trigger errors.
  510. *
  511. * KMSAN uses stackdepot to store origin stack traces, that's why we do not
  512. * instrument lib/stackdepot.c. Yet it must properly mark its outputs as
  513. * initialized because other kernel features (e.g. netdev tracker) may also
  514. * access stackdepot from instrumented code.
  515. */
  516. static void test_stackdepot_roundtrip(struct kunit *test)
  517. {
  518. unsigned long src_entries[16], *dst_entries;
  519. unsigned int src_nentries, dst_nentries;
  520. EXPECTATION_NO_REPORT(expect);
  521. depot_stack_handle_t handle;
  522. kunit_info(test, "testing stackdepot roundtrip (no reports)\n");
  523. src_nentries =
  524. stack_trace_save(src_entries, ARRAY_SIZE(src_entries), 1);
  525. handle = stack_depot_save(src_entries, src_nentries, GFP_KERNEL);
  526. stack_depot_print(handle);
  527. dst_nentries = stack_depot_fetch(handle, &dst_entries);
  528. KUNIT_EXPECT_TRUE(test, src_nentries == dst_nentries);
  529. kmsan_check_memory((void *)dst_entries,
  530. sizeof(*dst_entries) * dst_nentries);
  531. KUNIT_EXPECT_TRUE(test, report_matches(&expect));
  532. }
  533. /*
  534. * Test case: ensure that kmsan_unpoison_memory() and the instrumentation work
  535. * the same.
  536. */
  537. static void test_unpoison_memory(struct kunit *test)
  538. {
  539. EXPECTATION_UNINIT_VALUE_FN(expect, "test_unpoison_memory");
  540. volatile char a[4], b[4];
  541. kunit_info(
  542. test,
  543. "unpoisoning via the instrumentation vs. kmsan_unpoison_memory() (2 UMR reports)\n");
  544. /* Initialize a[0] and check a[1]--a[3]. */
  545. a[0] = 0;
  546. kmsan_check_memory((char *)&a[1], 3);
  547. KUNIT_EXPECT_TRUE(test, report_matches(&expect));
  548. report_reset();
  549. /* Initialize b[0] and check b[1]--b[3]. */
  550. kmsan_unpoison_memory((char *)&b[0], 1);
  551. kmsan_check_memory((char *)&b[1], 3);
  552. KUNIT_EXPECT_TRUE(test, report_matches(&expect));
  553. }
  554. static struct kunit_case kmsan_test_cases[] = {
  555. KUNIT_CASE(test_uninit_kmalloc),
  556. KUNIT_CASE(test_init_kmalloc),
  557. KUNIT_CASE(test_init_kzalloc),
  558. KUNIT_CASE(test_uninit_stack_var),
  559. KUNIT_CASE(test_init_stack_var),
  560. KUNIT_CASE(test_params),
  561. KUNIT_CASE(test_uninit_multiple_params),
  562. KUNIT_CASE(test_uninit_kmsan_check_memory),
  563. KUNIT_CASE(test_init_kmsan_vmap_vunmap),
  564. KUNIT_CASE(test_init_vmalloc),
  565. KUNIT_CASE(test_uaf),
  566. KUNIT_CASE(test_percpu_propagate),
  567. KUNIT_CASE(test_printk),
  568. KUNIT_CASE(test_init_memcpy),
  569. KUNIT_CASE(test_memcpy_aligned_to_aligned),
  570. KUNIT_CASE(test_memcpy_aligned_to_unaligned),
  571. KUNIT_CASE(test_memcpy_initialized_gap),
  572. KUNIT_CASE(test_memset16),
  573. KUNIT_CASE(test_memset32),
  574. KUNIT_CASE(test_memset64),
  575. KUNIT_CASE(test_long_origin_chain),
  576. KUNIT_CASE(test_stackdepot_roundtrip),
  577. KUNIT_CASE(test_unpoison_memory),
  578. {},
  579. };
  580. /* ===== End test cases ===== */
  581. static int test_init(struct kunit *test)
  582. {
  583. unsigned long flags;
  584. spin_lock_irqsave(&observed.lock, flags);
  585. observed.header[0] = '\0';
  586. observed.ignore = false;
  587. observed.available = false;
  588. spin_unlock_irqrestore(&observed.lock, flags);
  589. return 0;
  590. }
  591. static void test_exit(struct kunit *test)
  592. {
  593. }
  594. static int orig_panic_on_kmsan;
  595. static int kmsan_suite_init(struct kunit_suite *suite)
  596. {
  597. register_trace_console(probe_console, NULL);
  598. orig_panic_on_kmsan = panic_on_kmsan;
  599. panic_on_kmsan = 0;
  600. return 0;
  601. }
  602. static void kmsan_suite_exit(struct kunit_suite *suite)
  603. {
  604. unregister_trace_console(probe_console, NULL);
  605. tracepoint_synchronize_unregister();
  606. panic_on_kmsan = orig_panic_on_kmsan;
  607. }
  608. static struct kunit_suite kmsan_test_suite = {
  609. .name = "kmsan",
  610. .test_cases = kmsan_test_cases,
  611. .init = test_init,
  612. .exit = test_exit,
  613. .suite_init = kmsan_suite_init,
  614. .suite_exit = kmsan_suite_exit,
  615. };
  616. kunit_test_suites(&kmsan_test_suite);
  617. MODULE_LICENSE("GPL");
  618. MODULE_AUTHOR("Alexander Potapenko <glider@google.com>");