blob: 4120d0fead4da5befe30fe83504841de2aaf2f29 [file] [log] [blame]
Alexey Samsonov88459522015-01-12 22:39:12 +00001// RUN: %clang_cc1 -std=c++11 -fsanitize=signed-integer-overflow,integer-divide-by-zero,float-divide-by-zero,shift,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,vla-bound,alignment,null,vptr,object-size,float-cast-overflow,bool,enum,array-bounds,function -emit-llvm %s -o - -triple x86_64-linux-gnu | 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
Richard Smith69d0d262012-08-24 00:54:33 +00004
Richard Smithbe024a82012-12-18 00:22:45 +00005struct S {
6 double d;
7 int a, b;
8 virtual int f();
9};
10
Alexey Samsonov6c124142014-07-18 17:50:06 +000011// Check that type descriptor global is not modified by ASan.
12// 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" }
13
14// Check that type mismatch handler is not modified by ASan.
15// CHECK-ASAN: private unnamed_addr global { { [{{.*}} x i8]*, i32, i32 }, { i16, i16, [4 x i8] }*, i8*, i8 } { {{.*}}, { i16, i16, [4 x i8] }* [[TYPE_DESCR]], {{.*}} }
16
Richard Smith2c5868c2013-02-13 21:18:23 +000017struct T : S {};
18
Alexey Samsonovc9939332014-07-17 23:53:44 +000019// CHECK-LABEL: @_Z17reference_binding
Richard Smithbe024a82012-12-18 00:22:45 +000020void reference_binding(int *p, S *q) {
Richard Smith69d0d262012-08-24 00:54:33 +000021 // C++ core issue 453: If an lvalue to which a reference is directly bound
22 // designates neither an existing object or function of an appropriate type,
23 // nor a region of storage of suitable size and alignment to contain an object
24 // of the reference's type, the behavior is undefined.
25
26 // CHECK: icmp ne {{.*}}, null
27
28 // CHECK: %[[SIZE:.*]] = call i64 @llvm.objectsize.i64
29 // CHECK-NEXT: icmp uge i64 %[[SIZE]], 4
30
31 // CHECK: %[[PTRINT:.*]] = ptrtoint
32 // CHECK-NEXT: %[[MISALIGN:.*]] = and i64 %[[PTRINT]], 3
33 // CHECK-NEXT: icmp eq i64 %[[MISALIGN]], 0
34 int &r = *p;
Richard Smith69d0d262012-08-24 00:54:33 +000035
Richard Smithbe024a82012-12-18 00:22:45 +000036 // A reference is not required to refer to an object within its lifetime.
37 // CHECK-NOT: __ubsan_handle_dynamic_type_cache_miss
38 S &r2 = *q;
39}
Richard Smith69d0d262012-08-24 00:54:33 +000040
Alexey Samsonovc9939332014-07-17 23:53:44 +000041// CHECK-LABEL: @_Z13member_access
Alexey Samsonov6c124142014-07-18 17:50:06 +000042// CHECK-ASAN-LABEL: @_Z13member_access
Richard Smith69d0d262012-08-24 00:54:33 +000043void member_access(S *p) {
Richard Smith10483562012-10-25 21:59:45 +000044 // (1a) Check 'p' is appropriately sized and aligned for member access.
Richard Smith69d0d262012-08-24 00:54:33 +000045
46 // CHECK: icmp ne {{.*}}, null
47
48 // CHECK: %[[SIZE:.*]] = call i64 @llvm.objectsize.i64
49 // CHECK-NEXT: icmp uge i64 %[[SIZE]], 24
50
51 // CHECK: %[[PTRINT:.*]] = ptrtoint
52 // CHECK-NEXT: %[[MISALIGN:.*]] = and i64 %[[PTRINT]], 7
53 // CHECK-NEXT: icmp eq i64 %[[MISALIGN]], 0
54
Richard Smith10483562012-10-25 21:59:45 +000055 // (1b) Check that 'p' actually points to an 'S'.
56
57 // CHECK: %[[VPTRADDR:.*]] = bitcast {{.*}} to i64*
58 // CHECK-NEXT: %[[VPTR:.*]] = load i64* %[[VPTRADDR]]
59 //
60 // hash_16_bytes:
61 //
62 // If this number changes, it indicates that either the mangled name of ::S
63 // has changed, or that LLVM's hashing function has changed. The latter case
64 // is OK if the hashing function is still stable.
Richard Smith94211562012-10-25 22:27:30 +000065 //
66 // The two hash values are for 64- and 32-bit Clang binaries, respectively.
67 // FIXME: We should produce a 64-bit value either way.
68 //
69 // CHECK-NEXT: xor i64 {{-4030275160588942838|2562089159}}, %[[VPTR]]
Richard Smith10483562012-10-25 21:59:45 +000070 // CHECK-NEXT: mul i64 {{.*}}, -7070675565921424023
71 // CHECK-NEXT: lshr i64 {{.*}}, 47
72 // CHECK-NEXT: xor i64
73 // CHECK-NEXT: xor i64 %[[VPTR]]
74 // CHECK-NEXT: mul i64 {{.*}}, -7070675565921424023
75 // CHECK-NEXT: lshr i64 {{.*}}, 47
76 // CHECK-NEXT: xor i64
77 // CHECK-NEXT: %[[HASH:.*]] = mul i64 {{.*}}, -7070675565921424023
78 //
79 // Check the hash against the table:
80 //
81 // CHECK-NEXT: %[[IDX:.*]] = and i64 %{{.*}}, 127
82 // CHECK-NEXT: getelementptr inbounds [128 x i64]* @__ubsan_vptr_type_cache, i32 0, i64 %[[IDX]]
83 // CHECK-NEXT: %[[CACHEVAL:.*]] = load i64*
84 // CHECK-NEXT: icmp eq i64 %[[CACHEVAL]], %[[HASH]]
85 // CHECK-NEXT: br i1
86
Will Dietz3676d562012-12-30 20:53:28 +000087 // CHECK: call void @__ubsan_handle_dynamic_type_cache_miss({{.*}}, i64 %{{.*}}, i64 %[[HASH]])
Will Dietz88e02332012-12-02 19:50:33 +000088 // CHECK-NOT: unreachable
89 // CHECK: {{.*}}:
Richard Smith10483562012-10-25 21:59:45 +000090
Richard Smith69d0d262012-08-24 00:54:33 +000091 // (2) Check 'p->b' is appropriately sized and aligned for a load.
92
93 // FIXME: Suppress this in the trivial case of a member access, because we
94 // know we've just checked the member access expression itself.
95
96 // CHECK: %[[SIZE:.*]] = call i64 @llvm.objectsize.i64
97 // CHECK-NEXT: icmp uge i64 %[[SIZE]], 4
98
99 // CHECK: %[[PTRINT:.*]] = ptrtoint
100 // CHECK-NEXT: %[[MISALIGN:.*]] = and i64 %[[PTRINT]], 3
101 // CHECK-NEXT: icmp eq i64 %[[MISALIGN]], 0
102 int k = p->b;
103
Richard Smith10483562012-10-25 21:59:45 +0000104 // (3a) Check 'p' is appropriately sized and aligned for member function call.
Richard Smith69d0d262012-08-24 00:54:33 +0000105
106 // CHECK: icmp ne {{.*}}, null
107
108 // CHECK: %[[SIZE:.*]] = call i64 @llvm.objectsize.i64
109 // CHECK-NEXT: icmp uge i64 %[[SIZE]], 24
110
111 // CHECK: %[[PTRINT:.*]] = ptrtoint
112 // CHECK-NEXT: %[[MISALIGN:.*]] = and i64 %[[PTRINT]], 7
113 // CHECK-NEXT: icmp eq i64 %[[MISALIGN]], 0
Richard Smith10483562012-10-25 21:59:45 +0000114
115 // (3b) Check that 'p' actually points to an 'S'
116
117 // CHECK: load i64*
Richard Smith3750e772012-10-25 23:05:00 +0000118 // CHECK-NEXT: xor i64 {{-4030275160588942838|2562089159}},
Richard Smith10483562012-10-25 21:59:45 +0000119 // [...]
120 // CHECK: getelementptr inbounds [128 x i64]* @__ubsan_vptr_type_cache, i32 0, i64 %
121 // CHECK: br i1
Will Dietz3676d562012-12-30 20:53:28 +0000122 // CHECK: call void @__ubsan_handle_dynamic_type_cache_miss({{.*}}, i64 %{{.*}}, i64 %{{.*}})
Will Dietz88e02332012-12-02 19:50:33 +0000123 // CHECK-NOT: unreachable
124 // CHECK: {{.*}}:
Richard Smith10483562012-10-25 21:59:45 +0000125
Richard Smith69d0d262012-08-24 00:54:33 +0000126 k = p->f();
127}
Richard Smith3e056de2012-08-25 00:32:28 +0000128
Alexey Samsonovc9939332014-07-17 23:53:44 +0000129// CHECK-LABEL: @_Z12lsh_overflow
Richard Smith3e056de2012-08-25 00:32:28 +0000130int lsh_overflow(int a, int b) {
131 // CHECK: %[[INBOUNDS:.*]] = icmp ule i32 %[[RHS:.*]], 31
132 // CHECK-NEXT: br i1 %[[INBOUNDS]]
133
134 // CHECK: %[[SHIFTED_OUT_WIDTH:.*]] = sub nuw nsw i32 31, %[[RHS]]
135 // CHECK-NEXT: %[[SHIFTED_OUT:.*]] = lshr i32 %[[LHS:.*]], %[[SHIFTED_OUT_WIDTH]]
136
137 // This is present for C++11 but not for C: C++ core issue 1457 allows a '1'
138 // to be shifted into the sign bit, but not out of it.
139 // CHECK-NEXT: %[[SHIFTED_OUT_NOT_SIGN:.*]] = lshr i32 %[[SHIFTED_OUT]], 1
140
141 // CHECK-NEXT: %[[NO_OVERFLOW:.*]] = icmp eq i32 %[[SHIFTED_OUT_NOT_SIGN]], 0
Will Dietz11d0a9f2013-02-25 22:37:49 +0000142
143 // CHECK: %[[VALID:.*]] = phi i1 [ %[[INBOUNDS]], {{.*}} ], [ %[[NO_OVERFLOW]], {{.*}} ]
144 // CHECK-NEXT: br i1 %[[VALID]]
145
146 // CHECK: call void @__ubsan_handle_shift_out_of_bounds
147 // CHECK-NOT: call void @__ubsan_handle_shift_out_of_bounds
Richard Smith3e056de2012-08-25 00:32:28 +0000148
149 // CHECK: %[[RET:.*]] = shl i32 %[[LHS]], %[[RHS]]
150 // CHECK-NEXT: ret i32 %[[RET]]
151 return a << b;
152}
Richard Smith9f9e5822012-10-04 23:52:29 +0000153
Alexey Samsonovc9939332014-07-17 23:53:44 +0000154// CHECK-LABEL: @_Z9no_return
Richard Smith9f9e5822012-10-04 23:52:29 +0000155int no_return() {
Bill Wendlinge1c4a1b2013-02-22 09:10:20 +0000156 // CHECK: call void @__ubsan_handle_missing_return(i8* bitcast ({{.*}}* @{{.*}} to i8*)) [[NR_NUW:#[0-9]+]]
Richard Smithe30752c2012-10-09 19:52:38 +0000157 // CHECK-NEXT: unreachable
Richard Smith9f9e5822012-10-04 23:52:29 +0000158}
Richard Smith1629da92012-12-13 07:11:50 +0000159
Alexey Samsonovc9939332014-07-17 23:53:44 +0000160// CHECK-LABEL: @_Z9sour_bool
Richard Smith1629da92012-12-13 07:11:50 +0000161bool sour_bool(bool *p) {
162 // CHECK: %[[OK:.*]] = icmp ule i8 {{.*}}, 1
163 // CHECK: br i1 %[[OK]]
Will Dietz3676d562012-12-30 20:53:28 +0000164 // CHECK: call void @__ubsan_handle_load_invalid_value(i8* bitcast ({{.*}}), i64 {{.*}})
Richard Smith1629da92012-12-13 07:11:50 +0000165 return *p;
166}
167
168enum E1 { e1a = 0, e1b = 127 } e1;
169enum E2 { e2a = -1, e2b = 64 } e2;
170enum E3 { e3a = (1u << 31) - 1 } e3;
171
Alexey Samsonovc9939332014-07-17 23:53:44 +0000172// CHECK-LABEL: @_Z14bad_enum_value
Richard Smith1629da92012-12-13 07:11:50 +0000173int bad_enum_value() {
174 // CHECK: %[[E1:.*]] = icmp ule i32 {{.*}}, 127
175 // CHECK: br i1 %[[E1]]
Will Dietz3676d562012-12-30 20:53:28 +0000176 // CHECK: call void @__ubsan_handle_load_invalid_value(
Richard Smith1629da92012-12-13 07:11:50 +0000177 int a = e1;
178
179 // CHECK: %[[E2HI:.*]] = icmp sle i32 {{.*}}, 127
180 // CHECK: %[[E2LO:.*]] = icmp sge i32 {{.*}}, -128
181 // CHECK: %[[E2:.*]] = and i1 %[[E2HI]], %[[E2LO]]
182 // CHECK: br i1 %[[E2]]
Will Dietz3676d562012-12-30 20:53:28 +0000183 // CHECK: call void @__ubsan_handle_load_invalid_value(
Richard Smith1629da92012-12-13 07:11:50 +0000184 int b = e2;
185
186 // CHECK: %[[E3:.*]] = icmp ule i32 {{.*}}, 2147483647
187 // CHECK: br i1 %[[E3]]
Will Dietz3676d562012-12-30 20:53:28 +0000188 // CHECK: call void @__ubsan_handle_load_invalid_value(
Richard Smith1629da92012-12-13 07:11:50 +0000189 int c = e3;
190 return a + b + c;
191}
Richard Smith2c5868c2013-02-13 21:18:23 +0000192
Alexey Samsonovc9939332014-07-17 23:53:44 +0000193// CHECK-LABEL: @_Z20bad_downcast_pointer
Alexey Samsonov32f59d82014-07-18 18:15:39 +0000194// DOWNCAST-NULL-LABEL: @_Z20bad_downcast_pointer
Richard Smith2c5868c2013-02-13 21:18:23 +0000195void bad_downcast_pointer(S *p) {
196 // CHECK: %[[NONNULL:.*]] = icmp ne {{.*}}, null
197 // CHECK: br i1 %[[NONNULL]],
198
Alexey Samsonov32f59d82014-07-18 18:15:39 +0000199 // A null poiner access is guarded without -fsanitize=null.
200 // DOWNCAST-NULL: %[[NONNULL:.*]] = icmp ne {{.*}}, null
201 // DOWNCAST-NULL: br i1 %[[NONNULL]],
202
Matt Arsenault2f152632013-10-07 19:00:18 +0000203 // CHECK: %[[SIZE:.*]] = call i64 @llvm.objectsize.i64.p0i8(
Richard Smith2c5868c2013-02-13 21:18:23 +0000204 // CHECK: %[[E1:.*]] = icmp uge i64 %[[SIZE]], 24
205 // CHECK: %[[MISALIGN:.*]] = and i64 %{{.*}}, 7
206 // CHECK: %[[E2:.*]] = icmp eq i64 %[[MISALIGN]], 0
207 // CHECK: %[[E12:.*]] = and i1 %[[E1]], %[[E2]]
208 // CHECK: br i1 %[[E12]],
209
210 // CHECK: call void @__ubsan_handle_type_mismatch
211 // CHECK: br label
212
213 // CHECK: br i1 %{{.*}},
214
215 // CHECK: call void @__ubsan_handle_dynamic_type_cache_miss
216 // CHECK: br label
217 (void) static_cast<T*>(p);
218}
219
Alexey Samsonovc9939332014-07-17 23:53:44 +0000220// CHECK-LABEL: @_Z22bad_downcast_reference
Richard Smith2c5868c2013-02-13 21:18:23 +0000221void bad_downcast_reference(S &p) {
222 // CHECK: %[[E1:.*]] = icmp ne {{.*}}, null
223 // CHECK-NOT: br i1
Alexey Samsonove396bfc2014-11-11 22:03:54 +0000224
Matt Arsenault2f152632013-10-07 19:00:18 +0000225 // CHECK: %[[SIZE:.*]] = call i64 @llvm.objectsize.i64.p0i8(
Richard Smith2c5868c2013-02-13 21:18:23 +0000226 // CHECK: %[[E2:.*]] = icmp uge i64 %[[SIZE]], 24
Alexey Samsonove396bfc2014-11-11 22:03:54 +0000227
Richard Smith2c5868c2013-02-13 21:18:23 +0000228 // CHECK: %[[MISALIGN:.*]] = and i64 %{{.*}}, 7
229 // CHECK: %[[E3:.*]] = icmp eq i64 %[[MISALIGN]], 0
Alexey Samsonove396bfc2014-11-11 22:03:54 +0000230
231 // CHECK: %[[E12:.*]] = and i1 %[[E1]], %[[E2]]
Richard Smith2c5868c2013-02-13 21:18:23 +0000232 // CHECK: %[[E123:.*]] = and i1 %[[E12]], %[[E3]]
233 // CHECK: br i1 %[[E123]],
234
235 // CHECK: call void @__ubsan_handle_type_mismatch
236 // CHECK: br label
237
238 // CHECK: br i1 %{{.*}},
239
240 // CHECK: call void @__ubsan_handle_dynamic_type_cache_miss
241 // CHECK: br label
242 (void) static_cast<T&>(p);
243}
Bill Wendlinge1c4a1b2013-02-22 09:10:20 +0000244
Alexey Samsonovc9939332014-07-17 23:53:44 +0000245// CHECK-LABEL: @_Z11array_index
Richard Smith539e4a72013-02-23 02:53:19 +0000246int array_index(const int (&a)[4], int n) {
247 // CHECK: %[[K1_OK:.*]] = icmp ult i64 %{{.*}}, 4
248 // CHECK: br i1 %[[K1_OK]]
249 // CHECK: call void @__ubsan_handle_out_of_bounds(
250 int k1 = a[n];
251
252 // CHECK: %[[R1_OK:.*]] = icmp ule i64 %{{.*}}, 4
253 // CHECK: br i1 %[[R1_OK]]
254 // CHECK: call void @__ubsan_handle_out_of_bounds(
255 const int *r1 = &a[n];
256
257 // CHECK: %[[K2_OK:.*]] = icmp ult i64 %{{.*}}, 8
258 // CHECK: br i1 %[[K2_OK]]
259 // CHECK: call void @__ubsan_handle_out_of_bounds(
260 int k2 = ((const int(&)[8])a)[n];
261
262 // CHECK: %[[K3_OK:.*]] = icmp ult i64 %{{.*}}, 4
263 // CHECK: br i1 %[[K3_OK]]
264 // CHECK: call void @__ubsan_handle_out_of_bounds(
265 int k3 = n[a];
266
267 return k1 + *r1 + k2;
268}
269
Alexey Samsonovc9939332014-07-17 23:53:44 +0000270// CHECK-LABEL: @_Z17multi_array_index
Richard Smith539e4a72013-02-23 02:53:19 +0000271int multi_array_index(int n, int m) {
272 int arr[4][6];
273
274 // CHECK: %[[IDX2_OK:.*]] = icmp ult i64 %{{.*}}, 6
275 // CHECK: br i1 %[[IDX2_OK]]
276 // CHECK: call void @__ubsan_handle_out_of_bounds(
277
278 // CHECK: %[[IDX1_OK:.*]] = icmp ult i64 %{{.*}}, 4
279 // CHECK: br i1 %[[IDX1_OK]]
280 // CHECK: call void @__ubsan_handle_out_of_bounds(
281 return arr[n][m];
282}
283
Alexey Samsonovc9939332014-07-17 23:53:44 +0000284// CHECK-LABEL: @_Z11array_arith
Richard Smith539e4a72013-02-23 02:53:19 +0000285int array_arith(const int (&a)[4], int n) {
286 // CHECK: %[[K1_OK:.*]] = icmp ule i64 %{{.*}}, 4
287 // CHECK: br i1 %[[K1_OK]]
288 // CHECK: call void @__ubsan_handle_out_of_bounds(
289 const int *k1 = a + n;
290
291 // CHECK: %[[K2_OK:.*]] = icmp ule i64 %{{.*}}, 8
292 // CHECK: br i1 %[[K2_OK]]
293 // CHECK: call void @__ubsan_handle_out_of_bounds(
294 const int *k2 = (const int(&)[8])a + n;
295
296 return *k1 + *k2;
297}
298
299struct ArrayMembers {
300 int a1[5];
301 int a2[1];
302};
Alexey Samsonovc9939332014-07-17 23:53:44 +0000303// CHECK-LABEL: @_Z18struct_array_index
Richard Smith539e4a72013-02-23 02:53:19 +0000304int struct_array_index(ArrayMembers *p, int n) {
305 // CHECK: %[[IDX_OK:.*]] = icmp ult i64 %{{.*}}, 5
306 // CHECK: br i1 %[[IDX_OK]]
307 // CHECK: call void @__ubsan_handle_out_of_bounds(
308 return p->a1[n];
309}
310
Alexey Samsonovc9939332014-07-17 23:53:44 +0000311// CHECK-LABEL: @_Z16flex_array_index
Richard Smith539e4a72013-02-23 02:53:19 +0000312int flex_array_index(ArrayMembers *p, int n) {
313 // CHECK-NOT: call void @__ubsan_handle_out_of_bounds(
314 return p->a2[n];
315}
316
Richard Smith2847b222013-02-24 01:56:24 +0000317extern int incomplete[];
Alexey Samsonovc9939332014-07-17 23:53:44 +0000318// CHECK-LABEL: @_Z22incomplete_array_index
Richard Smith2847b222013-02-24 01:56:24 +0000319int incomplete_array_index(int n) {
320 // CHECK-NOT: call void @__ubsan_handle_out_of_bounds(
321 return incomplete[n];
322}
323
Richard Smith539e4a72013-02-23 02:53:19 +0000324typedef __attribute__((ext_vector_type(4))) int V4I;
Alexey Samsonovc9939332014-07-17 23:53:44 +0000325// CHECK-LABEL: @_Z12vector_index
Richard Smith539e4a72013-02-23 02:53:19 +0000326int vector_index(V4I v, int n) {
327 // CHECK: %[[IDX_OK:.*]] = icmp ult i64 %{{.*}}, 4
328 // CHECK: br i1 %[[IDX_OK]]
329 // CHECK: call void @__ubsan_handle_out_of_bounds(
330 return v[n];
331}
332
Alexey Samsonovc9939332014-07-17 23:53:44 +0000333// CHECK-LABEL: @_Z12string_index
Richard Smith539e4a72013-02-23 02:53:19 +0000334char string_index(int n) {
335 // CHECK: %[[IDX_OK:.*]] = icmp ult i64 %{{.*}}, 6
336 // CHECK: br i1 %[[IDX_OK]]
337 // CHECK: call void @__ubsan_handle_out_of_bounds(
338 return "Hello"[n];
339}
340
Filipe Cabecinhas178a8df2013-08-08 01:08:17 +0000341class A // align=4
342{
343 int a1, a2, a3;
344};
345
346class B // align=8
347{
348 long b1, b2;
349};
350
351class C : public A, public B // align=16
352{
353 alignas(16) int c1;
354};
355
356// Make sure we check the alignment of the pointer after subtracting any
357// offset. The pointer before subtraction doesn't need to be aligned for
358// the destination type.
359
Nick Lewycky8b4e3792013-09-11 02:03:20 +0000360// CHECK-LABEL: define void @_Z16downcast_pointerP1B(%class.B* %b)
Filipe Cabecinhas178a8df2013-08-08 01:08:17 +0000361void downcast_pointer(B *b) {
362 (void) static_cast<C*>(b);
363 // Alignment check from EmitTypeCheck(TCK_DowncastPointer, ...)
Filipe Cabecinhas496fec42013-08-08 01:18:31 +0000364 // CHECK: [[SUB:%[.a-z0-9]*]] = getelementptr i8* {{.*}}, i64 -16
Filipe Cabecinhas178a8df2013-08-08 01:08:17 +0000365 // CHECK-NEXT: [[C:%[0-9]*]] = bitcast i8* [[SUB]] to %class.C*
366 // null check goes here
Filipe Cabecinhasb2eb1d92013-08-08 01:24:29 +0000367 // CHECK: [[FROM_PHI:%[0-9]*]] = phi %class.C* [ [[C]], {{.*}} ], {{.*}}
Filipe Cabecinhas178a8df2013-08-08 01:08:17 +0000368 // Objectsize check goes here
369 // CHECK: [[C_INT:%[0-9]*]] = ptrtoint %class.C* [[FROM_PHI]] to i64
370 // CHECK-NEXT: [[MASKED:%[0-9]*]] = and i64 [[C_INT]], 15
371 // CHECK-NEXT: [[TEST:%[0-9]*]] = icmp eq i64 [[MASKED]], 0
372 // AND the alignment test with the objectsize test.
373 // CHECK-NEXT: [[AND:%[0-9]*]] = and i1 {{.*}}, [[TEST]]
Filipe Cabecinhasb2eb1d92013-08-08 01:24:29 +0000374 // CHECK-NEXT: br i1 [[AND]]
Filipe Cabecinhas178a8df2013-08-08 01:08:17 +0000375}
376
Hal Finkela2347ba2014-07-18 15:52:10 +0000377// CHECK-LABEL: define void @_Z18downcast_referenceR1B(%class.B* dereferenceable({{[0-9]+}}) %b)
Filipe Cabecinhas178a8df2013-08-08 01:08:17 +0000378void downcast_reference(B &b) {
379 (void) static_cast<C&>(b);
380 // Alignment check from EmitTypeCheck(TCK_DowncastReference, ...)
Filipe Cabecinhas496fec42013-08-08 01:18:31 +0000381 // CHECK: [[SUB:%[.a-z0-9]*]] = getelementptr i8* {{.*}}, i64 -16
Filipe Cabecinhas178a8df2013-08-08 01:08:17 +0000382 // CHECK-NEXT: [[C:%[0-9]*]] = bitcast i8* [[SUB]] to %class.C*
383 // Objectsize check goes here
384 // CHECK: [[C_INT:%[0-9]*]] = ptrtoint %class.C* [[C]] to i64
385 // CHECK-NEXT: [[MASKED:%[0-9]*]] = and i64 [[C_INT]], 15
386 // CHECK-NEXT: [[TEST:%[0-9]*]] = icmp eq i64 [[MASKED]], 0
387 // AND the alignment test with the objectsize test.
Alexey Samsonove396bfc2014-11-11 22:03:54 +0000388 // CHECK: [[AND:%[0-9]*]] = and i1 {{.*}}, [[TEST]]
Filipe Cabecinhasb2eb1d92013-08-08 01:24:29 +0000389 // CHECK-NEXT: br i1 [[AND]]
Filipe Cabecinhas178a8df2013-08-08 01:08:17 +0000390}
391
Peter Collingbourne6b46e382014-12-03 02:37:10 +0000392// CHECK-LABEL: @_Z22indirect_function_callPFviE({{.*}} prologue <{ i32, i8* }> <{ i32 1413876459, i8* bitcast ({ i8*, i8* }* @_ZTIFvPFviEE to i8*) }>
Peter Collingbourneb453cd62013-10-20 21:29:19 +0000393void indirect_function_call(void (*p)(int)) {
394 // CHECK: [[PTR:%[0-9]*]] = bitcast void (i32)* {{.*}} to <{ i32, i8* }>*
395
396 // Signature check
397 // CHECK-NEXT: [[SIGPTR:%[0-9]*]] = getelementptr <{ i32, i8* }>* [[PTR]], i32 0, i32 0
398 // CHECK-NEXT: [[SIG:%[0-9]*]] = load i32* [[SIGPTR]]
399 // CHECK-NEXT: [[SIGCMP:%[0-9]*]] = icmp eq i32 [[SIG]], 1413876459
400 // CHECK-NEXT: br i1 [[SIGCMP]]
401
402 // RTTI pointer check
403 // CHECK: [[RTTIPTR:%[0-9]*]] = getelementptr <{ i32, i8* }>* [[PTR]], i32 0, i32 1
404 // CHECK-NEXT: [[RTTI:%[0-9]*]] = load i8** [[RTTIPTR]]
405 // CHECK-NEXT: [[RTTICMP:%[0-9]*]] = icmp eq i8* [[RTTI]], bitcast ({ i8*, i8* }* @_ZTIFviE to i8*)
406 // CHECK-NEXT: br i1 [[RTTICMP]]
407 p(42);
408}
409
Alexey Samsonoveb47d8a2014-10-13 23:59:00 +0000410namespace UpcastPointerTest {
411struct S {};
412struct T : S { double d; };
413struct V : virtual S {};
414
415// CHECK-LABEL: upcast_pointer
416S* upcast_pointer(T* t) {
417 // Check for null pointer
418 // CHECK: %[[NONNULL:.*]] = icmp ne {{.*}}, null
419 // CHECK: br i1 %[[NONNULL]]
420
421 // Check alignment
422 // CHECK: %[[MISALIGN:.*]] = and i64 %{{.*}}, 7
423 // CHECK: icmp eq i64 %[[MISALIGN]], 0
424
425 // CHECK: call void @__ubsan_handle_type_mismatch
426 return t;
427}
428
429V getV();
430
431// CHECK-LABEL: upcast_to_vbase
432void upcast_to_vbase() {
433 // No need to check for null here, as we have a temporary here.
434
435 // CHECK-NOT: br i1
436
437 // CHECK: call i64 @llvm.objectsize
438 // CHECK: call void @__ubsan_handle_type_mismatch
439 // CHECK: call void @__ubsan_handle_dynamic_type_cache_miss
440 const S& s = getV();
441}
442}
443
Nick Lewycky8b4e3792013-09-11 02:03:20 +0000444namespace CopyValueRepresentation {
445 // CHECK-LABEL: define {{.*}} @_ZN23CopyValueRepresentation2S3aSERKS0_
446 // CHECK-NOT: call {{.*}} @__ubsan_handle_load_invalid_value
447 // CHECK-LABEL: define {{.*}} @_ZN23CopyValueRepresentation2S4aSEOS0_
448 // CHECK-NOT: call {{.*}} @__ubsan_handle_load_invalid_value
449 // CHECK-LABEL: define {{.*}} @_ZN23CopyValueRepresentation2S5C2ERKS0_
450 // CHECK-NOT: call {{.*}} __ubsan_handle_load_invalid_value
451 // CHECK-LABEL: define {{.*}} @_ZN23CopyValueRepresentation2S2C2ERKS0_
452 // CHECK: __ubsan_handle_load_invalid_value
453 // CHECK-LABEL: define {{.*}} @_ZN23CopyValueRepresentation2S1C2ERKS0_
454 // CHECK-NOT: call {{.*}} __ubsan_handle_load_invalid_value
455
456 struct CustomCopy { CustomCopy(); CustomCopy(const CustomCopy&); };
457 struct S1 {
458 CustomCopy CC;
459 bool b;
460 };
461 void callee1(S1);
462 void test1() {
463 S1 s11;
464 callee1(s11);
465 S1 s12;
466 s12 = s11;
467 }
468
469 static bool some_global_bool;
470 struct ExprCopy {
471 ExprCopy();
472 ExprCopy(const ExprCopy&, bool b = some_global_bool);
473 };
474 struct S2 {
475 ExprCopy EC;
476 bool b;
477 };
478 void callee2(S2);
479 void test2(void) {
480 S2 s21;
481 callee2(s21);
482 S2 s22;
483 s22 = s21;
484 }
485
486 struct CustomAssign { CustomAssign &operator=(const CustomAssign&); };
487 struct S3 {
488 CustomAssign CA;
489 bool b;
490 };
491 void test3() {
492 S3 x, y;
493 x = y;
494 }
495
496 struct CustomMove {
497 CustomMove();
498 CustomMove(const CustomMove&&);
499 CustomMove &operator=(const CustomMove&&);
500 };
501 struct S4 {
502 CustomMove CM;
503 bool b;
504 };
505 void test4() {
506 S4 x, y;
507 x = static_cast<S4&&>(y);
508 }
509
510 struct EnumCustomCopy {
511 EnumCustomCopy();
512 EnumCustomCopy(const EnumCustomCopy&);
513 };
514 struct S5 {
515 EnumCustomCopy ECC;
516 bool b;
517 };
518 void callee5(S5);
519 void test5() {
520 S5 s51;
521 callee5(s51);
522 S5 s52;
523 s52 = s51;
524 }
525}
526
Bill Wendlinge1c4a1b2013-02-22 09:10:20 +0000527// CHECK: attributes [[NR_NUW]] = { noreturn nounwind }