1// RUN: %clangxx_asan -fsanitize-coverage=func,trace-pc-guard -DSHARED %s -shared -o %dynamiclib -fPIC %ld_flags_rpath_so
2// RUN: %clangxx_asan -fsanitize-coverage=func,trace-pc-guard %s %ld_flags_rpath_exe -o %t
3// RUN: rm -rf %t-dir && mkdir -p %t-dir && cd %t-dir
4// RUN: %env_asan_opts=coverage=1:verbosity=1 %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-main
5// RUN: %sancov print coverage.*sancov 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 coverage.*sancov 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 coverage.*sancov 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 coverage.*sancov 2>&1 | FileCheck %s --check-prefix=CHECK-SANCOV2
12// RUN: %sancov print libcoverage.*sancov 2>&1 | FileCheck %s --check-prefix=CHECK-SANCOV1
13// RUN: %sancov merge coverage.*sancov > 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: cd .. && rm -rf %t-dir
18//
19// https://code.google.com/p/address-sanitizer/issues/detail?id=263
20// XFAIL: android
21// UNSUPPORTED: ios
22
23#include <assert.h>
24#include <stdio.h>
25#include <string.h>
26#include <unistd.h>
27
28#ifdef SHARED
29void bar() { printf("bar\n"); }
30#else
31__attribute__((noinline))
32void foo() { printf(format: "foo\n"); }
33extern void bar();
34
35int G[4];
36
37int main(int argc, char **argv) {
38 fprintf(stderr, format: "PID: %d\n", getpid());
39 for (int i = 1; i < argc; i++) {
40 if (!strcmp(s1: argv[i], s2: "foo"))
41 foo();
42 if (!strcmp(s1: argv[i], s2: "bar"))
43 bar();
44 }
45 if (argc == 5) {
46 static volatile char *zero = 0;
47 *zero = 0; // SEGV if argc == 5.
48 }
49 return G[argc]; // Buffer overflow if argc >= 4.
50}
51#endif
52
53// CHECK-main: PID: [[PID:[0-9]+]]
54// CHECK-main: [[PID]].sancov: 1 PCs written
55// CHECK-main-NOT: .so.[[PID]]
56//
57// CHECK-foo: PID: [[PID:[0-9]+]]
58// CHECK-foo: [[PID]].sancov: 2 PCs written
59// CHECK-foo-NOT: .so.[[PID]]
60//
61// CHECK-bar: PID: [[PID:[0-9]+]]
62// CHECK-bar-DAG: .so.[[PID]].sancov: 1 PCs written
63// CHECK-bar-DAG: [[PID]].sancov: 1 PCs written
64//
65// CHECK-foo-bar: PID: [[PID:[0-9]+]]
66// CHECK-foo-bar-DAG: so.[[PID]].sancov: 1 PCs written
67// CHECK-foo-bar-DAG: [[PID]].sancov: 2 PCs written
68//
69// CHECK-report: AddressSanitizer: global-buffer-overflow
70// CHECK-report: PCs written
71//
72// CHECK-segv: AddressSanitizer: SEGV
73// CHECK-segv: PCs written
74//
75// CHECK-SANCOV1: 1 PCs total
76// CHECK-SANCOV2: 2 PCs total
77

source code of compiler-rt/test/asan/TestCases/Posix/coverage.cpp