make leakmalloc work again

sort of, it looks like PIE has broken it on other ways
This commit is contained in:
Damien Miller
2012-09-20 03:39:41 +10:00
parent 54c2c68956
commit 88779e59e4
2 changed files with 27 additions and 12 deletions

View File

@@ -20,6 +20,7 @@
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <err.h>
#include <execinfo.h>
@@ -94,11 +95,16 @@ dump_leaks(void)
}
static void
internal_error(struct alloc *alloc)
internal_error(const char *tag, struct alloc *alloc)
{
char buf[256];
snprintf(buf, sizeof(buf), "ERROR/%s", tag);
#if 0
alloc->addr = NULL;
alloc->len = -1;
dump_leak(stderr, "ERROR", alloc);
#endif
dump_leak(stderr, buf, alloc);
initialised = -1;
}
@@ -119,7 +125,7 @@ new_alloc(void *addr, size_t len)
static void
__record_leak(void *addr, size_t len, void *oaddr)
{
struct alloc oalloc, *alloc;
struct alloc oalloc, *alloc, *ealloc;
char *cp;
if (initialised == -1)
@@ -149,10 +155,13 @@ __record_leak(void *addr, size_t len, void *oaddr)
if (addr == NULL)
return; /* alloc failed or free(NULL) */
alloc = new_alloc(addr, len);
if (RB_INSERT(alloc_tree, &alloc_tree, alloc) != NULL) {
internal_error(alloc);
errx(1, "%s: alloc for %p already exists",
ealloc = RB_INSERT(alloc_tree, &alloc_tree, alloc);
if (ealloc != NULL) {
internal_error("original", ealloc);
internal_error("new", alloc);
warnx("%s: alloc for fresh alloc %p already exists",
__func__, addr);
raise(SIGABRT);
}
} else {
oalloc.addr = oaddr;
@@ -172,17 +181,21 @@ __record_leak(void *addr, size_t len, void *oaddr)
*/
if (alloc == NULL) {
alloc = new_alloc(NULL, -1);
internal_error(alloc);
errx(1, "%s: realloc original addr %p missing",
internal_error("new", alloc);
warnx("%s: realloc original addr %p missing",
__func__, oaddr);
raise(SIGABRT);
}
RB_REMOVE(alloc_tree, &alloc_tree, alloc);
alloc->addr = addr;
alloc->len = len;
if (RB_INSERT(alloc_tree, &alloc_tree, alloc) != NULL) {
internal_error(alloc);
errx(1, "%s: alloc for %p already exists",
ealloc = RB_INSERT(alloc_tree, &alloc_tree, alloc);
if (ealloc != NULL) {
internal_error("original", ealloc);
internal_error("new", alloc);
warnx("%s: alloc for realloc %p already exists",
__func__, addr);
raise(SIGABRT);
}
}
}

View File

@@ -11,7 +11,6 @@ CDIAGFLAGS+= -Wpointer-arith
CDIAGFLAGS+= -Wstrict-prototypes
CDIAGFLAGS+= -Wmissing-prototypes
CDIAGFLAGS+= -Wunused
CDIAGFLAGS+= -Wuninitialized
CDIAGFLAGS+= -Wsign-compare
CDIAGFLAGS+= -Wshadow
@@ -47,6 +46,9 @@ LEAKMALLOC_DIR= ${.CURDIR}/../../leakmalloc/leakmalloc
CFLAGS+= -I${LEAKMALLOC_DIR} -DWITH_LEAKMALLOC
LDFLAGS+= -L/usr/local/lib
LDADD+= -lleakmalloc -lexecinfo
CFLAGS+= -Dmalloc=leak_malloc -Dstrdup=leak_strdup
CFLAGS+= -Dcalloc=leak_calloc -Drealloc=leak_realloc
CFLAGS+= -Dfree=leak_free
. if exists(${LEAKMALLOC_DIR}/${__objdir})
LDADD+= -L${LEAKMALLOC_DIR}/${__objdir}
DPADD+= ${LEAKMALLOC_DIR}/${__objdir}/libleakmalloc.a