blob: e817c1377e564f0e8a7173a77fc6a6c973a7759a [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
sewardja81709d2002-12-28 12:55:48 +00006 file (memcheck.h) only. The entire rest of Valgrind is licensed
njn25e49d8e72002-09-23 09:36:25 +00007 under the terms of the GNU General Public License, version 2. See
8 the COPYING file in the source distribution for details.
9
10 ----------------------------------------------------------------
11
nethercote137bc552003-11-14 17:47:54 +000012 This file is part of MemCheck, a heavyweight Valgrind tool for
njnc9539842002-10-02 13:26:35 +000013 detecting memory errors.
njn25e49d8e72002-09-23 09:36:25 +000014
nethercotebb1c9912004-01-04 16:43:23 +000015 Copyright (C) 2000-2004 Julian Seward. All rights reserved.
njn25e49d8e72002-09-23 09:36:25 +000016
17 Redistribution and use in source and binary forms, with or without
18 modification, are permitted provided that the following conditions
19 are met:
20
21 1. Redistributions of source code must retain the above copyright
22 notice, this list of conditions and the following disclaimer.
23
24 2. The origin of this software must not be misrepresented; you must
25 not claim that you wrote the original software. If you use this
26 software in a product, an acknowledgment in the product
27 documentation would be appreciated but is not required.
28
29 3. Altered source versions must be plainly marked as such, and must
30 not be misrepresented as being the original software.
31
32 4. The name of the author may not be used to endorse or promote
33 products derived from this software without specific prior written
34 permission.
35
36 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
37 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
38 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
40 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
42 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
43 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
45 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
46 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47
48 ----------------------------------------------------------------
49
50 Notice that the above BSD-style license applies to this one file
sewardja81709d2002-12-28 12:55:48 +000051 (memcheck.h) only. The entire rest of Valgrind is licensed under
njn25e49d8e72002-09-23 09:36:25 +000052 the terms of the GNU General Public License, version 2. See the
53 COPYING file in the source distribution for details.
54
55 ----------------------------------------------------------------
56*/
57
58
njn25cac76cb2002-09-23 11:21:57 +000059#ifndef __MEMCHECK_H
60#define __MEMCHECK_H
njn25e49d8e72002-09-23 09:36:25 +000061
62
63/* This file is for inclusion into client (your!) code.
64
65 You can use these macros to manipulate and query memory permissions
66 inside your own programs.
67
68 See comment near the top of valgrind.h on how to use them.
69*/
70
71#include "valgrind.h"
72
73typedef
74 enum {
sewardj34042512002-10-22 04:14:35 +000075 VG_USERREQ__MAKE_NOACCESS = VG_USERREQ_SKIN_BASE('M','C'),
njn25e49d8e72002-09-23 09:36:25 +000076 VG_USERREQ__MAKE_WRITABLE,
77 VG_USERREQ__MAKE_READABLE,
78 VG_USERREQ__DISCARD,
79 VG_USERREQ__CHECK_WRITABLE,
80 VG_USERREQ__CHECK_READABLE,
njn47363ab2003-04-21 13:24:40 +000081 VG_USERREQ__DO_LEAK_CHECK,
njn10785452003-05-20 16:38:24 +000082 VG_USERREQ__COUNT_LEAKS,
njnd7994182003-10-02 13:44:04 +000083
84 /* These two have been moved into core, because they are useful for
85 any skin that tracks heap blocks. Hence the suffix. But they're
86 still here for backwards compatibility, although Valgrind will
87 abort with an explanatory message if you use them. */
88 VG_USERREQ__MALLOCLIKE_BLOCK__OLD_DO_NOT_USE,
89 VG_USERREQ__FREELIKE_BLOCK__OLD_DO_NOT_USE,
90
sewardjee070842003-07-05 17:53:55 +000091 VG_USERREQ__GET_VBITS,
fitzhardinge98abfc72003-12-16 02:05:15 +000092 VG_USERREQ__SET_VBITS,
93
94 /* This is just for memcheck's internal use - don't use it */
95 _VG_USERREQ__MEMCHECK_GET_RECORD_OVERLAP = VG_USERREQ_SKIN_BASE('M','C')+256,
njn25e49d8e72002-09-23 09:36:25 +000096 } Vg_MemCheckClientRequest;
97
98
99
100/* Client-code macros to manipulate the state of memory. */
101
102/* Mark memory at _qzz_addr as unaddressible and undefined for
103 _qzz_len bytes. Returns an int handle pertaining to the block
104 descriptions Valgrind will use in subsequent error messages. */
105#define VALGRIND_MAKE_NOACCESS(_qzz_addr,_qzz_len) \
sewardj34a257c2003-03-15 19:20:52 +0000106 (__extension__({unsigned int _qzz_res; \
njn25e49d8e72002-09-23 09:36:25 +0000107 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0 /* default return */, \
108 VG_USERREQ__MAKE_NOACCESS, \
109 _qzz_addr, _qzz_len, 0, 0); \
110 _qzz_res; \
sewardj34a257c2003-03-15 19:20:52 +0000111 }))
njn25e49d8e72002-09-23 09:36:25 +0000112
113/* Similarly, mark memory at _qzz_addr as addressible but undefined
114 for _qzz_len bytes. */
115#define VALGRIND_MAKE_WRITABLE(_qzz_addr,_qzz_len) \
sewardj34a257c2003-03-15 19:20:52 +0000116 (__extension__({unsigned int _qzz_res; \
njn25e49d8e72002-09-23 09:36:25 +0000117 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0 /* default return */, \
118 VG_USERREQ__MAKE_WRITABLE, \
119 _qzz_addr, _qzz_len, 0, 0); \
120 _qzz_res; \
sewardj34a257c2003-03-15 19:20:52 +0000121 }))
njn25e49d8e72002-09-23 09:36:25 +0000122
123/* Similarly, mark memory at _qzz_addr as addressible and defined
124 for _qzz_len bytes. */
125#define VALGRIND_MAKE_READABLE(_qzz_addr,_qzz_len) \
sewardj34a257c2003-03-15 19:20:52 +0000126 (__extension__({unsigned int _qzz_res; \
njn25e49d8e72002-09-23 09:36:25 +0000127 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0 /* default return */, \
128 VG_USERREQ__MAKE_READABLE, \
129 _qzz_addr, _qzz_len, 0, 0); \
130 _qzz_res; \
sewardj34a257c2003-03-15 19:20:52 +0000131 }))
njn25e49d8e72002-09-23 09:36:25 +0000132
133/* Discard a block-description-handle obtained from the above three
134 macros. After this, Valgrind will no longer be able to relate
135 addressing errors to the user-defined block associated with the
136 handle. The permissions settings associated with the handle remain
137 in place. Returns 1 for an invalid handle, 0 for a valid
138 handle. */
139#define VALGRIND_DISCARD(_qzz_blkindex) \
sewardj34a257c2003-03-15 19:20:52 +0000140 (__extension__ ({unsigned int _qzz_res; \
njn25e49d8e72002-09-23 09:36:25 +0000141 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0 /* default return */, \
142 VG_USERREQ__DISCARD, \
143 0, _qzz_blkindex, 0, 0); \
144 _qzz_res; \
sewardj34a257c2003-03-15 19:20:52 +0000145 }))
njn25e49d8e72002-09-23 09:36:25 +0000146
147
148/* Client-code macros to check the state of memory. */
149
150/* Check that memory at _qzz_addr is addressible for _qzz_len bytes.
151 If suitable addressibility is not established, Valgrind prints an
152 error message and returns the address of the first offending byte.
153 Otherwise it returns zero. */
154#define VALGRIND_CHECK_WRITABLE(_qzz_addr,_qzz_len) \
sewardj34a257c2003-03-15 19:20:52 +0000155 (__extension__({unsigned int _qzz_res; \
njn25e49d8e72002-09-23 09:36:25 +0000156 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \
157 VG_USERREQ__CHECK_WRITABLE, \
158 _qzz_addr, _qzz_len, 0, 0); \
159 _qzz_res; \
sewardj34a257c2003-03-15 19:20:52 +0000160 }))
njn25e49d8e72002-09-23 09:36:25 +0000161
162/* Check that memory at _qzz_addr is addressible and defined for
163 _qzz_len bytes. If suitable addressibility and definedness are not
164 established, Valgrind prints an error message and returns the
165 address of the first offending byte. Otherwise it returns zero. */
166#define VALGRIND_CHECK_READABLE(_qzz_addr,_qzz_len) \
sewardj34a257c2003-03-15 19:20:52 +0000167 (__extension__({unsigned int _qzz_res; \
njn25e49d8e72002-09-23 09:36:25 +0000168 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \
169 VG_USERREQ__CHECK_READABLE, \
170 _qzz_addr, _qzz_len, 0, 0); \
171 _qzz_res; \
sewardj34a257c2003-03-15 19:20:52 +0000172 }))
njn25e49d8e72002-09-23 09:36:25 +0000173
174/* Use this macro to force the definedness and addressibility of a
175 value to be checked. If suitable addressibility and definedness
176 are not established, Valgrind prints an error message and returns
177 the address of the first offending byte. Otherwise it returns
178 zero. */
179#define VALGRIND_CHECK_DEFINED(__lvalue) \
180 (void) \
181 VALGRIND_CHECK_READABLE( \
182 (volatile unsigned char *)&(__lvalue), \
183 (unsigned int)(sizeof (__lvalue)))
184
njn6e69ecd2003-10-02 13:25:02 +0000185/* Do a memory leak check mid-execution. */
186#define VALGRIND_DO_LEAK_CHECK \
187 {unsigned int _qzz_res; \
188 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \
189 VG_USERREQ__DO_LEAK_CHECK, \
190 0, 0, 0, 0); \
191 }
192
193/* Return number of leaked, dubious, reachable and suppressed bytes found by
194 all previous leak checks. They must be lvalues. */
195#define VALGRIND_COUNT_LEAKS(leaked, dubious, reachable, suppressed) \
196 {unsigned int _qzz_res; \
197 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \
198 VG_USERREQ__COUNT_LEAKS, \
199 &leaked, &dubious, &reachable, &suppressed);\
200 }
201
njn6e69ecd2003-10-02 13:25:02 +0000202
njnd7994182003-10-02 13:44:04 +0000203/* These two have been moved to valgrind.h; still here so that a warning can
204 be printed out for any programs using the old ones. */
205#define VALGRIND_MALLOCLIKE_BLOCK__OLD_DO_NOT_USE(addr, sizeB, rzB, is_zeroed)\
njn10785452003-05-20 16:38:24 +0000206 {unsigned int _qzz_res; \
207 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \
208 VG_USERREQ__MALLOCLIKE_BLOCK, \
209 addr, sizeB, rzB, is_zeroed); \
210 }
njnd7994182003-10-02 13:44:04 +0000211#define VALGRIND_FREELIKE_BLOCK__OLD_DO_NOT_USE(addr, rzB) \
njn10785452003-05-20 16:38:24 +0000212 {unsigned int _qzz_res; \
213 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \
214 VG_USERREQ__FREELIKE_BLOCK, \
215 addr, rzB, 0, 0); \
216 }
217
njnd7994182003-10-02 13:44:04 +0000218
sewardjee070842003-07-05 17:53:55 +0000219/* Get in zzvbits the validity data for the zznbytes starting at
220 zzsrc. Return values:
221 0 if not running on valgrind
222 1 success
223 2 if zzsrc/zzvbits arrays are not aligned 0 % 4, or
224 zznbytes is not 0 % 4.
225 3 if any parts of zzsrc/zzvbits are not addressible.
226 The metadata is not copied in cases 0, 2 or 3 so it should be
227 impossible to segfault your system by using this call.
228*/
229#define VALGRIND_GET_VBITS(zzsrc,zzvbits,zznbytes) \
230 (__extension__({unsigned int _qzz_res; \
231 char* czzsrc = (char*)zzsrc; \
232 char* czzvbits = (char*)zzvbits; \
233 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \
234 VG_USERREQ__GET_VBITS, \
235 czzsrc, czzvbits, zznbytes,0 ); \
236 _qzz_res; \
237 }))
238
239/* Apply the validity data in zzvbits to the zznbytes starting at
240 zzdst. Return values:
241 0 if not running on valgrind
242 1 success
243 2 if zzdst/zzvbits arrays are not aligned 0 % 4, or
244 zznbytes is not 0 % 4.
245 3 if any parts of zzdst/zzvbits are not addressible.
246 The metadata is not copied in cases 0, 2 or 3 so it should be
247 impossible to segfault your system by using this call.
248*/
249#define VALGRIND_SET_VBITS(zzdst,zzvbits,zznbytes) \
250 (__extension__({unsigned int _qzz_res; \
251 char* czzdst = (char*)zzdst; \
252 char* czzvbits = (char*)zzvbits; \
253 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \
254 VG_USERREQ__SET_VBITS, \
255 czzdst, czzvbits, zznbytes,0 ); \
256 _qzz_res; \
257 }))
njnd7994182003-10-02 13:44:04 +0000258
259#endif
260