summaryrefslogtreecommitdiff
path: root/common/malloc_simple.c
diff options
context:
space:
mode:
authorSean Anderson <seanga2@gmail.com>2022-03-23 14:04:49 -0400
committerTom Rini <trini@konsulko.com>2022-04-11 10:00:30 -0400
commitbdaeea1b6863b0ec80f2d4bc15d50b8d16efa708 (patch)
treea9a68f315eceeabb9477bb58b8726e307dc2c99c /common/malloc_simple.c
parentfba0882bcdfd919727ee9ee8523ef3156daab507 (diff)
malloc: Annotate allocator for valgrind
This annotates malloc and friends so that valgrind can track the heap. To do this, we need to follow a few rules: * Call VALGRIND_MALLOCLIKE_BLOCK whenever we malloc something * Call VALGRIND_FREELIKE_BLOCK whenever we free something (generally after we have done our bookkeeping) * Call VALGRIND_RESIZEINPLACE_BLOCK whenever we change the size of an allocation. We don't record the original request size of a block, and neither does valgrind. For this reason, we pretend that the old size of the allocation was for 0 bytes. This marks the whole allocaton as undefined, so in order to mark all bits correctly, we must make the whole new allocation defined with VALGRIND_MAKE_MEM_DEFINED. This may cause us to miss some invalid reads, but there is no way to detect these without recording the original size of the allocation. In addition to the above, dlmalloc itself tends to make a lot of accesses which we know are safe, but which would be unsafe outside of dlmalloc. For this reason, we provide a suppression file which ignores errors ocurring in dlmalloc.c Signed-off-by: Sean Anderson <seanga2@gmail.com> Reviewed-by: Simon Glass <sjg@chromium.org>
Diffstat (limited to 'common/malloc_simple.c')
-rw-r--r--common/malloc_simple.c10
1 files changed, 10 insertions, 0 deletions
diff --git a/common/malloc_simple.c b/common/malloc_simple.c
index 67ee623850..0a004d40e1 100644
--- a/common/malloc_simple.c
+++ b/common/malloc_simple.c
@@ -13,6 +13,7 @@
#include <mapmem.h>
#include <asm/global_data.h>
#include <asm/io.h>
+#include <valgrind/valgrind.h>
DECLARE_GLOBAL_DATA_PTR;
@@ -45,6 +46,7 @@ void *malloc_simple(size_t bytes)
return ptr;
log_debug("%lx\n", (ulong)ptr);
+ VALGRIND_MALLOCLIKE_BLOCK(ptr, bytes, 0, false);
return ptr;
}
@@ -57,6 +59,7 @@ void *memalign_simple(size_t align, size_t bytes)
if (!ptr)
return ptr;
log_debug("aligned to %lx\n", (ulong)ptr);
+ VALGRIND_MALLOCLIKE_BLOCK(ptr, bytes, 0, false);
return ptr;
}
@@ -74,6 +77,13 @@ void *calloc(size_t nmemb, size_t elem_size)
return ptr;
}
+
+#if IS_ENABLED(CONFIG_VALGRIND)
+void free_simple(void *ptr)
+{
+ VALGRIND_FREELIKE_BLOCK(ptr, 0);
+}
+#endif
#endif
void malloc_simple_info(void)