]>
Commit | Line | Data |
---|---|---|
6d52618b | 1 | /* Standard debugging hooks for `malloc'. |
c4563d2d | 2 | Copyright (C) 1990,91,92,93,94,95,96,97,99 Free Software Foundation, Inc. |
6d52618b UD |
3 | Written May 1989 by Mike Haertel. |
4 | ||
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. | |
9 | ||
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. | |
14 | ||
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. | |
19 | ||
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. */ | |
22 | ||
23 | #ifndef _MALLOC_INTERNAL | |
9756dfe1 UD |
24 | # define _MALLOC_INTERNAL |
25 | # include <malloc.h> | |
26 | # include <mcheck.h> | |
27 | # include <stdio.h> | |
6d52618b UD |
28 | #endif |
29 | ||
30 | /* Old hook values. */ | |
a2b08ee5 UD |
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)); | |
6d52618b UD |
35 | |
36 | /* Function to call when something awful happens. */ | |
37 | static void (*abortfunc) __P ((enum mcheck_status)); | |
38 | ||
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) | |
45 | ||
46 | struct hdr | |
47 | { | |
48 | __malloc_size_t size; /* Exact size requested by user. */ | |
49 | unsigned long int magic; /* Magic number to check header integrity. */ | |
50 | }; | |
51 | ||
9756dfe1 UD |
52 | #if defined _LIBC || defined STDC_HEADERS || defined USG |
53 | # include <string.h> | |
54 | # define flood memset | |
6d52618b UD |
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; | |
62 | { | |
63 | char *cp = ptr; | |
64 | while (size--) | |
65 | *cp++ = val; | |
66 | } | |
67 | #endif | |
68 | ||
69 | static enum mcheck_status checkhdr __P ((const struct hdr *)); | |
70 | static enum mcheck_status | |
71 | checkhdr (hdr) | |
72 | const struct hdr *hdr; | |
73 | { | |
74 | enum mcheck_status status; | |
75 | switch (hdr->magic) | |
76 | { | |
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; | |
89 | } | |
90 | if (status != MCHECK_OK) | |
91 | (*abortfunc) (status); | |
92 | return status; | |
93 | } | |
94 | ||
a2b08ee5 | 95 | static void freehook __P ((__ptr_t, const __ptr_t)); |
6d52618b | 96 | static void |
a2b08ee5 | 97 | freehook (ptr, caller) |
6d52618b | 98 | __ptr_t ptr; |
a2b08ee5 | 99 | const __ptr_t caller; |
6d52618b UD |
100 | { |
101 | if (ptr) | |
102 | { | |
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; | |
108 | } | |
109 | __free_hook = old_free_hook; | |
a2b08ee5 UD |
110 | if (old_free_hook != NULL) |
111 | (*old_free_hook) (ptr, caller); | |
112 | else | |
113 | free (ptr); | |
6d52618b UD |
114 | __free_hook = freehook; |
115 | } | |
116 | ||
a2b08ee5 | 117 | static __ptr_t mallochook __P ((__malloc_size_t, const __ptr_t)); |
6d52618b | 118 | static __ptr_t |
a2b08ee5 | 119 | mallochook (size, caller) |
6d52618b | 120 | __malloc_size_t size; |
a2b08ee5 | 121 | const __ptr_t caller; |
6d52618b UD |
122 | { |
123 | struct hdr *hdr; | |
124 | ||
125 | __malloc_hook = old_malloc_hook; | |
a2b08ee5 UD |
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); | |
6d52618b UD |
131 | __malloc_hook = mallochook; |
132 | if (hdr == NULL) | |
133 | return NULL; | |
134 | ||
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); | |
140 | } | |
141 | ||
a2b08ee5 | 142 | static __ptr_t reallochook __P ((__ptr_t, __malloc_size_t, const __ptr_t)); |
6d52618b | 143 | static __ptr_t |
a2b08ee5 | 144 | reallochook (ptr, size, caller) |
6d52618b UD |
145 | __ptr_t ptr; |
146 | __malloc_size_t size; | |
a2b08ee5 | 147 | const __ptr_t caller; |
6d52618b UD |
148 | { |
149 | struct hdr *hdr; | |
150 | __malloc_size_t osize; | |
151 | ||
152 | if (ptr) | |
153 | { | |
154 | hdr = ((struct hdr *) ptr) - 1; | |
155 | osize = hdr->size; | |
156 | ||
157 | checkhdr (hdr); | |
158 | if (size < osize) | |
159 | flood ((char *) ptr + size, FREEFLOOD, osize - size); | |
160 | } | |
161 | else | |
162 | { | |
163 | osize = 0; | |
164 | hdr = NULL; | |
165 | } | |
166 | __free_hook = old_free_hook; | |
167 | __malloc_hook = old_malloc_hook; | |
168 | __realloc_hook = old_realloc_hook; | |
a2b08ee5 UD |
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); | |
6d52618b UD |
176 | __free_hook = freehook; |
177 | __malloc_hook = mallochook; | |
178 | __realloc_hook = reallochook; | |
179 | if (hdr == NULL) | |
180 | return NULL; | |
181 | ||
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); | |
188 | } | |
189 | ||
190 | static void mabort __P ((enum mcheck_status status)); | |
191 | static void | |
192 | mabort (status) | |
193 | enum mcheck_status status; | |
194 | { | |
195 | const char *msg; | |
196 | switch (status) | |
197 | { | |
198 | case MCHECK_OK: | |
9756dfe1 | 199 | msg = _("memory is consistent, library is buggy\n"); |
6d52618b UD |
200 | break; |
201 | case MCHECK_HEAD: | |
9756dfe1 | 202 | msg = _("memory clobbered before allocated block\n"); |
6d52618b UD |
203 | break; |
204 | case MCHECK_TAIL: | |
9756dfe1 | 205 | msg = _("memory clobbered past end of allocated block\n"); |
6d52618b UD |
206 | break; |
207 | case MCHECK_FREE: | |
9756dfe1 | 208 | msg = _("block freed twice\n"); |
6d52618b UD |
209 | break; |
210 | default: | |
9756dfe1 | 211 | msg = _("bogus mcheck_status, library is buggy\n"); |
6d52618b UD |
212 | break; |
213 | } | |
214 | #ifdef _LIBC | |
215 | __libc_fatal (msg); | |
216 | #else | |
9756dfe1 | 217 | fprintf (stderr, "mcheck: %s", msg); |
6d52618b UD |
218 | fflush (stderr); |
219 | abort (); | |
220 | #endif | |
221 | } | |
222 | ||
c4563d2d | 223 | static int mcheck_used; |
6d52618b UD |
224 | |
225 | int | |
226 | mcheck (func) | |
227 | void (*func) __P ((enum mcheck_status)); | |
228 | { | |
229 | abortfunc = (func != NULL) ? func : &mabort; | |
230 | ||
231 | /* These hooks may not be safely inserted if malloc is already in use. */ | |
9756dfe1 | 232 | if (__malloc_initialized <= 0 && !mcheck_used) |
6d52618b UD |
233 | { |
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; | |
241 | } | |
242 | ||
243 | return mcheck_used ? 0 : -1; | |
244 | } | |
245 | ||
246 | enum mcheck_status | |
247 | mprobe (__ptr_t ptr) | |
248 | { | |
997a4165 | 249 | return mcheck_used ? checkhdr (((struct hdr *) ptr) - 1) : MCHECK_DISABLED; |
6d52618b | 250 | } |