]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Add workaround for jemalloc linking order
authorOndřej Surý <ondrej@isc.org>
Thu, 21 Dec 2023 10:12:54 +0000 (11:12 +0100)
committerOndřej Surý <ondrej@isc.org>
Thu, 18 Jan 2024 08:34:36 +0000 (09:34 +0100)
Because we don't use jemalloc functions directly, but only via the
libisc library, the dynamic linker might pull the jemalloc library
too late when memory has been already allocated via standard libc
allocator.

Add a workaround round isc_mem_create() that makes the dynamic linker
to pull jemalloc earlier than libc.

Makefile.top
lib/isc/include/isc/mem.h
lib/isc/mem.c
tests/libtest/dns.c

index d01317dadb69f17922ffda8b7df15ab649c26af1..da95a2d47ac424d10ab3db798aa4730dc2b47b02 100644 (file)
@@ -23,12 +23,20 @@ AM_LDFLAGS +=                                       \
        -Wl,-flat_namespace
 endif HOST_MACOS
 
-LIBISC_CFLAGS =                                                \
+if HAVE_JEMALLOC
+LIBISC_CFLAGS = $(JEMALLOC_CFLAGS)
+LIBISC_LIBS = $(JEMALLOC_LIBS)
+else
+LIBISC_CFLAGS =
+LIBISC_LIBS =
+endif
+
+LIBISC_CFLAGS +=                                               \
        -I$(top_srcdir)/include                         \
        -I$(top_srcdir)/lib/isc/include                 \
        -I$(top_builddir)/lib/isc/include
 
-LIBISC_LIBS = $(top_builddir)/lib/isc/libisc.la
+LIBISC_LIBS += $(top_builddir)/lib/isc/libisc.la
 if HAVE_DTRACE
 LIBISC_DTRACE = $(top_builddir)/lib/isc/probes.lo
 endif
index 6c099d8742c059f6add72da51101d99a4acf59cc..b7143bdbddba7cb030b90546460ff7fe65bc1109 100644 (file)
@@ -23,6 +23,7 @@
 #include <isc/mutex.h>
 #include <isc/overflow.h>
 #include <isc/types.h>
+#include <isc/urcu.h>
 
 ISC_LANG_BEGINDECLS
 
@@ -183,7 +184,25 @@ extern unsigned int isc_mem_defaultflags;
        } while (0)
 
 /*@{*/
+/*
+ * This is a little hack to help with dynamic link order,
+ * see https://github.com/jemalloc/jemalloc/issues/2566
+ * for more information.
+ */
+#if HAVE_JEMALLOC
+#include <jemalloc/jemalloc.h>
+
+extern volatile void *isc__mem_malloc;
+
+#define isc_mem_create(cp)                                        \
+       {                                                         \
+               isc__mem_create((cp)_ISC_MEM_FILELINE);           \
+               isc__mem_malloc = mallocx;                        \
+               ISC_INSIST(CMM_ACCESS_ONCE(isc__mem_malloc) != NULL); \
+       }
+#else
 #define isc_mem_create(cp) isc__mem_create((cp)_ISC_MEM_FILELINE)
+#endif
 void
 isc__mem_create(isc_mem_t **_ISC_MEM_FLARG);
 
index e43ae4fc0333b58c70855a8e539dd07b29ce60e0..c23a39075e45cf198fa80f26a66d520ffa90d567 100644 (file)
@@ -69,6 +69,8 @@ unsigned int isc_mem_defaultflags = ISC_MEMFLAG_DEFAULT;
 
 #define ISC_MEM_ILLEGAL_ARENA (UINT_MAX)
 
+volatile void *isc__mem_malloc = mallocx;
+
 /*
  * Constants.
  */
index 73fb630807951e6dc478cb90cc5483b393834fec..29ac69e5facae8e367295e9a326a9a64a079e6a8 100644 (file)
@@ -24,9 +24,6 @@
 #include <time.h>
 #include <unistd.h>
 
-#define UNIT_TESTING
-#include <cmocka.h>
-
 #include <isc/buffer.h>
 #include <isc/file.h>
 #include <isc/hash.h>
@@ -264,7 +261,7 @@ dns_test_tohex(const unsigned char *data, size_t len, char *buf,
        memset(buf, 0, buflen);
        isc_buffer_init(&target, buf, buflen);
        result = isc_hex_totext((isc_region_t *)&source, 1, " ", &target);
-       assert_int_equal(result, ISC_R_SUCCESS);
+       INSIST(result == ISC_R_SUCCESS);
 
        return (buf);
 }
@@ -428,7 +425,7 @@ dns_test_namefromstring(const char *namestr, dns_fixedname_t *fname) {
 
        isc_buffer_putmem(b, (const unsigned char *)namestr, length);
        result = dns_name_fromtext(name, b, NULL, 0, NULL);
-       assert_int_equal(result, ISC_R_SUCCESS);
+       INSIST(result == ISC_R_SUCCESS);
 
        isc_buffer_free(&b);
 }