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 char buf[100];
30 if (child_pid == 0) {
31 snprintf(s: buf, maxlen: sizeof(buf), format: "Child PID: %ld\n", (long)getpid());
32 write(fd: 2, buf: buf, n: strlen(s: buf));
33 baz();
34 } else {
35 snprintf(s: buf, maxlen: sizeof(buf), format: "Parent PID: %ld\n", (long)getpid());
36 write(fd: 2, buf: buf, n: strlen(s: buf));
37 foo();
38 bar();
39
40 // Wait for the child process(s) to finish
41 while (wait(NULL) > 0)
42 ;
43 }
44 return 0;
45}
46
47// CHECK-DAG: Child PID: [[ChildPID:[0-9]+]]
48// CHECK-DAG: [[ChildPID]].sancov: {{.*}} PCs written
49// CHECK-DAG: Parent PID: [[ParentPID:[0-9]+]]
50// CHECK-DAG: [[ParentPID]].sancov: 3 PCs written
51

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