1 /* Standard debugging hooks for `malloc'.
2 Copyright (C) 1990-1997, 1999, 2000, 2001 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written May 1989 by Mike Haertel.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 #ifndef _MALLOC_INTERNAL
22 # 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. */
54 /* This is the beginning of the list of all memory blocks allocated.
55 It is only constructed if the pedantic testing is requested. */
56 static struct hdr
*root
;
58 /* Nonzero if pedentic checking of all blocks is requested. */
61 #if defined _LIBC || defined STDC_HEADERS || defined USG
65 static void flood
__P ((__ptr_t
, int, __malloc_size_t
));
67 flood (ptr
, val
, size
)
78 static enum mcheck_status checkhdr
__P ((const struct hdr
*));
79 static enum mcheck_status
81 const struct hdr
*hdr
;
83 enum mcheck_status status
;
84 switch (hdr
->magic
^ ((uintptr_t) hdr
->prev
+ (uintptr_t) hdr
->next
))
93 if (((char *) &hdr
[1])[hdr
->size
] != MAGICBYTE
)
99 if (status
!= MCHECK_OK
)
100 (*abortfunc
) (status
);
107 /* Walk through all the active blocks and test whether they were tempered
109 struct hdr
*runp
= root
;
111 /* Temporarily turn off the checks. */
116 (void) checkhdr (runp
);
121 /* Turn checks on again. */
125 static void unlink_blk
__P ((struct hdr
*ptr
));
130 if (ptr
->next
!= NULL
)
132 ptr
->next
->prev
= ptr
->prev
;
133 ptr
->next
->magic
= MAGICWORD
^ ((uintptr_t) ptr
->next
->prev
134 + (uintptr_t) ptr
->next
->next
);
136 if (ptr
->prev
!= NULL
)
138 ptr
->prev
->next
= ptr
->next
;
139 ptr
->prev
->magic
= MAGICWORD
^ ((uintptr_t) ptr
->prev
->prev
140 + (uintptr_t) ptr
->prev
->next
);
146 static void link_blk
__P ((struct hdr
*ptr
));
154 hdr
->magic
= MAGICWORD
^ (uintptr_t) hdr
->next
;
156 /* And the next block. */
157 if (hdr
->next
!= NULL
)
159 hdr
->next
->prev
= hdr
;
160 hdr
->next
->magic
= MAGICWORD
^ ((uintptr_t) hdr
161 + (uintptr_t) hdr
->next
->next
);
165 static void freehook
__P ((__ptr_t
, const __ptr_t
));
167 freehook (ptr
, caller
)
169 const __ptr_t caller
;
175 struct hdr
*hdr
= ((struct hdr
*) ptr
) - 1;
177 hdr
->magic
= MAGICFREE
;
179 hdr
->prev
= hdr
->next
= NULL
;
180 flood (ptr
, FREEFLOOD
, hdr
->size
);
183 __free_hook
= old_free_hook
;
184 if (old_free_hook
!= NULL
)
185 (*old_free_hook
) (ptr
, caller
);
188 __free_hook
= freehook
;
191 static __ptr_t mallochook
__P ((__malloc_size_t
, const __ptr_t
));
193 mallochook (size
, caller
)
194 __malloc_size_t size
;
195 const __ptr_t caller
;
202 __malloc_hook
= old_malloc_hook
;
203 if (old_malloc_hook
!= NULL
)
204 hdr
= (struct hdr
*) (*old_malloc_hook
) (sizeof (struct hdr
) + size
+ 1,
207 hdr
= (struct hdr
*) malloc (sizeof (struct hdr
) + size
+ 1);
208 __malloc_hook
= mallochook
;
214 ((char *) &hdr
[1])[size
] = MAGICBYTE
;
215 flood ((__ptr_t
) (hdr
+ 1), MALLOCFLOOD
, size
);
216 return (__ptr_t
) (hdr
+ 1);
219 static __ptr_t reallochook
__P ((__ptr_t
, __malloc_size_t
, const __ptr_t
));
221 reallochook (ptr
, size
, caller
)
223 __malloc_size_t size
;
224 const __ptr_t caller
;
227 __malloc_size_t osize
;
234 hdr
= ((struct hdr
*) ptr
) - 1;
240 flood ((char *) ptr
+ size
, FREEFLOOD
, osize
- size
);
247 __free_hook
= old_free_hook
;
248 __malloc_hook
= old_malloc_hook
;
249 __realloc_hook
= old_realloc_hook
;
250 if (old_realloc_hook
!= NULL
)
251 hdr
= (struct hdr
*) (*old_realloc_hook
) ((__ptr_t
) hdr
,
252 sizeof (struct hdr
) + size
+ 1,
255 hdr
= (struct hdr
*) realloc ((__ptr_t
) hdr
,
256 sizeof (struct hdr
) + size
+ 1);
257 __free_hook
= freehook
;
258 __malloc_hook
= mallochook
;
259 __realloc_hook
= reallochook
;
265 ((char *) &hdr
[1])[size
] = MAGICBYTE
;
267 flood ((char *) (hdr
+ 1) + osize
, MALLOCFLOOD
, size
- osize
);
268 return (__ptr_t
) (hdr
+ 1);
271 static void mabort
__P ((enum mcheck_status status
))
272 __attribute__ ((noreturn
));
275 enum mcheck_status status
;
281 msg
= _("memory is consistent, library is buggy\n");
284 msg
= _("memory clobbered before allocated block\n");
287 msg
= _("memory clobbered past end of allocated block\n");
290 msg
= _("block freed twice\n");
293 msg
= _("bogus mcheck_status, library is buggy\n");
299 fprintf (stderr
, "mcheck: %s", msg
);
305 static int mcheck_used
;
309 void (*func
) __P ((enum mcheck_status
));
311 abortfunc
= (func
!= NULL
) ? func
: &mabort
;
313 /* These hooks may not be safely inserted if malloc is already in use. */
314 if (__malloc_initialized
<= 0 && !mcheck_used
)
316 /* We call malloc() once here to ensure it is initialized. */
317 void *p
= malloc (0);
320 old_free_hook
= __free_hook
;
321 __free_hook
= freehook
;
322 old_malloc_hook
= __malloc_hook
;
323 __malloc_hook
= mallochook
;
324 old_realloc_hook
= __realloc_hook
;
325 __realloc_hook
= reallochook
;
329 return mcheck_used
? 0 : -1;
333 mcheck_pedantic (func
)
334 void (*func
) __P ((enum mcheck_status
));
336 int res
= mcheck (func
);
345 return mcheck_used
? checkhdr (((struct hdr
*) ptr
) - 1) : MCHECK_DISABLED
;