-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdebug.c
316 lines (280 loc) · 6.58 KB
/
debug.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* Copyright (C) 2000 [email protected]
This is free software distributed under the terms of the
GNU Public License. See the file COPYING for details.
$Id$ */
/* This is a very simple memory management debugger. It's useful for detecting
memory leaks, references to uninitialzed memory, bad pointers, buffer
overflow and getting an idea of how much memory is used by a program. */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "debug.h"
#ifdef DEBUG
#define MIN(a,b) ((a<b)?a:b)
/* tunable parameter. if you have a large amount of memory allocated, setting
this value higher will result in faster validation of pointers */
#define SIZE 4099
typedef struct _block
{
void *val;
int len;
const char *file;
int line;
struct _block *next;
}
BLOCK;
static BLOCK *Allocation[SIZE];
static int Memory_Usage = 0;
void
debug_init (void)
{
memset (Allocation, 0, sizeof (Allocation));
}
#if SIZEOF_LONG == 8
#define SHIFT 3
#else
#define SHIFT 2
#endif
/* hash the pointer value for insertion into the table */
static int
debug_hash (void *ptr)
{
int hash;
/* pointers are allocated on either 4 or 8 bytes boundaries, so we want
to ignore those values. this will cause consecutive pointers to hash
to the next bin */
hash = ((unsigned long) ptr) >> SHIFT;
return ((hash & 0x7fffffff) % SIZE);
}
static int
debug_overflow (BLOCK * block, const char *func)
{
if (*((unsigned char *) block->val + block->len) != END_BYTE)
{
fprintf (stderr,
"debug_%s: buffer overflow detected in data allocated at %s:%d\n",
func, block->file, block->line);
return 1;
}
return 0;
}
static void
debug_exhausted (const char *file, int line)
{
fprintf (stderr,
"debug_malloc(): memory exhausted at %s:%d (%d bytes allocated)\n",
file, line, Memory_Usage);
}
void *
debug_malloc (int bytes, const char *file, int line)
{
BLOCK *block, **ptr;
int offset;
if (bytes == 0)
{
fprintf (stderr, "debug_malloc(): 0 bytes requested at %s:%d\n",
file, line);
return 0;
}
block = malloc (sizeof (BLOCK));
if (!block)
{
debug_exhausted (file, line);
return 0;
}
block->val = malloc (bytes + 1);
if (!block->val)
{
debug_exhausted (__FILE__, __LINE__);
free (block);
return 0;
}
Memory_Usage += bytes;
block->len = bytes;
block->file = file;
block->line = line;
memset (block->val, ALLOC_BYTE, bytes);
*((unsigned char *) block->val + bytes) = END_BYTE;
offset = debug_hash (block->val);
for (ptr = &Allocation[offset]; *ptr; ptr = &(*ptr)->next)
{
if (block->val < (*ptr)->val)
break;
}
block->next = *ptr;
*ptr = block;
return block->val;
}
void *
debug_calloc (int count, int bytes, const char *file, int line)
{
void *ptr = debug_malloc (count * bytes, file, line);
if (!ptr)
return 0;
memset (ptr, 0, count * bytes);
return ptr;
}
static BLOCK *
find_block (void *ptr)
{
int offset = debug_hash (ptr);
BLOCK *block;
for (block = Allocation[offset]; block && ptr > block->val;
block = block->next);
return ((block != 0 && ptr == block->val) ? block : 0);
}
void *
debug_realloc (void *ptr, int bytes, const char *file, int line)
{
void *newptr;
BLOCK *block = 0;
if (bytes == 0)
{
debug_free (ptr, file, line);
return 0;
}
if (ptr)
{
block = find_block (ptr);
if (!block)
{
fprintf (stderr,
"debug_realloc(): invalid pointer at %s:%d\n", file,
line);
return 0;
}
debug_overflow (block, "realloc");
}
newptr = debug_malloc (bytes, file, line);
if (!newptr)
return 0;
if (ptr)
{
memcpy (newptr, ptr, MIN (bytes, block->len));
debug_free (ptr, file, line);
}
return newptr;
}
void
debug_free (void *ptr, const char *file, int line)
{
BLOCK **list, *block = 0;
int offset;
if (!ptr)
{
fprintf (stderr,
"debug_free: attempt to free NULL pointer at %s:%d\n",
file, line);
return;
}
offset = debug_hash (ptr);
if (!Allocation[offset])
{
fprintf (stderr,
"debug_free: attempt to free bogus pointer at %s:%d\n",
file, line);
return;
}
for (list = &Allocation[offset]; *list; list = &(*list)->next)
{
if ((*list)->val == ptr)
break;
}
if (!*list)
{
fprintf (stderr,
"debug_free: attempt to free bogus pointer at %s:%d\n",
file, line);
return;
}
block = *list;
/* remove the block from the list */
*list = (*list)->next;
debug_overflow (block, "free");
memset (block->val, FREE_BYTE, block->len);
free (block->val);
Memory_Usage -= block->len;
free (block);
}
#if 0
/* display the contents of an allocated block */
static void
debug_dump (BLOCK * block)
{
int i;
fputc ('\t', stderr);
for (i = 0; i < block->len && i < 8; i++)
fprintf (stderr, "%02x ", *((unsigned char *) block->val + i));
fputc ('\t', stderr);
for (i = 0; i < block->len && i < 8; i++)
fprintf (stderr, "%c",
isprint (*((unsigned char *) block->val + i)) ?
*((unsigned char *) block->val + i) : '.');
fputc ('\n', stderr);
}
#endif
void
debug_cleanup (void)
{
int i;
BLOCK *block;
for (i = 0; i < SIZE; i++)
{
for (block = Allocation[i]; block; block = block->next)
{
debug_overflow (block, "cleanup");
fprintf (stderr, "debug_cleanup: %d bytes allocated at %s:%d\n",
block->len, block->file, block->line);
#if 0
debug_dump (block);
#endif
}
}
if (Memory_Usage)
fprintf (stderr, "debug_cleanup: %d bytes total\n", Memory_Usage);
}
char *
debug_strdup (const char *s, const char *file, int line)
{
char *r;
r = debug_malloc (strlen (s) + 1, file, line);
if (!r)
return 0;
strcpy (r, s);
return r;
}
/* check to see if a pointer is valid. ptr can be an offset into an
allocation block, so don't use find_block() */
int
debug_valid (void *ptr, int len)
{
BLOCK *block;
int offset;
offset = debug_hash (ptr);
for(block=Allocation[offset];
block && (char*)ptr > (char*)block->val + block->len;
block=block->next)
;
if (!block)
{
fprintf (stderr, "debug_valid: invalid pointer\n");
return 0; /* not found */
}
/* ensure the pointer is within this block */
if(ptr<block->val)
{
fprintf(stderr,"debug_valid: invalid pointer\n");
return 0;
}
if (debug_overflow (block, "valid"))
return 0;
/* ensure that there are at least `len' bytes available */
return (((char*)ptr + len <= (char*)block->val + block->len));
}
int
debug_usage (void)
{
return Memory_Usage;
}
#endif /* DEBUG */