1 // Test basic new functionality.
2 // RUN: %clangxx_hwasan -std=c++17 %s -o %t
3 // RUN: %run %t
4 
5 #include <cassert>
6 #include <cstdint>
7 #include <cstdlib>
8 #include <new>
9 #include <sanitizer/allocator_interface.h>
10 #include <sanitizer/hwasan_interface.h>
11 
main()12 int main() {
13   __hwasan_enable_allocator_tagging();
14 
15   size_t volatile n = 0;
16   char *a1 = new char[n];
17   assert(a1 != nullptr);
18   assert(__sanitizer_get_allocated_size(a1) == 0);
19   delete[] a1;
20 
21 #if defined(__cpp_aligned_new) &&                                              \
22     (!defined(__GLIBCXX__) ||                                                  \
23      (defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE >= 7))
24   // Aligned new/delete
25   constexpr auto kAlign = std::align_val_t{8};
26   void *a2 = ::operator new(4, kAlign);
27   assert(a2 != nullptr);
28   assert(reinterpret_cast<uintptr_t>(a2) % static_cast<uintptr_t>(kAlign) == 0);
29   assert(__sanitizer_get_allocated_size(a2) >= 4);
30   ::operator delete(a2, kAlign);
31 #endif
32 }
33