llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[LifetimeSafety] Consider flowing origins through reinterpret_cast

Open NeKon69 opened this issue 6 months ago • 5 comments

const int *bad(const int &x [[clang::lifetimebound]]) { // could not verify that the return value can be lifetime bound to 'x'
  return (const int *)reinterpret_cast<uintptr_t>(&x);  // clearly escapes
}

https://godbolt.org/z/nfqha7az6

Theoretically, we might need to tell the analyzer to assume it escapes regardless.

NeKon69 avatar May 10 '26 19:05 NeKon69

@llvm/issue-subscribers-clang-frontend

Author: NeKon69

```cpp const int *bad(const int &x [[clang::lifetimebound]]) { // could not verify that the return value can be lifetime bound to 'x' return (const int *)reinterpret_cast<uintptr_t>(&x); // clearly escapes } ```

https://godbolt.org/z/3Ghb1WjTq

Theoretically, we might need to tell the analyzer to assume it escapes regardless.

False-positive or false-negative? Check again pls.

hstk30-hw avatar May 11 '26 01:05 hstk30-hw

Looks like clang trunk on godbolt didn't get rebuilt yet with this commit (which is weird, I thought trunk versions of compilers get rebuilt every day at 0:00 UTC..). And yes, it is a false positive, sorry for all the hassle.

EDIT: clang is rebuilt now and the warnings shows up.

NeKon69 avatar May 11 '26 04:05 NeKon69

@llvm/issue-subscribers-clang-temporal-safety

Author: NeKon69

```cpp const int *bad(const int &x [[clang::lifetimebound]]) { // could not verify that the return value can be lifetime bound to 'x' return (const int *)reinterpret_cast<uintptr_t>(&x); // clearly escapes } ```

https://godbolt.org/z/nfqha7az6

Theoretically, we might need to tell the analyzer to assume it escapes regardless.

We could consider flowing the origins optimistically through reinterpret_cast if the argument has an origin!

(There is no systematic way to support this in a lifetime model and should be considered a lower priority atm).

usx95 avatar May 12 '26 07:05 usx95