Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Indent include and src using this command:
[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   size_t size;                  /* Exact size requested by user.  */
38   unsigned long int magic;      /* Magic number to check header integrity.  */
39 };
40
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);
45
46 /* Check the magicword and magicbyte, and if either is corrupted then
47    call the emergency abort function specified for the heap in use. */
48
49 static void checkhdr(struct mdesc *mdp, const struct hdr *hdr)
50 {
51   if (hdr->magic != MAGICWORD ||
52       ((char *) &hdr[1])[hdr->size] != MAGICBYTE) {
53     (*mdp->abortfunc) ();
54   }
55 }
56
57 static void mfree_check(void *md, void *ptr)
58 {
59   struct hdr *hdr = ((struct hdr *) ptr) - 1;
60   struct mdesc *mdp;
61
62   mdp = MD_TO_MDP(md);
63   checkhdr(mdp, hdr);
64   hdr->magic = MAGICWORDFREE;
65   mdp->mfree_hook = NULL;
66   mfree(md, (void *) hdr);
67   mdp->mfree_hook = mfree_check;
68 }
69
70 static void *mmalloc_check(void *md, size_t size)
71 {
72   struct hdr *hdr;
73   struct mdesc *mdp;
74   size_t nbytes;
75
76   mdp = MD_TO_MDP(md);
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;
81   if (hdr != NULL) {
82     hdr->size = size;
83     hdr->magic = MAGICWORD;
84     hdr++;
85     *((char *) hdr + size) = MAGICBYTE;
86   }
87   return ((void *) hdr);
88 }
89
90 static void *mrealloc_check(void *md, void *ptr, size_t size)
91 {
92   struct hdr *hdr = ((struct hdr *) ptr) - 1;
93   struct mdesc *mdp;
94   size_t nbytes;
95
96   mdp = MD_TO_MDP(md);
97   checkhdr(mdp, hdr);
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;
106   if (hdr != NULL) {
107     hdr->size = size;
108     hdr++;
109     *((char *) hdr + size) = MAGICBYTE;
110   }
111   return ((void *) hdr);
112 }
113
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().
118
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
131    hooks.
132
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.
141
142    Returns non-zero if checking is successfully enabled, zero otherwise. */
143
144 int mmcheckf(void *md, void (*func) (void), int force)
145 {
146   struct mdesc *mdp;
147   int rtnval;
148
149   mdp = MD_TO_MDP(md);
150
151   /* We can safely set or update the abort function at any time, regardless
152      of whether or not we successfully do anything else. */
153
154   mdp->abortfunc = (func != NULL ? func : abort);
155
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. */
159
160   if (force ||
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;
166     rtnval = 1;
167   } else {
168     rtnval = 0;
169   }
170
171   return (rtnval);
172 }
173
174 /* This routine is for backwards compatibility only, in case there are
175    still callers to the original mmcheck function. */
176
177 int mmcheck(void *md, void (*func) (void))
178 {
179   int rtnval;
180
181   rtnval = mmcheckf(md, func, 0);
182   return (rtnval);
183 }