1 // RUN: %clang_cc1 -std=c++11 -triple x86_64-apple-darwin10 -emit-llvm -o - %s -fsanitize=alignment | FileCheck %s --check-prefixes=CHECK,ALIGN
2 // RUN: %clang_cc1 -std=c++11 -triple x86_64-apple-darwin10 -emit-llvm -o - %s -fsanitize=null | FileCheck %s --check-prefixes=CHECK,NULL
3 // RUN: %clang_cc1 -std=c++11 -triple x86_64-apple-darwin10 -emit-llvm -o - %s -fsanitize=alignment,null -DCHECK_LAMBDA | FileCheck %s --check-prefixes=LAMBDA
5 // CHECK-LABEL: define{{.*}} void @_Z22load_non_null_pointersv
6 void load_non_null_pointers() {
15 // CHECK-NOT: and i64 {{.*}}, !nosanitize
16 // CHECK-NOT: icmp ne {{.*}}, null, !nosanitize
20 // CHECK-LABEL: define{{.*}} void @_Z31use_us16_aligned_array_elementsv
21 void use_us16_aligned_array_elements() {
22 static const unsigned short Arr
[] = {0, 1, 2};
23 auto use_array
= [](const unsigned short(&X
)[3]) -> void {};
26 // CHECK-NOT: br i1 true
27 // ALIGN-NOT: call void @__ubsan_handle_type_mismatch
34 // CHECK-LABEL: define linkonce_odr void @_ZN1A10do_nothingEv
36 // ALIGN: %[[THISINT1:[0-9]+]] = ptrtoint ptr %{{.*}} to i64, !nosanitize
37 // ALIGN: and i64 %[[THISINT1]], 3, !nosanitize
38 // NULL: icmp ne ptr %[[THIS1:[a-z0-9]+]], null, !nosanitize
39 // NULL: ptrtoint ptr %[[THIS1]] to i64, !nosanitize
40 // CHECK: call void @__ubsan_handle_type_mismatch
41 // CHECK-NOT: call void @__ubsan_handle_type_mismatch
46 // LAMBDA-LABEL: define linkonce_odr void @_ZN1A22do_nothing_with_lambdaEv
47 void do_nothing_with_lambda() {
48 // LAMBDA: icmp ne ptr %[[THIS2:[a-z0-9]+]], null, !nosanitize
49 // LAMBDA: %[[THISINT2:[0-9]+]] = ptrtoint ptr %[[THIS2]] to i64, !nosanitize
50 // LAMBDA: and i64 %[[THISINT2]], 3, !nosanitize
51 // LAMBDA: call void @__ubsan_handle_type_mismatch
58 // LAMBDA-NOT: call void @__ubsan_handle_type_mismatch
62 // Check the IR for the lambda:
64 // LAMBDA-LABEL: define linkonce_odr void @_ZZN1A22do_nothing_with_lambdaEvENKUlvE_clEv
65 // LAMBDA: call void @__ubsan_handle_type_mismatch
66 // LAMBDA-NOT: call void @__ubsan_handle_type_mismatch
70 // CHECK-LABEL: define linkonce_odr noundef i32 @_ZN1A11load_memberEv
72 // ALIGN: %[[THISINT3:[0-9]+]] = ptrtoint ptr %{{.*}} to i64, !nosanitize
73 // ALIGN: and i64 %[[THISINT3]], 3, !nosanitize
74 // NULL: icmp ne ptr %[[THIS3:[a-z0-9]+]], null, !nosanitize
75 // NULL: ptrtoint ptr %[[THIS3]] to i64, !nosanitize
76 // CHECK: call void @__ubsan_handle_type_mismatch
77 // CHECK-NOT: call void @__ubsan_handle_type_mismatch
82 // CHECK-LABEL: define linkonce_odr noundef i32 @_ZN1A11call_methodEv
84 // ALIGN: %[[THISINT4:[0-9]+]] = ptrtoint ptr %{{.*}} to i64, !nosanitize
85 // ALIGN: and i64 %[[THISINT4]], 3, !nosanitize
86 // NULL: icmp ne ptr %[[THIS4:[a-z0-9]+]], null, !nosanitize
87 // NULL: ptrtoint ptr %[[THIS4]] to i64, !nosanitize
88 // CHECK: call void @__ubsan_handle_type_mismatch
89 // CHECK-NOT: call void @__ubsan_handle_type_mismatch
94 // CHECK-LABEL: define linkonce_odr void @_ZN1A15assign_member_1Ev
95 void assign_member_1() {
96 // ALIGN: %[[THISINT5:[0-9]+]] = ptrtoint ptr %{{.*}} to i64, !nosanitize
97 // ALIGN: and i64 %[[THISINT5]], 3, !nosanitize
98 // NULL: icmp ne ptr %[[THIS5:[a-z0-9]+]], null, !nosanitize
99 // NULL: ptrtoint ptr %[[THIS5]] to i64, !nosanitize
100 // CHECK: call void @__ubsan_handle_type_mismatch
101 // CHECK-NOT: call void @__ubsan_handle_type_mismatch
106 // CHECK-LABEL: define linkonce_odr void @_ZN1A15assign_member_2Ev
107 void assign_member_2() {
108 // ALIGN: %[[THISINT6:[0-9]+]] = ptrtoint ptr %{{.*}} to i64, !nosanitize
109 // ALIGN: and i64 %[[THISINT6]], 3, !nosanitize
110 // NULL: icmp ne ptr %[[THIS6:[a-z0-9]+]], null, !nosanitize
111 // NULL: ptrtoint ptr %[[THIS6]] to i64, !nosanitize
112 // CHECK: call void @__ubsan_handle_type_mismatch
113 // CHECK-NOT: call void @__ubsan_handle_type_mismatch
114 (__extension__ (this))->foo
= 0;
118 // CHECK-LABEL: define linkonce_odr void @_ZNK1A15assign_member_3Ev
119 void assign_member_3() const {
120 // ALIGN: %[[THISINT7:[0-9]+]] = ptrtoint ptr %{{.*}} to i64, !nosanitize
121 // ALIGN: and i64 %[[THISINT7]], 3, !nosanitize
122 // NULL: icmp ne ptr %[[THIS7:[a-z0-9]+]], null, !nosanitize
123 // NULL: ptrtoint ptr %[[THIS7]] to i64, !nosanitize
124 // CHECK: call void @__ubsan_handle_type_mismatch
125 // CHECK-NOT: call void @__ubsan_handle_type_mismatch
126 const_cast<A
*>(this)->foo
= 0;
130 // CHECK-LABEL: define linkonce_odr noundef i32 @_ZN1A22call_through_referenceERS_
131 static int call_through_reference(A
&a
) {
132 // ALIGN: %[[OBJINT:[0-9]+]] = ptrtoint ptr %{{.*}} to i64, !nosanitize
133 // ALIGN: and i64 %[[OBJINT]], 3, !nosanitize
134 // ALIGN: call void @__ubsan_handle_type_mismatch
135 // NULL-NOT: call void @__ubsan_handle_type_mismatch
136 return a
.load_member();
140 // CHECK-LABEL: define linkonce_odr noundef i32 @_ZN1A20call_through_pointerEPS_
141 static int call_through_pointer(A
*a
) {
142 // CHECK: call void @__ubsan_handle_type_mismatch
143 return a
->load_member();
149 operator A
*() const { return nullptr; }
151 // CHECK-LABEL: define linkonce_odr noundef i32 @_ZN1B11load_memberEPS_
152 static int load_member(B
*bp
) {
153 // Check &b before converting it to an A*.
154 // CHECK: call void @__ubsan_handle_type_mismatch
156 // Check the result of the conversion before using it.
157 // NULL: call void @__ubsan_handle_type_mismatch
159 // CHECK-NOT: call void @__ubsan_handle_type_mismatch
160 return static_cast<A
*>(*bp
)->load_member();
168 virtual int load_member_1() = 0;
171 struct Derived
: public Base
{
174 // CHECK-LABEL: define linkonce_odr noundef i32 @_ZN7Derived13load_member_2Ev
175 int load_member_2() {
176 // ALIGN: %[[THISINT8:[0-9]+]] = ptrtoint ptr %{{.*}} to i64, !nosanitize
177 // ALIGN: and i64 %[[THISINT8]], 7, !nosanitize
178 // ALIGN: call void @__ubsan_handle_type_mismatch
179 // NULL: icmp ne ptr %[[THIS8:[a-z0-9]+]], null, !nosanitize
180 // NULL: ptrtoint ptr %[[THIS8]] to i64, !nosanitize
181 // CHECK: call void @__ubsan_handle_type_mismatch
183 // Check the result of the cast before using it.
184 // CHECK: call void @__ubsan_handle_type_mismatch
186 // CHECK-NOT: call void @__ubsan_handle_type_mismatch
187 return dynamic_cast<Base
*>(this)->load_member_1();
191 // CHECK-LABEL: define linkonce_odr noundef i32 @_ZN7Derived13load_member_3Ev
192 int load_member_3() {
193 // ALIGN: %[[THISINT9:[0-9]+]] = ptrtoint ptr %{{.*}} to i64, !nosanitize
194 // ALIGN: and i64 %[[THISINT9]], 7, !nosanitize
195 // ALIGN: call void @__ubsan_handle_type_mismatch
196 // ALIGN: call void @__ubsan_handle_type_mismatch
197 // NULL: icmp ne ptr %[[THIS9:[a-z0-9]+]], null, !nosanitize
198 // NULL: ptrtoint ptr %[[THIS9]] to i64, !nosanitize
199 // CHECK: call void @__ubsan_handle_type_mismatch
200 // CHECK-NOT: call void @__ubsan_handle_type_mismatch
201 return reinterpret_cast<Derived
*>(static_cast<Base
*>(this))->foo
;
205 // CHECK-LABEL: define linkonce_odr noundef i32 @_ZN7Derived13load_member_1Ev
206 int load_member_1() override
{
207 // ALIGN: %[[THISINT10:[0-9]+]] = ptrtoint ptr %{{.*}} to i64, !nosanitize
208 // ALIGN: and i64 %[[THISINT10]], 7, !nosanitize
209 // ALIGN: call void @__ubsan_handle_type_mismatch
210 // NULL: icmp ne ptr %[[THIS10:[a-z0-9]+]], null, !nosanitize
211 // NULL: ptrtoint ptr %[[THIS10]] to i64, !nosanitize
212 // CHECK: call void @__ubsan_handle_type_mismatch
213 // CHECK-NOT: call void @__ubsan_handle_type_mismatch
223 a
->do_nothing_with_lambda();
227 a
->assign_member_1();
228 a
->assign_member_2();
229 a
->assign_member_3();
230 A::call_through_reference(*a
);
231 A::call_through_pointer(a
);
233 B::load_member(nullptr);
235 Base
*b
= new Derived
;
242 load_non_null_pointers();
243 use_us16_aligned_array_elements();