Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 1 | // RUN: %clangxx -fsanitize=nonnull-attribute -fno-sanitize-recover=all %s -O3 -o %t |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 2 | // RUN: %run %t nc |
| 3 | // RUN: %run %t nm |
| 4 | // RUN: %run %t nf |
| 5 | // RUN: %run %t nv |
| 6 | // RUN: not %run %t 0c 2>&1 | FileCheck %s --check-prefix=CTOR |
| 7 | // RUN: not %run %t 0m 2>&1 | FileCheck %s --check-prefix=METHOD |
| 8 | // RUN: not %run %t 0f 2>&1 | FileCheck %s --check-prefix=FUNC |
| 9 | // RUN: not %run %t 0v 2>&1 | FileCheck %s --check-prefix=VARIADIC |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 10 | // |
| 11 | // AArch64 lacks variadic instrumentation for MSAN. |
| 12 | // REQUIRES: stable-runtime |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 13 | |
| 14 | class C { |
| 15 | int *null_; |
| 16 | int *nonnull_; |
| 17 | |
| 18 | public: |
| 19 | C(int *null, __attribute__((nonnull)) int *nonnull) |
| 20 | : null_(null), nonnull_(nonnull) {} |
| 21 | int value() { return *nonnull_; } |
| 22 | int method(int *nonnull, int *null) __attribute__((nonnull(2))) { |
| 23 | return *nonnull_ + *nonnull; |
| 24 | } |
| 25 | }; |
| 26 | |
| 27 | __attribute__((nonnull)) int func(int *nonnull) { return *nonnull; } |
| 28 | |
| 29 | #include <stdarg.h> |
| 30 | __attribute__((nonnull)) int variadic(int x, ...) { |
| 31 | va_list args; |
| 32 | va_start(args, x); |
| 33 | int *nonnull = va_arg(args, int*); |
| 34 | int res = *nonnull; |
| 35 | va_end(args); |
| 36 | return res; |
| 37 | } |
| 38 | |
| 39 | int main(int argc, char *argv[]) { |
| 40 | int local = 0; |
| 41 | int *arg = (argv[1][0] == '0') ? 0x0 : &local; |
| 42 | switch (argv[1][1]) { |
| 43 | case 'c': |
| 44 | return C(0x0, arg).value(); |
| 45 | // CTOR: {{.*}}nonnull-arg.cpp:[[@LINE-1]]:21: runtime error: null pointer passed as argument 2, which is declared to never be null |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 46 | // CTOR-NEXT: {{.*}}nonnull-arg.cpp:19:31: note: nonnull attribute specified here |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 47 | case 'm': |
| 48 | return C(0x0, &local).method(arg, 0x0); |
| 49 | // METHOD: {{.*}}nonnull-arg.cpp:[[@LINE-1]]:36: runtime error: null pointer passed as argument 1, which is declared to never be null |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 50 | // METHOD-NEXT: {{.*}}nonnull-arg.cpp:22:54: note: nonnull attribute specified here |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 51 | case 'f': |
| 52 | return func(arg); |
| 53 | // FUNC: {{.*}}nonnull-arg.cpp:[[@LINE-1]]:19: runtime error: null pointer passed as argument 1, which is declared to never be null |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 54 | // FUNC-NEXT: {{.*}}nonnull-arg.cpp:27:16: note: nonnull attribute specified here |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 55 | case 'v': |
| 56 | return variadic(42, arg); |
| 57 | // VARIADIC: {{.*}}nonnull-arg.cpp:[[@LINE-1]]:27: runtime error: null pointer passed as argument 2, which is declared to never be null |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 58 | // VARIADIC-NEXT: {{.*}}nonnull-arg.cpp:30:16: note: nonnull attribute specified here |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 59 | } |
| 60 | return 0; |
| 61 | } |