Issue 149769
Summary [LLVM-COV] The use of the global variable errno leads an incorrect coverage statistic.
Labels new issue
Assignees
Reporter 8ss-boop
    llvm version:
clang version 21.0.0git (https://github.com/llvm/llvm-project.git 872eac7af0050813062baba9662beb81093b6b55)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /root/software/llvm-releases/llvm-0704/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/11
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/12
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/12
Candidate multilib: .;@m64
Selected multilib: .;@m64

```c
#include <stdatomic.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
int main() {
 atomic_int ai = 42; 
    int i = 0;
    volatile int flag = 1;
    while (flag) {
        switch (i++) {
            case 0:
                if (atomic_fetch_add(&ai, 1) >= 45)
                    flag = 0;
 break;
            case 1:
                if (atomic_load(&ai) % 2 == 0)
                    errno = EDOM;
                else
 errno = ERANGE;
                break;
            case 2:
 if (errno != EDOM && errno != ERANGE)
                    flag = 0;
 break;
            default:
                flag = 0;
 }
    }
    printf("Final atomic value: %d\n", atomic_load(&ai));
 return atomic_load(&ai);
}
```
for the program above, we got the following coverage report from llvm-cov 

```
/*
clang -std=c2x -lm -fcoverage-mapping -fprofile-instr-generate test.c
./a.out
llvm-profdata merge default.profraw  -o default.profdata;
llvm-cov show --show-line-counts -instr-profile="" ./a.out > test.c.lcov
*/

    1| |#include <stdatomic.h>
    2|       |#include <errno.h>
    3| |#include <stdlib.h>
    4|       |#include <stdio.h>
    5|      1|int main() {
    6|      1|    atomic_int ai = 42; 
    7|      1|    int i = 0;
    8|      1|    volatile int flag = 1;
    9|      5|    while (flag) {
   10|      4|        switch (i++) {
   11|      1|            case 0:
 12|      1|                if (atomic_fetch_add(&ai, 1) >= 45)
   13| 0|                    flag = 0;
   14|      1|                break;
   15| 1|            case 1:
   16|      1|                if (atomic_load(&ai) % 2 == 0)
   17|      1|                    errno = EDOM;
   18|      1|                else
   19|      1| errno = ERANGE;
   20|      1|                break;
   21|      1| case 2:
   22|      1|                if (errno != EDOM && errno != ERANGE)
   23|      0|                    flag = 0;
   24|      1| break;
   25|      1|            default:
   26|      1| flag = 0;
   27|      4|        }
   28|      4|    }
   29|      1| printf("Final atomic value: %d\n", atomic_load(&ai));
   30|       | return atomic_load(&ai);
   31|      1|}

```

the output is:
```
Final atomic value: 43
```
Line 19 was actually executed instead of line 17.

_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to