1 /* Standard debugging hooks for `malloc'.
2 Copyright (C) 1990-1997,1999,2000-2002,2007,2010
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Written May 1989 by Mike Haertel.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 #ifndef _MALLOC_INTERNAL
23 # define _MALLOC_INTERNAL
32 /* Old hook values. */
33 static void (*old_free_hook
) (__ptr_t ptr
, __const __ptr_t
);
34 static __ptr_t (*old_malloc_hook
) (__malloc_size_t size
, const __ptr_t
);
35 static __ptr_t (*old_memalign_hook
) (__malloc_size_t alignment
,
38 static __ptr_t (*old_realloc_hook
) (__ptr_t ptr
, __malloc_size_t size
,
41 /* Function to call when something awful happens. */
42 static void (*abortfunc
) (enum mcheck_status
);
44 /* Arbitrary magical numbers. */
45 #define MAGICWORD 0xfedabeeb
46 #define MAGICFREE 0xd8675309
47 #define MAGICBYTE ((char) 0xd7)
48 #define MALLOCFLOOD ((char) 0x93)
49 #define FREEFLOOD ((char) 0x95)
53 __malloc_size_t size
; /* Exact size requested by user. */
54 unsigned long int magic
; /* Magic number to check header integrity. */
57 __ptr_t block
; /* Real block allocated, for memalign. */
58 unsigned long int magic2
; /* Extra, keeps us doubleword aligned. */
61 /* This is the beginning of the list of all memory blocks allocated.
62 It is only constructed if the pedantic testing is requested. */
63 static struct hdr
*root
;
65 static int mcheck_used
;
67 /* Nonzero if pedentic checking of all blocks is requested. */
70 #if defined _LIBC || defined STDC_HEADERS || defined USG
74 static void flood (__ptr_t
, int, __malloc_size_t
);
76 flood (ptr
, val
, size
)
87 static enum mcheck_status
88 checkhdr (const struct hdr
*hdr
)
90 enum mcheck_status status
;
93 /* Maybe the mcheck used is disabled? This happens when we find
94 an error and report it. */
97 switch (hdr
->magic
^ ((uintptr_t) hdr
->prev
+ (uintptr_t) hdr
->next
))
100 status
= MCHECK_HEAD
;
103 status
= MCHECK_FREE
;
106 if (((char *) &hdr
[1])[hdr
->size
] != MAGICBYTE
)
107 status
= MCHECK_TAIL
;
108 else if ((hdr
->magic2
^ (uintptr_t) hdr
->block
) != MAGICWORD
)
109 status
= MCHECK_HEAD
;
114 if (status
!= MCHECK_OK
)
117 (*abortfunc
) (status
);
124 mcheck_check_all (void)
126 /* Walk through all the active blocks and test whether they were tempered
128 struct hdr
*runp
= root
;
130 /* Temporarily turn off the checks. */
135 (void) checkhdr (runp
);
140 /* Turn checks on again. */
144 libc_hidden_def (mcheck_check_all
)
148 unlink_blk (struct hdr
*ptr
)
150 if (ptr
->next
!= NULL
)
152 ptr
->next
->prev
= ptr
->prev
;
153 ptr
->next
->magic
= MAGICWORD
^ ((uintptr_t) ptr
->next
->prev
154 + (uintptr_t) ptr
->next
->next
);
156 if (ptr
->prev
!= NULL
)
158 ptr
->prev
->next
= ptr
->next
;
159 ptr
->prev
->magic
= MAGICWORD
^ ((uintptr_t) ptr
->prev
->prev
160 + (uintptr_t) ptr
->prev
->next
);
167 link_blk (struct hdr
*hdr
)
172 hdr
->magic
= MAGICWORD
^ (uintptr_t) hdr
->next
;
174 /* And the next block. */
175 if (hdr
->next
!= NULL
)
177 hdr
->next
->prev
= hdr
;
178 hdr
->next
->magic
= MAGICWORD
^ ((uintptr_t) hdr
179 + (uintptr_t) hdr
->next
->next
);
183 freehook (__ptr_t ptr
, const __ptr_t caller
)
189 struct hdr
*hdr
= ((struct hdr
*) ptr
) - 1;
191 hdr
->magic
= MAGICFREE
;
192 hdr
->magic2
= MAGICFREE
;
194 hdr
->prev
= hdr
->next
= NULL
;
195 flood (ptr
, FREEFLOOD
, hdr
->size
);
198 __free_hook
= old_free_hook
;
199 if (old_free_hook
!= NULL
)
200 (*old_free_hook
) (ptr
, caller
);
203 __free_hook
= freehook
;
207 mallochook (__malloc_size_t size
, const __ptr_t caller
)
214 if (size
> ~((size_t) 0) - (sizeof (struct hdr
) + 1))
216 __set_errno (ENOMEM
);
220 __malloc_hook
= old_malloc_hook
;
221 if (old_malloc_hook
!= NULL
)
222 hdr
= (struct hdr
*) (*old_malloc_hook
) (sizeof (struct hdr
) + size
+ 1,
225 hdr
= (struct hdr
*) malloc (sizeof (struct hdr
) + size
+ 1);
226 __malloc_hook
= mallochook
;
233 hdr
->magic2
= (uintptr_t) hdr
^ MAGICWORD
;
234 ((char *) &hdr
[1])[size
] = MAGICBYTE
;
235 flood ((__ptr_t
) (hdr
+ 1), MALLOCFLOOD
, size
);
236 return (__ptr_t
) (hdr
+ 1);
240 memalignhook (__malloc_size_t alignment
, __malloc_size_t size
,
241 const __ptr_t caller
)
244 __malloc_size_t slop
;
250 slop
= (sizeof *hdr
+ alignment
- 1) & -alignment
;
252 if (size
> ~((size_t) 0) - (slop
+ 1))
254 __set_errno (ENOMEM
);
258 __memalign_hook
= old_memalign_hook
;
259 if (old_memalign_hook
!= NULL
)
260 block
= (*old_memalign_hook
) (alignment
, slop
+ size
+ 1, caller
);
262 block
= memalign (alignment
, slop
+ size
+ 1);
263 __memalign_hook
= memalignhook
;
267 hdr
= ((struct hdr
*) (block
+ slop
)) - 1;
271 hdr
->block
= (__ptr_t
) block
;
272 hdr
->magic2
= (uintptr_t) block
^ MAGICWORD
;
273 ((char *) &hdr
[1])[size
] = MAGICBYTE
;
274 flood ((__ptr_t
) (hdr
+ 1), MALLOCFLOOD
, size
);
275 return (__ptr_t
) (hdr
+ 1);
279 reallochook (__ptr_t ptr
, __malloc_size_t size
, const __ptr_t caller
)
283 freehook (ptr
, caller
);
288 __malloc_size_t osize
;
293 if (size
> ~((size_t) 0) - (sizeof (struct hdr
) + 1))
295 __set_errno (ENOMEM
);
301 hdr
= ((struct hdr
*) ptr
) - 1;
307 flood ((char *) ptr
+ size
, FREEFLOOD
, osize
- size
);
314 __free_hook
= old_free_hook
;
315 __malloc_hook
= old_malloc_hook
;
316 __memalign_hook
= old_memalign_hook
;
317 __realloc_hook
= old_realloc_hook
;
318 if (old_realloc_hook
!= NULL
)
319 hdr
= (struct hdr
*) (*old_realloc_hook
) ((__ptr_t
) hdr
,
320 sizeof (struct hdr
) + size
+ 1,
323 hdr
= (struct hdr
*) realloc ((__ptr_t
) hdr
,
324 sizeof (struct hdr
) + size
+ 1);
325 __free_hook
= freehook
;
326 __malloc_hook
= mallochook
;
327 __memalign_hook
= memalignhook
;
328 __realloc_hook
= reallochook
;
335 hdr
->magic2
= (uintptr_t) hdr
^ MAGICWORD
;
336 ((char *) &hdr
[1])[size
] = MAGICBYTE
;
338 flood ((char *) (hdr
+ 1) + osize
, MALLOCFLOOD
, size
- osize
);
339 return (__ptr_t
) (hdr
+ 1);
342 __attribute__ ((noreturn
))
344 mabort (enum mcheck_status status
)
350 msg
= _("memory is consistent, library is buggy\n");
353 msg
= _("memory clobbered before allocated block\n");
356 msg
= _("memory clobbered past end of allocated block\n");
359 msg
= _("block freed twice\n");
362 msg
= _("bogus mcheck_status, library is buggy\n");
368 fprintf (stderr
, "mcheck: %s", msg
);
376 void (*func
) (enum mcheck_status
);
378 abortfunc
= (func
!= NULL
) ? func
: &mabort
;
380 /* These hooks may not be safely inserted if malloc is already in use. */
381 if (__malloc_initialized
<= 0 && !mcheck_used
)
383 /* We call malloc() once here to ensure it is initialized. */
384 void *p
= malloc (0);
387 old_free_hook
= __free_hook
;
388 __free_hook
= freehook
;
389 old_malloc_hook
= __malloc_hook
;
390 __malloc_hook
= mallochook
;
391 old_memalign_hook
= __memalign_hook
;
392 __memalign_hook
= memalignhook
;
393 old_realloc_hook
= __realloc_hook
;
394 __realloc_hook
= reallochook
;
398 return mcheck_used
? 0 : -1;
401 libc_hidden_def (mcheck
)
405 mcheck_pedantic (func
)
406 void (*func
) (enum mcheck_status
);
408 int res
= mcheck (func
);
417 return mcheck_used
? checkhdr (((struct hdr
*) ptr
) - 1) : MCHECK_DISABLED
;