Issue 243547
Summary [llvm-cov] CoverageMappingGen lacks VisitIndirectGotoStmt, leaking execution counts across computed gotos into unreached code
Labels new issue
Assignees
Reporter Durgesh-Shelke
    ### Summary

In source-based code coverage (`-fcoverage-mapping`), `CoverageMappingGen.cpp` implements `VisitGotoStmt` to terminate active counter regions across direct jumps. However, `MatchMapVisitor` / `CounterCoverageMappingBuilder` completely lacks a visitor for `IndirectGotoStmt` (`goto *ptr;`). 

When an indirect jump occurs, the visitor falls back to `VisitStmt`, failing to invoke `terminateRegion(S)`. Consequently, no zero-count gap region is emitted after the indirect jump, allowing the predecessor basic block's execution count to leak into subsequent unreached labels and statements, reporting unexecuted code as **100% EXECUTED (count 1)**.

### Minimal Standalone Reproducer (`test.c`, 7 lines)

    int test_dispatch(int op) {
        static void *targets[] = { &&op_exit, &&op_unreached };
        goto *targets[op];

 op_unreached:
        return 1;
    op_exit:
        return 0;
    }

 int main(void) {
        return test_dispatch(0);
    }

### Commands to Reproduce

    clang -std=gnu11 -O0 -g -fprofile-instr-generate -fcoverage-mapping test.c -o test
    LLVM_PROFILE_FILE="test.profraw" ./test
    llvm-profdata merge -sparse test.profraw -o test.profdata
 llvm-cov show test -instr-profile="" test.c

### Actual llvm-cov show Output

        1|      1|int test_dispatch(int op) {
        2| 1|    static void *targets[] = { &&op_exit, &&op_unreached };
        3| 1|    goto *targets[op];
        4|       |
        5| 1|op_unreached:    <-- BUG: GHOST COVERAGE! Never jumped to!
        6| 0|    return 1;
        7|      1|op_exit:
        8|      1|    return 0;
        9|      1|}
       10|       |
       11|      1|int main(void) {
       12|      1|    return test_dispatch(0);
       13|      1|}

If the unreached label and statement are written on a single line (op_unreached: return 1;):

        5|      1|op_unreached: return 1;  <-- CRITICAL: return 1 falsely certified as EXECUTED!
        6|      1|op_exit: return 0;

### Verification that Runtime Profiling is Correct (Bug is Isolated to CoverageMappingGen.cpp)

  Inspecting raw profile data using llvm-profdata show --all-functions --counts test.profdata:
    Counters:
 test_dispatch:
        Function count: 1
        Block counts: [0, 1]

Runtime instrumentation (CodeGenPGO.cpp) correctly records 0 for op_unreached and 1 for op_exit. The bug is strictly in clang/lib/CodeGen/CoverageMappingGen.cpp's AST traversal and region builder.

### Root Cause Analysis

In clang/lib/CodeGen/CoverageMappingGen.cpp:

    void VisitGotoStmt(const GotoStmt *S) { terminateRegion(S); }

Direct goto invokes terminateRegion(S), which emits a gap region with count 0 covering the dead space following the unconditional branch.

Because VisitIndirectGotoStmt is completely absent from CounterCoverageMappingBuilder, indirect gotos do not invoke terminateRegion(S). The active counter from Line 3 continues unabated across the jump and bleeds into Line 5.

GCC's gcov correctly reports Line 5 (op_unreached:) with count: 0.

### Proposed Fix

In clang/lib/CodeGen/CoverageMappingGen.cpp, implement VisitIndirectGotoStmt:

    void VisitIndirectGotoStmt(const IndirectGotoStmt *S) {
      extendRegion(S);
      if (S->getTarget())
 Visit(S->getTarget());
      terminateRegion(S);
    }

Visiting S->getTarget() ensures any _expression_ within the computed goto target (e.g. goto *(cond ? &&l1 : &&l2);) has its sub-regions visited before terminateRegion(S) terminates the region.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to