Skip to content

Commit c8575d1

Browse files
jhuber6rlavaee
authored andcommitted
[libc] Add and use 'cpp::launder' to guard placement new (llvm#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.
1 parent e6ace74 commit c8575d1

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

libc/src/__support/CPP/new.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ enum class align_val_t : size_t {};
2929

3030
namespace LIBC_NAMESPACE_DECL {
3131

32+
namespace cpp {
33+
template <class T> [[nodiscard]] constexpr T *launder(T *p) {
34+
static_assert(__has_builtin(__builtin_launder),
35+
"cpp::launder requires __builtin_launder");
36+
return __builtin_launder(p);
37+
}
38+
} // namespace cpp
39+
3240
class AllocChecker {
3341
bool success = false;
3442

libc/src/__support/GPU/allocator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,8 +544,8 @@ void deallocate(void *ptr) {
544544
return impl::rpc_free(ptr);
545545

546546
// The original slab pointer is the 2MiB boundary using the given pointer.
547-
Slab *slab = reinterpret_cast<Slab *>(
548-
(reinterpret_cast<uintptr_t>(ptr) & ~SLAB_ALIGNMENT));
547+
Slab *slab = cpp::launder(reinterpret_cast<Slab *>(
548+
(reinterpret_cast<uintptr_t>(ptr) & ~SLAB_ALIGNMENT)));
549549
slab->deallocate(ptr);
550550
release_slab(slab);
551551
}

0 commit comments

Comments
 (0)