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