[coreboot-gerrit] New patch to review for coreboot: libpayload: Fix CONFIG_LP_DEBUG_MALLOC for 64-bit archs

Julius Werner (jwerner@chromium.org) gerrit at coreboot.org
Thu May 19 22:26:56 CEST 2016


Julius Werner (jwerner at chromium.org) just uploaded a new patch set to gerrit, which you can find at https://review.coreboot.org/14921

-gerrit

commit 8812b2e88d59ea643b1ef48baf33aacbc0b794ac
Author: Julius Werner <jwerner at chromium.org>
Date:   Thu May 19 13:15:16 2016 -0700

    libpayload: Fix CONFIG_LP_DEBUG_MALLOC for 64-bit archs
    
    New compilers are a little more stringent about defining the same
    prototype more than once, so some of our CONFIG_LP_DEBUG_MALLOC wrappers
    don't quite work the way they are written anymore. Also, several of the
    printf()s weren't written 64-bit safe. And let's add some
    double-evaluation safety while I'm here anyway.
    
    Change-Id: Ib54ebc3cfba99f372690365b78c7ceb372c0bd45
    Signed-off-by: Julius Werner <jwerner at chromium.org>
---
 payloads/libpayload/include/stdlib.h | 67 +++++++++++++++++++-----------------
 payloads/libpayload/libc/malloc.c    |  7 ++--
 2 files changed, 39 insertions(+), 35 deletions(-)

diff --git a/payloads/libpayload/include/stdlib.h b/payloads/libpayload/include/stdlib.h
index 689bf01..fb50b13 100644
--- a/payloads/libpayload/include/stdlib.h
+++ b/payloads/libpayload/include/stdlib.h
@@ -46,98 +46,101 @@
  * @{
  */
 #if IS_ENABLED(CONFIG_LP_DEBUG_MALLOC) && !defined(IN_MALLOC_C)
+#include <stdio.h>
+extern void print_malloc_map(void);
+extern void free(void *);
+extern void *malloc(size_t);
+extern void *calloc(size_t,size_t);
+extern void *realloc(void*,size_t);
+extern void *memalign(size_t, size_t);
+extern void *dma_malloc(size_t);
+extern void *dma_memalign(size_t, size_t);
 #define free(p)	\
 	({ \
-	 extern void print_malloc_map(void); \
-	 extern void free(void *); \
-	 printf("free(%p) called from %s:%s:%d...\n", p, __FILE__, __func__, \
+	 void * __p = p; \
+	 printf("free(%p) called from %s:%s:%d...\n", __p, __FILE__, __func__, \
 	        __LINE__);\
 	 printf("PRE free()\n"); \
 	 print_malloc_map(); \
-	 free(p); \
+	 free(__p); \
 	 printf("POST free()\n"); \
 	 print_malloc_map(); \
 	 })
 #define malloc(s) \
 	({ \
-	 extern void print_malloc_map(void); \
-	 extern void *malloc(size_t); \
+	 size_t __s = s; \
 	 void *ptr; \
-	 printf("malloc(%u) called from %s:%s:%d...\n", s, __FILE__, __func__, \
-	        __LINE__);\
+	 printf("malloc(%zu) called from %s:%s:%d...\n", __s, __FILE__, \
+		__func__, __LINE__);\
 	 printf("PRE malloc\n"); \
 	 print_malloc_map(); \
-	 ptr = malloc(s); \
+	 ptr = malloc(__s); \
 	 printf("POST malloc (ptr = %p)\n", ptr); \
 	 print_malloc_map(); \
 	 ptr; \
 	 })
 #define calloc(n,s) \
 	({ \
-	 extern void print_malloc_map(void); \
-	 extern void *calloc(size_t,size_t); \
+	 size_t __n = n, __s = s; \
 	 void *ptr; \
-	 printf("calloc(%u, %u) called from %s:%s:%d...\n", n, s, __FILE__, \
-	        __func__, __LINE__);\
+	 printf("calloc(%zu, %zu) called from %s:%s:%d...\n", __n, __s, \
+		__FILE__, __func__, __LINE__);\
 	 printf("PRE calloc\n"); \
 	 print_malloc_map(); \
-	 ptr = calloc(n,s); \
+	 ptr = calloc(__n, __s); \
 	 printf("POST calloc (ptr = %p)\n", ptr); \
 	 print_malloc_map(); \
 	 ptr; \
 	 })
 #define realloc(p,s) \
 	({ \
-	 extern void print_malloc_map(void); \
-	 extern void *realloc(void*,size_t); \
+	 void *__p = p; \
+	 size_t __s = s; \
 	 void *ptr; \
-	 printf("realloc(%p, %u) called from %s:%s:%d...\n", p, s, __FILE__, \
-	        __func__, __LINE__);\
+	 printf("realloc(%p, %zu) called from %s:%s:%d...\n", __p, __s, \
+		__FILE__, __func__, __LINE__);\
 	 printf("PRE realloc\n"); \
 	 print_malloc_map(); \
-	 ptr = realloc(p,s); \
+	 ptr = realloc(__p, __s); \
 	 printf("POST realloc (ptr = %p)\n", ptr); \
 	 print_malloc_map(); \
 	 ptr; \
 	 })
 #define memalign(a,s) \
 	({ \
-	 extern void print_malloc_map(void); \
-	 extern void *memalign(size_t, size_t); \
+	 size_t __a = a, __s = s; \
 	 void *ptr; \
-	 printf("memalign(%u, %u) called from %s:%s:%d...\n", a, s, __FILE__, \
-	        __func__, __LINE__);\
+	 printf("memalign(%zu, %zu) called from %s:%s:%d...\n", __a, __s, \
+	 	__FILE__, __func__, __LINE__);\
 	 printf("PRE memalign\n"); \
 	 print_malloc_map(); \
-	 ptr = memalign(a,s); \
+	 ptr = memalign(__a, __s); \
 	 printf("POST memalign (ptr = %p)\n", ptr); \
 	 print_malloc_map(); \
 	 ptr; \
 	 })
 #define dma_malloc(s) \
 	({ \
-	 extern void print_malloc_map(void); \
-	 extern void *dma_malloc(size_t); \
+	 size_t __s = s; \
 	 void *ptr; \
-	 printf("dma_malloc(%u) called from %s:%s:%d...\n", s, __FILE__, \
+	 printf("dma_malloc(%zu) called from %s:%s:%d...\n", __s, __FILE__, \
 	        __func__, __LINE__);\
 	 printf("PRE dma_malloc\n"); \
 	 print_malloc_map(); \
-	 ptr = dma_malloc(s); \
+	 ptr = dma_malloc(__s); \
 	 printf("POST dma_malloc (ptr = %p)\n", ptr); \
 	 print_malloc_map(); \
 	 ptr; \
 	 })
 #define dma_memalign(a,s) \
 	({ \
-	 extern void print_malloc_map(void); \
-	 extern void *dma_memalign(size_t, size_t); \
+	 size_t __a = a, __s = s; \
 	 void *ptr; \
-	 printf("dma_memalign(%u, %u) called from %s:%s:%d...\n", a, s, \
+	 printf("dma_memalign(%zu, %zu) called from %s:%s:%d...\n", __a, __s, \
 	        __FILE__, __func__, __LINE__);\
 	 printf("PRE dma_memalign\n"); \
 	 print_malloc_map(); \
-	 ptr = dma_memalign(a,s); \
+	 ptr = dma_memalign(__a, __s); \
 	 printf("POST dma_memalign (ptr = %p)\n", ptr); \
 	 print_malloc_map(); \
 	 ptr; \
diff --git a/payloads/libpayload/libc/malloc.c b/payloads/libpayload/libc/malloc.c
index b7ac1a7..b88bb1c 100644
--- a/payloads/libpayload/libc/malloc.c
+++ b/payloads/libpayload/libc/malloc.c
@@ -480,7 +480,7 @@ look_further:
 		if ((reg->alignment == align) && (reg->free >= (size + align - 1)/align))
 		{
 #if IS_ENABLED(CONFIG_LP_DEBUG_MALLOC)
-			printf("  found memalign region. %x free, %x required\n", reg->free, (size + align - 1)/align);
+			printf("  found memalign region. %u free, %zu required\n", reg->free, (size + align - 1)/align);
 #endif
 			break;
 		}
@@ -565,7 +565,8 @@ again:
 
 		printf("%s %x: %s (%x bytes)\n", type->name,
 		       (unsigned int)(ptr - type->start),
-		       hdr & FLAG_FREE ? "FREE" : "USED", SIZE(hdr));
+		       hdr & FLAG_FREE ? "FREE" : "USED",
+		       (unsigned int)SIZE(hdr));
 
 		if (hdr & FLAG_FREE)
 			free_memory += SIZE(hdr);
@@ -575,7 +576,7 @@ again:
 
 	if (free_memory && (type->minimal_free > free_memory))
 		type->minimal_free = free_memory;
-	printf("%s: Maximum memory consumption: %u bytes\n", type->name,
+	printf("%s: Maximum memory consumption: %zu bytes\n", type->name,
 		(type->end - type->start) - HDRSIZE - type->minimal_free);
 
 	if (type != dma) {



More information about the coreboot-gerrit mailing list