hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/lib/test_ubsan.c
....@@ -5,79 +5,78 @@
55
66 typedef void(*test_ubsan_fp)(void);
77
8
-static void test_ubsan_add_overflow(void)
9
-{
10
- volatile int val = INT_MAX;
11
-
12
- val += 2;
13
-}
14
-
15
-static void test_ubsan_sub_overflow(void)
16
-{
17
- volatile int val = INT_MIN;
18
- volatile int val2 = 2;
19
-
20
- val -= val2;
21
-}
22
-
23
-static void test_ubsan_mul_overflow(void)
24
-{
25
- volatile int val = INT_MAX / 2;
26
-
27
- val *= 3;
28
-}
29
-
30
-static void test_ubsan_negate_overflow(void)
31
-{
32
- volatile int val = INT_MIN;
33
-
34
- val = -val;
35
-}
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)
3613
3714 static void test_ubsan_divrem_overflow(void)
3815 {
3916 volatile int val = 16;
4017 volatile int val2 = 0;
4118
19
+ UBSAN_TEST(CONFIG_UBSAN_DIV_ZERO);
4220 val /= val2;
43
-}
44
-
45
-static void test_ubsan_vla_bound_not_positive(void)
46
-{
47
- volatile int size = -1;
48
- char buf[size];
49
-
50
- (void)buf;
5121 }
5222
5323 static void test_ubsan_shift_out_of_bounds(void)
5424 {
55
- volatile int val = -1;
56
- int val2 = 10;
25
+ volatile int neg = -1, wrap = 4;
26
+ int val1 = 10;
27
+ int val2 = INT_MAX;
5728
58
- val2 <<= val;
29
+ UBSAN_TEST(CONFIG_UBSAN_SHIFT, "negative exponent");
30
+ val1 <<= neg;
31
+
32
+ UBSAN_TEST(CONFIG_UBSAN_SHIFT, "left overflow");
33
+ val2 <<= wrap;
5934 }
6035
6136 static void test_ubsan_out_of_bounds(void)
6237 {
63
- volatile int i = 4, j = 5;
64
- volatile int arr[i];
38
+ volatile int i = 4, j = 5, k = -1;
39
+ volatile char above[4] = { }; /* Protect surrounding memory. */
40
+ volatile int arr[4];
41
+ volatile char below[4] = { }; /* Protect surrounding memory. */
6542
43
+ above[0] = below[0];
44
+
45
+ UBSAN_TEST(CONFIG_UBSAN_BOUNDS, "above");
6646 arr[j] = i;
47
+
48
+ UBSAN_TEST(CONFIG_UBSAN_BOUNDS, "below");
49
+ arr[k] = i;
6750 }
51
+
52
+enum ubsan_test_enum {
53
+ UBSAN_TEST_ZERO = 0,
54
+ UBSAN_TEST_ONE,
55
+ UBSAN_TEST_MAX,
56
+};
6857
6958 static void test_ubsan_load_invalid_value(void)
7059 {
7160 volatile char *dst, *src;
7261 bool val, val2, *ptr;
73
- char c = 4;
62
+ enum ubsan_test_enum eval, eval2, *eptr;
63
+ unsigned char c = 0xff;
7464
65
+ UBSAN_TEST(CONFIG_UBSAN_BOOL, "bool");
7566 dst = (char *)&val;
7667 src = &c;
7768 *dst = *src;
7869
7970 ptr = &val2;
8071 val2 = val;
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;
8180 }
8281
8382 static void test_ubsan_null_ptr_deref(void)
....@@ -85,6 +84,7 @@
8584 volatile int *ptr = NULL;
8685 int val;
8786
87
+ UBSAN_TEST(CONFIG_UBSAN_OBJECT_SIZE);
8888 val = *ptr;
8989 }
9090
....@@ -93,33 +93,22 @@
9393 volatile char arr[5] __aligned(4) = {1, 2, 3, 4, 5};
9494 volatile int *ptr, val = 6;
9595
96
+ UBSAN_TEST(CONFIG_UBSAN_ALIGNMENT);
9697 ptr = (int *)(arr + 1);
9798 *ptr = val;
9899 }
99100
100
-static void test_ubsan_object_size_mismatch(void)
101
-{
102
- /* "((aligned(8)))" helps this not into be misaligned for ptr-access. */
103
- volatile int val __aligned(8) = 4;
104
- volatile long long *ptr, val2;
105
-
106
- ptr = (long long *)&val;
107
- val2 = *ptr;
108
-}
109
-
110101 static const test_ubsan_fp test_ubsan_array[] = {
111
- test_ubsan_add_overflow,
112
- test_ubsan_sub_overflow,
113
- test_ubsan_mul_overflow,
114
- test_ubsan_negate_overflow,
115
- test_ubsan_divrem_overflow,
116
- test_ubsan_vla_bound_not_positive,
117102 test_ubsan_shift_out_of_bounds,
118103 test_ubsan_out_of_bounds,
119104 test_ubsan_load_invalid_value,
120
- //test_ubsan_null_ptr_deref, /* exclude it because there is a crash */
121105 test_ubsan_misaligned_access,
122
- test_ubsan_object_size_mismatch,
106
+};
107
+
108
+/* Excluded because they Oops the module. */
109
+static const test_ubsan_fp skip_ubsan_array[] = {
110
+ test_ubsan_divrem_overflow,
111
+ test_ubsan_null_ptr_deref,
123112 };
124113
125114 static int __init test_ubsan_init(void)
....@@ -129,7 +118,6 @@
129118 for (i = 0; i < ARRAY_SIZE(test_ubsan_array); i++)
130119 test_ubsan_array[i]();
131120
132
- (void)test_ubsan_null_ptr_deref; /* to avoid unsed-function warning */
133121 return 0;
134122 }
135123 module_init(test_ubsan_init);