From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@barebox.org>
Subject: [PATCH 06/19] test: self: talloc: add destructor tests
Date: Wed, 28 Oct 2026 10:14:40 +0100 [thread overview]
Message-ID: <20261028092442.3300753-7-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20261028092442.3300753-1-a.fatoum@pengutronix.de>
From: Ahmad Fatoum <a.fatoum@barebox.org>
Cover ordering relative to children and siblings, realloc and frees
from within destructors.
Assisted-by: Claude:fable-5.1
Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
test/self/talloc.c | 124 +++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 124 insertions(+)
diff --git a/test/self/talloc.c b/test/self/talloc.c
index a7bbf90b59fb..85fc74336f43 100644
--- a/test/self/talloc.c
+++ b/test/self/talloc.c
@@ -142,11 +142,135 @@ static void talloc_corner_cases(void)
talloc_free(root);
}
+static char destructor_log[16];
+static unsigned destructor_log_len;
+
+static void reset_destructor_log(void)
+{
+ memset(destructor_log, 0, sizeof(destructor_log));
+ destructor_log_len = 0;
+}
+
+static void log_destructor(void *data)
+{
+ if (destructor_log_len < sizeof(destructor_log) - 1)
+ destructor_log[destructor_log_len++] = *(char *)data;
+}
+
+static void self_free_destructor(void *data)
+{
+ log_destructor(data);
+ /* Must do nothing, we are being freed already */
+ talloc_free(data);
+}
+
+static void *other_to_free;
+
+static void other_free_destructor(void *data)
+{
+ log_destructor(data);
+ talloc_free(other_to_free);
+}
+
+/* Chunk holding @c, with a destructor that is passed the chunk */
+static void *talloc_char(const void *parent, char c, void (*fn)(void *))
+{
+ char *mem = talloc_size(parent, 1);
+
+ if (!selftest_check(mem))
+ return NULL;
+
+ *mem = c;
+ selftest_check(talloc_add_destructor(mem, fn, mem) == 0);
+
+ return mem;
+}
+
+static void talloc_destructor_tests(void)
+{
+ void *root, *a, *b, *c, *d, *x, *tmp;
+
+ selftest_check(talloc_add_destructor(NULL, log_destructor, "") == -EINVAL);
+
+ root = talloc_new(NULL);
+
+ /* Children are freed before their parent's destructors run */
+ reset_destructor_log();
+ a = talloc_char(root, 'a', log_destructor);
+ b = talloc_char(a, 'b', log_destructor);
+ c = talloc_char(a, 'c', log_destructor);
+ d = talloc_char(c, 'd', log_destructor);
+ if (!a || !b || !c || !d)
+ return;
+
+ talloc_free(a);
+ selftest_check(!strcmp(destructor_log, "dcba"));
+ selftest_check(talloc_total_blocks(root) == 1);
+
+ /* Destructors run in reverse order, interleaved with frees */
+ reset_destructor_log();
+ a = talloc_new(root);
+ selftest_check(talloc_add_destructor(a, log_destructor, "1") == 0);
+ b = talloc_char(a, 'b', log_destructor);
+ selftest_check(talloc_add_destructor(a, log_destructor, "2") == 0);
+ selftest_check(talloc_total_blocks(a) == 2);
+ talloc_free(a);
+ selftest_check(!strcmp(destructor_log, "2b1"));
+
+ /* Destructors survive realloc */
+ reset_destructor_log();
+ a = talloc_size(root, 1);
+ selftest_check(talloc_add_destructor(a, log_destructor, "a") == 0);
+ tmp = talloc_realloc_size(root, a, SZ_64K);
+ if (selftest_check(tmp))
+ a = tmp;
+ talloc_free(a);
+ selftest_check(!strcmp(destructor_log, "a"));
+
+ /* Freeing a chunk from its own destructor does nothing */
+ reset_destructor_log();
+ a = talloc_char(root, 'a', self_free_destructor);
+ talloc_free(a);
+ selftest_check(!strcmp(destructor_log, "a"));
+ selftest_check(talloc_total_blocks(root) == 1);
+
+ /* Same for an ancestor that is being freed */
+ reset_destructor_log();
+ x = talloc_char(root, 'x', log_destructor);
+ a = talloc_char(x, 'a', other_free_destructor);
+ other_to_free = x;
+ talloc_free(x);
+ selftest_check(!strcmp(destructor_log, "ax"));
+ selftest_check(talloc_total_blocks(root) == 1);
+
+ /* Ancestors of the freed chunk can be freed */
+ reset_destructor_log();
+ x = talloc_char(root, 'x', log_destructor);
+ a = talloc_char(x, 'a', log_destructor);
+ b = talloc_char(a, 'b', other_free_destructor);
+ other_to_free = x;
+ talloc_free(a);
+ selftest_check(!strcmp(destructor_log, "bxa"));
+ selftest_check(talloc_total_blocks(root) == 1);
+
+ /* So can siblings that are still to be freed */
+ reset_destructor_log();
+ a = talloc_char(root, 'a', log_destructor);
+ other_to_free = talloc_char(a, 'b', log_destructor);
+ c = talloc_char(a, 'c', other_free_destructor);
+ talloc_free(a);
+ selftest_check(!strcmp(destructor_log, "cba"));
+ selftest_check(talloc_total_blocks(root) == 1);
+
+ talloc_free(root);
+}
+
static void test_talloc(void)
{
talloc_basic_tests();
talloc_hierarchy_tests();
talloc_stress_tests();
talloc_corner_cases();
+ talloc_destructor_tests();
}
bselftest(core, test_talloc);
--
2.47.3
next prev parent reply other threads:[~2026-10-28 9:26 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-28 9:14 [PATCH 00/19] driver: add devm_ support Ahmad Fatoum
2026-10-28 9:14 ` [PATCH 01/19] commands: devunbind: refuse to unbind devices that were never bound Ahmad Fatoum
2026-10-28 9:14 ` [PATCH 02/19] commands: devunbind: don't poison the active list entry Ahmad Fatoum
2026-10-28 9:14 ` [PATCH 03/19] talloc: remove unused talloc_steal() and talloc_steal_children() Ahmad Fatoum
2026-10-28 9:14 ` [PATCH 04/19] talloc: free children in a loop Ahmad Fatoum
2026-10-28 9:14 ` [PATCH 05/19] talloc: add support for destructors Ahmad Fatoum
2026-10-28 9:14 ` Ahmad Fatoum [this message]
2026-10-28 9:14 ` [PATCH 07/19] talloc: add talloc_run_destructors() Ahmad Fatoum
2026-10-28 9:14 ` [PATCH 08/19] mfd: pca9450: don't free device-managed memory in probe error path Ahmad Fatoum
2026-10-28 9:14 ` [PATCH 09/19] video: backlight-pwm: allocate brightness levels with devm_kcalloc Ahmad Fatoum
2026-10-28 9:14 ` [PATCH 10/19] driver: implement devm_ allocation helpers on top of talloc Ahmad Fatoum
2026-10-28 9:14 ` [PATCH 11/19] devres: Provide kmalloc_obj*-style API for devm_kmalloc Ahmad Fatoum
2026-10-28 9:14 ` [PATCH 12/19] memory: atmel-ebi: use generic devm_kzalloc() Ahmad Fatoum
2026-10-28 9:14 ` [PATCH 13/19] driver: add devm_add_action() Ahmad Fatoum
2026-10-28 9:14 ` [PATCH 14/19] test: self: add devm_ selftest Ahmad Fatoum
2026-10-28 9:14 ` [PATCH 15/19] driver: add devm_request_mem_resource() Ahmad Fatoum
2026-10-28 9:14 ` [PATCH 16/19] driver: make dev_platform_ioremap_resource() device-managed Ahmad Fatoum
2026-10-28 9:14 ` [PATCH 17/19] treewide: rename dev_platform_ioremap_resource() to devm_platform_ioremap_resource() Ahmad Fatoum
2026-10-28 9:14 ` [PATCH 18/19] driver: make dev_platform_get_and_ioremap_resource() device-managed Ahmad Fatoum
2026-10-28 9:14 ` [PATCH 19/19] serial: stm32: request the registers with devm_request_mem_resource() Ahmad Fatoum
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=20261028092442.3300753-7-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=a.fatoum@barebox.org \
--cc=barebox@lists.infradead.org \
/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