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