Update.
[glibc.git] / malloc / mcheck.c
blob9aa07a2e1fe6c3caa5d7c055db29e7b37869b97b
1 /* Standard debugging hooks for `malloc'.
2 Copyright (C) 1990-1997, 1999, 2000 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
25 # include <malloc.h>
26 # include <mcheck.h>
27 # include <stdint.h>
28 # include <stdio.h>
29 # include <libintl.h>
30 #endif
32 /* Old hook values. */
33 static void (*old_free_hook) __P ((__ptr_t ptr, __const __ptr_t));
34 static __ptr_t (*old_malloc_hook) __P ((__malloc_size_t size, const __ptr_t));
35 static __ptr_t (*old_realloc_hook) __P ((__ptr_t ptr, __malloc_size_t size,
36 __const __ptr_t));
38 /* Function to call when something awful happens. */
39 static void (*abortfunc) __P ((enum mcheck_status));
41 /* Arbitrary magical numbers. */
42 #define MAGICWORD 0xfedabeeb
43 #define MAGICFREE 0xd8675309
44 #define MAGICBYTE ((char) 0xd7)
45 #define MALLOCFLOOD ((char) 0x93)
46 #define FREEFLOOD ((char) 0x95)
48 struct hdr
50 __malloc_size_t size; /* Exact size requested by user. */
51 unsigned long int magic; /* Magic number to check header integrity. */
52 struct hdr *prev;
53 struct hdr *next;
56 /* This is the beginning of the list of all memory blocks allocated.
57 It is only constructed if the pedantic testing is requested. */
58 static struct hdr *root;
60 /* Nonzero if pedentic checking of all blocks is requested. */
61 static int pedantic;
63 #if defined _LIBC || defined STDC_HEADERS || defined USG
64 # include <string.h>
65 # define flood memset
66 #else
67 static void flood __P ((__ptr_t, int, __malloc_size_t));
68 static void
69 flood (ptr, val, size)
70 __ptr_t ptr;
71 int val;
72 __malloc_size_t size;
74 char *cp = ptr;
75 while (size--)
76 *cp++ = val;
78 #endif
80 static enum mcheck_status checkhdr __P ((const struct hdr *));
81 static enum mcheck_status
82 checkhdr (hdr)
83 const struct hdr *hdr;
85 enum mcheck_status status;
86 switch (hdr->magic ^ ((uintptr_t) hdr->prev + (uintptr_t) hdr->next))
88 default:
89 status = MCHECK_HEAD;
90 break;
91 case MAGICFREE:
92 status = MCHECK_FREE;
93 break;
94 case MAGICWORD:
95 if (((char *) &hdr[1])[hdr->size] != MAGICBYTE)
96 status = MCHECK_TAIL;
97 else
98 status = MCHECK_OK;
99 break;
101 if (status != MCHECK_OK)
102 (*abortfunc) (status);
103 return status;
106 void
107 mcheck_check_all ()
109 /* Walk through all the active blocks and test whether they were tempered
110 with. */
111 struct hdr *runp = root;
113 /* Temporarily turn off the checks. */
114 pedantic = 0;
116 while (runp != NULL)
118 (void) checkhdr (runp);
120 runp = runp->next;
123 /* Turn checks on again. */
124 pedantic = 1;
127 static void unlink_blk __P ((struct hdr *ptr));
128 static void
129 unlink_blk (ptr)
130 struct hdr *ptr;
132 if (ptr->next != NULL)
134 ptr->next->prev = ptr->prev;
135 ptr->next->magic = MAGICWORD ^ ((uintptr_t) ptr->next->prev
136 + (uintptr_t) ptr->next->next);
138 if (ptr->prev != NULL)
140 ptr->prev->next = ptr->next;
141 ptr->prev->magic = MAGICWORD ^ ((uintptr_t) ptr->prev->prev
142 + (uintptr_t) ptr->prev->next);
144 else
145 root = ptr->next;
148 static void link_blk __P ((struct hdr *ptr));
149 static void
150 link_blk (hdr)
151 struct hdr *hdr;
153 hdr->prev = NULL;
154 hdr->next = root;
155 root = hdr;
156 hdr->magic = MAGICWORD ^ (uintptr_t) hdr->next;
158 /* And the next block. */
159 if (hdr->next != NULL)
161 hdr->next->prev = hdr;
162 hdr->next->magic = MAGICWORD ^ ((uintptr_t) hdr
163 + (uintptr_t) hdr->next->next);
167 static void freehook __P ((__ptr_t, const __ptr_t));
168 static void
169 freehook (ptr, caller)
170 __ptr_t ptr;
171 const __ptr_t caller;
173 if (pedantic)
174 mcheck_check_all ();
175 if (ptr)
177 struct hdr *hdr = ((struct hdr *) ptr) - 1;
178 checkhdr (hdr);
179 hdr->magic = MAGICFREE;
180 unlink_blk (hdr);
181 hdr->prev = hdr->next = NULL;
182 flood (ptr, FREEFLOOD, hdr->size);
183 ptr = (__ptr_t) hdr;
185 __free_hook = old_free_hook;
186 if (old_free_hook != NULL)
187 (*old_free_hook) (ptr, caller);
188 else
189 free (ptr);
190 __free_hook = freehook;
193 static __ptr_t mallochook __P ((__malloc_size_t, const __ptr_t));
194 static __ptr_t
195 mallochook (size, caller)
196 __malloc_size_t size;
197 const __ptr_t caller;
199 struct hdr *hdr;
201 if (pedantic)
202 mcheck_check_all ();
204 __malloc_hook = old_malloc_hook;
205 if (old_malloc_hook != NULL)
206 hdr = (struct hdr *) (*old_malloc_hook) (sizeof (struct hdr) + size + 1,
207 caller);
208 else
209 hdr = (struct hdr *) malloc (sizeof (struct hdr) + size + 1);
210 __malloc_hook = mallochook;
211 if (hdr == NULL)
212 return NULL;
214 hdr->size = size;
215 link_blk (hdr);
216 ((char *) &hdr[1])[size] = MAGICBYTE;
217 flood ((__ptr_t) (hdr + 1), MALLOCFLOOD, size);
218 return (__ptr_t) (hdr + 1);
221 static __ptr_t reallochook __P ((__ptr_t, __malloc_size_t, const __ptr_t));
222 static __ptr_t
223 reallochook (ptr, size, caller)
224 __ptr_t ptr;
225 __malloc_size_t size;
226 const __ptr_t caller;
228 struct hdr *hdr;
229 __malloc_size_t osize;
231 if (pedantic)
232 mcheck_check_all ();
234 if (ptr)
236 hdr = ((struct hdr *) ptr) - 1;
237 osize = hdr->size;
239 checkhdr (hdr);
240 unlink_blk (hdr);
241 if (size < osize)
242 flood ((char *) ptr + size, FREEFLOOD, osize - size);
244 else
246 osize = 0;
247 hdr = NULL;
249 __free_hook = old_free_hook;
250 __malloc_hook = old_malloc_hook;
251 __realloc_hook = old_realloc_hook;
252 if (old_realloc_hook != NULL)
253 hdr = (struct hdr *) (*old_realloc_hook) ((__ptr_t) hdr,
254 sizeof (struct hdr) + size + 1,
255 caller);
256 else
257 hdr = (struct hdr *) realloc ((__ptr_t) hdr,
258 sizeof (struct hdr) + size + 1);
259 __free_hook = freehook;
260 __malloc_hook = mallochook;
261 __realloc_hook = reallochook;
262 if (hdr == NULL)
263 return NULL;
265 hdr->size = size;
266 link_blk (hdr);
267 ((char *) &hdr[1])[size] = MAGICBYTE;
268 if (size > osize)
269 flood ((char *) (hdr + 1) + osize, MALLOCFLOOD, size - osize);
270 return (__ptr_t) (hdr + 1);
273 static void mabort __P ((enum mcheck_status status));
274 static void
275 mabort (status)
276 enum mcheck_status status;
278 const char *msg;
279 switch (status)
281 case MCHECK_OK:
282 msg = _("memory is consistent, library is buggy\n");
283 break;
284 case MCHECK_HEAD:
285 msg = _("memory clobbered before allocated block\n");
286 break;
287 case MCHECK_TAIL:
288 msg = _("memory clobbered past end of allocated block\n");
289 break;
290 case MCHECK_FREE:
291 msg = _("block freed twice\n");
292 break;
293 default:
294 msg = _("bogus mcheck_status, library is buggy\n");
295 break;
297 #ifdef _LIBC
298 __libc_fatal (msg);
299 #else
300 fprintf (stderr, "mcheck: %s", msg);
301 fflush (stderr);
302 abort ();
303 #endif
306 static int mcheck_used;
309 mcheck (func)
310 void (*func) __P ((enum mcheck_status));
312 abortfunc = (func != NULL) ? func : &mabort;
314 /* These hooks may not be safely inserted if malloc is already in use. */
315 if (__malloc_initialized <= 0 && !mcheck_used)
317 old_free_hook = __free_hook;
318 __free_hook = freehook;
319 old_malloc_hook = __malloc_hook;
320 __malloc_hook = mallochook;
321 old_realloc_hook = __realloc_hook;
322 __realloc_hook = reallochook;
323 mcheck_used = 1;
326 return mcheck_used ? 0 : -1;
330 mcheck_pedantic (func)
331 void (*func) __P ((enum mcheck_status));
333 int res = mcheck (func);
334 if (res == 0)
335 pedantic = 1;
336 return res;
339 enum mcheck_status
340 mprobe (__ptr_t ptr)
342 return mcheck_used ? checkhdr (((struct hdr *) ptr) - 1) : MCHECK_DISABLED;