From 0c27ec5df73ba89c2c735c2b78cec784022effb6 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Wed, 22 Mar 2023 17:52:05 +0100 Subject: [PATCH] BUG/MINOR: pools: restore detection of built-in allocator The runtime detection of the default memory allocator was broken by Commit d8a97d8f6 ("BUG/MINOR: illegal use of the malloc_trim() function if jemalloc is used") due to a misunderstanding of its role. The purpose is not to detect whether we're on non-jemalloc but whether or not the allocator was changed from the one we booted with, in which case we must be extra cautious and absolutely refrain from calling malloc_trim() and its friends. This was done only to drop the message saying that malloc_trim() is supported, which will be totally removed in another commit, and could possibly be removed even in older versions if this patch would get backported since in the end it provides limited value. --- src/pool.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/pool.c b/src/pool.c index 8bc4805c46..07b122a61a 100644 --- a/src/pool.c +++ b/src/pool.c @@ -105,7 +105,7 @@ struct show_pools_ctx { }; static int mem_fail_rate __read_mostly = 0; -static int using_default_allocator __read_mostly = 1; +static int using_default_allocator __read_mostly = 1; // linked-in allocator or LD_PRELOADed one ? static int disable_trim __read_mostly = 0; static int(*my_mallctl)(const char *, void *, size_t *, void *, size_t) = NULL; static int(*_malloc_trim)(size_t) = NULL; @@ -144,10 +144,17 @@ static void detect_allocator(void) my_mallctl = mallctl; #endif - if (!my_mallctl) + if (!my_mallctl) { + /* trick: we won't enter here if mallctl() is known at link + * time. This allows to detect if the symbol was changed since + * the program was linked, indicating it's not running on the + * expected allocator (due to an LD_PRELOAD) and that we must + * be extra cautious and avoid some optimizations that are + * known to break such as malloc_trim(). + */ my_mallctl = get_sym_curr_addr("mallctl"); - - using_default_allocator = (my_mallctl == NULL); + using_default_allocator = (my_mallctl == NULL); + } if (!my_mallctl) { #if defined(HA_HAVE_MALLOC_TRIM)