Terminate last line
[glibc/history.git] / malloc / mcheck.c
blob1a80a5657005e22a1aa89b177eaaa48d484ab358
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));
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 #include <string.h>
53 #define flood memset
54 #else
55 static void flood __P ((__ptr_t, int, __malloc_size_t));
56 static void
57 flood (ptr, val, size)
58 __ptr_t ptr;
59 int val;
60 __malloc_size_t size;
62 char *cp = ptr;
63 while (size--)
64 *cp++ = val;
66 #endif
68 static enum mcheck_status checkhdr __P ((const struct hdr *));
69 static enum mcheck_status
70 checkhdr (hdr)
71 const struct hdr *hdr;
73 enum mcheck_status status;
74 switch (hdr->magic)
76 default:
77 status = MCHECK_HEAD;
78 break;
79 case MAGICFREE:
80 status = MCHECK_FREE;
81 break;
82 case MAGICWORD:
83 if (((char *) &hdr[1])[hdr->size] != MAGICBYTE)
84 status = MCHECK_TAIL;
85 else
86 status = MCHECK_OK;
87 break;
89 if (status != MCHECK_OK)
90 (*abortfunc) (status);
91 return status;
94 static void freehook __P ((__ptr_t));
95 static void
96 freehook (ptr)
97 __ptr_t ptr;
99 if (ptr)
101 struct hdr *hdr = ((struct hdr *) ptr) - 1;
102 checkhdr (hdr);
103 hdr->magic = MAGICFREE;
104 flood (ptr, FREEFLOOD, hdr->size);
105 ptr = (__ptr_t) hdr;
107 __free_hook = old_free_hook;
108 free (ptr);
109 __free_hook = freehook;
112 static __ptr_t mallochook __P ((__malloc_size_t));
113 static __ptr_t
114 mallochook (size)
115 __malloc_size_t size;
117 struct hdr *hdr;
119 __malloc_hook = old_malloc_hook;
120 hdr = (struct hdr *) malloc (sizeof (struct hdr) + size + 1);
121 __malloc_hook = mallochook;
122 if (hdr == NULL)
123 return NULL;
125 hdr->size = size;
126 hdr->magic = MAGICWORD;
127 ((char *) &hdr[1])[size] = MAGICBYTE;
128 flood ((__ptr_t) (hdr + 1), MALLOCFLOOD, size);
129 return (__ptr_t) (hdr + 1);
132 static __ptr_t reallochook __P ((__ptr_t, __malloc_size_t));
133 static __ptr_t
134 reallochook (ptr, size)
135 __ptr_t ptr;
136 __malloc_size_t size;
138 struct hdr *hdr;
139 __malloc_size_t osize;
141 if (ptr)
143 hdr = ((struct hdr *) ptr) - 1;
144 osize = hdr->size;
146 checkhdr (hdr);
147 if (size < osize)
148 flood ((char *) ptr + size, FREEFLOOD, osize - size);
150 else
152 osize = 0;
153 hdr = NULL;
155 __free_hook = old_free_hook;
156 __malloc_hook = old_malloc_hook;
157 __realloc_hook = old_realloc_hook;
158 hdr = (struct hdr *) realloc ((__ptr_t) hdr, sizeof (struct hdr) + size + 1);
159 __free_hook = freehook;
160 __malloc_hook = mallochook;
161 __realloc_hook = reallochook;
162 if (hdr == NULL)
163 return NULL;
165 hdr->size = size;
166 hdr->magic = MAGICWORD;
167 ((char *) &hdr[1])[size] = MAGICBYTE;
168 if (size > osize)
169 flood ((char *) (hdr + 1) + osize, MALLOCFLOOD, size - osize);
170 return (__ptr_t) (hdr + 1);
173 static void mabort __P ((enum mcheck_status status));
174 static void
175 mabort (status)
176 enum mcheck_status status;
178 const char *msg;
179 switch (status)
181 case MCHECK_OK:
182 msg = _("memory is consistent, library is buggy");
183 break;
184 case MCHECK_HEAD:
185 msg = _("memory clobbered before allocated block");
186 break;
187 case MCHECK_TAIL:
188 msg = _("memory clobbered past end of allocated block");
189 break;
190 case MCHECK_FREE:
191 msg = _("block freed twice");
192 break;
193 default:
194 msg = _("bogus mcheck_status, library is buggy");
195 break;
197 #ifdef _LIBC
198 __libc_fatal (msg);
199 #else
200 fprintf (stderr, "mcheck: %s\n", msg);
201 fflush (stderr);
202 abort ();
203 #endif
206 static int mcheck_used = 0;
209 mcheck (func)
210 void (*func) __P ((enum mcheck_status));
212 abortfunc = (func != NULL) ? func : &mabort;
214 /* These hooks may not be safely inserted if malloc is already in use. */
215 if (!__malloc_initialized && !mcheck_used)
217 old_free_hook = __free_hook;
218 __free_hook = freehook;
219 old_malloc_hook = __malloc_hook;
220 __malloc_hook = mallochook;
221 old_realloc_hook = __realloc_hook;
222 __realloc_hook = reallochook;
223 mcheck_used = 1;
226 return mcheck_used ? 0 : -1;
229 enum mcheck_status
230 mprobe (__ptr_t ptr)
232 return mcheck_used ? checkhdr (ptr) : MCHECK_DISABLED;