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