llvm/llvm-project

[LLVM-COV] "assert" function in "else" structure has wrong coverage

Closed this issue · 1 comments

Bugzilla Link 51267
Version 11.0
OS Linux
Reporter LLVM Bugzilla Contributor

Extended Description

$ 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
#include <assert.h>
#include <stdio.h>
#include <string.h>

static inline attribute((artificial)) void *a(char *c, const char *d,
long n) {
return __builtin___memcpy_chk(c, d, n, __builtin_object_size(c, 0));
}
typedef struct {
char *data;
int len;
} sb_t;
const char __sb_slop[1];
static void inline set0(sb_t *c) {
if (c->data != __sb_slop)
c->data[0] = 0;
else

assert(c->data[0] == 0);

}
char buf[5];
sb_t l = {.data = buf, .len = 0};
void o() {
char *data = "abcd";
sb_t h = l;
set0(&h);
a(h.data, data, strlen(data));
printf("%s\n", h.data);
printf("%d\n", h.data == __sb_slop);
printf("%d\n", h.data == buf);
set0(&h);
}
int main(void) {
o();
return 0;
}

$ 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
abcd
0
1
1| |#include <assert.h>
2| |#include <stdio.h>
3| |#include <string.h>
4| |
5| |static inline attribute((artificial)) void *a(char *c, const char *d,
6| 1| long n) {
7| 1| return __builtin___memcpy_chk(c, d, n, __builtin_object_size(c, 0));
8| 1|}
9| |typedef struct {
10| | char *data;
11| | int len;
12| |} sb_t;
13| |const char __sb_slop[1];
14| 2|static void inline set0(sb_t *c) {
15| 2| if (c->data != __sb_slop)
16| 2| c->data[0] = 0;
17| 2| else
18| 2|
19| 2| assert(c->data[0] == 0);
20| 2|}
21| |char buf[5];
22| |sb_t l = {.data = buf, .len = 0};
23| 1|void o() {
24| 1| char *data = "abcd";
25| 1| sb_t h = l;
26| 1| set0(&h);
27| 1| a(h.data, data, strlen(data));
28| 1| printf("%s\n", h.data);
29| 1| printf("%d\n", h.data == __sb_slop);
30| 1| printf("%d\n", h.data == buf);
31| 1| set0(&h);
32| 1|}
33| 1|int main(void) {
34| 1| o();
35| 1| return 0;
36| 1|}

Line 17 to 19 were not executed

In 14, the coverage is correct with the same commands.

    1|       |#include <assert.h>
    2|       |#include <stdio.h>
    3|       |#include <string.h>
    4|       |
    5|       |static inline __attribute__((__artificial__)) void *a(char *c, const char *d,
    6|      1|                                                      long n) {
    7|      1|  return __builtin___memcpy_chk(c, d, n, __builtin_object_size(c, 0));
    8|      1|}
    9|       |typedef struct {
   10|       |  char *data;
   11|       |  int len;
   12|       |} sb_t;
   13|       |const char __sb_slop[1];
   14|      2|static void inline set0(sb_t *c) {
   15|      2|  if (c->data != __sb_slop)
   16|      2|    c->data[0] = 0;
   17|      0|  else
   18|       |
   19|      0|    assert(c->data[0] == 0);
   20|      2|}
   21|       |char buf[5];
   22|       |sb_t l = {.data = buf, .len = 0};
   23|      1|void o() {
   24|      1|  char *data = "abcd";
   25|      1|  sb_t h = l;
   26|      1|  set0(&h);
   27|      1|  a(h.data, data, strlen(data));
   28|      1|  printf("%s\n", h.data);
   29|      1|  printf("%d\n", h.data == __sb_slop);
   30|      1|  printf("%d\n", h.data == buf);
   31|      1|  set0(&h);
   32|      1|}
   33|      1|int main(void) {
   34|      1|  o();
   35|      1|  return 0;
   36|      1|}