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