1 // RUN: %clangxx_asan -fsanitize-coverage=func,trace-pc-guard %s -o %t
2 // RUN: rm -rf %t-dir
3 // RUN: mkdir -p %t-dir && cd %t-dir
4 // RUN: %env_asan_opts=coverage=1:verbosity=1 %run %t 2>&1 | FileCheck %s
5 //
6 // UNSUPPORTED: android
7 // UNSUPPORTED: iossim
8 //
9 // Ideally a forked-subprocess should only report it's own coverage,
10 // not parent's one. But trace-pc-guard currently does nothing special for fork,
11 // and thus this test is relaxed.
12 
13 #include <stdio.h>
14 #include <string.h>
15 #include <unistd.h>
16 
17 __attribute__((noinline))
foo()18 void foo() { printf("foo\n"); }
19 
20 __attribute__((noinline))
bar()21 void bar() { printf("bar\n"); }
22 
23 __attribute__((noinline))
baz()24 void baz() { printf("baz\n"); }
25 
main(int argc,char ** argv)26 int main(int argc, char **argv) {
27   pid_t child_pid = fork();
28   if (child_pid == 0) {
29     fprintf(stderr, "Child PID: %d\n", getpid());
30     baz();
31   } else {
32     fprintf(stderr, "Parent PID: %d\n", getpid());
33     foo();
34     bar();
35   }
36   return 0;
37 }
38 
39 // CHECK-DAG: Child PID: [[ChildPID:[0-9]+]]
40 // CHECK-DAG: [[ChildPID]].sancov: {{.*}} PCs written
41 // CHECK-DAG: Parent PID: [[ParentPID:[0-9]+]]
42 // CHECK-DAG: [[ParentPID]].sancov: 3 PCs written
43