https://bugs.llvm.org/show_bug.cgi?id=41051

            Bug ID: 41051
           Summary: [LLVM-COV] Wrong coverage when using fork() to create
                    a new process
           Product: Runtime Libraries
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: libprofile library
          Assignee: unassignedb...@nondot.org
          Reporter: yangyib...@nju.edu.cn
                CC: llvm-bugs@lists.llvm.org

$ clang -v
clang version 9.0.0-svn352319-1~exp1+0~20190127174513.767~1.gbp29cf87 (trunk)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/6
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/6.5.0
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7.3.0
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6.5.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.3.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/8
Candidate multilib: .;@m64
Selected multilib: .;@m64

$ cat small.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main(int argc, char *argv[]) {
  printf("hello world (pid:%d)\n", (int) getpid());
  int rc = fork();
  if (rc < 0) { // fork failed; exit
    fprintf(stderr, "fork failed\n");
    exit(1);
  } else if (rc == 0) { // child (new process)
    printf("hello, I am child (pid:%d)\n", (int) getpid());
  } else { // parent goes down this path (main)
    printf("hello, I am parent of %d (pid:%d)\n", rc, (int) getpid());
  }
  return 0;
}

$ clang -w -O0 -g -fcoverage-mapping -fprofile-instr-generate=small.profraw
small.c; ./a.out; llvm-profdata merge small.profraw -o small.profdata; llvm-cov
show a.out -instr-profile=small.profdata small.c > small.gcov; cat small.gcov
hello world (pid:170848)
hello, I am parent of 170849 (pid:170848)
hello, I am child (pid:170849)
    1|       |#include <stdio.h>
    2|       |#include <stdlib.h>
    3|       |#include <unistd.h>
    4|       |#include <sys/wait.h>
    5|       |
    6|      2|int main(int argc, char *argv[]) {
    7|      2|  printf("hello world (pid:%d)\n", (int) getpid());
    8|      2|  int rc = fork();
    9|      2|  if (rc < 0) { // fork failed; exit
   10|      0|    fprintf(stderr, "fork failed\n");
   11|      0|    exit(1);
   12|      2|  } else if (rc == 0) { // child (new process)
   13|      1|    printf("hello, I am child (pid:%d)\n", (int) getpid());
   14|      1|  } else { // parent goes down this path (main)
   15|      1|    printf("hello, I am parent of %d (pid:%d)\n", rc, (int)
getpid());
   16|      1|  }
   17|      2|  return 0;
   18|      2|}

Line #6, #7, #8 are wrongly executed twice. Since this is fork a new process.
Thus, these three statements should only executed once.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to