| Issue |
241287
|
| Summary |
[clang][CodeGen] C lifetime markers across a backward goto (label before first cleanup) (miscompile at -O3)
|
| Labels |
clang
|
| Assignees |
|
| Reporter |
jeffchen006
|
In C, clang skips lifetime markers in a block once a label was seen, because a backward `goto` can jump over a declaration. `EmitLabel` records the label only if the block already has normal cleanups, so a label before the first cleanup is missed and `x` gets `lifetime.start/end`. DSE then removes `x = n + 1`.
**Reproducer** (`repro.c`)
```c
/* In C, x lives for the whole inner block (C17 6.2.4p6), so the backward
goto L stays inside x's lifetime and *p reads the value stored before the
jump: x == n (x was set to n + 1 before ++n). Correct output: 1 */
int printf(const char *, ...);
int *p;
int n;
int main(void) {
{
L:
if (p)
printf("%d\n", *p == n);
int x = n + 1;
p = &x;
if (++n < 2)
goto L;
}
return 0;
}
```
**Commands and output** (clang version 24.0.0git (https://github.com/llvm/llvm-project.git 8207e9143575), Target: x86_64-unknown-linux-gnu):
```console
$ clang -O0 repro.c -o o0
$ ./o0
1
$ clang -O3 repro.c -o o3
$ ./o3
0
```
Expected (as `-O0`): `1`. `-O3` prints `0`.
**Why the expected output is right**
C17 6.2.4p6: `x` (not a VLA) lives from entry into the block until the block ends; `goto L` stays in the block; `*p` reads `x` before its declaration is reached again, and `x` keeps its last value (6.2.4p2, 6.8.6.1). UBSan and MSan at -O0 are clean.
**How the -O0/-O3 difference arises**
At -O0, ASan reports stack-use-after-scope only because of the wrong `lifetime.end` that clang emits on the backward goto (the bug itself). -O0 does not use the markers, so its output is right.
**Potential Root cause**
`clang/lib/CodeGen/CGDecl.cpp` (`EmitAutoVarAlloca`) and `CGExpr.cpp` (compound literal) skip C lifetime markers only if `hasLabelBeenSeenInCurrentScope()` (`CurLexicalScope->hasLabels()`). `clang/lib/CodeGen/CGStmt.cpp`, `CodeGenFunction::EmitLabel`, adds the label to the scope only `if (EHStack.hasNormalCleanups() && CurLexicalScope)`. With a label before the first cleanup, `hasLabels()` is false. If a local with a cleanup comes before `L:`, the output is right.
**Possible Proposed fix**
Record that a label was seen independently of cleanups:
```diff
void CodeGenFunction::EmitLabel(const LabelDecl *D) {
if (EHStack.hasNormalCleanups() && CurLexicalScope)
CurLexicalScope->addLabel(D);
+ if (CurLexicalScope)
+ CurLexicalScope->setHasSeenLabel(); // new bool in LexicalScope
...
bool hasLabelBeenSeenInCurrentScope() const {
- if (CurLexicalScope) return CurLexicalScope->hasLabels();
+ if (CurLexicalScope) return CurLexicalScope->hasSeenLabel();
```
Related: fdcd18b4c90b (D27680, the "label seen" guard for this backward-goto case) and open PR #233579 ("Detect backward jumps for C lifetime markers"). This case is not covered by the D27680 guard because the label comes before the first cleanup; PR #233579 may fix it, but it is not merged and the bug still reproduces on trunk.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs