xref: /llvm-project/compiler-rt/test/nsan/new_delete_test.cpp (revision 652707a6457eeb3927a1fe82e6b2cbc2a1fa22f5)
1 /// From sanitizer_common/TestCases/Linux/new_delete_test.cpp
2 // RUN: %clangxx_nsan -fno-sized-deallocation -O0 %s -o %t && %run %t
3 // RUN: %clangxx_nsan -fsized-deallocation -O0 %s -o %t && %run %t
4 
5 #include <cstddef>
6 
7 namespace std {
8 struct nothrow_t {};
9 static const nothrow_t nothrow;
10 enum class align_val_t : size_t {};
11 } // namespace std
12 
13 void *operator new(size_t);
14 void *operator new[](size_t);
15 void *operator new(size_t, std::nothrow_t const &);
16 void *operator new[](size_t, std::nothrow_t const &);
17 void *operator new(size_t, std::align_val_t);
18 void *operator new[](size_t, std::align_val_t);
19 void *operator new(size_t, std::align_val_t, std::nothrow_t const &);
20 void *operator new[](size_t, std::align_val_t, std::nothrow_t const &);
21 
22 void operator delete(void *) throw();
23 void operator delete[](void *) throw();
24 void operator delete(void *, std::nothrow_t const &);
25 void operator delete[](void *, std::nothrow_t const &);
26 void operator delete(void *, size_t) throw();
27 void operator delete[](void *, size_t) throw();
28 void operator delete(void *, std::align_val_t) throw();
29 void operator delete[](void *, std::align_val_t) throw();
30 void operator delete(void *, std::align_val_t, std::nothrow_t const &);
31 void operator delete[](void *, std::align_val_t, std::nothrow_t const &);
32 void operator delete(void *, size_t, std::align_val_t) throw();
33 void operator delete[](void *, size_t, std::align_val_t) throw();
34 
35 template <typename T> inline T *break_optimization(T *arg) {
36   __asm__ __volatile__("" : : "r"(arg) : "memory");
37   return arg;
38 }
39 
40 struct S12 {
41   int a, b, c;
42 };
43 struct alignas(128) S12_128 {
44   int a, b, c;
45 };
46 struct alignas(256) S12_256 {
47   int a, b, c;
48 };
49 struct alignas(512) S1024_512 {
50   char a[1024];
51 };
52 struct alignas(1024) S1024_1024 {
53   char a[1024];
54 };
55 
56 int main(int argc, char **argv) {
57   delete break_optimization(new S12);
58   operator delete(break_optimization(new S12), std::nothrow);
59   delete[] break_optimization(new S12[100]);
60   operator delete[](break_optimization(new S12[100]), std::nothrow);
61 
62   delete break_optimization(new S12_128);
63   operator delete(break_optimization(new S12_128),
64                   std::align_val_t(alignof(S12_128)));
65   operator delete(break_optimization(new S12_128),
66                   std::align_val_t(alignof(S12_128)), std::nothrow);
67   operator delete(break_optimization(new S12_128), sizeof(S12_128),
68                   std::align_val_t(alignof(S12_128)));
69 
70   delete[] break_optimization(new S12_128[100]);
71   operator delete[](break_optimization(new S12_128[100]),
72                     std::align_val_t(alignof(S12_128)));
73   operator delete[](break_optimization(new S12_128[100]),
74                     std::align_val_t(alignof(S12_128)), std::nothrow);
75   operator delete[](break_optimization(new S12_128[100]), sizeof(S12_128[100]),
76                     std::align_val_t(alignof(S12_128)));
77 }
78