memcheck.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. ----------------------------------------------------------------
  3. Notice that the following BSD-style license applies to this one
  4. file (memcheck.h) only. The rest of Valgrind is licensed under the
  5. terms of the GNU General Public License, version 2, unless
  6. otherwise indicated. See the COPYING file in the source
  7. distribution for details.
  8. ----------------------------------------------------------------
  9. This file is part of MemCheck, a heavyweight Valgrind tool for
  10. detecting memory errors.
  11. Copyright (C) 2000-2010 Julian Seward. All rights reserved.
  12. Redistribution and use in source and binary forms, with or without
  13. modification, are permitted provided that the following conditions
  14. are met:
  15. 1. Redistributions of source code must retain the above copyright
  16. notice, this list of conditions and the following disclaimer.
  17. 2. The origin of this software must not be misrepresented; you must
  18. not claim that you wrote the original software. If you use this
  19. software in a product, an acknowledgment in the product
  20. documentation would be appreciated but is not required.
  21. 3. Altered source versions must be plainly marked as such, and must
  22. not be misrepresented as being the original software.
  23. 4. The name of the author may not be used to endorse or promote
  24. products derived from this software without specific prior written
  25. permission.
  26. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  27. OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  28. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  30. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  32. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  34. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  35. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  36. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. ----------------------------------------------------------------
  38. Notice that the above BSD-style license applies to this one file
  39. (memcheck.h) only. The entire rest of Valgrind is licensed under
  40. the terms of the GNU General Public License, version 2. See the
  41. COPYING file in the source distribution for details.
  42. ----------------------------------------------------------------
  43. */
  44. #ifndef __MEMCHECK_H
  45. #define __MEMCHECK_H
  46. /* This file is for inclusion into client (your!) code.
  47. You can use these macros to manipulate and query memory permissions
  48. inside your own programs.
  49. See comment near the top of valgrind.h on how to use them.
  50. */
  51. #include "valgrind.h"
  52. /* !! ABIWARNING !! ABIWARNING !! ABIWARNING !! ABIWARNING !!
  53. This enum comprises an ABI exported by Valgrind to programs
  54. which use client requests. DO NOT CHANGE THE ORDER OF THESE
  55. ENTRIES, NOR DELETE ANY -- add new ones at the end. */
  56. typedef
  57. enum {
  58. VG_USERREQ__MAKE_MEM_NOACCESS = VG_USERREQ_TOOL_BASE('M','C'),
  59. VG_USERREQ__MAKE_MEM_UNDEFINED,
  60. VG_USERREQ__MAKE_MEM_DEFINED,
  61. VG_USERREQ__DISCARD,
  62. VG_USERREQ__CHECK_MEM_IS_ADDRESSABLE,
  63. VG_USERREQ__CHECK_MEM_IS_DEFINED,
  64. VG_USERREQ__DO_LEAK_CHECK,
  65. VG_USERREQ__COUNT_LEAKS,
  66. VG_USERREQ__GET_VBITS,
  67. VG_USERREQ__SET_VBITS,
  68. VG_USERREQ__CREATE_BLOCK,
  69. VG_USERREQ__MAKE_MEM_DEFINED_IF_ADDRESSABLE,
  70. /* Not next to VG_USERREQ__COUNT_LEAKS because it was added later. */
  71. VG_USERREQ__COUNT_LEAK_BLOCKS,
  72. /* This is just for memcheck's internal use - don't use it */
  73. _VG_USERREQ__MEMCHECK_RECORD_OVERLAP_ERROR
  74. = VG_USERREQ_TOOL_BASE('M','C') + 256
  75. } Vg_MemCheckClientRequest;
  76. /* Client-code macros to manipulate the state of memory. */
  77. /* Mark memory at _qzz_addr as unaddressable for _qzz_len bytes. */
  78. #define VALGRIND_MAKE_MEM_NOACCESS(_qzz_addr,_qzz_len) \
  79. VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */, \
  80. VG_USERREQ__MAKE_MEM_NOACCESS, \
  81. (_qzz_addr), (_qzz_len), 0, 0, 0)
  82. /* Similarly, mark memory at _qzz_addr as addressable but undefined
  83. for _qzz_len bytes. */
  84. #define VALGRIND_MAKE_MEM_UNDEFINED(_qzz_addr,_qzz_len) \
  85. VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */, \
  86. VG_USERREQ__MAKE_MEM_UNDEFINED, \
  87. (_qzz_addr), (_qzz_len), 0, 0, 0)
  88. /* Similarly, mark memory at _qzz_addr as addressable and defined
  89. for _qzz_len bytes. */
  90. #define VALGRIND_MAKE_MEM_DEFINED(_qzz_addr,_qzz_len) \
  91. VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */, \
  92. VG_USERREQ__MAKE_MEM_DEFINED, \
  93. (_qzz_addr), (_qzz_len), 0, 0, 0)
  94. /* Similar to VALGRIND_MAKE_MEM_DEFINED except that addressability is
  95. not altered: bytes which are addressable are marked as defined,
  96. but those which are not addressable are left unchanged. */
  97. #define VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE(_qzz_addr,_qzz_len) \
  98. VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */, \
  99. VG_USERREQ__MAKE_MEM_DEFINED_IF_ADDRESSABLE, \
  100. (_qzz_addr), (_qzz_len), 0, 0, 0)
  101. /* Create a block-description handle. The description is an ascii
  102. string which is included in any messages pertaining to addresses
  103. within the specified memory range. Has no other effect on the
  104. properties of the memory range. */
  105. #define VALGRIND_CREATE_BLOCK(_qzz_addr,_qzz_len, _qzz_desc) \
  106. VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */, \
  107. VG_USERREQ__CREATE_BLOCK, \
  108. (_qzz_addr), (_qzz_len), (_qzz_desc), \
  109. 0, 0)
  110. /* Discard a block-description-handle. Returns 1 for an
  111. invalid handle, 0 for a valid handle. */
  112. #define VALGRIND_DISCARD(_qzz_blkindex) \
  113. VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */, \
  114. VG_USERREQ__DISCARD, \
  115. 0, (_qzz_blkindex), 0, 0, 0)
  116. /* Client-code macros to check the state of memory. */
  117. /* Check that memory at _qzz_addr is addressable for _qzz_len bytes.
  118. If suitable addressibility is not established, Valgrind prints an
  119. error message and returns the address of the first offending byte.
  120. Otherwise it returns zero. */
  121. #define VALGRIND_CHECK_MEM_IS_ADDRESSABLE(_qzz_addr,_qzz_len) \
  122. VALGRIND_DO_CLIENT_REQUEST_EXPR(0, \
  123. VG_USERREQ__CHECK_MEM_IS_ADDRESSABLE, \
  124. (_qzz_addr), (_qzz_len), 0, 0, 0)
  125. /* Check that memory at _qzz_addr is addressable and defined for
  126. _qzz_len bytes. If suitable addressibility and definedness are not
  127. established, Valgrind prints an error message and returns the
  128. address of the first offending byte. Otherwise it returns zero. */
  129. #define VALGRIND_CHECK_MEM_IS_DEFINED(_qzz_addr,_qzz_len) \
  130. VALGRIND_DO_CLIENT_REQUEST_EXPR(0, \
  131. VG_USERREQ__CHECK_MEM_IS_DEFINED, \
  132. (_qzz_addr), (_qzz_len), 0, 0, 0)
  133. /* Use this macro to force the definedness and addressibility of an
  134. lvalue to be checked. If suitable addressibility and definedness
  135. are not established, Valgrind prints an error message and returns
  136. the address of the first offending byte. Otherwise it returns
  137. zero. */
  138. #define VALGRIND_CHECK_VALUE_IS_DEFINED(__lvalue) \
  139. VALGRIND_CHECK_MEM_IS_DEFINED( \
  140. (volatile unsigned char *)&(__lvalue), \
  141. (unsigned long)(sizeof (__lvalue)))
  142. /* Do a full memory leak check (like --leak-check=full) mid-execution. */
  143. #define VALGRIND_DO_LEAK_CHECK \
  144. {unsigned long _qzz_res; \
  145. VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \
  146. VG_USERREQ__DO_LEAK_CHECK, \
  147. 0, 0, 0, 0, 0); \
  148. }
  149. /* Do a summary memory leak check (like --leak-check=summary) mid-execution. */
  150. #define VALGRIND_DO_QUICK_LEAK_CHECK \
  151. {unsigned long _qzz_res; \
  152. VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \
  153. VG_USERREQ__DO_LEAK_CHECK, \
  154. 1, 0, 0, 0, 0); \
  155. }
  156. /* Return number of leaked, dubious, reachable and suppressed bytes found by
  157. all previous leak checks. They must be lvalues. */
  158. #define VALGRIND_COUNT_LEAKS(leaked, dubious, reachable, suppressed) \
  159. /* For safety on 64-bit platforms we assign the results to private
  160. unsigned long variables, then assign these to the lvalues the user
  161. specified, which works no matter what type 'leaked', 'dubious', etc
  162. are. We also initialise '_qzz_leaked', etc because
  163. VG_USERREQ__COUNT_LEAKS doesn't mark the values returned as
  164. defined. */ \
  165. {unsigned long _qzz_res; \
  166. unsigned long _qzz_leaked = 0, _qzz_dubious = 0; \
  167. unsigned long _qzz_reachable = 0, _qzz_suppressed = 0; \
  168. VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \
  169. VG_USERREQ__COUNT_LEAKS, \
  170. &_qzz_leaked, &_qzz_dubious, \
  171. &_qzz_reachable, &_qzz_suppressed, 0); \
  172. leaked = _qzz_leaked; \
  173. dubious = _qzz_dubious; \
  174. reachable = _qzz_reachable; \
  175. suppressed = _qzz_suppressed; \
  176. }
  177. /* Return number of leaked, dubious, reachable and suppressed bytes found by
  178. all previous leak checks. They must be lvalues. */
  179. #define VALGRIND_COUNT_LEAK_BLOCKS(leaked, dubious, reachable, suppressed) \
  180. /* For safety on 64-bit platforms we assign the results to private
  181. unsigned long variables, then assign these to the lvalues the user
  182. specified, which works no matter what type 'leaked', 'dubious', etc
  183. are. We also initialise '_qzz_leaked', etc because
  184. VG_USERREQ__COUNT_LEAKS doesn't mark the values returned as
  185. defined. */ \
  186. {unsigned long _qzz_res; \
  187. unsigned long _qzz_leaked = 0, _qzz_dubious = 0; \
  188. unsigned long _qzz_reachable = 0, _qzz_suppressed = 0; \
  189. VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0, \
  190. VG_USERREQ__COUNT_LEAK_BLOCKS, \
  191. &_qzz_leaked, &_qzz_dubious, \
  192. &_qzz_reachable, &_qzz_suppressed, 0); \
  193. leaked = _qzz_leaked; \
  194. dubious = _qzz_dubious; \
  195. reachable = _qzz_reachable; \
  196. suppressed = _qzz_suppressed; \
  197. }
  198. /* Get the validity data for addresses [zza..zza+zznbytes-1] and copy it
  199. into the provided zzvbits array. Return values:
  200. 0 if not running on valgrind
  201. 1 success
  202. 2 [previously indicated unaligned arrays; these are now allowed]
  203. 3 if any parts of zzsrc/zzvbits are not addressable.
  204. The metadata is not copied in cases 0, 2 or 3 so it should be
  205. impossible to segfault your system by using this call.
  206. */
  207. #define VALGRIND_GET_VBITS(zza,zzvbits,zznbytes) \
  208. VALGRIND_DO_CLIENT_REQUEST_EXPR(0, \
  209. VG_USERREQ__GET_VBITS, \
  210. (const char*)(zza), \
  211. (char*)(zzvbits), \
  212. (zznbytes), 0, 0)
  213. /* Set the validity data for addresses [zza..zza+zznbytes-1], copying it
  214. from the provided zzvbits array. Return values:
  215. 0 if not running on valgrind
  216. 1 success
  217. 2 [previously indicated unaligned arrays; these are now allowed]
  218. 3 if any parts of zza/zzvbits are not addressable.
  219. The metadata is not copied in cases 0, 2 or 3 so it should be
  220. impossible to segfault your system by using this call.
  221. */
  222. #define VALGRIND_SET_VBITS(zza,zzvbits,zznbytes) \
  223. VALGRIND_DO_CLIENT_REQUEST_EXPR(0, \
  224. VG_USERREQ__SET_VBITS, \
  225. (const char*)(zza), \
  226. (const char*)(zzvbits), \
  227. (zznbytes), 0, 0 )
  228. #endif