njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 1 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2 | /*--------------------------------------------------------------------*/ |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 3 | /*--- MemCheck: Maintain bitmaps of memory, tracking the ---*/ |
| 4 | /*--- accessibility (A) and validity (V) status of each byte. ---*/ |
njn25 | cac76cb | 2002-09-23 11:21:57 +0000 | [diff] [blame] | 5 | /*--- mc_main.c ---*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 6 | /*--------------------------------------------------------------------*/ |
| 7 | |
| 8 | /* |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 9 | This file is part of MemCheck, a heavyweight Valgrind tool for |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 10 | detecting memory errors. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 11 | |
sewardj | e4b0bf0 | 2006-06-05 23:21:15 +0000 | [diff] [blame^] | 12 | Copyright (C) 2000-2006 Julian Seward |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +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 | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 33 | #include "pub_tool_basics.h" |
njn | 4802b38 | 2005-06-11 04:58:29 +0000 | [diff] [blame] | 34 | #include "pub_tool_aspacemgr.h" |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 35 | #include "pub_tool_hashtable.h" // For mc_include.h |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 36 | #include "pub_tool_libcbase.h" |
njn | 132bfcc | 2005-06-04 19:16:06 +0000 | [diff] [blame] | 37 | #include "pub_tool_libcassert.h" |
njn | 36a20fa | 2005-06-03 03:08:39 +0000 | [diff] [blame] | 38 | #include "pub_tool_libcprint.h" |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 39 | #include "pub_tool_machine.h" |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 40 | #include "pub_tool_mallocfree.h" |
| 41 | #include "pub_tool_options.h" |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 42 | #include "pub_tool_oset.h" |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 43 | #include "pub_tool_replacemalloc.h" |
| 44 | #include "pub_tool_tooliface.h" |
| 45 | #include "pub_tool_threadstate.h" |
| 46 | |
| 47 | #include "mc_include.h" |
| 48 | #include "memcheck.h" /* for client requests */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 49 | |
tom | d55121e | 2005-12-19 12:40:13 +0000 | [diff] [blame] | 50 | #ifdef HAVE_BUILTIN_EXPECT |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 51 | #define EXPECTED_TAKEN(cond) __builtin_expect((cond),1) |
| 52 | #define EXPECTED_NOT_TAKEN(cond) __builtin_expect((cond),0) |
tom | d55121e | 2005-12-19 12:40:13 +0000 | [diff] [blame] | 53 | #else |
| 54 | #define EXPECTED_TAKEN(cond) (cond) |
| 55 | #define EXPECTED_NOT_TAKEN(cond) (cond) |
| 56 | #endif |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 57 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 58 | /* Set to 1 to do a little more sanity checking */ |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 59 | #define VG_DEBUG_MEMORY 0 |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 60 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 61 | #define DEBUG(fmt, args...) //VG_(printf)(fmt, ## args) |
| 62 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 63 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 64 | /*------------------------------------------------------------*/ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 65 | /*--- Fast-case knobs ---*/ |
| 66 | /*------------------------------------------------------------*/ |
| 67 | |
| 68 | // Comment these out to disable the fast cases (don't just set them to zero). |
| 69 | |
| 70 | #define PERF_FAST_LOADV 1 |
| 71 | #define PERF_FAST_STOREV 1 |
| 72 | |
| 73 | #define PERF_FAST_SARP 1 |
| 74 | |
| 75 | #define PERF_FAST_STACK 1 |
| 76 | #define PERF_FAST_STACK2 1 |
| 77 | |
| 78 | /*------------------------------------------------------------*/ |
| 79 | /*--- V bits and A bits ---*/ |
| 80 | /*------------------------------------------------------------*/ |
| 81 | |
| 82 | /* Conceptually, every byte value has 8 V bits, which track whether Memcheck |
| 83 | thinks the corresponding value bit is defined. And every memory byte |
| 84 | has an A bit, which tracks whether Memcheck thinks the program can access |
| 85 | it safely. So every N-bit register is shadowed with N V bits, and every |
| 86 | memory byte is shadowed with 8 V bits and one A bit. |
| 87 | |
| 88 | In the implementation, we use two forms of compression (compressed V bits |
| 89 | and distinguished secondary maps) to avoid the 9-bit-per-byte overhead |
| 90 | for memory. |
| 91 | |
| 92 | Memcheck also tracks extra information about each heap block that is |
| 93 | allocated, for detecting memory leaks and other purposes. |
| 94 | */ |
| 95 | |
| 96 | /*------------------------------------------------------------*/ |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 97 | /*--- Basic A/V bitmap representation. ---*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 98 | /*------------------------------------------------------------*/ |
| 99 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 100 | /* All reads and writes are checked against a memory map (a.k.a. shadow |
| 101 | memory), which records the state of all memory in the process. |
| 102 | |
| 103 | On 32-bit machines the memory map is organised as follows. |
| 104 | The top 16 bits of an address are used to index into a top-level |
| 105 | map table, containing 65536 entries. Each entry is a pointer to a |
| 106 | second-level map, which records the accesibililty and validity |
| 107 | permissions for the 65536 bytes indexed by the lower 16 bits of the |
| 108 | address. Each byte is represented by two bits (details are below). So |
| 109 | each second-level map contains 16384 bytes. This two-level arrangement |
| 110 | conveniently divides the 4G address space into 64k lumps, each size 64k |
| 111 | bytes. |
| 112 | |
| 113 | All entries in the primary (top-level) map must point to a valid |
| 114 | secondary (second-level) map. Since many of the 64kB chunks will |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 115 | have the same status for every bit -- ie. noaccess (for unused |
| 116 | address space) or entirely addressable and defined (for code segments) -- |
| 117 | there are three distinguished secondary maps, which indicate 'noaccess', |
| 118 | 'undefined' and 'defined'. For these uniform 64kB chunks, the primary |
| 119 | map entry points to the relevant distinguished map. In practice, |
| 120 | typically more than half of the addressable memory is represented with |
| 121 | the 'undefined' or 'defined' distinguished secondary map, so it gives a |
| 122 | good saving. It also lets us set the V+A bits of large address regions |
| 123 | quickly in set_address_range_perms(). |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 124 | |
| 125 | On 64-bit machines it's more complicated. If we followed the same basic |
| 126 | scheme we'd have a four-level table which would require too many memory |
| 127 | accesses. So instead the top-level map table has 2^19 entries (indexed |
| 128 | using bits 16..34 of the address); this covers the bottom 32GB. Any |
| 129 | accesses above 32GB are handled with a slow, sparse auxiliary table. |
| 130 | Valgrind's address space manager tries very hard to keep things below |
| 131 | this 32GB barrier so that performance doesn't suffer too much. |
| 132 | |
| 133 | Note that this file has a lot of different functions for reading and |
| 134 | writing shadow memory. Only a couple are strictly necessary (eg. |
| 135 | get_vabits2 and set_vabits2), most are just specialised for specific |
| 136 | common cases to improve performance. |
| 137 | |
| 138 | Aside: the V+A bits are less precise than they could be -- we have no way |
| 139 | of marking memory as read-only. It would be great if we could add an |
| 140 | extra state VA_BITSn_READONLY. But then we'd have 5 different states, |
| 141 | which requires 2.3 bits to hold, and there's no way to do that elegantly |
| 142 | -- we'd have to double up to 4 bits of metadata per byte, which doesn't |
| 143 | seem worth it. |
| 144 | */ |
sewardj | c859fbf | 2005-04-22 21:10:28 +0000 | [diff] [blame] | 145 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 146 | /* --------------- Basic configuration --------------- */ |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 147 | |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 148 | /* Only change this. N_PRIMARY_MAP *must* be a power of 2. */ |
sewardj | 21f7ff4 | 2005-04-28 10:32:02 +0000 | [diff] [blame] | 149 | |
sewardj | e4ccc01 | 2005-05-02 12:53:38 +0000 | [diff] [blame] | 150 | #if VG_WORDSIZE == 4 |
sewardj | 21f7ff4 | 2005-04-28 10:32:02 +0000 | [diff] [blame] | 151 | |
| 152 | /* cover the entire address space */ |
| 153 | # define N_PRIMARY_BITS 16 |
| 154 | |
| 155 | #else |
| 156 | |
sewardj | 34483bc | 2005-09-28 11:50:20 +0000 | [diff] [blame] | 157 | /* Just handle the first 32G fast and the rest via auxiliary |
sewardj | 21f7ff4 | 2005-04-28 10:32:02 +0000 | [diff] [blame] | 158 | primaries. */ |
sewardj | 34483bc | 2005-09-28 11:50:20 +0000 | [diff] [blame] | 159 | # define N_PRIMARY_BITS 19 |
sewardj | 21f7ff4 | 2005-04-28 10:32:02 +0000 | [diff] [blame] | 160 | |
| 161 | #endif |
| 162 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 163 | |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 164 | /* Do not change this. */ |
sewardj | e4ccc01 | 2005-05-02 12:53:38 +0000 | [diff] [blame] | 165 | #define N_PRIMARY_MAP ( ((UWord)1) << N_PRIMARY_BITS) |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 166 | |
| 167 | /* Do not change this. */ |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 168 | #define MAX_PRIMARY_ADDRESS (Addr)((((Addr)65536) * N_PRIMARY_MAP)-1) |
| 169 | |
| 170 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 171 | /* --------------- Secondary maps --------------- */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 172 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 173 | // Each byte of memory conceptually has an A bit, which indicates its |
| 174 | // addressability, and 8 V bits, which indicates its definedness. |
| 175 | // |
| 176 | // But because very few bytes are partially defined, we can use a nice |
| 177 | // compression scheme to reduce the size of shadow memory. Each byte of |
| 178 | // memory has 2 bits which indicates its state (ie. V+A bits): |
| 179 | // |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 180 | // 00: noaccess (unaddressable but treated as fully defined) |
| 181 | // 01: undefined (addressable and fully undefined) |
| 182 | // 10: defined (addressable and fully defined) |
| 183 | // 11: partdefined (addressable and partially defined) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 184 | // |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 185 | // In the "partdefined" case, we use a secondary table to store the V bits. |
| 186 | // Each entry in the secondary-V-bits table maps a byte address to its 8 V |
| 187 | // bits. |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 188 | // |
| 189 | // We store the compressed V+A bits in 8-bit chunks, ie. the V+A bits for |
| 190 | // four bytes (32 bits) of memory are in each chunk. Hence the name |
| 191 | // "vabits8". This lets us get the V+A bits for four bytes at a time |
| 192 | // easily (without having to do any shifting and/or masking), and that is a |
| 193 | // very common operation. (Note that although each vabits8 chunk |
| 194 | // is 8 bits in size, it represents 32 bits of memory.) |
| 195 | // |
| 196 | // The representation is "inverse" little-endian... each 4 bytes of |
| 197 | // memory is represented by a 1 byte value, where: |
| 198 | // |
| 199 | // - the status of byte (a+0) is held in bits [1..0] |
| 200 | // - the status of byte (a+1) is held in bits [3..2] |
| 201 | // - the status of byte (a+2) is held in bits [5..4] |
| 202 | // - the status of byte (a+3) is held in bits [7..6] |
| 203 | // |
| 204 | // It's "inverse" because endianness normally describes a mapping from |
| 205 | // value bits to memory addresses; in this case the mapping is inverted. |
| 206 | // Ie. instead of particular value bits being held in certain addresses, in |
| 207 | // this case certain addresses are represented by particular value bits. |
| 208 | // See insert_vabits2_into_vabits8() for an example. |
| 209 | // |
| 210 | // But note that we don't compress the V bits stored in registers; they |
| 211 | // need to be explicit to made the shadow operations possible. Therefore |
| 212 | // when moving values between registers and memory we need to convert |
| 213 | // between the expanded in-register format and the compressed in-memory |
| 214 | // format. This isn't so difficult, it just requires careful attention in a |
| 215 | // few places. |
| 216 | |
| 217 | // These represent eight bits of memory. |
| 218 | #define VA_BITS2_NOACCESS 0x0 // 00b |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 219 | #define VA_BITS2_UNDEFINED 0x1 // 01b |
| 220 | #define VA_BITS2_DEFINED 0x2 // 10b |
| 221 | #define VA_BITS2_PARTDEFINED 0x3 // 11b |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 222 | |
| 223 | // These represent 16 bits of memory. |
| 224 | #define VA_BITS4_NOACCESS 0x0 // 00_00b |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 225 | #define VA_BITS4_UNDEFINED 0x5 // 01_01b |
| 226 | #define VA_BITS4_DEFINED 0xa // 10_10b |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 227 | |
| 228 | // These represent 32 bits of memory. |
| 229 | #define VA_BITS8_NOACCESS 0x00 // 00_00_00_00b |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 230 | #define VA_BITS8_UNDEFINED 0x55 // 01_01_01_01b |
| 231 | #define VA_BITS8_DEFINED 0xaa // 10_10_10_10b |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 232 | |
| 233 | // These represent 64 bits of memory. |
| 234 | #define VA_BITS16_NOACCESS 0x0000 // 00_00_00_00b x 2 |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 235 | #define VA_BITS16_UNDEFINED 0x5555 // 01_01_01_01b x 2 |
| 236 | #define VA_BITS16_DEFINED 0xaaaa // 10_10_10_10b x 2 |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 237 | |
| 238 | |
| 239 | #define SM_CHUNKS 16384 |
| 240 | #define SM_OFF(aaa) (((aaa) & 0xffff) >> 2) |
| 241 | #define SM_OFF_16(aaa) (((aaa) & 0xffff) >> 3) |
| 242 | |
| 243 | // Paranoia: it's critical for performance that the requested inlining |
| 244 | // occurs. So try extra hard. |
| 245 | #define INLINE inline __attribute__((always_inline)) |
| 246 | |
| 247 | static INLINE Addr start_of_this_sm ( Addr a ) { |
| 248 | return (a & (~SM_MASK)); |
| 249 | } |
| 250 | static INLINE Bool is_start_of_sm ( Addr a ) { |
| 251 | return (start_of_this_sm(a) == a); |
| 252 | } |
| 253 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 254 | typedef |
| 255 | struct { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 256 | UChar vabits8[SM_CHUNKS]; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 257 | } |
| 258 | SecMap; |
| 259 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 260 | // 3 distinguished secondary maps, one for no-access, one for |
| 261 | // accessible but undefined, and one for accessible and defined. |
| 262 | // Distinguished secondaries may never be modified. |
| 263 | #define SM_DIST_NOACCESS 0 |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 264 | #define SM_DIST_UNDEFINED 1 |
| 265 | #define SM_DIST_DEFINED 2 |
njn | b8dca86 | 2005-03-14 02:42:44 +0000 | [diff] [blame] | 266 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 267 | static SecMap sm_distinguished[3]; |
njn | b8dca86 | 2005-03-14 02:42:44 +0000 | [diff] [blame] | 268 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 269 | static INLINE Bool is_distinguished_sm ( SecMap* sm ) { |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 270 | return sm >= &sm_distinguished[0] && sm <= &sm_distinguished[2]; |
| 271 | } |
njn | b8dca86 | 2005-03-14 02:42:44 +0000 | [diff] [blame] | 272 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 273 | // Forward declaration |
| 274 | static void update_SM_counts(SecMap* oldSM, SecMap* newSM); |
| 275 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 276 | /* dist_sm points to one of our three distinguished secondaries. Make |
| 277 | a copy of it so that we can write to it. |
| 278 | */ |
| 279 | static SecMap* copy_for_writing ( SecMap* dist_sm ) |
| 280 | { |
| 281 | SecMap* new_sm; |
| 282 | tl_assert(dist_sm == &sm_distinguished[0] |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 283 | || dist_sm == &sm_distinguished[1] |
| 284 | || dist_sm == &sm_distinguished[2]); |
njn | b8dca86 | 2005-03-14 02:42:44 +0000 | [diff] [blame] | 285 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 286 | new_sm = VG_(am_shadow_alloc)(sizeof(SecMap)); |
| 287 | if (new_sm == NULL) |
| 288 | VG_(out_of_memory_NORETURN)( "memcheck:allocate new SecMap", |
| 289 | sizeof(SecMap) ); |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 290 | VG_(memcpy)(new_sm, dist_sm, sizeof(SecMap)); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 291 | update_SM_counts(dist_sm, new_sm); |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 292 | return new_sm; |
| 293 | } |
njn | b8dca86 | 2005-03-14 02:42:44 +0000 | [diff] [blame] | 294 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 295 | /* --------------- Stats --------------- */ |
| 296 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 297 | static Int n_issued_SMs = 0; |
| 298 | static Int n_deissued_SMs = 0; |
| 299 | static Int n_noaccess_SMs = N_PRIMARY_MAP; // start with many noaccess DSMs |
| 300 | static Int n_undefined_SMs = 0; |
| 301 | static Int n_defined_SMs = 0; |
| 302 | static Int n_non_DSM_SMs = 0; |
| 303 | static Int max_noaccess_SMs = 0; |
| 304 | static Int max_undefined_SMs = 0; |
| 305 | static Int max_defined_SMs = 0; |
| 306 | static Int max_non_DSM_SMs = 0; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 307 | |
| 308 | static ULong n_auxmap_searches = 0; |
| 309 | static ULong n_auxmap_cmps = 0; |
| 310 | static Int n_sanity_cheap = 0; |
| 311 | static Int n_sanity_expensive = 0; |
| 312 | |
| 313 | static Int n_secVBit_nodes = 0; |
| 314 | static Int max_secVBit_nodes = 0; |
| 315 | |
| 316 | static void update_SM_counts(SecMap* oldSM, SecMap* newSM) |
| 317 | { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 318 | if (oldSM == &sm_distinguished[SM_DIST_NOACCESS ]) n_noaccess_SMs --; |
| 319 | else if (oldSM == &sm_distinguished[SM_DIST_UNDEFINED]) n_undefined_SMs--; |
| 320 | else if (oldSM == &sm_distinguished[SM_DIST_DEFINED ]) n_defined_SMs --; |
| 321 | else { n_non_DSM_SMs --; |
| 322 | n_deissued_SMs ++; } |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 323 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 324 | if (newSM == &sm_distinguished[SM_DIST_NOACCESS ]) n_noaccess_SMs ++; |
| 325 | else if (newSM == &sm_distinguished[SM_DIST_UNDEFINED]) n_undefined_SMs++; |
| 326 | else if (newSM == &sm_distinguished[SM_DIST_DEFINED ]) n_defined_SMs ++; |
| 327 | else { n_non_DSM_SMs ++; |
| 328 | n_issued_SMs ++; } |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 329 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 330 | if (n_noaccess_SMs > max_noaccess_SMs ) max_noaccess_SMs = n_noaccess_SMs; |
| 331 | if (n_undefined_SMs > max_undefined_SMs) max_undefined_SMs = n_undefined_SMs; |
| 332 | if (n_defined_SMs > max_defined_SMs ) max_defined_SMs = n_defined_SMs; |
| 333 | if (n_non_DSM_SMs > max_non_DSM_SMs ) max_non_DSM_SMs = n_non_DSM_SMs; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 334 | } |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 335 | |
| 336 | /* --------------- Primary maps --------------- */ |
| 337 | |
| 338 | /* The main primary map. This covers some initial part of the address |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 339 | space, addresses 0 .. (N_PRIMARY_MAP << 16)-1. The rest of it is |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 340 | handled using the auxiliary primary map. |
| 341 | */ |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 342 | static SecMap* primary_map[N_PRIMARY_MAP]; |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 343 | |
| 344 | |
| 345 | /* An entry in the auxiliary primary map. base must be a 64k-aligned |
| 346 | value, and sm points at the relevant secondary map. As with the |
| 347 | main primary map, the secondary may be either a real secondary, or |
| 348 | one of the three distinguished secondaries. |
| 349 | */ |
| 350 | typedef |
| 351 | struct { |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 352 | Addr base; |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 353 | SecMap* sm; |
| 354 | } |
| 355 | AuxMapEnt; |
| 356 | |
| 357 | /* An expanding array of AuxMapEnts. */ |
sewardj | aba741d | 2005-06-09 13:56:07 +0000 | [diff] [blame] | 358 | #define N_AUXMAPS 20000 /* HACK */ |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 359 | static AuxMapEnt hacky_auxmaps[N_AUXMAPS]; |
| 360 | static Int auxmap_size = N_AUXMAPS; |
| 361 | static Int auxmap_used = 0; |
| 362 | static AuxMapEnt* auxmap = &hacky_auxmaps[0]; |
| 363 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 364 | |
| 365 | /* Find an entry in the auxiliary map. If an entry is found, move it |
| 366 | one step closer to the front of the array, then return its address. |
sewardj | 05fe85e | 2005-04-27 22:46:36 +0000 | [diff] [blame] | 367 | If an entry is not found, return NULL. Note carefully that |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 368 | because a each call potentially rearranges the entries, each call |
| 369 | to this function invalidates ALL AuxMapEnt*s previously obtained by |
| 370 | calling this fn. |
| 371 | */ |
sewardj | 05fe85e | 2005-04-27 22:46:36 +0000 | [diff] [blame] | 372 | static AuxMapEnt* maybe_find_in_auxmap ( Addr a ) |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 373 | { |
| 374 | UWord i; |
| 375 | tl_assert(a > MAX_PRIMARY_ADDRESS); |
| 376 | |
| 377 | a &= ~(Addr)0xFFFF; |
| 378 | |
| 379 | /* Search .. */ |
| 380 | n_auxmap_searches++; |
| 381 | for (i = 0; i < auxmap_used; i++) { |
| 382 | if (auxmap[i].base == a) |
| 383 | break; |
| 384 | } |
| 385 | n_auxmap_cmps += (ULong)(i+1); |
| 386 | |
| 387 | if (i < auxmap_used) { |
| 388 | /* Found it. Nudge it a bit closer to the front. */ |
| 389 | if (i > 0) { |
| 390 | AuxMapEnt tmp = auxmap[i-1]; |
| 391 | auxmap[i-1] = auxmap[i]; |
| 392 | auxmap[i] = tmp; |
| 393 | i--; |
| 394 | } |
| 395 | return &auxmap[i]; |
| 396 | } |
| 397 | |
sewardj | 05fe85e | 2005-04-27 22:46:36 +0000 | [diff] [blame] | 398 | return NULL; |
| 399 | } |
| 400 | |
| 401 | |
| 402 | /* Find an entry in the auxiliary map. If an entry is found, move it |
| 403 | one step closer to the front of the array, then return its address. |
| 404 | If an entry is not found, allocate one. Note carefully that |
| 405 | because a each call potentially rearranges the entries, each call |
| 406 | to this function invalidates ALL AuxMapEnt*s previously obtained by |
| 407 | calling this fn. |
| 408 | */ |
| 409 | static AuxMapEnt* find_or_alloc_in_auxmap ( Addr a ) |
| 410 | { |
| 411 | AuxMapEnt* am = maybe_find_in_auxmap(a); |
| 412 | if (am) |
| 413 | return am; |
| 414 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 415 | /* We didn't find it. Hmm. This is a new piece of address space. |
| 416 | We'll need to allocate a new AuxMap entry for it. */ |
| 417 | if (auxmap_used >= auxmap_size) { |
| 418 | tl_assert(auxmap_used == auxmap_size); |
| 419 | /* Out of auxmap entries. */ |
| 420 | tl_assert2(0, "failed to expand the auxmap table"); |
| 421 | } |
| 422 | |
| 423 | tl_assert(auxmap_used < auxmap_size); |
| 424 | |
| 425 | auxmap[auxmap_used].base = a & ~(Addr)0xFFFF; |
| 426 | auxmap[auxmap_used].sm = &sm_distinguished[SM_DIST_NOACCESS]; |
| 427 | |
| 428 | if (0) |
| 429 | VG_(printf)("new auxmap, base = 0x%llx\n", |
| 430 | (ULong)auxmap[auxmap_used].base ); |
| 431 | |
| 432 | auxmap_used++; |
| 433 | return &auxmap[auxmap_used-1]; |
| 434 | } |
| 435 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 436 | /* --------------- SecMap fundamentals --------------- */ |
| 437 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 438 | // In all these, 'low' means it's definitely in the main primary map, |
| 439 | // 'high' means it's definitely in the auxiliary table. |
| 440 | |
| 441 | static INLINE SecMap** get_secmap_low_ptr ( Addr a ) |
| 442 | { |
| 443 | UWord pm_off = a >> 16; |
| 444 | # if VG_DEBUG_MEMORY >= 1 |
| 445 | tl_assert(pm_off < N_PRIMARY_MAP); |
| 446 | # endif |
| 447 | return &primary_map[ pm_off ]; |
| 448 | } |
| 449 | |
| 450 | static INLINE SecMap** get_secmap_high_ptr ( Addr a ) |
| 451 | { |
| 452 | AuxMapEnt* am = find_or_alloc_in_auxmap(a); |
| 453 | return &am->sm; |
| 454 | } |
| 455 | |
| 456 | static SecMap** get_secmap_ptr ( Addr a ) |
| 457 | { |
| 458 | return ( a <= MAX_PRIMARY_ADDRESS |
| 459 | ? get_secmap_low_ptr(a) |
| 460 | : get_secmap_high_ptr(a)); |
| 461 | } |
| 462 | |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 463 | static INLINE SecMap* get_secmap_for_reading_low ( Addr a ) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 464 | { |
| 465 | return *get_secmap_low_ptr(a); |
| 466 | } |
| 467 | |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 468 | static INLINE SecMap* get_secmap_for_reading_high ( Addr a ) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 469 | { |
| 470 | return *get_secmap_high_ptr(a); |
| 471 | } |
| 472 | |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 473 | static INLINE SecMap* get_secmap_for_writing_low(Addr a) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 474 | { |
| 475 | SecMap** p = get_secmap_low_ptr(a); |
| 476 | if (EXPECTED_NOT_TAKEN(is_distinguished_sm(*p))) |
| 477 | *p = copy_for_writing(*p); |
| 478 | return *p; |
| 479 | } |
| 480 | |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 481 | static INLINE SecMap* get_secmap_for_writing_high ( Addr a ) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 482 | { |
| 483 | SecMap** p = get_secmap_high_ptr(a); |
| 484 | if (EXPECTED_NOT_TAKEN(is_distinguished_sm(*p))) |
| 485 | *p = copy_for_writing(*p); |
| 486 | return *p; |
| 487 | } |
| 488 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 489 | /* Produce the secmap for 'a', either from the primary map or by |
| 490 | ensuring there is an entry for it in the aux primary map. The |
| 491 | secmap may be a distinguished one as the caller will only want to |
| 492 | be able to read it. |
| 493 | */ |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 494 | static SecMap* get_secmap_for_reading ( Addr a ) |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 495 | { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 496 | return ( a <= MAX_PRIMARY_ADDRESS |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 497 | ? get_secmap_for_reading_low (a) |
| 498 | : get_secmap_for_reading_high(a) ); |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | /* Produce the secmap for 'a', either from the primary map or by |
| 502 | ensuring there is an entry for it in the aux primary map. The |
| 503 | secmap may not be a distinguished one, since the caller will want |
| 504 | to be able to write it. If it is a distinguished secondary, make a |
| 505 | writable copy of it, install it, and return the copy instead. (COW |
| 506 | semantics). |
| 507 | */ |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 508 | static SecMap* get_secmap_for_writing ( Addr a ) |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 509 | { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 510 | return ( a <= MAX_PRIMARY_ADDRESS |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 511 | ? get_secmap_for_writing_low (a) |
| 512 | : get_secmap_for_writing_high(a) ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | /* If 'a' has a SecMap, produce it. Else produce NULL. But don't |
| 516 | allocate one if one doesn't already exist. This is used by the |
| 517 | leak checker. |
| 518 | */ |
| 519 | static SecMap* maybe_get_secmap_for ( Addr a ) |
| 520 | { |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 521 | if (a <= MAX_PRIMARY_ADDRESS) { |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 522 | return get_secmap_for_reading_low(a); |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 523 | } else { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 524 | AuxMapEnt* am = maybe_find_in_auxmap(a); |
| 525 | return am ? am->sm : NULL; |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 529 | /* --------------- Fundamental functions --------------- */ |
| 530 | |
| 531 | static INLINE |
| 532 | void insert_vabits2_into_vabits8 ( Addr a, UChar vabits2, UChar* vabits8 ) |
| 533 | { |
| 534 | UInt shift = (a & 3) << 1; // shift by 0, 2, 4, or 6 |
| 535 | *vabits8 &= ~(0x3 << shift); // mask out the two old bits |
| 536 | *vabits8 |= (vabits2 << shift); // mask in the two new bits |
| 537 | } |
| 538 | |
| 539 | static INLINE |
| 540 | void insert_vabits4_into_vabits8 ( Addr a, UChar vabits4, UChar* vabits8 ) |
| 541 | { |
| 542 | UInt shift; |
| 543 | tl_assert(VG_IS_2_ALIGNED(a)); // Must be 2-aligned |
| 544 | shift = (a & 2) << 1; // shift by 0 or 4 |
| 545 | *vabits8 &= ~(0xf << shift); // mask out the four old bits |
| 546 | *vabits8 |= (vabits4 << shift); // mask in the four new bits |
| 547 | } |
| 548 | |
| 549 | static INLINE |
| 550 | UChar extract_vabits2_from_vabits8 ( Addr a, UChar vabits8 ) |
| 551 | { |
| 552 | UInt shift = (a & 3) << 1; // shift by 0, 2, 4, or 6 |
| 553 | vabits8 >>= shift; // shift the two bits to the bottom |
| 554 | return 0x3 & vabits8; // mask out the rest |
| 555 | } |
| 556 | |
| 557 | static INLINE |
| 558 | UChar extract_vabits4_from_vabits8 ( Addr a, UChar vabits8 ) |
| 559 | { |
| 560 | UInt shift; |
| 561 | tl_assert(VG_IS_2_ALIGNED(a)); // Must be 2-aligned |
| 562 | shift = (a & 2) << 1; // shift by 0 or 4 |
| 563 | vabits8 >>= shift; // shift the four bits to the bottom |
| 564 | return 0xf & vabits8; // mask out the rest |
| 565 | } |
| 566 | |
| 567 | // Note that these four are only used in slow cases. The fast cases do |
| 568 | // clever things like combine the auxmap check (in |
| 569 | // get_secmap_{read,writ}able) with alignment checks. |
| 570 | |
| 571 | // *** WARNING! *** |
| 572 | // Any time this function is called, if it is possible that vabits2 |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 573 | // is equal to VA_BITS2_PARTDEFINED, then the corresponding entry in the |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 574 | // sec-V-bits table must also be set! |
| 575 | static INLINE |
| 576 | void set_vabits2 ( Addr a, UChar vabits2 ) |
| 577 | { |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 578 | SecMap* sm = get_secmap_for_writing(a); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 579 | UWord sm_off = SM_OFF(a); |
| 580 | insert_vabits2_into_vabits8( a, vabits2, &(sm->vabits8[sm_off]) ); |
| 581 | } |
| 582 | |
| 583 | static INLINE |
| 584 | UChar get_vabits2 ( Addr a ) |
| 585 | { |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 586 | SecMap* sm = get_secmap_for_reading(a); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 587 | UWord sm_off = SM_OFF(a); |
| 588 | UChar vabits8 = sm->vabits8[sm_off]; |
| 589 | return extract_vabits2_from_vabits8(a, vabits8); |
| 590 | } |
| 591 | |
sewardj | f218491 | 2006-05-03 22:13:57 +0000 | [diff] [blame] | 592 | // *** WARNING! *** |
| 593 | // Any time this function is called, if it is possible that any of the |
| 594 | // 4 2-bit fields in vabits8 are equal to VA_BITS2_PARTDEFINED, then the |
| 595 | // corresponding entry(s) in the sec-V-bits table must also be set! |
| 596 | static INLINE |
| 597 | UChar get_vabits8_for_aligned_word32 ( Addr a ) |
| 598 | { |
| 599 | SecMap* sm = get_secmap_for_reading(a); |
| 600 | UWord sm_off = SM_OFF(a); |
| 601 | UChar vabits8 = sm->vabits8[sm_off]; |
| 602 | return vabits8; |
| 603 | } |
| 604 | |
| 605 | static INLINE |
| 606 | void set_vabits8_for_aligned_word32 ( Addr a, UChar vabits8 ) |
| 607 | { |
| 608 | SecMap* sm = get_secmap_for_writing(a); |
| 609 | UWord sm_off = SM_OFF(a); |
| 610 | sm->vabits8[sm_off] = vabits8; |
| 611 | } |
| 612 | |
| 613 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 614 | // Forward declarations |
| 615 | static UWord get_sec_vbits8(Addr a); |
| 616 | static void set_sec_vbits8(Addr a, UWord vbits8); |
| 617 | |
| 618 | // Returns False if there was an addressability error. |
| 619 | static INLINE |
| 620 | Bool set_vbits8 ( Addr a, UChar vbits8 ) |
| 621 | { |
| 622 | Bool ok = True; |
| 623 | UChar vabits2 = get_vabits2(a); |
| 624 | if ( VA_BITS2_NOACCESS != vabits2 ) { |
| 625 | // Addressable. Convert in-register format to in-memory format. |
| 626 | // Also remove any existing sec V bit entry for the byte if no |
| 627 | // longer necessary. |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 628 | if ( V_BITS8_DEFINED == vbits8 ) { vabits2 = VA_BITS2_DEFINED; } |
| 629 | else if ( V_BITS8_UNDEFINED == vbits8 ) { vabits2 = VA_BITS2_UNDEFINED; } |
| 630 | else { vabits2 = VA_BITS2_PARTDEFINED; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 631 | set_sec_vbits8(a, vbits8); } |
| 632 | set_vabits2(a, vabits2); |
| 633 | |
| 634 | } else { |
| 635 | // Unaddressable! Do nothing -- when writing to unaddressable |
| 636 | // memory it acts as a black hole, and the V bits can never be seen |
| 637 | // again. So we don't have to write them at all. |
| 638 | ok = False; |
| 639 | } |
| 640 | return ok; |
| 641 | } |
| 642 | |
| 643 | // Returns False if there was an addressability error. In that case, we put |
| 644 | // all defined bits into vbits8. |
| 645 | static INLINE |
| 646 | Bool get_vbits8 ( Addr a, UChar* vbits8 ) |
| 647 | { |
| 648 | Bool ok = True; |
| 649 | UChar vabits2 = get_vabits2(a); |
| 650 | |
| 651 | // Convert the in-memory format to in-register format. |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 652 | if ( VA_BITS2_DEFINED == vabits2 ) { *vbits8 = V_BITS8_DEFINED; } |
| 653 | else if ( VA_BITS2_UNDEFINED == vabits2 ) { *vbits8 = V_BITS8_UNDEFINED; } |
| 654 | else if ( VA_BITS2_NOACCESS == vabits2 ) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 655 | *vbits8 = V_BITS8_DEFINED; // Make V bits defined! |
| 656 | ok = False; |
| 657 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 658 | tl_assert( VA_BITS2_PARTDEFINED == vabits2 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 659 | *vbits8 = get_sec_vbits8(a); |
| 660 | } |
| 661 | return ok; |
| 662 | } |
| 663 | |
| 664 | |
| 665 | /* --------------- Secondary V bit table ------------ */ |
| 666 | |
| 667 | // This table holds the full V bit pattern for partially-defined bytes |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 668 | // (PDBs) that are represented by VA_BITS2_PARTDEFINED in the main shadow |
| 669 | // memory. |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 670 | // |
| 671 | // Note: the nodes in this table can become stale. Eg. if you write a PDB, |
| 672 | // then overwrite the same address with a fully defined byte, the sec-V-bit |
| 673 | // node will not necessarily be removed. This is because checking for |
| 674 | // whether removal is necessary would slow down the fast paths. |
| 675 | // |
| 676 | // To avoid the stale nodes building up too much, we periodically (once the |
| 677 | // table reaches a certain size) garbage collect (GC) the table by |
| 678 | // traversing it and evicting any "sufficiently stale" nodes, ie. nodes that |
| 679 | // are stale and haven't been touched for a certain number of collections. |
| 680 | // If more than a certain proportion of nodes survived, we increase the |
| 681 | // table size so that GCs occur less often. |
| 682 | // |
| 683 | // (So this a bit different to a traditional GC, where you definitely want |
| 684 | // to remove any dead nodes. It's more like we have a resizable cache and |
| 685 | // we're trying to find the right balance how many elements to evict and how |
| 686 | // big to make the cache.) |
| 687 | // |
| 688 | // This policy is designed to avoid bad table bloat in the worst case where |
| 689 | // a program creates huge numbers of stale PDBs -- we would get this bloat |
| 690 | // if we had no GC -- while handling well the case where a node becomes |
| 691 | // stale but shortly afterwards is rewritten with a PDB and so becomes |
| 692 | // non-stale again (which happens quite often, eg. in perf/bz2). If we just |
| 693 | // remove all stale nodes as soon as possible, we just end up re-adding a |
| 694 | // lot of them in later again. The "sufficiently stale" approach avoids |
| 695 | // this. (If a program has many live PDBs, performance will just suck, |
| 696 | // there's no way around that.) |
| 697 | |
| 698 | static OSet* secVBitTable; |
| 699 | |
| 700 | // Stats |
| 701 | static ULong sec_vbits_new_nodes = 0; |
| 702 | static ULong sec_vbits_updates = 0; |
| 703 | |
| 704 | // This must be a power of two; this is checked in mc_pre_clo_init(). |
| 705 | // The size chosen here is a trade-off: if the nodes are bigger (ie. cover |
| 706 | // a larger address range) they take more space but we can get multiple |
| 707 | // partially-defined bytes in one if they are close to each other, reducing |
| 708 | // the number of total nodes. In practice sometimes they are clustered (eg. |
| 709 | // perf/bz2 repeatedly writes then reads more than 20,000 in a contiguous |
| 710 | // row), but often not. So we choose something intermediate. |
| 711 | #define BYTES_PER_SEC_VBIT_NODE 16 |
| 712 | |
| 713 | // We make the table bigger if more than this many nodes survive a GC. |
| 714 | #define MAX_SURVIVOR_PROPORTION 0.5 |
| 715 | |
| 716 | // Each time we make the table bigger, we increase it by this much. |
| 717 | #define TABLE_GROWTH_FACTOR 2 |
| 718 | |
| 719 | // This defines "sufficiently stale" -- any node that hasn't been touched in |
| 720 | // this many GCs will be removed. |
| 721 | #define MAX_STALE_AGE 2 |
| 722 | |
| 723 | // We GC the table when it gets this many nodes in it, ie. it's effectively |
| 724 | // the table size. It can change. |
| 725 | static Int secVBitLimit = 1024; |
| 726 | |
| 727 | // The number of GCs done, used to age sec-V-bit nodes for eviction. |
| 728 | // Because it's unsigned, wrapping doesn't matter -- the right answer will |
| 729 | // come out anyway. |
| 730 | static UInt GCs_done = 0; |
| 731 | |
| 732 | typedef |
| 733 | struct { |
| 734 | Addr a; |
| 735 | UChar vbits8[BYTES_PER_SEC_VBIT_NODE]; |
| 736 | UInt last_touched; |
| 737 | } |
| 738 | SecVBitNode; |
| 739 | |
| 740 | static OSet* createSecVBitTable(void) |
| 741 | { |
| 742 | return VG_(OSet_Create)( offsetof(SecVBitNode, a), |
| 743 | NULL, // use fast comparisons |
| 744 | VG_(malloc), VG_(free) ); |
| 745 | } |
| 746 | |
| 747 | static void gcSecVBitTable(void) |
| 748 | { |
| 749 | OSet* secVBitTable2; |
| 750 | SecVBitNode* n; |
| 751 | Int i, n_nodes = 0, n_survivors = 0; |
| 752 | |
| 753 | GCs_done++; |
| 754 | |
| 755 | // Create the new table. |
| 756 | secVBitTable2 = createSecVBitTable(); |
| 757 | |
| 758 | // Traverse the table, moving fresh nodes into the new table. |
| 759 | VG_(OSet_ResetIter)(secVBitTable); |
| 760 | while ( (n = VG_(OSet_Next)(secVBitTable)) ) { |
| 761 | Bool keep = False; |
| 762 | if ( (GCs_done - n->last_touched) <= MAX_STALE_AGE ) { |
| 763 | // Keep node if it's been touched recently enough (regardless of |
| 764 | // freshness/staleness). |
| 765 | keep = True; |
| 766 | } else { |
| 767 | // Keep node if any of its bytes are non-stale. Using |
| 768 | // get_vabits2() for the lookup is not very efficient, but I don't |
| 769 | // think it matters. |
| 770 | for (i = 0; i < BYTES_PER_SEC_VBIT_NODE; i++) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 771 | if (VA_BITS2_PARTDEFINED == get_vabits2(n->a + i)) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 772 | keep = True; // Found a non-stale byte, so keep |
| 773 | break; |
| 774 | } |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | if ( keep ) { |
| 779 | // Insert a copy of the node into the new table. |
| 780 | SecVBitNode* n2 = |
| 781 | VG_(OSet_AllocNode)(secVBitTable2, sizeof(SecVBitNode)); |
| 782 | *n2 = *n; |
| 783 | VG_(OSet_Insert)(secVBitTable2, n2); |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | // Get the before and after sizes. |
| 788 | n_nodes = VG_(OSet_Size)(secVBitTable); |
| 789 | n_survivors = VG_(OSet_Size)(secVBitTable2); |
| 790 | |
| 791 | // Destroy the old table, and put the new one in its place. |
| 792 | VG_(OSet_Destroy)(secVBitTable, NULL); |
| 793 | secVBitTable = secVBitTable2; |
| 794 | |
| 795 | if (VG_(clo_verbosity) > 1) { |
| 796 | Char percbuf[6]; |
| 797 | VG_(percentify)(n_survivors, n_nodes, 1, 6, percbuf); |
| 798 | VG_(message)(Vg_DebugMsg, "memcheck GC: %d nodes, %d survivors (%s)", |
| 799 | n_nodes, n_survivors, percbuf); |
| 800 | } |
| 801 | |
| 802 | // Increase table size if necessary. |
| 803 | if (n_survivors > (secVBitLimit * MAX_SURVIVOR_PROPORTION)) { |
| 804 | secVBitLimit *= TABLE_GROWTH_FACTOR; |
| 805 | if (VG_(clo_verbosity) > 1) |
| 806 | VG_(message)(Vg_DebugMsg, "memcheck GC: increase table size to %d", |
| 807 | secVBitLimit); |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | static UWord get_sec_vbits8(Addr a) |
| 812 | { |
| 813 | Addr aAligned = VG_ROUNDDN(a, BYTES_PER_SEC_VBIT_NODE); |
| 814 | Int amod = a % BYTES_PER_SEC_VBIT_NODE; |
| 815 | SecVBitNode* n = VG_(OSet_Lookup)(secVBitTable, &aAligned); |
| 816 | UChar vbits8; |
| 817 | tl_assert2(n, "get_sec_vbits8: no node for address %p (%p)\n", aAligned, a); |
| 818 | // Shouldn't be fully defined or fully undefined -- those cases shouldn't |
| 819 | // make it to the secondary V bits table. |
| 820 | vbits8 = n->vbits8[amod]; |
| 821 | tl_assert(V_BITS8_DEFINED != vbits8 && V_BITS8_UNDEFINED != vbits8); |
| 822 | return vbits8; |
| 823 | } |
| 824 | |
| 825 | static void set_sec_vbits8(Addr a, UWord vbits8) |
| 826 | { |
| 827 | Addr aAligned = VG_ROUNDDN(a, BYTES_PER_SEC_VBIT_NODE); |
| 828 | Int i, amod = a % BYTES_PER_SEC_VBIT_NODE; |
| 829 | SecVBitNode* n = VG_(OSet_Lookup)(secVBitTable, &aAligned); |
| 830 | // Shouldn't be fully defined or fully undefined -- those cases shouldn't |
| 831 | // make it to the secondary V bits table. |
| 832 | tl_assert(V_BITS8_DEFINED != vbits8 && V_BITS8_UNDEFINED != vbits8); |
| 833 | if (n) { |
| 834 | n->vbits8[amod] = vbits8; // update |
| 835 | n->last_touched = GCs_done; |
| 836 | sec_vbits_updates++; |
| 837 | } else { |
| 838 | // New node: assign the specific byte, make the rest invalid (they |
| 839 | // should never be read as-is, but be cautious). |
| 840 | n = VG_(OSet_AllocNode)(secVBitTable, sizeof(SecVBitNode)); |
| 841 | n->a = aAligned; |
| 842 | for (i = 0; i < BYTES_PER_SEC_VBIT_NODE; i++) { |
| 843 | n->vbits8[i] = V_BITS8_UNDEFINED; |
| 844 | } |
| 845 | n->vbits8[amod] = vbits8; |
| 846 | n->last_touched = GCs_done; |
| 847 | |
| 848 | // Do a table GC if necessary. Nb: do this before inserting the new |
| 849 | // node, to avoid erroneously GC'ing the new node. |
| 850 | if (secVBitLimit == VG_(OSet_Size)(secVBitTable)) { |
| 851 | gcSecVBitTable(); |
| 852 | } |
| 853 | |
| 854 | // Insert the new node. |
| 855 | VG_(OSet_Insert)(secVBitTable, n); |
| 856 | sec_vbits_new_nodes++; |
| 857 | |
| 858 | n_secVBit_nodes = VG_(OSet_Size)(secVBitTable); |
| 859 | if (n_secVBit_nodes > max_secVBit_nodes) |
| 860 | max_secVBit_nodes = n_secVBit_nodes; |
| 861 | } |
| 862 | } |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 863 | |
| 864 | /* --------------- Endianness helpers --------------- */ |
| 865 | |
| 866 | /* Returns the offset in memory of the byteno-th most significant byte |
| 867 | in a wordszB-sized word, given the specified endianness. */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 868 | static INLINE UWord byte_offset_w ( UWord wordszB, Bool bigendian, |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 869 | UWord byteno ) { |
| 870 | return bigendian ? (wordszB-1-byteno) : byteno; |
| 871 | } |
| 872 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 873 | /* --------------- Load/store slow cases. --------------- */ |
| 874 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 875 | // Forward declarations |
| 876 | static void mc_record_address_error ( ThreadId tid, Addr a, |
| 877 | Int size, Bool isWrite ); |
| 878 | static void mc_record_core_mem_error ( ThreadId tid, Bool isUnaddr, Char* s ); |
| 879 | static void mc_record_param_error ( ThreadId tid, Addr a, Bool isReg, |
| 880 | Bool isUnaddr, Char* msg ); |
| 881 | static void mc_record_jump_error ( ThreadId tid, Addr a ); |
| 882 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 883 | static |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 884 | #ifndef PERF_FAST_LOADV |
| 885 | INLINE |
| 886 | #endif |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 887 | ULong mc_LOADVn_slow ( Addr a, SizeT nBits, Bool bigendian ) |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 888 | { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 889 | /* Make up a 64-bit result V word, which contains the loaded data for |
sewardj | f3d57dd | 2005-04-22 20:23:27 +0000 | [diff] [blame] | 890 | valid addresses and Defined for invalid addresses. Iterate over |
| 891 | the bytes in the word, from the most significant down to the |
| 892 | least. */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 893 | ULong vbits64 = V_BITS64_UNDEFINED; |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 894 | SizeT szB = nBits / 8; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 895 | SSizeT i = szB-1; // Must be signed |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 896 | SizeT n_addrs_bad = 0; |
| 897 | Addr ai; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 898 | Bool partial_load_exemption_applies; |
| 899 | UChar vbits8; |
| 900 | Bool ok; |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 901 | |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 902 | PROF_EVENT(30, "mc_LOADVn_slow"); |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 903 | tl_assert(nBits == 64 || nBits == 32 || nBits == 16 || nBits == 8); |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 904 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 905 | for (i = szB-1; i >= 0; i--) { |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 906 | PROF_EVENT(31, "mc_LOADVn_slow(loop)"); |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 907 | ai = a + byte_offset_w(szB, bigendian, i); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 908 | ok = get_vbits8(ai, &vbits8); |
| 909 | if (!ok) n_addrs_bad++; |
| 910 | vbits64 <<= 8; |
| 911 | vbits64 |= vbits8; |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 912 | } |
| 913 | |
sewardj | 0ded7a4 | 2005-11-08 02:25:37 +0000 | [diff] [blame] | 914 | /* This is a hack which avoids producing errors for code which |
| 915 | insists in stepping along byte strings in aligned word-sized |
| 916 | chunks, and there is a partially defined word at the end. (eg, |
| 917 | optimised strlen). Such code is basically broken at least WRT |
| 918 | semantics of ANSI C, but sometimes users don't have the option |
| 919 | to fix it, and so this option is provided. Note it is now |
| 920 | defaulted to not-engaged. |
| 921 | |
| 922 | A load from a partially-addressible place is allowed if: |
| 923 | - the command-line flag is set |
| 924 | - it's a word-sized, word-aligned load |
| 925 | - at least one of the addresses in the word *is* valid |
| 926 | */ |
| 927 | partial_load_exemption_applies |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 928 | = MC_(clo_partial_loads_ok) && szB == VG_WORDSIZE |
sewardj | 0ded7a4 | 2005-11-08 02:25:37 +0000 | [diff] [blame] | 929 | && VG_IS_WORD_ALIGNED(a) |
| 930 | && n_addrs_bad < VG_WORDSIZE; |
| 931 | |
| 932 | if (n_addrs_bad > 0 && !partial_load_exemption_applies) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 933 | mc_record_address_error( VG_(get_running_tid)(), a, szB, False ); |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 934 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 935 | return vbits64; |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 939 | static |
| 940 | #ifndef PERF_FAST_STOREV |
| 941 | INLINE |
| 942 | #endif |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 943 | void mc_STOREVn_slow ( Addr a, SizeT nBits, ULong vbytes, Bool bigendian ) |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 944 | { |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 945 | SizeT szB = nBits / 8; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 946 | SizeT i, n_addrs_bad = 0; |
| 947 | UChar vbits8; |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 948 | Addr ai; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 949 | Bool ok; |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 950 | |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 951 | PROF_EVENT(35, "mc_STOREVn_slow"); |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 952 | tl_assert(nBits == 64 || nBits == 32 || nBits == 16 || nBits == 8); |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 953 | |
| 954 | /* Dump vbytes in memory, iterating from least to most significant |
| 955 | byte. At the same time establish addressibility of the |
| 956 | location. */ |
| 957 | for (i = 0; i < szB; i++) { |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 958 | PROF_EVENT(36, "mc_STOREVn_slow(loop)"); |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 959 | ai = a + byte_offset_w(szB, bigendian, i); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 960 | vbits8 = vbytes & 0xff; |
| 961 | ok = set_vbits8(ai, vbits8); |
| 962 | if (!ok) n_addrs_bad++; |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 963 | vbytes >>= 8; |
| 964 | } |
| 965 | |
| 966 | /* If an address error has happened, report it. */ |
| 967 | if (n_addrs_bad > 0) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 968 | mc_record_address_error( VG_(get_running_tid)(), a, szB, True ); |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 972 | /*------------------------------------------------------------*/ |
| 973 | /*--- Setting permissions over address ranges. ---*/ |
| 974 | /*------------------------------------------------------------*/ |
| 975 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 976 | static void set_address_range_perms ( Addr a, SizeT lenT, UWord vabits16, |
| 977 | UWord dsm_num ) |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 978 | { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 979 | UWord sm_off, sm_off16; |
| 980 | UWord vabits2 = vabits16 & 0x3; |
| 981 | SizeT lenA, lenB, len_to_next_secmap; |
| 982 | Addr aNext; |
sewardj | ae986ca | 2005-10-12 12:53:20 +0000 | [diff] [blame] | 983 | SecMap* sm; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 984 | SecMap** sm_ptr; |
sewardj | ae986ca | 2005-10-12 12:53:20 +0000 | [diff] [blame] | 985 | SecMap* example_dsm; |
| 986 | |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 987 | PROF_EVENT(150, "set_address_range_perms"); |
| 988 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 989 | /* Check the V+A bits make sense. */ |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 990 | tl_assert(VA_BITS16_NOACCESS == vabits16 || |
| 991 | VA_BITS16_UNDEFINED == vabits16 || |
| 992 | VA_BITS16_DEFINED == vabits16); |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 993 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 994 | // This code should never write PDBs; ensure this. (See comment above |
| 995 | // set_vabits2().) |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 996 | tl_assert(VA_BITS2_PARTDEFINED != vabits2); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 997 | |
| 998 | if (lenT == 0) |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 999 | return; |
| 1000 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1001 | if (lenT > 100 * 1000 * 1000) { |
| 1002 | if (VG_(clo_verbosity) > 0 && !VG_(clo_xml)) { |
| 1003 | Char* s = "unknown???"; |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1004 | if (vabits16 == VA_BITS16_NOACCESS ) s = "noaccess"; |
| 1005 | if (vabits16 == VA_BITS16_UNDEFINED) s = "undefined"; |
| 1006 | if (vabits16 == VA_BITS16_DEFINED ) s = "defined"; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1007 | VG_(message)(Vg_UserMsg, "Warning: set address range perms: " |
| 1008 | "large range %lu (%s)", lenT, s); |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1009 | } |
| 1010 | } |
| 1011 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1012 | #ifndef PERF_FAST_SARP |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1013 | /*------------------ debug-only case ------------------ */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1014 | { |
| 1015 | // Endianness doesn't matter here because all bytes are being set to |
| 1016 | // the same value. |
| 1017 | // Nb: We don't have to worry about updating the sec-V-bits table |
| 1018 | // after these set_vabits2() calls because this code never writes |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1019 | // VA_BITS2_PARTDEFINED values. |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1020 | SizeT i; |
| 1021 | for (i = 0; i < lenT; i++) { |
| 1022 | set_vabits2(a + i, vabits2); |
| 1023 | } |
| 1024 | return; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1025 | } |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1026 | #endif |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1027 | |
| 1028 | /*------------------ standard handling ------------------ */ |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1029 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1030 | /* Get the distinguished secondary that we might want |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1031 | to use (part of the space-compression scheme). */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1032 | example_dsm = &sm_distinguished[dsm_num]; |
| 1033 | |
| 1034 | // We have to handle ranges covering various combinations of partial and |
| 1035 | // whole sec-maps. Here is how parts 1, 2 and 3 are used in each case. |
| 1036 | // Cases marked with a '*' are common. |
| 1037 | // |
| 1038 | // TYPE PARTS USED |
| 1039 | // ---- ---------- |
| 1040 | // * one partial sec-map (p) 1 |
| 1041 | // - one whole sec-map (P) 2 |
| 1042 | // |
| 1043 | // * two partial sec-maps (pp) 1,3 |
| 1044 | // - one partial, one whole sec-map (pP) 1,2 |
| 1045 | // - one whole, one partial sec-map (Pp) 2,3 |
| 1046 | // - two whole sec-maps (PP) 2,2 |
| 1047 | // |
| 1048 | // * one partial, one whole, one partial (pPp) 1,2,3 |
| 1049 | // - one partial, two whole (pPP) 1,2,2 |
| 1050 | // - two whole, one partial (PPp) 2,2,3 |
| 1051 | // - three whole (PPP) 2,2,2 |
| 1052 | // |
| 1053 | // * one partial, N-2 whole, one partial (pP...Pp) 1,2...2,3 |
| 1054 | // - one partial, N-1 whole (pP...PP) 1,2...2,2 |
| 1055 | // - N-1 whole, one partial (PP...Pp) 2,2...2,3 |
| 1056 | // - N whole (PP...PP) 2,2...2,3 |
| 1057 | |
| 1058 | // Break up total length (lenT) into two parts: length in the first |
| 1059 | // sec-map (lenA), and the rest (lenB); lenT == lenA + lenB. |
| 1060 | aNext = start_of_this_sm(a) + SM_SIZE; |
| 1061 | len_to_next_secmap = aNext - a; |
| 1062 | if ( lenT <= len_to_next_secmap ) { |
| 1063 | // Range entirely within one sec-map. Covers almost all cases. |
| 1064 | PROF_EVENT(151, "set_address_range_perms-single-secmap"); |
| 1065 | lenA = lenT; |
| 1066 | lenB = 0; |
| 1067 | } else if (is_start_of_sm(a)) { |
| 1068 | // Range spans at least one whole sec-map, and starts at the beginning |
| 1069 | // of a sec-map; skip to Part 2. |
| 1070 | PROF_EVENT(152, "set_address_range_perms-startof-secmap"); |
| 1071 | lenA = 0; |
| 1072 | lenB = lenT; |
| 1073 | goto part2; |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1074 | } else { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1075 | // Range spans two or more sec-maps, first one is partial. |
| 1076 | PROF_EVENT(153, "set_address_range_perms-multiple-secmaps"); |
| 1077 | lenA = len_to_next_secmap; |
| 1078 | lenB = lenT - lenA; |
| 1079 | } |
| 1080 | |
| 1081 | //------------------------------------------------------------------------ |
| 1082 | // Part 1: Deal with the first sec_map. Most of the time the range will be |
| 1083 | // entirely within a sec_map and this part alone will suffice. Also, |
| 1084 | // doing it this way lets us avoid repeatedly testing for the crossing of |
| 1085 | // a sec-map boundary within these loops. |
| 1086 | //------------------------------------------------------------------------ |
| 1087 | |
| 1088 | // If it's distinguished, make it undistinguished if necessary. |
| 1089 | sm_ptr = get_secmap_ptr(a); |
| 1090 | if (is_distinguished_sm(*sm_ptr)) { |
| 1091 | if (*sm_ptr == example_dsm) { |
| 1092 | // Sec-map already has the V+A bits that we want, so skip. |
| 1093 | PROF_EVENT(154, "set_address_range_perms-dist-sm1-quick"); |
| 1094 | a = aNext; |
| 1095 | lenA = 0; |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1096 | } else { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1097 | PROF_EVENT(155, "set_address_range_perms-dist-sm1"); |
| 1098 | *sm_ptr = copy_for_writing(*sm_ptr); |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1099 | } |
| 1100 | } |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1101 | sm = *sm_ptr; |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1102 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1103 | // 1 byte steps |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1104 | while (True) { |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1105 | if (VG_IS_8_ALIGNED(a)) break; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1106 | if (lenA < 1) break; |
| 1107 | PROF_EVENT(156, "set_address_range_perms-loop1a"); |
| 1108 | sm_off = SM_OFF(a); |
| 1109 | insert_vabits2_into_vabits8( a, vabits2, &(sm->vabits8[sm_off]) ); |
| 1110 | a += 1; |
| 1111 | lenA -= 1; |
| 1112 | } |
| 1113 | // 8-aligned, 8 byte steps |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1114 | while (True) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1115 | if (lenA < 8) break; |
| 1116 | PROF_EVENT(157, "set_address_range_perms-loop8a"); |
| 1117 | sm_off16 = SM_OFF_16(a); |
| 1118 | ((UShort*)(sm->vabits8))[sm_off16] = vabits16; |
| 1119 | a += 8; |
| 1120 | lenA -= 8; |
| 1121 | } |
| 1122 | // 1 byte steps |
| 1123 | while (True) { |
| 1124 | if (lenA < 1) break; |
| 1125 | PROF_EVENT(158, "set_address_range_perms-loop1b"); |
| 1126 | sm_off = SM_OFF(a); |
| 1127 | insert_vabits2_into_vabits8( a, vabits2, &(sm->vabits8[sm_off]) ); |
| 1128 | a += 1; |
| 1129 | lenA -= 1; |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1132 | // We've finished the first sec-map. Is that it? |
| 1133 | if (lenB == 0) |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1134 | return; |
| 1135 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1136 | //------------------------------------------------------------------------ |
| 1137 | // Part 2: Fast-set entire sec-maps at a time. |
| 1138 | //------------------------------------------------------------------------ |
| 1139 | part2: |
| 1140 | // 64KB-aligned, 64KB steps. |
| 1141 | // Nb: we can reach here with lenB < SM_SIZE |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1142 | while (True) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1143 | if (lenB < SM_SIZE) break; |
| 1144 | tl_assert(is_start_of_sm(a)); |
| 1145 | PROF_EVENT(159, "set_address_range_perms-loop64K"); |
| 1146 | sm_ptr = get_secmap_ptr(a); |
| 1147 | if (!is_distinguished_sm(*sm_ptr)) { |
| 1148 | PROF_EVENT(160, "set_address_range_perms-loop64K-free-dist-sm"); |
| 1149 | // Free the non-distinguished sec-map that we're replacing. This |
| 1150 | // case happens moderately often, enough to be worthwhile. |
| 1151 | VG_(am_munmap_valgrind)((Addr)*sm_ptr, sizeof(SecMap)); |
| 1152 | } |
| 1153 | update_SM_counts(*sm_ptr, example_dsm); |
| 1154 | // Make the sec-map entry point to the example DSM |
| 1155 | *sm_ptr = example_dsm; |
| 1156 | lenB -= SM_SIZE; |
| 1157 | a += SM_SIZE; |
| 1158 | } |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1159 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1160 | // We've finished the whole sec-maps. Is that it? |
| 1161 | if (lenB == 0) |
| 1162 | return; |
| 1163 | |
| 1164 | //------------------------------------------------------------------------ |
| 1165 | // Part 3: Finish off the final partial sec-map, if necessary. |
| 1166 | //------------------------------------------------------------------------ |
| 1167 | |
| 1168 | tl_assert(is_start_of_sm(a) && lenB < SM_SIZE); |
| 1169 | |
| 1170 | // If it's distinguished, make it undistinguished if necessary. |
| 1171 | sm_ptr = get_secmap_ptr(a); |
| 1172 | if (is_distinguished_sm(*sm_ptr)) { |
| 1173 | if (*sm_ptr == example_dsm) { |
| 1174 | // Sec-map already has the V+A bits that we want, so stop. |
| 1175 | PROF_EVENT(161, "set_address_range_perms-dist-sm2-quick"); |
| 1176 | return; |
| 1177 | } else { |
| 1178 | PROF_EVENT(162, "set_address_range_perms-dist-sm2"); |
| 1179 | *sm_ptr = copy_for_writing(*sm_ptr); |
| 1180 | } |
| 1181 | } |
| 1182 | sm = *sm_ptr; |
| 1183 | |
| 1184 | // 8-aligned, 8 byte steps |
| 1185 | while (True) { |
| 1186 | if (lenB < 8) break; |
| 1187 | PROF_EVENT(163, "set_address_range_perms-loop8b"); |
| 1188 | sm_off16 = SM_OFF_16(a); |
| 1189 | ((UShort*)(sm->vabits8))[sm_off16] = vabits16; |
| 1190 | a += 8; |
| 1191 | lenB -= 8; |
| 1192 | } |
| 1193 | // 1 byte steps |
| 1194 | while (True) { |
| 1195 | if (lenB < 1) return; |
| 1196 | PROF_EVENT(164, "set_address_range_perms-loop1c"); |
| 1197 | sm_off = SM_OFF(a); |
| 1198 | insert_vabits2_into_vabits8( a, vabits2, &(sm->vabits8[sm_off]) ); |
| 1199 | a += 1; |
| 1200 | lenB -= 1; |
| 1201 | } |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1202 | } |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 1203 | |
sewardj | c859fbf | 2005-04-22 21:10:28 +0000 | [diff] [blame] | 1204 | |
| 1205 | /* --- Set permissions for arbitrary address ranges --- */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1206 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1207 | void MC_(make_mem_noaccess) ( Addr a, SizeT len ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1208 | { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1209 | PROF_EVENT(40, "MC_(make_mem_noaccess)"); |
| 1210 | DEBUG("MC_(make_mem_noaccess)(%p, %lu)\n", a, len); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1211 | set_address_range_perms ( a, len, VA_BITS16_NOACCESS, SM_DIST_NOACCESS ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1214 | void MC_(make_mem_undefined) ( Addr a, SizeT len ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1215 | { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1216 | PROF_EVENT(41, "MC_(make_mem_undefined)"); |
| 1217 | DEBUG("MC_(make_mem_undefined)(%p, %lu)\n", a, len); |
| 1218 | set_address_range_perms ( a, len, VA_BITS16_UNDEFINED, SM_DIST_UNDEFINED ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1221 | void MC_(make_mem_defined) ( Addr a, SizeT len ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1222 | { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1223 | PROF_EVENT(42, "MC_(make_mem_defined)"); |
| 1224 | DEBUG("MC_(make_mem_defined)(%p, %lu)\n", a, len); |
| 1225 | set_address_range_perms ( a, len, VA_BITS16_DEFINED, SM_DIST_DEFINED ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
sewardj | fb1e9ad | 2006-03-10 13:41:58 +0000 | [diff] [blame] | 1228 | /* For each byte in [a,a+len), if the byte is addressable, make it be |
| 1229 | defined, but if it isn't addressible, leave it alone. In other |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1230 | words a version of MC_(make_mem_defined) that doesn't mess with |
sewardj | fb1e9ad | 2006-03-10 13:41:58 +0000 | [diff] [blame] | 1231 | addressibility. Low-performance implementation. */ |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1232 | static void make_mem_defined_if_addressable ( Addr a, SizeT len ) |
sewardj | fb1e9ad | 2006-03-10 13:41:58 +0000 | [diff] [blame] | 1233 | { |
| 1234 | SizeT i; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1235 | UChar vabits2; |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1236 | DEBUG("make_mem_defined_if_addressable(%p, %llu)\n", a, (ULong)len); |
sewardj | fb1e9ad | 2006-03-10 13:41:58 +0000 | [diff] [blame] | 1237 | for (i = 0; i < len; i++) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1238 | vabits2 = get_vabits2( a+i ); |
| 1239 | if (EXPECTED_TAKEN(VA_BITS2_NOACCESS != vabits2)) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1240 | set_vabits2(a+i, VA_BITS2_DEFINED); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1241 | } |
sewardj | fb1e9ad | 2006-03-10 13:41:58 +0000 | [diff] [blame] | 1242 | } |
| 1243 | } |
| 1244 | |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 1245 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1246 | /* --- Block-copy permissions (needed for implementing realloc() and |
| 1247 | sys_mremap). --- */ |
sewardj | c859fbf | 2005-04-22 21:10:28 +0000 | [diff] [blame] | 1248 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1249 | void MC_(copy_address_range_state) ( Addr src, Addr dst, SizeT len ) |
sewardj | c859fbf | 2005-04-22 21:10:28 +0000 | [diff] [blame] | 1250 | { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1251 | SizeT i, j; |
sewardj | f218491 | 2006-05-03 22:13:57 +0000 | [diff] [blame] | 1252 | UChar vabits2, vabits8; |
| 1253 | Bool aligned, nooverlap; |
sewardj | c859fbf | 2005-04-22 21:10:28 +0000 | [diff] [blame] | 1254 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1255 | DEBUG("MC_(copy_address_range_state)\n"); |
| 1256 | PROF_EVENT(50, "MC_(copy_address_range_state)"); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1257 | |
sewardj | f218491 | 2006-05-03 22:13:57 +0000 | [diff] [blame] | 1258 | if (len == 0 || src == dst) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1259 | return; |
| 1260 | |
sewardj | f218491 | 2006-05-03 22:13:57 +0000 | [diff] [blame] | 1261 | aligned = VG_IS_4_ALIGNED(src) && VG_IS_4_ALIGNED(dst); |
| 1262 | nooverlap = src+len <= dst || dst+len <= src; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1263 | |
sewardj | f218491 | 2006-05-03 22:13:57 +0000 | [diff] [blame] | 1264 | if (nooverlap && aligned) { |
| 1265 | |
| 1266 | /* Vectorised fast case, when no overlap and suitably aligned */ |
| 1267 | /* vector loop */ |
| 1268 | i = 0; |
| 1269 | while (len >= 4) { |
| 1270 | vabits8 = get_vabits8_for_aligned_word32( src+i ); |
| 1271 | set_vabits8_for_aligned_word32( dst+i, vabits8 ); |
| 1272 | if (EXPECTED_TAKEN(VA_BITS8_DEFINED == vabits8 |
| 1273 | || VA_BITS8_UNDEFINED == vabits8 |
| 1274 | || VA_BITS8_NOACCESS == vabits8)) { |
| 1275 | /* do nothing */ |
| 1276 | } else { |
| 1277 | /* have to copy secondary map info */ |
| 1278 | if (VA_BITS2_PARTDEFINED == get_vabits2( src+i+0 )) |
| 1279 | set_sec_vbits8( dst+i+0, get_sec_vbits8( src+i+0 ) ); |
| 1280 | if (VA_BITS2_PARTDEFINED == get_vabits2( src+i+1 )) |
| 1281 | set_sec_vbits8( dst+i+1, get_sec_vbits8( src+i+1 ) ); |
| 1282 | if (VA_BITS2_PARTDEFINED == get_vabits2( src+i+2 )) |
| 1283 | set_sec_vbits8( dst+i+2, get_sec_vbits8( src+i+2 ) ); |
| 1284 | if (VA_BITS2_PARTDEFINED == get_vabits2( src+i+3 )) |
| 1285 | set_sec_vbits8( dst+i+3, get_sec_vbits8( src+i+3 ) ); |
| 1286 | } |
| 1287 | i += 4; |
| 1288 | len -= 4; |
| 1289 | } |
| 1290 | /* fixup loop */ |
| 1291 | while (len >= 1) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1292 | vabits2 = get_vabits2( src+i ); |
| 1293 | set_vabits2( dst+i, vabits2 ); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1294 | if (VA_BITS2_PARTDEFINED == vabits2) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1295 | set_sec_vbits8( dst+i, get_sec_vbits8( src+i ) ); |
| 1296 | } |
sewardj | f218491 | 2006-05-03 22:13:57 +0000 | [diff] [blame] | 1297 | i++; |
| 1298 | len--; |
| 1299 | } |
| 1300 | |
| 1301 | } else { |
| 1302 | |
| 1303 | /* We have to do things the slow way */ |
| 1304 | if (src < dst) { |
| 1305 | for (i = 0, j = len-1; i < len; i++, j--) { |
| 1306 | PROF_EVENT(51, "MC_(copy_address_range_state)(loop)"); |
| 1307 | vabits2 = get_vabits2( src+j ); |
| 1308 | set_vabits2( dst+j, vabits2 ); |
| 1309 | if (VA_BITS2_PARTDEFINED == vabits2) { |
| 1310 | set_sec_vbits8( dst+j, get_sec_vbits8( src+j ) ); |
| 1311 | } |
| 1312 | } |
| 1313 | } |
| 1314 | |
| 1315 | if (src > dst) { |
| 1316 | for (i = 0; i < len; i++) { |
| 1317 | PROF_EVENT(52, "MC_(copy_address_range_state)(loop)"); |
| 1318 | vabits2 = get_vabits2( src+i ); |
| 1319 | set_vabits2( dst+i, vabits2 ); |
| 1320 | if (VA_BITS2_PARTDEFINED == vabits2) { |
| 1321 | set_sec_vbits8( dst+i, get_sec_vbits8( src+i ) ); |
| 1322 | } |
| 1323 | } |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1324 | } |
sewardj | c859fbf | 2005-04-22 21:10:28 +0000 | [diff] [blame] | 1325 | } |
sewardj | f218491 | 2006-05-03 22:13:57 +0000 | [diff] [blame] | 1326 | |
sewardj | c859fbf | 2005-04-22 21:10:28 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | |
| 1330 | /* --- Fast case permission setters, for dealing with stacks. --- */ |
| 1331 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1332 | static INLINE |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1333 | void make_aligned_word32_undefined ( Addr a ) |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 1334 | { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1335 | UWord sm_off; |
sewardj | ae986ca | 2005-10-12 12:53:20 +0000 | [diff] [blame] | 1336 | SecMap* sm; |
| 1337 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1338 | PROF_EVENT(300, "make_aligned_word32_undefined"); |
sewardj | 5d28efc | 2005-04-21 22:16:29 +0000 | [diff] [blame] | 1339 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1340 | #ifndef PERF_FAST_STACK2 |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1341 | MC_(make_mem_undefined)(a, 4); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1342 | #else |
| 1343 | if (EXPECTED_NOT_TAKEN(a > MAX_PRIMARY_ADDRESS)) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1344 | PROF_EVENT(301, "make_aligned_word32_undefined-slow1"); |
| 1345 | MC_(make_mem_undefined)(a, 4); |
sewardj | 5d28efc | 2005-04-21 22:16:29 +0000 | [diff] [blame] | 1346 | return; |
| 1347 | } |
| 1348 | |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 1349 | sm = get_secmap_for_writing_low(a); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1350 | sm_off = SM_OFF(a); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1351 | sm->vabits8[sm_off] = VA_BITS8_UNDEFINED; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1352 | #endif |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
sewardj | 5d28efc | 2005-04-21 22:16:29 +0000 | [diff] [blame] | 1355 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1356 | static INLINE |
| 1357 | void make_aligned_word32_noaccess ( Addr a ) |
sewardj | 5d28efc | 2005-04-21 22:16:29 +0000 | [diff] [blame] | 1358 | { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1359 | UWord sm_off; |
sewardj | ae986ca | 2005-10-12 12:53:20 +0000 | [diff] [blame] | 1360 | SecMap* sm; |
| 1361 | |
sewardj | 5d28efc | 2005-04-21 22:16:29 +0000 | [diff] [blame] | 1362 | PROF_EVENT(310, "make_aligned_word32_noaccess"); |
| 1363 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1364 | #ifndef PERF_FAST_STACK2 |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1365 | MC_(make_mem_noaccess)(a, 4); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1366 | #else |
| 1367 | if (EXPECTED_NOT_TAKEN(a > MAX_PRIMARY_ADDRESS)) { |
sewardj | 5d28efc | 2005-04-21 22:16:29 +0000 | [diff] [blame] | 1368 | PROF_EVENT(311, "make_aligned_word32_noaccess-slow1"); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1369 | MC_(make_mem_noaccess)(a, 4); |
sewardj | 5d28efc | 2005-04-21 22:16:29 +0000 | [diff] [blame] | 1370 | return; |
| 1371 | } |
| 1372 | |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 1373 | sm = get_secmap_for_writing_low(a); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1374 | sm_off = SM_OFF(a); |
| 1375 | sm->vabits8[sm_off] = VA_BITS8_NOACCESS; |
| 1376 | #endif |
sewardj | 5d28efc | 2005-04-21 22:16:29 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 1380 | /* Nb: by "aligned" here we mean 8-byte aligned */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1381 | static INLINE |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1382 | void make_aligned_word64_undefined ( Addr a ) |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 1383 | { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1384 | UWord sm_off16; |
sewardj | ae986ca | 2005-10-12 12:53:20 +0000 | [diff] [blame] | 1385 | SecMap* sm; |
| 1386 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1387 | PROF_EVENT(320, "make_aligned_word64_undefined"); |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1388 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1389 | #ifndef PERF_FAST_STACK2 |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1390 | MC_(make_mem_undefined)(a, 8); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1391 | #else |
| 1392 | if (EXPECTED_NOT_TAKEN(a > MAX_PRIMARY_ADDRESS)) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1393 | PROF_EVENT(321, "make_aligned_word64_undefined-slow1"); |
| 1394 | MC_(make_mem_undefined)(a, 8); |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1395 | return; |
| 1396 | } |
| 1397 | |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 1398 | sm = get_secmap_for_writing_low(a); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1399 | sm_off16 = SM_OFF_16(a); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1400 | ((UShort*)(sm->vabits8))[sm_off16] = VA_BITS16_UNDEFINED; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1401 | #endif |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1404 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1405 | static INLINE |
| 1406 | void make_aligned_word64_noaccess ( Addr a ) |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 1407 | { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1408 | UWord sm_off16; |
sewardj | ae986ca | 2005-10-12 12:53:20 +0000 | [diff] [blame] | 1409 | SecMap* sm; |
| 1410 | |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1411 | PROF_EVENT(330, "make_aligned_word64_noaccess"); |
| 1412 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1413 | #ifndef PERF_FAST_STACK2 |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1414 | MC_(make_mem_noaccess)(a, 8); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1415 | #else |
| 1416 | if (EXPECTED_NOT_TAKEN(a > MAX_PRIMARY_ADDRESS)) { |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1417 | PROF_EVENT(331, "make_aligned_word64_noaccess-slow1"); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1418 | MC_(make_mem_noaccess)(a, 8); |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1419 | return; |
| 1420 | } |
| 1421 | |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 1422 | sm = get_secmap_for_writing_low(a); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1423 | sm_off16 = SM_OFF_16(a); |
| 1424 | ((UShort*)(sm->vabits8))[sm_off16] = VA_BITS16_NOACCESS; |
| 1425 | #endif |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 1428 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1429 | /*------------------------------------------------------------*/ |
| 1430 | /*--- Stack pointer adjustment ---*/ |
| 1431 | /*------------------------------------------------------------*/ |
| 1432 | |
| 1433 | static void VG_REGPARM(1) mc_new_mem_stack_4(Addr new_SP) |
| 1434 | { |
| 1435 | PROF_EVENT(110, "new_mem_stack_4"); |
| 1436 | if (VG_IS_4_ALIGNED(new_SP)) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1437 | make_aligned_word32_undefined ( -VG_STACK_REDZONE_SZB + new_SP ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1438 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1439 | MC_(make_mem_undefined) ( -VG_STACK_REDZONE_SZB + new_SP, 4 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | static void VG_REGPARM(1) mc_die_mem_stack_4(Addr new_SP) |
| 1444 | { |
| 1445 | PROF_EVENT(120, "die_mem_stack_4"); |
| 1446 | if (VG_IS_4_ALIGNED(new_SP)) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1447 | make_aligned_word32_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-4 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1448 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1449 | MC_(make_mem_noaccess) ( -VG_STACK_REDZONE_SZB + new_SP-4, 4 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | static void VG_REGPARM(1) mc_new_mem_stack_8(Addr new_SP) |
| 1454 | { |
| 1455 | PROF_EVENT(111, "new_mem_stack_8"); |
| 1456 | if (VG_IS_8_ALIGNED(new_SP)) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1457 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1458 | } else if (VG_IS_4_ALIGNED(new_SP)) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1459 | make_aligned_word32_undefined ( -VG_STACK_REDZONE_SZB + new_SP ); |
| 1460 | make_aligned_word32_undefined ( -VG_STACK_REDZONE_SZB + new_SP+4 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1461 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1462 | MC_(make_mem_undefined) ( -VG_STACK_REDZONE_SZB + new_SP, 8 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1463 | } |
| 1464 | } |
| 1465 | |
| 1466 | static void VG_REGPARM(1) mc_die_mem_stack_8(Addr new_SP) |
| 1467 | { |
| 1468 | PROF_EVENT(121, "die_mem_stack_8"); |
| 1469 | if (VG_IS_8_ALIGNED(new_SP)) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1470 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-8 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1471 | } else if (VG_IS_4_ALIGNED(new_SP)) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1472 | make_aligned_word32_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-8 ); |
| 1473 | make_aligned_word32_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-4 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1474 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1475 | MC_(make_mem_noaccess) ( -VG_STACK_REDZONE_SZB + new_SP-8, 8 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1476 | } |
| 1477 | } |
| 1478 | |
| 1479 | static void VG_REGPARM(1) mc_new_mem_stack_12(Addr new_SP) |
| 1480 | { |
| 1481 | PROF_EVENT(112, "new_mem_stack_12"); |
| 1482 | if (VG_IS_8_ALIGNED(new_SP)) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1483 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP ); |
| 1484 | make_aligned_word32_undefined ( -VG_STACK_REDZONE_SZB + new_SP+8 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1485 | } else if (VG_IS_4_ALIGNED(new_SP)) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1486 | make_aligned_word32_undefined ( -VG_STACK_REDZONE_SZB + new_SP ); |
| 1487 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+4 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1488 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1489 | MC_(make_mem_undefined) ( -VG_STACK_REDZONE_SZB + new_SP, 12 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1490 | } |
| 1491 | } |
| 1492 | |
| 1493 | static void VG_REGPARM(1) mc_die_mem_stack_12(Addr new_SP) |
| 1494 | { |
| 1495 | PROF_EVENT(122, "die_mem_stack_12"); |
| 1496 | /* Note the -12 in the test */ |
| 1497 | if (VG_IS_8_ALIGNED(new_SP-12)) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1498 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-12 ); |
| 1499 | make_aligned_word32_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-4 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1500 | } else if (VG_IS_4_ALIGNED(new_SP)) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1501 | make_aligned_word32_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-12 ); |
| 1502 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-8 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1503 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1504 | MC_(make_mem_noaccess) ( -VG_STACK_REDZONE_SZB + new_SP-12, 12 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1505 | } |
| 1506 | } |
| 1507 | |
| 1508 | static void VG_REGPARM(1) mc_new_mem_stack_16(Addr new_SP) |
| 1509 | { |
| 1510 | PROF_EVENT(113, "new_mem_stack_16"); |
| 1511 | if (VG_IS_8_ALIGNED(new_SP)) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1512 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP ); |
| 1513 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+8 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1514 | } else if (VG_IS_4_ALIGNED(new_SP)) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1515 | make_aligned_word32_undefined ( -VG_STACK_REDZONE_SZB + new_SP ); |
| 1516 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+4 ); |
| 1517 | make_aligned_word32_undefined ( -VG_STACK_REDZONE_SZB + new_SP+12 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1518 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1519 | MC_(make_mem_undefined) ( -VG_STACK_REDZONE_SZB + new_SP, 16 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1520 | } |
| 1521 | } |
| 1522 | |
| 1523 | static void VG_REGPARM(1) mc_die_mem_stack_16(Addr new_SP) |
| 1524 | { |
| 1525 | PROF_EVENT(123, "die_mem_stack_16"); |
| 1526 | if (VG_IS_8_ALIGNED(new_SP)) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1527 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-16 ); |
| 1528 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-8 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1529 | } else if (VG_IS_4_ALIGNED(new_SP)) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1530 | make_aligned_word32_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-16 ); |
| 1531 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-12 ); |
| 1532 | make_aligned_word32_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-4 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1533 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1534 | MC_(make_mem_noaccess) ( -VG_STACK_REDZONE_SZB + new_SP-16, 16 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1535 | } |
| 1536 | } |
| 1537 | |
| 1538 | static void VG_REGPARM(1) mc_new_mem_stack_32(Addr new_SP) |
| 1539 | { |
| 1540 | PROF_EVENT(114, "new_mem_stack_32"); |
| 1541 | if (VG_IS_8_ALIGNED(new_SP)) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1542 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP ); |
| 1543 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+8 ); |
| 1544 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+16 ); |
| 1545 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+24 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1546 | } else if (VG_IS_4_ALIGNED(new_SP)) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1547 | make_aligned_word32_undefined ( -VG_STACK_REDZONE_SZB + new_SP ); |
| 1548 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+4 ); |
| 1549 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+12 ); |
| 1550 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+20 ); |
| 1551 | make_aligned_word32_undefined ( -VG_STACK_REDZONE_SZB + new_SP+28 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1552 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1553 | MC_(make_mem_undefined) ( -VG_STACK_REDZONE_SZB + new_SP, 32 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1554 | } |
| 1555 | } |
| 1556 | |
| 1557 | static void VG_REGPARM(1) mc_die_mem_stack_32(Addr new_SP) |
| 1558 | { |
| 1559 | PROF_EVENT(124, "die_mem_stack_32"); |
| 1560 | if (VG_IS_8_ALIGNED(new_SP)) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1561 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-32 ); |
| 1562 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-24 ); |
| 1563 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-16 ); |
| 1564 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP- 8 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1565 | } else if (VG_IS_4_ALIGNED(new_SP)) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1566 | make_aligned_word32_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-32 ); |
| 1567 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-28 ); |
| 1568 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-20 ); |
| 1569 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-12 ); |
| 1570 | make_aligned_word32_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-4 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1571 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1572 | MC_(make_mem_noaccess) ( -VG_STACK_REDZONE_SZB + new_SP-32, 32 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1573 | } |
| 1574 | } |
| 1575 | |
| 1576 | static void VG_REGPARM(1) mc_new_mem_stack_112(Addr new_SP) |
| 1577 | { |
| 1578 | PROF_EVENT(115, "new_mem_stack_112"); |
| 1579 | if (VG_IS_8_ALIGNED(new_SP)) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1580 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP ); |
| 1581 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+8 ); |
| 1582 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+16 ); |
| 1583 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+24 ); |
| 1584 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+32 ); |
| 1585 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+40 ); |
| 1586 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+48 ); |
| 1587 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+56 ); |
| 1588 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+64 ); |
| 1589 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+72 ); |
| 1590 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+80 ); |
| 1591 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+88 ); |
| 1592 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+96 ); |
| 1593 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+104); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1594 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1595 | MC_(make_mem_undefined) ( -VG_STACK_REDZONE_SZB + new_SP, 112 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1596 | } |
| 1597 | } |
| 1598 | |
| 1599 | static void VG_REGPARM(1) mc_die_mem_stack_112(Addr new_SP) |
| 1600 | { |
| 1601 | PROF_EVENT(125, "die_mem_stack_112"); |
| 1602 | if (VG_IS_8_ALIGNED(new_SP)) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1603 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-112); |
| 1604 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-104); |
| 1605 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-96 ); |
| 1606 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-88 ); |
| 1607 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-80 ); |
| 1608 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-72 ); |
| 1609 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-64 ); |
| 1610 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-56 ); |
| 1611 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-48 ); |
| 1612 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-40 ); |
| 1613 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-32 ); |
| 1614 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-24 ); |
| 1615 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-16 ); |
| 1616 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP- 8 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1617 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1618 | MC_(make_mem_noaccess) ( -VG_STACK_REDZONE_SZB + new_SP-112, 112 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1619 | } |
| 1620 | } |
| 1621 | |
| 1622 | static void VG_REGPARM(1) mc_new_mem_stack_128(Addr new_SP) |
| 1623 | { |
| 1624 | PROF_EVENT(116, "new_mem_stack_128"); |
| 1625 | if (VG_IS_8_ALIGNED(new_SP)) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1626 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP ); |
| 1627 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+8 ); |
| 1628 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+16 ); |
| 1629 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+24 ); |
| 1630 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+32 ); |
| 1631 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+40 ); |
| 1632 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+48 ); |
| 1633 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+56 ); |
| 1634 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+64 ); |
| 1635 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+72 ); |
| 1636 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+80 ); |
| 1637 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+88 ); |
| 1638 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+96 ); |
| 1639 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+104); |
| 1640 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+112); |
| 1641 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+120); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1642 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1643 | MC_(make_mem_undefined) ( -VG_STACK_REDZONE_SZB + new_SP, 128 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1644 | } |
| 1645 | } |
| 1646 | |
| 1647 | static void VG_REGPARM(1) mc_die_mem_stack_128(Addr new_SP) |
| 1648 | { |
| 1649 | PROF_EVENT(126, "die_mem_stack_128"); |
| 1650 | if (VG_IS_8_ALIGNED(new_SP)) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1651 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-128); |
| 1652 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-120); |
| 1653 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-112); |
| 1654 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-104); |
| 1655 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-96 ); |
| 1656 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-88 ); |
| 1657 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-80 ); |
| 1658 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-72 ); |
| 1659 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-64 ); |
| 1660 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-56 ); |
| 1661 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-48 ); |
| 1662 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-40 ); |
| 1663 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-32 ); |
| 1664 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-24 ); |
| 1665 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-16 ); |
| 1666 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP- 8 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1667 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1668 | MC_(make_mem_noaccess) ( -VG_STACK_REDZONE_SZB + new_SP-128, 128 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1669 | } |
| 1670 | } |
| 1671 | |
| 1672 | static void VG_REGPARM(1) mc_new_mem_stack_144(Addr new_SP) |
| 1673 | { |
| 1674 | PROF_EVENT(117, "new_mem_stack_144"); |
| 1675 | if (VG_IS_8_ALIGNED(new_SP)) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1676 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP ); |
| 1677 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+8 ); |
| 1678 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+16 ); |
| 1679 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+24 ); |
| 1680 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+32 ); |
| 1681 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+40 ); |
| 1682 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+48 ); |
| 1683 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+56 ); |
| 1684 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+64 ); |
| 1685 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+72 ); |
| 1686 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+80 ); |
| 1687 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+88 ); |
| 1688 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+96 ); |
| 1689 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+104); |
| 1690 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+112); |
| 1691 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+120); |
| 1692 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+128); |
| 1693 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+136); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1694 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1695 | MC_(make_mem_undefined) ( -VG_STACK_REDZONE_SZB + new_SP, 144 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1696 | } |
| 1697 | } |
| 1698 | |
| 1699 | static void VG_REGPARM(1) mc_die_mem_stack_144(Addr new_SP) |
| 1700 | { |
| 1701 | PROF_EVENT(127, "die_mem_stack_144"); |
| 1702 | if (VG_IS_8_ALIGNED(new_SP)) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1703 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-144); |
| 1704 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-136); |
| 1705 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-128); |
| 1706 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-120); |
| 1707 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-112); |
| 1708 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-104); |
| 1709 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-96 ); |
| 1710 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-88 ); |
| 1711 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-80 ); |
| 1712 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-72 ); |
| 1713 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-64 ); |
| 1714 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-56 ); |
| 1715 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-48 ); |
| 1716 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-40 ); |
| 1717 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-32 ); |
| 1718 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-24 ); |
| 1719 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-16 ); |
| 1720 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP- 8 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1721 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1722 | MC_(make_mem_noaccess) ( -VG_STACK_REDZONE_SZB + new_SP-144, 144 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1723 | } |
| 1724 | } |
| 1725 | |
| 1726 | static void VG_REGPARM(1) mc_new_mem_stack_160(Addr new_SP) |
| 1727 | { |
| 1728 | PROF_EVENT(118, "new_mem_stack_160"); |
| 1729 | if (VG_IS_8_ALIGNED(new_SP)) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1730 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP ); |
| 1731 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+8 ); |
| 1732 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+16 ); |
| 1733 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+24 ); |
| 1734 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+32 ); |
| 1735 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+40 ); |
| 1736 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+48 ); |
| 1737 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+56 ); |
| 1738 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+64 ); |
| 1739 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+72 ); |
| 1740 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+80 ); |
| 1741 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+88 ); |
| 1742 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+96 ); |
| 1743 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+104); |
| 1744 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+112); |
| 1745 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+120); |
| 1746 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+128); |
| 1747 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+136); |
| 1748 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+144); |
| 1749 | make_aligned_word64_undefined ( -VG_STACK_REDZONE_SZB + new_SP+152); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1750 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1751 | MC_(make_mem_undefined) ( -VG_STACK_REDZONE_SZB + new_SP, 160 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1752 | } |
| 1753 | } |
| 1754 | |
| 1755 | static void VG_REGPARM(1) mc_die_mem_stack_160(Addr new_SP) |
| 1756 | { |
| 1757 | PROF_EVENT(128, "die_mem_stack_160"); |
| 1758 | if (VG_IS_8_ALIGNED(new_SP)) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1759 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-160); |
| 1760 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-152); |
| 1761 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-144); |
| 1762 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-136); |
| 1763 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-128); |
| 1764 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-120); |
| 1765 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-112); |
| 1766 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-104); |
| 1767 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-96 ); |
| 1768 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-88 ); |
| 1769 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-80 ); |
| 1770 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-72 ); |
| 1771 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-64 ); |
| 1772 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-56 ); |
| 1773 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-48 ); |
| 1774 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-40 ); |
| 1775 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-32 ); |
| 1776 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-24 ); |
| 1777 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP-16 ); |
| 1778 | make_aligned_word64_noaccess ( -VG_STACK_REDZONE_SZB + new_SP- 8 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1779 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1780 | MC_(make_mem_noaccess) ( -VG_STACK_REDZONE_SZB + new_SP-160, 160 ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1781 | } |
| 1782 | } |
| 1783 | |
| 1784 | static void mc_new_mem_stack ( Addr a, SizeT len ) |
| 1785 | { |
| 1786 | PROF_EVENT(115, "new_mem_stack"); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1787 | MC_(make_mem_undefined) ( -VG_STACK_REDZONE_SZB + a, len ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1788 | } |
| 1789 | |
| 1790 | static void mc_die_mem_stack ( Addr a, SizeT len ) |
| 1791 | { |
| 1792 | PROF_EVENT(125, "die_mem_stack"); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1793 | MC_(make_mem_noaccess) ( -VG_STACK_REDZONE_SZB + a, len ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1794 | } |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 1795 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 1796 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1797 | /* The AMD64 ABI says: |
| 1798 | |
| 1799 | "The 128-byte area beyond the location pointed to by %rsp is considered |
| 1800 | to be reserved and shall not be modified by signal or interrupt |
| 1801 | handlers. Therefore, functions may use this area for temporary data |
| 1802 | that is not needed across function calls. In particular, leaf functions |
| 1803 | may use this area for their entire stack frame, rather than adjusting |
| 1804 | the stack pointer in the prologue and epilogue. This area is known as |
| 1805 | red zone [sic]." |
| 1806 | |
| 1807 | So after any call or return we need to mark this redzone as containing |
| 1808 | undefined values. |
| 1809 | |
| 1810 | Consider this: we're in function f. f calls g. g moves rsp down |
| 1811 | modestly (say 16 bytes) and writes stuff all over the red zone, making it |
| 1812 | defined. g returns. f is buggy and reads from parts of the red zone |
| 1813 | that it didn't write on. But because g filled that area in, f is going |
| 1814 | to be picking up defined V bits and so any errors from reading bits of |
| 1815 | the red zone it didn't write, will be missed. The only solution I could |
| 1816 | think of was to make the red zone undefined when g returns to f. |
| 1817 | |
| 1818 | This is in accordance with the ABI, which makes it clear the redzone |
| 1819 | is volatile across function calls. |
| 1820 | |
| 1821 | The problem occurs the other way round too: f could fill the RZ up |
| 1822 | with defined values and g could mistakenly read them. So the RZ |
| 1823 | also needs to be nuked on function calls. |
| 1824 | */ |
sewardj | 826ec49 | 2005-05-12 18:05:00 +0000 | [diff] [blame] | 1825 | void MC_(helperc_MAKE_STACK_UNINIT) ( Addr base, UWord len ) |
| 1826 | { |
| 1827 | tl_assert(sizeof(UWord) == sizeof(SizeT)); |
sewardj | 2a3a1a7 | 2005-05-12 23:25:43 +0000 | [diff] [blame] | 1828 | if (0) |
| 1829 | VG_(printf)("helperc_MAKE_STACK_UNINIT %p %d\n", base, len ); |
| 1830 | |
| 1831 | # if 0 |
| 1832 | /* Really slow version */ |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1833 | MC_(make_mem_undefined)(base, len); |
sewardj | 2a3a1a7 | 2005-05-12 23:25:43 +0000 | [diff] [blame] | 1834 | # endif |
| 1835 | |
| 1836 | # if 0 |
| 1837 | /* Slow(ish) version, which is fairly easily seen to be correct. |
| 1838 | */ |
| 1839 | if (EXPECTED_TAKEN( VG_IS_8_ALIGNED(base) && len==128 )) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1840 | make_aligned_word64_undefined(base + 0); |
| 1841 | make_aligned_word64_undefined(base + 8); |
| 1842 | make_aligned_word64_undefined(base + 16); |
| 1843 | make_aligned_word64_undefined(base + 24); |
sewardj | 2a3a1a7 | 2005-05-12 23:25:43 +0000 | [diff] [blame] | 1844 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1845 | make_aligned_word64_undefined(base + 32); |
| 1846 | make_aligned_word64_undefined(base + 40); |
| 1847 | make_aligned_word64_undefined(base + 48); |
| 1848 | make_aligned_word64_undefined(base + 56); |
sewardj | 2a3a1a7 | 2005-05-12 23:25:43 +0000 | [diff] [blame] | 1849 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1850 | make_aligned_word64_undefined(base + 64); |
| 1851 | make_aligned_word64_undefined(base + 72); |
| 1852 | make_aligned_word64_undefined(base + 80); |
| 1853 | make_aligned_word64_undefined(base + 88); |
sewardj | 2a3a1a7 | 2005-05-12 23:25:43 +0000 | [diff] [blame] | 1854 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1855 | make_aligned_word64_undefined(base + 96); |
| 1856 | make_aligned_word64_undefined(base + 104); |
| 1857 | make_aligned_word64_undefined(base + 112); |
| 1858 | make_aligned_word64_undefined(base + 120); |
sewardj | 2a3a1a7 | 2005-05-12 23:25:43 +0000 | [diff] [blame] | 1859 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1860 | MC_(make_mem_undefined)(base, len); |
sewardj | 2a3a1a7 | 2005-05-12 23:25:43 +0000 | [diff] [blame] | 1861 | } |
| 1862 | # endif |
| 1863 | |
| 1864 | /* Idea is: go fast when |
| 1865 | * 8-aligned and length is 128 |
| 1866 | * the sm is available in the main primary map |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1867 | * the address range falls entirely with a single secondary map |
| 1868 | If all those conditions hold, just update the V+A bits by writing |
| 1869 | directly into the vabits array. (If the sm was distinguished, this |
| 1870 | will make a copy and then write to it.) |
sewardj | 2a3a1a7 | 2005-05-12 23:25:43 +0000 | [diff] [blame] | 1871 | */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1872 | if (EXPECTED_TAKEN( len == 128 && VG_IS_8_ALIGNED(base) )) { |
| 1873 | /* Now we know the address range is suitably sized and aligned. */ |
| 1874 | UWord a_lo = (UWord)(base); |
| 1875 | UWord a_hi = (UWord)(base + 127); |
| 1876 | tl_assert(a_lo < a_hi); // paranoia: detect overflow |
| 1877 | if (a_hi < MAX_PRIMARY_ADDRESS) { |
| 1878 | // Now we know the entire range is within the main primary map. |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 1879 | SecMap* sm = get_secmap_for_writing_low(a_lo); |
| 1880 | SecMap* sm_hi = get_secmap_for_writing_low(a_hi); |
sewardj | 2a3a1a7 | 2005-05-12 23:25:43 +0000 | [diff] [blame] | 1881 | /* Now we know that the entire address range falls within a |
| 1882 | single secondary map, and that that secondary 'lives' in |
| 1883 | the main primary map. */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1884 | if (EXPECTED_TAKEN(sm == sm_hi)) { |
| 1885 | // Finally, we know that the range is entirely within one secmap. |
| 1886 | UWord v_off = SM_OFF(a_lo); |
| 1887 | UShort* p = (UShort*)(&sm->vabits8[v_off]); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1888 | p[ 0] = VA_BITS16_UNDEFINED; |
| 1889 | p[ 1] = VA_BITS16_UNDEFINED; |
| 1890 | p[ 2] = VA_BITS16_UNDEFINED; |
| 1891 | p[ 3] = VA_BITS16_UNDEFINED; |
| 1892 | p[ 4] = VA_BITS16_UNDEFINED; |
| 1893 | p[ 5] = VA_BITS16_UNDEFINED; |
| 1894 | p[ 6] = VA_BITS16_UNDEFINED; |
| 1895 | p[ 7] = VA_BITS16_UNDEFINED; |
| 1896 | p[ 8] = VA_BITS16_UNDEFINED; |
| 1897 | p[ 9] = VA_BITS16_UNDEFINED; |
| 1898 | p[10] = VA_BITS16_UNDEFINED; |
| 1899 | p[11] = VA_BITS16_UNDEFINED; |
| 1900 | p[12] = VA_BITS16_UNDEFINED; |
| 1901 | p[13] = VA_BITS16_UNDEFINED; |
| 1902 | p[14] = VA_BITS16_UNDEFINED; |
| 1903 | p[15] = VA_BITS16_UNDEFINED; |
sewardj | 2a3a1a7 | 2005-05-12 23:25:43 +0000 | [diff] [blame] | 1904 | return; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1905 | } |
sewardj | 2a3a1a7 | 2005-05-12 23:25:43 +0000 | [diff] [blame] | 1906 | } |
| 1907 | } |
| 1908 | |
sewardj | 2e1a677 | 2006-01-18 04:16:27 +0000 | [diff] [blame] | 1909 | /* 288 bytes (36 ULongs) is the magic value for ELF ppc64. */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1910 | if (EXPECTED_TAKEN( len == 128 && VG_IS_8_ALIGNED(base) )) { |
| 1911 | /* Now we know the address range is suitably sized and aligned. */ |
| 1912 | UWord a_lo = (UWord)(base); |
| 1913 | UWord a_hi = (UWord)(base + 127); |
| 1914 | tl_assert(a_lo < a_hi); // paranoia: detect overflow |
| 1915 | if (a_hi < MAX_PRIMARY_ADDRESS) { |
| 1916 | // Now we know the entire range is within the main primary map. |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 1917 | SecMap* sm = get_secmap_for_writing_low(a_lo); |
| 1918 | SecMap* sm_hi = get_secmap_for_writing_low(a_hi); |
sewardj | 2e1a677 | 2006-01-18 04:16:27 +0000 | [diff] [blame] | 1919 | /* Now we know that the entire address range falls within a |
| 1920 | single secondary map, and that that secondary 'lives' in |
| 1921 | the main primary map. */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1922 | if (EXPECTED_TAKEN(sm == sm_hi)) { |
| 1923 | // Finally, we know that the range is entirely within one secmap. |
| 1924 | UWord v_off = SM_OFF(a_lo); |
| 1925 | UShort* p = (UShort*)(&sm->vabits8[v_off]); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1926 | p[ 0] = VA_BITS16_UNDEFINED; |
| 1927 | p[ 1] = VA_BITS16_UNDEFINED; |
| 1928 | p[ 2] = VA_BITS16_UNDEFINED; |
| 1929 | p[ 3] = VA_BITS16_UNDEFINED; |
| 1930 | p[ 4] = VA_BITS16_UNDEFINED; |
| 1931 | p[ 5] = VA_BITS16_UNDEFINED; |
| 1932 | p[ 6] = VA_BITS16_UNDEFINED; |
| 1933 | p[ 7] = VA_BITS16_UNDEFINED; |
| 1934 | p[ 8] = VA_BITS16_UNDEFINED; |
| 1935 | p[ 9] = VA_BITS16_UNDEFINED; |
| 1936 | p[10] = VA_BITS16_UNDEFINED; |
| 1937 | p[11] = VA_BITS16_UNDEFINED; |
| 1938 | p[12] = VA_BITS16_UNDEFINED; |
| 1939 | p[13] = VA_BITS16_UNDEFINED; |
| 1940 | p[14] = VA_BITS16_UNDEFINED; |
| 1941 | p[15] = VA_BITS16_UNDEFINED; |
| 1942 | p[16] = VA_BITS16_UNDEFINED; |
| 1943 | p[17] = VA_BITS16_UNDEFINED; |
| 1944 | p[18] = VA_BITS16_UNDEFINED; |
| 1945 | p[19] = VA_BITS16_UNDEFINED; |
| 1946 | p[20] = VA_BITS16_UNDEFINED; |
| 1947 | p[21] = VA_BITS16_UNDEFINED; |
| 1948 | p[22] = VA_BITS16_UNDEFINED; |
| 1949 | p[23] = VA_BITS16_UNDEFINED; |
| 1950 | p[24] = VA_BITS16_UNDEFINED; |
| 1951 | p[25] = VA_BITS16_UNDEFINED; |
| 1952 | p[26] = VA_BITS16_UNDEFINED; |
| 1953 | p[27] = VA_BITS16_UNDEFINED; |
| 1954 | p[28] = VA_BITS16_UNDEFINED; |
| 1955 | p[29] = VA_BITS16_UNDEFINED; |
| 1956 | p[30] = VA_BITS16_UNDEFINED; |
| 1957 | p[31] = VA_BITS16_UNDEFINED; |
| 1958 | p[32] = VA_BITS16_UNDEFINED; |
| 1959 | p[33] = VA_BITS16_UNDEFINED; |
| 1960 | p[34] = VA_BITS16_UNDEFINED; |
| 1961 | p[35] = VA_BITS16_UNDEFINED; |
sewardj | 2e1a677 | 2006-01-18 04:16:27 +0000 | [diff] [blame] | 1962 | return; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1963 | } |
sewardj | 2e1a677 | 2006-01-18 04:16:27 +0000 | [diff] [blame] | 1964 | } |
| 1965 | } |
| 1966 | |
sewardj | 2a3a1a7 | 2005-05-12 23:25:43 +0000 | [diff] [blame] | 1967 | /* else fall into slow case */ |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1968 | MC_(make_mem_undefined)(base, len); |
sewardj | 826ec49 | 2005-05-12 18:05:00 +0000 | [diff] [blame] | 1969 | } |
| 1970 | |
| 1971 | |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 1972 | /*------------------------------------------------------------*/ |
| 1973 | /*--- Checking memory ---*/ |
| 1974 | /*------------------------------------------------------------*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1975 | |
sewardj | e4ccc01 | 2005-05-02 12:53:38 +0000 | [diff] [blame] | 1976 | typedef |
| 1977 | enum { |
| 1978 | MC_Ok = 5, |
| 1979 | MC_AddrErr = 6, |
| 1980 | MC_ValueErr = 7 |
| 1981 | } |
| 1982 | MC_ReadResult; |
| 1983 | |
| 1984 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1985 | /* Check permissions for address range. If inadequate permissions |
| 1986 | exist, *bad_addr is set to the offending address, so the caller can |
| 1987 | know what it is. */ |
| 1988 | |
sewardj | ecf8e10 | 2003-07-12 12:11:39 +0000 | [diff] [blame] | 1989 | /* Returns True if [a .. a+len) is not addressible. Otherwise, |
| 1990 | returns False, and if bad_addr is non-NULL, sets *bad_addr to |
| 1991 | indicate the lowest failing address. Functions below are |
| 1992 | similar. */ |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1993 | Bool MC_(check_mem_is_noaccess) ( Addr a, SizeT len, Addr* bad_addr ) |
sewardj | ecf8e10 | 2003-07-12 12:11:39 +0000 | [diff] [blame] | 1994 | { |
nethercote | 451eae9 | 2004-11-02 13:06:32 +0000 | [diff] [blame] | 1995 | SizeT i; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1996 | UWord vabits2; |
| 1997 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1998 | PROF_EVENT(60, "check_mem_is_noaccess"); |
sewardj | ecf8e10 | 2003-07-12 12:11:39 +0000 | [diff] [blame] | 1999 | for (i = 0; i < len; i++) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2000 | PROF_EVENT(61, "check_mem_is_noaccess(loop)"); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2001 | vabits2 = get_vabits2(a); |
| 2002 | if (VA_BITS2_NOACCESS != vabits2) { |
| 2003 | if (bad_addr != NULL) *bad_addr = a; |
sewardj | ecf8e10 | 2003-07-12 12:11:39 +0000 | [diff] [blame] | 2004 | return False; |
| 2005 | } |
| 2006 | a++; |
| 2007 | } |
| 2008 | return True; |
| 2009 | } |
| 2010 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2011 | static Bool is_mem_addressable ( Addr a, SizeT len, Addr* bad_addr ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2012 | { |
nethercote | 451eae9 | 2004-11-02 13:06:32 +0000 | [diff] [blame] | 2013 | SizeT i; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2014 | UWord vabits2; |
| 2015 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2016 | PROF_EVENT(62, "is_mem_addressable"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2017 | for (i = 0; i < len; i++) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2018 | PROF_EVENT(63, "is_mem_addressable(loop)"); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2019 | vabits2 = get_vabits2(a); |
| 2020 | if (VA_BITS2_NOACCESS == vabits2) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2021 | if (bad_addr != NULL) *bad_addr = a; |
| 2022 | return False; |
| 2023 | } |
| 2024 | a++; |
| 2025 | } |
| 2026 | return True; |
| 2027 | } |
| 2028 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2029 | static MC_ReadResult is_mem_defined ( Addr a, SizeT len, Addr* bad_addr ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2030 | { |
nethercote | 451eae9 | 2004-11-02 13:06:32 +0000 | [diff] [blame] | 2031 | SizeT i; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2032 | UWord vabits2; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2033 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2034 | PROF_EVENT(64, "is_mem_defined"); |
| 2035 | DEBUG("is_mem_defined\n"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2036 | for (i = 0; i < len; i++) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2037 | PROF_EVENT(65, "is_mem_defined(loop)"); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2038 | vabits2 = get_vabits2(a); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2039 | if (VA_BITS2_DEFINED != vabits2) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2040 | // Error! Nb: Report addressability errors in preference to |
| 2041 | // definedness errors. And don't report definedeness errors unless |
| 2042 | // --undef-value-errors=yes. |
| 2043 | if (bad_addr != NULL) *bad_addr = a; |
| 2044 | if ( VA_BITS2_NOACCESS == vabits2 ) return MC_AddrErr; |
| 2045 | else if ( MC_(clo_undef_value_errors) ) return MC_ValueErr; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2046 | } |
| 2047 | a++; |
| 2048 | } |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 2049 | return MC_Ok; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | |
| 2053 | /* Check a zero-terminated ascii string. Tricky -- don't want to |
| 2054 | examine the actual bytes, to find the end, until we're sure it is |
| 2055 | safe to do so. */ |
| 2056 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2057 | static Bool mc_is_defined_asciiz ( Addr a, Addr* bad_addr ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2058 | { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2059 | UWord vabits2; |
| 2060 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2061 | PROF_EVENT(66, "mc_is_defined_asciiz"); |
| 2062 | DEBUG("mc_is_defined_asciiz\n"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2063 | while (True) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2064 | PROF_EVENT(67, "mc_is_defined_asciiz(loop)"); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2065 | vabits2 = get_vabits2(a); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2066 | if (VA_BITS2_DEFINED != vabits2) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2067 | // Error! Nb: Report addressability errors in preference to |
| 2068 | // definedness errors. And don't report definedeness errors unless |
| 2069 | // --undef-value-errors=yes. |
| 2070 | if (bad_addr != NULL) *bad_addr = a; |
| 2071 | if ( VA_BITS2_NOACCESS == vabits2 ) return MC_AddrErr; |
| 2072 | else if ( MC_(clo_undef_value_errors) ) return MC_ValueErr; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2073 | } |
| 2074 | /* Ok, a is safe to read. */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2075 | if (* ((UChar*)a) == 0) { |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 2076 | return MC_Ok; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2077 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2078 | a++; |
| 2079 | } |
| 2080 | } |
| 2081 | |
| 2082 | |
| 2083 | /*------------------------------------------------------------*/ |
| 2084 | /*--- Memory event handlers ---*/ |
| 2085 | /*------------------------------------------------------------*/ |
| 2086 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2087 | static |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2088 | void check_mem_is_addressable ( CorePart part, ThreadId tid, Char* s, |
| 2089 | Addr base, SizeT size ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2090 | { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2091 | Addr bad_addr; |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2092 | Bool ok = is_mem_addressable ( base, size, &bad_addr ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2093 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2094 | if (!ok) { |
| 2095 | switch (part) { |
| 2096 | case Vg_CoreSysCall: |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2097 | mc_record_param_error ( tid, bad_addr, /*isReg*/False, |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 2098 | /*isUnaddr*/True, s ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2099 | break; |
| 2100 | |
| 2101 | case Vg_CorePThread: |
| 2102 | case Vg_CoreSignal: |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2103 | mc_record_core_mem_error( tid, /*isUnaddr*/True, s ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2104 | break; |
| 2105 | |
| 2106 | default: |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2107 | VG_(tool_panic)("check_mem_is_addressable: unexpected CorePart"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2108 | } |
| 2109 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2110 | } |
| 2111 | |
| 2112 | static |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2113 | void check_mem_is_defined ( CorePart part, ThreadId tid, Char* s, |
nethercote | 451eae9 | 2004-11-02 13:06:32 +0000 | [diff] [blame] | 2114 | Addr base, SizeT size ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2115 | { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2116 | Addr bad_addr; |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2117 | MC_ReadResult res = is_mem_defined ( base, size, &bad_addr ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2118 | |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 2119 | if (MC_Ok != res) { |
| 2120 | Bool isUnaddr = ( MC_AddrErr == res ? True : False ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2121 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2122 | switch (part) { |
| 2123 | case Vg_CoreSysCall: |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2124 | mc_record_param_error ( tid, bad_addr, /*isReg*/False, |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2125 | isUnaddr, s ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2126 | break; |
| 2127 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2128 | case Vg_CoreClientReq: // Kludge: make this a CoreMemErr |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2129 | case Vg_CorePThread: |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2130 | mc_record_core_mem_error( tid, isUnaddr, s ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2131 | break; |
| 2132 | |
| 2133 | /* If we're being asked to jump to a silly address, record an error |
| 2134 | message before potentially crashing the entire system. */ |
| 2135 | case Vg_CoreTranslate: |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2136 | mc_record_jump_error( tid, bad_addr ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2137 | break; |
| 2138 | |
| 2139 | default: |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2140 | VG_(tool_panic)("check_mem_is_defined: unexpected CorePart"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2141 | } |
| 2142 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2143 | } |
| 2144 | |
| 2145 | static |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2146 | void check_mem_is_defined_asciiz ( CorePart part, ThreadId tid, |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 2147 | Char* s, Addr str ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2148 | { |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 2149 | MC_ReadResult res; |
njn | 5ab96ac | 2005-05-08 02:59:50 +0000 | [diff] [blame] | 2150 | Addr bad_addr = 0; // shut GCC up |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2151 | |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 2152 | tl_assert(part == Vg_CoreSysCall); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2153 | res = mc_is_defined_asciiz ( (Addr)str, &bad_addr ); |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 2154 | if (MC_Ok != res) { |
| 2155 | Bool isUnaddr = ( MC_AddrErr == res ? True : False ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2156 | mc_record_param_error ( tid, bad_addr, /*isReg*/False, isUnaddr, s ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2157 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2158 | } |
| 2159 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2160 | static |
nethercote | 451eae9 | 2004-11-02 13:06:32 +0000 | [diff] [blame] | 2161 | void mc_new_mem_startup( Addr a, SizeT len, Bool rr, Bool ww, Bool xx ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2162 | { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2163 | /* Ignore the permissions, just make it defined. Seems to work... */ |
nethercote | 451eae9 | 2004-11-02 13:06:32 +0000 | [diff] [blame] | 2164 | DEBUG("mc_new_mem_startup(%p, %llu, rr=%u, ww=%u, xx=%u)\n", |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2165 | a, (ULong)len, rr, ww, xx); |
| 2166 | MC_(make_mem_defined)(a, len); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2167 | } |
| 2168 | |
| 2169 | static |
njn | b8dca86 | 2005-03-14 02:42:44 +0000 | [diff] [blame] | 2170 | void mc_new_mem_mmap ( Addr a, SizeT len, Bool rr, Bool ww, Bool xx ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2171 | { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2172 | MC_(make_mem_defined)(a, len); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2173 | } |
| 2174 | |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 2175 | static |
| 2176 | void mc_post_mem_write(CorePart part, ThreadId tid, Addr a, SizeT len) |
| 2177 | { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 2178 | MC_(make_mem_defined)(a, len); |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 2179 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2180 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 2181 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2182 | /*------------------------------------------------------------*/ |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2183 | /*--- Register event handlers ---*/ |
| 2184 | /*------------------------------------------------------------*/ |
| 2185 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 2186 | /* When some chunk of guest state is written, mark the corresponding |
| 2187 | shadow area as valid. This is used to initialise arbitrarily large |
sewardj | 62eae5f | 2006-01-17 01:58:24 +0000 | [diff] [blame] | 2188 | chunks of guest state, hence the _SIZE value, which has to be as |
| 2189 | big as the biggest guest state. |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 2190 | */ |
| 2191 | static void mc_post_reg_write ( CorePart part, ThreadId tid, |
| 2192 | OffT offset, SizeT size) |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2193 | { |
sewardj | d68ac3e | 2006-01-20 14:31:57 +0000 | [diff] [blame] | 2194 | # define MAX_REG_WRITE_SIZE 1392 |
cerion | 2108204 | 2005-12-06 19:07:08 +0000 | [diff] [blame] | 2195 | UChar area[MAX_REG_WRITE_SIZE]; |
| 2196 | tl_assert(size <= MAX_REG_WRITE_SIZE); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2197 | VG_(memset)(area, V_BITS8_DEFINED, size); |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 2198 | VG_(set_shadow_regs_area)( tid, offset, size, area ); |
cerion | 2108204 | 2005-12-06 19:07:08 +0000 | [diff] [blame] | 2199 | # undef MAX_REG_WRITE_SIZE |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2200 | } |
| 2201 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 2202 | static |
| 2203 | void mc_post_reg_write_clientcall ( ThreadId tid, |
| 2204 | OffT offset, SizeT size, |
| 2205 | Addr f) |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2206 | { |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 2207 | mc_post_reg_write(/*dummy*/0, tid, offset, size); |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2208 | } |
| 2209 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 2210 | /* Look at the definedness of the guest's shadow state for |
| 2211 | [offset, offset+len). If any part of that is undefined, record |
| 2212 | a parameter error. |
| 2213 | */ |
| 2214 | static void mc_pre_reg_read ( CorePart part, ThreadId tid, Char* s, |
| 2215 | OffT offset, SizeT size) |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 2216 | { |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 2217 | Int i; |
| 2218 | Bool bad; |
| 2219 | |
| 2220 | UChar area[16]; |
| 2221 | tl_assert(size <= 16); |
| 2222 | |
| 2223 | VG_(get_shadow_regs_area)( tid, offset, size, area ); |
| 2224 | |
| 2225 | bad = False; |
| 2226 | for (i = 0; i < size; i++) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2227 | if (area[i] != V_BITS8_DEFINED) { |
sewardj | 2c27f70 | 2005-05-03 18:19:05 +0000 | [diff] [blame] | 2228 | bad = True; |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 2229 | break; |
| 2230 | } |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 2231 | } |
| 2232 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 2233 | if (bad) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2234 | mc_record_param_error ( tid, 0, /*isReg*/True, /*isUnaddr*/False, s ); |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 2235 | } |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 2236 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2237 | |
sewardj | 6cf40ff | 2005-04-20 22:31:26 +0000 | [diff] [blame] | 2238 | /*------------------------------------------------------------*/ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2239 | /*--- Error and suppression types ---*/ |
| 2240 | /*------------------------------------------------------------*/ |
| 2241 | |
| 2242 | /* The classification of a faulting address. */ |
| 2243 | typedef |
| 2244 | enum { |
| 2245 | Undescribed, // as-yet unclassified |
| 2246 | Stack, |
| 2247 | Unknown, // classification yielded nothing useful |
| 2248 | Freed, Mallocd, |
| 2249 | UserG, // in a user-defined block |
| 2250 | Mempool, // in a mempool |
| 2251 | Register, // in a register; for Param errors only |
| 2252 | } |
| 2253 | AddrKind; |
| 2254 | |
| 2255 | /* Records info about a faulting address. */ |
| 2256 | typedef |
| 2257 | struct { // Used by: |
| 2258 | AddrKind akind; // ALL |
| 2259 | SizeT blksize; // Freed, Mallocd |
| 2260 | OffT rwoffset; // Freed, Mallocd |
| 2261 | ExeContext* lastchange; // Freed, Mallocd |
| 2262 | ThreadId stack_tid; // Stack |
| 2263 | const Char *desc; // UserG |
| 2264 | Bool maybe_gcc; // True if just below %esp -- could be a gcc bug. |
| 2265 | } |
| 2266 | AddrInfo; |
| 2267 | |
| 2268 | typedef |
| 2269 | enum { |
| 2270 | ParamSupp, // Bad syscall params |
| 2271 | CoreMemSupp, // Memory errors in core (pthread ops, signal handling) |
| 2272 | |
| 2273 | // Use of invalid values of given size (MemCheck only) |
| 2274 | Value0Supp, Value1Supp, Value2Supp, Value4Supp, Value8Supp, Value16Supp, |
| 2275 | |
| 2276 | // Invalid read/write attempt at given size |
| 2277 | Addr1Supp, Addr2Supp, Addr4Supp, Addr8Supp, Addr16Supp, |
| 2278 | |
| 2279 | FreeSupp, // Invalid or mismatching free |
| 2280 | OverlapSupp, // Overlapping blocks in memcpy(), strcpy(), etc |
| 2281 | LeakSupp, // Something to be suppressed in a leak check. |
| 2282 | MempoolSupp, // Memory pool suppression. |
| 2283 | } |
| 2284 | MC_SuppKind; |
| 2285 | |
| 2286 | /* What kind of error it is. */ |
| 2287 | typedef |
| 2288 | enum { ValueErr, |
| 2289 | CoreMemErr, // Error in core op (pthread, signals) or client req |
| 2290 | AddrErr, |
| 2291 | ParamErr, UserErr, /* behaves like an anonymous ParamErr */ |
| 2292 | FreeErr, FreeMismatchErr, |
| 2293 | OverlapErr, |
| 2294 | LeakErr, |
| 2295 | IllegalMempoolErr, |
| 2296 | } |
| 2297 | MC_ErrorKind; |
| 2298 | |
| 2299 | /* What kind of memory access is involved in the error? */ |
| 2300 | typedef |
| 2301 | enum { ReadAxs, WriteAxs, ExecAxs } |
| 2302 | AxsKind; |
| 2303 | |
| 2304 | /* Extra context for memory errors */ |
| 2305 | typedef |
| 2306 | struct { // Used by: |
| 2307 | AxsKind axskind; // AddrErr |
| 2308 | Int size; // AddrErr, ValueErr |
| 2309 | AddrInfo addrinfo; // {Addr,Free,FreeMismatch,Param,User}Err |
| 2310 | Bool isUnaddr; // {CoreMem,Param,User}Err |
| 2311 | } |
| 2312 | MC_Error; |
| 2313 | |
| 2314 | /*------------------------------------------------------------*/ |
njn | 9e63cb6 | 2005-05-08 18:34:59 +0000 | [diff] [blame] | 2315 | /*--- Printing errors ---*/ |
| 2316 | /*------------------------------------------------------------*/ |
| 2317 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2318 | static void mc_pp_AddrInfo ( Addr a, AddrInfo* ai ) |
| 2319 | { |
| 2320 | HChar* xpre = VG_(clo_xml) ? " <auxwhat>" : " "; |
| 2321 | HChar* xpost = VG_(clo_xml) ? "</auxwhat>" : ""; |
| 2322 | |
| 2323 | switch (ai->akind) { |
| 2324 | case Stack: |
| 2325 | VG_(message)(Vg_UserMsg, |
| 2326 | "%sAddress 0x%llx is on thread %d's stack%s", |
| 2327 | xpre, (ULong)a, ai->stack_tid, xpost); |
| 2328 | break; |
| 2329 | case Unknown: |
| 2330 | if (ai->maybe_gcc) { |
| 2331 | VG_(message)(Vg_UserMsg, |
| 2332 | "%sAddress 0x%llx is just below the stack ptr. " |
| 2333 | "To suppress, use: --workaround-gcc296-bugs=yes%s", |
| 2334 | xpre, (ULong)a, xpost |
| 2335 | ); |
| 2336 | } else { |
| 2337 | VG_(message)(Vg_UserMsg, |
| 2338 | "%sAddress 0x%llx " |
| 2339 | "is not stack'd, malloc'd or (recently) free'd%s", |
| 2340 | xpre, (ULong)a, xpost); |
| 2341 | } |
| 2342 | break; |
| 2343 | case Freed: case Mallocd: case UserG: case Mempool: { |
| 2344 | SizeT delta; |
| 2345 | const Char* relative; |
| 2346 | const Char* kind; |
| 2347 | if (ai->akind == Mempool) { |
| 2348 | kind = "mempool"; |
| 2349 | } else { |
| 2350 | kind = "block"; |
| 2351 | } |
| 2352 | if (ai->desc != NULL) |
| 2353 | kind = ai->desc; |
| 2354 | |
| 2355 | if (ai->rwoffset < 0) { |
| 2356 | delta = (SizeT)(- ai->rwoffset); |
| 2357 | relative = "before"; |
| 2358 | } else if (ai->rwoffset >= ai->blksize) { |
| 2359 | delta = ai->rwoffset - ai->blksize; |
| 2360 | relative = "after"; |
| 2361 | } else { |
| 2362 | delta = ai->rwoffset; |
| 2363 | relative = "inside"; |
| 2364 | } |
| 2365 | VG_(message)(Vg_UserMsg, |
| 2366 | "%sAddress 0x%lx is %,lu bytes %s a %s of size %,lu %s%s", |
| 2367 | xpre, |
| 2368 | a, delta, relative, kind, |
| 2369 | ai->blksize, |
| 2370 | ai->akind==Mallocd ? "alloc'd" |
| 2371 | : ai->akind==Freed ? "free'd" |
| 2372 | : "client-defined", |
| 2373 | xpost); |
| 2374 | VG_(pp_ExeContext)(ai->lastchange); |
| 2375 | break; |
| 2376 | } |
| 2377 | case Register: |
| 2378 | // print nothing |
| 2379 | tl_assert(0 == a); |
| 2380 | break; |
| 2381 | default: |
| 2382 | VG_(tool_panic)("mc_pp_AddrInfo"); |
| 2383 | } |
| 2384 | } |
| 2385 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 2386 | static void mc_pp_Error ( Error* err ) |
njn | 9e63cb6 | 2005-05-08 18:34:59 +0000 | [diff] [blame] | 2387 | { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2388 | MC_Error* err_extra = VG_(get_error_extra)(err); |
njn | 9e63cb6 | 2005-05-08 18:34:59 +0000 | [diff] [blame] | 2389 | |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 2390 | HChar* xpre = VG_(clo_xml) ? " <what>" : ""; |
| 2391 | HChar* xpost = VG_(clo_xml) ? "</what>" : ""; |
| 2392 | |
njn | 9e63cb6 | 2005-05-08 18:34:59 +0000 | [diff] [blame] | 2393 | switch (VG_(get_error_kind)(err)) { |
| 2394 | case CoreMemErr: { |
| 2395 | Char* s = ( err_extra->isUnaddr ? "unaddressable" : "uninitialised" ); |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 2396 | if (VG_(clo_xml)) |
| 2397 | VG_(message)(Vg_UserMsg, " <kind>CoreMemError</kind>"); |
| 2398 | /* What the hell *is* a CoreMemError? jrs 2005-May-18 */ |
| 2399 | VG_(message)(Vg_UserMsg, "%s%s contains %s byte(s)%s", |
| 2400 | xpre, VG_(get_error_string)(err), s, xpost); |
| 2401 | |
njn | 9e63cb6 | 2005-05-08 18:34:59 +0000 | [diff] [blame] | 2402 | VG_(pp_ExeContext)( VG_(get_error_where)(err) ); |
| 2403 | break; |
| 2404 | |
| 2405 | } |
| 2406 | |
| 2407 | case ValueErr: |
| 2408 | if (err_extra->size == 0) { |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 2409 | if (VG_(clo_xml)) |
| 2410 | VG_(message)(Vg_UserMsg, " <kind>UninitCondition</kind>"); |
| 2411 | VG_(message)(Vg_UserMsg, "%sConditional jump or move depends" |
| 2412 | " on uninitialised value(s)%s", |
| 2413 | xpre, xpost); |
njn | 9e63cb6 | 2005-05-08 18:34:59 +0000 | [diff] [blame] | 2414 | } else { |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 2415 | if (VG_(clo_xml)) |
| 2416 | VG_(message)(Vg_UserMsg, " <kind>UninitValue</kind>"); |
| 2417 | VG_(message)(Vg_UserMsg, |
| 2418 | "%sUse of uninitialised value of size %d%s", |
| 2419 | xpre, err_extra->size, xpost); |
njn | 9e63cb6 | 2005-05-08 18:34:59 +0000 | [diff] [blame] | 2420 | } |
| 2421 | VG_(pp_ExeContext)( VG_(get_error_where)(err) ); |
| 2422 | break; |
| 2423 | |
| 2424 | case ParamErr: { |
| 2425 | Bool isReg = ( Register == err_extra->addrinfo.akind ); |
| 2426 | Char* s1 = ( isReg ? "contains" : "points to" ); |
| 2427 | Char* s2 = ( err_extra->isUnaddr ? "unaddressable" : "uninitialised" ); |
| 2428 | if (isReg) tl_assert(!err_extra->isUnaddr); |
| 2429 | |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 2430 | if (VG_(clo_xml)) |
| 2431 | VG_(message)(Vg_UserMsg, " <kind>SyscallParam</kind>"); |
| 2432 | VG_(message)(Vg_UserMsg, "%sSyscall param %s %s %s byte(s)%s", |
| 2433 | xpre, VG_(get_error_string)(err), s1, s2, xpost); |
njn | 9e63cb6 | 2005-05-08 18:34:59 +0000 | [diff] [blame] | 2434 | |
| 2435 | VG_(pp_ExeContext)( VG_(get_error_where)(err) ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2436 | mc_pp_AddrInfo(VG_(get_error_address)(err), &err_extra->addrinfo); |
njn | 9e63cb6 | 2005-05-08 18:34:59 +0000 | [diff] [blame] | 2437 | break; |
| 2438 | } |
| 2439 | case UserErr: { |
| 2440 | Char* s = ( err_extra->isUnaddr ? "Unaddressable" : "Uninitialised" ); |
| 2441 | |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 2442 | if (VG_(clo_xml)) |
| 2443 | VG_(message)(Vg_UserMsg, " <kind>ClientCheck</kind>"); |
njn | 9e63cb6 | 2005-05-08 18:34:59 +0000 | [diff] [blame] | 2444 | VG_(message)(Vg_UserMsg, |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 2445 | "%s%s byte(s) found during client check request%s", |
| 2446 | xpre, s, xpost); |
njn | 9e63cb6 | 2005-05-08 18:34:59 +0000 | [diff] [blame] | 2447 | |
| 2448 | VG_(pp_ExeContext)( VG_(get_error_where)(err) ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2449 | mc_pp_AddrInfo(VG_(get_error_address)(err), &err_extra->addrinfo); |
njn | 9e63cb6 | 2005-05-08 18:34:59 +0000 | [diff] [blame] | 2450 | break; |
| 2451 | } |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2452 | case FreeErr: |
| 2453 | if (VG_(clo_xml)) |
| 2454 | VG_(message)(Vg_UserMsg, " <kind>InvalidFree</kind>"); |
| 2455 | VG_(message)(Vg_UserMsg, |
| 2456 | "%sInvalid free() / delete / delete[]%s", |
| 2457 | xpre, xpost); |
| 2458 | VG_(pp_ExeContext)( VG_(get_error_where)(err) ); |
| 2459 | mc_pp_AddrInfo(VG_(get_error_address)(err), &err_extra->addrinfo); |
njn | 9e63cb6 | 2005-05-08 18:34:59 +0000 | [diff] [blame] | 2460 | break; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2461 | |
| 2462 | case FreeMismatchErr: |
| 2463 | if (VG_(clo_xml)) |
| 2464 | VG_(message)(Vg_UserMsg, " <kind>MismatchedFree</kind>"); |
| 2465 | VG_(message)(Vg_UserMsg, |
| 2466 | "%sMismatched free() / delete / delete []%s", |
| 2467 | xpre, xpost); |
| 2468 | VG_(pp_ExeContext)( VG_(get_error_where)(err) ); |
| 2469 | mc_pp_AddrInfo(VG_(get_error_address)(err), &err_extra->addrinfo); |
| 2470 | break; |
| 2471 | |
| 2472 | case AddrErr: |
| 2473 | switch (err_extra->axskind) { |
| 2474 | case ReadAxs: |
| 2475 | if (VG_(clo_xml)) |
| 2476 | VG_(message)(Vg_UserMsg, " <kind>InvalidRead</kind>"); |
| 2477 | VG_(message)(Vg_UserMsg, |
| 2478 | "%sInvalid read of size %d%s", |
| 2479 | xpre, err_extra->size, xpost ); |
| 2480 | break; |
| 2481 | case WriteAxs: |
| 2482 | if (VG_(clo_xml)) |
| 2483 | VG_(message)(Vg_UserMsg, " <kind>InvalidWrite</kind>"); |
| 2484 | VG_(message)(Vg_UserMsg, |
| 2485 | "%sInvalid write of size %d%s", |
| 2486 | xpre, err_extra->size, xpost ); |
| 2487 | break; |
| 2488 | case ExecAxs: |
| 2489 | if (VG_(clo_xml)) |
| 2490 | VG_(message)(Vg_UserMsg, " <kind>InvalidJump</kind>"); |
| 2491 | VG_(message)(Vg_UserMsg, |
| 2492 | "%sJump to the invalid address " |
| 2493 | "stated on the next line%s", |
| 2494 | xpre, xpost); |
| 2495 | break; |
| 2496 | default: |
| 2497 | VG_(tool_panic)("mc_pp_Error(axskind)"); |
| 2498 | } |
| 2499 | VG_(pp_ExeContext)( VG_(get_error_where)(err) ); |
| 2500 | mc_pp_AddrInfo(VG_(get_error_address)(err), &err_extra->addrinfo); |
| 2501 | break; |
| 2502 | |
| 2503 | case OverlapErr: { |
| 2504 | OverlapExtra* ov_extra = (OverlapExtra*)VG_(get_error_extra)(err); |
| 2505 | if (VG_(clo_xml)) |
| 2506 | VG_(message)(Vg_UserMsg, " <kind>Overlap</kind>"); |
| 2507 | if (ov_extra->len == -1) |
| 2508 | VG_(message)(Vg_UserMsg, |
| 2509 | "%sSource and destination overlap in %s(%p, %p)%s", |
| 2510 | xpre, |
| 2511 | VG_(get_error_string)(err), |
| 2512 | ov_extra->dst, ov_extra->src, |
| 2513 | xpost); |
| 2514 | else |
| 2515 | VG_(message)(Vg_UserMsg, |
| 2516 | "%sSource and destination overlap in %s(%p, %p, %d)%s", |
| 2517 | xpre, |
| 2518 | VG_(get_error_string)(err), |
| 2519 | ov_extra->dst, ov_extra->src, ov_extra->len, |
| 2520 | xpost); |
| 2521 | VG_(pp_ExeContext)( VG_(get_error_where)(err) ); |
| 2522 | break; |
| 2523 | } |
| 2524 | case LeakErr: { |
| 2525 | MC_(pp_LeakError)(err_extra); |
| 2526 | break; |
| 2527 | } |
| 2528 | |
| 2529 | case IllegalMempoolErr: |
| 2530 | if (VG_(clo_xml)) |
| 2531 | VG_(message)(Vg_UserMsg, " <kind>InvalidMemPool</kind>"); |
| 2532 | VG_(message)(Vg_UserMsg, "%sIllegal memory pool address%s", |
| 2533 | xpre, xpost); |
| 2534 | VG_(pp_ExeContext)( VG_(get_error_where)(err) ); |
| 2535 | mc_pp_AddrInfo(VG_(get_error_address)(err), &err_extra->addrinfo); |
| 2536 | break; |
| 2537 | |
| 2538 | default: |
| 2539 | VG_(printf)("Error:\n unknown Memcheck error code %d\n", |
| 2540 | VG_(get_error_kind)(err)); |
| 2541 | VG_(tool_panic)("unknown error code in mc_pp_Error)"); |
njn | 9e63cb6 | 2005-05-08 18:34:59 +0000 | [diff] [blame] | 2542 | } |
| 2543 | } |
| 2544 | |
| 2545 | /*------------------------------------------------------------*/ |
| 2546 | /*--- Recording errors ---*/ |
| 2547 | /*------------------------------------------------------------*/ |
| 2548 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2549 | /* These many bytes below %ESP are considered addressible if we're |
| 2550 | doing the --workaround-gcc296-bugs hack. */ |
| 2551 | #define VG_GCC296_BUG_STACK_SLOP 1024 |
| 2552 | |
| 2553 | /* Is this address within some small distance below %ESP? Used only |
| 2554 | for the --workaround-gcc296-bugs kludge. */ |
| 2555 | static Bool is_just_below_ESP( Addr esp, Addr aa ) |
| 2556 | { |
| 2557 | if (esp > aa && (esp - aa) <= VG_GCC296_BUG_STACK_SLOP) |
| 2558 | return True; |
| 2559 | else |
| 2560 | return False; |
| 2561 | } |
| 2562 | |
| 2563 | static void mc_clear_MC_Error ( MC_Error* err_extra ) |
| 2564 | { |
| 2565 | err_extra->axskind = ReadAxs; |
| 2566 | err_extra->size = 0; |
| 2567 | err_extra->isUnaddr = True; |
| 2568 | err_extra->addrinfo.akind = Unknown; |
| 2569 | err_extra->addrinfo.blksize = 0; |
| 2570 | err_extra->addrinfo.rwoffset = 0; |
| 2571 | err_extra->addrinfo.lastchange = NULL; |
| 2572 | err_extra->addrinfo.stack_tid = VG_INVALID_THREADID; |
| 2573 | err_extra->addrinfo.maybe_gcc = False; |
| 2574 | err_extra->addrinfo.desc = NULL; |
| 2575 | } |
| 2576 | |
| 2577 | /* This one called from generated code and non-generated code. */ |
| 2578 | static void mc_record_address_error ( ThreadId tid, Addr a, Int size, |
| 2579 | Bool isWrite ) |
| 2580 | { |
| 2581 | MC_Error err_extra; |
| 2582 | Bool just_below_esp; |
| 2583 | |
| 2584 | just_below_esp = is_just_below_ESP( VG_(get_SP)(tid), a ); |
| 2585 | |
| 2586 | /* If this is caused by an access immediately below %ESP, and the |
| 2587 | user asks nicely, we just ignore it. */ |
| 2588 | if (MC_(clo_workaround_gcc296_bugs) && just_below_esp) |
| 2589 | return; |
| 2590 | |
| 2591 | mc_clear_MC_Error( &err_extra ); |
| 2592 | err_extra.axskind = isWrite ? WriteAxs : ReadAxs; |
| 2593 | err_extra.size = size; |
| 2594 | err_extra.addrinfo.akind = Undescribed; |
| 2595 | err_extra.addrinfo.maybe_gcc = just_below_esp; |
| 2596 | VG_(maybe_record_error)( tid, AddrErr, a, /*s*/NULL, &err_extra ); |
| 2597 | } |
| 2598 | |
| 2599 | /* These ones are called from non-generated code */ |
| 2600 | |
| 2601 | /* This is for memory errors in pthread functions, as opposed to pthread API |
| 2602 | errors which are found by the core. */ |
| 2603 | static void mc_record_core_mem_error ( ThreadId tid, Bool isUnaddr, Char* msg ) |
| 2604 | { |
| 2605 | MC_Error err_extra; |
| 2606 | |
| 2607 | mc_clear_MC_Error( &err_extra ); |
| 2608 | err_extra.isUnaddr = isUnaddr; |
| 2609 | VG_(maybe_record_error)( tid, CoreMemErr, /*addr*/0, msg, &err_extra ); |
| 2610 | } |
| 2611 | |
| 2612 | // Three kinds of param errors: |
| 2613 | // - register arg contains undefined bytes |
| 2614 | // - memory arg is unaddressable |
| 2615 | // - memory arg contains undefined bytes |
| 2616 | // 'isReg' and 'isUnaddr' dictate which of these it is. |
| 2617 | static void mc_record_param_error ( ThreadId tid, Addr a, Bool isReg, |
| 2618 | Bool isUnaddr, Char* msg ) |
| 2619 | { |
| 2620 | MC_Error err_extra; |
| 2621 | |
sewardj | 1cf56cf | 2006-05-22 13:59:42 +0000 | [diff] [blame] | 2622 | if (!isUnaddr) tl_assert(MC_(clo_undef_value_errors)); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2623 | tl_assert(VG_INVALID_THREADID != tid); |
| 2624 | if (isUnaddr) tl_assert(!isReg); // unaddressable register is impossible |
| 2625 | mc_clear_MC_Error( &err_extra ); |
| 2626 | err_extra.addrinfo.akind = ( isReg ? Register : Undescribed ); |
| 2627 | err_extra.isUnaddr = isUnaddr; |
| 2628 | VG_(maybe_record_error)( tid, ParamErr, a, msg, &err_extra ); |
| 2629 | } |
| 2630 | |
| 2631 | static void mc_record_jump_error ( ThreadId tid, Addr a ) |
| 2632 | { |
| 2633 | MC_Error err_extra; |
| 2634 | |
| 2635 | tl_assert(VG_INVALID_THREADID != tid); |
| 2636 | mc_clear_MC_Error( &err_extra ); |
| 2637 | err_extra.axskind = ExecAxs; |
| 2638 | err_extra.size = 1; // size only used for suppressions |
| 2639 | err_extra.addrinfo.akind = Undescribed; |
| 2640 | VG_(maybe_record_error)( tid, AddrErr, a, /*s*/NULL, &err_extra ); |
| 2641 | } |
| 2642 | |
| 2643 | void MC_(record_free_error) ( ThreadId tid, Addr a ) |
| 2644 | { |
| 2645 | MC_Error err_extra; |
| 2646 | |
| 2647 | tl_assert(VG_INVALID_THREADID != tid); |
| 2648 | mc_clear_MC_Error( &err_extra ); |
| 2649 | err_extra.addrinfo.akind = Undescribed; |
| 2650 | VG_(maybe_record_error)( tid, FreeErr, a, /*s*/NULL, &err_extra ); |
| 2651 | } |
| 2652 | |
| 2653 | void MC_(record_illegal_mempool_error) ( ThreadId tid, Addr a ) |
| 2654 | { |
| 2655 | MC_Error err_extra; |
| 2656 | |
| 2657 | tl_assert(VG_INVALID_THREADID != tid); |
| 2658 | mc_clear_MC_Error( &err_extra ); |
| 2659 | err_extra.addrinfo.akind = Undescribed; |
| 2660 | VG_(maybe_record_error)( tid, IllegalMempoolErr, a, /*s*/NULL, &err_extra ); |
| 2661 | } |
| 2662 | |
| 2663 | void MC_(record_freemismatch_error) ( ThreadId tid, Addr a, MC_Chunk* mc ) |
| 2664 | { |
| 2665 | MC_Error err_extra; |
| 2666 | AddrInfo* ai; |
| 2667 | |
| 2668 | tl_assert(VG_INVALID_THREADID != tid); |
| 2669 | mc_clear_MC_Error( &err_extra ); |
| 2670 | ai = &err_extra.addrinfo; |
| 2671 | ai->akind = Mallocd; // Nb: not 'Freed' |
| 2672 | ai->blksize = mc->size; |
| 2673 | ai->rwoffset = (Int)a - (Int)mc->data; |
| 2674 | ai->lastchange = mc->where; |
| 2675 | VG_(maybe_record_error)( tid, FreeMismatchErr, a, /*s*/NULL, &err_extra ); |
| 2676 | } |
| 2677 | |
| 2678 | static void mc_record_overlap_error ( ThreadId tid, |
| 2679 | Char* function, OverlapExtra* ov_extra ) |
| 2680 | { |
| 2681 | VG_(maybe_record_error)( |
| 2682 | tid, OverlapErr, /*addr*/0, /*s*/function, ov_extra ); |
| 2683 | } |
| 2684 | |
| 2685 | Bool MC_(record_leak_error) ( ThreadId tid, /*LeakExtra*/void* leak_extra, |
| 2686 | ExeContext* where, Bool print_record ) |
| 2687 | { |
| 2688 | return |
| 2689 | VG_(unique_error) ( tid, LeakErr, /*Addr*/0, /*s*/NULL, |
| 2690 | /*extra*/leak_extra, where, print_record, |
| 2691 | /*allow_GDB_attach*/False, /*count_error*/False ); |
| 2692 | } |
| 2693 | |
| 2694 | |
njn | 02bc4b8 | 2005-05-15 17:28:26 +0000 | [diff] [blame] | 2695 | /* Creates a copy of the 'extra' part, updates the copy with address info if |
njn | 9e63cb6 | 2005-05-08 18:34:59 +0000 | [diff] [blame] | 2696 | necessary, and returns the copy. */ |
| 2697 | /* This one called from generated code and non-generated code. */ |
njn | 9636482 | 2005-05-08 19:04:53 +0000 | [diff] [blame] | 2698 | static void mc_record_value_error ( ThreadId tid, Int size ) |
njn | 9e63cb6 | 2005-05-08 18:34:59 +0000 | [diff] [blame] | 2699 | { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2700 | MC_Error err_extra; |
njn | 9e63cb6 | 2005-05-08 18:34:59 +0000 | [diff] [blame] | 2701 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2702 | tl_assert(MC_(clo_undef_value_errors)); |
| 2703 | mc_clear_MC_Error( &err_extra ); |
njn | 9e63cb6 | 2005-05-08 18:34:59 +0000 | [diff] [blame] | 2704 | err_extra.size = size; |
| 2705 | err_extra.isUnaddr = False; |
| 2706 | VG_(maybe_record_error)( tid, ValueErr, /*addr*/0, /*s*/NULL, &err_extra ); |
| 2707 | } |
| 2708 | |
| 2709 | /* This called from non-generated code */ |
| 2710 | |
njn | 9636482 | 2005-05-08 19:04:53 +0000 | [diff] [blame] | 2711 | static void mc_record_user_error ( ThreadId tid, Addr a, Bool isWrite, |
| 2712 | Bool isUnaddr ) |
njn | 9e63cb6 | 2005-05-08 18:34:59 +0000 | [diff] [blame] | 2713 | { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2714 | MC_Error err_extra; |
njn | 9e63cb6 | 2005-05-08 18:34:59 +0000 | [diff] [blame] | 2715 | |
| 2716 | tl_assert(VG_INVALID_THREADID != tid); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2717 | mc_clear_MC_Error( &err_extra ); |
njn | 9e63cb6 | 2005-05-08 18:34:59 +0000 | [diff] [blame] | 2718 | err_extra.addrinfo.akind = Undescribed; |
| 2719 | err_extra.isUnaddr = isUnaddr; |
| 2720 | VG_(maybe_record_error)( tid, UserErr, a, /*s*/NULL, &err_extra ); |
| 2721 | } |
| 2722 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2723 | __attribute__ ((unused)) |
| 2724 | static Bool eq_AddrInfo ( VgRes res, AddrInfo* ai1, AddrInfo* ai2 ) |
| 2725 | { |
| 2726 | if (ai1->akind != Undescribed |
| 2727 | && ai2->akind != Undescribed |
| 2728 | && ai1->akind != ai2->akind) |
| 2729 | return False; |
| 2730 | if (ai1->akind == Freed || ai1->akind == Mallocd) { |
| 2731 | if (ai1->blksize != ai2->blksize) |
| 2732 | return False; |
| 2733 | if (!VG_(eq_ExeContext)(res, ai1->lastchange, ai2->lastchange)) |
| 2734 | return False; |
| 2735 | } |
| 2736 | return True; |
| 2737 | } |
| 2738 | |
| 2739 | /* Compare error contexts, to detect duplicates. Note that if they |
| 2740 | are otherwise the same, the faulting addrs and associated rwoffsets |
| 2741 | are allowed to be different. */ |
| 2742 | static Bool mc_eq_Error ( VgRes res, Error* e1, Error* e2 ) |
| 2743 | { |
| 2744 | MC_Error* e1_extra = VG_(get_error_extra)(e1); |
| 2745 | MC_Error* e2_extra = VG_(get_error_extra)(e2); |
| 2746 | |
| 2747 | /* Guaranteed by calling function */ |
| 2748 | tl_assert(VG_(get_error_kind)(e1) == VG_(get_error_kind)(e2)); |
| 2749 | |
| 2750 | switch (VG_(get_error_kind)(e1)) { |
| 2751 | case CoreMemErr: { |
| 2752 | Char *e1s, *e2s; |
| 2753 | if (e1_extra->isUnaddr != e2_extra->isUnaddr) return False; |
| 2754 | e1s = VG_(get_error_string)(e1); |
| 2755 | e2s = VG_(get_error_string)(e2); |
| 2756 | if (e1s == e2s) return True; |
| 2757 | if (0 == VG_(strcmp)(e1s, e2s)) return True; |
| 2758 | return False; |
| 2759 | } |
| 2760 | |
| 2761 | // Perhaps we should also check the addrinfo.akinds for equality. |
| 2762 | // That would result in more error reports, but only in cases where |
| 2763 | // a register contains uninitialised bytes and points to memory |
| 2764 | // containing uninitialised bytes. Currently, the 2nd of those to be |
| 2765 | // detected won't be reported. That is (nearly?) always the memory |
| 2766 | // error, which is good. |
| 2767 | case ParamErr: |
| 2768 | if (0 != VG_(strcmp)(VG_(get_error_string)(e1), |
| 2769 | VG_(get_error_string)(e2))) return False; |
| 2770 | // fall through |
| 2771 | case UserErr: |
| 2772 | if (e1_extra->isUnaddr != e2_extra->isUnaddr) return False; |
| 2773 | return True; |
| 2774 | |
| 2775 | case FreeErr: |
| 2776 | case FreeMismatchErr: |
| 2777 | /* JRS 2002-Aug-26: comparing addrs seems overkill and can |
| 2778 | cause excessive duplication of errors. Not even AddrErr |
| 2779 | below does that. So don't compare either the .addr field |
| 2780 | or the .addrinfo fields. */ |
| 2781 | /* if (e1->addr != e2->addr) return False; */ |
| 2782 | /* if (!eq_AddrInfo(res, &e1_extra->addrinfo, &e2_extra->addrinfo)) |
| 2783 | return False; |
| 2784 | */ |
| 2785 | return True; |
| 2786 | |
| 2787 | case AddrErr: |
| 2788 | /* if (e1_extra->axskind != e2_extra->axskind) return False; */ |
| 2789 | if (e1_extra->size != e2_extra->size) return False; |
| 2790 | /* |
| 2791 | if (!eq_AddrInfo(res, &e1_extra->addrinfo, &e2_extra->addrinfo)) |
| 2792 | return False; |
| 2793 | */ |
| 2794 | return True; |
| 2795 | |
| 2796 | case ValueErr: |
| 2797 | if (e1_extra->size != e2_extra->size) return False; |
| 2798 | return True; |
| 2799 | |
| 2800 | case OverlapErr: |
| 2801 | return True; |
| 2802 | |
| 2803 | case LeakErr: |
| 2804 | VG_(tool_panic)("Shouldn't get LeakErr in mc_eq_Error,\n" |
| 2805 | "since it's handled with VG_(unique_error)()!"); |
| 2806 | |
| 2807 | case IllegalMempoolErr: |
| 2808 | return True; |
| 2809 | |
| 2810 | default: |
| 2811 | VG_(printf)("Error:\n unknown error code %d\n", |
| 2812 | VG_(get_error_kind)(e1)); |
| 2813 | VG_(tool_panic)("unknown error code in mc_eq_Error"); |
| 2814 | } |
| 2815 | } |
| 2816 | |
| 2817 | /* Function used when searching MC_Chunk lists */ |
| 2818 | static Bool addr_is_in_MC_Chunk(MC_Chunk* mc, Addr a) |
| 2819 | { |
| 2820 | // Nb: this is not quite right! It assumes that the heap block has |
| 2821 | // a redzone of size MC_MALLOC_REDZONE_SZB. That's true for malloc'd |
| 2822 | // blocks, but not necessarily true for custom-alloc'd blocks. So |
| 2823 | // in some cases this could result in an incorrect description (eg. |
| 2824 | // saying "12 bytes after block A" when really it's within block B. |
| 2825 | // Fixing would require adding redzone size to MC_Chunks, though. |
| 2826 | return VG_(addr_is_in_block)( a, mc->data, mc->size, |
| 2827 | MC_MALLOC_REDZONE_SZB ); |
| 2828 | } |
| 2829 | |
| 2830 | // Forward declaration |
| 2831 | static Bool client_perm_maybe_describe( Addr a, AddrInfo* ai ); |
| 2832 | |
| 2833 | /* Describe an address as best you can, for error messages, |
| 2834 | putting the result in ai. */ |
| 2835 | static void describe_addr ( Addr a, AddrInfo* ai ) |
| 2836 | { |
| 2837 | MC_Chunk* mc; |
| 2838 | ThreadId tid; |
| 2839 | Addr stack_min, stack_max; |
| 2840 | |
| 2841 | /* Perhaps it's a user-def'd block? */ |
| 2842 | if (client_perm_maybe_describe( a, ai )) |
| 2843 | return; |
| 2844 | |
| 2845 | /* Perhaps it's on a thread's stack? */ |
| 2846 | VG_(thread_stack_reset_iter)(); |
| 2847 | while ( VG_(thread_stack_next)(&tid, &stack_min, &stack_max) ) { |
| 2848 | if (stack_min <= a && a <= stack_max) { |
| 2849 | ai->akind = Stack; |
| 2850 | ai->stack_tid = tid; |
| 2851 | return; |
| 2852 | } |
| 2853 | } |
| 2854 | /* Search for a recently freed block which might bracket it. */ |
| 2855 | mc = MC_(get_freed_list_head)(); |
| 2856 | while (mc) { |
| 2857 | if (addr_is_in_MC_Chunk(mc, a)) { |
| 2858 | ai->akind = Freed; |
| 2859 | ai->blksize = mc->size; |
| 2860 | ai->rwoffset = (Int)a - (Int)mc->data; |
| 2861 | ai->lastchange = mc->where; |
| 2862 | return; |
| 2863 | } |
| 2864 | mc = mc->next; |
| 2865 | } |
| 2866 | /* Search for a currently malloc'd block which might bracket it. */ |
| 2867 | VG_(HT_ResetIter)(MC_(malloc_list)); |
| 2868 | while ( (mc = VG_(HT_Next)(MC_(malloc_list))) ) { |
| 2869 | if (addr_is_in_MC_Chunk(mc, a)) { |
| 2870 | ai->akind = Mallocd; |
| 2871 | ai->blksize = mc->size; |
| 2872 | ai->rwoffset = (Int)(a) - (Int)mc->data; |
| 2873 | ai->lastchange = mc->where; |
| 2874 | return; |
| 2875 | } |
| 2876 | } |
| 2877 | /* Clueless ... */ |
| 2878 | ai->akind = Unknown; |
| 2879 | return; |
| 2880 | } |
| 2881 | |
| 2882 | /* Updates the copy with address info if necessary (but not for all errors). */ |
| 2883 | static UInt mc_update_extra( Error* err ) |
| 2884 | { |
| 2885 | switch (VG_(get_error_kind)(err)) { |
| 2886 | // These two don't have addresses associated with them, and so don't |
| 2887 | // need any updating. |
| 2888 | case CoreMemErr: |
| 2889 | case ValueErr: { |
| 2890 | MC_Error* extra = VG_(get_error_extra)(err); |
| 2891 | tl_assert(Unknown == extra->addrinfo.akind); |
| 2892 | return sizeof(MC_Error); |
| 2893 | } |
| 2894 | |
| 2895 | // ParamErrs sometimes involve a memory address; call describe_addr() in |
| 2896 | // this case. |
| 2897 | case ParamErr: { |
| 2898 | MC_Error* extra = VG_(get_error_extra)(err); |
| 2899 | tl_assert(Undescribed == extra->addrinfo.akind || |
| 2900 | Register == extra->addrinfo.akind); |
| 2901 | if (Undescribed == extra->addrinfo.akind) |
| 2902 | describe_addr ( VG_(get_error_address)(err), &(extra->addrinfo) ); |
| 2903 | return sizeof(MC_Error); |
| 2904 | } |
| 2905 | |
| 2906 | // These four always involve a memory address. |
| 2907 | case AddrErr: |
| 2908 | case UserErr: |
| 2909 | case FreeErr: |
| 2910 | case IllegalMempoolErr: { |
| 2911 | MC_Error* extra = VG_(get_error_extra)(err); |
| 2912 | tl_assert(Undescribed == extra->addrinfo.akind); |
| 2913 | describe_addr ( VG_(get_error_address)(err), &(extra->addrinfo) ); |
| 2914 | return sizeof(MC_Error); |
| 2915 | } |
| 2916 | |
| 2917 | // FreeMismatchErrs have already had their address described; this is |
| 2918 | // possible because we have the MC_Chunk on hand when the error is |
| 2919 | // detected. However, the address may be part of a user block, and if so |
| 2920 | // we override the pre-determined description with a user block one. |
| 2921 | case FreeMismatchErr: { |
| 2922 | MC_Error* extra = VG_(get_error_extra)(err); |
| 2923 | tl_assert(extra && Mallocd == extra->addrinfo.akind); |
| 2924 | (void)client_perm_maybe_describe( VG_(get_error_address)(err), |
| 2925 | &(extra->addrinfo) ); |
| 2926 | return sizeof(MC_Error); |
| 2927 | } |
| 2928 | |
| 2929 | // No memory address involved with these ones. Nb: for LeakErrs the |
| 2930 | // returned size does not matter -- LeakErrs are always shown with |
| 2931 | // VG_(unique_error)() so they're not copied. |
| 2932 | case LeakErr: return 0; |
| 2933 | case OverlapErr: return sizeof(OverlapExtra); |
| 2934 | |
| 2935 | default: VG_(tool_panic)("mc_update_extra: bad errkind"); |
| 2936 | } |
| 2937 | } |
| 2938 | |
njn | 9e63cb6 | 2005-05-08 18:34:59 +0000 | [diff] [blame] | 2939 | /*------------------------------------------------------------*/ |
| 2940 | /*--- Suppressions ---*/ |
| 2941 | /*------------------------------------------------------------*/ |
| 2942 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 2943 | static Bool mc_recognised_suppression ( Char* name, Supp* su ) |
njn | 9e63cb6 | 2005-05-08 18:34:59 +0000 | [diff] [blame] | 2944 | { |
| 2945 | SuppKind skind; |
| 2946 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2947 | if (VG_STREQ(name, "Param")) skind = ParamSupp; |
| 2948 | else if (VG_STREQ(name, "CoreMem")) skind = CoreMemSupp; |
| 2949 | else if (VG_STREQ(name, "Addr1")) skind = Addr1Supp; |
| 2950 | else if (VG_STREQ(name, "Addr2")) skind = Addr2Supp; |
| 2951 | else if (VG_STREQ(name, "Addr4")) skind = Addr4Supp; |
| 2952 | else if (VG_STREQ(name, "Addr8")) skind = Addr8Supp; |
| 2953 | else if (VG_STREQ(name, "Addr16")) skind = Addr16Supp; |
| 2954 | else if (VG_STREQ(name, "Free")) skind = FreeSupp; |
| 2955 | else if (VG_STREQ(name, "Leak")) skind = LeakSupp; |
| 2956 | else if (VG_STREQ(name, "Overlap")) skind = OverlapSupp; |
| 2957 | else if (VG_STREQ(name, "Mempool")) skind = MempoolSupp; |
njn | 9e63cb6 | 2005-05-08 18:34:59 +0000 | [diff] [blame] | 2958 | else if (VG_STREQ(name, "Cond")) skind = Value0Supp; |
| 2959 | else if (VG_STREQ(name, "Value0")) skind = Value0Supp;/* backwards compat */ |
| 2960 | else if (VG_STREQ(name, "Value1")) skind = Value1Supp; |
| 2961 | else if (VG_STREQ(name, "Value2")) skind = Value2Supp; |
| 2962 | else if (VG_STREQ(name, "Value4")) skind = Value4Supp; |
| 2963 | else if (VG_STREQ(name, "Value8")) skind = Value8Supp; |
| 2964 | else if (VG_STREQ(name, "Value16")) skind = Value16Supp; |
| 2965 | else |
| 2966 | return False; |
| 2967 | |
| 2968 | VG_(set_supp_kind)(su, skind); |
| 2969 | return True; |
| 2970 | } |
| 2971 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 2972 | static |
| 2973 | Bool mc_read_extra_suppression_info ( Int fd, Char* buf, Int nBuf, Supp *su ) |
| 2974 | { |
| 2975 | Bool eof; |
| 2976 | |
| 2977 | if (VG_(get_supp_kind)(su) == ParamSupp) { |
| 2978 | eof = VG_(get_line) ( fd, buf, nBuf ); |
| 2979 | if (eof) return False; |
| 2980 | VG_(set_supp_string)(su, VG_(strdup)(buf)); |
| 2981 | } |
| 2982 | return True; |
| 2983 | } |
| 2984 | |
| 2985 | static Bool mc_error_matches_suppression(Error* err, Supp* su) |
| 2986 | { |
| 2987 | Int su_size; |
| 2988 | MC_Error* err_extra = VG_(get_error_extra)(err); |
| 2989 | ErrorKind ekind = VG_(get_error_kind )(err); |
| 2990 | |
| 2991 | switch (VG_(get_supp_kind)(su)) { |
| 2992 | case ParamSupp: |
| 2993 | return (ekind == ParamErr |
| 2994 | && VG_STREQ(VG_(get_error_string)(err), |
| 2995 | VG_(get_supp_string)(su))); |
| 2996 | |
| 2997 | case CoreMemSupp: |
| 2998 | return (ekind == CoreMemErr |
| 2999 | && VG_STREQ(VG_(get_error_string)(err), |
| 3000 | VG_(get_supp_string)(su))); |
| 3001 | |
| 3002 | case Value0Supp: su_size = 0; goto value_case; |
| 3003 | case Value1Supp: su_size = 1; goto value_case; |
| 3004 | case Value2Supp: su_size = 2; goto value_case; |
| 3005 | case Value4Supp: su_size = 4; goto value_case; |
| 3006 | case Value8Supp: su_size = 8; goto value_case; |
| 3007 | case Value16Supp:su_size =16; goto value_case; |
| 3008 | value_case: |
| 3009 | return (ekind == ValueErr && err_extra->size == su_size); |
| 3010 | |
| 3011 | case Addr1Supp: su_size = 1; goto addr_case; |
| 3012 | case Addr2Supp: su_size = 2; goto addr_case; |
| 3013 | case Addr4Supp: su_size = 4; goto addr_case; |
| 3014 | case Addr8Supp: su_size = 8; goto addr_case; |
| 3015 | case Addr16Supp:su_size =16; goto addr_case; |
| 3016 | addr_case: |
| 3017 | return (ekind == AddrErr && err_extra->size == su_size); |
| 3018 | |
| 3019 | case FreeSupp: |
| 3020 | return (ekind == FreeErr || ekind == FreeMismatchErr); |
| 3021 | |
| 3022 | case OverlapSupp: |
| 3023 | return (ekind = OverlapErr); |
| 3024 | |
| 3025 | case LeakSupp: |
| 3026 | return (ekind == LeakErr); |
| 3027 | |
| 3028 | case MempoolSupp: |
| 3029 | return (ekind == IllegalMempoolErr); |
| 3030 | |
| 3031 | default: |
| 3032 | VG_(printf)("Error:\n" |
| 3033 | " unknown suppression type %d\n", |
| 3034 | VG_(get_supp_kind)(su)); |
| 3035 | VG_(tool_panic)("unknown suppression type in " |
| 3036 | "MC_(error_matches_suppression)"); |
| 3037 | } |
| 3038 | } |
| 3039 | |
| 3040 | static Char* mc_get_error_name ( Error* err ) |
| 3041 | { |
| 3042 | Char* s; |
| 3043 | switch (VG_(get_error_kind)(err)) { |
| 3044 | case ParamErr: return "Param"; |
| 3045 | case UserErr: return NULL; /* Can't suppress User errors */ |
| 3046 | case FreeMismatchErr: return "Free"; |
| 3047 | case IllegalMempoolErr: return "Mempool"; |
| 3048 | case FreeErr: return "Free"; |
| 3049 | case AddrErr: |
| 3050 | switch ( ((MC_Error*)VG_(get_error_extra)(err))->size ) { |
| 3051 | case 1: return "Addr1"; |
| 3052 | case 2: return "Addr2"; |
| 3053 | case 4: return "Addr4"; |
| 3054 | case 8: return "Addr8"; |
| 3055 | case 16: return "Addr16"; |
| 3056 | default: VG_(tool_panic)("unexpected size for Addr"); |
| 3057 | } |
| 3058 | |
| 3059 | case ValueErr: |
| 3060 | switch ( ((MC_Error*)VG_(get_error_extra)(err))->size ) { |
| 3061 | case 0: return "Cond"; |
| 3062 | case 1: return "Value1"; |
| 3063 | case 2: return "Value2"; |
| 3064 | case 4: return "Value4"; |
| 3065 | case 8: return "Value8"; |
| 3066 | case 16: return "Value16"; |
| 3067 | default: VG_(tool_panic)("unexpected size for Value"); |
| 3068 | } |
| 3069 | case CoreMemErr: return "CoreMem"; |
| 3070 | case OverlapErr: return "Overlap"; |
| 3071 | case LeakErr: return "Leak"; |
| 3072 | default: VG_(tool_panic)("get_error_name: unexpected type"); |
| 3073 | } |
| 3074 | VG_(printf)(s); |
| 3075 | } |
| 3076 | |
| 3077 | static void mc_print_extra_suppression_info ( Error* err ) |
| 3078 | { |
| 3079 | if (ParamErr == VG_(get_error_kind)(err)) { |
| 3080 | VG_(printf)(" %s\n", VG_(get_error_string)(err)); |
| 3081 | } |
| 3082 | } |
| 3083 | |
njn | 9e63cb6 | 2005-05-08 18:34:59 +0000 | [diff] [blame] | 3084 | /*------------------------------------------------------------*/ |
sewardj | c859fbf | 2005-04-22 21:10:28 +0000 | [diff] [blame] | 3085 | /*--- Functions called directly from generated code: ---*/ |
| 3086 | /*--- Load/store handlers. ---*/ |
sewardj | 6cf40ff | 2005-04-20 22:31:26 +0000 | [diff] [blame] | 3087 | /*------------------------------------------------------------*/ |
| 3088 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3089 | /* Types: LOADV32, LOADV16, LOADV8 are: |
sewardj | 6cf40ff | 2005-04-20 22:31:26 +0000 | [diff] [blame] | 3090 | UWord fn ( Addr a ) |
| 3091 | so they return 32-bits on 32-bit machines and 64-bits on |
| 3092 | 64-bit machines. Addr has the same size as a host word. |
| 3093 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3094 | LOADV64 is always ULong fn ( Addr a ) |
sewardj | 6cf40ff | 2005-04-20 22:31:26 +0000 | [diff] [blame] | 3095 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3096 | Similarly for STOREV8, STOREV16, STOREV32, the supplied vbits |
| 3097 | are a UWord, and for STOREV64 they are a ULong. |
sewardj | 6cf40ff | 2005-04-20 22:31:26 +0000 | [diff] [blame] | 3098 | */ |
| 3099 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3100 | /* If any part of '_a' indicated by the mask is 1, either |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 3101 | '_a' is not naturally '_sz/8'-aligned, or it exceeds the range |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3102 | covered by the primary map. */ |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 3103 | #define UNALIGNED_OR_HIGH(_a,_sz) ((_a) & MASK((_sz>>3))) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3104 | #define MASK(_sz) ( ~((0x10000-(_sz)) | ((N_PRIMARY_MAP-1) << 16)) ) |
| 3105 | |
| 3106 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3107 | /* ------------------------ Size = 8 ------------------------ */ |
| 3108 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3109 | static INLINE |
| 3110 | ULong mc_LOADV64 ( Addr a, Bool isBigEndian ) |
| 3111 | { |
| 3112 | UWord sm_off16, vabits16; |
| 3113 | SecMap* sm; |
| 3114 | |
| 3115 | PROF_EVENT(200, "mc_LOADV64"); |
| 3116 | |
| 3117 | #ifndef PERF_FAST_LOADV |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 3118 | return mc_LOADVn_slow( a, 64, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3119 | #else |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 3120 | if (EXPECTED_NOT_TAKEN( UNALIGNED_OR_HIGH(a,64) )) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3121 | PROF_EVENT(201, "mc_LOADV64-slow1"); |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 3122 | return (ULong)mc_LOADVn_slow( a, 64, isBigEndian ); |
sewardj | f9d8161 | 2005-04-23 23:25:49 +0000 | [diff] [blame] | 3123 | } |
| 3124 | |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 3125 | sm = get_secmap_for_reading_low(a); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3126 | sm_off16 = SM_OFF_16(a); |
| 3127 | vabits16 = ((UShort*)(sm->vabits8))[sm_off16]; |
| 3128 | |
| 3129 | // Handle common case quickly: a is suitably aligned, is mapped, and |
| 3130 | // addressible. |
| 3131 | // Convert V bits from compact memory form to expanded register form. |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3132 | if (EXPECTED_TAKEN(vabits16 == VA_BITS16_DEFINED)) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3133 | return V_BITS64_DEFINED; |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3134 | } else if (EXPECTED_TAKEN(vabits16 == VA_BITS16_UNDEFINED)) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3135 | return V_BITS64_UNDEFINED; |
| 3136 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3137 | /* Slow case: the 8 bytes are not all-defined or all-undefined. */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3138 | PROF_EVENT(202, "mc_LOADV64-slow2"); |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 3139 | return mc_LOADVn_slow( a, 64, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3140 | } |
| 3141 | #endif |
| 3142 | } |
| 3143 | |
| 3144 | VG_REGPARM(1) ULong MC_(helperc_LOADV64be) ( Addr a ) |
| 3145 | { |
| 3146 | return mc_LOADV64(a, True); |
| 3147 | } |
| 3148 | VG_REGPARM(1) ULong MC_(helperc_LOADV64le) ( Addr a ) |
| 3149 | { |
| 3150 | return mc_LOADV64(a, False); |
| 3151 | } |
sewardj | f9d8161 | 2005-04-23 23:25:49 +0000 | [diff] [blame] | 3152 | |
sewardj | f9d8161 | 2005-04-23 23:25:49 +0000 | [diff] [blame] | 3153 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3154 | static INLINE |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3155 | void mc_STOREV64 ( Addr a, ULong vbits64, Bool isBigEndian ) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3156 | { |
| 3157 | UWord sm_off16, vabits16; |
| 3158 | SecMap* sm; |
| 3159 | |
| 3160 | PROF_EVENT(210, "mc_STOREV64"); |
| 3161 | |
| 3162 | #ifndef PERF_FAST_STOREV |
| 3163 | // XXX: this slow case seems to be marginally faster than the fast case! |
| 3164 | // Investigate further. |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3165 | mc_STOREVn_slow( a, 64, vbits64, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3166 | #else |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 3167 | if (EXPECTED_NOT_TAKEN( UNALIGNED_OR_HIGH(a,64) )) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3168 | PROF_EVENT(211, "mc_STOREV64-slow1"); |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3169 | mc_STOREVn_slow( a, 64, vbits64, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3170 | return; |
sewardj | f9d8161 | 2005-04-23 23:25:49 +0000 | [diff] [blame] | 3171 | } |
| 3172 | |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 3173 | sm = get_secmap_for_reading_low(a); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3174 | sm_off16 = SM_OFF_16(a); |
| 3175 | vabits16 = ((UShort*)(sm->vabits8))[sm_off16]; |
| 3176 | |
| 3177 | if (EXPECTED_TAKEN( !is_distinguished_sm(sm) && |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3178 | (VA_BITS16_DEFINED == vabits16 || |
| 3179 | VA_BITS16_UNDEFINED == vabits16) )) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3180 | { |
| 3181 | /* Handle common case quickly: a is suitably aligned, */ |
| 3182 | /* is mapped, and is addressible. */ |
| 3183 | // Convert full V-bits in register to compact 2-bit form. |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3184 | if (V_BITS64_DEFINED == vbits64) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3185 | ((UShort*)(sm->vabits8))[sm_off16] = (UShort)VA_BITS16_DEFINED; |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3186 | } else if (V_BITS64_UNDEFINED == vbits64) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3187 | ((UShort*)(sm->vabits8))[sm_off16] = (UShort)VA_BITS16_UNDEFINED; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3188 | } else { |
| 3189 | /* Slow but general case -- writing partially defined bytes. */ |
| 3190 | PROF_EVENT(212, "mc_STOREV64-slow2"); |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3191 | mc_STOREVn_slow( a, 64, vbits64, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3192 | } |
| 3193 | } else { |
| 3194 | /* Slow but general case. */ |
| 3195 | PROF_EVENT(213, "mc_STOREV64-slow3"); |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3196 | mc_STOREVn_slow( a, 64, vbits64, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3197 | } |
| 3198 | #endif |
| 3199 | } |
| 3200 | |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3201 | VG_REGPARM(1) void MC_(helperc_STOREV64be) ( Addr a, ULong vbits64 ) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3202 | { |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3203 | mc_STOREV64(a, vbits64, True); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3204 | } |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3205 | VG_REGPARM(1) void MC_(helperc_STOREV64le) ( Addr a, ULong vbits64 ) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3206 | { |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3207 | mc_STOREV64(a, vbits64, False); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3208 | } |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3209 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3210 | |
| 3211 | /* ------------------------ Size = 4 ------------------------ */ |
| 3212 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3213 | static INLINE |
| 3214 | UWord mc_LOADV32 ( Addr a, Bool isBigEndian ) |
| 3215 | { |
| 3216 | UWord sm_off, vabits8; |
| 3217 | SecMap* sm; |
| 3218 | |
| 3219 | PROF_EVENT(220, "mc_LOADV32"); |
| 3220 | |
| 3221 | #ifndef PERF_FAST_LOADV |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 3222 | return (UWord)mc_LOADVn_slow( a, 32, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3223 | #else |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 3224 | if (EXPECTED_NOT_TAKEN( UNALIGNED_OR_HIGH(a,32) )) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3225 | PROF_EVENT(221, "mc_LOADV32-slow1"); |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 3226 | return (UWord)mc_LOADVn_slow( a, 32, isBigEndian ); |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 3227 | } |
| 3228 | |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 3229 | sm = get_secmap_for_reading_low(a); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3230 | sm_off = SM_OFF(a); |
| 3231 | vabits8 = sm->vabits8[sm_off]; |
| 3232 | |
| 3233 | // Handle common case quickly: a is suitably aligned, is mapped, and the |
| 3234 | // entire word32 it lives in is addressible. |
| 3235 | // Convert V bits from compact memory form to expanded register form. |
| 3236 | // For 64-bit platforms, set the high 32 bits of retval to 1 (undefined). |
| 3237 | // Almost certainly not necessary, but be paranoid. |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3238 | if (EXPECTED_TAKEN(vabits8 == VA_BITS8_DEFINED)) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3239 | return ((UWord)0xFFFFFFFF00000000ULL | (UWord)V_BITS32_DEFINED); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3240 | } else if (EXPECTED_TAKEN(vabits8 == VA_BITS8_UNDEFINED)) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3241 | return ((UWord)0xFFFFFFFF00000000ULL | (UWord)V_BITS32_UNDEFINED); |
| 3242 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3243 | /* Slow case: the 4 bytes are not all-defined or all-undefined. */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3244 | PROF_EVENT(222, "mc_LOADV32-slow2"); |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 3245 | return (UWord)mc_LOADVn_slow( a, 32, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3246 | } |
| 3247 | #endif |
| 3248 | } |
| 3249 | |
| 3250 | VG_REGPARM(1) UWord MC_(helperc_LOADV32be) ( Addr a ) |
| 3251 | { |
| 3252 | return mc_LOADV32(a, True); |
| 3253 | } |
| 3254 | VG_REGPARM(1) UWord MC_(helperc_LOADV32le) ( Addr a ) |
| 3255 | { |
| 3256 | return mc_LOADV32(a, False); |
| 3257 | } |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 3258 | |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 3259 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3260 | static INLINE |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3261 | void mc_STOREV32 ( Addr a, UWord vbits32, Bool isBigEndian ) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3262 | { |
| 3263 | UWord sm_off, vabits8; |
| 3264 | SecMap* sm; |
| 3265 | |
| 3266 | PROF_EVENT(230, "mc_STOREV32"); |
| 3267 | |
| 3268 | #ifndef PERF_FAST_STOREV |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3269 | mc_STOREVn_slow( a, 32, (ULong)vbits32, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3270 | #else |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 3271 | if (EXPECTED_NOT_TAKEN( UNALIGNED_OR_HIGH(a,32) )) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3272 | PROF_EVENT(231, "mc_STOREV32-slow1"); |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3273 | mc_STOREVn_slow( a, 32, (ULong)vbits32, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3274 | return; |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 3275 | } |
| 3276 | |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 3277 | sm = get_secmap_for_reading_low(a); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3278 | sm_off = SM_OFF(a); |
| 3279 | vabits8 = sm->vabits8[sm_off]; |
| 3280 | |
| 3281 | //--------------------------------------------------------------------------- |
| 3282 | #if 1 |
| 3283 | // Cleverness: sometimes we don't have to write the shadow memory at |
| 3284 | // all, if we can tell that what we want to write is the same as what is |
| 3285 | // already there. |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3286 | if (V_BITS32_DEFINED == vbits32) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3287 | if (vabits8 == (UInt)VA_BITS8_DEFINED) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3288 | return; |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3289 | } else if (!is_distinguished_sm(sm) && VA_BITS8_UNDEFINED == vabits8) { |
| 3290 | sm->vabits8[sm_off] = (UInt)VA_BITS8_DEFINED; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3291 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3292 | // not defined/undefined, or distinguished and changing state |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3293 | PROF_EVENT(232, "mc_STOREV32-slow2"); |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3294 | mc_STOREVn_slow( a, 32, (ULong)vbits32, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3295 | } |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3296 | } else if (V_BITS32_UNDEFINED == vbits32) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3297 | if (vabits8 == (UInt)VA_BITS8_UNDEFINED) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3298 | return; |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3299 | } else if (!is_distinguished_sm(sm) && VA_BITS8_DEFINED == vabits8) { |
| 3300 | sm->vabits8[sm_off] = (UInt)VA_BITS8_UNDEFINED; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3301 | } else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3302 | // not defined/undefined, or distinguished and changing state |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3303 | PROF_EVENT(233, "mc_STOREV32-slow3"); |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3304 | mc_STOREVn_slow( a, 32, (ULong)vbits32, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3305 | } |
| 3306 | } else { |
| 3307 | // Partially defined word |
| 3308 | PROF_EVENT(234, "mc_STOREV32-slow4"); |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3309 | mc_STOREVn_slow( a, 32, (ULong)vbits32, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3310 | } |
| 3311 | //--------------------------------------------------------------------------- |
| 3312 | #else |
| 3313 | if (EXPECTED_TAKEN( !is_distinguished_sm(sm) && |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3314 | (VA_BITS8_DEFINED == vabits8 || |
| 3315 | VA_BITS8_UNDEFINED == vabits8) )) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3316 | { |
| 3317 | /* Handle common case quickly: a is suitably aligned, */ |
| 3318 | /* is mapped, and is addressible. */ |
| 3319 | // Convert full V-bits in register to compact 2-bit form. |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3320 | if (V_BITS32_DEFINED == vbits32) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3321 | sm->vabits8[sm_off] = VA_BITS8_DEFINED; |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3322 | } else if (V_BITS32_UNDEFINED == vbits32) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3323 | sm->vabits8[sm_off] = VA_BITS8_UNDEFINED; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3324 | } else { |
| 3325 | /* Slow but general case -- writing partially defined bytes. */ |
| 3326 | PROF_EVENT(232, "mc_STOREV32-slow2"); |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3327 | mc_STOREVn_slow( a, 32, (ULong)vbits32, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3328 | } |
| 3329 | } else { |
| 3330 | /* Slow but general case. */ |
| 3331 | PROF_EVENT(233, "mc_STOREV32-slow3"); |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3332 | mc_STOREVn_slow( a, 32, (ULong)vbits32, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3333 | } |
| 3334 | #endif |
| 3335 | //--------------------------------------------------------------------------- |
| 3336 | #endif |
| 3337 | } |
| 3338 | |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3339 | VG_REGPARM(2) void MC_(helperc_STOREV32be) ( Addr a, UWord vbits32 ) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3340 | { |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3341 | mc_STOREV32(a, vbits32, True); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3342 | } |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3343 | VG_REGPARM(2) void MC_(helperc_STOREV32le) ( Addr a, UWord vbits32 ) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3344 | { |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3345 | mc_STOREV32(a, vbits32, False); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3346 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3347 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3348 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3349 | /* ------------------------ Size = 2 ------------------------ */ |
| 3350 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3351 | static INLINE |
| 3352 | UWord mc_LOADV16 ( Addr a, Bool isBigEndian ) |
| 3353 | { |
| 3354 | UWord sm_off, vabits8; |
| 3355 | SecMap* sm; |
| 3356 | |
| 3357 | PROF_EVENT(240, "mc_LOADV16"); |
| 3358 | |
| 3359 | #ifndef PERF_FAST_LOADV |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 3360 | return (UWord)mc_LOADVn_slow( a, 16, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3361 | #else |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 3362 | if (EXPECTED_NOT_TAKEN( UNALIGNED_OR_HIGH(a,16) )) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3363 | PROF_EVENT(241, "mc_LOADV16-slow1"); |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 3364 | return (UWord)mc_LOADVn_slow( a, 16, isBigEndian ); |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 3365 | } |
| 3366 | |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 3367 | sm = get_secmap_for_reading_low(a); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3368 | sm_off = SM_OFF(a); |
| 3369 | vabits8 = sm->vabits8[sm_off]; |
| 3370 | // Handle common case quickly: a is suitably aligned, is mapped, and is |
| 3371 | // addressible. |
| 3372 | // Convert V bits from compact memory form to expanded register form |
| 3373 | // XXX: set the high 16/48 bits of retval to 1 for 64-bit paranoia? |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3374 | if (vabits8 == VA_BITS8_DEFINED ) { return V_BITS16_DEFINED; } |
| 3375 | else if (vabits8 == VA_BITS8_UNDEFINED) { return V_BITS16_UNDEFINED; } |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3376 | else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3377 | // The 4 (yes, 4) bytes are not all-defined or all-undefined, check |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3378 | // the two sub-bytes. |
| 3379 | UChar vabits4 = extract_vabits4_from_vabits8(a, vabits8); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3380 | if (vabits4 == VA_BITS4_DEFINED ) { return V_BITS16_DEFINED; } |
| 3381 | else if (vabits4 == VA_BITS4_UNDEFINED) { return V_BITS16_UNDEFINED; } |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3382 | else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3383 | /* Slow case: the two bytes are not all-defined or all-undefined. */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3384 | PROF_EVENT(242, "mc_LOADV16-slow2"); |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 3385 | return (UWord)mc_LOADVn_slow( a, 16, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3386 | } |
| 3387 | } |
| 3388 | #endif |
| 3389 | } |
| 3390 | |
| 3391 | VG_REGPARM(1) UWord MC_(helperc_LOADV16be) ( Addr a ) |
| 3392 | { |
| 3393 | return mc_LOADV16(a, True); |
| 3394 | } |
| 3395 | VG_REGPARM(1) UWord MC_(helperc_LOADV16le) ( Addr a ) |
| 3396 | { |
| 3397 | return mc_LOADV16(a, False); |
| 3398 | } |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 3399 | |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 3400 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3401 | static INLINE |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3402 | void mc_STOREV16 ( Addr a, UWord vbits16, Bool isBigEndian ) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3403 | { |
| 3404 | UWord sm_off, vabits8; |
| 3405 | SecMap* sm; |
| 3406 | |
| 3407 | PROF_EVENT(250, "mc_STOREV16"); |
| 3408 | |
| 3409 | #ifndef PERF_FAST_STOREV |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3410 | mc_STOREVn_slow( a, 16, (ULong)vbits16, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3411 | #else |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 3412 | if (EXPECTED_NOT_TAKEN( UNALIGNED_OR_HIGH(a,16) )) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3413 | PROF_EVENT(251, "mc_STOREV16-slow1"); |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3414 | mc_STOREVn_slow( a, 16, (ULong)vbits16, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3415 | return; |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 3416 | } |
| 3417 | |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 3418 | sm = get_secmap_for_reading_low(a); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3419 | sm_off = SM_OFF(a); |
| 3420 | vabits8 = sm->vabits8[sm_off]; |
| 3421 | if (EXPECTED_TAKEN( !is_distinguished_sm(sm) && |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3422 | (VA_BITS8_DEFINED == vabits8 || |
| 3423 | VA_BITS8_UNDEFINED == vabits8) )) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3424 | { |
| 3425 | /* Handle common case quickly: a is suitably aligned, */ |
| 3426 | /* is mapped, and is addressible. */ |
| 3427 | // Convert full V-bits in register to compact 2-bit form. |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3428 | if (V_BITS16_DEFINED == vbits16) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3429 | insert_vabits4_into_vabits8( a, VA_BITS4_DEFINED , |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3430 | &(sm->vabits8[sm_off]) ); |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3431 | } else if (V_BITS16_UNDEFINED == vbits16) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3432 | insert_vabits4_into_vabits8( a, VA_BITS4_UNDEFINED, |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3433 | &(sm->vabits8[sm_off]) ); |
| 3434 | } else { |
| 3435 | /* Slow but general case -- writing partially defined bytes. */ |
| 3436 | PROF_EVENT(252, "mc_STOREV16-slow2"); |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3437 | mc_STOREVn_slow( a, 16, (ULong)vbits16, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3438 | } |
| 3439 | } else { |
| 3440 | /* Slow but general case. */ |
| 3441 | PROF_EVENT(253, "mc_STOREV16-slow3"); |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3442 | mc_STOREVn_slow( a, 16, (ULong)vbits16, isBigEndian ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3443 | } |
| 3444 | #endif |
| 3445 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3446 | |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3447 | VG_REGPARM(2) void MC_(helperc_STOREV16be) ( Addr a, UWord vbits16 ) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3448 | { |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3449 | mc_STOREV16(a, vbits16, True); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3450 | } |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3451 | VG_REGPARM(2) void MC_(helperc_STOREV16le) ( Addr a, UWord vbits16 ) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3452 | { |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3453 | mc_STOREV16(a, vbits16, False); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3454 | } |
sewardj | 5d28efc | 2005-04-21 22:16:29 +0000 | [diff] [blame] | 3455 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3456 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3457 | /* ------------------------ Size = 1 ------------------------ */ |
sewardj | 8cf88b7 | 2005-07-08 01:29:33 +0000 | [diff] [blame] | 3458 | /* Note: endianness is irrelevant for size == 1 */ |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3459 | |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 3460 | VG_REGPARM(1) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3461 | UWord MC_(helperc_LOADV8) ( Addr a ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3462 | { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3463 | UWord sm_off, vabits8; |
sewardj | ae986ca | 2005-10-12 12:53:20 +0000 | [diff] [blame] | 3464 | SecMap* sm; |
| 3465 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3466 | PROF_EVENT(260, "mc_LOADV8"); |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 3467 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3468 | #ifndef PERF_FAST_LOADV |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 3469 | return (UWord)mc_LOADVn_slow( a, 8, False/*irrelevant*/ ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3470 | #else |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 3471 | if (EXPECTED_NOT_TAKEN( UNALIGNED_OR_HIGH(a,8) )) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3472 | PROF_EVENT(261, "mc_LOADV8-slow1"); |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 3473 | return (UWord)mc_LOADVn_slow( a, 8, False/*irrelevant*/ ); |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 3474 | } |
| 3475 | |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 3476 | sm = get_secmap_for_reading_low(a); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3477 | sm_off = SM_OFF(a); |
| 3478 | vabits8 = sm->vabits8[sm_off]; |
| 3479 | // Convert V bits from compact memory form to expanded register form |
| 3480 | // Handle common case quickly: a is mapped, and the entire |
| 3481 | // word32 it lives in is addressible. |
| 3482 | // XXX: set the high 24/56 bits of retval to 1 for 64-bit paranoia? |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3483 | if (vabits8 == VA_BITS8_DEFINED ) { return V_BITS8_DEFINED; } |
| 3484 | else if (vabits8 == VA_BITS8_UNDEFINED) { return V_BITS8_UNDEFINED; } |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3485 | else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3486 | // The 4 (yes, 4) bytes are not all-defined or all-undefined, check |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3487 | // the single byte. |
| 3488 | UChar vabits2 = extract_vabits2_from_vabits8(a, vabits8); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3489 | if (vabits2 == VA_BITS2_DEFINED ) { return V_BITS8_DEFINED; } |
| 3490 | else if (vabits2 == VA_BITS2_UNDEFINED) { return V_BITS8_UNDEFINED; } |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3491 | else { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3492 | /* Slow case: the byte is not all-defined or all-undefined. */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3493 | PROF_EVENT(262, "mc_LOADV8-slow2"); |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 3494 | return (UWord)mc_LOADVn_slow( a, 8, False/*irrelevant*/ ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3495 | } |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 3496 | } |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3497 | #endif |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3498 | } |
| 3499 | |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 3500 | |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 3501 | VG_REGPARM(2) |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3502 | void MC_(helperc_STOREV8) ( Addr a, UWord vbits8 ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3503 | { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3504 | UWord sm_off, vabits8; |
sewardj | ae986ca | 2005-10-12 12:53:20 +0000 | [diff] [blame] | 3505 | SecMap* sm; |
| 3506 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3507 | PROF_EVENT(270, "mc_STOREV8"); |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 3508 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3509 | #ifndef PERF_FAST_STOREV |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3510 | mc_STOREVn_slow( a, 8, (ULong)vbits8, False/*irrelevant*/ ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3511 | #else |
njn | 45e8125 | 2006-03-28 12:35:08 +0000 | [diff] [blame] | 3512 | if (EXPECTED_NOT_TAKEN( UNALIGNED_OR_HIGH(a,8) )) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3513 | PROF_EVENT(271, "mc_STOREV8-slow1"); |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3514 | mc_STOREVn_slow( a, 8, (ULong)vbits8, False/*irrelevant*/ ); |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 3515 | return; |
| 3516 | } |
| 3517 | |
njn | a7c7ebd | 2006-03-28 12:51:02 +0000 | [diff] [blame] | 3518 | sm = get_secmap_for_reading_low(a); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3519 | sm_off = SM_OFF(a); |
| 3520 | vabits8 = sm->vabits8[sm_off]; |
| 3521 | if (EXPECTED_TAKEN |
| 3522 | ( !is_distinguished_sm(sm) && |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3523 | ( (VA_BITS8_DEFINED == vabits8 || VA_BITS8_UNDEFINED == vabits8) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3524 | || (VA_BITS2_NOACCESS != extract_vabits2_from_vabits8(a, vabits8)) |
| 3525 | ) |
| 3526 | ) |
| 3527 | ) |
| 3528 | { |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 3529 | /* Handle common case quickly: a is mapped, the entire word32 it |
| 3530 | lives in is addressible. */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3531 | // Convert full V-bits in register to compact 2-bit form. |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3532 | if (V_BITS8_DEFINED == vbits8) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3533 | insert_vabits2_into_vabits8( a, VA_BITS2_DEFINED, |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3534 | &(sm->vabits8[sm_off]) ); |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3535 | } else if (V_BITS8_UNDEFINED == vbits8) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3536 | insert_vabits2_into_vabits8( a, VA_BITS2_UNDEFINED, |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3537 | &(sm->vabits8[sm_off]) ); |
| 3538 | } else { |
| 3539 | /* Slow but general case -- writing partially defined bytes. */ |
| 3540 | PROF_EVENT(272, "mc_STOREV8-slow2"); |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3541 | mc_STOREVn_slow( a, 8, (ULong)vbits8, False/*irrelevant*/ ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3542 | } |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 3543 | } else { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3544 | /* Slow but general case. */ |
| 3545 | PROF_EVENT(273, "mc_STOREV8-slow3"); |
njn | 4cf530b | 2006-04-06 13:33:48 +0000 | [diff] [blame] | 3546 | mc_STOREVn_slow( a, 8, (ULong)vbits8, False/*irrelevant*/ ); |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 3547 | } |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3548 | #endif |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3549 | } |
| 3550 | |
| 3551 | |
sewardj | c859fbf | 2005-04-22 21:10:28 +0000 | [diff] [blame] | 3552 | /*------------------------------------------------------------*/ |
| 3553 | /*--- Functions called directly from generated code: ---*/ |
| 3554 | /*--- Value-check failure handlers. ---*/ |
| 3555 | /*------------------------------------------------------------*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3556 | |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 3557 | void MC_(helperc_value_check0_fail) ( void ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3558 | { |
njn | 9e63cb6 | 2005-05-08 18:34:59 +0000 | [diff] [blame] | 3559 | mc_record_value_error ( VG_(get_running_tid)(), 0 ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3560 | } |
| 3561 | |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 3562 | void MC_(helperc_value_check1_fail) ( void ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3563 | { |
njn | 9e63cb6 | 2005-05-08 18:34:59 +0000 | [diff] [blame] | 3564 | mc_record_value_error ( VG_(get_running_tid)(), 1 ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3565 | } |
| 3566 | |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 3567 | void MC_(helperc_value_check4_fail) ( void ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3568 | { |
njn | 9e63cb6 | 2005-05-08 18:34:59 +0000 | [diff] [blame] | 3569 | mc_record_value_error ( VG_(get_running_tid)(), 4 ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3570 | } |
| 3571 | |
sewardj | 11bcc4e | 2005-04-23 22:38:38 +0000 | [diff] [blame] | 3572 | void MC_(helperc_value_check8_fail) ( void ) |
| 3573 | { |
njn | 9e63cb6 | 2005-05-08 18:34:59 +0000 | [diff] [blame] | 3574 | mc_record_value_error ( VG_(get_running_tid)(), 8 ); |
sewardj | 11bcc4e | 2005-04-23 22:38:38 +0000 | [diff] [blame] | 3575 | } |
| 3576 | |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 3577 | VG_REGPARM(1) void MC_(helperc_complain_undef) ( HWord sz ) |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3578 | { |
njn | 9e63cb6 | 2005-05-08 18:34:59 +0000 | [diff] [blame] | 3579 | mc_record_value_error ( VG_(get_running_tid)(), (Int)sz ); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3580 | } |
| 3581 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3582 | |
sewardj | c2c12c2 | 2006-03-08 13:20:09 +0000 | [diff] [blame] | 3583 | /*------------------------------------------------------------*/ |
| 3584 | /*--- Metadata get/set functions, for client requests. ---*/ |
| 3585 | /*------------------------------------------------------------*/ |
| 3586 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3587 | // Nb: this expands the V+A bits out into register-form V bits, even though |
| 3588 | // they're in memory. This is for backward compatibility, and because it's |
| 3589 | // probably what the user wants. |
| 3590 | |
| 3591 | /* Copy Vbits from/to address 'a'. Returns: 1 == OK, 2 == alignment |
sewardj | c2c12c2 | 2006-03-08 13:20:09 +0000 | [diff] [blame] | 3592 | error [no longer used], 3 == addressing error. */ |
| 3593 | static Int mc_get_or_set_vbits_for_client ( |
| 3594 | ThreadId tid, |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3595 | Addr a, |
| 3596 | Addr vbits, |
| 3597 | SizeT szB, |
sewardj | c2c12c2 | 2006-03-08 13:20:09 +0000 | [diff] [blame] | 3598 | Bool setting /* True <=> set vbits, False <=> get vbits */ |
| 3599 | ) |
| 3600 | { |
sewardj | c2c12c2 | 2006-03-08 13:20:09 +0000 | [diff] [blame] | 3601 | SizeT i; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3602 | Bool ok; |
| 3603 | UChar vbits8; |
sewardj | c2c12c2 | 2006-03-08 13:20:09 +0000 | [diff] [blame] | 3604 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3605 | /* Check that arrays are addressible before doing any getting/setting. */ |
| 3606 | for (i = 0; i < szB; i++) { |
| 3607 | if (VA_BITS2_NOACCESS == get_vabits2(a + i)) { |
| 3608 | mc_record_address_error( tid, a + i, 1, setting ? True : False ); |
| 3609 | return 3; |
sewardj | c2c12c2 | 2006-03-08 13:20:09 +0000 | [diff] [blame] | 3610 | } |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3611 | if (VA_BITS2_NOACCESS == get_vabits2(vbits + i)) { |
| 3612 | mc_record_address_error( tid, vbits + i, 1, setting ? False : True ); |
| 3613 | return 3; |
sewardj | c2c12c2 | 2006-03-08 13:20:09 +0000 | [diff] [blame] | 3614 | } |
| 3615 | } |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3616 | |
sewardj | c2c12c2 | 2006-03-08 13:20:09 +0000 | [diff] [blame] | 3617 | /* Do the copy */ |
| 3618 | if (setting) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3619 | |
| 3620 | // It's actually a tool ClientReq, but Vg_CoreClientReq is the closest |
| 3621 | // thing we have. |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3622 | check_mem_is_defined(Vg_CoreClientReq, tid, "SET_VBITS(vbits)", |
| 3623 | vbits, szB); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3624 | |
| 3625 | /* setting */ |
| 3626 | for (i = 0; i < szB; i++) { |
| 3627 | ok = set_vbits8(a + i, ((UChar*)vbits)[i]); |
| 3628 | tl_assert(ok); |
sewardj | c2c12c2 | 2006-03-08 13:20:09 +0000 | [diff] [blame] | 3629 | } |
| 3630 | } else { |
| 3631 | /* getting */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3632 | for (i = 0; i < szB; i++) { |
| 3633 | ok = get_vbits8(a + i, &vbits8); |
| 3634 | tl_assert(ok); |
| 3635 | // XXX: used to do this, but it's a pain |
| 3636 | // if (V_BITS8_DEFINED != vbits8) |
| 3637 | // mc_record_value_error(tid, 1); |
| 3638 | ((UChar*)vbits)[i] = vbits8; |
sewardj | c2c12c2 | 2006-03-08 13:20:09 +0000 | [diff] [blame] | 3639 | } |
| 3640 | // The bytes in vbits[] have now been set, so mark them as such. |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3641 | MC_(make_mem_defined)(vbits, szB); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3642 | } |
sewardj | c2c12c2 | 2006-03-08 13:20:09 +0000 | [diff] [blame] | 3643 | |
| 3644 | return 1; |
| 3645 | } |
sewardj | 05fe85e | 2005-04-27 22:46:36 +0000 | [diff] [blame] | 3646 | |
| 3647 | |
| 3648 | /*------------------------------------------------------------*/ |
| 3649 | /*--- Detecting leaked (unreachable) malloc'd blocks. ---*/ |
| 3650 | /*------------------------------------------------------------*/ |
| 3651 | |
| 3652 | /* For the memory leak detector, say whether an entire 64k chunk of |
| 3653 | address space is possibly in use, or not. If in doubt return |
| 3654 | True. |
| 3655 | */ |
| 3656 | static |
| 3657 | Bool mc_is_within_valid_secondary ( Addr a ) |
| 3658 | { |
| 3659 | SecMap* sm = maybe_get_secmap_for ( a ); |
| 3660 | if (sm == NULL || sm == &sm_distinguished[SM_DIST_NOACCESS]) { |
| 3661 | /* Definitely not in use. */ |
| 3662 | return False; |
| 3663 | } else { |
| 3664 | return True; |
| 3665 | } |
| 3666 | } |
| 3667 | |
| 3668 | |
| 3669 | /* For the memory leak detector, say whether or not a given word |
| 3670 | address is to be regarded as valid. */ |
| 3671 | static |
| 3672 | Bool mc_is_valid_aligned_word ( Addr a ) |
| 3673 | { |
| 3674 | tl_assert(sizeof(UWord) == 4 || sizeof(UWord) == 8); |
| 3675 | if (sizeof(UWord) == 4) { |
| 3676 | tl_assert(VG_IS_4_ALIGNED(a)); |
| 3677 | } else { |
| 3678 | tl_assert(VG_IS_8_ALIGNED(a)); |
| 3679 | } |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3680 | if (is_mem_defined( a, sizeof(UWord), NULL ) == MC_Ok) { |
sewardj | 05fe85e | 2005-04-27 22:46:36 +0000 | [diff] [blame] | 3681 | return True; |
| 3682 | } else { |
| 3683 | return False; |
| 3684 | } |
| 3685 | } |
sewardj | a449568 | 2002-10-21 07:29:59 +0000 | [diff] [blame] | 3686 | |
| 3687 | |
nethercote | 996901a | 2004-08-03 13:29:09 +0000 | [diff] [blame] | 3688 | /* Leak detector for this tool. We don't actually do anything, merely |
sewardj | a449568 | 2002-10-21 07:29:59 +0000 | [diff] [blame] | 3689 | run the generic leak detector with suitable parameters for this |
nethercote | 996901a | 2004-08-03 13:29:09 +0000 | [diff] [blame] | 3690 | tool. */ |
njn | b8dca86 | 2005-03-14 02:42:44 +0000 | [diff] [blame] | 3691 | static void mc_detect_memory_leaks ( ThreadId tid, LeakCheckMode mode ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3692 | { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3693 | MC_(do_detect_memory_leaks) ( |
sewardj | 05fe85e | 2005-04-27 22:46:36 +0000 | [diff] [blame] | 3694 | tid, |
| 3695 | mode, |
| 3696 | mc_is_within_valid_secondary, |
| 3697 | mc_is_valid_aligned_word |
| 3698 | ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3699 | } |
| 3700 | |
| 3701 | |
sewardj | c859fbf | 2005-04-22 21:10:28 +0000 | [diff] [blame] | 3702 | /*------------------------------------------------------------*/ |
| 3703 | /*--- Initialisation ---*/ |
| 3704 | /*------------------------------------------------------------*/ |
| 3705 | |
| 3706 | static void init_shadow_memory ( void ) |
| 3707 | { |
| 3708 | Int i; |
| 3709 | SecMap* sm; |
| 3710 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3711 | tl_assert(V_BIT_UNDEFINED == 1); |
| 3712 | tl_assert(V_BIT_DEFINED == 0); |
| 3713 | tl_assert(V_BITS8_UNDEFINED == 0xFF); |
| 3714 | tl_assert(V_BITS8_DEFINED == 0); |
| 3715 | |
sewardj | c859fbf | 2005-04-22 21:10:28 +0000 | [diff] [blame] | 3716 | /* Build the 3 distinguished secondaries */ |
sewardj | c859fbf | 2005-04-22 21:10:28 +0000 | [diff] [blame] | 3717 | sm = &sm_distinguished[SM_DIST_NOACCESS]; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3718 | for (i = 0; i < SM_CHUNKS; i++) sm->vabits8[i] = VA_BITS8_NOACCESS; |
sewardj | c859fbf | 2005-04-22 21:10:28 +0000 | [diff] [blame] | 3719 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3720 | sm = &sm_distinguished[SM_DIST_UNDEFINED]; |
| 3721 | for (i = 0; i < SM_CHUNKS; i++) sm->vabits8[i] = VA_BITS8_UNDEFINED; |
sewardj | c859fbf | 2005-04-22 21:10:28 +0000 | [diff] [blame] | 3722 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3723 | sm = &sm_distinguished[SM_DIST_DEFINED]; |
| 3724 | for (i = 0; i < SM_CHUNKS; i++) sm->vabits8[i] = VA_BITS8_DEFINED; |
sewardj | c859fbf | 2005-04-22 21:10:28 +0000 | [diff] [blame] | 3725 | |
| 3726 | /* Set up the primary map. */ |
| 3727 | /* These entries gradually get overwritten as the used address |
| 3728 | space expands. */ |
| 3729 | for (i = 0; i < N_PRIMARY_MAP; i++) |
| 3730 | primary_map[i] = &sm_distinguished[SM_DIST_NOACCESS]; |
| 3731 | |
| 3732 | /* auxmap_size = auxmap_used = 0; |
| 3733 | no ... these are statically initialised */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3734 | |
| 3735 | /* Secondary V bit table */ |
| 3736 | secVBitTable = createSecVBitTable(); |
sewardj | c859fbf | 2005-04-22 21:10:28 +0000 | [diff] [blame] | 3737 | } |
| 3738 | |
| 3739 | |
| 3740 | /*------------------------------------------------------------*/ |
| 3741 | /*--- Sanity check machinery (permanently engaged) ---*/ |
| 3742 | /*------------------------------------------------------------*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3743 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 3744 | static Bool mc_cheap_sanity_check ( void ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3745 | { |
jseward | 9800fd3 | 2004-01-04 23:08:04 +0000 | [diff] [blame] | 3746 | /* nothing useful we can rapidly check */ |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 3747 | n_sanity_cheap++; |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 3748 | PROF_EVENT(490, "cheap_sanity_check"); |
jseward | 9800fd3 | 2004-01-04 23:08:04 +0000 | [diff] [blame] | 3749 | return True; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3750 | } |
| 3751 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 3752 | static Bool mc_expensive_sanity_check ( void ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3753 | { |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 3754 | Int i, n_secmaps_found; |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 3755 | SecMap* sm; |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 3756 | Bool bad = False; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3757 | |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 3758 | n_sanity_expensive++; |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 3759 | PROF_EVENT(491, "expensive_sanity_check"); |
| 3760 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3761 | /* Check that the 3 distinguished SMs are still as they should be. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3762 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3763 | /* Check noaccess DSM. */ |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 3764 | sm = &sm_distinguished[SM_DIST_NOACCESS]; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3765 | for (i = 0; i < SM_CHUNKS; i++) |
| 3766 | if (sm->vabits8[i] != VA_BITS8_NOACCESS) |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 3767 | bad = True; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3768 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3769 | /* Check undefined DSM. */ |
| 3770 | sm = &sm_distinguished[SM_DIST_UNDEFINED]; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3771 | for (i = 0; i < SM_CHUNKS; i++) |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3772 | if (sm->vabits8[i] != VA_BITS8_UNDEFINED) |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 3773 | bad = True; |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 3774 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3775 | /* Check defined DSM. */ |
| 3776 | sm = &sm_distinguished[SM_DIST_DEFINED]; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3777 | for (i = 0; i < SM_CHUNKS; i++) |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 3778 | if (sm->vabits8[i] != VA_BITS8_DEFINED) |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 3779 | bad = True; |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 3780 | |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 3781 | if (bad) { |
| 3782 | VG_(printf)("memcheck expensive sanity: " |
| 3783 | "distinguished_secondaries have changed\n"); |
| 3784 | return False; |
| 3785 | } |
| 3786 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3787 | /* If we're not checking for undefined value errors, the secondary V bit |
| 3788 | * table should be empty. */ |
| 3789 | if (!MC_(clo_undef_value_errors)) { |
| 3790 | if (0 != VG_(OSet_Size)(secVBitTable)) |
| 3791 | return False; |
| 3792 | } |
| 3793 | |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 3794 | /* check nonsensical auxmap sizing */ |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 3795 | if (auxmap_used > auxmap_size) |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 3796 | bad = True; |
| 3797 | |
| 3798 | if (bad) { |
| 3799 | VG_(printf)("memcheck expensive sanity: " |
| 3800 | "nonsensical auxmap sizing\n"); |
| 3801 | return False; |
| 3802 | } |
| 3803 | |
| 3804 | /* check that the number of secmaps issued matches the number that |
| 3805 | are reachable (iow, no secmap leaks) */ |
| 3806 | n_secmaps_found = 0; |
| 3807 | for (i = 0; i < N_PRIMARY_MAP; i++) { |
| 3808 | if (primary_map[i] == NULL) { |
| 3809 | bad = True; |
| 3810 | } else { |
| 3811 | if (!is_distinguished_sm(primary_map[i])) |
| 3812 | n_secmaps_found++; |
| 3813 | } |
| 3814 | } |
| 3815 | |
| 3816 | for (i = 0; i < auxmap_used; i++) { |
| 3817 | if (auxmap[i].sm == NULL) { |
| 3818 | bad = True; |
| 3819 | } else { |
| 3820 | if (!is_distinguished_sm(auxmap[i].sm)) |
| 3821 | n_secmaps_found++; |
| 3822 | } |
| 3823 | } |
| 3824 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3825 | if (n_secmaps_found != (n_issued_SMs - n_deissued_SMs)) |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 3826 | bad = True; |
| 3827 | |
| 3828 | if (bad) { |
| 3829 | VG_(printf)("memcheck expensive sanity: " |
| 3830 | "apparent secmap leakage\n"); |
| 3831 | return False; |
| 3832 | } |
| 3833 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3834 | /* check that auxmap only covers address space that the primary doesn't */ |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 3835 | |
| 3836 | for (i = 0; i < auxmap_used; i++) |
| 3837 | if (auxmap[i].base <= MAX_PRIMARY_ADDRESS) |
| 3838 | bad = True; |
| 3839 | |
| 3840 | if (bad) { |
| 3841 | VG_(printf)("memcheck expensive sanity: " |
| 3842 | "auxmap covers wrong address space\n"); |
| 3843 | return False; |
| 3844 | } |
| 3845 | |
| 3846 | /* there is only one pointer to each secmap (expensive) */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3847 | |
| 3848 | return True; |
| 3849 | } |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 3850 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3851 | /*------------------------------------------------------------*/ |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 3852 | /*--- Command line args ---*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3853 | /*------------------------------------------------------------*/ |
| 3854 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3855 | Bool MC_(clo_partial_loads_ok) = False; |
| 3856 | Int MC_(clo_freelist_vol) = 5000000; |
| 3857 | LeakCheckMode MC_(clo_leak_check) = LC_Summary; |
| 3858 | VgRes MC_(clo_leak_resolution) = Vg_LowRes; |
| 3859 | Bool MC_(clo_show_reachable) = False; |
| 3860 | Bool MC_(clo_workaround_gcc296_bugs) = False; |
| 3861 | Bool MC_(clo_undef_value_errors) = True; |
| 3862 | |
| 3863 | static Bool mc_process_cmd_line_options(Char* arg) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3864 | { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3865 | VG_BOOL_CLO(arg, "--partial-loads-ok", MC_(clo_partial_loads_ok)) |
| 3866 | else VG_BOOL_CLO(arg, "--show-reachable", MC_(clo_show_reachable)) |
| 3867 | else VG_BOOL_CLO(arg, "--workaround-gcc296-bugs",MC_(clo_workaround_gcc296_bugs)) |
| 3868 | |
| 3869 | else VG_BOOL_CLO(arg, "--undef-value-errors", MC_(clo_undef_value_errors)) |
| 3870 | |
| 3871 | else VG_BNUM_CLO(arg, "--freelist-vol", MC_(clo_freelist_vol), 0, 1000000000) |
| 3872 | |
| 3873 | else if (VG_CLO_STREQ(arg, "--leak-check=no")) |
| 3874 | MC_(clo_leak_check) = LC_Off; |
| 3875 | else if (VG_CLO_STREQ(arg, "--leak-check=summary")) |
| 3876 | MC_(clo_leak_check) = LC_Summary; |
| 3877 | else if (VG_CLO_STREQ(arg, "--leak-check=yes") || |
| 3878 | VG_CLO_STREQ(arg, "--leak-check=full")) |
| 3879 | MC_(clo_leak_check) = LC_Full; |
| 3880 | |
| 3881 | else if (VG_CLO_STREQ(arg, "--leak-resolution=low")) |
| 3882 | MC_(clo_leak_resolution) = Vg_LowRes; |
| 3883 | else if (VG_CLO_STREQ(arg, "--leak-resolution=med")) |
| 3884 | MC_(clo_leak_resolution) = Vg_MedRes; |
| 3885 | else if (VG_CLO_STREQ(arg, "--leak-resolution=high")) |
| 3886 | MC_(clo_leak_resolution) = Vg_HighRes; |
| 3887 | |
| 3888 | else |
| 3889 | return VG_(replacement_malloc_process_cmd_line_option)(arg); |
| 3890 | |
| 3891 | return True; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3892 | } |
| 3893 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 3894 | static void mc_print_usage(void) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3895 | { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3896 | VG_(printf)( |
| 3897 | " --leak-check=no|summary|full search for memory leaks at exit? [summary]\n" |
| 3898 | " --leak-resolution=low|med|high how much bt merging in leak check [low]\n" |
| 3899 | " --show-reachable=no|yes show reachable blocks in leak check? [no]\n" |
| 3900 | " --undef-value-errors=no|yes check for undefined value errors [yes]\n" |
| 3901 | " --partial-loads-ok=no|yes too hard to explain here; see manual [no]\n" |
| 3902 | " --freelist-vol=<number> volume of freed blocks queue [5000000]\n" |
| 3903 | " --workaround-gcc296-bugs=no|yes self explanatory [no]\n" |
| 3904 | ); |
| 3905 | VG_(replacement_malloc_print_usage)(); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 3906 | } |
| 3907 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 3908 | static void mc_print_debug_usage(void) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 3909 | { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3910 | VG_(replacement_malloc_print_debug_usage)(); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3911 | } |
| 3912 | |
sewardj | f3418c0 | 2005-11-08 14:10:24 +0000 | [diff] [blame] | 3913 | |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 3914 | /*------------------------------------------------------------*/ |
| 3915 | /*--- Client requests ---*/ |
| 3916 | /*------------------------------------------------------------*/ |
| 3917 | |
| 3918 | /* Client block management: |
| 3919 | |
| 3920 | This is managed as an expanding array of client block descriptors. |
| 3921 | Indices of live descriptors are issued to the client, so it can ask |
| 3922 | to free them later. Therefore we cannot slide live entries down |
| 3923 | over dead ones. Instead we must use free/inuse flags and scan for |
| 3924 | an empty slot at allocation time. This in turn means allocation is |
| 3925 | relatively expensive, so we hope this does not happen too often. |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 3926 | |
sewardj | edc75ab | 2005-03-15 23:30:32 +0000 | [diff] [blame] | 3927 | An unused block has start == size == 0 |
| 3928 | */ |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 3929 | |
| 3930 | typedef |
| 3931 | struct { |
| 3932 | Addr start; |
| 3933 | SizeT size; |
| 3934 | ExeContext* where; |
sewardj | 8cf88b7 | 2005-07-08 01:29:33 +0000 | [diff] [blame] | 3935 | Char* desc; |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 3936 | } |
| 3937 | CGenBlock; |
| 3938 | |
| 3939 | /* This subsystem is self-initialising. */ |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 3940 | static UInt cgb_size = 0; |
| 3941 | static UInt cgb_used = 0; |
| 3942 | static CGenBlock* cgbs = NULL; |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 3943 | |
| 3944 | /* Stats for this subsystem. */ |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 3945 | static UInt cgb_used_MAX = 0; /* Max in use. */ |
| 3946 | static UInt cgb_allocs = 0; /* Number of allocs. */ |
| 3947 | static UInt cgb_discards = 0; /* Number of discards. */ |
| 3948 | static UInt cgb_search = 0; /* Number of searches. */ |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 3949 | |
| 3950 | |
| 3951 | static |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 3952 | Int alloc_client_block ( void ) |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 3953 | { |
| 3954 | UInt i, sz_new; |
| 3955 | CGenBlock* cgbs_new; |
| 3956 | |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 3957 | cgb_allocs++; |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 3958 | |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 3959 | for (i = 0; i < cgb_used; i++) { |
| 3960 | cgb_search++; |
| 3961 | if (cgbs[i].start == 0 && cgbs[i].size == 0) |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 3962 | return i; |
| 3963 | } |
| 3964 | |
| 3965 | /* Not found. Try to allocate one at the end. */ |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 3966 | if (cgb_used < cgb_size) { |
| 3967 | cgb_used++; |
| 3968 | return cgb_used-1; |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 3969 | } |
| 3970 | |
| 3971 | /* Ok, we have to allocate a new one. */ |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 3972 | tl_assert(cgb_used == cgb_size); |
| 3973 | sz_new = (cgbs == NULL) ? 10 : (2 * cgb_size); |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 3974 | |
| 3975 | cgbs_new = VG_(malloc)( sz_new * sizeof(CGenBlock) ); |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 3976 | for (i = 0; i < cgb_used; i++) |
| 3977 | cgbs_new[i] = cgbs[i]; |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 3978 | |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 3979 | if (cgbs != NULL) |
| 3980 | VG_(free)( cgbs ); |
| 3981 | cgbs = cgbs_new; |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 3982 | |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 3983 | cgb_size = sz_new; |
| 3984 | cgb_used++; |
| 3985 | if (cgb_used > cgb_used_MAX) |
| 3986 | cgb_used_MAX = cgb_used; |
| 3987 | return cgb_used-1; |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 3988 | } |
| 3989 | |
| 3990 | |
| 3991 | static void show_client_block_stats ( void ) |
| 3992 | { |
| 3993 | VG_(message)(Vg_DebugMsg, |
| 3994 | "general CBs: %d allocs, %d discards, %d maxinuse, %d search", |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 3995 | cgb_allocs, cgb_discards, cgb_used_MAX, cgb_search |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 3996 | ); |
| 3997 | } |
| 3998 | |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 3999 | static Bool client_perm_maybe_describe( Addr a, AddrInfo* ai ) |
| 4000 | { |
| 4001 | UInt i; |
| 4002 | /* VG_(printf)("try to identify %d\n", a); */ |
| 4003 | |
| 4004 | /* Perhaps it's a general block ? */ |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 4005 | for (i = 0; i < cgb_used; i++) { |
| 4006 | if (cgbs[i].start == 0 && cgbs[i].size == 0) |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4007 | continue; |
njn | 717cde5 | 2005-05-10 02:47:21 +0000 | [diff] [blame] | 4008 | // Use zero as the redzone for client blocks. |
| 4009 | if (VG_(addr_is_in_block)(a, cgbs[i].start, cgbs[i].size, 0)) { |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4010 | /* OK - maybe it's a mempool, too? */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4011 | MC_Mempool* mp = VG_(HT_lookup)(MC_(mempool_list), |
njn | 1262727 | 2005-08-14 18:32:16 +0000 | [diff] [blame] | 4012 | (UWord)cgbs[i].start); |
njn | 1d0cb0d | 2005-08-15 01:52:02 +0000 | [diff] [blame] | 4013 | if (mp != NULL) { |
| 4014 | if (mp->chunks != NULL) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4015 | MC_Chunk* mc; |
njn | f66dbfc | 2005-08-15 01:54:05 +0000 | [diff] [blame] | 4016 | VG_(HT_ResetIter)(mp->chunks); |
| 4017 | while ( (mc = VG_(HT_Next)(mp->chunks)) ) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4018 | if (addr_is_in_MC_Chunk(mc, a)) { |
njn | 1d0cb0d | 2005-08-15 01:52:02 +0000 | [diff] [blame] | 4019 | ai->akind = UserG; |
| 4020 | ai->blksize = mc->size; |
| 4021 | ai->rwoffset = (Int)(a) - (Int)mc->data; |
| 4022 | ai->lastchange = mc->where; |
| 4023 | return True; |
| 4024 | } |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4025 | } |
| 4026 | } |
njn | 1d0cb0d | 2005-08-15 01:52:02 +0000 | [diff] [blame] | 4027 | ai->akind = Mempool; |
| 4028 | ai->blksize = cgbs[i].size; |
| 4029 | ai->rwoffset = (Int)(a) - (Int)(cgbs[i].start); |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 4030 | ai->lastchange = cgbs[i].where; |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4031 | return True; |
| 4032 | } |
njn | 1d0cb0d | 2005-08-15 01:52:02 +0000 | [diff] [blame] | 4033 | ai->akind = UserG; |
| 4034 | ai->blksize = cgbs[i].size; |
| 4035 | ai->rwoffset = (Int)(a) - (Int)(cgbs[i].start); |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 4036 | ai->lastchange = cgbs[i].where; |
njn | 1d0cb0d | 2005-08-15 01:52:02 +0000 | [diff] [blame] | 4037 | ai->desc = cgbs[i].desc; |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4038 | return True; |
| 4039 | } |
| 4040 | } |
| 4041 | return False; |
| 4042 | } |
| 4043 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 4044 | static Bool mc_handle_client_request ( ThreadId tid, UWord* arg, UWord* ret ) |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4045 | { |
| 4046 | Int i; |
| 4047 | Bool ok; |
| 4048 | Addr bad_addr; |
| 4049 | |
njn | fc26ff9 | 2004-11-22 19:12:49 +0000 | [diff] [blame] | 4050 | if (!VG_IS_TOOL_USERREQ('M','C',arg[0]) |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4051 | && VG_USERREQ__MALLOCLIKE_BLOCK != arg[0] |
| 4052 | && VG_USERREQ__FREELIKE_BLOCK != arg[0] |
| 4053 | && VG_USERREQ__CREATE_MEMPOOL != arg[0] |
| 4054 | && VG_USERREQ__DESTROY_MEMPOOL != arg[0] |
| 4055 | && VG_USERREQ__MEMPOOL_ALLOC != arg[0] |
| 4056 | && VG_USERREQ__MEMPOOL_FREE != arg[0]) |
| 4057 | return False; |
| 4058 | |
| 4059 | switch (arg[0]) { |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 4060 | case VG_USERREQ__CHECK_MEM_IS_ADDRESSABLE: |
| 4061 | ok = is_mem_addressable ( arg[1], arg[2], &bad_addr ); |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4062 | if (!ok) |
njn | 9e63cb6 | 2005-05-08 18:34:59 +0000 | [diff] [blame] | 4063 | mc_record_user_error ( tid, bad_addr, /*isWrite*/True, |
| 4064 | /*isUnaddr*/True ); |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4065 | *ret = ok ? (UWord)NULL : bad_addr; |
sewardj | 8cf88b7 | 2005-07-08 01:29:33 +0000 | [diff] [blame] | 4066 | break; |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4067 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 4068 | case VG_USERREQ__CHECK_MEM_IS_DEFINED: { |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4069 | MC_ReadResult res; |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 4070 | res = is_mem_defined ( arg[1], arg[2], &bad_addr ); |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4071 | if (MC_AddrErr == res) |
njn | 9e63cb6 | 2005-05-08 18:34:59 +0000 | [diff] [blame] | 4072 | mc_record_user_error ( tid, bad_addr, /*isWrite*/False, |
| 4073 | /*isUnaddr*/True ); |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4074 | else if (MC_ValueErr == res) |
njn | 9e63cb6 | 2005-05-08 18:34:59 +0000 | [diff] [blame] | 4075 | mc_record_user_error ( tid, bad_addr, /*isWrite*/False, |
| 4076 | /*isUnaddr*/False ); |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4077 | *ret = ( res==MC_Ok ? (UWord)NULL : bad_addr ); |
sewardj | 8cf88b7 | 2005-07-08 01:29:33 +0000 | [diff] [blame] | 4078 | break; |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4079 | } |
| 4080 | |
| 4081 | case VG_USERREQ__DO_LEAK_CHECK: |
njn | b8dca86 | 2005-03-14 02:42:44 +0000 | [diff] [blame] | 4082 | mc_detect_memory_leaks(tid, arg[1] ? LC_Summary : LC_Full); |
sewardj | 8cf88b7 | 2005-07-08 01:29:33 +0000 | [diff] [blame] | 4083 | *ret = 0; /* return value is meaningless */ |
| 4084 | break; |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4085 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 4086 | case VG_USERREQ__MAKE_MEM_NOACCESS: |
| 4087 | MC_(make_mem_noaccess) ( arg[1], arg[2] ); |
sewardj | 8cf88b7 | 2005-07-08 01:29:33 +0000 | [diff] [blame] | 4088 | *ret = -1; |
| 4089 | break; |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4090 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 4091 | case VG_USERREQ__MAKE_MEM_UNDEFINED: |
| 4092 | MC_(make_mem_undefined) ( arg[1], arg[2] ); |
sewardj | edc75ab | 2005-03-15 23:30:32 +0000 | [diff] [blame] | 4093 | *ret = -1; |
sewardj | 8cf88b7 | 2005-07-08 01:29:33 +0000 | [diff] [blame] | 4094 | break; |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4095 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 4096 | case VG_USERREQ__MAKE_MEM_DEFINED: |
| 4097 | MC_(make_mem_defined) ( arg[1], arg[2] ); |
sewardj | 8cf88b7 | 2005-07-08 01:29:33 +0000 | [diff] [blame] | 4098 | *ret = -1; |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4099 | break; |
| 4100 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 4101 | case VG_USERREQ__MAKE_MEM_DEFINED_IF_ADDRESSABLE: |
| 4102 | make_mem_defined_if_addressable ( arg[1], arg[2] ); |
sewardj | fb1e9ad | 2006-03-10 13:41:58 +0000 | [diff] [blame] | 4103 | *ret = -1; |
| 4104 | break; |
| 4105 | |
sewardj | edc75ab | 2005-03-15 23:30:32 +0000 | [diff] [blame] | 4106 | case VG_USERREQ__CREATE_BLOCK: /* describe a block */ |
sewardj | 8cf88b7 | 2005-07-08 01:29:33 +0000 | [diff] [blame] | 4107 | if (arg[1] != 0 && arg[2] != 0) { |
| 4108 | i = alloc_client_block(); |
| 4109 | /* VG_(printf)("allocated %d %p\n", i, cgbs); */ |
| 4110 | cgbs[i].start = arg[1]; |
| 4111 | cgbs[i].size = arg[2]; |
| 4112 | cgbs[i].desc = VG_(strdup)((Char *)arg[3]); |
| 4113 | cgbs[i].where = VG_(record_ExeContext) ( tid ); |
sewardj | edc75ab | 2005-03-15 23:30:32 +0000 | [diff] [blame] | 4114 | |
sewardj | 8cf88b7 | 2005-07-08 01:29:33 +0000 | [diff] [blame] | 4115 | *ret = i; |
| 4116 | } else |
| 4117 | *ret = -1; |
| 4118 | break; |
sewardj | edc75ab | 2005-03-15 23:30:32 +0000 | [diff] [blame] | 4119 | |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4120 | case VG_USERREQ__DISCARD: /* discard */ |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 4121 | if (cgbs == NULL |
| 4122 | || arg[2] >= cgb_used || |
sewardj | 8cf88b7 | 2005-07-08 01:29:33 +0000 | [diff] [blame] | 4123 | (cgbs[arg[2]].start == 0 && cgbs[arg[2]].size == 0)) { |
sewardj | edc75ab | 2005-03-15 23:30:32 +0000 | [diff] [blame] | 4124 | *ret = 1; |
sewardj | 8cf88b7 | 2005-07-08 01:29:33 +0000 | [diff] [blame] | 4125 | } else { |
| 4126 | tl_assert(arg[2] >= 0 && arg[2] < cgb_used); |
| 4127 | cgbs[arg[2]].start = cgbs[arg[2]].size = 0; |
| 4128 | VG_(free)(cgbs[arg[2]].desc); |
| 4129 | cgb_discards++; |
| 4130 | *ret = 0; |
| 4131 | } |
| 4132 | break; |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4133 | |
sewardj | c2c12c2 | 2006-03-08 13:20:09 +0000 | [diff] [blame] | 4134 | case VG_USERREQ__GET_VBITS: |
| 4135 | /* Returns: 1 == OK, 2 == alignment error, 3 == addressing |
| 4136 | error. */ |
| 4137 | /* VG_(printf)("get_vbits %p %p %d\n", arg[1], arg[2], arg[3] ); */ |
| 4138 | *ret = mc_get_or_set_vbits_for_client |
| 4139 | ( tid, arg[1], arg[2], arg[3], False /* get them */ ); |
| 4140 | break; |
| 4141 | |
| 4142 | case VG_USERREQ__SET_VBITS: |
| 4143 | /* Returns: 1 == OK, 2 == alignment error, 3 == addressing |
| 4144 | error. */ |
| 4145 | /* VG_(printf)("set_vbits %p %p %d\n", arg[1], arg[2], arg[3] ); */ |
| 4146 | *ret = mc_get_or_set_vbits_for_client |
| 4147 | ( tid, arg[1], arg[2], arg[3], True /* set them */ ); |
| 4148 | break; |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4149 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4150 | case VG_USERREQ__COUNT_LEAKS: { /* count leaked bytes */ |
| 4151 | UWord** argp = (UWord**)arg; |
| 4152 | // MC_(bytes_leaked) et al were set by the last leak check (or zero |
| 4153 | // if no prior leak checks performed). |
| 4154 | *argp[1] = MC_(bytes_leaked) + MC_(bytes_indirect); |
| 4155 | *argp[2] = MC_(bytes_dubious); |
| 4156 | *argp[3] = MC_(bytes_reachable); |
| 4157 | *argp[4] = MC_(bytes_suppressed); |
| 4158 | // there is no argp[5] |
| 4159 | //*argp[5] = MC_(bytes_indirect); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 4160 | // XXX need to make *argp[1-4] defined |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4161 | *ret = 0; |
| 4162 | return True; |
| 4163 | } |
| 4164 | case VG_USERREQ__MALLOCLIKE_BLOCK: { |
| 4165 | Addr p = (Addr)arg[1]; |
| 4166 | SizeT sizeB = arg[2]; |
| 4167 | UInt rzB = arg[3]; |
| 4168 | Bool is_zeroed = (Bool)arg[4]; |
| 4169 | |
| 4170 | MC_(new_block) ( tid, p, sizeB, /*ignored*/0, rzB, is_zeroed, |
| 4171 | MC_AllocCustom, MC_(malloc_list) ); |
| 4172 | return True; |
| 4173 | } |
| 4174 | case VG_USERREQ__FREELIKE_BLOCK: { |
| 4175 | Addr p = (Addr)arg[1]; |
| 4176 | UInt rzB = arg[2]; |
| 4177 | |
| 4178 | MC_(handle_free) ( tid, p, rzB, MC_AllocCustom ); |
| 4179 | return True; |
| 4180 | } |
| 4181 | |
| 4182 | case _VG_USERREQ__MEMCHECK_RECORD_OVERLAP_ERROR: { |
| 4183 | Char* s = (Char*) arg[1]; |
| 4184 | OverlapExtra* extra = (OverlapExtra*)arg[2]; |
| 4185 | mc_record_overlap_error(tid, s, extra); |
| 4186 | return True; |
| 4187 | } |
| 4188 | |
| 4189 | case VG_USERREQ__CREATE_MEMPOOL: { |
| 4190 | Addr pool = (Addr)arg[1]; |
| 4191 | UInt rzB = arg[2]; |
| 4192 | Bool is_zeroed = (Bool)arg[3]; |
| 4193 | |
| 4194 | MC_(create_mempool) ( pool, rzB, is_zeroed ); |
| 4195 | return True; |
| 4196 | } |
| 4197 | |
| 4198 | case VG_USERREQ__DESTROY_MEMPOOL: { |
| 4199 | Addr pool = (Addr)arg[1]; |
| 4200 | |
| 4201 | MC_(destroy_mempool) ( pool ); |
| 4202 | return True; |
| 4203 | } |
| 4204 | |
| 4205 | case VG_USERREQ__MEMPOOL_ALLOC: { |
| 4206 | Addr pool = (Addr)arg[1]; |
| 4207 | Addr addr = (Addr)arg[2]; |
| 4208 | UInt size = arg[3]; |
| 4209 | |
| 4210 | MC_(mempool_alloc) ( tid, pool, addr, size ); |
| 4211 | return True; |
| 4212 | } |
| 4213 | |
| 4214 | case VG_USERREQ__MEMPOOL_FREE: { |
| 4215 | Addr pool = (Addr)arg[1]; |
| 4216 | Addr addr = (Addr)arg[2]; |
| 4217 | |
| 4218 | MC_(mempool_free) ( pool, addr ); |
| 4219 | return True; |
| 4220 | } |
| 4221 | |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4222 | default: |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4223 | VG_(message)(Vg_UserMsg, |
| 4224 | "Warning: unknown memcheck client request code %llx", |
| 4225 | (ULong)arg[0]); |
| 4226 | return False; |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4227 | } |
| 4228 | return True; |
| 4229 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4230 | |
| 4231 | /*------------------------------------------------------------*/ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4232 | /*--- Crude profiling machinery. ---*/ |
| 4233 | /*------------------------------------------------------------*/ |
| 4234 | |
| 4235 | // We track a number of interesting events (using PROF_EVENT) |
| 4236 | // if MC_PROFILE_MEMORY is defined. |
| 4237 | |
| 4238 | #ifdef MC_PROFILE_MEMORY |
| 4239 | |
| 4240 | UInt MC_(event_ctr)[N_PROF_EVENTS]; |
| 4241 | HChar* MC_(event_ctr_name)[N_PROF_EVENTS]; |
| 4242 | |
| 4243 | static void init_prof_mem ( void ) |
| 4244 | { |
| 4245 | Int i; |
| 4246 | for (i = 0; i < N_PROF_EVENTS; i++) { |
| 4247 | MC_(event_ctr)[i] = 0; |
| 4248 | MC_(event_ctr_name)[i] = NULL; |
| 4249 | } |
| 4250 | } |
| 4251 | |
| 4252 | static void done_prof_mem ( void ) |
| 4253 | { |
| 4254 | Int i; |
| 4255 | Bool spaced = False; |
| 4256 | for (i = 0; i < N_PROF_EVENTS; i++) { |
| 4257 | if (!spaced && (i % 10) == 0) { |
| 4258 | VG_(printf)("\n"); |
| 4259 | spaced = True; |
| 4260 | } |
| 4261 | if (MC_(event_ctr)[i] > 0) { |
| 4262 | spaced = False; |
| 4263 | VG_(printf)( "prof mem event %3d: %9d %s\n", |
| 4264 | i, MC_(event_ctr)[i], |
| 4265 | MC_(event_ctr_name)[i] |
| 4266 | ? MC_(event_ctr_name)[i] : "unnamed"); |
| 4267 | } |
| 4268 | } |
| 4269 | } |
| 4270 | |
| 4271 | #else |
| 4272 | |
| 4273 | static void init_prof_mem ( void ) { } |
| 4274 | static void done_prof_mem ( void ) { } |
| 4275 | |
| 4276 | #endif |
| 4277 | |
| 4278 | /*------------------------------------------------------------*/ |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 4279 | /*--- Setup and finalisation ---*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4280 | /*------------------------------------------------------------*/ |
| 4281 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 4282 | static void mc_post_clo_init ( void ) |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 4283 | { |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 4284 | /* If we've been asked to emit XML, mash around various other |
| 4285 | options so as to constrain the output somewhat. */ |
| 4286 | if (VG_(clo_xml)) { |
| 4287 | /* Extract as much info as possible from the leak checker. */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4288 | /* MC_(clo_show_reachable) = True; */ |
| 4289 | MC_(clo_leak_check) = LC_Full; |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 4290 | } |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 4291 | } |
| 4292 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4293 | static void print_SM_info(char* type, int n_SMs) |
| 4294 | { |
| 4295 | VG_(message)(Vg_DebugMsg, |
| 4296 | " memcheck: SMs: %s = %d (%dk, %dM)", |
| 4297 | type, |
| 4298 | n_SMs, |
| 4299 | n_SMs * sizeof(SecMap) / 1024, |
| 4300 | n_SMs * sizeof(SecMap) / (1024 * 1024) ); |
| 4301 | } |
| 4302 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 4303 | static void mc_fini ( Int exitcode ) |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 4304 | { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4305 | MC_(print_malloc_stats)(); |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 4306 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4307 | if (VG_(clo_verbosity) == 1 && !VG_(clo_xml)) { |
| 4308 | if (MC_(clo_leak_check) == LC_Off) |
| 4309 | VG_(message)(Vg_UserMsg, |
| 4310 | "For a detailed leak analysis, rerun with: --leak-check=yes"); |
| 4311 | |
| 4312 | VG_(message)(Vg_UserMsg, |
| 4313 | "For counts of detected errors, rerun with: -v"); |
| 4314 | } |
| 4315 | if (MC_(clo_leak_check) != LC_Off) |
| 4316 | mc_detect_memory_leaks(1/*bogus ThreadId*/, MC_(clo_leak_check)); |
| 4317 | |
| 4318 | done_prof_mem(); |
sewardj | ae986ca | 2005-10-12 12:53:20 +0000 | [diff] [blame] | 4319 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 4320 | if (VG_(clo_verbosity) > 1) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4321 | SizeT max_secVBit_szB, max_SMs_szB, max_shmem_szB; |
| 4322 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 4323 | VG_(message)(Vg_DebugMsg, |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 4324 | " memcheck: sanity checks: %d cheap, %d expensive", |
| 4325 | n_sanity_cheap, n_sanity_expensive ); |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 4326 | VG_(message)(Vg_DebugMsg, |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 4327 | " memcheck: auxmaps: %d auxmap entries (%dk, %dM) in use", |
| 4328 | auxmap_used, |
| 4329 | auxmap_used * 64, |
| 4330 | auxmap_used / 16 ); |
| 4331 | VG_(message)(Vg_DebugMsg, |
| 4332 | " memcheck: auxmaps: %lld searches, %lld comparisons", |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 4333 | n_auxmap_searches, n_auxmap_cmps ); |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 4334 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 4335 | print_SM_info("n_issued ", n_issued_SMs); |
| 4336 | print_SM_info("n_deissued ", n_deissued_SMs); |
| 4337 | print_SM_info("max_noaccess ", max_noaccess_SMs); |
| 4338 | print_SM_info("max_undefined", max_undefined_SMs); |
| 4339 | print_SM_info("max_defined ", max_defined_SMs); |
| 4340 | print_SM_info("max_non_DSM ", max_non_DSM_SMs); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4341 | |
| 4342 | // Three DSMs, plus the non-DSM ones |
| 4343 | max_SMs_szB = (3 + max_non_DSM_SMs) * sizeof(SecMap); |
| 4344 | // The 3*sizeof(Word) bytes is the AVL node metadata size. |
| 4345 | // The 4*sizeof(Word) bytes is the malloc metadata size. |
| 4346 | // Hardwiring these sizes in sucks, but I don't see how else to do it. |
| 4347 | max_secVBit_szB = max_secVBit_nodes * |
| 4348 | (sizeof(SecVBitNode) + 3*sizeof(Word) + 4*sizeof(Word)); |
| 4349 | max_shmem_szB = sizeof(primary_map) + max_SMs_szB + max_secVBit_szB; |
sewardj | 23eb2fd | 2005-04-22 16:29:19 +0000 | [diff] [blame] | 4350 | |
| 4351 | VG_(message)(Vg_DebugMsg, |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4352 | " memcheck: max sec V bit nodes: %d (%dk, %dM)", |
| 4353 | max_secVBit_nodes, max_secVBit_szB / 1024, |
| 4354 | max_secVBit_szB / (1024 * 1024)); |
| 4355 | VG_(message)(Vg_DebugMsg, |
| 4356 | " memcheck: set_sec_vbits8 calls: %llu (new: %llu, updates: %llu)", |
| 4357 | sec_vbits_new_nodes + sec_vbits_updates, |
| 4358 | sec_vbits_new_nodes, sec_vbits_updates ); |
| 4359 | VG_(message)(Vg_DebugMsg, |
| 4360 | " memcheck: max shadow mem size: %dk, %dM", |
| 4361 | max_shmem_szB / 1024, max_shmem_szB / (1024 * 1024)); |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 4362 | } |
| 4363 | |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 4364 | if (0) { |
| 4365 | VG_(message)(Vg_DebugMsg, |
| 4366 | "------ Valgrind's client block stats follow ---------------" ); |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4367 | show_client_block_stats(); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 4368 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4369 | } |
| 4370 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 4371 | static void mc_pre_clo_init(void) |
| 4372 | { |
| 4373 | VG_(details_name) ("Memcheck"); |
| 4374 | VG_(details_version) (NULL); |
| 4375 | VG_(details_description) ("a memory error detector"); |
| 4376 | VG_(details_copyright_author)( |
sewardj | e4b0bf0 | 2006-06-05 23:21:15 +0000 | [diff] [blame^] | 4377 | "Copyright (C) 2002-2006, and GNU GPL'd, by Julian Seward et al."); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 4378 | VG_(details_bug_reports_to) (VG_BUGS_TO); |
| 4379 | VG_(details_avg_translation_sizeB) ( 370 ); |
| 4380 | |
| 4381 | VG_(basic_tool_funcs) (mc_post_clo_init, |
| 4382 | MC_(instrument), |
| 4383 | mc_fini); |
| 4384 | |
| 4385 | VG_(needs_core_errors) (); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4386 | VG_(needs_tool_errors) (mc_eq_Error, |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 4387 | mc_pp_Error, |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4388 | mc_update_extra, |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 4389 | mc_recognised_suppression, |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4390 | mc_read_extra_suppression_info, |
| 4391 | mc_error_matches_suppression, |
| 4392 | mc_get_error_name, |
| 4393 | mc_print_extra_suppression_info); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 4394 | VG_(needs_libc_freeres) (); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4395 | VG_(needs_command_line_options)(mc_process_cmd_line_options, |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 4396 | mc_print_usage, |
| 4397 | mc_print_debug_usage); |
| 4398 | VG_(needs_client_requests) (mc_handle_client_request); |
| 4399 | VG_(needs_sanity_checks) (mc_cheap_sanity_check, |
| 4400 | mc_expensive_sanity_check); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4401 | VG_(needs_malloc_replacement) (MC_(malloc), |
| 4402 | MC_(__builtin_new), |
| 4403 | MC_(__builtin_vec_new), |
| 4404 | MC_(memalign), |
| 4405 | MC_(calloc), |
| 4406 | MC_(free), |
| 4407 | MC_(__builtin_delete), |
| 4408 | MC_(__builtin_vec_delete), |
| 4409 | MC_(realloc), |
| 4410 | MC_MALLOC_REDZONE_SZB ); |
njn | ca54af3 | 2006-04-16 10:25:43 +0000 | [diff] [blame] | 4411 | VG_(needs_xml_output) (); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 4412 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4413 | VG_(track_new_mem_startup) ( mc_new_mem_startup ); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 4414 | VG_(track_new_mem_stack_signal)( MC_(make_mem_undefined) ); |
| 4415 | VG_(track_new_mem_brk) ( MC_(make_mem_undefined) ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4416 | VG_(track_new_mem_mmap) ( mc_new_mem_mmap ); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 4417 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4418 | VG_(track_copy_mem_remap) ( MC_(copy_address_range_state) ); |
njn | 8162371 | 2005-10-07 04:48:37 +0000 | [diff] [blame] | 4419 | |
| 4420 | // Nb: we don't do anything with mprotect. This means that V bits are |
| 4421 | // preserved if a program, for example, marks some memory as inaccessible |
| 4422 | // and then later marks it as accessible again. |
| 4423 | // |
| 4424 | // If an access violation occurs (eg. writing to read-only memory) we let |
| 4425 | // it fault and print an informative termination message. This doesn't |
| 4426 | // happen if the program catches the signal, though, which is bad. If we |
| 4427 | // had two A bits (for readability and writability) that were completely |
| 4428 | // distinct from V bits, then we could handle all this properly. |
| 4429 | VG_(track_change_mem_mprotect) ( NULL ); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 4430 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 4431 | VG_(track_die_mem_stack_signal)( MC_(make_mem_noaccess) ); |
| 4432 | VG_(track_die_mem_brk) ( MC_(make_mem_noaccess) ); |
| 4433 | VG_(track_die_mem_munmap) ( MC_(make_mem_noaccess) ); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 4434 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4435 | #ifdef PERF_FAST_STACK |
| 4436 | VG_(track_new_mem_stack_4) ( mc_new_mem_stack_4 ); |
| 4437 | VG_(track_new_mem_stack_8) ( mc_new_mem_stack_8 ); |
| 4438 | VG_(track_new_mem_stack_12) ( mc_new_mem_stack_12 ); |
| 4439 | VG_(track_new_mem_stack_16) ( mc_new_mem_stack_16 ); |
| 4440 | VG_(track_new_mem_stack_32) ( mc_new_mem_stack_32 ); |
| 4441 | VG_(track_new_mem_stack_112) ( mc_new_mem_stack_112 ); |
| 4442 | VG_(track_new_mem_stack_128) ( mc_new_mem_stack_128 ); |
| 4443 | VG_(track_new_mem_stack_144) ( mc_new_mem_stack_144 ); |
| 4444 | VG_(track_new_mem_stack_160) ( mc_new_mem_stack_160 ); |
| 4445 | #endif |
| 4446 | VG_(track_new_mem_stack) ( mc_new_mem_stack ); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 4447 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4448 | #ifdef PERF_FAST_STACK |
| 4449 | VG_(track_die_mem_stack_4) ( mc_die_mem_stack_4 ); |
| 4450 | VG_(track_die_mem_stack_8) ( mc_die_mem_stack_8 ); |
| 4451 | VG_(track_die_mem_stack_12) ( mc_die_mem_stack_12 ); |
| 4452 | VG_(track_die_mem_stack_16) ( mc_die_mem_stack_16 ); |
| 4453 | VG_(track_die_mem_stack_32) ( mc_die_mem_stack_32 ); |
| 4454 | VG_(track_die_mem_stack_112) ( mc_die_mem_stack_112 ); |
| 4455 | VG_(track_die_mem_stack_128) ( mc_die_mem_stack_128 ); |
| 4456 | VG_(track_die_mem_stack_144) ( mc_die_mem_stack_144 ); |
| 4457 | VG_(track_die_mem_stack_160) ( mc_die_mem_stack_160 ); |
| 4458 | #endif |
| 4459 | VG_(track_die_mem_stack) ( mc_die_mem_stack ); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 4460 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 4461 | VG_(track_ban_mem_stack) ( MC_(make_mem_noaccess) ); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 4462 | |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 4463 | VG_(track_pre_mem_read) ( check_mem_is_defined ); |
| 4464 | VG_(track_pre_mem_read_asciiz) ( check_mem_is_defined_asciiz ); |
| 4465 | VG_(track_pre_mem_write) ( check_mem_is_addressable ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4466 | VG_(track_post_mem_write) ( mc_post_mem_write ); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 4467 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4468 | if (MC_(clo_undef_value_errors)) |
| 4469 | VG_(track_pre_reg_read) ( mc_pre_reg_read ); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 4470 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4471 | VG_(track_post_reg_write) ( mc_post_reg_write ); |
| 4472 | VG_(track_post_reg_write_clientcall_return)( mc_post_reg_write_clientcall ); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 4473 | |
| 4474 | init_shadow_memory(); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4475 | MC_(malloc_list) = VG_(HT_construct)( 80021 ); // prime, big |
| 4476 | MC_(mempool_list) = VG_(HT_construct)( 1009 ); // prime, not so big |
| 4477 | init_prof_mem(); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 4478 | |
| 4479 | tl_assert( mc_expensive_sanity_check() ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4480 | |
| 4481 | // {LOADV,STOREV}[8421] will all fail horribly if this isn't true. |
| 4482 | tl_assert(sizeof(UWord) == sizeof(Addr)); |
| 4483 | |
| 4484 | // BYTES_PER_SEC_VBIT_NODE must be a power of two. |
| 4485 | tl_assert(-1 != VG_(log2)(BYTES_PER_SEC_VBIT_NODE)); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 4486 | } |
| 4487 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 4488 | VG_DETERMINE_INTERFACE_VERSION(mc_pre_clo_init) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 4489 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4490 | /*--------------------------------------------------------------------*/ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 4491 | /*--- end ---*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4492 | /*--------------------------------------------------------------------*/ |