aboutsummaryrefslogtreecommitdiff
path: root/common/dlmalloc.c
diff options
context:
space:
mode:
authorSimon Glass <sjg@chromium.org>2022-09-06 20:27:00 -0600
committerTom Rini <trini@konsulko.com>2022-09-29 16:07:58 -0400
commit62d638386c17d17b929ad10956c7f60825335a4e (patch)
tree9f0e6f82f9bdc3fac8a8c64e2850c14002cd6a2a /common/dlmalloc.c
parent7c14dc7f77705f79ba49e7f0b2879986fea70fea (diff)
downloadu-boot-62d638386c17d17b929ad10956c7f60825335a4e.zip
u-boot-62d638386c17d17b929ad10956c7f60825335a4e.tar.gz
u-boot-62d638386c17d17b929ad10956c7f60825335a4e.tar.bz2
test: Support testing malloc() failures
It is helpful to test that out-of-memory checks work correctly in code that calls malloc(). Add a simple way to force failure after a given number of malloc() calls. Fix a header guard to avoid a build error on sandbox_vpl. Signed-off-by: Simon Glass <sjg@chromium.org> Reviewed-by: Sean Anderson <seanga2@gmail.com>
Diffstat (limited to 'common/dlmalloc.c')
-rw-r--r--common/dlmalloc.c19
1 files changed, 19 insertions, 0 deletions
diff --git a/common/dlmalloc.c b/common/dlmalloc.c
index f48cd2a..41c7230 100644
--- a/common/dlmalloc.c
+++ b/common/dlmalloc.c
@@ -596,6 +596,9 @@ ulong mem_malloc_start = 0;
ulong mem_malloc_end = 0;
ulong mem_malloc_brk = 0;
+static bool malloc_testing; /* enable test mode */
+static int malloc_max_allocs; /* return NULL after this many calls to malloc() */
+
void *sbrk(ptrdiff_t increment)
{
ulong old = mem_malloc_brk;
@@ -1307,6 +1310,11 @@ Void_t* mALLOc(bytes) size_t bytes;
return malloc_simple(bytes);
#endif
+ if (CONFIG_IS_ENABLED(UNIT_TEST) && malloc_testing) {
+ if (--malloc_max_allocs < 0)
+ return NULL;
+ }
+
/* check if mem_malloc_init() was run */
if ((mem_malloc_start == 0) && (mem_malloc_end == 0)) {
/* not initialized yet */
@@ -2470,6 +2478,17 @@ int initf_malloc(void)
return 0;
}
+void malloc_enable_testing(int max_allocs)
+{
+ malloc_testing = true;
+ malloc_max_allocs = max_allocs;
+}
+
+void malloc_disable_testing(void)
+{
+ malloc_testing = false;
+}
+
/*
History: