Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
This commit 590f9c7e65ce6a64705272d79443599877d5cb72 add a segfault to compilation.
[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 /* Copyright (c) 2010. The SimGrid Team.
8  * All rights reserved.                                                     */
9
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. */
12
13 #include "mmprivate.h"
14
15 /* Default function to call when something awful happens.  The application
16    can specify an alternate function to be called instead (and probably will
17    want to). */
18
19 extern void abort (void);
20
21 /* Arbitrary magical numbers.  */
22
23 #define MAGICWORD       (unsigned int) 0xfedabeeb       /* Active chunk */
24 #define MAGICWORDFREE   (unsigned int) 0xdeadbeef       /* Inactive chunk */
25 #define MAGICBYTE       ((char) 0xd7)
26
27 /* Each memory allocation is bounded by a header structure and a trailer
28    byte.  I.E.
29
30         <size><magicword><user's allocation><magicbyte>
31
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. */
35
36 struct hdr
37   {
38     size_t size;                /* Exact size requested by user.  */
39     unsigned long int magic;    /* Magic number to check header integrity.  */
40   };
41
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);
46
47 /* Check the magicword and magicbyte, and if either is corrupted then
48    call the emergency abort function specified for the heap in use. */
49
50 static void
51 checkhdr (struct mdesc *mdp, const struct hdr *hdr)
52 {
53   if (hdr -> magic != MAGICWORD ||
54       ((char *) &hdr[1])[hdr -> size] != MAGICBYTE)
55     {
56       (*mdp -> abortfunc)();
57     }
58 }
59
60 static void
61 mfree_check (void *md, void *ptr)
62 {
63   struct hdr *hdr = ((struct hdr *) ptr) - 1;
64   struct mdesc *mdp;
65
66   mdp = MD_TO_MDP (md);
67   checkhdr (mdp, hdr);
68   hdr -> magic = MAGICWORDFREE;
69   mdp -> mfree_hook = NULL;
70   mfree (md, (void*)hdr);
71   mdp -> mfree_hook = mfree_check;
72 }
73
74 static void*
75 mmalloc_check (void *md, size_t size)
76 {
77   struct hdr *hdr;
78   struct mdesc *mdp;
79   size_t nbytes;
80
81   mdp = MD_TO_MDP (md);
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;
86   if (hdr != NULL)
87     {
88       hdr -> size = size;
89       hdr -> magic = MAGICWORD;
90       hdr++;
91       *((char *) hdr + size) = MAGICBYTE;
92     }
93   return ((void*) hdr);
94 }
95
96 static void*
97 mrealloc_check (void* md, void* ptr, size_t size)
98 {
99   struct hdr *hdr = ((struct hdr *) ptr) - 1;
100   struct mdesc *mdp;
101   size_t nbytes;
102
103   mdp = MD_TO_MDP (md);
104   checkhdr (mdp, hdr);
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;
113   if (hdr != NULL)
114     {
115       hdr -> size = size;
116       hdr++;
117       *((char *) hdr + size) = MAGICBYTE;
118     }
119   return ((void*) hdr);
120 }
121
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().
126
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
139    hooks.
140
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.
149
150    Returns non-zero if checking is successfully enabled, zero otherwise. */
151
152 int
153 mmcheckf (void *md, void (*func)(void), int force)
154 {
155   struct mdesc *mdp;
156   int rtnval;
157
158   mdp = MD_TO_MDP (md);
159
160   /* We can safely set or update the abort function at any time, regardless
161      of whether or not we successfully do anything else. */
162
163   mdp -> abortfunc = (func != NULL ? func : abort);
164
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. */
168
169   if (force ||
170       !(mdp -> flags & MMALLOC_INITIALIZED) ||
171       (mdp -> mfree_hook != NULL))
172     {
173       mdp -> mfree_hook = mfree_check;
174       mdp -> mmalloc_hook = mmalloc_check;
175       mdp -> mrealloc_hook = mrealloc_check;
176       mdp -> flags |= MMALLOC_MMCHECK_USED;
177       rtnval = 1;
178     }
179   else
180     {
181       rtnval = 0;
182     }
183
184   return (rtnval);
185 }
186
187 /* This routine is for backwards compatibility only, in case there are
188    still callers to the original mmcheck function. */
189
190 int
191 mmcheck (void *md, void (*func) (void))
192 {
193   int rtnval;
194
195   rtnval = mmcheckf (md, func, 0);
196   return (rtnval);
197 }