Files
clang-p2996/compiler-rt/test/asan/TestCases/Posix/coverage-fork.cpp
Roy Sundahl d2dad6287c Add wait for child processe(s) to exit. (amended+clang-formatted)
It was possible for the parent process to exit before the
forked child process had finished. In some shells, this
causes the pipe to close and FileCheck misses some output
from the child. Waiting for the child process to exit before
exiting the parent, assures that all output from stdout and
stderr is combined and forwarded through the pipe to FileCheck.

rdar://95241490

Differential Revision: https://reviews.llvm.org/D128565
2022-06-27 13:09:34 -07:00

48 lines
1.2 KiB
C++

// RUN: %clangxx_asan -fsanitize-coverage=func,trace-pc-guard %s -o %t
// RUN: rm -rf %t-dir
// RUN: mkdir -p %t-dir && cd %t-dir
// RUN: %env_asan_opts=coverage=1:verbosity=1 %run %t 2>&1 | FileCheck %s
//
// UNSUPPORTED: android
// UNSUPPORTED: iossim
//
// Ideally a forked-subprocess should only report it's own coverage,
// not parent's one. But trace-pc-guard currently does nothing special for fork,
// and thus this test is relaxed.
#include <stdio.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
__attribute__((noinline))
void foo() { printf("foo\n"); }
__attribute__((noinline))
void bar() { printf("bar\n"); }
__attribute__((noinline))
void baz() { printf("baz\n"); }
int main(int argc, char **argv) {
pid_t child_pid = fork();
if (child_pid == 0) {
fprintf(stderr, "Child PID: %d\n", getpid());
baz();
} else {
fprintf(stderr, "Parent PID: %d\n", getpid());
foo();
bar();
// Wait for the child process(s) to finish
while (wait(NULL) > 0)
;
}
return 0;
}
// CHECK-DAG: Child PID: [[ChildPID:[0-9]+]]
// CHECK-DAG: [[ChildPID]].sancov: {{.*}} PCs written
// CHECK-DAG: Parent PID: [[ParentPID:[0-9]+]]
// CHECK-DAG: [[ParentPID]].sancov: 3 PCs written