coregrind/m_gdbserver/remote-utils.c (prepare_resume_reply): Use memcpy
[valgrind.git] / memcheck / memcheck.h
blob6a71a25666cbbaf788a338aef062a6cfa195e816
2 /*
3 ----------------------------------------------------------------
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.
11 ----------------------------------------------------------------
13 This file is part of MemCheck, a heavyweight Valgrind tool for
14 detecting memory errors.
16 Copyright (C) 2000-2017 Julian Seward. All rights reserved.
18 Redistribution and use in source and binary forms, with or without
19 modification, are permitted provided that the following conditions
20 are met:
22 1. Redistributions of source code must retain the above copyright
23 notice, this list of conditions and the following disclaimer.
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.
30 3. Altered source versions must be plainly marked as such, and must
31 not be misrepresented as being the original software.
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.
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.
49 ----------------------------------------------------------------
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.
56 ----------------------------------------------------------------
60 #ifndef __MEMCHECK_H
61 #define __MEMCHECK_H
64 /* This file is for inclusion into client (your!) code.
66 You can use these macros to manipulate and query memory permissions
67 inside your own programs.
69 See comment near the top of valgrind.h on how to use them.
72 #include "valgrind.h"
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,
89 VG_USERREQ__GET_VBITS,
90 VG_USERREQ__SET_VBITS,
92 VG_USERREQ__CREATE_BLOCK,
94 VG_USERREQ__MAKE_MEM_DEFINED_IF_ADDRESSABLE,
96 /* Not next to VG_USERREQ__COUNT_LEAKS because it was added later. */
97 VG_USERREQ__COUNT_LEAK_BLOCKS,
99 VG_USERREQ__ENABLE_ADDR_ERROR_REPORTING_IN_RANGE,
100 VG_USERREQ__DISABLE_ADDR_ERROR_REPORTING_IN_RANGE,
102 /* This is just for memcheck's internal use - don't use it */
103 _VG_USERREQ__MEMCHECK_RECORD_OVERLAP_ERROR
104 = VG_USERREQ_TOOL_BASE('M','C') + 256,
105 _VG_USERREQ__MEMCHECK_VERIFY_ALIGNMENT
106 } Vg_MemCheckClientRequest;
110 /* Client-code macros to manipulate the state of memory. */
112 /* Mark memory at _qzz_addr as unaddressable for _qzz_len bytes. */
113 #define VALGRIND_MAKE_MEM_NOACCESS(_qzz_addr,_qzz_len) \
114 VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */, \
115 VG_USERREQ__MAKE_MEM_NOACCESS, \
116 (_qzz_addr), (_qzz_len), 0, 0, 0)
118 /* Similarly, mark memory at _qzz_addr as addressable but undefined
119 for _qzz_len bytes. */
120 #define VALGRIND_MAKE_MEM_UNDEFINED(_qzz_addr,_qzz_len) \
121 VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */, \
122 VG_USERREQ__MAKE_MEM_UNDEFINED, \
123 (_qzz_addr), (_qzz_len), 0, 0, 0)
125 /* Similarly, mark memory at _qzz_addr as addressable and defined
126 for _qzz_len bytes. */
127 #define VALGRIND_MAKE_MEM_DEFINED(_qzz_addr,_qzz_len) \
128 VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */, \
129 VG_USERREQ__MAKE_MEM_DEFINED, \
130 (_qzz_addr), (_qzz_len), 0, 0, 0)
132 /* Similar to VALGRIND_MAKE_MEM_DEFINED except that addressability is
133 not altered: bytes which are addressable are marked as defined,
134 but those which are not addressable are left unchanged. */
135 #define VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE(_qzz_addr,_qzz_len) \
136 VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */, \
137 VG_USERREQ__MAKE_MEM_DEFINED_IF_ADDRESSABLE, \
138 (_qzz_addr), (_qzz_len), 0, 0, 0)
140 /* Create a block-description handle. The description is an ascii
141 string which is included in any messages pertaining to addresses
142 within the specified memory range. Has no other effect on the
143 properties of the memory range. */
144 #define VALGRIND_CREATE_BLOCK(_qzz_addr,_qzz_len, _qzz_desc) \
145 VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */, \
146 VG_USERREQ__CREATE_BLOCK, \
147 (_qzz_addr), (_qzz_len), (_qzz_desc), \
148 0, 0)
150 /* Discard a block-description-handle. Returns 1 for an
151 invalid handle, 0 for a valid handle. */
152 #define VALGRIND_DISCARD(_qzz_blkindex) \
153 VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */, \
154 VG_USERREQ__DISCARD, \
155 0, (_qzz_blkindex), 0, 0, 0)
158 /* Client-code macros to check the state of memory. */
160 /* Check that memory at _qzz_addr is addressable for _qzz_len bytes.
161 If suitable addressibility is not established, Valgrind prints an
162 error message and returns the address of the first offending byte.
163 Otherwise it returns zero. */
164 #define VALGRIND_CHECK_MEM_IS_ADDRESSABLE(_qzz_addr,_qzz_len) \
165 VALGRIND_DO_CLIENT_REQUEST_EXPR(0, \
166 VG_USERREQ__CHECK_MEM_IS_ADDRESSABLE, \
167 (_qzz_addr), (_qzz_len), 0, 0, 0)
169 /* Check that memory at _qzz_addr is addressable and defined for
170 _qzz_len bytes. If suitable addressibility and definedness are not
171 established, Valgrind prints an error message and returns the
172 address of the first offending byte. Otherwise it returns zero. */
173 #define VALGRIND_CHECK_MEM_IS_DEFINED(_qzz_addr,_qzz_len) \
174 VALGRIND_DO_CLIENT_REQUEST_EXPR(0, \
175 VG_USERREQ__CHECK_MEM_IS_DEFINED, \
176 (_qzz_addr), (_qzz_len), 0, 0, 0)
178 /* Use this macro to force the definedness and addressibility of an
179 lvalue to be checked. If suitable addressibility and definedness
180 are not established, Valgrind prints an error message and returns
181 the address of the first offending byte. Otherwise it returns
182 zero. */
183 #define VALGRIND_CHECK_VALUE_IS_DEFINED(__lvalue) \
184 VALGRIND_CHECK_MEM_IS_DEFINED( \
185 (volatile unsigned char *)&(__lvalue), \
186 (unsigned long)(sizeof (__lvalue)))
189 /* Do a full memory leak check (like --leak-check=full) mid-execution. */
190 #define VALGRIND_DO_LEAK_CHECK \
191 VALGRIND_DO_CLIENT_REQUEST_STMT(VG_USERREQ__DO_LEAK_CHECK, \
192 0, 0, 0, 0, 0)
194 /* Same as VALGRIND_DO_LEAK_CHECK but only showing the entries for
195 which there was an increase in leaked bytes or leaked nr of blocks
196 since the previous leak search. */
197 #define VALGRIND_DO_ADDED_LEAK_CHECK \
198 VALGRIND_DO_CLIENT_REQUEST_STMT(VG_USERREQ__DO_LEAK_CHECK, \
199 0, 1, 0, 0, 0)
201 /* Same as VALGRIND_DO_ADDED_LEAK_CHECK but showing entries with
202 increased or decreased leaked bytes/blocks since previous leak
203 search. */
204 #define VALGRIND_DO_CHANGED_LEAK_CHECK \
205 VALGRIND_DO_CLIENT_REQUEST_STMT(VG_USERREQ__DO_LEAK_CHECK, \
206 0, 2, 0, 0, 0)
208 /* Same as VALGRIND_DO_LEAK_CHECK but only showing new entries
209 i.e. loss records that were not there in the previous leak
210 search. */
211 #define VALGRIND_DO_NEW_LEAK_CHECK \
212 VALGRIND_DO_CLIENT_REQUEST_STMT(VG_USERREQ__DO_LEAK_CHECK, \
213 0, 3, 0, 0, 0)
215 /* Do a summary memory leak check (like --leak-check=summary) mid-execution. */
216 #define VALGRIND_DO_QUICK_LEAK_CHECK \
217 VALGRIND_DO_CLIENT_REQUEST_STMT(VG_USERREQ__DO_LEAK_CHECK, \
218 1, 0, 0, 0, 0)
220 /* Return number of leaked, dubious, reachable and suppressed bytes found by
221 all previous leak checks. They must be lvalues. */
222 #define VALGRIND_COUNT_LEAKS(leaked, dubious, reachable, suppressed) \
223 /* For safety on 64-bit platforms we assign the results to private
224 unsigned long variables, then assign these to the lvalues the user
225 specified, which works no matter what type 'leaked', 'dubious', etc
226 are. We also initialise '_qzz_leaked', etc because
227 VG_USERREQ__COUNT_LEAKS doesn't mark the values returned as
228 defined. */ \
230 unsigned long _qzz_leaked = 0, _qzz_dubious = 0; \
231 unsigned long _qzz_reachable = 0, _qzz_suppressed = 0; \
232 VALGRIND_DO_CLIENT_REQUEST_STMT( \
233 VG_USERREQ__COUNT_LEAKS, \
234 &_qzz_leaked, &_qzz_dubious, \
235 &_qzz_reachable, &_qzz_suppressed, 0); \
236 leaked = _qzz_leaked; \
237 dubious = _qzz_dubious; \
238 reachable = _qzz_reachable; \
239 suppressed = _qzz_suppressed; \
242 /* Return number of leaked, dubious, reachable and suppressed bytes found by
243 all previous leak checks. They must be lvalues. */
244 #define VALGRIND_COUNT_LEAK_BLOCKS(leaked, dubious, reachable, suppressed) \
245 /* For safety on 64-bit platforms we assign the results to private
246 unsigned long variables, then assign these to the lvalues the user
247 specified, which works no matter what type 'leaked', 'dubious', etc
248 are. We also initialise '_qzz_leaked', etc because
249 VG_USERREQ__COUNT_LEAKS doesn't mark the values returned as
250 defined. */ \
252 unsigned long _qzz_leaked = 0, _qzz_dubious = 0; \
253 unsigned long _qzz_reachable = 0, _qzz_suppressed = 0; \
254 VALGRIND_DO_CLIENT_REQUEST_STMT( \
255 VG_USERREQ__COUNT_LEAK_BLOCKS, \
256 &_qzz_leaked, &_qzz_dubious, \
257 &_qzz_reachable, &_qzz_suppressed, 0); \
258 leaked = _qzz_leaked; \
259 dubious = _qzz_dubious; \
260 reachable = _qzz_reachable; \
261 suppressed = _qzz_suppressed; \
265 /* Get the validity data for addresses [zza..zza+zznbytes-1] and copy it
266 into the provided zzvbits array. Return values:
267 0 if not running on valgrind
268 1 success
269 2 [previously indicated unaligned arrays; these are now allowed]
270 3 if any parts of zzsrc/zzvbits are not addressable.
271 The metadata is not copied in cases 0, 2 or 3 so it should be
272 impossible to segfault your system by using this call.
274 #define VALGRIND_GET_VBITS(zza,zzvbits,zznbytes) \
275 (unsigned)VALGRIND_DO_CLIENT_REQUEST_EXPR(0, \
276 VG_USERREQ__GET_VBITS, \
277 (const char*)(zza), \
278 (char*)(zzvbits), \
279 (zznbytes), 0, 0)
281 /* Set the validity data for addresses [zza..zza+zznbytes-1], copying it
282 from the provided zzvbits array. Return values:
283 0 if not running on valgrind
284 1 success
285 2 [previously indicated unaligned arrays; these are now allowed]
286 3 if any parts of zza/zzvbits are not addressable.
287 The metadata is not copied in cases 0, 2 or 3 so it should be
288 impossible to segfault your system by using this call.
290 #define VALGRIND_SET_VBITS(zza,zzvbits,zznbytes) \
291 (unsigned)VALGRIND_DO_CLIENT_REQUEST_EXPR(0, \
292 VG_USERREQ__SET_VBITS, \
293 (const char*)(zza), \
294 (const char*)(zzvbits), \
295 (zznbytes), 0, 0 )
297 /* Disable and re-enable reporting of addressing errors in the
298 specified address range. */
299 #define VALGRIND_DISABLE_ADDR_ERROR_REPORTING_IN_RANGE(_qzz_addr,_qzz_len) \
300 VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */, \
301 VG_USERREQ__DISABLE_ADDR_ERROR_REPORTING_IN_RANGE, \
302 (_qzz_addr), (_qzz_len), 0, 0, 0)
304 #define VALGRIND_ENABLE_ADDR_ERROR_REPORTING_IN_RANGE(_qzz_addr,_qzz_len) \
305 VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */, \
306 VG_USERREQ__ENABLE_ADDR_ERROR_REPORTING_IN_RANGE, \
307 (_qzz_addr), (_qzz_len), 0, 0, 0)
309 #endif