blob: 151797a9f55bdd9425101f01da69575701048d21 [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
njnc9539842002-10-02 13:26:35 +000012 This file is part of MemCheck, a heavyweight Valgrind skin for
13 detecting memory errors.
njn25e49d8e72002-09-23 09:36:25 +000014
15 Copyright (C) 2000-2002 Julian Seward. All rights reserved.
16
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
sewardj6591dc22003-03-15 19:12:43 +000071#define __VALGRIND_SOMESKIN_H
njn25e49d8e72002-09-23 09:36:25 +000072#include "valgrind.h"
73
74typedef
75 enum {
sewardj34042512002-10-22 04:14:35 +000076 VG_USERREQ__MAKE_NOACCESS = VG_USERREQ_SKIN_BASE('M','C'),
njn25e49d8e72002-09-23 09:36:25 +000077 VG_USERREQ__MAKE_WRITABLE,
78 VG_USERREQ__MAKE_READABLE,
79 VG_USERREQ__DISCARD,
80 VG_USERREQ__CHECK_WRITABLE,
81 VG_USERREQ__CHECK_READABLE,
njn25e49d8e72002-09-23 09:36:25 +000082 VG_USERREQ__DO_LEAK_CHECK, /* untested */
83 } Vg_MemCheckClientRequest;
84
85
86
87/* Client-code macros to manipulate the state of memory. */
88
89/* Mark memory at _qzz_addr as unaddressible and undefined for
90 _qzz_len bytes. Returns an int handle pertaining to the block
91 descriptions Valgrind will use in subsequent error messages. */
92#define VALGRIND_MAKE_NOACCESS(_qzz_addr,_qzz_len) \
93 ({unsigned int _qzz_res; \
94 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0 /* default return */, \
95 VG_USERREQ__MAKE_NOACCESS, \
96 _qzz_addr, _qzz_len, 0, 0); \
97 _qzz_res; \
98 })
99
100/* Similarly, mark memory at _qzz_addr as addressible but undefined
101 for _qzz_len bytes. */
102#define VALGRIND_MAKE_WRITABLE(_qzz_addr,_qzz_len) \
103 ({unsigned int _qzz_res; \
104 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0 /* default return */, \
105 VG_USERREQ__MAKE_WRITABLE, \
106 _qzz_addr, _qzz_len, 0, 0); \
107 _qzz_res; \
108 })
109
110/* Similarly, mark memory at _qzz_addr as addressible and defined
111 for _qzz_len bytes. */
112#define VALGRIND_MAKE_READABLE(_qzz_addr,_qzz_len) \
113 ({unsigned int _qzz_res; \
114 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0 /* default return */, \
115 VG_USERREQ__MAKE_READABLE, \
116 _qzz_addr, _qzz_len, 0, 0); \
117 _qzz_res; \
118 })
119
120/* Discard a block-description-handle obtained from the above three
121 macros. After this, Valgrind will no longer be able to relate
122 addressing errors to the user-defined block associated with the
123 handle. The permissions settings associated with the handle remain
124 in place. Returns 1 for an invalid handle, 0 for a valid
125 handle. */
126#define VALGRIND_DISCARD(_qzz_blkindex) \
127 ({unsigned int _qzz_res; \
128 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0 /* default return */, \
129 VG_USERREQ__DISCARD, \
130 0, _qzz_blkindex, 0, 0); \
131 _qzz_res; \
132 })
133
134
135/* Client-code macros to check the state of memory. */
136
137/* Check that memory at _qzz_addr is addressible for _qzz_len bytes.
138 If suitable addressibility is not established, Valgrind prints an
139 error message and returns the address of the first offending byte.
140 Otherwise it returns zero. */
141#define VALGRIND_CHECK_WRITABLE(_qzz_addr,_qzz_len) \
142 ({unsigned int _qzz_res; \
143 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \
144 VG_USERREQ__CHECK_WRITABLE, \
145 _qzz_addr, _qzz_len, 0, 0); \
146 _qzz_res; \
147 })
148
149/* Check that memory at _qzz_addr is addressible and defined for
150 _qzz_len bytes. If suitable addressibility and definedness are not
151 established, Valgrind prints an error message and returns the
152 address of the first offending byte. Otherwise it returns zero. */
153#define VALGRIND_CHECK_READABLE(_qzz_addr,_qzz_len) \
154 ({unsigned int _qzz_res; \
155 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \
156 VG_USERREQ__CHECK_READABLE, \
157 _qzz_addr, _qzz_len, 0, 0); \
158 _qzz_res; \
159 })
160
161/* Use this macro to force the definedness and addressibility of a
162 value to be checked. If suitable addressibility and definedness
163 are not established, Valgrind prints an error message and returns
164 the address of the first offending byte. Otherwise it returns
165 zero. */
166#define VALGRIND_CHECK_DEFINED(__lvalue) \
167 (void) \
168 VALGRIND_CHECK_READABLE( \
169 (volatile unsigned char *)&(__lvalue), \
170 (unsigned int)(sizeof (__lvalue)))
171
njn25e49d8e72002-09-23 09:36:25 +0000172/* Do a memory leak check mid-execution.
173 Currently implemented but untested.
174*/
175#define VALGRIND_DO_LEAK_CHECK \
176 {unsigned int _qzz_res; \
177 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \
178 VG_USERREQ__DO_LEAK_CHECK, \
179 0, 0, 0, 0); \
180 }
181
182
183#endif