From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from cross.elm.relay.mailchannels.net (cross.elm.relay.mailchannels.net [23.83.212.46]) by sourceware.org (Postfix) with ESMTPS id 2255A388F035 for ; Tue, 6 Jul 2021 18:09:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2255A388F035 X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id C2CBA342433; Tue, 6 Jul 2021 18:09:48 +0000 (UTC) Received: from pdx1-sub0-mail-a29.g.dreamhost.com (100-96-11-26.trex.outbound.svc.cluster.local [100.96.11.26]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 67357342453; Tue, 6 Jul 2021 18:09:47 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from pdx1-sub0-mail-a29.g.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384) by 100.96.11.26 (trex/6.3.3); Tue, 06 Jul 2021 18:09:48 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Trade-Sponge: 1bdde3833414a529_1625594988623_3852664636 X-MC-Loop-Signature: 1625594988623:2518783 X-MC-Ingress-Time: 1625594988623 Received: from pdx1-sub0-mail-a29.g.dreamhost.com (localhost [127.0.0.1]) by pdx1-sub0-mail-a29.g.dreamhost.com (Postfix) with ESMTP id 1BC527E6C2; Tue, 6 Jul 2021 11:09:47 -0700 (PDT) Received: from rhbox.intra.reserved-bit.com (unknown [1.186.101.110]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a29.g.dreamhost.com (Postfix) with ESMTPSA id 1AEBE7F02A; Tue, 6 Jul 2021 11:09:42 -0700 (PDT) X-DH-BACKEND: pdx1-sub0-mail-a29 From: Siddhesh Poyarekar To: libc-alpha@sourceware.org Cc: dj@redhat.com, carlos@redhat.com, fweimer@redhat.com Subject: [PATCH v6 1/8] Move malloc hooks into a compat DSO Date: Tue, 6 Jul 2021 23:39:17 +0530 Message-Id: <20210706180924.95047-2-siddhesh@sourceware.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210706180924.95047-1-siddhesh@sourceware.org> References: <20210706180924.95047-1-siddhesh@sourceware.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-3494.3 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_NONE, KAM_DMARC_STATUS, KAM_SHORT, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NEUTRAL, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 06 Jul 2021 18:09:55 -0000 Remove all malloc hook uses from core malloc functions and move it into a new library libc_malloc_debug.so. With this, the hooks now no longer have any effect on the core library. libc_malloc_debug.so is a malloc interposer that needs to be preloaded to get hooks functionality back so that the debugging features that depend on the hooks, i.e. malloc-check, mcheck and mtrace work again. Without the preloaded DSO these debugging features will be nops. These features will be ported away from hooks in subsequent patches. Similarly, legacy applications that need hooks functionality need to preload libc_malloc_debug.so. Finally, static binaries will no longer be able to use malloc debugging features since they cannot preload the debugging DSO. --- Makeconfig | 2 +- NEWS | 6 + Rules | 9 +- catgets/Makefile | 4 +- elf/Makefile | 15 +- elf/tst-leaks1-static.c | 1 - iconvdata/Makefile | 3 +- intl/tst-gettext.sh | 1 + libio/Makefile | 12 +- localedata/Makefile | 3 +- malloc/Makefile | 44 +++-- malloc/arena.c | 7 - malloc/hooks.c | 60 +++++-- malloc/malloc-debug.c | 189 +++++++++++++++++++++ malloc/malloc.c | 77 ++------- malloc/mcheck.c | 1 + malloc/mtrace.c | 1 + malloc/tst-compathooks-off.c | 145 ++++++++++++++++ malloc/tst-compathooks-on.c | 2 + malloc/tst-malloc-usable-static-tunables.c | 1 - malloc/tst-malloc-usable-static.c | 1 - malloc/tst-mtrace.sh | 1 + manual/memory.texi | 16 +- manual/tunables.texi | 4 +- misc/Makefile | 6 +- nptl/Makefile | 3 +- posix/Makefile | 40 +++-- resolv/Makefile | 9 +- stdio-common/Makefile | 15 +- sysdeps/pthread/Makefile | 3 +- 30 files changed, 532 insertions(+), 149 deletions(-) delete mode 100644 elf/tst-leaks1-static.c create mode 100644 malloc/malloc-debug.c create mode 100644 malloc/tst-compathooks-off.c create mode 100644 malloc/tst-compathooks-on.c delete mode 100644 malloc/tst-malloc-usable-static-tunables.c delete mode 100644 malloc/tst-malloc-usable-static.c diff --git a/Makeconfig b/Makeconfig index efc7351d71..6319941ef9 100644 --- a/Makeconfig +++ b/Makeconfig @@ -951,7 +951,7 @@ libio-include =3D -I$(..)libio built-modules =3D iconvprogs iconvdata ldconfig lddlibc4 libmemusage \ libSegFault libpcprofile librpcsvc locale-programs \ memusagestat nonlib nscd extramodules libnldbl libsupport \ - testsuite + testsuite libc_malloc_debug =20 in-module =3D $(subst -,_,$(firstword $(libof-$(basename $(@F))) \ $(libof-$( $@; \ $(evaluate-test) -noload-ENV =3D MALLOC_TRACE=3D$(objpfx)noload.mtrace +noload-ENV =3D MALLOC_TRACE=3D$(objpfx)noload.mtrace \ + LD_PRELOAD=3D$(common-objpfx)/malloc/libc_malloc_debug.so =20 LDFLAGS-nodelete =3D -rdynamic LDFLAGS-nodelmod1.so =3D -Wl,--enable-new-dtags,-z,nodelete @@ -1273,12 +1274,8 @@ $(objpfx)tst-leaks1-mem.out: $(objpfx)tst-leaks1.o= ut $(common-objpfx)malloc/mtrace $(objpfx)tst-leaks1.mtrace > $@; \ $(evaluate-test) =20 -$(objpfx)tst-leaks1-static-mem.out: $(objpfx)tst-leaks1-static.out - $(common-objpfx)malloc/mtrace $(objpfx)tst-leaks1-static.mtrace > $@; \ - $(evaluate-test) - -tst-leaks1-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-leaks1.mtrace -tst-leaks1-static-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-leaks1-static.mtra= ce +tst-leaks1-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-leaks1.mtrace \ + LD_PRELOAD=3D$(common-objpfx)/malloc/libc_malloc_debug.so =20 $(objpfx)tst-thrlock: $(shared-thread-library) =20 diff --git a/elf/tst-leaks1-static.c b/elf/tst-leaks1-static.c deleted file mode 100644 index b956d66905..0000000000 --- a/elf/tst-leaks1-static.c +++ /dev/null @@ -1 +0,0 @@ -#include "tst-leaks1.c" diff --git a/iconvdata/Makefile b/iconvdata/Makefile index bb3f621b49..c216f959df 100644 --- a/iconvdata/Makefile +++ b/iconvdata/Makefile @@ -301,7 +301,8 @@ cpp-srcs-left :=3D $(modules) $(generated-modules) $(= libJIS-routines) \ lib :=3D iconvdata include $(patsubst %,$(..)libof-iterator.mk,$(cpp-srcs-left)) =20 -tst-loading-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-loading.mtrace +tst-loading-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-loading.mtrace \ + LD_PRELOAD=3D$(common-objpfx)/malloc/libc_malloc_debug.so $(objpfx)mtrace-tst-loading.out: $(objpfx)tst-loading.out $(common-objpfx)malloc/mtrace $(objpfx)tst-loading.mtrace > $@; \ $(evaluate-test) diff --git a/intl/tst-gettext.sh b/intl/tst-gettext.sh index 77864de18c..37d9fcb80a 100755 --- a/intl/tst-gettext.sh +++ b/intl/tst-gettext.sh @@ -50,6 +50,7 @@ msgfmt -o ${objpfx}domaindir/existing-locale/LC_TIME/ex= isting-time-domain.mo \ ${test_program_prefix_before_env} \ ${run_program_env} \ MALLOC_TRACE=3D$malloc_trace \ +LD_PRELOAD=3D${common_objpfx}malloc/libc_malloc_debug.so \ LOCPATH=3D${objpfx}localedir:${common_objpfx}localedata \ ${test_program_prefix_after_env} \ ${objpfx}tst-gettext > ${objpfx}tst-gettext.out ${objpfx}domaindir diff --git a/libio/Makefile b/libio/Makefile index 73f731e064..5336b7d595 100644 --- a/libio/Makefile +++ b/libio/Makefile @@ -165,10 +165,14 @@ LDFLAGS-tst-bz24228 =3D -Wl,--version-script=3Dtst-= bz24228.map =20 tst_wprintf2-ARGS =3D "Some Text" =20 -test-fmemopen-ENV =3D MALLOC_TRACE=3D$(objpfx)test-fmemopen.mtrace -tst-fopenloc-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-fopenloc.mtrace -tst-bz22415-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-bz22415.mtrace -tst-bz24228-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-bz24228.mtrace +test-fmemopen-ENV =3D MALLOC_TRACE=3D$(objpfx)test-fmemopen.mtrace \ + LD_PRELOAD=3D$(common-objpfx)/malloc/libc_malloc_debug.so +tst-fopenloc-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-fopenloc.mtrace \ + LD_PRELOAD=3D$(common-objpfx)/malloc/libc_malloc_debug.so +tst-bz22415-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-bz22415.mtrace \ + LD_PRELOAD=3D$(common-objpfx)/malloc/libc_malloc_debug.so +tst-bz24228-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-bz24228.mtrace \ + LD_PRELOAD=3D$(common-objpfx)/malloc/libc_malloc_debug.so =20 generated +=3D test-fmemopen.mtrace test-fmemopen.check generated +=3D tst-fopenloc.mtrace tst-fopenloc.check diff --git a/localedata/Makefile b/localedata/Makefile index 14e04cd3c5..f585e0dd41 100644 --- a/localedata/Makefile +++ b/localedata/Makefile @@ -456,7 +456,8 @@ $(INSTALL-SUPPORTED-LOCALE-FILES): install-locales-di= r tst-setlocale-ENV =3D LC_ALL=3Dja_JP.EUC-JP tst-wctype-ENV =3D LC_ALL=3Dja_JP.EUC-JP =20 -tst-leaks-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-leaks.mtrace +tst-leaks-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-leaks.mtrace \ + LD_PRELOAD=3D$(common-objpfx)/malloc/libc_malloc_debug.so $(objpfx)mtrace-tst-leaks.out: $(objpfx)tst-leaks.out $(common-objpfx)malloc/mtrace $(objpfx)tst-leaks.mtrace > $@; \ $(evaluate-test) diff --git a/malloc/Makefile b/malloc/Makefile index 2af1203a0a..86228b101d 100644 --- a/malloc/Makefile +++ b/malloc/Makefile @@ -42,11 +42,11 @@ tests :=3D mallocbug tst-malloc tst-valloc tst-calloc= tst-obstack \ tst-malloc-stats-cancellation \ tst-tcfree1 tst-tcfree2 tst-tcfree3 \ tst-safe-linking \ + tst-compathooks-off tst-compathooks-on =20 tests-static :=3D \ tst-interpose-static-nothread \ - tst-interpose-static-thread \ - tst-malloc-usable-static \ + tst-interpose-static-thread =20 # Test for the malloc_set_state symbol removed in glibc 2.25. ifeq ($(have-GLIBC_2.24)$(build-shared),yesyes) @@ -63,7 +63,6 @@ tests-internal +=3D \ =20 ifneq (no,$(have-tunables)) tests +=3D tst-malloc-usable-tunables tst-mxfast -tests-static +=3D tst-malloc-usable-static-tunables endif =20 tests +=3D $(tests-static) @@ -72,7 +71,8 @@ test-srcs =3D tst-mtrace # These tests either are run with MALLOC_CHECK_=3D3 by default or do not= work # with MALLOC_CHECK_=3D3 because they expect a specific failure. tests-exclude-malloc-check =3D tst-malloc-check tst-malloc-usable \ - tst-mxfast tst-safe-linking + tst-mxfast tst-safe-linking \ + tst-compathooks-off tst-compathooks-on =20 # Run all tests with MALLOC_CHECK_=3D3 tests-malloc-check =3D $(filter-out $(tests-exclude-malloc-check),$(test= s)) @@ -88,13 +88,12 @@ tests-exclude-mcheck =3D tst-mallocstate \ tst-malloc-tcache-leak \ tst-malloc-thread-exit \ tst-malloc-thread-fail \ - tst-malloc-usable-static \ - tst-malloc-usable-static-tunables \ tst-malloc-usable-tunables \ tst-malloc_info \ tst-memalign \ tst-posix_memalign \ - tst-posix-realloc + tst-posix-realloc \ + tst-compathooks-off tst-compathooks-on =20 tests-mcheck =3D $(filter-out $(tests-exclude-mcheck), $(tests)) =20 @@ -116,8 +115,8 @@ routines =3D malloc morecore mcheck mtrace obstack re= allocarray \ install-lib :=3D libmcheck.a non-lib.a :=3D libmcheck.a =20 -# Additional library. -extra-libs =3D libmemusage +# Additional libraries. +extra-libs =3D libmemusage libc_malloc_debug extra-libs-others =3D $(extra-libs) =20 # Helper objects for some tests. @@ -132,6 +131,9 @@ test-extras =3D \ libmemusage-routines =3D memusage libmemusage-inhibit-o =3D $(filter-out .os,$(object-suffixes)) =20 +libc_malloc_debug-routines =3D malloc-debug +libc_malloc_debug-inhibit-o =3D $(filter-out .os,$(object-suffixes)) + $(objpfx)tst-malloc-backtrace: $(shared-thread-library) $(objpfx)tst-malloc-thread-exit: $(shared-thread-library) $(objpfx)tst-malloc-thread-fail: $(shared-thread-library) @@ -237,11 +239,12 @@ endif endif endif =20 -tst-malloc-check-ENV =3D MALLOC_CHECK_=3D3 -tst-malloc-usable-ENV =3D MALLOC_CHECK_=3D3 -tst-malloc-usable-static-ENV =3D $(tst-malloc-usable-ENV) -tst-malloc-usable-tunables-ENV =3D GLIBC_TUNABLES=3Dglibc.malloc.check=3D= 3 -tst-malloc-usable-static-tunables-ENV =3D $(tst-malloc-usable-tunables-E= NV) +tst-malloc-check-ENV =3D MALLOC_CHECK_=3D3 \ + LD_PRELOAD=3D$(objpfx)/libc_malloc_debug.so +tst-malloc-usable-ENV =3D MALLOC_CHECK_=3D3 \ + LD_PRELOAD=3D$(objpfx)/libc_malloc_debug.so +tst-malloc-usable-tunables-ENV =3D GLIBC_TUNABLES=3Dglibc.malloc.check=3D= 3 \ + LD_PRELOAD=3D$(objpfx)/libc_malloc_debug.so =20 tst-mxfast-ENV =3D GLIBC_TUNABLES=3Dglibc.malloc.tcache_count=3D0:glibc.= malloc.mxfast=3D0 =20 @@ -296,12 +299,14 @@ $(objpfx)tst-interpose-static-thread-mcheck: \ $(objpfx)tst-interpose-static-thread-malloc-check: \ $(objpfx)tst-interpose-aux-thread.o $(static-thread-library) =20 -tst-dynarray-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-dynarray.mtrace +tst-dynarray-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-dynarray.mtrace \ + LD_PRELOAD=3D$(objpfx)libc_malloc_debug.so $(objpfx)tst-dynarray-mem.out: $(objpfx)tst-dynarray.out $(common-objpfx)malloc/mtrace $(objpfx)tst-dynarray.mtrace > $@; \ $(evaluate-test) =20 -tst-dynarray-fail-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-dynarray-fail.mtra= ce +tst-dynarray-fail-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-dynarray-fail.mtra= ce \ + LD_PRELOAD=3D$(objpfx)libc_malloc_debug.so $(objpfx)tst-dynarray-fail-mem.out: $(objpfx)tst-dynarray-fail.out $(common-objpfx)malloc/mtrace $(objpfx)tst-dynarray-fail.mtrace > $@; \ $(evaluate-test) @@ -315,3 +320,10 @@ $(objpfx)tst-mallocfork2-mcheck: $(shared-thread-lib= rary) $(objpfx)tst-malloc-tcache-leak-malloc-check: $(shared-thread-library) $(objpfx)tst-malloc_info-malloc-check: $(shared-thread-library) $(objpfx)tst-mallocfork2-malloc-check: $(shared-thread-library) + +tst-compathooks-on-ENV =3D LD_PRELOAD=3D$(objpfx)libc_malloc_debug.so +tst-compathooks-on-mcheck-ENV =3D LD_PRELOAD=3D$(objpfx)libc_malloc_debu= g.so +tst-compathooks-on-malloc-check-ENV =3D \ + LD_PRELOAD=3D$(objpfx)libc_malloc_debug.so +tst-mallocstate-ENV =3D LD_PRELOAD=3D$(objpfx)libc_malloc_debug.so +tst-mallocstate-malloc-check-ENV =3D LD_PRELOAD=3D$(objpfx)libc_malloc_d= ebug.so diff --git a/malloc/arena.c b/malloc/arena.c index 7eb110445e..8f890d5ff0 100644 --- a/malloc/arena.c +++ b/malloc/arena.c @@ -404,13 +404,6 @@ ptmalloc_init (void) if (s && s[0] !=3D '\0' && s[0] !=3D '0') __malloc_check_init (); #endif - -#if HAVE_MALLOC_INIT_HOOK - void (*hook) (void) =3D atomic_forced_read (__malloc_initialize_hook); - if (hook !=3D NULL) - (*hook)(); -#endif - __malloc_initialized =3D 1; } =20 /* Managing heaps and arenas (for concurrent threads) */ diff --git a/malloc/hooks.c b/malloc/hooks.c index daa5c7cfae..f0adcd9308 100644 --- a/malloc/hooks.c +++ b/malloc/hooks.c @@ -1,4 +1,4 @@ -/* Malloc implementation for multiple threads without lock contention. +/* Compatibility code for malloc debugging and state management. Copyright (C) 2001-2021 Free Software Foundation, Inc. This file is part of the GNU C Library. Contributed by Wolfram Gloger , 2001. @@ -17,32 +17,70 @@ License along with the GNU C Library; see the file COPYING.LIB. If not, see . */ =20 +#if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_24) +void (*__malloc_initialize_hook) (void) __attribute__ ((nocommon)); +compat_symbol (libc, __malloc_initialize_hook, + __malloc_initialize_hook, GLIBC_2_0); +#endif + +static void *malloc_hook_ini (size_t, const void *) __THROW; +static void *realloc_hook_ini (void *, size_t, const void *) __THROW; +static void *memalign_hook_ini (size_t, size_t, const void *) __THROW; + +void weak_variable (*__free_hook) (void *, const void *) =3D NULL; +void *weak_variable (*__malloc_hook) + (size_t, const void *) =3D malloc_hook_ini; +void *weak_variable (*__realloc_hook) + (void *, size_t, const void *) =3D realloc_hook_ini; +void *weak_variable (*__memalign_hook) + (size_t, size_t, const void *) =3D memalign_hook_ini; + /* Hooks for debugging versions. The initial hooks just call the initialization routine, then do the normal work. */ =20 -static void * -malloc_hook_ini (size_t sz, const void *caller) +/* These hooks will get executed only through the interposed allocator + functions in libc_malloc_debug.so. This means that the calls to mall= oc, + realloc, etc. will lead back into the interposed functions, which is = what we + want. + + These initial hooks are assumed to be called in a single-threaded con= text, + so it is safe to reset all hooks at once upon initialization. */ + +static void +generic_hook_ini (void) { __malloc_hook =3D NULL; + __realloc_hook =3D NULL; + __memalign_hook =3D NULL; ptmalloc_init (); - return __libc_malloc (sz); + +#if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_24) + void (*hook) (void) =3D atomic_forced_read (__malloc_initialize_hook); + if (hook !=3D NULL) + (*hook)(); +#endif + __malloc_initialized =3D 1; +} + +static void * +malloc_hook_ini (size_t sz, const void *caller) +{ + generic_hook_ini (); + return malloc (sz); } =20 static void * realloc_hook_ini (void *ptr, size_t sz, const void *caller) { - __malloc_hook =3D NULL; - __realloc_hook =3D NULL; - ptmalloc_init (); - return __libc_realloc (ptr, sz); + generic_hook_ini (); + return realloc (ptr, sz); } =20 static void * memalign_hook_ini (size_t alignment, size_t sz, const void *caller) { - __memalign_hook =3D NULL; - ptmalloc_init (); - return __libc_memalign (alignment, sz); + generic_hook_ini (); + return memalign (alignment, sz); } =20 #include "malloc-check.c" diff --git a/malloc/malloc-debug.c b/malloc/malloc-debug.c new file mode 100644 index 0000000000..1c80c33f43 --- /dev/null +++ b/malloc/malloc-debug.c @@ -0,0 +1,189 @@ +/* Malloc debug DSO. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; see the file COPYING.LIB. If + not, see . */ + +#include +#include +#include +#include +#include +#include + +/* Support only the glibc allocators. */ +extern void *__libc_malloc (size_t); +extern void __libc_free (void *); +extern void *__libc_realloc (void *, size_t); +extern void *__libc_memalign (size_t, size_t); +extern void *__libc_valloc (size_t); +extern void *__libc_pvalloc (size_t); +extern void *__libc_calloc (size_t, size_t); + +#define DEBUG_FN(fn) \ + static __typeof (__libc_ ## fn) __debug_ ## fn + +DEBUG_FN(malloc); +DEBUG_FN(free); +DEBUG_FN(realloc); +DEBUG_FN(memalign); +DEBUG_FN(valloc); +DEBUG_FN(pvalloc); +DEBUG_FN(calloc); + +extern void (*__free_hook) (void *, const void *); +compat_symbol_reference (libc, __free_hook, __free_hook, GLIBC_2_0); +extern void * (*__malloc_hook) (size_t, const void *); +compat_symbol_reference (libc, __malloc_hook, __malloc_hook, GLIBC_2_0); +extern void * (*__realloc_hook) (void *, size_t, const void *); +compat_symbol_reference (libc, __realloc_hook, __realloc_hook, GLIBC_2_0= ); +extern void * (*__memalign_hook) (size_t, size_t, const void *); +compat_symbol_reference (libc, __memalign_hook, __memalign_hook, GLIBC_2= _0); + +static size_t pagesize; + +/* The allocator functions. */ + +static void * +__debug_malloc (size_t bytes) +{ + void *(*hook) (size_t, const void *) =3D atomic_forced_read (__malloc_= hook); + if (__builtin_expect (hook !=3D NULL, 0)) + return (*hook)(bytes, RETURN_ADDRESS (0)); + + return __libc_malloc (bytes); +} +strong_alias (__debug_malloc, malloc) + +static void +__debug_free (void *mem) +{ + void (*hook) (void *, const void *) =3D atomic_forced_read (__free_hoo= k); + if (__builtin_expect (hook !=3D NULL, 0)) + { + (*hook)(mem, RETURN_ADDRESS (0)); + return; + } + __libc_free (mem); +} +strong_alias (__debug_free, free) + +static void * +__debug_realloc (void *oldmem, size_t bytes) +{ + void *(*hook) (void *, size_t, const void *) =3D + atomic_forced_read (__realloc_hook); + if (__builtin_expect (hook !=3D NULL, 0)) + return (*hook)(oldmem, bytes, RETURN_ADDRESS (0)); + + return __libc_realloc (oldmem, bytes); +} +strong_alias (__debug_realloc, realloc) + +static void * +_mid_memalign (size_t alignment, size_t bytes, const void *address) +{ + void *(*hook) (size_t, size_t, const void *) =3D + atomic_forced_read (__memalign_hook); + if (__builtin_expect (hook !=3D NULL, 0)) + return (*hook)(alignment, bytes, address); + + return __libc_memalign (alignment, bytes); +} + +static void * +__debug_memalign (size_t alignment, size_t bytes) +{ + return _mid_memalign (alignment, bytes, RETURN_ADDRESS (0)); +} +strong_alias (__debug_memalign, memalign) +strong_alias (__debug_memalign, aligned_alloc) + +static void * +__debug_pvalloc (size_t bytes) +{ + size_t rounded_bytes; + + if (!pagesize) + pagesize =3D sysconf (_SC_PAGESIZE); + + /* ALIGN_UP with overflow check. */ + if (__glibc_unlikely (__builtin_add_overflow (bytes, + pagesize - 1, + &rounded_bytes))) + { + errno =3D ENOMEM; + return NULL; + } + rounded_bytes =3D rounded_bytes & -(pagesize - 1); + + return _mid_memalign (pagesize, rounded_bytes, RETURN_ADDRESS (0)); +} +strong_alias (__debug_pvalloc, pvalloc) + +static void * +__debug_valloc (size_t bytes) +{ + if (!pagesize) + pagesize =3D sysconf (_SC_PAGESIZE); + + return _mid_memalign (pagesize, bytes, RETURN_ADDRESS (0)); +} +strong_alias (__debug_valloc, valloc) + +static int +__debug_posix_memalign (void **memptr, size_t alignment, size_t bytes) +{ + /* Test whether the SIZE argument is valid. It must be a power of + two multiple of sizeof (void *). */ + if (alignment % sizeof (void *) !=3D 0 + || !powerof2 (alignment / sizeof (void *)) + || alignment =3D=3D 0) + return EINVAL; + + *memptr =3D _mid_memalign (alignment, bytes, RETURN_ADDRESS (0)); + + if (*memptr =3D=3D NULL) + return ENOMEM; + + return 0; +} +strong_alias (__debug_posix_memalign, posix_memalign) + +static void * +__debug_calloc (size_t nmemb, size_t size) +{ + void *(*hook) (size_t, const void *) =3D atomic_forced_read (__malloc_= hook); + if (__builtin_expect (hook !=3D NULL, 0)) + { + size_t bytes; + + if (__glibc_unlikely (__builtin_mul_overflow (nmemb, size, &bytes)= )) + { + errno =3D ENOMEM; + return NULL; + } + + void *mem =3D (*hook)(bytes, RETURN_ADDRESS (0)); + + if (mem !=3D NULL) + memset (mem, 0, bytes); + + return mem; + } + + return __libc_calloc (nmemb, size); +} +strong_alias (__debug_calloc, calloc) diff --git a/malloc/malloc.c b/malloc/malloc.c index bb9a1642aa..d2e45664b1 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -570,16 +570,6 @@ tag_at (void *ptr) #define HAVE_MREMAP 0 #endif =20 -/* We may need to support __malloc_initialize_hook for backwards - compatibility. */ - -#if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_24) -# define HAVE_MALLOC_INIT_HOOK 1 -#else -# define HAVE_MALLOC_INIT_HOOK 0 -#endif - - /* This version of malloc supports the standard SVID/XPG mallinfo routine that returns a struct containing usage properties and @@ -2013,30 +2003,6 @@ static void malloc_consolidate (mstate); # define weak_variable weak_function #endif =20 -/* Forward declarations. */ -static void *malloc_hook_ini (size_t sz, - const void *caller) __THROW; -static void *realloc_hook_ini (void *ptr, size_t sz, - const void *caller) __THROW; -static void *memalign_hook_ini (size_t alignment, size_t sz, - const void *caller) __THROW; - -#if HAVE_MALLOC_INIT_HOOK -void (*__malloc_initialize_hook) (void) __attribute__ ((nocommon)); -compat_symbol (libc, __malloc_initialize_hook, - __malloc_initialize_hook, GLIBC_2_0); -#endif - -void weak_variable (*__free_hook) (void *__ptr, - const void *) =3D NULL; -void *weak_variable (*__malloc_hook) - (size_t __size, const void *) =3D malloc_hook_ini; -void *weak_variable (*__realloc_hook) - (void *__ptr, size_t __size, const void *) - =3D realloc_hook_ini; -void *weak_variable (*__memalign_hook) - (size_t __alignment, size_t __size, const void *) - =3D memalign_hook_ini; void weak_variable (*__after_morecore_hook) (void) =3D NULL; =20 /* This function is called from the arena shutdown hook, to free the @@ -3229,10 +3195,8 @@ __libc_malloc (size_t bytes) _Static_assert (PTRDIFF_MAX <=3D SIZE_MAX / 2, "PTRDIFF_MAX is not more than half of SIZE_MAX"); =20 - void *(*hook) (size_t, const void *) - =3D atomic_forced_read (__malloc_hook); - if (__builtin_expect (hook !=3D NULL, 0)) - return (*hook)(bytes, RETURN_ADDRESS (0)); + if (__malloc_initialized < 0) + ptmalloc_init (); #if USE_TCACHE /* int_free also calls request2size, be careful to not pad twice. */ size_t tbytes; @@ -3293,14 +3257,6 @@ __libc_free (void *mem) mstate ar_ptr; mchunkptr p; /* chunk corresponding to mem */ =20 - void (*hook) (void *, const void *) - =3D atomic_forced_read (__free_hook); - if (__builtin_expect (hook !=3D NULL, 0)) - { - (*hook)(mem, RETURN_ADDRESS (0)); - return; - } - if (mem =3D=3D 0) /* free(0) has no effec= t */ return; =20 @@ -3352,10 +3308,8 @@ __libc_realloc (void *oldmem, size_t bytes) =20 void *newp; /* chunk to return */ =20 - void *(*hook) (void *, size_t, const void *) =3D - atomic_forced_read (__realloc_hook); - if (__builtin_expect (hook !=3D NULL, 0)) - return (*hook)(oldmem, bytes, RETURN_ADDRESS (0)); + if (__malloc_initialized < 0) + ptmalloc_init (); =20 #if REALLOC_ZERO_BYTES_FREES if (bytes =3D=3D 0 && oldmem !=3D NULL) @@ -3490,6 +3444,9 @@ libc_hidden_def (__libc_realloc) void * __libc_memalign (size_t alignment, size_t bytes) { + if (__malloc_initialized < 0) + ptmalloc_init (); + void *address =3D RETURN_ADDRESS (0); return _mid_memalign (alignment, bytes, address); } @@ -3500,11 +3457,6 @@ _mid_memalign (size_t alignment, size_t bytes, voi= d *address) mstate ar_ptr; void *p; =20 - void *(*hook) (size_t, size_t, const void *) =3D - atomic_forced_read (__memalign_hook); - if (__builtin_expect (hook !=3D NULL, 0)) - return (*hook)(alignment, bytes, address); - /* If we need less alignment than we give anyway, just relay to malloc= . */ if (alignment <=3D MALLOC_ALIGNMENT) return __libc_malloc (bytes); @@ -3613,16 +3565,8 @@ __libc_calloc (size_t n, size_t elem_size) =20 sz =3D bytes; =20 - void *(*hook) (size_t, const void *) =3D - atomic_forced_read (__malloc_hook); - if (__builtin_expect (hook !=3D NULL, 0)) - { - mem =3D (*hook)(sz, RETURN_ADDRESS (0)); - if (mem =3D=3D 0) - return 0; - - return memset (mem, 0, sz); - } + if (__malloc_initialized < 0) + ptmalloc_init (); =20 MAYBE_INIT_TCACHE (); =20 @@ -5651,6 +5595,9 @@ __posix_memalign (void **memptr, size_t alignment, = size_t size) { void *mem; =20 + if (__malloc_initialized < 0) + ptmalloc_init (); + /* Test whether the SIZE argument is valid. It must be a power of two multiple of sizeof (void *). */ if (alignment % sizeof (void *) !=3D 0 diff --git a/malloc/mcheck.c b/malloc/mcheck.c index 2a1fc645d4..b46b9b26bf 100644 --- a/malloc/mcheck.c +++ b/malloc/mcheck.c @@ -25,6 +25,7 @@ # include # include # include +# include #endif =20 /* Old hook values. */ diff --git a/malloc/mtrace.c b/malloc/mtrace.c index f5b8321c6b..274db3fb47 100644 --- a/malloc/mtrace.c +++ b/malloc/mtrace.c @@ -22,6 +22,7 @@ # define _MALLOC_INTERNAL # include # include +# include # include #endif =20 diff --git a/malloc/tst-compathooks-off.c b/malloc/tst-compathooks-off.c new file mode 100644 index 0000000000..7b3722d8b3 --- /dev/null +++ b/malloc/tst-compathooks-off.c @@ -0,0 +1,145 @@ +/* Minimal tests to verify libc_malloc_debug.so functionality. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include + +#include +#include + +extern void (*volatile __free_hook) (void *, const void *); +extern void *(*volatile __malloc_hook)(size_t, const void *); +extern void *(*volatile __realloc_hook)(void *, size_t, const void *); +extern void *(*volatile __memalign_hook)(size_t, size_t, const void *); + +int hook_count, call_count; + +DIAG_PUSH_NEEDS_COMMENT; +DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wdeprecated-declarations"); + +void +free_called (void *mem, const void *address) +{ + hook_count++; + __free_hook =3D NULL; + free (mem); + __free_hook =3D free_called; +} + +void * +malloc_called (size_t bytes, const void *address) +{ + hook_count++; + __malloc_hook =3D NULL; + void *mem =3D malloc (bytes); + __malloc_hook =3D malloc_called; + return mem; +} + +void * +realloc_called (void *oldptr, size_t bytes, const void *address) +{ + hook_count++; + __realloc_hook =3D NULL; + void *mem =3D realloc (oldptr, bytes); + __realloc_hook =3D realloc_called; + return mem; +} + +void * +calloc_called (size_t n, size_t size, const void *address) +{ + hook_count++; + __malloc_hook =3D NULL; + void *mem =3D calloc (n, size); + __malloc_hook =3D malloc_called; + return mem; +} + +void * +memalign_called (size_t align, size_t size, const void *address) +{ + hook_count++; + __memalign_hook =3D NULL; + void *mem =3D memalign (align, size); + __memalign_hook =3D memalign_called; + return mem; +} + +static void initialize_hooks (void) +{ + __free_hook =3D free_called; + __malloc_hook =3D malloc_called; + __realloc_hook =3D realloc_called; + __memalign_hook =3D memalign_called; +} +void (*__malloc_initialize_hook) (void) =3D initialize_hooks; +compat_symbol_reference (libc, __malloc_initialize_hook, + __malloc_initialize_hook, GLIBC_2_0); +compat_symbol_reference (libc, __free_hook, + __free_hook, GLIBC_2_0); +compat_symbol_reference (libc, __malloc_hook, + __malloc_hook, GLIBC_2_0); +compat_symbol_reference (libc, __realloc_hook, + __realloc_hook, GLIBC_2_0); +compat_symbol_reference (libc, __memalign_hook, + __memalign_hook, GLIBC_2_0); + +DIAG_POP_NEEDS_COMMENT; + +static int +do_test (void) +{ + void *p; + p =3D malloc (0); + TEST_VERIFY_EXIT (p !=3D NULL); + call_count++; + + p =3D realloc (p, 0); + TEST_VERIFY_EXIT (p =3D=3D NULL); + call_count++; + + p =3D calloc (512, 1); + TEST_VERIFY_EXIT (p !=3D NULL); + call_count++; + + free (p); + call_count++; + + p =3D memalign (0x100, 0x100); + TEST_VERIFY_EXIT (p !=3D NULL); + call_count++; + + free (p); + call_count++; + + printf ("call_count: %d, hook_count: %d\n", call_count, hook_count); + +#ifdef HOOKS_ENABLED + TEST_VERIFY_EXIT (call_count =3D=3D hook_count); +#else + TEST_VERIFY_EXIT (hook_count =3D=3D 0); +#endif + + exit (0); +} + +#include diff --git a/malloc/tst-compathooks-on.c b/malloc/tst-compathooks-on.c new file mode 100644 index 0000000000..4da183687a --- /dev/null +++ b/malloc/tst-compathooks-on.c @@ -0,0 +1,2 @@ +#define HOOKS_ENABLED 1 +#include "tst-compathooks-off.c" diff --git a/malloc/tst-malloc-usable-static-tunables.c b/malloc/tst-mall= oc-usable-static-tunables.c deleted file mode 100644 index 8907db01a5..0000000000 --- a/malloc/tst-malloc-usable-static-tunables.c +++ /dev/null @@ -1 +0,0 @@ -#include diff --git a/malloc/tst-malloc-usable-static.c b/malloc/tst-malloc-usable= -static.c deleted file mode 100644 index 8907db01a5..0000000000 --- a/malloc/tst-malloc-usable-static.c +++ /dev/null @@ -1 +0,0 @@ -#include diff --git a/malloc/tst-mtrace.sh b/malloc/tst-mtrace.sh index 9295683aff..a830204d5e 100755 --- a/malloc/tst-mtrace.sh +++ b/malloc/tst-mtrace.sh @@ -30,6 +30,7 @@ trap "rm -f ${common_objpfx}malloc/tst-mtrace.leak; exi= t 1" 1 2 15 ${test_program_prefix_before_env} \ ${run_program_env} \ MALLOC_TRACE=3D${common_objpfx}malloc/tst-mtrace.leak \ +LD_PRELOAD=3D${common_objpfx}malloc/libc_malloc_debug.so \ ${test_program_prefix_after_env} \ ${common_objpfx}malloc/tst-mtrace || status=3D1 =20 diff --git a/manual/memory.texi b/manual/memory.texi index 28ec2e4e63..0aae1f8720 100644 --- a/manual/memory.texi +++ b/manual/memory.texi @@ -1256,8 +1256,9 @@ environment variable @env{MALLOC_ARENA_MAX} to the = desired value. @cindex consistency checking, of heap =20 You can ask @code{malloc} to check the consistency of dynamic memory by -using the @code{mcheck} function. This function is a GNU extension, -declared in @file{mcheck.h}. +using the @code{mcheck} function and preloading the malloc debug library +@file{libc_malloc_debug.so} using the @var{LD_PRELOAD} environment varia= ble. +This function is a GNU extension, declared in @file{mcheck.h}. @pindex mcheck.h =20 @deftypefun int mcheck (void (*@var{abortfn}) (enum mcheck_status @var{s= tatus})) @@ -1368,7 +1369,10 @@ non-zero value, a special (less efficient) impleme= ntation is used which is designed to be tolerant against simple errors, such as double calls of @code{free} with the same argument, or overruns of a single byte (off-by-one bugs). Not all such errors can be protected against, -however, and memory leaks can result. +however, and memory leaks can result. Like in the case of @code{mcheck}= , +one would need to preload the @file{libc_malloc_debug.so} library to +enable @code{MALLOC_CHECK_} functionality. Without this preloaded +library, setting @code{MALLOC_CHECK_} will have no effect. =20 Any detected heap corruption results in immediate termination of the process. @@ -1747,6 +1751,12 @@ penalties for the program if the debugging mode is= not enabled. @c fprintf dup (on newly-created stream) @aculock @c __cxa_atexit (once) dup @asulock @aculock @acsmem @c free dup @ascuheap @acsmem +The @code{mtrace} function provides a way to trace memory allocation +events in the program that calls it. It is disabled by default in the +library and can be enabled by preloading the debugging library +@file{libc_malloc_debug.so} using the @code{LD_PRELOAD} environment +variable. + When the @code{mtrace} function is called it looks for an environment variable named @code{MALLOC_TRACE}. This variable is supposed to contain a valid file name. The user must have write access. If the diff --git a/manual/tunables.texi b/manual/tunables.texi index d5d957fb5b..b9c52a0090 100644 --- a/manual/tunables.texi +++ b/manual/tunables.texi @@ -113,7 +113,9 @@ following tunables in the @code{malloc} namespace: =20 @deftp Tunable glibc.malloc.check This tunable supersedes the @env{MALLOC_CHECK_} environment variable and= is -identical in features. +identical in features. This tunable has no effect by default and needs t= he +debug library @file{libc_malloc_debug.so} to be preloaded using the +@code{LD_PRELOAD} environment variable. =20 Setting this tunable to a non-zero value enables a special (less efficient) memory allocator for the @code{malloc} family of functions th= at is diff --git a/misc/Makefile b/misc/Makefile index ae03e26f1b..b144a3df6c 100644 --- a/misc/Makefile +++ b/misc/Makefile @@ -152,13 +152,15 @@ $(objpfx)libg.a: $(dep-dummy-lib); $(make-dummy-lib= ) =20 $(objpfx)tst-tsearch: $(libm) =20 -tst-error1-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-error1.mtrace +tst-error1-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-error1.mtrace \ + LD_PRELOAD=3D$(common-objpfx)/malloc/libc_malloc_debug.so tst-error1-ARGS =3D $(objpfx)tst-error1.out $(objpfx)tst-error1-mem.out: $(objpfx)tst-error1.out $(common-objpfx)malloc/mtrace $(objpfx)tst-error1.mtrace > $@; \ $(evaluate-test) =20 -tst-allocate_once-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-allocate_once.mtra= ce +tst-allocate_once-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-allocate_once.mtra= ce \ + LD_PRELOAD=3D$(common-objpfx)/malloc/libc_malloc_debug.so $(objpfx)tst-allocate_once-mem.out: $(objpfx)tst-allocate_once.out $(common-objpfx)malloc/mtrace $(objpfx)tst-allocate_once.mtrace > $@; \ $(evaluate-test) diff --git a/nptl/Makefile b/nptl/Makefile index 9b94bfcd31..ff4d590f11 100644 --- a/nptl/Makefile +++ b/nptl/Makefile @@ -494,7 +494,8 @@ CFLAGS-tst-initializers1-gnu11.c +=3D $(CFLAGS-tst-in= itializers1-<) tst-cancel7-ARGS =3D --command "exec $(host-test-program-cmd)" tst-cancelx7-ARGS =3D $(tst-cancel7-ARGS) =20 -tst-stack3-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-stack3.mtrace +tst-stack3-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-stack3.mtrace \ + LD_PRELOAD=3D$(common-objpfx)/malloc/libc_malloc_debug.so $(objpfx)tst-stack3-mem.out: $(objpfx)tst-stack3.out $(common-objpfx)malloc/mtrace $(objpfx)tst-stack3.mtrace > $@; \ $(evaluate-test) diff --git a/posix/Makefile b/posix/Makefile index e91ea25ba1..3f1649369a 100644 --- a/posix/Makefile +++ b/posix/Makefile @@ -311,43 +311,50 @@ annexc-CFLAGS =3D -O $(objpfx)annexc: annexc.c $(native-compile) =20 -tst-fnmatch-ENV +=3D MALLOC_TRACE=3D$(objpfx)tst-fnmatch.mtrace +tst-fnmatch-ENV +=3D MALLOC_TRACE=3D$(objpfx)tst-fnmatch.mtrace \ + LD_PRELOAD=3D$(common-objpfx)/malloc/libc_malloc_debug.so =20 $(objpfx)tst-fnmatch-mem.out: $(objpfx)tst-fnmatch.out $(common-objpfx)malloc/mtrace $(objpfx)tst-fnmatch.mtrace > $@; \ $(evaluate-test) =20 -bug-regex2-ENV =3D MALLOC_TRACE=3D$(objpfx)bug-regex2.mtrace +bug-regex2-ENV =3D MALLOC_TRACE=3D$(objpfx)bug-regex2.mtrace \ + LD_PRELOAD=3D$(common-objpfx)/malloc/libc_malloc_debug.so =20 $(objpfx)bug-regex2-mem.out: $(objpfx)bug-regex2.out $(common-objpfx)malloc/mtrace $(objpfx)bug-regex2.mtrace > $@; \ $(evaluate-test) =20 -bug-regex14-ENV =3D MALLOC_TRACE=3D$(objpfx)bug-regex14.mtrace +bug-regex14-ENV =3D MALLOC_TRACE=3D$(objpfx)bug-regex14.mtrace \ + LD_PRELOAD=3D$(common-objpfx)/malloc/libc_malloc_debug.so =20 $(objpfx)bug-regex14-mem.out: $(objpfx)bug-regex14.out $(common-objpfx)malloc/mtrace $(objpfx)bug-regex14.mtrace > $@; \ $(evaluate-test) =20 -bug-regex21-ENV =3D MALLOC_TRACE=3D$(objpfx)bug-regex21.mtrace +bug-regex21-ENV =3D MALLOC_TRACE=3D$(objpfx)bug-regex21.mtrace \ + LD_PRELOAD=3D$(common-objpfx)/malloc/libc_malloc_debug.so =20 $(objpfx)bug-regex21-mem.out: $(objpfx)bug-regex21.out $(common-objpfx)malloc/mtrace $(objpfx)bug-regex21.mtrace > $@; \ $(evaluate-test) =20 -bug-regex31-ENV =3D MALLOC_TRACE=3D$(objpfx)bug-regex31.mtrace +bug-regex31-ENV =3D MALLOC_TRACE=3D$(objpfx)bug-regex31.mtrace \ + LD_PRELOAD=3D$(common-objpfx)/malloc/libc_malloc_debug.so =20 $(objpfx)bug-regex31-mem.out: $(objpfx)bug-regex31.out $(common-objpfx)malloc/mtrace $(objpfx)bug-regex31.mtrace > $@; \ $(evaluate-test) =20 -bug-regex36-ENV =3D MALLOC_TRACE=3D$(objpfx)bug-regex36.mtrace +bug-regex36-ENV =3D MALLOC_TRACE=3D$(objpfx)bug-regex36.mtrace \ + LD_PRELOAD=3D$(common-objpfx)/malloc/libc_malloc_debug.so =20 $(objpfx)bug-regex36-mem.out: $(objpfx)bug-regex36.out $(common-objpfx)malloc/mtrace $(objpfx)bug-regex36.mtrace > $@; \ $(evaluate-test) =20 -tst-vfork3-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-vfork3.mtrace +tst-vfork3-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-vfork3.mtrace \ + LD_PRELOAD=3D$(common-objpfx)/malloc/libc_malloc_debug.so =20 $(objpfx)tst-vfork3-mem.out: $(objpfx)tst-vfork3.out $(common-objpfx)malloc/mtrace $(objpfx)tst-vfork3.mtrace > $@; \ @@ -356,18 +363,22 @@ $(objpfx)tst-vfork3-mem.out: $(objpfx)tst-vfork3.ou= t # tst-rxspencer.mtrace is not generated, only # tst-rxspencer-no-utf8.mtrace, since otherwise the file has almost # 100M and takes very long time to process. -tst-rxspencer-no-utf8-ENV +=3D MALLOC_TRACE=3D$(objpfx)tst-rxspencer-no-= utf8.mtrace +tst-rxspencer-no-utf8-ENV +=3D \ + MALLOC_TRACE=3D$(objpfx)tst-rxspencer-no-utf8.mtrace \ + LD_PRELOAD=3D$(common-objpfx)/malloc/libc_malloc_debug.so $(objpfx)tst-rxspencer-no-utf8-mem.out: $(objpfx)tst-rxspencer-no-utf8.o= ut $(common-objpfx)malloc/mtrace $(objpfx)tst-rxspencer-no-utf8.mtrace \ > $@; \ $(evaluate-test) =20 -tst-pcre-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-pcre.mtrace +tst-pcre-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-pcre.mtrace \ + LD_PRELOAD=3D$(common-objpfx)/malloc/libc_malloc_debug.so $(objpfx)tst-pcre-mem.out: $(objpfx)tst-pcre.out $(common-objpfx)malloc/mtrace $(objpfx)tst-pcre.mtrace > $@; \ $(evaluate-test) =20 -tst-boost-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-boost.mtrace +tst-boost-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-boost.mtrace \ + LD_PRELOAD=3D$(common-objpfx)/malloc/libc_malloc_debug.so $(objpfx)tst-boost-mem.out: $(objpfx)tst-boost.out $(common-objpfx)malloc/mtrace $(objpfx)tst-boost.mtrace > $@; \ $(evaluate-test) @@ -382,15 +393,18 @@ $(objpfx)bug-ga2-mem.out: $(objpfx)bug-ga2.out && $(common-objpfx)malloc/mtrace $(objpfx)bug-ga2.mtrace; } > $@; \ $(evaluate-test) =20 -bug-ga2-ENV =3D MALLOC_TRACE=3D$(objpfx)bug-ga2.mtrace +bug-ga2-ENV =3D MALLOC_TRACE=3D$(objpfx)bug-ga2.mtrace \ + LD_PRELOAD=3D$(common-objpfx)/malloc/libc_malloc_debug.so =20 -bug-glob2-ENV =3D MALLOC_TRACE=3D$(objpfx)bug-glob2.mtrace +bug-glob2-ENV =3D MALLOC_TRACE=3D$(objpfx)bug-glob2.mtrace \ + LD_PRELOAD=3D$(common-objpfx)/malloc/libc_malloc_debug.so =20 $(objpfx)bug-glob2-mem.out: $(objpfx)bug-glob2.out $(common-objpfx)malloc/mtrace $(objpfx)bug-glob2.mtrace > $@; \ $(evaluate-test) =20 -tst-glob-tilde-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-glob-tilde.mtrace +tst-glob-tilde-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-glob-tilde.mtrace \ + LD_PRELOAD=3D$(common-objpfx)/malloc/libc_malloc_debug.so =20 $(objpfx)tst-glob-tilde-mem.out: $(objpfx)tst-glob-tilde.out $(common-objpfx)malloc/mtrace $(objpfx)tst-glob-tilde.mtrace > $@; \ diff --git a/resolv/Makefile b/resolv/Makefile index 1d3565d478..09b2f129eb 100644 --- a/resolv/Makefile +++ b/resolv/Makefile @@ -156,19 +156,22 @@ $(objpfx)tst-res_hconf_reorder: $(shared-thread-lib= rary) tst-res_hconf_reorder-ENV =3D RESOLV_REORDER=3Don =20 $(objpfx)tst-leaks: $(objpfx)libresolv.so -tst-leaks-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-leaks.mtrace +tst-leaks-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-leaks.mtrace \ + LD_PRELOAD=3D$(common-objpfx)/malloc/libc_malloc_debug.so $(objpfx)mtrace-tst-leaks.out: $(objpfx)tst-leaks.out $(common-objpfx)malloc/mtrace $(objpfx)tst-leaks.mtrace > $@; \ $(evaluate-test) =20 -tst-leaks2-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-leaks2.mtrace +tst-leaks2-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-leaks2.mtrace \ + LD_PRELOAD=3D$(common-objpfx)/malloc/libc_malloc_debug.so $(objpfx)mtrace-tst-leaks2.out: $(objpfx)tst-leaks2.out { test -r $(objpfx)tst-leaks2.mtrace \ || ( echo "tst-leaks2.mtrace does not exist"; exit 77; ) \ && $(common-objpfx)malloc/mtrace $(objpfx)tst-leaks2.mtrace; } > $@; \ $(evaluate-test) =20 -tst-resolv-res_ninit-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-resolv-res_nini= t.mtrace +tst-resolv-res_ninit-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-resolv-res_nini= t.mtrace \ + LD_PRELOAD=3D$(common-objpfx)/malloc/libc_malloc_debug.so $(objpfx)mtrace-tst-resolv-res_ninit.out: $(objpfx)tst-resolv-res_ninit.= out $(common-objpfx)malloc/mtrace \ $(objpfx)tst-resolv-res_ninit.mtrace > $@; \ diff --git a/stdio-common/Makefile b/stdio-common/Makefile index f87796a8ce..803f16dae0 100644 --- a/stdio-common/Makefile +++ b/stdio-common/Makefile @@ -110,15 +110,20 @@ $(objpfx)tst-swprintf.out: $(gen-locales) $(objpfx)tst-vfprintf-mbs-prec.out: $(gen-locales) endif =20 -tst-printf-bz18872-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-printf-bz18872.mt= race +tst-printf-bz18872-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-printf-bz18872.mt= race \ + LD_PRELOAD=3D$(common-objpfx)/malloc/libc_malloc_debug.so tst-vfprintf-width-prec-ENV =3D \ - MALLOC_TRACE=3D$(objpfx)tst-vfprintf-width-prec.mtrace + MALLOC_TRACE=3D$(objpfx)tst-vfprintf-width-prec.mtrace \ + LD_PRELOAD=3D$(common-objpfx)/malloc/libc_malloc_debug.so tst-printf-bz25691-ENV =3D \ - MALLOC_TRACE=3D$(objpfx)tst-printf-bz25691.mtrace + MALLOC_TRACE=3D$(objpfx)tst-printf-bz25691.mtrace \ + LD_PRELOAD=3D$(common-objpfx)/malloc/libc_malloc_debug.so tst-printf-fp-free-ENV =3D \ - MALLOC_TRACE=3D$(objpfx)tst-printf-fp-free.mtrace + MALLOC_TRACE=3D$(objpfx)tst-printf-fp-free.mtrace \ + LD_PRELOAD=3D$(common-objpfx)/malloc/libc_malloc_debug.so tst-printf-fp-leak-ENV =3D \ - MALLOC_TRACE=3D$(objpfx)tst-printf-fp-leak.mtrace + MALLOC_TRACE=3D$(objpfx)tst-printf-fp-leak.mtrace \ + LD_PRELOAD=3D$(common-objpfx)/malloc/libc_malloc_debug.so =20 $(objpfx)tst-unbputc.out: tst-unbputc.sh $(objpfx)tst-unbputc $(SHELL) $< $(common-objpfx) '$(test-program-prefix)'; \ diff --git a/sysdeps/pthread/Makefile b/sysdeps/pthread/Makefile index 9b862b93c4..42f9fc5072 100644 --- a/sysdeps/pthread/Makefile +++ b/sysdeps/pthread/Makefile @@ -211,7 +211,8 @@ tst-umask1-ARGS =3D $(objpfx)tst-umask1.temp =20 $(objpfx)tst-atfork2: $(shared-thread-library) LDFLAGS-tst-atfork2 =3D -rdynamic -tst-atfork2-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-atfork2.mtrace +tst-atfork2-ENV =3D MALLOC_TRACE=3D$(objpfx)tst-atfork2.mtrace \ + LD_PRELOAD=3D$(common-objpfx)/malloc/libc_malloc_debug.so $(objpfx)tst-atfork2mod.so: $(shared-thread-library) =20 ifeq ($(build-shared),yes) --=20 2.31.1