File: ubsan-shift-bitint.c

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (69 lines) | stat: -rw-r--r-- 2,726 bytes parent folder | download | duplicates (7)
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
// RUN: %clang_cc1 %s -O0 -fsanitize=shift-exponent -emit-llvm -std=c2x -triple=x86_64-unknown-linux -o - | FileCheck %s

// Checking that the code generation is using the unextended/untruncated
// exponent values and capping the values accordingly

// CHECK-LABEL: define{{.*}} i32 @test_left_variable
int test_left_variable(unsigned _BitInt(5) b, unsigned _BitInt(2) e) {
  // CHECK: load i8
  // CHECK: [[E_REG:%.+]] = trunc i8 {{.*}} to [[E_SIZE:i2]]
  // CHECK: icmp ule [[E_SIZE]] [[E_REG]], -1,
  return b << e;
}

// CHECK-LABEL: define{{.*}} i32 @test_right_variable
int test_right_variable(unsigned _BitInt(2) b, unsigned _BitInt(3) e) {
  // CHECK: load i8
  // CHECK: [[E_REG:%.+]] = trunc i8 {{.*}} to [[E_SIZE:i3]]
  // CHECK: icmp ule [[E_SIZE]] [[E_REG]], 1,
  return b >> e;
}

// Old code generation would give false positives on left shifts when:
//   value(e) > (width(b) - 1 % 2 ** width(e))
// CHECK-LABEL: define{{.*}} i32 @test_left_literal
int test_left_literal(unsigned _BitInt(5) b) {
  // CHECK-NOT: br i1 false, label %cont, label %handler.shift_out_of_bounds
  // CHECK: br i1 true, label %cont, label %handler.shift_out_of_bounds
  return b << 3uwb;
}

// Old code generation would give false positives on right shifts when:
//   (value(e) % 2 ** width(b)) < width(b)
// CHECK-LABEL: define{{.*}} i32 @test_right_literal
int test_right_literal(unsigned _BitInt(2) b) {
  // CHECK-NOT: br i1 true, label %cont, label %handler.shift_out_of_bounds
  // CHECK: br i1 false, label %cont, label %handler.shift_out_of_bounds
  return b >> 4uwb;
}

// CHECK-LABEL: define{{.*}} i32 @test_signed_left_variable
int test_signed_left_variable(unsigned _BitInt(15) b, _BitInt(2) e) {
  // CHECK: load i8
  // CHECK: [[E_REG:%.+]] = trunc i8 {{.*}} to [[E_SIZE:i2]]
  // CHECK: icmp ule [[E_SIZE]] [[E_REG]], 1,
  return b << e;
}

// CHECK-LABEL: define{{.*}} i32 @test_signed_right_variable
int test_signed_right_variable(unsigned _BitInt(32) b, _BitInt(4) e) {
  // CHECK: load i8
  // CHECK: [[E_REG:%.+]] = trunc i8 {{.*}} to [[E_SIZE:i4]]
  // CHECK: icmp ule [[E_SIZE]] [[E_REG]], 7,
  return b >> e;
}

// CHECK-LABEL: define{{.*}} i32 @test_signed_left_literal
int test_signed_left_literal(unsigned _BitInt(16) b) {
  // CHECK-NOT: br i1 true, label %cont, label %handler.shift_out_of_bounds
  // CHECK: br i1 false, label %cont, label %handler.shift_out_of_bounds
  return b << (_BitInt(4))-2wb;
}

// CHECK-LABEL: define{{.*}} i32 @test_signed_right_literal
int test_signed_right_literal(unsigned _BitInt(16) b) {
  // CHECK-NOT: br i1 true, label %cont, label %handler.shift_out_of_bounds
  // CHECK: br i1 false, label %cont, label %handler.shift_out_of_bounds
  return b >> (_BitInt(4))-8wb;
}