Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 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 | |
Ian Rogers | debeb3a | 2014-01-23 16:54:52 -0800 | [diff] [blame] | 19 | #include <inttypes.h> |
Christopher Ferris | 943af7d | 2014-01-16 12:41:46 -0800 | [diff] [blame] | 20 | #include <backtrace/BacktraceMap.h> |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 21 | |
Christopher Ferris | caf22ac | 2014-01-27 18:32:14 -0800 | [diff] [blame] | 22 | #include "UniquePtr.h" |
Elliott Hughes | e222ee0 | 2012-12-13 14:41:43 -0800 | [diff] [blame] | 23 | #include "base/stringprintf.h" |
| 24 | #include "ScopedFd.h" |
| 25 | #include "utils.h" |
| 26 | |
Elliott Hughes | 6c9c06d | 2011-11-07 16:43:47 -0800 | [diff] [blame] | 27 | #define USE_ASHMEM 1 |
| 28 | |
| 29 | #ifdef USE_ASHMEM |
| 30 | #include <cutils/ashmem.h> |
| 31 | #endif |
| 32 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 33 | namespace art { |
| 34 | |
Christopher Ferris | 943af7d | 2014-01-16 12:41:46 -0800 | [diff] [blame] | 35 | static 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()); |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 45 | } |
| 46 | return os; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Stuart Monteith | 8dba5aa | 2014-03-12 12:44:01 +0000 | [diff] [blame] | 49 | #if defined(__LP64__) && !defined(__x86_64__) |
Andreas Gampe | 7104cbf | 2014-03-21 11:44:43 -0700 | [diff] [blame] | 50 | // Where to start with low memory allocation. |
| 51 | static constexpr uintptr_t LOW_MEM_START = kPageSize * 2; |
| 52 | |
| 53 | uintptr_t MemMap::next_mem_pos_ = LOW_MEM_START; // first page to check for low-mem extent |
Stuart Monteith | 8dba5aa | 2014-03-12 12:44:01 +0000 | [diff] [blame] | 54 | #endif |
| 55 | |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 56 | static bool CheckMapRequest(byte* expected_ptr, void* actual_ptr, size_t byte_count, |
| 57 | std::ostringstream* error_msg) { |
| 58 | // Handled first by caller for more specific error messages. |
| 59 | CHECK(actual_ptr != MAP_FAILED); |
| 60 | |
| 61 | if (expected_ptr == nullptr) { |
| 62 | return true; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 63 | } |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 64 | |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 65 | if (expected_ptr == actual_ptr) { |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | // We asked for an address but didn't get what we wanted, all paths below here should fail. |
| 70 | int result = munmap(actual_ptr, byte_count); |
| 71 | if (result == -1) { |
| 72 | PLOG(WARNING) << StringPrintf("munmap(%p, %zd) failed", actual_ptr, byte_count); |
| 73 | } |
| 74 | |
| 75 | uintptr_t actual = reinterpret_cast<uintptr_t>(actual_ptr); |
| 76 | uintptr_t expected = reinterpret_cast<uintptr_t>(expected_ptr); |
| 77 | uintptr_t limit = expected + byte_count; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 78 | |
Christopher Ferris | caf22ac | 2014-01-27 18:32:14 -0800 | [diff] [blame] | 79 | UniquePtr<BacktraceMap> map(BacktraceMap::Create(getpid())); |
| 80 | if (!map->Build()) { |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 81 | *error_msg << StringPrintf("Failed to build process map to determine why mmap returned " |
| 82 | "0x%08" PRIxPTR " instead of 0x%08" PRIxPTR, actual, expected); |
| 83 | |
| 84 | return false; |
Christopher Ferris | 943af7d | 2014-01-16 12:41:46 -0800 | [diff] [blame] | 85 | } |
Christopher Ferris | caf22ac | 2014-01-27 18:32:14 -0800 | [diff] [blame] | 86 | for (BacktraceMap::const_iterator it = map->begin(); it != map->end(); ++it) { |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 87 | if ((expected >= it->start && expected < it->end) // start of new within old |
| 88 | || (limit > it->start && limit < it->end) // end of new within old |
| 89 | || (expected <= it->start && limit > it->end)) { // start/end of new includes all of old |
| 90 | *error_msg |
| 91 | << StringPrintf("Requested region 0x%08" PRIxPTR "-0x%08" PRIxPTR " overlaps with " |
| 92 | "existing map 0x%08" PRIxPTR "-0x%08" PRIxPTR " (%s)\n", |
| 93 | expected, limit, |
| 94 | static_cast<uintptr_t>(it->start), static_cast<uintptr_t>(it->end), |
| 95 | it->name.c_str()) |
| 96 | << std::make_pair(it, map->end()); |
| 97 | return false; |
| 98 | } |
Elliott Hughes | 96970cd | 2012-03-13 15:46:31 -0700 | [diff] [blame] | 99 | } |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 100 | *error_msg << StringPrintf("Failed to mmap at expected address, mapped at " |
| 101 | "0x%08" PRIxPTR " instead of 0x%08" PRIxPTR, actual, expected); |
| 102 | return false; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 105 | MemMap* MemMap::MapAnonymous(const char* name, byte* expected, size_t byte_count, int prot, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 106 | bool low_4gb, std::string* error_msg) { |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 107 | if (byte_count == 0) { |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 108 | return new MemMap(name, nullptr, 0, nullptr, 0, prot); |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 109 | } |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 110 | size_t page_aligned_byte_count = RoundUp(byte_count, kPageSize); |
Elliott Hughes | 6c9c06d | 2011-11-07 16:43:47 -0800 | [diff] [blame] | 111 | |
| 112 | #ifdef USE_ASHMEM |
Ian Rogers | a40307e | 2013-02-22 11:32:44 -0800 | [diff] [blame] | 113 | // android_os_Debug.cpp read_mapinfo assumes all ashmem regions associated with the VM are |
| 114 | // prefixed "dalvik-". |
| 115 | std::string debug_friendly_name("dalvik-"); |
| 116 | debug_friendly_name += name; |
| 117 | ScopedFd fd(ashmem_create_region(debug_friendly_name.c_str(), page_aligned_byte_count)); |
Elliott Hughes | 6c9c06d | 2011-11-07 16:43:47 -0800 | [diff] [blame] | 118 | if (fd.get() == -1) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 119 | *error_msg = StringPrintf("ashmem_create_region failed for '%s': %s", name, strerror(errno)); |
| 120 | return nullptr; |
Elliott Hughes | 6c9c06d | 2011-11-07 16:43:47 -0800 | [diff] [blame] | 121 | } |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 122 | int flags = MAP_PRIVATE; |
Elliott Hughes | 6c9c06d | 2011-11-07 16:43:47 -0800 | [diff] [blame] | 123 | #else |
| 124 | ScopedFd fd(-1); |
| 125 | int flags = MAP_PRIVATE | MAP_ANONYMOUS; |
| 126 | #endif |
Stuart Monteith | 8dba5aa | 2014-03-12 12:44:01 +0000 | [diff] [blame] | 127 | |
Brian Carlstrom | aa94cf3 | 2014-03-23 23:47:25 -0700 | [diff] [blame] | 128 | // We need to store and potentially set an error number for pretty printing of errors |
| 129 | int saved_errno = 0; |
| 130 | |
Qiming Shi | 84d49cc | 2014-04-24 15:38:41 +0800 | [diff] [blame^] | 131 | #ifdef __LP64__ |
| 132 | // When requesting low_4g memory and having an expectation, the requested range should fit into |
| 133 | // 4GB. |
| 134 | if (low_4gb && ( |
| 135 | // Start out of bounds. |
| 136 | (reinterpret_cast<uintptr_t>(expected) >> 32) != 0 || |
| 137 | // End out of bounds. For simplicity, this will fail for the last page of memory. |
| 138 | (reinterpret_cast<uintptr_t>(expected + page_aligned_byte_count) >> 32) != 0)) { |
| 139 | *error_msg = StringPrintf("The requested address space (%p, %p) cannot fit in low_4gb", |
| 140 | expected, expected + page_aligned_byte_count); |
| 141 | return nullptr; |
| 142 | } |
| 143 | #endif |
| 144 | |
Stuart Monteith | 8dba5aa | 2014-03-12 12:44:01 +0000 | [diff] [blame] | 145 | // TODO: |
| 146 | // A page allocator would be a useful abstraction here, as |
| 147 | // 1) It is doubtful that MAP_32BIT on x86_64 is doing the right job for us |
| 148 | // 2) The linear scheme, even with simple saving of the last known position, is very crude |
| 149 | #if defined(__LP64__) && !defined(__x86_64__) |
| 150 | // MAP_32BIT only available on x86_64. |
| 151 | void* actual = MAP_FAILED; |
Stuart Monteith | 8dba5aa | 2014-03-12 12:44:01 +0000 | [diff] [blame] | 152 | if (low_4gb && expected == nullptr) { |
| 153 | flags |= MAP_FIXED; |
| 154 | |
Andreas Gampe | 7104cbf | 2014-03-21 11:44:43 -0700 | [diff] [blame] | 155 | bool first_run = true; |
| 156 | |
Andreas Gampe | 71a3eba | 2014-03-17 12:57:08 -0700 | [diff] [blame] | 157 | for (uintptr_t ptr = next_mem_pos_; ptr < 4 * GB; ptr += kPageSize) { |
Andreas Gampe | 7104cbf | 2014-03-21 11:44:43 -0700 | [diff] [blame] | 158 | if (4U * GB - ptr < page_aligned_byte_count) { |
| 159 | // Not enough memory until 4GB. |
| 160 | if (first_run) { |
| 161 | // Try another time from the bottom; |
Andreas Gampe | 9de65ff | 2014-03-21 17:25:57 -0700 | [diff] [blame] | 162 | ptr = LOW_MEM_START - kPageSize; |
Andreas Gampe | 7104cbf | 2014-03-21 11:44:43 -0700 | [diff] [blame] | 163 | first_run = false; |
| 164 | continue; |
| 165 | } else { |
| 166 | // Second try failed. |
| 167 | break; |
| 168 | } |
| 169 | } |
| 170 | |
Stuart Monteith | 8dba5aa | 2014-03-12 12:44:01 +0000 | [diff] [blame] | 171 | uintptr_t tail_ptr; |
| 172 | |
| 173 | // Check pages are free. |
| 174 | bool safe = true; |
| 175 | for (tail_ptr = ptr; tail_ptr < ptr + page_aligned_byte_count; tail_ptr += kPageSize) { |
| 176 | if (msync(reinterpret_cast<void*>(tail_ptr), kPageSize, 0) == 0) { |
| 177 | safe = false; |
| 178 | break; |
| 179 | } else { |
| 180 | DCHECK_EQ(errno, ENOMEM); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | next_mem_pos_ = tail_ptr; // update early, as we break out when we found and mapped a region |
| 185 | |
| 186 | if (safe == true) { |
| 187 | actual = mmap(reinterpret_cast<void*>(ptr), page_aligned_byte_count, prot, flags, fd.get(), |
| 188 | 0); |
| 189 | if (actual != MAP_FAILED) { |
| 190 | break; |
| 191 | } |
| 192 | } else { |
| 193 | // Skip over last page. |
| 194 | ptr = tail_ptr; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | if (actual == MAP_FAILED) { |
Brian Carlstrom | aa94cf3 | 2014-03-23 23:47:25 -0700 | [diff] [blame] | 199 | LOG(ERROR) << "Could not find contiguous low-memory space."; |
| 200 | saved_errno = ENOMEM; |
Stuart Monteith | 8dba5aa | 2014-03-12 12:44:01 +0000 | [diff] [blame] | 201 | } |
| 202 | } else { |
| 203 | actual = mmap(expected, page_aligned_byte_count, prot, flags, fd.get(), 0); |
Brian Carlstrom | aa94cf3 | 2014-03-23 23:47:25 -0700 | [diff] [blame] | 204 | saved_errno = errno; |
Stuart Monteith | 8dba5aa | 2014-03-12 12:44:01 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | #else |
| 208 | #ifdef __x86_64__ |
Qiming Shi | 84d49cc | 2014-04-24 15:38:41 +0800 | [diff] [blame^] | 209 | if (low_4gb && expected == nullptr) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 210 | flags |= MAP_32BIT; |
| 211 | } |
| 212 | #endif |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 213 | |
| 214 | void* actual = mmap(expected, page_aligned_byte_count, prot, flags, fd.get(), 0); |
Brian Carlstrom | aa94cf3 | 2014-03-23 23:47:25 -0700 | [diff] [blame] | 215 | saved_errno = errno; |
Stuart Monteith | 8dba5aa | 2014-03-12 12:44:01 +0000 | [diff] [blame] | 216 | #endif |
| 217 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 218 | if (actual == MAP_FAILED) { |
jeffhao | 8161c03 | 2012-10-31 15:50:00 -0700 | [diff] [blame] | 219 | std::string maps; |
| 220 | ReadFileToString("/proc/self/maps", &maps); |
Brian Carlstrom | aa94cf3 | 2014-03-23 23:47:25 -0700 | [diff] [blame] | 221 | |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 222 | *error_msg = StringPrintf("Failed anonymous mmap(%p, %zd, 0x%x, 0x%x, %d, 0): %s\n%s", |
| 223 | expected, page_aligned_byte_count, prot, flags, fd.get(), |
Brian Carlstrom | aa94cf3 | 2014-03-23 23:47:25 -0700 | [diff] [blame] | 224 | strerror(saved_errno), maps.c_str()); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 225 | return nullptr; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 226 | } |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 227 | std::ostringstream check_map_request_error_msg; |
| 228 | if (!CheckMapRequest(expected, actual, page_aligned_byte_count, &check_map_request_error_msg)) { |
| 229 | *error_msg = check_map_request_error_msg.str(); |
| 230 | return nullptr; |
| 231 | } |
| 232 | return new MemMap(name, reinterpret_cast<byte*>(actual), byte_count, actual, |
| 233 | page_aligned_byte_count, prot); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 236 | MemMap* MemMap::MapFileAtAddress(byte* expected, size_t byte_count, int prot, int flags, int fd, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 237 | off_t start, bool reuse, const char* filename, |
| 238 | std::string* error_msg) { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 239 | CHECK_NE(0, prot); |
| 240 | CHECK_NE(0, flags & (MAP_SHARED | MAP_PRIVATE)); |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 241 | if (reuse) { |
| 242 | // reuse means it is okay that it overlaps an existing page mapping. |
| 243 | // Only use this if you actually made the page reservation yourself. |
| 244 | CHECK(expected != nullptr); |
| 245 | flags |= MAP_FIXED; |
| 246 | } else { |
| 247 | CHECK_EQ(0, flags & MAP_FIXED); |
| 248 | } |
| 249 | |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 250 | if (byte_count == 0) { |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 251 | return new MemMap(filename, nullptr, 0, nullptr, 0, prot); |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 252 | } |
Ian Rogers | f8adc60 | 2013-04-18 17:06:19 -0700 | [diff] [blame] | 253 | // Adjust 'offset' to be page-aligned as required by mmap. |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 254 | int page_offset = start % kPageSize; |
| 255 | off_t page_aligned_offset = start - page_offset; |
Ian Rogers | f8adc60 | 2013-04-18 17:06:19 -0700 | [diff] [blame] | 256 | // Adjust 'byte_count' to be page-aligned as we will map this anyway. |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 257 | size_t page_aligned_byte_count = RoundUp(byte_count + page_offset, kPageSize); |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 258 | // The 'expected' is modified (if specified, ie non-null) to be page aligned to the file but not |
| 259 | // necessarily to virtual memory. mmap will page align 'expected' for us. |
| 260 | byte* page_aligned_expected = (expected == nullptr) ? nullptr : (expected - page_offset); |
| 261 | |
| 262 | byte* actual = reinterpret_cast<byte*>(mmap(page_aligned_expected, |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 263 | page_aligned_byte_count, |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 264 | prot, |
| 265 | flags, |
| 266 | fd, |
| 267 | page_aligned_offset)); |
| 268 | if (actual == MAP_FAILED) { |
Brian Carlstrom | aa94cf3 | 2014-03-23 23:47:25 -0700 | [diff] [blame] | 269 | auto saved_errno = errno; |
| 270 | |
jeffhao | 8161c03 | 2012-10-31 15:50:00 -0700 | [diff] [blame] | 271 | std::string maps; |
| 272 | ReadFileToString("/proc/self/maps", &maps); |
Brian Carlstrom | aa94cf3 | 2014-03-23 23:47:25 -0700 | [diff] [blame] | 273 | |
Mathieu Chartier | c7cb190 | 2014-03-05 14:41:03 -0800 | [diff] [blame] | 274 | *error_msg = StringPrintf("mmap(%p, %zd, 0x%x, 0x%x, %d, %" PRId64 |
| 275 | ") of file '%s' failed: %s\n%s", |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 276 | page_aligned_expected, page_aligned_byte_count, prot, flags, fd, |
Brian Carlstrom | aa94cf3 | 2014-03-23 23:47:25 -0700 | [diff] [blame] | 277 | static_cast<int64_t>(page_aligned_offset), filename, |
| 278 | strerror(saved_errno), maps.c_str()); |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 279 | return nullptr; |
| 280 | } |
| 281 | std::ostringstream check_map_request_error_msg; |
| 282 | if (!CheckMapRequest(expected, actual, page_aligned_byte_count, &check_map_request_error_msg)) { |
| 283 | *error_msg = check_map_request_error_msg.str(); |
| 284 | return nullptr; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 285 | } |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 286 | return new MemMap(filename, actual + page_offset, byte_count, actual, page_aligned_byte_count, |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 287 | prot); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | MemMap::~MemMap() { |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 291 | if (base_begin_ == nullptr && base_size_ == 0) { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 292 | return; |
| 293 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 294 | int result = munmap(base_begin_, base_size_); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 295 | if (result == -1) { |
| 296 | PLOG(FATAL) << "munmap failed"; |
| 297 | } |
| 298 | } |
| 299 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 300 | MemMap::MemMap(const std::string& name, byte* begin, size_t size, void* base_begin, |
| 301 | size_t base_size, int prot) |
| 302 | : name_(name), begin_(begin), size_(size), base_begin_(base_begin), base_size_(base_size), |
| 303 | prot_(prot) { |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 304 | if (size_ == 0) { |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 305 | CHECK(begin_ == nullptr); |
| 306 | CHECK(base_begin_ == nullptr); |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 307 | CHECK_EQ(base_size_, 0U); |
| 308 | } else { |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 309 | CHECK(begin_ != nullptr); |
| 310 | CHECK(base_begin_ != nullptr); |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 311 | CHECK_NE(base_size_, 0U); |
| 312 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 313 | }; |
| 314 | |
Hiroshi Yamauchi | fd7e7f1 | 2013-10-22 14:17:48 -0700 | [diff] [blame] | 315 | MemMap* MemMap::RemapAtEnd(byte* new_end, const char* tail_name, int tail_prot, |
| 316 | std::string* error_msg) { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 317 | DCHECK_GE(new_end, Begin()); |
| 318 | DCHECK_LE(new_end, End()); |
Hiroshi Yamauchi | fd7e7f1 | 2013-10-22 14:17:48 -0700 | [diff] [blame] | 319 | DCHECK_LE(begin_ + size_, reinterpret_cast<byte*>(base_begin_) + base_size_); |
| 320 | DCHECK(IsAligned<kPageSize>(begin_)); |
| 321 | DCHECK(IsAligned<kPageSize>(base_begin_)); |
| 322 | DCHECK(IsAligned<kPageSize>(reinterpret_cast<byte*>(base_begin_) + base_size_)); |
| 323 | DCHECK(IsAligned<kPageSize>(new_end)); |
| 324 | byte* old_end = begin_ + size_; |
| 325 | byte* old_base_end = reinterpret_cast<byte*>(base_begin_) + base_size_; |
| 326 | byte* new_base_end = new_end; |
| 327 | DCHECK_LE(new_base_end, old_base_end); |
| 328 | if (new_base_end == old_base_end) { |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 329 | return new MemMap(tail_name, nullptr, 0, nullptr, 0, tail_prot); |
Hiroshi Yamauchi | fd7e7f1 | 2013-10-22 14:17:48 -0700 | [diff] [blame] | 330 | } |
| 331 | size_ = new_end - reinterpret_cast<byte*>(begin_); |
| 332 | base_size_ = new_base_end - reinterpret_cast<byte*>(base_begin_); |
| 333 | DCHECK_LE(begin_ + size_, reinterpret_cast<byte*>(base_begin_) + base_size_); |
| 334 | size_t tail_size = old_end - new_end; |
| 335 | byte* tail_base_begin = new_base_end; |
| 336 | size_t tail_base_size = old_base_end - new_base_end; |
| 337 | DCHECK_EQ(tail_base_begin + tail_base_size, old_base_end); |
| 338 | DCHECK(IsAligned<kPageSize>(tail_base_size)); |
| 339 | |
| 340 | #ifdef USE_ASHMEM |
| 341 | // android_os_Debug.cpp read_mapinfo assumes all ashmem regions associated with the VM are |
| 342 | // prefixed "dalvik-". |
| 343 | std::string debug_friendly_name("dalvik-"); |
| 344 | debug_friendly_name += tail_name; |
| 345 | ScopedFd fd(ashmem_create_region(debug_friendly_name.c_str(), tail_base_size)); |
Stuart Monteith | 8dba5aa | 2014-03-12 12:44:01 +0000 | [diff] [blame] | 346 | int flags = MAP_PRIVATE | MAP_FIXED; |
Hiroshi Yamauchi | fd7e7f1 | 2013-10-22 14:17:48 -0700 | [diff] [blame] | 347 | if (fd.get() == -1) { |
| 348 | *error_msg = StringPrintf("ashmem_create_region failed for '%s': %s", |
| 349 | tail_name, strerror(errno)); |
| 350 | return nullptr; |
| 351 | } |
| 352 | #else |
| 353 | ScopedFd fd(-1); |
| 354 | int flags = MAP_PRIVATE | MAP_ANONYMOUS; |
| 355 | #endif |
| 356 | |
| 357 | // Unmap/map the tail region. |
| 358 | int result = munmap(tail_base_begin, tail_base_size); |
| 359 | if (result == -1) { |
| 360 | std::string maps; |
| 361 | ReadFileToString("/proc/self/maps", &maps); |
| 362 | *error_msg = StringPrintf("munmap(%p, %zd) failed for '%s'\n%s", |
| 363 | tail_base_begin, tail_base_size, name_.c_str(), |
| 364 | maps.c_str()); |
| 365 | return nullptr; |
| 366 | } |
| 367 | // Don't cause memory allocation between the munmap and the mmap |
| 368 | // calls. Otherwise, libc (or something else) might take this memory |
| 369 | // region. Note this isn't perfect as there's no way to prevent |
| 370 | // other threads to try to take this memory region here. |
| 371 | byte* actual = reinterpret_cast<byte*>(mmap(tail_base_begin, tail_base_size, tail_prot, |
| 372 | flags, fd.get(), 0)); |
| 373 | if (actual == MAP_FAILED) { |
| 374 | std::string maps; |
| 375 | ReadFileToString("/proc/self/maps", &maps); |
Mathieu Chartier | c7cb190 | 2014-03-05 14:41:03 -0800 | [diff] [blame] | 376 | *error_msg = StringPrintf("anonymous mmap(%p, %zd, 0x%x, 0x%x, %d, 0) failed\n%s", |
Hiroshi Yamauchi | fd7e7f1 | 2013-10-22 14:17:48 -0700 | [diff] [blame] | 377 | tail_base_begin, tail_base_size, tail_prot, flags, fd.get(), |
| 378 | maps.c_str()); |
| 379 | return nullptr; |
| 380 | } |
| 381 | return new MemMap(tail_name, actual, tail_size, actual, tail_base_size, tail_prot); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 382 | } |
Logan Chien | d88fa26 | 2012-06-06 15:23:32 +0800 | [diff] [blame] | 383 | |
| 384 | bool MemMap::Protect(int prot) { |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 385 | if (base_begin_ == nullptr && base_size_ == 0) { |
Ian Rogers | 1c849e5 | 2012-06-28 14:00:33 -0700 | [diff] [blame] | 386 | prot_ = prot; |
Logan Chien | d88fa26 | 2012-06-06 15:23:32 +0800 | [diff] [blame] | 387 | return true; |
| 388 | } |
| 389 | |
| 390 | if (mprotect(base_begin_, base_size_, prot) == 0) { |
Ian Rogers | 1c849e5 | 2012-06-28 14:00:33 -0700 | [diff] [blame] | 391 | prot_ = prot; |
Logan Chien | d88fa26 | 2012-06-06 15:23:32 +0800 | [diff] [blame] | 392 | return true; |
| 393 | } |
| 394 | |
Shih-wei Liao | a060ed9 | 2012-06-07 09:25:28 -0700 | [diff] [blame] | 395 | PLOG(ERROR) << "mprotect(" << reinterpret_cast<void*>(base_begin_) << ", " << base_size_ << ", " |
| 396 | << prot << ") failed"; |
Logan Chien | d88fa26 | 2012-06-06 15:23:32 +0800 | [diff] [blame] | 397 | return false; |
| 398 | } |
| 399 | |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 400 | std::ostream& operator<<(std::ostream& os, const MemMap& mem_map) { |
Mathieu Chartier | c7cb190 | 2014-03-05 14:41:03 -0800 | [diff] [blame] | 401 | os << StringPrintf("[MemMap: %s prot=0x%x %p-%p]", |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 402 | mem_map.GetName().c_str(), mem_map.GetProtect(), |
| 403 | mem_map.BaseBegin(), mem_map.BaseEnd()); |
| 404 | return os; |
| 405 | } |
| 406 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 407 | } // namespace art |