/* Copyright (C) 2001-2013 Artifex Software, Inc.1All Rights Reserved.23This software is provided AS-IS with no warranty, either express or4implied.56This software is distributed under license and may not be copied, modified7or distributed except as expressly authorized under the terms of that8license. Refer to licensing information at http://www.artifex.com9or contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,10San Rafael, CA 94903, U.S.A., +1(415)492-9861, for further information.11*/1213/* Memento: A library to aid debugging of memory leaks/heap corruption.14*15* Usage:16* First, build your project with MEMENTO defined, and include this17* header file wherever you use malloc, realloc or free.18* This header file will use macros to point malloc, realloc and free to19* point to Memento_malloc, Memento_realloc, Memento_free.20*21* Run your program, and all mallocs/frees/reallocs should be redirected22* through here. When the program exits, you will get a list of all the23* leaked blocks, together with some helpful statistics. You can get the24* same list of allocated blocks at any point during program execution by25* calling Memento_listBlocks();26*27* Every call to malloc/free/realloc counts as an 'allocation event'.28* On each event Memento increments a counter. Every block is tagged with29* the current counter on allocation. Every so often during program30* execution, the heap is checked for consistency. By default this happens31* every 1024 events. This can be changed at runtime by using32* Memento_setParanoia(int level). 0 turns off such checking, 1 sets33* checking to happen on every event, any other number n sets checking to34* happen once every n events.35*36* Memento keeps blocks around for a while after they have been freed, and37* checks them as part of these heap checks to see if they have been38* written to (or are freed twice etc).39*40* A given heap block can be checked for consistency (it's 'pre' and41* 'post' guard blocks are checked to see if they have been written to)42* by calling Memento_checkBlock(void *blockAddress);43*44* A check of all the memory can be triggered by calling Memento_check();45* (or Memento_checkAllMemory(); if you'd like it to be quieter).46*47* A good place to breakpoint is Memento_breakpoint, as this will then48* trigger your debugger if an error is detected. This is done49* automatically for debug windows builds.50*51* If a block is found to be corrupt, information will be printed to the52* console, including the address of the block, the size of the block,53* the type of corruption, the number of the block and the event on which54* it last passed a check for correctness.55*56* If you rerun, and call Memento_paranoidAt(int event); with this number57* the the code will wait until it reaches that event and then start58* checking the heap after every allocation event. Assuming it is a59* deterministic failure, you should then find out where in your program60* the error is occurring (between event x-1 and event x).61*62* Then you can rerun the program again, and call63* Memento_breakAt(int event); and the program will call64* Memento_Breakpoint() when event x is reached, enabling you to step65* through.66*67* Memento_find(address) will tell you what block (if any) the given68* address is in.69*70* An example:71* Suppose we have a gs invocation that crashes with memory corruption.72* * Build with -DMEMENTO.73* * In your debugger put breakpoints on Memento_inited and74* Memento_Breakpoint.75* * Run the program. It will stop in Memento_inited.76* * Execute Memento_setParanoia(1); (In VS use Ctrl-Alt-Q). (Note #1)77* * Continue execution.78* * It will detect the memory corruption on the next allocation event79* after it happens, and stop in Memento_breakpoint. The console should80* show something like:81*82* Freed blocks:83* 0x172e610(size=288,num=1415) index 256 (0x172e710) onwards corrupted84* Block last checked OK at allocation 1457. Now 1458.85*86* * This means that the block became corrupted between allocation 145787* and 1458 - so if we rerun and stop the program at 1457, we can then88* step through, possibly with a data breakpoint at 0x172e710 and see89* when it occurs.90* * So restart the program from the beginning. When we hit Memento_inited91* execute Memento_breakAt(1457); (and maybe Memento_setParanoia(1), or92* Memento_setParanoidAt(1457))93* * Continue execution until we hit Memento_breakpoint.94* * Now you can step through and watch the memory corruption happen.95*96* Note #1: Using Memento_setParanoia(1) can cause your program to run97* very slowly. You may instead choose to use Memento_setParanoia(100)98* (or some other figure). This will only exhaustively check memory on99* every 100th allocation event. This trades speed for the size of the100* average allocation event range in which detection of memory corruption101* occurs. You may (for example) choose to run once checking every 100102* allocations and discover that the corruption happens between events103* X and X+100. You can then rerun using Memento_paranoidAt(X), and104* it'll only start exhaustively checking when it reaches X.105*106* More than one memory allocator?107*108* If you have more than one memory allocator in the system (like for109* instance the ghostscript chunk allocator, that builds on top of the110* standard malloc and returns chunks itself), then there are some things111* to note:112*113* * If the secondary allocator gets its underlying blocks from calling114* malloc, then those will be checked by Memento, but 'subblocks' that115* are returned to the secondary allocator will not. There is currently116* no way to fix this other than trying to bypass the secondary117* allocator. One way I have found to do this with the chunk allocator118* is to tweak its idea of a 'large block' so that it puts every119* allocation in its own chunk. Clearly this negates the point of having120* a secondary allocator, and is therefore not recommended for general121* use.122*123* * Again, if the secondary allocator gets its underlying blocks from124* calling malloc (and hence Memento) leak detection should still work125* (but whole blocks will be detected rather than subblocks).126*127* * If on every allocation attempt the secondary allocator calls into128* Memento_failThisEvent(), and fails the allocation if it returns true129* then more useful features can be used; firstly memory squeezing will130* work, and secondly, Memento will have a "finer grained" paranoia131* available to it.132*/133134#ifndef MEMENTO_H135136#include <memory.h>137138#ifdef __ANDROID__139#define MEMENTO_ANDROID140#include <stdio.h>141#include <stdlib.h>142#endif143144#define MEMENTO_H145146#ifndef MEMENTO_UNDERLYING_MALLOC147#define MEMENTO_UNDERLYING_MALLOC malloc148#endif149#ifndef MEMENTO_UNDERLYING_FREE150#define MEMENTO_UNDERLYING_FREE free151#endif152#ifndef MEMENTO_UNDERLYING_REALLOC153#define MEMENTO_UNDERLYING_REALLOC realloc154#endif155#ifndef MEMENTO_UNDERLYING_CALLOC156#define MEMENTO_UNDERLYING_CALLOC calloc157#endif158159#ifndef MEMENTO_MAXALIGN160#define MEMENTO_MAXALIGN (sizeof(int))161#endif162163#define MEMENTO_PREFILL 0xa6164#define MEMENTO_POSTFILL 0xa7165#define MEMENTO_ALLOCFILL 0xa8166#define MEMENTO_FREEFILL 0xa9167168#define MEMENTO_FREELIST_MAX 0x2000000169170int Memento_checkBlock(void *);171int Memento_checkAllMemory(void);172int Memento_check(void);173174int Memento_setParanoia(int);175int Memento_paranoidAt(int);176int Memento_breakAt(int);177void Memento_breakOnFree(void *a);178void Memento_breakOnRealloc(void *a);179int Memento_getBlockNum(void *);180int Memento_find(void *a);181void Memento_breakpoint(void);182int Memento_failAt(int);183int Memento_failThisEvent(void);184void Memento_listBlocks(void);185void Memento_listNewBlocks(void);186size_t Memento_setMax(size_t);187void Memento_stats(void);188void *Memento_label(void *, const char *);189190void *Memento_malloc(size_t s);191void *Memento_realloc(void *, size_t s);192void Memento_free(void *);193void *Memento_calloc(size_t, size_t);194195#ifdef MEMENTO196197#ifndef COMPILING_MEMENTO_C198#define malloc Memento_malloc199#define free Memento_free200#define realloc Memento_realloc201#define calloc Memento_calloc202#endif203204#else205206#define Memento_malloc MEMENTO_UNDERLYING_MALLOC207#define Memento_free MEMENTO_UNDERLYING_FREE208#define Memento_realloc MEMENTO_UNDERLYING_REALLOC209#define Memento_calloc MEMENTO_UNDERLYING_CALLOC210211#define Memento_checkBlock(A) 0212#define Memento_checkAllMemory() 0213#define Memento_check() 0214#define Memento_setParanoia(A) 0215#define Memento_paranoidAt(A) 0216#define Memento_breakAt(A) 0217#define Memento_breakOnFree(A) 0218#define Memento_breakOnRealloc(A) 0219#define Memento_getBlockNum(A) 0220#define Memento_find(A) 0221#define Memento_breakpoint() do {} while (0)222#define Memento_failAt(A) 0223#define Memento_failThisEvent() 0224#define Memento_listBlocks() do {} while (0)225#define Memento_listNewBlocks() do {} while (0)226#define Memento_setMax(A) 0227#define Memento_stats() do {} while (0)228#define Memento_label(A,B) (A)229230#endif /* MEMENTO */231232#endif /* MEMENTO_H */233234235