From 81b261e657bf07d7c4411db46f8fcae8a631c8ff Mon Sep 17 00:00:00 2001 From: Daan Date: Fri, 25 Nov 2022 15:44:24 -0800 Subject: [PATCH 1/2] add test for #587 --- test/test-api.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/test-api.c b/test/test-api.c index f202e7c1..c6d289de 100644 --- a/test/test-api.c +++ b/test/test-api.c @@ -205,6 +205,12 @@ int main(void) { } result = ok; } + CHECK_BODY("malloc_aligned11") { + mi_heap_t* heap = mi_heap_new(); + void* p = mi_heap_malloc_aligned(heap, 33554426, 8); + result = mi_heap_contains_block(heap, p); + mi_heap_destroy(heap); + } CHECK_BODY("malloc-aligned-at1") { void* p = mi_malloc_aligned_at(48,32,0); result = (p != NULL && ((uintptr_t)(p) + 0) % 32 == 0); mi_free(p); }; From 8098040c23d0267cc3fd7ce2aebb6410fdf991e8 Mon Sep 17 00:00:00 2001 From: Daan Date: Fri, 25 Nov 2022 16:38:20 -0800 Subject: [PATCH 2/2] add pointer validity check on malloc_size when overriding on macOSX; issue #638 --- src/alloc-override.c | 8 +++++++- src/alloc-posix.c | 4 ++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/alloc-override.c b/src/alloc-override.c index 9534e9d5..70cf3367 100644 --- a/src/alloc-override.c +++ b/src/alloc-override.c @@ -51,11 +51,17 @@ typedef struct mi_nothrow_s { int _tag; } mi_nothrow_t; #define MI_FORWARD02(fun,x,y) { fun(x,y); } #endif + #if defined(__APPLE__) && defined(MI_SHARED_LIB_EXPORT) && defined(MI_OSX_INTERPOSE) // define MI_OSX_IS_INTERPOSED as we should not provide forwarding definitions for // functions that are interposed (or the interposing does not work) #define MI_OSX_IS_INTERPOSED + mi_decl_externc static size_t mi_malloc_size_checked(void *p) { + if (!mi_is_in_heap_region(p)) return 0; + return mi_usable_size(p); + } + // use interposing so `DYLD_INSERT_LIBRARIES` works without `DYLD_FORCE_FLAT_NAMESPACE=1` // See: struct mi_interpose_s { @@ -76,7 +82,7 @@ typedef struct mi_nothrow_s { int _tag; } mi_nothrow_t; MI_INTERPOSE_MI(posix_memalign), MI_INTERPOSE_MI(reallocf), MI_INTERPOSE_MI(valloc), - MI_INTERPOSE_MI(malloc_size), + MI_INTERPOSE_FUN(malloc_size,mi_malloc_size_checked), MI_INTERPOSE_MI(malloc_good_size), #if defined(MAC_OS_X_VERSION_10_15) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_15 MI_INTERPOSE_MI(aligned_alloc), diff --git a/src/alloc-posix.c b/src/alloc-posix.c index 57e15d05..e73628f4 100644 --- a/src/alloc-posix.c +++ b/src/alloc-posix.c @@ -33,12 +33,12 @@ terms of the MIT license. A copy of the license can be found in the file mi_decl_nodiscard size_t mi_malloc_size(const void* p) mi_attr_noexcept { - //if (!mi_is_in_heap_region(p)) return 0; + // if (!mi_is_in_heap_region(p)) return 0; return mi_usable_size(p); } mi_decl_nodiscard size_t mi_malloc_usable_size(const void *p) mi_attr_noexcept { - //if (!mi_is_in_heap_region(p)) return 0; + // if (!mi_is_in_heap_region(p)) return 0; return mi_usable_size(p); }