[Concept,23/35] malloc: Update the valloc functions to use mcheck wrappers

Message ID 20251210000737.180797-24-sjg@u-boot.org
State New
Headers
Series malloc: Add heap debugging commands and mcheck caller tracking |

Commit Message

Simon Glass Dec. 10, 2025, 12:07 a.m. UTC
  From: Simon Glass <simon.glass@canonical.com>

Fix dlposix_memalign(), dlvalloc(), and dlpvalloc() to call dlmemalign()
instead of dlmemalign_impl() or internal_memalign() directly. This
ensures these functions go through the mcheck wrappers when
CONFIG_MCHECK_HEAP_PROTECTION is enabled.

Without this fix, internal_memalign is undefined when mcheck is enabled,
causing a build error.

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

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

Patch

diff --git a/common/dlmalloc.c b/common/dlmalloc.c
index bcdb4d29424..e288d94d433 100644
--- a/common/dlmalloc.c
+++ b/common/dlmalloc.c
@@ -4096,7 +4096,7 @@  static void unlink_large_chunk(mstate M, tchunkptr X) {
  * When mcheck is enabled, internal calls must use the _impl functions
  * to avoid going through the mcheck wrappers which expect user pointers.
  */
-#ifdef CONFIG_MCHECK_HEAP_PROTECTION
+#if CONFIG_IS_ENABLED(MCHECK_HEAP_PROTECTION)
 #define internal_malloc(m, b) dlmalloc_impl(b)
 #define internal_free(m, mem) dlfree_impl(mem)
 #else
@@ -5827,39 +5827,32 @@  void* dlmemalign_impl(size_t alignment, size_t bytes) {
 
 int dlposix_memalign(void** pp, size_t alignment, size_t bytes) {
   void* mem = 0;
-  if (alignment == MALLOC_ALIGNMENT)
-    mem = dlmalloc_impl(bytes);
-  else {
-    size_t d = alignment / sizeof(void*);
-    size_t r = alignment % sizeof(void*);
-    if (r != 0 || d == 0 || (d & (d-SIZE_T_ONE)) != 0)
-      return EINVAL;
-    else if (bytes <= MAX_REQUEST - alignment) {
-      if (alignment <  MIN_CHUNK_SIZE)
-        alignment = MIN_CHUNK_SIZE;
-      mem = internal_memalign(gm, alignment, bytes);
-    }
-  }
+  size_t d = alignment / sizeof(void*);
+  size_t r = alignment % sizeof(void*);
+
+  if (r != 0 || d == 0 || (d & (d-SIZE_T_ONE)) != 0)
+    return EINVAL;
+  if (bytes > MAX_REQUEST - alignment)
+    return ENOMEM;
+  mem = dlmemalign(alignment, bytes);
   if (mem == 0)
     return ENOMEM;
-  else {
-    *pp = mem;
-    return 0;
-  }
+  *pp = mem;
+  return 0;
 }
 
 void* dlvalloc(size_t bytes) {
   size_t pagesz;
   ensure_initialization();
   pagesz = mparams.page_size;
-  return dlmemalign_impl(pagesz, bytes);
+  return dlmemalign(pagesz, bytes);
 }
 
 void* dlpvalloc(size_t bytes) {
   size_t pagesz;
   ensure_initialization();
   pagesz = mparams.page_size;
-  return dlmemalign_impl(pagesz, (bytes + pagesz - SIZE_T_ONE) & ~(pagesz - SIZE_T_ONE));
+  return dlmemalign(pagesz, (bytes + pagesz - SIZE_T_ONE) & ~(pagesz - SIZE_T_ONE));
 }
 
 void** dlindependent_calloc(size_t n_elements, size_t elem_size,