Update.
[glibc.git] / malloc / mcheck.c
blob8612c8cf2945454afdfa34610b68e1deb1075982
1 /* Standard debugging hooks for `malloc'.
2 Copyright (C) 1990-1997, 1999, 2000, 2001 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written May 1989 by Mike Haertel.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #ifndef _MALLOC_INTERNAL
22 # define _MALLOC_INTERNAL
23 # include <malloc.h>
24 # include <mcheck.h>
25 # include <stdint.h>
26 # include <stdio.h>
27 # include <libintl.h>
28 #endif
30 /* Old hook values. */
31 static void (*old_free_hook) __P ((__ptr_t ptr, __const __ptr_t));
32 static __ptr_t (*old_malloc_hook) __P ((__malloc_size_t size, const __ptr_t));
33 static __ptr_t (*old_realloc_hook) __P ((__ptr_t ptr, __malloc_size_t size,
34 __const __ptr_t));
36 /* Function to call when something awful happens. */
37 static void (*abortfunc) __P ((enum mcheck_status));
39 /* Arbitrary magical numbers. */
40 #define MAGICWORD 0xfedabeeb
41 #define MAGICFREE 0xd8675309
42 #define MAGICBYTE ((char) 0xd7)
43 #define MALLOCFLOOD ((char) 0x93)
44 #define FREEFLOOD ((char) 0x95)
46 struct hdr
48 __malloc_size_t size; /* Exact size requested by user. */
49 unsigned long int magic; /* Magic number to check header integrity. */
50 struct hdr *prev;
51 struct hdr *next;
54 /* This is the beginning of the list of all memory blocks allocated.
55 It is only constructed if the pedantic testing is requested. */
56 static struct hdr *root;
58 /* Nonzero if pedentic checking of all blocks is requested. */
59 static int pedantic;
61 #if defined _LIBC || defined STDC_HEADERS || defined USG
62 # include <string.h>
63 # define flood memset
64 #else
65 static void flood __P ((__ptr_t, int, __malloc_size_t));
66 static void
67 flood (ptr, val, size)
68 __ptr_t ptr;
69 int val;
70 __malloc_size_t size;
72 char *cp = ptr;
73 while (size--)
74 *cp++ = val;
76 #endif
78 static enum mcheck_status checkhdr __P ((const struct hdr *));
79 static enum mcheck_status
80 checkhdr (hdr)
81 const struct hdr *hdr;
83 enum mcheck_status status;
84 switch (hdr->magic ^ ((uintptr_t) hdr->prev + (uintptr_t) hdr->next))
86 default:
87 status = MCHECK_HEAD;
88 break;
89 case MAGICFREE:
90 status = MCHECK_FREE;
91 break;
92 case MAGICWORD:
93 if (((char *) &hdr[1])[hdr->size] != MAGICBYTE)
94 status = MCHECK_TAIL;
95 else
96 status = MCHECK_OK;
97 break;
99 if (status != MCHECK_OK)
100 (*abortfunc) (status);
101 return status;
104 void
105 mcheck_check_all ()
107 /* Walk through all the active blocks and test whether they were tempered
108 with. */
109 struct hdr *runp = root;
111 /* Temporarily turn off the checks. */
112 pedantic = 0;
114 while (runp != NULL)
116 (void) checkhdr (runp);
118 runp = runp->next;
121 /* Turn checks on again. */
122 pedantic = 1;
125 static void unlink_blk __P ((struct hdr *ptr));
126 static void
127 unlink_blk (ptr)
128 struct hdr *ptr;
130 if (ptr->next != NULL)
132 ptr->next->prev = ptr->prev;
133 ptr->next->magic = MAGICWORD ^ ((uintptr_t) ptr->next->prev
134 + (uintptr_t) ptr->next->next);
136 if (ptr->prev != NULL)
138 ptr->prev->next = ptr->next;
139 ptr->prev->magic = MAGICWORD ^ ((uintptr_t) ptr->prev->prev
140 + (uintptr_t) ptr->prev->next);
142 else
143 root = ptr->next;
146 static void link_blk __P ((struct hdr *ptr));
147 static void
148 link_blk (hdr)
149 struct hdr *hdr;
151 hdr->prev = NULL;
152 hdr->next = root;
153 root = hdr;
154 hdr->magic = MAGICWORD ^ (uintptr_t) hdr->next;
156 /* And the next block. */
157 if (hdr->next != NULL)
159 hdr->next->prev = hdr;
160 hdr->next->magic = MAGICWORD ^ ((uintptr_t) hdr
161 + (uintptr_t) hdr->next->next);
165 static void freehook __P ((__ptr_t, const __ptr_t));
166 static void
167 freehook (ptr, caller)
168 __ptr_t ptr;
169 const __ptr_t caller;
171 if (pedantic)
172 mcheck_check_all ();
173 if (ptr)
175 struct hdr *hdr = ((struct hdr *) ptr) - 1;
176 checkhdr (hdr);
177 hdr->magic = MAGICFREE;
178 unlink_blk (hdr);
179 hdr->prev = hdr->next = NULL;
180 flood (ptr, FREEFLOOD, hdr->size);
181 ptr = (__ptr_t) hdr;
183 __free_hook = old_free_hook;
184 if (old_free_hook != NULL)
185 (*old_free_hook) (ptr, caller);
186 else
187 free (ptr);
188 __free_hook = freehook;
191 static __ptr_t mallochook __P ((__malloc_size_t, const __ptr_t));
192 static __ptr_t
193 mallochook (size, caller)
194 __malloc_size_t size;
195 const __ptr_t caller;
197 struct hdr *hdr;
199 if (pedantic)
200 mcheck_check_all ();
202 __malloc_hook = old_malloc_hook;
203 if (old_malloc_hook != NULL)
204 hdr = (struct hdr *) (*old_malloc_hook) (sizeof (struct hdr) + size + 1,
205 caller);
206 else
207 hdr = (struct hdr *) malloc (sizeof (struct hdr) + size + 1);
208 __malloc_hook = mallochook;
209 if (hdr == NULL)
210 return NULL;
212 hdr->size = size;
213 link_blk (hdr);
214 ((char *) &hdr[1])[size] = MAGICBYTE;
215 flood ((__ptr_t) (hdr + 1), MALLOCFLOOD, size);
216 return (__ptr_t) (hdr + 1);
219 static __ptr_t reallochook __P ((__ptr_t, __malloc_size_t, const __ptr_t));
220 static __ptr_t
221 reallochook (ptr, size, caller)
222 __ptr_t ptr;
223 __malloc_size_t size;
224 const __ptr_t caller;
226 struct hdr *hdr;
227 __malloc_size_t osize;
229 if (pedantic)
230 mcheck_check_all ();
232 if (ptr)
234 hdr = ((struct hdr *) ptr) - 1;
235 osize = hdr->size;
237 checkhdr (hdr);
238 unlink_blk (hdr);
239 if (size < osize)
240 flood ((char *) ptr + size, FREEFLOOD, osize - size);
242 else
244 osize = 0;
245 hdr = NULL;
247 __free_hook = old_free_hook;
248 __malloc_hook = old_malloc_hook;
249 __realloc_hook = old_realloc_hook;
250 if (old_realloc_hook != NULL)
251 hdr = (struct hdr *) (*old_realloc_hook) ((__ptr_t) hdr,
252 sizeof (struct hdr) + size + 1,
253 caller);
254 else
255 hdr = (struct hdr *) realloc ((__ptr_t) hdr,
256 sizeof (struct hdr) + size + 1);
257 __free_hook = freehook;
258 __malloc_hook = mallochook;
259 __realloc_hook = reallochook;
260 if (hdr == NULL)
261 return NULL;
263 hdr->size = size;
264 link_blk (hdr);
265 ((char *) &hdr[1])[size] = MAGICBYTE;
266 if (size > osize)
267 flood ((char *) (hdr + 1) + osize, MALLOCFLOOD, size - osize);
268 return (__ptr_t) (hdr + 1);
271 static void mabort __P ((enum mcheck_status status))
272 __attribute__ ((noreturn));
273 static void
274 mabort (status)
275 enum mcheck_status status;
277 const char *msg;
278 switch (status)
280 case MCHECK_OK:
281 msg = _("memory is consistent, library is buggy\n");
282 break;
283 case MCHECK_HEAD:
284 msg = _("memory clobbered before allocated block\n");
285 break;
286 case MCHECK_TAIL:
287 msg = _("memory clobbered past end of allocated block\n");
288 break;
289 case MCHECK_FREE:
290 msg = _("block freed twice\n");
291 break;
292 default:
293 msg = _("bogus mcheck_status, library is buggy\n");
294 break;
296 #ifdef _LIBC
297 __libc_fatal (msg);
298 #else
299 fprintf (stderr, "mcheck: %s", msg);
300 fflush (stderr);
301 abort ();
302 #endif
305 static int mcheck_used;
308 mcheck (func)
309 void (*func) __P ((enum mcheck_status));
311 abortfunc = (func != NULL) ? func : &mabort;
313 /* These hooks may not be safely inserted if malloc is already in use. */
314 if (__malloc_initialized <= 0 && !mcheck_used)
316 /* We call malloc() once here to ensure it is initialized. */
317 void *p = malloc (0);
318 free (p);
320 old_free_hook = __free_hook;
321 __free_hook = freehook;
322 old_malloc_hook = __malloc_hook;
323 __malloc_hook = mallochook;
324 old_realloc_hook = __realloc_hook;
325 __realloc_hook = reallochook;
326 mcheck_used = 1;
329 return mcheck_used ? 0 : -1;
333 mcheck_pedantic (func)
334 void (*func) __P ((enum mcheck_status));
336 int res = mcheck (func);
337 if (res == 0)
338 pedantic = 1;
339 return res;
342 enum mcheck_status
343 mprobe (__ptr_t ptr)
345 return mcheck_used ? checkhdr (((struct hdr *) ptr) - 1) : MCHECK_DISABLED;