1 /* Standard debugging hooks for `malloc'.
2 Copyright (C) 1990-2012 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, see
18 <http://www.gnu.org/licenses/>. */
20 #ifndef _MALLOC_INTERNAL
21 # define _MALLOC_INTERNAL
30 /* Old hook values. */
31 static void (*old_free_hook
) (__ptr_t ptr
, const __ptr_t
);
32 static __ptr_t (*old_malloc_hook
) (__malloc_size_t size
, const __ptr_t
);
33 static __ptr_t (*old_memalign_hook
) (__malloc_size_t alignment
,
36 static __ptr_t (*old_realloc_hook
) (__ptr_t ptr
, __malloc_size_t size
,
39 /* Function to call when something awful happens. */
40 static void (*abortfunc
) (enum mcheck_status
);
42 /* Arbitrary magical numbers. */
43 #define MAGICWORD 0xfedabeeb
44 #define MAGICFREE 0xd8675309
45 #define MAGICBYTE ((char) 0xd7)
46 #define MALLOCFLOOD ((char) 0x93)
47 #define FREEFLOOD ((char) 0x95)
51 __malloc_size_t size
; /* Exact size requested by user. */
52 unsigned long int magic
; /* Magic number to check header integrity. */
55 __ptr_t block
; /* Real block allocated, for memalign. */
56 unsigned long int magic2
; /* Extra, keeps us doubleword aligned. */
59 /* This is the beginning of the list of all memory blocks allocated.
60 It is only constructed if the pedantic testing is requested. */
61 static struct hdr
*root
;
63 static int mcheck_used
;
65 /* Nonzero if pedentic checking of all blocks is requested. */
68 #if defined _LIBC || defined STDC_HEADERS || defined USG
72 static void flood (__ptr_t
, int, __malloc_size_t
);
74 flood (ptr
, val
, size
)
85 static enum mcheck_status
86 checkhdr (const struct hdr
*hdr
)
88 enum mcheck_status status
;
91 /* Maybe the mcheck used is disabled? This happens when we find
92 an error and report it. */
95 switch (hdr
->magic
^ ((uintptr_t) hdr
->prev
+ (uintptr_t) hdr
->next
))
101 status
= MCHECK_FREE
;
104 if (((char *) &hdr
[1])[hdr
->size
] != MAGICBYTE
)
105 status
= MCHECK_TAIL
;
106 else if ((hdr
->magic2
^ (uintptr_t) hdr
->block
) != MAGICWORD
)
107 status
= MCHECK_HEAD
;
112 if (status
!= MCHECK_OK
)
115 (*abortfunc
) (status
);
122 mcheck_check_all (void)
124 /* Walk through all the active blocks and test whether they were tampered
126 struct hdr
*runp
= root
;
128 /* Temporarily turn off the checks. */
133 (void) checkhdr (runp
);
138 /* Turn checks on again. */
142 libc_hidden_def (mcheck_check_all
)
146 unlink_blk (struct hdr
*ptr
)
148 if (ptr
->next
!= NULL
)
150 ptr
->next
->prev
= ptr
->prev
;
151 ptr
->next
->magic
= MAGICWORD
^ ((uintptr_t) ptr
->next
->prev
152 + (uintptr_t) ptr
->next
->next
);
154 if (ptr
->prev
!= NULL
)
156 ptr
->prev
->next
= ptr
->next
;
157 ptr
->prev
->magic
= MAGICWORD
^ ((uintptr_t) ptr
->prev
->prev
158 + (uintptr_t) ptr
->prev
->next
);
165 link_blk (struct hdr
*hdr
)
170 hdr
->magic
= MAGICWORD
^ (uintptr_t) hdr
->next
;
172 /* And the next block. */
173 if (hdr
->next
!= NULL
)
175 hdr
->next
->prev
= hdr
;
176 hdr
->next
->magic
= MAGICWORD
^ ((uintptr_t) hdr
177 + (uintptr_t) hdr
->next
->next
);
181 freehook (__ptr_t ptr
, const __ptr_t caller
)
187 struct hdr
*hdr
= ((struct hdr
*) ptr
) - 1;
189 hdr
->magic
= MAGICFREE
;
190 hdr
->magic2
= MAGICFREE
;
192 hdr
->prev
= hdr
->next
= NULL
;
193 flood (ptr
, FREEFLOOD
, hdr
->size
);
196 __free_hook
= old_free_hook
;
197 if (old_free_hook
!= NULL
)
198 (*old_free_hook
) (ptr
, caller
);
201 __free_hook
= freehook
;
205 mallochook (__malloc_size_t size
, const __ptr_t caller
)
212 if (size
> ~((size_t) 0) - (sizeof (struct hdr
) + 1))
214 __set_errno (ENOMEM
);
218 __malloc_hook
= old_malloc_hook
;
219 if (old_malloc_hook
!= NULL
)
220 hdr
= (struct hdr
*) (*old_malloc_hook
) (sizeof (struct hdr
) + size
+ 1,
223 hdr
= (struct hdr
*) malloc (sizeof (struct hdr
) + size
+ 1);
224 __malloc_hook
= mallochook
;
231 hdr
->magic2
= (uintptr_t) hdr
^ MAGICWORD
;
232 ((char *) &hdr
[1])[size
] = MAGICBYTE
;
233 flood ((__ptr_t
) (hdr
+ 1), MALLOCFLOOD
, size
);
234 return (__ptr_t
) (hdr
+ 1);
238 memalignhook (__malloc_size_t alignment
, __malloc_size_t size
,
239 const __ptr_t caller
)
242 __malloc_size_t slop
;
248 slop
= (sizeof *hdr
+ alignment
- 1) & -alignment
;
250 if (size
> ~((size_t) 0) - (slop
+ 1))
252 __set_errno (ENOMEM
);
256 __memalign_hook
= old_memalign_hook
;
257 if (old_memalign_hook
!= NULL
)
258 block
= (*old_memalign_hook
) (alignment
, slop
+ size
+ 1, caller
);
260 block
= memalign (alignment
, slop
+ size
+ 1);
261 __memalign_hook
= memalignhook
;
265 hdr
= ((struct hdr
*) (block
+ slop
)) - 1;
269 hdr
->block
= (__ptr_t
) block
;
270 hdr
->magic2
= (uintptr_t) block
^ MAGICWORD
;
271 ((char *) &hdr
[1])[size
] = MAGICBYTE
;
272 flood ((__ptr_t
) (hdr
+ 1), MALLOCFLOOD
, size
);
273 return (__ptr_t
) (hdr
+ 1);
277 reallochook (__ptr_t ptr
, __malloc_size_t size
, const __ptr_t caller
)
281 freehook (ptr
, caller
);
286 __malloc_size_t osize
;
291 if (size
> ~((size_t) 0) - (sizeof (struct hdr
) + 1))
293 __set_errno (ENOMEM
);
299 hdr
= ((struct hdr
*) ptr
) - 1;
305 flood ((char *) ptr
+ size
, FREEFLOOD
, osize
- size
);
312 __free_hook
= old_free_hook
;
313 __malloc_hook
= old_malloc_hook
;
314 __memalign_hook
= old_memalign_hook
;
315 __realloc_hook
= old_realloc_hook
;
316 if (old_realloc_hook
!= NULL
)
317 hdr
= (struct hdr
*) (*old_realloc_hook
) ((__ptr_t
) hdr
,
318 sizeof (struct hdr
) + size
+ 1,
321 hdr
= (struct hdr
*) realloc ((__ptr_t
) hdr
,
322 sizeof (struct hdr
) + size
+ 1);
323 __free_hook
= freehook
;
324 __malloc_hook
= mallochook
;
325 __memalign_hook
= memalignhook
;
326 __realloc_hook
= reallochook
;
333 hdr
->magic2
= (uintptr_t) hdr
^ MAGICWORD
;
334 ((char *) &hdr
[1])[size
] = MAGICBYTE
;
336 flood ((char *) (hdr
+ 1) + osize
, MALLOCFLOOD
, size
- osize
);
337 return (__ptr_t
) (hdr
+ 1);
340 __attribute__ ((noreturn
))
342 mabort (enum mcheck_status status
)
348 msg
= _("memory is consistent, library is buggy\n");
351 msg
= _("memory clobbered before allocated block\n");
354 msg
= _("memory clobbered past end of allocated block\n");
357 msg
= _("block freed twice\n");
360 msg
= _("bogus mcheck_status, library is buggy\n");
366 fprintf (stderr
, "mcheck: %s", msg
);
372 /* Memory barrier so that GCC does not optimize out the argument. */
373 #define malloc_opt_barrier(x) \
374 ({ __typeof (x) __x = x; __asm ("" : "+m" (__x)); __x; })
378 void (*func
) (enum mcheck_status
);
380 abortfunc
= (func
!= NULL
) ? func
: &mabort
;
382 /* These hooks may not be safely inserted if malloc is already in use. */
383 if (__malloc_initialized
<= 0 && !mcheck_used
)
385 /* We call malloc() once here to ensure it is initialized. */
386 void *p
= malloc (0);
387 /* GCC might optimize out the malloc/free pair without a barrier. */
388 p
= malloc_opt_barrier (p
);
391 old_free_hook
= __free_hook
;
392 __free_hook
= freehook
;
393 old_malloc_hook
= __malloc_hook
;
394 __malloc_hook
= mallochook
;
395 old_memalign_hook
= __memalign_hook
;
396 __memalign_hook
= memalignhook
;
397 old_realloc_hook
= __realloc_hook
;
398 __realloc_hook
= reallochook
;
402 return mcheck_used
? 0 : -1;
405 libc_hidden_def (mcheck
)
409 mcheck_pedantic (func
)
410 void (*func
) (enum mcheck_status
);
412 int res
= mcheck (func
);
421 return mcheck_used
? checkhdr (((struct hdr
*) ptr
) - 1) : MCHECK_DISABLED
;