]>
Commit | Line | Data |
---|---|---|
6d52618b | 1 | /* Standard debugging hooks for `malloc'. |
df4ef2ab | 2 | Copyright (C) 1990,91,92,93,94,95,96,97 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 | |
24 | #define _MALLOC_INTERNAL | |
25 | #include <malloc.h> | |
26 | #include <mcheck.h> | |
27 | #include <stdio.h> | |
28 | #endif | |
29 | ||
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)); | |
34 | ||
35 | /* Function to call when something awful happens. */ | |
36 | static void (*abortfunc) __P ((enum mcheck_status)); | |
37 | ||
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) | |
44 | ||
45 | struct hdr | |
46 | { | |
47 | __malloc_size_t size; /* Exact size requested by user. */ | |
48 | unsigned long int magic; /* Magic number to check header integrity. */ | |
49 | }; | |
50 | ||
51 | #if defined(_LIBC) || defined(STDC_HEADERS) || defined(USG) | |
df4ef2ab | 52 | #include <string.h> |
6d52618b UD |
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; | |
61 | { | |
62 | char *cp = ptr; | |
63 | while (size--) | |
64 | *cp++ = val; | |
65 | } | |
66 | #endif | |
67 | ||
68 | static enum mcheck_status checkhdr __P ((const struct hdr *)); | |
69 | static enum mcheck_status | |
70 | checkhdr (hdr) | |
71 | const struct hdr *hdr; | |
72 | { | |
73 | enum mcheck_status status; | |
74 | switch (hdr->magic) | |
75 | { | |
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; | |
88 | } | |
89 | if (status != MCHECK_OK) | |
90 | (*abortfunc) (status); | |
91 | return status; | |
92 | } | |
93 | ||
94 | static void freehook __P ((__ptr_t)); | |
95 | static void | |
96 | freehook (ptr) | |
97 | __ptr_t ptr; | |
98 | { | |
99 | if (ptr) | |
100 | { | |
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; | |
106 | } | |
107 | __free_hook = old_free_hook; | |
108 | free (ptr); | |
109 | __free_hook = freehook; | |
110 | } | |
111 | ||
112 | static __ptr_t mallochook __P ((__malloc_size_t)); | |
113 | static __ptr_t | |
114 | mallochook (size) | |
115 | __malloc_size_t size; | |
116 | { | |
117 | struct hdr *hdr; | |
118 | ||
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; | |
124 | ||
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); | |
130 | } | |
131 | ||
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; | |
137 | { | |
138 | struct hdr *hdr; | |
139 | __malloc_size_t osize; | |
140 | ||
141 | if (ptr) | |
142 | { | |
143 | hdr = ((struct hdr *) ptr) - 1; | |
144 | osize = hdr->size; | |
145 | ||
146 | checkhdr (hdr); | |
147 | if (size < osize) | |
148 | flood ((char *) ptr + size, FREEFLOOD, osize - size); | |
149 | } | |
150 | else | |
151 | { | |
152 | osize = 0; | |
153 | hdr = NULL; | |
154 | } | |
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; | |
164 | ||
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); | |
171 | } | |
172 | ||
173 | static void mabort __P ((enum mcheck_status status)); | |
174 | static void | |
175 | mabort (status) | |
176 | enum mcheck_status status; | |
177 | { | |
178 | const char *msg; | |
179 | switch (status) | |
180 | { | |
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; | |
196 | } | |
197 | #ifdef _LIBC | |
198 | __libc_fatal (msg); | |
199 | #else | |
200 | fprintf (stderr, "mcheck: %s\n", msg); | |
201 | fflush (stderr); | |
202 | abort (); | |
203 | #endif | |
204 | } | |
205 | ||
206 | static int mcheck_used = 0; | |
207 | ||
208 | int | |
209 | mcheck (func) | |
210 | void (*func) __P ((enum mcheck_status)); | |
211 | { | |
212 | abortfunc = (func != NULL) ? func : &mabort; | |
213 | ||
214 | /* These hooks may not be safely inserted if malloc is already in use. */ | |
215 | if (!__malloc_initialized && !mcheck_used) | |
216 | { | |
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; | |
224 | } | |
225 | ||
226 | return mcheck_used ? 0 : -1; | |
227 | } | |
228 | ||
229 | enum mcheck_status | |
230 | mprobe (__ptr_t ptr) | |
231 | { | |
232 | return mcheck_used ? checkhdr (ptr) : MCHECK_DISABLED; | |
233 | } |