blob: 333855d0ba612f869b01405dc20e0bedaa690273 [file] [log] [blame]
Richard Smith6b53e222013-10-22 22:51:04 +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 -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck %s
Alexey Samsonov6c124142014-07-18 17:50:06 +00002// RUN: %clang_cc1 -std=c++11 -fsanitize=vptr,address -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck %s --check-prefix=CHECK-ASAN
Alexey Samsonov32f59d82014-07-18 18:15:39 +00003// RUN: %clang_cc1 -std=c++11 -fsanitize=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
Matt Arsenault2f152632013-10-07 19:00:18 +0000224 // CHECK: %[[SIZE:.*]] = call i64 @llvm.objectsize.i64.p0i8(
Richard Smith2c5868c2013-02-13 21:18:23 +0000225 // CHECK: %[[E2:.*]] = icmp uge i64 %[[SIZE]], 24
226 // CHECK: %[[E12:.*]] = and i1 %[[E1]], %[[E2]]
227 // CHECK: %[[MISALIGN:.*]] = and i64 %{{.*}}, 7
228 // CHECK: %[[E3:.*]] = icmp eq i64 %[[MISALIGN]], 0
229 // CHECK: %[[E123:.*]] = and i1 %[[E12]], %[[E3]]
230 // CHECK: br i1 %[[E123]],
231
232 // CHECK: call void @__ubsan_handle_type_mismatch
233 // CHECK: br label
234
235 // CHECK: br i1 %{{.*}},
236
237 // CHECK: call void @__ubsan_handle_dynamic_type_cache_miss
238 // CHECK: br label
239 (void) static_cast<T&>(p);
240}
Bill Wendlinge1c4a1b2013-02-22 09:10:20 +0000241
Alexey Samsonovc9939332014-07-17 23:53:44 +0000242// CHECK-LABEL: @_Z11array_index
Richard Smith539e4a72013-02-23 02:53:19 +0000243int array_index(const int (&a)[4], int n) {
244 // CHECK: %[[K1_OK:.*]] = icmp ult i64 %{{.*}}, 4
245 // CHECK: br i1 %[[K1_OK]]
246 // CHECK: call void @__ubsan_handle_out_of_bounds(
247 int k1 = a[n];
248
249 // CHECK: %[[R1_OK:.*]] = icmp ule i64 %{{.*}}, 4
250 // CHECK: br i1 %[[R1_OK]]
251 // CHECK: call void @__ubsan_handle_out_of_bounds(
252 const int *r1 = &a[n];
253
254 // CHECK: %[[K2_OK:.*]] = icmp ult i64 %{{.*}}, 8
255 // CHECK: br i1 %[[K2_OK]]
256 // CHECK: call void @__ubsan_handle_out_of_bounds(
257 int k2 = ((const int(&)[8])a)[n];
258
259 // CHECK: %[[K3_OK:.*]] = icmp ult i64 %{{.*}}, 4
260 // CHECK: br i1 %[[K3_OK]]
261 // CHECK: call void @__ubsan_handle_out_of_bounds(
262 int k3 = n[a];
263
264 return k1 + *r1 + k2;
265}
266
Alexey Samsonovc9939332014-07-17 23:53:44 +0000267// CHECK-LABEL: @_Z17multi_array_index
Richard Smith539e4a72013-02-23 02:53:19 +0000268int multi_array_index(int n, int m) {
269 int arr[4][6];
270
271 // CHECK: %[[IDX2_OK:.*]] = icmp ult i64 %{{.*}}, 6
272 // CHECK: br i1 %[[IDX2_OK]]
273 // CHECK: call void @__ubsan_handle_out_of_bounds(
274
275 // CHECK: %[[IDX1_OK:.*]] = icmp ult i64 %{{.*}}, 4
276 // CHECK: br i1 %[[IDX1_OK]]
277 // CHECK: call void @__ubsan_handle_out_of_bounds(
278 return arr[n][m];
279}
280
Alexey Samsonovc9939332014-07-17 23:53:44 +0000281// CHECK-LABEL: @_Z11array_arith
Richard Smith539e4a72013-02-23 02:53:19 +0000282int array_arith(const int (&a)[4], int n) {
283 // CHECK: %[[K1_OK:.*]] = icmp ule i64 %{{.*}}, 4
284 // CHECK: br i1 %[[K1_OK]]
285 // CHECK: call void @__ubsan_handle_out_of_bounds(
286 const int *k1 = a + n;
287
288 // CHECK: %[[K2_OK:.*]] = icmp ule i64 %{{.*}}, 8
289 // CHECK: br i1 %[[K2_OK]]
290 // CHECK: call void @__ubsan_handle_out_of_bounds(
291 const int *k2 = (const int(&)[8])a + n;
292
293 return *k1 + *k2;
294}
295
296struct ArrayMembers {
297 int a1[5];
298 int a2[1];
299};
Alexey Samsonovc9939332014-07-17 23:53:44 +0000300// CHECK-LABEL: @_Z18struct_array_index
Richard Smith539e4a72013-02-23 02:53:19 +0000301int struct_array_index(ArrayMembers *p, int n) {
302 // CHECK: %[[IDX_OK:.*]] = icmp ult i64 %{{.*}}, 5
303 // CHECK: br i1 %[[IDX_OK]]
304 // CHECK: call void @__ubsan_handle_out_of_bounds(
305 return p->a1[n];
306}
307
Alexey Samsonovc9939332014-07-17 23:53:44 +0000308// CHECK-LABEL: @_Z16flex_array_index
Richard Smith539e4a72013-02-23 02:53:19 +0000309int flex_array_index(ArrayMembers *p, int n) {
310 // CHECK-NOT: call void @__ubsan_handle_out_of_bounds(
311 return p->a2[n];
312}
313
Richard Smith2847b222013-02-24 01:56:24 +0000314extern int incomplete[];
Alexey Samsonovc9939332014-07-17 23:53:44 +0000315// CHECK-LABEL: @_Z22incomplete_array_index
Richard Smith2847b222013-02-24 01:56:24 +0000316int incomplete_array_index(int n) {
317 // CHECK-NOT: call void @__ubsan_handle_out_of_bounds(
318 return incomplete[n];
319}
320
Richard Smith539e4a72013-02-23 02:53:19 +0000321typedef __attribute__((ext_vector_type(4))) int V4I;
Alexey Samsonovc9939332014-07-17 23:53:44 +0000322// CHECK-LABEL: @_Z12vector_index
Richard Smith539e4a72013-02-23 02:53:19 +0000323int vector_index(V4I v, int n) {
324 // CHECK: %[[IDX_OK:.*]] = icmp ult i64 %{{.*}}, 4
325 // CHECK: br i1 %[[IDX_OK]]
326 // CHECK: call void @__ubsan_handle_out_of_bounds(
327 return v[n];
328}
329
Alexey Samsonovc9939332014-07-17 23:53:44 +0000330// CHECK-LABEL: @_Z12string_index
Richard Smith539e4a72013-02-23 02:53:19 +0000331char string_index(int n) {
332 // CHECK: %[[IDX_OK:.*]] = icmp ult i64 %{{.*}}, 6
333 // CHECK: br i1 %[[IDX_OK]]
334 // CHECK: call void @__ubsan_handle_out_of_bounds(
335 return "Hello"[n];
336}
337
Filipe Cabecinhas178a8df2013-08-08 01:08:17 +0000338class A // align=4
339{
340 int a1, a2, a3;
341};
342
343class B // align=8
344{
345 long b1, b2;
346};
347
348class C : public A, public B // align=16
349{
350 alignas(16) int c1;
351};
352
353// Make sure we check the alignment of the pointer after subtracting any
354// offset. The pointer before subtraction doesn't need to be aligned for
355// the destination type.
356
Nick Lewycky8b4e3792013-09-11 02:03:20 +0000357// CHECK-LABEL: define void @_Z16downcast_pointerP1B(%class.B* %b)
Filipe Cabecinhas178a8df2013-08-08 01:08:17 +0000358void downcast_pointer(B *b) {
359 (void) static_cast<C*>(b);
360 // Alignment check from EmitTypeCheck(TCK_DowncastPointer, ...)
Filipe Cabecinhas496fec42013-08-08 01:18:31 +0000361 // CHECK: [[SUB:%[.a-z0-9]*]] = getelementptr i8* {{.*}}, i64 -16
Filipe Cabecinhas178a8df2013-08-08 01:08:17 +0000362 // CHECK-NEXT: [[C:%[0-9]*]] = bitcast i8* [[SUB]] to %class.C*
363 // null check goes here
Filipe Cabecinhasb2eb1d92013-08-08 01:24:29 +0000364 // CHECK: [[FROM_PHI:%[0-9]*]] = phi %class.C* [ [[C]], {{.*}} ], {{.*}}
Filipe Cabecinhas178a8df2013-08-08 01:08:17 +0000365 // Objectsize check goes here
366 // CHECK: [[C_INT:%[0-9]*]] = ptrtoint %class.C* [[FROM_PHI]] to i64
367 // CHECK-NEXT: [[MASKED:%[0-9]*]] = and i64 [[C_INT]], 15
368 // CHECK-NEXT: [[TEST:%[0-9]*]] = icmp eq i64 [[MASKED]], 0
369 // AND the alignment test with the objectsize test.
370 // CHECK-NEXT: [[AND:%[0-9]*]] = and i1 {{.*}}, [[TEST]]
Filipe Cabecinhasb2eb1d92013-08-08 01:24:29 +0000371 // CHECK-NEXT: br i1 [[AND]]
Filipe Cabecinhas178a8df2013-08-08 01:08:17 +0000372}
373
Hal Finkela2347ba2014-07-18 15:52:10 +0000374// CHECK-LABEL: define void @_Z18downcast_referenceR1B(%class.B* dereferenceable({{[0-9]+}}) %b)
Filipe Cabecinhas178a8df2013-08-08 01:08:17 +0000375void downcast_reference(B &b) {
376 (void) static_cast<C&>(b);
377 // Alignment check from EmitTypeCheck(TCK_DowncastReference, ...)
Filipe Cabecinhas496fec42013-08-08 01:18:31 +0000378 // CHECK: [[SUB:%[.a-z0-9]*]] = getelementptr i8* {{.*}}, i64 -16
Filipe Cabecinhas178a8df2013-08-08 01:08:17 +0000379 // CHECK-NEXT: [[C:%[0-9]*]] = bitcast i8* [[SUB]] to %class.C*
380 // Objectsize check goes here
381 // CHECK: [[C_INT:%[0-9]*]] = ptrtoint %class.C* [[C]] to i64
382 // CHECK-NEXT: [[MASKED:%[0-9]*]] = and i64 [[C_INT]], 15
383 // CHECK-NEXT: [[TEST:%[0-9]*]] = icmp eq i64 [[MASKED]], 0
384 // AND the alignment test with the objectsize test.
385 // CHECK-NEXT: [[AND:%[0-9]*]] = and i1 {{.*}}, [[TEST]]
Filipe Cabecinhasb2eb1d92013-08-08 01:24:29 +0000386 // CHECK-NEXT: br i1 [[AND]]
Filipe Cabecinhas178a8df2013-08-08 01:08:17 +0000387}
388
Peter Collingbourneb453cd62013-10-20 21:29:19 +0000389// CHECK-LABEL: @_Z22indirect_function_callPFviE({{.*}} prefix <{ i32, i8* }> <{ i32 1413876459, i8* bitcast ({ i8*, i8* }* @_ZTIFvPFviEE to i8*) }>
390void indirect_function_call(void (*p)(int)) {
391 // CHECK: [[PTR:%[0-9]*]] = bitcast void (i32)* {{.*}} to <{ i32, i8* }>*
392
393 // Signature check
394 // CHECK-NEXT: [[SIGPTR:%[0-9]*]] = getelementptr <{ i32, i8* }>* [[PTR]], i32 0, i32 0
395 // CHECK-NEXT: [[SIG:%[0-9]*]] = load i32* [[SIGPTR]]
396 // CHECK-NEXT: [[SIGCMP:%[0-9]*]] = icmp eq i32 [[SIG]], 1413876459
397 // CHECK-NEXT: br i1 [[SIGCMP]]
398
399 // RTTI pointer check
400 // CHECK: [[RTTIPTR:%[0-9]*]] = getelementptr <{ i32, i8* }>* [[PTR]], i32 0, i32 1
401 // CHECK-NEXT: [[RTTI:%[0-9]*]] = load i8** [[RTTIPTR]]
402 // CHECK-NEXT: [[RTTICMP:%[0-9]*]] = icmp eq i8* [[RTTI]], bitcast ({ i8*, i8* }* @_ZTIFviE to i8*)
403 // CHECK-NEXT: br i1 [[RTTICMP]]
404 p(42);
405}
406
Nick Lewycky8b4e3792013-09-11 02:03:20 +0000407namespace CopyValueRepresentation {
408 // CHECK-LABEL: define {{.*}} @_ZN23CopyValueRepresentation2S3aSERKS0_
409 // CHECK-NOT: call {{.*}} @__ubsan_handle_load_invalid_value
410 // CHECK-LABEL: define {{.*}} @_ZN23CopyValueRepresentation2S4aSEOS0_
411 // CHECK-NOT: call {{.*}} @__ubsan_handle_load_invalid_value
412 // CHECK-LABEL: define {{.*}} @_ZN23CopyValueRepresentation2S5C2ERKS0_
413 // CHECK-NOT: call {{.*}} __ubsan_handle_load_invalid_value
414 // CHECK-LABEL: define {{.*}} @_ZN23CopyValueRepresentation2S2C2ERKS0_
415 // CHECK: __ubsan_handle_load_invalid_value
416 // CHECK-LABEL: define {{.*}} @_ZN23CopyValueRepresentation2S1C2ERKS0_
417 // CHECK-NOT: call {{.*}} __ubsan_handle_load_invalid_value
418
419 struct CustomCopy { CustomCopy(); CustomCopy(const CustomCopy&); };
420 struct S1 {
421 CustomCopy CC;
422 bool b;
423 };
424 void callee1(S1);
425 void test1() {
426 S1 s11;
427 callee1(s11);
428 S1 s12;
429 s12 = s11;
430 }
431
432 static bool some_global_bool;
433 struct ExprCopy {
434 ExprCopy();
435 ExprCopy(const ExprCopy&, bool b = some_global_bool);
436 };
437 struct S2 {
438 ExprCopy EC;
439 bool b;
440 };
441 void callee2(S2);
442 void test2(void) {
443 S2 s21;
444 callee2(s21);
445 S2 s22;
446 s22 = s21;
447 }
448
449 struct CustomAssign { CustomAssign &operator=(const CustomAssign&); };
450 struct S3 {
451 CustomAssign CA;
452 bool b;
453 };
454 void test3() {
455 S3 x, y;
456 x = y;
457 }
458
459 struct CustomMove {
460 CustomMove();
461 CustomMove(const CustomMove&&);
462 CustomMove &operator=(const CustomMove&&);
463 };
464 struct S4 {
465 CustomMove CM;
466 bool b;
467 };
468 void test4() {
469 S4 x, y;
470 x = static_cast<S4&&>(y);
471 }
472
473 struct EnumCustomCopy {
474 EnumCustomCopy();
475 EnumCustomCopy(const EnumCustomCopy&);
476 };
477 struct S5 {
478 EnumCustomCopy ECC;
479 bool b;
480 };
481 void callee5(S5);
482 void test5() {
483 S5 s51;
484 callee5(s51);
485 S5 s52;
486 s52 = s51;
487 }
488}
489
Bill Wendlinge1c4a1b2013-02-22 09:10:20 +0000490// CHECK: attributes [[NR_NUW]] = { noreturn nounwind }