Update.
[glibc.git] / malloc / mcheck.c
blobc5baa3d4a7623b8da4766686fb07c86c825f62ce
1 /* Standard debugging hooks for `malloc'.
2 Copyright (C) 1990,91,92,93,94,95,96,97 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 <stdio.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. */
52 #if defined _LIBC || defined STDC_HEADERS || defined USG
53 # include <string.h>
54 # define flood memset
55 #else
56 static void flood __P ((__ptr_t, int, __malloc_size_t));
57 static void
58 flood (ptr, val, size)
59 __ptr_t ptr;
60 int val;
61 __malloc_size_t size;
63 char *cp = ptr;
64 while (size--)
65 *cp++ = val;
67 #endif
69 static enum mcheck_status checkhdr __P ((const struct hdr *));
70 static enum mcheck_status
71 checkhdr (hdr)
72 const struct hdr *hdr;
74 enum mcheck_status status;
75 switch (hdr->magic)
77 default:
78 status = MCHECK_HEAD;
79 break;
80 case MAGICFREE:
81 status = MCHECK_FREE;
82 break;
83 case MAGICWORD:
84 if (((char *) &hdr[1])[hdr->size] != MAGICBYTE)
85 status = MCHECK_TAIL;
86 else
87 status = MCHECK_OK;
88 break;
90 if (status != MCHECK_OK)
91 (*abortfunc) (status);
92 return status;
95 static void freehook __P ((__ptr_t, const __ptr_t));
96 static void
97 freehook (ptr, caller)
98 __ptr_t ptr;
99 const __ptr_t caller;
101 if (ptr)
103 struct hdr *hdr = ((struct hdr *) ptr) - 1;
104 checkhdr (hdr);
105 hdr->magic = MAGICFREE;
106 flood (ptr, FREEFLOOD, hdr->size);
107 ptr = (__ptr_t) hdr;
109 __free_hook = old_free_hook;
110 if (old_free_hook != NULL)
111 (*old_free_hook) (ptr, caller);
112 else
113 free (ptr);
114 __free_hook = freehook;
117 static __ptr_t mallochook __P ((__malloc_size_t, const __ptr_t));
118 static __ptr_t
119 mallochook (size, caller)
120 __malloc_size_t size;
121 const __ptr_t caller;
123 struct hdr *hdr;
125 __malloc_hook = old_malloc_hook;
126 if (old_malloc_hook != NULL)
127 hdr = (struct hdr *) (*old_malloc_hook) (sizeof (struct hdr) + size + 1,
128 caller);
129 else
130 hdr = (struct hdr *) malloc (sizeof (struct hdr) + size + 1);
131 __malloc_hook = mallochook;
132 if (hdr == NULL)
133 return NULL;
135 hdr->size = size;
136 hdr->magic = MAGICWORD;
137 ((char *) &hdr[1])[size] = MAGICBYTE;
138 flood ((__ptr_t) (hdr + 1), MALLOCFLOOD, size);
139 return (__ptr_t) (hdr + 1);
142 static __ptr_t reallochook __P ((__ptr_t, __malloc_size_t, const __ptr_t));
143 static __ptr_t
144 reallochook (ptr, size, caller)
145 __ptr_t ptr;
146 __malloc_size_t size;
147 const __ptr_t caller;
149 struct hdr *hdr;
150 __malloc_size_t osize;
152 if (ptr)
154 hdr = ((struct hdr *) ptr) - 1;
155 osize = hdr->size;
157 checkhdr (hdr);
158 if (size < osize)
159 flood ((char *) ptr + size, FREEFLOOD, osize - size);
161 else
163 osize = 0;
164 hdr = NULL;
166 __free_hook = old_free_hook;
167 __malloc_hook = old_malloc_hook;
168 __realloc_hook = old_realloc_hook;
169 if (old_realloc_hook != NULL)
170 hdr = (struct hdr *) (*old_realloc_hook) ((__ptr_t) hdr,
171 sizeof (struct hdr) + size + 1,
172 caller);
173 else
174 hdr = (struct hdr *) realloc ((__ptr_t) hdr,
175 sizeof (struct hdr) + size + 1);
176 __free_hook = freehook;
177 __malloc_hook = mallochook;
178 __realloc_hook = reallochook;
179 if (hdr == NULL)
180 return NULL;
182 hdr->size = size;
183 hdr->magic = MAGICWORD;
184 ((char *) &hdr[1])[size] = MAGICBYTE;
185 if (size > osize)
186 flood ((char *) (hdr + 1) + osize, MALLOCFLOOD, size - osize);
187 return (__ptr_t) (hdr + 1);
190 static void mabort __P ((enum mcheck_status status));
191 static void
192 mabort (status)
193 enum mcheck_status status;
195 const char *msg;
196 switch (status)
198 case MCHECK_OK:
199 msg = _("memory is consistent, library is buggy\n");
200 break;
201 case MCHECK_HEAD:
202 msg = _("memory clobbered before allocated block\n");
203 break;
204 case MCHECK_TAIL:
205 msg = _("memory clobbered past end of allocated block\n");
206 break;
207 case MCHECK_FREE:
208 msg = _("block freed twice\n");
209 break;
210 default:
211 msg = _("bogus mcheck_status, library is buggy\n");
212 break;
214 #ifdef _LIBC
215 __libc_fatal (msg);
216 #else
217 fprintf (stderr, "mcheck: %s", msg);
218 fflush (stderr);
219 abort ();
220 #endif
223 static int mcheck_used = 0;
226 mcheck (func)
227 void (*func) __P ((enum mcheck_status));
229 abortfunc = (func != NULL) ? func : &mabort;
231 /* These hooks may not be safely inserted if malloc is already in use. */
232 if (__malloc_initialized <= 0 && !mcheck_used)
234 old_free_hook = __free_hook;
235 __free_hook = freehook;
236 old_malloc_hook = __malloc_hook;
237 __malloc_hook = mallochook;
238 old_realloc_hook = __realloc_hook;
239 __realloc_hook = reallochook;
240 mcheck_used = 1;
243 return mcheck_used ? 0 : -1;
246 enum mcheck_status
247 mprobe (__ptr_t ptr)
249 return mcheck_used ? checkhdr (((struct hdr *) ptr) - 1) : MCHECK_DISABLED;