]>
sourceware.org Git - glibc.git/blob - malloc/mcheck.c
1 /* Standard debugging hooks for `malloc'.
2 Copyright (C) 1990,91,92,93,94,95,96,97,99 Free Software Foundation, Inc.
3 Written May 1989 by Mike Haertel.
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
20 The author may be reached (Email) at the address mike@ai.mit.edu,
21 or (US mail) as Mike Haertel c/o Free Software Foundation. */
23 #ifndef _MALLOC_INTERNAL
24 # define _MALLOC_INTERNAL
31 /* Old hook values. */
32 static void (*old_free_hook
) __P ((__ptr_t ptr
, __const __ptr_t
));
33 static __ptr_t (*old_malloc_hook
) __P ((__malloc_size_t size
, const __ptr_t
));
34 static __ptr_t (*old_realloc_hook
) __P ((__ptr_t ptr
, __malloc_size_t size
,
37 /* Function to call when something awful happens. */
38 static void (*abortfunc
) __P ((enum mcheck_status
));
40 /* Arbitrary magical numbers. */
41 #define MAGICWORD 0xfedabeeb
42 #define MAGICFREE 0xd8675309
43 #define MAGICBYTE ((char) 0xd7)
44 #define MALLOCFLOOD ((char) 0x93)
45 #define FREEFLOOD ((char) 0x95)
49 __malloc_size_t size
; /* Exact size requested by user. */
50 unsigned long int magic
; /* Magic number to check header integrity. */
53 #if defined _LIBC || defined STDC_HEADERS || defined USG
57 static void flood
__P ((__ptr_t
, int, __malloc_size_t
));
59 flood (ptr
, val
, size
)
70 static enum mcheck_status checkhdr
__P ((const struct hdr
*));
71 static enum mcheck_status
73 const struct hdr
*hdr
;
75 enum mcheck_status status
;
85 if (((char *) &hdr
[1])[hdr
->size
] != MAGICBYTE
)
91 if (status
!= MCHECK_OK
)
92 (*abortfunc
) (status
);
96 static void freehook
__P ((__ptr_t
, const __ptr_t
));
98 freehook (ptr
, caller
)
100 const __ptr_t caller
;
104 struct hdr
*hdr
= ((struct hdr
*) ptr
) - 1;
106 hdr
->magic
= MAGICFREE
;
107 flood (ptr
, FREEFLOOD
, hdr
->size
);
110 __free_hook
= old_free_hook
;
111 if (old_free_hook
!= NULL
)
112 (*old_free_hook
) (ptr
, caller
);
115 __free_hook
= freehook
;
118 static __ptr_t mallochook
__P ((__malloc_size_t
, const __ptr_t
));
120 mallochook (size
, caller
)
121 __malloc_size_t size
;
122 const __ptr_t caller
;
126 __malloc_hook
= old_malloc_hook
;
127 if (old_malloc_hook
!= NULL
)
128 hdr
= (struct hdr
*) (*old_malloc_hook
) (sizeof (struct hdr
) + size
+ 1,
131 hdr
= (struct hdr
*) malloc (sizeof (struct hdr
) + size
+ 1);
132 __malloc_hook
= mallochook
;
137 hdr
->magic
= MAGICWORD
;
138 ((char *) &hdr
[1])[size
] = MAGICBYTE
;
139 flood ((__ptr_t
) (hdr
+ 1), MALLOCFLOOD
, size
);
140 return (__ptr_t
) (hdr
+ 1);
143 static __ptr_t reallochook
__P ((__ptr_t
, __malloc_size_t
, const __ptr_t
));
145 reallochook (ptr
, size
, caller
)
147 __malloc_size_t size
;
148 const __ptr_t caller
;
151 __malloc_size_t osize
;
155 hdr
= ((struct hdr
*) ptr
) - 1;
160 flood ((char *) ptr
+ size
, FREEFLOOD
, osize
- size
);
167 __free_hook
= old_free_hook
;
168 __malloc_hook
= old_malloc_hook
;
169 __realloc_hook
= old_realloc_hook
;
170 if (old_realloc_hook
!= NULL
)
171 hdr
= (struct hdr
*) (*old_realloc_hook
) ((__ptr_t
) hdr
,
172 sizeof (struct hdr
) + size
+ 1,
175 hdr
= (struct hdr
*) realloc ((__ptr_t
) hdr
,
176 sizeof (struct hdr
) + size
+ 1);
177 __free_hook
= freehook
;
178 __malloc_hook
= mallochook
;
179 __realloc_hook
= reallochook
;
184 hdr
->magic
= MAGICWORD
;
185 ((char *) &hdr
[1])[size
] = MAGICBYTE
;
187 flood ((char *) (hdr
+ 1) + osize
, MALLOCFLOOD
, size
- osize
);
188 return (__ptr_t
) (hdr
+ 1);
191 static void mabort
__P ((enum mcheck_status status
));
194 enum mcheck_status status
;
200 msg
= _("memory is consistent, library is buggy\n");
203 msg
= _("memory clobbered before allocated block\n");
206 msg
= _("memory clobbered past end of allocated block\n");
209 msg
= _("block freed twice\n");
212 msg
= _("bogus mcheck_status, library is buggy\n");
218 fprintf (stderr
, "mcheck: %s", msg
);
224 static int mcheck_used
;
228 void (*func
) __P ((enum mcheck_status
));
230 abortfunc
= (func
!= NULL
) ? func
: &mabort
;
232 /* These hooks may not be safely inserted if malloc is already in use. */
233 if (__malloc_initialized
<= 0 && !mcheck_used
)
235 old_free_hook
= __free_hook
;
236 __free_hook
= freehook
;
237 old_malloc_hook
= __malloc_hook
;
238 __malloc_hook
= mallochook
;
239 old_realloc_hook
= __realloc_hook
;
240 __realloc_hook
= reallochook
;
244 return mcheck_used
? 0 : -1;
250 return mcheck_used
? checkhdr (((struct hdr
*) ptr
) - 1) : MCHECK_DISABLED
;
This page took 0.0546 seconds and 6 git commands to generate.