1 /* Standard debugging hooks for `mmalloc'.
2 Copyright 1990, 1991, 1992 Free Software Foundation
4 Written May 1989 by Mike Haertel.
5 Heavily modified Mar 1992 by Fred Fish (fnf@cygnus.com)
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 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 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the GNU C Library; see the file COPYING.LIB. If
19 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
22 The author may be reached (Email) at the address mike@ai.mit.edu,
23 or (US mail) as Mike Haertel c/o Free Software Foundation. */
25 #include "mmprivate.h"
27 /* Default function to call when something awful happens. The application
28 can specify an alternate function to be called instead (and probably will
31 extern void abort (void);
33 /* Arbitrary magical numbers. */
35 #define MAGICWORD (unsigned int) 0xfedabeeb /* Active chunk */
36 #define MAGICWORDFREE (unsigned int) 0xdeadbeef /* Inactive chunk */
37 #define MAGICBYTE ((char) 0xd7)
39 /* Each memory allocation is bounded by a header structure and a trailer
42 <size><magicword><user's allocation><magicbyte>
44 The pointer returned to the user points to the first byte in the
45 user's allocation area. The magic word can be tested to detect
46 buffer underruns and the magic byte can be tested to detect overruns. */
50 size_t size; /* Exact size requested by user. */
51 unsigned long int magic; /* Magic number to check header integrity. */
54 static void checkhdr (struct mdesc *mdp, const struct hdr *hdr);
55 static void mfree_check (void* md, void* ptr);
56 static void* mmalloc_check (void* md, size_t size);
57 static void* mrealloc_check (void* md, void* ptr, size_t size);
59 /* Check the magicword and magicbyte, and if either is corrupted then
60 call the emergency abort function specified for the heap in use. */
63 checkhdr (struct mdesc *mdp, const struct hdr *hdr)
65 if (hdr -> magic != MAGICWORD ||
66 ((char *) &hdr[1])[hdr -> size] != MAGICBYTE)
68 (*mdp -> abortfunc)();
73 mfree_check (void *md, void *ptr)
75 struct hdr *hdr = ((struct hdr *) ptr) - 1;
80 hdr -> magic = MAGICWORDFREE;
81 mdp -> mfree_hook = NULL;
82 mfree (md, (void*)hdr);
83 mdp -> mfree_hook = mfree_check;
87 mmalloc_check (void *md, size_t size)
94 mdp -> mmalloc_hook = NULL;
95 nbytes = sizeof (struct hdr) + size + 1;
96 hdr = (struct hdr *) mmalloc (md, nbytes);
97 mdp -> mmalloc_hook = mmalloc_check;
101 hdr -> magic = MAGICWORD;
103 *((char *) hdr + size) = MAGICBYTE;
105 return ((void*) hdr);
109 mrealloc_check (void* md, void* ptr, size_t size)
111 struct hdr *hdr = ((struct hdr *) ptr) - 1;
115 mdp = MD_TO_MDP (md);
117 mdp -> mfree_hook = NULL;
118 mdp -> mmalloc_hook = NULL;
119 mdp -> mrealloc_hook = NULL;
120 nbytes = sizeof (struct hdr) + size + 1;
121 hdr = (struct hdr *) mrealloc (md, (void*) hdr, nbytes);
122 mdp -> mfree_hook = mfree_check;
123 mdp -> mmalloc_hook = mmalloc_check;
124 mdp -> mrealloc_hook = mrealloc_check;
129 *((char *) hdr + size) = MAGICBYTE;
131 return ((void*) hdr);
134 /* Turn on default checking for mmalloc/mrealloc/mfree, for the heap specified
135 by MD. If FUNC is non-NULL, it is a pointer to the function to call
136 to abort whenever memory corruption is detected. By default, this is the
137 standard library function abort().
139 Note that we disallow installation of initial checking hooks if mmalloc
140 has been called at any time for this particular heap, since if any region
141 that is allocated prior to installation of the hooks is subsequently
142 reallocated or freed after installation of the hooks, it is guaranteed
143 to trigger a memory corruption error. We do this by checking the state
144 of the MMALLOC_INITIALIZED flag. If the FORCE argument is non-zero, this
145 checking is disabled and it is allowed to install the checking hooks at any
146 time. This is useful on systems where the C runtime makes one or more
147 malloc calls before the user code had a chance to call mmcheck or mmcheckf,
148 but never calls free with these values. Thus if we are certain that only
149 values obtained from mallocs after an mmcheck/mmcheckf will ever be passed
150 to free(), we can go ahead and force installation of the useful checking
153 However, we can call this function at any time after the initial call,
154 to update the function pointers to the checking routines and to the
155 user defined corruption handler routine, as long as these function pointers
156 have been previously extablished by the initial call. Note that we
157 do this automatically when remapping a previously used heap, to ensure
158 that the hooks get updated to the correct values, although the corruption
159 handler pointer gets set back to the default. The application can then
160 call mmcheck to use a different corruption handler if desired.
162 Returns non-zero if checking is successfully enabled, zero otherwise. */
165 mmcheckf (void *md, void (*func)(void), int force)
170 mdp = MD_TO_MDP (md);
172 /* We can safely set or update the abort function at any time, regardless
173 of whether or not we successfully do anything else. */
175 mdp -> abortfunc = (func != NULL ? func : abort);
177 /* If we haven't yet called mmalloc the first time for this heap, or if we
178 have hooks that were previously installed, then allow the hooks to be
179 initialized or updated. */
182 !(mdp -> flags & MMALLOC_INITIALIZED) ||
183 (mdp -> mfree_hook != NULL))
185 mdp -> mfree_hook = mfree_check;
186 mdp -> mmalloc_hook = mmalloc_check;
187 mdp -> mrealloc_hook = mrealloc_check;
188 mdp -> flags |= MMALLOC_MMCHECK_USED;
199 /* This routine is for backwards compatibility only, in case there are
200 still callers to the original mmcheck function. */
203 mmcheck (void *md, void (*func) (void))
207 rtnval = mmcheckf (md, func, 0);