blob: fb0e26b5e898a50595bcea72a3e7f7979949a1c3 [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
sewardj4d474d02008-02-11 11:34:59 +000011 Copyright (C) 2000-2008 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
njn718d3b12006-12-16 00:54:12 +000059 SizeT szB : (sizeof(UWord)*8)-2; // size requested; 30 or 62 bits
njn1d0825f2006-03-27 11:37:07 +000060 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 );
sewardjc740d762006-10-05 17:59:23 +000090extern void MC_(move_mempool) ( Addr poolA, Addr poolB );
91extern void MC_(mempool_change) ( Addr pool, Addr addrA, Addr addrB, SizeT size );
92extern Bool MC_(mempool_exists) ( Addr pool );
njn1d0825f2006-03-27 11:37:07 +000093
94extern MC_Chunk* MC_(get_freed_list_head)( void );
95
96/* For tracking malloc'd blocks */
97extern VgHashTable MC_(malloc_list);
98
99/* For tracking memory pools. */
100extern VgHashTable MC_(mempool_list);
101
102/* Shadow memory functions */
njndbf7ca72006-03-31 11:57:59 +0000103extern Bool MC_(check_mem_is_noaccess)( Addr a, SizeT len, Addr* bad_addr );
sewardj7cf4e6b2008-05-01 20:24:26 +0000104extern void MC_(make_mem_noaccess) ( Addr a, SizeT len );
105extern void MC_(make_mem_undefined_w_otag)( Addr a, SizeT len, UInt otag );
106extern void MC_(make_mem_defined) ( Addr a, SizeT len );
njn1d0825f2006-03-27 11:37:07 +0000107extern void MC_(copy_address_range_state) ( Addr src, Addr dst, SizeT len );
108
109extern void MC_(print_malloc_stats) ( void );
110
111extern void* MC_(malloc) ( ThreadId tid, SizeT n );
112extern void* MC_(__builtin_new) ( ThreadId tid, SizeT n );
113extern void* MC_(__builtin_vec_new) ( ThreadId tid, SizeT n );
114extern void* MC_(memalign) ( ThreadId tid, SizeT align, SizeT n );
115extern void* MC_(calloc) ( ThreadId tid, SizeT nmemb, SizeT size1 );
116extern void MC_(free) ( ThreadId tid, void* p );
117extern void MC_(__builtin_delete) ( ThreadId tid, void* p );
118extern void MC_(__builtin_vec_delete) ( ThreadId tid, void* p );
119extern void* MC_(realloc) ( ThreadId tid, void* p, SizeT new_size );
njn43c799e2003-04-08 00:08:52 +0000120
sewardj7cf4e6b2008-05-01 20:24:26 +0000121/*------------------------------------------------------------*/
122/*--- Origin tracking translate-time support ---*/
123/*------------------------------------------------------------*/
124
125/* See detailed comments in mc_machine.c. */
126extern
127Int MC_(get_otrack_shadow_offset) ( Int offset, Int szB );
128extern
129IRType MC_(get_otrack_reg_array_equiv_int_type) ( IRRegArray* arr );
130
131/* Constants which are used as the lowest 2 bits in origin tags.
132
133 An origin tag comprises an upper 30-bit ECU field and a lower 2-bit
134 'kind' field. The ECU field is a number given out by m_execontext
135 and has a 1-1 mapping with ExeContext*s. An ECU can be used
136 directly as an origin tag (otag), but in fact we want to put
137 additional information 'kind' field to indicate roughly where the
138 tag came from. This helps print more understandable error messages
139 for the user -- it has no other purpose.
140
141 Hence the following 2-bit constants are needed for 'kind' field.
142
143 To summarise:
144
145 * Both ECUs and origin tags are represented as 32-bit words
146
147 * m_execontext and the core-tool interface deal purely in ECUs.
148 They have no knowledge of origin tags - that is a purely
149 Memcheck-internal matter.
150
151 * all valid ECUs have the lowest 2 bits zero and at least
152 one of the upper 30 bits nonzero (see VG_(is_plausible_ECU))
153
154 * to convert from an ECU to an otag, OR in one of the MC_OKIND_
155 constants below
156
157 * to convert an otag back to an ECU, AND it with ~3
158*/
159
160#define MC_OKIND_UNKNOWN 0 /* unknown origin */
161#define MC_OKIND_HEAP 1 /* this is a heap origin */
162#define MC_OKIND_STACK 2 /* this is a stack origin */
163#define MC_OKIND_USER 3 /* arises from user-supplied client req */
164
njn43c799e2003-04-08 00:08:52 +0000165
166/*------------------------------------------------------------*/
njn1d0825f2006-03-27 11:37:07 +0000167/*--- Profiling of memory events ---*/
168/*------------------------------------------------------------*/
169
170/* Define to collect detailed performance info. */
171/* #define MC_PROFILE_MEMORY */
172
173#ifdef MC_PROFILE_MEMORY
174# define N_PROF_EVENTS 500
175
176extern UInt MC_(event_ctr)[N_PROF_EVENTS];
177extern HChar* MC_(event_ctr_name)[N_PROF_EVENTS];
178
179# define PROF_EVENT(ev, name) \
180 do { tl_assert((ev) >= 0 && (ev) < N_PROF_EVENTS); \
181 /* crude and inaccurate check to ensure the same */ \
182 /* event isn't being used with > 1 name */ \
183 if (MC_(event_ctr_name)[ev]) \
184 tl_assert(name == MC_(event_ctr_name)[ev]); \
185 MC_(event_ctr)[ev]++; \
186 MC_(event_ctr_name)[ev] = (name); \
187 } while (False);
188
189#else
190
191# define PROF_EVENT(ev, name) /* */
192
193#endif /* MC_PROFILE_MEMORY */
194
195
196/*------------------------------------------------------------*/
197/*--- V and A bits (Victoria & Albert ?) ---*/
198/*------------------------------------------------------------*/
199
200/* The number of entries in the primary map can be altered. However
201 we hardwire the assumption that each secondary map covers precisely
202 64k of address space. */
203#define SM_SIZE 65536 /* DO NOT CHANGE */
204#define SM_MASK (SM_SIZE-1) /* DO NOT CHANGE */
205
206#define V_BIT_DEFINED 0
207#define V_BIT_UNDEFINED 1
208
209#define V_BITS8_DEFINED 0
210#define V_BITS8_UNDEFINED 0xFF
211
212#define V_BITS16_DEFINED 0
213#define V_BITS16_UNDEFINED 0xFFFF
214
215#define V_BITS32_DEFINED 0
216#define V_BITS32_UNDEFINED 0xFFFFFFFF
217
218#define V_BITS64_DEFINED 0ULL
219#define V_BITS64_UNDEFINED 0xFFFFFFFFFFFFFFFFULL
220
221
222/*------------------------------------------------------------*/
223/*--- Leak checking ---*/
224/*------------------------------------------------------------*/
225
njn718d3b12006-12-16 00:54:12 +0000226/* A block is either
227 -- Proper-ly reached; a pointer to its start has been found
228 -- Interior-ly reached; only an interior pointer to it has been found
229 -- Unreached; so far, no pointers to any part of it have been found.
230 -- IndirectLeak; leaked, but referred to by another leaked block
231*/
232typedef
233 enum {
234 Unreached =0,
235 IndirectLeak =1,
236 Interior =2,
237 Proper =3
238 }
239 Reachedness;
240
njn1d0825f2006-03-27 11:37:07 +0000241/* For VALGRIND_COUNT_LEAKS client request */
242extern SizeT MC_(bytes_leaked);
243extern SizeT MC_(bytes_indirect);
244extern SizeT MC_(bytes_dubious);
245extern SizeT MC_(bytes_reachable);
246extern SizeT MC_(bytes_suppressed);
247
njn1d0825f2006-03-27 11:37:07 +0000248typedef
249 enum {
250 LC_Off,
251 LC_Summary,
252 LC_Full,
253 }
254 LeakCheckMode;
255
njn718d3b12006-12-16 00:54:12 +0000256/* A block record, used for generating err msgs. */
257typedef
258 struct _LossRecord {
259 struct _LossRecord* next;
260 /* Where these lost blocks were allocated. */
261 ExeContext* allocated_at;
262 /* Their reachability. */
263 Reachedness loss_mode;
264 /* Number of blocks and total # bytes involved. */
265 SizeT total_bytes;
266 SizeT indirect_bytes;
267 UInt num_blocks;
268 }
269 LossRecord;
270
njn1d0825f2006-03-27 11:37:07 +0000271extern void MC_(do_detect_memory_leaks) (
272 ThreadId tid, LeakCheckMode mode,
273 Bool (*is_within_valid_secondary) ( Addr ),
274 Bool (*is_valid_aligned_word) ( Addr )
275 );
276
njn718d3b12006-12-16 00:54:12 +0000277extern void MC_(pp_LeakError)(UInt n_this_record, UInt n_total_records,
278 LossRecord* l);
279
280
281/*------------------------------------------------------------*/
282/*--- Errors and suppressions ---*/
283/*------------------------------------------------------------*/
284
285extern void MC_(record_free_error) ( ThreadId tid, Addr a );
286extern void MC_(record_illegal_mempool_error) ( ThreadId tid, Addr a );
287extern void MC_(record_freemismatch_error) ( ThreadId tid, MC_Chunk* mc );
288extern Bool MC_(record_leak_error) ( ThreadId tid,
289 UInt n_this_record,
290 UInt n_total_records,
291 LossRecord* lossRecord,
292 Bool print_record );
293
njn1d0825f2006-03-27 11:37:07 +0000294/*------------------------------------------------------------*/
295/*--- Command line options + defaults ---*/
296/*------------------------------------------------------------*/
297
298/* Allow loads from partially-valid addresses? default: YES */
299extern Bool MC_(clo_partial_loads_ok);
300
301/* Max volume of the freed blocks queue. */
sewardjfa4ca3b2007-11-30 17:19:36 +0000302extern Long MC_(clo_freelist_vol);
njn1d0825f2006-03-27 11:37:07 +0000303
304/* Do leak check at exit? default: NO */
305extern LeakCheckMode MC_(clo_leak_check);
306
307/* How closely should we compare ExeContexts in leak records? default: 2 */
308extern VgRes MC_(clo_leak_resolution);
309
310/* In leak check, show reachable-but-not-freed blocks? default: NO */
311extern Bool MC_(clo_show_reachable);
312
313/* Assume accesses immediately below %esp are due to gcc-2.96 bugs.
314 * default: NO */
315extern Bool MC_(clo_workaround_gcc296_bugs);
316
sewardjeb0fa932007-11-30 21:41:40 +0000317/* Fill malloc-d/free-d client blocks with a specific value? -1 if
318 not, else 0x00 .. 0xFF indicating the fill value to use. Can be
319 useful for causing programs with bad heap corruption to fail in
320 more repeatable ways. Note that malloc-filled and free-filled
321 areas are still undefined and noaccess respectively. This merely
322 causes them to contain the specified values. */
323extern Int MC_(clo_malloc_fill);
324extern Int MC_(clo_free_fill);
325
sewardj7cf4e6b2008-05-01 20:24:26 +0000326/* Indicates the level of instrumentation/checking done by Memcheck.
327
328 1 = No undefined value checking, Addrcheck-style behaviour only:
329 only address checking is done. This is faster but finds fewer
330 errors. Note that although Addrcheck had 1 bit per byte
331 overhead vs the old Memcheck's 9 bits per byte, with this mode
332 and compressed V bits, no memory is saved with this mode --
333 it's still 2 bits per byte overhead. This is a little wasteful
334 -- it could be done with 1 bit per byte -- but lets us reuse
335 the many shadow memory access functions. Note that in this
336 mode neither the secondary V bit table nor the origin-tag cache
337 are used.
338
339 2 = Address checking and Undefined value checking are performed,
340 but origins are not tracked. So the origin-tag cache is not
341 used in this mode. This setting is the default and corresponds
342 to the "normal" Memcheck behaviour that has shipped for years.
343
344 3 = Address checking, undefined value checking, and origins for
345 undefined values are tracked.
346
347 The default is 2.
348*/
349extern Int MC_(clo_mc_level);
350
njn1d0825f2006-03-27 11:37:07 +0000351
352/*------------------------------------------------------------*/
353/*--- Instrumentation ---*/
njn25e49d8e72002-09-23 09:36:25 +0000354/*------------------------------------------------------------*/
355
njn66fe05a2003-07-22 09:12:33 +0000356/* Functions defined in mc_main.c */
sewardj95448072004-11-22 20:19:51 +0000357
sewardj7cf4e6b2008-05-01 20:24:26 +0000358/* For the fail_w_o functions, the UWord arg is actually the 32-bit
359 origin tag and should really be UInt, but to be simple and safe
360 considering it's called from generated code, just claim it to be a
361 UWord. */
362extern VG_REGPARM(2) void MC_(helperc_value_checkN_fail_w_o) ( HWord, UWord );
363extern VG_REGPARM(1) void MC_(helperc_value_check8_fail_w_o) ( UWord );
364extern VG_REGPARM(1) void MC_(helperc_value_check4_fail_w_o) ( UWord );
365extern VG_REGPARM(1) void MC_(helperc_value_check1_fail_w_o) ( UWord );
366extern VG_REGPARM(1) void MC_(helperc_value_check0_fail_w_o) ( UWord );
367
368/* And call these ones instead to report an uninitialised value error
369 but with no origin available. */
370extern VG_REGPARM(1) void MC_(helperc_value_checkN_fail_no_o) ( HWord );
371extern VG_REGPARM(0) void MC_(helperc_value_check8_fail_no_o) ( void );
372extern VG_REGPARM(0) void MC_(helperc_value_check4_fail_no_o) ( void );
373extern VG_REGPARM(0) void MC_(helperc_value_check1_fail_no_o) ( void );
374extern VG_REGPARM(0) void MC_(helperc_value_check0_fail_no_o) ( void );
375
376/* V-bits load/store helpers */
njn1d0825f2006-03-27 11:37:07 +0000377extern VG_REGPARM(1) void MC_(helperc_STOREV64be) ( Addr, ULong );
378extern VG_REGPARM(1) void MC_(helperc_STOREV64le) ( Addr, ULong );
379extern VG_REGPARM(2) void MC_(helperc_STOREV32be) ( Addr, UWord );
380extern VG_REGPARM(2) void MC_(helperc_STOREV32le) ( Addr, UWord );
381extern VG_REGPARM(2) void MC_(helperc_STOREV16be) ( Addr, UWord );
382extern VG_REGPARM(2) void MC_(helperc_STOREV16le) ( Addr, UWord );
383extern VG_REGPARM(2) void MC_(helperc_STOREV8) ( Addr, UWord );
sewardj95448072004-11-22 20:19:51 +0000384
njn1d0825f2006-03-27 11:37:07 +0000385extern VG_REGPARM(1) ULong MC_(helperc_LOADV64be) ( Addr );
386extern VG_REGPARM(1) ULong MC_(helperc_LOADV64le) ( Addr );
387extern VG_REGPARM(1) UWord MC_(helperc_LOADV32be) ( Addr );
388extern VG_REGPARM(1) UWord MC_(helperc_LOADV32le) ( Addr );
389extern VG_REGPARM(1) UWord MC_(helperc_LOADV16be) ( Addr );
390extern VG_REGPARM(1) UWord MC_(helperc_LOADV16le) ( Addr );
391extern VG_REGPARM(1) UWord MC_(helperc_LOADV8) ( Addr );
njn25e49d8e72002-09-23 09:36:25 +0000392
sewardj7cf4e6b2008-05-01 20:24:26 +0000393extern void MC_(helperc_MAKE_STACK_UNINIT) ( Addr base, UWord len,
394 Addr nia );
395
396/* Origin tag load/store helpers */
397VG_REGPARM(2) void MC_(helperc_b_store1) ( Addr a, UWord d32 );
398VG_REGPARM(2) void MC_(helperc_b_store2) ( Addr a, UWord d32 );
399VG_REGPARM(2) void MC_(helperc_b_store4) ( Addr a, UWord d32 );
400VG_REGPARM(2) void MC_(helperc_b_store8) ( Addr a, UWord d32 );
401VG_REGPARM(2) void MC_(helperc_b_store16)( Addr a, UWord d32 );
402VG_REGPARM(1) UWord MC_(helperc_b_load1) ( Addr a );
403VG_REGPARM(1) UWord MC_(helperc_b_load2) ( Addr a );
404VG_REGPARM(1) UWord MC_(helperc_b_load4) ( Addr a );
405VG_REGPARM(1) UWord MC_(helperc_b_load8) ( Addr a );
406VG_REGPARM(1) UWord MC_(helperc_b_load16)( Addr a );
sewardj826ec492005-05-12 18:05:00 +0000407
njn51d827b2005-05-09 01:02:08 +0000408/* Functions defined in mc_translate.c */
sewardj4ba057c2005-10-18 12:04:18 +0000409extern
sewardj0b9d74a2006-12-24 02:24:11 +0000410IRSB* MC_(instrument) ( VgCallbackClosure* closure,
411 IRSB* bb_in,
sewardj461df9c2006-01-17 02:06:39 +0000412 VexGuestLayout* layout,
413 VexGuestExtents* vge,
sewardj4ba057c2005-10-18 12:04:18 +0000414 IRType gWordTy, IRType hWordTy );
sewardj8d61eb12005-07-08 09:46:53 +0000415
sewardj81651dc2007-08-28 06:05:20 +0000416extern
417IRSB* MC_(final_tidy) ( IRSB* );
418
sewardj8d61eb12005-07-08 09:46:53 +0000419#endif /* ndef __MC_INCLUDE_H */
njn25e49d8e72002-09-23 09:36:25 +0000420
421/*--------------------------------------------------------------------*/
nethercote8b76fe52004-11-08 19:20:09 +0000422/*--- end ---*/
njn25e49d8e72002-09-23 09:36:25 +0000423/*--------------------------------------------------------------------*/
424