File: ubsan-nonnull-and-nullability.m

package info (click to toggle)
llvm-toolchain-15 1%3A15.0.6-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,554,644 kB
  • sloc: cpp: 5,922,452; ansic: 1,012,136; asm: 674,362; python: 191,568; objc: 73,855; f90: 42,327; lisp: 31,913; pascal: 11,973; javascript: 10,144; sh: 9,421; perl: 7,447; ml: 5,527; awk: 3,523; makefile: 2,520; xml: 885; cs: 573; fortran: 567
file content (61 lines) | stat: -rw-r--r-- 2,382 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// REQUIRES: asserts
// 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

// If both the annotation and the attribute are present, prefer the attribute,
// since it actually affects IRGen.

// CHECK-LABEL: define{{.*}} nonnull i32* @f1
__attribute__((returns_nonnull)) int *_Nonnull f1(int *_Nonnull p) {
  // CHECK: entry:
  // CHECK-NEXT: [[SLOC_PTR:%.*]] = alloca i8*
  // CHECK-NEXT: [[ADDR:%.*]] = alloca i32*
  // CHECK-NEXT: store i8* null, i8** [[SLOC_PTR]]
  // CHECK-NEXT: store i32* [[P:%.*]], i32** [[ADDR]]
  // CHECK-NEXT: store {{.*}} [[SLOC_PTR]]
  // CHECK-NEXT: [[ARG:%.*]] = load i32*, i32** [[ADDR]]
  // CHECK-NEXT: [[SLOC:%.*]] = load {{.*}} [[SLOC_PTR]]
  // CHECK-NEXT: [[SLOC_NONNULL:%.*]] = icmp ne i8* [[SLOC]], null
  // CHECK-NEXT: br i1 [[SLOC_NONNULL]], label %nullcheck
  // 
  // CHECK: nullcheck:
  // CHECK-NEXT: [[ICMP:%.*]] = icmp ne i32* [[ARG]], null, !nosanitize
  // CHECK-NEXT: br i1 [[ICMP]], label %[[CONT:.+]], label %[[HANDLE:[^,]+]]
  // CHECK: [[HANDLE]]:
  // CHECK:      call void @__ubsan_handle_nonnull_return
  // CHECK-NEXT:   unreachable, !nosanitize
  // CHECK: [[CONT]]:
  // CHECK-NEXT:   br label %no.nullcheck
  // CHECK: no.nullcheck:
  // CHECK-NEXT: ret i32* [[ARG]]
  return p;
}

// CHECK-LABEL: define{{.*}} void @f2
void f2(int *_Nonnull __attribute__((nonnull)) p) {}

// CHECK-LABEL: define{{.*}} void @call_f2
void call_f2(void) {
  // CHECK: call void @__ubsan_handle_nonnull_arg_abort
  // CHECK-NOT: call void @__ubsan_handle_nonnull_arg_abort
  f2((void *)0);
}

// If the return value isn't meant to be checked, make sure we don't check it.
// CHECK-LABEL: define{{.*}} i32* @f3
int *f3(int *p) {
  // CHECK-NOT: return.sloc
  // CHECK-NOT: call{{.*}}ubsan
  return p;
}

// Check for a valid "return" source location, even when there is no return
// statement, to avoid accidentally calling the runtime.

// CHECK-LABEL: define{{.*}} nonnull i32* @f4
__attribute__((returns_nonnull)) int *f4(void) {
  // CHECK: store i8* null, i8** [[SLOC_PTR:%.*]]
  // CHECK: [[SLOC:%.*]] = load {{.*}} [[SLOC_PTR]]
  // CHECK: [[SLOC_NONNULL:%.*]] = icmp ne i8* [[SLOC]], null
  // CHECK: br i1 [[SLOC_NONNULL]], label %nullcheck
  // CHECK: nullcheck:
}