1 // RUN: %clang_cc1 -x c -triple x86_64-apple-darwin10 -w -emit-llvm -o - %s -fsanitize=pointer-overflow | FileCheck %s --check-prefixes=CHECK,CHECK-C
2 // RUN: %clang_cc1 -x c++ -triple x86_64-apple-darwin10 -w -emit-llvm -o - %s -fsanitize=pointer-overflow | FileCheck %s --check-prefixes=CHECK,CHECK-CPP
8 // CHECK-LABEL: define{{.*}} void @fixed_len_array
9 void fixed_len_array(int k
) {
10 // CHECK: getelementptr inbounds [10 x [10 x i32]], ptr [[ARR:%.*]], i64 0, i64 [[IDXPROM:%.*]]
11 // CHECK-NEXT: [[SMUL:%.*]] = call { i64, i1 } @llvm.smul.with.overflow.i64(i64 40, i64 [[IDXPROM]]), !nosanitize
12 // CHECK-NEXT: [[SMULOFLOW:%.*]] = extractvalue { i64, i1 } [[SMUL]], 1, !nosanitize
13 // CHECK-NEXT: [[OR:%.+]] = or i1 [[SMULOFLOW]], false, !nosanitize
14 // CHECK-NEXT: [[SMULVAL:%.*]] = extractvalue { i64, i1 } [[SMUL]], 0, !nosanitize
15 // CHECK-NEXT: [[BASE:%.*]] = ptrtoint ptr [[ARR]] to i64, !nosanitize
16 // CHECK-NEXT: [[COMPGEP:%.*]] = add i64 [[BASE]], [[SMULVAL]], !nosanitize
17 // CHECK: call void @__ubsan_handle_pointer_overflow{{.*}}, i64 [[BASE]], i64 [[COMPGEP]]){{.*}}, !nosanitize
19 // CHECK: getelementptr inbounds [10 x i32], ptr {{.*}}, i64 0, i64 [[IDXPROM1:%.*]]
20 // CHECK-NEXT: @llvm.smul.with.overflow.i64(i64 4, i64 [[IDXPROM1]]), !nosanitize
21 // CHECK: call void @__ubsan_handle_pointer_overflow{{.*}}
27 // CHECK-LABEL: define{{.*}} void @variable_len_array
28 void variable_len_array(int n
, int k
) {
29 // CHECK: getelementptr inbounds i32, ptr {{.*}}, i64 [[IDXPROM:%.*]]
30 // CHECK-NEXT: @llvm.smul.with.overflow.i64(i64 4, i64 [[IDXPROM]]), !nosanitize
31 // CHECK: call void @__ubsan_handle_pointer_overflow{{.*}}
33 // CHECK: getelementptr inbounds i32, ptr {{.*}}, i64 [[IDXPROM1:%.*]]
34 // CHECK-NEXT: @llvm.smul.with.overflow.i64(i64 4, i64 [[IDXPROM1]]), !nosanitize
35 // CHECK: call void @__ubsan_handle_pointer_overflow{{.*}}
41 // CHECK-LABEL: define{{.*}} void @pointer_array
42 void pointer_array(int **arr
, int k
) {
43 // CHECK: @llvm.smul.with.overflow.i64(i64 8, i64 {{.*}}), !nosanitize
44 // CHECK: call void @__ubsan_handle_pointer_overflow{{.*}}
46 // CHECK: @llvm.smul.with.overflow.i64(i64 4, i64 {{.*}}), !nosanitize
47 // CHECK: call void @__ubsan_handle_pointer_overflow{{.*}}
52 // CHECK-LABEL: define{{.*}} void @pointer_array_unsigned_indices
53 void pointer_array_unsigned_indices(int **arr
, unsigned k
) {
56 // CHECK: call void @__ubsan_handle_pointer_overflow{{.*}}
59 // CHECK: call void @__ubsan_handle_pointer_overflow{{.*}}
63 // CHECK-LABEL: define{{.*}} void @pointer_array_mixed_indices
64 void pointer_array_mixed_indices(int **arr
, int i
, unsigned j
) {
66 // CHECK: call void @__ubsan_handle_pointer_overflow{{.*}}
68 // CHECK: call void @__ubsan_handle_pointer_overflow{{.*}}
81 // TODO: Currently, structure GEPs are not checked, so there are several
82 // potentially unsafe GEPs here which we don't instrument.
84 // CHECK-LABEL: define{{.*}} void @struct_index
85 void struct_index(struct S1
*p
) {
86 // CHECK: getelementptr inbounds %struct.S1, ptr [[P:%.*]], i64 10
87 // CHECK-NEXT: [[BASE:%.*]] = ptrtoint ptr [[P]] to i64, !nosanitize
88 // CHECK-NEXT: [[COMPGEP:%.*]] = add i64 [[BASE]], 240, !nosanitize
90 // CHECK: @__ubsan_handle_pointer_overflow{{.*}} i64 [[BASE]], i64 [[COMPGEP]]) {{.*}}, !nosanitize
92 // CHECK-NOT: @__ubsan_handle_pointer_overflow
94 p
->arr
[10].u
.link
->u
.leaf
;
97 typedef void (*funcptr_t
)(void);
99 // CHECK-LABEL: define{{.*}} void @function_pointer_arith
100 void function_pointer_arith(funcptr_t
*p
, int k
) {
101 // CHECK: add i64 {{.*}}, 8, !nosanitize
103 // CHECK: @__ubsan_handle_pointer_overflow{{.*}}
106 // CHECK: @llvm.smul.with.overflow.i64(i64 8, i64 {{.*}}), !nosanitize
108 // CHECK: call void @__ubsan_handle_pointer_overflow{{.*}}
112 // CHECK-LABEL: define{{.*}} void @dont_emit_checks_for_no_op_GEPs
113 // CHECK-C: __ubsan_handle_pointer_overflow
114 // CHECK-CPP-NOT: __ubsan_handle_pointer_overflow
115 void dont_emit_checks_for_no_op_GEPs(char *p
) {