xref: /llvm-project/clang/test/CodeGen/bit-int-ubsan.c (revision d8c2874172ebe70d34c08dea5fcf885283c93562)
1 // REQUIRES: x86-registered-target
2 // RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -Wno-constant-conversion -Wno-array-bounds -Wno-division-by-zero -Wno-shift-negative-value -Wno-shift-count-negative -Wno-int-to-pointer-cast -fsanitize=array-bounds,enum,float-cast-overflow,integer-divide-by-zero,implicit-unsigned-integer-truncation,implicit-signed-integer-truncation,implicit-integer-sign-change,unsigned-integer-overflow,signed-integer-overflow,shift-base,shift-exponent -O0 -emit-llvm -o - %s | FileCheck %s
3 
4 // The runtime test checking the _BitInt ubsan feature is located in compiler-rt/test/ubsan/TestCases/Integer/bit-int.c
5 
6 typedef unsigned int uint32_t;
7 uint32_t float_divide_by_zero() {
8   float f = 1.0f / 0.0f;
9   // CHECK: constant { i16, i16, [8 x i8] } { i16 1, i16 32, [8 x i8] c"'float'\00" }
10   _BitInt(37) r = (_BitInt(37))f;
11   // CHECK: constant { i16, i16, [20 x i8] } { i16 2, i16 13, [20 x i8] c"'_BitInt(37)'\00%\00\00\00\00\00" }
12   return r;
13 }
14 
15 uint32_t integer_divide_by_zero() __attribute__((no_sanitize("memory"))) {
16   _BitInt(37) x = 1 / 0;
17   // CHECK: constant { i16, i16, [32 x i8] } { i16 0, i16 10, [32 x i8] c"'uint32_t' (aka 'unsigned int')\00" }
18   return x;
19 }
20 
21 uint32_t implicit_unsigned_integer_truncation() {
22   unsigned _BitInt(37) x = 2U;
23   x += float_divide_by_zero();
24   x += integer_divide_by_zero();
25   x = x + 0xFFFFFFFFFFFFFFFFULL;
26   // CHECK: constant { i16, i16, [23 x i8] } { i16 0, i16 12, [23 x i8] c"'unsigned _BitInt(37)'\00" }
27   uint32_t r = x & 0xFFFFFFFF;
28   return r;
29 }
30 
31 uint32_t array_bounds() {
32   _BitInt(37) x[4];
33   _BitInt(37) y = x[10];
34   // CHECK: constant { i16, i16, [17 x i8] } { i16 -1, i16 0, [17 x i8] c"'_BitInt(37)[4]'\00" }
35   return (uint32_t)y;
36 }
37 
38 uint32_t float_cast_overflow() {
39   float a = 100000000.0f;
40   _BitInt(7) b = (_BitInt(7))a;
41   // CHECK: constant { i16, i16, [19 x i8] } { i16 2, i16 7, [19 x i8] c"'_BitInt(7)'\00\07\00\00\00\00\00" }
42   return b;
43 }
44 
45 _BitInt(13) implicit_signed_integer_truncation() {
46   _BitInt(73) x = (_BitInt(73)) ~((~0UL) >> 1);
47   return x;
48   // CHECK: constant { i16, i16, [20 x i8] } { i16 2, i16 {{([[:xdigit:]]{2})}}, [20 x i8] c"'_BitInt(73)'\00I\00\00\00\00\00" }
49   // CHECK: constant { i16, i16, [20 x i8] } { i16 2, i16 9, [20 x i8] c"'_BitInt(13)'\00\0D\00\00\00\00\00" }
50 }
51 
52 uint32_t negative_shift1(unsigned _BitInt(37) x)
53     __attribute__((no_sanitize("memory"))) {
54   _BitInt(9) c = -2;
55   return x >> c;
56   // CHECK: constant { i16, i16, [19 x i8] } { i16 2, i16 9, [19 x i8] c"'_BitInt(9)'\00\09\00\00\00\00\00" }
57 }
58 
59 uint32_t negative_shift2(unsigned _BitInt(37) x)
60     __attribute__((no_sanitize("memory"))) {
61   _BitInt(17) c = -2;
62   return x >> c;
63   // CHECK: constant { i16, i16, [20 x i8] } { i16 2, i16 11, [20 x i8] c"'_BitInt(17)'\00\11\00\00\00\00\00" }
64 }
65 
66 uint32_t negative_shift3(unsigned _BitInt(37) x)
67     __attribute__((no_sanitize("memory"))) {
68   _BitInt(34) c = -2;
69   return x >> c;
70   // CHECK: constant { i16, i16, [20 x i8] } { i16 2, i16 13, [20 x i8] c"'_BitInt(34)'\00\22\00\00\00\00\00" }
71 }
72 
73 uint32_t negative_shift5(unsigned _BitInt(37) x)
74     __attribute__((no_sanitize("memory"))) {
75   _BitInt(68) c = -2;
76   return x >> c;
77   // CHECK: constant { i16, i16, [20 x i8] } { i16 2, i16 {{([[:xdigit:]]{2})}}, [20 x i8] c"'_BitInt(68)'\00D\00\00\00\00\00" }
78 }
79