njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 3 | /*--- Declarations shared between MemCheck and AddrCheck. ---*/ |
| 4 | /*--- mac_shared.h ---*/ |
njn | 5c004e4 | 2002-11-18 11:04:50 +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 |
| 9 | detecting memory errors, and AddrCheck, a lightweight Valgrind tool |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 10 | for detecting memory errors. |
| 11 | |
nethercote | bb1c991 | 2004-01-04 16:43:23 +0000 | [diff] [blame] | 12 | Copyright (C) 2000-2004 Julian Seward |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 13 | jseward@acm.org |
| 14 | |
| 15 | This program is free software; you can redistribute it and/or |
| 16 | modify it under the terms of the GNU General Public License as |
| 17 | published by the Free Software Foundation; either version 2 of the |
| 18 | License, or (at your option) any later version. |
| 19 | |
| 20 | This program is distributed in the hope that it will be useful, but |
| 21 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 23 | General Public License for more details. |
| 24 | |
| 25 | You should have received a copy of the GNU General Public License |
| 26 | along with this program; if not, write to the Free Software |
| 27 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 28 | 02111-1307, USA. |
| 29 | |
| 30 | The GNU General Public License is contained in the file COPYING. |
| 31 | */ |
| 32 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 33 | /* Note: This header contains the declarations shared between |
| 34 | Addrcheck and Memcheck, and is #included by both. */ |
| 35 | |
| 36 | #ifndef __MAC_SHARED_H |
| 37 | #define __MAC_SHARED_H |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 38 | |
nethercote | 4606320 | 2004-09-02 08:51:43 +0000 | [diff] [blame] | 39 | #include "tool.h" |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 40 | |
| 41 | #define MAC_(str) VGAPPEND(vgMAC_,str) |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 42 | |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 43 | /*------------------------------------------------------------*/ |
| 44 | /*--- Errors and suppressions ---*/ |
| 45 | /*------------------------------------------------------------*/ |
| 46 | |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 47 | /* The classification of a faulting address. */ |
| 48 | typedef |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 49 | enum { |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame^] | 50 | Undescribed, // as-yet unclassified |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 51 | Stack, |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame^] | 52 | Unknown, // classification yielded nothing useful |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 53 | Freed, Mallocd, |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame^] | 54 | UserG, // in a user-defined block |
| 55 | Mempool, // in a mempool |
| 56 | Register, // in a register; for Param errors only |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 57 | } |
| 58 | AddrKind; |
| 59 | |
| 60 | /* Records info about a faulting address. */ |
| 61 | typedef |
nethercote | 05675c8 | 2004-08-04 10:37:49 +0000 | [diff] [blame] | 62 | struct { // Used by: |
| 63 | AddrKind akind; // ALL |
nethercote | 928a5f7 | 2004-11-03 18:10:37 +0000 | [diff] [blame] | 64 | SizeT blksize; // Freed, Mallocd |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame^] | 65 | OffT rwoffset; // Freed, Mallocd |
nethercote | 05675c8 | 2004-08-04 10:37:49 +0000 | [diff] [blame] | 66 | ExeContext* lastchange; // Freed, Mallocd |
| 67 | ThreadId stack_tid; // Stack |
| 68 | Bool maybe_gcc; // True if just below %esp -- could be a gcc bug. |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 69 | } |
| 70 | AddrInfo; |
| 71 | |
| 72 | typedef |
| 73 | enum { |
nethercote | 05675c8 | 2004-08-04 10:37:49 +0000 | [diff] [blame] | 74 | ParamSupp, // Bad syscall params |
| 75 | CoreMemSupp, // Memory errors in core (pthread ops, signal handling) |
| 76 | |
| 77 | // Use of invalid values of given size (MemCheck only) |
njn | c061666 | 2003-06-12 09:58:41 +0000 | [diff] [blame] | 78 | Value0Supp, Value1Supp, Value2Supp, Value4Supp, Value8Supp, Value16Supp, |
nethercote | 05675c8 | 2004-08-04 10:37:49 +0000 | [diff] [blame] | 79 | |
| 80 | // Invalid read/write attempt at given size |
njn | c061666 | 2003-06-12 09:58:41 +0000 | [diff] [blame] | 81 | Addr1Supp, Addr2Supp, Addr4Supp, Addr8Supp, Addr16Supp, |
nethercote | 05675c8 | 2004-08-04 10:37:49 +0000 | [diff] [blame] | 82 | |
| 83 | FreeSupp, // Invalid or mismatching free |
| 84 | OverlapSupp, // Overlapping blocks in memcpy(), strcpy(), etc |
| 85 | LeakSupp, // Something to be suppressed in a leak check. |
| 86 | MempoolSupp, // Memory pool suppression. |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 87 | } |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 88 | MAC_SuppKind; |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 89 | |
| 90 | /* What kind of error it is. */ |
| 91 | typedef |
| 92 | enum { ValueErr, /* Memcheck only */ |
| 93 | CoreMemErr, |
| 94 | AddrErr, |
| 95 | ParamErr, UserErr, /* behaves like an anonymous ParamErr */ |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 96 | FreeErr, FreeMismatchErr, |
njn | 7201b2a | 2003-08-19 12:16:05 +0000 | [diff] [blame] | 97 | OverlapErr, |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 98 | LeakErr, |
| 99 | IllegalMempoolErr, |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 100 | } |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 101 | MAC_ErrorKind; |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 102 | |
| 103 | /* What kind of memory access is involved in the error? */ |
| 104 | typedef |
| 105 | enum { ReadAxs, WriteAxs, ExecAxs } |
| 106 | AxsKind; |
| 107 | |
| 108 | /* Extra context for memory errors */ |
| 109 | typedef |
nethercote | 05675c8 | 2004-08-04 10:37:49 +0000 | [diff] [blame] | 110 | struct { // Used by: |
| 111 | AxsKind axskind; // AddrErr |
| 112 | Int size; // AddrErr, ValueErr |
| 113 | AddrInfo addrinfo; // {Addr,Free,FreeMismatch,Param,User}Err |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame^] | 114 | Bool isUnaddr; // {CoreMem,Param,User}Err |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 115 | } |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 116 | MAC_Error; |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 117 | |
njn | b6cae9f | 2003-09-04 20:50:47 +0000 | [diff] [blame] | 118 | /* Extra info for overlap errors */ |
| 119 | typedef |
| 120 | struct { |
| 121 | Addr src; |
| 122 | Addr dst; |
| 123 | Int len; // -1 if unused |
| 124 | } |
| 125 | OverlapExtra; |
| 126 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 127 | /* For malloc()/new/new[] vs. free()/delete/delete[] mismatch checking. */ |
| 128 | typedef |
| 129 | enum { |
| 130 | MAC_AllocMalloc = 0, |
| 131 | MAC_AllocNew = 1, |
njn | 1078545 | 2003-05-20 16:38:24 +0000 | [diff] [blame] | 132 | MAC_AllocNewVec = 2, |
| 133 | MAC_AllocCustom = 3 |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 134 | } |
| 135 | MAC_AllocKind; |
| 136 | |
| 137 | /* Nb: first two fields must match core's VgHashNode. */ |
| 138 | typedef |
| 139 | struct _MAC_Chunk { |
| 140 | struct _MAC_Chunk* next; |
nethercote | 05675c8 | 2004-08-04 10:37:49 +0000 | [diff] [blame] | 141 | Addr data; // ptr to actual block |
| 142 | UInt size : 30; // size requested |
| 143 | MAC_AllocKind allockind : 2; // which wrapper did the allocation |
| 144 | ExeContext* where; // where it was allocated |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 145 | } |
| 146 | MAC_Chunk; |
| 147 | |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 148 | /* Memory pool. Nb: first two fields must match core's VgHashNode. */ |
| 149 | typedef |
| 150 | struct _MAC_Mempool { |
| 151 | struct _MAC_Mempool* next; |
nethercote | 05675c8 | 2004-08-04 10:37:49 +0000 | [diff] [blame] | 152 | Addr pool; // pool identifier |
| 153 | UInt rzB; // pool red-zone size |
| 154 | Bool is_zeroed; // allocations from this pool are zeroed |
| 155 | VgHashTable chunks; // chunks associated with this pool |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 156 | } |
| 157 | MAC_Mempool; |
| 158 | |
| 159 | |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 160 | /*------------------------------------------------------------*/ |
nethercote | 7cc9c23 | 2004-01-21 15:08:04 +0000 | [diff] [blame] | 161 | /*--- Profiling of tools and memory events ---*/ |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 162 | /*------------------------------------------------------------*/ |
| 163 | |
| 164 | typedef |
| 165 | enum { |
| 166 | VgpCheckMem = VgpFini+1, |
| 167 | VgpSetMem, |
| 168 | VgpESPAdj |
| 169 | } |
nethercote | 7cc9c23 | 2004-01-21 15:08:04 +0000 | [diff] [blame] | 170 | VgpToolCC; |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 171 | |
| 172 | /* Define to collect detailed performance info. */ |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 173 | /* #define MAC_PROFILE_MEMORY */ |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 174 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 175 | #ifdef MAC_PROFILE_MEMORY |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 176 | # define N_PROF_EVENTS 150 |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 177 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 178 | extern UInt MAC_(event_ctr)[N_PROF_EVENTS]; |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 179 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 180 | # define PROF_EVENT(ev) \ |
| 181 | do { sk_assert((ev) >= 0 && (ev) < N_PROF_EVENTS); \ |
| 182 | MAC_(event_ctr)[ev]++; \ |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 183 | } while (False); |
| 184 | |
| 185 | #else |
| 186 | |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 187 | # define PROF_EVENT(ev) /* */ |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 188 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 189 | #endif /* MAC_PROFILE_MEMORY */ |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 190 | |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 191 | /*------------------------------------------------------------*/ |
| 192 | /*--- V and A bits ---*/ |
| 193 | /*------------------------------------------------------------*/ |
| 194 | |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 195 | #define IS_DISTINGUISHED_SM(smap) \ |
| 196 | ((smap) == &distinguished_secondary_map) |
| 197 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 198 | #define ENSURE_MAPPABLE(addr,caller) \ |
| 199 | do { \ |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 200 | if (IS_DISTINGUISHED_SM(primary_map[(addr) >> 16])) { \ |
| 201 | primary_map[(addr) >> 16] = alloc_secondary_map(caller); \ |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 202 | /* VG_(printf)("new 2map because of %p\n", addr); */ \ |
| 203 | } \ |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 204 | } while(0) |
| 205 | |
| 206 | #define BITARR_SET(aaa_p,iii_p) \ |
| 207 | do { \ |
| 208 | UInt iii = (UInt)iii_p; \ |
| 209 | UChar* aaa = (UChar*)aaa_p; \ |
| 210 | aaa[iii >> 3] |= (1 << (iii & 7)); \ |
| 211 | } while (0) |
| 212 | |
| 213 | #define BITARR_CLEAR(aaa_p,iii_p) \ |
| 214 | do { \ |
| 215 | UInt iii = (UInt)iii_p; \ |
| 216 | UChar* aaa = (UChar*)aaa_p; \ |
| 217 | aaa[iii >> 3] &= ~(1 << (iii & 7)); \ |
| 218 | } while (0) |
| 219 | |
| 220 | #define BITARR_TEST(aaa_p,iii_p) \ |
| 221 | (0 != (((UChar*)aaa_p)[ ((UInt)iii_p) >> 3 ] \ |
| 222 | & (1 << (((UInt)iii_p) & 7)))) \ |
| 223 | |
| 224 | |
| 225 | #define VGM_BIT_VALID 0 |
| 226 | #define VGM_BIT_INVALID 1 |
| 227 | |
| 228 | #define VGM_NIBBLE_VALID 0 |
| 229 | #define VGM_NIBBLE_INVALID 0xF |
| 230 | |
| 231 | #define VGM_BYTE_VALID 0 |
| 232 | #define VGM_BYTE_INVALID 0xFF |
| 233 | |
| 234 | #define VGM_WORD_VALID 0 |
| 235 | #define VGM_WORD_INVALID 0xFFFFFFFF |
| 236 | |
| 237 | #define VGM_EFLAGS_VALID 0xFFFFFFFE |
| 238 | #define VGM_EFLAGS_INVALID 0xFFFFFFFF /* not used */ |
| 239 | |
| 240 | /*------------------------------------------------------------*/ |
| 241 | /*--- Command line options + defaults ---*/ |
| 242 | /*------------------------------------------------------------*/ |
| 243 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 244 | /* Memcheck defines a couple more. */ |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 245 | |
| 246 | /* Allow loads from partially-valid addresses? default: YES */ |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 247 | extern Bool MAC_(clo_partial_loads_ok); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 248 | |
| 249 | /* Max volume of the freed blocks queue. */ |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 250 | extern Int MAC_(clo_freelist_vol); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 251 | |
| 252 | /* Do leak check at exit? default: NO */ |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 253 | extern Bool MAC_(clo_leak_check); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 254 | |
| 255 | /* How closely should we compare ExeContexts in leak records? default: 2 */ |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 256 | extern VgRes MAC_(clo_leak_resolution); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 257 | |
| 258 | /* In leak check, show reachable-but-not-freed blocks? default: NO */ |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 259 | extern Bool MAC_(clo_show_reachable); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 260 | |
| 261 | /* Assume accesses immediately below %esp are due to gcc-2.96 bugs. |
| 262 | * default: NO*/ |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 263 | extern Bool MAC_(clo_workaround_gcc296_bugs); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 264 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 265 | extern Bool MAC_(process_common_cmd_line_option) ( Char* arg ); |
| 266 | extern void MAC_(print_common_usage) ( void ); |
| 267 | extern void MAC_(print_common_debug_usage) ( void ); |
| 268 | |
| 269 | |
| 270 | /*------------------------------------------------------------*/ |
| 271 | /*--- Variables ---*/ |
| 272 | /*------------------------------------------------------------*/ |
| 273 | |
| 274 | /* For tracking malloc'd blocks */ |
| 275 | extern VgHashTable MAC_(malloc_list); |
| 276 | |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 277 | /* For tracking memory pools. */ |
| 278 | extern VgHashTable MAC_(mempool_list); |
| 279 | |
nethercote | 7cc9c23 | 2004-01-21 15:08:04 +0000 | [diff] [blame] | 280 | /* Function pointers for the two tools to track interesting events. */ |
nethercote | 451eae9 | 2004-11-02 13:06:32 +0000 | [diff] [blame] | 281 | extern void (*MAC_(new_mem_heap)) ( Addr a, SizeT len, Bool is_inited ); |
| 282 | extern void (*MAC_(ban_mem_heap)) ( Addr a, SizeT len ); |
| 283 | extern void (*MAC_(die_mem_heap)) ( Addr a, SizeT len ); |
| 284 | extern void (*MAC_(copy_mem_heap))( Addr from, Addr to, SizeT len ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 285 | |
sewardj | ecf8e10 | 2003-07-12 12:11:39 +0000 | [diff] [blame] | 286 | /* Function pointers for internal sanity checking. */ |
nethercote | 451eae9 | 2004-11-02 13:06:32 +0000 | [diff] [blame] | 287 | extern Bool (*MAC_(check_noaccess))( Addr a, SizeT len, Addr* bad_addr ); |
sewardj | ecf8e10 | 2003-07-12 12:11:39 +0000 | [diff] [blame] | 288 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 289 | /* Used in describe_addr() */ |
| 290 | extern Bool (*MAC_(describe_addr_supp)) ( Addr a, AddrInfo* ai ); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 291 | |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 292 | /* For VALGRIND_COUNT_LEAKS client request */ |
njn | e8b5c05 | 2003-07-22 22:03:58 +0000 | [diff] [blame] | 293 | extern Int MAC_(bytes_leaked); |
| 294 | extern Int MAC_(bytes_dubious); |
| 295 | extern Int MAC_(bytes_reachable); |
| 296 | extern Int MAC_(bytes_suppressed); |
sewardj | 99aac97 | 2002-12-26 01:53:45 +0000 | [diff] [blame] | 297 | |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 298 | /*------------------------------------------------------------*/ |
| 299 | /*--- Functions ---*/ |
| 300 | /*------------------------------------------------------------*/ |
| 301 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 302 | extern void MAC_(pp_AddrInfo) ( Addr a, AddrInfo* ai ); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 303 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 304 | extern void MAC_(clear_MAC_Error) ( MAC_Error* err_extra ); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 305 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 306 | extern Bool MAC_(shared_recognised_suppression) ( Char* name, Supp* su ); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 307 | |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 308 | extern void* MAC_(new_block) ( Addr p, SizeT size, SizeT align, UInt rzB, |
nethercote | 57e36b3 | 2004-07-10 14:56:28 +0000 | [diff] [blame] | 309 | Bool is_zeroed, MAC_AllocKind kind, |
| 310 | VgHashTable table); |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 311 | extern void MAC_(handle_free) ( Addr p, UInt rzB, MAC_AllocKind kind ); |
njn | 1078545 | 2003-05-20 16:38:24 +0000 | [diff] [blame] | 312 | |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 313 | extern void MAC_(create_mempool)(Addr pool, UInt rzB, Bool is_zeroed); |
| 314 | extern void MAC_(destroy_mempool)(Addr pool); |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 315 | extern void MAC_(mempool_alloc)(Addr pool, Addr addr, SizeT size); |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 316 | extern void MAC_(mempool_free)(Addr pool, Addr addr); |
| 317 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 318 | extern void MAC_(record_address_error) ( ThreadId tid, Addr a, |
sewardj | af48a60 | 2003-07-06 00:54:47 +0000 | [diff] [blame] | 319 | Int size, Bool isWrite ); |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame^] | 320 | extern void MAC_(record_core_mem_error) ( ThreadId tid, Bool isUnaddr, |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 321 | Char* s ); |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame^] | 322 | extern void MAC_(record_param_error) ( ThreadId tid, Addr a, Bool isReg, |
| 323 | Bool isUnaddr, Char* msg ); |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 324 | extern void MAC_(record_jump_error) ( ThreadId tid, Addr a ); |
| 325 | extern void MAC_(record_free_error) ( ThreadId tid, Addr a ); |
| 326 | extern void MAC_(record_freemismatch_error)( ThreadId tid, Addr a ); |
njn | b6cae9f | 2003-09-04 20:50:47 +0000 | [diff] [blame] | 327 | extern void MAC_(record_overlap_error) ( Char* function, OverlapExtra* oe ); |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 328 | extern void MAC_(record_illegal_mempool_error) ( ThreadId tid, Addr pool ); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 329 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 330 | extern void MAC_(pp_shared_SkinError) ( Error* err); |
| 331 | |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 332 | extern MAC_Chunk* MAC_(first_matching_freed_MAC_Chunk)( Bool (*p)(MAC_Chunk*, void*), void* d ); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 333 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 334 | extern void MAC_(common_pre_clo_init) ( void ); |
| 335 | extern void MAC_(common_fini) ( void (*leak_check)(void) ); |
| 336 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 337 | extern Bool MAC_(handle_common_client_requests) ( ThreadId tid, |
nethercote | d1b64b2 | 2004-11-04 18:22:28 +0000 | [diff] [blame] | 338 | UWord* arg_block, UWord* ret ); |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 339 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 340 | extern void MAC_(print_malloc_stats) ( void ); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 341 | |
| 342 | /* For leak checking */ |
| 343 | extern void MAC_(pp_LeakError)(void* vl, UInt n_this_record, |
| 344 | UInt n_total_records); |
| 345 | |
| 346 | extern void MAC_(do_detect_memory_leaks) ( |
| 347 | Bool is_valid_64k_chunk ( UInt ), |
| 348 | Bool is_valid_address ( Addr ) |
| 349 | ); |
| 350 | |
nethercote | eec4630 | 2004-08-23 15:06:23 +0000 | [diff] [blame] | 351 | extern REGPARM(1) void MAC_(new_mem_stack_4) ( Addr old_ESP ); |
| 352 | extern REGPARM(1) void MAC_(die_mem_stack_4) ( Addr old_ESP ); |
| 353 | extern REGPARM(1) void MAC_(new_mem_stack_8) ( Addr old_ESP ); |
| 354 | extern REGPARM(1) void MAC_(die_mem_stack_8) ( Addr old_ESP ); |
| 355 | extern REGPARM(1) void MAC_(new_mem_stack_12) ( Addr old_ESP ); |
| 356 | extern REGPARM(1) void MAC_(die_mem_stack_12) ( Addr old_ESP ); |
| 357 | extern REGPARM(1) void MAC_(new_mem_stack_16) ( Addr old_ESP ); |
| 358 | extern REGPARM(1) void MAC_(die_mem_stack_16) ( Addr old_ESP ); |
| 359 | extern REGPARM(1) void MAC_(new_mem_stack_32) ( Addr old_ESP ); |
| 360 | extern REGPARM(1) void MAC_(die_mem_stack_32) ( Addr old_ESP ); |
nethercote | 451eae9 | 2004-11-02 13:06:32 +0000 | [diff] [blame] | 361 | extern void MAC_(die_mem_stack) ( Addr a, SizeT len); |
| 362 | extern void MAC_(new_mem_stack) ( Addr a, SizeT len); |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 363 | |
| 364 | |
| 365 | /*------------------------------------------------------------*/ |
| 366 | /*--- Stack pointer adjustment ---*/ |
| 367 | /*------------------------------------------------------------*/ |
| 368 | |
| 369 | /* Some noble preprocessor abuse, to enable Memcheck and Addrcheck to |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 370 | share this code, but call different functions. |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 371 | |
| 372 | Note that this code is executed very frequently and must be highly |
| 373 | optimised, which is why I resort to the preprocessor to achieve the |
| 374 | factoring, rather than eg. using function pointers. |
| 375 | */ |
| 376 | |
| 377 | #define ESP_UPDATE_HANDLERS(ALIGNED4_NEW, ALIGNED4_DIE, \ |
| 378 | ALIGNED8_NEW, ALIGNED8_DIE, \ |
| 379 | UNALIGNED_NEW, UNALIGNED_DIE) \ |
| 380 | \ |
nethercote | eec4630 | 2004-08-23 15:06:23 +0000 | [diff] [blame] | 381 | void REGPARM(1) MAC_(new_mem_stack_4)(Addr new_ESP) \ |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 382 | { \ |
| 383 | PROF_EVENT(110); \ |
| 384 | if (IS_ALIGNED4_ADDR(new_ESP)) { \ |
| 385 | ALIGNED4_NEW ( new_ESP ); \ |
| 386 | } else { \ |
| 387 | UNALIGNED_NEW ( new_ESP, 4 ); \ |
| 388 | } \ |
| 389 | } \ |
| 390 | \ |
nethercote | eec4630 | 2004-08-23 15:06:23 +0000 | [diff] [blame] | 391 | void REGPARM(1) MAC_(die_mem_stack_4)(Addr new_ESP) \ |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 392 | { \ |
| 393 | PROF_EVENT(120); \ |
| 394 | if (IS_ALIGNED4_ADDR(new_ESP)) { \ |
| 395 | ALIGNED4_DIE ( new_ESP-4 ); \ |
| 396 | } else { \ |
| 397 | UNALIGNED_DIE ( new_ESP-4, 4 ); \ |
| 398 | } \ |
| 399 | } \ |
| 400 | \ |
nethercote | eec4630 | 2004-08-23 15:06:23 +0000 | [diff] [blame] | 401 | void REGPARM(1) MAC_(new_mem_stack_8)(Addr new_ESP) \ |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 402 | { \ |
| 403 | PROF_EVENT(111); \ |
| 404 | if (IS_ALIGNED8_ADDR(new_ESP)) { \ |
| 405 | ALIGNED8_NEW ( new_ESP ); \ |
| 406 | } else if (IS_ALIGNED4_ADDR(new_ESP)) { \ |
| 407 | ALIGNED4_NEW ( new_ESP ); \ |
| 408 | ALIGNED4_NEW ( new_ESP+4 ); \ |
| 409 | } else { \ |
| 410 | UNALIGNED_NEW ( new_ESP, 8 ); \ |
| 411 | } \ |
| 412 | } \ |
| 413 | \ |
nethercote | eec4630 | 2004-08-23 15:06:23 +0000 | [diff] [blame] | 414 | void REGPARM(1) MAC_(die_mem_stack_8)(Addr new_ESP) \ |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 415 | { \ |
| 416 | PROF_EVENT(121); \ |
| 417 | if (IS_ALIGNED8_ADDR(new_ESP)) { \ |
| 418 | ALIGNED8_DIE ( new_ESP-8 ); \ |
| 419 | } else if (IS_ALIGNED4_ADDR(new_ESP)) { \ |
| 420 | ALIGNED4_DIE ( new_ESP-8 ); \ |
| 421 | ALIGNED4_DIE ( new_ESP-4 ); \ |
| 422 | } else { \ |
| 423 | UNALIGNED_DIE ( new_ESP-8, 8 ); \ |
| 424 | } \ |
| 425 | } \ |
| 426 | \ |
nethercote | eec4630 | 2004-08-23 15:06:23 +0000 | [diff] [blame] | 427 | void REGPARM(1) MAC_(new_mem_stack_12)(Addr new_ESP) \ |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 428 | { \ |
| 429 | PROF_EVENT(112); \ |
| 430 | if (IS_ALIGNED8_ADDR(new_ESP)) { \ |
| 431 | ALIGNED8_NEW ( new_ESP ); \ |
| 432 | ALIGNED4_NEW ( new_ESP+8 ); \ |
| 433 | } else if (IS_ALIGNED4_ADDR(new_ESP)) { \ |
| 434 | ALIGNED4_NEW ( new_ESP ); \ |
| 435 | ALIGNED8_NEW ( new_ESP+4 ); \ |
| 436 | } else { \ |
| 437 | UNALIGNED_NEW ( new_ESP, 12 ); \ |
| 438 | } \ |
| 439 | } \ |
| 440 | \ |
nethercote | eec4630 | 2004-08-23 15:06:23 +0000 | [diff] [blame] | 441 | void REGPARM(1) MAC_(die_mem_stack_12)(Addr new_ESP) \ |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 442 | { \ |
| 443 | PROF_EVENT(122); \ |
| 444 | /* Note the -12 in the test */ \ |
| 445 | if (IS_ALIGNED8_ADDR(new_ESP-12)) { \ |
| 446 | ALIGNED8_DIE ( new_ESP-12 ); \ |
| 447 | ALIGNED4_DIE ( new_ESP-4 ); \ |
| 448 | } else if (IS_ALIGNED4_ADDR(new_ESP)) { \ |
| 449 | ALIGNED4_DIE ( new_ESP-12 ); \ |
| 450 | ALIGNED8_DIE ( new_ESP-8 ); \ |
| 451 | } else { \ |
| 452 | UNALIGNED_DIE ( new_ESP-12, 12 ); \ |
| 453 | } \ |
| 454 | } \ |
| 455 | \ |
nethercote | eec4630 | 2004-08-23 15:06:23 +0000 | [diff] [blame] | 456 | void REGPARM(1) MAC_(new_mem_stack_16)(Addr new_ESP) \ |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 457 | { \ |
| 458 | PROF_EVENT(113); \ |
| 459 | if (IS_ALIGNED8_ADDR(new_ESP)) { \ |
| 460 | ALIGNED8_NEW ( new_ESP ); \ |
| 461 | ALIGNED8_NEW ( new_ESP+8 ); \ |
| 462 | } else if (IS_ALIGNED4_ADDR(new_ESP)) { \ |
| 463 | ALIGNED4_NEW ( new_ESP ); \ |
| 464 | ALIGNED8_NEW ( new_ESP+4 ); \ |
| 465 | ALIGNED4_NEW ( new_ESP+12 ); \ |
| 466 | } else { \ |
| 467 | UNALIGNED_NEW ( new_ESP, 16 ); \ |
| 468 | } \ |
| 469 | } \ |
| 470 | \ |
nethercote | eec4630 | 2004-08-23 15:06:23 +0000 | [diff] [blame] | 471 | void REGPARM(1) MAC_(die_mem_stack_16)(Addr new_ESP) \ |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 472 | { \ |
| 473 | PROF_EVENT(123); \ |
| 474 | if (IS_ALIGNED8_ADDR(new_ESP)) { \ |
| 475 | ALIGNED8_DIE ( new_ESP-16 ); \ |
| 476 | ALIGNED8_DIE ( new_ESP-8 ); \ |
| 477 | } else if (IS_ALIGNED4_ADDR(new_ESP)) { \ |
| 478 | ALIGNED4_DIE ( new_ESP-16 ); \ |
| 479 | ALIGNED8_DIE ( new_ESP-12 ); \ |
| 480 | ALIGNED4_DIE ( new_ESP-4 ); \ |
| 481 | } else { \ |
| 482 | UNALIGNED_DIE ( new_ESP-16, 16 ); \ |
| 483 | } \ |
| 484 | } \ |
| 485 | \ |
nethercote | eec4630 | 2004-08-23 15:06:23 +0000 | [diff] [blame] | 486 | void REGPARM(1) MAC_(new_mem_stack_32)(Addr new_ESP) \ |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 487 | { \ |
| 488 | PROF_EVENT(114); \ |
| 489 | if (IS_ALIGNED8_ADDR(new_ESP)) { \ |
| 490 | ALIGNED8_NEW ( new_ESP ); \ |
| 491 | ALIGNED8_NEW ( new_ESP+8 ); \ |
| 492 | ALIGNED8_NEW ( new_ESP+16 ); \ |
| 493 | ALIGNED8_NEW ( new_ESP+24 ); \ |
| 494 | } else if (IS_ALIGNED4_ADDR(new_ESP)) { \ |
| 495 | ALIGNED4_NEW ( new_ESP ); \ |
| 496 | ALIGNED8_NEW ( new_ESP+4 ); \ |
| 497 | ALIGNED8_NEW ( new_ESP+12 ); \ |
| 498 | ALIGNED8_NEW ( new_ESP+20 ); \ |
| 499 | ALIGNED4_NEW ( new_ESP+28 ); \ |
| 500 | } else { \ |
| 501 | UNALIGNED_NEW ( new_ESP, 32 ); \ |
| 502 | } \ |
| 503 | } \ |
| 504 | \ |
nethercote | eec4630 | 2004-08-23 15:06:23 +0000 | [diff] [blame] | 505 | void REGPARM(1) MAC_(die_mem_stack_32)(Addr new_ESP) \ |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 506 | { \ |
| 507 | PROF_EVENT(124); \ |
| 508 | if (IS_ALIGNED8_ADDR(new_ESP)) { \ |
| 509 | ALIGNED8_DIE ( new_ESP-32 ); \ |
| 510 | ALIGNED8_DIE ( new_ESP-24 ); \ |
| 511 | ALIGNED8_DIE ( new_ESP-16 ); \ |
| 512 | ALIGNED8_DIE ( new_ESP- 8 ); \ |
| 513 | } else if (IS_ALIGNED4_ADDR(new_ESP)) { \ |
| 514 | ALIGNED4_DIE ( new_ESP-32 ); \ |
| 515 | ALIGNED8_DIE ( new_ESP-28 ); \ |
| 516 | ALIGNED8_DIE ( new_ESP-20 ); \ |
| 517 | ALIGNED8_DIE ( new_ESP-12 ); \ |
| 518 | ALIGNED4_DIE ( new_ESP-4 ); \ |
| 519 | } else { \ |
| 520 | UNALIGNED_DIE ( new_ESP-32, 32 ); \ |
| 521 | } \ |
| 522 | } \ |
| 523 | \ |
nethercote | 451eae9 | 2004-11-02 13:06:32 +0000 | [diff] [blame] | 524 | void MAC_(new_mem_stack) ( Addr a, SizeT len ) \ |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 525 | { \ |
| 526 | PROF_EVENT(115); \ |
| 527 | UNALIGNED_NEW ( a, len ); \ |
| 528 | } \ |
| 529 | \ |
nethercote | 451eae9 | 2004-11-02 13:06:32 +0000 | [diff] [blame] | 530 | void MAC_(die_mem_stack) ( Addr a, SizeT len ) \ |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 531 | { \ |
| 532 | PROF_EVENT(125); \ |
| 533 | UNALIGNED_DIE ( a, len ); \ |
| 534 | } |
| 535 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 536 | #endif /* __MAC_SHARED_H */ |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 537 | |
| 538 | /*--------------------------------------------------------------------*/ |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 539 | /*--- end mac_shared.h ---*/ |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 540 | /*--------------------------------------------------------------------*/ |