From 69a0846478131810c85d6df903c5cf36aee858c7 Mon Sep 17 00:00:00 2001 From: daan Date: Tue, 7 Apr 2020 10:01:18 -0700 Subject: [PATCH] add MI_PADDING flag to cmake to supress use of padding in debug mode --- CMakeLists.txt | 6 ++++++ include/mimalloc-types.h | 4 ++-- src/alloc.c | 4 ++-- src/init.c | 4 ++-- test/main-override-static.c | 2 +- test/main-override.cpp | 2 -- 6 files changed, 13 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a0893007..61303345 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,7 @@ option(MI_OSX_ZONE "Use malloc zone to override standard malloc on macO option(MI_LOCAL_DYNAMIC_TLS "Use slightly slower, dlopen-compatible TLS mechanism (Unix)" OFF) option(MI_BUILD_TESTS "Build test executables" ON) option(MI_CHECK_FULL "Use full internal invariant checking in DEBUG mode (deprecated, use MI_DEBUG_FULL instead)" OFF) +option(MI_PADDING "Enable padding to detect heap block overflow (only in debug mode)" ON) include("cmake/mimalloc-config-version.cmake") @@ -99,6 +100,11 @@ if(MI_DEBUG_FULL MATCHES "ON") list(APPEND mi_defines MI_DEBUG=3) # full invariant checking endif() +if(MI_PADDING MATCHES "OFF") + message(STATUS "Disable padding of heap blocks in debug mode (MI_PADDING=OFF)") + list(APPEND mi_defines MI_PADDING=0) +endif() + if(MI_USE_CXX MATCHES "ON") message(STATUS "Use the C++ compiler to compile (MI_USE_CXX=ON)") set_source_files_properties(${mi_sources} PROPERTIES LANGUAGE CXX ) diff --git a/include/mimalloc-types.h b/include/mimalloc-types.h index 28606668..7e50a2bc 100644 --- a/include/mimalloc-types.h +++ b/include/mimalloc-types.h @@ -57,7 +57,7 @@ terms of the MIT license. A copy of the license can be found in the file // Encoded free lists allow detection of corrupted free lists // and can detect buffer overflows, modify after free, and double `free`s. -#if (MI_SECURE>=3 || MI_DEBUG>=1 || defined(MI_PADDING)) +#if (MI_SECURE>=3 || MI_DEBUG>=1 || MI_PADDING > 0) #define MI_ENCODE_FREELIST 1 #endif @@ -303,7 +303,7 @@ typedef struct mi_random_cxt_s { // In debug mode there is a padding stucture at the end of the blocks to check for buffer overflows -#if defined(MI_PADDING) +#if (MI_PADDING) typedef struct mi_padding_s { uint32_t canary; // encoded block value to check validity of the padding (in case of overflow) uint32_t delta; // padding bytes before the block. (mi_usable_size(p) - delta == exact allocated bytes) diff --git a/src/alloc.c b/src/alloc.c index 9b441499..d7b8219e 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -44,7 +44,7 @@ extern inline void* _mi_page_malloc(mi_heap_t* heap, mi_page_t* page, size_t siz mi_heap_stat_increase(heap, normal[bin], 1); } #endif -#if defined(MI_PADDING) && defined(MI_ENCODE_FREELIST) +#if (MI_PADDING > 0) && defined(MI_ENCODE_FREELIST) mi_padding_t* const padding = (mi_padding_t*)((uint8_t*)block + mi_page_usable_block_size(page)); ptrdiff_t delta = ((uint8_t*)padding - (uint8_t*)block - (size - MI_PADDING_SIZE)); mi_assert_internal(delta >= 0 && mi_page_usable_block_size(page) >= (size - MI_PADDING_SIZE + delta)); @@ -203,7 +203,7 @@ static inline bool mi_check_is_double_free(const mi_page_t* page, const mi_block // Check for heap block overflow by setting up padding at the end of the block // --------------------------------------------------------------------------- -#if defined(MI_PADDING) && defined(MI_ENCODE_FREELIST) +#if (MI_PADDING>0) && defined(MI_ENCODE_FREELIST) static bool mi_page_decode_padding(const mi_page_t* page, const mi_block_t* block, size_t* delta, size_t* bsize) { *bsize = mi_page_usable_block_size(page); const mi_padding_t* const padding = (mi_padding_t*)((uint8_t*)block + *bsize); diff --git a/src/init.c b/src/init.c index e95ff674..6b62e888 100644 --- a/src/init.c +++ b/src/init.c @@ -32,9 +32,9 @@ const mi_page_t _mi_page_empty = { #define MI_PAGE_EMPTY() ((mi_page_t*)&_mi_page_empty) -#if defined(MI_PADDING) && (MI_INTPTR_SIZE >= 8) +#if (MI_PADDING>0) && (MI_INTPTR_SIZE >= 8) #define MI_SMALL_PAGES_EMPTY { MI_INIT128(MI_PAGE_EMPTY), MI_PAGE_EMPTY(), MI_PAGE_EMPTY() } -#elif defined(MI_PADDING) +#elif (MI_PADDING>0) #define MI_SMALL_PAGES_EMPTY { MI_INIT128(MI_PAGE_EMPTY), MI_PAGE_EMPTY(), MI_PAGE_EMPTY(), MI_PAGE_EMPTY() } #else #define MI_SMALL_PAGES_EMPTY { MI_INIT128(MI_PAGE_EMPTY), MI_PAGE_EMPTY() } diff --git a/test/main-override-static.c b/test/main-override-static.c index 950392d0..9243fd21 100644 --- a/test/main-override-static.c +++ b/test/main-override-static.c @@ -19,7 +19,7 @@ int main() { // double_free1(); // double_free2(); // corrupt_free(); - // block_overflow1(); + block_overflow1(); void* p1 = malloc(78); void* p2 = malloc(24); diff --git a/test/main-override.cpp b/test/main-override.cpp index eda32ae4..734e4c94 100644 --- a/test/main-override.cpp +++ b/test/main-override.cpp @@ -29,12 +29,10 @@ void various_tests(); int main() { mi_stats_reset(); // ignore earlier allocations heap_thread_free_large(); - /* heap_no_delete(); heap_late_free(); padding_shrink(); various_tests(); - */ mi_stats_print(NULL); return 0; }