Commit | Line | Data |
---|---|---|
854686f4 JP |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | #include <linux/init.h> | |
3 | #include <linux/kernel.h> | |
4 | #include <linux/module.h> | |
5 | ||
6 | typedef void(*test_ubsan_fp)(void); | |
7 | ||
4a26f49b KC |
8 | #define UBSAN_TEST(config, ...) do { \ |
9 | pr_info("%s " __VA_ARGS__ "%s(%s=%s)\n", __func__, \ | |
10 | sizeof(" " __VA_ARGS__) > 2 ? " " : "", \ | |
11 | #config, IS_ENABLED(config) ? "y" : "n"); \ | |
12 | } while (0) | |
13 | ||
557f8c58 KC |
14 | static void test_ubsan_add_overflow(void) |
15 | { | |
16 | volatile int val = INT_MAX; | |
17 | ||
18 | UBSAN_TEST(CONFIG_UBSAN_SIGNED_WRAP); | |
19 | val += 2; | |
20 | } | |
21 | ||
22 | static void test_ubsan_sub_overflow(void) | |
23 | { | |
24 | volatile int val = INT_MIN; | |
25 | volatile int val2 = 2; | |
26 | ||
27 | UBSAN_TEST(CONFIG_UBSAN_SIGNED_WRAP); | |
28 | val -= val2; | |
29 | } | |
30 | ||
31 | static void test_ubsan_mul_overflow(void) | |
32 | { | |
33 | volatile int val = INT_MAX / 2; | |
34 | ||
35 | UBSAN_TEST(CONFIG_UBSAN_SIGNED_WRAP); | |
36 | val *= 3; | |
37 | } | |
38 | ||
39 | static void test_ubsan_negate_overflow(void) | |
40 | { | |
41 | volatile int val = INT_MIN; | |
42 | ||
43 | UBSAN_TEST(CONFIG_UBSAN_SIGNED_WRAP); | |
44 | val = -val; | |
45 | } | |
46 | ||
854686f4 JP |
47 | static void test_ubsan_divrem_overflow(void) |
48 | { | |
49 | volatile int val = 16; | |
50 | volatile int val2 = 0; | |
51 | ||
4a26f49b | 52 | UBSAN_TEST(CONFIG_UBSAN_DIV_ZERO); |
854686f4 JP |
53 | val /= val2; |
54 | } | |
55 | ||
854686f4 JP |
56 | static void test_ubsan_shift_out_of_bounds(void) |
57 | { | |
4a26f49b | 58 | volatile int neg = -1, wrap = 4; |
30edbdf9 KC |
59 | volatile int val1 = 10; |
60 | volatile int val2 = INT_MAX; | |
4a26f49b KC |
61 | |
62 | UBSAN_TEST(CONFIG_UBSAN_SHIFT, "negative exponent"); | |
63 | val1 <<= neg; | |
854686f4 | 64 | |
4a26f49b KC |
65 | UBSAN_TEST(CONFIG_UBSAN_SHIFT, "left overflow"); |
66 | val2 <<= wrap; | |
854686f4 JP |
67 | } |
68 | ||
69 | static void test_ubsan_out_of_bounds(void) | |
70 | { | |
4a26f49b KC |
71 | volatile int i = 4, j = 5, k = -1; |
72 | volatile char above[4] = { }; /* Protect surrounding memory. */ | |
9d7ca61b | 73 | volatile int arr[4]; |
4a26f49b | 74 | volatile char below[4] = { }; /* Protect surrounding memory. */ |
854686f4 | 75 | |
4a26f49b KC |
76 | above[0] = below[0]; |
77 | ||
78 | UBSAN_TEST(CONFIG_UBSAN_BOUNDS, "above"); | |
854686f4 | 79 | arr[j] = i; |
4a26f49b KC |
80 | |
81 | UBSAN_TEST(CONFIG_UBSAN_BOUNDS, "below"); | |
82 | arr[k] = i; | |
854686f4 JP |
83 | } |
84 | ||
4a26f49b KC |
85 | enum ubsan_test_enum { |
86 | UBSAN_TEST_ZERO = 0, | |
87 | UBSAN_TEST_ONE, | |
88 | UBSAN_TEST_MAX, | |
89 | }; | |
90 | ||
854686f4 JP |
91 | static void test_ubsan_load_invalid_value(void) |
92 | { | |
93 | volatile char *dst, *src; | |
94 | bool val, val2, *ptr; | |
4a26f49b KC |
95 | enum ubsan_test_enum eval, eval2, *eptr; |
96 | unsigned char c = 0xff; | |
854686f4 | 97 | |
4a26f49b | 98 | UBSAN_TEST(CONFIG_UBSAN_BOOL, "bool"); |
854686f4 JP |
99 | dst = (char *)&val; |
100 | src = &c; | |
101 | *dst = *src; | |
102 | ||
103 | ptr = &val2; | |
104 | val2 = val; | |
4a26f49b KC |
105 | |
106 | UBSAN_TEST(CONFIG_UBSAN_ENUM, "enum"); | |
107 | dst = (char *)&eval; | |
108 | src = &c; | |
109 | *dst = *src; | |
110 | ||
111 | eptr = &eval2; | |
112 | eval2 = eval; | |
854686f4 JP |
113 | } |
114 | ||
31750600 | 115 | static void test_ubsan_misaligned_access(void) |
854686f4 JP |
116 | { |
117 | volatile char arr[5] __aligned(4) = {1, 2, 3, 4, 5}; | |
118 | volatile int *ptr, val = 6; | |
119 | ||
4a26f49b | 120 | UBSAN_TEST(CONFIG_UBSAN_ALIGNMENT); |
854686f4 JP |
121 | ptr = (int *)(arr + 1); |
122 | *ptr = val; | |
123 | } | |
124 | ||
854686f4 | 125 | static const test_ubsan_fp test_ubsan_array[] = { |
557f8c58 KC |
126 | test_ubsan_add_overflow, |
127 | test_ubsan_sub_overflow, | |
128 | test_ubsan_mul_overflow, | |
129 | test_ubsan_negate_overflow, | |
854686f4 JP |
130 | test_ubsan_shift_out_of_bounds, |
131 | test_ubsan_out_of_bounds, | |
132 | test_ubsan_load_invalid_value, | |
854686f4 | 133 | test_ubsan_misaligned_access, |
854686f4 JP |
134 | }; |
135 | ||
4a26f49b | 136 | /* Excluded because they Oops the module. */ |
bbda3ba6 | 137 | static __used const test_ubsan_fp skip_ubsan_array[] = { |
4a26f49b | 138 | test_ubsan_divrem_overflow, |
4a26f49b KC |
139 | }; |
140 | ||
854686f4 JP |
141 | static int __init test_ubsan_init(void) |
142 | { | |
143 | unsigned int i; | |
144 | ||
145 | for (i = 0; i < ARRAY_SIZE(test_ubsan_array); i++) | |
146 | test_ubsan_array[i](); | |
147 | ||
854686f4 JP |
148 | return 0; |
149 | } | |
150 | module_init(test_ubsan_init); | |
151 | ||
152 | static void __exit test_ubsan_exit(void) | |
153 | { | |
154 | /* do nothing */ | |
155 | } | |
156 | module_exit(test_ubsan_exit); | |
157 | ||
158 | MODULE_AUTHOR("Jinbum Park <jinb.park7@gmail.com>"); | |
2ec83987 | 159 | MODULE_DESCRIPTION("UBSAN unit test"); |
854686f4 | 160 | MODULE_LICENSE("GPL v2"); |