diff --git a/compiler-rt/lib/hwasan/hwasan_new_delete.cpp b/compiler-rt/lib/hwasan/hwasan_new_delete.cpp --- a/compiler-rt/lib/hwasan/hwasan_new_delete.cpp +++ b/compiler-rt/lib/hwasan/hwasan_new_delete.cpp @@ -27,16 +27,17 @@ void *res = hwasan_malloc(size, &stack);\ if (!nothrow && UNLIKELY(!res)) ReportOutOfMemory(size, &stack);\ return res -#define OPERATOR_NEW_ALIGN_BODY(nothrow) \ - GET_MALLOC_STACK_TRACE; \ - void *res = hwasan_aligned_alloc(static_cast(align), size, &stack); \ - if (!nothrow && UNLIKELY(!res)) \ - ReportOutOfMemory(size, &stack); \ - return res - -#define OPERATOR_DELETE_BODY \ - GET_MALLOC_STACK_TRACE; \ - if (ptr) hwasan_free(ptr, &stack) +# define OPERATOR_NEW_ALIGN_BODY(nothrow) \ + GET_MALLOC_STACK_TRACE; \ + void *res = hwasan_memalign(static_cast(align), size, &stack); \ + if (!nothrow && UNLIKELY(!res)) \ + ReportOutOfMemory(size, &stack); \ + return res + +# define OPERATOR_DELETE_BODY \ + GET_MALLOC_STACK_TRACE; \ + if (ptr) \ + hwasan_free(ptr, &stack) #elif defined(__ANDROID__) diff --git a/compiler-rt/test/hwasan/TestCases/new-test.cpp b/compiler-rt/test/hwasan/TestCases/new-test.cpp --- a/compiler-rt/test/hwasan/TestCases/new-test.cpp +++ b/compiler-rt/test/hwasan/TestCases/new-test.cpp @@ -1,11 +1,13 @@ // Test basic new functionality. -// RUN: %clangxx_hwasan %s -o %t +// RUN: %clangxx_hwasan -std=c++17 %s -o %t // RUN: %run %t -#include -#include -#include +#include +#include +#include +#include #include +#include int main() { __hwasan_enable_allocator_tagging(); @@ -15,4 +17,14 @@ assert(a1 != nullptr); assert(__sanitizer_get_allocated_size(a1) == 0); 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(a2) % static_cast(kAlign) == 0); + assert(__sanitizer_get_allocated_size(a2) >= 4); + ::operator delete(a2, kAlign); +#endif }