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 <sys/wait.h>
16#include <unistd.h>
17
18__attribute__((noinline))
19void foo() { printf(format: "foo\n"); }
20
21__attribute__((noinline))
22void bar() { printf(format: "bar\n"); }
23
24__attribute__((noinline))
25void baz() { printf(format: "baz\n"); }
26
27int main(int argc, char **argv) {
28 pid_t child_pid = fork();
29 if (child_pid == 0) {
30 fprintf(stderr, format: "Child PID: %d\n", getpid());
31 baz();
32 } else {
33 fprintf(stderr, format: "Parent PID: %d\n", getpid());
34 foo();
35 bar();
36
37 // Wait for the child process(s) to finish
38 while (wait(NULL) > 0)
39 ;
40 }
41 return 0;
42}
43
44// CHECK-DAG: Child PID: [[ChildPID:[0-9]+]]
45// CHECK-DAG: [[ChildPID]].sancov: {{.*}} PCs written
46// CHECK-DAG: Parent PID: [[ParentPID:[0-9]+]]
47// CHECK-DAG: [[ParentPID]].sancov: 3 PCs written
48

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