blob: 262908640f3f3901b64ab2d8d2370b8a20430f81 [file] [log] [blame]
njn25e49d8e72002-09-23 09:36:25 +00001
2/*
3 ----------------------------------------------------------------
4
5 Notice that the following BSD-style license applies to this one
njn7fd15d62006-03-31 12:05:04 +00006 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.
njn25e49d8e72002-09-23 09:36:25 +000010
11 ----------------------------------------------------------------
12
nethercote137bc552003-11-14 17:47:54 +000013 This file is part of MemCheck, a heavyweight Valgrind tool for
njnc9539842002-10-02 13:26:35 +000014 detecting memory errors.
njn25e49d8e72002-09-23 09:36:25 +000015
sewardje4b0bf02006-06-05 23:21:15 +000016 Copyright (C) 2000-2006 Julian Seward. All rights reserved.
njn25e49d8e72002-09-23 09:36:25 +000017
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
sewardja81709d2002-12-28 12:55:48 +000052 (memcheck.h) only. The entire rest of Valgrind is licensed under
njn25e49d8e72002-09-23 09:36:25 +000053 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
njn25cac76cb2002-09-23 11:21:57 +000060#ifndef __MEMCHECK_H
61#define __MEMCHECK_H
njn25e49d8e72002-09-23 09:36:25 +000062
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
sewardjfb1e9ad2006-03-10 13:41:58 +000074/* !! 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
sewardj0928fed2006-03-11 12:58:03 +000077 ENTRIES, NOR DELETE ANY -- add new ones at the end. */
njn25e49d8e72002-09-23 09:36:25 +000078typedef
79 enum {
njndbf7ca72006-03-31 11:57:59 +000080 VG_USERREQ__MAKE_MEM_NOACCESS = VG_USERREQ_TOOL_BASE('M','C'),
81 VG_USERREQ__MAKE_MEM_UNDEFINED,
82 VG_USERREQ__MAKE_MEM_DEFINED,
njn25e49d8e72002-09-23 09:36:25 +000083 VG_USERREQ__DISCARD,
njndbf7ca72006-03-31 11:57:59 +000084 VG_USERREQ__CHECK_MEM_IS_ADDRESSABLE,
85 VG_USERREQ__CHECK_MEM_IS_DEFINED,
njn47363ab2003-04-21 13:24:40 +000086 VG_USERREQ__DO_LEAK_CHECK,
njn10785452003-05-20 16:38:24 +000087 VG_USERREQ__COUNT_LEAKS,
njnd7994182003-10-02 13:44:04 +000088
sewardjee070842003-07-05 17:53:55 +000089 VG_USERREQ__GET_VBITS,
fitzhardinge98abfc72003-12-16 02:05:15 +000090 VG_USERREQ__SET_VBITS,
91
sewardjb5f6f512005-03-10 23:59:00 +000092 VG_USERREQ__CREATE_BLOCK,
93
njndbf7ca72006-03-31 11:57:59 +000094 VG_USERREQ__MAKE_MEM_DEFINED_IF_ADDRESSABLE,
sewardjfb1e9ad2006-03-10 13:41:58 +000095
fitzhardinge98abfc72003-12-16 02:05:15 +000096 /* This is just for memcheck's internal use - don't use it */
njn1f8b3e72005-03-22 04:27:14 +000097 _VG_USERREQ__MEMCHECK_RECORD_OVERLAP_ERROR
98 = VG_USERREQ_TOOL_BASE('M','C') + 256
njn25e49d8e72002-09-23 09:36:25 +000099 } Vg_MemCheckClientRequest;
100
101
102
103/* Client-code macros to manipulate the state of memory. */
104
njndbf7ca72006-03-31 11:57:59 +0000105/* Mark memory at _qzz_addr as unaddressable for _qzz_len bytes. */
106#define VALGRIND_MAKE_MEM_NOACCESS(_qzz_addr,_qzz_len) \
sewardj34a257c2003-03-15 19:20:52 +0000107 (__extension__({unsigned int _qzz_res; \
sewardj0ec07f32006-01-12 12:32:32 +0000108 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0 /* default return */, \
njndbf7ca72006-03-31 11:57:59 +0000109 VG_USERREQ__MAKE_MEM_NOACCESS, \
sewardj9af10a12006-02-01 14:59:42 +0000110 _qzz_addr, _qzz_len, 0, 0, 0); \
njn25e49d8e72002-09-23 09:36:25 +0000111 _qzz_res; \
sewardj34a257c2003-03-15 19:20:52 +0000112 }))
njn25e49d8e72002-09-23 09:36:25 +0000113
njndbf7ca72006-03-31 11:57:59 +0000114/* Similarly, mark memory at _qzz_addr as addressable but undefined
njn25e49d8e72002-09-23 09:36:25 +0000115 for _qzz_len bytes. */
njndbf7ca72006-03-31 11:57:59 +0000116#define VALGRIND_MAKE_MEM_UNDEFINED(_qzz_addr,_qzz_len) \
117 (__extension__({unsigned int _qzz_res; \
118 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0 /* default return */, \
119 VG_USERREQ__MAKE_MEM_UNDEFINED, \
120 _qzz_addr, _qzz_len, 0, 0, 0); \
121 _qzz_res; \
122 }))
123
124/* Similarly, mark memory at _qzz_addr as addressable and defined
125 for _qzz_len bytes. */
126#define VALGRIND_MAKE_MEM_DEFINED(_qzz_addr,_qzz_len) \
127 (__extension__({unsigned int _qzz_res; \
128 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0 /* default return */, \
129 VG_USERREQ__MAKE_MEM_DEFINED, \
130 _qzz_addr, _qzz_len, 0, 0, 0); \
131 _qzz_res; \
132 }))
133
134/* This is the old name for VALGRIND_MAKE_MEM_NOACCESS. Deprecated. */
135#define VALGRIND_MAKE_NOACCESS(_qzz_addr,_qzz_len) \
136 VALGRIND_MAKE_MEM_NOACCESS(_qzz_addr,_qzz_len)
137
138/* This is the old name for VALGRIND_MAKE_MEM_UNDEFINED. Deprecated. */
njn25e49d8e72002-09-23 09:36:25 +0000139#define VALGRIND_MAKE_WRITABLE(_qzz_addr,_qzz_len) \
njndbf7ca72006-03-31 11:57:59 +0000140 VALGRIND_MAKE_MEM_UNDEFINED(_qzz_addr,_qzz_len)
njn25e49d8e72002-09-23 09:36:25 +0000141
njndbf7ca72006-03-31 11:57:59 +0000142/* This is the old name for VALGRIND_MAKE_MEM_DEFINED. Deprecated. */
njn25e49d8e72002-09-23 09:36:25 +0000143#define VALGRIND_MAKE_READABLE(_qzz_addr,_qzz_len) \
njndbf7ca72006-03-31 11:57:59 +0000144 VALGRIND_MAKE_MEM_DEFINED(_qzz_addr,_qzz_len)
njn25e49d8e72002-09-23 09:36:25 +0000145
njndbf7ca72006-03-31 11:57:59 +0000146/* Similar to VALGRIND_MAKE_MEM_DEFINED except that addressability is
147 not altered: bytes which are addressable are marked as defined,
148 but those which are not addressable are left unchanged. */
149#define VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE(_qzz_addr,_qzz_len) \
sewardjfb1e9ad2006-03-10 13:41:58 +0000150 (__extension__({unsigned int _qzz_res; \
151 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0 /* default return */, \
njndbf7ca72006-03-31 11:57:59 +0000152 VG_USERREQ__MAKE_MEM_DEFINED_IF_ADDRESSABLE, \
sewardjfb1e9ad2006-03-10 13:41:58 +0000153 _qzz_addr, _qzz_len, 0, 0, 0); \
154 _qzz_res; \
155 }))
156
sewardjb5f6f512005-03-10 23:59:00 +0000157/* Create a block-description handle. The description is an ascii
158 string which is included in any messages pertaining to addresses
159 within the specified memory range. Has no other effect on the
160 properties of the memory range. */
sewardj0ec07f32006-01-12 12:32:32 +0000161#define VALGRIND_CREATE_BLOCK(_qzz_addr,_qzz_len, _qzz_desc) \
162 (__extension__({unsigned int _qzz_res; \
163 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0 /* default return */, \
164 VG_USERREQ__CREATE_BLOCK, \
sewardj9af10a12006-02-01 14:59:42 +0000165 _qzz_addr, _qzz_len, _qzz_desc, \
166 0, 0); \
sewardj0ec07f32006-01-12 12:32:32 +0000167 _qzz_res; \
sewardjb5f6f512005-03-10 23:59:00 +0000168 }))
169
170/* Discard a block-description-handle. Returns 1 for an
171 invalid handle, 0 for a valid handle. */
njn25e49d8e72002-09-23 09:36:25 +0000172#define VALGRIND_DISCARD(_qzz_blkindex) \
sewardj34a257c2003-03-15 19:20:52 +0000173 (__extension__ ({unsigned int _qzz_res; \
sewardj0ec07f32006-01-12 12:32:32 +0000174 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0 /* default return */, \
njn25e49d8e72002-09-23 09:36:25 +0000175 VG_USERREQ__DISCARD, \
sewardj9af10a12006-02-01 14:59:42 +0000176 0, _qzz_blkindex, 0, 0, 0); \
njn25e49d8e72002-09-23 09:36:25 +0000177 _qzz_res; \
sewardj34a257c2003-03-15 19:20:52 +0000178 }))
njn25e49d8e72002-09-23 09:36:25 +0000179
180
181/* Client-code macros to check the state of memory. */
182
njndbf7ca72006-03-31 11:57:59 +0000183/* Check that memory at _qzz_addr is addressable for _qzz_len bytes.
njn25e49d8e72002-09-23 09:36:25 +0000184 If suitable addressibility is not established, Valgrind prints an
185 error message and returns the address of the first offending byte.
186 Otherwise it returns zero. */
njndbf7ca72006-03-31 11:57:59 +0000187#define VALGRIND_CHECK_MEM_IS_ADDRESSABLE(_qzz_addr,_qzz_len) \
sewardj0ec07f32006-01-12 12:32:32 +0000188 (__extension__({unsigned int _qzz_res; \
189 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \
njndbf7ca72006-03-31 11:57:59 +0000190 VG_USERREQ__CHECK_MEM_IS_ADDRESSABLE,\
sewardj9af10a12006-02-01 14:59:42 +0000191 _qzz_addr, _qzz_len, 0, 0, 0); \
sewardj0ec07f32006-01-12 12:32:32 +0000192 _qzz_res; \
sewardj34a257c2003-03-15 19:20:52 +0000193 }))
njn25e49d8e72002-09-23 09:36:25 +0000194
njndbf7ca72006-03-31 11:57:59 +0000195/* Check that memory at _qzz_addr is addressable and defined for
njn25e49d8e72002-09-23 09:36:25 +0000196 _qzz_len bytes. If suitable addressibility and definedness are not
197 established, Valgrind prints an error message and returns the
198 address of the first offending byte. Otherwise it returns zero. */
njndbf7ca72006-03-31 11:57:59 +0000199#define VALGRIND_CHECK_MEM_IS_DEFINED(_qzz_addr,_qzz_len) \
sewardj0ec07f32006-01-12 12:32:32 +0000200 (__extension__({unsigned int _qzz_res; \
201 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \
njndbf7ca72006-03-31 11:57:59 +0000202 VG_USERREQ__CHECK_MEM_IS_DEFINED, \
sewardj9af10a12006-02-01 14:59:42 +0000203 _qzz_addr, _qzz_len, 0, 0, 0); \
sewardj0ec07f32006-01-12 12:32:32 +0000204 _qzz_res; \
sewardj34a257c2003-03-15 19:20:52 +0000205 }))
njn25e49d8e72002-09-23 09:36:25 +0000206
njndbf7ca72006-03-31 11:57:59 +0000207/* Use this macro to force the definedness and addressibility of an
208 lvalue to be checked. If suitable addressibility and definedness
njn25e49d8e72002-09-23 09:36:25 +0000209 are not established, Valgrind prints an error message and returns
210 the address of the first offending byte. Otherwise it returns
211 zero. */
njndbf7ca72006-03-31 11:57:59 +0000212#define VALGRIND_CHECK_VALUE_IS_DEFINED(__lvalue) \
213 VALGRIND_CHECK_MEM_IS_DEFINED( \
sewardj0ec07f32006-01-12 12:32:32 +0000214 (volatile unsigned char *)&(__lvalue), \
njn25e49d8e72002-09-23 09:36:25 +0000215 (unsigned int)(sizeof (__lvalue)))
216
njndbf7ca72006-03-31 11:57:59 +0000217/* This is the old name for VALGRIND_CHECK_MEM_IS_ADDRESSABLE. Deprecated. */
218#define VALGRIND_CHECK_WRITABLE(_qzz_addr,_qzz_len) \
219 VALGRIND_CHECK_MEM_IS_ADDRESSABLE(_qzz_addr,_qzz_len)
220
221/* This is the old name for VALGRIND_CHECK_MEM_IS_DEFINED. Deprecated. */
222#define VALGRIND_CHECK_READABLE(_qzz_addr,_qzz_len) \
223 VALGRIND_CHECK_MEM_IS_DEFINED(_qzz_addr,_qzz_len)
224
225/* This is the old name for VALGRIND_CHECK_VALUE_IS_DEFINED. Deprecated. */
226#define VALGRIND_CHECK_DEFINED(__lvalue) \
227 VALGRIND_CHECK_VALUE_IS_DEFINED(__lvalue)
228
229
njn6e69ecd2003-10-02 13:25:02 +0000230/* Do a memory leak check mid-execution. */
sewardj0ec07f32006-01-12 12:32:32 +0000231#define VALGRIND_DO_LEAK_CHECK \
232 {unsigned int _qzz_res; \
233 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \
234 VG_USERREQ__DO_LEAK_CHECK, \
sewardj9af10a12006-02-01 14:59:42 +0000235 0, 0, 0, 0, 0); \
njn6e69ecd2003-10-02 13:25:02 +0000236 }
237
sewardjb5f6f512005-03-10 23:59:00 +0000238/* Just display summaries of leaked memory, rather than all the
239 details */
sewardj0ec07f32006-01-12 12:32:32 +0000240#define VALGRIND_DO_QUICK_LEAK_CHECK \
241 {unsigned int _qzz_res; \
242 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \
243 VG_USERREQ__DO_LEAK_CHECK, \
sewardj9af10a12006-02-01 14:59:42 +0000244 1, 0, 0, 0, 0); \
sewardjb5f6f512005-03-10 23:59:00 +0000245 }
246
njn6e69ecd2003-10-02 13:25:02 +0000247/* Return number of leaked, dubious, reachable and suppressed bytes found by
njn36c11e92006-04-06 14:21:42 +0000248 all previous leak checks. They must be lvalues. */
sewardj0ec07f32006-01-12 12:32:32 +0000249#define VALGRIND_COUNT_LEAKS(leaked, dubious, reachable, suppressed) \
njn36c11e92006-04-06 14:21:42 +0000250 /* For safety on 64-bit platforms we assign the results to private
251 unsigned long variables, then assign these to the lvalues the user
252 specified, which works no matter what type 'leaked', 'dubious', etc
253 are. We also initialise '_qzz_leaked', etc because
254 VG_USERREQ__COUNT_LEAKS doesn't mark the values returned as
255 initialised. */ \
256 {unsigned int _qzz_res; \
257 unsigned long _qzz_leaked = 0, _qzz_dubious = 0; \
258 unsigned long _qzz_reachable = 0, _qzz_suppressed = 0; \
sewardj0ec07f32006-01-12 12:32:32 +0000259 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \
njn36c11e92006-04-06 14:21:42 +0000260 VG_USERREQ__COUNT_LEAKS, \
261 &_qzz_leaked, &_qzz_dubious, \
262 &_qzz_reachable, &_qzz_suppressed, 0); \
263 leaked = _qzz_leaked; \
264 dubious = _qzz_dubious; \
265 reachable = _qzz_reachable; \
266 suppressed = _qzz_suppressed; \
njn6e69ecd2003-10-02 13:25:02 +0000267 }
268
njn6e69ecd2003-10-02 13:25:02 +0000269
njn1d0825f2006-03-27 11:37:07 +0000270/* Get the validity data for addresses [zza..zza+zznbytes-1] and copy it
271 into the provided zzvbits array. Return values:
sewardjee070842003-07-05 17:53:55 +0000272 0 if not running on valgrind
273 1 success
njn1d0825f2006-03-27 11:37:07 +0000274 2 [previously indicated unaligned arrays; these are now allowed]
njndbf7ca72006-03-31 11:57:59 +0000275 3 if any parts of zzsrc/zzvbits are not addressable.
sewardjee070842003-07-05 17:53:55 +0000276 The metadata is not copied in cases 0, 2 or 3 so it should be
277 impossible to segfault your system by using this call.
278*/
njn1d0825f2006-03-27 11:37:07 +0000279#define VALGRIND_GET_VBITS(zza,zzvbits,zznbytes) \
sewardjee070842003-07-05 17:53:55 +0000280 (__extension__({unsigned int _qzz_res; \
njn1d0825f2006-03-27 11:37:07 +0000281 char* czza = (char*)zza; \
sewardjee070842003-07-05 17:53:55 +0000282 char* czzvbits = (char*)zzvbits; \
sewardj0ec07f32006-01-12 12:32:32 +0000283 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \
sewardjee070842003-07-05 17:53:55 +0000284 VG_USERREQ__GET_VBITS, \
njn1d0825f2006-03-27 11:37:07 +0000285 czza, czzvbits, zznbytes, 0, 0 ); \
sewardjee070842003-07-05 17:53:55 +0000286 _qzz_res; \
287 }))
288
njn1d0825f2006-03-27 11:37:07 +0000289/* Set the validity data for addresses [zza..zza+zznbytes-1], copying it
290 from the provided zzvbits array. Return values:
sewardjee070842003-07-05 17:53:55 +0000291 0 if not running on valgrind
292 1 success
njn1d0825f2006-03-27 11:37:07 +0000293 2 [previously indicated unaligned arrays; these are now allowed]
njndbf7ca72006-03-31 11:57:59 +0000294 3 if any parts of zza/zzvbits are not addressable.
sewardjee070842003-07-05 17:53:55 +0000295 The metadata is not copied in cases 0, 2 or 3 so it should be
296 impossible to segfault your system by using this call.
297*/
njn1d0825f2006-03-27 11:37:07 +0000298#define VALGRIND_SET_VBITS(zza,zzvbits,zznbytes) \
sewardjee070842003-07-05 17:53:55 +0000299 (__extension__({unsigned int _qzz_res; \
njn1d0825f2006-03-27 11:37:07 +0000300 char* czza = (char*)zza; \
sewardjee070842003-07-05 17:53:55 +0000301 char* czzvbits = (char*)zzvbits; \
sewardj0ec07f32006-01-12 12:32:32 +0000302 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \
sewardjee070842003-07-05 17:53:55 +0000303 VG_USERREQ__SET_VBITS, \
njn1d0825f2006-03-27 11:37:07 +0000304 czza, czzvbits, zznbytes, 0, 0 ); \
sewardjee070842003-07-05 17:53:55 +0000305 _qzz_res; \
306 }))
njnd7994182003-10-02 13:44:04 +0000307
308#endif
309