public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Eugene Uriev <eugeneuriev@gmail.com>
To: Tom Rini <trini@konsulko.com>
Cc: u-boot@lists.denx.de, Eugene Uriev <eugeneuriev@gmail.com>
Subject: [PATCH 4/9] mcheck: integrate mcheck into dlmalloc.c
Date: Sun, 31 Mar 2024 23:03:22 +0300	[thread overview]
Message-ID: <20240331200327.29141-5-eugeneuriev@gmail.com> (raw)
In-Reply-To: <20240331200327.29141-1-eugeneuriev@gmail.com>

This changes are probable worth to be generalized in a
separate .h-file so, making it able to cover
libc-mallocs and others, without too much copy-paste.

But the malloc<=>mALLOc substitutions interfere
with an elegant way to do this.

Signed-off-by: Eugene Uriev <eugeneuriev@gmail.com>
---

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

diff --git a/common/dlmalloc.c b/common/dlmalloc.c
index 0813e7e8b1..8de15d7193 100644
--- a/common/dlmalloc.c
+++ b/common/dlmalloc.c
@@ -2225,6 +2225,73 @@ void cfree(mem) Void_t *mem;
 #endif
 
 
+#ifdef MCHECK_HEAP_PROTECTION
+ #include "mcheck_core.inc.h"
+ #if !__STD_C
+  #error "must have __STD_C"
+ #endif
+
+Void_t *mALLOc(size_t bytes)
+{
+	size_t fullsz = mcheck_alloc_prehook(bytes);
+	void *p = mALLOc_impl(fullsz);
+
+	if (!p)
+		return p;
+	return mcheck_alloc_posthook(p, bytes);
+}
+
+void fREe(Void_t *mem) { fREe_impl(mcheck_free_prehook(mem)); }
+
+Void_t *rEALLOc(Void_t *oldmem, size_t bytes)
+{
+	if (bytes == 0) {
+		if (oldmem)
+			fREe(oldmem);
+		return NULL;
+	}
+
+	if (oldmem == NULL)
+		return mALLOc(bytes);
+
+	void *p = mcheck_reallocfree_prehook(oldmem);
+	size_t newsz = mcheck_alloc_prehook(bytes);
+
+	p = rEALLOc_impl(p, newsz);
+	if (!p)
+		return p;
+	return mcheck_alloc_noclean_posthook(p, bytes);
+}
+
+Void_t *mEMALIGn(size_t alignment, size_t bytes)
+{
+	return NULL;
+}
+
+// pvALLOc, vALLOc - redirect to mEMALIGn, defined here, so they need no wrapping.
+
+Void_t *cALLOc(size_t n, size_t elem_size)
+{
+	// NB: here is no overflow check.
+	size_t fullsz = mcheck_alloc_prehook(n * elem_size);
+	void *p = cALLOc_impl(1, fullsz);
+
+	if (!p)
+		return p;
+	return mcheck_alloc_noclean_posthook(p, n * elem_size);
+}
+
+// mcheck API {
+int mcheck(mcheck_abortfunc_t f)
+{
+	mcheck_initialize(f, 0);
+	return 0;
+}
+
+enum mcheck_status mprobe(void *__ptr) { return mcheck_mprobe(__ptr); }
+// mcheck API }
+#endif
+
 
 /*
 
-- 
2.25.1


  parent reply	other threads:[~2024-03-31 21:00 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-31 20:03 [PATCH 0/9] mcheck implementation for U-Boot Eugene Uriev
2024-03-31 20:03 ` [PATCH 1/9] mcheck: prepare +1 tier for mcheck-wrappers, in dl-*alloc commands Eugene Uriev
2024-03-31 20:03 ` [PATCH 2/9] mcheck: Use memset/memcpy instead of MALLOC_ZERO/MALLOC_COPY for mcheck Eugene Uriev
2024-03-31 20:03 ` [PATCH 3/9] mcheck: introduce essentials of mcheck Eugene Uriev
2024-03-31 20:03 ` Eugene Uriev [this message]
2024-03-31 20:03 ` [PATCH 5/9] mcheck: support memalign Eugene Uriev
2024-03-31 20:03 ` [PATCH 6/9] mcheck: add pedantic mode support Eugene Uriev
2024-03-31 20:03 ` [PATCH 7/9] mcheck: introduce mcheck_on_ramrelocation(.) Eugene Uriev
2024-03-31 20:03 ` [PATCH 8/9] mcheck: add stats, add a comment with test results Eugene Uriev
2024-03-31 20:03 ` [PATCH 9/9] mcheck: let mcheck_abortfunc_t print the pointer Eugene Uriev
2024-04-13 16:18 ` [PATCH 0/9] mcheck implementation for U-Boot Tom Rini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240331200327.29141-5-eugeneuriev@gmail.com \
    --to=eugeneuriev@gmail.com \
    --cc=trini@konsulko.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox