51 lines · cpp
1// RUN: %clangxx_asan -fsanitize-coverage=func,trace-pc-guard %s -o %t2// RUN: rm -rf %t-dir3// RUN: mkdir -p %t-dir && cd %t-dir4// RUN: %env_asan_opts=coverage=1:verbosity=1 %run %t 2>&1 | FileCheck %s5//6// UNSUPPORTED: android7// UNSUPPORTED: iossim8//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("foo\n"); }20 21__attribute__((noinline))22void bar() { printf("bar\n"); }23 24__attribute__((noinline))25void baz() { printf("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(buf, sizeof(buf), "Child PID: %ld\n", (long)getpid());32 write(2, buf, strlen(buf));33 baz();34 } else {35 snprintf(buf, sizeof(buf), "Parent PID: %ld\n", (long)getpid());36 write(2, buf, strlen(buf));37 foo();38 bar();39 40 // Wait for the child process(s) to finish41 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 written49// CHECK-DAG: Parent PID: [[ParentPID:[0-9]+]]50// CHECK-DAG: [[ParentPID]].sancov: 3 PCs written51