Skip to content

Commit c47de7e

Browse files
committed
improve macOS overriding
1 parent e853f53 commit c47de7e

File tree

3 files changed

+108
-57
lines changed

3 files changed

+108
-57
lines changed

CMakeLists.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ set(mi_sources
4343
src/options.c
4444
src/init.c)
4545

46+
4647
# -----------------------------------------------------------------------------
4748
# Convenience: set default build type depending on the build directory
4849
# -----------------------------------------------------------------------------
@@ -62,6 +63,7 @@ if("${CMAKE_BINARY_DIR}" MATCHES ".*(S|s)ecure$")
6263
set(MI_SECURE "ON")
6364
endif()
6465

66+
6567
# -----------------------------------------------------------------------------
6668
# Process options
6769
# -----------------------------------------------------------------------------
@@ -85,11 +87,14 @@ if(MI_OVERRIDE)
8587
if(MI_OSX_INTERPOSE)
8688
# use interpose on macOS
8789
message(STATUS " Use interpose to override malloc (MI_OSX_INTERPOSE=ON)")
88-
list(APPEND mi_defines MI_OSX_INTERPOSE)
90+
list(APPEND mi_defines MI_OSX_INTERPOSE=1)
8991
if (NOT MI_OSX_ZONE)
9092
message(STATUS " WARNING: interpose usually also needs zone overriding (use -DMI_OSX_INTERPOSE=ON)")
9193
endif()
9294
endif()
95+
if((NOT MI_USE_CXX) AND MI_OVERRIDE)
96+
message(STATUS " WARNING: if overriding C++ new/delete, it is best to build mimalloc with a C++ compiler (use -DMI_USE_CXX=ON)")
97+
endif()
9398
endif()
9499
endif()
95100

@@ -260,9 +265,9 @@ message(STATUS "")
260265
message(STATUS "Library base name: ${mi_basename}")
261266
message(STATUS "Build type : ${CMAKE_BUILD_TYPE_LC}")
262267
if(MI_USE_CXX)
263-
message(STATUS "Compiler : ${CMAKE_CXX_COMPILER}")
268+
message(STATUS "C++ Compiler : ${CMAKE_CXX_COMPILER}")
264269
else()
265-
message(STATUS "Compiler : ${CMAKE_C_COMPILER}")
270+
message(STATUS "C Compiler : ${CMAKE_C_COMPILER}")
266271
endif()
267272
message(STATUS "Version : ${mi_version}")
268273
message(STATUS "Build targets : ${mi_build_targets}")

src/alloc-override-osx.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,16 @@ static malloc_zone_t mi_malloc_zone = {
232232
#endif
233233

234234

235-
#if defined(MI_OSX_INTERPOSE)
235+
#if defined(MI_OSX_INTERPOSE) && defined(MI_SHARED_LIB_EXPORT)
236236

237237
// ------------------------------------------------------
238-
// Override malloc_xxx and zone_xxx api's to use only
238+
// Override malloc_xxx and malloc_zone_xxx api's to use only
239239
// our mimalloc zone. Since even the loader uses malloc
240240
// on macOS, this ensures that all allocations go through
241241
// mimalloc (as all calls are interposed).
242+
// The main `malloc`, `free`, etc calls are interposed in `alloc-override.c`,
243+
// Here, we also override macOS specific API's like
244+
// `malloc_zone_calloc` etc. see <https://github.com/aosm/libmalloc/blob/master/man/malloc_zone_malloc.3>
242245
// ------------------------------------------------------
243246

244247
static inline malloc_zone_t* mi_get_default_zone(void)
@@ -386,6 +389,8 @@ __attribute__((used)) static const struct mi_interpose_s _mi_zone_interposes[]
386389

387390
// ------------------------------------------------------
388391
// hook into the zone api's without interposing
392+
// This is the official way of adding an allocator but
393+
// it seems less robust than using interpose.
389394
// ------------------------------------------------------
390395

391396
static inline malloc_zone_t* mi_get_default_zone(void)

src/alloc-override.c

Lines changed: 93 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,21 @@ terms of the MIT license. A copy of the license can be found in the file
1515

1616
#if defined(MI_MALLOC_OVERRIDE) && !(defined(_WIN32))
1717

18+
#if defined(__APPLE__)
19+
mi_decl_externc void vfree(void* p);
20+
mi_decl_externc size_t malloc_size(const void* p);
21+
mi_decl_externc size_t malloc_good_size(size_t size);
22+
#endif
23+
24+
// helper definition for C override of C++ new
25+
typedef struct mi_nothrow_s { int _tag; } mi_nothrow_t;
26+
1827
// ------------------------------------------------------
1928
// Override system malloc
2029
// ------------------------------------------------------
2130

22-
#if (defined(__GNUC__) || defined(__clang__)) && !defined(__APPLE__)
23-
// use aliasing to alias the exported function to one of our `mi_` functions
31+
#if (defined(__GNUC__) || defined(__clang__)) && !defined(__APPLE__) && !defined(MI_VALGRIND)
32+
// gcc, clang: use aliasing to alias the exported function to one of our `mi_` functions
2433
#if (defined(__GNUC__) && __GNUC__ >= 9)
2534
#pragma GCC diagnostic ignored "-Wattributes" // or we get warnings that nodiscard is ignored on a forward
2635
#define MI_FORWARD(fun) __attribute__((alias(#fun), used, visibility("default"), copy(fun)));
@@ -33,19 +42,18 @@ terms of the MIT license. A copy of the license can be found in the file
3342
#define MI_FORWARD0(fun,x) MI_FORWARD(fun)
3443
#define MI_FORWARD02(fun,x,y) MI_FORWARD(fun)
3544
#else
36-
// use forwarding by calling our `mi_` function
45+
// otherwise use forwarding by calling our `mi_` function
3746
#define MI_FORWARD1(fun,x) { return fun(x); }
3847
#define MI_FORWARD2(fun,x,y) { return fun(x,y); }
3948
#define MI_FORWARD3(fun,x,y,z) { return fun(x,y,z); }
4049
#define MI_FORWARD0(fun,x) { fun(x); }
4150
#define MI_FORWARD02(fun,x,y) { fun(x,y); }
4251
#endif
4352

44-
#if defined(__APPLE__) && defined(MI_SHARED_LIB_EXPORT) && defined(MI_OSX_INTERPOSE)
45-
#include <malloc/malloc.h>
46-
mi_decl_externc void vfree(void* p);
47-
mi_decl_externc size_t malloc_size(const void* p);
48-
mi_decl_externc size_t malloc_good_size(size_t size);
53+
#if defined(__APPLE__) && defined(MI_SHARED_LIB_EXPORT) && defined(MI_OSX_INTERPOSE)
54+
// define MI_OSX_IS_INTERPOSED as we should not provide forwarding definitions for
55+
// functions that are interposed (or the interposing does not work)
56+
#define MI_OSX_IS_INTERPOSED
4957

5058
// use interposing so `DYLD_INSERT_LIBRARIES` works without `DYLD_FORCE_FLAT_NAMESPACE=1`
5159
// See: <https://books.google.com/books?id=K8vUkpOXhN4C&pg=PA73>
@@ -70,16 +78,41 @@ terms of the MIT license. A copy of the license can be found in the file
7078
MI_INTERPOSE_MI(malloc_size),
7179
MI_INTERPOSE_MI(malloc_good_size),
7280
MI_INTERPOSE_MI(aligned_alloc),
73-
#ifndef MI_OSX_ZONE
74-
// sometimes code allocates from default zone but deallocates using plain free :-( (like NxHashResizeToCapacity <https://github.com/nneonneo/osx-10.9-opensource/blob/master/objc4-551.1/runtime/hashtable2.mm>)
75-
MI_INTERPOSE_FUN(free,mi_cfree), // use safe free that checks if pointers are from us
76-
MI_INTERPOSE_FUN(vfree,mi_cfree),
77-
#else
81+
#ifdef MI_OSX_ZONE
7882
// we interpose malloc_default_zone in alloc-override-osx.c so we can use mi_free safely
7983
MI_INTERPOSE_MI(free),
8084
MI_INTERPOSE_FUN(vfree,mi_free),
85+
#else
86+
// sometimes code allocates from default zone but deallocates using plain free :-( (like NxHashResizeToCapacity <https://github.com/nneonneo/osx-10.9-opensource/blob/master/objc4-551.1/runtime/hashtable2.mm>)
87+
MI_INTERPOSE_FUN(free,mi_cfree), // use safe free that checks if pointers are from us
88+
MI_INTERPOSE_FUN(vfree,mi_cfree),
8189
#endif
8290
};
91+
92+
#ifdef __cplusplus
93+
extern "C" {
94+
void _ZdlPv(void* p); // delete
95+
void _ZdaPv(void* p); // delete[]
96+
void _ZdlPvm(void* p, size_t n); // delete
97+
void _ZdaPvm(void* p, size_t n); // delete[]
98+
void* _Znwm(size_t n); // new
99+
void* _Znam(size_t n); // new[]
100+
void* _ZnwmRKSt9nothrow_t(size_t n, mi_nothrow_t tag); // new nothrow
101+
void* _ZnamRKSt9nothrow_t(size_t n, mi_nothrow_t tag); // new[] nothrow
102+
}
103+
__attribute__((used)) static struct mi_interpose_s _mi_cxx_interposes[] __attribute__((section("__DATA, __interpose"))) =
104+
{
105+
MI_INTERPOSE_FUN(_ZdlPv,mi_free),
106+
MI_INTERPOSE_FUN(_ZdaPv,mi_free),
107+
MI_INTERPOSE_FUN(_ZdlPvm,mi_free_size),
108+
MI_INTERPOSE_FUN(_ZdaPvm,mi_free_size),
109+
MI_INTERPOSE_FUN(_Znwm,mi_new),
110+
MI_INTERPOSE_FUN(_Znam,mi_new),
111+
MI_INTERPOSE_FUN(_ZnwmRKSt9nothrow_t,mi_new_nothrow),
112+
MI_INTERPOSE_FUN(_ZnamRKSt9nothrow_t,mi_new_nothrow),
113+
};
114+
#endif // __cplusplus
115+
83116
#elif defined(_MSC_VER)
84117
// cannot override malloc unless using a dll.
85118
// we just override new/delete which does work in a static library.
@@ -106,18 +139,21 @@ terms of the MIT license. A copy of the license can be found in the file
106139
// see <https://en.cppreference.com/w/cpp/memory/new/operator_new>
107140
// ------------------------------------------------------
108141
#include <new>
109-
void operator delete(void* p) noexcept MI_FORWARD0(mi_free,p)
110-
void operator delete[](void* p) noexcept MI_FORWARD0(mi_free,p)
111142

112-
void* operator new(std::size_t n) noexcept(false) MI_FORWARD1(mi_new,n)
113-
void* operator new[](std::size_t n) noexcept(false) MI_FORWARD1(mi_new,n)
143+
#ifndef MI_OSX_IS_INTERPOSED
144+
void operator delete(void* p) noexcept MI_FORWARD0(mi_free,p)
145+
void operator delete[](void* p) noexcept MI_FORWARD0(mi_free,p)
146+
147+
void* operator new(std::size_t n) noexcept(false) MI_FORWARD1(mi_new,n)
148+
void* operator new[](std::size_t n) noexcept(false) MI_FORWARD1(mi_new,n)
114149

115-
void* operator new (std::size_t n, const std::nothrow_t& tag) noexcept { UNUSED(tag); return mi_new_nothrow(n); }
116-
void* operator new[](std::size_t n, const std::nothrow_t& tag) noexcept { UNUSED(tag); return mi_new_nothrow(n); }
150+
void* operator new (std::size_t n, const std::nothrow_t& tag) noexcept { UNUSED(tag); return mi_new_nothrow(n); }
151+
void* operator new[](std::size_t n, const std::nothrow_t& tag) noexcept { UNUSED(tag); return mi_new_nothrow(n); }
117152

118-
#if (__cplusplus >= 201402L || _MSC_VER >= 1916)
119-
void operator delete (void* p, std::size_t n) noexcept MI_FORWARD02(mi_free_size,p,n)
120-
void operator delete[](void* p, std::size_t n) noexcept MI_FORWARD02(mi_free_size,p,n)
153+
#if (__cplusplus >= 201402L || _MSC_VER >= 1916)
154+
void operator delete (void* p, std::size_t n) noexcept MI_FORWARD02(mi_free_size,p,n)
155+
void operator delete[](void* p, std::size_t n) noexcept MI_FORWARD02(mi_free_size,p,n)
156+
#endif
121157
#endif
122158

123159
#if (__cplusplus > 201402L && defined(__cpp_aligned_new)) && (!defined(__GNUC__) || (__GNUC__ > 5))
@@ -132,12 +168,13 @@ terms of the MIT license. A copy of the license can be found in the file
132168
void* operator new[](std::size_t n, std::align_val_t al, const std::nothrow_t&) noexcept { return mi_new_aligned_nothrow(n, static_cast<size_t>(al)); }
133169
#endif
134170

135-
#elif (defined(__GNUC__) || defined(__clang__)) && !defined(MI_OSX_ZONE)
171+
#elif (defined(__GNUC__) || defined(__clang__))
136172
// ------------------------------------------------------
137173
// Override by defining the mangled C++ names of the operators (as
138174
// used by GCC and CLang).
139175
// See <https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling>
140176
// ------------------------------------------------------
177+
141178
void _ZdlPv(void* p) MI_FORWARD0(mi_free,p) // delete
142179
void _ZdaPv(void* p) MI_FORWARD0(mi_free,p) // delete[]
143180
void _ZdlPvm(void* p, size_t n) MI_FORWARD02(mi_free_size,p,n)
@@ -146,67 +183,71 @@ terms of the MIT license. A copy of the license can be found in the file
146183
void _ZdaPvSt11align_val_t(void* p, size_t al) { mi_free_aligned(p,al); }
147184
void _ZdlPvmSt11align_val_t(void* p, size_t n, size_t al) { mi_free_size_aligned(p,n,al); }
148185
void _ZdaPvmSt11align_val_t(void* p, size_t n, size_t al) { mi_free_size_aligned(p,n,al); }
149-
150-
typedef struct mi_nothrow_s { int _tag; } mi_nothrow_t;
186+
151187
#if (MI_INTPTR_SIZE==8)
152188
void* _Znwm(size_t n) MI_FORWARD1(mi_new,n) // new 64-bit
153189
void* _Znam(size_t n) MI_FORWARD1(mi_new,n) // new[] 64-bit
190+
void* _ZnwmRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { UNUSED(tag); return mi_new_nothrow(n); }
191+
void* _ZnamRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { UNUSED(tag); return mi_new_nothrow(n); }
154192
void* _ZnwmSt11align_val_t(size_t n, size_t al) MI_FORWARD2(mi_new_aligned, n, al)
155193
void* _ZnamSt11align_val_t(size_t n, size_t al) MI_FORWARD2(mi_new_aligned, n, al)
156-
void* _ZnwmRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { UNUSED(tag); return mi_new_nothrow(n); }
157-
void* _ZnamRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { UNUSED(tag); return mi_new_nothrow(n); }
158194
void* _ZnwmSt11align_val_tRKSt9nothrow_t(size_t n, size_t al, mi_nothrow_t tag) { UNUSED(tag); return mi_new_aligned_nothrow(n,al); }
159195
void* _ZnamSt11align_val_tRKSt9nothrow_t(size_t n, size_t al, mi_nothrow_t tag) { UNUSED(tag); return mi_new_aligned_nothrow(n,al); }
160196
#elif (MI_INTPTR_SIZE==4)
161197
void* _Znwj(size_t n) MI_FORWARD1(mi_new,n) // new 64-bit
162198
void* _Znaj(size_t n) MI_FORWARD1(mi_new,n) // new[] 64-bit
199+
void* _ZnwjRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { UNUSED(tag); return mi_new_nothrow(n); }
200+
void* _ZnajRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { UNUSED(tag); return mi_new_nothrow(n); }
163201
void* _ZnwjSt11align_val_t(size_t n, size_t al) MI_FORWARD2(mi_new_aligned, n, al)
164202
void* _ZnajSt11align_val_t(size_t n, size_t al) MI_FORWARD2(mi_new_aligned, n, al)
165-
void* _ZnwjRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { UNUSED(tag); return mi_new_nothrow(n); }
166-
void* _ZnajRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { UNUSED(tag); return mi_new_nothrow(n); }
167203
void* _ZnwjSt11align_val_tRKSt9nothrow_t(size_t n, size_t al, mi_nothrow_t tag) { UNUSED(tag); return mi_new_aligned_nothrow(n,al); }
168204
void* _ZnajSt11align_val_tRKSt9nothrow_t(size_t n, size_t al, mi_nothrow_t tag) { UNUSED(tag); return mi_new_aligned_nothrow(n,al); }
169205
#else
170-
#error "define overloads for new/delete for this platform (just for performance, can be skipped)"
206+
#error "define overloads for new/delete for this platform (just for performance, can be skipped)"
171207
#endif
172208
#endif // __cplusplus
173209

210+
// ------------------------------------------------------
211+
// Further Posix & Unix functions definitions
212+
// ------------------------------------------------------
174213

175214
#ifdef __cplusplus
176215
extern "C" {
177216
#endif
178217

179-
// ------------------------------------------------------
180-
// Posix & Unix functions definitions
181-
// ------------------------------------------------------
218+
#ifndef MI_OSX_IS_INTERPOSED
219+
// Forward Posix/Unix calls as well
220+
void* reallocf(void* p, size_t newsize) MI_FORWARD2(mi_reallocf,p,newsize)
221+
size_t malloc_size(const void* p) MI_FORWARD1(mi_usable_size,p)
222+
size_t malloc_good_size(size_t size) MI_FORWARD1(mi_malloc_good_size,size)
223+
#if !defined(__ANDROID__) && !defined(__FreeBSD__)
224+
size_t malloc_usable_size(void *p) MI_FORWARD1(mi_usable_size,p)
225+
#else
226+
size_t malloc_usable_size(const void *p) MI_FORWARD1(mi_usable_size,p)
227+
#endif
182228

183-
void cfree(void* p) MI_FORWARD0(mi_free, p)
184-
void* reallocf(void* p, size_t newsize) MI_FORWARD2(mi_reallocf,p,newsize)
185-
size_t malloc_size(const void* p) MI_FORWARD1(mi_usable_size,p)
186-
#if !defined(__ANDROID__) && !defined(__FreeBSD__)
187-
size_t malloc_usable_size(void *p) MI_FORWARD1(mi_usable_size,p)
188-
#else
189-
size_t malloc_usable_size(const void *p) MI_FORWARD1(mi_usable_size,p)
229+
// No forwarding here due to aliasing/name mangling issues
230+
void* valloc(size_t size) { return mi_valloc(size); }
231+
void vfree(void* p) { mi_free(p); }
232+
int posix_memalign(void** p, size_t alignment, size_t size) { return mi_posix_memalign(p, alignment, size); }
233+
234+
// `aligned_alloc` is only available when __USE_ISOC11 is defined.
235+
// Note: Conda has a custom glibc where `aligned_alloc` is declared `static inline` and we cannot
236+
// override it, but both _ISOC11_SOURCE and __USE_ISOC11 are undefined in Conda GCC7 or GCC9.
237+
// Fortunately, in the case where `aligned_alloc` is declared as `static inline` it
238+
// uses internally `memalign`, `posix_memalign`, or `_aligned_malloc` so we can avoid overriding it ourselves.
239+
#if __USE_ISOC11
240+
void* aligned_alloc(size_t alignment, size_t size) { return mi_aligned_alloc(alignment, size); }
241+
#endif
190242
#endif
191243

192244
// no forwarding here due to aliasing/name mangling issues
193-
void* valloc(size_t size) { return mi_valloc(size); }
245+
void cfree(void* p) { mi_free(p); }
194246
void* pvalloc(size_t size) { return mi_pvalloc(size); }
195247
void* reallocarray(void* p, size_t count, size_t size) { return mi_reallocarray(p, count, size); }
196248
void* memalign(size_t alignment, size_t size) { return mi_memalign(alignment, size); }
197-
int posix_memalign(void** p, size_t alignment, size_t size) { return mi_posix_memalign(p, alignment, size); }
198249
void* _aligned_malloc(size_t alignment, size_t size) { return mi_aligned_alloc(alignment, size); }
199250

200-
// `aligned_alloc` is only available when __USE_ISOC11 is defined.
201-
// Note: Conda has a custom glibc where `aligned_alloc` is declared `static inline` and we cannot
202-
// override it, but both _ISOC11_SOURCE and __USE_ISOC11 are undefined in Conda GCC7 or GCC9.
203-
// Fortunately, in the case where `aligned_alloc` is declared as `static inline` it
204-
// uses internally `memalign`, `posix_memalign`, or `_aligned_malloc` so we can avoid overriding it ourselves.
205-
#if __USE_ISOC11
206-
void* aligned_alloc(size_t alignment, size_t size) { return mi_aligned_alloc(alignment, size); }
207-
#endif
208-
209-
210251
#if defined(__GLIBC__) && defined(__linux__)
211252
// forward __libc interface (needed for glibc-based Linux distributions)
212253
void* __libc_malloc(size_t size) MI_FORWARD1(mi_malloc,size)

0 commit comments

Comments
 (0)