]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUG/MINOR: pools: restore detection of built-in allocator
authorWilly Tarreau <w@1wt.eu>
Wed, 22 Mar 2023 16:52:05 +0000 (17:52 +0100)
committerWilly Tarreau <w@1wt.eu>
Wed, 22 Mar 2023 16:57:13 +0000 (17:57 +0100)
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

index 8bc4805c465cf96728e44156c4c55f41317ec852..07b122a61a27a6f70f68799bc6c87f932fe610a2 100644 (file)
@@ -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)