Files
TheRock/compiler/amd-llvm/compiler-rt/test/hwasan/TestCases/new-test.cpp
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

146 lines
4.9 KiB
C++

// Test basic new functionality.
// RUN: %clangxx_hwasan -std=c++17 %s -o %t
// 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
}