memento.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /* Copyright (C) 2001-2023 Artifex Software, Inc.
  2. All Rights Reserved.
  3. This software is provided AS-IS with no warranty, either express or
  4. implied.
  5. This software is distributed under license and may not be copied,
  6. modified or distributed except as expressly authorized under the terms
  7. of the license contained in the file LICENSE in this distribution.
  8. Refer to licensing information at http://www.artifex.com or contact
  9. Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  10. CA 94129, USA, for further information.
  11. */
  12. /* Memento: A library to aid debugging of memory leaks/heap corruption.
  13. *
  14. * Usage (with C):
  15. * First, build your project with MEMENTO defined, and include this
  16. * header file wherever you use malloc, realloc or free.
  17. * This header file will use macros to point malloc, realloc and free to
  18. * point to Memento_malloc, Memento_realloc, Memento_free.
  19. *
  20. * Run your program, and all mallocs/frees/reallocs should be redirected
  21. * through here. When the program exits, you will get a list of all the
  22. * leaked blocks, together with some helpful statistics. You can get the
  23. * same list of allocated blocks at any point during program execution by
  24. * calling Memento_listBlocks();
  25. *
  26. * Every call to malloc/free/realloc counts as an 'allocation event'.
  27. * On each event Memento increments a counter. Every block is tagged with
  28. * the current counter on allocation. Every so often during program
  29. * execution, the heap is checked for consistency. By default this happens
  30. * after 1024 events, then after 2048 events, then after 4096 events, etc.
  31. * This can be changed at runtime by using Memento_setParanoia(int level).
  32. * 0 turns off such checking, 1 sets checking to happen on every event,
  33. * any positive number n sets checking to happen once every n events,
  34. * and any negative number n sets checking to happen after -n events, then
  35. * after -2n events etc.
  36. *
  37. * The default paranoia level is therefore -1024.
  38. *
  39. * Memento keeps blocks around for a while after they have been freed, and
  40. * checks them as part of these heap checks to see if they have been
  41. * written to (or are freed twice etc).
  42. *
  43. * A given heap block can be checked for consistency (it's 'pre' and
  44. * 'post' guard blocks are checked to see if they have been written to)
  45. * by calling Memento_checkBlock(void *blockAddress);
  46. *
  47. * A check of all the memory can be triggered by calling Memento_check();
  48. * (or Memento_checkAllMemory(); if you'd like it to be quieter).
  49. *
  50. * A good place to breakpoint is Memento_breakpoint, as this will then
  51. * trigger your debugger if an error is detected. This is done
  52. * automatically for debug windows builds.
  53. *
  54. * If a block is found to be corrupt, information will be printed to the
  55. * console, including the address of the block, the size of the block,
  56. * the type of corruption, the number of the block and the event on which
  57. * it last passed a check for correctness.
  58. *
  59. * If you rerun, and call Memento_paranoidAt(int event); with this number
  60. * the code will wait until it reaches that event and then start
  61. * checking the heap after every allocation event. Assuming it is a
  62. * deterministic failure, you should then find out where in your program
  63. * the error is occurring (between event x-1 and event x).
  64. *
  65. * Then you can rerun the program again, and call
  66. * Memento_breakAt(int event); and the program will call
  67. * Memento_Breakpoint() when event x is reached, enabling you to step
  68. * through.
  69. *
  70. * Memento_find(address) will tell you what block (if any) the given
  71. * address is in.
  72. *
  73. * An example:
  74. * Suppose we have a gs invocation that crashes with memory corruption.
  75. * * Build with -DMEMENTO.
  76. * * In your debugger put a breakpoint on Memento_breakpoint.
  77. * * Run the program. It will stop in Memento_inited.
  78. * * Execute Memento_setParanoia(1); (In VS use Ctrl-Alt-Q). (Note #1)
  79. * * Continue execution.
  80. * * It will detect the memory corruption on the next allocation event
  81. * after it happens, and stop in Memento_breakpoint. The console should
  82. * show something like:
  83. *
  84. * Freed blocks:
  85. * 0x172e610(size=288,num=1415) index 256 (0x172e710) onwards corrupted
  86. * Block last checked OK at allocation 1457. Now 1458.
  87. *
  88. * * This means that the block became corrupted between allocation 1457
  89. * and 1458 - so if we rerun and stop the program at 1457, we can then
  90. * step through, possibly with a data breakpoint at 0x172e710 and see
  91. * when it occurs.
  92. * * So restart the program from the beginning. When we stop after
  93. * initialisation execute Memento_breakAt(1457); (and maybe
  94. * Memento_setParanoia(1), or Memento_setParanoidAt(1457))
  95. * * Continue execution until we hit Memento_breakpoint.
  96. * * Now you can step through and watch the memory corruption happen.
  97. *
  98. * Note #1: Using Memento_setParanoia(1) can cause your program to run
  99. * very slowly. You may instead choose to use Memento_setParanoia(100)
  100. * (or some other figure). This will only exhaustively check memory on
  101. * every 100th allocation event. This trades speed for the size of the
  102. * average allocation event range in which detection of memory corruption
  103. * occurs. You may (for example) choose to run once checking every 100
  104. * allocations and discover that the corruption happens between events
  105. * X and X+100. You can then rerun using Memento_paranoidAt(X), and
  106. * it'll only start exhaustively checking when it reaches X.
  107. *
  108. * More than one memory allocator?
  109. *
  110. * If you have more than one memory allocator in the system (like for
  111. * instance the ghostscript chunk allocator, that builds on top of the
  112. * standard malloc and returns chunks itself), then there are some things
  113. * to note:
  114. *
  115. * * If the secondary allocator gets its underlying blocks from calling
  116. * malloc, then those will be checked by Memento, but 'subblocks' that
  117. * are returned to the secondary allocator will not. There is currently
  118. * no way to fix this other than trying to bypass the secondary
  119. * allocator. One way I have found to do this with the chunk allocator
  120. * is to tweak its idea of a 'large block' so that it puts every
  121. * allocation in its own chunk. Clearly this negates the point of having
  122. * a secondary allocator, and is therefore not recommended for general
  123. * use.
  124. *
  125. * * Again, if the secondary allocator gets its underlying blocks from
  126. * calling malloc (and hence Memento) leak detection should still work
  127. * (but whole blocks will be detected rather than subblocks).
  128. *
  129. * * If on every allocation attempt the secondary allocator calls into
  130. * Memento_failThisEvent(), and fails the allocation if it returns true
  131. * then more useful features can be used; firstly memory squeezing will
  132. * work, and secondly, Memento will have a "finer grained" paranoia
  133. * available to it.
  134. *
  135. * Usage with C++:
  136. *
  137. * Memento has some experimental code in it to trap new/delete (and
  138. * new[]/delete[] if required) calls.
  139. *
  140. * In order for this to work, either:
  141. *
  142. * 1) Build memento.c with the c++ compiler.
  143. *
  144. * or
  145. *
  146. * 2) Build memento.c as normal with the C compiler, then from any
  147. * one of your .cpp files, do:
  148. *
  149. * #define MEMENTO_CPP_EXTRAS_ONLY
  150. * #include "memento.c"
  151. *
  152. * In the case where MEMENTO is not defined, this will not do anything.
  153. *
  154. * Both Windows and GCC provide separate new[] and delete[] operators
  155. * for arrays. Apparently some systems do not. If this is the case for
  156. * your system, define MEMENTO_CPP_NO_ARRAY_CONSTRUCTORS.
  157. *
  158. * "libbacktrace.so failed to load"
  159. *
  160. * In order to give nice backtraces on unix, Memento will try to use
  161. * a libbacktrace dynamic library. If it can't find it, you'll see
  162. * that warning, and your backtraces won't include file/line information.
  163. *
  164. * To fix this you'll need to build your own libbacktrace. Don't worry
  165. * it's really easy:
  166. * git clone git://github.com/ianlancetaylor/libbacktrace
  167. * cd libbacktrace
  168. * ./configure
  169. * make
  170. *
  171. * This leaves the build .so as .libs/libbacktrace.so
  172. *
  173. * Memento will look for this on LD_LIBRARY_PATH, or in /opt/lib/,
  174. * or in /lib/, or in /usr/lib/, or in /usr/local/lib/. I recommend
  175. * using /opt/lib/ as this won't conflict with anything that you
  176. * get via a package manager like apt.
  177. *
  178. * sudo mkdir /opt
  179. * sudo mkdir /opt/lib
  180. * sudo cp .libs/libbacktrace.so /opt/lib/
  181. */
  182. #ifndef MEMENTO_H
  183. #include <stdlib.h>
  184. #define MEMENTO_H
  185. #ifndef MEMENTO_UNDERLYING_MALLOC
  186. #define MEMENTO_UNDERLYING_MALLOC malloc
  187. #endif
  188. #ifndef MEMENTO_UNDERLYING_FREE
  189. #define MEMENTO_UNDERLYING_FREE free
  190. #endif
  191. #ifndef MEMENTO_UNDERLYING_REALLOC
  192. #define MEMENTO_UNDERLYING_REALLOC realloc
  193. #endif
  194. #ifndef MEMENTO_UNDERLYING_CALLOC
  195. #define MEMENTO_UNDERLYING_CALLOC calloc
  196. #endif
  197. #ifndef MEMENTO_MAXALIGN
  198. #define MEMENTO_MAXALIGN (sizeof(int))
  199. #endif
  200. #define MEMENTO_PREFILL 0xa6
  201. #define MEMENTO_POSTFILL 0xa7
  202. #define MEMENTO_ALLOCFILL 0xa8
  203. #define MEMENTO_FREEFILL 0xa9
  204. #define MEMENTO_FREELIST_MAX 0x2000000
  205. int Memento_checkBlock(void *);
  206. int Memento_checkAllMemory(void);
  207. int Memento_check(void);
  208. int Memento_setParanoia(int);
  209. int Memento_paranoidAt(int);
  210. int Memento_breakAt(int);
  211. void Memento_breakOnFree(void *a);
  212. void Memento_breakOnRealloc(void *a);
  213. int Memento_getBlockNum(void *);
  214. int Memento_find(void *a);
  215. void Memento_breakpoint(void);
  216. int Memento_failAt(int);
  217. int Memento_failThisEvent(void);
  218. void Memento_listBlocks(void);
  219. void Memento_listNewBlocks(void);
  220. size_t Memento_setMax(size_t);
  221. void Memento_stats(void);
  222. void *Memento_label(void *, const char *);
  223. void Memento_tick(void);
  224. void *Memento_malloc(size_t s);
  225. void *Memento_realloc(void *, size_t s);
  226. void Memento_free(void *);
  227. void *Memento_calloc(size_t, size_t);
  228. void Memento_info(void *addr);
  229. void Memento_listBlockInfo(void);
  230. void *Memento_takeByteRef(void *blk);
  231. void *Memento_dropByteRef(void *blk);
  232. void *Memento_takeShortRef(void *blk);
  233. void *Memento_dropShortRef(void *blk);
  234. void *Memento_takeIntRef(void *blk);
  235. void *Memento_dropIntRef(void *blk);
  236. void *Memento_takeRef(void *blk);
  237. void *Memento_dropRef(void *blk);
  238. void *Memento_adjustRef(void *blk, int adjust);
  239. void *Memento_reference(void *blk);
  240. int Memento_checkPointerOrNull(void *blk);
  241. int Memento_checkBytePointerOrNull(void *blk);
  242. int Memento_checkShortPointerOrNull(void *blk);
  243. int Memento_checkIntPointerOrNull(void *blk);
  244. void Memento_startLeaking(void);
  245. void Memento_stopLeaking(void);
  246. int Memento_sequence(void);
  247. int Memento_squeezing(void);
  248. void Memento_fin(void);
  249. void Memento_bt(void);
  250. #ifdef MEMENTO
  251. #ifndef COMPILING_MEMENTO_C
  252. #define malloc Memento_malloc
  253. #define free Memento_free
  254. #define realloc Memento_realloc
  255. #define calloc Memento_calloc
  256. #endif
  257. #else
  258. #define Memento_malloc MEMENTO_UNDERLYING_MALLOC
  259. #define Memento_free MEMENTO_UNDERLYING_FREE
  260. #define Memento_realloc MEMENTO_UNDERLYING_REALLOC
  261. #define Memento_calloc MEMENTO_UNDERLYING_CALLOC
  262. #define Memento_checkBlock(A) 0
  263. #define Memento_checkAllMemory() 0
  264. #define Memento_check() 0
  265. #define Memento_setParanoia(A) 0
  266. #define Memento_paranoidAt(A) 0
  267. #define Memento_breakAt(A) 0
  268. #define Memento_breakOnFree(A) 0
  269. #define Memento_breakOnRealloc(A) 0
  270. #define Memento_getBlockNum(A) 0
  271. #define Memento_find(A) 0
  272. #define Memento_breakpoint() do {} while (0)
  273. #define Memento_failAt(A) 0
  274. #define Memento_failThisEvent() 0
  275. #define Memento_listBlocks() do {} while (0)
  276. #define Memento_listNewBlocks() do {} while (0)
  277. #define Memento_setMax(A) 0
  278. #define Memento_stats() do {} while (0)
  279. #define Memento_label(A,B) (A)
  280. #define Memento_info(A) do {} while (0)
  281. #define Memento_listBlockInfo() do {} while (0)
  282. #define Memento_takeByteRef(A) (A)
  283. #define Memento_dropByteRef(A) (A)
  284. #define Memento_takeShortRef(A) (A)
  285. #define Memento_dropShortRef(A) (A)
  286. #define Memento_takeIntRef(A) (A)
  287. #define Memento_dropIntRef(A) (A)
  288. #define Memento_takeRef(A) (A)
  289. #define Memento_dropRef(A) (A)
  290. #define Memento_adjustRef(A,V) (A)
  291. #define Memento_reference(A) (A)
  292. #define Memento_checkPointerOrNull(A) 0
  293. #define Memento_checkBytePointerOrNull(A) 0
  294. #define Memento_checkShortPointerOrNull(A) 0
  295. #define Memento_checkIntPointerOrNull(A) 0
  296. #define Memento_tick() do {} while (0)
  297. #define Memento_startLeaking() do {} while (0)
  298. #define Memento_stopLeaking() do {} while (0)
  299. #define Memento_fin() do {} while (0)
  300. #define Memento_bt() do {} while (0)
  301. #define Memento_sequence() (0)
  302. #define Memento_squeezing() (0)
  303. #endif /* MEMENTO */
  304. #endif /* MEMENTO_H */