blob: 4fa50d2433d9be95cb6368f7f325685b18ffd61b [file] [log] [blame]
njnc9539842002-10-02 13:26:35 +00001
njn25e49d8e72002-09-23 09:36:25 +00002/*--------------------------------------------------------------------*/
nethercote137bc552003-11-14 17:47:54 +00003/*--- A header file for all parts of the MemCheck tool. ---*/
njn25cac76cb2002-09-23 11:21:57 +00004/*--- mc_include.h ---*/
njn25e49d8e72002-09-23 09:36:25 +00005/*--------------------------------------------------------------------*/
6
7/*
nethercote137bc552003-11-14 17:47:54 +00008 This file is part of MemCheck, a heavyweight Valgrind tool for
njnc9539842002-10-02 13:26:35 +00009 detecting memory errors.
njn25e49d8e72002-09-23 09:36:25 +000010
sewardje4b0bf02006-06-05 23:21:15 +000011 Copyright (C) 2000-2006 Julian Seward
njn25e49d8e72002-09-23 09:36:25 +000012 jseward@acm.org
13
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation; either version 2 of the
17 License, or (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 02111-1307, USA.
28
29 The GNU General Public License is contained in the file COPYING.
30*/
31
njn25cac76cb2002-09-23 11:21:57 +000032#ifndef __MC_INCLUDE_H
33#define __MC_INCLUDE_H
njn25e49d8e72002-09-23 09:36:25 +000034
njn44acd3e2005-05-13 21:39:45 +000035#define MC_(str) VGAPPEND(vgMemCheck_,str)
njn25e49d8e72002-09-23 09:36:25 +000036
37/*------------------------------------------------------------*/
njn1d0825f2006-03-27 11:37:07 +000038/*--- Tracking the heap ---*/
njn43c799e2003-04-08 00:08:52 +000039/*------------------------------------------------------------*/
40
njn1d0825f2006-03-27 11:37:07 +000041/* We want at least a 16B redzone on client heap blocks for Memcheck */
42#define MC_MALLOC_REDZONE_SZB 16
43
44/* For malloc()/new/new[] vs. free()/delete/delete[] mismatch checking. */
45typedef
46 enum {
47 MC_AllocMalloc = 0,
48 MC_AllocNew = 1,
49 MC_AllocNewVec = 2,
50 MC_AllocCustom = 3
51 }
52 MC_AllocKind;
53
54/* Nb: first two fields must match core's VgHashNode. */
55typedef
56 struct _MC_Chunk {
57 struct _MC_Chunk* next;
58 Addr data; // ptr to actual block
59 SizeT size : (sizeof(UWord)*8)-2; // size requested; 30 or 62 bits
60 MC_AllocKind allockind : 2; // which wrapper did the allocation
61 ExeContext* where; // where it was allocated
62 }
63 MC_Chunk;
64
65/* Memory pool. Nb: first two fields must match core's VgHashNode. */
66typedef
67 struct _MC_Mempool {
68 struct _MC_Mempool* next;
69 Addr pool; // pool identifier
70 SizeT rzB; // pool red-zone size
71 Bool is_zeroed; // allocations from this pool are zeroed
72 VgHashTable chunks; // chunks associated with this pool
73 }
74 MC_Mempool;
75
76
77extern void* MC_(new_block) ( ThreadId tid,
78 Addr p, SizeT size, SizeT align, UInt rzB,
79 Bool is_zeroed, MC_AllocKind kind,
80 VgHashTable table);
81extern void MC_(handle_free) ( ThreadId tid,
82 Addr p, UInt rzB, MC_AllocKind kind );
83
84extern void MC_(create_mempool) ( Addr pool, UInt rzB, Bool is_zeroed );
85extern void MC_(destroy_mempool) ( Addr pool );
86extern void MC_(mempool_alloc) ( ThreadId tid, Addr pool,
87 Addr addr, SizeT size );
88extern void MC_(mempool_free) ( Addr pool, Addr addr );
sewardj2c1c9df2006-07-28 00:06:37 +000089extern void MC_(mempool_trim) ( Addr pool, Addr addr, SizeT size );
njn1d0825f2006-03-27 11:37:07 +000090
91extern MC_Chunk* MC_(get_freed_list_head)( void );
92
93/* For tracking malloc'd blocks */
94extern VgHashTable MC_(malloc_list);
95
96/* For tracking memory pools. */
97extern VgHashTable MC_(mempool_list);
98
99/* Shadow memory functions */
njndbf7ca72006-03-31 11:57:59 +0000100extern Bool MC_(check_mem_is_noaccess)( Addr a, SizeT len, Addr* bad_addr );
101extern void MC_(make_mem_noaccess) ( Addr a, SizeT len );
102extern void MC_(make_mem_undefined)( Addr a, SizeT len );
103extern void MC_(make_mem_defined) ( Addr a, SizeT len );
njn1d0825f2006-03-27 11:37:07 +0000104extern void MC_(copy_address_range_state) ( Addr src, Addr dst, SizeT len );
105
106extern void MC_(print_malloc_stats) ( void );
107
108extern void* MC_(malloc) ( ThreadId tid, SizeT n );
109extern void* MC_(__builtin_new) ( ThreadId tid, SizeT n );
110extern void* MC_(__builtin_vec_new) ( ThreadId tid, SizeT n );
111extern void* MC_(memalign) ( ThreadId tid, SizeT align, SizeT n );
112extern void* MC_(calloc) ( ThreadId tid, SizeT nmemb, SizeT size1 );
113extern void MC_(free) ( ThreadId tid, void* p );
114extern void MC_(__builtin_delete) ( ThreadId tid, void* p );
115extern void MC_(__builtin_vec_delete) ( ThreadId tid, void* p );
116extern void* MC_(realloc) ( ThreadId tid, void* p, SizeT new_size );
njn43c799e2003-04-08 00:08:52 +0000117
118
119/*------------------------------------------------------------*/
njn1d0825f2006-03-27 11:37:07 +0000120/*--- Errors and suppressions ---*/
121/*------------------------------------------------------------*/
122
123/* Extra info for overlap errors */
124typedef
125 struct {
126 Addr src;
127 Addr dst;
128 Int len; // -1 if unused
129 }
130 OverlapExtra;
131
132extern void MC_(record_free_error) ( ThreadId tid, Addr a );
133extern void MC_(record_illegal_mempool_error) ( ThreadId tid, Addr a );
134extern void MC_(record_freemismatch_error) ( ThreadId tid, Addr a,
135 MC_Chunk* mc );
136extern Bool MC_(record_leak_error) ( ThreadId tid,
137 void* leak_extra,
138 ExeContext* where,
139 Bool print_record );
140
141/*------------------------------------------------------------*/
142/*--- Profiling of memory events ---*/
143/*------------------------------------------------------------*/
144
145/* Define to collect detailed performance info. */
146/* #define MC_PROFILE_MEMORY */
147
148#ifdef MC_PROFILE_MEMORY
149# define N_PROF_EVENTS 500
150
151extern UInt MC_(event_ctr)[N_PROF_EVENTS];
152extern HChar* MC_(event_ctr_name)[N_PROF_EVENTS];
153
154# define PROF_EVENT(ev, name) \
155 do { tl_assert((ev) >= 0 && (ev) < N_PROF_EVENTS); \
156 /* crude and inaccurate check to ensure the same */ \
157 /* event isn't being used with > 1 name */ \
158 if (MC_(event_ctr_name)[ev]) \
159 tl_assert(name == MC_(event_ctr_name)[ev]); \
160 MC_(event_ctr)[ev]++; \
161 MC_(event_ctr_name)[ev] = (name); \
162 } while (False);
163
164#else
165
166# define PROF_EVENT(ev, name) /* */
167
168#endif /* MC_PROFILE_MEMORY */
169
170
171/*------------------------------------------------------------*/
172/*--- V and A bits (Victoria & Albert ?) ---*/
173/*------------------------------------------------------------*/
174
175/* The number of entries in the primary map can be altered. However
176 we hardwire the assumption that each secondary map covers precisely
177 64k of address space. */
178#define SM_SIZE 65536 /* DO NOT CHANGE */
179#define SM_MASK (SM_SIZE-1) /* DO NOT CHANGE */
180
181#define V_BIT_DEFINED 0
182#define V_BIT_UNDEFINED 1
183
184#define V_BITS8_DEFINED 0
185#define V_BITS8_UNDEFINED 0xFF
186
187#define V_BITS16_DEFINED 0
188#define V_BITS16_UNDEFINED 0xFFFF
189
190#define V_BITS32_DEFINED 0
191#define V_BITS32_UNDEFINED 0xFFFFFFFF
192
193#define V_BITS64_DEFINED 0ULL
194#define V_BITS64_UNDEFINED 0xFFFFFFFFFFFFFFFFULL
195
196
197/*------------------------------------------------------------*/
198/*--- Leak checking ---*/
199/*------------------------------------------------------------*/
200
201/* For VALGRIND_COUNT_LEAKS client request */
202extern SizeT MC_(bytes_leaked);
203extern SizeT MC_(bytes_indirect);
204extern SizeT MC_(bytes_dubious);
205extern SizeT MC_(bytes_reachable);
206extern SizeT MC_(bytes_suppressed);
207
208/* For leak checking */
209extern void MC_(pp_LeakError)(void* extra);
210
211typedef
212 enum {
213 LC_Off,
214 LC_Summary,
215 LC_Full,
216 }
217 LeakCheckMode;
218
219extern void MC_(do_detect_memory_leaks) (
220 ThreadId tid, LeakCheckMode mode,
221 Bool (*is_within_valid_secondary) ( Addr ),
222 Bool (*is_valid_aligned_word) ( Addr )
223 );
224
225/*------------------------------------------------------------*/
226/*--- Command line options + defaults ---*/
227/*------------------------------------------------------------*/
228
229/* Allow loads from partially-valid addresses? default: YES */
230extern Bool MC_(clo_partial_loads_ok);
231
232/* Max volume of the freed blocks queue. */
233extern Int MC_(clo_freelist_vol);
234
235/* Do leak check at exit? default: NO */
236extern LeakCheckMode MC_(clo_leak_check);
237
238/* How closely should we compare ExeContexts in leak records? default: 2 */
239extern VgRes MC_(clo_leak_resolution);
240
241/* In leak check, show reachable-but-not-freed blocks? default: NO */
242extern Bool MC_(clo_show_reachable);
243
244/* Assume accesses immediately below %esp are due to gcc-2.96 bugs.
245 * default: NO */
246extern Bool MC_(clo_workaround_gcc296_bugs);
247
248/* Do undefined value checking? "No" gives Addrcheck-style behaviour, ie.
249 * faster but fewer errors found. Note that although Addrcheck had 1 bit
250 * per byte overhead vs the old Memcheck's 9 bits per byte, with this mode
251 * and compressed V bits, no memory is saved with this mode -- it's still
252 * 2 bits per byte overhead. This is a little wasteful -- it could be done
253 * with 1 bit per byte -- but lets us reuse the many shadow memory access
254 * functions. Note also that in this mode the secondary V bit table is
255 * never used.
256 *
257 * default: YES */
258extern Bool MC_(clo_undef_value_errors);
259
260
261/*------------------------------------------------------------*/
262/*--- Instrumentation ---*/
njn25e49d8e72002-09-23 09:36:25 +0000263/*------------------------------------------------------------*/
264
njn66fe05a2003-07-22 09:12:33 +0000265/* Functions defined in mc_main.c */
njnaf839f52005-06-23 03:27:57 +0000266extern VG_REGPARM(1) void MC_(helperc_complain_undef) ( HWord );
sewardj11bcc4e2005-04-23 22:38:38 +0000267extern void MC_(helperc_value_check8_fail) ( void );
sewardj95448072004-11-22 20:19:51 +0000268extern void MC_(helperc_value_check4_fail) ( void );
269extern void MC_(helperc_value_check1_fail) ( void );
270extern void MC_(helperc_value_check0_fail) ( void );
271
njn1d0825f2006-03-27 11:37:07 +0000272extern VG_REGPARM(1) void MC_(helperc_STOREV64be) ( Addr, ULong );
273extern VG_REGPARM(1) void MC_(helperc_STOREV64le) ( Addr, ULong );
274extern VG_REGPARM(2) void MC_(helperc_STOREV32be) ( Addr, UWord );
275extern VG_REGPARM(2) void MC_(helperc_STOREV32le) ( Addr, UWord );
276extern VG_REGPARM(2) void MC_(helperc_STOREV16be) ( Addr, UWord );
277extern VG_REGPARM(2) void MC_(helperc_STOREV16le) ( Addr, UWord );
278extern VG_REGPARM(2) void MC_(helperc_STOREV8) ( Addr, UWord );
sewardj95448072004-11-22 20:19:51 +0000279
njn1d0825f2006-03-27 11:37:07 +0000280extern VG_REGPARM(1) ULong MC_(helperc_LOADV64be) ( Addr );
281extern VG_REGPARM(1) ULong MC_(helperc_LOADV64le) ( Addr );
282extern VG_REGPARM(1) UWord MC_(helperc_LOADV32be) ( Addr );
283extern VG_REGPARM(1) UWord MC_(helperc_LOADV32le) ( Addr );
284extern VG_REGPARM(1) UWord MC_(helperc_LOADV16be) ( Addr );
285extern VG_REGPARM(1) UWord MC_(helperc_LOADV16le) ( Addr );
286extern VG_REGPARM(1) UWord MC_(helperc_LOADV8) ( Addr );
njn25e49d8e72002-09-23 09:36:25 +0000287
sewardj826ec492005-05-12 18:05:00 +0000288extern void MC_(helperc_MAKE_STACK_UNINIT) ( Addr base, UWord len );
289
njn51d827b2005-05-09 01:02:08 +0000290/* Functions defined in mc_translate.c */
sewardj4ba057c2005-10-18 12:04:18 +0000291extern
sewardj461df9c2006-01-17 02:06:39 +0000292IRBB* MC_(instrument) ( VgCallbackClosure* closure,
293 IRBB* bb_in,
294 VexGuestLayout* layout,
295 VexGuestExtents* vge,
sewardj4ba057c2005-10-18 12:04:18 +0000296 IRType gWordTy, IRType hWordTy );
sewardj8d61eb12005-07-08 09:46:53 +0000297
298#endif /* ndef __MC_INCLUDE_H */
njn25e49d8e72002-09-23 09:36:25 +0000299
300/*--------------------------------------------------------------------*/
nethercote8b76fe52004-11-08 19:20:09 +0000301/*--- end ---*/
njn25e49d8e72002-09-23 09:36:25 +0000302/*--------------------------------------------------------------------*/
303