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 static int mcheck_used
;
60 /* Nonzero if pedentic checking of all blocks is requested. */
63 #if defined _LIBC || defined STDC_HEADERS || defined USG
67 static void flood
__P ((__ptr_t
, int, __malloc_size_t
));
69 flood (ptr
, val
, size
)
80 static enum mcheck_status checkhdr
__P ((const struct hdr
*));
81 static enum mcheck_status
83 const struct hdr
*hdr
;
85 enum mcheck_status status
;
88 /* Maybe the mcheck used is disabled? This happens when we find
89 an error and report it. */
92 switch (hdr
->magic
^ ((uintptr_t) hdr
->prev
+ (uintptr_t) hdr
->next
))
101 if (((char *) &hdr
[1])[hdr
->size
] != MAGICBYTE
)
102 status
= MCHECK_TAIL
;
107 if (status
!= MCHECK_OK
)
110 (*abortfunc
) (status
);
119 /* Walk through all the active blocks and test whether they were tempered
121 struct hdr
*runp
= root
;
123 /* Temporarily turn off the checks. */
128 (void) checkhdr (runp
);
133 /* Turn checks on again. */
137 static void unlink_blk
__P ((struct hdr
*ptr
));
142 if (ptr
->next
!= NULL
)
144 ptr
->next
->prev
= ptr
->prev
;
145 ptr
->next
->magic
= MAGICWORD
^ ((uintptr_t) ptr
->next
->prev
146 + (uintptr_t) ptr
->next
->next
);
148 if (ptr
->prev
!= NULL
)
150 ptr
->prev
->next
= ptr
->next
;
151 ptr
->prev
->magic
= MAGICWORD
^ ((uintptr_t) ptr
->prev
->prev
152 + (uintptr_t) ptr
->prev
->next
);
158 static void link_blk
__P ((struct hdr
*ptr
));
166 hdr
->magic
= MAGICWORD
^ (uintptr_t) hdr
->next
;
168 /* And the next block. */
169 if (hdr
->next
!= NULL
)
171 hdr
->next
->prev
= hdr
;
172 hdr
->next
->magic
= MAGICWORD
^ ((uintptr_t) hdr
173 + (uintptr_t) hdr
->next
->next
);
177 static void freehook
__P ((__ptr_t
, const __ptr_t
));
179 freehook (ptr
, caller
)
181 const __ptr_t caller
;
187 struct hdr
*hdr
= ((struct hdr
*) ptr
) - 1;
189 hdr
->magic
= MAGICFREE
;
191 hdr
->prev
= hdr
->next
= NULL
;
192 flood (ptr
, FREEFLOOD
, hdr
->size
);
195 __free_hook
= old_free_hook
;
196 if (old_free_hook
!= NULL
)
197 (*old_free_hook
) (ptr
, caller
);
200 __free_hook
= freehook
;
203 static __ptr_t mallochook
__P ((__malloc_size_t
, const __ptr_t
));
205 mallochook (size
, caller
)
206 __malloc_size_t size
;
207 const __ptr_t caller
;
214 __malloc_hook
= old_malloc_hook
;
215 if (old_malloc_hook
!= NULL
)
216 hdr
= (struct hdr
*) (*old_malloc_hook
) (sizeof (struct hdr
) + size
+ 1,
219 hdr
= (struct hdr
*) malloc (sizeof (struct hdr
) + size
+ 1);
220 __malloc_hook
= mallochook
;
226 ((char *) &hdr
[1])[size
] = MAGICBYTE
;
227 flood ((__ptr_t
) (hdr
+ 1), MALLOCFLOOD
, size
);
228 return (__ptr_t
) (hdr
+ 1);
231 static __ptr_t reallochook
__P ((__ptr_t
, __malloc_size_t
, const __ptr_t
));
233 reallochook (ptr
, size
, caller
)
235 __malloc_size_t size
;
236 const __ptr_t caller
;
239 __malloc_size_t osize
;
246 hdr
= ((struct hdr
*) ptr
) - 1;
252 flood ((char *) ptr
+ size
, FREEFLOOD
, osize
- size
);
259 __free_hook
= old_free_hook
;
260 __malloc_hook
= old_malloc_hook
;
261 __realloc_hook
= old_realloc_hook
;
262 if (old_realloc_hook
!= NULL
)
263 hdr
= (struct hdr
*) (*old_realloc_hook
) ((__ptr_t
) hdr
,
264 sizeof (struct hdr
) + size
+ 1,
267 hdr
= (struct hdr
*) realloc ((__ptr_t
) hdr
,
268 sizeof (struct hdr
) + size
+ 1);
269 __free_hook
= freehook
;
270 __malloc_hook
= mallochook
;
271 __realloc_hook
= reallochook
;
277 ((char *) &hdr
[1])[size
] = MAGICBYTE
;
279 flood ((char *) (hdr
+ 1) + osize
, MALLOCFLOOD
, size
- osize
);
280 return (__ptr_t
) (hdr
+ 1);
283 static void mabort
__P ((enum mcheck_status status
))
284 __attribute__ ((noreturn
));
287 enum mcheck_status status
;
293 msg
= _("memory is consistent, library is buggy\n");
296 msg
= _("memory clobbered before allocated block\n");
299 msg
= _("memory clobbered past end of allocated block\n");
302 msg
= _("block freed twice\n");
305 msg
= _("bogus mcheck_status, library is buggy\n");
311 fprintf (stderr
, "mcheck: %s", msg
);
319 void (*func
) __P ((enum mcheck_status
));
321 abortfunc
= (func
!= NULL
) ? func
: &mabort
;
323 /* These hooks may not be safely inserted if malloc is already in use. */
324 if (__malloc_initialized
<= 0 && !mcheck_used
)
326 /* We call malloc() once here to ensure it is initialized. */
327 void *p
= malloc (0);
330 old_free_hook
= __free_hook
;
331 __free_hook
= freehook
;
332 old_malloc_hook
= __malloc_hook
;
333 __malloc_hook
= mallochook
;
334 old_realloc_hook
= __realloc_hook
;
335 __realloc_hook
= reallochook
;
339 return mcheck_used
? 0 : -1;
343 mcheck_pedantic (func
)
344 void (*func
) __P ((enum mcheck_status
));
346 int res
= mcheck (func
);
355 return mcheck_used
? checkhdr (((struct hdr
*) ptr
) - 1) : MCHECK_DISABLED
;