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