Path: blob/main/system/lib/mimalloc/src/alloc-override.c
6175 views
/* ----------------------------------------------------------------------------1Copyright (c) 2018-2021, Microsoft Research, Daan Leijen2This is free software; you can redistribute it and/or modify it under the3terms of the MIT license. A copy of the license can be found in the file4"LICENSE" at the root of this distribution.5-----------------------------------------------------------------------------*/67#if !defined(MI_IN_ALLOC_C)8#error "this file should be included from 'alloc.c' (so aliases can work)"9#endif1011#if defined(MI_MALLOC_OVERRIDE) && defined(_WIN32) && !(defined(MI_SHARED_LIB) && defined(_DLL))12#error "It is only possible to override "malloc" on Windows when building as a DLL (and linking the C runtime as a DLL)"13#endif1415#if defined(MI_MALLOC_OVERRIDE) && !(defined(_WIN32))1617#if defined(__APPLE__)18#include <AvailabilityMacros.h>19mi_decl_externc void vfree(void* p);20mi_decl_externc size_t malloc_size(const void* p);21mi_decl_externc size_t malloc_good_size(size_t size);22#endif2324// helper definition for C override of C++ new25typedef void* mi_nothrow_t;2627// ------------------------------------------------------28// Override system malloc29// ------------------------------------------------------3031#if (defined(__GNUC__) || defined(__clang__)) && !defined(__APPLE__) && !MI_TRACK_ENABLED32// gcc, clang: use aliasing to alias the exported function to one of our `mi_` functions33#if (defined(__GNUC__) && __GNUC__ >= 9)34#pragma GCC diagnostic ignored "-Wattributes" // or we get warnings that nodiscard is ignored on a forward35#define MI_FORWARD(fun) __attribute__((alias(#fun), used, visibility("default"), copy(fun)));36#else37// XXX EMSCRIPTEN: Add "weak"38#define MI_FORWARD(fun) __attribute__((alias(#fun), used, visibility("default"), weak));39#endif40#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#else46// otherwise use forwarding by calling our `mi_` function47#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#endif535455#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 for57// functions that are interposed (or the interposing does not work)58#define MI_OSX_IS_INTERPOSED5960mi_decl_externc size_t mi_malloc_size_checked(void *p) {61if (!mi_is_in_heap_region(p)) return 0;62return mi_usable_size(p);63}6465// use interposing so `DYLD_INSERT_LIBRARIES` works without `DYLD_FORCE_FLAT_NAMESPACE=1`66// See: <https://books.google.com/books?id=K8vUkpOXhN4C&pg=PA73>67struct mi_interpose_s {68const void* replacement;69const 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)7374__attribute__((used)) static struct mi_interpose_s _mi_interposes[] __attribute__((section("__DATA, __interpose"))) =75{76MI_INTERPOSE_MI(malloc),77MI_INTERPOSE_MI(calloc),78MI_INTERPOSE_MI(realloc),79MI_INTERPOSE_MI(strdup),80#if defined(MAC_OS_X_VERSION_10_7) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_781MI_INTERPOSE_MI(strndup),82#endif83MI_INTERPOSE_MI(realpath),84MI_INTERPOSE_MI(posix_memalign),85MI_INTERPOSE_MI(reallocf),86MI_INTERPOSE_MI(valloc),87MI_INTERPOSE_FUN(malloc_size,mi_malloc_size_checked),88MI_INTERPOSE_MI(malloc_good_size),89#if defined(MAC_OS_X_VERSION_10_15) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_1590MI_INTERPOSE_MI(aligned_alloc),91#endif92#ifdef MI_OSX_ZONE93// we interpose malloc_default_zone in alloc-override-osx.c so we can use mi_free safely94MI_INTERPOSE_MI(free),95MI_INTERPOSE_FUN(vfree,mi_free),96#else97// 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>)98MI_INTERPOSE_FUN(free,mi_cfree), // use safe free that checks if pointers are from us99MI_INTERPOSE_FUN(vfree,mi_cfree),100#endif101};102103#ifdef __cplusplus104extern "C" {105#endif106void _ZdlPv(void* p); // delete107void _ZdaPv(void* p); // delete[]108void _ZdlPvm(void* p, size_t n); // delete109void _ZdaPvm(void* p, size_t n); // delete[]110void* _Znwm(size_t n); // new111void* _Znam(size_t n); // new[]112void* _ZnwmRKSt9nothrow_t(size_t n, mi_nothrow_t tag); // new nothrow113void* _ZnamRKSt9nothrow_t(size_t n, mi_nothrow_t tag); // new[] nothrow114#ifdef __cplusplus115}116#endif117__attribute__((used)) static struct mi_interpose_s _mi_cxx_interposes[] __attribute__((section("__DATA, __interpose"))) =118{119MI_INTERPOSE_FUN(_ZdlPv,mi_free),120MI_INTERPOSE_FUN(_ZdaPv,mi_free),121MI_INTERPOSE_FUN(_ZdlPvm,mi_free_size),122MI_INTERPOSE_FUN(_ZdaPvm,mi_free_size),123MI_INTERPOSE_FUN(_Znwm,mi_new),124MI_INTERPOSE_FUN(_Znam,mi_new),125MI_INTERPOSE_FUN(_ZnwmRKSt9nothrow_t,mi_new_nothrow),126MI_INTERPOSE_FUN(_ZnamRKSt9nothrow_t,mi_new_nothrow),127};128129#elif defined(_MSC_VER)130// cannot override malloc unless using a dll.131// we just override new/delete which does work in a static library.132#else133// On all other systems forward allocation primitives to our API134mi_decl_export void* malloc(size_t size) MI_FORWARD1(mi_malloc, size)135mi_decl_export void* calloc(size_t size, size_t n) MI_FORWARD2(mi_calloc, size, n)136mi_decl_export void* realloc(void* p, size_t newsize) MI_FORWARD2(mi_realloc, p, newsize)137mi_decl_export void free(void* p) MI_FORWARD0(mi_free, p)138// 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)139// We only override if `strdup` is not a macro (as on some older libc's, see issue #885)140#if !defined(strdup)141mi_decl_export char* strdup(const char* str) MI_FORWARD1(mi_strdup, str)142#endif143#if !defined(strndup) && (!defined(__APPLE__) || (defined(MAC_OS_X_VERSION_10_7) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7))144mi_decl_export char* strndup(const char* str, size_t n) MI_FORWARD2(mi_strndup, str, n)145#endif146#endif147148#if (defined(__GNUC__) || defined(__clang__)) && !defined(__APPLE__)149#pragma GCC visibility push(default)150#endif151152// ------------------------------------------------------153// Override new/delete154// This is not really necessary as they usually call155// malloc/free anyway, but it improves performance.156// ------------------------------------------------------157#ifdef __cplusplus158// ------------------------------------------------------159// With a C++ compiler we override the new/delete operators.160// see <https://en.cppreference.com/w/cpp/memory/new/operator_new>161// ------------------------------------------------------162#include <new>163164#ifndef MI_OSX_IS_INTERPOSED165void operator delete(void* p) noexcept MI_FORWARD0(mi_free,p)166void operator delete[](void* p) noexcept MI_FORWARD0(mi_free,p)167168void* operator new(std::size_t n) noexcept(false) MI_FORWARD1(mi_new,n)169void* operator new[](std::size_t n) noexcept(false) MI_FORWARD1(mi_new,n)170171void* operator new (std::size_t n, const std::nothrow_t& tag) noexcept { MI_UNUSED(tag); return mi_new_nothrow(n); }172void* operator new[](std::size_t n, const std::nothrow_t& tag) noexcept { MI_UNUSED(tag); return mi_new_nothrow(n); }173174#if (__cplusplus >= 201402L || _MSC_VER >= 1916)175void operator delete (void* p, std::size_t n) noexcept MI_FORWARD02(mi_free_size,p,n)176void operator delete[](void* p, std::size_t n) noexcept MI_FORWARD02(mi_free_size,p,n)177#endif178#endif179180#if (__cplusplus > 201402L && defined(__cpp_aligned_new)) && (!defined(__GNUC__) || (__GNUC__ > 5))181void operator delete (void* p, std::align_val_t al) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }182void operator delete[](void* p, std::align_val_t al) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }183void 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)); };184void 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)); };185void operator delete (void* p, std::align_val_t al, const std::nothrow_t&) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }186void operator delete[](void* p, std::align_val_t al, const std::nothrow_t&) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }187188void* operator new( std::size_t n, std::align_val_t al) noexcept(false) { return mi_new_aligned(n, static_cast<size_t>(al)); }189void* operator new[]( std::size_t n, std::align_val_t al) noexcept(false) { return mi_new_aligned(n, static_cast<size_t>(al)); }190void* 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)); }191void* 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#endif193194#elif (defined(__GNUC__) || defined(__clang__))195// ------------------------------------------------------196// Override by defining the mangled C++ names of the operators (as197// used by GCC and CLang).198// See <https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling>199// ------------------------------------------------------200201void _ZdlPv(void* p) MI_FORWARD0(mi_free,p) // delete202void _ZdaPv(void* p) MI_FORWARD0(mi_free,p) // delete[]203void _ZdlPvm(void* p, size_t n) MI_FORWARD02(mi_free_size,p,n)204void _ZdaPvm(void* p, size_t n) MI_FORWARD02(mi_free_size,p,n)205206void _ZdlPvSt11align_val_t(void* p, size_t al) { mi_free_aligned(p,al); }207void _ZdaPvSt11align_val_t(void* p, size_t al) { mi_free_aligned(p,al); }208void _ZdlPvmSt11align_val_t(void* p, size_t n, size_t al) { mi_free_size_aligned(p,n,al); }209void _ZdaPvmSt11align_val_t(void* p, size_t n, size_t al) { mi_free_size_aligned(p,n,al); }210211void _ZdlPvRKSt9nothrow_t(void* p, mi_nothrow_t tag) { MI_UNUSED(tag); mi_free(p); } // operator delete(void*, std::nothrow_t const&)212void _ZdaPvRKSt9nothrow_t(void* p, mi_nothrow_t tag) { MI_UNUSED(tag); mi_free(p); } // operator delete[](void*, std::nothrow_t const&)213void _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&)214void _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&)215216#if (MI_INTPTR_SIZE==8)217void* _Znwm(size_t n) MI_FORWARD1(mi_new,n) // new 64-bit218void* _Znam(size_t n) MI_FORWARD1(mi_new,n) // new[] 64-bit219void* _ZnwmRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_nothrow(n); }220void* _ZnamRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_nothrow(n); }221void* _ZnwmSt11align_val_t(size_t n, size_t al) MI_FORWARD2(mi_new_aligned, n, al)222void* _ZnamSt11align_val_t(size_t n, size_t al) MI_FORWARD2(mi_new_aligned, n, al)223void* _ZnwmSt11align_val_tRKSt9nothrow_t(size_t n, size_t al, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_aligned_nothrow(n,al); }224void* _ZnamSt11align_val_tRKSt9nothrow_t(size_t n, size_t al, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_aligned_nothrow(n,al); }225#elif (MI_INTPTR_SIZE==4)226void* _Znwj(size_t n) MI_FORWARD1(mi_new,n) // new 64-bit227void* _Znaj(size_t n) MI_FORWARD1(mi_new,n) // new[] 64-bit228void* _ZnwjRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_nothrow(n); }229void* _ZnajRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_nothrow(n); }230void* _ZnwjSt11align_val_t(size_t n, size_t al) MI_FORWARD2(mi_new_aligned, n, al)231void* _ZnajSt11align_val_t(size_t n, size_t al) MI_FORWARD2(mi_new_aligned, n, al)232void* _ZnwjSt11align_val_tRKSt9nothrow_t(size_t n, size_t al, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_aligned_nothrow(n,al); }233void* _ZnajSt11align_val_tRKSt9nothrow_t(size_t n, size_t al, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_aligned_nothrow(n,al); }234#else235#error "define overloads for new/delete for this platform (just for performance, can be skipped)"236#endif237#endif // __cplusplus238239// ------------------------------------------------------240// Further Posix & Unix functions definitions241// ------------------------------------------------------242243#ifdef __cplusplus244extern "C" {245#endif246247#ifndef MI_OSX_IS_INTERPOSED248// Forward Posix/Unix calls as well249void* reallocf(void* p, size_t newsize) MI_FORWARD2(mi_reallocf,p,newsize)250size_t malloc_size(const void* p) MI_FORWARD1(mi_usable_size,p)251#if !defined(__ANDROID__) && !defined(__FreeBSD__)252size_t malloc_usable_size(void *p) MI_FORWARD1(mi_usable_size,p)253#else254size_t malloc_usable_size(const void *p) MI_FORWARD1(mi_usable_size,p)255#endif256257// No forwarding here due to aliasing/name mangling issues258mi_decl_weak // XXX EMSCRIPTEN259void* valloc(size_t size) { return mi_valloc(size); }260void vfree(void* p) { mi_free(p); }261size_t malloc_good_size(size_t size) { return mi_malloc_good_size(size); }262mi_decl_weak // XXX EMSCRIPTEN263int posix_memalign(void** p, size_t alignment, size_t size) { return mi_posix_memalign(p, alignment, size); }264265// `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 check267// for it if using glibc.268// Note: Conda has a custom glibc where `aligned_alloc` is declared `static inline` and we cannot269// 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` it271// uses internally `memalign`, `posix_memalign`, or `_aligned_malloc` so we can avoid overriding it ourselves.272#if !defined(__GLIBC__) || __USE_ISOC11273mi_decl_weak // XXX EMSCRIPTEN274void* aligned_alloc(size_t alignment, size_t size) { return mi_aligned_alloc(alignment, size); }275#endif276#endif277278// no forwarding here due to aliasing/name mangling issues279void cfree(void* p) { mi_free(p); }280void* pvalloc(size_t size) { return mi_pvalloc(size); }281mi_decl_weak // XXX EMSCRIPTEN282void* memalign(size_t alignment, size_t size) { return mi_memalign(alignment, size); }283void* _aligned_malloc(size_t alignment, size_t size) { return mi_aligned_alloc(alignment, size); }284mi_decl_weak // XXX EMSCRIPTEN285void* reallocarray(void* p, size_t count, size_t size) { return mi_reallocarray(p, count, size); }286// some systems define reallocarr so mark it as a weak symbol (#751)287mi_decl_weak int reallocarr(void* p, size_t count, size_t size) { return mi_reallocarr(p, count, size); }288289#if defined(__wasi__)290// forward __libc interface (see PR #667)291void* __libc_malloc(size_t size) MI_FORWARD1(mi_malloc, size)292void* __libc_calloc(size_t count, size_t size) MI_FORWARD2(mi_calloc, count, size)293void* __libc_realloc(void* p, size_t size) MI_FORWARD2(mi_realloc, p, size)294void __libc_free(void* p) MI_FORWARD0(mi_free, p)295mi_decl_weak // XXX EMSCRIPTEN296void* __libc_memalign(size_t alignment, size_t size) { return mi_memalign(alignment, size); }297298#ifdef __EMSCRIPTEN__ // emscripten adds some more on top of WASI299void* emscripten_builtin_malloc(size_t size) MI_FORWARD1(mi_malloc, size)300void* emscripten_builtin_realloc(void* p, size_t size) MI_FORWARD2(mi_realloc, p, size)301void* emscripten_builtin_free(void* p) MI_FORWARD0(mi_free, p)302void* emscripten_builtin_memalign(size_t alignment, size_t size) { return mi_memalign(alignment, size); }303void* emscripten_builtin_calloc(size_t nmemb, size_t size) MI_FORWARD2(mi_calloc, nmemb, size)304#endif305306#elif defined(__GLIBC__) && defined(__linux__)307// forward __libc interface (needed for glibc-based Linux distributions)308void* __libc_malloc(size_t size) MI_FORWARD1(mi_malloc,size)309void* __libc_calloc(size_t count, size_t size) MI_FORWARD2(mi_calloc,count,size)310void* __libc_realloc(void* p, size_t size) MI_FORWARD2(mi_realloc,p,size)311void __libc_free(void* p) MI_FORWARD0(mi_free,p)312void __libc_cfree(void* p) MI_FORWARD0(mi_free,p)313314void* __libc_valloc(size_t size) { return mi_valloc(size); }315void* __libc_pvalloc(size_t size) { return mi_pvalloc(size); }316void* __libc_memalign(size_t alignment, size_t size) { return mi_memalign(alignment,size); }317int __posix_memalign(void** p, size_t alignment, size_t size) { return mi_posix_memalign(p,alignment,size); }318#endif319320#ifdef __cplusplus321}322#endif323324#if (defined(__GNUC__) || defined(__clang__)) && !defined(__APPLE__)325#pragma GCC visibility pop326#endif327328#endif // MI_MALLOC_OVERRIDE && !_WIN32329330331