[HWASan] Use hwasan_memalign for aligned new.

Aligned new does not require size to be a multiple of alignment, so
memalign is the correct choice instead of aligned_alloc.

Fixes false reports for unaligned sizes.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D119161
This commit is contained in:
Vitaly Buka 2022-02-23 14:05:00 -08:00
parent 142cedc283
commit c990d56d42
2 changed files with 21 additions and 9 deletions

View File

@ -28,11 +28,11 @@
if (!nothrow && UNLIKELY(!res)) \ if (!nothrow && UNLIKELY(!res)) \
ReportOutOfMemory(size, &stack); \ ReportOutOfMemory(size, &stack); \
return res return res
# define OPERATOR_NEW_ALIGN_BODY(nothrow) \ # define OPERATOR_NEW_ALIGN_BODY(nothrow) \
GET_MALLOC_STACK_TRACE; \ GET_MALLOC_STACK_TRACE; \
void *res = hwasan_aligned_alloc(static_cast<uptr>(align), size, &stack); \ void *res = hwasan_memalign(static_cast<uptr>(align), size, &stack); \
if (!nothrow && UNLIKELY(!res)) \ if (!nothrow && UNLIKELY(!res)) \
ReportOutOfMemory(size, &stack); \ ReportOutOfMemory(size, &stack); \
return res return res
# define OPERATOR_DELETE_BODY \ # define OPERATOR_DELETE_BODY \

View File

@ -1,11 +1,13 @@
// Test basic new functionality. // Test basic new functionality.
// RUN: %clangxx_hwasan %s -o %t // RUN: %clangxx_hwasan -std=c++17 %s -o %t
// RUN: %run %t // RUN: %run %t
#include <stdlib.h> #include <cassert>
#include <assert.h> #include <cstdint>
#include <sanitizer/hwasan_interface.h> #include <cstdlib>
#include <new>
#include <sanitizer/allocator_interface.h> #include <sanitizer/allocator_interface.h>
#include <sanitizer/hwasan_interface.h>
int main() { int main() {
__hwasan_enable_allocator_tagging(); __hwasan_enable_allocator_tagging();
@ -15,4 +17,14 @@ int main() {
assert(a1 != nullptr); assert(a1 != nullptr);
assert(__sanitizer_get_allocated_size(a1) == 0); assert(__sanitizer_get_allocated_size(a1) == 0);
delete[] a1; delete[] a1;
#ifdef __cpp_aligned_new
// Aligned new/delete
constexpr auto kAlign = std::align_val_t{8};
void *a2 = ::operator new(4, kAlign);
assert(a2 != nullptr);
assert(reinterpret_cast<uintptr_t>(a2) % static_cast<uintptr_t>(kAlign) == 0);
assert(__sanitizer_get_allocated_size(a2) >= 4);
::operator delete(a2, kAlign);
#endif
} }