njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 3 | /*--- The AddrCheck tool: like MemCheck, but only does address ---*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4 | /*--- checking. No definedness checking. ---*/ |
njn25 | cac76cb | 2002-09-23 11:21:57 +0000 | [diff] [blame] | 5 | /*--- ac_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 AddrCheck, a lightweight 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 | |
njn | 5361242 | 2005-03-12 16:22:54 +0000 | [diff] [blame] | 12 | Copyright (C) 2000-2005 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 | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 33 | #include "mac_shared.h" |
sewardj | d8033d9 | 2002-12-08 22:16:58 +0000 | [diff] [blame] | 34 | #include "memcheck.h" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 35 | |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 36 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 37 | /*------------------------------------------------------------*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 38 | /*--- Comparing and printing errors ---*/ |
| 39 | /*------------------------------------------------------------*/ |
| 40 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 41 | static void ac_pp_Error ( Error* err ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 42 | { |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 43 | MAC_Error* err_extra = VG_(get_error_extra)(err); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 44 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 45 | switch (VG_(get_error_kind)(err)) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 46 | case CoreMemErr: |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 47 | VG_(message)(Vg_UserMsg, "%s contains unaddressable byte(s)", |
| 48 | VG_(get_error_string)(err)); |
| 49 | VG_(pp_ExeContext)( VG_(get_error_where)(err) ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 50 | break; |
| 51 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 52 | case ParamErr: |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 53 | VG_(message)(Vg_UserMsg, |
| 54 | "Syscall param %s contains unaddressable byte(s)", |
| 55 | VG_(get_error_string)(err) ); |
| 56 | VG_(pp_ExeContext)( VG_(get_error_where)(err) ); |
| 57 | MAC_(pp_AddrInfo)(VG_(get_error_address)(err), &err_extra->addrinfo); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 58 | break; |
| 59 | |
| 60 | case UserErr: |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 61 | VG_(message)(Vg_UserMsg, |
| 62 | "Unaddressable byte(s) found during client check request"); |
| 63 | VG_(pp_ExeContext)( VG_(get_error_where)(err) ); |
| 64 | MAC_(pp_AddrInfo)(VG_(get_error_address)(err), &err_extra->addrinfo); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 65 | break; |
| 66 | |
| 67 | default: |
njn | b126f73 | 2004-11-22 17:57:07 +0000 | [diff] [blame] | 68 | MAC_(pp_shared_Error)(err); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 69 | break; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
| 73 | /*------------------------------------------------------------*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 74 | /*--- Suppressions ---*/ |
| 75 | /*------------------------------------------------------------*/ |
| 76 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 77 | static Bool ac_recognised_suppression ( Char* name, Supp* su ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 78 | { |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 79 | return MAC_(shared_recognised_suppression)(name, su); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 80 | } |
| 81 | |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 82 | #define DEBUG(fmt, args...) //VG_(printf)(fmt, ## args) |
| 83 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 84 | /*------------------------------------------------------------*/ |
| 85 | /*--- Low-level support for memory checking. ---*/ |
| 86 | /*------------------------------------------------------------*/ |
| 87 | |
| 88 | /* All reads and writes are checked against a memory map, which |
| 89 | records the state of all memory in the process. The memory map is |
| 90 | organised like this: |
| 91 | |
| 92 | The top 16 bits of an address are used to index into a top-level |
| 93 | map table, containing 65536 entries. Each entry is a pointer to a |
| 94 | second-level map, which records the accesibililty and validity |
| 95 | permissions for the 65536 bytes indexed by the lower 16 bits of the |
| 96 | address. Each byte is represented by one bit, indicating |
| 97 | accessibility. So each second-level map contains 8192 bytes. This |
| 98 | two-level arrangement conveniently divides the 4G address space |
| 99 | into 64k lumps, each size 64k bytes. |
| 100 | |
| 101 | All entries in the primary (top-level) map must point to a valid |
| 102 | secondary (second-level) map. Since most of the 4G of address |
| 103 | space will not be in use -- ie, not mapped at all -- there is a |
njn | 02bc4b8 | 2005-05-15 17:28:26 +0000 | [diff] [blame] | 104 | distinguished secondary map, which indicates 'not addressible and |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 105 | not valid' writeable for all bytes. Entries in the primary map for |
| 106 | which the entire 64k is not in use at all point at this |
| 107 | distinguished map. |
| 108 | |
| 109 | [...] lots of stuff deleted due to out of date-ness |
| 110 | |
| 111 | As a final optimisation, the alignment and address checks for |
| 112 | 4-byte loads and stores are combined in a neat way. The primary |
| 113 | map is extended to have 262144 entries (2^18), rather than 2^16. |
| 114 | The top 3/4 of these entries are permanently set to the |
| 115 | distinguished secondary map. For a 4-byte load/store, the |
| 116 | top-level map is indexed not with (addr >> 16) but instead f(addr), |
| 117 | where |
| 118 | |
| 119 | f( XXXX XXXX XXXX XXXX ____ ____ ____ __YZ ) |
| 120 | = ____ ____ ____ __YZ XXXX XXXX XXXX XXXX or |
| 121 | = ____ ____ ____ __ZY XXXX XXXX XXXX XXXX |
| 122 | |
| 123 | ie the lowest two bits are placed above the 16 high address bits. |
| 124 | If either of these two bits are nonzero, the address is misaligned; |
| 125 | this will select a secondary map from the upper 3/4 of the primary |
| 126 | map. Because this is always the distinguished secondary map, a |
| 127 | (bogus) address check failure will result. The failure handling |
| 128 | code can then figure out whether this is a genuine addr check |
| 129 | failure or whether it is a possibly-legitimate access at a |
| 130 | misaligned address. */ |
| 131 | |
| 132 | |
| 133 | /*------------------------------------------------------------*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 134 | /*--- Function declarations. ---*/ |
| 135 | /*------------------------------------------------------------*/ |
| 136 | |
njn | c2699f6 | 2003-09-05 23:29:33 +0000 | [diff] [blame] | 137 | static void ac_ACCESS4_SLOWLY ( Addr a, Bool isWrite ); |
| 138 | static void ac_ACCESS2_SLOWLY ( Addr a, Bool isWrite ); |
| 139 | static void ac_ACCESS1_SLOWLY ( Addr a, Bool isWrite ); |
nethercote | 928a5f7 | 2004-11-03 18:10:37 +0000 | [diff] [blame] | 140 | static void ac_fpu_ACCESS_check_SLOWLY ( Addr addr, SizeT size, Bool isWrite ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 141 | |
| 142 | /*------------------------------------------------------------*/ |
| 143 | /*--- Data defns. ---*/ |
| 144 | /*------------------------------------------------------------*/ |
| 145 | |
| 146 | typedef |
| 147 | struct { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 148 | UChar abits[SECONDARY_SIZE / 8]; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 149 | } |
| 150 | AcSecMap; |
| 151 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 152 | static AcSecMap* primary_map[ /*PRIMARY_SIZE*/ PRIMARY_SIZE*4 ]; |
| 153 | static const AcSecMap distinguished_secondary_maps[2] = { |
| 154 | [ VGM_BIT_INVALID ] = { { [0 ... (SECONDARY_SIZE/8) - 1] = VGM_BYTE_INVALID } }, |
| 155 | [ VGM_BIT_VALID ] = { { [0 ... (SECONDARY_SIZE/8) - 1] = VGM_BYTE_VALID } }, |
| 156 | }; |
| 157 | #define N_SECONDARY_MAPS (sizeof(distinguished_secondary_maps)/sizeof(*distinguished_secondary_maps)) |
| 158 | |
| 159 | #define DSM_IDX(a) ((a) & 1) |
| 160 | |
| 161 | #define DSM(a) ((AcSecMap *)&distinguished_secondary_maps[DSM_IDX(a)]) |
| 162 | |
| 163 | #define DSM_NOTADDR DSM(VGM_BIT_INVALID) |
| 164 | #define DSM_ADDR DSM(VGM_BIT_VALID) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 165 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 166 | static void init_shadow_memory ( void ) |
| 167 | { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 168 | Int i, a; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 169 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 170 | /* check construction of the distinguished secondaries */ |
sewardj | 76754cf | 2005-03-14 00:14:04 +0000 | [diff] [blame] | 171 | tl_assert(VGM_BIT_INVALID == 1); |
| 172 | tl_assert(VGM_BIT_VALID == 0); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 173 | |
| 174 | for(a = 0; a <= 1; a++) |
sewardj | 76754cf | 2005-03-14 00:14:04 +0000 | [diff] [blame] | 175 | tl_assert(distinguished_secondary_maps[DSM_IDX(a)].abits[0] == BIT_EXPAND(a)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 176 | |
| 177 | /* These entries gradually get overwritten as the used address |
| 178 | space expands. */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 179 | for (i = 0; i < PRIMARY_SIZE; i++) |
| 180 | primary_map[i] = DSM_NOTADDR; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 181 | |
| 182 | /* These ones should never change; it's a bug in Valgrind if they do. */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 183 | for (i = PRIMARY_SIZE; i < PRIMARY_SIZE*4; i++) |
| 184 | primary_map[i] = DSM_NOTADDR; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 185 | } |
| 186 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 187 | /*------------------------------------------------------------*/ |
| 188 | /*--- Basic bitmap management, reading and writing. ---*/ |
| 189 | /*------------------------------------------------------------*/ |
| 190 | |
| 191 | /* Allocate and initialise a secondary map. */ |
| 192 | |
| 193 | static AcSecMap* alloc_secondary_map ( __attribute__ ((unused)) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 194 | Char* caller, |
| 195 | const AcSecMap *prototype) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 196 | { |
| 197 | AcSecMap* map; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 198 | PROF_EVENT(10); |
| 199 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 200 | map = (AcSecMap *)VG_(shadow_alloc)(sizeof(AcSecMap)); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 201 | VG_(memcpy)(map, prototype, sizeof(*map)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 202 | |
| 203 | /* VG_(printf)("ALLOC_2MAP(%s)\n", caller ); */ |
| 204 | return map; |
| 205 | } |
| 206 | |
| 207 | |
| 208 | /* Basic reading/writing of the bitmaps, for byte-sized accesses. */ |
| 209 | |
| 210 | static __inline__ UChar get_abit ( Addr a ) |
| 211 | { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 212 | AcSecMap* sm = primary_map[PM_IDX(a)]; |
| 213 | UInt sm_off = SM_OFF(a); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 214 | PROF_EVENT(20); |
| 215 | # if 0 |
| 216 | if (IS_DISTINGUISHED_SM(sm)) |
| 217 | VG_(message)(Vg_DebugMsg, |
| 218 | "accessed distinguished 2ndary (A)map! 0x%x\n", a); |
| 219 | # endif |
| 220 | return BITARR_TEST(sm->abits, sm_off) |
| 221 | ? VGM_BIT_INVALID : VGM_BIT_VALID; |
| 222 | } |
| 223 | |
sewardj | 5686735 | 2003-10-12 10:27:06 +0000 | [diff] [blame] | 224 | static /* __inline__ */ void set_abit ( Addr a, UChar abit ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 225 | { |
| 226 | AcSecMap* sm; |
| 227 | UInt sm_off; |
| 228 | PROF_EVENT(22); |
| 229 | ENSURE_MAPPABLE(a, "set_abit"); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 230 | sm = primary_map[PM_IDX(a)]; |
| 231 | sm_off = SM_OFF(a); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 232 | if (abit) |
| 233 | BITARR_SET(sm->abits, sm_off); |
| 234 | else |
| 235 | BITARR_CLEAR(sm->abits, sm_off); |
| 236 | } |
| 237 | |
| 238 | |
| 239 | /* Reading/writing of the bitmaps, for aligned word-sized accesses. */ |
| 240 | |
| 241 | static __inline__ UChar get_abits4_ALIGNED ( Addr a ) |
| 242 | { |
| 243 | AcSecMap* sm; |
| 244 | UInt sm_off; |
| 245 | UChar abits8; |
| 246 | PROF_EVENT(24); |
| 247 | # ifdef VG_DEBUG_MEMORY |
njn | be91aae | 2005-03-27 01:42:41 +0000 | [diff] [blame] | 248 | tl_assert(VG_IS_4_ALIGNED(a)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 249 | # endif |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 250 | sm = primary_map[PM_IDX(a)]; |
| 251 | sm_off = SM_OFF(a); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 252 | abits8 = sm->abits[sm_off >> 3]; |
| 253 | abits8 >>= (a & 4 /* 100b */); /* a & 4 is either 0 or 4 */ |
| 254 | abits8 &= 0x0F; |
| 255 | return abits8; |
| 256 | } |
| 257 | |
| 258 | |
| 259 | |
| 260 | /*------------------------------------------------------------*/ |
| 261 | /*--- Setting permissions over address ranges. ---*/ |
| 262 | /*------------------------------------------------------------*/ |
| 263 | |
sewardj | 5686735 | 2003-10-12 10:27:06 +0000 | [diff] [blame] | 264 | static /* __inline__ */ |
nethercote | 451eae9 | 2004-11-02 13:06:32 +0000 | [diff] [blame] | 265 | void set_address_range_perms ( Addr a, SizeT len, UInt example_a_bit ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 266 | { |
| 267 | UChar abyte8; |
| 268 | UInt sm_off; |
| 269 | AcSecMap* sm; |
| 270 | |
| 271 | PROF_EVENT(30); |
| 272 | |
| 273 | if (len == 0) |
| 274 | return; |
| 275 | |
| 276 | if (len > 100 * 1000 * 1000) { |
| 277 | VG_(message)(Vg_UserMsg, |
| 278 | "Warning: set address range perms: " |
| 279 | "large range %u, a %d", |
| 280 | len, example_a_bit ); |
| 281 | } |
| 282 | |
| 283 | VGP_PUSHCC(VgpSetMem); |
| 284 | |
| 285 | /* Requests to change permissions of huge address ranges may |
| 286 | indicate bugs in our machinery. 30,000,000 is arbitrary, but so |
| 287 | far all legitimate requests have fallen beneath that size. */ |
| 288 | /* 4 Mar 02: this is just stupid; get rid of it. */ |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 289 | /* tl_assert(len < 30000000); */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 290 | |
| 291 | /* Check the permissions make sense. */ |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 292 | tl_assert(example_a_bit == VGM_BIT_VALID |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 293 | || example_a_bit == VGM_BIT_INVALID); |
| 294 | |
| 295 | /* In order that we can charge through the address space at 8 |
| 296 | bytes/main-loop iteration, make up some perms. */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 297 | abyte8 = BIT_EXPAND(example_a_bit); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 298 | |
| 299 | # ifdef VG_DEBUG_MEMORY |
| 300 | /* Do it ... */ |
| 301 | while (True) { |
| 302 | PROF_EVENT(31); |
| 303 | if (len == 0) break; |
| 304 | set_abit ( a, example_a_bit ); |
| 305 | set_vbyte ( a, vbyte ); |
| 306 | a++; |
| 307 | len--; |
| 308 | } |
| 309 | |
| 310 | # else |
| 311 | /* Slowly do parts preceding 8-byte alignment. */ |
| 312 | while (True) { |
| 313 | PROF_EVENT(31); |
| 314 | if (len == 0) break; |
| 315 | if ((a % 8) == 0) break; |
| 316 | set_abit ( a, example_a_bit ); |
| 317 | a++; |
| 318 | len--; |
| 319 | } |
| 320 | |
| 321 | if (len == 0) { |
| 322 | VGP_POPCC(VgpSetMem); |
| 323 | return; |
| 324 | } |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 325 | tl_assert((a % 8) == 0 && len > 0); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 326 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 327 | /* Once aligned, go fast up to primary boundary. */ |
| 328 | for (; (a & SECONDARY_MASK) && len >= 8; a += 8, len -= 8) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 329 | PROF_EVENT(32); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 330 | |
| 331 | /* If the primary is already pointing to a distinguished map |
| 332 | with the same properties as we're trying to set, then leave |
| 333 | it that way. */ |
| 334 | if (primary_map[PM_IDX(a)] == DSM(example_a_bit)) |
| 335 | continue; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 336 | ENSURE_MAPPABLE(a, "set_address_range_perms(fast)"); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 337 | sm = primary_map[PM_IDX(a)]; |
| 338 | sm_off = SM_OFF(a); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 339 | sm->abits[sm_off >> 3] = abyte8; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 340 | } |
| 341 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 342 | /* Now set whole secondary maps to the right distinguished value. |
| 343 | |
| 344 | Note that if the primary already points to a non-distinguished |
| 345 | secondary, then don't replace the reference. That would just |
| 346 | leak memory. |
| 347 | */ |
| 348 | for(; len >= SECONDARY_SIZE; a += SECONDARY_SIZE, len -= SECONDARY_SIZE) { |
| 349 | sm = primary_map[PM_IDX(a)]; |
| 350 | |
| 351 | if (IS_DISTINGUISHED_SM(sm)) |
| 352 | primary_map[PM_IDX(a)] = DSM(example_a_bit); |
| 353 | else |
| 354 | VG_(memset)(sm->abits, abyte8, sizeof(sm->abits)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 355 | } |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 356 | |
| 357 | /* Now finished the remains. */ |
| 358 | for (; len >= 8; a += 8, len -= 8) { |
| 359 | PROF_EVENT(32); |
| 360 | |
| 361 | /* If the primary is already pointing to a distinguished map |
| 362 | with the same properties as we're trying to set, then leave |
| 363 | it that way. */ |
| 364 | if (primary_map[PM_IDX(a)] == DSM(example_a_bit)) |
| 365 | continue; |
| 366 | ENSURE_MAPPABLE(a, "set_address_range_perms(fast)"); |
| 367 | sm = primary_map[PM_IDX(a)]; |
| 368 | sm_off = SM_OFF(a); |
| 369 | sm->abits[sm_off >> 3] = abyte8; |
| 370 | } |
| 371 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 372 | |
| 373 | /* Finish the upper fragment. */ |
| 374 | while (True) { |
| 375 | PROF_EVENT(33); |
| 376 | if (len == 0) break; |
| 377 | set_abit ( a, example_a_bit ); |
| 378 | a++; |
| 379 | len--; |
| 380 | } |
| 381 | # endif |
| 382 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 383 | VGP_POPCC(VgpSetMem); |
| 384 | } |
| 385 | |
| 386 | /* Set permissions for address ranges ... */ |
| 387 | |
nethercote | 451eae9 | 2004-11-02 13:06:32 +0000 | [diff] [blame] | 388 | static void ac_make_noaccess ( Addr a, SizeT len ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 389 | { |
| 390 | PROF_EVENT(35); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 391 | DEBUG("ac_make_noaccess(%p, %x)\n", a, len); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 392 | set_address_range_perms ( a, len, VGM_BIT_INVALID ); |
| 393 | } |
| 394 | |
nethercote | 451eae9 | 2004-11-02 13:06:32 +0000 | [diff] [blame] | 395 | static void ac_make_accessible ( Addr a, SizeT len ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 396 | { |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 397 | PROF_EVENT(38); |
| 398 | DEBUG("ac_make_accessible(%p, %x)\n", a, len); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 399 | set_address_range_perms ( a, len, VGM_BIT_VALID ); |
| 400 | } |
| 401 | |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 402 | static __inline__ |
| 403 | void make_aligned_word_noaccess(Addr a) |
| 404 | { |
| 405 | AcSecMap* sm; |
| 406 | UInt sm_off; |
| 407 | UChar mask; |
| 408 | |
| 409 | VGP_PUSHCC(VgpESPAdj); |
| 410 | ENSURE_MAPPABLE(a, "make_aligned_word_noaccess"); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 411 | sm = primary_map[PM_IDX(a)]; |
| 412 | sm_off = SM_OFF(a); |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 413 | mask = 0x0F; |
| 414 | mask <<= (a & 4 /* 100b */); /* a & 4 is either 0 or 4 */ |
| 415 | /* mask now contains 1s where we wish to make address bits invalid (1s). */ |
| 416 | sm->abits[sm_off >> 3] |= mask; |
| 417 | VGP_POPCC(VgpESPAdj); |
| 418 | } |
| 419 | |
| 420 | static __inline__ |
| 421 | void make_aligned_word_accessible(Addr a) |
| 422 | { |
| 423 | AcSecMap* sm; |
| 424 | UInt sm_off; |
| 425 | UChar mask; |
| 426 | |
| 427 | VGP_PUSHCC(VgpESPAdj); |
| 428 | ENSURE_MAPPABLE(a, "make_aligned_word_accessible"); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 429 | sm = primary_map[PM_IDX(a)]; |
| 430 | sm_off = SM_OFF(a); |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 431 | mask = 0x0F; |
| 432 | mask <<= (a & 4 /* 100b */); /* a & 4 is either 0 or 4 */ |
| 433 | /* mask now contains 1s where we wish to make address bits |
| 434 | invalid (0s). */ |
| 435 | sm->abits[sm_off >> 3] &= ~mask; |
| 436 | VGP_POPCC(VgpESPAdj); |
| 437 | } |
| 438 | |
| 439 | /* Nb: by "aligned" here we mean 8-byte aligned */ |
| 440 | static __inline__ |
| 441 | void make_aligned_doubleword_accessible(Addr a) |
| 442 | { |
| 443 | AcSecMap* sm; |
| 444 | UInt sm_off; |
| 445 | |
| 446 | VGP_PUSHCC(VgpESPAdj); |
| 447 | ENSURE_MAPPABLE(a, "make_aligned_doubleword_accessible"); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 448 | sm = primary_map[PM_IDX(a)]; |
| 449 | sm_off = SM_OFF(a); |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 450 | sm->abits[sm_off >> 3] = VGM_BYTE_VALID; |
| 451 | VGP_POPCC(VgpESPAdj); |
| 452 | } |
| 453 | |
| 454 | static __inline__ |
| 455 | void make_aligned_doubleword_noaccess(Addr a) |
| 456 | { |
| 457 | AcSecMap* sm; |
| 458 | UInt sm_off; |
| 459 | |
| 460 | VGP_PUSHCC(VgpESPAdj); |
| 461 | ENSURE_MAPPABLE(a, "make_aligned_doubleword_noaccess"); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 462 | sm = primary_map[PM_IDX(a)]; |
| 463 | sm_off = SM_OFF(a); |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 464 | sm->abits[sm_off >> 3] = VGM_BYTE_INVALID; |
| 465 | VGP_POPCC(VgpESPAdj); |
| 466 | } |
| 467 | |
| 468 | /* The %esp update handling functions */ |
| 469 | ESP_UPDATE_HANDLERS ( make_aligned_word_accessible, |
| 470 | make_aligned_word_noaccess, |
| 471 | make_aligned_doubleword_accessible, |
| 472 | make_aligned_doubleword_noaccess, |
| 473 | ac_make_accessible, |
| 474 | ac_make_noaccess |
| 475 | ); |
| 476 | |
| 477 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 478 | /* Block-copy permissions (needed for implementing realloc()). */ |
| 479 | |
nethercote | 451eae9 | 2004-11-02 13:06:32 +0000 | [diff] [blame] | 480 | static void ac_copy_address_range_state ( Addr src, Addr dst, SizeT len ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 481 | { |
| 482 | UInt i; |
| 483 | |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 484 | DEBUG("ac_copy_address_range_state\n"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 485 | |
| 486 | PROF_EVENT(40); |
| 487 | for (i = 0; i < len; i++) { |
| 488 | UChar abit = get_abit ( src+i ); |
| 489 | PROF_EVENT(41); |
| 490 | set_abit ( dst+i, abit ); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | |
| 495 | /* Check permissions for address range. If inadequate permissions |
| 496 | exist, *bad_addr is set to the offending address, so the caller can |
| 497 | know what it is. */ |
| 498 | |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 499 | static __inline__ |
nethercote | 451eae9 | 2004-11-02 13:06:32 +0000 | [diff] [blame] | 500 | Bool ac_check_accessible ( Addr a, SizeT len, Addr* bad_addr ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 501 | { |
| 502 | UInt i; |
| 503 | UChar abit; |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 504 | PROF_EVENT(48); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 505 | for (i = 0; i < len; i++) { |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 506 | PROF_EVENT(49); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 507 | abit = get_abit(a); |
| 508 | if (abit == VGM_BIT_INVALID) { |
| 509 | if (bad_addr != NULL) *bad_addr = a; |
| 510 | return False; |
| 511 | } |
| 512 | a++; |
| 513 | } |
| 514 | return True; |
| 515 | } |
| 516 | |
sewardj | ecf8e10 | 2003-07-12 12:11:39 +0000 | [diff] [blame] | 517 | /* The opposite; check that an address range is inaccessible. */ |
| 518 | static |
nethercote | 451eae9 | 2004-11-02 13:06:32 +0000 | [diff] [blame] | 519 | Bool ac_check_noaccess ( Addr a, SizeT len, Addr* bad_addr ) |
sewardj | ecf8e10 | 2003-07-12 12:11:39 +0000 | [diff] [blame] | 520 | { |
| 521 | UInt i; |
| 522 | UChar abit; |
| 523 | PROF_EVENT(48); |
| 524 | for (i = 0; i < len; i++) { |
| 525 | PROF_EVENT(49); |
| 526 | abit = get_abit(a); |
| 527 | if (abit == VGM_BIT_VALID) { |
| 528 | if (bad_addr != NULL) *bad_addr = a; |
| 529 | return False; |
| 530 | } |
| 531 | a++; |
| 532 | } |
| 533 | return True; |
| 534 | } |
| 535 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 536 | /* Check a zero-terminated ascii string. Tricky -- don't want to |
| 537 | examine the actual bytes, to find the end, until we're sure it is |
| 538 | safe to do so. */ |
| 539 | |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 540 | static __inline__ |
| 541 | Bool ac_check_readable_asciiz ( Addr a, Addr* bad_addr ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 542 | { |
| 543 | UChar abit; |
| 544 | PROF_EVENT(46); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 545 | DEBUG("ac_check_readable_asciiz\n"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 546 | while (True) { |
| 547 | PROF_EVENT(47); |
| 548 | abit = get_abit(a); |
| 549 | if (abit != VGM_BIT_VALID) { |
| 550 | if (bad_addr != NULL) *bad_addr = a; |
| 551 | return False; |
| 552 | } |
| 553 | /* Ok, a is safe to read. */ |
| 554 | if (* ((UChar*)a) == 0) return True; |
| 555 | a++; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | |
| 560 | /*------------------------------------------------------------*/ |
| 561 | /*--- Memory event handlers ---*/ |
| 562 | /*------------------------------------------------------------*/ |
| 563 | |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 564 | static __inline__ |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 565 | void ac_check_is_accessible ( CorePart part, ThreadId tid, |
nethercote | 451eae9 | 2004-11-02 13:06:32 +0000 | [diff] [blame] | 566 | Char* s, Addr base, SizeT size, Bool isWrite ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 567 | { |
| 568 | Bool ok; |
njn | 86f12dc | 2005-03-14 01:16:05 +0000 | [diff] [blame] | 569 | Addr bad_addr = 0; // Initialise to shut gcc up |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 570 | |
| 571 | VGP_PUSHCC(VgpCheckMem); |
| 572 | |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 573 | ok = ac_check_accessible ( base, size, &bad_addr ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 574 | if (!ok) { |
| 575 | switch (part) { |
| 576 | case Vg_CoreSysCall: |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 577 | MAC_(record_param_error) ( tid, bad_addr, /*isReg*/False, |
| 578 | /*isUnaddr*/True, s ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 579 | break; |
| 580 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 581 | case Vg_CoreSignal: |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 582 | tl_assert(isWrite); /* Should only happen with isWrite case */ |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 583 | /* fall through */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 584 | case Vg_CorePThread: |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 585 | MAC_(record_core_mem_error)( tid, /*isUnaddr*/True, s ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 586 | break; |
| 587 | |
| 588 | /* If we're being asked to jump to a silly address, record an error |
| 589 | message before potentially crashing the entire system. */ |
| 590 | case Vg_CoreTranslate: |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 591 | tl_assert(!isWrite); /* Should only happen with !isWrite case */ |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 592 | MAC_(record_jump_error)( tid, bad_addr ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 593 | break; |
| 594 | |
| 595 | default: |
njn | 6799325 | 2004-11-22 18:02:32 +0000 | [diff] [blame] | 596 | VG_(tool_panic)("ac_check_is_accessible: unexpected CorePart"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 597 | } |
| 598 | } |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 599 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 600 | VGP_POPCC(VgpCheckMem); |
| 601 | } |
| 602 | |
| 603 | static |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 604 | void ac_check_is_writable ( CorePart part, ThreadId tid, |
nethercote | 451eae9 | 2004-11-02 13:06:32 +0000 | [diff] [blame] | 605 | Char* s, Addr base, SizeT size ) |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 606 | { |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 607 | ac_check_is_accessible ( part, tid, s, base, size, /*isWrite*/True ); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | static |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 611 | void ac_check_is_readable ( CorePart part, ThreadId tid, |
nethercote | 451eae9 | 2004-11-02 13:06:32 +0000 | [diff] [blame] | 612 | Char* s, Addr base, SizeT size ) |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 613 | { |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 614 | ac_check_is_accessible ( part, tid, s, base, size, /*isWrite*/False ); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | static |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 618 | void ac_check_is_readable_asciiz ( CorePart part, ThreadId tid, |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 619 | Char* s, Addr str ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 620 | { |
| 621 | Bool ok = True; |
njn | 86f12dc | 2005-03-14 01:16:05 +0000 | [diff] [blame] | 622 | Addr bad_addr = 0; // Initialise to shut gcc up |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 623 | |
| 624 | VGP_PUSHCC(VgpCheckMem); |
| 625 | |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 626 | tl_assert(part == Vg_CoreSysCall); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 627 | ok = ac_check_readable_asciiz ( (Addr)str, &bad_addr ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 628 | if (!ok) { |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 629 | MAC_(record_param_error) ( tid, bad_addr, /*IsReg*/False, |
| 630 | /*IsUnaddr*/True, s ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | VGP_POPCC(VgpCheckMem); |
| 634 | } |
| 635 | |
| 636 | static |
nethercote | 451eae9 | 2004-11-02 13:06:32 +0000 | [diff] [blame] | 637 | void ac_new_mem_startup( Addr a, SizeT len, Bool rr, Bool ww, Bool xx ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 638 | { |
njn | 1f3a909 | 2002-10-04 09:22:30 +0000 | [diff] [blame] | 639 | /* Ignore the permissions, just make it readable. Seems to work... */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 640 | DEBUG("new_mem_startup(%p, %u, rr=%u, ww=%u, xx=%u)\n", a,len,rr,ww,xx); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 641 | ac_make_accessible(a, len); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | static |
nethercote | 451eae9 | 2004-11-02 13:06:32 +0000 | [diff] [blame] | 645 | void ac_new_mem_heap ( Addr a, SizeT len, Bool is_inited ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 646 | { |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 647 | ac_make_accessible(a, len); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | static |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 651 | void ac_new_mem_mmap (Addr a, SizeT len, Bool rr, Bool ww, Bool xx) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 652 | { |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 653 | DEBUG("ac_set_perms(%p, %u, rr=%u ww=%u, xx=%u)\n", |
sewardj | 40f8ebe | 2002-10-23 21:46:13 +0000 | [diff] [blame] | 654 | a, len, rr, ww, xx); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 655 | ac_make_accessible(a, len); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 656 | } |
| 657 | |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 658 | static |
| 659 | void ac_post_mem_write(CorePart part, ThreadId tid, Addr a, SizeT len) |
| 660 | { |
| 661 | ac_make_accessible(a, len); |
| 662 | } |
| 663 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 664 | |
| 665 | /*------------------------------------------------------------*/ |
| 666 | /*--- Functions called directly from generated code. ---*/ |
| 667 | /*------------------------------------------------------------*/ |
| 668 | |
| 669 | static __inline__ UInt rotateRight16 ( UInt x ) |
| 670 | { |
| 671 | /* Amazingly, gcc turns this into a single rotate insn. */ |
| 672 | return (x >> 16) | (x << 16); |
| 673 | } |
| 674 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 675 | static __inline__ UInt shiftRight16 ( UInt x ) |
| 676 | { |
| 677 | return x >> 16; |
| 678 | } |
| 679 | |
| 680 | |
| 681 | /* Read/write 1/2/4 sized V bytes, and emit an address error if |
| 682 | needed. */ |
| 683 | |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 684 | /* ac_helperc_ACCESS{1,2,4} handle the common case fast. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 685 | Under all other circumstances, it defers to the relevant _SLOWLY |
| 686 | function, which can handle all situations. |
| 687 | */ |
njn | c2699f6 | 2003-09-05 23:29:33 +0000 | [diff] [blame] | 688 | static __inline__ void ac_helperc_ACCESS4 ( Addr a, Bool isWrite ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 689 | { |
| 690 | # ifdef VG_DEBUG_MEMORY |
njn | c2699f6 | 2003-09-05 23:29:33 +0000 | [diff] [blame] | 691 | return ac_ACCESS4_SLOWLY(a, isWrite); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 692 | # else |
| 693 | UInt sec_no = rotateRight16(a) & 0x3FFFF; |
njn | da2e36d | 2003-09-30 13:33:24 +0000 | [diff] [blame] | 694 | AcSecMap* sm = primary_map[sec_no]; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 695 | UInt a_off = (SM_OFF(a)) >> 3; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 696 | UChar abits = sm->abits[a_off]; |
| 697 | abits >>= (a & 4); |
| 698 | abits &= 15; |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 699 | PROF_EVENT(66); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 700 | if (abits == VGM_NIBBLE_VALID) { |
| 701 | /* Handle common case quickly: a is suitably aligned, is mapped, |
| 702 | and is addressible. So just return. */ |
| 703 | return; |
| 704 | } else { |
| 705 | /* Slow but general case. */ |
njn | c2699f6 | 2003-09-05 23:29:33 +0000 | [diff] [blame] | 706 | ac_ACCESS4_SLOWLY(a, isWrite); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 707 | } |
| 708 | # endif |
| 709 | } |
| 710 | |
njn | c2699f6 | 2003-09-05 23:29:33 +0000 | [diff] [blame] | 711 | static __inline__ void ac_helperc_ACCESS2 ( Addr a, Bool isWrite ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 712 | { |
| 713 | # ifdef VG_DEBUG_MEMORY |
njn | c2699f6 | 2003-09-05 23:29:33 +0000 | [diff] [blame] | 714 | return ac_ACCESS2_SLOWLY(a, isWrite); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 715 | # else |
| 716 | UInt sec_no = rotateRight16(a) & 0x1FFFF; |
| 717 | AcSecMap* sm = primary_map[sec_no]; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 718 | UInt a_off = (SM_OFF(a)) >> 3; |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 719 | PROF_EVENT(67); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 720 | if (sm->abits[a_off] == VGM_BYTE_VALID) { |
| 721 | /* Handle common case quickly. */ |
| 722 | return; |
| 723 | } else { |
| 724 | /* Slow but general case. */ |
njn | c2699f6 | 2003-09-05 23:29:33 +0000 | [diff] [blame] | 725 | ac_ACCESS2_SLOWLY(a, isWrite); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 726 | } |
| 727 | # endif |
| 728 | } |
| 729 | |
njn | c2699f6 | 2003-09-05 23:29:33 +0000 | [diff] [blame] | 730 | static __inline__ void ac_helperc_ACCESS1 ( Addr a, Bool isWrite ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 731 | { |
| 732 | # ifdef VG_DEBUG_MEMORY |
njn | c2699f6 | 2003-09-05 23:29:33 +0000 | [diff] [blame] | 733 | return ac_ACCESS1_SLOWLY(a, isWrite); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 734 | # else |
| 735 | UInt sec_no = shiftRight16(a); |
| 736 | AcSecMap* sm = primary_map[sec_no]; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 737 | UInt a_off = (SM_OFF(a)) >> 3; |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 738 | PROF_EVENT(68); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 739 | if (sm->abits[a_off] == VGM_BYTE_VALID) { |
| 740 | /* Handle common case quickly. */ |
| 741 | return; |
| 742 | } else { |
| 743 | /* Slow but general case. */ |
njn | c2699f6 | 2003-09-05 23:29:33 +0000 | [diff] [blame] | 744 | ac_ACCESS1_SLOWLY(a, isWrite); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 745 | } |
| 746 | # endif |
| 747 | } |
| 748 | |
njn | 9fb73db | 2005-03-27 01:55:21 +0000 | [diff] [blame] | 749 | VGA_REGPARM(1) |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 750 | static void ach_LOAD4 ( Addr a ) |
njn | c2699f6 | 2003-09-05 23:29:33 +0000 | [diff] [blame] | 751 | { |
| 752 | ac_helperc_ACCESS4 ( a, /*isWrite*/False ); |
| 753 | } |
njn | 9fb73db | 2005-03-27 01:55:21 +0000 | [diff] [blame] | 754 | VGA_REGPARM(1) |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 755 | static void ach_STORE4 ( Addr a ) |
njn | c2699f6 | 2003-09-05 23:29:33 +0000 | [diff] [blame] | 756 | { |
| 757 | ac_helperc_ACCESS4 ( a, /*isWrite*/True ); |
| 758 | } |
| 759 | |
njn | 9fb73db | 2005-03-27 01:55:21 +0000 | [diff] [blame] | 760 | VGA_REGPARM(1) |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 761 | static void ach_LOAD2 ( Addr a ) |
njn | c2699f6 | 2003-09-05 23:29:33 +0000 | [diff] [blame] | 762 | { |
| 763 | ac_helperc_ACCESS2 ( a, /*isWrite*/False ); |
| 764 | } |
njn | 9fb73db | 2005-03-27 01:55:21 +0000 | [diff] [blame] | 765 | VGA_REGPARM(1) |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 766 | static void ach_STORE2 ( Addr a ) |
njn | c2699f6 | 2003-09-05 23:29:33 +0000 | [diff] [blame] | 767 | { |
| 768 | ac_helperc_ACCESS2 ( a, /*isWrite*/True ); |
| 769 | } |
| 770 | |
njn | 9fb73db | 2005-03-27 01:55:21 +0000 | [diff] [blame] | 771 | VGA_REGPARM(1) |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 772 | static void ach_LOAD1 ( Addr a ) |
njn | c2699f6 | 2003-09-05 23:29:33 +0000 | [diff] [blame] | 773 | { |
| 774 | ac_helperc_ACCESS1 ( a, /*isWrite*/False ); |
| 775 | } |
njn | 9fb73db | 2005-03-27 01:55:21 +0000 | [diff] [blame] | 776 | VGA_REGPARM(1) |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 777 | static void ach_STORE1 ( Addr a ) |
njn | c2699f6 | 2003-09-05 23:29:33 +0000 | [diff] [blame] | 778 | { |
| 779 | ac_helperc_ACCESS1 ( a, /*isWrite*/True ); |
| 780 | } |
| 781 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 782 | |
| 783 | /*------------------------------------------------------------*/ |
| 784 | /*--- Fallback functions to handle cases that the above ---*/ |
njn | c2699f6 | 2003-09-05 23:29:33 +0000 | [diff] [blame] | 785 | /*--- ac_helperc_ACCESS{1,2,4} can't manage. ---*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 786 | /*------------------------------------------------------------*/ |
| 787 | |
njn | c2699f6 | 2003-09-05 23:29:33 +0000 | [diff] [blame] | 788 | static void ac_ACCESS4_SLOWLY ( Addr a, Bool isWrite ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 789 | { |
| 790 | Bool a0ok, a1ok, a2ok, a3ok; |
| 791 | |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 792 | PROF_EVENT(76); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 793 | |
| 794 | /* First establish independently the addressibility of the 4 bytes |
| 795 | involved. */ |
| 796 | a0ok = get_abit(a+0) == VGM_BIT_VALID; |
| 797 | a1ok = get_abit(a+1) == VGM_BIT_VALID; |
| 798 | a2ok = get_abit(a+2) == VGM_BIT_VALID; |
| 799 | a3ok = get_abit(a+3) == VGM_BIT_VALID; |
| 800 | |
| 801 | /* Now distinguish 3 cases */ |
| 802 | |
| 803 | /* Case 1: the address is completely valid, so: |
| 804 | - no addressing error |
| 805 | */ |
| 806 | if (a0ok && a1ok && a2ok && a3ok) { |
| 807 | return; |
| 808 | } |
| 809 | |
| 810 | /* Case 2: the address is completely invalid. |
| 811 | - emit addressing error |
| 812 | */ |
| 813 | /* VG_(printf)("%p (%d %d %d %d)\n", a, a0ok, a1ok, a2ok, a3ok); */ |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 814 | if (!MAC_(clo_partial_loads_ok) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 815 | || ((a & 3) != 0) |
| 816 | || (!a0ok && !a1ok && !a2ok && !a3ok)) { |
sewardj | 76754cf | 2005-03-14 00:14:04 +0000 | [diff] [blame] | 817 | MAC_(record_address_error)( VG_(get_running_tid)(), a, 4, isWrite ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 818 | return; |
| 819 | } |
| 820 | |
| 821 | /* Case 3: the address is partially valid. |
| 822 | - no addressing error |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 823 | Case 3 is only allowed if MAC_(clo_partial_loads_ok) is True |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 824 | (which is the default), and the address is 4-aligned. |
| 825 | If not, Case 2 will have applied. |
| 826 | */ |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 827 | tl_assert(MAC_(clo_partial_loads_ok)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 828 | { |
| 829 | return; |
| 830 | } |
| 831 | } |
| 832 | |
njn | c2699f6 | 2003-09-05 23:29:33 +0000 | [diff] [blame] | 833 | static void ac_ACCESS2_SLOWLY ( Addr a, Bool isWrite ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 834 | { |
| 835 | /* Check the address for validity. */ |
| 836 | Bool aerr = False; |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 837 | PROF_EVENT(77); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 838 | |
| 839 | if (get_abit(a+0) != VGM_BIT_VALID) aerr = True; |
| 840 | if (get_abit(a+1) != VGM_BIT_VALID) aerr = True; |
| 841 | |
| 842 | /* If an address error has happened, report it. */ |
| 843 | if (aerr) { |
sewardj | 76754cf | 2005-03-14 00:14:04 +0000 | [diff] [blame] | 844 | MAC_(record_address_error)( VG_(get_running_tid)(), a, 2, isWrite ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 845 | } |
| 846 | } |
| 847 | |
njn | c2699f6 | 2003-09-05 23:29:33 +0000 | [diff] [blame] | 848 | static void ac_ACCESS1_SLOWLY ( Addr a, Bool isWrite) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 849 | { |
| 850 | /* Check the address for validity. */ |
| 851 | Bool aerr = False; |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 852 | PROF_EVENT(78); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 853 | |
| 854 | if (get_abit(a+0) != VGM_BIT_VALID) aerr = True; |
| 855 | |
| 856 | /* If an address error has happened, report it. */ |
| 857 | if (aerr) { |
sewardj | 76754cf | 2005-03-14 00:14:04 +0000 | [diff] [blame] | 858 | MAC_(record_address_error)( VG_(get_running_tid)(), a, 1, isWrite ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 859 | } |
| 860 | } |
| 861 | |
| 862 | |
| 863 | /* --------------------------------------------------------------------- |
| 864 | FPU load and store checks, called from generated code. |
| 865 | ------------------------------------------------------------------ */ |
| 866 | |
sewardj | 5686735 | 2003-10-12 10:27:06 +0000 | [diff] [blame] | 867 | static |
nethercote | 928a5f7 | 2004-11-03 18:10:37 +0000 | [diff] [blame] | 868 | void ac_fpu_ACCESS_check ( Addr addr, SizeT size, Bool isWrite ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 869 | { |
| 870 | /* Ensure the read area is both addressible and valid (ie, |
| 871 | readable). If there's an address error, don't report a value |
| 872 | error too; but if there isn't an address error, check for a |
| 873 | value error. |
| 874 | |
| 875 | Try to be reasonably fast on the common case; wimp out and defer |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 876 | to ac_fpu_ACCESS_check_SLOWLY for everything else. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 877 | |
| 878 | AcSecMap* sm; |
| 879 | UInt sm_off, a_off; |
| 880 | Addr addr4; |
| 881 | |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 882 | PROF_EVENT(90); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 883 | |
| 884 | # ifdef VG_DEBUG_MEMORY |
njn | c2699f6 | 2003-09-05 23:29:33 +0000 | [diff] [blame] | 885 | ac_fpu_ACCESS_check_SLOWLY ( addr, size, isWrite ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 886 | # else |
| 887 | |
| 888 | if (size == 4) { |
njn | be91aae | 2005-03-27 01:42:41 +0000 | [diff] [blame] | 889 | if (!VG_IS_4_ALIGNED(addr)) goto slow4; |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 890 | PROF_EVENT(91); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 891 | /* Properly aligned. */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 892 | sm = primary_map[PM_IDX(addr)]; |
| 893 | sm_off = SM_OFF(addr); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 894 | a_off = sm_off >> 3; |
| 895 | if (sm->abits[a_off] != VGM_BYTE_VALID) goto slow4; |
| 896 | /* Properly aligned and addressible. */ |
| 897 | return; |
| 898 | slow4: |
njn | c2699f6 | 2003-09-05 23:29:33 +0000 | [diff] [blame] | 899 | ac_fpu_ACCESS_check_SLOWLY ( addr, 4, isWrite ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 900 | return; |
| 901 | } |
| 902 | |
| 903 | if (size == 8) { |
njn | be91aae | 2005-03-27 01:42:41 +0000 | [diff] [blame] | 904 | if (!VG_IS_4_ALIGNED(addr)) goto slow8; |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 905 | PROF_EVENT(92); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 906 | /* Properly aligned. Do it in two halves. */ |
| 907 | addr4 = addr + 4; |
| 908 | /* First half. */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 909 | sm = primary_map[PM_IDX(addr)]; |
| 910 | sm_off = SM_OFF(addr); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 911 | a_off = sm_off >> 3; |
| 912 | if (sm->abits[a_off] != VGM_BYTE_VALID) goto slow8; |
| 913 | /* First half properly aligned and addressible. */ |
| 914 | /* Second half. */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 915 | sm = primary_map[PM_IDX(addr4)]; |
| 916 | sm_off = SM_OFF(addr4); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 917 | a_off = sm_off >> 3; |
| 918 | if (sm->abits[a_off] != VGM_BYTE_VALID) goto slow8; |
| 919 | /* Second half properly aligned and addressible. */ |
| 920 | /* Both halves properly aligned and addressible. */ |
| 921 | return; |
| 922 | slow8: |
njn | c2699f6 | 2003-09-05 23:29:33 +0000 | [diff] [blame] | 923 | ac_fpu_ACCESS_check_SLOWLY ( addr, 8, isWrite ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 924 | return; |
| 925 | } |
| 926 | |
| 927 | /* Can't be bothered to huff'n'puff to make these (allegedly) rare |
| 928 | cases go quickly. */ |
| 929 | if (size == 2) { |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 930 | PROF_EVENT(93); |
njn | c2699f6 | 2003-09-05 23:29:33 +0000 | [diff] [blame] | 931 | ac_fpu_ACCESS_check_SLOWLY ( addr, 2, isWrite ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 932 | return; |
| 933 | } |
| 934 | |
jseward | fca6018 | 2004-01-04 23:30:55 +0000 | [diff] [blame] | 935 | if (size == 16 || size == 10 || size == 28 || size == 108 || size == 512) { |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 936 | PROF_EVENT(94); |
njn | c2699f6 | 2003-09-05 23:29:33 +0000 | [diff] [blame] | 937 | ac_fpu_ACCESS_check_SLOWLY ( addr, size, isWrite ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 938 | return; |
| 939 | } |
| 940 | |
| 941 | VG_(printf)("size is %d\n", size); |
njn | 6799325 | 2004-11-22 18:02:32 +0000 | [diff] [blame] | 942 | VG_(tool_panic)("fpu_ACCESS_check: unhandled size"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 943 | # endif |
| 944 | } |
| 945 | |
njn | 9fb73db | 2005-03-27 01:55:21 +0000 | [diff] [blame] | 946 | VGA_REGPARM(2) |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 947 | static void ach_LOADN ( Addr addr, SizeT size ) |
njn | c2699f6 | 2003-09-05 23:29:33 +0000 | [diff] [blame] | 948 | { |
| 949 | ac_fpu_ACCESS_check ( addr, size, /*isWrite*/False ); |
| 950 | } |
| 951 | |
njn | 9fb73db | 2005-03-27 01:55:21 +0000 | [diff] [blame] | 952 | VGA_REGPARM(2) |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 953 | static void ach_STOREN ( Addr addr, SizeT size ) |
njn | c2699f6 | 2003-09-05 23:29:33 +0000 | [diff] [blame] | 954 | { |
| 955 | ac_fpu_ACCESS_check ( addr, size, /*isWrite*/True ); |
| 956 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 957 | |
sewardj | d62e03b | 2004-12-10 12:01:14 +0000 | [diff] [blame] | 958 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 959 | /* --------------------------------------------------------------------- |
| 960 | Slow, general cases for FPU access checks. |
| 961 | ------------------------------------------------------------------ */ |
| 962 | |
nethercote | 928a5f7 | 2004-11-03 18:10:37 +0000 | [diff] [blame] | 963 | void ac_fpu_ACCESS_check_SLOWLY ( Addr addr, SizeT size, Bool isWrite ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 964 | { |
| 965 | Int i; |
| 966 | Bool aerr = False; |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 967 | PROF_EVENT(100); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 968 | for (i = 0; i < size; i++) { |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 969 | PROF_EVENT(101); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 970 | if (get_abit(addr+i) != VGM_BIT_VALID) |
| 971 | aerr = True; |
| 972 | } |
| 973 | |
| 974 | if (aerr) { |
sewardj | 76754cf | 2005-03-14 00:14:04 +0000 | [diff] [blame] | 975 | MAC_(record_address_error)( VG_(get_running_tid)(), addr, size, isWrite ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 976 | } |
| 977 | } |
| 978 | |
| 979 | |
| 980 | /*------------------------------------------------------------*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 981 | /*--- Our instrumenter ---*/ |
| 982 | /*------------------------------------------------------------*/ |
| 983 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 984 | static IRBB* ac_instrument(IRBB* bb_in, VexGuestLayout* layout, |
| 985 | IRType gWordTy, IRType hWordTy ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 986 | { |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 987 | Int i, hsz; |
| 988 | IRStmt* st; |
| 989 | IRExpr* data; |
| 990 | IRExpr* aexpr; |
| 991 | IRExpr* guard; |
| 992 | IRDirty* di; |
| 993 | Bool isLoad; |
sewardj | d54babf | 2005-03-21 00:55:49 +0000 | [diff] [blame] | 994 | IRBB* bb; |
| 995 | |
| 996 | if (gWordTy != hWordTy) { |
| 997 | /* We don't currently support this case. */ |
| 998 | VG_(tool_panic)("host/guest word size mismatch"); |
| 999 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1000 | |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 1001 | /* Set up BB */ |
sewardj | d54babf | 2005-03-21 00:55:49 +0000 | [diff] [blame] | 1002 | bb = emptyIRBB(); |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 1003 | bb->tyenv = dopyIRTypeEnv(bb_in->tyenv); |
| 1004 | bb->next = dopyIRExpr(bb_in->next); |
| 1005 | bb->jumpkind = bb_in->jumpkind; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1006 | |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 1007 | /* No loads to consider in ->next. */ |
sewardj | 710d6c2 | 2005-03-20 18:55:15 +0000 | [diff] [blame] | 1008 | tl_assert(isIRAtom(bb_in->next)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1009 | |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 1010 | for (i = 0; i < bb_in->stmts_used; i++) { |
| 1011 | st = bb_in->stmts[i]; |
sewardj | 21dc345 | 2005-03-21 00:27:41 +0000 | [diff] [blame] | 1012 | tl_assert(st); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1013 | |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 1014 | /* Examine each stmt in turn to figure out if it needs to be |
| 1015 | preceded by a memory access check. If so, collect up the |
| 1016 | relevant pieces of information. */ |
| 1017 | hsz = 0; |
| 1018 | aexpr = NULL; |
| 1019 | guard = NULL; |
| 1020 | isLoad = True; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1021 | |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 1022 | switch (st->tag) { |
| 1023 | |
| 1024 | case Ist_Tmp: |
| 1025 | data = st->Ist.Tmp.data; |
| 1026 | if (data->tag == Iex_LDle) { |
| 1027 | aexpr = data->Iex.LDle.addr; |
| 1028 | hsz = sizeofIRType(data->Iex.LDle.ty); |
| 1029 | isLoad = True; |
| 1030 | } |
| 1031 | break; |
| 1032 | |
| 1033 | case Ist_STle: |
| 1034 | data = st->Ist.STle.data; |
| 1035 | aexpr = st->Ist.STle.addr; |
sewardj | 710d6c2 | 2005-03-20 18:55:15 +0000 | [diff] [blame] | 1036 | tl_assert(isIRAtom(data)); |
| 1037 | tl_assert(isIRAtom(aexpr)); |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 1038 | hsz = sizeofIRType(typeOfIRExpr(bb_in->tyenv, data)); |
| 1039 | isLoad = False; |
sewardj | 08ccc5c | 2005-03-17 03:17:54 +0000 | [diff] [blame] | 1040 | break; |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 1041 | |
| 1042 | case Ist_Put: |
sewardj | 710d6c2 | 2005-03-20 18:55:15 +0000 | [diff] [blame] | 1043 | tl_assert(isIRAtom(st->Ist.Put.data)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1044 | break; |
| 1045 | |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 1046 | case Ist_PutI: |
sewardj | 710d6c2 | 2005-03-20 18:55:15 +0000 | [diff] [blame] | 1047 | tl_assert(isIRAtom(st->Ist.PutI.ix)); |
| 1048 | tl_assert(isIRAtom(st->Ist.PutI.data)); |
njn | c2699f6 | 2003-09-05 23:29:33 +0000 | [diff] [blame] | 1049 | break; |
| 1050 | |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 1051 | case Ist_Exit: |
sewardj | 710d6c2 | 2005-03-20 18:55:15 +0000 | [diff] [blame] | 1052 | tl_assert(isIRAtom(st->Ist.Exit.guard)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1053 | break; |
| 1054 | |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 1055 | case Ist_Dirty: |
| 1056 | if (st->Ist.Dirty.details->mFx != Ifx_None) { |
| 1057 | /* We classify Ifx_Modify as a load. */ |
| 1058 | isLoad = st->Ist.Dirty.details->mFx != Ifx_Write; |
| 1059 | hsz = st->Ist.Dirty.details->mSize; |
| 1060 | aexpr = st->Ist.Dirty.details->mAddr; |
| 1061 | guard = st->Ist.Dirty.details->guard; |
sewardj | 710d6c2 | 2005-03-20 18:55:15 +0000 | [diff] [blame] | 1062 | tl_assert(isIRAtom(aexpr)); |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 1063 | } |
| 1064 | break; |
sewardj | e3891fa | 2003-06-15 03:13:48 +0000 | [diff] [blame] | 1065 | |
sewardj | 21dc345 | 2005-03-21 00:27:41 +0000 | [diff] [blame] | 1066 | case Ist_NoOp: |
sewardj | 29faa50 | 2005-03-16 18:20:21 +0000 | [diff] [blame] | 1067 | case Ist_IMark: |
sewardj | bd598e1 | 2005-01-07 12:10:21 +0000 | [diff] [blame] | 1068 | case Ist_MFence: |
| 1069 | break; |
| 1070 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1071 | default: |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 1072 | VG_(printf)("\n"); |
| 1073 | ppIRStmt(st); |
| 1074 | VG_(printf)("\n"); |
| 1075 | VG_(tool_panic)("addrcheck: unhandled IRStmt"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1076 | } |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 1077 | |
| 1078 | /* If needed, add a helper call. */ |
| 1079 | if (aexpr) { |
| 1080 | tl_assert(hsz > 0); |
| 1081 | switch (hsz) { |
| 1082 | case 4: |
| 1083 | if (isLoad) |
| 1084 | di = unsafeIRDirty_0_N( 1, "ach_LOAD4", &ach_LOAD4, |
| 1085 | mkIRExprVec_1(aexpr)); |
| 1086 | else |
| 1087 | di = unsafeIRDirty_0_N( 1, "ach_STORE4", &ach_STORE4, |
| 1088 | mkIRExprVec_1(aexpr)); |
| 1089 | break; |
| 1090 | case 2: |
| 1091 | if (isLoad) |
| 1092 | di = unsafeIRDirty_0_N( 1, "ach_LOAD2", &ach_LOAD2, |
| 1093 | mkIRExprVec_1(aexpr)); |
| 1094 | else |
| 1095 | di = unsafeIRDirty_0_N( 1, "ach_STORE2", &ach_STORE2, |
| 1096 | mkIRExprVec_1(aexpr)); |
| 1097 | break; |
| 1098 | case 1: |
| 1099 | if (isLoad) |
| 1100 | di = unsafeIRDirty_0_N( 1, "ach_LOAD1", &ach_LOAD1, |
| 1101 | mkIRExprVec_1(aexpr)); |
| 1102 | else |
| 1103 | di = unsafeIRDirty_0_N( 1, "ach_STORE1", &ach_STORE1, |
| 1104 | mkIRExprVec_1(aexpr)); |
| 1105 | break; |
| 1106 | default: |
| 1107 | if (isLoad) |
| 1108 | di = unsafeIRDirty_0_N( |
| 1109 | 2, "ach_LOADN", &ach_LOADN, |
| 1110 | mkIRExprVec_2(aexpr,mkIRExpr_HWord(hsz))); |
| 1111 | else |
| 1112 | di = unsafeIRDirty_0_N( |
| 1113 | 2, "ach_STOREN", &ach_STOREN, |
| 1114 | mkIRExprVec_2(aexpr,mkIRExpr_HWord(hsz))); |
| 1115 | break; |
| 1116 | } |
| 1117 | |
| 1118 | /* If the call has arisen as a result of a dirty helper which |
| 1119 | references memory, we need to inherit the guard from the |
| 1120 | dirty helper. */ |
| 1121 | if (guard) |
| 1122 | di->guard = dopyIRExpr(guard); |
| 1123 | |
| 1124 | /* emit the helper call */ |
| 1125 | addStmtToIRBB( bb, IRStmt_Dirty(di) ); |
| 1126 | |
| 1127 | } |
| 1128 | |
| 1129 | /* And finally, copy the expr itself to the output. */ |
sewardj | bd598e1 | 2005-01-07 12:10:21 +0000 | [diff] [blame] | 1130 | addStmtToIRBB( bb, st ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 1133 | return bb; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1137 | /*------------------------------------------------------------*/ |
| 1138 | /*--- Detecting leaked (unreachable) malloc'd blocks. ---*/ |
| 1139 | /*------------------------------------------------------------*/ |
| 1140 | |
sewardj | a449568 | 2002-10-21 07:29:59 +0000 | [diff] [blame] | 1141 | /* For the memory leak detector, say whether an entire 64k chunk of |
| 1142 | address space is possibly in use, or not. If in doubt return |
| 1143 | True. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1144 | */ |
sewardj | a449568 | 2002-10-21 07:29:59 +0000 | [diff] [blame] | 1145 | static |
| 1146 | Bool ac_is_valid_64k_chunk ( UInt chunk_number ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1147 | { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1148 | tl_assert(chunk_number >= 0 && chunk_number < PRIMARY_SIZE); |
| 1149 | if (primary_map[chunk_number] == DSM_NOTADDR) { |
sewardj | a449568 | 2002-10-21 07:29:59 +0000 | [diff] [blame] | 1150 | /* Definitely not in use. */ |
| 1151 | return False; |
| 1152 | } else { |
| 1153 | return True; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | |
sewardj | a449568 | 2002-10-21 07:29:59 +0000 | [diff] [blame] | 1158 | /* For the memory leak detector, say whether or not a given word |
| 1159 | address is to be regarded as valid. */ |
| 1160 | static |
| 1161 | Bool ac_is_valid_address ( Addr a ) |
| 1162 | { |
| 1163 | UChar abits; |
njn | be91aae | 2005-03-27 01:42:41 +0000 | [diff] [blame] | 1164 | tl_assert(VG_IS_4_ALIGNED(a)); |
sewardj | a449568 | 2002-10-21 07:29:59 +0000 | [diff] [blame] | 1165 | abits = get_abits4_ALIGNED(a); |
| 1166 | if (abits == VGM_NIBBLE_VALID) { |
| 1167 | return True; |
| 1168 | } else { |
| 1169 | return False; |
| 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | |
nethercote | 996901a | 2004-08-03 13:29:09 +0000 | [diff] [blame] | 1174 | /* Leak detector for this tool. We don't actually do anything, merely |
sewardj | a449568 | 2002-10-21 07:29:59 +0000 | [diff] [blame] | 1175 | run the generic leak detector with suitable parameters for this |
nethercote | 996901a | 2004-08-03 13:29:09 +0000 | [diff] [blame] | 1176 | tool. */ |
njn | b8dca86 | 2005-03-14 02:42:44 +0000 | [diff] [blame] | 1177 | static void ac_detect_memory_leaks ( ThreadId tid, LeakCheckMode mode ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1178 | { |
njn | b8dca86 | 2005-03-14 02:42:44 +0000 | [diff] [blame] | 1179 | MAC_(do_detect_memory_leaks) ( tid, mode, ac_is_valid_64k_chunk, ac_is_valid_address ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | |
| 1183 | /* --------------------------------------------------------------------- |
| 1184 | Sanity check machinery (permanently engaged). |
| 1185 | ------------------------------------------------------------------ */ |
| 1186 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1187 | static Bool ac_cheap_sanity_check ( void ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1188 | { |
jseward | 9800fd3 | 2004-01-04 23:08:04 +0000 | [diff] [blame] | 1189 | /* nothing useful we can rapidly check */ |
| 1190 | return True; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1193 | static Bool ac_expensive_sanity_check ( void ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1194 | { |
| 1195 | Int i; |
| 1196 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1197 | #if 0 |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1198 | /* Make sure nobody changed the distinguished secondary. */ |
| 1199 | for (i = 0; i < 8192; i++) |
| 1200 | if (distinguished_secondary_map.abits[i] != VGM_BYTE_INVALID) |
| 1201 | return False; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1202 | #endif |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1203 | |
| 1204 | /* Make sure that the upper 3/4 of the primary map hasn't |
| 1205 | been messed with. */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1206 | for (i = PRIMARY_SIZE; i < PRIMARY_SIZE*4; i++) |
| 1207 | if (primary_map[i] != DSM_NOTADDR) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1208 | return False; |
| 1209 | |
| 1210 | return True; |
| 1211 | } |
| 1212 | |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 1213 | /*------------------------------------------------------------*/ |
| 1214 | /*--- Client requests ---*/ |
| 1215 | /*------------------------------------------------------------*/ |
| 1216 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1217 | static Bool ac_handle_client_request ( ThreadId tid, UWord* arg, UWord *ret ) |
sewardj | d8033d9 | 2002-12-08 22:16:58 +0000 | [diff] [blame] | 1218 | { |
sewardj | bf310d9 | 2002-12-28 13:09:57 +0000 | [diff] [blame] | 1219 | #define IGNORE(what) \ |
| 1220 | do { \ |
| 1221 | if (moans-- > 0) { \ |
| 1222 | VG_(message)(Vg_UserMsg, \ |
njn | 02bc4b8 | 2005-05-15 17:28:26 +0000 | [diff] [blame] | 1223 | "Warning: Addrcheck: ignoring '%s' request.", what); \ |
sewardj | bf310d9 | 2002-12-28 13:09:57 +0000 | [diff] [blame] | 1224 | VG_(message)(Vg_UserMsg, \ |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 1225 | " To honour this request, rerun with --tool=memcheck."); \ |
sewardj | bf310d9 | 2002-12-28 13:09:57 +0000 | [diff] [blame] | 1226 | } \ |
| 1227 | } while (0) |
| 1228 | |
sewardj | bf310d9 | 2002-12-28 13:09:57 +0000 | [diff] [blame] | 1229 | static Int moans = 3; |
sewardj | d8033d9 | 2002-12-08 22:16:58 +0000 | [diff] [blame] | 1230 | |
| 1231 | /* Overload memcheck client reqs */ |
njn | fc26ff9 | 2004-11-22 19:12:49 +0000 | [diff] [blame] | 1232 | if (!VG_IS_TOOL_USERREQ('M','C',arg[0]) |
sewardj | d62e03b | 2004-12-10 12:01:14 +0000 | [diff] [blame] | 1233 | && VG_USERREQ__MALLOCLIKE_BLOCK != arg[0] |
| 1234 | && VG_USERREQ__FREELIKE_BLOCK != arg[0] |
| 1235 | && VG_USERREQ__CREATE_MEMPOOL != arg[0] |
| 1236 | && VG_USERREQ__DESTROY_MEMPOOL != arg[0] |
| 1237 | && VG_USERREQ__MEMPOOL_ALLOC != arg[0] |
| 1238 | && VG_USERREQ__MEMPOOL_FREE != arg[0]) |
sewardj | d8033d9 | 2002-12-08 22:16:58 +0000 | [diff] [blame] | 1239 | return False; |
| 1240 | |
| 1241 | switch (arg[0]) { |
| 1242 | case VG_USERREQ__DO_LEAK_CHECK: |
njn | b8dca86 | 2005-03-14 02:42:44 +0000 | [diff] [blame] | 1243 | ac_detect_memory_leaks(tid, arg[1] ? LC_Summary : LC_Full); |
sewardj | d8033d9 | 2002-12-08 22:16:58 +0000 | [diff] [blame] | 1244 | *ret = 0; /* return value is meaningless */ |
| 1245 | break; |
| 1246 | |
sewardj | bf310d9 | 2002-12-28 13:09:57 +0000 | [diff] [blame] | 1247 | /* Ignore these */ |
sewardj | d8033d9 | 2002-12-08 22:16:58 +0000 | [diff] [blame] | 1248 | case VG_USERREQ__CHECK_WRITABLE: /* check writable */ |
sewardj | bf310d9 | 2002-12-28 13:09:57 +0000 | [diff] [blame] | 1249 | IGNORE("VALGRIND_CHECK_WRITABLE"); |
| 1250 | return False; |
sewardj | d8033d9 | 2002-12-08 22:16:58 +0000 | [diff] [blame] | 1251 | case VG_USERREQ__CHECK_READABLE: /* check readable */ |
sewardj | bf310d9 | 2002-12-28 13:09:57 +0000 | [diff] [blame] | 1252 | IGNORE("VALGRIND_CHECK_READABLE"); |
| 1253 | return False; |
sewardj | d8033d9 | 2002-12-08 22:16:58 +0000 | [diff] [blame] | 1254 | case VG_USERREQ__MAKE_NOACCESS: /* make no access */ |
sewardj | bf310d9 | 2002-12-28 13:09:57 +0000 | [diff] [blame] | 1255 | IGNORE("VALGRIND_MAKE_NOACCESS"); |
| 1256 | return False; |
sewardj | d8033d9 | 2002-12-08 22:16:58 +0000 | [diff] [blame] | 1257 | case VG_USERREQ__MAKE_WRITABLE: /* make writable */ |
sewardj | bf310d9 | 2002-12-28 13:09:57 +0000 | [diff] [blame] | 1258 | IGNORE("VALGRIND_MAKE_WRITABLE"); |
| 1259 | return False; |
sewardj | d8033d9 | 2002-12-08 22:16:58 +0000 | [diff] [blame] | 1260 | case VG_USERREQ__MAKE_READABLE: /* make readable */ |
sewardj | bf310d9 | 2002-12-28 13:09:57 +0000 | [diff] [blame] | 1261 | IGNORE("VALGRIND_MAKE_READABLE"); |
| 1262 | return False; |
sewardj | d8033d9 | 2002-12-08 22:16:58 +0000 | [diff] [blame] | 1263 | case VG_USERREQ__DISCARD: /* discard */ |
sewardj | bf310d9 | 2002-12-28 13:09:57 +0000 | [diff] [blame] | 1264 | IGNORE("VALGRIND_CHECK_DISCARD"); |
| 1265 | return False; |
sewardj | d8033d9 | 2002-12-08 22:16:58 +0000 | [diff] [blame] | 1266 | |
| 1267 | default: |
nethercote | d1b64b2 | 2004-11-04 18:22:28 +0000 | [diff] [blame] | 1268 | if (MAC_(handle_common_client_requests)(tid, arg, ret )) { |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 1269 | return True; |
| 1270 | } else { |
| 1271 | VG_(message)(Vg_UserMsg, |
nethercote | d1b64b2 | 2004-11-04 18:22:28 +0000 | [diff] [blame] | 1272 | "Warning: unknown addrcheck client request code %llx", |
| 1273 | (ULong)arg[0]); |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 1274 | return False; |
| 1275 | } |
sewardj | d8033d9 | 2002-12-08 22:16:58 +0000 | [diff] [blame] | 1276 | } |
| 1277 | return True; |
sewardj | bf310d9 | 2002-12-28 13:09:57 +0000 | [diff] [blame] | 1278 | |
| 1279 | #undef IGNORE |
sewardj | d8033d9 | 2002-12-08 22:16:58 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1282 | /*------------------------------------------------------------*/ |
| 1283 | /*--- Setup ---*/ |
| 1284 | /*------------------------------------------------------------*/ |
| 1285 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1286 | static Bool ac_process_cmd_line_option(Char* arg) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1287 | { |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1288 | return MAC_(process_common_cmd_line_option)(arg); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1291 | static void ac_print_usage(void) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1292 | { |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1293 | MAC_(print_common_usage)(); |
| 1294 | } |
| 1295 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1296 | static void ac_print_debug_usage(void) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1297 | { |
| 1298 | MAC_(print_common_debug_usage)(); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | |
| 1302 | /*------------------------------------------------------------*/ |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1303 | /*--- Setup and finalisation ---*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1304 | /*------------------------------------------------------------*/ |
| 1305 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1306 | static void ac_post_clo_init ( void ) |
| 1307 | { |
| 1308 | } |
| 1309 | |
| 1310 | static void ac_fini ( Int exitcode ) |
| 1311 | { |
| 1312 | MAC_(common_fini)( ac_detect_memory_leaks ); |
| 1313 | } |
| 1314 | |
| 1315 | static void ac_pre_clo_init(void) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1316 | { |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1317 | VG_(details_name) ("Addrcheck"); |
| 1318 | VG_(details_version) (NULL); |
| 1319 | VG_(details_description) ("a fine-grained address checker"); |
| 1320 | VG_(details_copyright_author)( |
njn | 5361242 | 2005-03-12 16:22:54 +0000 | [diff] [blame] | 1321 | "Copyright (C) 2002-2005, and GNU GPL'd, by Julian Seward et al."); |
nethercote | 421281e | 2003-11-20 16:20:55 +0000 | [diff] [blame] | 1322 | VG_(details_bug_reports_to) (VG_BUGS_TO); |
sewardj | 78210aa | 2002-12-01 02:55:46 +0000 | [diff] [blame] | 1323 | VG_(details_avg_translation_sizeB) ( 135 ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1324 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1325 | VG_(basic_tool_funcs) (ac_post_clo_init, |
| 1326 | ac_instrument, |
| 1327 | ac_fini); |
njn | 8a97c6d | 2005-03-31 04:37:24 +0000 | [diff] [blame] | 1328 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1329 | VG_(needs_core_errors) (); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1330 | VG_(needs_tool_errors) (MAC_(eq_Error), |
| 1331 | ac_pp_Error, |
| 1332 | MAC_(update_extra), |
| 1333 | MAC_(shared_recognised_suppression), |
| 1334 | MAC_(read_extra_suppression_info), |
| 1335 | MAC_(error_matches_suppression), |
| 1336 | MAC_(get_error_name), |
| 1337 | MAC_(print_extra_suppression_info)); |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1338 | VG_(needs_libc_freeres) (); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1339 | VG_(needs_command_line_options)(MAC_(process_common_cmd_line_option), |
| 1340 | MAC_(print_common_usage), |
| 1341 | MAC_(print_common_debug_usage)); |
| 1342 | VG_(needs_client_requests) (ac_handle_client_request); |
| 1343 | VG_(needs_sanity_checks) (ac_cheap_sanity_check, |
| 1344 | ac_expensive_sanity_check); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1345 | VG_(needs_shadow_memory) (); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1346 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1347 | VG_(malloc_funcs) (MAC_(malloc), |
| 1348 | MAC_(__builtin_new), |
| 1349 | MAC_(__builtin_vec_new), |
| 1350 | MAC_(memalign), |
| 1351 | MAC_(calloc), |
| 1352 | MAC_(free), |
| 1353 | MAC_(__builtin_delete), |
| 1354 | MAC_(__builtin_vec_delete), |
| 1355 | MAC_(realloc), |
| 1356 | MAC_MALLOC_REDZONE_SZB ); |
njn | 8a97c6d | 2005-03-31 04:37:24 +0000 | [diff] [blame] | 1357 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1358 | MAC_( new_mem_heap) = & ac_new_mem_heap; |
| 1359 | MAC_( ban_mem_heap) = & ac_make_noaccess; |
| 1360 | MAC_(copy_mem_heap) = & ac_copy_address_range_state; |
| 1361 | MAC_( die_mem_heap) = & ac_make_noaccess; |
sewardj | ecf8e10 | 2003-07-12 12:11:39 +0000 | [diff] [blame] | 1362 | MAC_(check_noaccess) = & ac_check_noaccess; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1363 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1364 | VG_(track_new_mem_startup) ( & ac_new_mem_startup ); |
| 1365 | VG_(track_new_mem_stack_signal)( & ac_make_accessible ); |
| 1366 | VG_(track_new_mem_brk) ( & ac_make_accessible ); |
| 1367 | VG_(track_new_mem_mmap) ( & ac_new_mem_mmap ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1368 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1369 | VG_(track_copy_mem_remap) ( & ac_copy_address_range_state ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1370 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1371 | VG_(track_die_mem_stack_signal)( & ac_make_noaccess ); |
| 1372 | VG_(track_die_mem_brk) ( & ac_make_noaccess ); |
| 1373 | VG_(track_die_mem_munmap) ( & ac_make_noaccess ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1374 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1375 | VG_(track_new_mem_stack_4) ( & MAC_(new_mem_stack_4) ); |
| 1376 | VG_(track_new_mem_stack_8) ( & MAC_(new_mem_stack_8) ); |
| 1377 | VG_(track_new_mem_stack_12) ( & MAC_(new_mem_stack_12) ); |
| 1378 | VG_(track_new_mem_stack_16) ( & MAC_(new_mem_stack_16) ); |
| 1379 | VG_(track_new_mem_stack_32) ( & MAC_(new_mem_stack_32) ); |
| 1380 | VG_(track_new_mem_stack) ( & MAC_(new_mem_stack) ); |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 1381 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1382 | VG_(track_die_mem_stack_4) ( & MAC_(die_mem_stack_4) ); |
| 1383 | VG_(track_die_mem_stack_8) ( & MAC_(die_mem_stack_8) ); |
| 1384 | VG_(track_die_mem_stack_12) ( & MAC_(die_mem_stack_12) ); |
| 1385 | VG_(track_die_mem_stack_16) ( & MAC_(die_mem_stack_16) ); |
| 1386 | VG_(track_die_mem_stack_32) ( & MAC_(die_mem_stack_32) ); |
| 1387 | VG_(track_die_mem_stack) ( & MAC_(die_mem_stack) ); |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 1388 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1389 | VG_(track_ban_mem_stack) ( & ac_make_noaccess ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1390 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1391 | VG_(track_pre_mem_read) ( & ac_check_is_readable ); |
| 1392 | VG_(track_pre_mem_read_asciiz) ( & ac_check_is_readable_asciiz ); |
| 1393 | VG_(track_pre_mem_write) ( & ac_check_is_writable ); |
| 1394 | VG_(track_post_mem_write) ( & ac_post_mem_write ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1395 | |
njn | 31066fd | 2005-03-26 00:42:02 +0000 | [diff] [blame] | 1396 | VG_(register_profile_event) ( VgpSetMem, "set-mem-perms" ); |
| 1397 | VG_(register_profile_event) ( VgpCheckMem, "check-mem-perms" ); |
| 1398 | VG_(register_profile_event) ( VgpESPAdj, "adjust-ESP" ); |
njn | d04b7c6 | 2002-10-03 14:05:52 +0000 | [diff] [blame] | 1399 | |
| 1400 | init_shadow_memory(); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1401 | MAC_(common_pre_clo_init)(); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1404 | VG_DETERMINE_INTERFACE_VERSION(ac_pre_clo_init, 1./8) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1405 | |
| 1406 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1407 | /*--------------------------------------------------------------------*/ |
njn25 | cac76cb | 2002-09-23 11:21:57 +0000 | [diff] [blame] | 1408 | /*--- end ac_main.c ---*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1409 | /*--------------------------------------------------------------------*/ |