Add heap poisoning mechanism to fill absorbed block header with the right pattern.

Note: block_absorb() is called in tlsf_free(), which is eventually called in multi_heap_free()
after the memory fill is done. As the block_absorb merges 2 blocks together, the previous block
header of the merged block is now in the middle of the memory block but not filled with 0xfe.

- Remove dependencies with the heap component of the IDF.
- The tlsf_poison_fill_region_hook() function is defined as weak in IDF and checked for NULL
in block_absorb() function in order to minimize the dependencies between IDF and the TLSF.
- The the implementation of tlsf_poison_fill_region_hook() must be provided at the discretion
of the user.
This commit is contained in:
Ivan Grokhotkov
2021-12-13 22:19:27 +01:00
committed by Guillaume Souchere
parent 1322e839d2
commit b76f0fefd1
3 changed files with 25 additions and 4 deletions

6
tlsf.c
View File

@@ -456,6 +456,12 @@ static inline __attribute__((always_inline)) block_header_t* block_absorb(block_
/* Note: Leaves flags untouched. */
prev->size += block_size(block) + block_header_overhead;
block_link_next(prev);
if (block_absorb_post_hook != NULL)
{
block_absorb_post_hook(block, sizeof(block_header_t), POISONING_AFTER_FREE);
}
return prev;
}

11
tlsf.h
View File

@@ -8,6 +8,7 @@
#define INCLUDED_tlsf
#include <stddef.h>
#include <stdbool.h>
#if defined(__cplusplus)
extern "C" {
@@ -52,6 +53,16 @@ void tlsf_walk_pool(pool_t pool, tlsf_walker walker, void* user);
int tlsf_check(tlsf_t tlsf);
int tlsf_check_pool(pool_t pool);
/*!
* @brief Weak function filling the given memory with a given fill pattern.
*
* @param start: pointer to the start of the memory region to fill
* @param size: size of the memory region to fill
* @param is_free: Indicate if the pattern to use the fill the region should be
* an after free or after allocation pattern.
*/
__attribute__((weak)) void block_absorb_post_hook(void *start, size_t size, bool is_free);
#if defined(__cplusplus)
};
#endif

View File

@@ -6,15 +6,19 @@
#pragma once
#include <stddef.h>
#if defined __has_include && __has_include("tlsf_platform.h")
#include "tlsf_platform.h"
#endif
#include <assert.h>
#if defined(__cplusplus)
extern "C" {
#endif
/*
** Constants definition for poisoning.
** These defines are used as 3rd argument of tlsf_poison_fill_region() for readability purposes.
*/
#define POISONING_AFTER_FREE true
#define POISONING_AFTER_MALLOC !POISONING_AFTER_FREE
/*
** Cast and min/max macros.
*/