1// 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// RUN: %env_scudo_opts=allocator_may_return_null=0 not %run %t invalid 2>&1 | FileCheck %s
5
6// Tests that the C++17 aligned new/delete operators are working as expected.
7// Currently we do not check the consistency of the alignment on deallocation,
8// so this just tests that the APIs work.
9
10#include <assert.h>
11#include <stdio.h>
12#include <stdint.h>
13#include <string.h>
14
15// Define all new/delete to not depend on the version provided by the platform.
16
17namespace std {
18struct nothrow_t {};
19static const nothrow_t nothrow;
20enum class align_val_t : size_t {};
21} // namespace std
22
23void *operator new(size_t);
24void *operator new[](size_t);
25void *operator new(size_t, std::nothrow_t const&);
26void *operator new[](size_t, std::nothrow_t const&);
27void *operator new(size_t, std::align_val_t);
28void *operator new[](size_t, std::align_val_t);
29void *operator new(size_t, std::align_val_t, std::nothrow_t const&);
30void *operator new[](size_t, std::align_val_t, std::nothrow_t const&);
31
32void operator delete(void*) throw();
33void operator delete[](void*) throw();
34void operator delete(void*, std::nothrow_t const&);
35void operator delete[](void*, std::nothrow_t const&);
36void operator delete(void*, size_t) throw();
37void operator delete[](void*, size_t) throw();
38void operator delete(void*, std::align_val_t) throw();
39void operator delete[](void*, std::align_val_t) throw();
40void operator delete(void*, std::align_val_t, std::nothrow_t const&);
41void operator delete[](void*, std::align_val_t, std::nothrow_t const&);
42void operator delete(void*, size_t, std::align_val_t) throw();
43void operator delete[](void*, size_t, std::align_val_t) throw();
44
45template<typename T>
46inline T* break_optimization(T *arg) {
47 __asm__ __volatile__("" : : "r" (arg) : "memory");
48 return arg;
49}
50
51struct S12 { int a, b, c; };
52struct alignas(128) S12_128 { int a, b, c; };
53struct alignas(256) S12_256 { int a, b, c; };
54struct alignas(512) S1024_512 { char a[1024]; };
55struct alignas(1024) S1024_1024 { char a[1024]; };
56
57int main(int argc, char **argv) {
58 assert(argc == 2);
59
60 if (!strcmp(argv[1], "valid")) {
61 // Standard use case.
62 delete break_optimization(new S12);
63 delete break_optimization(new S12_128);
64 delete[] break_optimization(new S12_128[4]);
65 delete break_optimization(new S12_256);
66 delete break_optimization(new S1024_512);
67 delete[] break_optimization(new S1024_512[4]);
68 delete break_optimization(new S1024_1024);
69
70 // Call directly the aligned versions of the operators.
71 const size_t alignment = 1U << 8;
72 void *p = operator new(1, static_cast<std::align_val_t>(alignment));
73 assert((reinterpret_cast<uintptr_t>(p) & (alignment - 1)) == 0);
74 operator delete(p, static_cast<std::align_val_t>(alignment));
75 }
76 if (!strcmp(argv[1], "invalid")) {
77 // Alignment must be a power of 2.
78 const size_t alignment = (1U << 8) - 1;
79 void *p = operator new(1, static_cast<std::align_val_t>(alignment),
80 std::nothrow);
81 // CHECK: Scudo ERROR: invalid allocation alignment
82 assert(!p);
83 }
84
85 return 0;
86}
87