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-2012 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 /* Not next to VG_USERREQ__COUNT_LEAKS because it was added later. */ 97 VG_USERREQ__COUNT_LEAK_BLOCKS, 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 } Vg_MemCheckClientRequest; 103 104 105 106 /* Client-code macros to manipulate the state of memory. */ 107 108 /* Mark memory at _qzz_addr as unaddressable for _qzz_len bytes. */ 109 #define VALGRIND_MAKE_MEM_NOACCESS(_qzz_addr,_qzz_len) \ 110 VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */, \ 111 VG_USERREQ__MAKE_MEM_NOACCESS, \ 112 (_qzz_addr), (_qzz_len), 0, 0, 0) 113 114 /* Similarly, mark memory at _qzz_addr as addressable but undefined 115 for _qzz_len bytes. */ 116 #define VALGRIND_MAKE_MEM_UNDEFINED(_qzz_addr,_qzz_len) \ 117 VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */, \ 118 VG_USERREQ__MAKE_MEM_UNDEFINED, \ 119 (_qzz_addr), (_qzz_len), 0, 0, 0) 120 121 /* Similarly, mark memory at _qzz_addr as addressable and defined 122 for _qzz_len bytes. */ 123 #define VALGRIND_MAKE_MEM_DEFINED(_qzz_addr,_qzz_len) \ 124 VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */, \ 125 VG_USERREQ__MAKE_MEM_DEFINED, \ 126 (_qzz_addr), (_qzz_len), 0, 0, 0) 127 128 /* Similar to VALGRIND_MAKE_MEM_DEFINED except that addressability is 129 not altered: bytes which are addressable are marked as defined, 130 but those which are not addressable are left unchanged. */ 131 #define VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE(_qzz_addr,_qzz_len) \ 132 VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */, \ 133 VG_USERREQ__MAKE_MEM_DEFINED_IF_ADDRESSABLE, \ 134 (_qzz_addr), (_qzz_len), 0, 0, 0) 135 136 /* Create a block-description handle. The description is an ascii 137 string which is included in any messages pertaining to addresses 138 within the specified memory range. Has no other effect on the 139 properties of the memory range. */ 140 #define VALGRIND_CREATE_BLOCK(_qzz_addr,_qzz_len, _qzz_desc) \ 141 VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */, \ 142 VG_USERREQ__CREATE_BLOCK, \ 143 (_qzz_addr), (_qzz_len), (_qzz_desc), \ 144 0, 0) 145 146 /* Discard a block-description-handle. Returns 1 for an 147 invalid handle, 0 for a valid handle. */ 148 #define VALGRIND_DISCARD(_qzz_blkindex) \ 149 VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */, \ 150 VG_USERREQ__DISCARD, \ 151 0, (_qzz_blkindex), 0, 0, 0) 152 153 154 /* Client-code macros to check the state of memory. */ 155 156 /* Check that memory at _qzz_addr is addressable for _qzz_len bytes. 157 If suitable addressibility is not established, Valgrind prints an 158 error message and returns the address of the first offending byte. 159 Otherwise it returns zero. */ 160 #define VALGRIND_CHECK_MEM_IS_ADDRESSABLE(_qzz_addr,_qzz_len) \ 161 VALGRIND_DO_CLIENT_REQUEST_EXPR(0, \ 162 VG_USERREQ__CHECK_MEM_IS_ADDRESSABLE, \ 163 (_qzz_addr), (_qzz_len), 0, 0, 0) 164 165 /* Check that memory at _qzz_addr is addressable and defined for 166 _qzz_len bytes. If suitable addressibility and definedness are not 167 established, Valgrind prints an error message and returns the 168 address of the first offending byte. Otherwise it returns zero. */ 169 #define VALGRIND_CHECK_MEM_IS_DEFINED(_qzz_addr,_qzz_len) \ 170 VALGRIND_DO_CLIENT_REQUEST_EXPR(0, \ 171 VG_USERREQ__CHECK_MEM_IS_DEFINED, \ 172 (_qzz_addr), (_qzz_len), 0, 0, 0) 173 174 /* Use this macro to force the definedness and addressibility of an 175 lvalue 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_VALUE_IS_DEFINED(__lvalue) \ 180 VALGRIND_CHECK_MEM_IS_DEFINED( \ 181 (volatile unsigned char *)&(__lvalue), \ 182 (unsigned long)(sizeof (__lvalue))) 183 184 185 /* Do a full memory leak check (like --leak-check=full) mid-execution. */ 186 #define VALGRIND_DO_LEAK_CHECK \ 187 VALGRIND_DO_CLIENT_REQUEST_STMT(VG_USERREQ__DO_LEAK_CHECK, \ 188 0, 0, 0, 0, 0) 189 190 /* Same as VALGRIND_DO_LEAK_CHECK but only showing the entries for 191 which there was an increase in leaked bytes or leaked nr of blocks 192 since the previous leak search. */ 193 #define VALGRIND_DO_ADDED_LEAK_CHECK \ 194 VALGRIND_DO_CLIENT_REQUEST_STMT(VG_USERREQ__DO_LEAK_CHECK, \ 195 0, 1, 0, 0, 0) 196 197 /* Same as VALGRIND_DO_ADDED_LEAK_CHECK but showing entries with 198 increased or decreased leaked bytes/blocks since previous leak 199 search. */ 200 #define VALGRIND_DO_CHANGED_LEAK_CHECK \ 201 VALGRIND_DO_CLIENT_REQUEST_STMT(VG_USERREQ__DO_LEAK_CHECK, \ 202 0, 2, 0, 0, 0) 203 204 /* Do a summary memory leak check (like --leak-check=summary) mid-execution. */ 205 #define VALGRIND_DO_QUICK_LEAK_CHECK \ 206 VALGRIND_DO_CLIENT_REQUEST_STMT(VG_USERREQ__DO_LEAK_CHECK, \ 207 1, 0, 0, 0, 0) 208 209 /* Return number of leaked, dubious, reachable and suppressed bytes found by 210 all previous leak checks. They must be lvalues. */ 211 #define VALGRIND_COUNT_LEAKS(leaked, dubious, reachable, suppressed) \ 212 /* For safety on 64-bit platforms we assign the results to private 213 unsigned long variables, then assign these to the lvalues the user 214 specified, which works no matter what type 'leaked', 'dubious', etc 215 are. We also initialise '_qzz_leaked', etc because 216 VG_USERREQ__COUNT_LEAKS doesn't mark the values returned as 217 defined. */ \ 218 { \ 219 unsigned long _qzz_leaked = 0, _qzz_dubious = 0; \ 220 unsigned long _qzz_reachable = 0, _qzz_suppressed = 0; \ 221 VALGRIND_DO_CLIENT_REQUEST_STMT( \ 222 VG_USERREQ__COUNT_LEAKS, \ 223 &_qzz_leaked, &_qzz_dubious, \ 224 &_qzz_reachable, &_qzz_suppressed, 0); \ 225 leaked = _qzz_leaked; \ 226 dubious = _qzz_dubious; \ 227 reachable = _qzz_reachable; \ 228 suppressed = _qzz_suppressed; \ 229 } 230 231 /* Return number of leaked, dubious, reachable and suppressed bytes found by 232 all previous leak checks. They must be lvalues. */ 233 #define VALGRIND_COUNT_LEAK_BLOCKS(leaked, dubious, reachable, suppressed) \ 234 /* For safety on 64-bit platforms we assign the results to private 235 unsigned long variables, then assign these to the lvalues the user 236 specified, which works no matter what type 'leaked', 'dubious', etc 237 are. We also initialise '_qzz_leaked', etc because 238 VG_USERREQ__COUNT_LEAKS doesn't mark the values returned as 239 defined. */ \ 240 { \ 241 unsigned long _qzz_leaked = 0, _qzz_dubious = 0; \ 242 unsigned long _qzz_reachable = 0, _qzz_suppressed = 0; \ 243 VALGRIND_DO_CLIENT_REQUEST_STMT( \ 244 VG_USERREQ__COUNT_LEAK_BLOCKS, \ 245 &_qzz_leaked, &_qzz_dubious, \ 246 &_qzz_reachable, &_qzz_suppressed, 0); \ 247 leaked = _qzz_leaked; \ 248 dubious = _qzz_dubious; \ 249 reachable = _qzz_reachable; \ 250 suppressed = _qzz_suppressed; \ 251 } 252 253 254 /* Get the validity data for addresses [zza..zza+zznbytes-1] and copy it 255 into the provided zzvbits array. Return values: 256 0 if not running on valgrind 257 1 success 258 2 [previously indicated unaligned arrays; these are now allowed] 259 3 if any parts of zzsrc/zzvbits are not addressable. 260 The metadata is not copied in cases 0, 2 or 3 so it should be 261 impossible to segfault your system by using this call. 262 */ 263 #define VALGRIND_GET_VBITS(zza,zzvbits,zznbytes) \ 264 (unsigned)VALGRIND_DO_CLIENT_REQUEST_EXPR(0, \ 265 VG_USERREQ__GET_VBITS, \ 266 (const char*)(zza), \ 267 (char*)(zzvbits), \ 268 (zznbytes), 0, 0) 269 270 /* Set the validity data for addresses [zza..zza+zznbytes-1], copying it 271 from the provided zzvbits array. Return values: 272 0 if not running on valgrind 273 1 success 274 2 [previously indicated unaligned arrays; these are now allowed] 275 3 if any parts of zza/zzvbits are not addressable. 276 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 */ 279 #define VALGRIND_SET_VBITS(zza,zzvbits,zznbytes) \ 280 (unsigned)VALGRIND_DO_CLIENT_REQUEST_EXPR(0, \ 281 VG_USERREQ__SET_VBITS, \ 282 (const char*)(zza), \ 283 (const char*)(zzvbits), \ 284 (zznbytes), 0, 0 ) 285 286 #endif 287 288