Issue 241250
Summary [clang][CodeGen] Array filler ends a DMI temporary before the end of the full-_expression_ (miscompile at -O3)
Labels clang
Assignees
Reporter jeffchen006
    `T a[1] = {}` uses an array filler. The filler is an aggregate init whose default member initializer makes a temporary `Q{7}`. Clang ends the lifetime of the temporary right after the element, before the end of the full-_expression_, and the optimizer removes the store to it.

**Reproducer** (`repro.cpp`)
```cpp
extern "C" int printf(const char *, ...);

struct Q { int v; };
const Q *addr(const Q &q) { return &q; }

struct S { const Q *p = addr(Q{7}); };  // aggregate with a default member initializer
struct T { S a[1]; };

int get(const T &t) { return t.a[0].p->v; }

/* Correct output (clang -O0, gcc -O0/-O2): 1.   clang -O3: 0.
   T{} aggregate-initializes a[0] from {} (no constructor call), so evaluating the
 default member initializer is part of the full-_expression_ of the printf call
   ([intro.execution]/5); the temporary Q{7} lives until that full-_expression_ ends
   ([class.temporary]/4), and get() reads it inside that full-_expression_.  */
int main() {
  printf("%d\n", get(T{}) == 7);
}
```

**Commands and output** (clang version 24.0.0git (https://github.com/llvm/llvm-project.git 8207e9143575), Target: x86_64-unknown-linux-gnu):
```console
$ clang++ -std=c++17 -O0 repro.cpp -o ./o0
$ ./o0
1
$ clang++ -std=c++17 -O3 repro.cpp -o ./o3
$ ./o3
0
```
Expected (as `-O0`, g++ -O0/-O2): `1`. `-O3` prints `0`.

**Why the expected output is right**
`T{}` aggregate-initializes the element from the array filler; there is no constructor call. The default member initializer is part of the full-_expression_ ([intro.execution]/5), and `Q{7}` lives to its end ([class.temporary]/4). The array-element exception in [class.temporary]/5 needs a default constructor call (there is none). UBSan and MSan at -O0 are clean.

**How the -O0/-O3 difference arises**
At -O0, ASan reports stack-use-after-scope, but only because of the early `lifetime.end` that the front end emits (the bug itself). At -O0 the stale value is still in memory, so the output is right; at -O3 SROA/DSE use the lifetime marker and drop the store.

**Possible Root cause**
`clang/lib/CodeGen/CGExprAgg.cpp`, `AggExprEmitter::EmitArrayInit`, filler loop: each remaining element is emitted inside a per-element `RunCleanupsScope`. The comment cites C++17 [class.temporary]p5, which applies only to a default constructor call. Here the filler is an `InitListExpr` whose DMI makes a temporary, so the scope pops its cleanup (`lifetime.end`) after each element, and a later read through a stored pointer reads a dead object. The loop also makes all elements share one temporary.

**Possible Proposed fix**
In `EmitArrayInit`, use the per-element `RunCleanupsScope` + loop only when the filler is a `CXXConstructExpr` (default ctor) or makes no temporaries. Otherwise emit the remaining elements unrolled, like explicit initializers, without an inner cleanup scope, so the temporaries are destroyed at the end of the enclosing full-_expression_.

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

Reply via email to