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