1 // REQUIRES: x86_64-target-arch
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
8 uint32_t float_divide_by_zero() {
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
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
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;
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
40 uint32_t vla_bound(_BitInt(37) x
) {
42 // CHECK: {{.*}}bit-int.c:[[#@LINE-1]]:17: runtime error: variable length array bound evaluates to non-positive value
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
52 uint32_t array_bounds() {
54 _BitInt(37) y
= x
[10];
55 // CHECK: {{.*}}bit-int.c:[[#@LINE-1]]:19: runtime error: index 10 out of bounds for type
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
66 uint32_t implicit_integer_sign_change(unsigned _BitInt(37) x
) {
68 // CHECK: {{.*}}bit-int.c:[[#@LINE-1]]:19: runtime error: implicit conversion from type '{{[^']+}}' of value
69 return r
& 0xFFFFFFFF;
72 _BitInt(13) implicit_signed_integer_truncation() {
73 #ifdef __SIZEOF_INT128__
74 _BitInt(73) x
= (_BitInt(73)) ~((~0UL) >> 1);
76 uint32_t x
= 0x7FFFFFFFUL
;
79 // CHECK: {{.*}}bit-int.c:[[#@LINE-1]]:10: runtime error: implicit conversion from type
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
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
94 uint32_t negative_shift1(unsigned _BitInt(37) x
)
95 __attribute__((no_sanitize("memory"))) {
98 // CHECK: {{.*}}bit-int.c:[[#@LINE-1]]:12: runtime error: shift exponent -2 is negative
101 uint32_t negative_shift2(unsigned _BitInt(37) x
)
102 __attribute__((no_sanitize("memory"))) {
105 // CHECK: {{.*}}bit-int.c:[[#@LINE-1]]:12: runtime error: shift exponent -2 is negative
108 uint32_t negative_shift3(unsigned _BitInt(37) x
)
109 __attribute__((no_sanitize("memory"))) {
112 // CHECK: {{.*}}bit-int.c:[[#@LINE-1]]:12: runtime error: shift exponent -2 is negative
115 uint32_t negative_shift4(unsigned _BitInt(37) x
)
116 __attribute__((no_sanitize("memory"))) {
119 // CHECK: {{.*}}bit-int.c:[[#@LINE-1]]:12: runtime error: shift exponent -2 is negative
122 uint32_t negative_shift5(unsigned _BitInt(37) x
)
123 __attribute__((no_sanitize("memory"))) {
124 #ifdef __SIZEOF_INT128__
127 // We cannot check BitInt values > 64 without int128_t support
131 // CHECK: {{.*}}bit-int.c:[[#@LINE-1]]:12: runtime error: shift exponent -2 is negative
134 uint32_t unsigned_integer_overflow() __attribute__((no_sanitize("memory"))) {
135 unsigned _BitInt(37) x
= ~0U;
138 // CHECK: {{.*}}bit-int.c:[[#@LINE-1]]:10: runtime error: implicit conversion from type
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);
146 // CHECK: {{.*}}bit-int.c:[[#@LINE-1]]:10: runtime error: implicit conversion from type
149 int main(int argc
, char **argv
) {
153 implicit_unsigned_integer_truncation() +
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() +
162 (uint32_t)shift_base() +
168 unsigned_integer_overflow() +
169 signed_integer_overflow();
171 printf("%u\n", (uint32_t)(result
& 0xFFFFFFFF));