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
));
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)
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)
55 static void flood
__P ((__ptr_t
, int, __malloc_size_t
));
57 flood (ptr
, val
, size
)
68 static enum mcheck_status checkhdr
__P ((const struct hdr
*));
69 static enum mcheck_status
71 const struct hdr
*hdr
;
73 enum mcheck_status status
;
83 if (((char *) &hdr
[1])[hdr
->size
] != MAGICBYTE
)
89 if (status
!= MCHECK_OK
)
90 (*abortfunc
) (status
);
94 static void freehook
__P ((__ptr_t
));
101 struct hdr
*hdr
= ((struct hdr
*) ptr
) - 1;
103 hdr
->magic
= MAGICFREE
;
104 flood (ptr
, FREEFLOOD
, hdr
->size
);
107 __free_hook
= old_free_hook
;
109 __free_hook
= freehook
;
112 static __ptr_t mallochook
__P ((__malloc_size_t
));
115 __malloc_size_t size
;
119 __malloc_hook
= old_malloc_hook
;
120 hdr
= (struct hdr
*) malloc (sizeof (struct hdr
) + size
+ 1);
121 __malloc_hook
= mallochook
;
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
));
134 reallochook (ptr
, size
)
136 __malloc_size_t size
;
139 __malloc_size_t osize
;
143 hdr
= ((struct hdr
*) ptr
) - 1;
148 flood ((char *) ptr
+ size
, FREEFLOOD
, osize
- size
);
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
;
166 hdr
->magic
= MAGICWORD
;
167 ((char *) &hdr
[1])[size
] = MAGICBYTE
;
169 flood ((char *) (hdr
+ 1) + osize
, MALLOCFLOOD
, size
- osize
);
170 return (__ptr_t
) (hdr
+ 1);
173 static void mabort
__P ((enum mcheck_status status
));
176 enum mcheck_status status
;
182 msg
= _("memory is consistent, library is buggy");
185 msg
= _("memory clobbered before allocated block");
188 msg
= _("memory clobbered past end of allocated block");
191 msg
= _("block freed twice");
194 msg
= _("bogus mcheck_status, library is buggy");
200 fprintf (stderr
, "mcheck: %s\n", msg
);
206 static int mcheck_used
= 0;
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
;
226 return mcheck_used
? 0 : -1;
232 return mcheck_used
? checkhdr (ptr
) : MCHECK_DISABLED
;