2010-01-11 Miguel de Icaza <miguel@novell.com>
[mono.git] / mono / utils / memcheck.h
1  
2 /*
3    ----------------------------------------------------------------
4
5    Notice that the following BSD-style license applies to this one
6    file (memcheck.h) only.  The rest of Valgrind is licensed under the
7    terms of the GNU General Public License, version 2, unless
8    otherwise indicated.  See the COPYING file in the source
9    distribution for details.
10
11    ----------------------------------------------------------------
12
13    This file is part of MemCheck, a heavyweight Valgrind tool for
14    detecting memory errors.
15
16    Copyright (C) 2000-2009 Julian Seward.  All rights reserved.
17
18    Redistribution and use in source and binary forms, with or without
19    modification, are permitted provided that the following conditions
20    are met:
21
22    1. Redistributions of source code must retain the above copyright
23       notice, this list of conditions and the following disclaimer.
24
25    2. The origin of this software must not be misrepresented; you must 
26       not claim that you wrote the original software.  If you use this 
27       software in a product, an acknowledgment in the product 
28       documentation would be appreciated but is not required.
29
30    3. Altered source versions must be plainly marked as such, and must
31       not be misrepresented as being the original software.
32
33    4. The name of the author may not be used to endorse or promote 
34       products derived from this software without specific prior written 
35       permission.
36
37    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
38    OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
39    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40    ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
41    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
43    GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
44    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
45    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
46    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
47    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48
49    ----------------------------------------------------------------
50
51    Notice that the above BSD-style license applies to this one file
52    (memcheck.h) only.  The entire rest of Valgrind is licensed under
53    the terms of the GNU General Public License, version 2.  See the
54    COPYING file in the source distribution for details.
55
56    ---------------------------------------------------------------- 
57 */
58
59
60 #ifndef __MEMCHECK_H
61 #define __MEMCHECK_H
62
63 #ifdef __GNUC__
64
65 /* This file is for inclusion into client (your!) code.
66
67    You can use these macros to manipulate and query memory permissions
68    inside your own programs.
69
70    See comment near the top of valgrind.h on how to use them.
71 */
72
73 #include "valgrind.h"
74
75 /* !! ABIWARNING !! ABIWARNING !! ABIWARNING !! ABIWARNING !! 
76    This enum comprises an ABI exported by Valgrind to programs
77    which use client requests.  DO NOT CHANGE THE ORDER OF THESE
78    ENTRIES, NOR DELETE ANY -- add new ones at the end. */
79 typedef
80    enum { 
81       VG_USERREQ__MAKE_MEM_NOACCESS = VG_USERREQ_TOOL_BASE('M','C'),
82       VG_USERREQ__MAKE_MEM_UNDEFINED,
83       VG_USERREQ__MAKE_MEM_DEFINED,
84       VG_USERREQ__DISCARD,
85       VG_USERREQ__CHECK_MEM_IS_ADDRESSABLE,
86       VG_USERREQ__CHECK_MEM_IS_DEFINED,
87       VG_USERREQ__DO_LEAK_CHECK,
88       VG_USERREQ__COUNT_LEAKS,
89
90       VG_USERREQ__GET_VBITS,
91       VG_USERREQ__SET_VBITS,
92
93       VG_USERREQ__CREATE_BLOCK,
94
95       VG_USERREQ__MAKE_MEM_DEFINED_IF_ADDRESSABLE,
96
97       /* Not next to VG_USERREQ__COUNT_LEAKS because it was added later. */
98       VG_USERREQ__COUNT_LEAK_BLOCKS,
99
100       /* This is just for memcheck's internal use - don't use it */
101       _VG_USERREQ__MEMCHECK_RECORD_OVERLAP_ERROR 
102          = VG_USERREQ_TOOL_BASE('M','C') + 256
103    } Vg_MemCheckClientRequest;
104
105
106
107 /* Client-code macros to manipulate the state of memory. */
108
109 /* Mark memory at _qzz_addr as unaddressable for _qzz_len bytes. */
110 #define VALGRIND_MAKE_MEM_NOACCESS(_qzz_addr,_qzz_len)           \
111    (__extension__({unsigned long _qzz_res;                       \
112     VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0 /* default return */, \
113                             VG_USERREQ__MAKE_MEM_NOACCESS,       \
114                             _qzz_addr, _qzz_len, 0, 0, 0);       \
115     _qzz_res;                                                    \
116    }))
117       
118 /* Similarly, mark memory at _qzz_addr as addressable but undefined
119    for _qzz_len bytes. */
120 #define VALGRIND_MAKE_MEM_UNDEFINED(_qzz_addr,_qzz_len)          \
121    (__extension__({unsigned long _qzz_res;                       \
122     VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0 /* default return */, \
123                             VG_USERREQ__MAKE_MEM_UNDEFINED,      \
124                             _qzz_addr, _qzz_len, 0, 0, 0);       \
125     _qzz_res;                                                    \
126    }))
127
128 /* Similarly, mark memory at _qzz_addr as addressable and defined
129    for _qzz_len bytes. */
130 #define VALGRIND_MAKE_MEM_DEFINED(_qzz_addr,_qzz_len)            \
131    (__extension__({unsigned long _qzz_res;                       \
132     VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0 /* default return */, \
133                             VG_USERREQ__MAKE_MEM_DEFINED,        \
134                             _qzz_addr, _qzz_len, 0, 0, 0);       \
135     _qzz_res;                                                    \
136    }))
137
138 /* Similar to VALGRIND_MAKE_MEM_DEFINED except that addressability is
139    not altered: bytes which are addressable are marked as defined,
140    but those which are not addressable are left unchanged. */
141 #define VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE(_qzz_addr,_qzz_len) \
142    (__extension__({unsigned long _qzz_res;                       \
143     VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0 /* default return */, \
144                             VG_USERREQ__MAKE_MEM_DEFINED_IF_ADDRESSABLE, \
145                             _qzz_addr, _qzz_len, 0, 0, 0);       \
146     _qzz_res;                                                    \
147    }))
148
149 /* Create a block-description handle.  The description is an ascii
150    string which is included in any messages pertaining to addresses
151    within the specified memory range.  Has no other effect on the
152    properties of the memory range. */
153 #define VALGRIND_CREATE_BLOCK(_qzz_addr,_qzz_len, _qzz_desc)     \
154         (__extension__({unsigned long _qzz_res;                  \
155     VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0 /* default return */, \
156                             VG_USERREQ__CREATE_BLOCK,            \
157                             _qzz_addr, _qzz_len, _qzz_desc,      \
158                             0, 0);                               \
159     _qzz_res;                                                    \
160    }))
161
162 /* Discard a block-description-handle. Returns 1 for an
163    invalid handle, 0 for a valid handle. */
164 #define VALGRIND_DISCARD(_qzz_blkindex)                          \
165    (__extension__ ({unsigned long _qzz_res;                      \
166     VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0 /* default return */, \
167                             VG_USERREQ__DISCARD,                 \
168                             0, _qzz_blkindex, 0, 0, 0);          \
169     _qzz_res;                                                    \
170    }))
171
172
173 /* Client-code macros to check the state of memory. */
174
175 /* Check that memory at _qzz_addr is addressable for _qzz_len bytes.
176    If suitable addressibility is not established, Valgrind prints an
177    error message and returns the address of the first offending byte.
178    Otherwise it returns zero. */
179 #define VALGRIND_CHECK_MEM_IS_ADDRESSABLE(_qzz_addr,_qzz_len)    \
180    (__extension__({unsigned long _qzz_res;                       \
181     VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,                      \
182                             VG_USERREQ__CHECK_MEM_IS_ADDRESSABLE,\
183                             _qzz_addr, _qzz_len, 0, 0, 0);       \
184     _qzz_res;                                                    \
185    }))
186
187 /* Check that memory at _qzz_addr is addressable and defined for
188    _qzz_len bytes.  If suitable addressibility and definedness are not
189    established, Valgrind prints an error message and returns the
190    address of the first offending byte.  Otherwise it returns zero. */
191 #define VALGRIND_CHECK_MEM_IS_DEFINED(_qzz_addr,_qzz_len)        \
192    (__extension__({unsigned long _qzz_res;                       \
193     VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,                      \
194                             VG_USERREQ__CHECK_MEM_IS_DEFINED,    \
195                             _qzz_addr, _qzz_len, 0, 0, 0);       \
196     _qzz_res;                                                    \
197    }))
198
199 /* Use this macro to force the definedness and addressibility of an
200    lvalue to be checked.  If suitable addressibility and definedness
201    are not established, Valgrind prints an error message and returns
202    the address of the first offending byte.  Otherwise it returns
203    zero. */
204 #define VALGRIND_CHECK_VALUE_IS_DEFINED(__lvalue)                \
205    VALGRIND_CHECK_MEM_IS_DEFINED(                                \
206       (volatile unsigned char *)&(__lvalue),                     \
207                       (unsigned long)(sizeof (__lvalue)))
208
209
210 /* Do a full memory leak check (like --leak-check=full) mid-execution. */
211 #define VALGRIND_DO_LEAK_CHECK                                   \
212    {unsigned long _qzz_res;                                      \
213     VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,                      \
214                             VG_USERREQ__DO_LEAK_CHECK,           \
215                             0, 0, 0, 0, 0);                      \
216    }
217
218 /* Do a summary memory leak check (like --leak-check=summary) mid-execution. */
219 #define VALGRIND_DO_QUICK_LEAK_CHECK                             \
220    {unsigned long _qzz_res;                                      \
221     VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,                      \
222                             VG_USERREQ__DO_LEAK_CHECK,           \
223                             1, 0, 0, 0, 0);                      \
224    }
225
226 /* Return number of leaked, dubious, reachable and suppressed bytes found by
227    all previous leak checks.  They must be lvalues.  */
228 #define VALGRIND_COUNT_LEAKS(leaked, dubious, reachable, suppressed)     \
229    /* For safety on 64-bit platforms we assign the results to private
230       unsigned long variables, then assign these to the lvalues the user
231       specified, which works no matter what type 'leaked', 'dubious', etc
232       are.  We also initialise '_qzz_leaked', etc because
233       VG_USERREQ__COUNT_LEAKS doesn't mark the values returned as
234       defined. */                                                        \
235    {unsigned long _qzz_res;                                              \
236     unsigned long _qzz_leaked    = 0, _qzz_dubious    = 0;               \
237     unsigned long _qzz_reachable = 0, _qzz_suppressed = 0;               \
238     VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,                              \
239                                VG_USERREQ__COUNT_LEAKS,                  \
240                                &_qzz_leaked, &_qzz_dubious,              \
241                                &_qzz_reachable, &_qzz_suppressed, 0);    \
242     leaked     = _qzz_leaked;                                            \
243     dubious    = _qzz_dubious;                                           \
244     reachable  = _qzz_reachable;                                         \
245     suppressed = _qzz_suppressed;                                        \
246    }
247
248 /* Return number of leaked, dubious, reachable and suppressed bytes found by
249    all previous leak checks.  They must be lvalues.  */
250 #define VALGRIND_COUNT_LEAK_BLOCKS(leaked, dubious, reachable, suppressed) \
251    /* For safety on 64-bit platforms we assign the results to private
252       unsigned long variables, then assign these to the lvalues the user
253       specified, which works no matter what type 'leaked', 'dubious', etc
254       are.  We also initialise '_qzz_leaked', etc because
255       VG_USERREQ__COUNT_LEAKS doesn't mark the values returned as
256       defined. */                                                        \
257    {unsigned long _qzz_res;                                              \
258     unsigned long _qzz_leaked    = 0, _qzz_dubious    = 0;               \
259     unsigned long _qzz_reachable = 0, _qzz_suppressed = 0;               \
260     VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,                              \
261                                VG_USERREQ__COUNT_LEAK_BLOCKS,            \
262                                &_qzz_leaked, &_qzz_dubious,              \
263                                &_qzz_reachable, &_qzz_suppressed, 0);    \
264     leaked     = _qzz_leaked;                                            \
265     dubious    = _qzz_dubious;                                           \
266     reachable  = _qzz_reachable;                                         \
267     suppressed = _qzz_suppressed;                                        \
268    }
269
270
271 /* Get the validity data for addresses [zza..zza+zznbytes-1] and copy it
272    into the provided zzvbits array.  Return values:
273       0   if not running on valgrind
274       1   success
275       2   [previously indicated unaligned arrays;  these are now allowed]
276       3   if any parts of zzsrc/zzvbits are not addressable.
277    The metadata is not copied in cases 0, 2 or 3 so it should be
278    impossible to segfault your system by using this call.
279 */
280 #define VALGRIND_GET_VBITS(zza,zzvbits,zznbytes)                 \
281    (__extension__({unsigned long _qzz_res;                       \
282     char* czza     = (char*)zza;                                 \
283     char* czzvbits = (char*)zzvbits;                             \
284     VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,                      \
285                             VG_USERREQ__GET_VBITS,               \
286                             czza, czzvbits, zznbytes, 0, 0 );    \
287     _qzz_res;                                                    \
288    }))
289
290 /* Set the validity data for addresses [zza..zza+zznbytes-1], copying it
291    from the provided zzvbits array.  Return values:
292       0   if not running on valgrind
293       1   success
294       2   [previously indicated unaligned arrays;  these are now allowed]
295       3   if any parts of zza/zzvbits are not addressable.
296    The metadata is not copied in cases 0, 2 or 3 so it should be
297    impossible to segfault your system by using this call.
298 */
299 #define VALGRIND_SET_VBITS(zza,zzvbits,zznbytes)                 \
300    (__extension__({unsigned int _qzz_res;                        \
301     char* czza     = (char*)zza;                                 \
302     char* czzvbits = (char*)zzvbits;                             \
303     VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,                      \
304                             VG_USERREQ__SET_VBITS,               \
305                             czza, czzvbits, zznbytes, 0, 0 );    \
306     _qzz_res;                                                    \
307    }))
308
309 #else /* __GNUC__ */
310
311 #define RUNNING_ON_VALGRIND 0
312 #define VALGRIND_DISCARD_TRANSLATIONS(x,y) do { } while (0)
313
314 #endif /* __GNUC__ */
315
316 #endif
317