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

            Bug ID: 101556
           Summary: [GCOV] Wrong coverage that may be caused by "ASM" and
                    "volatile"
           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: ---

$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-pc-linux-gnu/10.2.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../configure -enable-checking=release -enable-languages=c,c++
-disable-multilib
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 10.2.0 (GCC) 

$ cat test.c
#include<stdlib.h>
#include<stdio.h>
volatile int cnt = 0;
__attribute__((noinline, noclone)) static int last(void) {
  return ++cnt % 5 == 0;
}

__attribute__((noinline, noclone)) static void do_it(void) {
  asm volatile("" : : "r"(&cnt) : "memory");
}

__attribute__((noinline, noclone)) static void f1(void) {
  printf("Enter function\n");
  for (;; do_it()) {
    printf("Enter loop\n");
    if (last())
      break;
  }
  do_it(); /* { dg-final { gdb-test . "cnt" "5" } } */
}

void (*volatile fnp1)(void) = f1;

int main() {
  asm volatile("" : : "r"(&fnp1) : "memory");
  fnp1();
}

$ gcc -O0 --coverage test.c;./a.out;gcov test;cat test.c.gcov
Enter loop
Enter loop
Enter loop
Enter loop
Enter loop
File 'test.c'
Lines executed:100.00% of 15
Creating 'test.c.gcov'

        -:    0:Source:test.c
        -:    0:Graph:test.gcno
        -:    0:Data:test.gcda
        -:    0:Runs:1
        -:    1:#include<stdlib.h>
        -:    2:#include<stdio.h>
        -:    3:volatile int cnt = 0;
        5:    4:__attribute__((noinline, noclone)) static int last(void) {
        5:    5:  return ++cnt % 5 == 0;
        -:    6:}
        -:    7:
        5:    8:__attribute__((noinline, noclone)) static void do_it(void) {
        5:    9:  asm volatile("" : : "r"(&cnt) : "memory");
        5:   10:}
        -:   11:
        5:   12:__attribute__((noinline, noclone)) static void f1(void) {
        4:   13:  for (;; do_it()) {
        5:   14:    printf("Enter loop\n");
        5:   15:    if (last())
        1:   16:      break;
        -:   17:  }
        1:   18:  do_it(); /* { dg-final { gdb-test . "cnt" "5" } } */
        1:   19:}
        -:   20:
        -:   21:void (*volatile fnp1)(void) = f1;
        -:   22:
        1:   23:int main() {
        1:   24:  asm volatile("" : : "r"(&fnp1) : "memory");
        1:   25:  fnp1();
        -:   26:}

Coverage of line 12 should be 1 and line 13 should be executed 5 times

Reply via email to