From b0440701995f9d09ff87b210856b5e426ad073eb Mon Sep 17 00:00:00 2001 From: Vedant Kumar Date: Fri, 17 Mar 2017 20:27:33 +0000 Subject: [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 --- test/ubsan/TestCases/Misc/nullability.c | 64 +++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 test/ubsan/TestCases/Misc/nullability.c (limited to 'test/ubsan') 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; +} -- cgit v1.2.3