From 82c85d1a130eb1e204177169476785728888c0ae Mon Sep 17 00:00:00 2001 From: Daan Leijen Date: Sun, 5 Mar 2023 18:03:04 -0800 Subject: [PATCH] fix valgrind mem for large alignment --- src/alloc-aligned.c | 17 +++++++---------- test/test-wrong.c | 24 +++++++++++++++++++++++- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/alloc-aligned.c b/src/alloc-aligned.c index c24f5c9d..08ad9814 100644 --- a/src/alloc-aligned.c +++ b/src/alloc-aligned.c @@ -74,20 +74,17 @@ static mi_decl_noinline void* mi_heap_malloc_zero_aligned_at_fallback(mi_heap_t* mi_assert_internal(mi_usable_size(p) == mi_usable_size(aligned_p)+adjust); // now zero the block if needed - if (zero && alignment > MI_ALIGNMENT_MAX) { - const ptrdiff_t diff = (uint8_t*)aligned_p - (uint8_t*)p; - ptrdiff_t zsize = mi_page_usable_block_size(_mi_ptr_page(p)) - diff - MI_PADDING_SIZE; - #if MI_PADDING - zsize -= MI_MAX_ALIGN_SIZE; - #endif - if (zsize > 0) { _mi_memzero(aligned_p, zsize); } + if (alignment > MI_ALIGNMENT_MAX) { + // for the tracker, on huge aligned allocations only from the start of the large block is defined + mi_track_mem_undefined(aligned_p, size); + if (zero) { + _mi_memzero(aligned_p, mi_usable_size(aligned_p)); + } } - #if MI_TRACK_ENABLED if (p != aligned_p) { mi_track_align(p,aligned_p,adjust,mi_usable_size(aligned_p)); - } - #endif + } return aligned_p; } diff --git a/test/test-wrong.c b/test/test-wrong.c index 17d253b6..aaaf60b9 100644 --- a/test/test-wrong.c +++ b/test/test-wrong.c @@ -5,7 +5,10 @@ terms of the MIT license. A copy of the license can be found in the file "LICENSE" at the root of this distribution. -----------------------------------------------------------------------------*/ -/* test file for valgrind support. +/* test file for valgrind/asan support. + + VALGRIND: + ---------- Compile in an "out/debug" folder: > cd out/debug @@ -19,6 +22,25 @@ terms of the MIT license. A copy of the license can be found in the file and test as: > valgrind ./test-wrong + + + ASAN + ---------- + Compile in an "out/debug" folder: + + > cd out/debug + > cmake ../.. -DMI_ASAN=1 + > make -j8 + + and then compile this file as: + + > clang -g -o test-wrong -I../../include ../../test/test-wrong.c libmimalloc-asan-debug.a -lpthread -fsanitize=address -fsanitize-recover=address + + and test as: + + > ASAN_OPTIONS=verbosity=1:halt_on_error=0 ./test-wrong + + */ #include #include