Vedant Kumar | 60b8b69 | 2017-03-14 01:56:34 +0000 | [diff] [blame] | 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 i32* @f1 |
| 8 | __attribute__((returns_nonnull)) int *_Nonnull f1(int *_Nonnull p) { |
| 9 | // CHECK: entry: |
Vedant Kumar | 9766b30 | 2017-06-23 21:32:38 +0000 | [diff] [blame] | 10 | // CHECK-NEXT: [[SLOC_PTR:%.*]] = alloca i8* |
Vedant Kumar | 60b8b69 | 2017-03-14 01:56:34 +0000 | [diff] [blame] | 11 | // CHECK-NEXT: [[ADDR:%.*]] = alloca i32* |
Vedant Kumar | 9766b30 | 2017-06-23 21:32:38 +0000 | [diff] [blame] | 12 | // CHECK-NEXT: store i8* null, i8** [[SLOC_PTR]] |
Vedant Kumar | 60b8b69 | 2017-03-14 01:56:34 +0000 | [diff] [blame] | 13 | // CHECK-NEXT: store i32* [[P:%.*]], i32** [[ADDR]] |
Vedant Kumar | 9766b30 | 2017-06-23 21:32:38 +0000 | [diff] [blame] | 14 | // CHECK-NEXT: store {{.*}} [[SLOC_PTR]] |
Vedant Kumar | 60b8b69 | 2017-03-14 01:56:34 +0000 | [diff] [blame] | 15 | // CHECK-NEXT: [[ARG:%.*]] = load i32*, i32** [[ADDR]] |
Vedant Kumar | 9766b30 | 2017-06-23 21:32:38 +0000 | [diff] [blame] | 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 |
| 19 | // |
| 20 | // CHECK: nullcheck: |
Vedant Kumar | 60b8b69 | 2017-03-14 01:56:34 +0000 | [diff] [blame] | 21 | // CHECK-NEXT: [[ICMP:%.*]] = icmp ne i32* [[ARG]], null, !nosanitize |
| 22 | // CHECK-NEXT: br i1 [[ICMP]], label %[[CONT:.+]], label %[[HANDLE:[^,]+]] |
| 23 | // CHECK: [[HANDLE]]: |
Vedant Kumar | 9766b30 | 2017-06-23 21:32:38 +0000 | [diff] [blame] | 24 | // CHECK: call void @__ubsan_handle_nonnull_return |
Vedant Kumar | 60b8b69 | 2017-03-14 01:56:34 +0000 | [diff] [blame] | 25 | // CHECK-NEXT: unreachable, !nosanitize |
| 26 | // CHECK: [[CONT]]: |
Vedant Kumar | 9766b30 | 2017-06-23 21:32:38 +0000 | [diff] [blame] | 27 | // CHECK-NEXT: br label %no.nullcheck |
| 28 | // CHECK: no.nullcheck: |
| 29 | // CHECK-NEXT: ret i32* [[ARG]] |
Vedant Kumar | 60b8b69 | 2017-03-14 01:56:34 +0000 | [diff] [blame] | 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() { |
| 38 | // CHECK: call void @__ubsan_handle_nonnull_arg_abort |
| 39 | // CHECK-NOT: call void @__ubsan_handle_nonnull_arg_abort |
| 40 | f2((void *)0); |
| 41 | } |
Vedant Kumar | 9766b30 | 2017-06-23 21:32:38 +0000 | [diff] [blame] | 42 | |
| 43 | // If the return value isn't meant to be checked, make sure we don't check it. |
| 44 | // CHECK-LABEL: define i32* @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 i32* @f4 |
| 55 | __attribute__((returns_nonnull)) int *f4() { |
| 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 |
| 60 | // CHECK: nullcheck: |
| 61 | } |