Kernel+AK: Generate compile-time error for non-sized delete

This is a much more ergonomic option than getting a
`VERIFY_NOT_REACHED()` failure at run-time. I encountered this issue
with Clang, where sized deallocation is not the default due to ABI
breakage concerns.

Note that we can't simply just not declare these functions, because the
C++ standard states:
> If this function with size parameter is defined, the program shall
> also define the version without the size parameter.
This commit is contained in:
Daniel Bertalan 2021-07-15 13:50:55 +02:00 committed by Andreas Kling
parent dd4ed4d22d
commit 3099a6bf2a
Notes: sideshowbarker 2024-07-18 08:54:57 +09:00
2 changed files with 12 additions and 2 deletions

View file

@ -59,6 +59,15 @@
#endif #endif
#define NAKED __attribute__((naked)) #define NAKED __attribute__((naked))
#ifdef DISALLOW
# undef DISALLOW
#endif
#ifdef __clang__
# define DISALLOW(message) __attribute__((diagnose_if(1, message, "error")))
#else
# define DISALLOW(message) __attribute__((error(message)))
#endif
// GCC doesn't have __has_feature but clang does // GCC doesn't have __has_feature but clang does
#ifndef __has_feature #ifndef __has_feature
# define __has_feature(...) 0 # define __has_feature(...) 0

View file

@ -64,14 +64,15 @@ inline void* operator new[](size_t, void* p) { return p; }
[[nodiscard]] void* operator new(size_t size, std::align_val_t); [[nodiscard]] void* operator new(size_t size, std::align_val_t);
[[nodiscard]] void* operator new(size_t size, std::align_val_t, const std::nothrow_t&) noexcept; [[nodiscard]] void* operator new(size_t size, std::align_val_t, const std::nothrow_t&) noexcept;
void operator delete(void* ptr) noexcept; void operator delete(void* ptr) noexcept DISALLOW("All deletes in the kernel should have a known size.");
void operator delete(void* ptr, size_t) noexcept; void operator delete(void* ptr, size_t) noexcept;
void operator delete(void* ptr, std::align_val_t) noexcept DISALLOW("All deletes in the kernel should have a known size.");
void operator delete(void* ptr, size_t, std::align_val_t) noexcept; void operator delete(void* ptr, size_t, std::align_val_t) noexcept;
[[nodiscard]] void* operator new[](size_t size); [[nodiscard]] void* operator new[](size_t size);
[[nodiscard]] void* operator new[](size_t size, const std::nothrow_t&) noexcept; [[nodiscard]] void* operator new[](size_t size, const std::nothrow_t&) noexcept;
void operator delete[](void* ptrs) noexcept; void operator delete[](void* ptrs) noexcept DISALLOW("All deletes in the kernel should have a known size.");
void operator delete[](void* ptr, size_t) noexcept; void operator delete[](void* ptr, size_t) noexcept;
[[gnu::malloc, gnu::returns_nonnull, gnu::alloc_size(1)]] void* kmalloc(size_t); [[gnu::malloc, gnu::returns_nonnull, gnu::alloc_size(1)]] void* kmalloc(size_t);