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 | ||
854686f4 JP |
14 | static void test_ubsan_divrem_overflow(void) |
15 | { | |
16 | volatile int val = 16; | |
17 | volatile int val2 = 0; | |
18 | ||
4a26f49b | 19 | UBSAN_TEST(CONFIG_UBSAN_DIV_ZERO); |
854686f4 JP |
20 | val /= val2; |
21 | } | |
22 | ||
854686f4 JP |
23 | static void test_ubsan_shift_out_of_bounds(void) |
24 | { | |
4a26f49b KC |
25 | volatile int neg = -1, wrap = 4; |
26 | int val1 = 10; | |
27 | int val2 = INT_MAX; | |
28 | ||
29 | UBSAN_TEST(CONFIG_UBSAN_SHIFT, "negative exponent"); | |
30 | val1 <<= neg; | |
854686f4 | 31 | |
4a26f49b KC |
32 | UBSAN_TEST(CONFIG_UBSAN_SHIFT, "left overflow"); |
33 | val2 <<= wrap; | |
854686f4 JP |
34 | } |
35 | ||
36 | static void test_ubsan_out_of_bounds(void) | |
37 | { | |
4a26f49b KC |
38 | volatile int i = 4, j = 5, k = -1; |
39 | volatile char above[4] = { }; /* Protect surrounding memory. */ | |
9d7ca61b | 40 | volatile int arr[4]; |
4a26f49b | 41 | volatile char below[4] = { }; /* Protect surrounding memory. */ |
854686f4 | 42 | |
4a26f49b KC |
43 | above[0] = below[0]; |
44 | ||
45 | UBSAN_TEST(CONFIG_UBSAN_BOUNDS, "above"); | |
854686f4 | 46 | arr[j] = i; |
4a26f49b KC |
47 | |
48 | UBSAN_TEST(CONFIG_UBSAN_BOUNDS, "below"); | |
49 | arr[k] = i; | |
854686f4 JP |
50 | } |
51 | ||
4a26f49b KC |
52 | enum ubsan_test_enum { |
53 | UBSAN_TEST_ZERO = 0, | |
54 | UBSAN_TEST_ONE, | |
55 | UBSAN_TEST_MAX, | |
56 | }; | |
57 | ||
854686f4 JP |
58 | static void test_ubsan_load_invalid_value(void) |
59 | { | |
60 | volatile char *dst, *src; | |
61 | bool val, val2, *ptr; | |
4a26f49b KC |
62 | enum ubsan_test_enum eval, eval2, *eptr; |
63 | unsigned char c = 0xff; | |
854686f4 | 64 | |
4a26f49b | 65 | UBSAN_TEST(CONFIG_UBSAN_BOOL, "bool"); |
854686f4 JP |
66 | dst = (char *)&val; |
67 | src = &c; | |
68 | *dst = *src; | |
69 | ||
70 | ptr = &val2; | |
71 | val2 = val; | |
4a26f49b KC |
72 | |
73 | UBSAN_TEST(CONFIG_UBSAN_ENUM, "enum"); | |
74 | dst = (char *)&eval; | |
75 | src = &c; | |
76 | *dst = *src; | |
77 | ||
78 | eptr = &eval2; | |
79 | eval2 = eval; | |
854686f4 JP |
80 | } |
81 | ||
31750600 | 82 | static void test_ubsan_misaligned_access(void) |
854686f4 JP |
83 | { |
84 | volatile char arr[5] __aligned(4) = {1, 2, 3, 4, 5}; | |
85 | volatile int *ptr, val = 6; | |
86 | ||
4a26f49b | 87 | UBSAN_TEST(CONFIG_UBSAN_ALIGNMENT); |
854686f4 JP |
88 | ptr = (int *)(arr + 1); |
89 | *ptr = val; | |
90 | } | |
91 | ||
854686f4 | 92 | static const test_ubsan_fp test_ubsan_array[] = { |
854686f4 JP |
93 | test_ubsan_shift_out_of_bounds, |
94 | test_ubsan_out_of_bounds, | |
95 | test_ubsan_load_invalid_value, | |
854686f4 | 96 | test_ubsan_misaligned_access, |
854686f4 JP |
97 | }; |
98 | ||
4a26f49b KC |
99 | /* Excluded because they Oops the module. */ |
100 | static const test_ubsan_fp skip_ubsan_array[] = { | |
101 | test_ubsan_divrem_overflow, | |
4a26f49b KC |
102 | }; |
103 | ||
854686f4 JP |
104 | static int __init test_ubsan_init(void) |
105 | { | |
106 | unsigned int i; | |
107 | ||
108 | for (i = 0; i < ARRAY_SIZE(test_ubsan_array); i++) | |
109 | test_ubsan_array[i](); | |
110 | ||
854686f4 JP |
111 | return 0; |
112 | } | |
113 | module_init(test_ubsan_init); | |
114 | ||
115 | static void __exit test_ubsan_exit(void) | |
116 | { | |
117 | /* do nothing */ | |
118 | } | |
119 | module_exit(test_ubsan_exit); | |
120 | ||
121 | MODULE_AUTHOR("Jinbum Park <jinb.park7@gmail.com>"); | |
122 | MODULE_LICENSE("GPL v2"); |