MEDIUM: memory: add the ability to poison memory at run time
From time to time, some bugs are discovered that are caused by non-initialized
memory areas. It happens that most platforms return a zero-filled area upon
first malloc() thus hiding potential bugs. This patch also replaces malloc()
in pools with calloc() to ensure that all platforms exhibit the same behaviour
upon startup. In order to catch these bugs more easily, add a -dM command line
flag to enable memory poisonning. Optionally, passing -dM<byte> forces the
poisonning byte to <byte>.
diff --git a/src/memory.c b/src/memory.c
index 36db92e..128f6ef 100644
--- a/src/memory.c
+++ b/src/memory.c
@@ -19,6 +19,7 @@
#include <proto/log.h>
static struct list pools = LIST_HEAD_INIT(pools);
+char mem_poison_byte = 0;
/* Try to find an existing shared pool with the same characteristics and
* returns it, otherwise creates this one. NULL is returned if no memory
@@ -87,13 +88,15 @@
if (pool->limit && (pool->allocated >= pool->limit))
return NULL;
- ret = MALLOC(pool->size);
+ ret = CALLOC(1, pool->size);
if (!ret) {
pool_gc2();
- ret = MALLOC(pool->size);
+ ret = CALLOC(1, pool->size);
if (!ret)
return NULL;
}
+ if (mem_poison_byte)
+ memset(ret, mem_poison_byte, pool->size);
pool->allocated++;
pool->used++;
return ret;