njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame^] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
| 3 | /*--- Code that is shared between MemCheck and AddrCheck. ---*/ |
| 4 | /*--- mc_common.h ---*/ |
| 5 | /*--------------------------------------------------------------------*/ |
| 6 | |
| 7 | /* |
| 8 | This file is part of MemCheck, a heavyweight Valgrind skin for |
| 9 | detecting memory errors, and AddrCheck, a lightweight Valgrind skin |
| 10 | for detecting memory errors. |
| 11 | |
| 12 | Copyright (C) 2000-2002 Julian Seward |
| 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 | |
| 33 | #ifndef __MC_COMMON_H |
| 34 | #define __MC_COMMON_H |
| 35 | |
| 36 | #include "vg_skin.h" |
| 37 | #include "mc_constants.h" |
| 38 | |
| 39 | /* The classification of a faulting address. */ |
| 40 | typedef |
| 41 | enum { Undescribed, /* as-yet unclassified */ |
| 42 | Stack, |
| 43 | Unknown, /* classification yielded nothing useful */ |
| 44 | Freed, Mallocd, |
| 45 | UserG, UserS /* MemCheck only */ |
| 46 | } |
| 47 | AddrKind; |
| 48 | |
| 49 | /* Records info about a faulting address. */ |
| 50 | typedef |
| 51 | struct { |
| 52 | /* ALL */ |
| 53 | AddrKind akind; |
| 54 | /* Freed, Mallocd */ |
| 55 | Int blksize; |
| 56 | /* Freed, Mallocd */ |
| 57 | Int rwoffset; |
| 58 | /* Freed, Mallocd */ |
| 59 | ExeContext* lastchange; |
| 60 | /* Stack */ |
| 61 | ThreadId stack_tid; |
| 62 | /* True if is just-below %esp -- could be a gcc bug. */ |
| 63 | Bool maybe_gcc; |
| 64 | } |
| 65 | AddrInfo; |
| 66 | |
| 67 | typedef |
| 68 | enum { |
| 69 | /* Bad syscall params */ |
| 70 | ParamSupp, |
| 71 | /* Memory errors in core (pthread ops, signal handling) */ |
| 72 | CoreMemSupp, |
| 73 | /* Use of invalid values of given size (MemCheck only) */ |
| 74 | Value0Supp, Value1Supp, Value2Supp, Value4Supp, Value8Supp, |
| 75 | /* Invalid read/write attempt at given size */ |
| 76 | Addr1Supp, Addr2Supp, Addr4Supp, Addr8Supp, |
| 77 | /* Invalid or mismatching free */ |
| 78 | FreeSupp |
| 79 | } |
| 80 | MemCheckSuppKind; |
| 81 | |
| 82 | /* What kind of error it is. */ |
| 83 | typedef |
| 84 | enum { ValueErr, /* Memcheck only */ |
| 85 | CoreMemErr, |
| 86 | AddrErr, |
| 87 | ParamErr, UserErr, /* behaves like an anonymous ParamErr */ |
| 88 | FreeErr, FreeMismatchErr |
| 89 | } |
| 90 | MemCheckErrorKind; |
| 91 | |
| 92 | /* What kind of memory access is involved in the error? */ |
| 93 | typedef |
| 94 | enum { ReadAxs, WriteAxs, ExecAxs } |
| 95 | AxsKind; |
| 96 | |
| 97 | /* Extra context for memory errors */ |
| 98 | typedef |
| 99 | struct { |
| 100 | /* AddrErr */ |
| 101 | AxsKind axskind; |
| 102 | /* AddrErr, ValueErr */ |
| 103 | Int size; |
| 104 | /* AddrErr, FreeErr, FreeMismatchErr, ParamErr, UserErr */ |
| 105 | AddrInfo addrinfo; |
| 106 | /* ParamErr, UserErr, CoreMemErr */ |
| 107 | Bool isWrite; |
| 108 | } |
| 109 | MemCheckError; |
| 110 | |
| 111 | |
| 112 | #ifdef VG_PROFILE_MEMORY |
| 113 | |
| 114 | #define PROF_EVENT(ev) \ |
| 115 | do { sk_assert((ev) >= 0 && (ev) < N_PROF_EVENTS); \ |
| 116 | MC_(event_ctr)[ev]++; \ |
| 117 | } while (False); |
| 118 | |
| 119 | #else |
| 120 | |
| 121 | #define PROF_EVENT(ev) /* */ |
| 122 | |
| 123 | #endif /* VG_PROFILE_MEMORY */ |
| 124 | |
| 125 | #define IS_DISTINGUISHED_SM(smap) \ |
| 126 | ((smap) == &distinguished_secondary_map) |
| 127 | |
| 128 | #define ENSURE_MAPPABLE(addr,caller) \ |
| 129 | do { \ |
| 130 | if (IS_DISTINGUISHED_SM(primary_map[(addr) >> 16])) { \ |
| 131 | primary_map[(addr) >> 16] = alloc_secondary_map(caller); \ |
| 132 | /* VG_(printf)("new 2map because of %p\n", addr); */ \ |
| 133 | } \ |
| 134 | } while(0) |
| 135 | |
| 136 | #define BITARR_SET(aaa_p,iii_p) \ |
| 137 | do { \ |
| 138 | UInt iii = (UInt)iii_p; \ |
| 139 | UChar* aaa = (UChar*)aaa_p; \ |
| 140 | aaa[iii >> 3] |= (1 << (iii & 7)); \ |
| 141 | } while (0) |
| 142 | |
| 143 | #define BITARR_CLEAR(aaa_p,iii_p) \ |
| 144 | do { \ |
| 145 | UInt iii = (UInt)iii_p; \ |
| 146 | UChar* aaa = (UChar*)aaa_p; \ |
| 147 | aaa[iii >> 3] &= ~(1 << (iii & 7)); \ |
| 148 | } while (0) |
| 149 | |
| 150 | #define BITARR_TEST(aaa_p,iii_p) \ |
| 151 | (0 != (((UChar*)aaa_p)[ ((UInt)iii_p) >> 3 ] \ |
| 152 | & (1 << (((UInt)iii_p) & 7)))) \ |
| 153 | |
| 154 | |
| 155 | #define VGM_BIT_VALID 0 |
| 156 | #define VGM_BIT_INVALID 1 |
| 157 | |
| 158 | #define VGM_NIBBLE_VALID 0 |
| 159 | #define VGM_NIBBLE_INVALID 0xF |
| 160 | |
| 161 | #define VGM_BYTE_VALID 0 |
| 162 | #define VGM_BYTE_INVALID 0xFF |
| 163 | |
| 164 | #define VGM_WORD_VALID 0 |
| 165 | #define VGM_WORD_INVALID 0xFFFFFFFF |
| 166 | |
| 167 | #define VGM_EFLAGS_VALID 0xFFFFFFFE |
| 168 | #define VGM_EFLAGS_INVALID 0xFFFFFFFF /* not used */ |
| 169 | |
| 170 | /*------------------------------------------------------------*/ |
| 171 | /*--- Command line options + defaults ---*/ |
| 172 | /*------------------------------------------------------------*/ |
| 173 | |
| 174 | /* Most are shared between MemCheck and AddrCheck, the last two are |
| 175 | MemCheck only (but here anyway for simplicity) */ |
| 176 | |
| 177 | /* Allow loads from partially-valid addresses? default: YES */ |
| 178 | extern Bool MC_(clo_partial_loads_ok); |
| 179 | |
| 180 | /* Max volume of the freed blocks queue. */ |
| 181 | extern Int MC_(clo_freelist_vol); |
| 182 | |
| 183 | /* Do leak check at exit? default: NO */ |
| 184 | extern Bool MC_(clo_leak_check); |
| 185 | |
| 186 | /* How closely should we compare ExeContexts in leak records? default: 2 */ |
| 187 | extern VgRes MC_(clo_leak_resolution); |
| 188 | |
| 189 | /* In leak check, show reachable-but-not-freed blocks? default: NO */ |
| 190 | extern Bool MC_(clo_show_reachable); |
| 191 | |
| 192 | /* Assume accesses immediately below %esp are due to gcc-2.96 bugs. |
| 193 | * default: NO*/ |
| 194 | extern Bool MC_(clo_workaround_gcc296_bugs); |
| 195 | |
| 196 | /* DEBUG: clean up instrumented code? default: YES */ |
| 197 | extern Bool MC_(clo_cleanup); |
| 198 | |
| 199 | /* Shall we V-check addrs? (they are always A checked too) default: YES */ |
| 200 | extern Bool MC_(clo_check_addrVs); |
| 201 | |
| 202 | /* When instrumenting, omit some checks if tell-tale literals for |
| 203 | inlined strlen() are visible in the basic block. default: YES */ |
| 204 | extern Bool MC_(clo_avoid_strlen_errors); |
| 205 | |
| 206 | |
| 207 | extern Bool MC_(process_common_cmd_line_option)(Char* arg); |
| 208 | |
| 209 | /*------------------------------------------------------------*/ |
| 210 | /*--- Functions ---*/ |
| 211 | /*------------------------------------------------------------*/ |
| 212 | |
| 213 | extern void MC_(set_where) ( ShadowChunk* sc, ExeContext* ec ); |
| 214 | extern ExeContext *MC_(get_where) ( ShadowChunk* sc ); |
| 215 | |
| 216 | extern void MC_(pp_AddrInfo) ( Addr a, AddrInfo* ai ); |
| 217 | |
| 218 | extern void MC_(clear_MemCheckError) ( MemCheckError* err_extra ); |
| 219 | |
| 220 | extern void MC_(record_address_error) ( Addr a, Int size, Bool isWrite ); |
| 221 | extern void MC_(record_core_mem_error) ( ThreadState* tst, Bool isWrite, |
| 222 | Char* s ); |
| 223 | extern void MC_(record_param_error) ( ThreadState* tst, Addr a, |
| 224 | Bool isWriteLack, Char* msg ); |
| 225 | extern void MC_(record_jump_error) ( ThreadState* tst, Addr a ); |
| 226 | extern void MC_(record_free_error) ( ThreadState* tst, Addr a ); |
| 227 | extern void MC_(record_freemismatch_error)( ThreadState* tst, Addr a ); |
| 228 | |
| 229 | extern void MC_(init_prof_mem) ( void ); |
| 230 | extern void MC_(done_prof_mem) ( void ); |
| 231 | |
| 232 | extern Int MC_(count_freelist) ( void ) __attribute__ ((unused)); |
| 233 | extern void MC_(freelist_sanity) ( void ) __attribute__ ((unused)); |
| 234 | extern ShadowChunk* MC_(any_matching_freed_ShadowChunks) |
| 235 | ( Bool (*p)(ShadowChunk*) ); |
| 236 | |
| 237 | #endif /* __MC_COMMON_H */ |
| 238 | |
| 239 | /*--------------------------------------------------------------------*/ |
| 240 | /*--- end mc_common.h ---*/ |
| 241 | /*--------------------------------------------------------------------*/ |