blob: 99e348fdaf9d7586a16e53b97474962d4515280f [file] [log] [blame]
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -07001// 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 Nainar7c915052015-04-08 08:58:29 -07004// RUN: rm -rf %T/coverage && mkdir -p %T/coverage && cd %T/coverage
Stephen Hines2d1fdb22014-05-28 23:58:16 -07005// RUN: %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-main
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -07006// RUN: %sancov print `ls coverage.*sancov | grep -v '.so'` 2>&1 | FileCheck %s --check-prefix=CHECK-SANCOV1
Stephen Hines2d1fdb22014-05-28 23:58:16 -07007// RUN: %run %t foo 2>&1 | FileCheck %s --check-prefix=CHECK-foo
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -07008// RUN: %sancov print `ls coverage.*sancov | grep -v '.so'` 2>&1 | FileCheck %s --check-prefix=CHECK-SANCOV2
Stephen Hines2d1fdb22014-05-28 23:58:16 -07009// RUN: %run %t bar 2>&1 | FileCheck %s --check-prefix=CHECK-bar
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -070010// RUN: %sancov print `ls *coverage.*sancov | grep -v '.so'` 2>&1 | FileCheck %s --check-prefix=CHECK-SANCOV2
Stephen Hines2d1fdb22014-05-28 23:58:16 -070011// RUN: %run %t foo bar 2>&1 | FileCheck %s --check-prefix=CHECK-foo-bar
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -070012// 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 Nainar7c915052015-04-08 08:58:29 -070015// RUN: %sancov print merged-cov 2>&1 | FileCheck %s --check-prefix=CHECK-SANCOV2
Stephen Hines2d1fdb22014-05-28 23:58:16 -070016// 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 Nainar259f7062015-05-06 11:49:53 -070018// RUN: rm -r %T/coverage
Stephen Hines2d1fdb22014-05-28 23:58:16 -070019//
20// https://code.google.com/p/address-sanitizer/issues/detail?id=263
21// XFAIL: android
22
Stephen Hines86277eb2015-03-23 12:06:32 -070023#include <sanitizer/coverage_interface.h>
Stephen Hines6d186232014-11-26 17:56:19 -080024#include <assert.h>
Stephen Hines2d1fdb22014-05-28 23:58:16 -070025#include <stdio.h>
26#include <string.h>
27#include <unistd.h>
28
29#ifdef SHARED
30void bar() { printf("bar\n"); }
31#else
32__attribute__((noinline))
33void foo() { printf("foo\n"); }
34extern void bar();
35
36int G[4];
37
38int main(int argc, char **argv) {
39 fprintf(stderr, "PID: %d\n", getpid());
40 for (int i = 1; i < argc; i++) {
Stephen Hines6d186232014-11-26 17:56:19 -080041 if (!strcmp(argv[i], "foo")) {
42 uintptr_t old_coverage = __sanitizer_get_total_unique_coverage();
Stephen Hines2d1fdb22014-05-28 23:58:16 -070043 foo();
Stephen Hines6d186232014-11-26 17:56:19 -080044 uintptr_t new_coverage = __sanitizer_get_total_unique_coverage();
45 assert(new_coverage > old_coverage);
46 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -070047 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 Hines2d1fdb22014-05-28 23:58:16 -070067// CHECK-bar: .so.[[PID]].sancov: 1 PCs written
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -070068// CHECK-bar: [[PID]].sancov: 1 PCs written
Stephen Hines2d1fdb22014-05-28 23:58:16 -070069//
70// CHECK-foo-bar: PID: [[PID:[0-9]+]]
Stephen Hines2d1fdb22014-05-28 23:58:16 -070071// CHECK-foo-bar: so.[[PID]].sancov: 1 PCs written
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -070072// CHECK-foo-bar: [[PID]].sancov: 2 PCs written
Stephen Hines2d1fdb22014-05-28 23:58:16 -070073//
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 Nainar7c915052015-04-08 08:58:29 -070079//
80// CHECK-SANCOV1: 1 PCs total
81// CHECK-SANCOV2: 2 PCs total