Issue 237184
Summary [llvm-cov] export -format=lcov truncates branch counts to 32 bits (BRDA records)
Labels new issue
Assignees
Reporter Thanmai99
    ### Environment

* `clang` / `llvm-profdata` / `llvm-cov` **23.1.4** (official release `LLVM-23.1.4-Linux-X64`), Linux x86_64.
* Also present in 18.1.3. The code is unchanged on `main` at `0bb039df436d` (checked in the source, not re-run on main).

### Reproducer

```c
volatile unsigned long sink;
int main(void) {
  for (unsigned long i = 0; i < 5000000000UL; i++)
    if (i & 1)
      sink++;
 return 0;
}
```

```
clang -O0 -fprofile-instr-generate -fcoverage-mapping repro.c -o p
LLVM_PROFILE_FILE=p.profraw ./p          # takes ~15 s
llvm-profdata merge -o p.profdata p.profraw
llvm-cov show ./p -instr-profile="" -show-branches=count
llvm-cov export -format=lcov ./p -instr-profile="" | grep -E '^(DA|BRDA):3,'
llvm-cov export ./p -instr-profile=""     # JSON
```

### What I see

The same branch is printed with three different values:

```
llvm-cov show Branch (3:29): [True: 5.00G, False: 1]
llvm-cov export (JSON) [3, 29, 3, 45, 5000000000, 1, 0, 0, 4]
llvm-cov export -format=lcov DA:3,5000000001
                               BRDA:3,0,0,705032704      <-- 5000000000 mod 2^32
                               BRDA:3,0,1,1
```

`DA` for the same line is correct, `BRDA` is not. A branch taken exactly 2^32 times (and never the other way) is even printed as `-`, i.e. "never executed".

### Expected

`BRDA:3,0,0,5000000000`, the same value as `show` and the JSON export.

### Likely cause

`renderBranchExecutionCounts` in `llvm/tools/llvm-cov/CoverageExporterLcov.cpp` copies the 64-bit counts into 32-bit variables:

```cpp
unsigned BC1 = NextBranch->ExecutionCount;
unsigned BC2 = NextBranch->FalseExecutionCount;
```

Changing both to `uint64_t` fixes it. I have a patch with a lit test (an existing `.proftext` input with one counter raised by 2^32) and will open a PR.

### Related issues

Noticed while reproducing #131505: there one wrapped counter is printed as 18.4E by `show`, 9223372036854775807 in JSON and 4294967295 in LCOV. That issue is about the counter itself; this one is only about the LCOV exporter, and it happens with correct, large counts.

### Why I think it's worth fixing

LCOV is the format read by genhtml, Codecov, Coveralls and most CI dashboards. Counts above 2^32 are normal for long test runs and fuzzing, and the result is silently wrong, with no warning.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to