[libc] Add and use 'cpp::launder' to guard placement new (#146123)

Summary:
In the GPU allocator we reinterpret cast from a void pointer. We know
that an actual object was constructed there according to the C++ object
model, but to make it fully standards compliant we need to 'launder' it
to forward that information to the compiler. Add this function and call
it as appropriate.
This commit is contained in:
Joseph Huber
2025-06-27 14:34:33 -05:00
committed by GitHub
parent 1eacdddc0c
commit d34214a85e
2 changed files with 10 additions and 2 deletions

View File

@@ -29,6 +29,14 @@ enum class align_val_t : size_t {};
namespace LIBC_NAMESPACE_DECL {
namespace cpp {
template <class T> [[nodiscard]] constexpr T *launder(T *p) {
static_assert(__has_builtin(__builtin_launder),
"cpp::launder requires __builtin_launder");
return __builtin_launder(p);
}
} // namespace cpp
class AllocChecker {
bool success = false;

View File

@@ -544,8 +544,8 @@ void deallocate(void *ptr) {
return impl::rpc_free(ptr);
// The original slab pointer is the 2MiB boundary using the given pointer.
Slab *slab = reinterpret_cast<Slab *>(
(reinterpret_cast<uintptr_t>(ptr) & ~SLAB_ALIGNMENT));
Slab *slab = cpp::launder(reinterpret_cast<Slab *>(
(reinterpret_cast<uintptr_t>(ptr) & ~SLAB_ALIGNMENT)));
slab->deallocate(ptr);
release_slab(slab);
}