From 53b88f9534ab0d0bafb5161bd47130d3bcee2263 Mon Sep 17 00:00:00 2001 From: Matthew Parkinson Date: Mon, 27 Feb 2023 11:18:40 +0000 Subject: [PATCH] Alter glibc override to work with RTL_DEEPBIND When using dlopen with RTL_DEEPBIND the LD_PRELOAD used by the majority of allocators does not work as both libc and snmallocs allocators can be called by various dso. This patch uses GLIBC's __malloc_hook to override the allocator as well as LD_PRELOAD. This means that all libraries will call snmalloc when performing allocation. --- src/snmalloc/override/malloc.cc | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/snmalloc/override/malloc.cc b/src/snmalloc/override/malloc.cc index 14e770b47..512ba3dce 100644 --- a/src/snmalloc/override/malloc.cc +++ b/src/snmalloc/override/malloc.cc @@ -227,4 +227,22 @@ extern "C" return SNMALLOC_NAME_MANGLE(memalign)( OS_PAGE_SIZE, (size + OS_PAGE_SIZE - 1) & ~(OS_PAGE_SIZE - 1)); } + +#if __has_include() +# include +#endif +#if defined(__GLIBC__) && !defined(SNMALLOC_PASS_THROUGH) + // glibc uses these hooks to replace malloc. + // This is required when RTL_DEEPBIND is used and the library is + // LD_PRELOADed. + // See https://github.com/microsoft/snmalloc/issues/595 + SNMALLOC_EXPORT void (*SNMALLOC_NAME_MANGLE(__free_hook))(void* ptr) = + &SNMALLOC_NAME_MANGLE(free); + SNMALLOC_EXPORT void* (*SNMALLOC_NAME_MANGLE(__malloc_hook))(size_t size) = + &SNMALLOC_NAME_MANGLE(malloc); + SNMALLOC_EXPORT void* (*SNMALLOC_NAME_MANGLE(__realloc_hook))( + void* ptr, size_t size) = &SNMALLOC_NAME_MANGLE(realloc); + SNMALLOC_EXPORT void* (*SNMALLOC_NAME_MANGLE(__memalign_hook))( + size_t alignment, size_t size) = &SNMALLOC_NAME_MANGLE(memalign); +#endif }