[Concept,26/37] mcheck: add pedantic mode support

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

Commit Message

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

Add mcheck_pedantic_prehook() calls to dlmalloc, dlrealloc,
dlmemalign, and dlcalloc wrapper functions. Also add the
mcheck_pedantic() and mcheck_check_all() API functions.

The pedantic mode is runtime controlled, so the registry hooks
are called on every allocation operation.

Changes from original commit:
- Uses dl* function names instead of mALLOc style names

Signed-off-by: Eugene Uriev <eugeneuriev@gmail.com>
Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
(cherry picked from commit 18c1bfafe0ccdd3229d91bbb07ed942e9f233f93)
---

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

Patch

diff --git a/common/dlmalloc.c b/common/dlmalloc.c
index c9eb18787e8..4ee7c6c133f 100644
--- a/common/dlmalloc.c
+++ b/common/dlmalloc.c
@@ -5596,6 +5596,7 @@  size_t dlmalloc_usable_size(const void* mem) {
 
 void *dlmalloc(size_t bytes)
 {
+	mcheck_pedantic_prehook();
 	size_t fullsz = mcheck_alloc_prehook(bytes);
 	void *p = dlmalloc_impl(fullsz);
 
@@ -5608,6 +5609,7 @@  void dlfree(void *mem) { dlfree_impl(mcheck_free_prehook(mem)); }
 
 void *dlrealloc(void *oldmem, size_t bytes)
 {
+	mcheck_pedantic_prehook();
 	if (bytes == 0) {
 		if (oldmem)
 			dlfree(oldmem);
@@ -5628,6 +5630,7 @@  void *dlrealloc(void *oldmem, size_t bytes)
 
 void *dlmemalign(size_t alignment, size_t bytes)
 {
+	mcheck_pedantic_prehook();
 	size_t fullsz = mcheck_memalign_prehook(alignment, bytes);
 	void *p = dlmemalign_impl(alignment, fullsz);
 
@@ -5640,6 +5643,7 @@  void *dlmemalign(size_t alignment, size_t bytes)
 
 void *dlcalloc(size_t n, size_t elem_size)
 {
+	mcheck_pedantic_prehook();
 	/* NB: no overflow check here */
 	size_t fullsz = mcheck_alloc_prehook(n * elem_size);
 	void *p = dlcalloc_impl(1, fullsz);
@@ -5650,12 +5654,20 @@  void *dlcalloc(size_t n, size_t elem_size)
 }
 
 /* mcheck API */
+int mcheck_pedantic(mcheck_abortfunc_t f)
+{
+	mcheck_initialize(f, 1);
+	return 0;
+}
+
 int mcheck(mcheck_abortfunc_t f)
 {
 	mcheck_initialize(f, 0);
 	return 0;
 }
 
+void mcheck_check_all(void) { mcheck_pedantic_check(); }
+
 enum mcheck_status mprobe(void *__ptr) { return mcheck_mprobe(__ptr); }
 #endif /* MCHECK_HEAP_PROTECTION */