Skip to content

Commit c990d56

Browse files
committed
[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
1 parent 142cedc commit c990d56

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

compiler-rt/lib/hwasan/hwasan_new_delete.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828
if (!nothrow && UNLIKELY(!res)) \
2929
ReportOutOfMemory(size, &stack); \
3030
return res
31-
# define OPERATOR_NEW_ALIGN_BODY(nothrow) \
32-
GET_MALLOC_STACK_TRACE; \
33-
void *res = hwasan_aligned_alloc(static_cast<uptr>(align), size, &stack); \
34-
if (!nothrow && UNLIKELY(!res)) \
35-
ReportOutOfMemory(size, &stack); \
31+
# define OPERATOR_NEW_ALIGN_BODY(nothrow) \
32+
GET_MALLOC_STACK_TRACE; \
33+
void *res = hwasan_memalign(static_cast<uptr>(align), size, &stack); \
34+
if (!nothrow && UNLIKELY(!res)) \
35+
ReportOutOfMemory(size, &stack); \
3636
return res
3737

3838
# define OPERATOR_DELETE_BODY \
Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
// Test basic new functionality.
2-
// RUN: %clangxx_hwasan %s -o %t
2+
// RUN: %clangxx_hwasan -std=c++17 %s -o %t
33
// RUN: %run %t
44

5-
#include <stdlib.h>
6-
#include <assert.h>
7-
#include <sanitizer/hwasan_interface.h>
5+
#include <cassert>
6+
#include <cstdint>
7+
#include <cstdlib>
8+
#include <new>
89
#include <sanitizer/allocator_interface.h>
10+
#include <sanitizer/hwasan_interface.h>
911

1012
int main() {
1113
__hwasan_enable_allocator_tagging();
@@ -15,4 +17,14 @@ int main() {
1517
assert(a1 != nullptr);
1618
assert(__sanitizer_get_allocated_size(a1) == 0);
1719
delete[] a1;
20+
21+
#ifdef __cpp_aligned_new
22+
// Aligned new/delete
23+
constexpr auto kAlign = std::align_val_t{8};
24+
void *a2 = ::operator new(4, kAlign);
25+
assert(a2 != nullptr);
26+
assert(reinterpret_cast<uintptr_t>(a2) % static_cast<uintptr_t>(kAlign) == 0);
27+
assert(__sanitizer_get_allocated_size(a2) >= 4);
28+
::operator delete(a2, kAlign);
29+
#endif
1830
}

0 commit comments

Comments
 (0)