[PATCH] dlmalloc: hide out of bounds accesses from compiler

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxxxx>
---
 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





[Index of Archives]     [Linux Embedded]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux