From 262d11fcfa5f5811fa59d7e7865a1cc5aec71c6c Mon Sep 17 00:00:00 2001 From: Guillaume Souchere Date: Wed, 27 Sep 2023 13:22:02 +0200 Subject: [PATCH] tlsf: move the call the tlsf_check_hook from tlsf_check to the intergrity_walker function Previously the hook was called from tlsf_check function leading to only check the free blocks and leaving the used blocks unchecked by the hook. By moving the function hook call to the integrity_walker function, free and used blocks are now passed to the function hook. --- tlsf.c | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/tlsf.c b/tlsf.c index 68105d0..a8018c7 100644 --- a/tlsf.c +++ b/tlsf.c @@ -668,14 +668,32 @@ static void integrity_walker(void* ptr, size_t size, int used, void* user) const size_t this_block_size = block_size(block); int status = 0; - (void)used; tlsf_insist(integ->prev_status == this_prev_status && "prev status incorrect"); tlsf_insist(size == this_block_size && "block size incorrect"); + if (tlsf_check_hook != NULL) + { + /* block_size(block) returns the size of the usable memory when the block is allocated. + * As the block under test is free, we need to subtract to the block size the next_free + * and prev_free fields of the block header as they are not a part of the usable memory + * when the block is free. In addition, we also need to subtract the size of prev_phys_block + * as this field is in fact part of the current free block and not part of the next (allocated) + * block. Check the comments in block_split function for more details. + */ + const size_t actual_free_block_size = used ? this_block_size : + this_block_size - offsetof(block_header_t, next_free)- block_header_overhead; + + void* ptr_block = used ? (void*)block + block_start_offset : + (void*)block + sizeof(block_header_t); + + tlsf_insist(tlsf_check_hook(ptr_block, actual_free_block_size, !used)); + } + integ->prev_status = this_status; integ->status += status; } + int tlsf_check(tlsf_t tlsf) { int i, j; @@ -722,23 +740,6 @@ int tlsf_check(tlsf_t tlsf) mapping_insert(control, block_size(block), &fli, &sli); tlsf_insist(fli == i && sli == j && "block size indexed in wrong list"); - if (tlsf_check_hook != NULL) - { - /* block_size(block) returns the size of the usable memory when the block is allocated. - * As the block under test is free, we need to subtract to the block size the next_free - * and prev_free fields of the block header as they are not a part of the usable memory - * when the block is free. In addition, we also need to subtract the size of prev_phys_block - * as this field is in fact part of the current free block and not part of the next (allocated) - * block. Check the comments in block_split function for more details. - */ - const size_t actual_free_block_size = block_size(block) - - offsetof(block_header_t, next_free) - - block_header_overhead; - - tlsf_insist(tlsf_check_hook((void*)block + sizeof(block_header_t), - actual_free_block_size, is_block_free)); - } - block = block->next_free; } }