blob: ef37c9b0cd03b9b208d987f8d8b204093d11fbd0 [file] [log] [blame]
Kostya Kortchinsky76969ea2018-06-12 14:42:40 +00001// RUN: %clangxx_scudo -std=c++1z -faligned-allocation %s -o %t
2// RUN: %run %t valid 2>&1
3// RUN: %env_scudo_opts=allocator_may_return_null=1 %run %t invalid 2>&1
4
5// Tests that the C++17 aligned new/delete operators are working as expected.
6// Currently we do not check the consistency of the alignment on deallocation,
7// so this just tests that the APIs work.
8
9#include <assert.h>
10#include <stdio.h>
11#include <stdint.h>
12#include <string.h>
13
14// Define all new/delete to not depend on the version provided by the platform.
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
44template<typename T>
45inline T* break_optimization(T *arg) {
46 __asm__ __volatile__("" : : "r" (arg) : "memory");
47 return arg;
48}
49
50struct S12 { int a, b, c; };
51struct alignas(128) S12_128 { int a, b, c; };
52struct alignas(256) S12_256 { int a, b, c; };
53struct alignas(512) S1024_512 { char a[1024]; };
54struct alignas(1024) S1024_1024 { char a[1024]; };
55
56int main(int argc, char **argv) {
57 assert(argc == 2);
58
59 if (!strcmp(argv[1], "valid")) {
60 // Standard use case.
61 delete break_optimization(new S12);
62 delete break_optimization(new S12_128);
63 delete[] break_optimization(new S12_128[4]);
64 delete break_optimization(new S12_256);
65 delete break_optimization(new S1024_512);
66 delete[] break_optimization(new S1024_512[4]);
67 delete break_optimization(new S1024_1024);
68
69 // Call directly the aligned versions of the operators.
70 const size_t alignment = 1U << 8;
71 void *p = operator new(1, static_cast<std::align_val_t>(alignment));
72 assert((reinterpret_cast<uintptr_t>(p) & (alignment - 1)) == 0);
73 operator delete(p, static_cast<std::align_val_t>(alignment));
74 }
75 if (!strcmp(argv[1], "invalid")) {
76 // Alignment must be a power of 2.
77 const size_t alignment = (1U << 8) - 1;
78 void *p = operator new(1, static_cast<std::align_val_t>(alignment),
79 std::nothrow);
80 assert(!p);
81 }
82
83 return 0;
84}