xref: /freebsd/contrib/llvm-project/compiler-rt/lib/scudo/standalone/wrappers_cpp.cpp (revision cb14a3fe5122c879eae1fb480ed7ce82a699ddb6)
1 //===-- wrappers_cpp.cpp ----------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "platform.h"
10 
11 // Skip this compilation unit if compiled as part of Bionic.
12 #if !SCUDO_ANDROID || !_BIONIC
13 
14 #include "allocator_config.h"
15 #include "internal_defs.h"
16 #include "platform.h"
17 #include "scudo/interface.h"
18 #include "wrappers_c.h"
19 
20 #include <stdint.h>
21 
22 namespace std {
23 struct nothrow_t {};
24 enum class align_val_t : size_t {};
25 } // namespace std
26 
27 static void reportAllocation(void *ptr, size_t size) {
28   if (SCUDO_ENABLE_HOOKS)
29     if (__scudo_allocate_hook && ptr)
30       __scudo_allocate_hook(ptr, size);
31 }
32 static void reportDeallocation(void *ptr) {
33   if (SCUDO_ENABLE_HOOKS)
34     if (__scudo_deallocate_hook)
35       __scudo_deallocate_hook(ptr);
36 }
37 
38 INTERFACE WEAK void *operator new(size_t size) {
39   void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::New);
40   reportAllocation(Ptr, size);
41   return Ptr;
42 }
43 INTERFACE WEAK void *operator new[](size_t size) {
44   void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::NewArray);
45   reportAllocation(Ptr, size);
46   return Ptr;
47 }
48 INTERFACE WEAK void *operator new(size_t size,
49                                   std::nothrow_t const &) NOEXCEPT {
50   void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::New);
51   reportAllocation(Ptr, size);
52   return Ptr;
53 }
54 INTERFACE WEAK void *operator new[](size_t size,
55                                     std::nothrow_t const &) NOEXCEPT {
56   void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::NewArray);
57   reportAllocation(Ptr, size);
58   return Ptr;
59 }
60 INTERFACE WEAK void *operator new(size_t size, std::align_val_t align) {
61   void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::New,
62                                  static_cast<scudo::uptr>(align));
63   reportAllocation(Ptr, size);
64   return Ptr;
65 }
66 INTERFACE WEAK void *operator new[](size_t size, std::align_val_t align) {
67   void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::NewArray,
68                                  static_cast<scudo::uptr>(align));
69   reportAllocation(Ptr, size);
70   return Ptr;
71 }
72 INTERFACE WEAK void *operator new(size_t size, std::align_val_t align,
73                                   std::nothrow_t const &) NOEXCEPT {
74   void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::New,
75                                  static_cast<scudo::uptr>(align));
76   reportAllocation(Ptr, size);
77   return Ptr;
78 }
79 INTERFACE WEAK void *operator new[](size_t size, std::align_val_t align,
80                                     std::nothrow_t const &) NOEXCEPT {
81   void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::NewArray,
82                                  static_cast<scudo::uptr>(align));
83   reportAllocation(Ptr, size);
84   return Ptr;
85 }
86 
87 INTERFACE WEAK void operator delete(void *ptr) NOEXCEPT {
88   reportDeallocation(ptr);
89   Allocator.deallocate(ptr, scudo::Chunk::Origin::New);
90 }
91 INTERFACE WEAK void operator delete[](void *ptr) NOEXCEPT {
92   reportDeallocation(ptr);
93   Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray);
94 }
95 INTERFACE WEAK void operator delete(void *ptr,
96                                     std::nothrow_t const &) NOEXCEPT {
97   reportDeallocation(ptr);
98   Allocator.deallocate(ptr, scudo::Chunk::Origin::New);
99 }
100 INTERFACE WEAK void operator delete[](void *ptr,
101                                       std::nothrow_t const &) NOEXCEPT {
102   reportDeallocation(ptr);
103   Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray);
104 }
105 INTERFACE WEAK void operator delete(void *ptr, size_t size) NOEXCEPT {
106   reportDeallocation(ptr);
107   Allocator.deallocate(ptr, scudo::Chunk::Origin::New, size);
108 }
109 INTERFACE WEAK void operator delete[](void *ptr, size_t size) NOEXCEPT {
110   reportDeallocation(ptr);
111   Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray, size);
112 }
113 INTERFACE WEAK void operator delete(void *ptr,
114                                     std::align_val_t align) NOEXCEPT {
115   reportDeallocation(ptr);
116   Allocator.deallocate(ptr, scudo::Chunk::Origin::New, 0,
117                        static_cast<scudo::uptr>(align));
118 }
119 INTERFACE WEAK void operator delete[](void *ptr,
120                                       std::align_val_t align) NOEXCEPT {
121   reportDeallocation(ptr);
122   Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray, 0,
123                        static_cast<scudo::uptr>(align));
124 }
125 INTERFACE WEAK void operator delete(void *ptr, std::align_val_t align,
126                                     std::nothrow_t const &) NOEXCEPT {
127   reportDeallocation(ptr);
128   Allocator.deallocate(ptr, scudo::Chunk::Origin::New, 0,
129                        static_cast<scudo::uptr>(align));
130 }
131 INTERFACE WEAK void operator delete[](void *ptr, std::align_val_t align,
132                                       std::nothrow_t const &) NOEXCEPT {
133   reportDeallocation(ptr);
134   Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray, 0,
135                        static_cast<scudo::uptr>(align));
136 }
137 INTERFACE WEAK void operator delete(void *ptr, size_t size,
138                                     std::align_val_t align) NOEXCEPT {
139   reportDeallocation(ptr);
140   Allocator.deallocate(ptr, scudo::Chunk::Origin::New, size,
141                        static_cast<scudo::uptr>(align));
142 }
143 INTERFACE WEAK void operator delete[](void *ptr, size_t size,
144                                       std::align_val_t align) NOEXCEPT {
145   reportDeallocation(ptr);
146   Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray, size,
147                        static_cast<scudo::uptr>(align));
148 }
149 
150 #endif // !SCUDO_ANDROID || !_BIONIC
151