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