[Concept,05/33] malloc: Find aligned allocations using the registry

Message ID 20260416023021.626949-6-sjg@u-boot.org
State New
Headers
Series Fix memory leaks and test pollution in sandbox tests |

Commit Message

Simon Glass April 16, 2026, 2:29 a.m. UTC
  From: Simon Glass <sjg@chromium.org>

The leak-check printer reads the mcheck header from the start of each
chunk, but for memalign() allocations the header is offset by aln_skip
to satisfy alignment. Those chunks end up with no caller in the leak
report, which hides up to several dozen leaks per test under a
'no-trace' bucket.

Use find_mcheck_hdr_in_chunk(), which walks the registry and accounts
for aln_skip, the same way malloc_dump_impl() already does.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 common/dlmalloc.c | 20 +++++++-------------
 1 file changed, 7 insertions(+), 13 deletions(-)
  

Patch

diff --git a/common/dlmalloc.c b/common/dlmalloc.c
index 6d0bdc5b64e..6cc703f06fc 100644
--- a/common/dlmalloc.c
+++ b/common/dlmalloc.c
@@ -7833,21 +7833,15 @@  static void print_new_allocs(struct malloc_leak_snap *snap)
 
 #if CONFIG_IS_ENABLED(MCHECK_HEAP_PROTECTION)
 				/*
-				 * Read the caller directly from the mcheck
-				 * header at the start of the chunk rather
-				 * than searching the registry, which may
-				 * have overflowed. Validate the canary first
-				 * to avoid printing garbage from chunks
-				 * allocated without mcheck (e.g. when mcheck
-				 * was temporarily disabled).
+				 * For memalign()ed chunks the header is
+				 * offset by aln_skip, so use the registry-
+				 * based lookup rather than assuming the
+				 * header is at chunk2mem(q).
 				 */
-				struct mcheck_hdr *hdr = mem;
-				int j;
+				struct mcheck_hdr *hdr;
 
-				for (j = 0; j < CANARY_DEPTH; j++)
-					if (hdr->canary.elems[j] != MAGICWORD)
-						break;
-				if (j == CANARY_DEPTH && hdr->caller[0])
+				hdr = find_mcheck_hdr_in_chunk(mem, sz);
+				if (hdr && hdr->caller[0])
 					caller = hdr->caller;
 #endif
 				printf("  %lx %zx %s\n", (ulong)mem, sz,