File: bit-int-ubsan.c

package info (click to toggle)
llvm-toolchain-20 1%3A20.1.6-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,111,304 kB
  • sloc: cpp: 7,438,677; ansic: 1,393,822; asm: 1,012,926; python: 241,650; f90: 86,635; objc: 75,479; lisp: 42,144; pascal: 17,286; sh: 10,027; ml: 5,082; perl: 4,730; awk: 3,523; makefile: 3,349; javascript: 2,251; xml: 892; fortran: 672
file content (78 lines) | stat: -rw-r--r-- 3,290 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// REQUIRES: x86-registered-target
// 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

// The runtime test checking the _BitInt ubsan feature is located in compiler-rt/test/ubsan/TestCases/Integer/bit-int.c

typedef unsigned int uint32_t;
uint32_t float_divide_by_zero() {
  float f = 1.0f / 0.0f;
  // CHECK: constant { i16, i16, [8 x i8] } { i16 1, i16 32, [8 x i8] c"'float'\00" }
  _BitInt(37) r = (_BitInt(37))f;
  // CHECK: constant { i16, i16, [20 x i8] } { i16 2, i16 13, [20 x i8] c"'_BitInt(37)'\00%\00\00\00\00\00" }
  return r;
}

uint32_t integer_divide_by_zero() __attribute__((no_sanitize("memory"))) {
  _BitInt(37) x = 1 / 0;
  // CHECK: constant { i16, i16, [32 x i8] } { i16 0, i16 10, [32 x i8] c"'uint32_t' (aka 'unsigned int')\00" }
  return x;
}

uint32_t implicit_unsigned_integer_truncation() {
  unsigned _BitInt(37) x = 2U;
  x += float_divide_by_zero();
  x += integer_divide_by_zero();
  x = x + 0xFFFFFFFFFFFFFFFFULL;
  // CHECK: constant { i16, i16, [23 x i8] } { i16 0, i16 12, [23 x i8] c"'unsigned _BitInt(37)'\00" }
  uint32_t r = x & 0xFFFFFFFF;
  return r;
}

uint32_t array_bounds() {
  _BitInt(37) x[4];
  _BitInt(37) y = x[10];
  // CHECK: constant { i16, i16, [17 x i8] } { i16 -1, i16 0, [17 x i8] c"'_BitInt(37)[4]'\00" }
  return (uint32_t)y;
}

uint32_t float_cast_overflow() {
  float a = 100000000.0f;
  _BitInt(7) b = (_BitInt(7))a;
  // CHECK: constant { i16, i16, [19 x i8] } { i16 2, i16 7, [19 x i8] c"'_BitInt(7)'\00\07\00\00\00\00\00" }
  return b;
}

_BitInt(13) implicit_signed_integer_truncation() {
  _BitInt(73) x = (_BitInt(73)) ~((~0UL) >> 1);
  return x;
  // CHECK: constant { i16, i16, [20 x i8] } { i16 2, i16 {{([[:xdigit:]]{2})}}, [20 x i8] c"'_BitInt(73)'\00I\00\00\00\00\00" }
  // CHECK: constant { i16, i16, [20 x i8] } { i16 2, i16 9, [20 x i8] c"'_BitInt(13)'\00\0D\00\00\00\00\00" }
}

uint32_t negative_shift1(unsigned _BitInt(37) x)
    __attribute__((no_sanitize("memory"))) {
  _BitInt(9) c = -2;
  return x >> c;
  // CHECK: constant { i16, i16, [19 x i8] } { i16 2, i16 9, [19 x i8] c"'_BitInt(9)'\00\09\00\00\00\00\00" }
}

uint32_t negative_shift2(unsigned _BitInt(37) x)
    __attribute__((no_sanitize("memory"))) {
  _BitInt(17) c = -2;
  return x >> c;
  // CHECK: constant { i16, i16, [20 x i8] } { i16 2, i16 11, [20 x i8] c"'_BitInt(17)'\00\11\00\00\00\00\00" }
}

uint32_t negative_shift3(unsigned _BitInt(37) x)
    __attribute__((no_sanitize("memory"))) {
  _BitInt(34) c = -2;
  return x >> c;
  // CHECK: constant { i16, i16, [20 x i8] } { i16 2, i16 13, [20 x i8] c"'_BitInt(34)'\00\22\00\00\00\00\00" }
}

uint32_t negative_shift5(unsigned _BitInt(37) x)
    __attribute__((no_sanitize("memory"))) {
  _BitInt(68) c = -2;
  return x >> c;
  // CHECK: constant { i16, i16, [20 x i8] } { i16 2, i16 {{([[:xdigit:]]{2})}}, [20 x i8] c"'_BitInt(68)'\00D\00\00\00\00\00" }
}