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
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
,
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)
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
56 static void flood
__P ((__ptr_t
, int, __malloc_size_t
));
58 flood (ptr
, val
, size
)
69 static enum mcheck_status checkhdr
__P ((const struct hdr
*));
70 static enum mcheck_status
72 const struct hdr
*hdr
;
74 enum mcheck_status status
;
84 if (((char *) &hdr
[1])[hdr
->size
] != MAGICBYTE
)
90 if (status
!= MCHECK_OK
)
91 (*abortfunc
) (status
);
95 static void freehook
__P ((__ptr_t
, const __ptr_t
));
97 freehook (ptr
, caller
)
103 struct hdr
*hdr
= ((struct hdr
*) ptr
) - 1;
105 hdr
->magic
= MAGICFREE
;
106 flood (ptr
, FREEFLOOD
, hdr
->size
);
109 __free_hook
= old_free_hook
;
110 if (old_free_hook
!= NULL
)
111 (*old_free_hook
) (ptr
, caller
);
114 __free_hook
= freehook
;
117 static __ptr_t mallochook
__P ((__malloc_size_t
, const __ptr_t
));
119 mallochook (size
, caller
)
120 __malloc_size_t size
;
121 const __ptr_t caller
;
125 __malloc_hook
= old_malloc_hook
;
126 if (old_malloc_hook
!= NULL
)
127 hdr
= (struct hdr
*) (*old_malloc_hook
) (sizeof (struct hdr
) + size
+ 1,
130 hdr
= (struct hdr
*) malloc (sizeof (struct hdr
) + size
+ 1);
131 __malloc_hook
= mallochook
;
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
));
144 reallochook (ptr
, size
, caller
)
146 __malloc_size_t size
;
147 const __ptr_t caller
;
150 __malloc_size_t osize
;
154 hdr
= ((struct hdr
*) ptr
) - 1;
159 flood ((char *) ptr
+ size
, FREEFLOOD
, osize
- size
);
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,
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
;
183 hdr
->magic
= MAGICWORD
;
184 ((char *) &hdr
[1])[size
] = MAGICBYTE
;
186 flood ((char *) (hdr
+ 1) + osize
, MALLOCFLOOD
, size
- osize
);
187 return (__ptr_t
) (hdr
+ 1);
190 static void mabort
__P ((enum mcheck_status status
));
193 enum mcheck_status status
;
199 msg
= _("memory is consistent, library is buggy\n");
202 msg
= _("memory clobbered before allocated block\n");
205 msg
= _("memory clobbered past end of allocated block\n");
208 msg
= _("block freed twice\n");
211 msg
= _("bogus mcheck_status, library is buggy\n");
217 fprintf (stderr
, "mcheck: %s", msg
);
223 static int mcheck_used
= 0;
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
;
243 return mcheck_used
? 0 : -1;
249 return mcheck_used
? checkhdr (((struct hdr
*) ptr
) - 1) : MCHECK_DISABLED
;