From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 2/2] test: self: malloc: fix false positive when malloc store is registered
Date: Tue, 21 Oct 2025 16:54:14 +0200 [thread overview]
Message-ID: <20251021145420.1274668-2-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20251021145420.1274668-1-a.fatoum@pengutronix.de>
If malloc_register_store has been called, a malloc of the total size of
the current malloc area may still succeed, because it requests a buffer
from the external store.
Adapt the test to account for this.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
common/tlsf_malloc.c | 5 +++++
include/malloc.h | 3 +++
test/self/malloc.c | 16 ++++++++++++----
3 files changed, 20 insertions(+), 4 deletions(-)
diff --git a/common/tlsf_malloc.c b/common/tlsf_malloc.c
index 9d55014117f0..7de39ebeef09 100644
--- a/common/tlsf_malloc.c
+++ b/common/tlsf_malloc.c
@@ -147,3 +147,8 @@ void malloc_register_store(void (*cb)(size_t bytes))
malloc_request_store = cb;
tlsf_register_store(tlsf_mem_pool, tlsf_request_store);
}
+
+bool malloc_store_is_registered(void)
+{
+ return malloc_request_store;
+}
diff --git a/include/malloc.h b/include/malloc.h
index 69ff23b4a058..81ab0f457b01 100644
--- a/include/malloc.h
+++ b/include/malloc.h
@@ -24,6 +24,9 @@
#ifdef CONFIG_MALLOC_TLSF
void *malloc_add_pool(void *mem, size_t bytes);
void malloc_register_store(void (*cb)(size_t bytes));
+bool malloc_store_is_registered(void);
+#else
+static inline bool malloc_store_is_registered(void) { return false; }
#endif
#if IN_PROPER
diff --git a/test/self/malloc.c b/test/self/malloc.c
index a60e95b2233f..cf307158fb7e 100644
--- a/test/self/malloc.c
+++ b/test/self/malloc.c
@@ -67,6 +67,7 @@ static void *__expect(void *ptr, bool expect,
static void test_malloc(void)
{
size_t mem_malloc_size = mem_malloc_end() - mem_malloc_start();
+ bool have_overcommit = false;
u8 *p, *tmp;
pr_debug("mem_malloc_size = %zu\n", mem_malloc_size);
@@ -78,7 +79,7 @@ static void test_malloc(void)
*/
if (IS_ENABLED(CONFIG_MALLOC_LIBC)) {
pr_info("built with host libc allocator: Skipping tests that may trigger overcommit\n");
- mem_malloc_size = 0;
+ have_overcommit = true;
}
p = expect_alloc_ok(malloc(1));
@@ -90,7 +91,7 @@ static void test_malloc(void)
p = expect_alloc_fail(malloc(RELOC_HIDE(MALLOC_MAX_SIZE, 1)));
free(p);
- if (mem_malloc_size) {
+ if (!have_overcommit) {
tmp = expect_alloc_fail(malloc(RELOC_HIDE(MALLOC_MAX_SIZE, -1)));
free(tmp);
} else {
@@ -108,19 +109,26 @@ static void test_malloc(void)
__expect_cond(*p == 0x42, true, "reread after realloc", __func__, __LINE__);
if (mem_malloc_size) {
- tmp = expect_alloc_fail(realloc(p, mem_malloc_size));
+ tmp = realloc(p, mem_malloc_size);
+ if (!malloc_store_is_registered())
+ __expect_cond(tmp, false, "realloc of mem_malloc_size", __func__, __LINE__);
+
if (tmp)
p = NULL;
free(tmp);
+ } else {
+ skipped_tests++;
+ }
+ if (!have_overcommit) {
tmp = expect_alloc_fail(realloc(p, RELOC_HIDE(MALLOC_MAX_SIZE, -1)));
if (tmp)
p = NULL;
free(tmp);
} else {
- skipped_tests += 2;
+ skipped_tests++;
}
free(p);
--
2.47.3
next prev parent reply other threads:[~2025-10-21 14:54 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-21 14:54 [PATCH 1/2] test: self: malloc: fix double free after expect_alloc_fail Ahmad Fatoum
2025-10-21 14:54 ` Ahmad Fatoum [this message]
2025-10-22 6:27 ` 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=20251021145420.1274668-2-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--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