Skip to content

Commit d6ff080

Browse files
committed
[hwasan] Add definitions for missing operator delete functions
Differential Revision: https://reviews.llvm.org/D144030
1 parent 5650485 commit d6ff080

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

compiler-rt/lib/hwasan/hwasan_new_delete.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void operator delete[](
9292
void *ptr, std::nothrow_t const &) {
9393
OPERATOR_DELETE_BODY;
9494
}
95+
INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void operator delete(
96+
void *ptr, size_t) NOEXCEPT {
97+
OPERATOR_DELETE_BODY;
98+
}
99+
INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void operator delete[](
100+
void *ptr, size_t) NOEXCEPT {
101+
OPERATOR_DELETE_BODY;
102+
}
95103

96104
#endif // OPERATOR_NEW_BODY
97105

@@ -134,5 +142,13 @@ INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void operator delete[](
134142
void *ptr, std::align_val_t, std::nothrow_t const &) NOEXCEPT {
135143
OPERATOR_DELETE_BODY;
136144
}
145+
INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void operator delete(
146+
void *ptr, size_t, std::align_val_t, std::nothrow_t const &) NOEXCEPT {
147+
OPERATOR_DELETE_BODY;
148+
}
149+
INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void operator delete[](
150+
void *ptr, size_t, std::align_val_t, std::nothrow_t const &) NOEXCEPT {
151+
OPERATOR_DELETE_BODY;
152+
}
137153

138154
#endif // OPERATOR_NEW_ALIGN_BODY

compiler-rt/test/hwasan/TestCases/new-test.cpp

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,100 @@
99
#include <sanitizer/allocator_interface.h>
1010
#include <sanitizer/hwasan_interface.h>
1111

12+
void operator_new_delete(size_t size) {
13+
void *alloc = operator new(size);
14+
assert(alloc != nullptr);
15+
assert(__sanitizer_get_allocated_size(alloc) == size);
16+
operator delete(alloc);
17+
18+
alloc = operator new(size);
19+
assert(alloc != nullptr);
20+
assert(__sanitizer_get_allocated_size(alloc) == size);
21+
operator delete(alloc, size);
22+
}
23+
24+
void operator_new_delete_array(size_t size) {
25+
void *alloc = operator new[](size);
26+
assert(alloc != nullptr);
27+
assert(__sanitizer_get_allocated_size(alloc) == size);
28+
operator delete[](alloc);
29+
30+
alloc = operator new[](size);
31+
assert(alloc != nullptr);
32+
assert(__sanitizer_get_allocated_size(alloc) == size);
33+
operator delete[](alloc, size);
34+
}
35+
36+
void operator_new_delete(size_t size, std::align_val_t align) {
37+
void *alloc = operator new(size, align);
38+
assert(alloc != nullptr);
39+
assert(reinterpret_cast<uintptr_t>(alloc) % static_cast<uintptr_t>(align) == 0);
40+
assert(__sanitizer_get_allocated_size(alloc) >= size);
41+
operator delete(alloc, align);
42+
43+
alloc = operator new(size, align);
44+
assert(alloc != nullptr);
45+
assert(reinterpret_cast<uintptr_t>(alloc) % static_cast<uintptr_t>(align) == 0);
46+
assert(__sanitizer_get_allocated_size(alloc) >= size);
47+
operator delete(alloc, size, align);
48+
}
49+
50+
void operator_new_delete_array(size_t size, std::align_val_t align) {
51+
void *alloc = operator new[](size, align);
52+
assert(alloc != nullptr);
53+
assert(reinterpret_cast<uintptr_t>(alloc) % static_cast<uintptr_t>(align) == 0);
54+
assert(__sanitizer_get_allocated_size(alloc) >= size);
55+
operator delete[](alloc, align);
56+
57+
alloc = operator new[](size, align);
58+
assert(alloc != nullptr);
59+
assert(reinterpret_cast<uintptr_t>(alloc) % static_cast<uintptr_t>(align) == 0);
60+
assert(__sanitizer_get_allocated_size(alloc) >= size);
61+
operator delete[](alloc, size, align);
62+
}
63+
64+
void operator_new_delete(size_t size, const std::nothrow_t &tag) {
65+
void *alloc = operator new(size, tag);
66+
assert(alloc != nullptr);
67+
assert(__sanitizer_get_allocated_size(alloc) == size);
68+
operator delete(alloc, tag);
69+
}
70+
71+
void operator_new_delete_array(size_t size, const std::nothrow_t &tag) {
72+
void *alloc = operator new[](size, tag);
73+
assert(alloc != nullptr);
74+
assert(__sanitizer_get_allocated_size(alloc) == size);
75+
operator delete[](alloc, tag);
76+
}
77+
78+
void operator_new_delete(size_t size, std::align_val_t align, const std::nothrow_t &tag) {
79+
void *alloc = operator new(size, align, tag);
80+
assert(alloc != nullptr);
81+
assert(reinterpret_cast<uintptr_t>(alloc) % static_cast<uintptr_t>(align) == 0);
82+
assert(__sanitizer_get_allocated_size(alloc) >= size);
83+
operator delete(alloc, align, tag);
84+
}
85+
86+
void operator_new_delete_array(size_t size, std::align_val_t align, const std::nothrow_t &tag) {
87+
void *alloc = operator new[](size, align, tag);
88+
assert(alloc != nullptr);
89+
assert(reinterpret_cast<uintptr_t>(alloc) % static_cast<uintptr_t>(align) == 0);
90+
assert(__sanitizer_get_allocated_size(alloc) >= size);
91+
operator delete[](alloc, align, tag);
92+
}
93+
94+
void operator_new_delete(size_t size, void *ptr) {
95+
void *alloc = operator new(size, ptr);
96+
assert(alloc == ptr);
97+
operator delete(alloc, ptr);
98+
}
99+
100+
void operator_new_delete_array(size_t size, void *ptr) {
101+
void *alloc = operator new[](size, ptr);
102+
assert(alloc == ptr);
103+
operator delete[](alloc, ptr);
104+
}
105+
12106
int main() {
13107
__hwasan_enable_allocator_tagging();
14108

@@ -18,6 +112,16 @@ int main() {
18112
assert(__sanitizer_get_allocated_size(a1) == 1);
19113
delete[] a1;
20114

115+
constexpr size_t kSize = 8;
116+
operator_new_delete(kSize);
117+
operator_new_delete_array(kSize);
118+
operator_new_delete(kSize, std::nothrow);
119+
operator_new_delete_array(kSize, std::nothrow);
120+
121+
char buffer[kSize];
122+
operator_new_delete(kSize, buffer);
123+
operator_new_delete_array(kSize, buffer);
124+
21125
#if defined(__cpp_aligned_new) && \
22126
(!defined(__GLIBCXX__) || \
23127
(defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE >= 7))
@@ -28,5 +132,14 @@ int main() {
28132
assert(reinterpret_cast<uintptr_t>(a2) % static_cast<uintptr_t>(kAlign) == 0);
29133
assert(__sanitizer_get_allocated_size(a2) >= 4);
30134
::operator delete(a2, kAlign);
135+
136+
operator_new_delete(kSize, std::align_val_t{kSize});
137+
operator_new_delete_array(kSize, std::align_val_t{kSize});
138+
operator_new_delete(kSize, std::align_val_t{kSize * 2});
139+
operator_new_delete_array(kSize, std::align_val_t{kSize * 2});
140+
operator_new_delete(kSize, std::align_val_t{kSize}, std::nothrow);
141+
operator_new_delete_array(kSize, std::align_val_t{kSize}, std::nothrow);
142+
operator_new_delete(kSize, std::align_val_t{kSize * 2}, std::nothrow);
143+
operator_new_delete_array(kSize, std::align_val_t{kSize * 2}, std::nothrow);
31144
#endif
32145
}

0 commit comments

Comments
 (0)