[Bug gcov-profile/101552] New: [GCOV] Wrong coverage with "for( )" statement

njuwy at smail dot nju.edu.cn gcc-bugzilla@gcc.gnu.org
Wed Jul 21 12:46:30 GMT 2021


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101552

            Bug ID: 101552
           Summary: [GCOV] Wrong coverage with "for( )" statement
           Product: gcc
           Version: 10.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: gcov-profile
          Assignee: unassigned at gcc dot gnu.org
          Reporter: njuwy at smail dot nju.edu.cn
                CC: marxin at gcc dot gnu.org
  Target Milestone: ---

$ clang -v
clang version 11.0.0
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/wangyang/llvm-project/build/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.5.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.5.0
Candidate multilib: .;@m64
Selected multilib: .;@m64

$ cat test.c
extern int strcmp(const char *, const char *);
extern char *strcpy(char *, const char *);
extern void abort(void);
extern void exit(int);

void *buf[20];

void __attribute__((noinline)) sub2(void) { __builtin_longjmp(buf, 1); }

int main() {
  char *p = (char *)__builtin_alloca(20);

  strcpy(p, "test");

  if (__builtin_setjmp(buf)) {
    if (strcmp(p, "test") != 0)
      abort();

    exit(0);
  }

  {
    int *q = (int *)__builtin_alloca(p[2] * sizeof(int));
    printf("Excuted!\n");
    int i;

    for (i = 0; i < p[2]; i++)
      q[i] = 0;

    while (1)
      sub2();
  }
}

$ clang -w -O0 -g -fcoverage-mapping -fprofile-instr-generate=test.profraw
test.c; ./a.out; llvm-profdata merge test.profraw -o test.profdata; llvm-cov
show a.out -instr-profile=test.profdata test.c > test.lcov; cat test.lcov
Excuted!
    1|       |#include<stdio.h>
    2|       |extern int strcmp(const char *, const char *);
    3|       |extern char *strcpy(char *, const char *);
    4|       |extern void abort(void);
    5|       |extern void exit(int);
    6|       |
    7|       |void *buf[20];
    8|       |
    9|      1|void __attribute__((noinline)) sub2(void) {
__builtin_longjmp(buf, 1); }
   10|       |
   11|      1|int main() {
   12|      1|  char *p = (char *)__builtin_alloca(20);
   13|      1|
   14|      1|  strcpy(p, "test");
   15|      1|
   16|      1|  if (__builtin_setjmp(buf)) {
   17|      1|    if (strcmp(p, "test") != 0)
   18|      0|      abort();
   19|      1|
   20|      1|    exit(0);
   21|      1|  }
   22|      0|
   23|      0|  {
   24|      0|    int *q = (int *)__builtin_alloca(p[2] * sizeof(int));
   25|      0|    printf("Excuted!\n");
   26|      0|    int i;
   27|      0|
   28|    115|    for (i = 0; i < p[2]; i++)
   29|    115|      q[i] = 0;
   30|      0|
   31|      1|    while (1)
   32|      1|      sub2();
   33|      0|  }
   34|      0|}



line 16 was executed twice, line 24 to 26 was executed once and line 28 was
executed 116 times.


More information about the Gcc-bugs mailing list