1// RUN: %clangxx -std=c++1z -faligned-allocation -O0 %s -o %t && %run %t
2// RUN: %clangxx -std=c++1z -faligned-allocation -fsized-deallocation -O0 %s -o %t && %run %t
3
4// ubsan does not intercept new/delete.
5// UNSUPPORTED: ubsan
6
7// Check that all new/delete variants are defined and work with supported
8// sanitizers. Sanitizer-specific failure modes tests are supposed to go to
9// the particular sanitizer's test suites.
10
11#include <cstddef>
12
13// Define all new/delete to do not depend on the version provided by the
14// platform. The implementation is provided by the sanitizer anyway.
15
16namespace std {
17struct nothrow_t {};
18static const nothrow_t nothrow;
19enum class align_val_t : size_t {};
20} // namespace std
21
22void *operator new(size_t);
23void *operator new[](size_t);
24void *operator new(size_t, std::nothrow_t const&);
25void *operator new[](size_t, std::nothrow_t const&);
26void *operator new(size_t, std::align_val_t);
27void *operator new[](size_t, std::align_val_t);
28void *operator new(size_t, std::align_val_t, std::nothrow_t const&);
29void *operator new[](size_t, std::align_val_t, std::nothrow_t const&);
30
31void operator delete(void*) throw();
32void operator delete[](void*) throw();
33void operator delete(void*, std::nothrow_t const&);
34void operator delete[](void*, std::nothrow_t const&);
35void operator delete(void*, size_t) throw();
36void operator delete[](void*, size_t) throw();
37void operator delete(void*, std::align_val_t) throw();
38void operator delete[](void*, std::align_val_t) throw();
39void operator delete(void*, std::align_val_t, std::nothrow_t const&);
40void operator delete[](void*, std::align_val_t, std::nothrow_t const&);
41void operator delete(void*, size_t, std::align_val_t) throw();
42void operator delete[](void*, size_t, std::align_val_t) throw();
43
44
45template<typename T>
46inline T* break_optimization(T *arg) {
47 __asm__ __volatile__("" : : "r" (arg) : "memory");
48 return arg;
49}
50
51
52struct S12 { int a, b, c; };
53struct alignas(128) S12_128 { int a, b, c; };
54struct alignas(256) S12_256 { int a, b, c; };
55struct alignas(512) S1024_512 { char a[1024]; };
56struct alignas(1024) S1024_1024 { char a[1024]; };
57
58
59int main(int argc, char **argv) {
60 delete break_optimization(arg: new S12);
61 operator delete(break_optimization(arg: new S12), std::nothrow);
62 delete [] break_optimization(arg: new S12[100]);
63 operator delete[](break_optimization(arg: new S12[100]), std::nothrow);
64
65 delete break_optimization(arg: new S12_128);
66 operator delete(break_optimization(arg: new S12_128),
67 std::align_val_t(alignof(S12_128)));
68 operator delete(break_optimization(arg: new S12_128),
69 std::align_val_t(alignof(S12_128)), std::nothrow);
70 operator delete(break_optimization(arg: new S12_128), sizeof(S12_128),
71 std::align_val_t(alignof(S12_128)));
72
73 delete [] break_optimization(arg: new S12_128[100]);
74 operator delete[](break_optimization(arg: new S12_128[100]),
75 std::align_val_t(alignof(S12_128)));
76 operator delete[](break_optimization(arg: new S12_128[100]),
77 std::align_val_t(alignof(S12_128)), std::nothrow);
78 operator delete[](break_optimization(arg: new S12_128[100]), sizeof(S12_128[100]),
79 std::align_val_t(alignof(S12_128)));
80}
81

source code of compiler-rt/test/sanitizer_common/TestCases/Linux/new_delete_test.cpp