summaryrefslogtreecommitdiff
path: root/test/ubsan
diff options
context:
space:
mode:
authorVedant Kumar <vsk@apple.com>2017-03-17 20:27:33 +0000
committerVedant Kumar <vsk@apple.com>2017-03-17 20:27:33 +0000
commitb0440701995f9d09ff87b210856b5e426ad073eb (patch)
treede3c125877a12029812681ecb92c4f95d2400304 /test/ubsan
parent4fdab5b726a155dd3da614734caa6ba69f2ee80c (diff)
[ubsan] Add e2e test for -fsanitize=nullability
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@298117 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test/ubsan')
-rw-r--r--test/ubsan/TestCases/Misc/nullability.c64
1 files changed, 64 insertions, 0 deletions
diff --git a/test/ubsan/TestCases/Misc/nullability.c b/test/ubsan/TestCases/Misc/nullability.c
new file mode 100644
index 000000000..37b8ddc8d
--- /dev/null
+++ b/test/ubsan/TestCases/Misc/nullability.c
@@ -0,0 +1,64 @@
+// RUN: %clang -w -fsanitize=nullability-arg,nullability-assign,nullability-return %s -O3 -o %t
+// RUN: %run %t foo 2>&1 | count 0
+// RUN: %run %t 2>&1 | FileCheck %s
+
+// CHECK: nullability.c:[[@LINE+2]]:51: runtime error: null pointer returned from function declared to never return null
+// CHECK-NEXT: nullability.c:[[@LINE+1]]:6: note: _Nonnull return type annotation specified here
+int *_Nonnull nonnull_retval1(int *p) { return p; }
+
+// CHECK: nullability.c:1001:19: runtime error: null pointer passed as argument 1, which is declared to never be null
+// CHECK-NEXT: nullability.c:[[@LINE+3]]:36: note: _Nonnull type annotation specified here
+// CHECK: nullability.c:1001:22: runtime error: null pointer passed as argument 2, which is declared to never be null
+// CHECK-NEXT: nullability.c:[[@LINE+1]]:56: note: _Nonnull type annotation specified here
+int *_Nonnull nonnull_retval2(int *_Nonnull arg1, int *_Nonnull arg2,
+ int *_Nullable arg3, int *arg4, int arg5, ...) {
+ return arg1;
+}
+
+// CHECK: nullability.c:1002:15: runtime error: null pointer passed as argument 1, which is declared to never be null
+// CHECK-NEXT: nullability.c:[[@LINE+1]]:23: note: _Nonnull type annotation specified here
+void nonnull_arg(int *_Nonnull p) {}
+
+void nonnull_assign1(int *p) {
+ int *_Nonnull local;
+// CHECK: nullability.c:[[@LINE+1]]:9: runtime error: _Nonnull binding to null pointer of type 'int * _Nonnull'
+ local = p;
+}
+
+void nonnull_assign2(int *p) {
+ int *_Nonnull arr[1];
+ // CHECK: nullability.c:[[@LINE+1]]:10: runtime error: _Nonnull binding to null pointer of type 'int * _Nonnull'
+ arr[0] = p;
+}
+
+struct S1 {
+ int *_Nonnull mptr;
+};
+
+void nonnull_assign3(int *p) {
+ struct S1 s;
+ // CHECK: nullability.c:[[@LINE+1]]:10: runtime error: _Nonnull binding to null pointer of type 'int * _Nonnull'
+ s.mptr = p;
+}
+
+// CHECK: nullability.c:[[@LINE+1]]:52: runtime error: _Nonnull binding to null pointer of type 'int * _Nonnull'
+void nonnull_init1(int *p) { int *_Nonnull local = p; }
+
+// CHECK: nullability.c:[[@LINE+2]]:53: runtime error: _Nonnull binding to null pointer of type 'int * _Nonnull'
+// CHECK: nullability.c:[[@LINE+1]]:56: runtime error: _Nonnull binding to null pointer of type 'int * _Nonnull'
+void nonnull_init2(int *p) { int *_Nonnull arr[] = {p, p}; }
+
+int main(int argc, char **argv) {
+ int *p = (argc > 1) ? &argc : ((int *)0);
+
+#line 1000
+ nonnull_retval1(p);
+ nonnull_retval2(p, p, p, p, 0, 0, 0, 0);
+ nonnull_arg(p);
+ nonnull_assign1(p);
+ nonnull_assign2(p);
+ nonnull_assign3(p);
+ nonnull_init1(p);
+ nonnull_init2(p);
+ return 0;
+}