[Concept,17/37] dlmalloc: calloc: fix zeroing early allocations

Message ID 20251201170529.3237986-18-sjg@u-boot.org
State New
Headers
Series malloc: Import dlmalloc 2.8.6 |

Commit Message

Simon Glass Dec. 1, 2025, 5:04 p.m. UTC
  From: Simon Glass <simon.glass@canonical.com>

When full malloc is enabled and SYS_MALLOC_F is also enabled, the
simple pre-reloc heap is used before relocation. The calloc_must_clear
macro relies on chunk metadata which does not exist for simple malloc
allocations.

Use memset directly to zero out memory from simple malloc.

Changes from original commit:
- Port to dlcalloc() in dlmalloc 2.8.6
- Update memset() second arg to be a char

Signed-off-by: Simon Goldschmidt <simon.k.r.goldschmidt@gmail.com>
Reviewed-by: Tom Rini <trini@konsulko.com>
Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
(cherry picked from bb71a2d9dcd9c53aa4d4b8e4d26c24d9b59b74c3)
---

 common/dlmalloc.c | 9 +++++++++
 1 file changed, 9 insertions(+)
  

Patch

diff --git a/common/dlmalloc.c b/common/dlmalloc.c
index d53cbf2f2e1..a07166206dc 100644
--- a/common/dlmalloc.c
+++ b/common/dlmalloc.c
@@ -4877,6 +4877,15 @@  void* dlcalloc(size_t n_elements, size_t elem_size) {
       req = MAX_SIZE_T; /* force downstream failure on overflow */
   }
   mem = dlmalloc(req);
+#ifdef __UBOOT__
+#if CONFIG_IS_ENABLED(SYS_MALLOC_F)
+  /* For pre-reloc simple malloc, just zero the memory directly */
+  if (mem != 0 && !(gd->flags & GD_FLG_FULL_MALLOC_INIT)) {
+    memset(mem, '\0', req);
+    return mem;
+  }
+#endif
+#endif
   if (mem != 0 && calloc_must_clear(mem2chunk(mem)))
     memset(mem, 0, req);
   return mem;