From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: mfe@pengutronix.de, Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 5/5] test: self: add cleanup selftest
Date: Tue, 22 Sep 2026 12:47:56 +0200 [thread overview]
Message-ID: <20260922104813.317584-6-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20260922104813.317584-1-a.fatoum@pengutronix.de>
The new __free() handlers have no user in the tree yet, so add a
selftest that exercises them: it checks that leaving the scope really
runs the cleanup, that NULL and error pointers are never passed to free(),
and that no_free_ptr() inhibits the cleanup.
Assisted-by: Claude:opus-5-1m
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
test/self/Kconfig | 10 +++++
test/self/Makefile | 1 +
test/self/cleanup.c | 89 +++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 100 insertions(+)
create mode 100644 test/self/cleanup.c
diff --git a/test/self/Kconfig b/test/self/Kconfig
index 85a3ef790116..804fe6ac8d31 100644
--- a/test/self/Kconfig
+++ b/test/self/Kconfig
@@ -32,6 +32,7 @@ config SELFTEST_ENABLE_ALL
select SELFTEST_RANGE
select SELFTEST_PRINTF
select SELFTEST_MALLOC
+ select SELFTEST_CLEANUP
select SELFTEST_PROGRESS_NOTIFIER
select SELFTEST_OF_MANIPULATION
select SELFTEST_ENVIRONMENT_VARIABLES if ENVIRONMENT_VARIABLES
@@ -71,6 +72,15 @@ config SELFTEST_MALLOC
help
Tests barebox memory allocator
+config SELFTEST_CLEANUP
+ bool "scope-based cleanup selftest"
+ help
+ Tests the __free() cleanup handlers the barebox allocators define,
+ i.e. that leaving a scope frees the buffer, that NULL and error
+ pointers are never freed and that no_free_ptr() inhibits cleanup.
+
+ If unsure, say n.
+
config SELFTEST_TALLOC
bool "talloc() selftest"
help
diff --git a/test/self/Makefile b/test/self/Makefile
index 2bfdbb9949df..2fecab1f8ee3 100644
--- a/test/self/Makefile
+++ b/test/self/Makefile
@@ -4,6 +4,7 @@ obj-$(CONFIG_SELFTEST) += core.o
obj-$(CONFIG_SELFTEST_BASE64) += base64.o
obj-$(CONFIG_SELFTEST_RANGE) += range.o
obj-$(CONFIG_SELFTEST_MALLOC) += malloc.o
+obj-$(CONFIG_SELFTEST_CLEANUP) += cleanup.o
obj-$(CONFIG_SELFTEST_TALLOC) += talloc.o
obj-$(CONFIG_SELFTEST_PRINTF) += printf.o
CFLAGS_printf.o += -Wno-format-security -Wno-format
diff --git a/test/self/cleanup.c b/test/self/cleanup.c
new file mode 100644
index 000000000000..7a3b63cc897f
--- /dev/null
+++ b/test/self/cleanup.c
@@ -0,0 +1,89 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <common.h>
+#include <bselftest.h>
+#include <malloc.h>
+#include <linux/err.h>
+#include <linux/slab.h>
+
+BSELFTEST_GLOBALS();
+
+static void *freed[2];
+static int frees;
+static void count_free(void *mem)
+{
+ if (frees < ARRAY_SIZE(freed))
+ freed[frees] = mem;
+ frees++;
+ free(mem);
+}
+DEFINE_FREE(count_free, void *, if (!IS_ERR_OR_NULL(_T)) count_free(_T))
+
+static void test_cleanup(void)
+{
+ void *first, *second;
+
+ /* leaving the scope must free the buffer, and only then */
+ frees = 0;
+ {
+ void *p __free(count_free) = malloc(64);
+
+ assert_cond(p != NULL);
+ assert_cond(frees == 0);
+ first = p;
+ }
+ assert_cond(frees == 1);
+ assert_cond(freed[0] == first);
+ /* the variable defined last is freed first */
+ frees = 0;
+ {
+ void *p __free(count_free) = malloc(64);
+ void *q __free(count_free) = malloc(64);
+
+ first = p;
+ second = q;
+ }
+ assert_cond(frees == 2);
+ assert_cond(freed[0] == second);
+ assert_cond(freed[1] == first);
+
+ /* NULL must not reach the allocator */
+ {
+ void *p __free(free) = NULL;
+ void *q __free(free_sensitive) = NULL;
+ void *r __free(kfree) = NULL;
+ void *s __free(kfree_sensitive) = NULL;
+
+ assert_cond(p == NULL);
+ assert_cond(q == NULL);
+ assert_cond(r == NULL);
+ assert_cond(s == NULL);
+ }
+
+ /* and neither may error pointers */
+ {
+ void *p __free(free) = ERR_PTR(-EINVAL);
+ void *q __free(free_sensitive) = ERR_PTR(-EINVAL);
+ void *r __free(kfree) = ERR_PTR(-ENOMEM);
+ void *s __free(kfree_sensitive) = ERR_PTR(-ENOMEM);
+
+ assert_cond(IS_ERR(p));
+ assert_cond(IS_ERR(q));
+ assert_cond(IS_ERR(r));
+ assert_cond(IS_ERR(s));
+ }
+
+ /* no_free_ptr() inhibits the cleanup, so the buffer stays taken */
+ {
+ void *p __free(kfree) = kmalloc(64, GFP_KERNEL);
+ assert_cond(p != NULL);
+ first = no_free_ptr(p);
+ }
+ second = kmalloc(64, GFP_KERNEL);
+ assert_cond(second != first);
+ kfree(first);
+ kfree(second);
+}
+bselftest(core, test_cleanup);
--
2.47.3
next prev parent reply other threads:[~2026-09-22 10:49 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-22 10:47 [PATCH 0/5] malloc: add __free() cleanup handlers Ahmad Fatoum
2026-09-22 10:47 ` [PATCH 1/5] commands: rksecure: exit if invalid integer passed to -b Ahmad Fatoum
2026-09-22 10:47 ` [PATCH 2/5] firmware-zynqmp: don't ignore the kstrtouint() result in parse_reg() Ahmad Fatoum
2026-09-22 10:47 ` [PATCH 3/5] include: compiler: make __must_check a yes-op Ahmad Fatoum
2026-09-22 10:47 ` [PATCH 4/5] malloc: add __free() cleanup handlers Ahmad Fatoum
2026-09-22 10:47 ` Ahmad Fatoum [this message]
2026-09-23 6:33 ` [PATCH 0/5] " Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260922104813.317584-6-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=barebox@lists.infradead.org \
--cc=mfe@pengutronix.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox