1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
// Test basic new functionality.
// RUN: %clangxx_hwasan -std=c++17 %s -o %t -fsized-deallocation
// RUN: %run %t
#include <cassert>
#include <cstdint>
#include <cstdlib>
#include <new>
#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();
size_t volatile n = 0;
char *a1 = new char[n];
assert(a1 != nullptr);
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))
// 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);
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
}
|