[compiler-rt] 22b7685 - Reland "[hwasan] Add definitions for missing operator delete functions"

Leonard Chan via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 15 15:47:15 PST 2023


Author: Leonard Chan
Date: 2023-02-15T23:47:01Z
New Revision: 22b7685401e82a845a37eed349fa1dafa58d762b

URL: https://github.com/llvm/llvm-project/commit/22b7685401e82a845a37eed349fa1dafa58d762b
DIFF: https://github.com/llvm/llvm-project/commit/22b7685401e82a845a37eed349fa1dafa58d762b.diff

LOG: Reland "[hwasan] Add definitions for missing operator delete functions"

Looks like builders were failing for the sized variants of operator
delete. These are hidden behind the __cpp_sized_deallocation macro
which can be defined with -fsized-deallocation.

Added: 
    

Modified: 
    compiler-rt/lib/hwasan/hwasan_new_delete.cpp
    compiler-rt/test/hwasan/TestCases/new-test.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/hwasan/hwasan_new_delete.cpp b/compiler-rt/lib/hwasan/hwasan_new_delete.cpp
index 495046a754f10..f31ac3a04cced 100644
--- a/compiler-rt/lib/hwasan/hwasan_new_delete.cpp
+++ b/compiler-rt/lib/hwasan/hwasan_new_delete.cpp
@@ -92,6 +92,14 @@ INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void operator delete[](
     void *ptr, std::nothrow_t const &) {
   OPERATOR_DELETE_BODY;
 }
+INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void operator delete(
+    void *ptr, size_t) NOEXCEPT {
+  OPERATOR_DELETE_BODY;
+}
+INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void operator delete[](
+    void *ptr, size_t) NOEXCEPT {
+  OPERATOR_DELETE_BODY;
+}
 
 #endif  // OPERATOR_NEW_BODY
 
@@ -134,5 +142,13 @@ INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void operator delete[](
     void *ptr, std::align_val_t, std::nothrow_t const &) NOEXCEPT {
   OPERATOR_DELETE_BODY;
 }
+INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void operator delete(
+    void *ptr, size_t, std::align_val_t, std::nothrow_t const &) NOEXCEPT {
+  OPERATOR_DELETE_BODY;
+}
+INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void operator delete[](
+    void *ptr, size_t, std::align_val_t, std::nothrow_t const &) NOEXCEPT {
+  OPERATOR_DELETE_BODY;
+}
 
 #endif  // OPERATOR_NEW_ALIGN_BODY

diff  --git a/compiler-rt/test/hwasan/TestCases/new-test.cpp b/compiler-rt/test/hwasan/TestCases/new-test.cpp
index 184c0a69529cc..5e22412aa0507 100644
--- a/compiler-rt/test/hwasan/TestCases/new-test.cpp
+++ b/compiler-rt/test/hwasan/TestCases/new-test.cpp
@@ -1,5 +1,5 @@
 // Test basic new functionality.
-// RUN: %clangxx_hwasan -std=c++17 %s -o %t
+// RUN: %clangxx_hwasan -std=c++17 %s -o %t -fsized-deallocation
 // RUN: %run %t
 
 #include <cassert>
@@ -9,6 +9,100 @@
 #include <sanitizer/allocator_interface.h>
 #include <sanitizer/hwasan_interface.h>
 
+void operator_new_delete(size_t size) {
+	void *alloc = operator new(size);
+	assert(alloc != nullptr);
+	assert(__sanitizer_get_allocated_size(alloc) == size);
+	operator delete(alloc);
+
+	alloc = operator new(size);
+	assert(alloc != nullptr);
+	assert(__sanitizer_get_allocated_size(alloc) == size);
+	operator delete(alloc, size);
+}
+
+void operator_new_delete_array(size_t size) {
+	void *alloc = operator new[](size);
+	assert(alloc != nullptr);
+	assert(__sanitizer_get_allocated_size(alloc) == size);
+	operator delete[](alloc);
+
+	alloc = operator new[](size);
+	assert(alloc != nullptr);
+	assert(__sanitizer_get_allocated_size(alloc) == size);
+	operator delete[](alloc, size);
+}
+
+void operator_new_delete(size_t size, std::align_val_t align) {
+	void *alloc = operator new(size, align);
+	assert(alloc != nullptr);
+  assert(reinterpret_cast<uintptr_t>(alloc) % static_cast<uintptr_t>(align) == 0);
+	assert(__sanitizer_get_allocated_size(alloc) >= size);
+	operator delete(alloc, align);
+
+	alloc = operator new(size, align);
+	assert(alloc != nullptr);
+  assert(reinterpret_cast<uintptr_t>(alloc) % static_cast<uintptr_t>(align) == 0);
+	assert(__sanitizer_get_allocated_size(alloc) >= size);
+	operator delete(alloc, size, align);
+}
+
+void operator_new_delete_array(size_t size, std::align_val_t align) {
+	void *alloc = operator new[](size, align);
+	assert(alloc != nullptr);
+  assert(reinterpret_cast<uintptr_t>(alloc) % static_cast<uintptr_t>(align) == 0);
+	assert(__sanitizer_get_allocated_size(alloc) >= size);
+	operator delete[](alloc, align);
+
+	alloc = operator new[](size, align);
+	assert(alloc != nullptr);
+  assert(reinterpret_cast<uintptr_t>(alloc) % static_cast<uintptr_t>(align) == 0);
+	assert(__sanitizer_get_allocated_size(alloc) >= size);
+	operator delete[](alloc, size, align);
+}
+
+void operator_new_delete(size_t size, const std::nothrow_t &tag) {
+	void *alloc = operator new(size, tag);
+	assert(alloc != nullptr);
+	assert(__sanitizer_get_allocated_size(alloc) == size);
+	operator delete(alloc, tag);
+}
+
+void operator_new_delete_array(size_t size, const std::nothrow_t &tag) {
+	void *alloc = operator new[](size, tag);
+	assert(alloc != nullptr);
+	assert(__sanitizer_get_allocated_size(alloc) == size);
+	operator delete[](alloc, tag);
+}
+
+void operator_new_delete(size_t size, std::align_val_t align, const std::nothrow_t &tag) {
+	void *alloc = operator new(size, align, tag);
+	assert(alloc != nullptr);
+  assert(reinterpret_cast<uintptr_t>(alloc) % static_cast<uintptr_t>(align) == 0);
+	assert(__sanitizer_get_allocated_size(alloc) >= size);
+	operator delete(alloc, align, tag);
+}
+
+void operator_new_delete_array(size_t size, std::align_val_t align, const std::nothrow_t &tag) {
+	void *alloc = operator new[](size, align, tag);
+	assert(alloc != nullptr);
+  assert(reinterpret_cast<uintptr_t>(alloc) % static_cast<uintptr_t>(align) == 0);
+	assert(__sanitizer_get_allocated_size(alloc) >= size);
+	operator delete[](alloc, align, tag);
+}
+
+void operator_new_delete(size_t size, void *ptr) {
+	void *alloc = operator new(size, ptr);
+	assert(alloc == ptr);
+	operator delete(alloc, ptr);
+}
+
+void operator_new_delete_array(size_t size, void *ptr) {
+	void *alloc = operator new[](size, ptr);
+	assert(alloc == ptr);
+	operator delete[](alloc, ptr);
+}
+
 int main() {
   __hwasan_enable_allocator_tagging();
 
@@ -18,6 +112,16 @@ int main() {
   assert(__sanitizer_get_allocated_size(a1) == 1);
   delete[] a1;
 
+	constexpr size_t kSize = 8;
+	operator_new_delete(kSize);
+	operator_new_delete_array(kSize);
+	operator_new_delete(kSize, std::nothrow);
+	operator_new_delete_array(kSize, std::nothrow);
+
+	char buffer[kSize];
+	operator_new_delete(kSize, buffer);
+	operator_new_delete_array(kSize, buffer);
+
 #if defined(__cpp_aligned_new) &&                                              \
     (!defined(__GLIBCXX__) ||                                                  \
      (defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE >= 7))
@@ -28,5 +132,14 @@ int main() {
   assert(reinterpret_cast<uintptr_t>(a2) % static_cast<uintptr_t>(kAlign) == 0);
   assert(__sanitizer_get_allocated_size(a2) >= 4);
   ::operator delete(a2, kAlign);
+
+	operator_new_delete(kSize, std::align_val_t{kSize});
+	operator_new_delete_array(kSize, std::align_val_t{kSize});
+	operator_new_delete(kSize, std::align_val_t{kSize * 2});
+	operator_new_delete_array(kSize, std::align_val_t{kSize * 2});
+	operator_new_delete(kSize, std::align_val_t{kSize}, std::nothrow);
+	operator_new_delete_array(kSize, std::align_val_t{kSize}, std::nothrow);
+	operator_new_delete(kSize, std::align_val_t{kSize * 2}, std::nothrow);
+	operator_new_delete_array(kSize, std::align_val_t{kSize * 2}, std::nothrow);
 #endif
 }


        


More information about the llvm-commits mailing list