Update from main archive 961219
[glibc.git] / malloc / mcheck.c
blob7f1112ea8b60882e6a08cfe519a292f3dfcc2032
1 /* Standard debugging hooks for `malloc'.
2 Copyright (C) 1990,91,92,93,94,95,96 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));
32 static __ptr_t (*old_malloc_hook) __P ((__malloc_size_t size));
33 static __ptr_t (*old_realloc_hook) __P ((__ptr_t ptr, __malloc_size_t size));
35 /* Function to call when something awful happens. */
36 static void (*abortfunc) __P ((enum mcheck_status));
38 /* Arbitrary magical numbers. */
39 #define MAGICWORD 0xfedabeeb
40 #define MAGICFREE 0xd8675309
41 #define MAGICBYTE ((char) 0xd7)
42 #define MALLOCFLOOD ((char) 0x93)
43 #define FREEFLOOD ((char) 0x95)
45 struct hdr
47 __malloc_size_t size; /* Exact size requested by user. */
48 unsigned long int magic; /* Magic number to check header integrity. */
51 #if defined(_LIBC) || defined(STDC_HEADERS) || defined(USG)
52 #define flood memset
53 #else
54 static void flood __P ((__ptr_t, int, __malloc_size_t));
55 static void
56 flood (ptr, val, size)
57 __ptr_t ptr;
58 int val;
59 __malloc_size_t size;
61 char *cp = ptr;
62 while (size--)
63 *cp++ = val;
65 #endif
67 static enum mcheck_status checkhdr __P ((const struct hdr *));
68 static enum mcheck_status
69 checkhdr (hdr)
70 const struct hdr *hdr;
72 enum mcheck_status status;
73 switch (hdr->magic)
75 default:
76 status = MCHECK_HEAD;
77 break;
78 case MAGICFREE:
79 status = MCHECK_FREE;
80 break;
81 case MAGICWORD:
82 if (((char *) &hdr[1])[hdr->size] != MAGICBYTE)
83 status = MCHECK_TAIL;
84 else
85 status = MCHECK_OK;
86 break;
88 if (status != MCHECK_OK)
89 (*abortfunc) (status);
90 return status;
93 static void freehook __P ((__ptr_t));
94 static void
95 freehook (ptr)
96 __ptr_t ptr;
98 if (ptr)
100 struct hdr *hdr = ((struct hdr *) ptr) - 1;
101 checkhdr (hdr);
102 hdr->magic = MAGICFREE;
103 flood (ptr, FREEFLOOD, hdr->size);
104 ptr = (__ptr_t) hdr;
106 __free_hook = old_free_hook;
107 free (ptr);
108 __free_hook = freehook;
111 static __ptr_t mallochook __P ((__malloc_size_t));
112 static __ptr_t
113 mallochook (size)
114 __malloc_size_t size;
116 struct hdr *hdr;
118 __malloc_hook = old_malloc_hook;
119 hdr = (struct hdr *) malloc (sizeof (struct hdr) + size + 1);
120 __malloc_hook = mallochook;
121 if (hdr == NULL)
122 return NULL;
124 hdr->size = size;
125 hdr->magic = MAGICWORD;
126 ((char *) &hdr[1])[size] = MAGICBYTE;
127 flood ((__ptr_t) (hdr + 1), MALLOCFLOOD, size);
128 return (__ptr_t) (hdr + 1);
131 static __ptr_t reallochook __P ((__ptr_t, __malloc_size_t));
132 static __ptr_t
133 reallochook (ptr, size)
134 __ptr_t ptr;
135 __malloc_size_t size;
137 struct hdr *hdr;
138 __malloc_size_t osize;
140 if (ptr)
142 hdr = ((struct hdr *) ptr) - 1;
143 osize = hdr->size;
145 checkhdr (hdr);
146 if (size < osize)
147 flood ((char *) ptr + size, FREEFLOOD, osize - size);
149 else
151 osize = 0;
152 hdr = NULL;
154 __free_hook = old_free_hook;
155 __malloc_hook = old_malloc_hook;
156 __realloc_hook = old_realloc_hook;
157 hdr = (struct hdr *) realloc ((__ptr_t) hdr, sizeof (struct hdr) + size + 1);
158 __free_hook = freehook;
159 __malloc_hook = mallochook;
160 __realloc_hook = reallochook;
161 if (hdr == NULL)
162 return NULL;
164 hdr->size = size;
165 hdr->magic = MAGICWORD;
166 ((char *) &hdr[1])[size] = MAGICBYTE;
167 if (size > osize)
168 flood ((char *) (hdr + 1) + osize, MALLOCFLOOD, size - osize);
169 return (__ptr_t) (hdr + 1);
172 static void mabort __P ((enum mcheck_status status));
173 static void
174 mabort (status)
175 enum mcheck_status status;
177 const char *msg;
178 switch (status)
180 case MCHECK_OK:
181 msg = _("memory is consistent, library is buggy");
182 break;
183 case MCHECK_HEAD:
184 msg = _("memory clobbered before allocated block");
185 break;
186 case MCHECK_TAIL:
187 msg = _("memory clobbered past end of allocated block");
188 break;
189 case MCHECK_FREE:
190 msg = _("block freed twice");
191 break;
192 default:
193 msg = _("bogus mcheck_status, library is buggy");
194 break;
196 #ifdef _LIBC
197 __libc_fatal (msg);
198 #else
199 fprintf (stderr, "mcheck: %s\n", msg);
200 fflush (stderr);
201 abort ();
202 #endif
205 static int mcheck_used = 0;
208 mcheck (func)
209 void (*func) __P ((enum mcheck_status));
211 abortfunc = (func != NULL) ? func : &mabort;
213 /* These hooks may not be safely inserted if malloc is already in use. */
214 if (!__malloc_initialized && !mcheck_used)
216 old_free_hook = __free_hook;
217 __free_hook = freehook;
218 old_malloc_hook = __malloc_hook;
219 __malloc_hook = mallochook;
220 old_realloc_hook = __realloc_hook;
221 __realloc_hook = reallochook;
222 mcheck_used = 1;
225 return mcheck_used ? 0 : -1;
228 enum mcheck_status
229 mprobe (__ptr_t ptr)
231 return mcheck_used ? checkhdr (ptr) : MCHECK_DISABLED;