| 1 |
// REQUIRES: asserts |
| 2 |
// RUN: %clang_cc1 -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 |
| 3 |
|
| 4 |
// If both the annotation and the attribute are present, prefer the attribute, |
| 5 |
// since it actually affects IRGen. |
| 6 |
|
| 7 |
// CHECK-LABEL: define{{.*}} nonnull ptr @f1 |
| 8 |
__attribute__((returns_nonnull)) int *_Nonnull f1(int *_Nonnull p) { |
| 9 |
// CHECK: entry: |
| 10 |
// CHECK-NEXT: [[SLOC_PTR:%.*]] = alloca ptr |
| 11 |
// CHECK-NEXT: [[ADDR:%.*]] = alloca ptr |
| 12 |
// CHECK-NEXT: store ptr null, ptr [[SLOC_PTR]] |
| 13 |
// CHECK-NEXT: store ptr [[P:%.*]], ptr [[ADDR]] |
| 14 |
// CHECK-NEXT: store {{.*}} [[SLOC_PTR]] |
| 15 |
// CHECK-NEXT: [[ARG:%.*]] = load ptr, ptr [[ADDR]] |
| 16 |
// CHECK-NEXT: [[SLOC:%.*]] = load {{.*}} [[SLOC_PTR]] |
| 17 |
// CHECK-NEXT: [[SLOC_NONNULL:%.*]] = icmp ne ptr [[SLOC]], null |
| 18 |
// CHECK-NEXT: br i1 [[SLOC_NONNULL]], label %nullcheck |
| 19 |
// |
| 20 |
// CHECK: nullcheck: |
| 21 |
// CHECK-NEXT: [[ICMP:%.*]] = icmp ne ptr [[ARG]], null, !nosanitize |
| 22 |
// CHECK-NEXT: br i1 [[ICMP]], label %[[CONT:.+]], label %[[HANDLE:[^,]+]] |
| 23 |
// CHECK: [[HANDLE]]: |
| 24 |
// CHECK: call void @__ubsan_handle_nonnull_return |
| 25 |
// CHECK-NEXT: unreachable, !nosanitize |
| 26 |
// CHECK: [[CONT]]: |
| 27 |
// CHECK-NEXT: br label %no.nullcheck |
| 28 |
// CHECK: no.nullcheck: |
| 29 |
// CHECK-NEXT: ret ptr [[ARG]] |
| 30 |
return p; |
| 31 |
} |
| 32 |
|
| 33 |
// CHECK-LABEL: define{{.*}} void @f2 |
| 34 |
void f2(int *_Nonnull __attribute__((nonnull)) p) {} |
| 35 |
|
| 36 |
// CHECK-LABEL: define{{.*}} void @call_f2 |
| 37 |
void call_f2(void) { |
| 38 |
// CHECK: call void @__ubsan_handle_nonnull_arg_abort |
| 39 |
// CHECK-NOT: call void @__ubsan_handle_nonnull_arg_abort |
| 40 |
f2((void *)0); |
| 41 |
} |
| 42 |
|
| 43 |
// If the return value isn't meant to be checked, make sure we don't check it. |
| 44 |
// CHECK-LABEL: define{{.*}} ptr @f3 |
| 45 |
int *f3(int *p) { |
| 46 |
// CHECK-NOT: return.sloc |
| 47 |
// CHECK-NOT: call{{.*}}ubsan |
| 48 |
return p; |
| 49 |
} |
| 50 |
|
| 51 |
// Check for a valid "return" source location, even when there is no return |
| 52 |
// statement, to avoid accidentally calling the runtime. |
| 53 |
|
| 54 |
// CHECK-LABEL: define{{.*}} nonnull ptr @f4 |
| 55 |
__attribute__((returns_nonnull)) int *f4(void) { |
| 56 |
// CHECK: store ptr null, ptr [[SLOC_PTR:%.*]] |
| 57 |
// CHECK: [[SLOC:%.*]] = load {{.*}} [[SLOC_PTR]] |
| 58 |
// CHECK: [[SLOC_NONNULL:%.*]] = icmp ne ptr [[SLOC]], null |
| 59 |
// CHECK: br i1 [[SLOC_NONNULL]], label %nullcheck |
| 60 |
// CHECK: nullcheck: |
| 61 |
} |