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