1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/init.h>
3 #include <linux/kernel.h>
4 #include <linux/module.h>
6 typedef void(*test_ubsan_fp
)(void);
8 static void test_ubsan_add_overflow(void)
10 volatile int val
= INT_MAX
;
15 static void test_ubsan_sub_overflow(void)
17 volatile int val
= INT_MIN
;
18 volatile int val2
= 2;
23 static void test_ubsan_mul_overflow(void)
25 volatile int val
= INT_MAX
/ 2;
30 static void test_ubsan_negate_overflow(void)
32 volatile int val
= INT_MIN
;
37 static void test_ubsan_divrem_overflow(void)
39 volatile int val
= 16;
40 volatile int val2
= 0;
45 static void test_ubsan_vla_bound_not_positive(void)
47 volatile int size
= -1;
53 static void test_ubsan_shift_out_of_bounds(void)
55 volatile int val
= -1;
61 static void test_ubsan_out_of_bounds(void)
63 volatile int i
= 4, j
= 5;
69 static void test_ubsan_load_invalid_value(void)
71 volatile char *dst
, *src
;
83 static void test_ubsan_null_ptr_deref(void)
85 volatile int *ptr
= NULL
;
91 static void test_ubsan_misaligned_access(void)
93 volatile char arr
[5] __aligned(4) = {1, 2, 3, 4, 5};
94 volatile int *ptr
, val
= 6;
96 ptr
= (int *)(arr
+ 1);
100 static void test_ubsan_object_size_mismatch(void)
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
;
106 ptr
= (long long *)&val
;
110 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
,
117 test_ubsan_shift_out_of_bounds
,
118 test_ubsan_out_of_bounds
,
119 test_ubsan_load_invalid_value
,
120 //test_ubsan_null_ptr_deref, /* exclude it because there is a crash */
121 test_ubsan_misaligned_access
,
122 test_ubsan_object_size_mismatch
,
125 static int __init
test_ubsan_init(void)
129 for (i
= 0; i
< ARRAY_SIZE(test_ubsan_array
); i
++)
130 test_ubsan_array
[i
]();
132 (void)test_ubsan_null_ptr_deref
; /* to avoid unsed-function warning */
135 module_init(test_ubsan_init
);
137 static void __exit
test_ubsan_exit(void)
141 module_exit(test_ubsan_exit
);
143 MODULE_AUTHOR("Jinbum Park <jinb.park7@gmail.com>");
144 MODULE_LICENSE("GPL v2");