mem_map.cc revision 8dba5aaaffc0bc2b2580bf02f0d9095c00d26a17
1/* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include "mem_map.h" 18 19#include <inttypes.h> 20#include <backtrace/BacktraceMap.h> 21 22#include "UniquePtr.h" 23#include "base/stringprintf.h" 24#include "ScopedFd.h" 25#include "utils.h" 26 27#define USE_ASHMEM 1 28 29#ifdef USE_ASHMEM 30#include <cutils/ashmem.h> 31#endif 32 33namespace art { 34 35static std::ostream& operator<<( 36 std::ostream& os, 37 std::pair<BacktraceMap::const_iterator, BacktraceMap::const_iterator> iters) { 38 for (BacktraceMap::const_iterator it = iters.first; it != iters.second; ++it) { 39 os << StringPrintf("0x%08x-0x%08x %c%c%c %s\n", 40 static_cast<uint32_t>(it->start), 41 static_cast<uint32_t>(it->end), 42 (it->flags & PROT_READ) ? 'r' : '-', 43 (it->flags & PROT_WRITE) ? 'w' : '-', 44 (it->flags & PROT_EXEC) ? 'x' : '-', it->name.c_str()); 45 } 46 return os; 47} 48 49#if defined(__LP64__) && !defined(__x86_64__) 50MemMap::next_mem_pos_ = kPageSize * 2; // first page to check for low-mem extent 51#endif 52 53static bool CheckMapRequest(byte* expected_ptr, void* actual_ptr, size_t byte_count, 54 std::ostringstream* error_msg) { 55 // Handled first by caller for more specific error messages. 56 CHECK(actual_ptr != MAP_FAILED); 57 58 if (expected_ptr == nullptr) { 59 return true; 60 } 61 62 if (expected_ptr == actual_ptr) { 63 return true; 64 } 65 66 // We asked for an address but didn't get what we wanted, all paths below here should fail. 67 int result = munmap(actual_ptr, byte_count); 68 if (result == -1) { 69 PLOG(WARNING) << StringPrintf("munmap(%p, %zd) failed", actual_ptr, byte_count); 70 } 71 72 uintptr_t actual = reinterpret_cast<uintptr_t>(actual_ptr); 73 uintptr_t expected = reinterpret_cast<uintptr_t>(expected_ptr); 74 uintptr_t limit = expected + byte_count; 75 76 UniquePtr<BacktraceMap> map(BacktraceMap::Create(getpid())); 77 if (!map->Build()) { 78 *error_msg << StringPrintf("Failed to build process map to determine why mmap returned " 79 "0x%08" PRIxPTR " instead of 0x%08" PRIxPTR, actual, expected); 80 81 return false; 82 } 83 for (BacktraceMap::const_iterator it = map->begin(); it != map->end(); ++it) { 84 if ((expected >= it->start && expected < it->end) // start of new within old 85 || (limit > it->start && limit < it->end) // end of new within old 86 || (expected <= it->start && limit > it->end)) { // start/end of new includes all of old 87 *error_msg 88 << StringPrintf("Requested region 0x%08" PRIxPTR "-0x%08" PRIxPTR " overlaps with " 89 "existing map 0x%08" PRIxPTR "-0x%08" PRIxPTR " (%s)\n", 90 expected, limit, 91 static_cast<uintptr_t>(it->start), static_cast<uintptr_t>(it->end), 92 it->name.c_str()) 93 << std::make_pair(it, map->end()); 94 return false; 95 } 96 } 97 *error_msg << StringPrintf("Failed to mmap at expected address, mapped at " 98 "0x%08" PRIxPTR " instead of 0x%08" PRIxPTR, actual, expected); 99 return false; 100} 101 102MemMap* MemMap::MapAnonymous(const char* name, byte* expected, size_t byte_count, int prot, 103 bool low_4gb, std::string* error_msg) { 104 if (byte_count == 0) { 105 return new MemMap(name, nullptr, 0, nullptr, 0, prot); 106 } 107 size_t page_aligned_byte_count = RoundUp(byte_count, kPageSize); 108 109#ifdef USE_ASHMEM 110 // android_os_Debug.cpp read_mapinfo assumes all ashmem regions associated with the VM are 111 // prefixed "dalvik-". 112 std::string debug_friendly_name("dalvik-"); 113 debug_friendly_name += name; 114 ScopedFd fd(ashmem_create_region(debug_friendly_name.c_str(), page_aligned_byte_count)); 115 if (fd.get() == -1) { 116 *error_msg = StringPrintf("ashmem_create_region failed for '%s': %s", name, strerror(errno)); 117 return nullptr; 118 } 119 int flags = MAP_PRIVATE; 120#else 121 ScopedFd fd(-1); 122 int flags = MAP_PRIVATE | MAP_ANONYMOUS; 123#endif 124 125 // TODO: 126 // A page allocator would be a useful abstraction here, as 127 // 1) It is doubtful that MAP_32BIT on x86_64 is doing the right job for us 128 // 2) The linear scheme, even with simple saving of the last known position, is very crude 129#if defined(__LP64__) && !defined(__x86_64__) 130 // MAP_32BIT only available on x86_64. 131 void* actual = MAP_FAILED; 132 std::string strerr; 133 if (low_4gb && expected == nullptr) { 134 flags |= MAP_FIXED; 135 136 for (uintptr_t ptr = next_mem_pos; ptr < 4 * GB; ptr += kPageSize) { 137 uintptr_t tail_ptr; 138 139 // Check pages are free. 140 bool safe = true; 141 for (tail_ptr = ptr; tail_ptr < ptr + page_aligned_byte_count; tail_ptr += kPageSize) { 142 if (msync(reinterpret_cast<void*>(tail_ptr), kPageSize, 0) == 0) { 143 safe = false; 144 break; 145 } else { 146 DCHECK_EQ(errno, ENOMEM); 147 } 148 } 149 150 next_mem_pos_ = tail_ptr; // update early, as we break out when we found and mapped a region 151 152 if (safe == true) { 153 actual = mmap(reinterpret_cast<void*>(ptr), page_aligned_byte_count, prot, flags, fd.get(), 154 0); 155 if (actual != MAP_FAILED) { 156 break; 157 } 158 } else { 159 // Skip over last page. 160 ptr = tail_ptr; 161 } 162 } 163 164 if (actual == MAP_FAILED) { 165 strerr = "Could not find contiguous low-memory space."; 166 } 167 } else { 168 actual = mmap(expected, page_aligned_byte_count, prot, flags, fd.get(), 0); 169 strerr = strerror(errno); 170 } 171 172#else 173#ifdef __x86_64__ 174 if (low_4gb) { 175 flags |= MAP_32BIT; 176 } 177#endif 178 179 void* actual = mmap(expected, page_aligned_byte_count, prot, flags, fd.get(), 0); 180 std::string strerr(strerror(errno)); 181#endif 182 183 if (actual == MAP_FAILED) { 184 std::string maps; 185 ReadFileToString("/proc/self/maps", &maps); 186 *error_msg = StringPrintf("Failed anonymous mmap(%p, %zd, 0x%x, 0x%x, %d, 0): %s\n%s", 187 expected, page_aligned_byte_count, prot, flags, fd.get(), 188 strerr.c_str(), maps.c_str()); 189 return nullptr; 190 } 191 std::ostringstream check_map_request_error_msg; 192 if (!CheckMapRequest(expected, actual, page_aligned_byte_count, &check_map_request_error_msg)) { 193 *error_msg = check_map_request_error_msg.str(); 194 return nullptr; 195 } 196 return new MemMap(name, reinterpret_cast<byte*>(actual), byte_count, actual, 197 page_aligned_byte_count, prot); 198} 199 200MemMap* MemMap::MapFileAtAddress(byte* expected, size_t byte_count, int prot, int flags, int fd, 201 off_t start, bool reuse, const char* filename, 202 std::string* error_msg) { 203 CHECK_NE(0, prot); 204 CHECK_NE(0, flags & (MAP_SHARED | MAP_PRIVATE)); 205 if (reuse) { 206 // reuse means it is okay that it overlaps an existing page mapping. 207 // Only use this if you actually made the page reservation yourself. 208 CHECK(expected != nullptr); 209 flags |= MAP_FIXED; 210 } else { 211 CHECK_EQ(0, flags & MAP_FIXED); 212 } 213 214 if (byte_count == 0) { 215 return new MemMap(filename, nullptr, 0, nullptr, 0, prot); 216 } 217 // Adjust 'offset' to be page-aligned as required by mmap. 218 int page_offset = start % kPageSize; 219 off_t page_aligned_offset = start - page_offset; 220 // Adjust 'byte_count' to be page-aligned as we will map this anyway. 221 size_t page_aligned_byte_count = RoundUp(byte_count + page_offset, kPageSize); 222 // The 'expected' is modified (if specified, ie non-null) to be page aligned to the file but not 223 // necessarily to virtual memory. mmap will page align 'expected' for us. 224 byte* page_aligned_expected = (expected == nullptr) ? nullptr : (expected - page_offset); 225 226 byte* actual = reinterpret_cast<byte*>(mmap(page_aligned_expected, 227 page_aligned_byte_count, 228 prot, 229 flags, 230 fd, 231 page_aligned_offset)); 232 std::string strerr(strerror(errno)); 233 if (actual == MAP_FAILED) { 234 std::string maps; 235 ReadFileToString("/proc/self/maps", &maps); 236 *error_msg = StringPrintf("mmap(%p, %zd, 0x%x, 0x%x, %d, %" PRId64 237 ") of file '%s' failed: %s\n%s", 238 page_aligned_expected, page_aligned_byte_count, prot, flags, fd, 239 static_cast<int64_t>(page_aligned_offset), filename, strerr.c_str(), 240 maps.c_str()); 241 return nullptr; 242 } 243 std::ostringstream check_map_request_error_msg; 244 if (!CheckMapRequest(expected, actual, page_aligned_byte_count, &check_map_request_error_msg)) { 245 *error_msg = check_map_request_error_msg.str(); 246 return nullptr; 247 } 248 return new MemMap(filename, actual + page_offset, byte_count, actual, page_aligned_byte_count, 249 prot); 250} 251 252MemMap::~MemMap() { 253 if (base_begin_ == nullptr && base_size_ == 0) { 254 return; 255 } 256 int result = munmap(base_begin_, base_size_); 257 if (result == -1) { 258 PLOG(FATAL) << "munmap failed"; 259 } 260} 261 262MemMap::MemMap(const std::string& name, byte* begin, size_t size, void* base_begin, 263 size_t base_size, int prot) 264 : name_(name), begin_(begin), size_(size), base_begin_(base_begin), base_size_(base_size), 265 prot_(prot) { 266 if (size_ == 0) { 267 CHECK(begin_ == nullptr); 268 CHECK(base_begin_ == nullptr); 269 CHECK_EQ(base_size_, 0U); 270 } else { 271 CHECK(begin_ != nullptr); 272 CHECK(base_begin_ != nullptr); 273 CHECK_NE(base_size_, 0U); 274 } 275}; 276 277MemMap* MemMap::RemapAtEnd(byte* new_end, const char* tail_name, int tail_prot, 278 std::string* error_msg) { 279 DCHECK_GE(new_end, Begin()); 280 DCHECK_LE(new_end, End()); 281 DCHECK_LE(begin_ + size_, reinterpret_cast<byte*>(base_begin_) + base_size_); 282 DCHECK(IsAligned<kPageSize>(begin_)); 283 DCHECK(IsAligned<kPageSize>(base_begin_)); 284 DCHECK(IsAligned<kPageSize>(reinterpret_cast<byte*>(base_begin_) + base_size_)); 285 DCHECK(IsAligned<kPageSize>(new_end)); 286 byte* old_end = begin_ + size_; 287 byte* old_base_end = reinterpret_cast<byte*>(base_begin_) + base_size_; 288 byte* new_base_end = new_end; 289 DCHECK_LE(new_base_end, old_base_end); 290 if (new_base_end == old_base_end) { 291 return new MemMap(tail_name, nullptr, 0, nullptr, 0, tail_prot); 292 } 293 size_ = new_end - reinterpret_cast<byte*>(begin_); 294 base_size_ = new_base_end - reinterpret_cast<byte*>(base_begin_); 295 DCHECK_LE(begin_ + size_, reinterpret_cast<byte*>(base_begin_) + base_size_); 296 size_t tail_size = old_end - new_end; 297 byte* tail_base_begin = new_base_end; 298 size_t tail_base_size = old_base_end - new_base_end; 299 DCHECK_EQ(tail_base_begin + tail_base_size, old_base_end); 300 DCHECK(IsAligned<kPageSize>(tail_base_size)); 301 302#ifdef USE_ASHMEM 303 // android_os_Debug.cpp read_mapinfo assumes all ashmem regions associated with the VM are 304 // prefixed "dalvik-". 305 std::string debug_friendly_name("dalvik-"); 306 debug_friendly_name += tail_name; 307 ScopedFd fd(ashmem_create_region(debug_friendly_name.c_str(), tail_base_size)); 308 int flags = MAP_PRIVATE | MAP_FIXED; 309 if (fd.get() == -1) { 310 *error_msg = StringPrintf("ashmem_create_region failed for '%s': %s", 311 tail_name, strerror(errno)); 312 return nullptr; 313 } 314#else 315 ScopedFd fd(-1); 316 int flags = MAP_PRIVATE | MAP_ANONYMOUS; 317#endif 318 319 // Unmap/map the tail region. 320 int result = munmap(tail_base_begin, tail_base_size); 321 if (result == -1) { 322 std::string maps; 323 ReadFileToString("/proc/self/maps", &maps); 324 *error_msg = StringPrintf("munmap(%p, %zd) failed for '%s'\n%s", 325 tail_base_begin, tail_base_size, name_.c_str(), 326 maps.c_str()); 327 return nullptr; 328 } 329 // Don't cause memory allocation between the munmap and the mmap 330 // calls. Otherwise, libc (or something else) might take this memory 331 // region. Note this isn't perfect as there's no way to prevent 332 // other threads to try to take this memory region here. 333 byte* actual = reinterpret_cast<byte*>(mmap(tail_base_begin, tail_base_size, tail_prot, 334 flags, fd.get(), 0)); 335 if (actual == MAP_FAILED) { 336 std::string maps; 337 ReadFileToString("/proc/self/maps", &maps); 338 *error_msg = StringPrintf("anonymous mmap(%p, %zd, 0x%x, 0x%x, %d, 0) failed\n%s", 339 tail_base_begin, tail_base_size, tail_prot, flags, fd.get(), 340 maps.c_str()); 341 return nullptr; 342 } 343 return new MemMap(tail_name, actual, tail_size, actual, tail_base_size, tail_prot); 344} 345 346bool MemMap::Protect(int prot) { 347 if (base_begin_ == nullptr && base_size_ == 0) { 348 prot_ = prot; 349 return true; 350 } 351 352 if (mprotect(base_begin_, base_size_, prot) == 0) { 353 prot_ = prot; 354 return true; 355 } 356 357 PLOG(ERROR) << "mprotect(" << reinterpret_cast<void*>(base_begin_) << ", " << base_size_ << ", " 358 << prot << ") failed"; 359 return false; 360} 361 362std::ostream& operator<<(std::ostream& os, const MemMap& mem_map) { 363 os << StringPrintf("[MemMap: %s prot=0x%x %p-%p]", 364 mem_map.GetName().c_str(), mem_map.GetProtect(), 365 mem_map.BaseBegin(), mem_map.BaseEnd()); 366 return os; 367} 368 369} // namespace art 370