From 76b0873ce2ebe3bc28543cb2293c0c62e4617243 Mon Sep 17 00:00:00 2001 From: daanx Date: Mon, 3 Jun 2024 20:28:47 -0700 Subject: [PATCH] fix asan tracking by explicitly setting memory to undefined before a free --- src/arena.c | 6 +++--- src/page.c | 2 +- test/test-stress.c | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/arena.c b/src/arena.c index 8a5e8952..0bd58aaa 100644 --- a/src/arena.c +++ b/src/arena.c @@ -627,6 +627,9 @@ void _mi_arena_free(void* p, size_t size, size_t committed_size, mi_memid_t memi if (size==0) return; const bool all_committed = (committed_size == size); + // need to set all memory to undefined as some parts may still be marked as no_access (like padding etc.) + mi_track_mem_undefined(p,size); + if (mi_memkind_is_os(memid.memkind)) { // was a direct OS allocation, pass through if (!all_committed && committed_size > 0) { @@ -656,9 +659,6 @@ void _mi_arena_free(void* p, size_t size, size_t committed_size, mi_memid_t memi return; } - // need to set all memory to undefined as some parts may still be marked as no_access (like padding etc.) - mi_track_mem_undefined(p,size); - // potentially decommit if (arena->memid.is_pinned || arena->blocks_committed == NULL) { mi_assert_internal(all_committed); diff --git a/src/page.c b/src/page.c index 5a18b780..96d1b24c 100644 --- a/src/page.c +++ b/src/page.c @@ -857,7 +857,7 @@ static mi_page_t* mi_find_page(mi_heap_t* heap, size_t size, size_t huge_alignme // huge allocation? const size_t req_size = size - MI_PADDING_SIZE; // correct for padding_size in case of an overflow on `size` if mi_unlikely(req_size > (MI_LARGE_OBJ_SIZE_MAX - MI_PADDING_SIZE) || huge_alignment > 0) { - if mi_unlikely(req_size > MI_MAX_ALLOC_SIZE) { + if mi_unlikely(req_size > MI_MAX_ALLOC_SIZE) { _mi_error_message(EOVERFLOW, "allocation request is too large (%zu bytes)\n", req_size); return NULL; } diff --git a/test/test-stress.c b/test/test-stress.c index 675c54bc..2c031894 100644 --- a/test/test-stress.c +++ b/test/test-stress.c @@ -133,9 +133,9 @@ static void free_items(void* p) { custom_free(p); } -#ifdef HEAP_WALK +#ifdef HEAP_WALK static bool visit_blocks(const mi_heap_t* heap, const mi_heap_area_t* area, void* block, size_t block_size, void* arg) { - (void)(heap); (void)(area); + (void)(heap); (void)(area); size_t* total = (size_t*)arg; if (block != NULL) { *total += block_size; @@ -260,7 +260,7 @@ static void test_leak(void) { int main(int argc, char** argv) { #ifdef HEAP_WALK - mi_option_enable(mi_option_visit_abandoned); + mi_option_enable(mi_option_visit_abandoned); #endif #ifndef NDEBUG mi_option_set(mi_option_arena_reserve, 32 * 1024 /* in kib = 32MiB */);