• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: rm -rf %T/coverage && mkdir -p %T/coverage && cd %T/coverage
4 // RUN: %env_asan_opts=coverage=1:verbosity=1 %run %t 2>&1         | FileCheck %s --check-prefix=CHECK-main
5 // RUN: %sancov print `ls coverage.*sancov | grep -v '.so'` 2>&1 | FileCheck %s --check-prefix=CHECK-SANCOV1
6 // RUN: %env_asan_opts=coverage=1:verbosity=1 %run %t foo 2>&1     | FileCheck %s --check-prefix=CHECK-foo
7 // RUN: %sancov print `ls coverage.*sancov | grep -v '.so'` 2>&1 | FileCheck %s --check-prefix=CHECK-SANCOV2
8 // RUN: %env_asan_opts=coverage=1:verbosity=1 %run %t bar 2>&1     | FileCheck %s --check-prefix=CHECK-bar
9 // RUN: %sancov print `ls *coverage.*sancov | grep -v '.so'` 2>&1 | FileCheck %s --check-prefix=CHECK-SANCOV2
10 // RUN: %env_asan_opts=coverage=1:verbosity=1 %run %t foo bar 2>&1 | FileCheck %s --check-prefix=CHECK-foo-bar
11 // RUN: %sancov print `ls *coverage.*sancov | grep -v '.so'` 2>&1 | FileCheck %s --check-prefix=CHECK-SANCOV2
12 // RUN: %sancov print `ls *coverage.*sancov | grep '.so'` 2>&1 | FileCheck %s --check-prefix=CHECK-SANCOV1
13 // RUN: %sancov merge `ls *coverage.*sancov | grep -v '.so'` > merged-cov
14 // RUN: %sancov print merged-cov 2>&1 | FileCheck %s --check-prefix=CHECK-SANCOV2
15 // RUN: %env_asan_opts=coverage=1:verbosity=1 not %run %t foo bar 4    2>&1 | FileCheck %s --check-prefix=CHECK-report
16 // RUN: %env_asan_opts=coverage=1:verbosity=1 not %run %t foo bar 4 5  2>&1 | FileCheck %s --check-prefix=CHECK-segv
17 // RUN: rm -r %T/coverage
18 //
19 // https://code.google.com/p/address-sanitizer/issues/detail?id=263
20 // XFAIL: android
21 
22 #include <sanitizer/coverage_interface.h>
23 #include <assert.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <unistd.h>
27 
28 #ifdef SHARED
bar()29 void bar() { printf("bar\n"); }
30 #else
31 __attribute__((noinline))
foo()32 void foo() { printf("foo\n"); }
33 extern void bar();
34 
35 int G[4];
36 
main(int argc,char ** argv)37 int main(int argc, char **argv) {
38   fprintf(stderr, "PID: %d\n", getpid());
39   for (int i = 1; i < argc; i++) {
40     if (!strcmp(argv[i], "foo")) {
41       uintptr_t old_coverage = __sanitizer_get_total_unique_coverage();
42       foo();
43       uintptr_t new_coverage = __sanitizer_get_total_unique_coverage();
44       assert(new_coverage > old_coverage);
45     }
46     if (!strcmp(argv[i], "bar"))
47       bar();
48   }
49   if (argc == 5) {
50     static volatile char *zero = 0;
51     *zero = 0;  // SEGV if argc == 5.
52   }
53   return G[argc];  // Buffer overflow if argc >= 4.
54 }
55 #endif
56 
57 // CHECK-main: PID: [[PID:[0-9]+]]
58 // CHECK-main: [[PID]].sancov: 1 PCs written
59 // CHECK-main-NOT: .so.[[PID]]
60 //
61 // CHECK-foo: PID: [[PID:[0-9]+]]
62 // CHECK-foo: [[PID]].sancov: 2 PCs written
63 // CHECK-foo-NOT: .so.[[PID]]
64 //
65 // CHECK-bar: PID: [[PID:[0-9]+]]
66 // CHECK-bar: .so.[[PID]].sancov: 1 PCs written
67 // CHECK-bar: [[PID]].sancov: 1 PCs written
68 //
69 // CHECK-foo-bar: PID: [[PID:[0-9]+]]
70 // CHECK-foo-bar: so.[[PID]].sancov: 1 PCs written
71 // CHECK-foo-bar: [[PID]].sancov: 2 PCs written
72 //
73 // CHECK-report: AddressSanitizer: global-buffer-overflow
74 // CHECK-report: PCs written
75 //
76 // CHECK-segv: AddressSanitizer: SEGV
77 // CHECK-segv: PCs written
78 //
79 // CHECK-SANCOV1: 1 PCs total
80 // CHECK-SANCOV2: 2 PCs total
81