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 fe4b71b3..3b16b29c 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 @@ -318,8 +318,8 @@ typedef struct mi_random_cxt_s { int output_available; } mi_random_ctx_t; -#define MI_PAGES_DIRECT (MI_SMALL_WSIZE_MAX + MI_PADDING_WSIZE + 1) +#define MI_PAGES_DIRECT (MI_SMALL_WSIZE_MAX + MI_PADDING_WSIZE + 1) // A heap owns a set of pages. struct mi_heap_s { diff --git a/src/alloc.c b/src/alloc.c index 15c6e12f..76342771 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -51,7 +51,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 - __extra_padding)); mi_assert_internal(delta >= 0 && mi_page_usable_block_size(page) >= (size - __extra_padding + delta)); @@ -209,7 +209,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 void mi_check_padding(const mi_page_t* page, const mi_block_t* block); static const mi_padding_t* mi_page_get_padding(const mi_page_t* page, const mi_block_t* block, size_t* bsize) { diff --git a/src/init.c b/src/init.c index 3bcc7094..f5ed3014 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) -#define MI_SMALL_PAGES_EMPTY { MI_INIT128(MI_PAGE_EMPTY), MI_PAGE_EMPTY(), MI_PAGE_EMPTY(), MI_PAGE_EMPTY() } -#elif defined(MI_PADDING) +#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 (MI_PADDING>0) #define MI_SMALL_PAGES_EMPTY { MI_INIT128(MI_PAGE_EMPTY), MI_PAGE_EMPTY(), 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/src/options.c b/src/options.c index d0cf46cb..7cf90fa8 100644 --- a/src/options.c +++ b/src/options.c @@ -390,9 +390,7 @@ void _mi_error_message(int err, const char* fmt, ...) { // lsb=1: bit 63-19: relative file name char* (to `mi_fname_base`), bit 18-1: line number // lsb=0: bit 63-01: return address // ----------------------------------------------------------------------------------------------- -#ifndef NDEBUG static const char* mi_debug_fname_base = "mimalloc_fname_base"; -static const char* mi_debug_fname_invalid = ""; #define MI_FNAME_SHIFT 16 #define MI_LINE_SHIFT (MI_FNAME_SHIFT + MI_INTPTR_SHIFT) @@ -459,14 +457,14 @@ void* mi_source_unpack(mi_source_t source, const char** pfname, int* plineno) { return ((void*)(source.src >> 1)); } } -#endif + // ----------------------------------------------------------------------------------------------- // Error message for a specific heap block // ----------------------------------------------------------------------------------------------- void _mi_page_block_error_message(int err, const mi_page_t* page, const mi_block_t* block, const char* msg) { -#if defined(MI_PADDING) && defined(MI_ENCODE_FREELIST) +#if (MI_PADDING>0) && defined(MI_ENCODE_FREELIST) const size_t bsize = mi_page_usable_block_size(page); const mi_padding_t* const padding = (mi_padding_t*)((uint8_t*)block + bsize); const size_t size = (padding->delta <= bsize ? bsize - padding->delta : bsize);