1 /* Standard debugging hooks for `malloc'.
2 Copyright (C) 1990-1997,1999,2000-2002,2007,2010,2012
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, see
19 <http://www.gnu.org/licenses/>. */
21 #ifndef _MALLOC_INTERNAL
22 # define _MALLOC_INTERNAL
31 /* Old hook values. */
32 static void (*old_free_hook
) (__ptr_t ptr
, const __ptr_t
);
33 static __ptr_t (*old_malloc_hook
) (__malloc_size_t size
, const __ptr_t
);
34 static __ptr_t (*old_memalign_hook
) (__malloc_size_t alignment
,
37 static __ptr_t (*old_realloc_hook
) (__ptr_t ptr
, __malloc_size_t size
,
40 /* Function to call when something awful happens. */
41 static void (*abortfunc
) (enum mcheck_status
);
43 /* Arbitrary magical numbers. */
44 #define MAGICWORD 0xfedabeeb
45 #define MAGICFREE 0xd8675309
46 #define MAGICBYTE ((char) 0xd7)
47 #define MALLOCFLOOD ((char) 0x93)
48 #define FREEFLOOD ((char) 0x95)
52 __malloc_size_t size
; /* Exact size requested by user. */
53 unsigned long int magic
; /* Magic number to check header integrity. */
56 __ptr_t block
; /* Real block allocated, for memalign. */
57 unsigned long int magic2
; /* Extra, keeps us doubleword aligned. */
60 /* This is the beginning of the list of all memory blocks allocated.
61 It is only constructed if the pedantic testing is requested. */
62 static struct hdr
*root
;
64 static int mcheck_used
;
66 /* Nonzero if pedentic checking of all blocks is requested. */
69 #if defined _LIBC || defined STDC_HEADERS || defined USG
73 static void flood (__ptr_t
, int, __malloc_size_t
);
75 flood (ptr
, val
, size
)
86 static enum mcheck_status
87 checkhdr (const struct hdr
*hdr
)
89 enum mcheck_status status
;
92 /* Maybe the mcheck used is disabled? This happens when we find
93 an error and report it. */
96 switch (hdr
->magic
^ ((uintptr_t) hdr
->prev
+ (uintptr_t) hdr
->next
))
102 status
= MCHECK_FREE
;
105 if (((char *) &hdr
[1])[hdr
->size
] != MAGICBYTE
)
106 status
= MCHECK_TAIL
;
107 else if ((hdr
->magic2
^ (uintptr_t) hdr
->block
) != MAGICWORD
)
108 status
= MCHECK_HEAD
;
113 if (status
!= MCHECK_OK
)
116 (*abortfunc
) (status
);
123 mcheck_check_all (void)
125 /* Walk through all the active blocks and test whether they were tempered
127 struct hdr
*runp
= root
;
129 /* Temporarily turn off the checks. */
134 (void) checkhdr (runp
);
139 /* Turn checks on again. */
143 libc_hidden_def (mcheck_check_all
)
147 unlink_blk (struct hdr
*ptr
)
149 if (ptr
->next
!= NULL
)
151 ptr
->next
->prev
= ptr
->prev
;
152 ptr
->next
->magic
= MAGICWORD
^ ((uintptr_t) ptr
->next
->prev
153 + (uintptr_t) ptr
->next
->next
);
155 if (ptr
->prev
!= NULL
)
157 ptr
->prev
->next
= ptr
->next
;
158 ptr
->prev
->magic
= MAGICWORD
^ ((uintptr_t) ptr
->prev
->prev
159 + (uintptr_t) ptr
->prev
->next
);
166 link_blk (struct hdr
*hdr
)
171 hdr
->magic
= MAGICWORD
^ (uintptr_t) hdr
->next
;
173 /* And the next block. */
174 if (hdr
->next
!= NULL
)
176 hdr
->next
->prev
= hdr
;
177 hdr
->next
->magic
= MAGICWORD
^ ((uintptr_t) hdr
178 + (uintptr_t) hdr
->next
->next
);
182 freehook (__ptr_t ptr
, const __ptr_t caller
)
188 struct hdr
*hdr
= ((struct hdr
*) ptr
) - 1;
190 hdr
->magic
= MAGICFREE
;
191 hdr
->magic2
= MAGICFREE
;
193 hdr
->prev
= hdr
->next
= NULL
;
194 flood (ptr
, FREEFLOOD
, hdr
->size
);
197 __free_hook
= old_free_hook
;
198 if (old_free_hook
!= NULL
)
199 (*old_free_hook
) (ptr
, caller
);
202 __free_hook
= freehook
;
206 mallochook (__malloc_size_t size
, const __ptr_t caller
)
213 if (size
> ~((size_t) 0) - (sizeof (struct hdr
) + 1))
215 __set_errno (ENOMEM
);
219 __malloc_hook
= old_malloc_hook
;
220 if (old_malloc_hook
!= NULL
)
221 hdr
= (struct hdr
*) (*old_malloc_hook
) (sizeof (struct hdr
) + size
+ 1,
224 hdr
= (struct hdr
*) malloc (sizeof (struct hdr
) + size
+ 1);
225 __malloc_hook
= mallochook
;
232 hdr
->magic2
= (uintptr_t) hdr
^ MAGICWORD
;
233 ((char *) &hdr
[1])[size
] = MAGICBYTE
;
234 flood ((__ptr_t
) (hdr
+ 1), MALLOCFLOOD
, size
);
235 return (__ptr_t
) (hdr
+ 1);
239 memalignhook (__malloc_size_t alignment
, __malloc_size_t size
,
240 const __ptr_t caller
)
243 __malloc_size_t slop
;
249 slop
= (sizeof *hdr
+ alignment
- 1) & -alignment
;
251 if (size
> ~((size_t) 0) - (slop
+ 1))
253 __set_errno (ENOMEM
);
257 __memalign_hook
= old_memalign_hook
;
258 if (old_memalign_hook
!= NULL
)
259 block
= (*old_memalign_hook
) (alignment
, slop
+ size
+ 1, caller
);
261 block
= memalign (alignment
, slop
+ size
+ 1);
262 __memalign_hook
= memalignhook
;
266 hdr
= ((struct hdr
*) (block
+ slop
)) - 1;
270 hdr
->block
= (__ptr_t
) block
;
271 hdr
->magic2
= (uintptr_t) block
^ MAGICWORD
;
272 ((char *) &hdr
[1])[size
] = MAGICBYTE
;
273 flood ((__ptr_t
) (hdr
+ 1), MALLOCFLOOD
, size
);
274 return (__ptr_t
) (hdr
+ 1);
278 reallochook (__ptr_t ptr
, __malloc_size_t size
, const __ptr_t caller
)
282 freehook (ptr
, caller
);
287 __malloc_size_t osize
;
292 if (size
> ~((size_t) 0) - (sizeof (struct hdr
) + 1))
294 __set_errno (ENOMEM
);
300 hdr
= ((struct hdr
*) ptr
) - 1;
306 flood ((char *) ptr
+ size
, FREEFLOOD
, osize
- size
);
313 __free_hook
= old_free_hook
;
314 __malloc_hook
= old_malloc_hook
;
315 __memalign_hook
= old_memalign_hook
;
316 __realloc_hook
= old_realloc_hook
;
317 if (old_realloc_hook
!= NULL
)
318 hdr
= (struct hdr
*) (*old_realloc_hook
) ((__ptr_t
) hdr
,
319 sizeof (struct hdr
) + size
+ 1,
322 hdr
= (struct hdr
*) realloc ((__ptr_t
) hdr
,
323 sizeof (struct hdr
) + size
+ 1);
324 __free_hook
= freehook
;
325 __malloc_hook
= mallochook
;
326 __memalign_hook
= memalignhook
;
327 __realloc_hook
= reallochook
;
334 hdr
->magic2
= (uintptr_t) hdr
^ MAGICWORD
;
335 ((char *) &hdr
[1])[size
] = MAGICBYTE
;
337 flood ((char *) (hdr
+ 1) + osize
, MALLOCFLOOD
, size
- osize
);
338 return (__ptr_t
) (hdr
+ 1);
341 __attribute__ ((noreturn
))
343 mabort (enum mcheck_status status
)
349 msg
= _("memory is consistent, library is buggy\n");
352 msg
= _("memory clobbered before allocated block\n");
355 msg
= _("memory clobbered past end of allocated block\n");
358 msg
= _("block freed twice\n");
361 msg
= _("bogus mcheck_status, library is buggy\n");
367 fprintf (stderr
, "mcheck: %s", msg
);
373 /* Memory barrier so that GCC does not optimize out the argument. */
374 #define malloc_opt_barrier(x) \
375 ({ __typeof (x) __x = x; __asm ("" : "+m" (__x)); __x; })
379 void (*func
) (enum mcheck_status
);
381 abortfunc
= (func
!= NULL
) ? func
: &mabort
;
383 /* These hooks may not be safely inserted if malloc is already in use. */
384 if (__malloc_initialized
<= 0 && !mcheck_used
)
386 /* We call malloc() once here to ensure it is initialized. */
387 void *p
= malloc (0);
388 /* GCC might optimize out the malloc/free pair without a barrier. */
389 p
= malloc_opt_barrier (p
);
392 old_free_hook
= __free_hook
;
393 __free_hook
= freehook
;
394 old_malloc_hook
= __malloc_hook
;
395 __malloc_hook
= mallochook
;
396 old_memalign_hook
= __memalign_hook
;
397 __memalign_hook
= memalignhook
;
398 old_realloc_hook
= __realloc_hook
;
399 __realloc_hook
= reallochook
;
403 return mcheck_used
? 0 : -1;
406 libc_hidden_def (mcheck
)
410 mcheck_pedantic (func
)
411 void (*func
) (enum mcheck_status
);
413 int res
= mcheck (func
);
422 return mcheck_used
? checkhdr (((struct hdr
*) ptr
) - 1) : MCHECK_DISABLED
;