Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
dcef8c34fb3aeb6d7110c4edb1bf94bb3b2a9011
[simgrid.git] / src / xbt / mmalloc / mmcheck.c
1 /* Standard debugging hooks for `mmalloc'.
2    Copyright 1990, 1991, 1992 Free Software Foundation
3
4    Written May 1989 by Mike Haertel.
5    Heavily modified Mar 1992 by Fred Fish (fnf@cygnus.com)
6
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.
11
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.
16
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.
21
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. */
24
25 #include "mmprivate.h"
26
27 /* Default function to call when something awful happens.  The application
28    can specify an alternate function to be called instead (and probably will
29    want to). */
30
31 extern void abort (void);
32
33 /* Arbitrary magical numbers.  */
34
35 #define MAGICWORD       (unsigned int) 0xfedabeeb       /* Active chunk */
36 #define MAGICWORDFREE   (unsigned int) 0xdeadbeef       /* Inactive chunk */
37 #define MAGICBYTE       ((char) 0xd7)
38
39 /* Each memory allocation is bounded by a header structure and a trailer
40    byte.  I.E.
41
42         <size><magicword><user's allocation><magicbyte>
43
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. */
47
48 struct hdr
49   {
50     size_t size;                /* Exact size requested by user.  */
51     unsigned long int magic;    /* Magic number to check header integrity.  */
52   };
53
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);
58
59 /* Check the magicword and magicbyte, and if either is corrupted then
60    call the emergency abort function specified for the heap in use. */
61
62 static void
63 checkhdr (struct mdesc *mdp, const struct hdr *hdr)
64 {
65   if (hdr -> magic != MAGICWORD ||
66       ((char *) &hdr[1])[hdr -> size] != MAGICBYTE)
67     {
68       (*mdp -> abortfunc)();
69     }
70 }
71
72 static void
73 mfree_check (void *md, void *ptr)
74 {
75   struct hdr *hdr = ((struct hdr *) ptr) - 1;
76   struct mdesc *mdp;
77
78   mdp = MD_TO_MDP (md);
79   checkhdr (mdp, hdr);
80   hdr -> magic = MAGICWORDFREE;
81   mdp -> mfree_hook = NULL;
82   mfree (md, (void*)hdr);
83   mdp -> mfree_hook = mfree_check;
84 }
85
86 static void*
87 mmalloc_check (void *md, size_t size)
88 {
89   struct hdr *hdr;
90   struct mdesc *mdp;
91   size_t nbytes;
92
93   mdp = MD_TO_MDP (md);
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;
98   if (hdr != NULL)
99     {
100       hdr -> size = size;
101       hdr -> magic = MAGICWORD;
102       hdr++;
103       *((char *) hdr + size) = MAGICBYTE;
104     }
105   return ((void*) hdr);
106 }
107
108 static void*
109 mrealloc_check (void* md, void* ptr, size_t size)
110 {
111   struct hdr *hdr = ((struct hdr *) ptr) - 1;
112   struct mdesc *mdp;
113   size_t nbytes;
114
115   mdp = MD_TO_MDP (md);
116   checkhdr (mdp, hdr);
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;
125   if (hdr != NULL)
126     {
127       hdr -> size = size;
128       hdr++;
129       *((char *) hdr + size) = MAGICBYTE;
130     }
131   return ((void*) hdr);
132 }
133
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().
138
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
151    hooks.
152
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.
161
162    Returns non-zero if checking is successfully enabled, zero otherwise. */
163
164 int
165 mmcheckf (void *md, void (*func)(void), int force)
166 {
167   struct mdesc *mdp;
168   int rtnval;
169
170   mdp = MD_TO_MDP (md);
171
172   /* We can safely set or update the abort function at any time, regardless
173      of whether or not we successfully do anything else. */
174
175   mdp -> abortfunc = (func != NULL ? func : abort);
176
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. */
180
181   if (force ||
182       !(mdp -> flags & MMALLOC_INITIALIZED) ||
183       (mdp -> mfree_hook != NULL))
184     {
185       mdp -> mfree_hook = mfree_check;
186       mdp -> mmalloc_hook = mmalloc_check;
187       mdp -> mrealloc_hook = mrealloc_check;
188       mdp -> flags |= MMALLOC_MMCHECK_USED;
189       rtnval = 1;
190     }
191   else
192     {
193       rtnval = 0;
194     }
195
196   return (rtnval);
197 }
198
199 /* This routine is for backwards compatibility only, in case there are
200    still callers to the original mmcheck function. */
201
202 int
203 mmcheck (void *md, void (*func) (void))
204 {
205   int rtnval;
206
207   rtnval = mmcheckf (md, func, 0);
208   return (rtnval);
209 }