1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
// 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,bounds -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck %s
struct S {
double d;
int a, b;
virtual int f();
};
struct T : S {};
// CHECK: @_Z17reference_binding
void reference_binding(int *p, S *q) {
// C++ core issue 453: If an lvalue to which a reference is directly bound
// designates neither an existing object or function of an appropriate type,
// nor a region of storage of suitable size and alignment to contain an object
// of the reference's type, the behavior is undefined.
// CHECK: icmp ne {{.*}}, null
// CHECK: %[[SIZE:.*]] = call i64 @llvm.objectsize.i64
// CHECK-NEXT: icmp uge i64 %[[SIZE]], 4
// CHECK: %[[PTRINT:.*]] = ptrtoint
// CHECK-NEXT: %[[MISALIGN:.*]] = and i64 %[[PTRINT]], 3
// CHECK-NEXT: icmp eq i64 %[[MISALIGN]], 0
int &r = *p;
// A reference is not required to refer to an object within its lifetime.
// CHECK-NOT: __ubsan_handle_dynamic_type_cache_miss
S &r2 = *q;
}
// CHECK: @_Z13member_access
void member_access(S *p) {
// (1a) Check 'p' is appropriately sized and aligned for member access.
// CHECK: icmp ne {{.*}}, null
// CHECK: %[[SIZE:.*]] = call i64 @llvm.objectsize.i64
// CHECK-NEXT: icmp uge i64 %[[SIZE]], 24
// CHECK: %[[PTRINT:.*]] = ptrtoint
// CHECK-NEXT: %[[MISALIGN:.*]] = and i64 %[[PTRINT]], 7
// CHECK-NEXT: icmp eq i64 %[[MISALIGN]], 0
// (1b) Check that 'p' actually points to an 'S'.
// CHECK: %[[VPTRADDR:.*]] = bitcast {{.*}} to i64*
// CHECK-NEXT: %[[VPTR:.*]] = load i64* %[[VPTRADDR]]
//
// hash_16_bytes:
//
// If this number changes, it indicates that either the mangled name of ::S
// has changed, or that LLVM's hashing function has changed. The latter case
// is OK if the hashing function is still stable.
//
// The two hash values are for 64- and 32-bit Clang binaries, respectively.
// FIXME: We should produce a 64-bit value either way.
//
// CHECK-NEXT: xor i64 {{-4030275160588942838|2562089159}}, %[[VPTR]]
// CHECK-NEXT: mul i64 {{.*}}, -7070675565921424023
// CHECK-NEXT: lshr i64 {{.*}}, 47
// CHECK-NEXT: xor i64
// CHECK-NEXT: xor i64 %[[VPTR]]
// CHECK-NEXT: mul i64 {{.*}}, -7070675565921424023
// CHECK-NEXT: lshr i64 {{.*}}, 47
// CHECK-NEXT: xor i64
// CHECK-NEXT: %[[HASH:.*]] = mul i64 {{.*}}, -7070675565921424023
//
// Check the hash against the table:
//
// CHECK-NEXT: %[[IDX:.*]] = and i64 %{{.*}}, 127
// CHECK-NEXT: getelementptr inbounds [128 x i64]* @__ubsan_vptr_type_cache, i32 0, i64 %[[IDX]]
// CHECK-NEXT: %[[CACHEVAL:.*]] = load i64*
// CHECK-NEXT: icmp eq i64 %[[CACHEVAL]], %[[HASH]]
// CHECK-NEXT: br i1
// CHECK: call void @__ubsan_handle_dynamic_type_cache_miss({{.*}}, i64 %{{.*}}, i64 %[[HASH]])
// CHECK-NOT: unreachable
// CHECK: {{.*}}:
// (2) Check 'p->b' is appropriately sized and aligned for a load.
// FIXME: Suppress this in the trivial case of a member access, because we
// know we've just checked the member access expression itself.
// CHECK: %[[SIZE:.*]] = call i64 @llvm.objectsize.i64
// CHECK-NEXT: icmp uge i64 %[[SIZE]], 4
// CHECK: %[[PTRINT:.*]] = ptrtoint
// CHECK-NEXT: %[[MISALIGN:.*]] = and i64 %[[PTRINT]], 3
// CHECK-NEXT: icmp eq i64 %[[MISALIGN]], 0
int k = p->b;
// (3a) Check 'p' is appropriately sized and aligned for member function call.
// CHECK: icmp ne {{.*}}, null
// CHECK: %[[SIZE:.*]] = call i64 @llvm.objectsize.i64
// CHECK-NEXT: icmp uge i64 %[[SIZE]], 24
// CHECK: %[[PTRINT:.*]] = ptrtoint
// CHECK-NEXT: %[[MISALIGN:.*]] = and i64 %[[PTRINT]], 7
// CHECK-NEXT: icmp eq i64 %[[MISALIGN]], 0
// (3b) Check that 'p' actually points to an 'S'
// CHECK: load i64*
// CHECK-NEXT: xor i64 {{-4030275160588942838|2562089159}},
// [...]
// CHECK: getelementptr inbounds [128 x i64]* @__ubsan_vptr_type_cache, i32 0, i64 %
// CHECK: br i1
// CHECK: call void @__ubsan_handle_dynamic_type_cache_miss({{.*}}, i64 %{{.*}}, i64 %{{.*}})
// CHECK-NOT: unreachable
// CHECK: {{.*}}:
k = p->f();
}
// CHECK: @_Z12lsh_overflow
int lsh_overflow(int a, int b) {
// CHECK: %[[INBOUNDS:.*]] = icmp ule i32 %[[RHS:.*]], 31
// CHECK-NEXT: br i1 %[[INBOUNDS]]
// CHECK: %[[SHIFTED_OUT_WIDTH:.*]] = sub nuw nsw i32 31, %[[RHS]]
// CHECK-NEXT: %[[SHIFTED_OUT:.*]] = lshr i32 %[[LHS:.*]], %[[SHIFTED_OUT_WIDTH]]
// This is present for C++11 but not for C: C++ core issue 1457 allows a '1'
// to be shifted into the sign bit, but not out of it.
// CHECK-NEXT: %[[SHIFTED_OUT_NOT_SIGN:.*]] = lshr i32 %[[SHIFTED_OUT]], 1
// CHECK-NEXT: %[[NO_OVERFLOW:.*]] = icmp eq i32 %[[SHIFTED_OUT_NOT_SIGN]], 0
// CHECK: %[[VALID:.*]] = phi i1 [ %[[INBOUNDS]], {{.*}} ], [ %[[NO_OVERFLOW]], {{.*}} ]
// CHECK-NEXT: br i1 %[[VALID]]
// CHECK: call void @__ubsan_handle_shift_out_of_bounds
// CHECK-NOT: call void @__ubsan_handle_shift_out_of_bounds
// CHECK: %[[RET:.*]] = shl i32 %[[LHS]], %[[RHS]]
// CHECK-NEXT: ret i32 %[[RET]]
return a << b;
}
// CHECK: @_Z9no_return
int no_return() {
// CHECK: call void @__ubsan_handle_missing_return(i8* bitcast ({{.*}}* @{{.*}} to i8*)) [[NR_NUW:#[0-9]+]]
// CHECK-NEXT: unreachable
}
// CHECK: @_Z9sour_bool
bool sour_bool(bool *p) {
// CHECK: %[[OK:.*]] = icmp ule i8 {{.*}}, 1
// CHECK: br i1 %[[OK]]
// CHECK: call void @__ubsan_handle_load_invalid_value(i8* bitcast ({{.*}}), i64 {{.*}})
return *p;
}
enum E1 { e1a = 0, e1b = 127 } e1;
enum E2 { e2a = -1, e2b = 64 } e2;
enum E3 { e3a = (1u << 31) - 1 } e3;
// CHECK: @_Z14bad_enum_value
int bad_enum_value() {
// CHECK: %[[E1:.*]] = icmp ule i32 {{.*}}, 127
// CHECK: br i1 %[[E1]]
// CHECK: call void @__ubsan_handle_load_invalid_value(
int a = e1;
// CHECK: %[[E2HI:.*]] = icmp sle i32 {{.*}}, 127
// CHECK: %[[E2LO:.*]] = icmp sge i32 {{.*}}, -128
// CHECK: %[[E2:.*]] = and i1 %[[E2HI]], %[[E2LO]]
// CHECK: br i1 %[[E2]]
// CHECK: call void @__ubsan_handle_load_invalid_value(
int b = e2;
// CHECK: %[[E3:.*]] = icmp ule i32 {{.*}}, 2147483647
// CHECK: br i1 %[[E3]]
// CHECK: call void @__ubsan_handle_load_invalid_value(
int c = e3;
return a + b + c;
}
// CHECK: @_Z20bad_downcast_pointer
void bad_downcast_pointer(S *p) {
// CHECK: %[[NONNULL:.*]] = icmp ne {{.*}}, null
// CHECK: br i1 %[[NONNULL]],
// CHECK: %[[SIZE:.*]] = call i64 @llvm.objectsize.i64(
// CHECK: %[[E1:.*]] = icmp uge i64 %[[SIZE]], 24
// CHECK: %[[MISALIGN:.*]] = and i64 %{{.*}}, 7
// CHECK: %[[E2:.*]] = icmp eq i64 %[[MISALIGN]], 0
// CHECK: %[[E12:.*]] = and i1 %[[E1]], %[[E2]]
// CHECK: br i1 %[[E12]],
// CHECK: call void @__ubsan_handle_type_mismatch
// CHECK: br label
// CHECK: br i1 %{{.*}},
// CHECK: call void @__ubsan_handle_dynamic_type_cache_miss
// CHECK: br label
(void) static_cast<T*>(p);
}
// CHECK: @_Z22bad_downcast_reference
void bad_downcast_reference(S &p) {
// CHECK: %[[E1:.*]] = icmp ne {{.*}}, null
// CHECK-NOT: br i1
// CHECK: %[[SIZE:.*]] = call i64 @llvm.objectsize.i64(
// CHECK: %[[E2:.*]] = icmp uge i64 %[[SIZE]], 24
// CHECK: %[[E12:.*]] = and i1 %[[E1]], %[[E2]]
// CHECK: %[[MISALIGN:.*]] = and i64 %{{.*}}, 7
// CHECK: %[[E3:.*]] = icmp eq i64 %[[MISALIGN]], 0
// CHECK: %[[E123:.*]] = and i1 %[[E12]], %[[E3]]
// CHECK: br i1 %[[E123]],
// CHECK: call void @__ubsan_handle_type_mismatch
// CHECK: br label
// CHECK: br i1 %{{.*}},
// CHECK: call void @__ubsan_handle_dynamic_type_cache_miss
// CHECK: br label
(void) static_cast<T&>(p);
}
// CHECK: @_Z11array_index
int array_index(const int (&a)[4], int n) {
// CHECK: %[[K1_OK:.*]] = icmp ult i64 %{{.*}}, 4
// CHECK: br i1 %[[K1_OK]]
// CHECK: call void @__ubsan_handle_out_of_bounds(
int k1 = a[n];
// CHECK: %[[R1_OK:.*]] = icmp ule i64 %{{.*}}, 4
// CHECK: br i1 %[[R1_OK]]
// CHECK: call void @__ubsan_handle_out_of_bounds(
const int *r1 = &a[n];
// CHECK: %[[K2_OK:.*]] = icmp ult i64 %{{.*}}, 8
// CHECK: br i1 %[[K2_OK]]
// CHECK: call void @__ubsan_handle_out_of_bounds(
int k2 = ((const int(&)[8])a)[n];
// CHECK: %[[K3_OK:.*]] = icmp ult i64 %{{.*}}, 4
// CHECK: br i1 %[[K3_OK]]
// CHECK: call void @__ubsan_handle_out_of_bounds(
int k3 = n[a];
return k1 + *r1 + k2;
}
// CHECK: @_Z17multi_array_index
int multi_array_index(int n, int m) {
int arr[4][6];
// CHECK: %[[IDX2_OK:.*]] = icmp ult i64 %{{.*}}, 6
// CHECK: br i1 %[[IDX2_OK]]
// CHECK: call void @__ubsan_handle_out_of_bounds(
// CHECK: %[[IDX1_OK:.*]] = icmp ult i64 %{{.*}}, 4
// CHECK: br i1 %[[IDX1_OK]]
// CHECK: call void @__ubsan_handle_out_of_bounds(
return arr[n][m];
}
// CHECK: @_Z11array_arith
int array_arith(const int (&a)[4], int n) {
// CHECK: %[[K1_OK:.*]] = icmp ule i64 %{{.*}}, 4
// CHECK: br i1 %[[K1_OK]]
// CHECK: call void @__ubsan_handle_out_of_bounds(
const int *k1 = a + n;
// CHECK: %[[K2_OK:.*]] = icmp ule i64 %{{.*}}, 8
// CHECK: br i1 %[[K2_OK]]
// CHECK: call void @__ubsan_handle_out_of_bounds(
const int *k2 = (const int(&)[8])a + n;
return *k1 + *k2;
}
struct ArrayMembers {
int a1[5];
int a2[1];
};
// CHECK: @_Z18struct_array_index
int struct_array_index(ArrayMembers *p, int n) {
// CHECK: %[[IDX_OK:.*]] = icmp ult i64 %{{.*}}, 5
// CHECK: br i1 %[[IDX_OK]]
// CHECK: call void @__ubsan_handle_out_of_bounds(
return p->a1[n];
}
// CHECK: @_Z16flex_array_index
int flex_array_index(ArrayMembers *p, int n) {
// CHECK-NOT: call void @__ubsan_handle_out_of_bounds(
return p->a2[n];
}
extern int incomplete[];
// CHECK: @_Z22incomplete_array_index
int incomplete_array_index(int n) {
// CHECK-NOT: call void @__ubsan_handle_out_of_bounds(
return incomplete[n];
}
typedef __attribute__((ext_vector_type(4))) int V4I;
// CHECK: @_Z12vector_index
int vector_index(V4I v, int n) {
// CHECK: %[[IDX_OK:.*]] = icmp ult i64 %{{.*}}, 4
// CHECK: br i1 %[[IDX_OK]]
// CHECK: call void @__ubsan_handle_out_of_bounds(
return v[n];
}
// CHECK: @_Z12string_index
char string_index(int n) {
// CHECK: %[[IDX_OK:.*]] = icmp ult i64 %{{.*}}, 6
// CHECK: br i1 %[[IDX_OK]]
// CHECK: call void @__ubsan_handle_out_of_bounds(
return "Hello"[n];
}
class A // align=4
{
int a1, a2, a3;
};
class B // align=8
{
long b1, b2;
};
class C : public A, public B // align=16
{
alignas(16) int c1;
};
// Make sure we check the alignment of the pointer after subtracting any
// offset. The pointer before subtraction doesn't need to be aligned for
// the destination type.
// CHECK-LABEL: define void @_Z16downcast_pointerP1B(%class.B* %b)
void downcast_pointer(B *b) {
(void) static_cast<C*>(b);
// Alignment check from EmitTypeCheck(TCK_DowncastPointer, ...)
// CHECK: [[SUB:%[.a-z0-9]*]] = getelementptr i8* {{.*}}, i64 -16
// CHECK-NEXT: [[C:%[0-9]*]] = bitcast i8* [[SUB]] to %class.C*
// null check goes here
// CHECK: [[FROM_PHI:%[0-9]*]] = phi %class.C* [ [[C]], {{.*}} ], {{.*}}
// Objectsize check goes here
// CHECK: [[C_INT:%[0-9]*]] = ptrtoint %class.C* [[FROM_PHI]] to i64
// CHECK-NEXT: [[MASKED:%[0-9]*]] = and i64 [[C_INT]], 15
// CHECK-NEXT: [[TEST:%[0-9]*]] = icmp eq i64 [[MASKED]], 0
// AND the alignment test with the objectsize test.
// CHECK-NEXT: [[AND:%[0-9]*]] = and i1 {{.*}}, [[TEST]]
// CHECK-NEXT: br i1 [[AND]]
}
// CHECK-LABEL: define void @_Z18downcast_referenceR1B(%class.B* %b)
void downcast_reference(B &b) {
(void) static_cast<C&>(b);
// Alignment check from EmitTypeCheck(TCK_DowncastReference, ...)
// CHECK: [[SUB:%[.a-z0-9]*]] = getelementptr i8* {{.*}}, i64 -16
// CHECK-NEXT: [[C:%[0-9]*]] = bitcast i8* [[SUB]] to %class.C*
// Objectsize check goes here
// CHECK: [[C_INT:%[0-9]*]] = ptrtoint %class.C* [[C]] to i64
// CHECK-NEXT: [[MASKED:%[0-9]*]] = and i64 [[C_INT]], 15
// CHECK-NEXT: [[TEST:%[0-9]*]] = icmp eq i64 [[MASKED]], 0
// AND the alignment test with the objectsize test.
// CHECK-NEXT: [[AND:%[0-9]*]] = and i1 {{.*}}, [[TEST]]
// CHECK-NEXT: br i1 [[AND]]
}
// CHECK: attributes [[NR_NUW]] = { noreturn nounwind }
|