Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / CodeGen / catch-undef-behavior.c
blobb33c13a68a5c66f581a4b01ed39d88bd54b998f4
1 // RUN: %clang_cc1 -fsanitize=alignment,null,object-size,shift-base,shift-exponent,return,signed-integer-overflow,vla-bound,float-cast-overflow,integer-divide-by-zero,bool,returns-nonnull-attribute,nonnull-attribute -fsanitize-recover=alignment,null,object-size,shift-base,shift-exponent,signed-integer-overflow,vla-bound,float-cast-overflow,integer-divide-by-zero,bool,returns-nonnull-attribute,nonnull-attribute -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck %s --check-prefixes=CHECK-COMMON,CHECK-UBSAN,CHECK-ALIGNMENT-BUILTIN
2 // RUN: %clang_cc1 -fsanitize-trap=alignment,null,object-size,shift-base,shift-exponent,return,signed-integer-overflow,vla-bound,float-cast-overflow,integer-divide-by-zero,bool,returns-nonnull-attribute,nonnull-attribute -fsanitize-recover=alignment,null,object-size,shift-base,shift-exponent,signed-integer-overflow,vla-bound,float-cast-overflow,integer-divide-by-zero,bool,returns-nonnull-attribute,nonnull-attribute -fsanitize=alignment,null,object-size,shift-base,shift-exponent,return,signed-integer-overflow,vla-bound,float-cast-overflow,integer-divide-by-zero,bool,returns-nonnull-attribute,nonnull-attribute -fsanitize-recover=alignment,null,object-size,shift-base,shift-exponent,signed-integer-overflow,vla-bound,float-cast-overflow,integer-divide-by-zero,bool,returns-nonnull-attribute,nonnull-attribute -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck %s --check-prefixes=CHECK-COMMON,CHECK-ALIGNMENT-BUILTIN,CHECK-TRAP
3 // RUN: %clang_cc1 -fsanitize=signed-integer-overflow -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck %s --check-prefix=CHECK-OVERFLOW
4 /// A variant of CHECK-UBSAN with -sanitize-alignment-builtin disabled
5 // RUN: %clang_cc1 -fsanitize=alignment,null,object-size,shift-base,shift-exponent,return,signed-integer-overflow,vla-bound,float-cast-overflow,integer-divide-by-zero,bool,returns-nonnull-attribute,nonnull-attribute -fsanitize-recover=alignment,null,object-size,shift-base,shift-exponent,signed-integer-overflow,vla-bound,float-cast-overflow,integer-divide-by-zero,bool,returns-nonnull-attribute,nonnull-attribute -emit-llvm %s -o - -triple x86_64-linux-gnu -mllvm -sanitize-alignment-builtin=0 | FileCheck %s --check-prefixes=CHECK-COMMON,CHECK-UBSAN-NO-ALIGNMENT-BUILTIN
7 // CHECK-UBSAN: @[[INT:.*]] = private unnamed_addr constant { i16, i16, [6 x i8] } { i16 0, i16 11, [6 x i8] c"'int'\00" }
9 // FIXME: When we only emit each type once, use [[INT]] more below.
10 // CHECK-UBSAN: @[[LINE_100:.*]] = private unnamed_addr global {{.*}}, i32 100, i32 5 {{.*}} @[[INT]], i8 2, i8 1
11 // CHECK-UBSAN: @[[LINE_200:.*]] = {{.*}}, i32 200, i32 10 {{.*}}, i8 2, i8 0
12 // CHECK-UBSAN: @[[LINE_300:.*]] = {{.*}}, i32 300, i32 12 {{.*}} @{{.*}}, {{.*}} @{{.*}}
13 // CHECK-UBSAN: @[[LINE_400:.*]] = {{.*}}, i32 400, i32 12 {{.*}} @{{.*}}, {{.*}} @{{.*}}
14 // CHECK-UBSAN: @[[LINE_500:.*]] = {{.*}}, i32 500, i32 10 {{.*}} @{{.*}}, i8 2, i8 0 }
15 // CHECK-UBSAN: @[[LINE_600:.*]] = {{.*}}, i32 600, i32 3 {{.*}} @{{.*}}, i8 2, i8 1 }
17 // CHECK-UBSAN: @[[STRUCT_S:.*]] = private unnamed_addr constant { i16, i16, [11 x i8] } { i16 -1, i16 0, [11 x i8] c"'struct S'\00" }
19 // CHECK-UBSAN: @[[LINE_700:.*]] = {{.*}}, i32 700, i32 14 {{.*}} @[[STRUCT_S]], i8 2, i8 3 }
20 // CHECK-UBSAN: @[[LINE_800:.*]] = {{.*}}, i32 800, i32 12 {{.*}} @{{.*}} }
21 // CHECK-UBSAN: @[[LINE_900:.*]] = {{.*}}, i32 900, i32 11 {{.*}} @{{.*}} }
22 // CHECK-UBSAN: @[[LINE_1000:.*]] = {{.*}}, i32 1000, i32 11 {{.*}} @{{.*}} }
23 // CHECK-UBSAN: @[[FP16:.*]] = private unnamed_addr constant { i16, i16, [9 x i8] } { i16 1, i16 16, [9 x i8] c"'__fp16'\00" }
24 // CHECK-UBSAN: @[[LINE_1200:.*]] = {{.*}}, i32 1200, i32 10 {{.*}} @{{.*}} }
25 // CHECK-UBSAN: @[[LINE_1300:.*]] = {{.*}}, i32 1300, i32 10 {{.*}} @{{.*}} }
26 // CHECK-UBSAN: @[[LINE_1400:.*]] = {{.*}}, i32 1400, i32 10 {{.*}} @{{.*}} }
27 // Make sure we check the fp16 type_mismatch data so we can easily match the signed char float_cast_overflow
28 // CHECK-UBSAN: @[[LINE_1500:.*]] = {{.*}}, i32 1500, i32 10 {{.*}} @[[FP16]], {{.*}} }
29 // CHECK-UBSAN: @[[SCHAR:.*]] = private unnamed_addr constant { i16, i16, [14 x i8] } { i16 0, i16 7, [14 x i8] c"'signed char'\00" }
30 // CHECK-UBSAN: @[[LINE_1500:.*]] = {{.*}}, i32 1500, i32 10 {{.*}} @[[FP16]], {{.*}} }
32 // CHECK-UBSAN: @[[PLONG:.*]] = private unnamed_addr constant { i16, i16, [9 x i8] } { i16 -1, i16 0, [9 x i8] c"'long *'\00" }
33 // CHECK-UBSAN: @[[LINE_1600:.*]] = {{.*}}, i32 1600, i32 10 {{.*}} @[[PLONG]], {{.*}} }
35 // PR6805
36 // CHECK-COMMON-LABEL: @foo
37 void foo(void) {
38 union { int i; } u;
40 // CHECK-COMMON: %[[SIZE:.*]] = call i64 @llvm.objectsize.i64.p0(ptr %[[PTR:.*]], i1 false, i1 false, i1 false)
41 // CHECK-COMMON-NEXT: %[[OK:.*]] = icmp uge i64 %[[SIZE]], 4
43 // CHECK-UBSAN: br i1 %[[OK]], {{.*}} !prof ![[WEIGHT_MD:.*]], !nosanitize
44 // CHECK-TRAP: br i1 %[[OK]], {{.*}}
46 // CHECK-UBSAN: %[[ARG:.*]] = ptrtoint {{.*}} %[[PTR]] to i64
47 // CHECK-UBSAN-NEXT: call void @__ubsan_handle_type_mismatch_v1(ptr @[[LINE_100]], i64 %[[ARG]])
49 // CHECK-TRAP: call void @llvm.ubsantrap(i8 22) [[NR_NUW:#[0-9]+]]
50 // CHECK-TRAP-NEXT: unreachable
51 #line 100
52 u.i=1;
55 // CHECK-COMMON-LABEL: @bar
56 int bar(int *a) {
57 // CHECK-COMMON: %[[SIZE:.*]] = call i64 @llvm.objectsize.i64
58 // CHECK-COMMON-NEXT: icmp uge i64 %[[SIZE]], 4
60 // CHECK-COMMON: %[[PTRINT:.*]] = ptrtoint
61 // CHECK-COMMON-NEXT: %[[MISALIGN:.*]] = and i64 %[[PTRINT]], 3
62 // CHECK-COMMON-NEXT: icmp eq i64 %[[MISALIGN]], 0
64 // CHECK-UBSAN: call void @__ubsan_handle_type_mismatch_v1(ptr @[[LINE_200]], i64 %[[PTRINT]])
66 // CHECK-TRAP: call void @llvm.ubsantrap(i8 22) [[NR_NUW]]
67 // CHECK-TRAP-NEXT: unreachable
69 #line 200
70 return *a;
73 // CHECK-UBSAN-LABEL: @addr_space
74 int addr_space(int __attribute__((address_space(256))) *a) {
75 // CHECK-UBSAN-NOT: __ubsan
76 return *a;
79 // CHECK-COMMON-LABEL: @lsh_overflow
80 int lsh_overflow(int a, int b) {
81 // CHECK-COMMON: %[[RHS_INBOUNDS:.*]] = icmp ule i32 %[[RHS:.*]], 31
82 // CHECK-COMMON-NEXT: br i1 %[[RHS_INBOUNDS]], label %[[CHECK_BB:.*]], label %[[CONT_BB:.*]],
84 // CHECK-COMMON: [[CHECK_BB]]:
85 // CHECK-COMMON-NEXT: %[[SHIFTED_OUT_WIDTH:.*]] = sub nuw nsw i32 31, %[[RHS]]
86 // CHECK-COMMON-NEXT: %[[SHIFTED_OUT:.*]] = lshr i32 %[[LHS:.*]], %[[SHIFTED_OUT_WIDTH]]
87 // CHECK-COMMON-NEXT: %[[NO_OVERFLOW:.*]] = icmp eq i32 %[[SHIFTED_OUT]], 0
88 // CHECK-COMMON-NEXT: br label %[[CONT_BB]]
90 // CHECK-COMMON: [[CONT_BB]]:
91 // CHECK-COMMON-NEXT: %[[VALID_BASE:.*]] = phi i1 [ true, {{.*}} ], [ %[[NO_OVERFLOW]], %[[CHECK_BB]] ]
92 // CHECK-COMMON-NEXT: %[[VALID:.*]] = and i1 %[[RHS_INBOUNDS]], %[[VALID_BASE]]
94 // CHECK-UBSAN: br i1 %[[VALID]], {{.*}} !prof ![[WEIGHT_MD]]
95 // CHECK-TRAP: br i1 %[[VALID]]
97 // CHECK-UBSAN: %[[ARG1:.*]] = zext
98 // CHECK-UBSAN-NEXT: %[[ARG2:.*]] = zext
99 // CHECK-UBSAN-NEXT: call void @__ubsan_handle_shift_out_of_bounds(ptr @[[LINE_300]], i64 %[[ARG1]], i64 %[[ARG2]])
100 // CHECK-UBSAN-NOT: call void @__ubsan_handle_shift_out_of_bounds
102 // CHECK-TRAP: call void @llvm.ubsantrap(i8 20) [[NR_NUW]]
103 // CHECK-TRAP: unreachable
104 // CHECK-TRAP-NOT: call void @llvm.ubsantrap
106 // CHECK-COMMON: %[[RET:.*]] = shl i32 %[[LHS]], %[[RHS]]
107 // CHECK-COMMON-NEXT: ret i32 %[[RET]]
108 #line 300
109 return a << b;
112 // CHECK-COMMON-LABEL: @rsh_inbounds
113 int rsh_inbounds(int a, int b) {
114 // CHECK-COMMON: %[[INBOUNDS:.*]] = icmp ule i32 %[[RHS:.*]], 31
115 // CHECK-COMMON: br i1 %[[INBOUNDS]]
117 // CHECK-UBSAN: %[[ARG1:.*]] = zext
118 // CHECK-UBSAN-NEXT: %[[ARG2:.*]] = zext
119 // CHECK-UBSAN-NEXT: call void @__ubsan_handle_shift_out_of_bounds(ptr @[[LINE_400]], i64 %[[ARG1]], i64 %[[ARG2]])
121 // CHECK-TRAP: call void @llvm.ubsantrap(i8 20) [[NR_NUW]]
122 // CHECK-TRAP-NEXT: unreachable
124 // CHECK-COMMON: %[[RET:.*]] = ashr i32 {{.*}}, %[[RHS]]
125 // CHECK-COMMON-NEXT: ret i32 %[[RET]]
126 #line 400
127 return a >> b;
130 // CHECK-COMMON-LABEL: @load
131 int load(int *p) {
132 // CHECK-UBSAN: call void @__ubsan_handle_type_mismatch_v1(ptr @[[LINE_500]], i64 %{{.*}})
134 // CHECK-TRAP: call void @llvm.ubsantrap(i8 22) [[NR_NUW]]
135 // CHECK-TRAP-NEXT: unreachable
136 #line 500
137 return *p;
140 // CHECK-COMMON-LABEL: @store
141 void store(int *p, int q) {
142 // CHECK-UBSAN: call void @__ubsan_handle_type_mismatch_v1(ptr @[[LINE_600]], i64 %{{.*}})
144 // CHECK-TRAP: call void @llvm.ubsantrap(i8 22) [[NR_NUW]]
145 // CHECK-TRAP-NEXT: unreachable
146 #line 600
147 *p = q;
150 struct S { int k; };
152 // CHECK-COMMON-LABEL: @member_access
153 int *member_access(struct S *p) {
154 // CHECK-UBSAN: call void @__ubsan_handle_type_mismatch_v1(ptr @[[LINE_700]], i64 %{{.*}})
156 // CHECK-TRAP: call void @llvm.ubsantrap(i8 22) [[NR_NUW]]
157 // CHECK-TRAP-NEXT: unreachable
158 #line 700
159 return &p->k;
162 // CHECK-COMMON-LABEL: @signed_overflow
163 int signed_overflow(int a, int b) {
164 // CHECK-UBSAN: %[[ARG1:.*]] = zext
165 // CHECK-UBSAN-NEXT: %[[ARG2:.*]] = zext
166 // CHECK-UBSAN-NEXT: call void @__ubsan_handle_add_overflow(ptr @[[LINE_800]], i64 %[[ARG1]], i64 %[[ARG2]])
168 // CHECK-TRAP: call void @llvm.ubsantrap(i8 0) [[NR_NUW]]
169 // CHECK-TRAP-NEXT: unreachable
170 #line 800
171 return a + b;
174 // CHECK-COMMON-LABEL: @no_return
175 int no_return(void) {
176 // Reaching the end of a noreturn function is fine in C.
177 // FIXME: If the user explicitly requests -fsanitize=return, we should catch
178 // that here even though it's not undefined behavior.
179 // CHECK-COMMON-NOT: call
180 // CHECK-COMMON-NOT: unreachable
181 // CHECK-COMMON: ret i32
184 // CHECK-UBSAN-LABEL: @vla_bound
185 void vla_bound(int n) {
186 // CHECK-UBSAN: icmp sgt i32 %[[PARAM:.*]], 0
188 // CHECK-UBSAN: %[[ARG:.*]] = zext i32 %[[PARAM]] to i64
189 // CHECK-UBSAN-NEXT: call void @__ubsan_handle_vla_bound_not_positive(ptr @[[LINE_900]], i64 %[[ARG]])
190 #line 900
191 int arr[n * 3];
194 // CHECK-UBSAN-LABEL: @vla_bound_unsigned
195 void vla_bound_unsigned(unsigned int n) {
196 // CHECK-UBSAN: icmp ugt i32 %[[PARAM:.*]], 0
198 // CHECK-UBSAN: %[[ARG:.*]] = zext i32 %[[PARAM]] to i64
199 // CHECK-UBSAN-NEXT: call void @__ubsan_handle_vla_bound_not_positive(ptr @[[LINE_1000]], i64 %[[ARG]])
200 #line 1000
201 int arr[n * 3];
204 // CHECK-UBSAN-LABEL: @int_float_no_overflow
205 float int_float_no_overflow(__int128 n) {
206 // CHECK-UBSAN-NOT: call void @__ubsan_handle
207 return n;
210 // CHECK-COMMON-LABEL: @int_float_overflow
211 float int_float_overflow(unsigned __int128 n) {
212 // CHECK-UBSAN-NOT: call {{.*}} @__ubsan_handle_float_cast_overflow(
213 // CHECK-TRAP-NOT: call {{.*}} @llvm.trap(
214 // CHECK-COMMON: }
215 return n;
218 // CHECK-COMMON-LABEL: @int_fp16_overflow
219 void int_fp16_overflow(int n, __fp16 *p) {
220 // CHECK-UBSAN-NOT: call {{.*}} @__ubsan_handle_float_cast_overflow(
221 // CHECK-COMMON: }
222 *p = n;
225 // CHECK-COMMON-LABEL: @float_int_overflow
226 int float_int_overflow(float f) {
227 // CHECK-COMMON: %[[GE:.*]] = fcmp ogt float %[[F:.*]], 0xC1E0000020000000
228 // CHECK-COMMON: %[[LE:.*]] = fcmp olt float %[[F]], 0x41E0000000000000
229 // CHECK-COMMON: %[[INBOUNDS:.*]] = and i1 %[[GE]], %[[LE]]
230 // CHECK-COMMON-NEXT: br i1 %[[INBOUNDS]]
232 // CHECK-UBSAN: %[[CAST:.*]] = bitcast float %[[F]] to i32
233 // CHECK-UBSAN: %[[ARG:.*]] = zext i32 %[[CAST]] to i64
234 // CHECK-UBSAN: call void @__ubsan_handle_float_cast_overflow(ptr @[[LINE_1200]], i64 %[[ARG]]
236 // CHECK-TRAP: call void @llvm.ubsantrap(i8 5) [[NR_NUW]]
237 // CHECK-TRAP-NEXT: unreachable
238 #line 1200
239 return f;
242 // CHECK-COMMON-LABEL: @long_double_int_overflow
243 int long_double_int_overflow(long double ld) {
244 // CHECK-UBSAN: alloca x86_fp80
246 // CHECK-COMMON: %[[GE:.*]] = fcmp ogt x86_fp80 %[[F:.*]], 0xKC01E800000010000000
247 // CHECK-COMMON: %[[LE:.*]] = fcmp olt x86_fp80 %[[F]], 0xK401E800000000000000
248 // CHECK-COMMON: %[[INBOUNDS:.*]] = and i1 %[[GE]], %[[LE]]
249 // CHECK-COMMON-NEXT: br i1 %[[INBOUNDS]]
251 // CHECK-UBSAN: store x86_fp80 %[[F]], ptr %[[ALLOCA:.*]], align 16, !nosanitize
252 // CHECK-UBSAN: %[[ARG:.*]] = ptrtoint ptr %[[ALLOCA]] to i64
253 // CHECK-UBSAN: call void @__ubsan_handle_float_cast_overflow(ptr @[[LINE_1300]], i64 %[[ARG]]
255 // CHECK-TRAP: call void @llvm.ubsantrap(i8 5) [[NR_NUW]]
256 // CHECK-TRAP-NEXT: unreachable
257 #line 1300
258 return ld;
261 // CHECK-COMMON-LABEL: @float_uint_overflow
262 unsigned float_uint_overflow(float f) {
263 // CHECK-COMMON: %[[GE:.*]] = fcmp ogt float %[[F:.*]], -1.{{0*}}e+00
264 // CHECK-COMMON: %[[LE:.*]] = fcmp olt float %[[F]], 0x41F0000000000000
265 // CHECK-COMMON: %[[INBOUNDS:.*]] = and i1 %[[GE]], %[[LE]]
266 // CHECK-COMMON-NEXT: br i1 %[[INBOUNDS]]
268 // CHECK-UBSAN: call void @__ubsan_handle_float_cast_overflow(ptr @[[LINE_1400]],
270 // CHECK-TRAP: call void @llvm.ubsantrap(i8 5) [[NR_NUW]]
271 // CHECK-TRAP-NEXT: unreachable
272 #line 1400
273 return f;
276 // CHECK-COMMON-LABEL: @fp16_char_overflow
277 signed char fp16_char_overflow(__fp16 *p) {
278 // CHECK-COMMON: %[[GE:.*]] = fcmp ogt float %[[F:.*]], -1.29{{0*}}e+02
279 // CHECK-COMMON: %[[LE:.*]] = fcmp olt float %[[F]], 1.28{{0*}}e+02
280 // CHECK-COMMON: %[[INBOUNDS:.*]] = and i1 %[[GE]], %[[LE]]
281 // CHECK-COMMON-NEXT: br i1 %[[INBOUNDS]]
283 // CHECK-UBSAN: call void @__ubsan_handle_float_cast_overflow(ptr @[[LINE_1500]],
285 // CHECK-TRAP: call void @llvm.ubsantrap(i8 5) [[NR_NUW]]
286 // CHECK-TRAP-NEXT: unreachable
287 #line 1500
288 return *p;
291 // CHECK-COMMON-LABEL: @float_float_overflow
292 float float_float_overflow(double f) {
293 // CHECK-UBSAN-NOT: call {{.*}} @__ubsan_handle_float_cast_overflow(
294 // CHECK-TRAP-NOT: call {{.*}} @llvm.ubsantrap(i8 19) [[NR_NUW]]
295 // CHECK-COMMON: }
296 return f;
299 // CHECK-COMMON-LABEL: @int_divide_overflow
300 // CHECK-OVERFLOW-LABEL: @int_divide_overflow
301 int int_divide_overflow(int a, int b) {
302 // CHECK-COMMON: %[[ZERO:.*]] = icmp ne i32 %[[B:.*]], 0
303 // CHECK-OVERFLOW-NOT: icmp ne i32 %{{.*}}, 0
305 // CHECK-COMMON: %[[AOK:.*]] = icmp ne i32 %[[A:.*]], -2147483648
306 // CHECK-COMMON-NEXT: %[[BOK:.*]] = icmp ne i32 %[[B]], -1
307 // CHECK-COMMON-NEXT: %[[OVER:.*]] = or i1 %[[AOK]], %[[BOK]]
308 // CHECK-COMMON: %[[OK:.*]] = and i1 %[[ZERO]], %[[OVER]]
309 // CHECK-COMMON: br i1 %[[OK]]
311 // CHECK-OVERFLOW: %[[AOK:.*]] = icmp ne i32 %[[A:.*]], -2147483648
312 // CHECK-OVERFLOW-NEXT: %[[BOK:.*]] = icmp ne i32 %[[B:.*]], -1
313 // CHECK-OVERFLOW-NEXT: %[[OK:.*]] = or i1 %[[AOK]], %[[BOK]]
314 // CHECK-OVERFLOW: br i1 %[[OK]]
316 // CHECK-TRAP: call void @llvm.ubsantrap(i8 3) [[NR_NUW]]
317 // CHECK-TRAP: unreachable
318 return a / b;
320 // CHECK-COMMON: }
321 // CHECK-OVERFLOW: }
324 // CHECK-COMMON-LABEL: @sour_bool
325 _Bool sour_bool(_Bool *p) {
326 // CHECK-COMMON: %[[OK:.*]] = icmp ule i8 {{.*}}, 1
327 // CHECK-COMMON: br i1 %[[OK]]
329 // CHECK-UBSAN: call void @__ubsan_handle_load_invalid_value(ptr {{.*}}, i64 {{.*}})
331 // CHECK-TRAP: call void @llvm.ubsantrap(i8 10) [[NR_NUW]]
332 // CHECK-TRAP: unreachable
333 return *p;
336 // CHECK-COMMON-LABEL: @ret_nonnull
337 __attribute__((returns_nonnull))
338 int *ret_nonnull(int *a) {
339 // CHECK-COMMON: [[OK:%.*]] = icmp ne ptr {{.*}}, null
340 // CHECK-COMMON: br i1 [[OK]]
342 // CHECK-UBSAN: call void @__ubsan_handle_nonnull_return
344 // CHECK-TRAP: call void @llvm.ubsantrap(i8 17) [[NR_NUW]]
345 // CHECK-TRAP: unreachable
346 return a;
349 // CHECK-COMMON-LABEL: @call_decl_nonnull
350 __attribute__((nonnull)) void decl_nonnull(int *a);
351 void call_decl_nonnull(int *a) {
352 // CHECK-COMMON: [[OK:%.*]] = icmp ne ptr {{.*}}, null
353 // CHECK-COMMON: br i1 [[OK]]
355 // CHECK-UBSAN: call void @__ubsan_handle_nonnull_arg
357 // CHECK-TRAP: call void @llvm.ubsantrap(i8 16) [[NR_NUW]]
358 // CHECK-TRAP: unreachable
359 decl_nonnull(a);
362 extern void *memcpy(void *, const void *, unsigned long) __attribute__((nonnull(1, 2)));
364 // CHECK-COMMON-LABEL: @call_memcpy_nonnull
365 void call_memcpy_nonnull(void *p, void *q, int sz) {
366 // CHECK-COMMON: icmp ne ptr {{.*}}, null
367 // CHECK-UBSAN: call void @__ubsan_handle_nonnull_arg
368 // CHECK-UBSAN-NO-ALIGNMENT-BUILTIN: call void @__ubsan_handle_nonnull_arg
369 // CHECK-TRAP: call void @llvm.ubsantrap(i8 16)
370 // CHECK-COMMON-NOT: call
372 // CHECK-COMMON: icmp ne ptr {{.*}}, null
373 // CHECK-UBSAN: call void @__ubsan_handle_nonnull_arg
374 // CHECK-UBSAN-NO-ALIGNMENT-BUILTIN: call void @__ubsan_handle_nonnull_arg
375 // CHECK-TRAP: call void @llvm.ubsantrap(i8 16)
376 // CHECK-COMMON-NOT: call
378 // CHECK-COMMON: call void @llvm.memcpy.p0.p0.i64(ptr align 1 %0, ptr align 1 %1, i64 %conv, i1 false)
379 memcpy(p, q, sz);
382 // CHECK-COMMON-LABEL: define{{.*}} void @call_memcpy(
383 void call_memcpy(long *p, short *q, int sz) {
384 // CHECK-COMMON: icmp ne ptr {{.*}}, null
385 // CHECK-UBSAN: call void @__ubsan_handle_nonnull_arg(
386 // CHECK-UBSAN-NO-ALIGNMENT-BUILTIN: call void @__ubsan_handle_nonnull_arg(
387 // CHECK-TRAP: call void @llvm.ubsantrap(i8 16)
388 // CHECK-ALIGNMENT-BUILTIN: and i64 %[[#]], 7, !nosanitize
389 // CHECK-ALIGNMENT-BUILTIN: icmp eq i64 %[[#]], 0, !nosanitize
390 // CHECK-UBSAN: call void @__ubsan_handle_type_mismatch_v1(ptr @[[LINE_1600]]
391 // CHECK-UBSAN-NO-ALIGNMENT-BUILTIN-NOT: call void @__ubsan_handle_type_mismatch_v1(
392 // CHECK-TRAP: call void @llvm.ubsantrap(i8 22)
394 // CHECK-COMMON: icmp ne ptr {{.*}}, null
395 // CHECK-UBSAN: call void @__ubsan_handle_nonnull_arg(
396 // CHECK-UBSAN-NO-ALIGNMENT-BUILTIN: call void @__ubsan_handle_nonnull_arg(
397 // CHECK-UBSAN-DISABLE-BUILTIN: call
398 // CHECK-TRAP: call void @llvm.ubsantrap(i8 16)
399 // CHECK-ALIGNMENT-BUILTIN: and i64 %[[#]], 1, !nosanitize
400 // CHECK-ALIGNMENT-BUILTIN: icmp eq i64 %[[#]], 0, !nosanitize
401 // CHECK-UBSAN: call void @__ubsan_handle_type_mismatch_v1(
402 // CHECK-UBSAN-NO-ALIGNMENT-BUILTIN-NOT: call
403 // CHECK-TRAP: call void @llvm.ubsantrap(i8 22)
405 // CHECK-COMMON: call void @llvm.memcpy.p0.p0.i64(ptr align 8 %0, ptr align 2 %1, i64 %conv, i1 false)
407 // CHECK-UBSAN-NOT: call void @__ubsan_handle_type_mismatch_v1(
408 // CHECK-COMMON: call void @llvm.memcpy.p0.p0.i64(ptr align 1 %[[#]], ptr align 1 %[[#]], i64 %{{.*}}, i1 false)
409 #line 1600
410 memcpy(p, q, sz);
411 /// Casting to void * or char * drops the alignment requirement.
412 memcpy((void *)p, (char *)q, sz);
415 // CHECK-COMMON-LABEL: define{{.*}} void @call_memcpy_inline(
416 void call_memcpy_inline(long *p, short *q) {
417 // CHECK-ALIGNMENT-BUILTIN: and i64 %[[#]], 7, !nosanitize
418 // CHECK-ALIGNMENT-BUILTIN: icmp eq i64 %[[#]], 0, !nosanitize
419 // CHECK-UBSAN: call void @__ubsan_handle_type_mismatch_v1(
420 // CHECK-UBSAN-NO-ALIGNMENT-BUILTIN-NOT: call
421 // CHECK-TRAP: call void @llvm.ubsantrap(i8 22)
423 // CHECK-ALIGNMENT-BUILTIN: and i64 %[[#]], 1, !nosanitize
424 // CHECK-ALIGNMENT-BUILTIN: icmp eq i64 %[[#]], 0, !nosanitize
425 // CHECK-UBSAN: call void @__ubsan_handle_type_mismatch_v1(
426 // CHECK-UBSAN-NO-ALIGNMENT-BUILTIN-NOT: call
427 // CHECK-TRAP: call void @llvm.ubsantrap(i8 22)
429 // CHECK-COMMON: call void @llvm.memcpy.inline.p0.p0.i64(ptr align 8 %0, ptr align 2 %1, i64 2, i1 false)
430 __builtin_memcpy_inline(p, q, 2);
433 extern void *memmove(void *, const void *, unsigned long) __attribute__((nonnull(1, 2)));
435 // CHECK-COMMON-LABEL: @call_memmove_nonnull
436 void call_memmove_nonnull(void *p, void *q, int sz) {
437 // CHECK-COMMON: icmp ne ptr {{.*}}, null
438 // CHECK-UBSAN: call void @__ubsan_handle_nonnull_arg
439 // CHECK-UBSAN-NO-ALIGNMENT-BUILTIN: call void @__ubsan_handle_nonnull_arg(
440 // CHECK-TRAP: call void @llvm.ubsantrap(i8 16)
442 // CHECK-COMMON: icmp ne ptr {{.*}}, null
443 // CHECK-UBSAN: call void @__ubsan_handle_nonnull_arg
444 // CHECK-UBSAN-NO-ALIGNMENT-BUILTIN: call void @__ubsan_handle_nonnull_arg(
445 // CHECK-TRAP: call void @llvm.ubsantrap(i8 16)
446 memmove(p, q, sz);
449 // CHECK-COMMON-LABEL: define{{.*}} void @call_memmove(
450 void call_memmove(long *p, short *q, int sz) {
451 // CHECK-COMMON: icmp ne ptr {{.*}}, null
452 // CHECK-UBSAN: call void @__ubsan_handle_nonnull_arg(
453 // CHECK-UBSAN-NO-ALIGNMENT-BUILTIN: call void @__ubsan_handle_nonnull_arg(
454 // CHECK-TRAP: call void @llvm.ubsantrap(i8 16)
455 // CHECK-ALIGNMENT-BUILTIN: and i64 %[[#]], 7, !nosanitize
456 // CHECK-ALIGNMENT-BUILTIN: icmp eq i64 %[[#]], 0, !nosanitize
457 // CHECK-UBSAN: call void @__ubsan_handle_type_mismatch_v1(
458 // CHECK-UBSAN-NO-ALIGNMENT-BUILTIN-NOT: call
459 // CHECK-TRAP: call void @llvm.ubsantrap(i8 22)
461 // CHECK-COMMON: icmp ne ptr {{.*}}, null
462 // CHECK-UBSAN: call void @__ubsan_handle_nonnull_arg(
463 // CHECK-UBSAN-NO-ALIGNMENT-BUILTIN: call void @__ubsan_handle_nonnull_arg(
464 // CHECK-TRAP: call void @llvm.ubsantrap(i8 16)
465 // CHECK-ALIGNMENT-BUILTIN: and i64 %[[#]], 1, !nosanitize
466 // CHECK-ALIGNMENT-BUILTIN: icmp eq i64 %[[#]], 0, !nosanitize
467 // CHECK-UBSAN: call void @__ubsan_handle_type_mismatch_v1(
468 // CHECK-UBSAN-NO-ALIGNMENT-BUILTIN-NOT: call
469 // CHECK-TRAP: call void @llvm.ubsantrap(i8 22)
471 // CHECK-COMMON: call void @llvm.memmove.p0.p0.i64(ptr align 8 %0, ptr align 2 %1, i64 %conv, i1 false)
472 memmove(p, q, sz);
475 // CHECK-COMMON-LABEL: @call_nonnull_variadic
476 __attribute__((nonnull)) void nonnull_variadic(int a, ...);
477 void call_nonnull_variadic(int a, int *b) {
478 // CHECK-COMMON: [[OK:%.*]] = icmp ne ptr {{.*}}, null
479 // CHECK-COMMON: br i1 [[OK]]
481 // CHECK-UBSAN: call void @__ubsan_handle_nonnull_arg
482 // CHECK-UBSAN-NOT: __ubsan_handle_nonnull_arg
484 // CHECK-COMMON: call void (i32, ...) @nonnull_variadic
485 nonnull_variadic(a, b);
488 // CHECK-UBSAN: ![[WEIGHT_MD]] = !{!"branch_weights", i32 1048575, i32 1}
490 // CHECK-TRAP: attributes [[NR_NUW]] = { noreturn nounwind }