1 /*
2  * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include <stdint.h>
7 #include <stdlib.h>
8 #include <stdbool.h>
9 #include <assert.h>
10 #include <string.h>
11 #include <stddef.h>
12 #include <stdio.h>
13 #include <sys/cdefs.h>
14 #include "multi_heap.h"
15 #include "multi_heap_internal.h"
16 
17 #if !CONFIG_HEAP_TLSF_USE_ROM_IMPL
18 #include "tlsf.h"
19 #include "tlsf_block_functions.h"
20 #endif
21 
22 /* Note: Keep platform-specific parts in this header, this source
23    file should depend on libc only */
24 #include "multi_heap_platform.h"
25 
26 /* Defines compile-time configuration macros */
27 #include "multi_heap_config.h"
28 
29 #if (!defined MULTI_HEAP_POISONING) && (!defined CONFIG_HEAP_TLSF_USE_ROM_IMPL)
30 /* if no heap poisoning, public API aliases directly to these implementations */
31 void *multi_heap_malloc(multi_heap_handle_t heap, size_t size)
32     __attribute__((alias("multi_heap_malloc_impl")));
33 
34 void *multi_heap_aligned_alloc(multi_heap_handle_t heap, size_t size, size_t alignment)
35     __attribute__((alias("multi_heap_aligned_alloc_impl")));
36 
37 void multi_heap_aligned_free(multi_heap_handle_t heap, void *p)
38     __attribute__((alias("multi_heap_free_impl")));
39 
40 void multi_heap_free(multi_heap_handle_t heap, void *p)
41     __attribute__((alias("multi_heap_free_impl")));
42 
43 void *multi_heap_realloc(multi_heap_handle_t heap, void *p, size_t size)
44     __attribute__((alias("multi_heap_realloc_impl")));
45 
46 size_t multi_heap_get_allocated_size(multi_heap_handle_t heap, void *p)
47     __attribute__((alias("multi_heap_get_allocated_size_impl")));
48 
49 multi_heap_handle_t multi_heap_register(void *start, size_t size)
50     __attribute__((alias("multi_heap_register_impl")));
51 
52 void multi_heap_get_info(multi_heap_handle_t heap, multi_heap_info_t *info)
53     __attribute__((alias("multi_heap_get_info_impl")));
54 
55 size_t multi_heap_free_size(multi_heap_handle_t heap)
56     __attribute__((alias("multi_heap_free_size_impl")));
57 
58 size_t multi_heap_minimum_free_size(multi_heap_handle_t heap)
59     __attribute__((alias("multi_heap_minimum_free_size_impl")));
60 
61 void *multi_heap_get_block_address(multi_heap_block_handle_t block)
62     __attribute__((alias("multi_heap_get_block_address_impl")));
63 
multi_heap_get_block_owner(multi_heap_block_handle_t block)64 void *multi_heap_get_block_owner(multi_heap_block_handle_t block)
65 {
66     return NULL;
67 }
68 
69 #endif
70 
71 #define ALIGN(X) ((X) & ~(sizeof(void *)-1))
72 #define ALIGN_UP(X) ALIGN((X)+sizeof(void *)-1)
73 #define ALIGN_UP_BY(num, align) (((num) + ((align) - 1)) & ~((align) - 1))
74 
75 
76 typedef struct multi_heap_info {
77     void *lock;
78     size_t free_bytes;
79     size_t minimum_free_bytes;
80     size_t pool_size;
81     void* heap_data;
82 } heap_t;
83 
84 #if CONFIG_HEAP_TLSF_USE_ROM_IMPL
85 
_multi_heap_lock(void * lock)86 void _multi_heap_lock(void *lock)
87 {
88     MULTI_HEAP_LOCK(lock);
89 }
90 
_multi_heap_unlock(void * lock)91 void _multi_heap_unlock(void *lock)
92 {
93     MULTI_HEAP_UNLOCK(lock);
94 }
95 
96 multi_heap_os_funcs_t multi_heap_os_funcs = {
97     .lock = _multi_heap_lock,
98     .unlock = _multi_heap_unlock,
99 };
100 
multi_heap_in_rom_init(void)101 void multi_heap_in_rom_init(void)
102 {
103     multi_heap_os_funcs_init(&multi_heap_os_funcs);
104 }
105 
106 #else // CONFIG_HEAP_TLSF_USE_ROM_IMPL
107 
108 /* Check a block is valid for this heap. Used to verify parameters. */
assert_valid_block(const heap_t * heap,const block_header_t * block)109 __attribute__((noinline)) NOCLONE_ATTR static void assert_valid_block(const heap_t *heap, const block_header_t *block)
110 {
111     pool_t pool = tlsf_get_pool(heap->heap_data);
112     void *ptr = block_to_ptr(block);
113 
114     MULTI_HEAP_ASSERT((ptr >= pool) &&
115                     (ptr < pool + heap->pool_size),
116                     (uintptr_t)ptr);
117 }
118 
multi_heap_get_block_address_impl(multi_heap_block_handle_t block)119 void *multi_heap_get_block_address_impl(multi_heap_block_handle_t block)
120 {
121     return block_to_ptr(block);
122 }
123 
multi_heap_get_allocated_size_impl(multi_heap_handle_t heap,void * p)124 size_t multi_heap_get_allocated_size_impl(multi_heap_handle_t heap, void *p)
125 {
126     return tlsf_block_size(p);
127 }
128 
multi_heap_register_impl(void * start_ptr,size_t size)129 multi_heap_handle_t multi_heap_register_impl(void *start_ptr, size_t size)
130 {
131     assert(start_ptr);
132     if(size < (sizeof(heap_t))) {
133         //Region too small to be a heap.
134         return NULL;
135     }
136 
137     heap_t *result = (heap_t *)start_ptr;
138     size -= sizeof(heap_t);
139 
140     /* Do not specify any maximum size for the allocations so that the default configuration is used */
141     const size_t max_bytes = 0;
142 
143     result->heap_data = tlsf_create_with_pool(start_ptr + sizeof(heap_t), size, max_bytes);
144     if(!result->heap_data) {
145         return NULL;
146     }
147 
148     result->lock = NULL;
149     result->free_bytes = size - tlsf_size(result->heap_data);
150     result->pool_size = size;
151     result->minimum_free_bytes = result->free_bytes;
152     return result;
153 }
154 
multi_heap_set_lock(multi_heap_handle_t heap,void * lock)155 void multi_heap_set_lock(multi_heap_handle_t heap, void *lock)
156 {
157     heap->lock = lock;
158 }
159 
multi_heap_internal_lock(multi_heap_handle_t heap)160 void multi_heap_internal_lock(multi_heap_handle_t heap)
161 {
162     MULTI_HEAP_LOCK(heap->lock);
163 }
164 
multi_heap_internal_unlock(multi_heap_handle_t heap)165 void multi_heap_internal_unlock(multi_heap_handle_t heap)
166 {
167     MULTI_HEAP_UNLOCK(heap->lock);
168 }
169 
multi_heap_get_first_block(multi_heap_handle_t heap)170 multi_heap_block_handle_t multi_heap_get_first_block(multi_heap_handle_t heap)
171 {
172     assert(heap != NULL);
173     pool_t pool = tlsf_get_pool(heap->heap_data);
174     block_header_t* block = offset_to_block(pool, -(int)block_header_overhead);
175 
176     return (multi_heap_block_handle_t)block;
177 }
178 
multi_heap_get_next_block(multi_heap_handle_t heap,multi_heap_block_handle_t block)179 multi_heap_block_handle_t multi_heap_get_next_block(multi_heap_handle_t heap, multi_heap_block_handle_t block)
180 {
181     assert(heap != NULL);
182     assert_valid_block(heap, block);
183     block_header_t* next = block_next(block);
184 
185     if(block_size(next) == 0) {
186         //Last block:
187         return NULL;
188     } else {
189         return (multi_heap_block_handle_t)next;
190     }
191 
192 }
193 
multi_heap_is_free(multi_heap_block_handle_t block)194 bool multi_heap_is_free(multi_heap_block_handle_t block)
195 {
196     return block_is_free(block);
197 }
198 
multi_heap_malloc_impl(multi_heap_handle_t heap,size_t size)199 void *multi_heap_malloc_impl(multi_heap_handle_t heap, size_t size)
200 {
201     if (size == 0 || heap == NULL) {
202         return NULL;
203     }
204 
205 
206     multi_heap_internal_lock(heap);
207     void *result = tlsf_malloc(heap->heap_data, size);
208     if(result) {
209         heap->free_bytes -= tlsf_block_size(result);
210         heap->free_bytes -= tlsf_alloc_overhead();
211         if (heap->free_bytes < heap->minimum_free_bytes) {
212             heap->minimum_free_bytes = heap->free_bytes;
213         }
214     }
215     multi_heap_internal_unlock(heap);
216 
217     return result;
218 }
219 
multi_heap_free_impl(multi_heap_handle_t heap,void * p)220 void multi_heap_free_impl(multi_heap_handle_t heap, void *p)
221 {
222     if (heap == NULL || p == NULL) {
223         return;
224     }
225 
226     assert_valid_block(heap, block_from_ptr(p));
227 
228     multi_heap_internal_lock(heap);
229     heap->free_bytes += tlsf_block_size(p);
230     heap->free_bytes += tlsf_alloc_overhead();
231     tlsf_free(heap->heap_data, p);
232     multi_heap_internal_unlock(heap);
233 }
234 
multi_heap_realloc_impl(multi_heap_handle_t heap,void * p,size_t size)235 void *multi_heap_realloc_impl(multi_heap_handle_t heap, void *p, size_t size)
236 {
237     assert(heap != NULL);
238 
239     if (p == NULL) {
240         return multi_heap_malloc_impl(heap, size);
241     }
242 
243     assert_valid_block(heap, block_from_ptr(p));
244 
245     if (heap == NULL) {
246         return NULL;
247     }
248 
249     multi_heap_internal_lock(heap);
250     size_t previous_block_size =  tlsf_block_size(p);
251     void *result = tlsf_realloc(heap->heap_data, p, size);
252     if(result) {
253         /* No need to subtract the tlsf_alloc_overhead() as it has already
254          * been subtracted when allocating the block at first with malloc */
255         heap->free_bytes += previous_block_size;
256         heap->free_bytes -= tlsf_block_size(result);
257         if (heap->free_bytes < heap->minimum_free_bytes) {
258             heap->minimum_free_bytes = heap->free_bytes;
259         }
260     }
261 
262     multi_heap_internal_unlock(heap);
263 
264     return result;
265 }
266 
multi_heap_aligned_alloc_impl_offs(multi_heap_handle_t heap,size_t size,size_t alignment,size_t offset)267 void *multi_heap_aligned_alloc_impl_offs(multi_heap_handle_t heap, size_t size, size_t alignment, size_t offset)
268 {
269     if(heap == NULL) {
270         return NULL;
271     }
272 
273     if(!size) {
274         return NULL;
275     }
276 
277     //Alignment must be a power of two:
278     if(((alignment & (alignment - 1)) != 0) ||(!alignment)) {
279         return NULL;
280     }
281 
282     multi_heap_internal_lock(heap);
283     void *result = tlsf_memalign_offs(heap->heap_data, alignment, size, offset);
284     if(result) {
285         heap->free_bytes -= tlsf_block_size(result);
286         heap->free_bytes -= tlsf_alloc_overhead();
287         if(heap->free_bytes < heap->minimum_free_bytes) {
288             heap->minimum_free_bytes = heap->free_bytes;
289         }
290     }
291     multi_heap_internal_unlock(heap);
292 
293     return result;
294 }
295 
296 
multi_heap_aligned_alloc_impl(multi_heap_handle_t heap,size_t size,size_t alignment)297 void *multi_heap_aligned_alloc_impl(multi_heap_handle_t heap, size_t size, size_t alignment)
298 {
299     return multi_heap_aligned_alloc_impl_offs(heap, size, alignment, 0);
300 }
301 
302 #ifdef MULTI_HEAP_POISONING
303 /*!
304  * @brief Global definition of print_errors set in multi_heap_check() when
305  * MULTI_HEAP_POISONING is active. Allows the transfer of the value to
306  * multi_heap_poisoning.c without having to propagate it to the tlsf submodule
307  * and back.
308  */
309 static bool g_print_errors = false;
310 
311 /*!
312  * @brief Definition of the weak function declared in TLSF repository.
313  * The call of this function execute a check for block poisoning on the memory
314  * chunk passed as parameter.
315  *
316  * @param start: pointer to the start of the memory region to check for corruption
317  * @param size: size of the memory region to check for corruption
318  * @param is_free: indicate if the pattern to use the fill the region should be
319  * an after free or after allocation pattern.
320  *
321  * @return bool: true if the the memory is not corrupted, false if the memory if corrupted.
322  */
tlsf_check_hook(void * start,size_t size,bool is_free)323 bool tlsf_check_hook(void *start, size_t size, bool is_free)
324 {
325     return multi_heap_internal_check_block_poisoning(start, size, is_free, g_print_errors);
326 }
327 #endif // MULTI_HEAP_POISONING
328 
multi_heap_check(multi_heap_handle_t heap,bool print_errors)329 bool multi_heap_check(multi_heap_handle_t heap, bool print_errors)
330 {
331     bool valid = true;
332     assert(heap != NULL);
333 
334     multi_heap_internal_lock(heap);
335 
336 #ifdef MULTI_HEAP_POISONING
337     g_print_errors = print_errors;
338 #else
339     (void) print_errors;
340 #endif
341 
342     if(tlsf_check(heap->heap_data)) {
343         valid = false;
344     }
345 
346     if(tlsf_check_pool(tlsf_get_pool(heap->heap_data))) {
347         valid = false;
348     }
349 
350     multi_heap_internal_unlock(heap);
351     return valid;
352 }
353 
multi_heap_dump_tlsf(void * ptr,size_t size,int used,void * user)354 __attribute__((noinline)) static void multi_heap_dump_tlsf(void* ptr, size_t size, int used, void* user)
355 {
356     (void)user;
357     MULTI_HEAP_STDERR_PRINTF("Block %p data, size: %d bytes, Free: %s \n",
358                             (void *)ptr,
359                             size,
360                             used ? "No" : "Yes");
361 }
362 
multi_heap_dump(multi_heap_handle_t heap)363 void multi_heap_dump(multi_heap_handle_t heap)
364 {
365     assert(heap != NULL);
366 
367     multi_heap_internal_lock(heap);
368     MULTI_HEAP_STDERR_PRINTF("Showing data for heap: %p \n", (void *)heap);
369     tlsf_walk_pool(tlsf_get_pool(heap->heap_data), multi_heap_dump_tlsf, NULL);
370     multi_heap_internal_unlock(heap);
371 }
372 
multi_heap_free_size_impl(multi_heap_handle_t heap)373 size_t multi_heap_free_size_impl(multi_heap_handle_t heap)
374 {
375     if (heap == NULL) {
376         return 0;
377     }
378 
379     return heap->free_bytes;
380 }
381 
multi_heap_minimum_free_size_impl(multi_heap_handle_t heap)382 size_t multi_heap_minimum_free_size_impl(multi_heap_handle_t heap)
383 {
384     if (heap == NULL) {
385         return 0;
386     }
387 
388     return heap->minimum_free_bytes;
389 }
390 
multi_heap_get_info_tlsf(void * ptr,size_t size,int used,void * user)391 __attribute__((noinline)) static void multi_heap_get_info_tlsf(void* ptr, size_t size, int used, void* user)
392 {
393     multi_heap_info_t *info = user;
394 
395     if(used) {
396         info->allocated_blocks++;
397     } else {
398         info->free_blocks++;
399 
400         if(size > info->largest_free_block ) {
401             info->largest_free_block = size;
402         }
403     }
404 
405     info->total_blocks++;
406 }
407 
multi_heap_get_info_impl(multi_heap_handle_t heap,multi_heap_info_t * info)408 void multi_heap_get_info_impl(multi_heap_handle_t heap, multi_heap_info_t *info)
409 {
410     uint32_t overhead;
411 
412     memset(info, 0, sizeof(multi_heap_info_t));
413 
414     if (heap == NULL) {
415         return;
416     }
417 
418     multi_heap_internal_lock(heap);
419     tlsf_walk_pool(tlsf_get_pool(heap->heap_data), multi_heap_get_info_tlsf, info);
420     /* TLSF has an overhead per block. Calculate the total amount of overhead, it shall not be
421      * part of the allocated bytes */
422     overhead = info->allocated_blocks * tlsf_alloc_overhead();
423     info->total_allocated_bytes = (heap->pool_size - tlsf_size(heap->heap_data)) - heap->free_bytes - overhead;
424     info->minimum_free_bytes = heap->minimum_free_bytes;
425     info->total_free_bytes = heap->free_bytes;
426     info->largest_free_block = tlsf_fit_size(heap->heap_data, info->largest_free_block);
427     multi_heap_internal_unlock(heap);
428 }
429 #endif
430