Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 1 | // RUN: %clangxx_asan -fsanitize-coverage=func -DSHARED %s -shared -o %dynamiclib -fPIC %ld_flags_rpath_so |
| 2 | // RUN: %clangxx_asan -fsanitize-coverage=func %s %ld_flags_rpath_exe -o %t |
| 3 | // RUN: export ASAN_OPTIONS=$ASAN_OPTIONS:coverage=1:verbosity=1 |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 4 | // RUN: rm -rf %T/coverage && mkdir -p %T/coverage && cd %T/coverage |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 5 | // RUN: %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-main |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 6 | // RUN: %sancov print `ls coverage.*sancov | grep -v '.so'` 2>&1 | FileCheck %s --check-prefix=CHECK-SANCOV1 |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 7 | // RUN: %run %t foo 2>&1 | FileCheck %s --check-prefix=CHECK-foo |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 8 | // RUN: %sancov print `ls coverage.*sancov | grep -v '.so'` 2>&1 | FileCheck %s --check-prefix=CHECK-SANCOV2 |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 9 | // RUN: %run %t bar 2>&1 | FileCheck %s --check-prefix=CHECK-bar |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 10 | // RUN: %sancov print `ls *coverage.*sancov | grep -v '.so'` 2>&1 | FileCheck %s --check-prefix=CHECK-SANCOV2 |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 11 | // RUN: %run %t foo bar 2>&1 | FileCheck %s --check-prefix=CHECK-foo-bar |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 12 | // RUN: %sancov print `ls *coverage.*sancov | grep -v '.so'` 2>&1 | FileCheck %s --check-prefix=CHECK-SANCOV2 |
| 13 | // RUN: %sancov print `ls *coverage.*sancov | grep '.so'` 2>&1 | FileCheck %s --check-prefix=CHECK-SANCOV1 |
| 14 | // RUN: %sancov merge `ls *coverage.*sancov | grep -v '.so'` > merged-cov |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 15 | // RUN: %sancov print merged-cov 2>&1 | FileCheck %s --check-prefix=CHECK-SANCOV2 |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 16 | // RUN: not %run %t foo bar 4 2>&1 | FileCheck %s --check-prefix=CHECK-report |
| 17 | // RUN: not %run %t foo bar 4 5 2>&1 | FileCheck %s --check-prefix=CHECK-segv |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 18 | // RUN: rm -r %T/coverage |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 19 | // |
| 20 | // https://code.google.com/p/address-sanitizer/issues/detail?id=263 |
| 21 | // XFAIL: android |
| 22 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 23 | #include <sanitizer/coverage_interface.h> |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 24 | #include <assert.h> |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #include <string.h> |
| 27 | #include <unistd.h> |
| 28 | |
| 29 | #ifdef SHARED |
| 30 | void bar() { printf("bar\n"); } |
| 31 | #else |
| 32 | __attribute__((noinline)) |
| 33 | void foo() { printf("foo\n"); } |
| 34 | extern void bar(); |
| 35 | |
| 36 | int G[4]; |
| 37 | |
| 38 | int main(int argc, char **argv) { |
| 39 | fprintf(stderr, "PID: %d\n", getpid()); |
| 40 | for (int i = 1; i < argc; i++) { |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 41 | if (!strcmp(argv[i], "foo")) { |
| 42 | uintptr_t old_coverage = __sanitizer_get_total_unique_coverage(); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 43 | foo(); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 44 | uintptr_t new_coverage = __sanitizer_get_total_unique_coverage(); |
| 45 | assert(new_coverage > old_coverage); |
| 46 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 47 | if (!strcmp(argv[i], "bar")) |
| 48 | bar(); |
| 49 | } |
| 50 | if (argc == 5) { |
| 51 | static volatile char *zero = 0; |
| 52 | *zero = 0; // SEGV if argc == 5. |
| 53 | } |
| 54 | return G[argc]; // Buffer overflow if argc >= 4. |
| 55 | } |
| 56 | #endif |
| 57 | |
| 58 | // CHECK-main: PID: [[PID:[0-9]+]] |
| 59 | // CHECK-main: [[PID]].sancov: 1 PCs written |
| 60 | // CHECK-main-NOT: .so.[[PID]] |
| 61 | // |
| 62 | // CHECK-foo: PID: [[PID:[0-9]+]] |
| 63 | // CHECK-foo: [[PID]].sancov: 2 PCs written |
| 64 | // CHECK-foo-NOT: .so.[[PID]] |
| 65 | // |
| 66 | // CHECK-bar: PID: [[PID:[0-9]+]] |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 67 | // CHECK-bar: .so.[[PID]].sancov: 1 PCs written |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 68 | // CHECK-bar: [[PID]].sancov: 1 PCs written |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 69 | // |
| 70 | // CHECK-foo-bar: PID: [[PID:[0-9]+]] |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 71 | // CHECK-foo-bar: so.[[PID]].sancov: 1 PCs written |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 72 | // CHECK-foo-bar: [[PID]].sancov: 2 PCs written |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 73 | // |
| 74 | // CHECK-report: AddressSanitizer: global-buffer-overflow |
| 75 | // CHECK-report: PCs written |
| 76 | // |
| 77 | // CHECK-segv: AddressSanitizer: SEGV |
| 78 | // CHECK-segv: PCs written |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 79 | // |
| 80 | // CHECK-SANCOV1: 1 PCs total |
| 81 | // CHECK-SANCOV2: 2 PCs total |