1 // RUN: %clang_cc1 -std=c++11 -fsanitize=signed-integer-overflow,integer-divide-by-zero,float-divide-by-zero,shift-base,shift-exponent,unreachable,return,vla-bound,alignment,null,vptr,object-size,float-cast-overflow,bool,enum,array-bounds,function -fsanitize-recover=signed-integer-overflow,integer-divide-by-zero,float-divide-by-zero,shift-base,shift-exponent,vla-bound,alignment,null,vptr,object-size,float-cast-overflow,bool,enum,array-bounds,function -emit-llvm %s -o - -triple x86_64-linux-gnu | opt -instnamer -S | FileCheck %s
2 // RUN: %clang_cc1 -std=c++11 -fsanitize=vptr,address -fsanitize-recover=vptr,address -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck %s --check-prefix=CHECK-ASAN
3 // RUN: %clang_cc1 -std=c++11 -fsanitize=vptr -fsanitize-recover=vptr -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck %s --check-prefix=DOWNCAST-NULL
4 // RUN: %clang_cc1 -std=c++11 -fsanitize=function -emit-llvm %s -o - -triple x86_64-linux-gnux32 | FileCheck %s --check-prefix=CHECK-X32
5 // RUN: %clang_cc1 -std=c++11 -fsanitize=function -emit-llvm %s -o - -triple i386-linux-gnu | FileCheck %s --check-prefix=CHECK-X86
6 
7 struct S {
8   double d;
9   int a, b;
10   virtual int f();
11 };
12 
13 // Check that type descriptor global is not modified by ASan.
14 // CHECK-ASAN: [[TYPE_DESCR:@[0-9]+]] = private unnamed_addr constant { i16, i16, [4 x i8] } { i16 -1, i16 0, [4 x i8] c"'S'\00" }
15 
16 // Check that type mismatch handler is not modified by ASan.
17 // CHECK-ASAN: private unnamed_addr global { { [{{.*}} x i8]*, i32, i32 }, { i16, i16, [4 x i8] }*, i8*, i8 } { {{.*}}, { i16, i16, [4 x i8] }* [[TYPE_DESCR]], {{.*}} }
18 
19 // CHECK: [[IndirectRTTI_ZTIFvPFviEE:@.+]] = private constant i8* bitcast ({ i8*, i8* }* @_ZTIFvPFviEE to i8*)
20 // CHECK-X86: [[IndirectRTTI_ZTIFvPFviEE:@.+]] = private constant i8* bitcast ({ i8*, i8* }* @_ZTIFvPFviEE to i8*)
21 // CHECK-X32: [[IndirectRTTI_ZTIFvPFviEE:@.+]] = private constant i8* bitcast ({ i8*, i8* }* @_ZTIFvPFviEE to i8*)
22 
23 struct T : S {};
24 
25 // CHECK-LABEL: @_Z17reference_binding
26 void reference_binding(int *p, S *q) {
27   // C++ core issue 453: If an lvalue to which a reference is directly bound
28   // designates neither an existing object or function of an appropriate type,
29   // nor a region of storage of suitable size and alignment to contain an object
30   // of the reference's type, the behavior is undefined.
31 
32   // CHECK: icmp ne {{.*}}, null
33 
34   // CHECK: %[[SIZE:.*]] = call i64 @llvm.objectsize.i64
35   // CHECK-NEXT: icmp uge i64 %[[SIZE]], 4
36 
37   // CHECK: %[[PTRINT:.*]] = ptrtoint
38   // CHECK-NEXT: %[[MISALIGN:.*]] = and i64 %[[PTRINT]], 3
39   // CHECK-NEXT: icmp eq i64 %[[MISALIGN]], 0
40   int &r = *p;
41 
42   // A reference is not required to refer to an object within its lifetime.
43   // CHECK-NOT: __ubsan_handle_dynamic_type_cache_miss
44   S &r2 = *q;
45 }
46 
47 // CHECK-LABEL: @_Z13member_access
48 // CHECK-ASAN-LABEL: @_Z13member_access
49 void member_access(S *p) {
50   // (1a) Check 'p' is appropriately sized and aligned for member access.
51 
52   // CHECK: icmp ne {{.*}}, null
53 
54   // CHECK: %[[SIZE:.*]] = call i64 @llvm.objectsize.i64
55   // CHECK-NEXT: icmp uge i64 %[[SIZE]], 24
56 
57   // CHECK: %[[PTRINT:.*]] = ptrtoint
58   // CHECK-NEXT: %[[MISALIGN:.*]] = and i64 %[[PTRINT]], 7
59   // CHECK-NEXT: icmp eq i64 %[[MISALIGN]], 0
60 
61   // (1b) Check that 'p' actually points to an 'S'.
62 
63   // CHECK: %[[VPTRADDR:.*]] = bitcast {{.*}} to i64*
64   // CHECK-NEXT: %[[VPTR:.*]] = load i64, i64* %[[VPTRADDR]]
65   //
66   // hash_16_bytes:
67   //
68   // If this number changes, it indicates that either the mangled name of ::S
69   // has changed, or that LLVM's hashing function has changed. The latter case
70   // is OK if the hashing function is still stable.
71   //
72   // The two hash values are for 64- and 32-bit Clang binaries, respectively.
73   // FIXME: We should produce a 64-bit value either way.
74   //
75   // CHECK-NEXT: xor i64 {{-4030275160588942838|1107558922}}, %[[VPTR]]
76   // CHECK-NEXT: mul i64 {{.*}}, -7070675565921424023
77   // CHECK-NEXT: lshr i64 {{.*}}, 47
78   // CHECK-NEXT: xor i64
79   // CHECK-NEXT: xor i64 %[[VPTR]]
80   // CHECK-NEXT: mul i64 {{.*}}, -7070675565921424023
81   // CHECK-NEXT: lshr i64 {{.*}}, 47
82   // CHECK-NEXT: xor i64
83   // CHECK-NEXT: %[[HASH:.*]] = mul i64 {{.*}}, -7070675565921424023
84   //
85   // Check the hash against the table:
86   //
87   // CHECK-NEXT: %[[IDX:.*]] = and i64 %{{.*}}, 127
88   // CHECK-NEXT: getelementptr inbounds [128 x i64], [128 x i64]* @__ubsan_vptr_type_cache, i32 0, i64 %[[IDX]]
89   // CHECK-NEXT: %[[CACHEVAL:.*]] = load i64, i64*
90   // CHECK-NEXT: icmp eq i64 %[[CACHEVAL]], %[[HASH]]
91   // CHECK-NEXT: br i1
92 
93   // CHECK: call void @__ubsan_handle_dynamic_type_cache_miss({{.*}}, i64 %{{.*}}, i64 %[[HASH]])
94   // CHECK-NOT: unreachable
95   // CHECK: {{.*}}:
96 
97   // (2) Check 'p->b' is appropriately sized and aligned for a load.
98 
99   // FIXME: Suppress this in the trivial case of a member access, because we
100   // know we've just checked the member access expression itself.
101 
102   // CHECK: %[[SIZE:.*]] = call i64 @llvm.objectsize.i64
103   // CHECK-NEXT: icmp uge i64 %[[SIZE]], 4
104 
105   // CHECK: %[[PTRINT:.*]] = ptrtoint
106   // CHECK-NEXT: %[[MISALIGN:.*]] = and i64 %[[PTRINT]], 3
107   // CHECK-NEXT: icmp eq i64 %[[MISALIGN]], 0
108   int k = p->b;
109 
110   // (3a) Check 'p' is appropriately sized and aligned for member function call.
111 
112   // CHECK: icmp ne {{.*}}, null
113 
114   // CHECK: %[[SIZE:.*]] = call i64 @llvm.objectsize.i64
115   // CHECK-NEXT: icmp uge i64 %[[SIZE]], 24
116 
117   // CHECK: %[[PTRINT:.*]] = ptrtoint
118   // CHECK-NEXT: %[[MISALIGN:.*]] = and i64 %[[PTRINT]], 7
119   // CHECK-NEXT: icmp eq i64 %[[MISALIGN]], 0
120 
121   // (3b) Check that 'p' actually points to an 'S'
122 
123   // CHECK: load i64, i64*
124   // CHECK-NEXT: xor i64 {{-4030275160588942838|1107558922}},
125   // [...]
126   // CHECK: getelementptr inbounds [128 x i64], [128 x i64]* @__ubsan_vptr_type_cache, i32 0, i64 %
127   // CHECK: br i1
128   // CHECK: call void @__ubsan_handle_dynamic_type_cache_miss({{.*}}, i64 %{{.*}}, i64 %{{.*}})
129   // CHECK-NOT: unreachable
130   // CHECK: {{.*}}:
131 
132   k = p->f();
133 }
134 
135 // CHECK-LABEL: @_Z12lsh_overflow
136 int lsh_overflow(int a, int b) {
137   // CHECK: %[[RHS_INBOUNDS:.*]] = icmp ule i32 %[[RHS:.*]], 31
138   // CHECK-NEXT: br i1 %[[RHS_INBOUNDS]], label %[[CHECK_BB:.*]], label %[[CONT_BB:.*]],
139 
140   // CHECK:      [[CHECK_BB]]:
141   // CHECK-NEXT: %[[SHIFTED_OUT_WIDTH:.*]] = sub nuw nsw i32 31, %[[RHS]]
142   // CHECK-NEXT: %[[SHIFTED_OUT:.*]] = lshr i32 %[[LHS:.*]], %[[SHIFTED_OUT_WIDTH]]
143 
144   // This is present for C++11 but not for C: C++ core issue 1457 allows a '1'
145   // to be shifted into the sign bit, but not out of it.
146   // CHECK-NEXT: %[[SHIFTED_OUT_NOT_SIGN:.*]] = lshr i32 %[[SHIFTED_OUT]], 1
147 
148   // CHECK-NEXT: %[[NO_OVERFLOW:.*]] = icmp eq i32 %[[SHIFTED_OUT_NOT_SIGN]], 0
149   // CHECK-NEXT: br label %[[CONT_BB]]
150 
151   // CHECK:      [[CONT_BB]]:
152   // CHECK-NEXT: %[[VALID_BASE:.*]] = phi i1 [ true, {{.*}} ], [ %[[NO_OVERFLOW]], %[[CHECK_BB]] ]
153   // CHECK-NEXT: %[[VALID:.*]] = and i1 %[[RHS_INBOUNDS]], %[[VALID_BASE]]
154   // CHECK-NEXT: br i1 %[[VALID]]
155 
156   // CHECK: call void @__ubsan_handle_shift_out_of_bounds
157   // CHECK-NOT: call void @__ubsan_handle_shift_out_of_bounds
158 
159   // CHECK: %[[RET:.*]] = shl i32 %[[LHS]], %[[RHS]]
160   // CHECK-NEXT: ret i32 %[[RET]]
161   return a << b;
162 }
163 
164 // CHECK-LABEL: @_Z9no_return
165 int no_return() {
166   // CHECK:      call void @__ubsan_handle_missing_return(i8* bitcast ({{.*}}* @{{.*}} to i8*)) [[NR_NUW:#[0-9]+]]
167   // CHECK-NEXT: unreachable
168 }
169 
170 // CHECK-LABEL: @_Z9sour_bool
171 bool sour_bool(bool *p) {
172   // CHECK: %[[OK:.*]] = icmp ule i8 {{.*}}, 1
173   // CHECK: br i1 %[[OK]]
174   // CHECK: call void @__ubsan_handle_load_invalid_value(i8* bitcast ({{.*}}), i64 {{.*}})
175   return *p;
176 }
177 
178 enum E1 { e1a = 0, e1b = 127 } e1;
179 enum E2 { e2a = -1, e2b = 64 } e2;
180 enum E3 { e3a = (1u << 31) - 1 } e3;
181 
182 // CHECK-LABEL: @_Z14bad_enum_value
183 int bad_enum_value() {
184   // CHECK: %[[E1:.*]] = icmp ule i32 {{.*}}, 127
185   // CHECK: br i1 %[[E1]]
186   // CHECK: call void @__ubsan_handle_load_invalid_value(
187   int a = e1;
188 
189   // CHECK: %[[E2HI:.*]] = icmp sle i32 {{.*}}, 127
190   // CHECK: %[[E2LO:.*]] = icmp sge i32 {{.*}}, -128
191   // CHECK: %[[E2:.*]] = and i1 %[[E2HI]], %[[E2LO]]
192   // CHECK: br i1 %[[E2]]
193   // CHECK: call void @__ubsan_handle_load_invalid_value(
194   int b = e2;
195 
196   // CHECK: %[[E3:.*]] = icmp ule i32 {{.*}}, 2147483647
197   // CHECK: br i1 %[[E3]]
198   // CHECK: call void @__ubsan_handle_load_invalid_value(
199   int c = e3;
200   return a + b + c;
201 }
202 
203 // CHECK-LABEL: @_Z20bad_downcast_pointer
204 // DOWNCAST-NULL-LABEL: @_Z20bad_downcast_pointer
205 void bad_downcast_pointer(S *p) {
206   // CHECK: %[[NONNULL:.*]] = icmp ne {{.*}}, null
207   // CHECK: br i1 %[[NONNULL]],
208 
209   // A null pointer access is guarded without -fsanitize=null.
210   // DOWNCAST-NULL: %[[NONNULL:.*]] = icmp ne {{.*}}, null
211   // DOWNCAST-NULL: br i1 %[[NONNULL]],
212 
213   // CHECK: %[[SIZE:.*]] = call i64 @llvm.objectsize.i64.p0i8(
214   // CHECK: %[[E1:.*]] = icmp uge i64 %[[SIZE]], 24
215   // CHECK: %[[MISALIGN:.*]] = and i64 %{{.*}}, 7
216   // CHECK: %[[E2:.*]] = icmp eq i64 %[[MISALIGN]], 0
217   // CHECK: %[[E12:.*]] = and i1 %[[E1]], %[[E2]]
218   // CHECK: br i1 %[[E12]],
219 
220   // CHECK: call void @__ubsan_handle_type_mismatch
221   // CHECK: br label
222 
223   // CHECK: br i1 %{{.*}},
224 
225   // CHECK: call void @__ubsan_handle_dynamic_type_cache_miss
226   // CHECK: br label
227   (void) static_cast<T*>(p);
228 }
229 
230 // CHECK-LABEL: @_Z22bad_downcast_reference
231 void bad_downcast_reference(S &p) {
232   // CHECK: %[[E1:.*]] = icmp ne {{.*}}, null
233   // CHECK-NOT: br i1
234 
235   // CHECK: %[[SIZE:.*]] = call i64 @llvm.objectsize.i64.p0i8(
236   // CHECK: %[[E2:.*]] = icmp uge i64 %[[SIZE]], 24
237 
238   // CHECK: %[[MISALIGN:.*]] = and i64 %{{.*}}, 7
239   // CHECK: %[[E3:.*]] = icmp eq i64 %[[MISALIGN]], 0
240 
241   // CHECK: %[[E12:.*]] = and i1 %[[E1]], %[[E2]]
242   // CHECK: %[[E123:.*]] = and i1 %[[E12]], %[[E3]]
243   // CHECK: br i1 %[[E123]],
244 
245   // CHECK: call void @__ubsan_handle_type_mismatch
246   // CHECK: br label
247 
248   // CHECK: br i1 %{{.*}},
249 
250   // CHECK: call void @__ubsan_handle_dynamic_type_cache_miss
251   // CHECK: br label
252   (void) static_cast<T&>(p);
253 }
254 
255 // CHECK-LABEL: @_Z11array_index
256 int array_index(const int (&a)[4], int n) {
257   // CHECK: %[[K1_OK:.*]] = icmp ult i64 %{{.*}}, 4
258   // CHECK: br i1 %[[K1_OK]]
259   // CHECK: call void @__ubsan_handle_out_of_bounds(
260   int k1 = a[n];
261 
262   // CHECK: %[[R1_OK:.*]] = icmp ule i64 %{{.*}}, 4
263   // CHECK: br i1 %[[R1_OK]]
264   // CHECK: call void @__ubsan_handle_out_of_bounds(
265   const int *r1 = &a[n];
266 
267   // CHECK: %[[K2_OK:.*]] = icmp ult i64 %{{.*}}, 8
268   // CHECK: br i1 %[[K2_OK]]
269   // CHECK: call void @__ubsan_handle_out_of_bounds(
270   int k2 = ((const int(&)[8])a)[n];
271 
272   // CHECK: %[[K3_OK:.*]] = icmp ult i64 %{{.*}}, 4
273   // CHECK: br i1 %[[K3_OK]]
274   // CHECK: call void @__ubsan_handle_out_of_bounds(
275   int k3 = n[a];
276 
277   return k1 + *r1 + k2;
278 }
279 
280 // CHECK-LABEL: @_Z17multi_array_index
281 int multi_array_index(int n, int m) {
282   int arr[4][6];
283 
284   // CHECK: %[[IDX1_OK:.*]] = icmp ult i64 %{{.*}}, 4
285   // CHECK: br i1 %[[IDX1_OK]]
286   // CHECK: call void @__ubsan_handle_out_of_bounds(
287 
288   // CHECK: %[[IDX2_OK:.*]] = icmp ult i64 %{{.*}}, 6
289   // CHECK: br i1 %[[IDX2_OK]]
290   // CHECK: call void @__ubsan_handle_out_of_bounds(
291   return arr[n][m];
292 }
293 
294 // CHECK-LABEL: @_Z11array_arith
295 int array_arith(const int (&a)[4], int n) {
296   // CHECK: %[[K1_OK:.*]] = icmp ule i64 %{{.*}}, 4
297   // CHECK: br i1 %[[K1_OK]]
298   // CHECK: call void @__ubsan_handle_out_of_bounds(
299   const int *k1 = a + n;
300 
301   // CHECK: %[[K2_OK:.*]] = icmp ule i64 %{{.*}}, 8
302   // CHECK: br i1 %[[K2_OK]]
303   // CHECK: call void @__ubsan_handle_out_of_bounds(
304   const int *k2 = (const int(&)[8])a + n;
305 
306   return *k1 + *k2;
307 }
308 
309 struct ArrayMembers {
310   int a1[5];
311   int a2[1];
312 };
313 // CHECK-LABEL: @_Z18struct_array_index
314 int struct_array_index(ArrayMembers *p, int n) {
315   // CHECK: %[[IDX_OK:.*]] = icmp ult i64 %{{.*}}, 5
316   // CHECK: br i1 %[[IDX_OK]]
317   // CHECK: call void @__ubsan_handle_out_of_bounds(
318   return p->a1[n];
319 }
320 
321 // CHECK-LABEL: @_Z16flex_array_index
322 int flex_array_index(ArrayMembers *p, int n) {
323   // CHECK-NOT: call void @__ubsan_handle_out_of_bounds(
324   return p->a2[n];
325 }
326 
327 extern int incomplete[];
328 // CHECK-LABEL: @_Z22incomplete_array_index
329 int incomplete_array_index(int n) {
330   // CHECK-NOT: call void @__ubsan_handle_out_of_bounds(
331   return incomplete[n];
332 }
333 
334 typedef __attribute__((ext_vector_type(4))) int V4I;
335 // CHECK-LABEL: @_Z12vector_index
336 int vector_index(V4I v, int n) {
337   // CHECK: %[[IDX_OK:.*]] = icmp ult i64 %{{.*}}, 4
338   // CHECK: br i1 %[[IDX_OK]]
339   // CHECK: call void @__ubsan_handle_out_of_bounds(
340   return v[n];
341 }
342 
343 // CHECK-LABEL: @_Z12string_index
344 char string_index(int n) {
345   // CHECK: %[[IDX_OK:.*]] = icmp ult i64 %{{.*}}, 6
346   // CHECK: br i1 %[[IDX_OK]]
347   // CHECK: call void @__ubsan_handle_out_of_bounds(
348   return "Hello"[n];
349 }
350 
351 class A // align=4
352 {
353   int a1, a2, a3;
354 };
355 
356 class B // align=8
357 {
358   long b1, b2;
359 };
360 
361 class C : public A, public B // align=16
362 {
363   alignas(16) int c1;
364 };
365 
366 // Make sure we check the alignment of the pointer after subtracting any
367 // offset. The pointer before subtraction doesn't need to be aligned for
368 // the destination type.
369 
370 // CHECK-LABEL: define void @_Z16downcast_pointerP1B(%class.B* %b)
371 void downcast_pointer(B *b) {
372   (void) static_cast<C*>(b);
373   // Alignment check from EmitTypeCheck(TCK_DowncastPointer, ...)
374   // CHECK: [[SUB:%[.a-z0-9]*]] = getelementptr inbounds i8, i8* {{.*}}, i64 -16
375   // CHECK-NEXT: [[C:%.+]] = bitcast i8* [[SUB]] to %class.C*
376   // null check goes here
377   // CHECK: [[FROM_PHI:%.+]] = phi %class.C* [ [[C]], {{.*}} ], {{.*}}
378   // Objectsize check goes here
379   // CHECK: [[C_INT:%.+]] = ptrtoint %class.C* [[FROM_PHI]] to i64
380   // CHECK-NEXT: [[MASKED:%.+]] = and i64 [[C_INT]], 15
381   // CHECK-NEXT: [[TEST:%.+]] = icmp eq i64 [[MASKED]], 0
382   // AND the alignment test with the objectsize test.
383   // CHECK-NEXT: [[AND:%.+]] = and i1 {{.*}}, [[TEST]]
384   // CHECK-NEXT: br i1 [[AND]]
385 }
386 
387 // CHECK-LABEL: define void @_Z18downcast_referenceR1B(%class.B* nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) %b)
388 void downcast_reference(B &b) {
389   (void) static_cast<C&>(b);
390   // Alignment check from EmitTypeCheck(TCK_DowncastReference, ...)
391   // CHECK:      [[SUB:%[.a-z0-9]*]] = getelementptr inbounds i8, i8* {{.*}}, i64 -16
392   // CHECK-NEXT: [[C:%.+]] = bitcast i8* [[SUB]] to %class.C*
393   // Objectsize check goes here
394   // CHECK:      [[C_INT:%.+]] = ptrtoint %class.C* [[C]] to i64
395   // CHECK-NEXT: [[MASKED:%.+]] = and i64 [[C_INT]], 15
396   // CHECK-NEXT: [[TEST:%.+]] = icmp eq i64 [[MASKED]], 0
397   // AND the alignment test with the objectsize test.
398   // CHECK:      [[AND:%.+]] = and i1 {{.*}}, [[TEST]]
399   // CHECK-NEXT: br i1 [[AND]]
400 }
401 
402 //
403 // CHECK-LABEL: @_Z22indirect_function_callPFviE({{.*}} prologue <{ i32, i32 }> <{ i32 846595819, i32 trunc (i64 sub (i64 ptrtoint (i8** {{.*}} to i64), i64 ptrtoint (void (void (i32)*)* @_Z22indirect_function_callPFviE to i64)) to i32) }>
404 // CHECK-X32: @_Z22indirect_function_callPFviE({{.*}} prologue <{ i32, i32 }> <{ i32 846595819, i32 sub (i32 ptrtoint (i8** [[IndirectRTTI_ZTIFvPFviEE]] to i32), i32 ptrtoint (void (void (i32)*)* @_Z22indirect_function_callPFviE to i32)) }>
405 // CHECK-X86: @_Z22indirect_function_callPFviE({{.*}} prologue <{ i32, i32 }> <{ i32 846595819, i32 sub (i32 ptrtoint (i8** [[IndirectRTTI_ZTIFvPFviEE]] to i32), i32 ptrtoint (void (void (i32)*)* @_Z22indirect_function_callPFviE to i32)) }>
406 void indirect_function_call(void (*p)(int)) {
407   // CHECK: [[PTR:%.+]] = bitcast void (i32)* {{.*}} to <{ i32, i32 }>*
408 
409   // Signature check
410   // CHECK-NEXT: [[SIGPTR:%.+]] = getelementptr <{ i32, i32 }>, <{ i32, i32 }>* [[PTR]], i32 0, i32 0
411   // CHECK-NEXT: [[SIG:%.+]] = load i32, i32* [[SIGPTR]]
412   // CHECK-NEXT: [[SIGCMP:%.+]] = icmp eq i32 [[SIG]], 846595819
413   // CHECK-NEXT: br i1 [[SIGCMP]]
414 
415   // RTTI pointer check
416   // CHECK: [[RTTIPTR:%.+]] = getelementptr <{ i32, i32 }>, <{ i32, i32 }>* [[PTR]], i32 0, i32 1
417   // CHECK-NEXT: [[RTTIEncIntTrunc:%.+]] = load i32, i32* [[RTTIPTR]]
418   // CHECK-NEXT: [[RTTIEncInt:%.+]] = sext i32 [[RTTIEncIntTrunc]] to i64
419   // CHECK-NEXT: [[FuncAddrInt:%.+]] = ptrtoint void (i32)* {{.*}} to i64
420   // CHECK-NEXT: [[IndirectGVInt:%.+]] = add i64 [[RTTIEncInt]], [[FuncAddrInt]]
421   // CHECK-NEXT: [[IndirectGV:%.+]] = inttoptr i64 [[IndirectGVInt]] to i8**
422   // CHECK-NEXT: [[RTTI:%.+]] = load i8*, i8** [[IndirectGV]], align 8
423   // CHECK-NEXT: [[RTTICMP:%.+]] = icmp eq i8* [[RTTI]], bitcast ({ i8*, i8* }* @_ZTIFviE to i8*)
424   // CHECK-NEXT: br i1 [[RTTICMP]]
425 
426   p(42);
427 }
428 
429 namespace VBaseObjectSize {
430   // Note: C is laid out such that offsetof(C, B) + sizeof(B) extends outside
431   // the C object.
432   struct alignas(16) A { void *a1, *a2; };
433   struct B : virtual A { void *b; };
434   struct C : virtual A, virtual B {};
435   // CHECK-LABEL: define {{.*}} @_ZN15VBaseObjectSize1fERNS_1BE(
436   B &f(B &b) {
437     // Size check: check for nvsize(B) == 16 (do not require size(B) == 32)
438     // CHECK: [[SIZE:%.+]] = call i{{32|64}} @llvm.objectsize.i64.p0i8(
439     // CHECK: icmp uge i{{32|64}} [[SIZE]], 16,
440 
441     // Alignment check: check for nvalign(B) == 8 (do not require align(B) == 16)
442     // CHECK: [[PTRTOINT:%.+]] = ptrtoint {{.*}} to i64,
443     // CHECK: and i64 [[PTRTOINT]], 7,
444     return b;
445   }
446 }
447 
448 namespace FunctionSanitizerVirtualCalls {
449 struct A {
450   virtual void f() {}
451   virtual void g() {}
452   void h() {}
453 };
454 
455 struct B : virtual A {
456   virtual void b() {}
457   virtual void f();
458   void g() final {}
459   static void q() {}
460 };
461 
462 void B::f() {}
463 
464 void force_irgen() {
465   A a;
466   a.g();
467   a.h();
468 
469   B b;
470   b.f();
471   b.b();
472   b.g();
473   B::q();
474 }
475 
476 // CHECK-LABEL: define void @_ZN29FunctionSanitizerVirtualCalls1B1fEv
477 // CHECK-NOT: prologue
478 //
479 // CHECK-LABEL: define void @_ZTv0_n24_N29FunctionSanitizerVirtualCalls1B1fEv
480 // CHECK-NOT: prologue
481 //
482 // CHECK-LABEL: define void @_ZN29FunctionSanitizerVirtualCalls11force_irgenEv()
483 // CHECK: prologue
484 //
485 // CHECK-LABEL: define linkonce_odr void @_ZN29FunctionSanitizerVirtualCalls1AC1Ev
486 // CHECK-NOT: prologue
487 //
488 // CHECK-LABEL: define linkonce_odr void @_ZN29FunctionSanitizerVirtualCalls1A1gEv
489 // CHECK-NOT: prologue
490 //
491 // CHECK-LABEL: define linkonce_odr void @_ZN29FunctionSanitizerVirtualCalls1A1hEv
492 // CHECK-NOT: prologue
493 //
494 // CHECK-LABEL: define linkonce_odr void @_ZN29FunctionSanitizerVirtualCalls1BC1Ev
495 // CHECK-NOT: prologue
496 //
497 // CHECK-LABEL: define linkonce_odr void @_ZN29FunctionSanitizerVirtualCalls1B1bEv
498 // CHECK-NOT: prologue
499 //
500 // CHECK-LABEL: define linkonce_odr void @_ZN29FunctionSanitizerVirtualCalls1B1gEv
501 // CHECK-NOT: prologue
502 //
503 // CHECK-LABEL: define linkonce_odr void @_ZN29FunctionSanitizerVirtualCalls1B1qEv
504 // CHECK: prologue
505 
506 }
507 
508 namespace UpcastPointerTest {
509 struct S {};
510 struct T : S { double d; };
511 struct V : virtual S {};
512 
513 // CHECK-LABEL: upcast_pointer
514 S* upcast_pointer(T* t) {
515   // Check for null pointer
516   // CHECK: %[[NONNULL:.*]] = icmp ne {{.*}}, null
517   // CHECK: br i1 %[[NONNULL]]
518 
519   // Check alignment
520   // CHECK: %[[MISALIGN:.*]] = and i64 %{{.*}}, 7
521   // CHECK: icmp eq i64 %[[MISALIGN]], 0
522 
523   // CHECK: call void @__ubsan_handle_type_mismatch
524   return t;
525 }
526 
527 V getV();
528 
529 // CHECK-LABEL: upcast_to_vbase
530 void upcast_to_vbase() {
531   // No need to check for null here, as we have a temporary here.
532 
533   // CHECK-NOT: br i1
534 
535   // CHECK: call i64 @llvm.objectsize
536   // CHECK: call void @__ubsan_handle_type_mismatch
537   // CHECK: call void @__ubsan_handle_dynamic_type_cache_miss
538   const S& s = getV();
539 }
540 }
541 
542 struct nothrow {};
543 void *operator new[](__SIZE_TYPE__, nothrow) noexcept;
544 
545 namespace NothrowNew {
546   struct X { X(); };
547 
548   // CHECK-LABEL: define{{.*}}nothrow_new_trivial
549   void *nothrow_new_trivial() {
550     // CHECK: %[[is_null:.*]] = icmp eq i8*{{.*}}, null
551     // CHECK: br i1 %[[is_null]], label %[[null:.*]], label %[[nonnull:.*]]
552 
553     // CHECK: [[nonnull]]:
554     // CHECK: llvm.objectsize
555     // CHECK: icmp uge i64 {{.*}}, 123456,
556     // CHECK: br i1
557     //
558     // CHECK: call {{.*}}__ubsan_handle_type_mismatch
559     //
560     // CHECK: [[null]]:
561     // CHECK-NOT: {{ }}br{{ }}
562     // CHECK: ret
563     return new (nothrow{}) char[123456];
564   }
565 
566   // CHECK-LABEL: define{{.*}}nothrow_new_nontrivial
567   void *nothrow_new_nontrivial() {
568     // CHECK: %[[is_null:.*]] = icmp eq i8*{{.*}}, null
569     // CHECK: br i1 %[[is_null]], label %[[null:.*]], label %[[nonnull:.*]]
570 
571     // CHECK: [[nonnull]]:
572     // CHECK: llvm.objectsize
573     // CHECK: icmp uge i64 {{.*}}, 123456,
574     // CHECK: br i1
575     //
576     // CHECK: call {{.*}}__ubsan_handle_type_mismatch
577     //
578     // CHECK: call {{.*}}_ZN10NothrowNew1XC1Ev
579     //
580     // CHECK: [[null]]:
581     // CHECK-NOT: {{ }}br{{ }}
582     // CHECK: ret
583     return new (nothrow{}) X[123456];
584   }
585 
586   // CHECK-LABEL: define{{.*}}throwing_new
587   void *throwing_new(int size) {
588     // CHECK: icmp ne i8*{{.*}}, null
589     // CHECK: %[[size:.*]] = mul
590     // CHECK: llvm.objectsize
591     // CHECK: icmp uge i64 {{.*}}, %[[size]],
592     // CHECK: %[[ok:.*]] = and
593     // CHECK: br i1 %[[ok]], label %[[good:.*]], label %[[bad:[^,]*]]
594     //
595     // CHECK: [[bad]]:
596     // CHECK: call {{.*}}__ubsan_handle_type_mismatch
597     //
598     // CHECK: [[good]]:
599     // CHECK-NOT: {{ }}br{{ }}
600     // CHECK: ret
601     return new char[size];
602   }
603 
604   // CHECK-LABEL: define{{.*}}nothrow_new_zero_size
605   void *nothrow_new_zero_size() {
606     // CHECK: %[[nonnull:.*]] = icmp ne i8*{{.*}}, null
607     // CHECK-NOT: llvm.objectsize
608     // CHECK: br i1 %[[nonnull]], label %[[good:.*]], label %[[bad:[^,]*]]
609     //
610     // CHECK: [[bad]]:
611     // CHECK: call {{.*}}__ubsan_handle_type_mismatch
612     //
613     // CHECK: [[good]]:
614     // CHECK-NOT: {{ }}br{{ }}
615     // CHECK: ret
616     return new char[0];
617   }
618 
619   // CHECK-LABEL: define{{.*}}throwing_new_zero_size
620   void *throwing_new_zero_size() {
621     // Nothing to check here.
622     // CHECK-NOT: __ubsan_handle_type_mismatch
623     return new (nothrow{}) char[0];
624     // CHECK: ret
625   }
626 }
627 
628 struct ThisAlign {
629   void this_align_lambda();
630   void this_align_lambda_2();
631 };
632 void ThisAlign::this_align_lambda() {
633   // CHECK-LABEL: define internal %struct.ThisAlign* @"_ZZN9ThisAlign17this_align_lambdaEvENK3$_0clEv"
634   // CHECK-SAME: (%{{.*}}* %[[this:[^)]*]])
635   // CHECK: %[[this_addr:.*]] = alloca
636   // CHECK: store %{{.*}}* %[[this]], %{{.*}}** %[[this_addr]],
637   // CHECK: %[[this_inner:.*]] = load %{{.*}}*, %{{.*}}** %[[this_addr]],
638   // CHECK: %[[this_outer_addr:.*]] = getelementptr inbounds %{{.*}}, %{{.*}}* %[[this_inner]], i32 0, i32 0
639   // CHECK: %[[this_outer:.*]] = load %{{.*}}*, %{{.*}}** %[[this_outer_addr]],
640   //
641   // CHECK: %[[this_inner_isnonnull:.*]] = icmp ne %{{.*}}* %[[this_inner]], null
642   // CHECK: %[[this_inner_asint:.*]] = ptrtoint %{{.*}}* %[[this_inner]] to i
643   // CHECK: %[[this_inner_misalignment:.*]] = and i{{32|64}} %[[this_inner_asint]], {{3|7}},
644   // CHECK: %[[this_inner_isaligned:.*]] = icmp eq i{{32|64}} %[[this_inner_misalignment]], 0
645   // CHECK: %[[this_inner_valid:.*]] = and i1 %[[this_inner_isnonnull]], %[[this_inner_isaligned]],
646   // CHECK: br i1 %[[this_inner_valid:.*]]
647   [&] { return this; } ();
648 }
649 
650 namespace CopyValueRepresentation {
651   // CHECK-LABEL: define {{.*}} @_ZN23CopyValueRepresentation2S3aSERKS0_
652   // CHECK-NOT: call {{.*}} @__ubsan_handle_load_invalid_value
653   // CHECK-LABEL: define {{.*}} @_ZN23CopyValueRepresentation2S4aSEOS0_
654   // CHECK-NOT: call {{.*}} @__ubsan_handle_load_invalid_value
655   // CHECK-LABEL: define {{.*}} @_ZN23CopyValueRepresentation2S1C2ERKS0_
656   // CHECK-NOT: call {{.*}} __ubsan_handle_load_invalid_value
657   // CHECK-LABEL: define {{.*}} @_ZN23CopyValueRepresentation2S2C2ERKS0_
658   // CHECK: __ubsan_handle_load_invalid_value
659   // CHECK-LABEL: define {{.*}} @_ZN23CopyValueRepresentation2S5C2ERKS0_
660   // CHECK-NOT: call {{.*}} __ubsan_handle_load_invalid_value
661 
662   struct CustomCopy { CustomCopy(); CustomCopy(const CustomCopy&); };
663   struct S1 {
664     CustomCopy CC;
665     bool b;
666   };
667   void callee1(S1);
668   void test1() {
669     S1 s11;
670     callee1(s11);
671     S1 s12;
672     s12 = s11;
673   }
674 
675   static bool some_global_bool;
676   struct ExprCopy {
677     ExprCopy();
678     ExprCopy(const ExprCopy&, bool b = some_global_bool);
679   };
680   struct S2 {
681     ExprCopy EC;
682     bool b;
683   };
684   void callee2(S2);
685   void test2(void) {
686     S2 s21;
687     callee2(s21);
688     S2 s22;
689     s22 = s21;
690   }
691 
692   struct CustomAssign { CustomAssign &operator=(const CustomAssign&); };
693   struct S3 {
694     CustomAssign CA;
695     bool b;
696   };
697   void test3() {
698     S3 x, y;
699     x = y;
700   }
701 
702   struct CustomMove {
703     CustomMove();
704     CustomMove(const CustomMove&&);
705     CustomMove &operator=(const CustomMove&&);
706   };
707   struct S4 {
708     CustomMove CM;
709     bool b;
710   };
711   void test4() {
712     S4 x, y;
713     x = static_cast<S4&&>(y);
714   }
715 
716   struct EnumCustomCopy {
717     EnumCustomCopy();
718     EnumCustomCopy(const EnumCustomCopy&);
719   };
720   struct S5 {
721     EnumCustomCopy ECC;
722     bool b;
723   };
724   void callee5(S5);
725   void test5() {
726     S5 s51;
727     callee5(s51);
728     S5 s52;
729     s52 = s51;
730   }
731 }
732 
733 void ThisAlign::this_align_lambda_2() {
734   // CHECK-LABEL: define internal void @"_ZZN9ThisAlign19this_align_lambda_2EvENK3$_1clEv"
735   // CHECK-SAME: (%{{.*}}* %[[this:[^)]*]])
736   // CHECK: %[[this_addr:.*]] = alloca
737   // CHECK: store %{{.*}}* %[[this]], %{{.*}}** %[[this_addr]],
738   // CHECK: %[[this_inner:.*]] = load %{{.*}}*, %{{.*}}** %[[this_addr]],
739   //
740   // Do not perform a null check on the 'this' pointer if the function might be
741   // called from a static invoker.
742   // CHECK-NOT: icmp ne %{{.*}}* %[[this_inner]], null
743   auto *p = +[] {};
744   p();
745 }
746 
747 // CHECK: attributes [[NR_NUW]] = { noreturn nounwind }
748