sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
sewardj | 55f9d1a | 2005-04-25 11:11:44 +0000 | [diff] [blame] | 3 | /*--- The address space manager: segment initialisation and ---*/ |
| 4 | /*--- tracking, stack operations ---*/ |
| 5 | /*--- aspacemgr.c ---*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6 | /*--------------------------------------------------------------------*/ |
| 7 | |
| 8 | /* |
njn | b9c427c | 2004-12-01 14:14:42 +0000 | [diff] [blame] | 9 | This file is part of Valgrind, a dynamic binary instrumentation |
| 10 | framework. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 11 | |
njn | 5361242 | 2005-03-12 16:22:54 +0000 | [diff] [blame] | 12 | Copyright (C) 2000-2005 Julian Seward |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 13 | jseward@acm.org |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 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 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 30 | The GNU General Public License is contained in the file COPYING. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 31 | */ |
| 32 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 33 | #include "pub_core_basics.h" |
| 34 | #include "pub_core_threadstate.h" |
sewardj | 55f9d1a | 2005-04-25 11:11:44 +0000 | [diff] [blame] | 35 | #include "pub_core_aspacemgr.h" |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 36 | #include "pub_core_libcbase.h" |
njn | 132bfcc | 2005-06-04 19:16:06 +0000 | [diff] [blame] | 37 | #include "pub_core_libcassert.h" |
njn | ae7359b | 2005-06-19 21:10:42 +0000 | [diff] [blame] | 38 | #include "pub_core_libcfile.h" |
njn | e9befc6 | 2005-06-11 15:51:30 +0000 | [diff] [blame] | 39 | #include "pub_core_libcmman.h" |
njn | 36a20fa | 2005-06-03 03:08:39 +0000 | [diff] [blame] | 40 | #include "pub_core_libcprint.h" |
njn | f39e9a3 | 2005-06-12 02:43:17 +0000 | [diff] [blame] | 41 | #include "pub_core_libcproc.h" |
njn | af1d7df | 2005-06-11 01:31:52 +0000 | [diff] [blame] | 42 | #include "pub_core_mallocfree.h" |
njn | 2024234 | 2005-05-16 23:31:24 +0000 | [diff] [blame] | 43 | #include "pub_core_options.h" |
njn | 9abd608 | 2005-06-17 21:31:45 +0000 | [diff] [blame] | 44 | #include "pub_core_syscall.h" |
njn | 43b9a8a | 2005-05-10 04:37:01 +0000 | [diff] [blame] | 45 | #include "pub_core_tooliface.h" |
njn | 8bddf58 | 2005-05-13 23:40:55 +0000 | [diff] [blame] | 46 | #include "pub_core_transtab.h" |
njn | 3c660b6 | 2005-05-13 22:18:47 +0000 | [diff] [blame] | 47 | #include "vki_unistd.h" |
sewardj | 55f9d1a | 2005-04-25 11:11:44 +0000 | [diff] [blame] | 48 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 49 | |
sewardj | a449568 | 2002-10-21 07:29:59 +0000 | [diff] [blame] | 50 | /* Define to debug the memory-leak-detector. */ |
| 51 | /* #define VG_DEBUG_LEAKCHECK */ |
| 52 | |
sewardj | e517b80 | 2005-02-16 01:58:51 +0000 | [diff] [blame] | 53 | static const Bool mem_debug = False; |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 54 | |
| 55 | /*--------------------------------------------------------------*/ |
njn | 04e1698 | 2005-05-31 00:23:43 +0000 | [diff] [blame] | 56 | /*--- Basic globals about the address space. ---*/ |
| 57 | /*--------------------------------------------------------------*/ |
| 58 | |
| 59 | /* Client address space, lowest to highest (see top of ume.c) */ |
| 60 | Addr VG_(client_base); /* client address space limits */ |
| 61 | Addr VG_(client_end); |
| 62 | Addr VG_(client_mapbase); |
njn | 04e1698 | 2005-05-31 00:23:43 +0000 | [diff] [blame] | 63 | Addr VG_(clstk_base); |
| 64 | Addr VG_(clstk_end); |
rjwalsh | 0140af5 | 2005-06-04 20:42:33 +0000 | [diff] [blame] | 65 | UWord VG_(clstk_id); |
njn | 04e1698 | 2005-05-31 00:23:43 +0000 | [diff] [blame] | 66 | |
| 67 | Addr VG_(brk_base); /* start of brk */ |
| 68 | Addr VG_(brk_limit); /* current brk */ |
| 69 | |
| 70 | Addr VG_(shadow_base); /* tool's shadow memory */ |
| 71 | Addr VG_(shadow_end); |
| 72 | |
| 73 | Addr VG_(valgrind_base); /* valgrind's address range */ |
| 74 | |
| 75 | // Note that VG_(valgrind_last) names the last byte of the section, whereas |
| 76 | // the VG_(*_end) vars name the byte one past the end of the section. |
| 77 | Addr VG_(valgrind_last); |
| 78 | |
| 79 | /*--------------------------------------------------------------*/ |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 80 | /*--- A simple, self-contained ordered array of segments. ---*/ |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 81 | /*--------------------------------------------------------------*/ |
| 82 | |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 83 | /* Max number of segments we can track. */ |
| 84 | #define VG_N_SEGMENTS 1000 |
| 85 | |
| 86 | /* Max number of segment file names we can track. */ |
| 87 | #define VG_N_SEGNAMES 200 |
| 88 | |
| 89 | /* Max length of a segment file name. */ |
| 90 | #define VG_MAX_SEGNAMELEN 1000 |
| 91 | |
| 92 | |
| 93 | /* ------ STATE for the address-space manager ------ */ |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 94 | |
| 95 | /* Array [0 .. segments_used-1] of all mappings. */ |
| 96 | /* Sorted by .addr field. */ |
| 97 | /* I: len may not be zero. */ |
| 98 | /* I: overlapping segments are not allowed. */ |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 99 | /* Each segment can optionally hold an index into the filename table. */ |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 100 | |
| 101 | static Segment segments[VG_N_SEGMENTS]; |
| 102 | static Int segments_used = 0; |
| 103 | |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 104 | typedef |
| 105 | struct { |
| 106 | Bool inUse; |
| 107 | Bool mark; |
| 108 | HChar fname[VG_MAX_SEGNAMELEN]; |
| 109 | } |
| 110 | SegName; |
| 111 | |
| 112 | /* Filename table. _used is the high water mark; an entry is only |
| 113 | valid if its index >= 0, < _used, and its .inUse field == True. |
| 114 | The .mark field is used to garbage-collect dead entries. |
| 115 | */ |
| 116 | static SegName segnames[VG_N_SEGNAMES]; |
| 117 | static Int segnames_used = 0; |
| 118 | |
| 119 | |
| 120 | /* ------ end of STATE for the address-space manager ------ */ |
| 121 | |
| 122 | |
| 123 | /* Searches the filename table to find an index for the given name. |
| 124 | If none is found, an index is allocated and the name stored. If no |
| 125 | space is available we just give up. If the string is too long to |
| 126 | store, return -1. |
| 127 | */ |
| 128 | static Int allocate_segname ( const HChar* name ) |
| 129 | { |
| 130 | Int i, j, len; |
| 131 | |
| 132 | vg_assert(name); |
| 133 | |
| 134 | if (0) VG_(printf)("alloc_segname %s\n", name); |
| 135 | |
| 136 | len = VG_(strlen)(name); |
| 137 | if (len >= VG_MAX_SEGNAMELEN-1) { |
| 138 | return -1; |
| 139 | } |
| 140 | |
| 141 | /* first see if we already have the name. */ |
| 142 | for (i = 0; i < segnames_used; i++) { |
| 143 | if (!segnames[i].inUse) |
| 144 | continue; |
| 145 | if (0 == VG_(strcmp)(name, &segnames[i].fname[0])) { |
| 146 | return i; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /* no we don't. So look for a free slot. */ |
| 151 | for (i = 0; i < segnames_used; i++) |
| 152 | if (!segnames[i].inUse) |
| 153 | break; |
| 154 | |
| 155 | if (i == segnames_used) { |
| 156 | /* no free slots .. advance the high-water mark. */ |
| 157 | if (segnames_used+1 < VG_N_SEGNAMES) { |
| 158 | i = segnames_used; |
| 159 | segnames_used++; |
| 160 | } else { |
| 161 | VG_(printf)( |
tom | 3a07e9b | 2005-05-17 11:03:52 +0000 | [diff] [blame] | 162 | "coregrind/m_aspacemgr/aspacemgr.c:\n" |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 163 | " VG_N_SEGNAMES is too small: " |
| 164 | "increase it and rebuild Valgrind.\n" |
| 165 | ); |
| 166 | VG_(printf)( |
tom | 3a07e9b | 2005-05-17 11:03:52 +0000 | [diff] [blame] | 167 | "coregrind/m_aspacemgr/aspacemgr.c:\n" |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 168 | " giving up now.\n\n" |
| 169 | ); |
| 170 | VG_(exit)(0); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /* copy it in */ |
| 175 | segnames[i].inUse = True; |
| 176 | for (j = 0; j < len; j++) |
| 177 | segnames[i].fname[j] = name[j]; |
| 178 | vg_assert(len < VG_MAX_SEGNAMELEN); |
| 179 | segnames[i].fname[len] = 0; |
| 180 | return i; |
| 181 | } |
| 182 | |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 183 | |
| 184 | /* Returns -1 if 'a' denotes an address prior to seg, 1 if it denotes |
| 185 | an address after it, and 0 if it denotes an address covered by |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 186 | seg. |
| 187 | */ |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 188 | static Int compare_addr_with_seg ( Addr a, Segment* seg ) |
| 189 | { |
| 190 | if (a < seg->addr) |
| 191 | return -1; |
| 192 | if (a >= seg->addr + seg->len) |
| 193 | return 1; |
| 194 | return 0; |
| 195 | } |
| 196 | |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 197 | |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 198 | /* Find the (index of the) segment that contains 'a', or -1 if |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 199 | none. |
| 200 | */ |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 201 | static Int find_segment ( Addr a ) |
| 202 | { |
| 203 | Int i; |
| 204 | for (i = 0; i < segments_used; i++) { |
| 205 | if (compare_addr_with_seg(a, &segments[i]) == 0) |
| 206 | return i; |
| 207 | } |
| 208 | return -1; |
| 209 | } |
| 210 | |
| 211 | |
| 212 | /* Assumes that 'a' is not in any segment. Finds the index of the |
| 213 | lowest-addressed segment above 'a', or -1 if none. Passing 'a' |
| 214 | which is in fact in a segment is a checked error. |
| 215 | */ |
| 216 | static Int find_segment_above_unmapped ( Addr a ) |
| 217 | { |
| 218 | Int i, r; |
| 219 | for (i = 0; i < segments_used; i++) { |
| 220 | r = compare_addr_with_seg(a, &segments[i]); |
| 221 | vg_assert(r != 0); /* 'a' should not be in any segment. */ |
| 222 | if (r == 1) |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 223 | continue; |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 224 | vg_assert(r == -1); |
| 225 | break; |
| 226 | } |
| 227 | |
| 228 | if (i == segments_used) |
| 229 | return -1; /* not found */ |
| 230 | else |
| 231 | return i; |
| 232 | } |
| 233 | |
| 234 | |
| 235 | /* Assumes that 'a' is in some segment. Finds the next segment along, |
| 236 | or NULL if none. Passing 'a' which is in fact not in a segment is |
| 237 | a checked error. |
| 238 | */ |
| 239 | static Int find_segment_above_mapped ( Addr a ) |
| 240 | { |
| 241 | Int i, r; |
| 242 | for (i = 0; i < segments_used; i++) { |
| 243 | r = compare_addr_with_seg(a, &segments[i]); |
| 244 | if (r == 1) |
| 245 | continue; /* not yet there */ |
| 246 | if (r == 0) |
| 247 | break; /* found it */ |
| 248 | vg_assert(0); |
| 249 | /* we shouldn't get here -- r == -1 and so it means we went past |
| 250 | 'a' without seeing it -- it is therefore unmapped. */ |
| 251 | /*NOTREACHED*/ |
| 252 | } |
| 253 | |
| 254 | vg_assert(i < segments_used); |
| 255 | if (i == segments_used-1) |
| 256 | return -1; /* not found */ |
| 257 | else |
| 258 | return i+1; |
| 259 | } |
| 260 | |
| 261 | |
| 262 | /* Shift segments[i .. segments_used-1] up by one. */ |
| 263 | static void make_space_at ( Int i ) |
| 264 | { |
| 265 | Int j; |
| 266 | vg_assert(i >= 0 && i <= segments_used); |
| 267 | vg_assert(segments_used >= 0); |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 268 | if (segments_used+1 == VG_N_SEGMENTS) { |
| 269 | VG_(printf)( |
tom | 8a51bb8 | 2005-06-15 10:24:26 +0000 | [diff] [blame] | 270 | "coregrind/m_aspacemgr/aspacemgr.c:\n" |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 271 | " VG_N_SEGMENTS is too small: " |
| 272 | "increase it and rebuild Valgrind.\n" |
| 273 | ); |
| 274 | VG_(printf)( |
tom | 8a51bb8 | 2005-06-15 10:24:26 +0000 | [diff] [blame] | 275 | "coregrind/m_aspacemgr/aspacemgr.c:\n" |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 276 | " giving up now.\n\n" |
| 277 | ); |
| 278 | VG_(exit)(0); |
| 279 | } |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 280 | vg_assert(segments_used+1 < VG_N_SEGMENTS); |
| 281 | for (j = segments_used; j > i; j--) |
| 282 | segments[j] = segments[j-1]; |
| 283 | segments_used++; |
| 284 | } |
| 285 | |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 286 | |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 287 | /* Shift segments [i+1 .. segments_used-1] down by one, and decrement |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 288 | segments_used. |
| 289 | */ |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 290 | static void delete_segment_at ( Int i ) |
| 291 | { |
| 292 | Int j; |
| 293 | vg_assert(i >= 0 && i < segments_used); |
| 294 | for (j = i+1; j < segments_used; j++) |
| 295 | segments[j-1] = segments[j]; |
| 296 | segments_used--; |
| 297 | vg_assert(segments_used >= 0 && segments_used < VG_N_SEGMENTS); |
| 298 | } |
| 299 | |
| 300 | |
| 301 | /* Fill the i'th record all with zeroes. */ |
| 302 | static void zeroise_segment ( Int i ) |
| 303 | { |
| 304 | vg_assert(i >= 0 && i < segments_used); |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 305 | segments[i].prot = 0; |
| 306 | segments[i].flags = 0; |
| 307 | segments[i].addr = 0; |
| 308 | segments[i].len = 0; |
| 309 | segments[i].offset = 0; |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 310 | segments[i].filename = NULL; |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 311 | segments[i].fnIdx = -1; |
| 312 | segments[i].dev = 0; |
| 313 | segments[i].ino = 0; |
njn | 36ef6ba | 2005-05-14 18:42:26 +0000 | [diff] [blame] | 314 | segments[i].seginfo = NULL; |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 315 | } |
| 316 | |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 317 | |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 318 | /* Create a segment to contain 'a', and return its index. Or -1 if |
| 319 | this failed because some other segment already contains 'a'. If |
| 320 | successful, fill in the segment's .addr field with 'a' but leave |
| 321 | all other fields alone. |
| 322 | */ |
| 323 | static Int create_segment ( Addr a ) |
| 324 | { |
| 325 | Int i, r; |
| 326 | for (i = 0; i < segments_used; i++) { |
| 327 | r = compare_addr_with_seg( a, &segments[i] ); |
| 328 | if (r == 1) |
| 329 | continue; /* seg[i] precedes a */ |
| 330 | if (r == 0) |
| 331 | return -1; /* seg[i] contains a. Give up */ |
| 332 | vg_assert(r == -1); |
| 333 | break; |
| 334 | } |
| 335 | /* a precedes seg[i]. Shift segs at i and above up one, and use |
| 336 | this slot. */ |
| 337 | make_space_at(i); |
| 338 | zeroise_segment(i); |
| 339 | segments[i].addr = a; |
| 340 | return i; |
| 341 | } |
| 342 | |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 343 | |
| 344 | /* Print out the segment array (debugging only!). Note, this calls |
| 345 | VG_(printf), and I'm not 100% clear that that wouldn't require |
| 346 | dynamic memory allocation and hence more segments to be allocated. |
| 347 | */ |
| 348 | static void show_segments ( HChar* who ) |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 349 | { |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 350 | Int i; |
| 351 | VG_(printf)("<<< SHOW_SEGMENTS: %s (%d segments, %d segnames)\n", |
| 352 | who, segments_used, segnames_used); |
| 353 | for (i = 0; i < segnames_used; i++) { |
| 354 | if (!segnames[i].inUse) |
| 355 | continue; |
| 356 | VG_(printf)("(%2d) %s\n", i, segnames[i].fname); |
| 357 | } |
| 358 | for (i = 0; i < segments_used; i++) { |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 359 | VG_(printf)( |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 360 | "%3d: %08p-%08p %7llu pr=0x%x fl=0x%04x d=0x%03x i=%-7d o=%-7lld (%d)\n", |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 361 | i, |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 362 | segments[i].addr, segments[i].addr + segments[i].len, |
| 363 | (ULong)segments[i].len, segments[i].prot, |
| 364 | segments[i].flags, segments[i].dev, segments[i].ino, |
| 365 | (Long)segments[i].offset, |
| 366 | segments[i].fnIdx); |
| 367 | } |
| 368 | VG_(printf)(">>>\n"); |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 369 | } |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 370 | |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 371 | |
| 372 | /* Find the segment containing 'a' and split it into two pieces at |
| 373 | 'a'. Does nothing if no segment contains 'a', or if the split |
| 374 | would cause either of the pieces to have zero size. |
| 375 | |
| 376 | If 'a' is not found, or if no splitting happens, -1 is returned. |
| 377 | |
| 378 | If a value 'r' other than -1 is returned, this is the index of the |
| 379 | higher-addressed segment resulting from the split, and the index of |
| 380 | the lower-addressed segment is r-1. |
| 381 | */ |
| 382 | static Int split_segment ( Addr a ) |
| 383 | { |
| 384 | Int r; |
| 385 | HWord delta; |
njn | be91aae | 2005-03-27 01:42:41 +0000 | [diff] [blame] | 386 | vg_assert(VG_IS_PAGE_ALIGNED(a)); |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 387 | r = find_segment(a); |
| 388 | if (r == -1) |
| 389 | /* not found */ |
| 390 | return -1; |
| 391 | if (segments[r].addr == a) |
| 392 | /* segment starts at 'a', so splitting it would create a |
| 393 | zero-sized segment */ |
| 394 | return -1; |
| 395 | |
| 396 | /* copy original; make adjustments. */ |
| 397 | vg_assert(a > segments[r].addr); |
| 398 | delta = a - segments[r].addr; |
| 399 | make_space_at(r); |
| 400 | segments[r] = segments[r+1]; |
| 401 | segments[r].len = delta; |
| 402 | segments[r+1].len -= delta; |
| 403 | segments[r+1].addr += delta; |
| 404 | segments[r+1].offset += delta; |
| 405 | return r+1; |
| 406 | } |
| 407 | |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 408 | |
| 409 | /* Return true if two segments are adjacent and mergable (s1 is |
| 410 | assumed to have a lower ->addr than s2) */ |
| 411 | static inline Bool segments_are_mergeable(Segment *s1, Segment *s2) |
| 412 | { |
| 413 | if (s1->addr+s1->len != s2->addr) |
| 414 | return False; |
| 415 | |
| 416 | if (s1->flags != s2->flags) |
| 417 | return False; |
| 418 | |
| 419 | if (s1->prot != s2->prot) |
| 420 | return False; |
| 421 | |
njn | 36ef6ba | 2005-05-14 18:42:26 +0000 | [diff] [blame] | 422 | if (s1->seginfo != s2->seginfo) |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 423 | return False; |
| 424 | |
| 425 | if (s1->flags & SF_FILE){ |
| 426 | if ((s1->offset + s1->len) != s2->offset) |
| 427 | return False; |
| 428 | if (s1->dev != s2->dev) |
| 429 | return False; |
| 430 | if (s1->ino != s2->ino) |
| 431 | return False; |
| 432 | if (s1->fnIdx != s2->fnIdx) |
| 433 | return False; |
| 434 | } |
| 435 | |
| 436 | return True; |
| 437 | } |
| 438 | |
| 439 | |
| 440 | /* Clean up and sanity check the segment array: |
| 441 | - check segments are in ascending order |
| 442 | - check segments do not overlap |
| 443 | - check no segment has zero size |
| 444 | - merge adjacent where possible |
| 445 | - perform checks on the filename table, and reclaim dead entries |
| 446 | */ |
| 447 | static void preen_segments ( void ) |
| 448 | { |
| 449 | Int i, j, rd, wr; |
| 450 | Segment *s, *s1; |
| 451 | vg_assert(segments_used >= 0 && segments_used < VG_N_SEGMENTS); |
| 452 | vg_assert(segnames_used >= 0 && segnames_used < VG_N_SEGNAMES); |
| 453 | |
| 454 | if (0) show_segments("before preen"); |
| 455 | |
| 456 | /* clear string table mark bits */ |
| 457 | for (i = 0; i < segnames_used; i++) |
| 458 | segnames[i].mark = False; |
| 459 | |
| 460 | /* check for non-zero size, and set mark bits for any used strings */ |
| 461 | for (i = 0; i < segments_used; i++) { |
| 462 | vg_assert(segments[i].len > 0); |
| 463 | j = segments[i].fnIdx; |
| 464 | vg_assert(j >= -1 && j < segnames_used); |
| 465 | if (j >= 0) { |
| 466 | vg_assert(segnames[j].inUse); |
| 467 | segnames[j].mark = True; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | /* check ascendingness and non-overlap */ |
| 472 | for (i = 0; i < segments_used-1; i++) { |
| 473 | s = &segments[i]; |
| 474 | s1 = &segments[i+1]; |
| 475 | vg_assert(s->addr < s1->addr); |
| 476 | vg_assert(s->addr + s->len <= s1->addr); |
| 477 | } |
| 478 | |
| 479 | /* merge */ |
| 480 | if (segments_used < 1) |
| 481 | return; |
| 482 | |
| 483 | wr = 1; |
| 484 | for (rd = 1; rd < segments_used; rd++) { |
| 485 | s = &segments[wr-1]; |
| 486 | s1 = &segments[rd]; |
| 487 | if (segments_are_mergeable(s,s1)) { |
| 488 | if (0) |
| 489 | VG_(printf)("merge %p-%p with %p-%p\n", |
| 490 | s->addr, s->addr+s->len, |
| 491 | s1->addr, s1->addr+s1->len); |
| 492 | s->len += s1->len; |
| 493 | continue; |
| 494 | } |
| 495 | if (wr < rd) |
| 496 | segments[wr] = segments[rd]; |
| 497 | wr++; |
| 498 | } |
| 499 | vg_assert(wr >= 0 && wr <= segments_used); |
| 500 | segments_used = wr; |
| 501 | |
| 502 | /* Free up any strings which are no longer referenced. */ |
| 503 | for (i = 0; i < segnames_used; i++) { |
| 504 | if (segnames[i].mark == False) { |
| 505 | segnames[i].inUse = False; |
| 506 | segnames[i].fname[0] = 0; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | if (0) show_segments("after preen"); |
| 511 | } |
| 512 | |
| 513 | |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 514 | /*--------------------------------------------------------------*/ |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 515 | /*--- Maintain an ordered list of all the client's mappings ---*/ |
| 516 | /*--------------------------------------------------------------*/ |
| 517 | |
nethercote | 8991d5a | 2004-11-03 17:07:46 +0000 | [diff] [blame] | 518 | Bool VG_(seg_contains)(const Segment *s, Addr p, SizeT len) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 519 | { |
| 520 | Addr se = s->addr+s->len; |
| 521 | Addr pe = p+len; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 522 | vg_assert(pe >= p); |
| 523 | |
| 524 | return (p >= s->addr && pe <= se); |
| 525 | } |
| 526 | |
nethercote | 8991d5a | 2004-11-03 17:07:46 +0000 | [diff] [blame] | 527 | Bool VG_(seg_overlaps)(const Segment *s, Addr p, SizeT len) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 528 | { |
| 529 | Addr se = s->addr+s->len; |
| 530 | Addr pe = p+len; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 531 | vg_assert(pe >= p); |
| 532 | |
| 533 | return (p < se && pe > s->addr); |
| 534 | } |
| 535 | |
sewardj | 1024cf7 | 2005-02-28 14:39:21 +0000 | [diff] [blame] | 536 | #if 0 |
| 537 | /* 20050228: apparently unused */ |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 538 | /* Prepare a Segment structure for recycling by freeing everything |
| 539 | hanging off it. */ |
| 540 | static void recycleseg(Segment *s) |
| 541 | { |
| 542 | if (s->flags & SF_CODE) |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 543 | VG_(discard_translations)(s->addr, s->len); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 544 | |
| 545 | if (s->filename != NULL) |
| 546 | VG_(arena_free)(VG_AR_CORE, (Char *)s->filename); |
| 547 | |
| 548 | /* keep the SegInfo, if any - it probably still applies */ |
| 549 | } |
| 550 | |
| 551 | /* When freeing a Segment, also clean up every one else's ideas of |
| 552 | what was going on in that range of memory */ |
| 553 | static void freeseg(Segment *s) |
| 554 | { |
| 555 | recycleseg(s); |
njn | 36ef6ba | 2005-05-14 18:42:26 +0000 | [diff] [blame] | 556 | if (s->seginfo != NULL) { |
| 557 | VG_(seginfo_decref)(s->seginfo, s->addr); |
| 558 | s->seginfo = NULL; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | VG_(SkipNode_Free)(&sk_segments, s); |
| 562 | } |
sewardj | 1024cf7 | 2005-02-28 14:39:21 +0000 | [diff] [blame] | 563 | #endif |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 564 | |
sewardj | 1024cf7 | 2005-02-28 14:39:21 +0000 | [diff] [blame] | 565 | |
| 566 | /* Get rid of any translations arising from s. */ |
| 567 | /* Note, this is not really the job of the low level memory manager. |
| 568 | When it comes time to rewrite this subsystem, clean this up. */ |
| 569 | static void dump_translations_from ( Segment* s ) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 570 | { |
sewardj | 1024cf7 | 2005-02-28 14:39:21 +0000 | [diff] [blame] | 571 | if (s->flags & SF_CODE) { |
| 572 | VG_(discard_translations)(s->addr, s->len); |
| 573 | if (0) |
| 574 | VG_(printf)("dumping translations in %p .. %p\n", |
| 575 | s->addr, s->addr+s->len); |
| 576 | } |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 577 | } |
| 578 | |
sewardj | 1024cf7 | 2005-02-28 14:39:21 +0000 | [diff] [blame] | 579 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 580 | /* This unmaps all the segments in the range [addr, addr+len); any |
| 581 | partial mappings at the ends are truncated. */ |
nethercote | 8b5f40c | 2004-11-02 13:29:50 +0000 | [diff] [blame] | 582 | void VG_(unmap_range)(Addr addr, SizeT len) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 583 | { |
fitzhardinge | e3632c6 | 2003-12-22 10:58:06 +0000 | [diff] [blame] | 584 | static const Bool debug = False || mem_debug; |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 585 | Segment* s; |
| 586 | Addr end, s_end; |
| 587 | Int i; |
sewardj | 47c98a7 | 2005-03-12 20:36:15 +0000 | [diff] [blame] | 588 | Bool deleted; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 589 | |
| 590 | if (len == 0) |
| 591 | return; |
| 592 | |
njn | 13bfd85 | 2005-06-02 03:52:53 +0000 | [diff] [blame] | 593 | len = VG_PGROUNDUP(len); |
fitzhardinge | 1a4adf0 | 2003-12-22 10:42:59 +0000 | [diff] [blame] | 594 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 595 | if (debug) |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 596 | VG_(printf)("unmap_range(%p, %llu)\n", addr, (ULong)len); |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 597 | if (0) show_segments("unmap_range(BEFORE)"); |
fitzhardinge | 1a4adf0 | 2003-12-22 10:42:59 +0000 | [diff] [blame] | 598 | end = addr+len; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 599 | |
| 600 | /* Everything must be page-aligned */ |
njn | be91aae | 2005-03-27 01:42:41 +0000 | [diff] [blame] | 601 | vg_assert(VG_IS_PAGE_ALIGNED(addr)); |
| 602 | vg_assert(VG_IS_PAGE_ALIGNED(len)); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 603 | |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 604 | for (i = 0; i < segments_used; i++) { |
sewardj | 47c98a7 | 2005-03-12 20:36:15 +0000 | [diff] [blame] | 605 | |
| 606 | /* do not delete .. even though it looks stupid */ |
| 607 | vg_assert(i >= 0); |
| 608 | |
| 609 | deleted = False; |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 610 | s = &segments[i]; |
| 611 | s_end = s->addr + s->len; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 612 | |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 613 | if (0 && debug) |
fitzhardinge | 1a30304 | 2003-12-22 08:48:50 +0000 | [diff] [blame] | 614 | VG_(printf)("unmap: addr=%p-%p s=%p ->addr=%p-%p len=%d\n", |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 615 | addr, end, s, s->addr, s_end, s->len); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 616 | |
fitzhardinge | 1a30304 | 2003-12-22 08:48:50 +0000 | [diff] [blame] | 617 | if (!VG_(seg_overlaps)(s, addr, len)) { |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 618 | if (0 && debug) |
fitzhardinge | 1a30304 | 2003-12-22 08:48:50 +0000 | [diff] [blame] | 619 | VG_(printf)(" (no overlap)\n"); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 620 | continue; |
fitzhardinge | 1a30304 | 2003-12-22 08:48:50 +0000 | [diff] [blame] | 621 | } |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 622 | |
| 623 | /* 4 cases: */ |
fitzhardinge | 1a30304 | 2003-12-22 08:48:50 +0000 | [diff] [blame] | 624 | if (addr > s->addr && |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 625 | addr < s_end && |
| 626 | end >= s_end) { |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 627 | /* this segment's tail is truncated by [addr, addr+len) |
| 628 | -> truncate tail |
| 629 | */ |
sewardj | 1024cf7 | 2005-02-28 14:39:21 +0000 | [diff] [blame] | 630 | dump_translations_from(s); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 631 | s->len = addr - s->addr; |
| 632 | |
| 633 | if (debug) |
tom | 9be7c98 | 2005-04-25 16:55:44 +0000 | [diff] [blame] | 634 | VG_(printf)(" case 1: s->len=%lu\n", s->len); |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 635 | } else if (addr <= s->addr && end > s->addr && end < s_end) { |
fitzhardinge | e3632c6 | 2003-12-22 10:58:06 +0000 | [diff] [blame] | 636 | /* this segment's head is truncated by [addr, addr+len) |
| 637 | -> truncate head |
| 638 | */ |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 639 | Word delta = end - s->addr; |
fitzhardinge | e3632c6 | 2003-12-22 10:58:06 +0000 | [diff] [blame] | 640 | |
| 641 | if (debug) |
tom | 9be7c98 | 2005-04-25 16:55:44 +0000 | [diff] [blame] | 642 | VG_(printf)(" case 2: s->addr=%p s->len=%lu delta=%d\n", |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 643 | s->addr, s->len, delta); |
fitzhardinge | e3632c6 | 2003-12-22 10:58:06 +0000 | [diff] [blame] | 644 | |
sewardj | 1024cf7 | 2005-02-28 14:39:21 +0000 | [diff] [blame] | 645 | dump_translations_from(s); |
fitzhardinge | e3632c6 | 2003-12-22 10:58:06 +0000 | [diff] [blame] | 646 | s->addr += delta; |
| 647 | s->offset += delta; |
| 648 | s->len -= delta; |
| 649 | |
| 650 | vg_assert(s->len != 0); |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 651 | } else if (addr <= s->addr && end >= s_end) { |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 652 | /* this segment is completely contained within [addr, addr+len) |
| 653 | -> delete segment |
| 654 | */ |
sewardj | 1024cf7 | 2005-02-28 14:39:21 +0000 | [diff] [blame] | 655 | dump_translations_from(s); |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 656 | delete_segment_at(i); |
sewardj | 47c98a7 | 2005-03-12 20:36:15 +0000 | [diff] [blame] | 657 | deleted = True; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 658 | |
| 659 | if (debug) |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 660 | VG_(printf)(" case 3: seg %d deleted\n", i); |
| 661 | } else if (addr > s->addr && end < s_end) { |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 662 | /* [addr, addr+len) is contained within a single segment |
| 663 | -> split segment into 3, delete middle portion |
| 664 | */ |
sewardj | 1024cf7 | 2005-02-28 14:39:21 +0000 | [diff] [blame] | 665 | Int i_middle; |
| 666 | dump_translations_from(s); |
| 667 | i_middle = split_segment(addr); |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 668 | vg_assert(i_middle != -1); |
| 669 | (void)split_segment(addr+len); |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 670 | vg_assert(segments[i_middle].addr == addr); |
| 671 | delete_segment_at(i_middle); |
sewardj | 47c98a7 | 2005-03-12 20:36:15 +0000 | [diff] [blame] | 672 | deleted = True; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 673 | |
| 674 | if (debug) |
| 675 | VG_(printf)(" case 4: subrange %p-%p deleted\n", |
| 676 | addr, addr+len); |
| 677 | } |
sewardj | 47c98a7 | 2005-03-12 20:36:15 +0000 | [diff] [blame] | 678 | |
| 679 | /* If we deleted this segment (or any above), those above will |
| 680 | have been moved down to fill in the hole in the segment |
| 681 | array. In order that we don't miss them, we have to |
| 682 | re-consider this slot number; hence the i--. */ |
| 683 | if (deleted) |
| 684 | i--; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 685 | } |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 686 | preen_segments(); |
| 687 | if (0) show_segments("unmap_range(AFTER)"); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 688 | } |
| 689 | |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 690 | |
| 691 | /* Add a binding of [addr,addr+len) to |
| 692 | (prot,flags,dev,ino,off,filename) in the segment array. |
| 693 | Delete/truncate any previous mapping(s) covering that range. |
| 694 | */ |
| 695 | void |
| 696 | VG_(map_file_segment)( Addr addr, SizeT len, |
| 697 | UInt prot, UInt flags, |
| 698 | UInt dev, UInt ino, ULong off, |
| 699 | const Char *filename) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 700 | { |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 701 | static const Bool debug = False || mem_debug; |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 702 | Segment* s; |
| 703 | Int idx; |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 704 | HChar* stage2_suffix = "lib/valgrind/stage2"; |
| 705 | Bool is_stage2 = VG_(strstr)(filename, stage2_suffix) != NULL; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 706 | |
| 707 | if (debug) |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 708 | VG_(printf)( |
| 709 | "\n" |
tom | 9be7c98 | 2005-04-25 16:55:44 +0000 | [diff] [blame] | 710 | "map_file_segment(addr=%p len=%lu prot=0x%x flags=0x%x\n" |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 711 | " dev=0x%4x ino=%d off=%ld\n" |
| 712 | " filename='%s')\n", |
| 713 | addr, (ULong)len, prot, flags, dev, ino, off, filename); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 714 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 715 | if (0) show_segments("before map_file_segment"); |
| 716 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 717 | /* Everything must be page-aligned */ |
njn | be91aae | 2005-03-27 01:42:41 +0000 | [diff] [blame] | 718 | vg_assert(VG_IS_PAGE_ALIGNED(addr)); |
njn | 13bfd85 | 2005-06-02 03:52:53 +0000 | [diff] [blame] | 719 | len = VG_PGROUNDUP(len); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 720 | |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 721 | /* Nuke/truncate any existing segment(s) covering [addr,addr+len) */ |
| 722 | VG_(unmap_range)(addr, len); |
| 723 | |
| 724 | /* and now install this one */ |
| 725 | idx = create_segment(addr); |
| 726 | vg_assert(segments_used >= 0 && segments_used <= VG_N_SEGMENTS); |
| 727 | vg_assert(idx != -1); |
| 728 | vg_assert(idx >= 0 && idx < segments_used); |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 729 | |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 730 | s = &segments[idx]; |
| 731 | vg_assert(s->addr == addr); |
| 732 | s->prot = prot; |
| 733 | s->flags = flags; |
| 734 | s->len = len; |
| 735 | s->offset = off; |
| 736 | s->fnIdx = filename==NULL ? -1 : allocate_segname(filename); |
| 737 | s->filename = s->fnIdx==-1 ? NULL : &segnames[s->fnIdx].fname[0]; |
| 738 | s->dev = dev; |
| 739 | s->ino = ino; |
njn | 36ef6ba | 2005-05-14 18:42:26 +0000 | [diff] [blame] | 740 | s->seginfo = NULL; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 741 | |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 742 | /* Clean up right now */ |
| 743 | preen_segments(); |
| 744 | if (0) show_segments("after map_file_segment"); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 745 | |
| 746 | /* If this mapping is of the beginning of a file, isn't part of |
| 747 | Valgrind, is at least readable and seems to contain an object |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 748 | file, then try reading symbols from it. |
| 749 | */ |
njn | 36ef6ba | 2005-05-14 18:42:26 +0000 | [diff] [blame] | 750 | if (s->seginfo == NULL |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 751 | && ( (addr+len < VG_(valgrind_base) || addr > VG_(valgrind_last)) |
| 752 | || is_stage2 |
| 753 | ) |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 754 | && (flags & (SF_MMAP|SF_NOSYMS)) == SF_MMAP) { |
| 755 | if (off == 0 |
| 756 | && s->fnIdx != -1 |
| 757 | && (prot & (VKI_PROT_READ|VKI_PROT_EXEC)) == (VKI_PROT_READ|VKI_PROT_EXEC) |
| 758 | && len >= VKI_PAGE_SIZE |
| 759 | && VG_(is_object_file)((void *)addr)) { |
njn | 36ef6ba | 2005-05-14 18:42:26 +0000 | [diff] [blame] | 760 | s->seginfo = VG_(read_seg_symbols)(s); |
| 761 | if (s->seginfo != NULL) { |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 762 | s->flags |= SF_DYNLIB; |
| 763 | } |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 764 | } else if (flags & SF_MMAP) { |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 765 | #if 0 |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 766 | const SegInfo *info; |
| 767 | |
njn | 36ef6ba | 2005-05-14 18:42:26 +0000 | [diff] [blame] | 768 | /* Otherwise see if an existing SegInfo applies to this Segment */ |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 769 | for(info = VG_(next_seginfo)(NULL); |
| 770 | info != NULL; |
| 771 | info = VG_(next_seginfo)(info)) { |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 772 | if (VG_(seg_overlaps)(s, VG_(seg_start)(info), VG_(seg_size)(info))) |
| 773 | { |
njn | 36ef6ba | 2005-05-14 18:42:26 +0000 | [diff] [blame] | 774 | s->seginfo = (SegInfo *)info; |
| 775 | VG_(seginfo_incref)((SegInfo *)info); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 776 | } |
| 777 | } |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 778 | #endif |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 779 | } |
| 780 | } |
| 781 | |
| 782 | /* clean up */ |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 783 | preen_segments(); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 784 | } |
| 785 | |
nethercote | 8b5f40c | 2004-11-02 13:29:50 +0000 | [diff] [blame] | 786 | void VG_(map_fd_segment)(Addr addr, SizeT len, UInt prot, UInt flags, |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 787 | Int fd, ULong off, const Char *filename) |
| 788 | { |
| 789 | struct vki_stat st; |
| 790 | Char *name = NULL; |
| 791 | |
| 792 | st.st_dev = 0; |
| 793 | st.st_ino = 0; |
| 794 | |
| 795 | if (fd != -1 && (flags & SF_FILE)) { |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 796 | vg_assert((off & (VKI_PAGE_SIZE-1)) == 0); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 797 | |
| 798 | if (VG_(fstat)(fd, &st) < 0) |
| 799 | flags &= ~SF_FILE; |
| 800 | } |
| 801 | |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 802 | if ((flags & SF_FILE) && filename == NULL && fd != -1) |
| 803 | name = VG_(resolve_filename_nodup)(fd); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 804 | |
| 805 | if (filename == NULL) |
| 806 | filename = name; |
| 807 | |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 808 | VG_(map_file_segment)(addr, len, prot, flags, |
| 809 | st.st_dev, st.st_ino, off, filename); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 810 | } |
| 811 | |
nethercote | 8b5f40c | 2004-11-02 13:29:50 +0000 | [diff] [blame] | 812 | void VG_(map_segment)(Addr addr, SizeT len, UInt prot, UInt flags) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 813 | { |
| 814 | flags &= ~SF_FILE; |
| 815 | |
| 816 | VG_(map_file_segment)(addr, len, prot, flags, 0, 0, 0, 0); |
| 817 | } |
| 818 | |
| 819 | /* set new protection flags on an address range */ |
nethercote | 8b5f40c | 2004-11-02 13:29:50 +0000 | [diff] [blame] | 820 | void VG_(mprotect_range)(Addr a, SizeT len, UInt prot) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 821 | { |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 822 | Int r; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 823 | static const Bool debug = False || mem_debug; |
| 824 | |
| 825 | if (debug) |
tom | 9be7c98 | 2005-04-25 16:55:44 +0000 | [diff] [blame] | 826 | VG_(printf)("\nmprotect_range(%p, %lu, %x)\n", a, len, prot); |
sewardj | e517b80 | 2005-02-16 01:58:51 +0000 | [diff] [blame] | 827 | |
| 828 | if (0) show_segments( "mprotect_range(before)" ); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 829 | |
| 830 | /* Everything must be page-aligned */ |
njn | be91aae | 2005-03-27 01:42:41 +0000 | [diff] [blame] | 831 | vg_assert(VG_IS_PAGE_ALIGNED(a)); |
njn | 13bfd85 | 2005-06-02 03:52:53 +0000 | [diff] [blame] | 832 | len = VG_PGROUNDUP(len); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 833 | |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 834 | split_segment(a); |
| 835 | split_segment(a+len); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 836 | |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 837 | r = find_segment(a); |
| 838 | vg_assert(r != -1); |
| 839 | segments[r].prot = prot; |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 840 | |
| 841 | preen_segments(); |
| 842 | |
sewardj | e517b80 | 2005-02-16 01:58:51 +0000 | [diff] [blame] | 843 | if (0) show_segments( "mprotect_range(after)"); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 844 | } |
| 845 | |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 846 | |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 847 | /* Try to find a map space for [addr,addr+len). If addr==0, it means |
| 848 | the caller is prepared to accept a space at any location; if not, |
| 849 | we will try for addr, but fail if we can't get it. This mimics |
| 850 | mmap fixed vs mmap not-fixed. |
| 851 | */ |
nethercote | 8b5f40c | 2004-11-02 13:29:50 +0000 | [diff] [blame] | 852 | Addr VG_(find_map_space)(Addr addr, SizeT len, Bool for_client) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 853 | { |
fitzhardinge | e3632c6 | 2003-12-22 10:58:06 +0000 | [diff] [blame] | 854 | static const Bool debug = False || mem_debug; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 855 | Addr ret; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 856 | Addr addrOrig = addr; |
nethercote | a8bc380 | 2004-10-19 17:54:02 +0000 | [diff] [blame] | 857 | Addr limit = (for_client ? VG_(client_end)-1 : VG_(valgrind_last)); |
| 858 | Addr base = (for_client ? VG_(client_mapbase) : VG_(valgrind_base)); |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 859 | Addr hole_start, hole_end, hstart_any, hstart_fixed, hstart_final; |
| 860 | Int i, i_any, i_fixed, i_final; |
| 861 | SizeT hole_len; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 862 | |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 863 | Bool fixed; |
| 864 | |
sewardj | e517b80 | 2005-02-16 01:58:51 +0000 | [diff] [blame] | 865 | if (debug) { |
| 866 | VG_(printf)("\n\n"); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 867 | VG_(printf)("find_map_space(%p, %llu, %d) ...\n", |
| 868 | addr, (ULong)len, for_client); |
sewardj | e517b80 | 2005-02-16 01:58:51 +0000 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | if (0) show_segments("find_map_space: start"); |
| 872 | |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 873 | if (addr == 0) { |
| 874 | fixed = False; |
| 875 | } else { |
| 876 | fixed = True; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 877 | /* leave space for redzone and still try to get the exact |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 878 | address asked for */ |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 879 | addr -= VKI_PAGE_SIZE; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 880 | } |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 881 | |
| 882 | /* Everything must be page-aligned */ |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 883 | vg_assert((addr & (VKI_PAGE_SIZE-1)) == 0); |
njn | 13bfd85 | 2005-06-02 03:52:53 +0000 | [diff] [blame] | 884 | len = VG_PGROUNDUP(len); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 885 | |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 886 | len += VKI_PAGE_SIZE * 2; /* leave redzone gaps before and after mapping */ |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 887 | |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 888 | /* Scan the segment list, looking for a hole which satisfies the |
| 889 | requirements. At each point i we ask the question "can we use |
| 890 | the hole in between segments[i-1] and segments[i] ?" */ |
| 891 | i_any = i_fixed = -1; |
| 892 | hstart_any = hstart_fixed = 0; |
| 893 | |
| 894 | hole_start = hole_end = 0; |
| 895 | |
| 896 | /* Iterate over all possible holes, generating them into |
| 897 | hole_start/hole_end. Filter out invalid ones. Then see if any |
| 898 | are usable; if so set i_fixed/i_any and hstart_fixed/hstart_any. |
| 899 | */ |
| 900 | for (i = 0; i <=/*yes,really*/ segments_used; i++) { |
| 901 | if (i == 0) { |
| 902 | hole_start = 0; |
| 903 | hole_end = segments[0].addr-1; |
| 904 | } |
| 905 | else { |
| 906 | vg_assert(segments_used > 0); |
| 907 | if (i == segments_used) { |
| 908 | hole_start = segments[i-1].addr + segments[i-1].len; |
| 909 | hole_end = ~(Addr)0; |
| 910 | } else { |
| 911 | hole_start = segments[i-1].addr + segments[i-1].len; |
| 912 | hole_end = segments[i].addr - 1; |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | vg_assert(hole_start <= hole_end || hole_start == hole_end+1); |
| 917 | |
| 918 | /* ignore zero-sized holes */ |
| 919 | if (hole_start == hole_end+1) |
| 920 | continue; |
| 921 | |
njn | be91aae | 2005-03-27 01:42:41 +0000 | [diff] [blame] | 922 | vg_assert(VG_IS_PAGE_ALIGNED(hole_start)); |
| 923 | vg_assert(VG_IS_PAGE_ALIGNED(hole_end+1)); |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 924 | |
| 925 | /* ignore holes which fall outside the allowable area */ |
| 926 | if (!(hole_start >= base && hole_end <= limit)) |
| 927 | continue; |
| 928 | |
| 929 | vg_assert(hole_end > hole_start); |
| 930 | hole_len = hole_end - hole_start + 1; |
njn | be91aae | 2005-03-27 01:42:41 +0000 | [diff] [blame] | 931 | vg_assert(VG_IS_PAGE_ALIGNED(hole_len)); |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 932 | |
| 933 | if (hole_len >= len && i_any == -1) { |
| 934 | /* It will at least fit in this hole. */ |
| 935 | i_any = i; |
| 936 | hstart_any = hole_start; |
| 937 | } |
| 938 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 939 | if (fixed && hole_start <= addr |
| 940 | && hole_start+hole_len >= addr+len) { |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 941 | /* We were asked for a fixed mapping, and this hole works. |
| 942 | Bag it -- and stop searching as further searching is |
| 943 | pointless. */ |
| 944 | i_fixed = i; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 945 | hstart_fixed = addr; |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 946 | break; |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | /* Summarise the final decision into i_final/hstart_final. */ |
| 951 | i_final = -1; |
| 952 | hstart_final = 0; |
| 953 | |
| 954 | if (fixed) { |
| 955 | i_final = i_fixed; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 956 | hstart_final = hstart_fixed + VKI_PAGE_SIZE; /* skip leading redzone */ |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 957 | } else { |
| 958 | i_final = i_any; |
| 959 | hstart_final = hstart_any; |
| 960 | } |
| 961 | |
| 962 | |
| 963 | if (i_final != -1) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 964 | ret = hstart_final; |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 965 | else |
| 966 | ret = 0; /* not found */ |
| 967 | |
| 968 | if (debug) |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 969 | VG_(printf)("find_map_space(%p, %llu, %d) -> %p\n\n", |
| 970 | addr, (ULong)len, for_client, ret); |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 971 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 972 | if (fixed) { |
| 973 | vg_assert(ret == 0 || ret == addrOrig); |
| 974 | } |
| 975 | |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 976 | return ret; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 977 | } |
| 978 | |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 979 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 980 | /* Pad the entire process address space, from "start" |
nethercote | 820bd8c | 2004-09-07 23:04:49 +0000 | [diff] [blame] | 981 | to VG_(valgrind_last) by creating an anonymous and inaccessible |
thughes | 646c6aa | 2004-07-16 15:36:45 +0000 | [diff] [blame] | 982 | mapping over any part of the address space which is not covered |
| 983 | by an entry in the segment list. |
| 984 | |
| 985 | This is designed for use around system calls which allocate |
| 986 | memory in the process address space without providing a way to |
nethercote | ccc9ecd | 2004-07-16 17:32:15 +0000 | [diff] [blame] | 987 | control its location such as io_setup. By choosing a suitable |
thughes | 646c6aa | 2004-07-16 15:36:45 +0000 | [diff] [blame] | 988 | address with VG_(find_map_space) and then adding a segment for |
| 989 | it and padding the address space valgrind can ensure that the |
| 990 | kernel has no choice but to put the memory where we want it. */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 991 | void VG_(pad_address_space)(Addr start) |
thughes | 9aaebc3 | 2004-07-15 23:13:37 +0000 | [diff] [blame] | 992 | { |
sewardj | 394790d | 2005-03-17 02:14:44 +0000 | [diff] [blame] | 993 | Addr addr = (start == 0) ? VG_(client_base) : start; |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 994 | SysRes ret; |
sewardj | 394790d | 2005-03-17 02:14:44 +0000 | [diff] [blame] | 995 | |
| 996 | Int i = 0; |
| 997 | Segment* s = i >= segments_used ? NULL : &segments[i]; |
thughes | 9aaebc3 | 2004-07-15 23:13:37 +0000 | [diff] [blame] | 998 | |
nethercote | 820bd8c | 2004-09-07 23:04:49 +0000 | [diff] [blame] | 999 | while (s && addr <= VG_(valgrind_last)) { |
thughes | 9aaebc3 | 2004-07-15 23:13:37 +0000 | [diff] [blame] | 1000 | if (addr < s->addr) { |
njn | 7df470b | 2005-05-29 18:46:38 +0000 | [diff] [blame] | 1001 | ret = VG_(mmap_native)((void*)addr, s->addr - addr, 0, |
njn | f6d228b | 2005-03-26 02:42:31 +0000 | [diff] [blame] | 1002 | VKI_MAP_FIXED | VKI_MAP_PRIVATE | VKI_MAP_ANONYMOUS, |
| 1003 | -1, 0); |
thughes | 9aaebc3 | 2004-07-15 23:13:37 +0000 | [diff] [blame] | 1004 | } |
thughes | 9aaebc3 | 2004-07-15 23:13:37 +0000 | [diff] [blame] | 1005 | addr = s->addr + s->len; |
sewardj | 394790d | 2005-03-17 02:14:44 +0000 | [diff] [blame] | 1006 | i++; |
| 1007 | s = i >= segments_used ? NULL : &segments[i]; |
thughes | 9aaebc3 | 2004-07-15 23:13:37 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
nethercote | 820bd8c | 2004-09-07 23:04:49 +0000 | [diff] [blame] | 1010 | if (addr <= VG_(valgrind_last)) { |
njn | 7df470b | 2005-05-29 18:46:38 +0000 | [diff] [blame] | 1011 | ret = VG_(mmap_native)((void*)addr, VG_(valgrind_last) - addr + 1, 0, |
njn | f6d228b | 2005-03-26 02:42:31 +0000 | [diff] [blame] | 1012 | VKI_MAP_FIXED | VKI_MAP_PRIVATE | VKI_MAP_ANONYMOUS, |
| 1013 | -1, 0); |
thughes | 9aaebc3 | 2004-07-15 23:13:37 +0000 | [diff] [blame] | 1014 | } |
thughes | 9aaebc3 | 2004-07-15 23:13:37 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
thughes | c70b8c6 | 2004-07-16 23:07:58 +0000 | [diff] [blame] | 1017 | /* Remove the address space padding added by VG_(pad_address_space) |
thughes | 646c6aa | 2004-07-16 15:36:45 +0000 | [diff] [blame] | 1018 | by removing any mappings that it created. */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1019 | void VG_(unpad_address_space)(Addr start) |
thughes | 9aaebc3 | 2004-07-15 23:13:37 +0000 | [diff] [blame] | 1020 | { |
sewardj | 394790d | 2005-03-17 02:14:44 +0000 | [diff] [blame] | 1021 | Addr addr = (start == 0) ? VG_(client_base) : start; |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 1022 | SysRes ret; |
sewardj | 394790d | 2005-03-17 02:14:44 +0000 | [diff] [blame] | 1023 | |
| 1024 | Int i = 0; |
| 1025 | Segment* s = i >= segments_used ? NULL : &segments[i]; |
thughes | 9aaebc3 | 2004-07-15 23:13:37 +0000 | [diff] [blame] | 1026 | |
nethercote | 820bd8c | 2004-09-07 23:04:49 +0000 | [diff] [blame] | 1027 | while (s && addr <= VG_(valgrind_last)) { |
thughes | 9aaebc3 | 2004-07-15 23:13:37 +0000 | [diff] [blame] | 1028 | if (addr < s->addr) { |
njn | ca6fef0 | 2004-11-29 16:49:18 +0000 | [diff] [blame] | 1029 | ret = VG_(do_syscall2)(__NR_munmap, addr, s->addr - addr); |
thughes | 9aaebc3 | 2004-07-15 23:13:37 +0000 | [diff] [blame] | 1030 | } |
thughes | 9aaebc3 | 2004-07-15 23:13:37 +0000 | [diff] [blame] | 1031 | addr = s->addr + s->len; |
sewardj | 394790d | 2005-03-17 02:14:44 +0000 | [diff] [blame] | 1032 | i++; |
| 1033 | s = i >= segments_used ? NULL : &segments[i]; |
thughes | 9aaebc3 | 2004-07-15 23:13:37 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
nethercote | 820bd8c | 2004-09-07 23:04:49 +0000 | [diff] [blame] | 1036 | if (addr <= VG_(valgrind_last)) { |
sewardj | 394790d | 2005-03-17 02:14:44 +0000 | [diff] [blame] | 1037 | ret = VG_(do_syscall2)(__NR_munmap, addr, |
| 1038 | (VG_(valgrind_last) - addr) + 1); |
thughes | 9aaebc3 | 2004-07-15 23:13:37 +0000 | [diff] [blame] | 1039 | } |
thughes | 9aaebc3 | 2004-07-15 23:13:37 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 1042 | /* Find the segment holding 'a', or NULL if none. */ |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1043 | Segment *VG_(find_segment)(Addr a) |
| 1044 | { |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 1045 | Int r = find_segment(a); |
| 1046 | if (0) show_segments("find_segment"); |
| 1047 | if (r == -1) return NULL; |
| 1048 | return &segments[r]; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 1051 | /* Assumes that 'a' is not in any segment. Finds the lowest-addressed |
| 1052 | segment above 'a', or NULL if none. Passing 'a' which is in fact in |
| 1053 | a segment is a checked error. |
| 1054 | */ |
| 1055 | Segment *VG_(find_segment_above_unmapped)(Addr a) |
| 1056 | { |
| 1057 | Int r = find_segment_above_unmapped(a); |
| 1058 | if (0) show_segments("find_segment_above_unmapped"); |
| 1059 | if (r == -1) return NULL; |
| 1060 | return &segments[r]; |
| 1061 | } |
| 1062 | |
| 1063 | /* Assumes that 'a' is in some segment. Finds the next segment along, |
| 1064 | or NULL if none. Passing 'a' which is in fact not in a segment is |
| 1065 | a checked error. |
| 1066 | */ |
| 1067 | Segment *VG_(find_segment_above_mapped)(Addr a) |
| 1068 | { |
| 1069 | Int r = find_segment_above_mapped(a); |
| 1070 | if (0) show_segments("find_segment_above_mapped"); |
| 1071 | if (r == -1) return NULL; |
| 1072 | return &segments[r]; |
| 1073 | } |
| 1074 | |
| 1075 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1076 | /*------------------------------------------------------------*/ |
| 1077 | /*--- Tracking permissions around %esp changes. ---*/ |
| 1078 | /*------------------------------------------------------------*/ |
| 1079 | |
| 1080 | /* |
| 1081 | The stack |
| 1082 | ~~~~~~~~~ |
sewardj | fa8202f | 2005-04-24 11:05:55 +0000 | [diff] [blame] | 1083 | The stack's segment seems to be dynamically extended downwards by |
| 1084 | the kernel as the stack pointer moves down. Initially, a 1-page |
| 1085 | (4k) stack is allocated. When SP moves below that for the first |
| 1086 | time, presumably a page fault occurs. The kernel detects that the |
njn | a60a7c1 | 2005-05-08 17:49:37 +0000 | [diff] [blame] | 1087 | faulting address is in the range from SP - VGA_STACK_REDZONE_SZB |
sewardj | fa8202f | 2005-04-24 11:05:55 +0000 | [diff] [blame] | 1088 | upwards to the current valid stack. It then extends the stack |
| 1089 | segment downwards for enough to cover the faulting address, and |
| 1090 | resumes the process (invisibly). The process is unaware of any of |
| 1091 | this. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1092 | |
sewardj | fa8202f | 2005-04-24 11:05:55 +0000 | [diff] [blame] | 1093 | That means that Valgrind can't spot when the stack segment is being |
| 1094 | extended. Fortunately, we want to precisely and continuously |
| 1095 | update stack permissions around SP, so we need to spot all writes |
| 1096 | to SP anyway. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1097 | |
sewardj | fa8202f | 2005-04-24 11:05:55 +0000 | [diff] [blame] | 1098 | The deal is: when SP is assigned a lower value, the stack is being |
| 1099 | extended. Create suitably-permissioned pages to fill in any holes |
| 1100 | between the old stack ptr and this one, if necessary. Then mark |
| 1101 | all bytes in the area just "uncovered" by this SP change as |
| 1102 | write-only. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1103 | |
sewardj | fa8202f | 2005-04-24 11:05:55 +0000 | [diff] [blame] | 1104 | When SP goes back up, mark the area receded over as unreadable and |
| 1105 | unwritable. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1106 | |
njn | a60a7c1 | 2005-05-08 17:49:37 +0000 | [diff] [blame] | 1107 | Just to record the SP boundary conditions somewhere convenient: |
| 1108 | SP - VGA_STACK_REDZONE_SZB always points to the lowest live byte in |
| 1109 | the stack. All addresses below SP - VGA_STACK_REDZONE_SZB are not |
sewardj | fa8202f | 2005-04-24 11:05:55 +0000 | [diff] [blame] | 1110 | live; those at and above it are. |
| 1111 | |
njn | a60a7c1 | 2005-05-08 17:49:37 +0000 | [diff] [blame] | 1112 | We do not concern ourselves here with the VGA_STACK_REDZONE_SZB |
sewardj | fa8202f | 2005-04-24 11:05:55 +0000 | [diff] [blame] | 1113 | bias; that is handled by new_mem_stack/die_mem_stack. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1114 | */ |
| 1115 | |
rjwalsh | 0140af5 | 2005-06-04 20:42:33 +0000 | [diff] [blame] | 1116 | /* |
| 1117 | * This structure holds information about the start and end addresses of |
| 1118 | * registered stacks. There's always at least one stack registered: |
| 1119 | * the main process stack. It will be the first stack registered and |
| 1120 | * so will have a stack id of 0. The user does not need to register |
| 1121 | * this stack: Valgrind does it automatically right before it starts |
| 1122 | * running the client. No other stacks are automatically registered by |
| 1123 | * Valgrind, however. |
| 1124 | */ |
| 1125 | |
| 1126 | typedef struct _Stack { |
| 1127 | UWord id; |
| 1128 | Addr start; |
| 1129 | Addr end; |
| 1130 | struct _Stack *next; |
| 1131 | } Stack; |
| 1132 | |
| 1133 | static Stack *stacks; |
| 1134 | static UWord next_id; /* Next id we hand out to a newly registered stack */ |
| 1135 | |
| 1136 | /* |
| 1137 | * These are the id, start and end values of the current stack. If the |
| 1138 | * stack pointer falls outside the range of the current stack, we search |
| 1139 | * the stacks list above for a matching stack. |
| 1140 | */ |
| 1141 | |
| 1142 | static Addr current_stack_start; |
| 1143 | static Addr current_stack_end; |
| 1144 | static UWord current_stack_id; |
| 1145 | |
| 1146 | /* Search for a particular stack by id number. */ |
| 1147 | static Bool find_stack_by_id(UWord id, Addr *start, Addr *end) |
| 1148 | { |
| 1149 | Stack *i = stacks; |
| 1150 | while(i) { |
| 1151 | if(i->id == id) { |
| 1152 | *start = i->start; |
| 1153 | *end = i->end; |
| 1154 | return True; |
| 1155 | } |
| 1156 | i = i->next; |
| 1157 | } |
| 1158 | return False; |
| 1159 | } |
| 1160 | |
| 1161 | /* Find what stack an address falls into. */ |
| 1162 | static Bool find_stack_by_addr(Addr sp, Addr *start, Addr *end, UWord *id) |
| 1163 | { |
| 1164 | Stack *i = stacks; |
| 1165 | while(i) { |
| 1166 | if(sp >= i->start && sp <= i->end) { |
| 1167 | *start = i->start; |
| 1168 | *end = i->end; |
| 1169 | *id = i->id; |
| 1170 | return True; |
| 1171 | } |
| 1172 | i = i->next; |
| 1173 | } |
| 1174 | return False; |
| 1175 | } |
| 1176 | |
| 1177 | /* Change over to a new stack. */ |
| 1178 | static Bool set_current_stack(UWord id) |
| 1179 | { |
| 1180 | Addr start, end; |
| 1181 | if (find_stack_by_id(id, &start, &end)) { |
| 1182 | current_stack_id = id; |
| 1183 | current_stack_start = start; |
| 1184 | current_stack_end = end; |
| 1185 | return True; |
| 1186 | } |
| 1187 | return False; |
| 1188 | } |
| 1189 | |
| 1190 | /* |
| 1191 | * Register a new stack from start - end. This is invoked from the |
| 1192 | * VALGRIND_STACK_REGISTER client request, and is also called just before |
| 1193 | * we start the client running, to register the main process stack. |
| 1194 | * |
| 1195 | * Note: this requires allocating a piece of memory to store the Stack |
| 1196 | * structure, which places a dependency between this module and the |
| 1197 | * mallocfree module. However, there is no real chance of a circular |
| 1198 | * dependency here, since the mallocfree module would never call back to |
| 1199 | * this function. |
| 1200 | */ |
| 1201 | |
| 1202 | UWord VG_(handle_stack_register)(Addr start, Addr end) |
| 1203 | { |
| 1204 | Stack *i; |
| 1205 | if (start > end) { |
| 1206 | Addr t = end; |
| 1207 | end = start; |
| 1208 | start = t; |
| 1209 | } |
| 1210 | |
| 1211 | i = (Stack *)VG_(arena_malloc)(VG_AR_CORE, sizeof(Stack)); |
| 1212 | i->start = start; |
| 1213 | i->end = end; |
| 1214 | i->id = next_id++; |
| 1215 | i->next = stacks; |
| 1216 | stacks = i; |
| 1217 | |
| 1218 | if(i->id == 0) { |
| 1219 | set_current_stack(i->id); |
| 1220 | } |
| 1221 | |
| 1222 | return i->id; |
| 1223 | } |
| 1224 | |
| 1225 | /* |
| 1226 | * Deregister a stack. This is invoked from the VALGRIND_STACK_DEREGISTER |
| 1227 | * client request. |
| 1228 | * |
| 1229 | * Note: this requires freeing the piece of memory that was used to store |
| 1230 | * the Stack structure, which places a dependency between this module |
| 1231 | * and the mallocfree module. However, there is no real chance of |
| 1232 | * a circular dependency here, since the mallocfree module would never |
| 1233 | * call back to this function. |
| 1234 | */ |
| 1235 | |
| 1236 | void VG_(handle_stack_deregister)(UWord id) |
| 1237 | { |
| 1238 | Stack *i = stacks; |
| 1239 | Stack *prev = NULL; |
| 1240 | |
| 1241 | if(current_stack_id == id) { |
| 1242 | return; |
| 1243 | } |
| 1244 | |
| 1245 | while(i) { |
| 1246 | if (i->id == id) { |
| 1247 | if(prev == NULL) { |
| 1248 | stacks = i->next; |
| 1249 | } else { |
| 1250 | prev->next = i->next; |
| 1251 | } |
| 1252 | VG_(arena_free)(VG_AR_CORE, i); |
| 1253 | return; |
| 1254 | } |
| 1255 | prev = i; |
| 1256 | i = i->next; |
| 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | /* |
| 1261 | * Change a stack. This is invoked from the VALGRIND_STACK_CHANGE client |
| 1262 | * request and from the stack growth stuff the signals module when |
| 1263 | * extending the main process stack. |
| 1264 | */ |
| 1265 | |
| 1266 | void VG_(handle_stack_change)(UWord id, Addr start, Addr end) |
| 1267 | { |
| 1268 | Stack *i = stacks; |
| 1269 | |
| 1270 | if (id == current_stack_id) { |
| 1271 | current_stack_start = start; |
| 1272 | current_stack_end = end; |
| 1273 | } |
| 1274 | |
| 1275 | while(i) { |
| 1276 | if (i->id == id) { |
| 1277 | i->start = start; |
| 1278 | i->end = end; |
| 1279 | return; |
| 1280 | } |
| 1281 | i = i->next; |
| 1282 | } |
| 1283 | } |
| 1284 | |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 1285 | /* This function gets called if new_mem_stack and/or die_mem_stack are |
sewardj | fa8202f | 2005-04-24 11:05:55 +0000 | [diff] [blame] | 1286 | tracked by the tool, and one of the specialised cases |
| 1287 | (eg. new_mem_stack_4) isn't used in preference. |
| 1288 | */ |
njn | 9fb73db | 2005-03-27 01:55:21 +0000 | [diff] [blame] | 1289 | VGA_REGPARM(2) |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 1290 | void VG_(unknown_SP_update)( Addr old_SP, Addr new_SP ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1291 | { |
sewardj | 97724e5 | 2005-04-02 23:40:59 +0000 | [diff] [blame] | 1292 | static Int moans = 3; |
nethercote | fc646ee | 2004-11-04 19:29:06 +0000 | [diff] [blame] | 1293 | Word delta = (Word)new_SP - (Word)old_SP; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1294 | |
rjwalsh | 0140af5 | 2005-06-04 20:42:33 +0000 | [diff] [blame] | 1295 | /* Check if the stack pointer is still in the same stack as before. */ |
| 1296 | if (new_SP < current_stack_start || new_SP > current_stack_end) { |
| 1297 | Addr start, end; |
| 1298 | UWord new_id; |
| 1299 | Bool found = find_stack_by_addr(new_SP, &start, &end, &new_id); |
| 1300 | if (found && new_id != current_stack_id) { |
| 1301 | /* The stack pointer is now in another stack. Update the current |
| 1302 | stack information and return without doing anything else. */ |
| 1303 | set_current_stack(new_id); |
| 1304 | return; |
| 1305 | } |
| 1306 | } |
| 1307 | |
sewardj | 97724e5 | 2005-04-02 23:40:59 +0000 | [diff] [blame] | 1308 | if (delta < -VG_(clo_max_stackframe) || VG_(clo_max_stackframe) < delta) { |
| 1309 | /* SP has changed by more than some threshold amount (by |
| 1310 | default, 2MB). We take this to mean that the application is |
| 1311 | switching to a new stack, for whatever reason. |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 1312 | |
| 1313 | JRS 20021001: following discussions with John Regehr, if a stack |
| 1314 | switch happens, it seems best not to mess at all with memory |
| 1315 | permissions. Seems to work well with Netscape 4.X. Really the |
| 1316 | only remaining difficulty is knowing exactly when a stack switch is |
| 1317 | happening. */ |
sewardj | 97724e5 | 2005-04-02 23:40:59 +0000 | [diff] [blame] | 1318 | if (VG_(clo_verbosity) > 0 && moans > 0) { |
| 1319 | moans--; |
| 1320 | VG_(message)(Vg_UserMsg, |
| 1321 | "Warning: client switching stacks? " |
| 1322 | "SP change: %p --> %p", old_SP, new_SP); |
| 1323 | VG_(message)(Vg_UserMsg, |
| 1324 | " to suppress, use: --max-stackframe=%d or greater", |
| 1325 | (delta < 0 ? -delta : delta)); |
| 1326 | if (moans == 0) |
| 1327 | VG_(message)(Vg_UserMsg, |
| 1328 | " further instances of this message " |
| 1329 | "will not be shown."); |
| 1330 | } |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 1331 | } else if (delta < 0) { |
nethercote | 15218bd | 2004-09-11 15:11:47 +0000 | [diff] [blame] | 1332 | VG_TRACK( new_mem_stack, new_SP, -delta ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1333 | |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 1334 | } else if (delta > 0) { |
nethercote | 15218bd | 2004-09-11 15:11:47 +0000 | [diff] [blame] | 1335 | VG_TRACK( die_mem_stack, old_SP, delta ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1336 | } |
| 1337 | } |
| 1338 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1339 | /* |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1340 | Test if a piece of memory is addressable with at least the "prot" |
| 1341 | protection permissions by examining the underlying segments. |
| 1342 | |
| 1343 | Really this is a very stupid algorithm and we could do much |
| 1344 | better by iterating through the segment array instead of through |
| 1345 | the address space. |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1346 | */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1347 | Bool VG_(is_addressable)(Addr p, SizeT size, UInt prot) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1348 | { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1349 | Segment *seg; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1350 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1351 | if ((p + size) < p) |
| 1352 | return False; /* reject wraparounds */ |
| 1353 | if (size == 0) |
| 1354 | return True; /* isn't this a bit of a strange case? */ |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1355 | |
njn | 13bfd85 | 2005-06-02 03:52:53 +0000 | [diff] [blame] | 1356 | p = VG_PGROUNDDN(p); |
| 1357 | size = VG_PGROUNDUP(size); |
njn | be91aae | 2005-03-27 01:42:41 +0000 | [diff] [blame] | 1358 | vg_assert(VG_IS_PAGE_ALIGNED(p)); |
| 1359 | vg_assert(VG_IS_PAGE_ALIGNED(size)); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1360 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1361 | for (; size > 0; size -= VKI_PAGE_SIZE) { |
| 1362 | seg = VG_(find_segment)(p); |
| 1363 | if (!seg) |
| 1364 | return False; |
| 1365 | if ((seg->prot & prot) != prot) |
| 1366 | return False; |
| 1367 | p += VKI_PAGE_SIZE; |
| 1368 | } |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1369 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1370 | return True; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1373 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1374 | /*--------------------------------------------------------------------*/ |
njn | 9260c78 | 2005-06-19 21:57:54 +0000 | [diff] [blame^] | 1375 | /*--- Random function that doesn't really belong here ---*/ |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1376 | /*--------------------------------------------------------------------*/ |
| 1377 | |
njn | 86f12dc | 2005-03-14 01:16:05 +0000 | [diff] [blame] | 1378 | /* We'll call any RW mmaped memory segment, within the client address |
sewardj | 882af5f | 2005-03-16 12:27:53 +0000 | [diff] [blame] | 1379 | range, which isn't SF_CORE, a root. |
| 1380 | */ |
njn | 86f12dc | 2005-03-14 01:16:05 +0000 | [diff] [blame] | 1381 | void VG_(find_root_memory)(void (*add_rootrange)(Addr a, SizeT sz)) |
| 1382 | { |
sewardj | 882af5f | 2005-03-16 12:27:53 +0000 | [diff] [blame] | 1383 | Int i; |
| 1384 | UInt flags; |
njn | 86f12dc | 2005-03-14 01:16:05 +0000 | [diff] [blame] | 1385 | Segment *s; |
| 1386 | |
sewardj | 882af5f | 2005-03-16 12:27:53 +0000 | [diff] [blame] | 1387 | for (i = 0; i < segments_used; i++) { |
| 1388 | s = &segments[i]; |
| 1389 | flags = s->flags & (SF_SHARED|SF_MMAP|SF_VALGRIND |
| 1390 | |SF_CORE|SF_STACK|SF_DEVICE); |
njn | 86f12dc | 2005-03-14 01:16:05 +0000 | [diff] [blame] | 1391 | if (flags != SF_MMAP && flags != SF_STACK) |
| 1392 | continue; |
sewardj | 882af5f | 2005-03-16 12:27:53 +0000 | [diff] [blame] | 1393 | if ((s->prot & (VKI_PROT_READ|VKI_PROT_WRITE)) |
| 1394 | != (VKI_PROT_READ|VKI_PROT_WRITE)) |
njn | 86f12dc | 2005-03-14 01:16:05 +0000 | [diff] [blame] | 1395 | continue; |
| 1396 | if (!VG_(is_client_addr)(s->addr) || |
| 1397 | !VG_(is_client_addr)(s->addr+s->len)) |
| 1398 | continue; |
| 1399 | |
| 1400 | (*add_rootrange)(s->addr, s->len); |
| 1401 | } |
njn | 86f12dc | 2005-03-14 01:16:05 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
| 1404 | |
nethercote | 88a9016 | 2004-07-10 16:59:25 +0000 | [diff] [blame] | 1405 | /*--------------------------------------------------------------------*/ |
| 1406 | /*--- Querying memory layout ---*/ |
| 1407 | /*--------------------------------------------------------------------*/ |
| 1408 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1409 | Bool VG_(is_client_addr)(Addr a) |
| 1410 | { |
| 1411 | return a >= VG_(client_base) && a < VG_(client_end); |
| 1412 | } |
| 1413 | |
| 1414 | Bool VG_(is_shadow_addr)(Addr a) |
| 1415 | { |
| 1416 | return a >= VG_(shadow_base) && a < VG_(shadow_end); |
| 1417 | } |
| 1418 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1419 | Addr VG_(get_shadow_size)(void) |
| 1420 | { |
| 1421 | return VG_(shadow_end)-VG_(shadow_base); |
| 1422 | } |
| 1423 | |
sewardj | fa8202f | 2005-04-24 11:05:55 +0000 | [diff] [blame] | 1424 | |
nethercote | 88a9016 | 2004-07-10 16:59:25 +0000 | [diff] [blame] | 1425 | /*--------------------------------------------------------------------*/ |
nethercote | 2f54e0d | 2004-07-10 17:27:20 +0000 | [diff] [blame] | 1426 | /*--- Handling shadow memory ---*/ |
nethercote | 88a9016 | 2004-07-10 16:59:25 +0000 | [diff] [blame] | 1427 | /*--------------------------------------------------------------------*/ |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1428 | |
| 1429 | void VG_(init_shadow_range)(Addr p, UInt sz, Bool call_init) |
| 1430 | { |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 1431 | vg_assert(0); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1432 | if (0) |
| 1433 | VG_(printf)("init_shadow_range(%p, %d)\n", p, sz); |
| 1434 | |
| 1435 | vg_assert(VG_(needs).shadow_memory); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1436 | vg_assert(VG_(tdict).track_init_shadow_page); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1437 | |
njn | 13bfd85 | 2005-06-02 03:52:53 +0000 | [diff] [blame] | 1438 | sz = VG_PGROUNDUP(p+sz) - VG_PGROUNDDN(p); |
| 1439 | p = VG_PGROUNDDN(p); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1440 | |
| 1441 | VG_(mprotect)((void *)p, sz, VKI_PROT_READ|VKI_PROT_WRITE); |
| 1442 | |
| 1443 | if (call_init) |
| 1444 | while(sz) { |
nethercote | 996901a | 2004-08-03 13:29:09 +0000 | [diff] [blame] | 1445 | /* ask the tool to initialize each page */ |
njn | 13bfd85 | 2005-06-02 03:52:53 +0000 | [diff] [blame] | 1446 | VG_TRACK( init_shadow_page, VG_PGROUNDDN(p) ); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1447 | |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 1448 | p += VKI_PAGE_SIZE; |
| 1449 | sz -= VKI_PAGE_SIZE; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | void *VG_(shadow_alloc)(UInt size) |
| 1454 | { |
| 1455 | static Addr shadow_alloc = 0; |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 1456 | Addr try_here; |
njn | e9befc6 | 2005-06-11 15:51:30 +0000 | [diff] [blame] | 1457 | SysRes r; |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 1458 | |
| 1459 | if (0) show_segments("shadow_alloc(before)"); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1460 | |
| 1461 | vg_assert(VG_(needs).shadow_memory); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1462 | vg_assert(!VG_(tdict).track_init_shadow_page); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1463 | |
njn | 13bfd85 | 2005-06-02 03:52:53 +0000 | [diff] [blame] | 1464 | size = VG_PGROUNDUP(size); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1465 | |
| 1466 | if (shadow_alloc == 0) |
| 1467 | shadow_alloc = VG_(shadow_base); |
| 1468 | |
| 1469 | if (shadow_alloc >= VG_(shadow_end)) |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 1470 | goto failed; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1471 | |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 1472 | try_here = shadow_alloc; |
njn | be91aae | 2005-03-27 01:42:41 +0000 | [diff] [blame] | 1473 | vg_assert(VG_IS_PAGE_ALIGNED(try_here)); |
| 1474 | vg_assert(VG_IS_PAGE_ALIGNED(size)); |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 1475 | vg_assert(size > 0); |
| 1476 | |
| 1477 | if (0) |
| 1478 | VG_(printf)("shadow_alloc: size %d, trying at %p\n", size, (void*)try_here); |
| 1479 | |
| 1480 | /* this is big-bang allocated, so we don't expect to find a listed |
| 1481 | segment for it. */ |
| 1482 | /* This is really an absolute disgrace. Sometimes the big-bang |
| 1483 | mapping is in the list (due to re-reads of /proc/self/maps, |
| 1484 | presumably) and sometimes it isn't. */ |
| 1485 | #if 0 |
| 1486 | r = find_segment(try_here); |
| 1487 | vg_assert(r == -1); |
| 1488 | r = find_segment(try_here+size-1); |
| 1489 | vg_assert(r == -1); |
| 1490 | #endif |
| 1491 | |
| 1492 | r = VG_(mprotect_native)( (void*)try_here, |
| 1493 | size, VKI_PROT_READ|VKI_PROT_WRITE ); |
| 1494 | |
njn | e9befc6 | 2005-06-11 15:51:30 +0000 | [diff] [blame] | 1495 | if (r.isError) |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 1496 | goto failed; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1497 | |
| 1498 | shadow_alloc += size; |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 1499 | return (void*)try_here; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1500 | |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 1501 | failed: |
| 1502 | VG_(printf)( |
sewardj | 96db5e1 | 2005-03-21 17:43:34 +0000 | [diff] [blame] | 1503 | "valgrind: Could not allocate address space (0x%x bytes)\n" |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 1504 | "valgrind: for shadow memory chunk.\n", |
sewardj | 96db5e1 | 2005-03-21 17:43:34 +0000 | [diff] [blame] | 1505 | size |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 1506 | ); |
| 1507 | VG_(exit)(1); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
njn | e3f0635 | 2005-06-01 03:48:33 +0000 | [diff] [blame] | 1510 | /*------------------------------------------------------------*/ |
| 1511 | /*--- pointercheck ---*/ |
| 1512 | /*------------------------------------------------------------*/ |
| 1513 | |
| 1514 | Bool VGA_(setup_pointercheck)(Addr client_base, Addr client_end) |
| 1515 | { |
| 1516 | vg_assert(0 != client_end); |
| 1517 | #if defined(VGP_x86_linux) |
| 1518 | /* Client address space segment limit descriptor entry */ |
| 1519 | #define POINTERCHECK_SEGIDX 1 |
| 1520 | |
| 1521 | vki_modify_ldt_t ldt = { |
| 1522 | POINTERCHECK_SEGIDX, // entry_number |
| 1523 | client_base, // base_addr |
| 1524 | (client_end - client_base) / VKI_PAGE_SIZE, // limit |
| 1525 | 1, // seg_32bit |
| 1526 | 0, // contents: data, RW, non-expanding |
| 1527 | 0, // ! read_exec_only |
| 1528 | 1, // limit_in_pages |
| 1529 | 0, // ! seg not present |
| 1530 | 1, // useable |
| 1531 | }; |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 1532 | SysRes ret = VG_(do_syscall3)(__NR_modify_ldt, 1, (UWord)&ldt, sizeof(ldt)); |
| 1533 | if (ret.isError) { |
njn | e3f0635 | 2005-06-01 03:48:33 +0000 | [diff] [blame] | 1534 | VG_(message)(Vg_UserMsg, |
| 1535 | "Warning: ignoring --pointercheck=yes, " |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 1536 | "because modify_ldt failed (errno=%d)", ret.val); |
njn | e3f0635 | 2005-06-01 03:48:33 +0000 | [diff] [blame] | 1537 | return False; |
| 1538 | } else { |
| 1539 | return True; |
| 1540 | } |
| 1541 | #elif defined(VGP_amd64_linux) |
| 1542 | if (0) |
| 1543 | VG_(message)(Vg_DebugMsg, "ignoring --pointercheck (unimplemented)"); |
| 1544 | return True; |
| 1545 | #else |
| 1546 | # error Unknown architecture |
| 1547 | #endif |
| 1548 | } |
| 1549 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1550 | /*--------------------------------------------------------------------*/ |
sewardj | 55f9d1a | 2005-04-25 11:11:44 +0000 | [diff] [blame] | 1551 | /*--- end aspacemgr.c ---*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1552 | /*--------------------------------------------------------------------*/ |