2 // RUN: %clang_cc1 -no-opaque-pointers -x objective-c -emit-llvm -triple x86_64-apple-macosx10.10.0 -fsanitize=nullability-return,returns-nonnull-attribute,nullability-arg,nonnull-attribute %s -o - -w | FileCheck %s
4 // If both the annotation and the attribute are present, prefer the attribute,
5 // since it actually affects IRGen.
7 // CHECK-LABEL: define{{.*}} nonnull i32* @f1
8 __attribute__((returns_nonnull)) int *_Nonnull f1(int *_Nonnull p) {
10 // CHECK-NEXT: [[SLOC_PTR:%.*]] = alloca i8*
11 // CHECK-NEXT: [[ADDR:%.*]] = alloca i32*
12 // CHECK-NEXT: store i8* null, i8** [[SLOC_PTR]]
13 // CHECK-NEXT: store i32* [[P:%.*]], i32** [[ADDR]]
14 // CHECK-NEXT: store {{.*}} [[SLOC_PTR]]
15 // CHECK-NEXT: [[ARG:%.*]] = load i32*, i32** [[ADDR]]
16 // CHECK-NEXT: [[SLOC:%.*]] = load {{.*}} [[SLOC_PTR]]
17 // CHECK-NEXT: [[SLOC_NONNULL:%.*]] = icmp ne i8* [[SLOC]], null
18 // CHECK-NEXT: br i1 [[SLOC_NONNULL]], label %nullcheck
21 // CHECK-NEXT: [[ICMP:%.*]] = icmp ne i32* [[ARG]], null, !nosanitize
22 // CHECK-NEXT: br i1 [[ICMP]], label %[[CONT:.+]], label %[[HANDLE:[^,]+]]
24 // CHECK: call void @__ubsan_handle_nonnull_return
25 // CHECK-NEXT: unreachable, !nosanitize
27 // CHECK-NEXT: br label %no.nullcheck
28 // CHECK: no.nullcheck:
29 // CHECK-NEXT: ret i32* [[ARG]]
33 // CHECK-LABEL: define{{.*}} void @f2
34 void f2(int *_Nonnull __attribute__((nonnull)) p) {}
36 // CHECK-LABEL: define{{.*}} void @call_f2
38 // CHECK: call void @__ubsan_handle_nonnull_arg_abort
39 // CHECK-NOT: call void @__ubsan_handle_nonnull_arg_abort
43 // If the return value isn't meant to be checked, make sure we don't check it.
44 // CHECK-LABEL: define{{.*}} i32* @f3
46 // CHECK-NOT: return.sloc
47 // CHECK-NOT: call{{.*}}ubsan
51 // Check for a valid "return" source location, even when there is no return
52 // statement, to avoid accidentally calling the runtime.
54 // CHECK-LABEL: define{{.*}} nonnull i32* @f4
55 __attribute__((returns_nonnull)) int *f4(void) {
56 // CHECK: store i8* null, i8** [[SLOC_PTR:%.*]]
57 // CHECK: [[SLOC:%.*]] = load {{.*}} [[SLOC_PTR]]
58 // CHECK: [[SLOC_NONNULL:%.*]] = icmp ne i8* [[SLOC]], null
59 // CHECK: br i1 [[SLOC_NONNULL]], label %nullcheck