blob: 3ca7f6d65ad488cafe768a0ba425f45ba21b0857 [file] [log] [blame]
NAKAMURA Takumif9c52dc2015-09-15 09:50:24 +00001// 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
Alexey Samsonov88459522015-01-12 22:39:12 +00002// 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
Peter Collingbourne69b004d2015-02-25 23:18:42 +00004// 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
Richard Smith69d0d262012-08-24 00:54:33 +00006
Richard Smithbe024a82012-12-18 00:22:45 +00007struct S {
8 double d;
9 int a, b;
10 virtual int f();
11};
12
Alexey Samsonov6c124142014-07-18 17:50:06 +000013// 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
Richard Smith2c5868c2013-02-13 21:18:23 +000019struct T : S {};
20
Alexey Samsonovc9939332014-07-17 23:53:44 +000021// CHECK-LABEL: @_Z17reference_binding
Richard Smithbe024a82012-12-18 00:22:45 +000022void reference_binding(int *p, S *q) {
Richard Smith69d0d262012-08-24 00:54:33 +000023 // C++ core issue 453: If an lvalue to which a reference is directly bound
24 // designates neither an existing object or function of an appropriate type,
25 // nor a region of storage of suitable size and alignment to contain an object
26 // of the reference's type, the behavior is undefined.
27
28 // CHECK: icmp ne {{.*}}, null
29
30 // CHECK: %[[SIZE:.*]] = call i64 @llvm.objectsize.i64
31 // CHECK-NEXT: icmp uge i64 %[[SIZE]], 4
32
33 // CHECK: %[[PTRINT:.*]] = ptrtoint
34 // CHECK-NEXT: %[[MISALIGN:.*]] = and i64 %[[PTRINT]], 3
35 // CHECK-NEXT: icmp eq i64 %[[MISALIGN]], 0
36 int &r = *p;
Richard Smith69d0d262012-08-24 00:54:33 +000037
Richard Smithbe024a82012-12-18 00:22:45 +000038 // A reference is not required to refer to an object within its lifetime.
39 // CHECK-NOT: __ubsan_handle_dynamic_type_cache_miss
40 S &r2 = *q;
41}
Richard Smith69d0d262012-08-24 00:54:33 +000042
Alexey Samsonovc9939332014-07-17 23:53:44 +000043// CHECK-LABEL: @_Z13member_access
Alexey Samsonov6c124142014-07-18 17:50:06 +000044// CHECK-ASAN-LABEL: @_Z13member_access
Richard Smith69d0d262012-08-24 00:54:33 +000045void member_access(S *p) {
Richard Smith10483562012-10-25 21:59:45 +000046 // (1a) Check 'p' is appropriately sized and aligned for member access.
Richard Smith69d0d262012-08-24 00:54:33 +000047
48 // CHECK: icmp ne {{.*}}, null
49
50 // CHECK: %[[SIZE:.*]] = call i64 @llvm.objectsize.i64
51 // CHECK-NEXT: icmp uge i64 %[[SIZE]], 24
52
53 // CHECK: %[[PTRINT:.*]] = ptrtoint
54 // CHECK-NEXT: %[[MISALIGN:.*]] = and i64 %[[PTRINT]], 7
55 // CHECK-NEXT: icmp eq i64 %[[MISALIGN]], 0
56
Richard Smith10483562012-10-25 21:59:45 +000057 // (1b) Check that 'p' actually points to an 'S'.
58
59 // CHECK: %[[VPTRADDR:.*]] = bitcast {{.*}} to i64*
David Blaikiea953f282015-02-27 21:19:58 +000060 // CHECK-NEXT: %[[VPTR:.*]] = load i64, i64* %[[VPTRADDR]]
Richard Smith10483562012-10-25 21:59:45 +000061 //
62 // hash_16_bytes:
63 //
64 // If this number changes, it indicates that either the mangled name of ::S
65 // has changed, or that LLVM's hashing function has changed. The latter case
66 // is OK if the hashing function is still stable.
Richard Smith94211562012-10-25 22:27:30 +000067 //
68 // The two hash values are for 64- and 32-bit Clang binaries, respectively.
69 // FIXME: We should produce a 64-bit value either way.
70 //
71 // CHECK-NEXT: xor i64 {{-4030275160588942838|2562089159}}, %[[VPTR]]
Richard Smith10483562012-10-25 21:59:45 +000072 // CHECK-NEXT: mul i64 {{.*}}, -7070675565921424023
73 // CHECK-NEXT: lshr i64 {{.*}}, 47
74 // CHECK-NEXT: xor i64
75 // CHECK-NEXT: xor i64 %[[VPTR]]
76 // CHECK-NEXT: mul i64 {{.*}}, -7070675565921424023
77 // CHECK-NEXT: lshr i64 {{.*}}, 47
78 // CHECK-NEXT: xor i64
79 // CHECK-NEXT: %[[HASH:.*]] = mul i64 {{.*}}, -7070675565921424023
80 //
81 // Check the hash against the table:
82 //
83 // CHECK-NEXT: %[[IDX:.*]] = and i64 %{{.*}}, 127
David Blaikie218b7832015-02-27 19:18:17 +000084 // CHECK-NEXT: getelementptr inbounds [128 x i64], [128 x i64]* @__ubsan_vptr_type_cache, i32 0, i64 %[[IDX]]
David Blaikiea953f282015-02-27 21:19:58 +000085 // CHECK-NEXT: %[[CACHEVAL:.*]] = load i64, i64*
Richard Smith10483562012-10-25 21:59:45 +000086 // CHECK-NEXT: icmp eq i64 %[[CACHEVAL]], %[[HASH]]
87 // CHECK-NEXT: br i1
88
Will Dietz3676d562012-12-30 20:53:28 +000089 // CHECK: call void @__ubsan_handle_dynamic_type_cache_miss({{.*}}, i64 %{{.*}}, i64 %[[HASH]])
Will Dietz88e02332012-12-02 19:50:33 +000090 // CHECK-NOT: unreachable
91 // CHECK: {{.*}}:
Richard Smith10483562012-10-25 21:59:45 +000092
Richard Smith69d0d262012-08-24 00:54:33 +000093 // (2) Check 'p->b' is appropriately sized and aligned for a load.
94
95 // FIXME: Suppress this in the trivial case of a member access, because we
96 // know we've just checked the member access expression itself.
97
98 // CHECK: %[[SIZE:.*]] = call i64 @llvm.objectsize.i64
99 // CHECK-NEXT: icmp uge i64 %[[SIZE]], 4
100
101 // CHECK: %[[PTRINT:.*]] = ptrtoint
102 // CHECK-NEXT: %[[MISALIGN:.*]] = and i64 %[[PTRINT]], 3
103 // CHECK-NEXT: icmp eq i64 %[[MISALIGN]], 0
104 int k = p->b;
105
Richard Smith10483562012-10-25 21:59:45 +0000106 // (3a) Check 'p' is appropriately sized and aligned for member function call.
Richard Smith69d0d262012-08-24 00:54:33 +0000107
108 // CHECK: icmp ne {{.*}}, null
109
110 // CHECK: %[[SIZE:.*]] = call i64 @llvm.objectsize.i64
111 // CHECK-NEXT: icmp uge i64 %[[SIZE]], 24
112
113 // CHECK: %[[PTRINT:.*]] = ptrtoint
114 // CHECK-NEXT: %[[MISALIGN:.*]] = and i64 %[[PTRINT]], 7
115 // CHECK-NEXT: icmp eq i64 %[[MISALIGN]], 0
Richard Smith10483562012-10-25 21:59:45 +0000116
117 // (3b) Check that 'p' actually points to an 'S'
118
David Blaikiea953f282015-02-27 21:19:58 +0000119 // CHECK: load i64, i64*
Richard Smith3750e772012-10-25 23:05:00 +0000120 // CHECK-NEXT: xor i64 {{-4030275160588942838|2562089159}},
Richard Smith10483562012-10-25 21:59:45 +0000121 // [...]
David Blaikie218b7832015-02-27 19:18:17 +0000122 // CHECK: getelementptr inbounds [128 x i64], [128 x i64]* @__ubsan_vptr_type_cache, i32 0, i64 %
Richard Smith10483562012-10-25 21:59:45 +0000123 // CHECK: br i1
Will Dietz3676d562012-12-30 20:53:28 +0000124 // CHECK: call void @__ubsan_handle_dynamic_type_cache_miss({{.*}}, i64 %{{.*}}, i64 %{{.*}})
Will Dietz88e02332012-12-02 19:50:33 +0000125 // CHECK-NOT: unreachable
126 // CHECK: {{.*}}:
Richard Smith10483562012-10-25 21:59:45 +0000127
Richard Smith69d0d262012-08-24 00:54:33 +0000128 k = p->f();
129}
Richard Smith3e056de2012-08-25 00:32:28 +0000130
Alexey Samsonovc9939332014-07-17 23:53:44 +0000131// CHECK-LABEL: @_Z12lsh_overflow
Richard Smith3e056de2012-08-25 00:32:28 +0000132int lsh_overflow(int a, int b) {
Alexey Samsonov21d2dda2015-03-09 21:50:19 +0000133 // CHECK: %[[RHS_INBOUNDS:.*]] = icmp ule i32 %[[RHS:.*]], 31
134 // CHECK-NEXT: br i1 %[[RHS_INBOUNDS]], label %[[CHECK_BB:.*]], label %[[CONT_BB:.*]],
Alexey Samsonov48a9db02015-03-05 21:57:35 +0000135
Alexey Samsonov21d2dda2015-03-09 21:50:19 +0000136 // CHECK: [[CHECK_BB]]:
137 // CHECK-NEXT: %[[SHIFTED_OUT_WIDTH:.*]] = sub nuw nsw i32 31, %[[RHS]]
Richard Smith3e056de2012-08-25 00:32:28 +0000138 // CHECK-NEXT: %[[SHIFTED_OUT:.*]] = lshr i32 %[[LHS:.*]], %[[SHIFTED_OUT_WIDTH]]
139
140 // This is present for C++11 but not for C: C++ core issue 1457 allows a '1'
141 // to be shifted into the sign bit, but not out of it.
142 // CHECK-NEXT: %[[SHIFTED_OUT_NOT_SIGN:.*]] = lshr i32 %[[SHIFTED_OUT]], 1
143
144 // CHECK-NEXT: %[[NO_OVERFLOW:.*]] = icmp eq i32 %[[SHIFTED_OUT_NOT_SIGN]], 0
Alexey Samsonov21d2dda2015-03-09 21:50:19 +0000145 // CHECK-NEXT: br label %[[CONT_BB]]
Will Dietz11d0a9f2013-02-25 22:37:49 +0000146
Alexey Samsonov21d2dda2015-03-09 21:50:19 +0000147 // CHECK: [[CONT_BB]]:
148 // CHECK-NEXT: %[[VALID_BASE:.*]] = phi i1 [ true, {{.*}} ], [ %[[NO_OVERFLOW]], %[[CHECK_BB]] ]
149 // CHECK-NEXT: %[[VALID:.*]] = and i1 %[[RHS_INBOUNDS]], %[[VALID_BASE]]
Will Dietz11d0a9f2013-02-25 22:37:49 +0000150 // CHECK-NEXT: br i1 %[[VALID]]
151
152 // CHECK: call void @__ubsan_handle_shift_out_of_bounds
153 // CHECK-NOT: call void @__ubsan_handle_shift_out_of_bounds
Richard Smith3e056de2012-08-25 00:32:28 +0000154
155 // CHECK: %[[RET:.*]] = shl i32 %[[LHS]], %[[RHS]]
156 // CHECK-NEXT: ret i32 %[[RET]]
157 return a << b;
158}
Richard Smith9f9e5822012-10-04 23:52:29 +0000159
Alexey Samsonovc9939332014-07-17 23:53:44 +0000160// CHECK-LABEL: @_Z9no_return
Richard Smith9f9e5822012-10-04 23:52:29 +0000161int no_return() {
Bill Wendlinge1c4a1b2013-02-22 09:10:20 +0000162 // CHECK: call void @__ubsan_handle_missing_return(i8* bitcast ({{.*}}* @{{.*}} to i8*)) [[NR_NUW:#[0-9]+]]
Richard Smithe30752c2012-10-09 19:52:38 +0000163 // CHECK-NEXT: unreachable
Richard Smith9f9e5822012-10-04 23:52:29 +0000164}
Richard Smith1629da92012-12-13 07:11:50 +0000165
Alexey Samsonovc9939332014-07-17 23:53:44 +0000166// CHECK-LABEL: @_Z9sour_bool
Richard Smith1629da92012-12-13 07:11:50 +0000167bool sour_bool(bool *p) {
168 // CHECK: %[[OK:.*]] = icmp ule i8 {{.*}}, 1
169 // CHECK: br i1 %[[OK]]
Will Dietz3676d562012-12-30 20:53:28 +0000170 // CHECK: call void @__ubsan_handle_load_invalid_value(i8* bitcast ({{.*}}), i64 {{.*}})
Richard Smith1629da92012-12-13 07:11:50 +0000171 return *p;
172}
173
174enum E1 { e1a = 0, e1b = 127 } e1;
175enum E2 { e2a = -1, e2b = 64 } e2;
176enum E3 { e3a = (1u << 31) - 1 } e3;
177
Alexey Samsonovc9939332014-07-17 23:53:44 +0000178// CHECK-LABEL: @_Z14bad_enum_value
Richard Smith1629da92012-12-13 07:11:50 +0000179int bad_enum_value() {
180 // CHECK: %[[E1:.*]] = icmp ule i32 {{.*}}, 127
181 // CHECK: br i1 %[[E1]]
Will Dietz3676d562012-12-30 20:53:28 +0000182 // CHECK: call void @__ubsan_handle_load_invalid_value(
Richard Smith1629da92012-12-13 07:11:50 +0000183 int a = e1;
184
185 // CHECK: %[[E2HI:.*]] = icmp sle i32 {{.*}}, 127
186 // CHECK: %[[E2LO:.*]] = icmp sge i32 {{.*}}, -128
187 // CHECK: %[[E2:.*]] = and i1 %[[E2HI]], %[[E2LO]]
188 // CHECK: br i1 %[[E2]]
Will Dietz3676d562012-12-30 20:53:28 +0000189 // CHECK: call void @__ubsan_handle_load_invalid_value(
Richard Smith1629da92012-12-13 07:11:50 +0000190 int b = e2;
191
192 // CHECK: %[[E3:.*]] = icmp ule i32 {{.*}}, 2147483647
193 // CHECK: br i1 %[[E3]]
Will Dietz3676d562012-12-30 20:53:28 +0000194 // CHECK: call void @__ubsan_handle_load_invalid_value(
Richard Smith1629da92012-12-13 07:11:50 +0000195 int c = e3;
196 return a + b + c;
197}
Richard Smith2c5868c2013-02-13 21:18:23 +0000198
Alexey Samsonovc9939332014-07-17 23:53:44 +0000199// CHECK-LABEL: @_Z20bad_downcast_pointer
Alexey Samsonov32f59d82014-07-18 18:15:39 +0000200// DOWNCAST-NULL-LABEL: @_Z20bad_downcast_pointer
Richard Smith2c5868c2013-02-13 21:18:23 +0000201void bad_downcast_pointer(S *p) {
202 // CHECK: %[[NONNULL:.*]] = icmp ne {{.*}}, null
203 // CHECK: br i1 %[[NONNULL]],
204
Alexey Samsonov32f59d82014-07-18 18:15:39 +0000205 // A null poiner access is guarded without -fsanitize=null.
206 // DOWNCAST-NULL: %[[NONNULL:.*]] = icmp ne {{.*}}, null
207 // DOWNCAST-NULL: br i1 %[[NONNULL]],
208
Matt Arsenault2f152632013-10-07 19:00:18 +0000209 // CHECK: %[[SIZE:.*]] = call i64 @llvm.objectsize.i64.p0i8(
Richard Smith2c5868c2013-02-13 21:18:23 +0000210 // CHECK: %[[E1:.*]] = icmp uge i64 %[[SIZE]], 24
211 // CHECK: %[[MISALIGN:.*]] = and i64 %{{.*}}, 7
212 // CHECK: %[[E2:.*]] = icmp eq i64 %[[MISALIGN]], 0
213 // CHECK: %[[E12:.*]] = and i1 %[[E1]], %[[E2]]
214 // CHECK: br i1 %[[E12]],
215
216 // CHECK: call void @__ubsan_handle_type_mismatch
217 // CHECK: br label
218
219 // CHECK: br i1 %{{.*}},
220
221 // CHECK: call void @__ubsan_handle_dynamic_type_cache_miss
222 // CHECK: br label
223 (void) static_cast<T*>(p);
224}
225
Alexey Samsonovc9939332014-07-17 23:53:44 +0000226// CHECK-LABEL: @_Z22bad_downcast_reference
Richard Smith2c5868c2013-02-13 21:18:23 +0000227void bad_downcast_reference(S &p) {
228 // CHECK: %[[E1:.*]] = icmp ne {{.*}}, null
229 // CHECK-NOT: br i1
Alexey Samsonove396bfc2014-11-11 22:03:54 +0000230
Matt Arsenault2f152632013-10-07 19:00:18 +0000231 // CHECK: %[[SIZE:.*]] = call i64 @llvm.objectsize.i64.p0i8(
Richard Smith2c5868c2013-02-13 21:18:23 +0000232 // CHECK: %[[E2:.*]] = icmp uge i64 %[[SIZE]], 24
Alexey Samsonove396bfc2014-11-11 22:03:54 +0000233
Richard Smith2c5868c2013-02-13 21:18:23 +0000234 // CHECK: %[[MISALIGN:.*]] = and i64 %{{.*}}, 7
235 // CHECK: %[[E3:.*]] = icmp eq i64 %[[MISALIGN]], 0
Alexey Samsonove396bfc2014-11-11 22:03:54 +0000236
237 // CHECK: %[[E12:.*]] = and i1 %[[E1]], %[[E2]]
Richard Smith2c5868c2013-02-13 21:18:23 +0000238 // CHECK: %[[E123:.*]] = and i1 %[[E12]], %[[E3]]
239 // CHECK: br i1 %[[E123]],
240
241 // CHECK: call void @__ubsan_handle_type_mismatch
242 // CHECK: br label
243
244 // CHECK: br i1 %{{.*}},
245
246 // CHECK: call void @__ubsan_handle_dynamic_type_cache_miss
247 // CHECK: br label
248 (void) static_cast<T&>(p);
249}
Bill Wendlinge1c4a1b2013-02-22 09:10:20 +0000250
Alexey Samsonovc9939332014-07-17 23:53:44 +0000251// CHECK-LABEL: @_Z11array_index
Richard Smith539e4a72013-02-23 02:53:19 +0000252int array_index(const int (&a)[4], int n) {
253 // CHECK: %[[K1_OK:.*]] = icmp ult i64 %{{.*}}, 4
254 // CHECK: br i1 %[[K1_OK]]
255 // CHECK: call void @__ubsan_handle_out_of_bounds(
256 int k1 = a[n];
257
258 // CHECK: %[[R1_OK:.*]] = icmp ule i64 %{{.*}}, 4
259 // CHECK: br i1 %[[R1_OK]]
260 // CHECK: call void @__ubsan_handle_out_of_bounds(
261 const int *r1 = &a[n];
262
263 // CHECK: %[[K2_OK:.*]] = icmp ult i64 %{{.*}}, 8
264 // CHECK: br i1 %[[K2_OK]]
265 // CHECK: call void @__ubsan_handle_out_of_bounds(
266 int k2 = ((const int(&)[8])a)[n];
267
268 // CHECK: %[[K3_OK:.*]] = icmp ult i64 %{{.*}}, 4
269 // CHECK: br i1 %[[K3_OK]]
270 // CHECK: call void @__ubsan_handle_out_of_bounds(
271 int k3 = n[a];
272
273 return k1 + *r1 + k2;
274}
275
Alexey Samsonovc9939332014-07-17 23:53:44 +0000276// CHECK-LABEL: @_Z17multi_array_index
Richard Smith539e4a72013-02-23 02:53:19 +0000277int multi_array_index(int n, int m) {
278 int arr[4][6];
279
280 // CHECK: %[[IDX2_OK:.*]] = icmp ult i64 %{{.*}}, 6
281 // CHECK: br i1 %[[IDX2_OK]]
282 // CHECK: call void @__ubsan_handle_out_of_bounds(
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 return arr[n][m];
288}
289
Alexey Samsonovc9939332014-07-17 23:53:44 +0000290// CHECK-LABEL: @_Z11array_arith
Richard Smith539e4a72013-02-23 02:53:19 +0000291int array_arith(const int (&a)[4], int n) {
292 // CHECK: %[[K1_OK:.*]] = icmp ule i64 %{{.*}}, 4
293 // CHECK: br i1 %[[K1_OK]]
294 // CHECK: call void @__ubsan_handle_out_of_bounds(
295 const int *k1 = a + n;
296
297 // CHECK: %[[K2_OK:.*]] = icmp ule i64 %{{.*}}, 8
298 // CHECK: br i1 %[[K2_OK]]
299 // CHECK: call void @__ubsan_handle_out_of_bounds(
300 const int *k2 = (const int(&)[8])a + n;
301
302 return *k1 + *k2;
303}
304
305struct ArrayMembers {
306 int a1[5];
307 int a2[1];
308};
Alexey Samsonovc9939332014-07-17 23:53:44 +0000309// CHECK-LABEL: @_Z18struct_array_index
Richard Smith539e4a72013-02-23 02:53:19 +0000310int struct_array_index(ArrayMembers *p, int n) {
311 // CHECK: %[[IDX_OK:.*]] = icmp ult i64 %{{.*}}, 5
312 // CHECK: br i1 %[[IDX_OK]]
313 // CHECK: call void @__ubsan_handle_out_of_bounds(
314 return p->a1[n];
315}
316
Alexey Samsonovc9939332014-07-17 23:53:44 +0000317// CHECK-LABEL: @_Z16flex_array_index
Richard Smith539e4a72013-02-23 02:53:19 +0000318int flex_array_index(ArrayMembers *p, int n) {
319 // CHECK-NOT: call void @__ubsan_handle_out_of_bounds(
320 return p->a2[n];
321}
322
Richard Smith2847b222013-02-24 01:56:24 +0000323extern int incomplete[];
Alexey Samsonovc9939332014-07-17 23:53:44 +0000324// CHECK-LABEL: @_Z22incomplete_array_index
Richard Smith2847b222013-02-24 01:56:24 +0000325int incomplete_array_index(int n) {
326 // CHECK-NOT: call void @__ubsan_handle_out_of_bounds(
327 return incomplete[n];
328}
329
Richard Smith539e4a72013-02-23 02:53:19 +0000330typedef __attribute__((ext_vector_type(4))) int V4I;
Alexey Samsonovc9939332014-07-17 23:53:44 +0000331// CHECK-LABEL: @_Z12vector_index
Richard Smith539e4a72013-02-23 02:53:19 +0000332int vector_index(V4I v, int n) {
333 // CHECK: %[[IDX_OK:.*]] = icmp ult i64 %{{.*}}, 4
334 // CHECK: br i1 %[[IDX_OK]]
335 // CHECK: call void @__ubsan_handle_out_of_bounds(
336 return v[n];
337}
338
Alexey Samsonovc9939332014-07-17 23:53:44 +0000339// CHECK-LABEL: @_Z12string_index
Richard Smith539e4a72013-02-23 02:53:19 +0000340char string_index(int n) {
341 // CHECK: %[[IDX_OK:.*]] = icmp ult i64 %{{.*}}, 6
342 // CHECK: br i1 %[[IDX_OK]]
343 // CHECK: call void @__ubsan_handle_out_of_bounds(
344 return "Hello"[n];
345}
346
Filipe Cabecinhas178a8df2013-08-08 01:08:17 +0000347class A // align=4
348{
349 int a1, a2, a3;
350};
351
352class B // align=8
353{
354 long b1, b2;
355};
356
357class C : public A, public B // align=16
358{
359 alignas(16) int c1;
360};
361
362// Make sure we check the alignment of the pointer after subtracting any
363// offset. The pointer before subtraction doesn't need to be aligned for
364// the destination type.
365
Nick Lewycky8b4e3792013-09-11 02:03:20 +0000366// CHECK-LABEL: define void @_Z16downcast_pointerP1B(%class.B* %b)
Filipe Cabecinhas178a8df2013-08-08 01:08:17 +0000367void downcast_pointer(B *b) {
368 (void) static_cast<C*>(b);
369 // Alignment check from EmitTypeCheck(TCK_DowncastPointer, ...)
David Blaikie218b7832015-02-27 19:18:17 +0000370 // CHECK: [[SUB:%[.a-z0-9]*]] = getelementptr i8, i8* {{.*}}, i64 -16
NAKAMURA Takumif9c52dc2015-09-15 09:50:24 +0000371 // CHECK-NEXT: [[C:%.+]] = bitcast i8* [[SUB]] to %class.C*
Filipe Cabecinhas178a8df2013-08-08 01:08:17 +0000372 // null check goes here
NAKAMURA Takumif9c52dc2015-09-15 09:50:24 +0000373 // CHECK: [[FROM_PHI:%.+]] = phi %class.C* [ [[C]], {{.*}} ], {{.*}}
Filipe Cabecinhas178a8df2013-08-08 01:08:17 +0000374 // Objectsize check goes here
NAKAMURA Takumif9c52dc2015-09-15 09:50:24 +0000375 // CHECK: [[C_INT:%.+]] = ptrtoint %class.C* [[FROM_PHI]] to i64
376 // CHECK-NEXT: [[MASKED:%.+]] = and i64 [[C_INT]], 15
377 // CHECK-NEXT: [[TEST:%.+]] = icmp eq i64 [[MASKED]], 0
Filipe Cabecinhas178a8df2013-08-08 01:08:17 +0000378 // AND the alignment test with the objectsize test.
NAKAMURA Takumif9c52dc2015-09-15 09:50:24 +0000379 // CHECK-NEXT: [[AND:%.+]] = and i1 {{.*}}, [[TEST]]
Filipe Cabecinhasb2eb1d92013-08-08 01:24:29 +0000380 // CHECK-NEXT: br i1 [[AND]]
Filipe Cabecinhas178a8df2013-08-08 01:08:17 +0000381}
382
Hal Finkela2347ba2014-07-18 15:52:10 +0000383// CHECK-LABEL: define void @_Z18downcast_referenceR1B(%class.B* dereferenceable({{[0-9]+}}) %b)
Filipe Cabecinhas178a8df2013-08-08 01:08:17 +0000384void downcast_reference(B &b) {
385 (void) static_cast<C&>(b);
386 // Alignment check from EmitTypeCheck(TCK_DowncastReference, ...)
David Blaikie218b7832015-02-27 19:18:17 +0000387 // CHECK: [[SUB:%[.a-z0-9]*]] = getelementptr i8, i8* {{.*}}, i64 -16
NAKAMURA Takumif9c52dc2015-09-15 09:50:24 +0000388 // CHECK-NEXT: [[C:%.+]] = bitcast i8* [[SUB]] to %class.C*
Filipe Cabecinhas178a8df2013-08-08 01:08:17 +0000389 // Objectsize check goes here
NAKAMURA Takumif9c52dc2015-09-15 09:50:24 +0000390 // CHECK: [[C_INT:%.+]] = ptrtoint %class.C* [[C]] to i64
391 // CHECK-NEXT: [[MASKED:%.+]] = and i64 [[C_INT]], 15
392 // CHECK-NEXT: [[TEST:%.+]] = icmp eq i64 [[MASKED]], 0
Filipe Cabecinhas178a8df2013-08-08 01:08:17 +0000393 // AND the alignment test with the objectsize test.
NAKAMURA Takumif9c52dc2015-09-15 09:50:24 +0000394 // CHECK: [[AND:%.+]] = and i1 {{.*}}, [[TEST]]
Filipe Cabecinhasb2eb1d92013-08-08 01:24:29 +0000395 // CHECK-NEXT: br i1 [[AND]]
Filipe Cabecinhas178a8df2013-08-08 01:08:17 +0000396}
397
Peter Collingbourne6b46e382014-12-03 02:37:10 +0000398// CHECK-LABEL: @_Z22indirect_function_callPFviE({{.*}} prologue <{ i32, i8* }> <{ i32 1413876459, i8* bitcast ({ i8*, i8* }* @_ZTIFvPFviEE to i8*) }>
Peter Collingbourne69b004d2015-02-25 23:18:42 +0000399// CHECK-X32: @_Z22indirect_function_callPFviE({{.*}} prologue <{ i32, i8* }> <{ i32 1413875435, i8* bitcast ({ i8*, i8* }* @_ZTIFvPFviEE to i8*) }>
400// CHECK-X86: @_Z22indirect_function_callPFviE({{.*}} prologue <{ i32, i8* }> <{ i32 1413875435, i8* bitcast ({ i8*, i8* }* @_ZTIFvPFviEE to i8*) }>
Peter Collingbourneb453cd62013-10-20 21:29:19 +0000401void indirect_function_call(void (*p)(int)) {
NAKAMURA Takumif9c52dc2015-09-15 09:50:24 +0000402 // CHECK: [[PTR:%.+]] = bitcast void (i32)* {{.*}} to <{ i32, i8* }>*
Peter Collingbourneb453cd62013-10-20 21:29:19 +0000403
404 // Signature check
NAKAMURA Takumif9c52dc2015-09-15 09:50:24 +0000405 // CHECK-NEXT: [[SIGPTR:%.+]] = getelementptr <{ i32, i8* }>, <{ i32, i8* }>* [[PTR]], i32 0, i32 0
406 // CHECK-NEXT: [[SIG:%.+]] = load i32, i32* [[SIGPTR]]
407 // CHECK-NEXT: [[SIGCMP:%.+]] = icmp eq i32 [[SIG]], 1413876459
Peter Collingbourneb453cd62013-10-20 21:29:19 +0000408 // CHECK-NEXT: br i1 [[SIGCMP]]
409
410 // RTTI pointer check
NAKAMURA Takumif9c52dc2015-09-15 09:50:24 +0000411 // CHECK: [[RTTIPTR:%.+]] = getelementptr <{ i32, i8* }>, <{ i32, i8* }>* [[PTR]], i32 0, i32 1
412 // CHECK-NEXT: [[RTTI:%.+]] = load i8*, i8** [[RTTIPTR]]
413 // CHECK-NEXT: [[RTTICMP:%.+]] = icmp eq i8* [[RTTI]], bitcast ({ i8*, i8* }* @_ZTIFviE to i8*)
Peter Collingbourneb453cd62013-10-20 21:29:19 +0000414 // CHECK-NEXT: br i1 [[RTTICMP]]
415 p(42);
416}
417
Alexey Samsonoveb47d8a2014-10-13 23:59:00 +0000418namespace UpcastPointerTest {
419struct S {};
420struct T : S { double d; };
421struct V : virtual S {};
422
423// CHECK-LABEL: upcast_pointer
424S* upcast_pointer(T* t) {
425 // Check for null pointer
426 // CHECK: %[[NONNULL:.*]] = icmp ne {{.*}}, null
427 // CHECK: br i1 %[[NONNULL]]
428
429 // Check alignment
430 // CHECK: %[[MISALIGN:.*]] = and i64 %{{.*}}, 7
431 // CHECK: icmp eq i64 %[[MISALIGN]], 0
432
433 // CHECK: call void @__ubsan_handle_type_mismatch
434 return t;
435}
436
437V getV();
438
439// CHECK-LABEL: upcast_to_vbase
440void upcast_to_vbase() {
441 // No need to check for null here, as we have a temporary here.
442
443 // CHECK-NOT: br i1
444
445 // CHECK: call i64 @llvm.objectsize
446 // CHECK: call void @__ubsan_handle_type_mismatch
447 // CHECK: call void @__ubsan_handle_dynamic_type_cache_miss
448 const S& s = getV();
449}
450}
451
Nick Lewycky8b4e3792013-09-11 02:03:20 +0000452namespace CopyValueRepresentation {
453 // CHECK-LABEL: define {{.*}} @_ZN23CopyValueRepresentation2S3aSERKS0_
454 // CHECK-NOT: call {{.*}} @__ubsan_handle_load_invalid_value
455 // CHECK-LABEL: define {{.*}} @_ZN23CopyValueRepresentation2S4aSEOS0_
456 // CHECK-NOT: call {{.*}} @__ubsan_handle_load_invalid_value
Rafael Espindolae5df59f2015-01-22 00:24:57 +0000457 // CHECK-LABEL: define {{.*}} @_ZN23CopyValueRepresentation2S1C2ERKS0_
Nick Lewycky8b4e3792013-09-11 02:03:20 +0000458 // CHECK-NOT: call {{.*}} __ubsan_handle_load_invalid_value
459 // CHECK-LABEL: define {{.*}} @_ZN23CopyValueRepresentation2S2C2ERKS0_
460 // CHECK: __ubsan_handle_load_invalid_value
Rafael Espindolae5df59f2015-01-22 00:24:57 +0000461 // CHECK-LABEL: define {{.*}} @_ZN23CopyValueRepresentation2S5C2ERKS0_
Nick Lewycky8b4e3792013-09-11 02:03:20 +0000462 // CHECK-NOT: call {{.*}} __ubsan_handle_load_invalid_value
463
464 struct CustomCopy { CustomCopy(); CustomCopy(const CustomCopy&); };
465 struct S1 {
466 CustomCopy CC;
467 bool b;
468 };
469 void callee1(S1);
470 void test1() {
471 S1 s11;
472 callee1(s11);
473 S1 s12;
474 s12 = s11;
475 }
476
477 static bool some_global_bool;
478 struct ExprCopy {
479 ExprCopy();
480 ExprCopy(const ExprCopy&, bool b = some_global_bool);
481 };
482 struct S2 {
483 ExprCopy EC;
484 bool b;
485 };
486 void callee2(S2);
487 void test2(void) {
488 S2 s21;
489 callee2(s21);
490 S2 s22;
491 s22 = s21;
492 }
493
494 struct CustomAssign { CustomAssign &operator=(const CustomAssign&); };
495 struct S3 {
496 CustomAssign CA;
497 bool b;
498 };
499 void test3() {
500 S3 x, y;
501 x = y;
502 }
503
504 struct CustomMove {
505 CustomMove();
506 CustomMove(const CustomMove&&);
507 CustomMove &operator=(const CustomMove&&);
508 };
509 struct S4 {
510 CustomMove CM;
511 bool b;
512 };
513 void test4() {
514 S4 x, y;
515 x = static_cast<S4&&>(y);
516 }
517
518 struct EnumCustomCopy {
519 EnumCustomCopy();
520 EnumCustomCopy(const EnumCustomCopy&);
521 };
522 struct S5 {
523 EnumCustomCopy ECC;
524 bool b;
525 };
526 void callee5(S5);
527 void test5() {
528 S5 s51;
529 callee5(s51);
530 S5 s52;
531 s52 = s51;
532 }
533}
534
Bill Wendlinge1c4a1b2013-02-22 09:10:20 +0000535// CHECK: attributes [[NR_NUW]] = { noreturn nounwind }