aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEugene Uriev <eugeneuriev@gmail.com>2024-03-31 23:03:23 +0300
committerTom Rini <trini@konsulko.com>2024-04-12 16:23:06 -0600
commitae838768d79cbb834c4a8a5f4810df373e58b622 (patch)
treead65d99ccd7aac92d665f337bd13ef3d73943faa
parent151493a875071448e2582489f6fa84d1630b3368 (diff)
downloadu-boot-ae838768d79cbb834c4a8a5f4810df373e58b622.zip
u-boot-ae838768d79cbb834c4a8a5f4810df373e58b622.tar.gz
u-boot-ae838768d79cbb834c4a8a5f4810df373e58b622.tar.bz2
mcheck: support memalign
Signed-off-by: Eugene Uriev <eugeneuriev@gmail.com>
-rw-r--r--common/dlmalloc.c7
-rw-r--r--common/mcheck_core.inc.h20
2 files changed, 24 insertions, 3 deletions
diff --git a/common/dlmalloc.c b/common/dlmalloc.c
index 8de15d7..73c04af 100644
--- a/common/dlmalloc.c
+++ b/common/dlmalloc.c
@@ -2265,7 +2265,12 @@ Void_t *rEALLOc(Void_t *oldmem, size_t bytes)
Void_t *mEMALIGn(size_t alignment, size_t bytes)
{
- return NULL;
+ size_t fullsz = mcheck_memalign_prehook(alignment, bytes);
+ void *p = mEMALIGn_impl(alignment, fullsz);
+
+ if (!p)
+ return p;
+ return mcheck_memalign_posthook(alignment, p, bytes);
}
// pvALLOc, vALLOc - redirect to mEMALIGn, defined here, so they need no wrapping.
diff --git a/common/mcheck_core.inc.h b/common/mcheck_core.inc.h
index 6f26ef0..b038bb0 100644
--- a/common/mcheck_core.inc.h
+++ b/common/mcheck_core.inc.h
@@ -69,6 +69,7 @@ typedef struct {
} mcheck_canary;
struct mcheck_hdr {
size_t size; /* Exact size requested by user. */
+ size_t aln_skip; /* Ignored bytes, before the mcheck_hdr, to fulfill alignment */
mcheck_canary canary; /* Magic number to check header integrity. */
};
@@ -104,6 +105,7 @@ static inline size_t allign_size_up(size_t sz, size_t grain)
}
#define mcheck_allign_customer_size(SZ) allign_size_up(SZ, sizeof(mcheck_elem))
+#define mcheck_evaluate_memalign_prefix_size(ALIGN) allign_size_up(sizeof(struct mcheck_hdr), ALIGN)
static enum mcheck_status mcheck_OnNok(enum mcheck_status status)
{
@@ -156,7 +158,8 @@ static void *mcheck_free_helper(void *ptr, int clean_content)
if (clean_content)
mcheck_flood(ptr, FREEFLOOD, mcheck_allign_customer_size(hdr->size));
- return hdr;
+
+ return (char *)hdr - hdr->aln_skip;
}
static void *mcheck_free_prehook(void *ptr) { return mcheck_free_helper(ptr, CLEAN_CONTENT); }
@@ -171,10 +174,13 @@ static size_t mcheck_alloc_prehook(size_t sz)
static void *mcheck_allocated_helper(void *altoghether_ptr, size_t customer_sz,
size_t alignment, int clean_content)
{
- struct mcheck_hdr *hdr = (struct mcheck_hdr *)altoghether_ptr;
+ const size_t slop = alignment ?
+ mcheck_evaluate_memalign_prefix_size(alignment) - sizeof(struct mcheck_hdr) : 0;
+ struct mcheck_hdr *hdr = (struct mcheck_hdr *)((char *)altoghether_ptr + slop);
int i;
hdr->size = customer_sz;
+ hdr->aln_skip = slop;
for (i = 0; i < CANARY_DEPTH; ++i)
hdr->canary.elems[i] = MAGICWORD;
@@ -204,6 +210,16 @@ static void *mcheck_alloc_noclean_posthook(void *altoghether_ptr, size_t custome
return mcheck_allocated_helper(altoghether_ptr, customer_sz, ANY_ALIGNMENT, KEEP_CONTENT);
}
+static size_t mcheck_memalign_prehook(size_t alig, size_t sz)
+{
+ return mcheck_evaluate_memalign_prefix_size(alig) + sz + sizeof(mcheck_canary);
+}
+
+static void *mcheck_memalign_posthook(size_t alignment, void *altoghether_ptr, size_t customer_sz)
+{
+ return mcheck_allocated_helper(altoghether_ptr, customer_sz, alignment, CLEAN_CONTENT);
+}
+
static enum mcheck_status mcheck_mprobe(void *ptr)
{
struct mcheck_hdr *hdr = &((struct mcheck_hdr *)ptr)[-1];