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-2010 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 FILE_LICENCE ( BSD3 ); 64 65 66 /* This file is for inclusion into client (your!) code. 67 68 You can use these macros to manipulate and query memory permissions 69 inside your own programs. 70 71 See comment near the top of valgrind.h on how to use them. 72 */ 73 74 #include "valgrind.h" 75 76 /* !! ABIWARNING !! ABIWARNING !! ABIWARNING !! ABIWARNING !! 77 This enum comprises an ABI exported by Valgrind to programs 78 which use client requests. DO NOT CHANGE THE ORDER OF THESE 79 ENTRIES, NOR DELETE ANY -- add new ones at the end. */ 80 typedef 81 enum { 82 VG_USERREQ__MAKE_MEM_NOACCESS = VG_USERREQ_TOOL_BASE('M','C'), 83 VG_USERREQ__MAKE_MEM_UNDEFINED, 84 VG_USERREQ__MAKE_MEM_DEFINED, 85 VG_USERREQ__DISCARD, 86 VG_USERREQ__CHECK_MEM_IS_ADDRESSABLE, 87 VG_USERREQ__CHECK_MEM_IS_DEFINED, 88 VG_USERREQ__DO_LEAK_CHECK, 89 VG_USERREQ__COUNT_LEAKS, 90 91 VG_USERREQ__GET_VBITS, 92 VG_USERREQ__SET_VBITS, 93 94 VG_USERREQ__CREATE_BLOCK, 95 96 VG_USERREQ__MAKE_MEM_DEFINED_IF_ADDRESSABLE, 97 98 /* Not next to VG_USERREQ__COUNT_LEAKS because it was added later. */ 99 VG_USERREQ__COUNT_LEAK_BLOCKS, 100 101 /* This is just for memcheck's internal use - don't use it */ 102 _VG_USERREQ__MEMCHECK_RECORD_OVERLAP_ERROR 103 = VG_USERREQ_TOOL_BASE('M','C') + 256 104 } Vg_MemCheckClientRequest; 105 106 107 108 /* Client-code macros to manipulate the state of memory. */ 109 110 /* Mark memory at _qzz_addr as unaddressable for _qzz_len bytes. */ 111 #define VALGRIND_MAKE_MEM_NOACCESS(_qzz_addr,_qzz_len) \ 112 (__extension__({unsigned long _qzz_res; \ 113 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0 /* default return */, \ 114 VG_USERREQ__MAKE_MEM_NOACCESS, \ 115 _qzz_addr, _qzz_len, 0, 0, 0); \ 116 _qzz_res; \ 117 })) 118 119 /* Similarly, mark memory at _qzz_addr as addressable but undefined 120 for _qzz_len bytes. */ 121 #define VALGRIND_MAKE_MEM_UNDEFINED(_qzz_addr,_qzz_len) \ 122 (__extension__({unsigned long _qzz_res; \ 123 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0 /* default return */, \ 124 VG_USERREQ__MAKE_MEM_UNDEFINED, \ 125 _qzz_addr, _qzz_len, 0, 0, 0); \ 126 _qzz_res; \ 127 })) 128 129 /* Similarly, mark memory at _qzz_addr as addressable and defined 130 for _qzz_len bytes. */ 131 #define VALGRIND_MAKE_MEM_DEFINED(_qzz_addr,_qzz_len) \ 132 (__extension__({unsigned long _qzz_res; \ 133 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0 /* default return */, \ 134 VG_USERREQ__MAKE_MEM_DEFINED, \ 135 _qzz_addr, _qzz_len, 0, 0, 0); \ 136 _qzz_res; \ 137 })) 138 139 /* Similar to VALGRIND_MAKE_MEM_DEFINED except that addressability is 140 not altered: bytes which are addressable are marked as defined, 141 but those which are not addressable are left unchanged. */ 142 #define VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE(_qzz_addr,_qzz_len) \ 143 (__extension__({unsigned long _qzz_res; \ 144 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0 /* default return */, \ 145 VG_USERREQ__MAKE_MEM_DEFINED_IF_ADDRESSABLE, \ 146 _qzz_addr, _qzz_len, 0, 0, 0); \ 147 _qzz_res; \ 148 })) 149 150 /* Create a block-description handle. The description is an ascii 151 string which is included in any messages pertaining to addresses 152 within the specified memory range. Has no other effect on the 153 properties of the memory range. */ 154 #define VALGRIND_CREATE_BLOCK(_qzz_addr,_qzz_len, _qzz_desc) \ 155 (__extension__({unsigned long _qzz_res; \ 156 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0 /* default return */, \ 157 VG_USERREQ__CREATE_BLOCK, \ 158 _qzz_addr, _qzz_len, _qzz_desc, \ 159 0, 0); \ 160 _qzz_res; \ 161 })) 162 163 /* Discard a block-description-handle. Returns 1 for an 164 invalid handle, 0 for a valid handle. */ 165 #define VALGRIND_DISCARD(_qzz_blkindex) \ 166 (__extension__ ({unsigned long _qzz_res; \ 167 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0 /* default return */, \ 168 VG_USERREQ__DISCARD, \ 169 0, _qzz_blkindex, 0, 0, 0); \ 170 _qzz_res; \ 171 })) 172 173 174 /* Client-code macros to check the state of memory. */ 175 176 /* Check that memory at _qzz_addr is addressable for _qzz_len bytes. 177 If suitable addressibility is not established, Valgrind prints an 178 error message and returns the address of the first offending byte. 179 Otherwise it returns zero. */ 180 #define VALGRIND_CHECK_MEM_IS_ADDRESSABLE(_qzz_addr,_qzz_len) \ 181 (__extension__({unsigned long _qzz_res; \ 182 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \ 183 VG_USERREQ__CHECK_MEM_IS_ADDRESSABLE,\ 184 _qzz_addr, _qzz_len, 0, 0, 0); \ 185 _qzz_res; \ 186 })) 187 188 /* Check that memory at _qzz_addr is addressable and defined for 189 _qzz_len bytes. If suitable addressibility and definedness are not 190 established, Valgrind prints an error message and returns the 191 address of the first offending byte. Otherwise it returns zero. */ 192 #define VALGRIND_CHECK_MEM_IS_DEFINED(_qzz_addr,_qzz_len) \ 193 (__extension__({unsigned long _qzz_res; \ 194 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \ 195 VG_USERREQ__CHECK_MEM_IS_DEFINED, \ 196 _qzz_addr, _qzz_len, 0, 0, 0); \ 197 _qzz_res; \ 198 })) 199 200 /* Use this macro to force the definedness and addressibility of an 201 lvalue to be checked. If suitable addressibility and definedness 202 are not established, Valgrind prints an error message and returns 203 the address of the first offending byte. Otherwise it returns 204 zero. */ 205 #define VALGRIND_CHECK_VALUE_IS_DEFINED(__lvalue) \ 206 VALGRIND_CHECK_MEM_IS_DEFINED( \ 207 (volatile unsigned char *)&(__lvalue), \ 208 (unsigned long)(sizeof (__lvalue))) 209 210 211 /* Do a full memory leak check (like --leak-check=full) mid-execution. */ 212 #define VALGRIND_DO_LEAK_CHECK \ 213 {unsigned long _qzz_res; \ 214 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \ 215 VG_USERREQ__DO_LEAK_CHECK, \ 216 0, 0, 0, 0, 0); \ 217 } 218 219 /* Do a summary memory leak check (like --leak-check=summary) mid-execution. */ 220 #define VALGRIND_DO_QUICK_LEAK_CHECK \ 221 {unsigned long _qzz_res; \ 222 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \ 223 VG_USERREQ__DO_LEAK_CHECK, \ 224 1, 0, 0, 0, 0); \ 225 } 226 227 /* Return number of leaked, dubious, reachable and suppressed bytes found by 228 all previous leak checks. They must be lvalues. */ 229 #define VALGRIND_COUNT_LEAKS(leaked, dubious, reachable, suppressed) \ 230 /* For safety on 64-bit platforms we assign the results to private 231 unsigned long variables, then assign these to the lvalues the user 232 specified, which works no matter what type 'leaked', 'dubious', etc 233 are. We also initialise '_qzz_leaked', etc because 234 VG_USERREQ__COUNT_LEAKS doesn't mark the values returned as 235 defined. */ \ 236 {unsigned long _qzz_res; \ 237 unsigned long _qzz_leaked = 0, _qzz_dubious = 0; \ 238 unsigned long _qzz_reachable = 0, _qzz_suppressed = 0; \ 239 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \ 240 VG_USERREQ__COUNT_LEAKS, \ 241 &_qzz_leaked, &_qzz_dubious, \ 242 &_qzz_reachable, &_qzz_suppressed, 0); \ 243 leaked = _qzz_leaked; \ 244 dubious = _qzz_dubious; \ 245 reachable = _qzz_reachable; \ 246 suppressed = _qzz_suppressed; \ 247 } 248 249 /* Return number of leaked, dubious, reachable and suppressed bytes found by 250 all previous leak checks. They must be lvalues. */ 251 #define VALGRIND_COUNT_LEAK_BLOCKS(leaked, dubious, reachable, suppressed) \ 252 /* For safety on 64-bit platforms we assign the results to private 253 unsigned long variables, then assign these to the lvalues the user 254 specified, which works no matter what type 'leaked', 'dubious', etc 255 are. We also initialise '_qzz_leaked', etc because 256 VG_USERREQ__COUNT_LEAKS doesn't mark the values returned as 257 defined. */ \ 258 {unsigned long _qzz_res; \ 259 unsigned long _qzz_leaked = 0, _qzz_dubious = 0; \ 260 unsigned long _qzz_reachable = 0, _qzz_suppressed = 0; \ 261 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \ 262 VG_USERREQ__COUNT_LEAK_BLOCKS, \ 263 &_qzz_leaked, &_qzz_dubious, \ 264 &_qzz_reachable, &_qzz_suppressed, 0); \ 265 leaked = _qzz_leaked; \ 266 dubious = _qzz_dubious; \ 267 reachable = _qzz_reachable; \ 268 suppressed = _qzz_suppressed; \ 269 } 270 271 272 /* Get the validity data for addresses [zza..zza+zznbytes-1] and copy it 273 into the provided zzvbits array. Return values: 274 0 if not running on valgrind 275 1 success 276 2 [previously indicated unaligned arrays; these are now allowed] 277 3 if any parts of zzsrc/zzvbits are not addressable. 278 The metadata is not copied in cases 0, 2 or 3 so it should be 279 impossible to segfault your system by using this call. 280 */ 281 #define VALGRIND_GET_VBITS(zza,zzvbits,zznbytes) \ 282 (__extension__({unsigned long _qzz_res; \ 283 char* czza = (char*)zza; \ 284 char* czzvbits = (char*)zzvbits; \ 285 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \ 286 VG_USERREQ__GET_VBITS, \ 287 czza, czzvbits, zznbytes, 0, 0 ); \ 288 _qzz_res; \ 289 })) 290 291 /* Set the validity data for addresses [zza..zza+zznbytes-1], copying it 292 from the provided zzvbits array. Return values: 293 0 if not running on valgrind 294 1 success 295 2 [previously indicated unaligned arrays; these are now allowed] 296 3 if any parts of zza/zzvbits are not addressable. 297 The metadata is not copied in cases 0, 2 or 3 so it should be 298 impossible to segfault your system by using this call. 299 */ 300 #define VALGRIND_SET_VBITS(zza,zzvbits,zznbytes) \ 301 (__extension__({unsigned int _qzz_res; \ 302 char* czza = (char*)zza; \ 303 char* czzvbits = (char*)zzvbits; \ 304 VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \ 305 VG_USERREQ__SET_VBITS, \ 306 czza, czzvbits, zznbytes, 0, 0 ); \ 307 _qzz_res; \ 308 })) 309 310 #endif 311 312