You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

memcheck.h 15KB

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