talloc and memory leak debugging
v2 cover talloc and memory leak debugging
This picks up the talloc series previously sent by Ahmad. I also added the memory leak debugging patch tooling for sandbox.
This series includes my review comments to v1.
Changes since v1:
- pass originally allocated pointer to malloc_usable_size()
- align talloc allocations to 8 bytes
- Use __lsan_do_recoverable_leak_check() instead of __lsan_do_leak_check() to keep barebox running after memory leaks have been detected
---
Ahmad Fatoum (4):
lib: add talloc for overlaying a tree onto allocations
test: self: add talloc selftest
hush: fix memory leaks
sandbox: add memory leak debugging tooling around LeakSanitizer
arch/sandbox/Makefile | 2 +- arch/sandbox/os/libc_malloc.c | 9 + commands/Kconfig | 9 + commands/Makefile | 1 + commands/checkleak.c | 52 ++++++ common/Kconfig.debug | 6 + common/hush.c | 41 +++-- include/malloc.h | 6 + include/talloc.h | 43 +++++ include/xfuncs.h | 6 + lib/Makefile | 1 + lib/talloc.c | 415 ++++++++++++++++++++++++++++++++++++++++++ lib/xfuncs.c | 20 ++ test/self/Kconfig | 6 + test/self/Makefile | 1 + test/self/talloc.c | 153 ++++++++++++++++ 16 files changed, 754 insertions(+), 17 deletions(-)
---
base-commit: 594850b2de53300ae25c103e4ec88b2dac418106
change-id: 20251107-talloc-68c86806dc53
Best regards,
Signature · 1 line
Sascha Hauer <[email protected]>
PATCH v2 1/4 lib: add talloc for overlaying a tree onto allocations
From: Ahmad Fatoum <[email protected]>
Talloc is the hierarchical allocator used by Samba, which keeps all allocated buffers in a tree, where each child may not live longer than its parent. When a parent is talloc_free'd, all its children are as well. This can simplify memory management a great deal.
In barebox, this can also be used to implement devm_.
The implementation here is based on MIT-licensed:
https://github.com/esneider/talloc
But the API was adapted to the Samba3 talloc API, as described here:
https://linux.die.net/man/3/talloc
https://talloc.samba.org/talloc/doc/html/index.html
Support for destructors is planned, but not implemented yet.
Also, the TLSF allocator has two spare bits in each block's size:
One bit could be used to mark talloc buffers to prevent use of talloc_ family functions with non-talloc allocated buffers.
--- include/talloc.h | 43 ++++++ include/xfuncs.h | 6 + lib/Makefile | 1 + lib/talloc.c | 415 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/xfuncs.c | 20 +++ 5 files changed, 485 insertions(+)
include/talloc.h new file +43 -0
@@ -0,0 +1,43 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/** +* Talloc is a replacement for the standard memory allocation routines that +* provides structure aware allocations. +* +* @author Dario Sneidermanis +*/ + +#ifndef __TALLOC_H__ +#define __TALLOC_H__ + +#include <linux/types.h> +#include <linux/compiler_types.h> + +struct talloc { +struct talloc *child; +struct talloc *next; +union { +struct talloc *prev; +struct talloc *parent;/* Valid only when is_first(mem) */ + }; +}__aligned(8) ;/* align to 8 byte to maintain malloc alignment */ + +void mem_set_parent (struct talloc *child,struct talloc *parent); + +void *talloc_new (const void *parent); +size_t talloc_usable_size (void *mem); +void *talloc_size (const void *parent,size_t size); +void *talloc_zero_size (const void *parent,size_t size); +void *talloc_realloc_size (const void *parent,void *mem,size_t size); +void talloc_free (void *mem); + +char *talloc_strdup (const void *parent,const char *str); +const char *talloc_strdup_const (const void *parent,const char *str); +void talloc_free_const (const void *ptr); + +void *talloc_parent (const void *mem); +void talloc_steal (const void *parent,void *mem); +void talloc_steal_children (const void *parent,void *mem); + +size_t talloc_total_blocks (const void *mem); + +#endif /* __TALLOC_H__ */
include/xfuncs.h +6 -0
@@ -26,6 +26,9 @@ wchar_t *xstrdup_wchar(const wchar_t *src);wchar_t *xstrdup_char_to_wchar (const char *src);char *xstrdup_wchar_to_char (const wchar_t *src); +void *xtalloc_size (const void *parent,size_t size)__xalloc_size (2 ); +void *xtalloc_zero_size (const void *parent,size_t size)__xalloc_size (2 ); +#else static inline void *xmalloc (size_t size) {BUG (); }
@@ -43,6 +46,9 @@ static inline __printf(2, 3) char *xrasprintf(char *str, const char *fmt, ...) {static inline wchar_t *xstrdup_wchar (const wchar_t *src) {BUG (); }static inline wchar_t *xstrdup_char_to_wchar (const char *src) {BUG (); }static inline char *xstrdup_wchar_to_char (const wchar_t *src) {BUG (); } + +static inline void *xtalloc_size (const void *paent,size_t size) {BUG (); } +static inline void *xtalloc_zero_size (const void *paent,size_t size) {BUG (); }#endif #endif /* __XFUNCS_H */
lib/Makefile +1 -0
@@ -12,6 +12,7 @@ obj-$(CONFIG_VERSION_CMP) += strverscmp.o obj-y += strtox.o obj-y += kstrtox.o obj-y += vsprintf.o +obj-y += talloc.o obj-$(CONFIG_KASAN) += kasan/ obj-pbl-$(CONFIG_STACKPROTECTOR) += stackprot.o pbl-$(CONFIG_PBL_CONSOLE) += vsprintf.o
lib/talloc.c new file +415 -0
415 lines not on this page · raw message
lib/xfuncs.c +20 -0
@@ -19,6 +19,7 @@#include <common.h> #include <malloc.h> +#include <talloc.h> #include <module.h> #include <wchar.h>
@@ -44,6 +45,17 @@ void *xmalloc(size_t size) }EXPORT_SYMBOL (xmalloc); +void *xtalloc_size (const void *parent,size_t size) +{ +void *p =NULL ; + +if (!(p =talloc_size (parent, size))) +enomem_panic (size); + +return p; +} +EXPORT_SYMBOL (xtalloc_size); +void *xrealloc (void *ptr,size_t size) {void *p =NULL ;
@@ -63,6 +75,14 @@ void *xzalloc(size_t size) }EXPORT_SYMBOL (xzalloc); +void *xtalloc_zero_size (const void *parent,size_t size) +{ +void *ptr =xtalloc_size (parent, size); +memset (ptr,0 , size); +return ptr; +} +EXPORT_SYMBOL (xtalloc_zero_size); +char *xstrdup (const char *s) {char *p;
Signature · 1 line
2.47.3
PATCH v2 2/4 test: self: add talloc selftest
In reply to Sascha HauerFrom: Ahmad Fatoum <[email protected]>
This self test exercises the hierarchical properties of talloc.
--- test/self/Kconfig | 6 +++ test/self/Makefile | 1 + test/self/talloc.c | 153 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 160 insertions(+)
test/self/Kconfig +6 -0
@@ -48,6 +48,7 @@ config SELFTEST_ENABLE_ALL select SELFTEST_IDR select SELFTEST_TLV select SELFTEST_DM + select SELFTEST_TALLOC help Selects all self-tests compatible with current configuration
@@ -61,6 +62,11 @@ config SELFTEST_MALLOC help Tests barebox memory allocator +config SELFTEST_TALLOC + bool "talloc() selftest" + help + Tests barebox talloc allocator + config SELFTEST_PRINTF bool "printf selftest" help
test/self/Makefile +1 -0
@@ -3,6 +3,7 @@ obj-$(CONFIG_SELFTEST) += core.o obj-$(CONFIG_SELFTEST_RANGE) += range.o obj-$(CONFIG_SELFTEST_MALLOC) += malloc.o +obj-$(CONFIG_SELFTEST_TALLOC) += talloc.o obj-$(CONFIG_SELFTEST_PRINTF) += printf.o CFLAGS_printf.o += -Wno-format-security -Wno-format obj-$(CONFIG_SELFTEST_PROGRESS_NOTIFIER) += progress-notifier.o
test/self/talloc.c new file +153 -0
153 lines not on this page · raw message
Signature · 1 line
2.47.3
PATCH v2 3/4 hush: fix memory leaks
In reply to Sascha HauerFrom: Ahmad Fatoum <[email protected]>
The memory leaks in hush have been bothered me for years and every time I look into it, I give up after a while, because the code is convoluted.
Talloc shines here as we can just allocate a "zero-size" talloc object as parent for a context and associate all allocations from that context with it.
Then when that context is free'd all children will be freed as well.
--- common/hush.c | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-)
common/hush.c +25 -16
@@ -97,6 +97,7 @@#define pr_fmt (fmt) "hush: " fmt +#include <talloc.h> #include <malloc.h> /* malloc, free, realloc*/ #include <xfuncs.h> #include <linux/ctype.h> /* isalpha, isdigit */
@@ -180,6 +181,7 @@ struct option {/* This holds pointers to the various results of parsing */ struct p_context { +const void *scope;struct child_prog *child;struct pipe *list_head;struct pipe *pipe;
@@ -308,7 +310,7 @@ static int run_pipe_real(struct p_context *ctx, struct pipe *pi);/* variable assignment: */ static int is_assignment (const char *s);/* data structure manipulation: */ -static void initialize_context (struct p_context *ctx); +static void initialize_context (struct p_context *ctx,bool newscope);static void release_context (struct p_context *ctx);static int done_word (o_string *dest,struct p_context *ctx);static int done_command (struct p_context *ctx);
@@ -435,7 +437,7 @@ static char *getprompt(void)if (prompt_command) {unsigned int lr = last_return_code; -initialize_context (&ctx); +initialize_context (&ctx, false);parse_string_outer (&ctx, prompt_command,FLAG_PARSE_SEMICOLON );release_context (&ctx);
@@ -837,7 +839,7 @@ static int run_pipe_real(struct p_context *ctx, struct pipe *pi)char * str =NULL ;struct p_context ctx1; -initialize_context (&ctx1); +initialize_context (&ctx1, true); str =make_string ((child->argv + i)); rcode =parse_string_outer (&ctx1, str,FLAG_EXIT_FROM_LOOP |FLAG_REPARSING );
@@ -1062,7 +1064,8 @@ static int free_pipe(struct pipe *pi, int indent) } } -free (pi->progs);/* children are an array, they get freed all at once */ +/* children are an array, they get freed all at once */ +talloc_free (pi->progs); pi->progs =NULL ;return ret_code;
@@ -1079,7 +1082,7 @@ static int free_pipe_list(struct pipe *head, int indent)final_printf ("%s pipe followup code %d\n" ,indenter (indent), pi->followup); next = pi->next; pi->next =NULL ; -free (pi); +talloc_free (pi); }return rcode; }
@@ -1182,16 +1185,18 @@ static int is_assignment(const char *s) } -static struct pipe *new_pipe (void ) +static struct pipe *new_pipe (struct p_context *ctx) { -return xzalloc (sizeof (struct pipe )); +return xtalloc_zero_size (ctx->scope,sizeof (struct pipe )); } -static void initialize_context (struct p_context *ctx) +static void initialize_context (struct p_context *ctx,bool newscope) { +if (newscope) + ctx->scope =talloc_new (NULL ); ctx->pipe =NULL ; ctx->child =NULL ; - ctx->list_head =new_pipe (); + ctx->list_head =new_pipe (ctx); ctx->pipe = ctx->list_head; ctx->w =RES_NONE ; ctx->stack =NULL ;
@@ -1211,6 +1216,7 @@ static void release_context(struct p_context *ctx) free(opt); }#endif +talloc_free ((void *)ctx->scope); }/* normal return is 0
@@ -1269,7 +1275,7 @@ static int reserved_word(o_string *dest, struct p_context *ctx)return 1 ; } *new = *ctx;/* physical copy */ -initialize_context (ctx); +initialize_context (ctx, false); ctx->stack = new; }else if (ctx->w ==RES_NONE || !(ctx->old_flag & (1 << r->code))) {syntax_unexpected_token (r->literal);
@@ -1366,7 +1372,10 @@ static int done_command(struct p_context *ctx) }else {hush_debug ("%s: initializing\n" , __func__); } - pi->progs =xrealloc (pi->progs,sizeof (*pi->progs) * (pi->num_progs +1 )); + pi->progs =talloc_realloc_size (ctx->scope, pi->progs, +sizeof (*pi->progs) * (pi->num_progs +1 )); +if (!pi->progs) +panic ("enomem" ); prog = pi->progs + pi->num_progs; prog->glob_result.gl_pathv =NULL ;
@@ -1392,7 +1401,7 @@ static int done_pipe(struct p_context *ctx, pipe_style type) ctx->pipe->followup = type; ctx->pipe->r_mode = ctx->w; - new_p =new_pipe (); + new_p =new_pipe (ctx); ctx->pipe->next = new_p; ctx->pipe = new_p;
@@ -1705,7 +1714,7 @@ char *shell_expand(char *str) o.quote =1 ; -initialize_context (&ctx); +initialize_context (&ctx, false);parse_string (&o, &ctx, str);
@@ -1732,7 +1741,7 @@ static int parse_stream_outer(struct p_context *ctx, struct in_str *inp, int fla do { ctx->type = flag; - initialize_context(ctx); + initialize_context(ctx, false); update_ifs_map(); if (!(flag & FLAG_PARSE_SEMICOLON) || (flag & FLAG_REPARSING))
@@ -1921,7 +1930,7 @@ int run_command(const char *cmd)if (!IS_ALLOWED (SCONFIG_SHELL ))return -EPERM ; -initialize_context (&ctx); +initialize_context (&ctx, true); ret =parse_string_outer (&ctx, cmd,FLAG_PARSE_SEMICOLON );release_context (&ctx);
@@ -1949,7 +1958,7 @@ static int source_script(const char *path, int argc, char *argv[])char *script;int ret; -initialize_context (&ctx); +initialize_context (&ctx, true); ctx.global_argc = argc; ctx.global_argv = argv;
Signature · 1 line
2.47.3
PATCH v2 4/4 sandbox: add memory leak debugging tooling around LeakSanitizer
In reply to Sascha HauerFrom: Ahmad Fatoum <[email protected]>
When enabled, this allows calling barebox_memleak_check() or running the checkleak command to instruct LeakSanitizer to sweep the memory and find unreferenced allocations.
LeakSanitizier is also enabled along AddressSanitizer and runs on AMD64 Linux automatically on exit already.
--- arch/sandbox/Makefile | 2 +- arch/sandbox/os/libc_malloc.c | 9 ++++++++ commands/Kconfig | 9 ++++++++ commands/Makefile | 1 + commands/checkleak.c | 52 +++++++++++++++++++++++++++++++++++++++++++ common/Kconfig.debug | 6 +++++ include/malloc.h | 6 +++++ 7 files changed, 84 insertions(+), 1 deletion(-)
arch/sandbox/Makefile +1 -1
@@ -36,7 +36,7 @@ TEXT_BASE = $(CONFIG_TEXT_BASE) SANDBOX_PBL2PROPER_GLUE_SYMS := \ putchar errno setjmp longjmp \ - malloc_stats memalign malloc free realloc calloc brk sbrk + malloc_stats memalign malloc free realloc calloc memleak_check brk sbrk KBUILD_CFLAGS += $(foreach s,$(SANDBOX_PBL2PROPER_GLUE_SYMS),-D$(s)=barebox_$(s))
arch/sandbox/os/libc_malloc.c +9 -0
@@ -98,3 +98,12 @@ void *barebox_calloc(size_t n, size_t elem_size)return mem; } + +#ifdef CONFIG_DEBUG_MEMLEAK +void barebox_memleak_check (void ) +{ +void __lsan_do_recoverable_leak_check (void ); + +__lsan_do_recoverable_leak_check (); +} +#endif
commands/Kconfig +9 -0
@@ -210,6 +210,15 @@ config CMD_MEMINFO system bytes = 282616 in use bytes = 274752 +config CMD_CHECKLEAK + tristate + prompt "checkleak" + depends on DEBUG_MEMLEAK + default y + help + List memory leaks encountered since the last time + the command ran. + config CMD_ARM_MMUINFO bool "mmuinfo command" depends on CPU_V7 || CPU_V8
commands/Makefile +1 -0
@@ -47,6 +47,7 @@ obj-$(CONFIG_CMD_TRUNCATE) += truncate.o obj-$(CONFIG_CMD_SYNC) += sync.o obj-$(CONFIG_CMD_FLASH) += flash.o obj-$(CONFIG_CMD_MEMINFO) += meminfo.o +obj-$(CONFIG_CMD_CHECKLEAK) += checkleak.o obj-$(CONFIG_CMD_TIMEOUT) += timeout.o obj-$(CONFIG_CMD_READLINE) += readline.o obj-$(CONFIG_CMD_SETENV) += setenv.o
commands/checkleak.c new file +52 -0
52 lines not on this page · raw message
common/Kconfig.debug +6 -0
@@ -124,6 +124,12 @@ config PRINTF_FULL source "lib/Kconfig.ubsan" source "lib/kasan/Kconfig" +config DEBUG_MEMLEAK + bool "barebox memory leak detector" + depends on MALLOC_LIBC && ASAN + help + Say Y here if you want to enable LeakSanitizer. + config COMPILE_TEST bool "compile-test drivers of other platforms" default n
include/malloc.h +6 -0
@@ -88,4 +88,10 @@ static inline bool want_init_on_free(void) return IS_ENABLED(CONFIG_INIT_ON_FREE_DEFAULT_ON); } +#ifdef CONFIG_DEBUG_MEMLEAK +void memleak_check (void ); +#else +static inline void memleak_check (void ) {} +#endif +#endif /* __MALLOC_H */
Signature · 1 line
2.47.3
Sascha Hauer
AppliedQuoted 5 lines · Sascha Hauer
This picks up the talloc series previously sent by Ahmad. I also added the memory leak debugging patch tooling for sandbox.
This series includes my review comments to v1.
Quoted 8 lines
Changes since v1:
- pass originally allocated pointer tomalloc_usable_size()
- align talloc allocations to 8 bytes
- Use__lsan_do_recoverable_leak_check()instead of__lsan_do_leak_check()to keep barebox running after memory leaks have been detected[...]
Applied, thanks!
[1/4] lib: add talloc for overlaying a tree onto allocations
https://git.pengutronix.de/cgit/barebox/commit/?id=9c35b3c2ac7e (link may not be stable)
[2/4] test: self: add talloc selftest
https://git.pengutronix.de/cgit/barebox/commit/?id=2e65ba56f3ee (link may not be stable)
[3/4] hush: fix memory leaks
https://git.pengutronix.de/cgit/barebox/commit/?id=f86d68979fa6 (link may not be stable)
[4/4] sandbox: add memory leak debugging tooling around LeakSanitizer
https://git.pengutronix.de/cgit/barebox/commit/?id=bfa34bbddb8a (link may not be stable)
Best regards,
Signature · 1 line
Sascha Hauer <[email protected]>