mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Cc: afa@pengutronix.de
Subject: [PATCH] dlmalloc: hide out of bounds accesses from compiler
Date: Tue, 14 Nov 2023 09:16:00 +0100	[thread overview]
Message-ID: <20231114081600.867531-1-s.hauer@pengutronix.de> (raw)

Once we add __alloc_size attributes to allocations, GCC will complain
about violation of memory safety in dlmalloc.c.

dlmalloc uses this macro to convert a pointer returned by malloc()
to its own data structures:

 #define mem2chunk(mem) ((mchunkptr)((char*)(mem) - 2*SIZE_SZ))

As dlmalloc internally uses malloc itself and uses mem2chunk afterwards
on this pointer gcc rightfully complains about illegal accesses. Silence
these warnings by converting mem2chunk to a static inline function.

I would have expected that we need a OPTIMIZER_HIDE_VAR() in
mem2chunk(), but that doesn't seem to be necessary right now. Maybe it
will in later gcc versions.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 common/dlmalloc.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/common/dlmalloc.c b/common/dlmalloc.c
index 16ea3cafb6..d420e8bd9d 100644
--- a/common/dlmalloc.c
+++ b/common/dlmalloc.c
@@ -628,7 +628,11 @@ nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 /* conversion from malloc headers to user pointers, and back */
 
 #define chunk2mem(p)   ((void*)((char*)(p) + 2*SIZE_SZ))
-#define mem2chunk(mem) ((mchunkptr)((char*)(mem) - 2*SIZE_SZ))
+
+static inline mchunkptr mem2chunk(void *mem)
+{
+	return mem - 2 * SIZE_SZ;
+}
 
 /* pad request bytes into a usable size */
 
@@ -1683,8 +1687,8 @@ void *memalign(size_t alignment, size_t bytes)
 		   this is always possible.
 		 */
 
-		brk = (char*) mem2chunk(((unsigned long) (m + alignment - 1)) &
-					  -((signed) alignment));
+		brk = (char*) mem2chunk((void *)(((unsigned long) (m + alignment - 1)) &
+					  -((signed) alignment)));
 		if ((long)(brk - (char*)(p)) < MINSIZE)
 			brk = brk + alignment;
 
-- 
2.39.2




                 reply	other threads:[~2023-11-14  8:17 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20231114081600.867531-1-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=afa@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /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