From 4a0c53ba5a76ae4b1016f88ddba48eb47674c64a Mon Sep 17 00:00:00 2001 From: Anton Korobeynikov Date: Tue, 21 Apr 2020 15:07:32 +0300 Subject: [PATCH 1/3] Fix the prototype of malloc_size. Otherwise we'll end with invalid redeclaration if malloc/malloc.h is pulled on Darwin --- src/alloc-override.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/alloc-override.c b/src/alloc-override.c index 0e908f42..75c7990f 100644 --- a/src/alloc-override.c +++ b/src/alloc-override.c @@ -165,7 +165,7 @@ extern "C" { void cfree(void* p) MI_FORWARD0(mi_free, p); void* reallocf(void* p, size_t newsize) MI_FORWARD2(mi_reallocf,p,newsize); -size_t malloc_size(void* p) MI_FORWARD1(mi_usable_size,p); +size_t malloc_size(const void* p) MI_FORWARD1(mi_usable_size,p); #if !defined(__ANDROID__) size_t malloc_usable_size(void *p) MI_FORWARD1(mi_usable_size,p); #else From 07d72f4fba97c698191a855fdf24efddc4387e74 Mon Sep 17 00:00:00 2001 From: Anton Korobeynikov Date: Tue, 21 Apr 2020 15:08:27 +0300 Subject: [PATCH 2/3] Do not forget to include malloc zone implementation in the static object --- CMakeLists.txt | 1 + src/static.c | 3 +++ 2 files changed, 4 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index e37b5083..d0d03991 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -67,6 +67,7 @@ if(MI_OVERRIDE MATCHES "ON") # use zone's on macOS message(STATUS " Use malloc zone to override malloc (MI_OSX_ZONE=ON)") list(APPEND mi_sources src/alloc-override-osx.c) + list(APPEND mi_defines MI_OSX_ZONE=1) if(NOT MI_INTERPOSE MATCHES "ON") message(STATUS " (enabling INTERPOSE as well since zone's require this)") set(MI_INTERPOSE "ON") diff --git a/src/static.c b/src/static.c index ec9370eb..bf86166d 100644 --- a/src/static.c +++ b/src/static.c @@ -24,5 +24,8 @@ terms of the MIT license. A copy of the license can be found in the file #include "alloc.c" #include "alloc-aligned.c" #include "alloc-posix.c" +#if MI_OSX_ZONE +#include "alloc-override-osx.c" +#endif #include "init.c" #include "options.c" From fe976caaea6d208f1c8499bdf5e478f35df6e67e Mon Sep 17 00:00:00 2001 From: Anton Korobeynikov Date: Tue, 21 Apr 2020 15:10:49 +0300 Subject: [PATCH 3/3] Provide zone_size function: free() uses it to find the zone pointer belongs to in order to call the corresponding zone_free function --- src/alloc-override-osx.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/alloc-override-osx.c b/src/alloc-override-osx.c index cc03f5e2..c1c880ca 100644 --- a/src/alloc-override-osx.c +++ b/src/alloc-override-osx.c @@ -41,8 +41,11 @@ extern malloc_zone_t* malloc_default_purgeable_zone(void) __attribute__((weak_im ------------------------------------------------------ */ static size_t zone_size(malloc_zone_t* zone, const void* p) { - UNUSED(zone); UNUSED(p); - return 0; // as we cannot guarantee that `p` comes from us, just return 0 + UNUSED(zone); + if (!mi_is_in_heap_region(p)) + return 0; // not our pointer, bail out + + return mi_usable_size(p); } static void* zone_malloc(malloc_zone_t* zone, size_t size) {