1*86d7f5d3SJohn Marino /* $NetBSD: dbg_malloc.c,v 1.1.1.2 2009/12/02 00:26:09 haad Exp $ */
2*86d7f5d3SJohn Marino
3*86d7f5d3SJohn Marino /*
4*86d7f5d3SJohn Marino * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5*86d7f5d3SJohn Marino * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
6*86d7f5d3SJohn Marino *
7*86d7f5d3SJohn Marino * This file is part of the device-mapper userspace tools.
8*86d7f5d3SJohn Marino *
9*86d7f5d3SJohn Marino * This copyrighted material is made available to anyone wishing to use,
10*86d7f5d3SJohn Marino * modify, copy, or redistribute it subject to the terms and conditions
11*86d7f5d3SJohn Marino * of the GNU Lesser General Public License v.2.1.
12*86d7f5d3SJohn Marino *
13*86d7f5d3SJohn Marino * You should have received a copy of the GNU Lesser General Public License
14*86d7f5d3SJohn Marino * along with this program; if not, write to the Free Software Foundation,
15*86d7f5d3SJohn Marino * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16*86d7f5d3SJohn Marino */
17*86d7f5d3SJohn Marino
18*86d7f5d3SJohn Marino #include "dmlib.h"
19*86d7f5d3SJohn Marino
20*86d7f5d3SJohn Marino #include <assert.h>
21*86d7f5d3SJohn Marino #include <stdarg.h>
22*86d7f5d3SJohn Marino
dm_strdup_aux(const char * str,const char * file,int line)23*86d7f5d3SJohn Marino char *dm_strdup_aux(const char *str, const char *file, int line)
24*86d7f5d3SJohn Marino {
25*86d7f5d3SJohn Marino char *ret;
26*86d7f5d3SJohn Marino
27*86d7f5d3SJohn Marino if (!str) {
28*86d7f5d3SJohn Marino log_error("Internal error: dm_strdup called with NULL pointer");
29*86d7f5d3SJohn Marino return NULL;
30*86d7f5d3SJohn Marino }
31*86d7f5d3SJohn Marino
32*86d7f5d3SJohn Marino if ((ret = dm_malloc_aux_debug(strlen(str) + 1, file, line)))
33*86d7f5d3SJohn Marino strcpy(ret, str);
34*86d7f5d3SJohn Marino
35*86d7f5d3SJohn Marino return ret;
36*86d7f5d3SJohn Marino }
37*86d7f5d3SJohn Marino
38*86d7f5d3SJohn Marino struct memblock {
39*86d7f5d3SJohn Marino struct memblock *prev, *next; /* All allocated blocks are linked */
40*86d7f5d3SJohn Marino size_t length; /* Size of the requested block */
41*86d7f5d3SJohn Marino int id; /* Index of the block */
42*86d7f5d3SJohn Marino const char *file; /* File that allocated */
43*86d7f5d3SJohn Marino int line; /* Line that allocated */
44*86d7f5d3SJohn Marino void *magic; /* Address of this block */
45*86d7f5d3SJohn Marino } __attribute__((aligned(8)));
46*86d7f5d3SJohn Marino
47*86d7f5d3SJohn Marino static struct {
48*86d7f5d3SJohn Marino unsigned block_serialno;/* Non-decreasing serialno of block */
49*86d7f5d3SJohn Marino unsigned blocks_allocated; /* Current number of blocks allocated */
50*86d7f5d3SJohn Marino unsigned blocks_max; /* Max no of concurrently-allocated blocks */
51*86d7f5d3SJohn Marino unsigned int bytes, mbytes;
52*86d7f5d3SJohn Marino
53*86d7f5d3SJohn Marino } _mem_stats = {
54*86d7f5d3SJohn Marino 0, 0, 0, 0, 0};
55*86d7f5d3SJohn Marino
56*86d7f5d3SJohn Marino static struct memblock *_head = 0;
57*86d7f5d3SJohn Marino static struct memblock *_tail = 0;
58*86d7f5d3SJohn Marino
dm_malloc_aux_debug(size_t s,const char * file,int line)59*86d7f5d3SJohn Marino void *dm_malloc_aux_debug(size_t s, const char *file, int line)
60*86d7f5d3SJohn Marino {
61*86d7f5d3SJohn Marino struct memblock *nb;
62*86d7f5d3SJohn Marino size_t tsize = s + sizeof(*nb) + sizeof(unsigned long);
63*86d7f5d3SJohn Marino
64*86d7f5d3SJohn Marino if (s > 50000000) {
65*86d7f5d3SJohn Marino log_error("Huge memory allocation (size %" PRIsize_t
66*86d7f5d3SJohn Marino ") rejected - metadata corruption?", s);
67*86d7f5d3SJohn Marino return 0;
68*86d7f5d3SJohn Marino }
69*86d7f5d3SJohn Marino
70*86d7f5d3SJohn Marino if (!(nb = malloc(tsize))) {
71*86d7f5d3SJohn Marino log_error("couldn't allocate any memory, size = %" PRIsize_t,
72*86d7f5d3SJohn Marino s);
73*86d7f5d3SJohn Marino return 0;
74*86d7f5d3SJohn Marino }
75*86d7f5d3SJohn Marino
76*86d7f5d3SJohn Marino /* set up the file and line info */
77*86d7f5d3SJohn Marino nb->file = file;
78*86d7f5d3SJohn Marino nb->line = line;
79*86d7f5d3SJohn Marino
80*86d7f5d3SJohn Marino dm_bounds_check();
81*86d7f5d3SJohn Marino
82*86d7f5d3SJohn Marino /* setup fields */
83*86d7f5d3SJohn Marino nb->magic = nb + 1;
84*86d7f5d3SJohn Marino nb->length = s;
85*86d7f5d3SJohn Marino nb->id = ++_mem_stats.block_serialno;
86*86d7f5d3SJohn Marino nb->next = 0;
87*86d7f5d3SJohn Marino
88*86d7f5d3SJohn Marino /* stomp a pretty pattern across the new memory
89*86d7f5d3SJohn Marino and fill in the boundary bytes */
90*86d7f5d3SJohn Marino {
91*86d7f5d3SJohn Marino char *ptr = (char *) (nb + 1);
92*86d7f5d3SJohn Marino size_t i;
93*86d7f5d3SJohn Marino for (i = 0; i < s; i++)
94*86d7f5d3SJohn Marino *ptr++ = i & 0x1 ? (char) 0xba : (char) 0xbe;
95*86d7f5d3SJohn Marino
96*86d7f5d3SJohn Marino for (i = 0; i < sizeof(unsigned long); i++)
97*86d7f5d3SJohn Marino *ptr++ = (char) nb->id;
98*86d7f5d3SJohn Marino }
99*86d7f5d3SJohn Marino
100*86d7f5d3SJohn Marino nb->prev = _tail;
101*86d7f5d3SJohn Marino
102*86d7f5d3SJohn Marino /* link to tail of the list */
103*86d7f5d3SJohn Marino if (!_head)
104*86d7f5d3SJohn Marino _head = _tail = nb;
105*86d7f5d3SJohn Marino else {
106*86d7f5d3SJohn Marino _tail->next = nb;
107*86d7f5d3SJohn Marino _tail = nb;
108*86d7f5d3SJohn Marino }
109*86d7f5d3SJohn Marino
110*86d7f5d3SJohn Marino _mem_stats.blocks_allocated++;
111*86d7f5d3SJohn Marino if (_mem_stats.blocks_allocated > _mem_stats.blocks_max)
112*86d7f5d3SJohn Marino _mem_stats.blocks_max = _mem_stats.blocks_allocated;
113*86d7f5d3SJohn Marino
114*86d7f5d3SJohn Marino _mem_stats.bytes += s;
115*86d7f5d3SJohn Marino if (_mem_stats.bytes > _mem_stats.mbytes)
116*86d7f5d3SJohn Marino _mem_stats.mbytes = _mem_stats.bytes;
117*86d7f5d3SJohn Marino
118*86d7f5d3SJohn Marino /* log_debug("Allocated: %u %u %u", nb->id, _mem_stats.blocks_allocated,
119*86d7f5d3SJohn Marino _mem_stats.bytes); */
120*86d7f5d3SJohn Marino
121*86d7f5d3SJohn Marino return nb + 1;
122*86d7f5d3SJohn Marino }
123*86d7f5d3SJohn Marino
dm_free_aux(void * p)124*86d7f5d3SJohn Marino void dm_free_aux(void *p)
125*86d7f5d3SJohn Marino {
126*86d7f5d3SJohn Marino char *ptr;
127*86d7f5d3SJohn Marino size_t i;
128*86d7f5d3SJohn Marino struct memblock *mb = ((struct memblock *) p) - 1;
129*86d7f5d3SJohn Marino if (!p)
130*86d7f5d3SJohn Marino return;
131*86d7f5d3SJohn Marino
132*86d7f5d3SJohn Marino dm_bounds_check();
133*86d7f5d3SJohn Marino
134*86d7f5d3SJohn Marino /* sanity check */
135*86d7f5d3SJohn Marino assert(mb->magic == p);
136*86d7f5d3SJohn Marino
137*86d7f5d3SJohn Marino /* check data at the far boundary */
138*86d7f5d3SJohn Marino ptr = ((char *) mb) + sizeof(struct memblock) + mb->length;
139*86d7f5d3SJohn Marino for (i = 0; i < sizeof(unsigned long); i++)
140*86d7f5d3SJohn Marino if (*ptr++ != (char) mb->id)
141*86d7f5d3SJohn Marino assert(!"Damage at far end of block");
142*86d7f5d3SJohn Marino
143*86d7f5d3SJohn Marino /* have we freed this before ? */
144*86d7f5d3SJohn Marino assert(mb->id != 0);
145*86d7f5d3SJohn Marino
146*86d7f5d3SJohn Marino /* unlink */
147*86d7f5d3SJohn Marino if (mb->prev)
148*86d7f5d3SJohn Marino mb->prev->next = mb->next;
149*86d7f5d3SJohn Marino else
150*86d7f5d3SJohn Marino _head = mb->next;
151*86d7f5d3SJohn Marino
152*86d7f5d3SJohn Marino if (mb->next)
153*86d7f5d3SJohn Marino mb->next->prev = mb->prev;
154*86d7f5d3SJohn Marino else
155*86d7f5d3SJohn Marino _tail = mb->prev;
156*86d7f5d3SJohn Marino
157*86d7f5d3SJohn Marino mb->id = 0;
158*86d7f5d3SJohn Marino
159*86d7f5d3SJohn Marino /* stomp a different pattern across the memory */
160*86d7f5d3SJohn Marino ptr = ((char *) mb) + sizeof(struct memblock);
161*86d7f5d3SJohn Marino for (i = 0; i < mb->length; i++)
162*86d7f5d3SJohn Marino *ptr++ = i & 1 ? (char) 0xde : (char) 0xad;
163*86d7f5d3SJohn Marino
164*86d7f5d3SJohn Marino assert(_mem_stats.blocks_allocated);
165*86d7f5d3SJohn Marino _mem_stats.blocks_allocated--;
166*86d7f5d3SJohn Marino _mem_stats.bytes -= mb->length;
167*86d7f5d3SJohn Marino
168*86d7f5d3SJohn Marino /* free the memory */
169*86d7f5d3SJohn Marino free(mb);
170*86d7f5d3SJohn Marino }
171*86d7f5d3SJohn Marino
dm_realloc_aux(void * p,unsigned int s,const char * file,int line)172*86d7f5d3SJohn Marino void *dm_realloc_aux(void *p, unsigned int s, const char *file, int line)
173*86d7f5d3SJohn Marino {
174*86d7f5d3SJohn Marino void *r;
175*86d7f5d3SJohn Marino struct memblock *mb = ((struct memblock *) p) - 1;
176*86d7f5d3SJohn Marino
177*86d7f5d3SJohn Marino r = dm_malloc_aux_debug(s, file, line);
178*86d7f5d3SJohn Marino
179*86d7f5d3SJohn Marino if (p) {
180*86d7f5d3SJohn Marino memcpy(r, p, mb->length);
181*86d7f5d3SJohn Marino dm_free_aux(p);
182*86d7f5d3SJohn Marino }
183*86d7f5d3SJohn Marino
184*86d7f5d3SJohn Marino return r;
185*86d7f5d3SJohn Marino }
186*86d7f5d3SJohn Marino
dm_dump_memory_debug(void)187*86d7f5d3SJohn Marino int dm_dump_memory_debug(void)
188*86d7f5d3SJohn Marino {
189*86d7f5d3SJohn Marino unsigned long tot = 0;
190*86d7f5d3SJohn Marino struct memblock *mb;
191*86d7f5d3SJohn Marino char str[32];
192*86d7f5d3SJohn Marino size_t c;
193*86d7f5d3SJohn Marino
194*86d7f5d3SJohn Marino if (_head)
195*86d7f5d3SJohn Marino log_very_verbose("You have a memory leak:");
196*86d7f5d3SJohn Marino
197*86d7f5d3SJohn Marino for (mb = _head; mb; mb = mb->next) {
198*86d7f5d3SJohn Marino for (c = 0; c < sizeof(str) - 1; c++) {
199*86d7f5d3SJohn Marino if (c >= mb->length)
200*86d7f5d3SJohn Marino str[c] = ' ';
201*86d7f5d3SJohn Marino else if (*(char *)(mb->magic + c) == '\0')
202*86d7f5d3SJohn Marino str[c] = '\0';
203*86d7f5d3SJohn Marino else if (*(char *)(mb->magic + c) < ' ')
204*86d7f5d3SJohn Marino str[c] = '?';
205*86d7f5d3SJohn Marino else
206*86d7f5d3SJohn Marino str[c] = *(char *)(mb->magic + c);
207*86d7f5d3SJohn Marino }
208*86d7f5d3SJohn Marino str[sizeof(str) - 1] = '\0';
209*86d7f5d3SJohn Marino
210*86d7f5d3SJohn Marino LOG_MESG(_LOG_INFO, mb->file, mb->line, 0,
211*86d7f5d3SJohn Marino "block %d at %p, size %" PRIsize_t "\t [%s]",
212*86d7f5d3SJohn Marino mb->id, mb->magic, mb->length, str);
213*86d7f5d3SJohn Marino tot += mb->length;
214*86d7f5d3SJohn Marino }
215*86d7f5d3SJohn Marino
216*86d7f5d3SJohn Marino if (_head)
217*86d7f5d3SJohn Marino log_very_verbose("%ld bytes leaked in total", tot);
218*86d7f5d3SJohn Marino
219*86d7f5d3SJohn Marino return 1;
220*86d7f5d3SJohn Marino }
221*86d7f5d3SJohn Marino
dm_bounds_check_debug(void)222*86d7f5d3SJohn Marino void dm_bounds_check_debug(void)
223*86d7f5d3SJohn Marino {
224*86d7f5d3SJohn Marino struct memblock *mb = _head;
225*86d7f5d3SJohn Marino while (mb) {
226*86d7f5d3SJohn Marino size_t i;
227*86d7f5d3SJohn Marino char *ptr = ((char *) (mb + 1)) + mb->length;
228*86d7f5d3SJohn Marino for (i = 0; i < sizeof(unsigned long); i++)
229*86d7f5d3SJohn Marino if (*ptr++ != (char) mb->id)
230*86d7f5d3SJohn Marino assert(!"Memory smash");
231*86d7f5d3SJohn Marino
232*86d7f5d3SJohn Marino mb = mb->next;
233*86d7f5d3SJohn Marino }
234*86d7f5d3SJohn Marino }
235*86d7f5d3SJohn Marino
dm_malloc_aux(size_t s,const char * file __attribute ((unused)),int line __attribute ((unused)))236*86d7f5d3SJohn Marino void *dm_malloc_aux(size_t s, const char *file __attribute((unused)),
237*86d7f5d3SJohn Marino int line __attribute((unused)))
238*86d7f5d3SJohn Marino {
239*86d7f5d3SJohn Marino if (s > 50000000) {
240*86d7f5d3SJohn Marino log_error("Huge memory allocation (size %" PRIsize_t
241*86d7f5d3SJohn Marino ") rejected - metadata corruption?", s);
242*86d7f5d3SJohn Marino return 0;
243*86d7f5d3SJohn Marino }
244*86d7f5d3SJohn Marino
245*86d7f5d3SJohn Marino return malloc(s);
246*86d7f5d3SJohn Marino }
247