Debian Codegraph @

vendor/Heap-Layers/wrappers/heapredirect.h

raw symbols SCIP
1 /*
2
3 Heap Layers: An Extensible Memory Allocation Infrastructure
4
5 Copyright (C) 2000-2020 by Emery Berger
6 http://www.emeryberger.com
7 [email protected]
8
9 Heap Layers is distributed under the terms of the Apache 2.0 license.
10
11 You may obtain a copy of the License at
12 http://www.apache.org/licenses/LICENSE-2.0
13
14 */
15 #pragma once
16
17 #ifndef HL_HEAPREDIRECT_H
18 #define HL_HEAPREDIRECT_Hcxx . . $ `vendor/Heap-Layers/wrappers/heapredirect.h:18:9`!
19
20 #if !defined(likelycxx . . $ `src/include/common.hpp:7:9`!)
21 #define likely(x) __builtin_expect(!!(x), 1)
22 #endif
23 #if !defined(unlikelycxx . . $ `src/include/common.hpp:8:9`!)
24 #define unlikely(x) __builtin_expect(!!(x), 0)
25 #endif
26 #if !defined ATTRIBUTE_EXPORTcxx . . $ `src/include/common.hpp:14:9`!
27 #define ATTRIBUTE_EXPORT __attribute__((visibility("default")))
28 #endif
29
30 #include "printf.h"cxx . . $ `<file>/vendor/printf/printf.h`/
31
32 namespace HLcxx . . $ HL/ {
33
34 /**
35 * Redirects the system heap calls to a custom heap, using Heap-Layers' wrappers.
36 *
37 * In order to service ::malloc through a custom heap, the custom heap's constructor
38 * and its malloc method need to run; if either one of these, directly or indirectly,
39 * need to call ::malloc, that would lead to an infinite recursion. To avoid this, we
40 * detect such recursive ::malloc calls using a thread-local boolean variable and service
41 * them from a statically allocated heap.
42 *
43 * The thread-local variable's and the static heap's initialization must be done carefully
44 * so as not to require ::malloc when no heap is available to service it from. To further
45 * complicate things, ::malloc is/can be invoked early during executable startup, before
46 * C++ constructors for global objects. On MacOS, this seems to happen before thread
47 * initialization: attempts to use thread_local or __thread lead to an abort() in _tlv_bootstrap.
48 *
49 * To work around the C++ constructor issue, we use static initializers within functions;
50 * for the thread_local/__thread issue, we use POSIX thread-local data. We prefer __atomic
51 * calls to std::atomic because STL code feels more likely to invoke malloc.
52 * We use std::recursive_mutex and std::lock_guard to avoid introducing more code and because
53 * they don't seem to require malloc, but may need to move to pthread_mutex if that should change.
54 *
55 * The problem is really that "won't use malloc" AFAIK isn't part of most of these primitives'
56 * contracts, so we stand on shaky ground.
57 *
58 * @author Juan Altmayer Pizzorno
59 */
60
61 // NOTE: The static heap functionality is currently disabled (as of 8/27/2021).
62
63 template<typename CustomHeapType, int STATIC_HEAP_SIZE>
64 class HeapWrappercxx . . $ HL/HeapWrapper# {
65 typedef LockedHeapcxx . . $ HL/LockedHeap#<stdcxx . . $ std/::mutexcxx . . $ std/mutex#, StaticBufferHeapcxx . . $ HL/StaticBufferHeap#<STATIC_HEAP_SIZE>> StaticHeapTypecxx . . $ HL/HeapWrapper#StaticHeapType#;
66
67 // Using a static bool is not thread safe, but gives us a benchmarking
68 // baseline for this implementation.
69 #define HW_USE_STATIC_BOOLcxx . . $ `vendor/Heap-Layers/wrappers/heapredirect.h:69:9`! 0
70
71 static bool* getInMallocFlagcxx . . $ HL/HeapWrapper#getInMallocFlag(a9586dbdf53ff439).() {
72 #if !HW_USE_STATIC_BOOLcxx . . $ `vendor/Heap-Layers/wrappers/heapredirect.h:69:9`!
73 // modified double-checked locking pattern (https://en.wikipedia.org/wiki/Double-checked_locking)
74 static enumcxx . . $ HL/HeapWrapper#getInMallocFlag(a9586dbdf53ff439).$anonymous_type_0# {NEEDS_KEYcxx . . $ HL/HeapWrapper#getInMallocFlag(a9586dbdf53ff439).NEEDS_KEY.=0, CREATING_KEYcxx . . $ HL/HeapWrapper#getInMallocFlag(a9586dbdf53ff439).CREATING_KEY.=1, DONEcxx . . $ HL/HeapWrapper#getInMallocFlag(a9586dbdf53ff439).DONE.=2} inMallocKeyState{NEEDS_KEYcxx . . $ HL/HeapWrapper#getInMallocFlag(a9586dbdf53ff439).NEEDS_KEY.};
75 static pthread_key_t inMallocKey;
76 static stdcxx . . $ std/::recursive_mutexcxx . . $ std/recursive_mutex# mcxx . . $ std/recursive_mutex#recursive_mutex(49f6e7a06ebc5aa8).;
77
78 auto state = __atomic_load_n(&cxx . . $ std/`operator&`(3fbb01592cd53f2a).inMallocKeyState, __ATOMIC_ACQUIREcxx . . $ `<built-in>:16:9`!);
79 if (state !=cxx . . $ HL/`operator!=`(fef5daa2f5cf3d2). DONEcxx . . $ HL/HeapWrapper#getInMallocFlag(a9586dbdf53ff439).DONE.) {
80 stdcxx . . $ std/::lock_guardcxx . . $ std/lock_guard#<decltype(m)> gcxx . . $ std/lock_guard#lock_guard(8e935d9c2e576e3a).{m};
81
82 state = __atomic_load_n(&cxx . . $ std/`operator&`(3fbb01592cd53f2a).inMallocKeyState, __ATOMIC_RELAXEDcxx . . $ `<built-in>:14:9`!);
83 if (unlikelycxx . . $ `src/include/common.hpp:8:9`!(state == CREATING_KEY)) {
84 return nullptr; // recursively invoked
85 }
86 else if (unlikelycxx . . $ `src/include/common.hpp:8:9`!(state == NEEDS_KEY)) {
87 __atomic_store_n(&cxx . . $ std/`operator&`(3fbb01592cd53f2a).inMallocKeyState, CREATING_KEYcxx . . $ HL/HeapWrapper#getInMallocFlag(a9586dbdf53ff439).CREATING_KEY., __ATOMIC_RELAXEDcxx . . $ `<built-in>:14:9`!);
88 if (pthread_key_create(&inMallocKey, 0) != 0) { // may call malloc/calloc/...
89 abortcxx . . $ abort(8df32a62017fbbf6).();
90 }
91 __atomic_store_n(&cxx . . $ std/`operator&`(3fbb01592cd53f2a).inMallocKeyState, DONEcxx . . $ HL/HeapWrapper#getInMallocFlag(a9586dbdf53ff439).DONE., __ATOMIC_RELEASEcxx . . $ `<built-in>:17:9`!);
92 }
93 }
94
95 bool* flag = (bool*)pthread_getspecific(inMallocKey); // not expected to call malloc
96 if (unlikelycxx . . $ `src/include/common.hpp:8:9`!(flag == nullptr)) {
97 stdcxx . . $ std/::lock_guardcxx . . $ std/lock_guard#<decltype(m)> gcxx . . $ std/lock_guard#lock_guard(8e935d9c2e576e3a).{m};
98
99 static bool initializing = false;
100 if (initializing) {
101 return nullptr; // recursively invoked
102 }
103
104 initializing = true;
105 flag = (bool*)getHeapcxx . . $ HL/HeapWrapper#getHeap(6a31f33bb40568).<StaticHeapTypecxx . . $ HL/HeapWrapper#StaticHeapType#>()->malloc(sizeof(bool));
106 *flag = false;
107 if (pthread_setspecific(inMallocKey, flag) != 0) { // may call malloc/calloc/...
108 abortcxx . . $ abort(8df32a62017fbbf6).();
109 }
110 initializing = false;
111 }
112
113 return flag;
114 #else
115 static bool inMalloc = false;
116 return &inMalloc;
117 #endif
118 }
119
120 public:
121 template<class HEAP>
122 static inline HEAP* getHeapcxx . . $ HL/HeapWrapper#getHeap(6a31f33bb40568).() {
123 // Allocate heap on first use and never destroy it, for malloc and such
124 // may still be used in atexit()
125 alignas(stdcxx . . $ std/::max_align_t) static char buffer[sizeof(HEAP)];
126 static HEAP* heap = new (buffer) HEAP;
127 return heap;
128 }
129
130 static inline void* malloccxx . . $ HL/HeapWrapper#malloc(bfcfa4d6b7f7ef64).(size_t sz) {
131 auto ptr = getHeapcxx . . $ HL/HeapWrapper#getHeap(6a31f33bb40568).<CustomHeapType>()->malloc(sz);
132 assertcxx . . $ `/usr/include/assert.h:50:10`!(isValid(ptr));
133 return ptr;
134 }
135
136 static inline void *memaligncxx . . $ HL/HeapWrapper#memalign(63a7bf2941bfcd7a).(size_t alignment, size_t sz) {
137 auto ptr = getHeapcxx . . $ HL/HeapWrapper#getHeap(6a31f33bb40568).<CustomHeapType>()->memalign(alignment, sz);
138 assertcxx . . $ `/usr/include/assert.h:50:10`!(isValid(ptr));
139 return ptr;
140 }
141
142 #if HL_USE_XXREALLOCcxx . . $ `<built-in>:460:9`!
143 static inline void* realloccxx . . $ HL/HeapWrapper#realloc(417ab160f6baadc6).(void * ptr, size_t sz) {
144 auto buf = getHeapcxx . . $ HL/HeapWrapper#getHeap(6a31f33bb40568).<CustomHeapType>()->realloc(ptr, sz);
145 assertcxx . . $ `/usr/include/assert.h:50:10`!(isValid(buf));
146 return buf;
147 }
148 #endif
149
150 static inline bool isValidcxx . . $ HL/HeapWrapper#isValid(db00c6a08f501612).(void * ptr) {
151 #if !defined(__GLIBC__cxx . . $ `<external>/18206667465210621810/features.h:514:9`!)
152 (void) ptr;
153 return true;
154 #else
155 // Special handling for glibc, adapted from
156 // https://sources.debian.org/src/glibc/2.31-17/malloc/malloc.c/
157 if (!ptr) {
158 return true;
159 }
160 enumcxx . . $ HL/HeapWrapper#isValid(db00c6a08f501612).$anonymous_type_1# {
161 PREV_INUSEcxx . . $ HL/HeapWrapper#isValid(db00c6a08f501612).PREV_INUSE. = 0x01,
162 IS_MMAPPEDcxx . . $ HL/HeapWrapper#isValid(db00c6a08f501612).IS_MMAPPED. = 0x02,
163 NON_MAIN_ARENAcxx . . $ HL/HeapWrapper#isValid(db00c6a08f501612).NON_MAIN_ARENA. = 0x04 };
164 struct malloc_chunkcxx . . $ HL/HeapWrapper#isValid(db00c6a08f501612).malloc_chunk# {
165 size_t mchunk_prev_sizecxx . . $ HL/HeapWrapper#isValid(db00c6a08f501612).malloc_chunk#mchunk_prev_size.;
166 size_t mchunk_sizecxx . . $ HL/HeapWrapper#isValid(db00c6a08f501612).malloc_chunk#mchunk_size.;
167 };
168 auto mchunk = (malloc_chunkcxx . . $ HL/HeapWrapper#isValid(db00c6a08f501612).malloc_chunk# *) ptr -cxx . . $ std/operator-(a6e85813984128c7). 1;
169 auto size = mchunk->mchunk_size &cxx . . $ std/`operator&`(3fbb01592cd53f2a). ~(PREV_INUSEcxx . . $ HL/HeapWrapper#isValid(db00c6a08f501612).PREV_INUSE. |cxx . . $ std/`operator|`(3fbb01592cd53f2a). IS_MMAPPEDcxx . . $ HL/HeapWrapper#isValid(db00c6a08f501612).IS_MMAPPED. |cxx . . $ std/`operator|`(3fbb01592cd53f2a). NON_MAIN_ARENAcxx . . $ HL/HeapWrapper#isValid(db00c6a08f501612).NON_MAIN_ARENA.);
170 if ((uintptr_tcxx . . $ uintptr_t#) ptr > (uintptr_tcxx . . $ uintptr_t#) -cxx . . $ std/operator-(a6e85813984128c7).size) {
171 return false;
172 }
173 return true;
174 #endif
175 }
176
177 static inline void freecxx . . $ HL/HeapWrapper#free(5d22bdacc48458e8).(void* ptr) {
178 if (ptr) {
179 getHeapcxx . . $ HL/HeapWrapper#getHeap(6a31f33bb40568).<CustomHeapType>()->free(ptr);
180 }
181 }
182
183 static inline size_t getSizecxx . . $ HL/HeapWrapper#getSize(751e5d26f8574c32).(void *ptr) {
184 if (ptr) {
185 // if (isValid(ptr)) {
186 return getHeapcxx . . $ HL/HeapWrapper#getHeap(6a31f33bb40568).<CustomHeapType>()->getSize(ptr);
187 }
188 return 0;
189 }
190
191 static inline void xxmalloc_lockcxx . . $ HL/HeapWrapper#xxmalloc_lock(49f6e7a06ebc5aa8).() {
192 getHeapcxx . . $ HL/HeapWrapper#getHeap(6a31f33bb40568).<CustomHeapType>()->lock();
193 }
194
195 static inline void xxmalloc_unlockcxx . . $ HL/HeapWrapper#xxmalloc_unlock(49f6e7a06ebc5aa8).() {
196 getHeapcxx . . $ HL/HeapWrapper#getHeap(6a31f33bb40568).<CustomHeapType>()->unlock();
197 }
198
199 // For use with sampling allocation from https://github.com/plasma-umass/scalene
200 static inline void register_malloccxx . . $ HL/HeapWrapper#register_malloc(3bdecba87b0fe3d5).(size_t sz, void * ptr) {
201 getHeapcxx . . $ HL/HeapWrapper#getHeap(6a31f33bb40568).<CustomHeapType>()->register_malloc(sz, ptr);
202 }
203
204 static inline void register_freecxx . . $ HL/HeapWrapper#register_free(3bdecba87b0fe3d5).(size_t sz, void * ptr) {
205 if (ptr) {
206 getHeapcxx . . $ HL/HeapWrapper#getHeap(6a31f33bb40568).<CustomHeapType>()->register_free(sz, ptr);
207 }
208 }
209
210 };
211
212 } // namespace
213
214 #if HL_USE_XXREALLOCcxx . . $ `<built-in>:460:9`!
215 #define HEAP_REDIRECTcxx . . $ `vendor/Heap-Layers/wrappers/heapredirect.h:215:9`!(CustomHeap, staticSize)\
216 typedef HL::HeapWrapper<CustomHeap, staticSize> TheHeapWrapper;\
217 extern "C" {\
218 ATTRIBUTE_EXPORTcxx . . $ `src/include/common.hpp:14:9`! void *xxmalloc(size_t sz) {\
219 return TheHeapWrapper::malloc(sz);\
220 }\
221 \
222 ATTRIBUTE_EXPORTcxx . . $ `src/include/common.hpp:14:9`! void xxfree(void *ptr) {\
223 TheHeapWrapper::free(ptr);\
224 }\
225 \
226 ATTRIBUTE_EXPORTcxx . . $ `src/include/common.hpp:14:9`! void *xxmemalign(size_t alignment, size_t sz) {\
227 return TheHeapWrapper::memalign(alignment, sz);\
228 }\
229 \
230 ATTRIBUTE_EXPORTcxx . . $ `src/include/common.hpp:14:9`! size_t xxmalloc_usable_size(void *ptr) {\
231 return TheHeapWrapper::getSize(ptr); \
232 }\
233 \
234 ATTRIBUTE_EXPORTcxx . . $ `src/include/common.hpp:14:9`! void xxmalloc_lock() {\
235 TheHeapWrapper::xxmalloc_lock();\
236 }\
237 \
238 ATTRIBUTE_EXPORTcxx . . $ `src/include/common.hpp:14:9`! void xxmalloc_unlock() {\
239 TheHeapWrapper::xxmalloc_unlock();\
240 }\
241 ATTRIBUTE_EXPORTcxx . . $ `src/include/common.hpp:14:9`! void* xxrealloc(void * ptr, size_t sz) {\
242 return TheHeapWrapper::realloc(ptr, sz); \
243 }\
244 }
245 #else
246 #define HEAP_REDIRECT(CustomHeap, staticSize)\
247 typedef HL::HeapWrapper<CustomHeap, staticSize> TheHeapWrapper;\
248 extern "C" {\
249 ATTRIBUTE_EXPORT void *xxmalloc(size_t sz) {\
250 return TheHeapWrapper::malloc(sz);\
251 }\
252 \
253 ATTRIBUTE_EXPORT void xxfree(void *ptr) {\
254 TheHeapWrapper::free(ptr);\
255 }\
256 \
257 ATTRIBUTE_EXPORT void *xxmemalign(size_t alignment, size_t sz) {\
258 return TheHeapWrapper::memalign(alignment, sz);\
259 }\
260 \
261 ATTRIBUTE_EXPORT size_t xxmalloc_usable_size(void *ptr) {\
262 return TheHeapWrapper::getSize(ptr); \
263 }\
264 \
265 ATTRIBUTE_EXPORT void xxmalloc_lock() {\
266 TheHeapWrapper::xxmalloc_lock();\
267 }\
268 \
269 ATTRIBUTE_EXPORT void xxmalloc_unlock() {\
270 TheHeapWrapper::xxmalloc_unlock();\
271 }\
272 }
273
274 #endif
275 #endif