xref: /llvm-project/compiler-rt/test/ubsan/TestCases/Integer/bit-int.c (revision a567d4598755219e535eb04d21b43c3624526714)
1 // REQUIRES: x86_64-target-arch
2 // REQUIRES: !windows
3 // RUN: %clang -Wno-constant-conversion -Wno-array-bounds -Wno-division-by-zero -Wno-shift-negative-value -Wno-shift-count-negative -Wno-int-to-pointer-cast -O0 -fsanitize=array-bounds,float-cast-overflow,implicit-integer-sign-change,implicit-signed-integer-truncation,implicit-unsigned-integer-truncation,integer-divide-by-zero,pointer-overflow,shift-base,shift-exponent,signed-integer-overflow,unsigned-integer-overflow,unsigned-shift-base,vla-bound %s -o %t1 && %run %t1 2>&1 | FileCheck %s
4 
5 #include <stdint.h>
6 #include <stdio.h>
7 
8 uint32_t float_divide_by_zero() {
9   float f = 1.0f / 0.0f;
10   _BitInt(37) r = (_BitInt(37))f;
11   // CHECK: {{.*}}bit-int.c:[[#@LINE-1]]:19: runtime error: inf is outside the range of representable values of type
12   return r;
13 }
14 
15 uint32_t integer_divide_by_zero() __attribute__((no_sanitize("memory"))) {
16   _BitInt(37) x = 1 / 0;
17   // CHECK: {{.*}}bit-int.c:[[#@LINE-1]]:21: runtime error: division by zero
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: {{.*}}bit-int.c:[[#@LINE-1]]:9: runtime error: unsigned integer overflow:
27   uint32_t r = x & 0xFFFFFFFF;
28   return r;
29 }
30 
31 uint32_t pointer_overflow() __attribute__((no_sanitize("address"))) {
32   _BitInt(37) *x = (_BitInt(37) *)1;
33   _BitInt(37) *y = x - 1;
34   // CHECK: {{.*}}bit-int.c:[[#@LINE-1]]:22: runtime error: pointer index expression with base
35   uint32_t r = *(_BitInt(37) *)&y;
36   // CHECK: {{.*}}bit-int.c:[[#@LINE-1]]:16: runtime error: implicit conversion from type
37   return r;
38 }
39 
40 uint32_t vla_bound(_BitInt(37) x) {
41   _BitInt(37) a[x - 1];
42   // CHECK: {{.*}}bit-int.c:[[#@LINE-1]]:17: runtime error: variable length array bound evaluates to non-positive value
43   return 0;
44 }
45 
46 uint32_t unsigned_shift_base() {
47   unsigned _BitInt(37) x = ~0U << 1;
48   // CHECK: {{.*}}bit-int.c:[[#@LINE-1]]:32: runtime error: left shift of 4294967295 by 1 places cannot be represented in type
49   return x;
50 }
51 
52 uint32_t array_bounds() {
53   _BitInt(37) x[4];
54   _BitInt(37) y = x[10];
55   // CHECK: {{.*}}bit-int.c:[[#@LINE-1]]:19: runtime error: index 10 out of bounds for type
56   return (uint32_t)y;
57 }
58 
59 uint32_t float_cast_overflow() {
60   float a = 100000000.0f;
61   _BitInt(7) b = (_BitInt(7))a;
62   // CHECK: {{.*}}bit-int.c:[[#@LINE-1]]:18: runtime error: 1e+08 is outside the range of representable values of type
63   return b;
64 }
65 
66 uint32_t implicit_integer_sign_change(unsigned _BitInt(37) x) {
67   _BitInt(37) r = x;
68   // CHECK: {{.*}}bit-int.c:[[#@LINE-1]]:19: runtime error: implicit conversion from type '{{[^']+}}' of value
69   return r & 0xFFFFFFFF;
70 }
71 
72 _BitInt(13) implicit_signed_integer_truncation() {
73 #ifdef __SIZEOF_INT128__
74   _BitInt(73) x = (_BitInt(73)) ~((~0UL) >> 1);
75 #else
76   uint32_t x = 0x7FFFFFFFUL;
77 #endif
78   return x;
79   // CHECK: {{.*}}bit-int.c:[[#@LINE-1]]:10: runtime error: implicit conversion from type
80 }
81 
82 _BitInt(37) shift_exponent() __attribute__((no_sanitize("memory"))) {
83   _BitInt(37) x = 1 << (-1);
84   // CHECK: {{.*}}bit-int.c:[[#@LINE-1]]:21: runtime error: shift exponent -1 is negative
85   return x;
86 }
87 
88 _BitInt(37) shift_base() __attribute__((no_sanitize("memory"))) {
89   _BitInt(37) x = (-1) << 1;
90   // CHECK: {{.*}}bit-int.c:[[#@LINE-1]]:24: runtime error: left shift of negative value -1
91   return x;
92 }
93 
94 uint32_t negative_shift1(unsigned _BitInt(37) x)
95     __attribute__((no_sanitize("memory"))) {
96   _BitInt(9) c = -2;
97   return x >> c;
98   // CHECK: {{.*}}bit-int.c:[[#@LINE-1]]:12: runtime error: shift exponent -2 is negative
99 }
100 
101 uint32_t negative_shift2(unsigned _BitInt(37) x)
102     __attribute__((no_sanitize("memory"))) {
103   _BitInt(17) c = -2;
104   return x >> c;
105   // CHECK: {{.*}}bit-int.c:[[#@LINE-1]]:12: runtime error: shift exponent -2 is negative
106 }
107 
108 uint32_t negative_shift3(unsigned _BitInt(37) x)
109     __attribute__((no_sanitize("memory"))) {
110   _BitInt(34) c = -2;
111   return x >> c;
112   // CHECK: {{.*}}bit-int.c:[[#@LINE-1]]:12: runtime error: shift exponent -2 is negative
113 }
114 
115 uint32_t negative_shift4(unsigned _BitInt(37) x)
116     __attribute__((no_sanitize("memory"))) {
117   int64_t c = -2;
118   return x >> c;
119   // CHECK: {{.*}}bit-int.c:[[#@LINE-1]]:12: runtime error: shift exponent -2 is negative
120 }
121 
122 uint32_t negative_shift5(unsigned _BitInt(37) x)
123     __attribute__((no_sanitize("memory"))) {
124 #ifdef __SIZEOF_INT128__
125   _BitInt(68) c = -2;
126 #else
127   // We cannot check BitInt values > 64 without int128_t support
128   _BitInt(48) c = -2;
129 #endif
130   return x >> c;
131   // CHECK: {{.*}}bit-int.c:[[#@LINE-1]]:12: runtime error: shift exponent -2 is negative
132 }
133 
134 uint32_t unsigned_integer_overflow() __attribute__((no_sanitize("memory"))) {
135   unsigned _BitInt(37) x = ~0U;
136   ++x;
137   return x;
138   // CHECK: {{.*}}bit-int.c:[[#@LINE-1]]:10: runtime error: implicit conversion from type
139 }
140 
141 // In this test no run-time overflow expected, so no diagnostics here, but should be a conversion error from the negative number on return.
142 uint32_t signed_integer_overflow() __attribute__((no_sanitize("memory"))) {
143   _BitInt(37) x = (_BitInt(37)) ~((0x8FFFFFFFFFFFFFFFULL) >> 1);
144   --x;
145   return x;
146   // CHECK: {{.*}}bit-int.c:[[#@LINE-1]]:10: runtime error: implicit conversion from type
147 }
148 
149 int main(int argc, char **argv) {
150   // clang-format off
151   uint64_t result =
152       1ULL +
153       implicit_unsigned_integer_truncation() +
154       pointer_overflow() +
155       vla_bound(argc) +
156       unsigned_shift_base() +
157       (uint32_t)array_bounds() +
158       float_cast_overflow() +
159       implicit_integer_sign_change((unsigned _BitInt(37))(argc - 2)) +
160       (uint64_t)implicit_signed_integer_truncation() +
161       shift_exponent() +
162       (uint32_t)shift_base() +
163       negative_shift1(5) +
164       negative_shift2(5) +
165       negative_shift3(5) +
166       negative_shift4(5) +
167       negative_shift5(5) +
168       unsigned_integer_overflow() +
169       signed_integer_overflow();
170   // clang-format on
171   printf("%u\n", (uint32_t)(result & 0xFFFFFFFF));
172 }
173