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 /* Copyright (c) 2010. The SimGrid Team.
8 * All rights reserved. */
10 /* This program is free software; you can redistribute it and/or modify it
11 * under the terms of the license (GNU LGPL) which comes with this package. */
13 #include "mmprivate.h"
15 /* Default function to call when something awful happens. The application
16 can specify an alternate function to be called instead (and probably will
19 extern void abort(void);
21 /* Arbitrary magical numbers. */
23 #define MAGICWORD (unsigned int) 0xfedabeeb /* Active chunk */
24 #define MAGICWORDFREE (unsigned int) 0xdeadbeef /* Inactive chunk */
25 #define MAGICBYTE ((char) 0xd7)
27 /* Each memory allocation is bounded by a header structure and a trailer
30 <size><magicword><user's allocation><magicbyte>
32 The pointer returned to the user points to the first byte in the
33 user's allocation area. The magic word can be tested to detect
34 buffer underruns and the magic byte can be tested to detect overruns. */
37 size_t size; /* Exact size requested by user. */
38 unsigned long int magic; /* Magic number to check header integrity. */
41 static void checkhdr(struct mdesc *mdp, const struct hdr *hdr);
42 static void mfree_check(void *md, void *ptr);
43 static void *mmalloc_check(void *md, size_t size);
44 static void *mrealloc_check(void *md, void *ptr, size_t size);
46 /* Check the magicword and magicbyte, and if either is corrupted then
47 call the emergency abort function specified for the heap in use. */
49 static void checkhdr(struct mdesc *mdp, const struct hdr *hdr)
51 if (hdr->magic != MAGICWORD ||
52 ((char *) &hdr[1])[hdr->size] != MAGICBYTE) {
57 static void mfree_check(void *md, void *ptr)
59 struct hdr *hdr = ((struct hdr *) ptr) - 1;
64 hdr->magic = MAGICWORDFREE;
65 mdp->mfree_hook = NULL;
66 mfree(md, (void *) hdr);
67 mdp->mfree_hook = mfree_check;
70 static void *mmalloc_check(void *md, size_t size)
77 mdp->mmalloc_hook = NULL;
78 nbytes = sizeof(struct hdr) + size + 1;
79 hdr = (struct hdr *) mmalloc(md, nbytes);
80 mdp->mmalloc_hook = mmalloc_check;
83 hdr->magic = MAGICWORD;
85 *((char *) hdr + size) = MAGICBYTE;
87 return ((void *) hdr);
90 static void *mrealloc_check(void *md, void *ptr, size_t size)
92 struct hdr *hdr = ((struct hdr *) ptr) - 1;
98 mdp->mfree_hook = NULL;
99 mdp->mmalloc_hook = NULL;
100 mdp->mrealloc_hook = NULL;
101 nbytes = sizeof(struct hdr) + size + 1;
102 hdr = (struct hdr *) mrealloc(md, (void *) hdr, nbytes);
103 mdp->mfree_hook = mfree_check;
104 mdp->mmalloc_hook = mmalloc_check;
105 mdp->mrealloc_hook = mrealloc_check;
109 *((char *) hdr + size) = MAGICBYTE;
111 return ((void *) hdr);
114 /* Turn on default checking for mmalloc/mrealloc/mfree, for the heap specified
115 by MD. If FUNC is non-NULL, it is a pointer to the function to call
116 to abort whenever memory corruption is detected. By default, this is the
117 standard library function abort().
119 Note that we disallow installation of initial checking hooks if mmalloc
120 has been called at any time for this particular heap, since if any region
121 that is allocated prior to installation of the hooks is subsequently
122 reallocated or freed after installation of the hooks, it is guaranteed
123 to trigger a memory corruption error. We do this by checking the state
124 of the MMALLOC_INITIALIZED flag. If the FORCE argument is non-zero, this
125 checking is disabled and it is allowed to install the checking hooks at any
126 time. This is useful on systems where the C runtime makes one or more
127 malloc calls before the user code had a chance to call mmcheck or mmcheckf,
128 but never calls free with these values. Thus if we are certain that only
129 values obtained from mallocs after an mmcheck/mmcheckf will ever be passed
130 to free(), we can go ahead and force installation of the useful checking
133 However, we can call this function at any time after the initial call,
134 to update the function pointers to the checking routines and to the
135 user defined corruption handler routine, as long as these function pointers
136 have been previously extablished by the initial call. Note that we
137 do this automatically when remapping a previously used heap, to ensure
138 that the hooks get updated to the correct values, although the corruption
139 handler pointer gets set back to the default. The application can then
140 call mmcheck to use a different corruption handler if desired.
142 Returns non-zero if checking is successfully enabled, zero otherwise. */
144 int mmcheckf(void *md, void (*func) (void), int force)
151 /* We can safely set or update the abort function at any time, regardless
152 of whether or not we successfully do anything else. */
154 mdp->abortfunc = (func != NULL ? func : abort);
156 /* If we haven't yet called mmalloc the first time for this heap, or if we
157 have hooks that were previously installed, then allow the hooks to be
158 initialized or updated. */
161 !(mdp->flags & MMALLOC_INITIALIZED) || (mdp->mfree_hook != NULL)) {
162 mdp->mfree_hook = mfree_check;
163 mdp->mmalloc_hook = mmalloc_check;
164 mdp->mrealloc_hook = mrealloc_check;
165 mdp->flags |= MMALLOC_MMCHECK_USED;
174 /* This routine is for backwards compatibility only, in case there are
175 still callers to the original mmcheck function. */
177 int mmcheck(void *md, void (*func) (void))
181 rtnval = mmcheckf(md, func, 0);