mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] dlmalloc: hide out of bounds accesses from compiler
@ 2023-11-14  8:16 Sascha Hauer
  0 siblings, 0 replies; only message in thread
From: Sascha Hauer @ 2023-11-14  8:16 UTC (permalink / raw)
  To: Barebox List; +Cc: afa

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




^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-11-14  8:17 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-14  8:16 [PATCH] dlmalloc: hide out of bounds accesses from compiler Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox