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 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
|
/*
C K C M D B . C -- malloc debugger.
*/
/*
Author: Howie Kaye, Columbia University Center for Computing Activities.
Copyright (C) 1985, 1999,
Trustees of Columbia University in the City of New York.
All rights reserved. See the C-Kermit COPYING.TXT file or the
copyright text in the ckcmai.c module for disclaimer and permissions.
*/
/* Use the real ones in this module! */
#ifdef malloc
#undef malloc
#endif /* malloc */
#ifdef calloc
#undef calloc
#endif /* calloc */
#ifdef realloc
#undef realloc
#endif /* realloc */
#ifdef free
#undef free
#endif /* free */
#include "ckcsym.h"
#include <stdio.h>
#include "ckcdeb.h"
#ifdef COHERENT
_PROTOTYP ( FILE * fdopen, (int, char *) );
#endif /* COHERENT */
/*
memdebug:
variable to control memory debugging.
if memdebug == 1, then action is always taken.
if memdebug == 0, then no action is taken.
if memdebug == -1, then the user is asked (works well with gdb).
*/
int memdebug = -1;
int disabled = 0;
int inited = 0;
/*
To use this package, compile your program with:
-Dmalloc=dmalloc -Dfree=dfree =Dcalloc=dcalloc ... -DMDEBUG
and then link it with ckcmdb.c.
*/
#ifdef MDEBUG
#ifndef M_SIZE_T
#ifdef NEXT
#define M_SIZE_T size_t
#else
#ifdef SUNOS41
#define M_SIZE_T unsigned
#else
#define M_SIZE_T int
#endif /* SUNOS41 */
#endif /* NEXT */
#endif /* M_SIZE_T */
#ifdef CK_ANSIC
_PROTOTYP( void free, (void *) );
_PROTOTYP( void * malloc, (size_t) );
_PROTOTYP( void * realloc, (void *, size_t) );
#else
_PROTOTYP( VOID free, (char *) );
_PROTOTYP( char * malloc, (M_SIZE_T) );
_PROTOTYP( char * realloc, (char *, M_SIZE_T) );
#endif /* NEXT */
_PROTOTYP( VOID m_insert, (char *) );
_PROTOTYP( int m_delete, (char *) );
_PROTOTYP( char * dmalloc, (int) );
_PROTOTYP( char * dcalloc, (int, int) );
_PROTOTYP( char * drealloc, (char *, int) );
_PROTOTYP( char *set_range_check, (char *, int) );
_PROTOTYP( char *check_range, (char *) );
_PROTOTYP( static char *maybe_check_range, (char *) );
_PROTOTYP( static VOID maybe_quit, (char *) );
_PROTOTYP( static int ask, (char *) );
#ifndef min
#define min(x,y) ((x) < (y) ? (x) : (y))
#endif /* min */
#define RANGE "ABCDEFGHIJKLMNOP"
#define INTSIZE sizeof(int)
#define LONGSIZE sizeof(long)
#define RSIZE sizeof(RANGE)
#define RFRONT min((RSIZE/2),LONGSIZE)
#define RBACK min((RSIZE-RFRONT),LONGSIZE)
char *
dmalloc(size) int size; {
char *cp;
cp = malloc(size + RSIZE + INTSIZE);
if (cp) {
cp = set_range_check(cp, size);
m_insert(cp);
}
return(cp);
}
char *
dcalloc(nelem, elsize) int nelem, elsize; {
char *cp;
cp = dmalloc(nelem * elsize);
if (cp)
memset(cp, 0, nelem * elsize);
return(cp);
}
char *
drealloc(bp,size) char *bp; int size; {
char *cp;
if (bp == NULL) {
maybe_quit("Freeing NULL pointer");
} else {
m_delete(bp);
cp = check_range(bp);
}
cp = realloc(cp, size + RSIZE + INTSIZE);
if (cp) {
cp = set_range_check(cp, size);
m_insert(cp);
}
return(cp);
}
VOID
dfree(cp) char *cp; {
if (cp == NULL)
maybe_quit("Freeing NULL pointer");
else {
switch(m_delete(cp)) {
case 0:
cp = maybe_check_range(cp);
break;
case 1:
cp = check_range(cp);
break;
case 2:
break;
}
}
#ifndef CK_ANSIC
return(free(cp));
#endif /* CK_ANSIC */
}
char *
set_range_check(cp,size) char *cp; int size; {
register int i;
int tmp = size;
for(i = 0; i < INTSIZE; i++) { /* set the size in the string */
cp[i] = tmp & 0xff;
tmp >>= 8;
}
cp += INTSIZE; /* skip the size */
for(i = 0; i < RFRONT; i++) /* set the front of the range check */
cp[i] = RANGE[i]; /* string */
cp += RFRONT; /* skip the front range check */
for(i = 0; i < RBACK; i++) /* set the back odf the range check */
cp[i+size] = RANGE[i+RFRONT];
return(cp);
}
/*
Put calls to this routine in your code any place where you want to
check whether you've copied too many characters into a malloc'd space.
*/
char *
check_range(cp) char *cp; {
register char *bp = cp - RFRONT - INTSIZE;
char *xp = bp;
register int i;
int size = 0;
for(i = 0 ; i < INTSIZE; i++) { /* get the size out of the string */
size <<= 8;
size |= bp[INTSIZE-i-1] & 0xff;
}
bp += INTSIZE;
for(i = 0; i < RFRONT; i++) /* check front range check */
if (bp[i] != RANGE[i]) {
maybe_quit("leftside malloc buffer overrun");
break;
}
bp += RFRONT; /* skip front range check */
for(i = 0; i < RBACK; i++) /* check back range check */
if (bp[i+size] != RANGE[i+RFRONT]) {
maybe_quit("rightside malloc buffer overrun");
break;
}
return(xp);
}
static char *
maybe_check_range(cp) char *cp; {
register char *bp = cp - RFRONT - INTSIZE;
char *xp = bp;
register int i;
int size = 0;
for(i = 0 ; i < INTSIZE; i++) { /* get the size out of the string */
size <<= 8;
size |= bp[INTSIZE-i-1] & 0xff;
}
bp += INTSIZE;
for(i = 0; i < RFRONT; i++) /* check front range check */
if (bp[i] != RANGE[i]) {
return(cp);
}
bp += RFRONT; /* skip front range check */
for(i = 0; i < RBACK; i++) /* check back range check */
if (bp[i+size] != RANGE[i+RFRONT]) {
fprintf(stderr,"rightside malloc buffer overrun\n");
abort();
break;
}
return(xp);
}
#define BUCKETS 10000
char *m_used[BUCKETS];
char *m_used2[BUCKETS];
VOID
m_insert(cp) register char *cp; {
register int i;
if (disabled)
return;
for(i = 0; i < BUCKETS; i++)
if (m_used[i] == 0) {
m_used[i] = cp;
return;
}
disabled ++;
}
static VOID
m_insert2(cp) register char *cp; {
register int i;
if (disabled)
return;
for(i = 0; i < BUCKETS; i++)
if (m_used2[i] == 0) {
m_used2[i] = cp;
return;
}
disabled ++;
}
int
m_delete(cp) register char *cp; {
register int i;
for(i = 0; i < BUCKETS; i++)
if (m_used[i] == cp) {
m_used[i] = 0;
return(1);
}
for(i = 0; i < BUCKETS; i++)
if (m_used2[i] == cp) {
m_used2[i] = 0;
return(2);
}
if (disabled)
return(0);
maybe_quit("Freeing unmalloc'ed pointer");
return(0);
}
VOID
m_init() {
register int i;
inited = 1;
disabled = 0;
#ifdef NEXT
malloc_debug(2+4+8+16);
#endif /* NEXT */
for(i = 0; i < BUCKETS; i++)
m_used[i] = 0;
}
VOID
m_done() {
register int i,j=0;
if (disabled)
return;
for(i = 0; i < BUCKETS; i++)
if (m_used[i] != 0) {
if (memdebug) {
if (j == 0)
fprintf(stderr,"unfree'ed buffers, indices: ");
fprintf(stderr,"%d, ", i);
j++;
}
}
if (j)
fprintf(stderr,"\n");
for(i = 0; i < BUCKETS; i++)
if (m_used2[i] != 0) {
if (memdebug) {
if (j == 0)
fprintf(stderr,"unfree'ed registered buffers, indices: ");
fprintf(stderr,"%d, ", i);
j++;
}
}
if (j)
fprintf(stderr,"\n");
if (j)
maybe_quit("Unfree'ed malloc buffers");
}
VOID
m_checkranges() {
int i;
for ( i = 0; i < BUCKETS; i++)
if (m_used[i])
check_range(m_used[i]);
}
static VOID
maybe_quit(str) char *str; {
debug(F100,"mdebug maybe_quit","",0);
if (memdebug == 0)
return;
fprintf(stderr,"%s\n",str);
if (memdebug == 1)
abort();
if (memdebug == -1)
if (ask("Quit? "))
abort();
}
static int
ask(str) char *str; {
char buf[100];
FILE *in;
int fd;
fd = dup(fileno(stdin));
in = fdopen(fd, "r");
while(1) {
fprintf(stderr,str);
fflush(stderr);
if (fgets(buf, 99, in) == NULL) /* EOF? */
return(0);
if (buf[0] == 'n' || buf[0] == 'N') {
fclose(in);
return(0);
}
if (buf[0] == 'y' || buf[0] == 'Y') {
fclose(in);
return(1);
}
fprintf(stderr,"please answer y/n.\n");
}
}
#endif /* MDEBUG */
|