[compiler-rt] c990d56 - [HWASan] Use hwasan_memalign for aligned new.

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 23 14:22:06 PST 2022


Author: Vitaly Buka
Date: 2022-02-23T14:22:03-08:00
New Revision: c990d56d4228052fff079619770db9967157dfd4

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

LOG: [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

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 f2ae96fed2afa..495046a754f10 100644
--- a/compiler-rt/lib/hwasan/hwasan_new_delete.cpp
+++ b/compiler-rt/lib/hwasan/hwasan_new_delete.cpp
@@ -28,11 +28,11 @@
     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<uptr>(align), size, &stack); \
-    if (!nothrow && UNLIKELY(!res))                                           \
-      ReportOutOfMemory(size, &stack);                                        \
+#  define OPERATOR_NEW_ALIGN_BODY(nothrow)                               \
+    GET_MALLOC_STACK_TRACE;                                              \
+    void *res = hwasan_memalign(static_cast<uptr>(align), size, &stack); \
+    if (!nothrow && UNLIKELY(!res))                                      \
+      ReportOutOfMemory(size, &stack);                                   \
     return res
 
 #  define OPERATOR_DELETE_BODY \

diff  --git a/compiler-rt/test/hwasan/TestCases/new-test.cpp b/compiler-rt/test/hwasan/TestCases/new-test.cpp
index 3b1991e4deaa5..7e4a4ae6557f2 100644
--- 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 <stdlib.h>
-#include <assert.h>
-#include <sanitizer/hwasan_interface.h>
+#include <cassert>
+#include <cstdint>
+#include <cstdlib>
+#include <new>
 #include <sanitizer/allocator_interface.h>
+#include <sanitizer/hwasan_interface.h>
 
 int main() {
   __hwasan_enable_allocator_tagging();
@@ -15,4 +17,14 @@ int main() {
   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<uintptr_t>(a2) % static_cast<uintptr_t>(kAlign) == 0);
+  assert(__sanitizer_get_allocated_size(a2) >= 4);
+  ::operator delete(a2, kAlign);
+#endif
 }


        


More information about the llvm-commits mailing list