1/* ----------------------------------------------------------------------------
2Copyright (c) 2018-2021, Microsoft Research, Daan Leijen
3This is free software; you can redistribute it and/or modify it under the
4terms of the MIT license. A copy of the license can be found in the file
5"LICENSE" at the root of this distribution.
6-----------------------------------------------------------------------------*/
7
8#if !defined(MI_IN_ALLOC_C)
9#error "this file should be included from 'alloc.c' (so aliases can work)"
10#endif
11
12#if defined(MI_MALLOC_OVERRIDE) && defined(_WIN32) && !(defined(MI_SHARED_LIB) && defined(_DLL))
13#error "It is only possible to override "malloc" on Windows when building as a DLL (and linking the C runtime as a DLL)"
14#endif
15
16#if defined(MI_MALLOC_OVERRIDE) && !(defined(_WIN32))
17
18#if defined(__APPLE__)
19#include <AvailabilityMacros.h>
20mi_decl_externc void vfree(void* p);
21mi_decl_externc size_t malloc_size(const void* p);
22mi_decl_externc size_t malloc_good_size(size_t size);
23#endif
24
25// helper definition for C override of C++ new
26typedef struct mi_nothrow_s { int _tag; } mi_nothrow_t;
27
28// ------------------------------------------------------
29// Override system malloc
30// ------------------------------------------------------
31
32#if (defined(__GNUC__) || defined(__clang__)) && !defined(__APPLE__) && !MI_TRACK_ENABLED
33 // gcc, clang: use aliasing to alias the exported function to one of our `mi_` functions
34 #if (defined(__GNUC__) && __GNUC__ >= 9)
35 #pragma GCC diagnostic ignored "-Wattributes" // or we get warnings that nodiscard is ignored on a forward
36 #define MI_FORWARD(fun) __attribute__((alias(#fun), used, visibility("default"), copy(fun)));
37 #else
38 #define MI_FORWARD(fun) __attribute__((alias(#fun), used, visibility("default")));
39 #endif
40 #define MI_FORWARD1(fun,x) MI_FORWARD(fun)
41 #define MI_FORWARD2(fun,x,y) MI_FORWARD(fun)
42 #define MI_FORWARD3(fun,x,y,z) MI_FORWARD(fun)
43 #define MI_FORWARD0(fun,x) MI_FORWARD(fun)
44 #define MI_FORWARD02(fun,x,y) MI_FORWARD(fun)
45#else
46 // otherwise use forwarding by calling our `mi_` function
47 #define MI_FORWARD1(fun,x) { return fun(x); }
48 #define MI_FORWARD2(fun,x,y) { return fun(x,y); }
49 #define MI_FORWARD3(fun,x,y,z) { return fun(x,y,z); }
50 #define MI_FORWARD0(fun,x) { fun(x); }
51 #define MI_FORWARD02(fun,x,y) { fun(x,y); }
52#endif
53
54#if defined(__APPLE__) && defined(MI_SHARED_LIB_EXPORT) && defined(MI_OSX_INTERPOSE)
55 // define MI_OSX_IS_INTERPOSED as we should not provide forwarding definitions for
56 // functions that are interposed (or the interposing does not work)
57 #define MI_OSX_IS_INTERPOSED
58
59 // use interposing so `DYLD_INSERT_LIBRARIES` works without `DYLD_FORCE_FLAT_NAMESPACE=1`
60 // See: <https://books.google.com/books?id=K8vUkpOXhN4C&pg=PA73>
61 struct mi_interpose_s {
62 const void* replacement;
63 const void* target;
64 };
65 #define MI_INTERPOSE_FUN(oldfun,newfun) { (const void*)&newfun, (const void*)&oldfun }
66 #define MI_INTERPOSE_MI(fun) MI_INTERPOSE_FUN(fun,mi_##fun)
67
68 __attribute__((used)) static struct mi_interpose_s _mi_interposes[] __attribute__((section("__DATA, __interpose"))) =
69 {
70 MI_INTERPOSE_MI(malloc),
71 MI_INTERPOSE_MI(calloc),
72 MI_INTERPOSE_MI(realloc),
73 MI_INTERPOSE_MI(strdup),
74 MI_INTERPOSE_MI(strndup),
75 MI_INTERPOSE_MI(realpath),
76 MI_INTERPOSE_MI(posix_memalign),
77 MI_INTERPOSE_MI(reallocf),
78 MI_INTERPOSE_MI(valloc),
79 MI_INTERPOSE_MI(malloc_size),
80 MI_INTERPOSE_MI(malloc_good_size),
81 #if defined(MAC_OS_X_VERSION_10_15) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_15
82 MI_INTERPOSE_MI(aligned_alloc),
83 #endif
84 #ifdef MI_OSX_ZONE
85 // we interpose malloc_default_zone in alloc-override-osx.c so we can use mi_free safely
86 MI_INTERPOSE_MI(free),
87 MI_INTERPOSE_FUN(vfree,mi_free),
88 #else
89 // 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>)
90 MI_INTERPOSE_FUN(free,mi_cfree), // use safe free that checks if pointers are from us
91 MI_INTERPOSE_FUN(vfree,mi_cfree),
92 #endif
93 };
94
95 #ifdef __cplusplus
96 extern "C" {
97 #endif
98 void _ZdlPv(void* p); // delete
99 void _ZdaPv(void* p); // delete[]
100 void _ZdlPvm(void* p, size_t n); // delete
101 void _ZdaPvm(void* p, size_t n); // delete[]
102 void* _Znwm(size_t n); // new
103 void* _Znam(size_t n); // new[]
104 void* _ZnwmRKSt9nothrow_t(size_t n, mi_nothrow_t tag); // new nothrow
105 void* _ZnamRKSt9nothrow_t(size_t n, mi_nothrow_t tag); // new[] nothrow
106 #ifdef __cplusplus
107 }
108 #endif
109 __attribute__((used)) static struct mi_interpose_s _mi_cxx_interposes[] __attribute__((section("__DATA, __interpose"))) =
110 {
111 MI_INTERPOSE_FUN(_ZdlPv,mi_free),
112 MI_INTERPOSE_FUN(_ZdaPv,mi_free),
113 MI_INTERPOSE_FUN(_ZdlPvm,mi_free_size),
114 MI_INTERPOSE_FUN(_ZdaPvm,mi_free_size),
115 MI_INTERPOSE_FUN(_Znwm,mi_new),
116 MI_INTERPOSE_FUN(_Znam,mi_new),
117 MI_INTERPOSE_FUN(_ZnwmRKSt9nothrow_t,mi_new_nothrow),
118 MI_INTERPOSE_FUN(_ZnamRKSt9nothrow_t,mi_new_nothrow),
119 };
120
121#elif defined(_MSC_VER)
122 // cannot override malloc unless using a dll.
123 // we just override new/delete which does work in a static library.
124#else
125 // On all other systems forward to our API
126 mi_decl_export void* malloc(size_t size) MI_FORWARD1(mi_malloc, size)
127 mi_decl_export void* calloc(size_t size, size_t n) MI_FORWARD2(mi_calloc, size, n)
128 mi_decl_export void* realloc(void* p, size_t newsize) MI_FORWARD2(mi_realloc, p, newsize)
129 mi_decl_export void free(void* p) MI_FORWARD0(mi_free, p)
130#endif
131
132#if (defined(__GNUC__) || defined(__clang__)) && !defined(__APPLE__)
133#pragma GCC visibility push(default)
134#endif
135
136// ------------------------------------------------------
137// Override new/delete
138// This is not really necessary as they usually call
139// malloc/free anyway, but it improves performance.
140// ------------------------------------------------------
141#ifdef __cplusplus
142 // ------------------------------------------------------
143 // With a C++ compiler we override the new/delete operators.
144 // see <https://en.cppreference.com/w/cpp/memory/new/operator_new>
145 // ------------------------------------------------------
146 #include <new>
147
148 #ifndef MI_OSX_IS_INTERPOSED
149 void operator delete(void* p) noexcept MI_FORWARD0(mi_free,p)
150 void operator delete[](void* p) noexcept MI_FORWARD0(mi_free,p)
151
152 void* operator new(std::size_t n) noexcept(false) MI_FORWARD1(mi_new,n)
153 void* operator new[](std::size_t n) noexcept(false) MI_FORWARD1(mi_new,n)
154
155 void* operator new (std::size_t n, const std::nothrow_t& tag) noexcept { MI_UNUSED(tag); return mi_new_nothrow(n); }
156 void* operator new[](std::size_t n, const std::nothrow_t& tag) noexcept { MI_UNUSED(tag); return mi_new_nothrow(n); }
157
158 #if (__cplusplus >= 201402L || _MSC_VER >= 1916)
159 void operator delete (void* p, std::size_t n) noexcept MI_FORWARD02(mi_free_size,p,n)
160 void operator delete[](void* p, std::size_t n) noexcept MI_FORWARD02(mi_free_size,p,n)
161 #endif
162 #endif
163
164 #if (__cplusplus > 201402L && defined(__cpp_aligned_new)) && (!defined(__GNUC__) || (__GNUC__ > 5))
165 void operator delete (void* p, std::align_val_t al) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }
166 void operator delete[](void* p, std::align_val_t al) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }
167 void operator delete (void* p, std::size_t n, std::align_val_t al) noexcept { mi_free_size_aligned(p, n, static_cast<size_t>(al)); };
168 void operator delete[](void* p, std::size_t n, std::align_val_t al) noexcept { mi_free_size_aligned(p, n, static_cast<size_t>(al)); };
169 void operator delete (void* p, std::align_val_t al, const std::nothrow_t&) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }
170 void operator delete[](void* p, std::align_val_t al, const std::nothrow_t&) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }
171
172 void* operator new( std::size_t n, std::align_val_t al) noexcept(false) { return mi_new_aligned(n, static_cast<size_t>(al)); }
173 void* operator new[]( std::size_t n, std::align_val_t al) noexcept(false) { return mi_new_aligned(n, static_cast<size_t>(al)); }
174 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)); }
175 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)); }
176 #endif
177
178#elif (defined(__GNUC__) || defined(__clang__))
179 // ------------------------------------------------------
180 // Override by defining the mangled C++ names of the operators (as
181 // used by GCC and CLang).
182 // See <https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling>
183 // ------------------------------------------------------
184
185 void _ZdlPv(void* p) MI_FORWARD0(mi_free,p) // delete
186 void _ZdaPv(void* p) MI_FORWARD0(mi_free,p) // delete[]
187 void _ZdlPvm(void* p, size_t n) MI_FORWARD02(mi_free_size,p,n)
188 void _ZdaPvm(void* p, size_t n) MI_FORWARD02(mi_free_size,p,n)
189 void _ZdlPvSt11align_val_t(void* p, size_t al) { mi_free_aligned(p,al); }
190 void _ZdaPvSt11align_val_t(void* p, size_t al) { mi_free_aligned(p,al); }
191 void _ZdlPvmSt11align_val_t(void* p, size_t n, size_t al) { mi_free_size_aligned(p,n,al); }
192 void _ZdaPvmSt11align_val_t(void* p, size_t n, size_t al) { mi_free_size_aligned(p,n,al); }
193
194 #if (MI_INTPTR_SIZE==8)
195 void* _Znwm(size_t n) MI_FORWARD1(mi_new,n) // new 64-bit
196 void* _Znam(size_t n) MI_FORWARD1(mi_new,n) // new[] 64-bit
197 void* _ZnwmRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_nothrow(n); }
198 void* _ZnamRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_nothrow(n); }
199 void* _ZnwmSt11align_val_t(size_t n, size_t al) MI_FORWARD2(mi_new_aligned, n, al)
200 void* _ZnamSt11align_val_t(size_t n, size_t al) MI_FORWARD2(mi_new_aligned, n, al)
201 void* _ZnwmSt11align_val_tRKSt9nothrow_t(size_t n, size_t al, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_aligned_nothrow(n,al); }
202 void* _ZnamSt11align_val_tRKSt9nothrow_t(size_t n, size_t al, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_aligned_nothrow(n,al); }
203 #elif (MI_INTPTR_SIZE==4)
204 void* _Znwj(size_t n) MI_FORWARD1(mi_new,n) // new 64-bit
205 void* _Znaj(size_t n) MI_FORWARD1(mi_new,n) // new[] 64-bit
206 void* _ZnwjRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_nothrow(n); }
207 void* _ZnajRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_nothrow(n); }
208 void* _ZnwjSt11align_val_t(size_t n, size_t al) MI_FORWARD2(mi_new_aligned, n, al)
209 void* _ZnajSt11align_val_t(size_t n, size_t al) MI_FORWARD2(mi_new_aligned, n, al)
210 void* _ZnwjSt11align_val_tRKSt9nothrow_t(size_t n, size_t al, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_aligned_nothrow(n,al); }
211 void* _ZnajSt11align_val_tRKSt9nothrow_t(size_t n, size_t al, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_aligned_nothrow(n,al); }
212 #else
213 #error "define overloads for new/delete for this platform (just for performance, can be skipped)"
214 #endif
215#endif // __cplusplus
216
217// ------------------------------------------------------
218// Further Posix & Unix functions definitions
219// ------------------------------------------------------
220
221#ifdef __cplusplus
222extern "C" {
223#endif
224
225#ifndef MI_OSX_IS_INTERPOSED
226 // Forward Posix/Unix calls as well
227 void* reallocf(void* p, size_t newsize) MI_FORWARD2(mi_reallocf,p,newsize)
228 size_t malloc_size(const void* p) MI_FORWARD1(mi_usable_size,p)
229 #if !defined(__ANDROID__) && !defined(__FreeBSD__)
230 size_t malloc_usable_size(void *p) MI_FORWARD1(mi_usable_size,p)
231 #else
232 size_t malloc_usable_size(const void *p) MI_FORWARD1(mi_usable_size,p)
233 #endif
234
235 // No forwarding here due to aliasing/name mangling issues
236 void* valloc(size_t size) { return mi_valloc(size); }
237 void vfree(void* p) { mi_free(p); }
238 size_t malloc_good_size(size_t size) { return mi_malloc_good_size(size); }
239 int posix_memalign(void** p, size_t alignment, size_t size) { return mi_posix_memalign(p, alignment, size); }
240
241 // `aligned_alloc` is only available when __USE_ISOC11 is defined.
242 // Note: Conda has a custom glibc where `aligned_alloc` is declared `static inline` and we cannot
243 // override it, but both _ISOC11_SOURCE and __USE_ISOC11 are undefined in Conda GCC7 or GCC9.
244 // Fortunately, in the case where `aligned_alloc` is declared as `static inline` it
245 // uses internally `memalign`, `posix_memalign`, or `_aligned_malloc` so we can avoid overriding it ourselves.
246 #if __USE_ISOC11
247 void* aligned_alloc(size_t alignment, size_t size) { return mi_aligned_alloc(alignment, size); }
248 #endif
249#endif
250
251// no forwarding here due to aliasing/name mangling issues
252void cfree(void* p) { mi_free(p); }
253void* pvalloc(size_t size) { return mi_pvalloc(size); }
254void* reallocarray(void* p, size_t count, size_t size) { return mi_reallocarray(p, count, size); }
255int reallocarr(void* p, size_t count, size_t size) { return mi_reallocarr(p, count, size); }
256void* memalign(size_t alignment, size_t size) { return mi_memalign(alignment, size); }
257void* _aligned_malloc(size_t alignment, size_t size) { return mi_aligned_alloc(alignment, size); }
258
259#if defined(__GLIBC__) && defined(__linux__)
260 // forward __libc interface (needed for glibc-based Linux distributions)
261 void* __libc_malloc(size_t size) MI_FORWARD1(mi_malloc,size)
262 void* __libc_calloc(size_t count, size_t size) MI_FORWARD2(mi_calloc,count,size)
263 void* __libc_realloc(void* p, size_t size) MI_FORWARD2(mi_realloc,p,size)
264 void __libc_free(void* p) MI_FORWARD0(mi_free,p)
265 void __libc_cfree(void* p) MI_FORWARD0(mi_free,p)
266
267 void* __libc_valloc(size_t size) { return mi_valloc(size); }
268 void* __libc_pvalloc(size_t size) { return mi_pvalloc(size); }
269 void* __libc_memalign(size_t alignment, size_t size) { return mi_memalign(alignment,size); }
270 int __posix_memalign(void** p, size_t alignment, size_t size) { return mi_posix_memalign(p,alignment,size); }
271#endif
272
273#ifdef __cplusplus
274}
275#endif
276
277#if (defined(__GNUC__) || defined(__clang__)) && !defined(__APPLE__)
278#pragma GCC visibility pop
279#endif
280
281#endif // MI_MALLOC_OVERRIDE && !_WIN32
282