1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * KCSAN short boot-time selftests.
4 *
5 * Copyright (C) 2019, Google LLC.
6 */
7
8 #define pr_fmt(fmt) "kcsan: " fmt
9
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/printk.h>
13 #include <linux/random.h>
14 #include <linux/types.h>
15
16 #include "encoding.h"
17
18 #define ITERS_PER_TEST 2000
19
20 /* Test requirements. */
test_requires(void)21 static bool __init test_requires(void)
22 {
23 /* random should be initialized for the below tests */
24 return prandom_u32() + prandom_u32() != 0;
25 }
26
27 /*
28 * Test watchpoint encode and decode: check that encoding some access's info,
29 * and then subsequent decode preserves the access's info.
30 */
test_encode_decode(void)31 static bool __init test_encode_decode(void)
32 {
33 int i;
34
35 for (i = 0; i < ITERS_PER_TEST; ++i) {
36 size_t size = prandom_u32_max(MAX_ENCODABLE_SIZE) + 1;
37 bool is_write = !!prandom_u32_max(2);
38 unsigned long verif_masked_addr;
39 long encoded_watchpoint;
40 bool verif_is_write;
41 unsigned long addr;
42 size_t verif_size;
43
44 prandom_bytes(&addr, sizeof(addr));
45 if (addr < PAGE_SIZE)
46 addr = PAGE_SIZE;
47
48 if (WARN_ON(!check_encodable(addr, size)))
49 return false;
50
51 encoded_watchpoint = encode_watchpoint(addr, size, is_write);
52
53 /* Check special watchpoints */
54 if (WARN_ON(decode_watchpoint(INVALID_WATCHPOINT, &verif_masked_addr, &verif_size, &verif_is_write)))
55 return false;
56 if (WARN_ON(decode_watchpoint(CONSUMED_WATCHPOINT, &verif_masked_addr, &verif_size, &verif_is_write)))
57 return false;
58
59 /* Check decoding watchpoint returns same data */
60 if (WARN_ON(!decode_watchpoint(encoded_watchpoint, &verif_masked_addr, &verif_size, &verif_is_write)))
61 return false;
62 if (WARN_ON(verif_masked_addr != (addr & WATCHPOINT_ADDR_MASK)))
63 goto fail;
64 if (WARN_ON(verif_size != size))
65 goto fail;
66 if (WARN_ON(is_write != verif_is_write))
67 goto fail;
68
69 continue;
70 fail:
71 pr_err("%s fail: %s %zu bytes @ %lx -> encoded: %lx -> %s %zu bytes @ %lx\n",
72 __func__, is_write ? "write" : "read", size, addr, encoded_watchpoint,
73 verif_is_write ? "write" : "read", verif_size, verif_masked_addr);
74 return false;
75 }
76
77 return true;
78 }
79
80 /* Test access matching function. */
test_matching_access(void)81 static bool __init test_matching_access(void)
82 {
83 if (WARN_ON(!matching_access(10, 1, 10, 1)))
84 return false;
85 if (WARN_ON(!matching_access(10, 2, 11, 1)))
86 return false;
87 if (WARN_ON(!matching_access(10, 1, 9, 2)))
88 return false;
89 if (WARN_ON(matching_access(10, 1, 11, 1)))
90 return false;
91 if (WARN_ON(matching_access(9, 1, 10, 1)))
92 return false;
93
94 /*
95 * An access of size 0 could match another access, as demonstrated here.
96 * Rather than add more comparisons to 'matching_access()', which would
97 * end up in the fast-path for *all* checks, check_access() simply
98 * returns for all accesses of size 0.
99 */
100 if (WARN_ON(!matching_access(8, 8, 12, 0)))
101 return false;
102
103 return true;
104 }
105
kcsan_selftest(void)106 static int __init kcsan_selftest(void)
107 {
108 int passed = 0;
109 int total = 0;
110
111 #define RUN_TEST(do_test) \
112 do { \
113 ++total; \
114 if (do_test()) \
115 ++passed; \
116 else \
117 pr_err("selftest: " #do_test " failed"); \
118 } while (0)
119
120 RUN_TEST(test_requires);
121 RUN_TEST(test_encode_decode);
122 RUN_TEST(test_matching_access);
123
124 pr_info("selftest: %d/%d tests passed\n", passed, total);
125 if (passed != total)
126 panic("selftests failed");
127 return 0;
128 }
129 postcore_initcall(kcsan_selftest);
130