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