Linux 6.9-rc1
[linux-2.6-microblaze.git] / lib / test_ubsan.c
index 2062be1..276c121 100644 (file)
@@ -11,6 +11,39 @@ typedef void(*test_ubsan_fp)(void);
                        #config, IS_ENABLED(config) ? "y" : "n");       \
        } while (0)
 
+static void test_ubsan_add_overflow(void)
+{
+       volatile int val = INT_MAX;
+
+       UBSAN_TEST(CONFIG_UBSAN_SIGNED_WRAP);
+       val += 2;
+}
+
+static void test_ubsan_sub_overflow(void)
+{
+       volatile int val = INT_MIN;
+       volatile int val2 = 2;
+
+       UBSAN_TEST(CONFIG_UBSAN_SIGNED_WRAP);
+       val -= val2;
+}
+
+static void test_ubsan_mul_overflow(void)
+{
+       volatile int val = INT_MAX / 2;
+
+       UBSAN_TEST(CONFIG_UBSAN_SIGNED_WRAP);
+       val *= 3;
+}
+
+static void test_ubsan_negate_overflow(void)
+{
+       volatile int val = INT_MIN;
+
+       UBSAN_TEST(CONFIG_UBSAN_SIGNED_WRAP);
+       val = -val;
+}
+
 static void test_ubsan_divrem_overflow(void)
 {
        volatile int val = 16;
@@ -23,8 +56,8 @@ static void test_ubsan_divrem_overflow(void)
 static void test_ubsan_shift_out_of_bounds(void)
 {
        volatile int neg = -1, wrap = 4;
-       int val1 = 10;
-       int val2 = INT_MAX;
+       volatile int val1 = 10;
+       volatile int val2 = INT_MAX;
 
        UBSAN_TEST(CONFIG_UBSAN_SHIFT, "negative exponent");
        val1 <<= neg;
@@ -90,6 +123,10 @@ static void test_ubsan_misaligned_access(void)
 }
 
 static const test_ubsan_fp test_ubsan_array[] = {
+       test_ubsan_add_overflow,
+       test_ubsan_sub_overflow,
+       test_ubsan_mul_overflow,
+       test_ubsan_negate_overflow,
        test_ubsan_shift_out_of_bounds,
        test_ubsan_out_of_bounds,
        test_ubsan_load_invalid_value,