Issue 243451
Summary [llvm-cov] Function-terminating statements (return) fail to terminate preceding label regions, leaking backward goto loop counter to epilogue/closing brace
Labels new issue
Assignees
Reporter ParthAher07
    ### Summary

    When a backward loop is constructed using a `goto` statement and followed by an explicit `return`, `llvm-cov` leaks the cumulative loop iteration counter to the function's closing
    brace `}` and epilogue. For a function called 1 time whose loop executes $N$ times, the function closing brace `}` is reported as having executed $N$ times (e.g. 100 or 1,000,000
    times) instead of 1 time.

    In C++, any automatic variable destructors (or GCC `__attribute__((cleanup))` in C) executed at scope exit on line `}` are also misreported as having executed $N$ times.

    ---

    ### The "Return Paradox" Minimal Pair

    The defect is demonstrated by this paradox: adding an explicit `return` statement after a backward loop causes the closing brace execution count to jump from 1 to 100:

    #### Case A: Without `return` (Closing brace correctly reports 1):
    ```c
    void test_without_return(int n) {
        int i = 0;
    loop:
        i++;
        if (i < n)
            goto loop;
        int k = 1;
    } // llvm-cov reports line 8 (closing brace) with count 1
  #### Case B: With return (Closing brace jumps to 100!):

    int test_with_return(int n) {
        int i = 0;
    loop:
        i++;
        if (i < n)
            goto loop;
        int k = 1;
        return k;
    } // llvm-cov reports line 18 (closing brace) with count 100!
  ──────
  ### Standalone Reproducer (test_goto_leak.c)

    int test_goto_leak(int n) {
        int i = 0;
    loop:
        i++;
        if (i < n)
            goto loop;
        return i;
    }

    int main(void) {
        test_goto_leak(100);
        return 0;
    }
  ──────
  ### Steps to Reproduce

    # Compile and run with profile instrumentation
    clang -O0 -g -fprofile-instr-generate -fcoverage-mapping test_goto_leak.c -o test_goto_leak
    LLVM_PROFILE_FILE="test_goto_leak.profraw" ./test_goto_leak
    llvm-profdata merge -sparse test_goto_leak.profraw -o test_goto_leak.profdata

    # View coverage
    llvm-cov show ./test_goto_leak -instr-profile="" test_goto_leak.c

    # Dump coverage mapping
    clang -O0 -fprofile-instr-generate -fcoverage-mapping -Xclang -dump-coverage-mapping -c test_goto_leak.c
  ──────
  ### Observed Output
  #### 1. llvm-cov show Output:

        1|      1|int test_goto_leak(int n) {
        2|      1|    int i = 0;
        3|    100|loop:
        4|    100|    i++;
        5|    100|    if (i < n)
        6|     99|        goto loop;
        7|      1|    return i;
        8|    100|}    <--- DEFECT: Function called 1 time, but closing brace reports 100

  (If n = 1,000,000, line 8 reports 1.00M)
  #### 2. Coverage Mapping (-Xclang -dump-coverage-mapping):

    _Z14test_goto_leaki:
      File 0, 1:27 -> 8:2 = #0
      File 0, 3:1 -> 8:2 = #1
      File 0, 5:9 -> 5:14 = #1
      Branch,File 0, 5:9 -> 5:14 = #2, (#1 - #2)
      Gap,File 0, 5:15 -> 6:9 = #2
      File 0, 6:9 -> 6:18 = #2
      Gap,File 0, 6:19 -> 7:5 = (#1 - #2)
      File 0, 7:5 -> 7:13 = (#1 - #2)

  Notice that return i; spans 7:5 -> 7:13. Immediately after column 13 (the semicolon), the active mapping reverts to File 0, 3:1 -> 8:2 = #1 (the loop: label region). Line 8 column 2 is
  the closing brace }.
  ──────
  ### Comparison with GCC gcov
  GCC 11.4.0 and GCC 15.2.0 correctly mark line 8 as non-executable (-:):

            1:    1:int test_goto_leak(int n) {
            1:    2:    int i = 0;
          100:    3:loop:
          100:    4:    i++;
          100:    5:    if (i < n)
           99:    6:        goto loop;
            1:    7:    return i;
            -:    8:}
  ──────
  ### Root Cause Analysis in CoverageMappingGen.cpp

  In clang/lib/CodeGen/CoverageMappingGen.cpp:

  1. In VisitLabelStmt(const LabelStmt *S) (line 1661):
    void VisitLabelStmt(const LabelStmt *S) {
      Counter LabelCount = getRegionCounter(S);
      SourceLocation Start = getStart(S);
      handleFileExit(Start);
      pushRegion(LabelCount, Start);
      Visit(S->getSubStmt());
    }
  pushRegion(LabelCount, Start) pushes an open-ended region with LabelCount without specifying an EndLoc and without calling popRegions.
  2. When the loop exits, VisitIfStmt pushes an exit region with count (#1 - #2) = 1 onto RegionStack (RegionStack.back()).
  3. When return i; is visited, VisitReturnStmt calls terminateRegion(S). terminateRegion terminates only RegionStack.back() by setting its EndLoc to the semicolon.
  4. Because only the top region is terminated, the preceding unended LabelStmt region (RegionStack[1] with count 100) remains alive on RegionStack.
  5. At the end of the function body, popRegions(0) sees that RegionStack[1] has no EndLoc, so it stretches it to FunctionEndLoc (the closing brace }).

  This exact pattern of terminateRegion failing to terminate preceding unpopped regions on RegionStack is acknowledged in LLVM source code at CoverageMappingGen.cpp:1674:

    // FIXME: a break in a switch should terminate regions for all preceding
    // case statements, not just the most recent one.
  ──────
  ### Suggested Fix

  When terminateRegion is called for a function-terminating statement (such as ReturnStmt), it should terminate or pop preceding unended label regions within the enclosing function before
  pushing Counter::getZero(). Alternatively, popRegions(0) should clamp any unended LabelStmt regions to the end of the statement immediately preceding the loop exit rather than
  stretching them to the function closing brace.

  Verified on Clang 16.0.6 and reproduced on Clang trunk (2026).
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to