186d7f5d3SJohn Marino /* $NetBSD: dbg_malloc.c,v 1.1.1.2 2009/12/02 00:26:09 haad Exp $ */
286d7f5d3SJohn Marino
386d7f5d3SJohn Marino /*
486d7f5d3SJohn Marino * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
586d7f5d3SJohn Marino * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
686d7f5d3SJohn Marino *
786d7f5d3SJohn Marino * This file is part of the device-mapper userspace tools.
886d7f5d3SJohn Marino *
986d7f5d3SJohn Marino * This copyrighted material is made available to anyone wishing to use,
1086d7f5d3SJohn Marino * modify, copy, or redistribute it subject to the terms and conditions
1186d7f5d3SJohn Marino * of the GNU Lesser General Public License v.2.1.
1286d7f5d3SJohn Marino *
1386d7f5d3SJohn Marino * You should have received a copy of the GNU Lesser General Public License
1486d7f5d3SJohn Marino * along with this program; if not, write to the Free Software Foundation,
1586d7f5d3SJohn Marino * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1686d7f5d3SJohn Marino */
1786d7f5d3SJohn Marino
1886d7f5d3SJohn Marino #include "dmlib.h"
1986d7f5d3SJohn Marino
2086d7f5d3SJohn Marino #include <assert.h>
2186d7f5d3SJohn Marino #include <stdarg.h>
2286d7f5d3SJohn Marino
dm_strdup_aux(const char * str,const char * file,int line)2386d7f5d3SJohn Marino char *dm_strdup_aux(const char *str, const char *file, int line)
2486d7f5d3SJohn Marino {
2586d7f5d3SJohn Marino char *ret;
2686d7f5d3SJohn Marino
2786d7f5d3SJohn Marino if (!str) {
2886d7f5d3SJohn Marino log_error("Internal error: dm_strdup called with NULL pointer");
2986d7f5d3SJohn Marino return NULL;
3086d7f5d3SJohn Marino }
3186d7f5d3SJohn Marino
3286d7f5d3SJohn Marino if ((ret = dm_malloc_aux_debug(strlen(str) + 1, file, line)))
3386d7f5d3SJohn Marino strcpy(ret, str);
3486d7f5d3SJohn Marino
3586d7f5d3SJohn Marino return ret;
3686d7f5d3SJohn Marino }
3786d7f5d3SJohn Marino
3886d7f5d3SJohn Marino struct memblock {
3986d7f5d3SJohn Marino struct memblock *prev, *next; /* All allocated blocks are linked */
4086d7f5d3SJohn Marino size_t length; /* Size of the requested block */
4186d7f5d3SJohn Marino int id; /* Index of the block */
4286d7f5d3SJohn Marino const char *file; /* File that allocated */
4386d7f5d3SJohn Marino int line; /* Line that allocated */
4486d7f5d3SJohn Marino void *magic; /* Address of this block */
4586d7f5d3SJohn Marino } __attribute__((aligned(8)));
4686d7f5d3SJohn Marino
4786d7f5d3SJohn Marino static struct {
4886d7f5d3SJohn Marino unsigned block_serialno;/* Non-decreasing serialno of block */
4986d7f5d3SJohn Marino unsigned blocks_allocated; /* Current number of blocks allocated */
5086d7f5d3SJohn Marino unsigned blocks_max; /* Max no of concurrently-allocated blocks */
5186d7f5d3SJohn Marino unsigned int bytes, mbytes;
5286d7f5d3SJohn Marino
5386d7f5d3SJohn Marino } _mem_stats = {
5486d7f5d3SJohn Marino 0, 0, 0, 0, 0};
5586d7f5d3SJohn Marino
5686d7f5d3SJohn Marino static struct memblock *_head = 0;
5786d7f5d3SJohn Marino static struct memblock *_tail = 0;
5886d7f5d3SJohn Marino
dm_malloc_aux_debug(size_t s,const char * file,int line)5986d7f5d3SJohn Marino void *dm_malloc_aux_debug(size_t s, const char *file, int line)
6086d7f5d3SJohn Marino {
6186d7f5d3SJohn Marino struct memblock *nb;
6286d7f5d3SJohn Marino size_t tsize = s + sizeof(*nb) + sizeof(unsigned long);
6386d7f5d3SJohn Marino
6486d7f5d3SJohn Marino if (s > 50000000) {
6586d7f5d3SJohn Marino log_error("Huge memory allocation (size %" PRIsize_t
6686d7f5d3SJohn Marino ") rejected - metadata corruption?", s);
6786d7f5d3SJohn Marino return 0;
6886d7f5d3SJohn Marino }
6986d7f5d3SJohn Marino
7086d7f5d3SJohn Marino if (!(nb = malloc(tsize))) {
7186d7f5d3SJohn Marino log_error("couldn't allocate any memory, size = %" PRIsize_t,
7286d7f5d3SJohn Marino s);
7386d7f5d3SJohn Marino return 0;
7486d7f5d3SJohn Marino }
7586d7f5d3SJohn Marino
7686d7f5d3SJohn Marino /* set up the file and line info */
7786d7f5d3SJohn Marino nb->file = file;
7886d7f5d3SJohn Marino nb->line = line;
7986d7f5d3SJohn Marino
8086d7f5d3SJohn Marino dm_bounds_check();
8186d7f5d3SJohn Marino
8286d7f5d3SJohn Marino /* setup fields */
8386d7f5d3SJohn Marino nb->magic = nb + 1;
8486d7f5d3SJohn Marino nb->length = s;
8586d7f5d3SJohn Marino nb->id = ++_mem_stats.block_serialno;
8686d7f5d3SJohn Marino nb->next = 0;
8786d7f5d3SJohn Marino
8886d7f5d3SJohn Marino /* stomp a pretty pattern across the new memory
8986d7f5d3SJohn Marino and fill in the boundary bytes */
9086d7f5d3SJohn Marino {
9186d7f5d3SJohn Marino char *ptr = (char *) (nb + 1);
9286d7f5d3SJohn Marino size_t i;
9386d7f5d3SJohn Marino for (i = 0; i < s; i++)
9486d7f5d3SJohn Marino *ptr++ = i & 0x1 ? (char) 0xba : (char) 0xbe;
9586d7f5d3SJohn Marino
9686d7f5d3SJohn Marino for (i = 0; i < sizeof(unsigned long); i++)
9786d7f5d3SJohn Marino *ptr++ = (char) nb->id;
9886d7f5d3SJohn Marino }
9986d7f5d3SJohn Marino
10086d7f5d3SJohn Marino nb->prev = _tail;
10186d7f5d3SJohn Marino
10286d7f5d3SJohn Marino /* link to tail of the list */
10386d7f5d3SJohn Marino if (!_head)
10486d7f5d3SJohn Marino _head = _tail = nb;
10586d7f5d3SJohn Marino else {
10686d7f5d3SJohn Marino _tail->next = nb;
10786d7f5d3SJohn Marino _tail = nb;
10886d7f5d3SJohn Marino }
10986d7f5d3SJohn Marino
11086d7f5d3SJohn Marino _mem_stats.blocks_allocated++;
11186d7f5d3SJohn Marino if (_mem_stats.blocks_allocated > _mem_stats.blocks_max)
11286d7f5d3SJohn Marino _mem_stats.blocks_max = _mem_stats.blocks_allocated;
11386d7f5d3SJohn Marino
11486d7f5d3SJohn Marino _mem_stats.bytes += s;
11586d7f5d3SJohn Marino if (_mem_stats.bytes > _mem_stats.mbytes)
11686d7f5d3SJohn Marino _mem_stats.mbytes = _mem_stats.bytes;
11786d7f5d3SJohn Marino
11886d7f5d3SJohn Marino /* log_debug("Allocated: %u %u %u", nb->id, _mem_stats.blocks_allocated,
11986d7f5d3SJohn Marino _mem_stats.bytes); */
12086d7f5d3SJohn Marino
12186d7f5d3SJohn Marino return nb + 1;
12286d7f5d3SJohn Marino }
12386d7f5d3SJohn Marino
dm_free_aux(void * p)12486d7f5d3SJohn Marino void dm_free_aux(void *p)
12586d7f5d3SJohn Marino {
12686d7f5d3SJohn Marino char *ptr;
12786d7f5d3SJohn Marino size_t i;
12886d7f5d3SJohn Marino struct memblock *mb = ((struct memblock *) p) - 1;
12986d7f5d3SJohn Marino if (!p)
13086d7f5d3SJohn Marino return;
13186d7f5d3SJohn Marino
13286d7f5d3SJohn Marino dm_bounds_check();
13386d7f5d3SJohn Marino
13486d7f5d3SJohn Marino /* sanity check */
13586d7f5d3SJohn Marino assert(mb->magic == p);
13686d7f5d3SJohn Marino
13786d7f5d3SJohn Marino /* check data at the far boundary */
13886d7f5d3SJohn Marino ptr = ((char *) mb) + sizeof(struct memblock) + mb->length;
13986d7f5d3SJohn Marino for (i = 0; i < sizeof(unsigned long); i++)
14086d7f5d3SJohn Marino if (*ptr++ != (char) mb->id)
14186d7f5d3SJohn Marino assert(!"Damage at far end of block");
14286d7f5d3SJohn Marino
14386d7f5d3SJohn Marino /* have we freed this before ? */
14486d7f5d3SJohn Marino assert(mb->id != 0);
14586d7f5d3SJohn Marino
14686d7f5d3SJohn Marino /* unlink */
14786d7f5d3SJohn Marino if (mb->prev)
14886d7f5d3SJohn Marino mb->prev->next = mb->next;
14986d7f5d3SJohn Marino else
15086d7f5d3SJohn Marino _head = mb->next;
15186d7f5d3SJohn Marino
15286d7f5d3SJohn Marino if (mb->next)
15386d7f5d3SJohn Marino mb->next->prev = mb->prev;
15486d7f5d3SJohn Marino else
15586d7f5d3SJohn Marino _tail = mb->prev;
15686d7f5d3SJohn Marino
15786d7f5d3SJohn Marino mb->id = 0;
15886d7f5d3SJohn Marino
15986d7f5d3SJohn Marino /* stomp a different pattern across the memory */
16086d7f5d3SJohn Marino ptr = ((char *) mb) + sizeof(struct memblock);
16186d7f5d3SJohn Marino for (i = 0; i < mb->length; i++)
16286d7f5d3SJohn Marino *ptr++ = i & 1 ? (char) 0xde : (char) 0xad;
16386d7f5d3SJohn Marino
16486d7f5d3SJohn Marino assert(_mem_stats.blocks_allocated);
16586d7f5d3SJohn Marino _mem_stats.blocks_allocated--;
16686d7f5d3SJohn Marino _mem_stats.bytes -= mb->length;
16786d7f5d3SJohn Marino
16886d7f5d3SJohn Marino /* free the memory */
16986d7f5d3SJohn Marino free(mb);
17086d7f5d3SJohn Marino }
17186d7f5d3SJohn Marino
dm_realloc_aux(void * p,unsigned int s,const char * file,int line)17286d7f5d3SJohn Marino void *dm_realloc_aux(void *p, unsigned int s, const char *file, int line)
17386d7f5d3SJohn Marino {
17486d7f5d3SJohn Marino void *r;
17586d7f5d3SJohn Marino struct memblock *mb = ((struct memblock *) p) - 1;
17686d7f5d3SJohn Marino
17786d7f5d3SJohn Marino r = dm_malloc_aux_debug(s, file, line);
17886d7f5d3SJohn Marino
17986d7f5d3SJohn Marino if (p) {
18086d7f5d3SJohn Marino memcpy(r, p, mb->length);
18186d7f5d3SJohn Marino dm_free_aux(p);
18286d7f5d3SJohn Marino }
18386d7f5d3SJohn Marino
18486d7f5d3SJohn Marino return r;
18586d7f5d3SJohn Marino }
18686d7f5d3SJohn Marino
dm_dump_memory_debug(void)18786d7f5d3SJohn Marino int dm_dump_memory_debug(void)
18886d7f5d3SJohn Marino {
18986d7f5d3SJohn Marino unsigned long tot = 0;
19086d7f5d3SJohn Marino struct memblock *mb;
19186d7f5d3SJohn Marino char str[32];
19286d7f5d3SJohn Marino size_t c;
19386d7f5d3SJohn Marino
19486d7f5d3SJohn Marino if (_head)
19586d7f5d3SJohn Marino log_very_verbose("You have a memory leak:");
19686d7f5d3SJohn Marino
19786d7f5d3SJohn Marino for (mb = _head; mb; mb = mb->next) {
19886d7f5d3SJohn Marino for (c = 0; c < sizeof(str) - 1; c++) {
19986d7f5d3SJohn Marino if (c >= mb->length)
20086d7f5d3SJohn Marino str[c] = ' ';
20186d7f5d3SJohn Marino else if (*(char *)(mb->magic + c) == '\0')
20286d7f5d3SJohn Marino str[c] = '\0';
20386d7f5d3SJohn Marino else if (*(char *)(mb->magic + c) < ' ')
20486d7f5d3SJohn Marino str[c] = '?';
20586d7f5d3SJohn Marino else
20686d7f5d3SJohn Marino str[c] = *(char *)(mb->magic + c);
20786d7f5d3SJohn Marino }
20886d7f5d3SJohn Marino str[sizeof(str) - 1] = '\0';
20986d7f5d3SJohn Marino
21086d7f5d3SJohn Marino LOG_MESG(_LOG_INFO, mb->file, mb->line, 0,
21186d7f5d3SJohn Marino "block %d at %p, size %" PRIsize_t "\t [%s]",
21286d7f5d3SJohn Marino mb->id, mb->magic, mb->length, str);
21386d7f5d3SJohn Marino tot += mb->length;
21486d7f5d3SJohn Marino }
21586d7f5d3SJohn Marino
21686d7f5d3SJohn Marino if (_head)
21786d7f5d3SJohn Marino log_very_verbose("%ld bytes leaked in total", tot);
21886d7f5d3SJohn Marino
21986d7f5d3SJohn Marino return 1;
22086d7f5d3SJohn Marino }
22186d7f5d3SJohn Marino
dm_bounds_check_debug(void)22286d7f5d3SJohn Marino void dm_bounds_check_debug(void)
22386d7f5d3SJohn Marino {
22486d7f5d3SJohn Marino struct memblock *mb = _head;
22586d7f5d3SJohn Marino while (mb) {
22686d7f5d3SJohn Marino size_t i;
22786d7f5d3SJohn Marino char *ptr = ((char *) (mb + 1)) + mb->length;
22886d7f5d3SJohn Marino for (i = 0; i < sizeof(unsigned long); i++)
22986d7f5d3SJohn Marino if (*ptr++ != (char) mb->id)
23086d7f5d3SJohn Marino assert(!"Memory smash");
23186d7f5d3SJohn Marino
23286d7f5d3SJohn Marino mb = mb->next;
23386d7f5d3SJohn Marino }
23486d7f5d3SJohn Marino }
23586d7f5d3SJohn Marino
dm_malloc_aux(size_t s,const char * file __attribute ((unused)),int line __attribute ((unused)))23686d7f5d3SJohn Marino void *dm_malloc_aux(size_t s, const char *file __attribute((unused)),
23786d7f5d3SJohn Marino int line __attribute((unused)))
23886d7f5d3SJohn Marino {
23986d7f5d3SJohn Marino if (s > 50000000) {
24086d7f5d3SJohn Marino log_error("Huge memory allocation (size %" PRIsize_t
24186d7f5d3SJohn Marino ") rejected - metadata corruption?", s);
24286d7f5d3SJohn Marino return 0;
24386d7f5d3SJohn Marino }
24486d7f5d3SJohn Marino
24586d7f5d3SJohn Marino return malloc(s);
24686d7f5d3SJohn Marino }
247