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. */
38 size_t size; /* Exact size requested by user. */
39 unsigned long int magic; /* Magic number to check header integrity. */
42 static void checkhdr (struct mdesc *mdp, const struct hdr *hdr);
43 static void mfree_check (void* md, void* ptr);
44 static void* mmalloc_check (void* md, size_t size);
45 static void* mrealloc_check (void* md, void* ptr, size_t size);
47 /* Check the magicword and magicbyte, and if either is corrupted then
48 call the emergency abort function specified for the heap in use. */
51 checkhdr (struct mdesc *mdp, const struct hdr *hdr)
53 if (hdr -> magic != MAGICWORD ||
54 ((char *) &hdr[1])[hdr -> size] != MAGICBYTE)
56 (*mdp -> abortfunc)();
61 mfree_check (void *md, void *ptr)
63 struct hdr *hdr = ((struct hdr *) ptr) - 1;
68 hdr -> magic = MAGICWORDFREE;
69 mdp -> mfree_hook = NULL;
70 mfree (md, (void*)hdr);
71 mdp -> mfree_hook = mfree_check;
75 mmalloc_check (void *md, size_t size)
82 mdp -> mmalloc_hook = NULL;
83 nbytes = sizeof (struct hdr) + size + 1;
84 hdr = (struct hdr *) mmalloc (md, nbytes);
85 mdp -> mmalloc_hook = mmalloc_check;
89 hdr -> magic = MAGICWORD;
91 *((char *) hdr + size) = MAGICBYTE;
97 mrealloc_check (void* md, void* ptr, size_t size)
99 struct hdr *hdr = ((struct hdr *) ptr) - 1;
103 mdp = MD_TO_MDP (md);
105 mdp -> mfree_hook = NULL;
106 mdp -> mmalloc_hook = NULL;
107 mdp -> mrealloc_hook = NULL;
108 nbytes = sizeof (struct hdr) + size + 1;
109 hdr = (struct hdr *) mrealloc (md, (void*) hdr, nbytes);
110 mdp -> mfree_hook = mfree_check;
111 mdp -> mmalloc_hook = mmalloc_check;
112 mdp -> mrealloc_hook = mrealloc_check;
117 *((char *) hdr + size) = MAGICBYTE;
119 return ((void*) hdr);
122 /* Turn on default checking for mmalloc/mrealloc/mfree, for the heap specified
123 by MD. If FUNC is non-NULL, it is a pointer to the function to call
124 to abort whenever memory corruption is detected. By default, this is the
125 standard library function abort().
127 Note that we disallow installation of initial checking hooks if mmalloc
128 has been called at any time for this particular heap, since if any region
129 that is allocated prior to installation of the hooks is subsequently
130 reallocated or freed after installation of the hooks, it is guaranteed
131 to trigger a memory corruption error. We do this by checking the state
132 of the MMALLOC_INITIALIZED flag. If the FORCE argument is non-zero, this
133 checking is disabled and it is allowed to install the checking hooks at any
134 time. This is useful on systems where the C runtime makes one or more
135 malloc calls before the user code had a chance to call mmcheck or mmcheckf,
136 but never calls free with these values. Thus if we are certain that only
137 values obtained from mallocs after an mmcheck/mmcheckf will ever be passed
138 to free(), we can go ahead and force installation of the useful checking
141 However, we can call this function at any time after the initial call,
142 to update the function pointers to the checking routines and to the
143 user defined corruption handler routine, as long as these function pointers
144 have been previously extablished by the initial call. Note that we
145 do this automatically when remapping a previously used heap, to ensure
146 that the hooks get updated to the correct values, although the corruption
147 handler pointer gets set back to the default. The application can then
148 call mmcheck to use a different corruption handler if desired.
150 Returns non-zero if checking is successfully enabled, zero otherwise. */
153 mmcheckf (void *md, void (*func)(void), int force)
158 mdp = MD_TO_MDP (md);
160 /* We can safely set or update the abort function at any time, regardless
161 of whether or not we successfully do anything else. */
163 mdp -> abortfunc = (func != NULL ? func : abort);
165 /* If we haven't yet called mmalloc the first time for this heap, or if we
166 have hooks that were previously installed, then allow the hooks to be
167 initialized or updated. */
170 !(mdp -> flags & MMALLOC_INITIALIZED) ||
171 (mdp -> mfree_hook != NULL))
173 mdp -> mfree_hook = mfree_check;
174 mdp -> mmalloc_hook = mmalloc_check;
175 mdp -> mrealloc_hook = mrealloc_check;
176 mdp -> flags |= MMALLOC_MMCHECK_USED;
187 /* This routine is for backwards compatibility only, in case there are
188 still callers to the original mmcheck function. */
191 mmcheck (void *md, void (*func) (void))
195 rtnval = mmcheckf (md, func, 0);