File: PR32874.c

package info (click to toggle)
llvm-toolchain-15 1%3A15.0.6-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,554,644 kB
  • sloc: cpp: 5,922,452; ansic: 1,012,136; asm: 674,362; python: 191,568; objc: 73,855; f90: 42,327; lisp: 31,913; pascal: 11,973; javascript: 10,144; sh: 9,421; perl: 7,447; ml: 5,527; awk: 3,523; makefile: 2,520; xml: 885; cs: 573; fortran: 567
file content (61 lines) | stat: -rw-r--r-- 2,014 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
// RUN: %clang_cc1 -x c -S -emit-llvm -o - -triple x86_64-apple-darwin10 %s \
// RUN:   -w -fsanitize=signed-integer-overflow,unsigned-integer-overflow,integer-divide-by-zero,float-divide-by-zero \
// RUN:   | FileCheck %s

// CHECK-LABEL: define{{.*}} void @foo
// CHECK-NOT: !nosanitize
void foo(const int *p) {
  // __builtin_prefetch expects its optional arguments to be constant integers.
  // Check that ubsan does not instrument any safe arithmetic performed in
  // operands to __builtin_prefetch. (A clang frontend check should reject
  // unsafe arithmetic in these operands.)

  __builtin_prefetch(p, 0 + 1, 0 + 3);
  __builtin_prefetch(p, 1 - 0, 3 - 0);
  __builtin_prefetch(p, 1 * 1, 1 * 3);
  __builtin_prefetch(p, 1 / 1, 3 / 1);
  __builtin_prefetch(p, 3 % 2, 3 % 1);

  __builtin_prefetch(p, 0U + 1U, 0U + 3U);
  __builtin_prefetch(p, 1U - 0U, 3U - 0U);
  __builtin_prefetch(p, 1U * 1U, 1U * 3U);
  __builtin_prefetch(p, 1U / 1U, 3U / 1U);
  __builtin_prefetch(p, 3U % 2U, 3U % 1U);
}

// CHECK-LABEL: define{{.*}} void @ub_constant_arithmetic
void ub_constant_arithmetic(void) {
  // Check that we still instrument unsafe arithmetic, even if it is known to
  // be unsafe at compile time.

  int INT_MIN = 0xffffffff;
  int INT_MAX = 0x7fffffff;

  // CHECK: call void @__ubsan_handle_add_overflow
  // CHECK: call void @__ubsan_handle_add_overflow
  INT_MAX + 1;
  INT_MAX + -1;

  // CHECK: call void @__ubsan_handle_negate_overflow
  // CHECK: call void @__ubsan_handle_sub_overflow
  -INT_MIN;
  -INT_MAX - 2;

  // CHECK: call void @__ubsan_handle_mul_overflow
  // CHECK: call void @__ubsan_handle_mul_overflow
  INT_MAX * INT_MAX;
  INT_MIN * INT_MIN;

  // CHECK: call void @__ubsan_handle_divrem_overflow
  // CHECK: call void @__ubsan_handle_divrem_overflow
  1 / 0;
  INT_MIN / -1;

  // CHECK: call void @__ubsan_handle_divrem_overflow
  // CHECK: call void @__ubsan_handle_divrem_overflow
  1 % 0;
  INT_MIN % -1;

  // CHECK: call void @__ubsan_handle_divrem_overflow
  1.0 / 0.0;
}