xref: /llvm-project/clang/test/CodeGen/bit-int-ubsan.c (revision d8c2874172ebe70d34c08dea5fcf885283c93562)
175cb7de4Searnol // REQUIRES: x86-registered-target
2*d8c28741Searnol // 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
375cb7de4Searnol 
475cb7de4Searnol // The runtime test checking the _BitInt ubsan feature is located in compiler-rt/test/ubsan/TestCases/Integer/bit-int.c
575cb7de4Searnol 
67493ea22Searnol typedef unsigned int uint32_t;
775cb7de4Searnol uint32_t float_divide_by_zero() {
875cb7de4Searnol   float f = 1.0f / 0.0f;
975cb7de4Searnol   // CHECK: constant { i16, i16, [8 x i8] } { i16 1, i16 32, [8 x i8] c"'float'\00" }
1075cb7de4Searnol   _BitInt(37) r = (_BitInt(37))f;
1175cb7de4Searnol   // CHECK: constant { i16, i16, [20 x i8] } { i16 2, i16 13, [20 x i8] c"'_BitInt(37)'\00%\00\00\00\00\00" }
1275cb7de4Searnol   return r;
1375cb7de4Searnol }
1475cb7de4Searnol 
1575cb7de4Searnol uint32_t integer_divide_by_zero() __attribute__((no_sanitize("memory"))) {
1675cb7de4Searnol   _BitInt(37) x = 1 / 0;
1775cb7de4Searnol   // CHECK: constant { i16, i16, [32 x i8] } { i16 0, i16 10, [32 x i8] c"'uint32_t' (aka 'unsigned int')\00" }
1875cb7de4Searnol   return x;
1975cb7de4Searnol }
2075cb7de4Searnol 
2175cb7de4Searnol uint32_t implicit_unsigned_integer_truncation() {
2275cb7de4Searnol   unsigned _BitInt(37) x = 2U;
2375cb7de4Searnol   x += float_divide_by_zero();
2475cb7de4Searnol   x += integer_divide_by_zero();
2575cb7de4Searnol   x = x + 0xFFFFFFFFFFFFFFFFULL;
2675cb7de4Searnol   // CHECK: constant { i16, i16, [23 x i8] } { i16 0, i16 12, [23 x i8] c"'unsigned _BitInt(37)'\00" }
2775cb7de4Searnol   uint32_t r = x & 0xFFFFFFFF;
2875cb7de4Searnol   return r;
2975cb7de4Searnol }
3075cb7de4Searnol 
3175cb7de4Searnol uint32_t array_bounds() {
3275cb7de4Searnol   _BitInt(37) x[4];
3375cb7de4Searnol   _BitInt(37) y = x[10];
3475cb7de4Searnol   // CHECK: constant { i16, i16, [17 x i8] } { i16 -1, i16 0, [17 x i8] c"'_BitInt(37)[4]'\00" }
3575cb7de4Searnol   return (uint32_t)y;
3675cb7de4Searnol }
3775cb7de4Searnol 
3875cb7de4Searnol uint32_t float_cast_overflow() {
3975cb7de4Searnol   float a = 100000000.0f;
4075cb7de4Searnol   _BitInt(7) b = (_BitInt(7))a;
4175cb7de4Searnol   // CHECK: constant { i16, i16, [19 x i8] } { i16 2, i16 7, [19 x i8] c"'_BitInt(7)'\00\07\00\00\00\00\00" }
4275cb7de4Searnol   return b;
4375cb7de4Searnol }
4475cb7de4Searnol 
4575cb7de4Searnol _BitInt(13) implicit_signed_integer_truncation() {
4675cb7de4Searnol   _BitInt(73) x = (_BitInt(73)) ~((~0UL) >> 1);
4775cb7de4Searnol   return x;
4875cb7de4Searnol   // CHECK: constant { i16, i16, [20 x i8] } { i16 2, i16 {{([[:xdigit:]]{2})}}, [20 x i8] c"'_BitInt(73)'\00I\00\00\00\00\00" }
4975cb7de4Searnol   // CHECK: constant { i16, i16, [20 x i8] } { i16 2, i16 9, [20 x i8] c"'_BitInt(13)'\00\0D\00\00\00\00\00" }
5075cb7de4Searnol }
5175cb7de4Searnol 
5275cb7de4Searnol uint32_t negative_shift1(unsigned _BitInt(37) x)
5375cb7de4Searnol     __attribute__((no_sanitize("memory"))) {
5475cb7de4Searnol   _BitInt(9) c = -2;
5575cb7de4Searnol   return x >> c;
5675cb7de4Searnol   // CHECK: constant { i16, i16, [19 x i8] } { i16 2, i16 9, [19 x i8] c"'_BitInt(9)'\00\09\00\00\00\00\00" }
5775cb7de4Searnol }
5875cb7de4Searnol 
5975cb7de4Searnol uint32_t negative_shift2(unsigned _BitInt(37) x)
6075cb7de4Searnol     __attribute__((no_sanitize("memory"))) {
6175cb7de4Searnol   _BitInt(17) c = -2;
6275cb7de4Searnol   return x >> c;
6375cb7de4Searnol   // CHECK: constant { i16, i16, [20 x i8] } { i16 2, i16 11, [20 x i8] c"'_BitInt(17)'\00\11\00\00\00\00\00" }
6475cb7de4Searnol }
6575cb7de4Searnol 
6675cb7de4Searnol uint32_t negative_shift3(unsigned _BitInt(37) x)
6775cb7de4Searnol     __attribute__((no_sanitize("memory"))) {
6875cb7de4Searnol   _BitInt(34) c = -2;
6975cb7de4Searnol   return x >> c;
7075cb7de4Searnol   // CHECK: constant { i16, i16, [20 x i8] } { i16 2, i16 13, [20 x i8] c"'_BitInt(34)'\00\22\00\00\00\00\00" }
7175cb7de4Searnol }
7275cb7de4Searnol 
7375cb7de4Searnol uint32_t negative_shift5(unsigned _BitInt(37) x)
7475cb7de4Searnol     __attribute__((no_sanitize("memory"))) {
7575cb7de4Searnol   _BitInt(68) c = -2;
7675cb7de4Searnol   return x >> c;
7775cb7de4Searnol   // CHECK: constant { i16, i16, [20 x i8] } { i16 2, i16 {{([[:xdigit:]]{2})}}, [20 x i8] c"'_BitInt(68)'\00D\00\00\00\00\00" }
7875cb7de4Searnol }
79