[Concept,v2,17/29] test: malloc: Account for mcheck overhead in the large test

Message ID 20260103200510.3605009-18-sjg@u-boot.org
State New
Headers
Series Malloc debugging and test/py improvements |

Commit Message

Simon Glass Jan. 3, 2026, 8:04 p.m. UTC
  From: Simon Glass <simon.glass@canonical.com>

The malloc_very_large() test fails when mcheck is enabled with large
CONFIG_MCHECK_CALLER_LEN because the 64K margin does not account for
the per-allocation overhead (header + canaries).

Use a larger margin (256K) when mcheck is enabled to ensure the test
passes regardless of the mcheck caller length setting.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
---

(no changes since v1)

 test/common/malloc.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
  

Patch

diff --git a/test/common/malloc.c b/test/common/malloc.c
index 9fdc1789645..436aac503be 100644
--- a/test/common/malloc.c
+++ b/test/common/malloc.c
@@ -535,11 +535,21 @@  COMMON_TEST(common_test_mallinfo, 0);
 /* Test allocating a very large size */
 static int common_test_malloc_very_large(struct unit_test_state *uts)
 {
-	size_t size, before;
+	size_t size, before, margin;
 	void *ptr;
 
 	before = get_alloced_size();
-	size = TOTAL_MALLOC_LEN - before - SZ_64K;
+
+	/*
+	 * When mcheck is enabled, it adds overhead per allocation (header +
+	 * canaries). With large CONFIG_MCHECK_CALLER_LEN, this can be
+	 * significant. Use a larger margin to account for mcheck overhead.
+	 */
+	if (CONFIG_IS_ENABLED(MCHECK_HEAP_PROTECTION))
+		margin = SZ_256K;
+	else
+		margin = SZ_64K;
+	size = TOTAL_MALLOC_LEN - before - margin;
 
 	ptr = malloc(size);
 	ut_assertnonnull(ptr);