diff --git a/include/common/memory.h b/include/common/memory.h index ae483ec73c..96519d462c 100644 --- a/include/common/memory.h +++ b/include/common/memory.h @@ -129,6 +129,8 @@ struct pool_head { char name[12]; /* name of the pool */ }; +/* poison each newly allocated area with this byte if not null */ +extern char mem_poison_byte; /* Allocate a new entry for pool , and return it for immediate use. * NULL is returned if no memory is available for a new creation. diff --git a/src/haproxy.c b/src/haproxy.c index 776f0dd4ff..170433572c 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -222,6 +222,7 @@ void usage(char *name) " [ -p ] [ -m ] [ -C ]\n" " -v displays version ; -vv shows known build options.\n" " -d enters debug mode ; -db only disables background mode.\n" + " -dM[] poisons memory with (defaults to 0x50)\n" " -V enters verbose mode (disables quiet mode)\n" " -D goes daemon ; -C changes to before loading files.\n" " -q quiet mode : don't display messages\n" @@ -451,6 +452,8 @@ void init(int argc, char **argv) arg_mode |= MODE_VERBOSE; else if (*flag == 'd' && flag[1] == 'b') arg_mode |= MODE_FOREGROUND; + else if (*flag == 'd' && flag[1] == 'M') + mem_poison_byte = flag[2] ? strtol(flag + 2, NULL, 0) : 'P'; else if (*flag == 'd') arg_mode |= MODE_DEBUG; else if (*flag == 'c') diff --git a/src/memory.c b/src/memory.c index 36db92e34e..128f6ef125 100644 --- a/src/memory.c +++ b/src/memory.c @@ -19,6 +19,7 @@ #include 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 @@ void *pool_refill_alloc(struct pool_head *pool) 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;