microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
73ca361d760b28bb5a9c231f916024b08f884fae

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

source/allocator/mimalloc-sys/mimalloc/src/alloc-override.c

316lines · modecode

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 void* 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
55#if defined(__APPLE__) && defined(MI_SHARED_LIB_EXPORT) && defined(MI_OSX_INTERPOSE)
56 // define MI_OSX_IS_INTERPOSED as we should not provide forwarding definitions for
57 // functions that are interposed (or the interposing does not work)
58 #define MI_OSX_IS_INTERPOSED
59
60 mi_decl_externc size_t mi_malloc_size_checked(void *p) {
61 if (!mi_is_in_heap_region(p)) return 0;
62 return mi_usable_size(p);
63 }
64
65 // use interposing so `DYLD_INSERT_LIBRARIES` works without `DYLD_FORCE_FLAT_NAMESPACE=1`
66 // See: <https://books.google.com/books?id=K8vUkpOXhN4C&pg=PA73>
67 struct mi_interpose_s {
68 const void* replacement;
69 const void* target;
70 };
71 #define MI_INTERPOSE_FUN(oldfun,newfun) { (const void*)&newfun, (const void*)&oldfun }
72 #define MI_INTERPOSE_MI(fun) MI_INTERPOSE_FUN(fun,mi_##fun)
73
74 #define MI_INTERPOSE_DECLS(name) __attribute__((used)) static struct mi_interpose_s name[] __attribute__((section("__DATA, __interpose")))
75
76 MI_INTERPOSE_DECLS(_mi_interposes) =
77 {
78 MI_INTERPOSE_MI(malloc),
79 MI_INTERPOSE_MI(calloc),
80 MI_INTERPOSE_MI(realloc),
81 MI_INTERPOSE_MI(strdup),
82 MI_INTERPOSE_MI(realpath),
83 MI_INTERPOSE_MI(posix_memalign),
84 MI_INTERPOSE_MI(reallocf),
85 MI_INTERPOSE_MI(valloc),
86 MI_INTERPOSE_FUN(malloc_size,mi_malloc_size_checked),
87 MI_INTERPOSE_MI(malloc_good_size),
88 #ifdef MI_OSX_ZONE
89 // we interpose malloc_default_zone in alloc-override-osx.c so we can use mi_free safely
90 MI_INTERPOSE_MI(free),
91 MI_INTERPOSE_FUN(vfree,mi_free),
92 #else
93 // 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>)
94 MI_INTERPOSE_FUN(free,mi_cfree), // use safe free that checks if pointers are from us
95 MI_INTERPOSE_FUN(vfree,mi_cfree),
96 #endif
97 };
98 MI_INTERPOSE_DECLS(_mi_interposes_10_7) __OSX_AVAILABLE(10.7) = {
99 MI_INTERPOSE_MI(strndup),
100 };
101 MI_INTERPOSE_DECLS(_mi_interposes_10_15) __OSX_AVAILABLE(10.15) = {
102 MI_INTERPOSE_MI(aligned_alloc),
103 };
104
105 #ifdef __cplusplus
106 extern "C" {
107 #endif
108 void _ZdlPv(void* p); // delete
109 void _ZdaPv(void* p); // delete[]
110 void _ZdlPvm(void* p, size_t n); // delete
111 void _ZdaPvm(void* p, size_t n); // delete[]
112 void* _Znwm(size_t n); // new
113 void* _Znam(size_t n); // new[]
114 void* _ZnwmRKSt9nothrow_t(size_t n, mi_nothrow_t tag); // new nothrow
115 void* _ZnamRKSt9nothrow_t(size_t n, mi_nothrow_t tag); // new[] nothrow
116 #ifdef __cplusplus
117 }
118 #endif
119 __attribute__((used)) static struct mi_interpose_s _mi_cxx_interposes[] __attribute__((section("__DATA, __interpose"))) =
120 {
121 MI_INTERPOSE_FUN(_ZdlPv,mi_free),
122 MI_INTERPOSE_FUN(_ZdaPv,mi_free),
123 MI_INTERPOSE_FUN(_ZdlPvm,mi_free_size),
124 MI_INTERPOSE_FUN(_ZdaPvm,mi_free_size),
125 MI_INTERPOSE_FUN(_Znwm,mi_new),
126 MI_INTERPOSE_FUN(_Znam,mi_new),
127 MI_INTERPOSE_FUN(_ZnwmRKSt9nothrow_t,mi_new_nothrow),
128 MI_INTERPOSE_FUN(_ZnamRKSt9nothrow_t,mi_new_nothrow),
129 };
130
131#elif defined(_MSC_VER)
132 // cannot override malloc unless using a dll.
133 // we just override new/delete which does work in a static library.
134#else
135 // On all other systems forward allocation primitives to our API
136 mi_decl_export void* malloc(size_t size) MI_FORWARD1(mi_malloc, size)
137 mi_decl_export void* calloc(size_t size, size_t n) MI_FORWARD2(mi_calloc, size, n)
138 mi_decl_export void* realloc(void* p, size_t newsize) MI_FORWARD2(mi_realloc, p, newsize)
139 mi_decl_export void free(void* p) MI_FORWARD0(mi_free, p)
140 // In principle we do not need to forward `strdup`/`strndup` but on some systems these do not use `malloc` internally (but a more primitive call)
141 // We only override if `strdup` is not a macro (as on some older libc's, see issue #885)
142 #if !defined(strdup)
143 mi_decl_export char* strdup(const char* str) MI_FORWARD1(mi_strdup, str)
144 #endif
145 #if !defined(strndup) && (!defined(__APPLE__) || (defined(MAC_OS_X_VERSION_10_7) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7))
146 mi_decl_export char* strndup(const char* str, size_t n) MI_FORWARD2(mi_strndup, str, n)
147 #endif
148#endif
149
150#if (defined(__GNUC__) || defined(__clang__)) && !defined(__APPLE__)
151#pragma GCC visibility push(default)
152#endif
153
154// ------------------------------------------------------
155// Override new/delete
156// This is not really necessary as they usually call
157// malloc/free anyway, but it improves performance.
158// ------------------------------------------------------
159#ifdef __cplusplus
160 // ------------------------------------------------------
161 // With a C++ compiler we override the new/delete operators.
162 // see <https://en.cppreference.com/w/cpp/memory/new/operator_new>
163 // ------------------------------------------------------
164 #include <new>
165
166 #ifndef MI_OSX_IS_INTERPOSED
167 void operator delete(void* p) noexcept MI_FORWARD0(mi_free,p)
168 void operator delete[](void* p) noexcept MI_FORWARD0(mi_free,p)
169
170 void* operator new(std::size_t n) noexcept(false) MI_FORWARD1(mi_new,n)
171 void* operator new[](std::size_t n) noexcept(false) MI_FORWARD1(mi_new,n)
172
173 void* operator new (std::size_t n, const std::nothrow_t& tag) noexcept { MI_UNUSED(tag); return mi_new_nothrow(n); }
174 void* operator new[](std::size_t n, const std::nothrow_t& tag) noexcept { MI_UNUSED(tag); return mi_new_nothrow(n); }
175
176 #if (__cplusplus >= 201402L || _MSC_VER >= 1916)
177 void operator delete (void* p, std::size_t n) noexcept MI_FORWARD02(mi_free_size,p,n)
178 void operator delete[](void* p, std::size_t n) noexcept MI_FORWARD02(mi_free_size,p,n)
179 #endif
180 #endif
181
182 #if (__cplusplus > 201402L && defined(__cpp_aligned_new)) && (!defined(__GNUC__) || (__GNUC__ > 5))
183 void operator delete (void* p, std::align_val_t al) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }
184 void operator delete[](void* p, std::align_val_t al) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }
185 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)); };
186 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)); };
187 void operator delete (void* p, std::align_val_t al, const std::nothrow_t&) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }
188 void operator delete[](void* p, std::align_val_t al, const std::nothrow_t&) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }
189
190 void* operator new( std::size_t n, std::align_val_t al) noexcept(false) { return mi_new_aligned(n, static_cast<size_t>(al)); }
191 void* operator new[]( std::size_t n, std::align_val_t al) noexcept(false) { return mi_new_aligned(n, static_cast<size_t>(al)); }
192 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)); }
193 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)); }
194 #endif
195
196#elif (defined(__GNUC__) || defined(__clang__))
197 // ------------------------------------------------------
198 // Override by defining the mangled C++ names of the operators (as
199 // used by GCC and CLang).
200 // See <https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling>
201 // ------------------------------------------------------
202
203 void _ZdlPv(void* p) MI_FORWARD0(mi_free,p) // delete
204 void _ZdaPv(void* p) MI_FORWARD0(mi_free,p) // delete[]
205 void _ZdlPvm(void* p, size_t n) MI_FORWARD02(mi_free_size,p,n)
206 void _ZdaPvm(void* p, size_t n) MI_FORWARD02(mi_free_size,p,n)
207
208 void _ZdlPvSt11align_val_t(void* p, size_t al) { mi_free_aligned(p,al); }
209 void _ZdaPvSt11align_val_t(void* p, size_t al) { mi_free_aligned(p,al); }
210 void _ZdlPvmSt11align_val_t(void* p, size_t n, size_t al) { mi_free_size_aligned(p,n,al); }
211 void _ZdaPvmSt11align_val_t(void* p, size_t n, size_t al) { mi_free_size_aligned(p,n,al); }
212
213 void _ZdlPvRKSt9nothrow_t(void* p, mi_nothrow_t tag) { MI_UNUSED(tag); mi_free(p); } // operator delete(void*, std::nothrow_t const&)
214 void _ZdaPvRKSt9nothrow_t(void* p, mi_nothrow_t tag) { MI_UNUSED(tag); mi_free(p); } // operator delete[](void*, std::nothrow_t const&)
215 void _ZdlPvSt11align_val_tRKSt9nothrow_t(void* p, size_t al, mi_nothrow_t tag) { MI_UNUSED(tag); mi_free_aligned(p,al); } // operator delete(void*, std::align_val_t, std::nothrow_t const&)
216 void _ZdaPvSt11align_val_tRKSt9nothrow_t(void* p, size_t al, mi_nothrow_t tag) { MI_UNUSED(tag); mi_free_aligned(p,al); } // operator delete[](void*, std::align_val_t, std::nothrow_t const&)
217
218 #if (MI_INTPTR_SIZE==8)
219 void* _Znwm(size_t n) MI_FORWARD1(mi_new,n) // new 64-bit
220 void* _Znam(size_t n) MI_FORWARD1(mi_new,n) // new[] 64-bit
221 void* _ZnwmRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_nothrow(n); }
222 void* _ZnamRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_nothrow(n); }
223 void* _ZnwmSt11align_val_t(size_t n, size_t al) MI_FORWARD2(mi_new_aligned, n, al)
224 void* _ZnamSt11align_val_t(size_t n, size_t al) MI_FORWARD2(mi_new_aligned, n, al)
225 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); }
226 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); }
227 #elif (MI_INTPTR_SIZE==4)
228 void* _Znwj(size_t n) MI_FORWARD1(mi_new,n) // new 64-bit
229 void* _Znaj(size_t n) MI_FORWARD1(mi_new,n) // new[] 64-bit
230 void* _ZnwjRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_nothrow(n); }
231 void* _ZnajRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_nothrow(n); }
232 void* _ZnwjSt11align_val_t(size_t n, size_t al) MI_FORWARD2(mi_new_aligned, n, al)
233 void* _ZnajSt11align_val_t(size_t n, size_t al) MI_FORWARD2(mi_new_aligned, n, al)
234 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); }
235 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); }
236 #else
237 #error "define overloads for new/delete for this platform (just for performance, can be skipped)"
238 #endif
239#endif // __cplusplus
240
241// ------------------------------------------------------
242// Further Posix & Unix functions definitions
243// ------------------------------------------------------
244
245#ifdef __cplusplus
246extern "C" {
247#endif
248
249#ifndef MI_OSX_IS_INTERPOSED
250 // Forward Posix/Unix calls as well
251 void* reallocf(void* p, size_t newsize) MI_FORWARD2(mi_reallocf,p,newsize)
252 size_t malloc_size(const void* p) MI_FORWARD1(mi_usable_size,p)
253 #if !defined(__ANDROID__) && !defined(__FreeBSD__) && !defined(__DragonFly__)
254 size_t malloc_usable_size(void *p) MI_FORWARD1(mi_usable_size,p)
255 #else
256 size_t malloc_usable_size(const void *p) MI_FORWARD1(mi_usable_size,p)
257 #endif
258
259 // No forwarding here due to aliasing/name mangling issues
260 void* valloc(size_t size) { return mi_valloc(size); }
261 void vfree(void* p) { mi_free(p); }
262 size_t malloc_good_size(size_t size) { return mi_malloc_good_size(size); }
263 int posix_memalign(void** p, size_t alignment, size_t size) { return mi_posix_memalign(p, alignment, size); }
264
265 // `aligned_alloc` is only available when __USE_ISOC11 is defined.
266 // Note: it seems __USE_ISOC11 is not defined in musl (and perhaps other libc's) so we only check
267 // for it if using glibc.
268 // Note: Conda has a custom glibc where `aligned_alloc` is declared `static inline` and we cannot
269 // override it, but both _ISOC11_SOURCE and __USE_ISOC11 are undefined in Conda GCC7 or GCC9.
270 // Fortunately, in the case where `aligned_alloc` is declared as `static inline` it
271 // uses internally `memalign`, `posix_memalign`, or `_aligned_malloc` so we can avoid overriding it ourselves.
272 #if !defined(__GLIBC__) || __USE_ISOC11
273 void* aligned_alloc(size_t alignment, size_t size) { return mi_aligned_alloc(alignment, size); }
274 #endif
275#endif
276
277// no forwarding here due to aliasing/name mangling issues
278void cfree(void* p) { mi_free(p); }
279void* pvalloc(size_t size) { return mi_pvalloc(size); }
280void* memalign(size_t alignment, size_t size) { return mi_memalign(alignment, size); }
281void* _aligned_malloc(size_t alignment, size_t size) { return mi_aligned_alloc(alignment, size); }
282void* reallocarray(void* p, size_t count, size_t size) { return mi_reallocarray(p, count, size); }
283// some systems define reallocarr so mark it as a weak symbol (#751)
284mi_decl_weak int reallocarr(void* p, size_t count, size_t size) { return mi_reallocarr(p, count, size); }
285
286#if defined(__wasi__)
287 // forward __libc interface (see PR #667)
288 void* __libc_malloc(size_t size) MI_FORWARD1(mi_malloc, size)
289 void* __libc_calloc(size_t count, size_t size) MI_FORWARD2(mi_calloc, count, size)
290 void* __libc_realloc(void* p, size_t size) MI_FORWARD2(mi_realloc, p, size)
291 void __libc_free(void* p) MI_FORWARD0(mi_free, p)
292 void* __libc_memalign(size_t alignment, size_t size) { return mi_memalign(alignment, size); }
293
294#elif defined(__linux__)
295 // forward __libc interface (needed for glibc-based and musl-based Linux distributions)
296 void* __libc_malloc(size_t size) MI_FORWARD1(mi_malloc,size)
297 void* __libc_calloc(size_t count, size_t size) MI_FORWARD2(mi_calloc,count,size)
298 void* __libc_realloc(void* p, size_t size) MI_FORWARD2(mi_realloc,p,size)
299 void __libc_free(void* p) MI_FORWARD0(mi_free,p)
300 void __libc_cfree(void* p) MI_FORWARD0(mi_free,p)
301
302 void* __libc_valloc(size_t size) { return mi_valloc(size); }
303 void* __libc_pvalloc(size_t size) { return mi_pvalloc(size); }
304 void* __libc_memalign(size_t alignment, size_t size) { return mi_memalign(alignment,size); }
305 int __posix_memalign(void** p, size_t alignment, size_t size) { return mi_posix_memalign(p,alignment,size); }
306#endif
307
308#ifdef __cplusplus
309}
310#endif
311
312#if (defined(__GNUC__) || defined(__clang__)) && !defined(__APPLE__)
313#pragma GCC visibility pop
314#endif
315
316#endif // MI_MALLOC_OVERRIDE && !_WIN32
317