From 41a0ee10717a5afaa2bfdd0225245feecc09eea9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Thu, 21 Dec 2023 11:12:54 +0100 Subject: [PATCH] Add workaround for jemalloc linking order 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 | 12 ++++++++++-- lib/isc/include/isc/mem.h | 19 +++++++++++++++++++ lib/isc/mem.c | 2 ++ tests/libtest/dns.c | 7 ++----- 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/Makefile.top b/Makefile.top index d01317dadb..da95a2d47a 100644 --- a/Makefile.top +++ b/Makefile.top @@ -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 diff --git a/lib/isc/include/isc/mem.h b/lib/isc/include/isc/mem.h index 6c099d8742..b7143bdbdd 100644 --- a/lib/isc/include/isc/mem.h +++ b/lib/isc/include/isc/mem.h @@ -23,6 +23,7 @@ #include #include #include +#include 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 + +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); diff --git a/lib/isc/mem.c b/lib/isc/mem.c index e43ae4fc03..c23a39075e 100644 --- a/lib/isc/mem.c +++ b/lib/isc/mem.c @@ -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. */ diff --git a/tests/libtest/dns.c b/tests/libtest/dns.c index 73fb630807..29ac69e5fa 100644 --- a/tests/libtest/dns.c +++ b/tests/libtest/dns.c @@ -24,9 +24,6 @@ #include #include -#define UNIT_TESTING -#include - #include #include #include @@ -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); }