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

[LifetimeSafety] Detect lifetime safety issues in placement new and manual destructor calls

Open SidneyCogdill opened this issue 1 year ago • 6 comments

https://godbolt.org/z/d5cPd7hsW

#include <iostream>
#include <string>

constexpr auto long_string = "Test42Test42Test42Test42Test42";

int main() {
    alignas(std::string) char storage[sizeof(std::string)];
    std::string* str = new (storage) std::string{long_string};

    std::cout << *str << '\n';

    str->~basic_string();

    std::cout << *str << '\n'; // Use after destruction
}

https://godbolt.org/z/c6zYMz1Y8

#include <string>

constexpr auto long_string = "Test42Test42Test42Test42Test42";

int main() {
    std::string a{long_string};
    a.~basic_string();
} // <- implicit ~basic_string() invocation here; causes double free

Similarly, in C++ std::construct_at and std::destroy_at can be used for in-place construction/destruction but that's probably much more complicated to detect in a generalized way.

#include <iostream>
#include <string>

constexpr auto long_string = "Test42Test42Test42Test42Test42";

int main() {
    alignas(std::string) char storage[sizeof(std::string)];
    std::string* str = std::construct_at((std::string*)storage, long_string);

    std::cout << *str << '\n';

    std::destroy_at(str);

    std::cout << *str << '\n';  // Use after destruction
}

Other than the "use after destruction" there is also the "construction before destruction" issue:

#include <iostream>
#include <string>

int main() {
    alignas(std::string) char storage[sizeof(std::string)];
    std::string* str1 = new (storage) std::string{"Old"};
    auto p1 = str1->c_str();

    std::string* str2 = new (storage) std::string{"New"}; // Construction overwriting existing object
    
    std::cout << p1 << '\n';
}

I'm not sure if it can be made to detect this class of bugs under the current framework of Clang LifetimeSafety implementation. But I'll leave the code snippet here to raise the awareness.

SidneyCogdill avatar Oct 24 '25 12:10 SidneyCogdill

Hi @usx95

Is detecting those cases within the scope of LifetimeSafety project?

SidneyCogdill avatar Oct 24 '25 12:10 SidneyCogdill

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

Author: Sidney Cogdill (SidneyCogdill)

https://godbolt.org/z/d5cPd7hsW
#include &lt;iostream&gt;
#include &lt;string&gt;

constexpr auto long_string = "Test42Test42Test42Test42Test42";

int main() {
    alignas(std::string) char storage[sizeof(std::string)];
    std::string* str = new (storage) std::string{long_string};

    std::cout &lt;&lt; *str &lt;&lt; '\n';

    str-&gt;~basic_string();

    std::cout &lt;&lt; *str &lt;&lt; '\n'; // Use after destruction
}

https://godbolt.org/z/c6zYMz1Y8

#include &lt;string&gt;

constexpr auto long_string = "Test42Test42Test42Test42Test42";

int main() {
    std::string a{long_string};
    a.~basic_string();
} // &lt;- implicit ~basic_string() invocation here; causes double free

Similarly, in C++ std::construct_at and std::destroy_at can be used for in-place construction/destruction but that's probably much more complicated to detect in a generalized way.

#include &lt;iostream&gt;
#include &lt;string&gt;

constexpr auto long_string = "Test42Test42Test42Test42Test42";

int main() {
    alignas(std::string) char storage[sizeof(std::string)];
    std::string* str = std::construct_at((std::string*)storage, long_string);

    std::cout &lt;&lt; *str &lt;&lt; '\n';

    std::destroy_at(str);

    std::cout &lt;&lt; *str &lt;&lt; '\n';  // Use after destruction
}

Other than the "use after destruction" there is also the "construction before destruction" issue:

#include &lt;iostream&gt;
#include &lt;string&gt;

int main() {
    alignas(std::string) char storage[sizeof(std::string)];
    std::string* str1 = new (storage) std::string{"Old"};
    auto p1 = str1-&gt;c_str();

    std::string* str2 = new (storage) std::string{"New"}; // Construction overwriting existing object
    
    std::cout &lt;&lt; p1 &lt;&lt; '\n';
}

I'm not sure if it can be made to detect this class of bugs under the current framework of Clang LifetimeSafety implementation. But I'll leave the code snippet here to raise the awareness.

llvmbot avatar Oct 24 '25 12:10 llvmbot

As a first step to address, I think detecting UAF of heap-allocated objects by supporting new and delete should be a good start.

  • Facts: We should introduce DestroyOriginFact to signal that the storage pointed by specific origin has been destroyed (this is very analogous to InvalidateOriginFact).
  • Fact generator: Support new and delete expressions in visitor in .
  • Checker: Similar to invalidations, report a warning if a live origin is used while holding loans associated with an destroyed origin. Possibly share code with invalidation checker.

Target examples:

  void foo() {
      std::string* str = new std::string{"abcdefgh"};
      delete str; // Emits ExpireOriginFact for pointee
      std::cout << *str << '\n'; // Warn: use after destruction
  }

This naturally handles conditional deletes as the underlying dataflow analysis merges origins:

  void foo(bool cond) {
      std::string* p1 = new std::string{"abcd"};
      std::string* p2 = new std::string{"efgh"};
      delete cond ? p1: p2; // Emits ExpireOriginFact for merged pointee origin
      std::cout << *p1 << '\n'; // Warn: use after destruction
      std::cout << *p2 << '\n'; // Warn: use after destruction
  }

usx95 avatar Apr 13 '26 08:04 usx95

I will try to work on this

NeKon69 avatar Apr 13 '26 08:04 NeKon69

I feel like I've done enough for this issue. Even though it's still not closed, but this is in much better shape than before.

The only thing that would be realistic to support here is double-free, but that is a lower priority right now. (I might come back for this later)

I did not touch std::construct_at because it does not provide a clear AST for the constructor and it is probably much harder to support this kind of stuff in general.

NeKon69 avatar May 01 '26 16:05 NeKon69

Yes. I think this is mostly done as far as we can prioritise these at this moment. For the left over items, I would suggest to file separate bugs if there is need. As noted, they would be lower priority. I will close this one for now.

Thanks @NeKon69

usx95 avatar May 01 '26 17:05 usx95