Clang Project

clang_source_code/test/CodeGen/PR32874.c
1// RUN: %clang_cc1 -x c -S -emit-llvm -o - -triple x86_64-apple-darwin10 %s \
2// RUN:   -w -fsanitize=signed-integer-overflow,unsigned-integer-overflow,integer-divide-by-zero,float-divide-by-zero \
3// RUN:   | FileCheck %s
4
5// CHECK-LABEL: define void @foo
6// CHECK-NOT: !nosanitize
7void foo(const int *p) {
8  // __builtin_prefetch expects its optional arguments to be constant integers.
9  // Check that ubsan does not instrument any safe arithmetic performed in
10  // operands to __builtin_prefetch. (A clang frontend check should reject
11  // unsafe arithmetic in these operands.)
12
13  __builtin_prefetch(p, 0 + 1, 0 + 3);
14  __builtin_prefetch(p, 1 - 0, 3 - 0);
15  __builtin_prefetch(p, 1 * 1, 1 * 3);
16  __builtin_prefetch(p, 1 / 1, 3 / 1);
17  __builtin_prefetch(p, 3 % 2, 3 % 1);
18
19  __builtin_prefetch(p, 0U + 1U, 0U + 3U);
20  __builtin_prefetch(p, 1U - 0U, 3U - 0U);
21  __builtin_prefetch(p, 1U * 1U, 1U * 3U);
22  __builtin_prefetch(p, 1U / 1U, 3U / 1U);
23  __builtin_prefetch(p, 3U % 2U, 3U % 1U);
24}
25
26// CHECK-LABEL: define void @ub_constant_arithmetic
27void ub_constant_arithmetic() {
28  // Check that we still instrument unsafe arithmetic, even if it is known to
29  // be unsafe at compile time.
30
31  int INT_MIN = 0xffffffff;
32  int INT_MAX = 0x7fffffff;
33
34  // CHECK: call void @__ubsan_handle_add_overflow
35  // CHECK: call void @__ubsan_handle_add_overflow
36  INT_MAX + 1;
37  INT_MAX + -1;
38
39  // CHECK: call void @__ubsan_handle_negate_overflow
40  // CHECK: call void @__ubsan_handle_sub_overflow
41  -INT_MIN;
42  -INT_MAX - 2;
43
44  // CHECK: call void @__ubsan_handle_mul_overflow
45  // CHECK: call void @__ubsan_handle_mul_overflow
46  INT_MAX * INT_MAX;
47  INT_MIN * INT_MIN;
48
49  // CHECK: call void @__ubsan_handle_divrem_overflow
50  // CHECK: call void @__ubsan_handle_divrem_overflow
51  1 / 0;
52  INT_MIN / -1;
53
54  // CHECK: call void @__ubsan_handle_divrem_overflow
55  // CHECK: call void @__ubsan_handle_divrem_overflow
56  1 % 0;
57  INT_MIN % -1;
58
59  // CHECK: call void @__ubsan_handle_divrem_overflow
60  1.0 / 0.0;
61}
62