Skip to content

Commit ccac92e

Browse files
committed
add MI_FREE_IS_CHECKED option; shared libraries always check pointers in free now (see PR #1285)
1 parent ba446ee commit ccac92e

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ option(MI_GUARDED "Build with guard pages behind certain object alloca
1313
option(MI_USE_CXX "Use the C++ compiler to compile the library (instead of the C compiler)" OFF)
1414
option(MI_OPT_ARCH "Only for optimized builds: turn on architecture specific optimizations (for x64: '-march=haswell;-mavx2' (2013), for arm64: '-march=armv8.1-a' (2016))" OFF)
1515
option(MI_OPT_SIMD "Use SIMD instructions (requires MI_OPT_ARCH to be enabled)" OFF)
16+
option(MI_FREE_IS_CHECKED "Always check for invalid pointers in 'free' (always enabled in debug and secure builds, or when MI_OVERRIDE is ON)" OFF)
1617

1718
option(MI_DEBUG "Enable assertion checks (enabled by default in a debug build)" OFF)
1819
option(MI_DEBUG_INTERNAL "Enable assertion and internal invariant checks (enabled by default in a debug build)" OFF)
@@ -378,6 +379,17 @@ else()
378379
endif()
379380
endif()
380381

382+
if(MI_DEBUG OR MI_SECURE)
383+
set(MI_FREE_IS_CHECKED ON)
384+
endif()
385+
if(MI_FREE_IS_CHECKED)
386+
message(STATUS "Check validity of pointers passed to 'mi_free' (MI_FREE_IS_CHECKED=ON)")
387+
list(APPEND mi_defines MI_FREE_IS_CHECKED=1)
388+
elseif(MI_OVERRIDE)
389+
message(STATUS "Check validity of pointers passed to 'mi_free' only in the dynamic link library (MI_OVERRIDE=ON, MI_FREE_IS_CHECKED=OFF)")
390+
list(APPEND mi_cflags_dynamic -DMI_FREE_IS_CHECKED=1)
391+
endif()
392+
381393
if(MI_XMALLOC)
382394
message(STATUS "Enable abort() calls on memory allocation failure (MI_XMALLOC=ON)")
383395
list(APPEND mi_defines MI_XMALLOC=1)

include/mimalloc/internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ static inline mi_page_t* _mi_checked_ptr_page(const void* p) {
667667

668668
static inline mi_page_t* _mi_ptr_page(const void* p) {
669669
mi_assert_internal(p==NULL || mi_is_in_heap_region(p));
670-
#if MI_DEBUG || MI_SECURE || defined(__APPLE__)
670+
#if MI_DEBUG || MI_SECURE || MI_FREE_IS_CHECKED
671671
return _mi_checked_ptr_page(p);
672672
#else
673673
return _mi_unchecked_ptr_page(p);

0 commit comments

Comments
 (0)