/src/llvm-project-18.1.8.src/libcxxabi/src/fallback_malloc.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===----------------------------------------------------------------------===// |
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 "fallback_malloc.h" |
10 | | #include "abort_message.h" |
11 | | |
12 | | #include <__threading_support> |
13 | | #ifndef _LIBCXXABI_HAS_NO_THREADS |
14 | | #if defined(__ELF__) && defined(_LIBCXXABI_LINK_PTHREAD_LIB) |
15 | | #pragma comment(lib, "pthread") |
16 | | #endif |
17 | | #endif |
18 | | |
19 | | #include <__memory/aligned_alloc.h> |
20 | | #include <__assert> |
21 | | #include <stdlib.h> // for malloc, calloc, free |
22 | | #include <string.h> // for memset |
23 | | |
24 | | // A small, simple heap manager based (loosely) on |
25 | | // the startup heap manager from FreeBSD, optimized for space. |
26 | | // |
27 | | // Manages a fixed-size memory pool, supports malloc and free only. |
28 | | // No support for realloc. |
29 | | // |
30 | | // Allocates chunks in multiples of four bytes, with a four byte header |
31 | | // for each chunk. The overhead of each chunk is kept low by keeping pointers |
32 | | // as two byte offsets within the heap, rather than (4 or 8 byte) pointers. |
33 | | |
34 | | namespace { |
35 | | |
36 | | // When POSIX threads are not available, make the mutex operations a nop |
37 | | #ifndef _LIBCXXABI_HAS_NO_THREADS |
38 | | static _LIBCPP_CONSTINIT std::__libcpp_mutex_t heap_mutex = _LIBCPP_MUTEX_INITIALIZER; |
39 | | #else |
40 | | static _LIBCPP_CONSTINIT void* heap_mutex = 0; |
41 | | #endif |
42 | | |
43 | | class mutexor { |
44 | | public: |
45 | | #ifndef _LIBCXXABI_HAS_NO_THREADS |
46 | 0 | mutexor(std::__libcpp_mutex_t* m) : mtx_(m) { |
47 | 0 | std::__libcpp_mutex_lock(mtx_); |
48 | 0 | } |
49 | 0 | ~mutexor() { std::__libcpp_mutex_unlock(mtx_); } |
50 | | #else |
51 | | mutexor(void*) {} |
52 | | ~mutexor() {} |
53 | | #endif |
54 | | private: |
55 | | mutexor(const mutexor& rhs); |
56 | | mutexor& operator=(const mutexor& rhs); |
57 | | #ifndef _LIBCXXABI_HAS_NO_THREADS |
58 | | std::__libcpp_mutex_t* mtx_; |
59 | | #endif |
60 | | }; |
61 | | |
62 | | static const size_t HEAP_SIZE = 512; |
63 | | char heap[HEAP_SIZE] __attribute__((aligned)); |
64 | | |
65 | | typedef unsigned short heap_offset; |
66 | | typedef unsigned short heap_size; |
67 | | |
68 | | // On both 64 and 32 bit targets heap_node should have the following properties |
69 | | // Size: 4 |
70 | | // Alignment: 2 |
71 | | struct heap_node { |
72 | | heap_offset next_node; // offset into heap |
73 | | heap_size len; // size in units of "sizeof(heap_node)" |
74 | | }; |
75 | | |
76 | | // All pointers returned by fallback_malloc must be at least aligned |
77 | | // as RequiredAligned. Note that RequiredAlignment can be greater than |
78 | | // alignof(std::max_align_t) on 64 bit systems compiling 32 bit code. |
79 | | struct FallbackMaxAlignType { |
80 | | } __attribute__((aligned)); |
81 | | const size_t RequiredAlignment = alignof(FallbackMaxAlignType); |
82 | | |
83 | | static_assert(alignof(FallbackMaxAlignType) % sizeof(heap_node) == 0, |
84 | | "The required alignment must be evenly divisible by the sizeof(heap_node)"); |
85 | | |
86 | | // The number of heap_node's that can fit in a chunk of memory with the size |
87 | | // of the RequiredAlignment. On 64 bit targets NodesPerAlignment should be 4. |
88 | | const size_t NodesPerAlignment = alignof(FallbackMaxAlignType) / sizeof(heap_node); |
89 | | |
90 | | static const heap_node* list_end = |
91 | | (heap_node*)(&heap[HEAP_SIZE]); // one past the end of the heap |
92 | | static heap_node* freelist = NULL; |
93 | | |
94 | 0 | heap_node* node_from_offset(const heap_offset offset) { |
95 | 0 | return (heap_node*)(heap + (offset * sizeof(heap_node))); |
96 | 0 | } |
97 | | |
98 | 0 | heap_offset offset_from_node(const heap_node* ptr) { |
99 | 0 | return static_cast<heap_offset>( |
100 | 0 | static_cast<size_t>(reinterpret_cast<const char*>(ptr) - heap) / |
101 | 0 | sizeof(heap_node)); |
102 | 0 | } |
103 | | |
104 | | // Return a pointer to the first address, 'A', in `heap` that can actually be |
105 | | // used to represent a heap_node. 'A' must be aligned so that |
106 | | // '(A + sizeof(heap_node)) % RequiredAlignment == 0'. On 64 bit systems this |
107 | | // address should be 12 bytes after the first 16 byte boundary. |
108 | 0 | heap_node* getFirstAlignedNodeInHeap() { |
109 | 0 | heap_node* node = (heap_node*)heap; |
110 | 0 | const size_t alignNBytesAfterBoundary = RequiredAlignment - sizeof(heap_node); |
111 | 0 | size_t boundaryOffset = reinterpret_cast<size_t>(node) % RequiredAlignment; |
112 | 0 | size_t requiredOffset = alignNBytesAfterBoundary - boundaryOffset; |
113 | 0 | size_t NElemOffset = requiredOffset / sizeof(heap_node); |
114 | 0 | return node + NElemOffset; |
115 | 0 | } |
116 | | |
117 | 0 | void init_heap() { |
118 | 0 | freelist = getFirstAlignedNodeInHeap(); |
119 | 0 | freelist->next_node = offset_from_node(list_end); |
120 | 0 | freelist->len = static_cast<heap_size>(list_end - freelist); |
121 | 0 | } |
122 | | |
123 | | // How big a chunk we allocate |
124 | 0 | size_t alloc_size(size_t len) { |
125 | 0 | return (len + sizeof(heap_node) - 1) / sizeof(heap_node) + 1; |
126 | 0 | } |
127 | | |
128 | 2.17M | bool is_fallback_ptr(void* ptr) { |
129 | 2.17M | return ptr >= heap && ptr < (heap + HEAP_SIZE); |
130 | 2.17M | } |
131 | | |
132 | 0 | void* fallback_malloc(size_t len) { |
133 | 0 | heap_node *p, *prev; |
134 | 0 | const size_t nelems = alloc_size(len); |
135 | 0 | mutexor mtx(&heap_mutex); |
136 | |
|
137 | 0 | if (NULL == freelist) |
138 | 0 | init_heap(); |
139 | | |
140 | | // Walk the free list, looking for a "big enough" chunk |
141 | 0 | for (p = freelist, prev = 0; p && p != list_end; |
142 | 0 | prev = p, p = node_from_offset(p->next_node)) { |
143 | | |
144 | | // Check the invariant that all heap_nodes pointers 'p' are aligned |
145 | | // so that 'p + 1' has an alignment of at least RequiredAlignment |
146 | 0 | _LIBCXXABI_ASSERT(reinterpret_cast<size_t>(p + 1) % RequiredAlignment == 0, ""); |
147 | | |
148 | | // Calculate the number of extra padding elements needed in order |
149 | | // to split 'p' and create a properly aligned heap_node from the tail |
150 | | // of 'p'. We calculate aligned_nelems such that 'p->len - aligned_nelems' |
151 | | // will be a multiple of NodesPerAlignment. |
152 | 0 | size_t aligned_nelems = nelems; |
153 | 0 | if (p->len > nelems) { |
154 | 0 | heap_size remaining_len = static_cast<heap_size>(p->len - nelems); |
155 | 0 | aligned_nelems += remaining_len % NodesPerAlignment; |
156 | 0 | } |
157 | | |
158 | | // chunk is larger and we can create a properly aligned heap_node |
159 | | // from the tail. In this case we shorten 'p' and return the tail. |
160 | 0 | if (p->len > aligned_nelems) { |
161 | 0 | heap_node* q; |
162 | 0 | p->len = static_cast<heap_size>(p->len - aligned_nelems); |
163 | 0 | q = p + p->len; |
164 | 0 | q->next_node = 0; |
165 | 0 | q->len = static_cast<heap_size>(aligned_nelems); |
166 | 0 | void* ptr = q + 1; |
167 | 0 | _LIBCXXABI_ASSERT(reinterpret_cast<size_t>(ptr) % RequiredAlignment == 0, ""); |
168 | 0 | return ptr; |
169 | 0 | } |
170 | | |
171 | | // The chunk is the exact size or the chunk is larger but not large |
172 | | // enough to split due to alignment constraints. |
173 | 0 | if (p->len >= nelems) { |
174 | 0 | if (prev == 0) |
175 | 0 | freelist = node_from_offset(p->next_node); |
176 | 0 | else |
177 | 0 | prev->next_node = p->next_node; |
178 | 0 | p->next_node = 0; |
179 | 0 | void* ptr = p + 1; |
180 | 0 | _LIBCXXABI_ASSERT(reinterpret_cast<size_t>(ptr) % RequiredAlignment == 0, ""); |
181 | 0 | return ptr; |
182 | 0 | } |
183 | 0 | } |
184 | 0 | return NULL; // couldn't find a spot big enough |
185 | 0 | } |
186 | | |
187 | | // Return the start of the next block |
188 | 0 | heap_node* after(struct heap_node* p) { return p + p->len; } |
189 | | |
190 | 0 | void fallback_free(void* ptr) { |
191 | 0 | struct heap_node* cp = ((struct heap_node*)ptr) - 1; // retrieve the chunk |
192 | 0 | struct heap_node *p, *prev; |
193 | |
|
194 | 0 | mutexor mtx(&heap_mutex); |
195 | |
|
196 | | #ifdef DEBUG_FALLBACK_MALLOC |
197 | | std::printf("Freeing item at %d of size %d\n", offset_from_node(cp), cp->len); |
198 | | #endif |
199 | |
|
200 | 0 | for (p = freelist, prev = 0; p && p != list_end; |
201 | 0 | prev = p, p = node_from_offset(p->next_node)) { |
202 | | #ifdef DEBUG_FALLBACK_MALLOC |
203 | | std::printf(" p=%d, cp=%d, after(p)=%d, after(cp)=%d\n", |
204 | | offset_from_node(p), offset_from_node(cp), |
205 | | offset_from_node(after(p)), offset_from_node(after(cp))); |
206 | | #endif |
207 | 0 | if (after(p) == cp) { |
208 | | #ifdef DEBUG_FALLBACK_MALLOC |
209 | | std::printf(" Appending onto chunk at %d\n", offset_from_node(p)); |
210 | | #endif |
211 | 0 | p->len = static_cast<heap_size>( |
212 | 0 | p->len + cp->len); // make the free heap_node larger |
213 | 0 | return; |
214 | 0 | } else if (after(cp) == p) { // there's a free heap_node right after |
215 | | #ifdef DEBUG_FALLBACK_MALLOC |
216 | | std::printf(" Appending free chunk at %d\n", offset_from_node(p)); |
217 | | #endif |
218 | 0 | cp->len = static_cast<heap_size>(cp->len + p->len); |
219 | 0 | if (prev == 0) { |
220 | 0 | freelist = cp; |
221 | 0 | cp->next_node = p->next_node; |
222 | 0 | } else |
223 | 0 | prev->next_node = offset_from_node(cp); |
224 | 0 | return; |
225 | 0 | } |
226 | 0 | } |
227 | | // Nothing to merge with, add it to the start of the free list |
228 | | #ifdef DEBUG_FALLBACK_MALLOC |
229 | | std::printf(" Making new free list entry %d\n", offset_from_node(cp)); |
230 | | #endif |
231 | 0 | cp->next_node = offset_from_node(freelist); |
232 | 0 | freelist = cp; |
233 | 0 | } |
234 | | |
235 | | #ifdef INSTRUMENT_FALLBACK_MALLOC |
236 | | size_t print_free_list() { |
237 | | struct heap_node *p, *prev; |
238 | | heap_size total_free = 0; |
239 | | if (NULL == freelist) |
240 | | init_heap(); |
241 | | |
242 | | for (p = freelist, prev = 0; p && p != list_end; |
243 | | prev = p, p = node_from_offset(p->next_node)) { |
244 | | std::printf("%sOffset: %d\tsize: %d Next: %d\n", |
245 | | (prev == 0 ? "" : " "), offset_from_node(p), p->len, p->next_node); |
246 | | total_free += p->len; |
247 | | } |
248 | | std::printf("Total Free space: %d\n", total_free); |
249 | | return total_free; |
250 | | } |
251 | | #endif |
252 | | } // end unnamed namespace |
253 | | |
254 | | namespace __cxxabiv1 { |
255 | | |
256 | | struct __attribute__((aligned)) __aligned_type {}; |
257 | | |
258 | 2.17M | void* __aligned_malloc_with_fallback(size_t size) { |
259 | | #if defined(_WIN32) |
260 | | if (void* dest = std::__libcpp_aligned_alloc(alignof(__aligned_type), size)) |
261 | | return dest; |
262 | | #elif defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) |
263 | | if (void* dest = ::malloc(size)) |
264 | | return dest; |
265 | | #else |
266 | 2.17M | if (size == 0) |
267 | 0 | size = 1; |
268 | 2.17M | if (void* dest = std::__libcpp_aligned_alloc(__alignof(__aligned_type), size)) |
269 | 2.17M | return dest; |
270 | 18.4E | #endif |
271 | 18.4E | return fallback_malloc(size); |
272 | 2.17M | } |
273 | | |
274 | 348 | void* __calloc_with_fallback(size_t count, size_t size) { |
275 | 348 | void* ptr = ::calloc(count, size); |
276 | 348 | if (NULL != ptr) |
277 | 347 | return ptr; |
278 | | // if calloc fails, fall back to emergency stash |
279 | 1 | ptr = fallback_malloc(size * count); |
280 | 1 | if (NULL != ptr) |
281 | 0 | ::memset(ptr, 0, size * count); |
282 | 1 | return ptr; |
283 | 348 | } |
284 | | |
285 | 2.17M | void __aligned_free_with_fallback(void* ptr) { |
286 | 2.17M | if (is_fallback_ptr(ptr)) |
287 | 0 | fallback_free(ptr); |
288 | 2.17M | else { |
289 | | #if defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) |
290 | | ::free(ptr); |
291 | | #else |
292 | 2.17M | std::__libcpp_aligned_free(ptr); |
293 | 2.17M | #endif |
294 | 2.17M | } |
295 | 2.17M | } |
296 | | |
297 | 269 | void __free_with_fallback(void* ptr) { |
298 | 269 | if (is_fallback_ptr(ptr)) |
299 | 0 | fallback_free(ptr); |
300 | 269 | else |
301 | 269 | ::free(ptr); |
302 | 269 | } |
303 | | |
304 | | } // namespace __cxxabiv1 |