memcheck.h 14 KB

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