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., 675 Mass Ave,
20 Cambridge, MA 02139, 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. */
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
PARAMS ((void));
33 /* Arbitrary magical numbers. */
35 #define MAGICWORD 0xfedabeeb /* Magic word for active chunk */
36 #define MAGICWORDFREE 0xdeadbeef /* Magic word for 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 /* Check the magicword and magicbyte, and if either is corrupted then
55 call the emergency abort function specified for the heap in use. */
60 CONST
struct hdr
*hdr
;
62 if (hdr
-> magic
!= MAGICWORD
||
63 ((char *) &hdr
[1])[hdr
-> size
] != MAGICBYTE
)
65 (*mdp
-> abortfunc
)();
74 struct hdr
*hdr
= ((struct hdr
*) ptr
) - 1;
79 hdr
-> magic
= MAGICWORDFREE
;
80 mdp
-> mfree_hook
= NULL
;
82 mdp
-> mfree_hook
= mfree_check
;
86 mmalloc_check (md
, size
)
95 mdp
-> mmalloc_hook
= NULL
;
96 nbytes
= sizeof (struct hdr
) + size
+ 1;
97 hdr
= (struct hdr
*) mmalloc (md
, nbytes
);
98 mdp
-> mmalloc_hook
= mmalloc_check
;
102 hdr
-> magic
= MAGICWORD
;
104 *((char *) hdr
+ size
) = MAGICBYTE
;
110 mrealloc_check (md
, ptr
, size
)
115 struct hdr
*hdr
= ((struct hdr
*) ptr
) - 1;
119 mdp
= MD_TO_MDP (md
);
121 mdp
-> mfree_hook
= NULL
;
122 mdp
-> mmalloc_hook
= NULL
;
123 mdp
-> mrealloc_hook
= NULL
;
124 nbytes
= sizeof (struct hdr
) + size
+ 1;
125 hdr
= (struct hdr
*) mrealloc (md
, (PTR
) hdr
, nbytes
);
126 mdp
-> mfree_hook
= mfree_check
;
127 mdp
-> mmalloc_hook
= mmalloc_check
;
128 mdp
-> mrealloc_hook
= mrealloc_check
;
133 *((char *) hdr
+ size
) = MAGICBYTE
;
138 /* Turn on default checking for mmalloc/mrealloc/mfree, for the heap specified
139 by MD. If FUNC is non-NULL, it is a pointer to the function to call
140 to abort whenever memory corruption is detected. By default, this is the
141 standard library function abort().
143 Note that we disallow installation of initial checking hooks if mmalloc
144 has been called at any time for this particular heap, since if any region
145 that is allocated prior to installation of the hooks is subsequently
146 reallocated or freed after installation of the hooks, it is guaranteed
147 to trigger a memory corruption error. We do this by checking the state
148 of the MMALLOC_INITIALIZED flag.
150 However, we can call this function at any time after the initial call,
151 to update the function pointers to the checking routines and to the
152 user defined corruption handler routine, as long as these function pointers
153 have been previously extablished by the initial call. Note that we
154 do this automatically when remapping an previously used heap, to ensure
155 that the hooks get updated to the correct values, although the corruption
156 handler pointer gets set back to the default. The application can then
157 call mmcheck to use a different corruption handler if desired.
159 Returns non-zero if checking is successfully enabled, zero otherwise. */
164 void (*func
) PARAMS ((void));
169 mdp
= MD_TO_MDP (md
);
171 /* We can safely set or update the abort function at any time, regardless
172 of whether or not we successfully do anything else. */
174 mdp
-> abortfunc
= (func
!= NULL
? func
: abort
);
176 /* If we haven't yet called mmalloc the first time for this heap, or if we
177 have hooks that were previously installed, then allow the hooks to be
178 initialized or updated. */
180 if (1 /* FIXME: Always allow installation for now. */ ||
181 !(mdp
-> flags
& MMALLOC_INITIALIZED
) ||
182 (mdp
-> mfree_hook
!= NULL
))
184 mdp
-> mfree_hook
= mfree_check
;
185 mdp
-> mmalloc_hook
= mmalloc_check
;
186 mdp
-> mrealloc_hook
= mrealloc_check
;
187 mdp
-> flags
|= MMALLOC_MMCHECK_USED
;
This page took 0.032407 seconds and 4 git commands to generate.