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-prefix=CHECK-COMMON --check-prefix=CHECK-UBSAN
2 // RUN: %clang_cc1 -fsanitize-undefined-trap-on-error -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-prefix=CHECK-COMMON --check-prefix=CHECK-TRAP
3 // RUN: %clang_cc1 -fsanitize=null -fsanitize-recover=null -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck %s --check-prefix=CHECK-NULL
4 // RUN: %clang_cc1 -fsanitize=signed-integer-overflow -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck %s --check-prefix=CHECK-OVERFLOW
5 // REQUIRES: asserts
6 
7 // CHECK-UBSAN: @[[INT:.*]] = private unnamed_addr constant { i16, i16, [6 x i8] } { i16 0, i16 11, [6 x i8] c"'int'\00" }
8 
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]], i64 4, i8 1
11 // CHECK-UBSAN: @[[LINE_200:.*]] = {{.*}}, i32 200, i32 10 {{.*}}, i64 4, 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 {{.*}} @{{.*}}, i64 4, i8 0 }
15 // CHECK-UBSAN: @[[LINE_600:.*]] = {{.*}}, i32 600, i32 3 {{.*}} @{{.*}}, i64 4, i8 1 }
16 
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" }
18 
19 // CHECK-UBSAN: @[[LINE_700:.*]] = {{.*}}, i32 700, i32 14 {{.*}} @[[STRUCT_S]], i64 4, i8 3 }
20 // CHECK-UBSAN: @[[LINE_800:.*]] = {{.*}}, i32 800, i32 12 {{.*}} @{{.*}} }
21 // CHECK-UBSAN: @[[LINE_900:.*]] = {{.*}}, i32 900, i32 11 {{.*}} @{{.*}} }
22 
23 // CHECK-NULL: @[[LINE_100:.*]] = private unnamed_addr global {{.*}}, i32 100, i32 5 {{.*}}
24 
25 // PR6805
26 // CHECK-COMMON-LABEL: @foo
27 // CHECK-NULL-LABEL: @foo
foo()28 void foo() {
29   union { int i; } u;
30   // CHECK-COMMON:      %[[CHECK0:.*]] = icmp ne {{.*}}* %[[PTR:.*]], null
31 
32   // CHECK-COMMON:      %[[I8PTR:.*]] = bitcast i32* %[[PTR]] to i8*
33   // CHECK-COMMON-NEXT: %[[SIZE:.*]] = call i64 @llvm.objectsize.i64.p0i8(i8* %[[I8PTR]], i1 false)
34   // CHECK-COMMON-NEXT: %[[CHECK1:.*]] = icmp uge i64 %[[SIZE]], 4
35 
36   // CHECK-COMMON:      %[[PTRTOINT:.*]] = ptrtoint {{.*}}* %[[PTR]] to i64
37   // CHECK-COMMON-NEXT: %[[MISALIGN:.*]] = and i64 %[[PTRTOINT]], 3
38   // CHECK-COMMON-NEXT: %[[CHECK2:.*]] = icmp eq i64 %[[MISALIGN]], 0
39 
40   // CHECK-COMMON:       %[[CHECK01:.*]] = and i1 %[[CHECK0]], %[[CHECK1]]
41   // CHECK-COMMON-NEXT:  %[[OK:.*]] = and i1 %[[CHECK01]], %[[CHECK2]]
42 
43   // CHECK-UBSAN: br i1 %[[OK]], {{.*}} !prof ![[WEIGHT_MD:.*]], !nosanitize
44   // CHECK-TRAP:  br i1 %[[OK]], {{.*}}
45 
46   // CHECK-UBSAN:      %[[ARG:.*]] = ptrtoint {{.*}} %[[PTR]] to i64
47   // CHECK-UBSAN-NEXT: call void @__ubsan_handle_type_mismatch(i8* bitcast ({{.*}} @[[LINE_100]] to i8*), i64 %[[ARG]])
48 
49   // CHECK-TRAP:      call void @llvm.trap() [[NR_NUW:#[0-9]+]]
50   // CHECK-TRAP-NEXT: unreachable
51 
52   // With -fsanitize=null, only perform the null check.
53   // CHECK-NULL: %[[NULL:.*]] = icmp ne {{.*}}, null
54   // CHECK-NULL: br i1 %[[NULL]]
55   // CHECK-NULL: call void @__ubsan_handle_type_mismatch(i8* bitcast ({{.*}} @[[LINE_100]] to i8*), i64 %{{.*}})
56 #line 100
57   u.i=1;
58 }
59 
60 // CHECK-COMMON-LABEL: @bar
bar(int * a)61 int bar(int *a) {
62   // CHECK-COMMON:      %[[SIZE:.*]] = call i64 @llvm.objectsize.i64
63   // CHECK-COMMON-NEXT: icmp uge i64 %[[SIZE]], 4
64 
65   // CHECK-COMMON:      %[[PTRINT:.*]] = ptrtoint
66   // CHECK-COMMON-NEXT: %[[MISALIGN:.*]] = and i64 %[[PTRINT]], 3
67   // CHECK-COMMON-NEXT: icmp eq i64 %[[MISALIGN]], 0
68 
69   // CHECK-UBSAN:      %[[ARG:.*]] = ptrtoint
70   // CHECK-UBSAN-NEXT: call void @__ubsan_handle_type_mismatch(i8* bitcast ({{.*}} @[[LINE_200]] to i8*), i64 %[[ARG]])
71 
72   // CHECK-TRAP:      call void @llvm.trap() [[NR_NUW]]
73   // CHECK-TRAP-NEXT: unreachable
74 
75 #line 200
76   return *a;
77 }
78 
79 // CHECK-UBSAN-LABEL: @addr_space
addr_space(int * a)80 int addr_space(int __attribute__((address_space(256))) *a) {
81   // CHECK-UBSAN-NOT: __ubsan
82   return *a;
83 }
84 
85 // CHECK-COMMON-LABEL: @lsh_overflow
lsh_overflow(int a,int b)86 int lsh_overflow(int a, int b) {
87   // CHECK-COMMON:      %[[RHS_INBOUNDS:.*]] = icmp ule i32 %[[RHS:.*]], 31
88   // CHECK-COMMON-NEXT: br i1 %[[RHS_INBOUNDS]], label %[[CHECK_BB:.*]], label %[[CONT_BB:.*]],
89 
90   // CHECK-COMMON:      [[CHECK_BB]]:
91   // CHECK-COMMON-NEXT: %[[SHIFTED_OUT_WIDTH:.*]] = sub nuw nsw i32 31, %[[RHS]]
92   // CHECK-COMMON-NEXT: %[[SHIFTED_OUT:.*]] = lshr i32 %[[LHS:.*]], %[[SHIFTED_OUT_WIDTH]]
93   // CHECK-COMMON-NEXT: %[[NO_OVERFLOW:.*]] = icmp eq i32 %[[SHIFTED_OUT]], 0
94   // CHECK-COMMON-NEXT: br label %[[CONT_BB]]
95 
96   // CHECK-COMMON:      [[CONT_BB]]:
97   // CHECK-COMMON-NEXT: %[[VALID_BASE:.*]] = phi i1 [ true, {{.*}} ], [ %[[NO_OVERFLOW]], %[[CHECK_BB]] ]
98   // CHECK-COMMON-NEXT: %[[VALID:.*]] = and i1 %[[RHS_INBOUNDS]], %[[VALID_BASE]]
99 
100   // CHECK-UBSAN: br i1 %[[VALID]], {{.*}} !prof ![[WEIGHT_MD]]
101   // CHECK-TRAP:  br i1 %[[VALID]]
102 
103   // CHECK-UBSAN:      %[[ARG1:.*]] = zext
104   // CHECK-UBSAN-NEXT: %[[ARG2:.*]] = zext
105   // CHECK-UBSAN-NEXT: call void @__ubsan_handle_shift_out_of_bounds(i8* bitcast ({{.*}} @[[LINE_300]] to i8*), i64 %[[ARG1]], i64 %[[ARG2]])
106   // CHECK-UBSAN-NOT:  call void @__ubsan_handle_shift_out_of_bounds
107 
108   // CHECK-TRAP:      call void @llvm.trap() [[NR_NUW]]
109   // CHECK-TRAP:      unreachable
110   // CHECK-TRAP-NOT:  call void @llvm.trap()
111 
112   // CHECK-COMMON:      %[[RET:.*]] = shl i32 %[[LHS]], %[[RHS]]
113   // CHECK-COMMON-NEXT: ret i32 %[[RET]]
114 #line 300
115   return a << b;
116 }
117 
118 // CHECK-COMMON-LABEL: @rsh_inbounds
rsh_inbounds(int a,int b)119 int rsh_inbounds(int a, int b) {
120   // CHECK-COMMON:      %[[INBOUNDS:.*]] = icmp ule i32 %[[RHS:.*]], 31
121   // CHECK-COMMON:      br i1 %[[INBOUNDS]]
122 
123   // CHECK-UBSAN:      %[[ARG1:.*]] = zext
124   // CHECK-UBSAN-NEXT: %[[ARG2:.*]] = zext
125   // CHECK-UBSAN-NEXT: call void @__ubsan_handle_shift_out_of_bounds(i8* bitcast ({{.*}} @[[LINE_400]] to i8*), i64 %[[ARG1]], i64 %[[ARG2]])
126 
127   // CHECK-TRAP:      call void @llvm.trap() [[NR_NUW]]
128   // CHECK-TRAP-NEXT: unreachable
129 
130   // CHECK-COMMON:      %[[RET:.*]] = ashr i32 {{.*}}, %[[RHS]]
131   // CHECK-COMMON-NEXT: ret i32 %[[RET]]
132 #line 400
133   return a >> b;
134 }
135 
136 // CHECK-COMMON-LABEL: @load
load(int * p)137 int load(int *p) {
138   // CHECK-UBSAN: call void @__ubsan_handle_type_mismatch(i8* bitcast ({{.*}} @[[LINE_500]] to i8*), i64 %{{.*}})
139 
140   // CHECK-TRAP:      call void @llvm.trap() [[NR_NUW]]
141   // CHECK-TRAP-NEXT: unreachable
142 #line 500
143   return *p;
144 }
145 
146 // CHECK-COMMON-LABEL: @store
store(int * p,int q)147 void store(int *p, int q) {
148   // CHECK-UBSAN: call void @__ubsan_handle_type_mismatch(i8* bitcast ({{.*}} @[[LINE_600]] to i8*), i64 %{{.*}})
149 
150   // CHECK-TRAP:      call void @llvm.trap() [[NR_NUW]]
151   // CHECK-TRAP-NEXT: unreachable
152 #line 600
153   *p = q;
154 }
155 
156 struct S { int k; };
157 
158 // CHECK-COMMON-LABEL: @member_access
member_access(struct S * p)159 int *member_access(struct S *p) {
160   // CHECK-UBSAN: call void @__ubsan_handle_type_mismatch(i8* bitcast ({{.*}} @[[LINE_700]] to i8*), i64 %{{.*}})
161 
162   // CHECK-TRAP:      call void @llvm.trap() [[NR_NUW]]
163   // CHECK-TRAP-NEXT: unreachable
164 #line 700
165   return &p->k;
166 }
167 
168 // CHECK-COMMON-LABEL: @signed_overflow
signed_overflow(int a,int b)169 int signed_overflow(int a, int b) {
170   // CHECK-UBSAN:      %[[ARG1:.*]] = zext
171   // CHECK-UBSAN-NEXT: %[[ARG2:.*]] = zext
172   // CHECK-UBSAN-NEXT: call void @__ubsan_handle_add_overflow(i8* bitcast ({{.*}} @[[LINE_800]] to i8*), i64 %[[ARG1]], i64 %[[ARG2]])
173 
174   // CHECK-TRAP:      call void @llvm.trap() [[NR_NUW]]
175   // CHECK-TRAP-NEXT: unreachable
176 #line 800
177   return a + b;
178 }
179 
180 // CHECK-COMMON-LABEL: @no_return
no_return()181 int no_return() {
182   // Reaching the end of a noreturn function is fine in C.
183   // FIXME: If the user explicitly requests -fsanitize=return, we should catch
184   //        that here even though it's not undefined behavior.
185   // CHECK-COMMON-NOT: call
186   // CHECK-COMMON-NOT: unreachable
187   // CHECK-COMMON: ret i32
188 }
189 
190 // CHECK-UBSAN-LABEL: @vla_bound
vla_bound(int n)191 void vla_bound(int n) {
192   // CHECK-UBSAN:      icmp sgt i32 %[[PARAM:.*]], 0
193   //
194   // CHECK-UBSAN:      %[[ARG:.*]] = zext i32 %[[PARAM]] to i64
195   // CHECK-UBSAN-NEXT: call void @__ubsan_handle_vla_bound_not_positive(i8* bitcast ({{.*}} @[[LINE_900]] to i8*), i64 %[[ARG]])
196 #line 900
197   int arr[n * 3];
198 }
199 
200 // CHECK-UBSAN-LABEL: @int_float_no_overflow
int_float_no_overflow(__int128 n)201 float int_float_no_overflow(__int128 n) {
202   // CHECK-UBSAN-NOT: call void @__ubsan_handle
203   return n;
204 }
205 
206 // CHECK-COMMON-LABEL: @int_float_overflow
int_float_overflow(unsigned __int128 n)207 float int_float_overflow(unsigned __int128 n) {
208   // This is 2**104. FLT_MAX is 2**128 - 2**104.
209   // CHECK-COMMON: %[[INBOUNDS:.*]] = icmp ule i128 %{{.*}}, -20282409603651670423947251286016
210   // CHECK-COMMON-NEXT: br i1 %[[INBOUNDS]]
211 
212   // CHECK-UBSAN: call void @__ubsan_handle_float_cast_overflow(
213 
214   // CHECK-TRAP:      call void @llvm.trap() [[NR_NUW]]
215   // CHECK-TRAP-NEXT: unreachable
216   return n;
217 }
218 
219 // CHECK-COMMON-LABEL: @int_fp16_overflow
int_fp16_overflow(int n,__fp16 * p)220 void int_fp16_overflow(int n, __fp16 *p) {
221   // CHECK-COMMON: %[[GE:.*]] = icmp sge i32 %{{.*}}, -65504
222   // CHECK-COMMON: %[[LE:.*]] = icmp sle i32 %{{.*}}, 65504
223   // CHECK-COMMON: %[[INBOUNDS:.*]] = and i1 %[[GE]], %[[LE]]
224   // CHECK-COMMON-NEXT: br i1 %[[INBOUNDS]]
225 
226   // CHECK-UBSAN: call void @__ubsan_handle_float_cast_overflow(
227 
228   // CHECK-TRAP:      call void @llvm.trap() [[NR_NUW]]
229   // CHECK-TRAP-NEXT: unreachable
230   *p = n;
231 }
232 
233 // CHECK-COMMON-LABEL: @float_int_overflow
float_int_overflow(float f)234 int float_int_overflow(float f) {
235   // CHECK-COMMON: %[[GE:.*]] = fcmp ogt float %[[F:.*]], 0xC1E0000020000000
236   // CHECK-COMMON: %[[LE:.*]] = fcmp olt float %[[F]], 0x41E0000000000000
237   // CHECK-COMMON: %[[INBOUNDS:.*]] = and i1 %[[GE]], %[[LE]]
238   // CHECK-COMMON-NEXT: br i1 %[[INBOUNDS]]
239 
240   // CHECK-UBSAN: %[[CAST:.*]] = bitcast float %[[F]] to i32
241   // CHECK-UBSAN: %[[ARG:.*]] = zext i32 %[[CAST]] to i64
242   // CHECK-UBSAN: call void @__ubsan_handle_float_cast_overflow({{.*}}, i64 %[[ARG]]
243 
244   // CHECK-TRAP:      call void @llvm.trap() [[NR_NUW]]
245   // CHECK-TRAP-NEXT: unreachable
246   return f;
247 }
248 
249 // CHECK-COMMON-LABEL: @long_double_int_overflow
long_double_int_overflow(long double ld)250 int long_double_int_overflow(long double ld) {
251   // CHECK-UBSAN: alloca x86_fp80
252 
253   // CHECK-COMMON: %[[GE:.*]] = fcmp ogt x86_fp80 %[[F:.*]], 0xKC01E800000010000000
254   // CHECK-COMMON: %[[LE:.*]] = fcmp olt x86_fp80 %[[F]], 0xK401E800000000000000
255   // CHECK-COMMON: %[[INBOUNDS:.*]] = and i1 %[[GE]], %[[LE]]
256   // CHECK-COMMON-NEXT: br i1 %[[INBOUNDS]]
257 
258   // CHECK-UBSAN: store x86_fp80 %[[F]], x86_fp80* %[[ALLOCA:.*]], !nosanitize
259   // CHECK-UBSAN: %[[ARG:.*]] = ptrtoint x86_fp80* %[[ALLOCA]] to i64
260   // CHECK-UBSAN: call void @__ubsan_handle_float_cast_overflow({{.*}}, i64 %[[ARG]]
261 
262   // CHECK-TRAP:      call void @llvm.trap() [[NR_NUW]]
263   // CHECK-TRAP-NEXT: unreachable
264   return ld;
265 }
266 
267 // CHECK-COMMON-LABEL: @float_uint_overflow
float_uint_overflow(float f)268 unsigned float_uint_overflow(float f) {
269   // CHECK-COMMON: %[[GE:.*]] = fcmp ogt float %[[F:.*]], -1.{{0*}}e+00
270   // CHECK-COMMON: %[[LE:.*]] = fcmp olt float %[[F]], 0x41F0000000000000
271   // CHECK-COMMON: %[[INBOUNDS:.*]] = and i1 %[[GE]], %[[LE]]
272   // CHECK-COMMON-NEXT: br i1 %[[INBOUNDS]]
273 
274   // CHECK-UBSAN: call void @__ubsan_handle_float_cast_overflow(
275 
276   // CHECK-TRAP:      call void @llvm.trap() [[NR_NUW]]
277   // CHECK-TRAP-NEXT: unreachable
278   return f;
279 }
280 
281 // CHECK-COMMON-LABEL: @fp16_char_overflow
fp16_char_overflow(__fp16 * p)282 signed char fp16_char_overflow(__fp16 *p) {
283   // CHECK-COMMON: %[[GE:.*]] = fcmp ogt float %[[F:.*]], -1.29{{0*}}e+02
284   // CHECK-COMMON: %[[LE:.*]] = fcmp olt float %[[F]], 1.28{{0*}}e+02
285   // CHECK-COMMON: %[[INBOUNDS:.*]] = and i1 %[[GE]], %[[LE]]
286   // CHECK-COMMON-NEXT: br i1 %[[INBOUNDS]]
287 
288   // CHECK-UBSAN: call void @__ubsan_handle_float_cast_overflow(
289 
290   // CHECK-TRAP:      call void @llvm.trap() [[NR_NUW]]
291   // CHECK-TRAP-NEXT: unreachable
292   return *p;
293 }
294 
295 // CHECK-COMMON-LABEL: @float_float_overflow
float_float_overflow(double f)296 float float_float_overflow(double f) {
297   // CHECK-COMMON: %[[F:.*]] = call double @llvm.fabs.f64(
298   // CHECK-COMMON: %[[GE:.*]] = fcmp ogt double %[[F]], 0x47EFFFFFE0000000
299   // CHECK-COMMON: %[[LE:.*]] = fcmp olt double %[[F]], 0x7FF0000000000000
300   // CHECK-COMMON: %[[OUTOFBOUNDS:.*]] = and i1 %[[GE]], %[[LE]]
301   // CHECK-COMMON: %[[INBOUNDS:.*]] = xor i1 %[[OUTOFBOUNDS]], true
302   // CHECK-COMMON-NEXT: br i1 %[[INBOUNDS]]
303 
304   // CHECK-UBSAN: call void @__ubsan_handle_float_cast_overflow(
305 
306   // CHECK-TRAP:      call void @llvm.trap() [[NR_NUW]]
307   // CHECK-TRAP-NEXT: unreachable
308   return f;
309 }
310 
311 // CHECK-COMMON-LABEL:   @int_divide_overflow
312 // CHECK-OVERFLOW-LABEL: @int_divide_overflow
int_divide_overflow(int a,int b)313 int int_divide_overflow(int a, int b) {
314   // CHECK-COMMON:         %[[ZERO:.*]] = icmp ne i32 %[[B:.*]], 0
315   // CHECK-OVERFLOW-NOT:  icmp ne i32 %{{.*}}, 0
316 
317   // CHECK-COMMON:               %[[AOK:.*]] = icmp ne i32 %[[A:.*]], -2147483648
318   // CHECK-COMMON-NEXT:          %[[BOK:.*]] = icmp ne i32 %[[B]], -1
319   // CHECK-COMMON-NEXT:          %[[OVER:.*]] = or i1 %[[AOK]], %[[BOK]]
320   // CHECK-COMMON:         %[[OK:.*]] = and i1 %[[ZERO]], %[[OVER]]
321   // CHECK-COMMON:         br i1 %[[OK]]
322 
323   // CHECK-OVERFLOW:      %[[AOK:.*]] = icmp ne i32 %[[A:.*]], -2147483648
324   // CHECK-OVERFLOW-NEXT: %[[BOK:.*]] = icmp ne i32 %[[B:.*]], -1
325   // CHECK-OVERFLOW-NEXT: %[[OK:.*]] = or i1 %[[AOK]], %[[BOK]]
326   // CHECK-OVERFLOW:      br i1 %[[OK]]
327 
328   // CHECK-TRAP: call void @llvm.trap() [[NR_NUW]]
329   // CHECK-TRAP: unreachable
330   return a / b;
331 
332   // CHECK-COMMON:          }
333   // CHECK-OVERFLOW: }
334 }
335 
336 // CHECK-COMMON-LABEL: @sour_bool
sour_bool(_Bool * p)337 _Bool sour_bool(_Bool *p) {
338   // CHECK-COMMON: %[[OK:.*]] = icmp ule i8 {{.*}}, 1
339   // CHECK-COMMON: br i1 %[[OK]]
340 
341   // CHECK-UBSAN: call void @__ubsan_handle_load_invalid_value(i8* bitcast ({{.*}}), i64 {{.*}})
342 
343   // CHECK-TRAP: call void @llvm.trap() [[NR_NUW]]
344   // CHECK-TRAP: unreachable
345   return *p;
346 }
347 
348 // CHECK-COMMON-LABEL: @ret_nonnull
349 __attribute__((returns_nonnull))
ret_nonnull(int * a)350 int *ret_nonnull(int *a) {
351   // CHECK-COMMON: [[OK:%.*]] = icmp ne i32* {{.*}}, null
352   // CHECK-COMMON: br i1 [[OK]]
353 
354   // CHECK-UBSAN: call void @__ubsan_handle_nonnull_return
355 
356   // CHECK-TRAP: call void @llvm.trap() [[NR_NUW]]
357   // CHECK-TRAP: unreachable
358   return a;
359 }
360 
361 // CHECK-COMMON-LABEL: @call_decl_nonnull
362 __attribute__((nonnull)) void decl_nonnull(int *a);
call_decl_nonnull(int * a)363 void call_decl_nonnull(int *a) {
364   // CHECK-COMMON: [[OK:%.*]] = icmp ne i32* {{.*}}, null
365   // CHECK-COMMON: br i1 [[OK]]
366 
367   // CHECK-UBSAN: call void @__ubsan_handle_nonnull_arg
368 
369   // CHECK-TRAP: call void @llvm.trap() [[NR_NUW]]
370   // CHECK-TRAP: unreachable
371   decl_nonnull(a);
372 }
373 
374 // CHECK-COMMON-LABEL: @call_nonnull_variadic
375 __attribute__((nonnull)) void nonnull_variadic(int a, ...);
call_nonnull_variadic(int a,int * b)376 void call_nonnull_variadic(int a, int *b) {
377   // CHECK-COMMON: [[OK:%.*]] = icmp ne i32* {{.*}}, null
378   // CHECK-COMMON: br i1 [[OK]]
379 
380   // CHECK-UBSAN: call void @__ubsan_handle_nonnull_arg
381   // CHECK-UBSAN-NOT: __ubsan_handle_nonnull_arg
382 
383   // CHECK-COMMON: call void (i32, ...) @nonnull_variadic
384   nonnull_variadic(a, b);
385 }
386 
387 // CHECK-UBSAN: ![[WEIGHT_MD]] = !{!"branch_weights", i32 1048575, i32 1}
388 
389 // CHECK-TRAP: attributes [[NR_NUW]] = { noreturn nounwind }
390