| Issue |
241256
|
| Summary |
[clang][CodeGen] Discarded sret result: lifetime ends before the end of the full-_expression_ (miscompile at -O3)
|
| Labels |
clang
|
| Assignees |
|
| Reporter |
jeffchen006
|
For a discarded class prvalue returned with sret, clang ends the lifetime of the temporary right after the call. In C++17 that temporary is the result object; the constructor can publish `this`, and the object must live until the end of the full-_expression_.
**Reproducer** (`repro.cpp`)
```cpp
// The result of make(42) is discarded (left operand of ','), but it is still a
// temporary object that lives until the end of the full-_expression_.
// Expected output (g++ -O0/-O2, clang -O0): ok
extern "C" int printf(const char *, ...);
struct S;
S *last; // address of the S object constructed last
struct S {
int v;
S(int x) : v(x) { last = this; }
// User-provided copy constructor: S is not trivially copyable, so
// [class.temporary]/3 does not allow an extra copy of the result object.
S(const S &o) : v(o.v) { last = this; }
};
// C++17 guaranteed copy elision: S(x) constructs the caller's result object.
S make(int x) { return S(x); }
int main() {
// [expr.context]/2: the discarded prvalue make(42) is materialized as a
// temporary; [class.temporary]/4: it is destroyed at the end of the
// full-_expression_. So last->v reads a live object with value 42.
int r = (make(42), last->v);
printf("%s\n", r == 42 ? "ok" : "BAD");
}
```
**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
ok
$ clang++ -std=c++17 -O3 repro.cpp -o ./o3
$ ./o3
BAD
```
Expected (as `-O0`, g++ -O0/-O2): `ok`. `-O3` prints `BAD`.
**Why the expected output is right**
The discarded prvalue `make(42)` is materialized ([expr.context]/2) and constructed in place (guaranteed elision), so `last` points to it. `S` has a user-provided copy ctor, so [class.temporary]/3 allows no extra copy. It lives to the end of the full-_expression_ ([class.temporary]/4), and `,` sequences the read after the call. 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 early `lifetime.end` that clang emits (the bug itself). -O0 output is right because nothing uses the marker; at -O3 DSE removes the callee's store.
**Potential Root cause**
`clang/lib/CodeGen/CGCall.cpp`, `CodeGenFunction::EmitCall`: for an indirect (sret) result with `ReturnValue.isUnused()` it makes a temporary, pushes `CallLifetimeEnd`, and pops it eagerly right after the call (`PopCleanupBlock()` for Indirect, and for noreturn). The comment says the temporary is "invisible" outside the callee. That is wrong in C++: the callee can store `this`. FE IR: `call @_Z4makei(sret %2, 42)`, `lifetime.end(%2)`, then `load @last; load v`. (From 6aab1117e859, D10042.)
**Possible Proposed fix**
In C++, when the return type is a class that is not trivially copyable (`getLangOpts().CPlusPlus && RD && !RD->isTriviallyCopyable()`), do not pop the cleanup eagerly: leave the `CallLifetimeEnd` cleanup to run at the end of the full-_expression_. Keep the eager pop for C and for trivially copyable types.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs