njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 1 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2 | /*--------------------------------------------------------------------*/ |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 3 | /*--- A header file for all parts of the MemCheck tool. ---*/ |
njn25 | cac76cb | 2002-09-23 11:21:57 +0000 | [diff] [blame] | 4 | /*--- mc_include.h ---*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 5 | /*--------------------------------------------------------------------*/ |
| 6 | |
| 7 | /* |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 8 | This file is part of MemCheck, a heavyweight Valgrind tool for |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 9 | detecting memory errors. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 10 | |
sewardj | 9ebd6e0 | 2007-01-08 06:01:59 +0000 | [diff] [blame] | 11 | Copyright (C) 2000-2007 Julian Seward |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 12 | 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 | |
njn25 | cac76cb | 2002-09-23 11:21:57 +0000 | [diff] [blame] | 32 | #ifndef __MC_INCLUDE_H |
| 33 | #define __MC_INCLUDE_H |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 34 | |
njn | 44acd3e | 2005-05-13 21:39:45 +0000 | [diff] [blame] | 35 | #define MC_(str) VGAPPEND(vgMemCheck_,str) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 36 | |
| 37 | /*------------------------------------------------------------*/ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 38 | /*--- Tracking the heap ---*/ |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 39 | /*------------------------------------------------------------*/ |
| 40 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 41 | /* 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. */ |
| 45 | typedef |
| 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. */ |
| 55 | typedef |
| 56 | struct _MC_Chunk { |
| 57 | struct _MC_Chunk* next; |
| 58 | Addr data; // ptr to actual block |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 59 | SizeT szB : (sizeof(UWord)*8)-2; // size requested; 30 or 62 bits |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 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. */ |
| 66 | typedef |
| 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 | |
| 77 | extern 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); |
| 81 | extern void MC_(handle_free) ( ThreadId tid, |
| 82 | Addr p, UInt rzB, MC_AllocKind kind ); |
| 83 | |
| 84 | extern void MC_(create_mempool) ( Addr pool, UInt rzB, Bool is_zeroed ); |
| 85 | extern void MC_(destroy_mempool) ( Addr pool ); |
| 86 | extern void MC_(mempool_alloc) ( ThreadId tid, Addr pool, |
| 87 | Addr addr, SizeT size ); |
| 88 | extern void MC_(mempool_free) ( Addr pool, Addr addr ); |
sewardj | 2c1c9df | 2006-07-28 00:06:37 +0000 | [diff] [blame] | 89 | extern void MC_(mempool_trim) ( Addr pool, Addr addr, SizeT size ); |
sewardj | c740d76 | 2006-10-05 17:59:23 +0000 | [diff] [blame] | 90 | extern void MC_(move_mempool) ( Addr poolA, Addr poolB ); |
| 91 | extern void MC_(mempool_change) ( Addr pool, Addr addrA, Addr addrB, SizeT size ); |
| 92 | extern Bool MC_(mempool_exists) ( Addr pool ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 93 | |
| 94 | extern MC_Chunk* MC_(get_freed_list_head)( void ); |
| 95 | |
| 96 | /* For tracking malloc'd blocks */ |
| 97 | extern VgHashTable MC_(malloc_list); |
| 98 | |
| 99 | /* For tracking memory pools. */ |
| 100 | extern VgHashTable MC_(mempool_list); |
| 101 | |
| 102 | /* Shadow memory functions */ |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 103 | extern Bool MC_(check_mem_is_noaccess)( Addr a, SizeT len, Addr* bad_addr ); |
| 104 | extern void MC_(make_mem_noaccess) ( Addr a, SizeT len ); |
| 105 | extern void MC_(make_mem_undefined)( Addr a, SizeT len ); |
| 106 | extern void MC_(make_mem_defined) ( Addr a, SizeT len ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 107 | extern void MC_(copy_address_range_state) ( Addr src, Addr dst, SizeT len ); |
| 108 | |
| 109 | extern void MC_(print_malloc_stats) ( void ); |
| 110 | |
| 111 | extern void* MC_(malloc) ( ThreadId tid, SizeT n ); |
| 112 | extern void* MC_(__builtin_new) ( ThreadId tid, SizeT n ); |
| 113 | extern void* MC_(__builtin_vec_new) ( ThreadId tid, SizeT n ); |
| 114 | extern void* MC_(memalign) ( ThreadId tid, SizeT align, SizeT n ); |
| 115 | extern void* MC_(calloc) ( ThreadId tid, SizeT nmemb, SizeT size1 ); |
| 116 | extern void MC_(free) ( ThreadId tid, void* p ); |
| 117 | extern void MC_(__builtin_delete) ( ThreadId tid, void* p ); |
| 118 | extern void MC_(__builtin_vec_delete) ( ThreadId tid, void* p ); |
| 119 | extern void* MC_(realloc) ( ThreadId tid, void* p, SizeT new_size ); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 120 | |
| 121 | |
| 122 | /*------------------------------------------------------------*/ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 123 | /*--- Profiling of memory events ---*/ |
| 124 | /*------------------------------------------------------------*/ |
| 125 | |
| 126 | /* Define to collect detailed performance info. */ |
| 127 | /* #define MC_PROFILE_MEMORY */ |
| 128 | |
| 129 | #ifdef MC_PROFILE_MEMORY |
| 130 | # define N_PROF_EVENTS 500 |
| 131 | |
| 132 | extern UInt MC_(event_ctr)[N_PROF_EVENTS]; |
| 133 | extern HChar* MC_(event_ctr_name)[N_PROF_EVENTS]; |
| 134 | |
| 135 | # define PROF_EVENT(ev, name) \ |
| 136 | do { tl_assert((ev) >= 0 && (ev) < N_PROF_EVENTS); \ |
| 137 | /* crude and inaccurate check to ensure the same */ \ |
| 138 | /* event isn't being used with > 1 name */ \ |
| 139 | if (MC_(event_ctr_name)[ev]) \ |
| 140 | tl_assert(name == MC_(event_ctr_name)[ev]); \ |
| 141 | MC_(event_ctr)[ev]++; \ |
| 142 | MC_(event_ctr_name)[ev] = (name); \ |
| 143 | } while (False); |
| 144 | |
| 145 | #else |
| 146 | |
| 147 | # define PROF_EVENT(ev, name) /* */ |
| 148 | |
| 149 | #endif /* MC_PROFILE_MEMORY */ |
| 150 | |
| 151 | |
| 152 | /*------------------------------------------------------------*/ |
| 153 | /*--- V and A bits (Victoria & Albert ?) ---*/ |
| 154 | /*------------------------------------------------------------*/ |
| 155 | |
| 156 | /* The number of entries in the primary map can be altered. However |
| 157 | we hardwire the assumption that each secondary map covers precisely |
| 158 | 64k of address space. */ |
| 159 | #define SM_SIZE 65536 /* DO NOT CHANGE */ |
| 160 | #define SM_MASK (SM_SIZE-1) /* DO NOT CHANGE */ |
| 161 | |
| 162 | #define V_BIT_DEFINED 0 |
| 163 | #define V_BIT_UNDEFINED 1 |
| 164 | |
| 165 | #define V_BITS8_DEFINED 0 |
| 166 | #define V_BITS8_UNDEFINED 0xFF |
| 167 | |
| 168 | #define V_BITS16_DEFINED 0 |
| 169 | #define V_BITS16_UNDEFINED 0xFFFF |
| 170 | |
| 171 | #define V_BITS32_DEFINED 0 |
| 172 | #define V_BITS32_UNDEFINED 0xFFFFFFFF |
| 173 | |
| 174 | #define V_BITS64_DEFINED 0ULL |
| 175 | #define V_BITS64_UNDEFINED 0xFFFFFFFFFFFFFFFFULL |
| 176 | |
| 177 | |
| 178 | /*------------------------------------------------------------*/ |
| 179 | /*--- Leak checking ---*/ |
| 180 | /*------------------------------------------------------------*/ |
| 181 | |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 182 | /* A block is either |
| 183 | -- Proper-ly reached; a pointer to its start has been found |
| 184 | -- Interior-ly reached; only an interior pointer to it has been found |
| 185 | -- Unreached; so far, no pointers to any part of it have been found. |
| 186 | -- IndirectLeak; leaked, but referred to by another leaked block |
| 187 | */ |
| 188 | typedef |
| 189 | enum { |
| 190 | Unreached =0, |
| 191 | IndirectLeak =1, |
| 192 | Interior =2, |
| 193 | Proper =3 |
| 194 | } |
| 195 | Reachedness; |
| 196 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 197 | /* For VALGRIND_COUNT_LEAKS client request */ |
| 198 | extern SizeT MC_(bytes_leaked); |
| 199 | extern SizeT MC_(bytes_indirect); |
| 200 | extern SizeT MC_(bytes_dubious); |
| 201 | extern SizeT MC_(bytes_reachable); |
| 202 | extern SizeT MC_(bytes_suppressed); |
| 203 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 204 | typedef |
| 205 | enum { |
| 206 | LC_Off, |
| 207 | LC_Summary, |
| 208 | LC_Full, |
| 209 | } |
| 210 | LeakCheckMode; |
| 211 | |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 212 | /* A block record, used for generating err msgs. */ |
| 213 | typedef |
| 214 | struct _LossRecord { |
| 215 | struct _LossRecord* next; |
| 216 | /* Where these lost blocks were allocated. */ |
| 217 | ExeContext* allocated_at; |
| 218 | /* Their reachability. */ |
| 219 | Reachedness loss_mode; |
| 220 | /* Number of blocks and total # bytes involved. */ |
| 221 | SizeT total_bytes; |
| 222 | SizeT indirect_bytes; |
| 223 | UInt num_blocks; |
| 224 | } |
| 225 | LossRecord; |
| 226 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 227 | extern void MC_(do_detect_memory_leaks) ( |
| 228 | ThreadId tid, LeakCheckMode mode, |
| 229 | Bool (*is_within_valid_secondary) ( Addr ), |
| 230 | Bool (*is_valid_aligned_word) ( Addr ) |
| 231 | ); |
| 232 | |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 233 | extern void MC_(pp_LeakError)(UInt n_this_record, UInt n_total_records, |
| 234 | LossRecord* l); |
| 235 | |
| 236 | |
| 237 | /*------------------------------------------------------------*/ |
| 238 | /*--- Errors and suppressions ---*/ |
| 239 | /*------------------------------------------------------------*/ |
| 240 | |
| 241 | extern void MC_(record_free_error) ( ThreadId tid, Addr a ); |
| 242 | extern void MC_(record_illegal_mempool_error) ( ThreadId tid, Addr a ); |
| 243 | extern void MC_(record_freemismatch_error) ( ThreadId tid, MC_Chunk* mc ); |
| 244 | extern Bool MC_(record_leak_error) ( ThreadId tid, |
| 245 | UInt n_this_record, |
| 246 | UInt n_total_records, |
| 247 | LossRecord* lossRecord, |
| 248 | Bool print_record ); |
| 249 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 250 | /*------------------------------------------------------------*/ |
| 251 | /*--- Command line options + defaults ---*/ |
| 252 | /*------------------------------------------------------------*/ |
| 253 | |
| 254 | /* Allow loads from partially-valid addresses? default: YES */ |
| 255 | extern Bool MC_(clo_partial_loads_ok); |
| 256 | |
| 257 | /* Max volume of the freed blocks queue. */ |
sewardj | fa4ca3b | 2007-11-30 17:19:36 +0000 | [diff] [blame] | 258 | extern Long MC_(clo_freelist_vol); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 259 | |
| 260 | /* Do leak check at exit? default: NO */ |
| 261 | extern LeakCheckMode MC_(clo_leak_check); |
| 262 | |
| 263 | /* How closely should we compare ExeContexts in leak records? default: 2 */ |
| 264 | extern VgRes MC_(clo_leak_resolution); |
| 265 | |
| 266 | /* In leak check, show reachable-but-not-freed blocks? default: NO */ |
| 267 | extern Bool MC_(clo_show_reachable); |
| 268 | |
| 269 | /* Assume accesses immediately below %esp are due to gcc-2.96 bugs. |
| 270 | * default: NO */ |
| 271 | extern Bool MC_(clo_workaround_gcc296_bugs); |
| 272 | |
| 273 | /* Do undefined value checking? "No" gives Addrcheck-style behaviour, ie. |
| 274 | * faster but fewer errors found. Note that although Addrcheck had 1 bit |
| 275 | * per byte overhead vs the old Memcheck's 9 bits per byte, with this mode |
| 276 | * and compressed V bits, no memory is saved with this mode -- it's still |
| 277 | * 2 bits per byte overhead. This is a little wasteful -- it could be done |
| 278 | * with 1 bit per byte -- but lets us reuse the many shadow memory access |
| 279 | * functions. Note also that in this mode the secondary V bit table is |
| 280 | * never used. |
| 281 | * |
| 282 | * default: YES */ |
| 283 | extern Bool MC_(clo_undef_value_errors); |
| 284 | |
sewardj | eb0fa93 | 2007-11-30 21:41:40 +0000 | [diff] [blame^] | 285 | /* Fill malloc-d/free-d client blocks with a specific value? -1 if |
| 286 | not, else 0x00 .. 0xFF indicating the fill value to use. Can be |
| 287 | useful for causing programs with bad heap corruption to fail in |
| 288 | more repeatable ways. Note that malloc-filled and free-filled |
| 289 | areas are still undefined and noaccess respectively. This merely |
| 290 | causes them to contain the specified values. */ |
| 291 | extern Int MC_(clo_malloc_fill); |
| 292 | extern Int MC_(clo_free_fill); |
| 293 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 294 | |
| 295 | /*------------------------------------------------------------*/ |
| 296 | /*--- Instrumentation ---*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 297 | /*------------------------------------------------------------*/ |
| 298 | |
njn | 66fe05a | 2003-07-22 09:12:33 +0000 | [diff] [blame] | 299 | /* Functions defined in mc_main.c */ |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 300 | extern VG_REGPARM(1) void MC_(helperc_complain_undef) ( HWord ); |
sewardj | 11bcc4e | 2005-04-23 22:38:38 +0000 | [diff] [blame] | 301 | extern void MC_(helperc_value_check8_fail) ( void ); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 302 | extern void MC_(helperc_value_check4_fail) ( void ); |
| 303 | extern void MC_(helperc_value_check1_fail) ( void ); |
| 304 | extern void MC_(helperc_value_check0_fail) ( void ); |
| 305 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 306 | extern VG_REGPARM(1) void MC_(helperc_STOREV64be) ( Addr, ULong ); |
| 307 | extern VG_REGPARM(1) void MC_(helperc_STOREV64le) ( Addr, ULong ); |
| 308 | extern VG_REGPARM(2) void MC_(helperc_STOREV32be) ( Addr, UWord ); |
| 309 | extern VG_REGPARM(2) void MC_(helperc_STOREV32le) ( Addr, UWord ); |
| 310 | extern VG_REGPARM(2) void MC_(helperc_STOREV16be) ( Addr, UWord ); |
| 311 | extern VG_REGPARM(2) void MC_(helperc_STOREV16le) ( Addr, UWord ); |
| 312 | extern VG_REGPARM(2) void MC_(helperc_STOREV8) ( Addr, UWord ); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 313 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 314 | extern VG_REGPARM(1) ULong MC_(helperc_LOADV64be) ( Addr ); |
| 315 | extern VG_REGPARM(1) ULong MC_(helperc_LOADV64le) ( Addr ); |
| 316 | extern VG_REGPARM(1) UWord MC_(helperc_LOADV32be) ( Addr ); |
| 317 | extern VG_REGPARM(1) UWord MC_(helperc_LOADV32le) ( Addr ); |
| 318 | extern VG_REGPARM(1) UWord MC_(helperc_LOADV16be) ( Addr ); |
| 319 | extern VG_REGPARM(1) UWord MC_(helperc_LOADV16le) ( Addr ); |
| 320 | extern VG_REGPARM(1) UWord MC_(helperc_LOADV8) ( Addr ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 321 | |
sewardj | 826ec49 | 2005-05-12 18:05:00 +0000 | [diff] [blame] | 322 | extern void MC_(helperc_MAKE_STACK_UNINIT) ( Addr base, UWord len ); |
| 323 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 324 | /* Functions defined in mc_translate.c */ |
sewardj | 4ba057c | 2005-10-18 12:04:18 +0000 | [diff] [blame] | 325 | extern |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 326 | IRSB* MC_(instrument) ( VgCallbackClosure* closure, |
| 327 | IRSB* bb_in, |
sewardj | 461df9c | 2006-01-17 02:06:39 +0000 | [diff] [blame] | 328 | VexGuestLayout* layout, |
| 329 | VexGuestExtents* vge, |
sewardj | 4ba057c | 2005-10-18 12:04:18 +0000 | [diff] [blame] | 330 | IRType gWordTy, IRType hWordTy ); |
sewardj | 8d61eb1 | 2005-07-08 09:46:53 +0000 | [diff] [blame] | 331 | |
sewardj | 81651dc | 2007-08-28 06:05:20 +0000 | [diff] [blame] | 332 | extern |
| 333 | IRSB* MC_(final_tidy) ( IRSB* ); |
| 334 | |
sewardj | 8d61eb1 | 2005-07-08 09:46:53 +0000 | [diff] [blame] | 335 | #endif /* ndef __MC_INCLUDE_H */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 336 | |
| 337 | /*--------------------------------------------------------------------*/ |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 338 | /*--- end ---*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 339 | /*--------------------------------------------------------------------*/ |
| 340 | |