Created attachment 13513 [details] malloc-debug-tunable add tunables for MALLOC_DEBUG From aae155501ca1b3b056d040e72d63317b1ab40685 Mon Sep 17 00:00:00 2001 From: WANGXU <wangxuszcn@formail.com> Date: Thu, 11 Jun 2020 17:30:22 +0800 Subject: [PATCH] malloc debug -> tunalbe --- elf/dl-tunables.list | 5 +++ malloc/Makefile | 3 +- malloc/arena.c | 6 ++++ malloc/malloc.c | 49 +++++++++++++++++++++++++++- 4 files changed, 111 insertions(+), 46 deletions(-) diff --git a/elf/dl-tunables.list b/elf/dl-tunables.list index 0d398dd..4e741dc 100644 --- a/elf/dl-tunables.list +++ b/elf/dl-tunables.list @@ -90,6 +90,11 @@ glibc { minval: 0 security_level: SXID_IGNORE } + malloc_debug { + type: SIZE_T + minval: 0 + maxval: 1 + } } cpu { hwcap_mask { diff --git a/malloc/Makefile b/malloc/Makefile index e22cbde..eeb044a 100644 --- a/malloc/Makefile +++ b/malloc/Makefile @@ -204,8 +204,9 @@ CPPFLAGS-malloc.c += -DUSE_TCACHE=1 else CPPFLAGS-malloc.c += -DUSE_TCACHE=0 endif +CFLAGS-malloc.c += -Wno-frame-address # Uncomment this for test releases. For public releases it is too expensive. -#CPPFLAGS-malloc.o += -DMALLOC_DEBUG=1 +CPPFLAGS-malloc.o += -DMALLOC_DEBUG=1 sLIBdir := $(shell echo $(slibdir) | sed 's,lib\(\|64\)$$,\\\\$$LIB,') diff --git a/malloc/arena.c b/malloc/arena.c index cecdb7f..5b4bfee 100644 --- a/malloc/arena.c +++ b/malloc/arena.c @@ -237,6 +237,9 @@ TUNABLE_CALLBACK_FNDECL (set_tcache_count, size_t) TUNABLE_CALLBACK_FNDECL (set_tcache_unsorted_limit, size_t) #endif TUNABLE_CALLBACK_FNDECL (set_mxfast, size_t) +#if MALLOC_DEBUG +TUNABLE_CALLBACK_FNDECL (set_malloc_debug, size_t) +#endif #else /* Initialization routine. */ #include <string.h> @@ -325,6 +328,9 @@ ptmalloc_init (void) TUNABLE_CALLBACK (set_tcache_unsorted_limit)); # endif TUNABLE_GET (mxfast, size_t, TUNABLE_CALLBACK (set_mxfast)); +# if MALLOC_DEBUG + TUNABLE_GET (malloc_debug, size_t, TUNABLE_CALLBACK (set_malloc_debug)); +# endif #else const char *s = NULL; if (__glibc_likely (_environ != NULL)) diff --git a/malloc/malloc.c b/malloc/malloc.c index ee87ddb..0fac4d3 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -1743,6 +1743,9 @@ struct malloc_par aren't used to prefill the cache. */ size_t tcache_unsorted_limit; #endif +#if MALLOC_DEBUG + size_t malloc_debug; +#endif }; /* There are several instances of this struct ("arenas") in this @@ -1788,6 +1791,10 @@ static struct malloc_par mp_ = .tcache_max_bytes = tidx2usize (TCACHE_MAX_BINS-1), .tcache_unsorted_limit = 0 /* No limit. */ #endif +#if MALLOC_DEBUG + , + .malloc_debug = 0 +#endif }; /* @@ -1930,6 +1937,10 @@ free_perturb (char *p, size_t n) static void do_check_chunk (mstate av, mchunkptr p) { +#if MALLOC_DEBUG + if (__glibc_likely(!mp_.malloc_debug)) + return; +#endif unsigned long sz = chunksize (p); /* min and max possible addresses assuming contiguous allocation */ char *max_address = (char *) (av->top) + chunksize (av->top); @@ -1975,6 +1986,10 @@ do_check_chunk (mstate av, mchunkptr p) static void do_check_free_chunk (mstate av, mchunkptr p) { +#if MALLOC_DEBUG + if (__glibc_likely(!mp_.malloc_debug)) + return; +#endif INTERNAL_SIZE_T sz = chunksize_nomask (p) & ~(PREV_INUSE | NON_MAIN_ARENA); mchunkptr next = chunk_at_offset (p, sz); @@ -2012,6 +2027,10 @@ do_check_inuse_chunk (mstate av, mchunkptr p) { mchunkptr next; +#if MALLOC_DEBUG + if (__glibc_likely(!mp_.malloc_debug)) + return; +#endif do_check_chunk (av, p); if (chunk_is_mmapped (p)) @@ -2050,6 +2069,10 @@ do_check_inuse_chunk (mstate av, mchunkptr p) static void do_check_remalloced_chunk (mstate av, mchunkptr p, INTERNAL_SIZE_T s) { +#if MALLOC_DEBUG + if (__glibc_likely(!mp_.malloc_debug)) + return; +#endif INTERNAL_SIZE_T sz = chunksize_nomask (p) & ~(PREV_INUSE | NON_MAIN_ARENA); if (!chunk_is_mmapped (p)) @@ -2080,6 +2103,10 @@ do_check_remalloced_chunk (mstate av, mchunkptr p, INTERNAL_SIZE_T s) static void do_check_malloced_chunk (mstate av, mchunkptr p, INTERNAL_SIZE_T s) { +#if MALLOC_DEBUG + if (__glibc_likely(!mp_.malloc_debug)) + return; +#endif /* same as recycled case ... */ do_check_remalloced_chunk (av, p, s); @@ -2120,6 +2147,10 @@ do_check_malloc_state (mstate av) unsigned long total = 0; int max_fast_bin; +#if MALLOC_DEBUG + if (__glibc_likely(!mp_.malloc_debug)) + return; +#endif /* internal size_t must be no wider than pointer type */ assert (sizeof (INTERNAL_SIZE_T) <= sizeof (char *)); @@ -3552,7 +3583,9 @@ _int_malloc (mstate av, size_t bytes) #if USE_TCACHE size_t tcache_unsorted_count; /* count of unsorted chunks processed */ #endif - + //void *func = __builtin_return_address(1); + //if (func) +// return func; /* Convert request size to internal form by adding SIZE_SZ bytes overhead plus possibly more to obtain necessary alignment and/or @@ -5172,6 +5205,20 @@ do_set_mxfast (size_t value) return 0; } +#if MALLOC_DEBUG +static __always_inline int +do_set_malloc_debug (size_t value) +{ + if (value <= MAX_TCACHE_COUNT) + { + LIBC_PROBE (memory_tunable_malloc_debug, 2, value, mp_.malloc_debug); + mp_.malloc_debug = value; + return 1; + } + return 0; +} +#endif + int __libc_mallopt (int param_number, int value) { 1.8.5.6
With malloc debug now provided by a different library (libc_malloc_debug.so), I don't think a tunable makes sense anymore.