[llvm-bugs] [Bug 41051] New: [LLVM-COV] Wrong coverage when using fork() to create a new process

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Mar 13 05:17:31 PDT 2019


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: unassignedbugs at nondot.org
          Reporter: yangyibiao at nju.edu.cn
                CC: llvm-bugs at 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20190313/01833145/attachment.html>


More information about the llvm-bugs mailing list