]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: memory: add the ability to poison memory at run time
authorWilly Tarreau <w@1wt.eu>
Tue, 8 May 2012 13:40:42 +0000 (15:40 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 8 May 2012 19:28:16 +0000 (21:28 +0200)
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>.

include/common/memory.h
src/haproxy.c
src/memory.c

index ae483ec73c5bd6819bd9d095e4c208ace23af787..96519d462c5b676e47b945070a2c82be25e96b02 100644 (file)
@@ -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 <pool>, and return it for immediate use.
  * NULL is returned if no memory is available for a new creation.
index 776f0dd4ffcc9b996c45da946314477cc8985a50..170433572ced11882635542daea76d0d765888c0 100644 (file)
@@ -222,6 +222,7 @@ void usage(char *name)
                "        [ -p <pidfile> ] [ -m <max megs> ] [ -C <dir> ]\n"
                "        -v displays version ; -vv shows known build options.\n"
                "        -d enters debug mode ; -db only disables background mode.\n"
+               "        -dM[<byte>] poisons memory with <byte> (defaults to 0x50)\n"
                "        -V enters verbose mode (disables quiet mode)\n"
                "        -D goes daemon ; -C changes to <dir> 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')
index 36db92e34eedd8a9ff2b9819f3e35d2458c787dd..128f6ef125173f154662a2f1ad983fd266b5bd16 100644 (file)
@@ -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 @@ 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;