Skip to contentSkip to conversation

sandbox: add memory leak debugging tooling around LeakSanitizer

PATCH Applied Ahmad Fatoum 3 participants 6 messages Oct 27, 2025 – Nov 10, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sandbox: add memory leak debugging tooling around LeakSanitizer

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(-)
 create mode 100644 commands/checkleak.c
arch/sandbox/Makefile +1 -1
index fdff09c07cb9..1b1774cc4b0a 100644
@@ -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
index bb4fb1c9ead4..fee4550f327c 100644
@@ -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_leak_check(void);
+
+	__lsan_do_leak_check();
+}
+#endif
commands/Kconfig +9 -0
index 78b1e69dd38e..c7c03a65477b 100644
@@ -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
index 858e0c257eba..8fffac8fd442 100644
@@ -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
index 9c70555eb83c..2de885ebb3f8 100644
@@ -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
index 81ab0f457b01..31a2ff1b3d8e 100644
@@ -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
#1 lore raw
Quoted 21 lines · Ahmad Fatoum

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.

commands/checkleak.c new file +14 -0
+static int do_checkleak(int argc, char *argv[])
+{
+	unsigned int count;
+	int opt;
+
+	while((opt = getopt(argc, argv, "l:")) > 0) {
+		switch(opt) {
+		case 'l':
+			if (kstrtouint(optarg, 0, &count))
+				return COMMAND_ERROR;
+			(void)malloc(count);
+			break;
+		default:
+			return COMMAND_ERROR_USAGE;

commands/checkleak.c line 32

Quoted 10 lines
commands/checkleak.c new file +10 -0
+		}
+	}
+
+	argv += optind;
+	argc -= optind;
+
+	if (argc)
+		return COMMAND_ERROR_USAGE;
+
+	memleak_check();

barebox exits in this call. Is this intended?

Sascha

Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#2 lore raw

Hi Sascha,

Quoted 26 lines · Sascha Hauer
Quoted 25 lines · Ahmad Fatoum

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.

commands/checkleak.c new file +18 -0
+static int do_checkleak(int argc, char *argv[])
+{
+	unsigned int count;
+	int opt;
+
+	while((opt = getopt(argc, argv, "l:")) > 0) {
+		switch(opt) {
+		case 'l':
+			if (kstrtouint(optarg, 0, &count))
+				return COMMAND_ERROR;
+			(void)malloc(count);
+			break;
+		default:
+			return COMMAND_ERROR_USAGE;
+		}
+	}
+
+	argv += optind;
Quoted 8 lines

commands/checkleak.c line 32

Quoted 6 lines
commands/checkleak.c new file +6 -0
+	argc -= optind;
+
+	if (argc)
+		return COMMAND_ERROR_USAGE;
+
+	memleak_check();

barebox exits in this call. Is this intended?

Until I figure out how to suppress it, yes, unfortunately. Haven't dug into the source code yet.

On the bright side my talloc series removes the long standing leak in hush, so it's less of a bother. :D

Cheers,
Ahmad

Quoted 3 lines

Sascha

Signature · 4 lines
Pengutronix e.K.                  |                             |
Steuerwalder Str. 21              | http://www.pengutronix.de/  |
31137 Hildesheim, Germany         | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686  | Fax:   +49-5121-206917-5555 |
#3 lore raw
Quoted 38 lines · Ahmad Fatoum

Hi Sascha,

Quoted 26 lines · Sascha Hauer
Quoted 25 lines · Ahmad Fatoum

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.

commands/checkleak.c new file +18 -0
+static int do_checkleak(int argc, char *argv[])
+{
+	unsigned int count;
+	int opt;
+
+	while((opt = getopt(argc, argv, "l:")) > 0) {
+		switch(opt) {
+		case 'l':
+			if (kstrtouint(optarg, 0, &count))
+				return COMMAND_ERROR;
+			(void)malloc(count);
+			break;
+		default:
+			return COMMAND_ERROR_USAGE;
+		}
+	}
+
+	argv += optind;
Quoted 8 lines

commands/checkleak.c line 32

Quoted 6 lines
commands/checkleak.c new file +6 -0
+	argc -= optind;
+
+	if (argc)
+		return COMMAND_ERROR_USAGE;
+
+	memleak_check();

barebox exits in this call. Is this intended?

Quoted 2 lines

Until I figure out how to suppress it, yes, unfortunately. Haven't dug into the source code yet.

Until this is fixed we might want to adjust this text to reality:

BAREBOX_CMD_HELP_TEXT("list memory leaks encountered since the last time") BAREBOX_CMD_HELP_TEXT("the command ran.")

Quoted 3 lines

On the bright side my talloc series removes the long standing leak in hush, so it's less of a bother. :D

Indeed, it's useful as is, but let's mention the issue somewhere.

Sascha

Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#4 lore raw
In reply to Ahmad Fatoum
Quoted 38 lines · Ahmad Fatoum

Hi Sascha,

Quoted 26 lines · Sascha Hauer
Quoted 25 lines · Ahmad Fatoum

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.

commands/checkleak.c new file +18 -0
+static int do_checkleak(int argc, char *argv[])
+{
+	unsigned int count;
+	int opt;
+
+	while((opt = getopt(argc, argv, "l:")) > 0) {
+		switch(opt) {
+		case 'l':
+			if (kstrtouint(optarg, 0, &count))
+				return COMMAND_ERROR;
+			(void)malloc(count);
+			break;
+		default:
+			return COMMAND_ERROR_USAGE;
+		}
+	}
+
+	argv += optind;
Quoted 8 lines

commands/checkleak.c line 32

Quoted 6 lines
commands/checkleak.c new file +6 -0
+	argc -= optind;
+
+	if (argc)
+		return COMMAND_ERROR_USAGE;
+
+	memleak_check();

barebox exits in this call. Is this intended?

Quoted 2 lines

Until I figure out how to suppress it, yes, unfortunately. Haven't dug into the source code yet.

With __lsan_do_recoverable_leak_check() instead of __lsan_do_leak_check() the progam doesn't exit.

Sascha

Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
Applied
#5 lore raw
In reply to Ahmad Fatoum
Quoted 8 lines · Ahmad Fatoum

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.

[...]

Applied, thanks!

[1/1] 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]>