1 /* Standard debugging hooks for `malloc'.
2 Copyright (C) 1990-2015 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) (size_t size, const __ptr_t);
33 static __ptr_t (*old_memalign_hook) (size_t alignment, size_t size,
35 static __ptr_t (*old_realloc_hook) (__ptr_t ptr, size_t size,
38 /* Function to call when something awful happens. */
39 static void (*abortfunc) (enum mcheck_status);
41 /* Arbitrary magical numbers. */
42 #define MAGICWORD 0xfedabeeb
43 #define MAGICFREE 0xd8675309
44 #define MAGICBYTE ((char) 0xd7)
45 #define MALLOCFLOOD ((char) 0x93)
46 #define FREEFLOOD ((char) 0x95)
50 size_t size; /* Exact size requested by user. */
51 unsigned long int magic; /* Magic number to check header integrity. */
54 __ptr_t block; /* Real block allocated, for memalign. */
55 unsigned long int magic2; /* Extra, keeps us doubleword aligned. */
58 /* This is the beginning of the list of all memory blocks allocated.
59 It is only constructed if the pedantic testing is requested. */
60 static struct hdr *root;
62 static int mcheck_used;
64 /* Nonzero if pedentic checking of all blocks is requested. */
67 #if defined _LIBC || defined STDC_HEADERS || defined USG
71 static void flood (__ptr_t, int, size_t);
72 static void flood (ptr, val, size)
83 static enum mcheck_status
84 checkhdr (const struct hdr *hdr)
86 enum mcheck_status status;
89 /* Maybe the mcheck used is disabled? This happens when we find
90 an error and report it. */
93 switch (hdr->magic ^ ((uintptr_t) hdr->prev + (uintptr_t) hdr->next))
102 if (((char *) &hdr[1])[hdr->size] != MAGICBYTE)
103 status = MCHECK_TAIL;
104 else if ((hdr->magic2 ^ (uintptr_t) hdr->block) != MAGICWORD)
105 status = MCHECK_HEAD;
110 if (status != MCHECK_OK)
113 (*abortfunc) (status);
120 mcheck_check_all (void)
122 /* Walk through all the active blocks and test whether they were tampered
124 struct hdr *runp = root;
126 /* Temporarily turn off the checks. */
131 (void) checkhdr (runp);
136 /* Turn checks on again. */
140 libc_hidden_def (mcheck_check_all)
144 unlink_blk (struct hdr *ptr)
146 if (ptr->next != NULL)
148 ptr->next->prev = ptr->prev;
149 ptr->next->magic = MAGICWORD ^ ((uintptr_t) ptr->next->prev
150 + (uintptr_t) ptr->next->next);
152 if (ptr->prev != NULL)
154 ptr->prev->next = ptr->next;
155 ptr->prev->magic = MAGICWORD ^ ((uintptr_t) ptr->prev->prev
156 + (uintptr_t) ptr->prev->next);
163 link_blk (struct hdr *hdr)
168 hdr->magic = MAGICWORD ^ (uintptr_t) hdr->next;
170 /* And the next block. */
171 if (hdr->next != NULL)
173 hdr->next->prev = hdr;
174 hdr->next->magic = MAGICWORD ^ ((uintptr_t) hdr
175 + (uintptr_t) hdr->next->next);
179 freehook (__ptr_t ptr, const __ptr_t caller)
185 struct hdr *hdr = ((struct hdr *) ptr) - 1;
187 hdr->magic = MAGICFREE;
188 hdr->magic2 = MAGICFREE;
190 hdr->prev = hdr->next = NULL;
191 flood (ptr, FREEFLOOD, hdr->size);
194 __free_hook = old_free_hook;
195 if (old_free_hook != NULL)
196 (*old_free_hook)(ptr, caller);
199 __free_hook = freehook;
203 mallochook (size_t size, const __ptr_t caller)
210 if (size > ~((size_t) 0) - (sizeof (struct hdr) + 1))
212 __set_errno (ENOMEM);
216 __malloc_hook = old_malloc_hook;
217 if (old_malloc_hook != NULL)
218 hdr = (struct hdr *) (*old_malloc_hook)(sizeof (struct hdr) + size + 1,
221 hdr = (struct hdr *) malloc (sizeof (struct hdr) + size + 1);
222 __malloc_hook = mallochook;
229 hdr->magic2 = (uintptr_t) hdr ^ MAGICWORD;
230 ((char *) &hdr[1])[size] = MAGICBYTE;
231 flood ((__ptr_t) (hdr + 1), MALLOCFLOOD, size);
232 return (__ptr_t) (hdr + 1);
236 memalignhook (size_t alignment, size_t size,
237 const __ptr_t caller)
246 slop = (sizeof *hdr + alignment - 1) & - alignment;
248 if (size > ~((size_t) 0) - (slop + 1))
250 __set_errno (ENOMEM);
254 __memalign_hook = old_memalign_hook;
255 if (old_memalign_hook != NULL)
256 block = (*old_memalign_hook)(alignment, slop + size + 1, caller);
258 block = memalign (alignment, slop + size + 1);
259 __memalign_hook = memalignhook;
263 hdr = ((struct hdr *) (block + slop)) - 1;
267 hdr->block = (__ptr_t) block;
268 hdr->magic2 = (uintptr_t) block ^ MAGICWORD;
269 ((char *) &hdr[1])[size] = MAGICBYTE;
270 flood ((__ptr_t) (hdr + 1), MALLOCFLOOD, size);
271 return (__ptr_t) (hdr + 1);
275 reallochook (__ptr_t ptr, size_t size, const __ptr_t caller)
279 freehook (ptr, caller);
289 if (size > ~((size_t) 0) - (sizeof (struct hdr) + 1))
291 __set_errno (ENOMEM);
297 hdr = ((struct hdr *) ptr) - 1;
303 flood ((char *) ptr + size, FREEFLOOD, osize - size);
310 __free_hook = old_free_hook;
311 __malloc_hook = old_malloc_hook;
312 __memalign_hook = old_memalign_hook;
313 __realloc_hook = old_realloc_hook;
314 if (old_realloc_hook != NULL)
315 hdr = (struct hdr *) (*old_realloc_hook)((__ptr_t) hdr,
316 sizeof (struct hdr) + size + 1,
319 hdr = (struct hdr *) realloc ((__ptr_t) hdr,
320 sizeof (struct hdr) + size + 1);
321 __free_hook = freehook;
322 __malloc_hook = mallochook;
323 __memalign_hook = memalignhook;
324 __realloc_hook = reallochook;
331 hdr->magic2 = (uintptr_t) hdr ^ MAGICWORD;
332 ((char *) &hdr[1])[size] = MAGICBYTE;
334 flood ((char *) (hdr + 1) + osize, MALLOCFLOOD, size - osize);
335 return (__ptr_t) (hdr + 1);
338 __attribute__ ((noreturn))
340 mabort (enum mcheck_status status)
346 msg = _ ("memory is consistent, library is buggy\n");
349 msg = _ ("memory clobbered before allocated block\n");
352 msg = _ ("memory clobbered past end of allocated block\n");
355 msg = _ ("block freed twice\n");
358 msg = _ ("bogus mcheck_status, library is buggy\n");
364 fprintf (stderr, "mcheck: %s", msg);
370 /* Memory barrier so that GCC does not optimize out the argument. */
371 #define malloc_opt_barrier(x) \
372 ({ __typeof (x) __x = x; __asm ("" : "+m" (__x)); __x; })
375 void (*func)(enum mcheck_status);
377 abortfunc = (func != NULL) ? func : &mabort;
379 /* These hooks may not be safely inserted if malloc is already in use. */
380 if (__malloc_initialized <= 0 && !mcheck_used)
382 /* We call malloc() once here to ensure it is initialized. */
383 void *p = malloc (0);
384 /* GCC might optimize out the malloc/free pair without a barrier. */
385 p = malloc_opt_barrier (p);
388 old_free_hook = __free_hook;
389 __free_hook = freehook;
390 old_malloc_hook = __malloc_hook;
391 __malloc_hook = mallochook;
392 old_memalign_hook = __memalign_hook;
393 __memalign_hook = memalignhook;
394 old_realloc_hook = __realloc_hook;
395 __realloc_hook = reallochook;
399 return mcheck_used ? 0 : -1;
402 libc_hidden_def (mcheck)
405 int 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;