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 ---*/ |
njn | 945ed2e | 2005-06-24 03:28:30 +0000 | [diff] [blame] | 5 | /*--- m_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" |
njn | 899ce73 | 2005-06-21 00:28:11 +0000 | [diff] [blame] | 34 | #include "pub_core_debuginfo.h" // Needed for pub_core_aspacemgr :( |
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 | 67741c6 | 2005-06-20 02:23:07 +0000 | [diff] [blame] | 38 | #include "pub_core_libcfile.h" // For VG_(fstat), VG_(resolve_filename_nodup) |
njn | 36a20fa | 2005-06-03 03:08:39 +0000 | [diff] [blame] | 39 | #include "pub_core_libcprint.h" |
njn | 9abd608 | 2005-06-17 21:31:45 +0000 | [diff] [blame] | 40 | #include "pub_core_syscall.h" |
njn | 43b9a8a | 2005-05-10 04:37:01 +0000 | [diff] [blame] | 41 | #include "pub_core_tooliface.h" |
njn | 945ed2e | 2005-06-24 03:28:30 +0000 | [diff] [blame] | 42 | #include "pub_core_transtab.h" // For VG_(discard_translations) |
njn | 3c660b6 | 2005-05-13 22:18:47 +0000 | [diff] [blame] | 43 | #include "vki_unistd.h" |
sewardj | 55f9d1a | 2005-04-25 11:11:44 +0000 | [diff] [blame] | 44 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 45 | |
sewardj | a449568 | 2002-10-21 07:29:59 +0000 | [diff] [blame] | 46 | /* Define to debug the memory-leak-detector. */ |
| 47 | /* #define VG_DEBUG_LEAKCHECK */ |
| 48 | |
sewardj | e517b80 | 2005-02-16 01:58:51 +0000 | [diff] [blame] | 49 | static const Bool mem_debug = False; |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 50 | |
| 51 | /*--------------------------------------------------------------*/ |
njn | 04e1698 | 2005-05-31 00:23:43 +0000 | [diff] [blame] | 52 | /*--- Basic globals about the address space. ---*/ |
| 53 | /*--------------------------------------------------------------*/ |
| 54 | |
| 55 | /* Client address space, lowest to highest (see top of ume.c) */ |
| 56 | Addr VG_(client_base); /* client address space limits */ |
| 57 | Addr VG_(client_end); |
| 58 | Addr VG_(client_mapbase); |
njn | 04e1698 | 2005-05-31 00:23:43 +0000 | [diff] [blame] | 59 | Addr VG_(clstk_base); |
| 60 | Addr VG_(clstk_end); |
rjwalsh | 0140af5 | 2005-06-04 20:42:33 +0000 | [diff] [blame] | 61 | UWord VG_(clstk_id); |
njn | 04e1698 | 2005-05-31 00:23:43 +0000 | [diff] [blame] | 62 | |
| 63 | Addr VG_(brk_base); /* start of brk */ |
| 64 | Addr VG_(brk_limit); /* current brk */ |
| 65 | |
| 66 | Addr VG_(shadow_base); /* tool's shadow memory */ |
| 67 | Addr VG_(shadow_end); |
| 68 | |
| 69 | Addr VG_(valgrind_base); /* valgrind's address range */ |
| 70 | |
| 71 | // Note that VG_(valgrind_last) names the last byte of the section, whereas |
| 72 | // the VG_(*_end) vars name the byte one past the end of the section. |
| 73 | Addr VG_(valgrind_last); |
| 74 | |
| 75 | /*--------------------------------------------------------------*/ |
njn | b69f7c1 | 2005-06-24 22:17:38 +0000 | [diff] [blame] | 76 | /*--- The raw mman syscalls ---*/ |
| 77 | /*--------------------------------------------------------------*/ |
| 78 | |
| 79 | SysRes VG_(mmap_native)(void *start, SizeT length, UInt prot, UInt flags, |
| 80 | UInt fd, OffT offset) |
| 81 | { |
| 82 | SysRes res; |
| 83 | #if defined(VGP_x86_linux) |
| 84 | { |
| 85 | UWord args[6]; |
| 86 | args[0] = (UWord)start; |
| 87 | args[1] = length; |
| 88 | args[2] = prot; |
| 89 | args[3] = flags; |
| 90 | args[4] = fd; |
| 91 | args[5] = offset; |
| 92 | res = VG_(do_syscall1)(__NR_mmap, (UWord)args ); |
| 93 | } |
| 94 | #elif defined(VGP_amd64_linux) |
| 95 | res = VG_(do_syscall6)(__NR_mmap, (UWord)start, length, |
| 96 | prot, flags, fd, offset); |
| 97 | #elif defined(VGP_ppc32_linux) |
| 98 | res = VG_(do_syscall6)(__NR_mmap, (UWord)(start), (length), |
| 99 | prot, flags, fd, offset); |
| 100 | #else |
| 101 | # error Unknown platform |
| 102 | #endif |
| 103 | return res; |
| 104 | } |
| 105 | |
| 106 | SysRes VG_(munmap_native)(void *start, SizeT length) |
| 107 | { |
| 108 | return VG_(do_syscall2)(__NR_munmap, (UWord)start, length ); |
| 109 | } |
| 110 | |
| 111 | SysRes VG_(mprotect_native)( void *start, SizeT length, UInt prot ) |
| 112 | { |
| 113 | return VG_(do_syscall3)(__NR_mprotect, (UWord)start, length, prot ); |
| 114 | } |
| 115 | |
| 116 | /*--------------------------------------------------------------*/ |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 117 | /*--- A simple, self-contained ordered array of segments. ---*/ |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 118 | /*--------------------------------------------------------------*/ |
| 119 | |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 120 | /* Max number of segments we can track. */ |
| 121 | #define VG_N_SEGMENTS 1000 |
| 122 | |
| 123 | /* Max number of segment file names we can track. */ |
| 124 | #define VG_N_SEGNAMES 200 |
| 125 | |
| 126 | /* Max length of a segment file name. */ |
| 127 | #define VG_MAX_SEGNAMELEN 1000 |
| 128 | |
| 129 | |
| 130 | /* ------ STATE for the address-space manager ------ */ |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 131 | |
| 132 | /* Array [0 .. segments_used-1] of all mappings. */ |
| 133 | /* Sorted by .addr field. */ |
| 134 | /* I: len may not be zero. */ |
| 135 | /* I: overlapping segments are not allowed. */ |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 136 | /* Each segment can optionally hold an index into the filename table. */ |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 137 | |
| 138 | static Segment segments[VG_N_SEGMENTS]; |
| 139 | static Int segments_used = 0; |
| 140 | |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 141 | typedef |
| 142 | struct { |
| 143 | Bool inUse; |
| 144 | Bool mark; |
| 145 | HChar fname[VG_MAX_SEGNAMELEN]; |
| 146 | } |
| 147 | SegName; |
| 148 | |
| 149 | /* Filename table. _used is the high water mark; an entry is only |
| 150 | valid if its index >= 0, < _used, and its .inUse field == True. |
| 151 | The .mark field is used to garbage-collect dead entries. |
| 152 | */ |
| 153 | static SegName segnames[VG_N_SEGNAMES]; |
| 154 | static Int segnames_used = 0; |
| 155 | |
| 156 | |
| 157 | /* ------ end of STATE for the address-space manager ------ */ |
| 158 | |
| 159 | |
| 160 | /* Searches the filename table to find an index for the given name. |
| 161 | If none is found, an index is allocated and the name stored. If no |
| 162 | space is available we just give up. If the string is too long to |
| 163 | store, return -1. |
| 164 | */ |
| 165 | static Int allocate_segname ( const HChar* name ) |
| 166 | { |
| 167 | Int i, j, len; |
| 168 | |
| 169 | vg_assert(name); |
| 170 | |
| 171 | if (0) VG_(printf)("alloc_segname %s\n", name); |
| 172 | |
| 173 | len = VG_(strlen)(name); |
| 174 | if (len >= VG_MAX_SEGNAMELEN-1) { |
| 175 | return -1; |
| 176 | } |
| 177 | |
| 178 | /* first see if we already have the name. */ |
| 179 | for (i = 0; i < segnames_used; i++) { |
| 180 | if (!segnames[i].inUse) |
| 181 | continue; |
| 182 | if (0 == VG_(strcmp)(name, &segnames[i].fname[0])) { |
| 183 | return i; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | /* no we don't. So look for a free slot. */ |
| 188 | for (i = 0; i < segnames_used; i++) |
| 189 | if (!segnames[i].inUse) |
| 190 | break; |
| 191 | |
| 192 | if (i == segnames_used) { |
| 193 | /* no free slots .. advance the high-water mark. */ |
| 194 | if (segnames_used+1 < VG_N_SEGNAMES) { |
| 195 | i = segnames_used; |
| 196 | segnames_used++; |
| 197 | } else { |
| 198 | VG_(printf)( |
tom | 3a07e9b | 2005-05-17 11:03:52 +0000 | [diff] [blame] | 199 | "coregrind/m_aspacemgr/aspacemgr.c:\n" |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 200 | " VG_N_SEGNAMES is too small: " |
| 201 | "increase it and rebuild Valgrind.\n" |
| 202 | ); |
| 203 | VG_(printf)( |
tom | 3a07e9b | 2005-05-17 11:03:52 +0000 | [diff] [blame] | 204 | "coregrind/m_aspacemgr/aspacemgr.c:\n" |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 205 | " giving up now.\n\n" |
| 206 | ); |
| 207 | VG_(exit)(0); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | /* copy it in */ |
| 212 | segnames[i].inUse = True; |
| 213 | for (j = 0; j < len; j++) |
| 214 | segnames[i].fname[j] = name[j]; |
| 215 | vg_assert(len < VG_MAX_SEGNAMELEN); |
| 216 | segnames[i].fname[len] = 0; |
| 217 | return i; |
| 218 | } |
| 219 | |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 220 | |
| 221 | /* Returns -1 if 'a' denotes an address prior to seg, 1 if it denotes |
| 222 | an address after it, and 0 if it denotes an address covered by |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 223 | seg. |
| 224 | */ |
sewardj | 447f2a1 | 2005-07-07 13:52:53 +0000 | [diff] [blame] | 225 | static inline Int compare_addr_with_seg ( Addr a, Segment* seg ) |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 226 | { |
| 227 | if (a < seg->addr) |
| 228 | return -1; |
| 229 | if (a >= seg->addr + seg->len) |
| 230 | return 1; |
| 231 | return 0; |
| 232 | } |
| 233 | |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 234 | |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 235 | /* Find the (index of the) segment that contains 'a', or -1 if |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 236 | none. |
| 237 | */ |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 238 | static Int find_segment ( Addr a ) |
| 239 | { |
| 240 | Int i; |
| 241 | for (i = 0; i < segments_used; i++) { |
| 242 | if (compare_addr_with_seg(a, &segments[i]) == 0) |
| 243 | return i; |
| 244 | } |
| 245 | return -1; |
| 246 | } |
| 247 | |
| 248 | |
| 249 | /* Assumes that 'a' is not in any segment. Finds the index of the |
| 250 | lowest-addressed segment above 'a', or -1 if none. Passing 'a' |
| 251 | which is in fact in a segment is a checked error. |
| 252 | */ |
| 253 | static Int find_segment_above_unmapped ( Addr a ) |
| 254 | { |
| 255 | Int i, r; |
| 256 | for (i = 0; i < segments_used; i++) { |
| 257 | r = compare_addr_with_seg(a, &segments[i]); |
| 258 | vg_assert(r != 0); /* 'a' should not be in any segment. */ |
| 259 | if (r == 1) |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 260 | continue; |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 261 | vg_assert(r == -1); |
| 262 | break; |
| 263 | } |
| 264 | |
| 265 | if (i == segments_used) |
| 266 | return -1; /* not found */ |
| 267 | else |
| 268 | return i; |
| 269 | } |
| 270 | |
| 271 | |
| 272 | /* Assumes that 'a' is in some segment. Finds the next segment along, |
| 273 | or NULL if none. Passing 'a' which is in fact not in a segment is |
| 274 | a checked error. |
| 275 | */ |
| 276 | static Int find_segment_above_mapped ( Addr a ) |
| 277 | { |
| 278 | Int i, r; |
| 279 | for (i = 0; i < segments_used; i++) { |
| 280 | r = compare_addr_with_seg(a, &segments[i]); |
| 281 | if (r == 1) |
| 282 | continue; /* not yet there */ |
| 283 | if (r == 0) |
| 284 | break; /* found it */ |
| 285 | vg_assert(0); |
| 286 | /* we shouldn't get here -- r == -1 and so it means we went past |
| 287 | 'a' without seeing it -- it is therefore unmapped. */ |
| 288 | /*NOTREACHED*/ |
| 289 | } |
| 290 | |
| 291 | vg_assert(i < segments_used); |
| 292 | if (i == segments_used-1) |
| 293 | return -1; /* not found */ |
| 294 | else |
| 295 | return i+1; |
| 296 | } |
| 297 | |
| 298 | |
| 299 | /* Shift segments[i .. segments_used-1] up by one. */ |
| 300 | static void make_space_at ( Int i ) |
| 301 | { |
| 302 | Int j; |
| 303 | vg_assert(i >= 0 && i <= segments_used); |
| 304 | vg_assert(segments_used >= 0); |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 305 | if (segments_used+1 == VG_N_SEGMENTS) { |
| 306 | VG_(printf)( |
tom | 8a51bb8 | 2005-06-15 10:24:26 +0000 | [diff] [blame] | 307 | "coregrind/m_aspacemgr/aspacemgr.c:\n" |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 308 | " VG_N_SEGMENTS is too small: " |
| 309 | "increase it and rebuild Valgrind.\n" |
| 310 | ); |
| 311 | VG_(printf)( |
tom | 8a51bb8 | 2005-06-15 10:24:26 +0000 | [diff] [blame] | 312 | "coregrind/m_aspacemgr/aspacemgr.c:\n" |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 313 | " giving up now.\n\n" |
| 314 | ); |
| 315 | VG_(exit)(0); |
| 316 | } |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 317 | vg_assert(segments_used+1 < VG_N_SEGMENTS); |
| 318 | for (j = segments_used; j > i; j--) |
| 319 | segments[j] = segments[j-1]; |
| 320 | segments_used++; |
| 321 | } |
| 322 | |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 323 | |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 324 | /* Shift segments [i+1 .. segments_used-1] down by one, and decrement |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 325 | segments_used. |
| 326 | */ |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 327 | static void delete_segment_at ( Int i ) |
| 328 | { |
| 329 | Int j; |
| 330 | vg_assert(i >= 0 && i < segments_used); |
| 331 | for (j = i+1; j < segments_used; j++) |
| 332 | segments[j-1] = segments[j]; |
| 333 | segments_used--; |
| 334 | vg_assert(segments_used >= 0 && segments_used < VG_N_SEGMENTS); |
| 335 | } |
| 336 | |
| 337 | |
| 338 | /* Fill the i'th record all with zeroes. */ |
| 339 | static void zeroise_segment ( Int i ) |
| 340 | { |
| 341 | vg_assert(i >= 0 && i < segments_used); |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 342 | segments[i].prot = 0; |
| 343 | segments[i].flags = 0; |
| 344 | segments[i].addr = 0; |
| 345 | segments[i].len = 0; |
| 346 | segments[i].offset = 0; |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 347 | segments[i].filename = NULL; |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 348 | segments[i].fnIdx = -1; |
| 349 | segments[i].dev = 0; |
| 350 | segments[i].ino = 0; |
njn | 36ef6ba | 2005-05-14 18:42:26 +0000 | [diff] [blame] | 351 | segments[i].seginfo = NULL; |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 352 | } |
| 353 | |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 354 | |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 355 | /* Create a segment to contain 'a', and return its index. Or -1 if |
| 356 | this failed because some other segment already contains 'a'. If |
| 357 | successful, fill in the segment's .addr field with 'a' but leave |
| 358 | all other fields alone. |
| 359 | */ |
| 360 | static Int create_segment ( Addr a ) |
| 361 | { |
| 362 | Int i, r; |
| 363 | for (i = 0; i < segments_used; i++) { |
| 364 | r = compare_addr_with_seg( a, &segments[i] ); |
| 365 | if (r == 1) |
| 366 | continue; /* seg[i] precedes a */ |
| 367 | if (r == 0) |
| 368 | return -1; /* seg[i] contains a. Give up */ |
| 369 | vg_assert(r == -1); |
| 370 | break; |
| 371 | } |
| 372 | /* a precedes seg[i]. Shift segs at i and above up one, and use |
| 373 | this slot. */ |
| 374 | make_space_at(i); |
| 375 | zeroise_segment(i); |
| 376 | segments[i].addr = a; |
| 377 | return i; |
| 378 | } |
| 379 | |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 380 | |
| 381 | /* Print out the segment array (debugging only!). Note, this calls |
| 382 | VG_(printf), and I'm not 100% clear that that wouldn't require |
| 383 | dynamic memory allocation and hence more segments to be allocated. |
| 384 | */ |
| 385 | static void show_segments ( HChar* who ) |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 386 | { |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 387 | Int i; |
| 388 | VG_(printf)("<<< SHOW_SEGMENTS: %s (%d segments, %d segnames)\n", |
| 389 | who, segments_used, segnames_used); |
| 390 | for (i = 0; i < segnames_used; i++) { |
| 391 | if (!segnames[i].inUse) |
| 392 | continue; |
| 393 | VG_(printf)("(%2d) %s\n", i, segnames[i].fname); |
| 394 | } |
| 395 | for (i = 0; i < segments_used; i++) { |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 396 | VG_(printf)( |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 397 | "%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] | 398 | i, |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 399 | segments[i].addr, segments[i].addr + segments[i].len, |
| 400 | (ULong)segments[i].len, segments[i].prot, |
| 401 | segments[i].flags, segments[i].dev, segments[i].ino, |
| 402 | (Long)segments[i].offset, |
| 403 | segments[i].fnIdx); |
| 404 | } |
| 405 | VG_(printf)(">>>\n"); |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 406 | } |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 407 | |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 408 | |
| 409 | /* Find the segment containing 'a' and split it into two pieces at |
| 410 | 'a'. Does nothing if no segment contains 'a', or if the split |
| 411 | would cause either of the pieces to have zero size. |
| 412 | |
| 413 | If 'a' is not found, or if no splitting happens, -1 is returned. |
| 414 | |
| 415 | If a value 'r' other than -1 is returned, this is the index of the |
| 416 | higher-addressed segment resulting from the split, and the index of |
| 417 | the lower-addressed segment is r-1. |
| 418 | */ |
| 419 | static Int split_segment ( Addr a ) |
| 420 | { |
| 421 | Int r; |
| 422 | HWord delta; |
njn | be91aae | 2005-03-27 01:42:41 +0000 | [diff] [blame] | 423 | vg_assert(VG_IS_PAGE_ALIGNED(a)); |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 424 | r = find_segment(a); |
| 425 | if (r == -1) |
| 426 | /* not found */ |
| 427 | return -1; |
| 428 | if (segments[r].addr == a) |
| 429 | /* segment starts at 'a', so splitting it would create a |
| 430 | zero-sized segment */ |
| 431 | return -1; |
| 432 | |
| 433 | /* copy original; make adjustments. */ |
| 434 | vg_assert(a > segments[r].addr); |
| 435 | delta = a - segments[r].addr; |
| 436 | make_space_at(r); |
| 437 | segments[r] = segments[r+1]; |
| 438 | segments[r].len = delta; |
| 439 | segments[r+1].len -= delta; |
| 440 | segments[r+1].addr += delta; |
| 441 | segments[r+1].offset += delta; |
| 442 | return r+1; |
| 443 | } |
| 444 | |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 445 | |
| 446 | /* Return true if two segments are adjacent and mergable (s1 is |
| 447 | assumed to have a lower ->addr than s2) */ |
| 448 | static inline Bool segments_are_mergeable(Segment *s1, Segment *s2) |
| 449 | { |
| 450 | if (s1->addr+s1->len != s2->addr) |
| 451 | return False; |
| 452 | |
| 453 | if (s1->flags != s2->flags) |
| 454 | return False; |
| 455 | |
| 456 | if (s1->prot != s2->prot) |
| 457 | return False; |
| 458 | |
njn | 36ef6ba | 2005-05-14 18:42:26 +0000 | [diff] [blame] | 459 | if (s1->seginfo != s2->seginfo) |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 460 | return False; |
| 461 | |
| 462 | if (s1->flags & SF_FILE){ |
| 463 | if ((s1->offset + s1->len) != s2->offset) |
| 464 | return False; |
| 465 | if (s1->dev != s2->dev) |
| 466 | return False; |
| 467 | if (s1->ino != s2->ino) |
| 468 | return False; |
| 469 | if (s1->fnIdx != s2->fnIdx) |
| 470 | return False; |
| 471 | } |
| 472 | |
| 473 | return True; |
| 474 | } |
| 475 | |
| 476 | |
| 477 | /* Clean up and sanity check the segment array: |
| 478 | - check segments are in ascending order |
| 479 | - check segments do not overlap |
| 480 | - check no segment has zero size |
| 481 | - merge adjacent where possible |
| 482 | - perform checks on the filename table, and reclaim dead entries |
| 483 | */ |
| 484 | static void preen_segments ( void ) |
| 485 | { |
| 486 | Int i, j, rd, wr; |
| 487 | Segment *s, *s1; |
| 488 | vg_assert(segments_used >= 0 && segments_used < VG_N_SEGMENTS); |
| 489 | vg_assert(segnames_used >= 0 && segnames_used < VG_N_SEGNAMES); |
| 490 | |
| 491 | if (0) show_segments("before preen"); |
| 492 | |
| 493 | /* clear string table mark bits */ |
| 494 | for (i = 0; i < segnames_used; i++) |
| 495 | segnames[i].mark = False; |
| 496 | |
| 497 | /* check for non-zero size, and set mark bits for any used strings */ |
| 498 | for (i = 0; i < segments_used; i++) { |
| 499 | vg_assert(segments[i].len > 0); |
| 500 | j = segments[i].fnIdx; |
| 501 | vg_assert(j >= -1 && j < segnames_used); |
| 502 | if (j >= 0) { |
| 503 | vg_assert(segnames[j].inUse); |
| 504 | segnames[j].mark = True; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | /* check ascendingness and non-overlap */ |
| 509 | for (i = 0; i < segments_used-1; i++) { |
| 510 | s = &segments[i]; |
| 511 | s1 = &segments[i+1]; |
| 512 | vg_assert(s->addr < s1->addr); |
| 513 | vg_assert(s->addr + s->len <= s1->addr); |
| 514 | } |
| 515 | |
| 516 | /* merge */ |
| 517 | if (segments_used < 1) |
| 518 | return; |
| 519 | |
| 520 | wr = 1; |
| 521 | for (rd = 1; rd < segments_used; rd++) { |
| 522 | s = &segments[wr-1]; |
| 523 | s1 = &segments[rd]; |
| 524 | if (segments_are_mergeable(s,s1)) { |
| 525 | if (0) |
| 526 | VG_(printf)("merge %p-%p with %p-%p\n", |
| 527 | s->addr, s->addr+s->len, |
| 528 | s1->addr, s1->addr+s1->len); |
| 529 | s->len += s1->len; |
| 530 | continue; |
| 531 | } |
| 532 | if (wr < rd) |
| 533 | segments[wr] = segments[rd]; |
| 534 | wr++; |
| 535 | } |
| 536 | vg_assert(wr >= 0 && wr <= segments_used); |
| 537 | segments_used = wr; |
| 538 | |
| 539 | /* Free up any strings which are no longer referenced. */ |
| 540 | for (i = 0; i < segnames_used; i++) { |
| 541 | if (segnames[i].mark == False) { |
| 542 | segnames[i].inUse = False; |
| 543 | segnames[i].fname[0] = 0; |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | if (0) show_segments("after preen"); |
| 548 | } |
| 549 | |
| 550 | |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 551 | /*--------------------------------------------------------------*/ |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 552 | /*--- Maintain an ordered list of all the client's mappings ---*/ |
| 553 | /*--------------------------------------------------------------*/ |
| 554 | |
nethercote | 8991d5a | 2004-11-03 17:07:46 +0000 | [diff] [blame] | 555 | Bool VG_(seg_contains)(const Segment *s, Addr p, SizeT len) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 556 | { |
| 557 | Addr se = s->addr+s->len; |
| 558 | Addr pe = p+len; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 559 | vg_assert(pe >= p); |
| 560 | |
| 561 | return (p >= s->addr && pe <= se); |
| 562 | } |
| 563 | |
nethercote | 8991d5a | 2004-11-03 17:07:46 +0000 | [diff] [blame] | 564 | Bool VG_(seg_overlaps)(const Segment *s, Addr p, SizeT len) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 565 | { |
| 566 | Addr se = s->addr+s->len; |
| 567 | Addr pe = p+len; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 568 | vg_assert(pe >= p); |
| 569 | |
| 570 | return (p < se && pe > s->addr); |
| 571 | } |
| 572 | |
sewardj | 1024cf7 | 2005-02-28 14:39:21 +0000 | [diff] [blame] | 573 | #if 0 |
| 574 | /* 20050228: apparently unused */ |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 575 | /* Prepare a Segment structure for recycling by freeing everything |
| 576 | hanging off it. */ |
| 577 | static void recycleseg(Segment *s) |
| 578 | { |
| 579 | if (s->flags & SF_CODE) |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 580 | VG_(discard_translations)(s->addr, s->len); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 581 | |
| 582 | if (s->filename != NULL) |
| 583 | VG_(arena_free)(VG_AR_CORE, (Char *)s->filename); |
| 584 | |
| 585 | /* keep the SegInfo, if any - it probably still applies */ |
| 586 | } |
| 587 | |
| 588 | /* When freeing a Segment, also clean up every one else's ideas of |
| 589 | what was going on in that range of memory */ |
| 590 | static void freeseg(Segment *s) |
| 591 | { |
| 592 | recycleseg(s); |
njn | 36ef6ba | 2005-05-14 18:42:26 +0000 | [diff] [blame] | 593 | if (s->seginfo != NULL) { |
| 594 | VG_(seginfo_decref)(s->seginfo, s->addr); |
| 595 | s->seginfo = NULL; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | VG_(SkipNode_Free)(&sk_segments, s); |
| 599 | } |
sewardj | 1024cf7 | 2005-02-28 14:39:21 +0000 | [diff] [blame] | 600 | #endif |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 601 | |
sewardj | 1024cf7 | 2005-02-28 14:39:21 +0000 | [diff] [blame] | 602 | |
| 603 | /* Get rid of any translations arising from s. */ |
| 604 | /* Note, this is not really the job of the low level memory manager. |
| 605 | When it comes time to rewrite this subsystem, clean this up. */ |
| 606 | static void dump_translations_from ( Segment* s ) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 607 | { |
sewardj | 1024cf7 | 2005-02-28 14:39:21 +0000 | [diff] [blame] | 608 | if (s->flags & SF_CODE) { |
| 609 | VG_(discard_translations)(s->addr, s->len); |
| 610 | if (0) |
| 611 | VG_(printf)("dumping translations in %p .. %p\n", |
| 612 | s->addr, s->addr+s->len); |
| 613 | } |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 614 | } |
| 615 | |
sewardj | 1024cf7 | 2005-02-28 14:39:21 +0000 | [diff] [blame] | 616 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 617 | /* This unmaps all the segments in the range [addr, addr+len); any |
| 618 | partial mappings at the ends are truncated. */ |
nethercote | 8b5f40c | 2004-11-02 13:29:50 +0000 | [diff] [blame] | 619 | void VG_(unmap_range)(Addr addr, SizeT len) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 620 | { |
fitzhardinge | e3632c6 | 2003-12-22 10:58:06 +0000 | [diff] [blame] | 621 | static const Bool debug = False || mem_debug; |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 622 | Segment* s; |
| 623 | Addr end, s_end; |
| 624 | Int i; |
sewardj | 47c98a7 | 2005-03-12 20:36:15 +0000 | [diff] [blame] | 625 | Bool deleted; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 626 | |
| 627 | if (len == 0) |
| 628 | return; |
| 629 | |
njn | 13bfd85 | 2005-06-02 03:52:53 +0000 | [diff] [blame] | 630 | len = VG_PGROUNDUP(len); |
fitzhardinge | 1a4adf0 | 2003-12-22 10:42:59 +0000 | [diff] [blame] | 631 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 632 | if (debug) |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 633 | VG_(printf)("unmap_range(%p, %llu)\n", addr, (ULong)len); |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 634 | if (0) show_segments("unmap_range(BEFORE)"); |
fitzhardinge | 1a4adf0 | 2003-12-22 10:42:59 +0000 | [diff] [blame] | 635 | end = addr+len; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 636 | |
| 637 | /* Everything must be page-aligned */ |
njn | be91aae | 2005-03-27 01:42:41 +0000 | [diff] [blame] | 638 | vg_assert(VG_IS_PAGE_ALIGNED(addr)); |
| 639 | vg_assert(VG_IS_PAGE_ALIGNED(len)); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 640 | |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 641 | for (i = 0; i < segments_used; i++) { |
sewardj | 47c98a7 | 2005-03-12 20:36:15 +0000 | [diff] [blame] | 642 | |
| 643 | /* do not delete .. even though it looks stupid */ |
| 644 | vg_assert(i >= 0); |
| 645 | |
| 646 | deleted = False; |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 647 | s = &segments[i]; |
| 648 | s_end = s->addr + s->len; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 649 | |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 650 | if (0 && debug) |
fitzhardinge | 1a30304 | 2003-12-22 08:48:50 +0000 | [diff] [blame] | 651 | 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] | 652 | addr, end, s, s->addr, s_end, s->len); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 653 | |
fitzhardinge | 1a30304 | 2003-12-22 08:48:50 +0000 | [diff] [blame] | 654 | if (!VG_(seg_overlaps)(s, addr, len)) { |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 655 | if (0 && debug) |
fitzhardinge | 1a30304 | 2003-12-22 08:48:50 +0000 | [diff] [blame] | 656 | VG_(printf)(" (no overlap)\n"); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 657 | continue; |
fitzhardinge | 1a30304 | 2003-12-22 08:48:50 +0000 | [diff] [blame] | 658 | } |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 659 | |
| 660 | /* 4 cases: */ |
fitzhardinge | 1a30304 | 2003-12-22 08:48:50 +0000 | [diff] [blame] | 661 | if (addr > s->addr && |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 662 | addr < s_end && |
| 663 | end >= s_end) { |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 664 | /* this segment's tail is truncated by [addr, addr+len) |
| 665 | -> truncate tail |
| 666 | */ |
sewardj | 1024cf7 | 2005-02-28 14:39:21 +0000 | [diff] [blame] | 667 | dump_translations_from(s); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 668 | s->len = addr - s->addr; |
| 669 | |
| 670 | if (debug) |
tom | 9be7c98 | 2005-04-25 16:55:44 +0000 | [diff] [blame] | 671 | VG_(printf)(" case 1: s->len=%lu\n", s->len); |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 672 | } else if (addr <= s->addr && end > s->addr && end < s_end) { |
fitzhardinge | e3632c6 | 2003-12-22 10:58:06 +0000 | [diff] [blame] | 673 | /* this segment's head is truncated by [addr, addr+len) |
| 674 | -> truncate head |
| 675 | */ |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 676 | Word delta = end - s->addr; |
fitzhardinge | e3632c6 | 2003-12-22 10:58:06 +0000 | [diff] [blame] | 677 | |
| 678 | if (debug) |
tom | 9be7c98 | 2005-04-25 16:55:44 +0000 | [diff] [blame] | 679 | VG_(printf)(" case 2: s->addr=%p s->len=%lu delta=%d\n", |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 680 | s->addr, s->len, delta); |
fitzhardinge | e3632c6 | 2003-12-22 10:58:06 +0000 | [diff] [blame] | 681 | |
sewardj | 1024cf7 | 2005-02-28 14:39:21 +0000 | [diff] [blame] | 682 | dump_translations_from(s); |
fitzhardinge | e3632c6 | 2003-12-22 10:58:06 +0000 | [diff] [blame] | 683 | s->addr += delta; |
| 684 | s->offset += delta; |
| 685 | s->len -= delta; |
| 686 | |
| 687 | vg_assert(s->len != 0); |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 688 | } else if (addr <= s->addr && end >= s_end) { |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 689 | /* this segment is completely contained within [addr, addr+len) |
| 690 | -> delete segment |
| 691 | */ |
sewardj | 1024cf7 | 2005-02-28 14:39:21 +0000 | [diff] [blame] | 692 | dump_translations_from(s); |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 693 | delete_segment_at(i); |
sewardj | 47c98a7 | 2005-03-12 20:36:15 +0000 | [diff] [blame] | 694 | deleted = True; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 695 | |
| 696 | if (debug) |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 697 | VG_(printf)(" case 3: seg %d deleted\n", i); |
| 698 | } else if (addr > s->addr && end < s_end) { |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 699 | /* [addr, addr+len) is contained within a single segment |
| 700 | -> split segment into 3, delete middle portion |
| 701 | */ |
sewardj | 1024cf7 | 2005-02-28 14:39:21 +0000 | [diff] [blame] | 702 | Int i_middle; |
| 703 | dump_translations_from(s); |
| 704 | i_middle = split_segment(addr); |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 705 | vg_assert(i_middle != -1); |
| 706 | (void)split_segment(addr+len); |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 707 | vg_assert(segments[i_middle].addr == addr); |
| 708 | delete_segment_at(i_middle); |
sewardj | 47c98a7 | 2005-03-12 20:36:15 +0000 | [diff] [blame] | 709 | deleted = True; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 710 | |
| 711 | if (debug) |
| 712 | VG_(printf)(" case 4: subrange %p-%p deleted\n", |
| 713 | addr, addr+len); |
| 714 | } |
sewardj | 47c98a7 | 2005-03-12 20:36:15 +0000 | [diff] [blame] | 715 | |
| 716 | /* If we deleted this segment (or any above), those above will |
| 717 | have been moved down to fill in the hole in the segment |
| 718 | array. In order that we don't miss them, we have to |
| 719 | re-consider this slot number; hence the i--. */ |
| 720 | if (deleted) |
| 721 | i--; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 722 | } |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 723 | preen_segments(); |
| 724 | if (0) show_segments("unmap_range(AFTER)"); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 725 | } |
| 726 | |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 727 | |
| 728 | /* Add a binding of [addr,addr+len) to |
| 729 | (prot,flags,dev,ino,off,filename) in the segment array. |
| 730 | Delete/truncate any previous mapping(s) covering that range. |
| 731 | */ |
| 732 | void |
| 733 | VG_(map_file_segment)( Addr addr, SizeT len, |
| 734 | UInt prot, UInt flags, |
| 735 | UInt dev, UInt ino, ULong off, |
| 736 | const Char *filename) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 737 | { |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 738 | static const Bool debug = False || mem_debug; |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 739 | Segment* s; |
| 740 | Int idx; |
njn | 6081fce | 2005-06-25 19:45:34 +0000 | [diff] [blame] | 741 | HChar* stage2_suffix1 = "lib/valgrind/stage2"; |
| 742 | HChar* stage2_suffix2 = "coregrind/stage2"; |
| 743 | Bool is_stage2 = False; |
| 744 | |
| 745 | is_stage2 = is_stage2 || ( VG_(strstr)(filename, stage2_suffix1) != NULL ); |
| 746 | is_stage2 = is_stage2 || ( VG_(strstr)(filename, stage2_suffix2) != NULL ); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 747 | |
| 748 | if (debug) |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 749 | VG_(printf)( |
| 750 | "\n" |
tom | 9be7c98 | 2005-04-25 16:55:44 +0000 | [diff] [blame] | 751 | "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] | 752 | " dev=0x%4x ino=%d off=%ld\n" |
| 753 | " filename='%s')\n", |
| 754 | addr, (ULong)len, prot, flags, dev, ino, off, filename); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 755 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 756 | if (0) show_segments("before map_file_segment"); |
| 757 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 758 | /* Everything must be page-aligned */ |
njn | be91aae | 2005-03-27 01:42:41 +0000 | [diff] [blame] | 759 | vg_assert(VG_IS_PAGE_ALIGNED(addr)); |
njn | 13bfd85 | 2005-06-02 03:52:53 +0000 | [diff] [blame] | 760 | len = VG_PGROUNDUP(len); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 761 | |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 762 | /* Nuke/truncate any existing segment(s) covering [addr,addr+len) */ |
| 763 | VG_(unmap_range)(addr, len); |
| 764 | |
| 765 | /* and now install this one */ |
| 766 | idx = create_segment(addr); |
| 767 | vg_assert(segments_used >= 0 && segments_used <= VG_N_SEGMENTS); |
| 768 | vg_assert(idx != -1); |
| 769 | vg_assert(idx >= 0 && idx < segments_used); |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 770 | |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 771 | s = &segments[idx]; |
| 772 | vg_assert(s->addr == addr); |
| 773 | s->prot = prot; |
| 774 | s->flags = flags; |
| 775 | s->len = len; |
| 776 | s->offset = off; |
| 777 | s->fnIdx = filename==NULL ? -1 : allocate_segname(filename); |
| 778 | s->filename = s->fnIdx==-1 ? NULL : &segnames[s->fnIdx].fname[0]; |
| 779 | s->dev = dev; |
| 780 | s->ino = ino; |
njn | 36ef6ba | 2005-05-14 18:42:26 +0000 | [diff] [blame] | 781 | s->seginfo = NULL; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 782 | |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 783 | /* Clean up right now */ |
| 784 | preen_segments(); |
| 785 | if (0) show_segments("after map_file_segment"); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 786 | |
| 787 | /* If this mapping is of the beginning of a file, isn't part of |
| 788 | Valgrind, is at least readable and seems to contain an object |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 789 | file, then try reading symbols from it. |
sewardj | 78164b9 | 2005-07-09 10:36:25 +0000 | [diff] [blame] | 790 | |
| 791 | Getting this heuristic right is critical. On x86-linux, |
| 792 | objects are typically mapped twice: |
| 793 | |
| 794 | 1b8fb000-1b8ff000 r-xp 00000000 08:02 4471477 vgpreload_memcheck.so |
| 795 | 1b8ff000-1b900000 rw-p 00004000 08:02 4471477 vgpreload_memcheck.so |
| 796 | |
| 797 | whereas ppc32-linux mysteriously does this: |
| 798 | |
| 799 | 118a6000-118ad000 r-xp 00000000 08:05 14209428 vgpreload_memcheck.so |
| 800 | 118ad000-118b6000 ---p 00007000 08:05 14209428 vgpreload_memcheck.so |
| 801 | 118b6000-118bd000 rwxp 00000000 08:05 14209428 vgpreload_memcheck.so |
| 802 | |
| 803 | The third mapping should not be considered to have executable code in. |
| 804 | Therefore a test which works for both is: r and x and NOT w. Reading |
| 805 | symbols from the rwx segment -- which overlaps the r-x segment in the |
| 806 | file -- causes the redirection mechanism to redirect to addresses in |
| 807 | that third segment, which is wrong and causes crashes. |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 808 | */ |
njn | 36ef6ba | 2005-05-14 18:42:26 +0000 | [diff] [blame] | 809 | if (s->seginfo == NULL |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 810 | && ( (addr+len < VG_(valgrind_base) || addr > VG_(valgrind_last)) |
| 811 | || is_stage2 |
| 812 | ) |
njn | 88c5148 | 2005-06-25 20:49:33 +0000 | [diff] [blame] | 813 | && (flags & (SF_MMAP|SF_NOSYMS)) == SF_MMAP |
njn | 88c5148 | 2005-06-25 20:49:33 +0000 | [diff] [blame] | 814 | ) { |
sewardj | 78164b9 | 2005-07-09 10:36:25 +0000 | [diff] [blame] | 815 | if (off == 0 |
| 816 | && s->fnIdx != -1 |
| 817 | /* r, x are set */ |
| 818 | && (prot & (VKI_PROT_READ|VKI_PROT_EXEC)) == (VKI_PROT_READ|VKI_PROT_EXEC) |
| 819 | /* w is clear */ |
| 820 | && (prot & VKI_PROT_WRITE) == 0 |
| 821 | /* other checks .. */ |
| 822 | && len >= VKI_PAGE_SIZE |
| 823 | && VG_(is_object_file)((void *)addr) ) { |
njn | 88c5148 | 2005-06-25 20:49:33 +0000 | [diff] [blame] | 824 | s->seginfo = VG_(read_seg_symbols)(s->addr, s->len, s->offset, |
| 825 | s->filename); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 826 | } |
sewardj | 78164b9 | 2005-07-09 10:36:25 +0000 | [diff] [blame] | 827 | //else |
| 828 | //if (flags & SF_MMAP) { |
| 829 | // const SegInfo *info; |
| 830 | // |
| 831 | // /* Otherwise see if an existing SegInfo applies to this Segment */ |
| 832 | // for(info = VG_(next_seginfo)(NULL); |
| 833 | // info != NULL; |
| 834 | // info = VG_(next_seginfo)(info)) { |
| 835 | // if (VG_(seg_overlaps)(s, VG_(seg_start)(info), VG_(seg_size)(info))) |
| 836 | // { |
| 837 | // s->seginfo = (SegInfo *)info; |
| 838 | // VG_(seginfo_incref)((SegInfo *)info); |
| 839 | // } |
| 840 | // } |
| 841 | //} |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | /* clean up */ |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 845 | preen_segments(); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 846 | } |
| 847 | |
nethercote | 8b5f40c | 2004-11-02 13:29:50 +0000 | [diff] [blame] | 848 | void VG_(map_fd_segment)(Addr addr, SizeT len, UInt prot, UInt flags, |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 849 | Int fd, ULong off, const Char *filename) |
| 850 | { |
njn | f845f8f | 2005-06-23 02:26:47 +0000 | [diff] [blame] | 851 | Char buf[VKI_PATH_MAX]; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 852 | struct vki_stat st; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 853 | |
| 854 | st.st_dev = 0; |
| 855 | st.st_ino = 0; |
| 856 | |
| 857 | if (fd != -1 && (flags & SF_FILE)) { |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 858 | vg_assert((off & (VKI_PAGE_SIZE-1)) == 0); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 859 | |
| 860 | if (VG_(fstat)(fd, &st) < 0) |
| 861 | flags &= ~SF_FILE; |
| 862 | } |
| 863 | |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 864 | if ((flags & SF_FILE) && filename == NULL && fd != -1) |
njn | f845f8f | 2005-06-23 02:26:47 +0000 | [diff] [blame] | 865 | if (VG_(resolve_filename)(fd, buf, VKI_PATH_MAX)) |
| 866 | filename = buf; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 867 | |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 868 | VG_(map_file_segment)(addr, len, prot, flags, |
| 869 | st.st_dev, st.st_ino, off, filename); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 870 | } |
| 871 | |
nethercote | 8b5f40c | 2004-11-02 13:29:50 +0000 | [diff] [blame] | 872 | void VG_(map_segment)(Addr addr, SizeT len, UInt prot, UInt flags) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 873 | { |
| 874 | flags &= ~SF_FILE; |
| 875 | |
| 876 | VG_(map_file_segment)(addr, len, prot, flags, 0, 0, 0, 0); |
| 877 | } |
| 878 | |
| 879 | /* set new protection flags on an address range */ |
nethercote | 8b5f40c | 2004-11-02 13:29:50 +0000 | [diff] [blame] | 880 | void VG_(mprotect_range)(Addr a, SizeT len, UInt prot) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 881 | { |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 882 | Int r; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 883 | static const Bool debug = False || mem_debug; |
| 884 | |
| 885 | if (debug) |
tom | 9be7c98 | 2005-04-25 16:55:44 +0000 | [diff] [blame] | 886 | VG_(printf)("\nmprotect_range(%p, %lu, %x)\n", a, len, prot); |
sewardj | e517b80 | 2005-02-16 01:58:51 +0000 | [diff] [blame] | 887 | |
| 888 | if (0) show_segments( "mprotect_range(before)" ); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 889 | |
| 890 | /* Everything must be page-aligned */ |
njn | be91aae | 2005-03-27 01:42:41 +0000 | [diff] [blame] | 891 | vg_assert(VG_IS_PAGE_ALIGNED(a)); |
njn | 13bfd85 | 2005-06-02 03:52:53 +0000 | [diff] [blame] | 892 | len = VG_PGROUNDUP(len); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 893 | |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 894 | split_segment(a); |
| 895 | split_segment(a+len); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 896 | |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 897 | r = find_segment(a); |
| 898 | vg_assert(r != -1); |
| 899 | segments[r].prot = prot; |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 900 | |
| 901 | preen_segments(); |
| 902 | |
sewardj | e517b80 | 2005-02-16 01:58:51 +0000 | [diff] [blame] | 903 | if (0) show_segments( "mprotect_range(after)"); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 904 | } |
| 905 | |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 906 | |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 907 | /* Try to find a map space for [addr,addr+len). If addr==0, it means |
| 908 | the caller is prepared to accept a space at any location; if not, |
| 909 | we will try for addr, but fail if we can't get it. This mimics |
| 910 | mmap fixed vs mmap not-fixed. |
| 911 | */ |
nethercote | 8b5f40c | 2004-11-02 13:29:50 +0000 | [diff] [blame] | 912 | Addr VG_(find_map_space)(Addr addr, SizeT len, Bool for_client) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 913 | { |
fitzhardinge | e3632c6 | 2003-12-22 10:58:06 +0000 | [diff] [blame] | 914 | static const Bool debug = False || mem_debug; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 915 | Addr ret; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 916 | Addr addrOrig = addr; |
nethercote | a8bc380 | 2004-10-19 17:54:02 +0000 | [diff] [blame] | 917 | Addr limit = (for_client ? VG_(client_end)-1 : VG_(valgrind_last)); |
| 918 | Addr base = (for_client ? VG_(client_mapbase) : VG_(valgrind_base)); |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 919 | Addr hole_start, hole_end, hstart_any, hstart_fixed, hstart_final; |
| 920 | Int i, i_any, i_fixed, i_final; |
| 921 | SizeT hole_len; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 922 | |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 923 | Bool fixed; |
| 924 | |
sewardj | e517b80 | 2005-02-16 01:58:51 +0000 | [diff] [blame] | 925 | if (debug) { |
| 926 | VG_(printf)("\n\n"); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 927 | VG_(printf)("find_map_space(%p, %llu, %d) ...\n", |
| 928 | addr, (ULong)len, for_client); |
sewardj | e517b80 | 2005-02-16 01:58:51 +0000 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | if (0) show_segments("find_map_space: start"); |
| 932 | |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 933 | if (addr == 0) { |
| 934 | fixed = False; |
| 935 | } else { |
| 936 | fixed = True; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 937 | /* leave space for redzone and still try to get the exact |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 938 | address asked for */ |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 939 | addr -= VKI_PAGE_SIZE; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 940 | } |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 941 | |
| 942 | /* Everything must be page-aligned */ |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 943 | vg_assert((addr & (VKI_PAGE_SIZE-1)) == 0); |
njn | 13bfd85 | 2005-06-02 03:52:53 +0000 | [diff] [blame] | 944 | len = VG_PGROUNDUP(len); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 945 | |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 946 | len += VKI_PAGE_SIZE * 2; /* leave redzone gaps before and after mapping */ |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 947 | |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 948 | /* Scan the segment list, looking for a hole which satisfies the |
| 949 | requirements. At each point i we ask the question "can we use |
| 950 | the hole in between segments[i-1] and segments[i] ?" */ |
| 951 | i_any = i_fixed = -1; |
| 952 | hstart_any = hstart_fixed = 0; |
| 953 | |
| 954 | hole_start = hole_end = 0; |
| 955 | |
| 956 | /* Iterate over all possible holes, generating them into |
| 957 | hole_start/hole_end. Filter out invalid ones. Then see if any |
| 958 | are usable; if so set i_fixed/i_any and hstart_fixed/hstart_any. |
| 959 | */ |
| 960 | for (i = 0; i <=/*yes,really*/ segments_used; i++) { |
| 961 | if (i == 0) { |
| 962 | hole_start = 0; |
| 963 | hole_end = segments[0].addr-1; |
| 964 | } |
| 965 | else { |
| 966 | vg_assert(segments_used > 0); |
| 967 | if (i == segments_used) { |
| 968 | hole_start = segments[i-1].addr + segments[i-1].len; |
| 969 | hole_end = ~(Addr)0; |
| 970 | } else { |
| 971 | hole_start = segments[i-1].addr + segments[i-1].len; |
| 972 | hole_end = segments[i].addr - 1; |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | vg_assert(hole_start <= hole_end || hole_start == hole_end+1); |
| 977 | |
| 978 | /* ignore zero-sized holes */ |
| 979 | if (hole_start == hole_end+1) |
| 980 | continue; |
| 981 | |
njn | be91aae | 2005-03-27 01:42:41 +0000 | [diff] [blame] | 982 | vg_assert(VG_IS_PAGE_ALIGNED(hole_start)); |
| 983 | vg_assert(VG_IS_PAGE_ALIGNED(hole_end+1)); |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 984 | |
| 985 | /* ignore holes which fall outside the allowable area */ |
| 986 | if (!(hole_start >= base && hole_end <= limit)) |
| 987 | continue; |
| 988 | |
| 989 | vg_assert(hole_end > hole_start); |
| 990 | hole_len = hole_end - hole_start + 1; |
njn | be91aae | 2005-03-27 01:42:41 +0000 | [diff] [blame] | 991 | vg_assert(VG_IS_PAGE_ALIGNED(hole_len)); |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 992 | |
| 993 | if (hole_len >= len && i_any == -1) { |
| 994 | /* It will at least fit in this hole. */ |
| 995 | i_any = i; |
| 996 | hstart_any = hole_start; |
| 997 | } |
| 998 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 999 | if (fixed && hole_start <= addr |
| 1000 | && hole_start+hole_len >= addr+len) { |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 1001 | /* We were asked for a fixed mapping, and this hole works. |
| 1002 | Bag it -- and stop searching as further searching is |
| 1003 | pointless. */ |
| 1004 | i_fixed = i; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1005 | hstart_fixed = addr; |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 1006 | break; |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | /* Summarise the final decision into i_final/hstart_final. */ |
| 1011 | i_final = -1; |
| 1012 | hstart_final = 0; |
| 1013 | |
| 1014 | if (fixed) { |
| 1015 | i_final = i_fixed; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1016 | hstart_final = hstart_fixed + VKI_PAGE_SIZE; /* skip leading redzone */ |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 1017 | } else { |
| 1018 | i_final = i_any; |
| 1019 | hstart_final = hstart_any; |
| 1020 | } |
| 1021 | |
| 1022 | |
| 1023 | if (i_final != -1) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1024 | ret = hstart_final; |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 1025 | else |
| 1026 | ret = 0; /* not found */ |
| 1027 | |
| 1028 | if (debug) |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 1029 | VG_(printf)("find_map_space(%p, %llu, %d) -> %p\n\n", |
| 1030 | addr, (ULong)len, for_client, ret); |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 1031 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1032 | if (fixed) { |
| 1033 | vg_assert(ret == 0 || ret == addrOrig); |
| 1034 | } |
| 1035 | |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 1036 | return ret; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 1039 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1040 | /* Pad the entire process address space, from "start" |
nethercote | 820bd8c | 2004-09-07 23:04:49 +0000 | [diff] [blame] | 1041 | to VG_(valgrind_last) by creating an anonymous and inaccessible |
thughes | 646c6aa | 2004-07-16 15:36:45 +0000 | [diff] [blame] | 1042 | mapping over any part of the address space which is not covered |
| 1043 | by an entry in the segment list. |
| 1044 | |
| 1045 | This is designed for use around system calls which allocate |
| 1046 | memory in the process address space without providing a way to |
nethercote | ccc9ecd | 2004-07-16 17:32:15 +0000 | [diff] [blame] | 1047 | control its location such as io_setup. By choosing a suitable |
thughes | 646c6aa | 2004-07-16 15:36:45 +0000 | [diff] [blame] | 1048 | address with VG_(find_map_space) and then adding a segment for |
| 1049 | it and padding the address space valgrind can ensure that the |
| 1050 | kernel has no choice but to put the memory where we want it. */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1051 | void VG_(pad_address_space)(Addr start) |
thughes | 9aaebc3 | 2004-07-15 23:13:37 +0000 | [diff] [blame] | 1052 | { |
sewardj | 394790d | 2005-03-17 02:14:44 +0000 | [diff] [blame] | 1053 | Addr addr = (start == 0) ? VG_(client_base) : start; |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 1054 | SysRes ret; |
sewardj | 394790d | 2005-03-17 02:14:44 +0000 | [diff] [blame] | 1055 | |
| 1056 | Int i = 0; |
| 1057 | Segment* s = i >= segments_used ? NULL : &segments[i]; |
thughes | 9aaebc3 | 2004-07-15 23:13:37 +0000 | [diff] [blame] | 1058 | |
nethercote | 820bd8c | 2004-09-07 23:04:49 +0000 | [diff] [blame] | 1059 | while (s && addr <= VG_(valgrind_last)) { |
thughes | 9aaebc3 | 2004-07-15 23:13:37 +0000 | [diff] [blame] | 1060 | if (addr < s->addr) { |
njn | 7df470b | 2005-05-29 18:46:38 +0000 | [diff] [blame] | 1061 | ret = VG_(mmap_native)((void*)addr, s->addr - addr, 0, |
njn | f6d228b | 2005-03-26 02:42:31 +0000 | [diff] [blame] | 1062 | VKI_MAP_FIXED | VKI_MAP_PRIVATE | VKI_MAP_ANONYMOUS, |
| 1063 | -1, 0); |
thughes | 9aaebc3 | 2004-07-15 23:13:37 +0000 | [diff] [blame] | 1064 | } |
thughes | 9aaebc3 | 2004-07-15 23:13:37 +0000 | [diff] [blame] | 1065 | addr = s->addr + s->len; |
sewardj | 394790d | 2005-03-17 02:14:44 +0000 | [diff] [blame] | 1066 | i++; |
| 1067 | s = i >= segments_used ? NULL : &segments[i]; |
thughes | 9aaebc3 | 2004-07-15 23:13:37 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
nethercote | 820bd8c | 2004-09-07 23:04:49 +0000 | [diff] [blame] | 1070 | if (addr <= VG_(valgrind_last)) { |
njn | 7df470b | 2005-05-29 18:46:38 +0000 | [diff] [blame] | 1071 | ret = VG_(mmap_native)((void*)addr, VG_(valgrind_last) - addr + 1, 0, |
njn | f6d228b | 2005-03-26 02:42:31 +0000 | [diff] [blame] | 1072 | VKI_MAP_FIXED | VKI_MAP_PRIVATE | VKI_MAP_ANONYMOUS, |
| 1073 | -1, 0); |
thughes | 9aaebc3 | 2004-07-15 23:13:37 +0000 | [diff] [blame] | 1074 | } |
thughes | 9aaebc3 | 2004-07-15 23:13:37 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
thughes | c70b8c6 | 2004-07-16 23:07:58 +0000 | [diff] [blame] | 1077 | /* Remove the address space padding added by VG_(pad_address_space) |
thughes | 646c6aa | 2004-07-16 15:36:45 +0000 | [diff] [blame] | 1078 | by removing any mappings that it created. */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1079 | void VG_(unpad_address_space)(Addr start) |
thughes | 9aaebc3 | 2004-07-15 23:13:37 +0000 | [diff] [blame] | 1080 | { |
sewardj | 394790d | 2005-03-17 02:14:44 +0000 | [diff] [blame] | 1081 | Addr addr = (start == 0) ? VG_(client_base) : start; |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 1082 | SysRes ret; |
sewardj | 394790d | 2005-03-17 02:14:44 +0000 | [diff] [blame] | 1083 | |
| 1084 | Int i = 0; |
| 1085 | Segment* s = i >= segments_used ? NULL : &segments[i]; |
thughes | 9aaebc3 | 2004-07-15 23:13:37 +0000 | [diff] [blame] | 1086 | |
nethercote | 820bd8c | 2004-09-07 23:04:49 +0000 | [diff] [blame] | 1087 | while (s && addr <= VG_(valgrind_last)) { |
thughes | 9aaebc3 | 2004-07-15 23:13:37 +0000 | [diff] [blame] | 1088 | if (addr < s->addr) { |
njn | b69f7c1 | 2005-06-24 22:17:38 +0000 | [diff] [blame] | 1089 | //ret = VG_(do_syscall2)(__NR_munmap, addr, s->addr - addr); |
njn | ca6fef0 | 2004-11-29 16:49:18 +0000 | [diff] [blame] | 1090 | ret = VG_(do_syscall2)(__NR_munmap, addr, s->addr - addr); |
thughes | 9aaebc3 | 2004-07-15 23:13:37 +0000 | [diff] [blame] | 1091 | } |
thughes | 9aaebc3 | 2004-07-15 23:13:37 +0000 | [diff] [blame] | 1092 | addr = s->addr + s->len; |
sewardj | 394790d | 2005-03-17 02:14:44 +0000 | [diff] [blame] | 1093 | i++; |
| 1094 | s = i >= segments_used ? NULL : &segments[i]; |
thughes | 9aaebc3 | 2004-07-15 23:13:37 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
nethercote | 820bd8c | 2004-09-07 23:04:49 +0000 | [diff] [blame] | 1097 | if (addr <= VG_(valgrind_last)) { |
sewardj | 394790d | 2005-03-17 02:14:44 +0000 | [diff] [blame] | 1098 | ret = VG_(do_syscall2)(__NR_munmap, addr, |
| 1099 | (VG_(valgrind_last) - addr) + 1); |
thughes | 9aaebc3 | 2004-07-15 23:13:37 +0000 | [diff] [blame] | 1100 | } |
thughes | 9aaebc3 | 2004-07-15 23:13:37 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 1103 | /* Find the segment holding 'a', or NULL if none. */ |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1104 | Segment *VG_(find_segment)(Addr a) |
| 1105 | { |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 1106 | Int r = find_segment(a); |
| 1107 | if (0) show_segments("find_segment"); |
| 1108 | if (r == -1) return NULL; |
| 1109 | return &segments[r]; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 1112 | /* Assumes that 'a' is not in any segment. Finds the lowest-addressed |
| 1113 | segment above 'a', or NULL if none. Passing 'a' which is in fact in |
| 1114 | a segment is a checked error. |
| 1115 | */ |
| 1116 | Segment *VG_(find_segment_above_unmapped)(Addr a) |
| 1117 | { |
| 1118 | Int r = find_segment_above_unmapped(a); |
| 1119 | if (0) show_segments("find_segment_above_unmapped"); |
| 1120 | if (r == -1) return NULL; |
| 1121 | return &segments[r]; |
| 1122 | } |
| 1123 | |
| 1124 | /* Assumes that 'a' is in some segment. Finds the next segment along, |
| 1125 | or NULL if none. Passing 'a' which is in fact not in a segment is |
| 1126 | a checked error. |
| 1127 | */ |
| 1128 | Segment *VG_(find_segment_above_mapped)(Addr a) |
| 1129 | { |
| 1130 | Int r = find_segment_above_mapped(a); |
| 1131 | if (0) show_segments("find_segment_above_mapped"); |
| 1132 | if (r == -1) return NULL; |
| 1133 | return &segments[r]; |
| 1134 | } |
| 1135 | |
| 1136 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1137 | /* |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1138 | Test if a piece of memory is addressable with at least the "prot" |
| 1139 | protection permissions by examining the underlying segments. |
| 1140 | |
| 1141 | Really this is a very stupid algorithm and we could do much |
| 1142 | better by iterating through the segment array instead of through |
| 1143 | the address space. |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1144 | */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1145 | Bool VG_(is_addressable)(Addr p, SizeT size, UInt prot) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1146 | { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1147 | Segment *seg; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1148 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1149 | if ((p + size) < p) |
| 1150 | return False; /* reject wraparounds */ |
| 1151 | if (size == 0) |
| 1152 | return True; /* isn't this a bit of a strange case? */ |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1153 | |
njn | 13bfd85 | 2005-06-02 03:52:53 +0000 | [diff] [blame] | 1154 | p = VG_PGROUNDDN(p); |
| 1155 | size = VG_PGROUNDUP(size); |
njn | be91aae | 2005-03-27 01:42:41 +0000 | [diff] [blame] | 1156 | vg_assert(VG_IS_PAGE_ALIGNED(p)); |
| 1157 | vg_assert(VG_IS_PAGE_ALIGNED(size)); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1158 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1159 | for (; size > 0; size -= VKI_PAGE_SIZE) { |
| 1160 | seg = VG_(find_segment)(p); |
| 1161 | if (!seg) |
| 1162 | return False; |
| 1163 | if ((seg->prot & prot) != prot) |
| 1164 | return False; |
| 1165 | p += VKI_PAGE_SIZE; |
| 1166 | } |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1167 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1168 | return True; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1171 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1172 | /*--------------------------------------------------------------------*/ |
njn | 9260c78 | 2005-06-19 21:57:54 +0000 | [diff] [blame] | 1173 | /*--- Random function that doesn't really belong here ---*/ |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1174 | /*--------------------------------------------------------------------*/ |
| 1175 | |
njn | 86f12dc | 2005-03-14 01:16:05 +0000 | [diff] [blame] | 1176 | /* We'll call any RW mmaped memory segment, within the client address |
sewardj | 882af5f | 2005-03-16 12:27:53 +0000 | [diff] [blame] | 1177 | range, which isn't SF_CORE, a root. |
| 1178 | */ |
njn | 86f12dc | 2005-03-14 01:16:05 +0000 | [diff] [blame] | 1179 | void VG_(find_root_memory)(void (*add_rootrange)(Addr a, SizeT sz)) |
| 1180 | { |
sewardj | 882af5f | 2005-03-16 12:27:53 +0000 | [diff] [blame] | 1181 | Int i; |
| 1182 | UInt flags; |
njn | 86f12dc | 2005-03-14 01:16:05 +0000 | [diff] [blame] | 1183 | Segment *s; |
| 1184 | |
sewardj | 882af5f | 2005-03-16 12:27:53 +0000 | [diff] [blame] | 1185 | for (i = 0; i < segments_used; i++) { |
| 1186 | s = &segments[i]; |
njn | f253dac | 2005-06-28 22:38:24 +0000 | [diff] [blame] | 1187 | flags = s->flags & (SF_SHARED|SF_MMAP|SF_VALGRIND|SF_CORE|SF_STACK); |
tom | 5912dd7 | 2005-07-18 09:40:29 +0000 | [diff] [blame^] | 1188 | if (flags != SF_MMAP && flags != SF_STACK && flags != (SF_MMAP|SF_STACK)) |
njn | 86f12dc | 2005-03-14 01:16:05 +0000 | [diff] [blame] | 1189 | continue; |
sewardj | 882af5f | 2005-03-16 12:27:53 +0000 | [diff] [blame] | 1190 | if ((s->prot & (VKI_PROT_READ|VKI_PROT_WRITE)) |
| 1191 | != (VKI_PROT_READ|VKI_PROT_WRITE)) |
njn | 86f12dc | 2005-03-14 01:16:05 +0000 | [diff] [blame] | 1192 | continue; |
| 1193 | if (!VG_(is_client_addr)(s->addr) || |
njn | f253dac | 2005-06-28 22:38:24 +0000 | [diff] [blame] | 1194 | !VG_(is_client_addr)(s->addr+s->len-1)) |
njn | 86f12dc | 2005-03-14 01:16:05 +0000 | [diff] [blame] | 1195 | continue; |
| 1196 | |
| 1197 | (*add_rootrange)(s->addr, s->len); |
| 1198 | } |
njn | 86f12dc | 2005-03-14 01:16:05 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | |
nethercote | 88a9016 | 2004-07-10 16:59:25 +0000 | [diff] [blame] | 1202 | /*--------------------------------------------------------------------*/ |
| 1203 | /*--- Querying memory layout ---*/ |
| 1204 | /*--------------------------------------------------------------------*/ |
| 1205 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1206 | Bool VG_(is_client_addr)(Addr a) |
| 1207 | { |
| 1208 | return a >= VG_(client_base) && a < VG_(client_end); |
| 1209 | } |
| 1210 | |
| 1211 | Bool VG_(is_shadow_addr)(Addr a) |
| 1212 | { |
| 1213 | return a >= VG_(shadow_base) && a < VG_(shadow_end); |
| 1214 | } |
| 1215 | |
sewardj | fa8202f | 2005-04-24 11:05:55 +0000 | [diff] [blame] | 1216 | |
nethercote | 88a9016 | 2004-07-10 16:59:25 +0000 | [diff] [blame] | 1217 | /*--------------------------------------------------------------------*/ |
nethercote | 2f54e0d | 2004-07-10 17:27:20 +0000 | [diff] [blame] | 1218 | /*--- Handling shadow memory ---*/ |
nethercote | 88a9016 | 2004-07-10 16:59:25 +0000 | [diff] [blame] | 1219 | /*--------------------------------------------------------------------*/ |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1220 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1221 | void *VG_(shadow_alloc)(UInt size) |
| 1222 | { |
| 1223 | static Addr shadow_alloc = 0; |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 1224 | Addr try_here; |
njn | e9befc6 | 2005-06-11 15:51:30 +0000 | [diff] [blame] | 1225 | SysRes r; |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 1226 | |
| 1227 | if (0) show_segments("shadow_alloc(before)"); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1228 | |
| 1229 | vg_assert(VG_(needs).shadow_memory); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1230 | |
njn | 13bfd85 | 2005-06-02 03:52:53 +0000 | [diff] [blame] | 1231 | size = VG_PGROUNDUP(size); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1232 | |
| 1233 | if (shadow_alloc == 0) |
| 1234 | shadow_alloc = VG_(shadow_base); |
| 1235 | |
| 1236 | if (shadow_alloc >= VG_(shadow_end)) |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 1237 | goto failed; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1238 | |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 1239 | try_here = shadow_alloc; |
njn | be91aae | 2005-03-27 01:42:41 +0000 | [diff] [blame] | 1240 | vg_assert(VG_IS_PAGE_ALIGNED(try_here)); |
| 1241 | vg_assert(VG_IS_PAGE_ALIGNED(size)); |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 1242 | vg_assert(size > 0); |
| 1243 | |
| 1244 | if (0) |
| 1245 | VG_(printf)("shadow_alloc: size %d, trying at %p\n", size, (void*)try_here); |
| 1246 | |
| 1247 | /* this is big-bang allocated, so we don't expect to find a listed |
| 1248 | segment for it. */ |
| 1249 | /* This is really an absolute disgrace. Sometimes the big-bang |
| 1250 | mapping is in the list (due to re-reads of /proc/self/maps, |
| 1251 | presumably) and sometimes it isn't. */ |
| 1252 | #if 0 |
| 1253 | r = find_segment(try_here); |
| 1254 | vg_assert(r == -1); |
| 1255 | r = find_segment(try_here+size-1); |
| 1256 | vg_assert(r == -1); |
| 1257 | #endif |
| 1258 | |
| 1259 | r = VG_(mprotect_native)( (void*)try_here, |
| 1260 | size, VKI_PROT_READ|VKI_PROT_WRITE ); |
| 1261 | |
njn | e9befc6 | 2005-06-11 15:51:30 +0000 | [diff] [blame] | 1262 | if (r.isError) |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 1263 | goto failed; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1264 | |
| 1265 | shadow_alloc += size; |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 1266 | return (void*)try_here; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1267 | |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 1268 | failed: |
| 1269 | VG_(printf)( |
sewardj | 96db5e1 | 2005-03-21 17:43:34 +0000 | [diff] [blame] | 1270 | "valgrind: Could not allocate address space (0x%x bytes)\n" |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 1271 | "valgrind: for shadow memory chunk.\n", |
sewardj | 96db5e1 | 2005-03-21 17:43:34 +0000 | [diff] [blame] | 1272 | size |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 1273 | ); |
| 1274 | VG_(exit)(1); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1275 | } |
| 1276 | |
njn | e3f0635 | 2005-06-01 03:48:33 +0000 | [diff] [blame] | 1277 | /*------------------------------------------------------------*/ |
| 1278 | /*--- pointercheck ---*/ |
| 1279 | /*------------------------------------------------------------*/ |
| 1280 | |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 1281 | Bool VG_(setup_pointercheck)(Addr client_base, Addr client_end) |
njn | e3f0635 | 2005-06-01 03:48:33 +0000 | [diff] [blame] | 1282 | { |
| 1283 | vg_assert(0 != client_end); |
| 1284 | #if defined(VGP_x86_linux) |
| 1285 | /* Client address space segment limit descriptor entry */ |
| 1286 | #define POINTERCHECK_SEGIDX 1 |
| 1287 | |
| 1288 | vki_modify_ldt_t ldt = { |
| 1289 | POINTERCHECK_SEGIDX, // entry_number |
| 1290 | client_base, // base_addr |
| 1291 | (client_end - client_base) / VKI_PAGE_SIZE, // limit |
| 1292 | 1, // seg_32bit |
| 1293 | 0, // contents: data, RW, non-expanding |
| 1294 | 0, // ! read_exec_only |
| 1295 | 1, // limit_in_pages |
| 1296 | 0, // ! seg not present |
| 1297 | 1, // useable |
| 1298 | }; |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 1299 | SysRes ret = VG_(do_syscall3)(__NR_modify_ldt, 1, (UWord)&ldt, sizeof(ldt)); |
| 1300 | if (ret.isError) { |
njn | e3f0635 | 2005-06-01 03:48:33 +0000 | [diff] [blame] | 1301 | VG_(message)(Vg_UserMsg, |
| 1302 | "Warning: ignoring --pointercheck=yes, " |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 1303 | "because modify_ldt failed (errno=%d)", ret.val); |
njn | e3f0635 | 2005-06-01 03:48:33 +0000 | [diff] [blame] | 1304 | return False; |
| 1305 | } else { |
| 1306 | return True; |
| 1307 | } |
| 1308 | #elif defined(VGP_amd64_linux) |
| 1309 | if (0) |
| 1310 | VG_(message)(Vg_DebugMsg, "ignoring --pointercheck (unimplemented)"); |
| 1311 | return True; |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 1312 | #elif defined(VGP_ppc32_linux) |
| 1313 | if (0) |
| 1314 | VG_(message)(Vg_DebugMsg, "ignoring --pointercheck (unimplemented)"); |
| 1315 | return True; |
njn | e3f0635 | 2005-06-01 03:48:33 +0000 | [diff] [blame] | 1316 | #else |
| 1317 | # error Unknown architecture |
| 1318 | #endif |
| 1319 | } |
| 1320 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1321 | /*--------------------------------------------------------------------*/ |
njn | 945ed2e | 2005-06-24 03:28:30 +0000 | [diff] [blame] | 1322 | /*--- end ---*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1323 | /*--------------------------------------------------------------------*/ |