iPXE
memcheck.h
Go to the documentation of this file.
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-2010 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 FILE_LICENCE ( BSD3 );
64 FILE_SECBOOT ( PERMITTED );
65 
66 
67 /* This file is for inclusion into client (your!) code.
68 
69  You can use these macros to manipulate and query memory permissions
70  inside your own programs.
71 
72  See comment near the top of valgrind.h on how to use them.
73 */
74 
75 #include "valgrind.h"
76 
77 /* !! ABIWARNING !! ABIWARNING !! ABIWARNING !! ABIWARNING !!
78  This enum comprises an ABI exported by Valgrind to programs
79  which use client requests. DO NOT CHANGE THE ORDER OF THESE
80  ENTRIES, NOR DELETE ANY -- add new ones at the end. */
81 typedef
82  enum {
91 
94 
96 
98 
99  /* Not next to VG_USERREQ__COUNT_LEAKS because it was added later. */
101 
102  /* This is just for memcheck's internal use - don't use it */
104  = VG_USERREQ_TOOL_BASE('M','C') + 256
106 
107 
108 
109 /* Client-code macros to manipulate the state of memory. */
110 
111 /* Mark memory at _qzz_addr as unaddressable for _qzz_len bytes. */
112 #define VALGRIND_MAKE_MEM_NOACCESS(_qzz_addr,_qzz_len) \
113  (__extension__({unsigned long _qzz_res; \
114  VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0 /* default return */, \
115  VG_USERREQ__MAKE_MEM_NOACCESS, \
116  _qzz_addr, _qzz_len, 0, 0, 0); \
117  _qzz_res; \
118  }))
119 
120 /* Similarly, mark memory at _qzz_addr as addressable but undefined
121  for _qzz_len bytes. */
122 #define VALGRIND_MAKE_MEM_UNDEFINED(_qzz_addr,_qzz_len) \
123  (__extension__({unsigned long _qzz_res; \
124  VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0 /* default return */, \
125  VG_USERREQ__MAKE_MEM_UNDEFINED, \
126  _qzz_addr, _qzz_len, 0, 0, 0); \
127  _qzz_res; \
128  }))
129 
130 /* Similarly, mark memory at _qzz_addr as addressable and defined
131  for _qzz_len bytes. */
132 #define VALGRIND_MAKE_MEM_DEFINED(_qzz_addr,_qzz_len) \
133  (__extension__({unsigned long _qzz_res; \
134  VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0 /* default return */, \
135  VG_USERREQ__MAKE_MEM_DEFINED, \
136  _qzz_addr, _qzz_len, 0, 0, 0); \
137  _qzz_res; \
138  }))
139 
140 /* Similar to VALGRIND_MAKE_MEM_DEFINED except that addressability is
141  not altered: bytes which are addressable are marked as defined,
142  but those which are not addressable are left unchanged. */
143 #define VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE(_qzz_addr,_qzz_len) \
144  (__extension__({unsigned long _qzz_res; \
145  VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0 /* default return */, \
146  VG_USERREQ__MAKE_MEM_DEFINED_IF_ADDRESSABLE, \
147  _qzz_addr, _qzz_len, 0, 0, 0); \
148  _qzz_res; \
149  }))
150 
151 /* Create a block-description handle. The description is an ascii
152  string which is included in any messages pertaining to addresses
153  within the specified memory range. Has no other effect on the
154  properties of the memory range. */
155 #define VALGRIND_CREATE_BLOCK(_qzz_addr,_qzz_len, _qzz_desc) \
156  (__extension__({unsigned long _qzz_res; \
157  VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0 /* default return */, \
158  VG_USERREQ__CREATE_BLOCK, \
159  _qzz_addr, _qzz_len, _qzz_desc, \
160  0, 0); \
161  _qzz_res; \
162  }))
163 
164 /* Discard a block-description-handle. Returns 1 for an
165  invalid handle, 0 for a valid handle. */
166 #define VALGRIND_DISCARD(_qzz_blkindex) \
167  (__extension__ ({unsigned long _qzz_res; \
168  VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0 /* default return */, \
169  VG_USERREQ__DISCARD, \
170  0, _qzz_blkindex, 0, 0, 0); \
171  _qzz_res; \
172  }))
173 
174 
175 /* Client-code macros to check the state of memory. */
176 
177 /* Check that memory at _qzz_addr is addressable for _qzz_len bytes.
178  If suitable addressibility is not established, Valgrind prints an
179  error message and returns the address of the first offending byte.
180  Otherwise it returns zero. */
181 #define VALGRIND_CHECK_MEM_IS_ADDRESSABLE(_qzz_addr,_qzz_len) \
182  (__extension__({unsigned long _qzz_res; \
183  VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \
184  VG_USERREQ__CHECK_MEM_IS_ADDRESSABLE,\
185  _qzz_addr, _qzz_len, 0, 0, 0); \
186  _qzz_res; \
187  }))
188 
189 /* Check that memory at _qzz_addr is addressable and defined for
190  _qzz_len bytes. If suitable addressibility and definedness are not
191  established, Valgrind prints an error message and returns the
192  address of the first offending byte. Otherwise it returns zero. */
193 #define VALGRIND_CHECK_MEM_IS_DEFINED(_qzz_addr,_qzz_len) \
194  (__extension__({unsigned long _qzz_res; \
195  VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \
196  VG_USERREQ__CHECK_MEM_IS_DEFINED, \
197  _qzz_addr, _qzz_len, 0, 0, 0); \
198  _qzz_res; \
199  }))
200 
201 /* Use this macro to force the definedness and addressibility of an
202  lvalue to be checked. If suitable addressibility and definedness
203  are not established, Valgrind prints an error message and returns
204  the address of the first offending byte. Otherwise it returns
205  zero. */
206 #define VALGRIND_CHECK_VALUE_IS_DEFINED(__lvalue) \
207  VALGRIND_CHECK_MEM_IS_DEFINED( \
208  (volatile unsigned char *)&(__lvalue), \
209  (unsigned long)(sizeof (__lvalue)))
210 
211 
212 /* Do a full memory leak check (like --leak-check=full) mid-execution. */
213 #define VALGRIND_DO_LEAK_CHECK \
214  {unsigned long _qzz_res; \
215  VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \
216  VG_USERREQ__DO_LEAK_CHECK, \
217  0, 0, 0, 0, 0); \
218  }
219 
220 /* Do a summary memory leak check (like --leak-check=summary) mid-execution. */
221 #define VALGRIND_DO_QUICK_LEAK_CHECK \
222  {unsigned long _qzz_res; \
223  VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \
224  VG_USERREQ__DO_LEAK_CHECK, \
225  1, 0, 0, 0, 0); \
226  }
227 
228 /* Return number of leaked, dubious, reachable and suppressed bytes found by
229  all previous leak checks. They must be lvalues. */
230 #define VALGRIND_COUNT_LEAKS(leaked, dubious, reachable, suppressed) \
231  /* For safety on 64-bit platforms we assign the results to private
232  unsigned long variables, then assign these to the lvalues the user
233  specified, which works no matter what type 'leaked', 'dubious', etc
234  are. We also initialise '_qzz_leaked', etc because
235  VG_USERREQ__COUNT_LEAKS doesn't mark the values returned as
236  defined. */ \
237  {unsigned long _qzz_res; \
238  unsigned long _qzz_leaked = 0, _qzz_dubious = 0; \
239  unsigned long _qzz_reachable = 0, _qzz_suppressed = 0; \
240  VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \
241  VG_USERREQ__COUNT_LEAKS, \
242  &_qzz_leaked, &_qzz_dubious, \
243  &_qzz_reachable, &_qzz_suppressed, 0); \
244  leaked = _qzz_leaked; \
245  dubious = _qzz_dubious; \
246  reachable = _qzz_reachable; \
247  suppressed = _qzz_suppressed; \
248  }
249 
250 /* Return number of leaked, dubious, reachable and suppressed bytes found by
251  all previous leak checks. They must be lvalues. */
252 #define VALGRIND_COUNT_LEAK_BLOCKS(leaked, dubious, reachable, suppressed) \
253  /* For safety on 64-bit platforms we assign the results to private
254  unsigned long variables, then assign these to the lvalues the user
255  specified, which works no matter what type 'leaked', 'dubious', etc
256  are. We also initialise '_qzz_leaked', etc because
257  VG_USERREQ__COUNT_LEAKS doesn't mark the values returned as
258  defined. */ \
259  {unsigned long _qzz_res; \
260  unsigned long _qzz_leaked = 0, _qzz_dubious = 0; \
261  unsigned long _qzz_reachable = 0, _qzz_suppressed = 0; \
262  VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \
263  VG_USERREQ__COUNT_LEAK_BLOCKS, \
264  &_qzz_leaked, &_qzz_dubious, \
265  &_qzz_reachable, &_qzz_suppressed, 0); \
266  leaked = _qzz_leaked; \
267  dubious = _qzz_dubious; \
268  reachable = _qzz_reachable; \
269  suppressed = _qzz_suppressed; \
270  }
271 
272 
273 /* Get the validity data for addresses [zza..zza+zznbytes-1] and copy it
274  into the provided zzvbits array. Return values:
275  0 if not running on valgrind
276  1 success
277  2 [previously indicated unaligned arrays; these are now allowed]
278  3 if any parts of zzsrc/zzvbits are not addressable.
279  The metadata is not copied in cases 0, 2 or 3 so it should be
280  impossible to segfault your system by using this call.
281 */
282 #define VALGRIND_GET_VBITS(zza,zzvbits,zznbytes) \
283  (__extension__({unsigned long _qzz_res; \
284  char* czza = (char*)zza; \
285  char* czzvbits = (char*)zzvbits; \
286  VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \
287  VG_USERREQ__GET_VBITS, \
288  czza, czzvbits, zznbytes, 0, 0 ); \
289  _qzz_res; \
290  }))
291 
292 /* Set the validity data for addresses [zza..zza+zznbytes-1], copying it
293  from the provided zzvbits array. Return values:
294  0 if not running on valgrind
295  1 success
296  2 [previously indicated unaligned arrays; these are now allowed]
297  3 if any parts of zza/zzvbits are not addressable.
298  The metadata is not copied in cases 0, 2 or 3 so it should be
299  impossible to segfault your system by using this call.
300 */
301 #define VALGRIND_SET_VBITS(zza,zzvbits,zznbytes) \
302  (__extension__({unsigned int _qzz_res; \
303  char* czza = (char*)zza; \
304  char* czzvbits = (char*)zzvbits; \
305  VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \
306  VG_USERREQ__SET_VBITS, \
307  czza, czzvbits, zznbytes, 0, 0 ); \
308  _qzz_res; \
309  }))
310 
311 #endif
312 
Vg_MemCheckClientRequest
Definition: memcheck.h:81
FILE_LICENCE(BSD3)
#define VG_USERREQ_TOOL_BASE(a, b)
Definition: valgrind.h:4104
FILE_SECBOOT(PERMITTED)