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