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" |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 18 | #include "thread-inl.h" |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 19 | |
Ian Rogers | debeb3a | 2014-01-23 16:54:52 -0800 | [diff] [blame] | 20 | #include <inttypes.h> |
Christopher Ferris | 943af7d | 2014-01-16 12:41:46 -0800 | [diff] [blame] | 21 | #include <backtrace/BacktraceMap.h> |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 22 | #include <memory> |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 23 | |
Andreas Gampe | d8f26db | 2014-05-19 17:01:13 -0700 | [diff] [blame] | 24 | // See CreateStartPos below. |
| 25 | #ifdef __BIONIC__ |
| 26 | #include <sys/auxv.h> |
| 27 | #endif |
| 28 | |
Elliott Hughes | e222ee0 | 2012-12-13 14:41:43 -0800 | [diff] [blame] | 29 | #include "base/stringprintf.h" |
| 30 | #include "ScopedFd.h" |
| 31 | #include "utils.h" |
| 32 | |
Elliott Hughes | 6c9c06d | 2011-11-07 16:43:47 -0800 | [diff] [blame] | 33 | #define USE_ASHMEM 1 |
| 34 | |
| 35 | #ifdef USE_ASHMEM |
| 36 | #include <cutils/ashmem.h> |
| 37 | #endif |
| 38 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 39 | namespace art { |
| 40 | |
Christopher Ferris | 943af7d | 2014-01-16 12:41:46 -0800 | [diff] [blame] | 41 | static std::ostream& operator<<( |
| 42 | std::ostream& os, |
| 43 | std::pair<BacktraceMap::const_iterator, BacktraceMap::const_iterator> iters) { |
| 44 | for (BacktraceMap::const_iterator it = iters.first; it != iters.second; ++it) { |
| 45 | os << StringPrintf("0x%08x-0x%08x %c%c%c %s\n", |
| 46 | static_cast<uint32_t>(it->start), |
| 47 | static_cast<uint32_t>(it->end), |
| 48 | (it->flags & PROT_READ) ? 'r' : '-', |
| 49 | (it->flags & PROT_WRITE) ? 'w' : '-', |
| 50 | (it->flags & PROT_EXEC) ? 'x' : '-', it->name.c_str()); |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 51 | } |
| 52 | return os; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 55 | std::ostream& operator<<(std::ostream& os, const std::multimap<void*, MemMap*>& mem_maps) { |
| 56 | os << "MemMap:" << std::endl; |
| 57 | for (auto it = mem_maps.begin(); it != mem_maps.end(); ++it) { |
| 58 | void* base = it->first; |
| 59 | MemMap* map = it->second; |
| 60 | CHECK_EQ(base, map->BaseBegin()); |
| 61 | os << *map << std::endl; |
| 62 | } |
| 63 | return os; |
| 64 | } |
| 65 | |
| 66 | std::multimap<void*, MemMap*> MemMap::maps_; |
| 67 | |
Stuart Monteith | 8dba5aa | 2014-03-12 12:44:01 +0000 | [diff] [blame] | 68 | #if defined(__LP64__) && !defined(__x86_64__) |
Andreas Gampe | d8f26db | 2014-05-19 17:01:13 -0700 | [diff] [blame] | 69 | // Handling mem_map in 32b address range for 64b architectures that do not support MAP_32BIT. |
| 70 | |
| 71 | // The regular start of memory allocations. The first 64KB is protected by SELinux. |
Andreas Gampe | 6bd621a | 2014-05-16 17:28:58 -0700 | [diff] [blame] | 72 | static constexpr uintptr_t LOW_MEM_START = 64 * KB; |
Andreas Gampe | 7104cbf | 2014-03-21 11:44:43 -0700 | [diff] [blame] | 73 | |
Andreas Gampe | d8f26db | 2014-05-19 17:01:13 -0700 | [diff] [blame] | 74 | // Generate random starting position. |
| 75 | // To not interfere with image position, take the image's address and only place it below. Current |
| 76 | // formula (sketch): |
| 77 | // |
| 78 | // ART_BASE_ADDR = 0001XXXXXXXXXXXXXXX |
| 79 | // ---------------------------------------- |
| 80 | // = 0000111111111111111 |
| 81 | // & ~(kPageSize - 1) =~0000000000000001111 |
| 82 | // ---------------------------------------- |
| 83 | // mask = 0000111111111110000 |
| 84 | // & random data = YYYYYYYYYYYYYYYYYYY |
| 85 | // ----------------------------------- |
| 86 | // tmp = 0000YYYYYYYYYYY0000 |
| 87 | // + LOW_MEM_START = 0000000000001000000 |
| 88 | // -------------------------------------- |
| 89 | // start |
| 90 | // |
| 91 | // getauxval as an entropy source is exposed in Bionic, but not in glibc before 2.16. When we |
| 92 | // do not have Bionic, simply start with LOW_MEM_START. |
| 93 | |
| 94 | // Function is standalone so it can be tested somewhat in mem_map_test.cc. |
| 95 | #ifdef __BIONIC__ |
| 96 | uintptr_t CreateStartPos(uint64_t input) { |
| 97 | CHECK_NE(0, ART_BASE_ADDRESS); |
| 98 | |
| 99 | // Start with all bits below highest bit in ART_BASE_ADDRESS. |
| 100 | constexpr size_t leading_zeros = CLZ(static_cast<uint32_t>(ART_BASE_ADDRESS)); |
| 101 | constexpr uintptr_t mask_ones = (1 << (31 - leading_zeros)) - 1; |
| 102 | |
| 103 | // Lowest (usually 12) bits are not used, as aligned by page size. |
| 104 | constexpr uintptr_t mask = mask_ones & ~(kPageSize - 1); |
| 105 | |
| 106 | // Mask input data. |
| 107 | return (input & mask) + LOW_MEM_START; |
| 108 | } |
| 109 | #endif |
| 110 | |
| 111 | static uintptr_t GenerateNextMemPos() { |
| 112 | #ifdef __BIONIC__ |
| 113 | uint8_t* random_data = reinterpret_cast<uint8_t*>(getauxval(AT_RANDOM)); |
| 114 | // The lower 8B are taken for the stack guard. Use the upper 8B (with mask). |
| 115 | return CreateStartPos(*reinterpret_cast<uintptr_t*>(random_data + 8)); |
| 116 | #else |
| 117 | // No auxv on host, see above. |
| 118 | return LOW_MEM_START; |
| 119 | #endif |
| 120 | } |
| 121 | |
| 122 | // Initialize linear scan to random position. |
| 123 | uintptr_t MemMap::next_mem_pos_ = GenerateNextMemPos(); |
Stuart Monteith | 8dba5aa | 2014-03-12 12:44:01 +0000 | [diff] [blame] | 124 | #endif |
| 125 | |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 126 | static bool CheckMapRequest(byte* expected_ptr, void* actual_ptr, size_t byte_count, |
| 127 | std::ostringstream* error_msg) { |
| 128 | // Handled first by caller for more specific error messages. |
| 129 | CHECK(actual_ptr != MAP_FAILED); |
| 130 | |
| 131 | if (expected_ptr == nullptr) { |
| 132 | return true; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 133 | } |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 134 | |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 135 | if (expected_ptr == actual_ptr) { |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | // We asked for an address but didn't get what we wanted, all paths below here should fail. |
| 140 | int result = munmap(actual_ptr, byte_count); |
| 141 | if (result == -1) { |
| 142 | PLOG(WARNING) << StringPrintf("munmap(%p, %zd) failed", actual_ptr, byte_count); |
| 143 | } |
| 144 | |
| 145 | uintptr_t actual = reinterpret_cast<uintptr_t>(actual_ptr); |
| 146 | uintptr_t expected = reinterpret_cast<uintptr_t>(expected_ptr); |
| 147 | uintptr_t limit = expected + byte_count; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 148 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 149 | std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid())); |
Christopher Ferris | caf22ac | 2014-01-27 18:32:14 -0800 | [diff] [blame] | 150 | if (!map->Build()) { |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 151 | *error_msg << StringPrintf("Failed to build process map to determine why mmap returned " |
| 152 | "0x%08" PRIxPTR " instead of 0x%08" PRIxPTR, actual, expected); |
| 153 | |
| 154 | return false; |
Christopher Ferris | 943af7d | 2014-01-16 12:41:46 -0800 | [diff] [blame] | 155 | } |
Christopher Ferris | caf22ac | 2014-01-27 18:32:14 -0800 | [diff] [blame] | 156 | for (BacktraceMap::const_iterator it = map->begin(); it != map->end(); ++it) { |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 157 | if ((expected >= it->start && expected < it->end) // start of new within old |
| 158 | || (limit > it->start && limit < it->end) // end of new within old |
| 159 | || (expected <= it->start && limit > it->end)) { // start/end of new includes all of old |
| 160 | *error_msg |
| 161 | << StringPrintf("Requested region 0x%08" PRIxPTR "-0x%08" PRIxPTR " overlaps with " |
| 162 | "existing map 0x%08" PRIxPTR "-0x%08" PRIxPTR " (%s)\n", |
| 163 | expected, limit, |
| 164 | static_cast<uintptr_t>(it->start), static_cast<uintptr_t>(it->end), |
| 165 | it->name.c_str()) |
| 166 | << std::make_pair(it, map->end()); |
| 167 | return false; |
| 168 | } |
Elliott Hughes | 96970cd | 2012-03-13 15:46:31 -0700 | [diff] [blame] | 169 | } |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 170 | *error_msg << StringPrintf("Failed to mmap at expected address, mapped at " |
| 171 | "0x%08" PRIxPTR " instead of 0x%08" PRIxPTR, actual, expected); |
| 172 | return false; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 175 | 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] | 176 | bool low_4gb, std::string* error_msg) { |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 177 | if (byte_count == 0) { |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 178 | return new MemMap(name, nullptr, 0, nullptr, 0, prot); |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 179 | } |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 180 | size_t page_aligned_byte_count = RoundUp(byte_count, kPageSize); |
Elliott Hughes | 6c9c06d | 2011-11-07 16:43:47 -0800 | [diff] [blame] | 181 | |
| 182 | #ifdef USE_ASHMEM |
Ian Rogers | a40307e | 2013-02-22 11:32:44 -0800 | [diff] [blame] | 183 | // android_os_Debug.cpp read_mapinfo assumes all ashmem regions associated with the VM are |
| 184 | // prefixed "dalvik-". |
| 185 | std::string debug_friendly_name("dalvik-"); |
| 186 | debug_friendly_name += name; |
| 187 | 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] | 188 | if (fd.get() == -1) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 189 | *error_msg = StringPrintf("ashmem_create_region failed for '%s': %s", name, strerror(errno)); |
| 190 | return nullptr; |
Elliott Hughes | 6c9c06d | 2011-11-07 16:43:47 -0800 | [diff] [blame] | 191 | } |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 192 | int flags = MAP_PRIVATE; |
Elliott Hughes | 6c9c06d | 2011-11-07 16:43:47 -0800 | [diff] [blame] | 193 | #else |
| 194 | ScopedFd fd(-1); |
| 195 | int flags = MAP_PRIVATE | MAP_ANONYMOUS; |
| 196 | #endif |
Stuart Monteith | 8dba5aa | 2014-03-12 12:44:01 +0000 | [diff] [blame] | 197 | |
Brian Carlstrom | aa94cf3 | 2014-03-23 23:47:25 -0700 | [diff] [blame] | 198 | // We need to store and potentially set an error number for pretty printing of errors |
| 199 | int saved_errno = 0; |
| 200 | |
Qiming Shi | 84d49cc | 2014-04-24 15:38:41 +0800 | [diff] [blame] | 201 | #ifdef __LP64__ |
| 202 | // When requesting low_4g memory and having an expectation, the requested range should fit into |
| 203 | // 4GB. |
| 204 | if (low_4gb && ( |
| 205 | // Start out of bounds. |
| 206 | (reinterpret_cast<uintptr_t>(expected) >> 32) != 0 || |
| 207 | // End out of bounds. For simplicity, this will fail for the last page of memory. |
| 208 | (reinterpret_cast<uintptr_t>(expected + page_aligned_byte_count) >> 32) != 0)) { |
| 209 | *error_msg = StringPrintf("The requested address space (%p, %p) cannot fit in low_4gb", |
| 210 | expected, expected + page_aligned_byte_count); |
| 211 | return nullptr; |
| 212 | } |
| 213 | #endif |
| 214 | |
Stuart Monteith | 8dba5aa | 2014-03-12 12:44:01 +0000 | [diff] [blame] | 215 | // TODO: |
| 216 | // A page allocator would be a useful abstraction here, as |
| 217 | // 1) It is doubtful that MAP_32BIT on x86_64 is doing the right job for us |
| 218 | // 2) The linear scheme, even with simple saving of the last known position, is very crude |
| 219 | #if defined(__LP64__) && !defined(__x86_64__) |
| 220 | // MAP_32BIT only available on x86_64. |
| 221 | void* actual = MAP_FAILED; |
Stuart Monteith | 8dba5aa | 2014-03-12 12:44:01 +0000 | [diff] [blame] | 222 | if (low_4gb && expected == nullptr) { |
Andreas Gampe | 7104cbf | 2014-03-21 11:44:43 -0700 | [diff] [blame] | 223 | bool first_run = true; |
| 224 | |
Andreas Gampe | 71a3eba | 2014-03-17 12:57:08 -0700 | [diff] [blame] | 225 | for (uintptr_t ptr = next_mem_pos_; ptr < 4 * GB; ptr += kPageSize) { |
Andreas Gampe | 7104cbf | 2014-03-21 11:44:43 -0700 | [diff] [blame] | 226 | if (4U * GB - ptr < page_aligned_byte_count) { |
| 227 | // Not enough memory until 4GB. |
| 228 | if (first_run) { |
| 229 | // Try another time from the bottom; |
Andreas Gampe | 9de65ff | 2014-03-21 17:25:57 -0700 | [diff] [blame] | 230 | ptr = LOW_MEM_START - kPageSize; |
Andreas Gampe | 7104cbf | 2014-03-21 11:44:43 -0700 | [diff] [blame] | 231 | first_run = false; |
| 232 | continue; |
| 233 | } else { |
| 234 | // Second try failed. |
| 235 | break; |
| 236 | } |
| 237 | } |
| 238 | |
Stuart Monteith | 8dba5aa | 2014-03-12 12:44:01 +0000 | [diff] [blame] | 239 | uintptr_t tail_ptr; |
| 240 | |
| 241 | // Check pages are free. |
| 242 | bool safe = true; |
| 243 | for (tail_ptr = ptr; tail_ptr < ptr + page_aligned_byte_count; tail_ptr += kPageSize) { |
| 244 | if (msync(reinterpret_cast<void*>(tail_ptr), kPageSize, 0) == 0) { |
| 245 | safe = false; |
| 246 | break; |
| 247 | } else { |
| 248 | DCHECK_EQ(errno, ENOMEM); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | next_mem_pos_ = tail_ptr; // update early, as we break out when we found and mapped a region |
| 253 | |
| 254 | if (safe == true) { |
| 255 | actual = mmap(reinterpret_cast<void*>(ptr), page_aligned_byte_count, prot, flags, fd.get(), |
| 256 | 0); |
| 257 | if (actual != MAP_FAILED) { |
Mathieu Chartier | c355a2a | 2014-05-30 13:02:46 -0700 | [diff] [blame] | 258 | // Since we didn't use MAP_FIXED the kernel may have mapped it somewhere not in the low |
| 259 | // 4GB. If this is the case, unmap and retry. |
| 260 | if (reinterpret_cast<uintptr_t>(actual) + page_aligned_byte_count < 4 * GB) { |
| 261 | break; |
| 262 | } else { |
| 263 | munmap(actual, page_aligned_byte_count); |
| 264 | actual = MAP_FAILED; |
| 265 | } |
Stuart Monteith | 8dba5aa | 2014-03-12 12:44:01 +0000 | [diff] [blame] | 266 | } |
| 267 | } else { |
| 268 | // Skip over last page. |
| 269 | ptr = tail_ptr; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | if (actual == MAP_FAILED) { |
Brian Carlstrom | aa94cf3 | 2014-03-23 23:47:25 -0700 | [diff] [blame] | 274 | LOG(ERROR) << "Could not find contiguous low-memory space."; |
| 275 | saved_errno = ENOMEM; |
Stuart Monteith | 8dba5aa | 2014-03-12 12:44:01 +0000 | [diff] [blame] | 276 | } |
| 277 | } else { |
| 278 | actual = mmap(expected, page_aligned_byte_count, prot, flags, fd.get(), 0); |
Brian Carlstrom | aa94cf3 | 2014-03-23 23:47:25 -0700 | [diff] [blame] | 279 | saved_errno = errno; |
Stuart Monteith | 8dba5aa | 2014-03-12 12:44:01 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | #else |
| 283 | #ifdef __x86_64__ |
Qiming Shi | 84d49cc | 2014-04-24 15:38:41 +0800 | [diff] [blame] | 284 | if (low_4gb && expected == nullptr) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 285 | flags |= MAP_32BIT; |
| 286 | } |
| 287 | #endif |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 288 | |
| 289 | 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] | 290 | saved_errno = errno; |
Stuart Monteith | 8dba5aa | 2014-03-12 12:44:01 +0000 | [diff] [blame] | 291 | #endif |
| 292 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 293 | if (actual == MAP_FAILED) { |
jeffhao | 8161c03 | 2012-10-31 15:50:00 -0700 | [diff] [blame] | 294 | std::string maps; |
| 295 | ReadFileToString("/proc/self/maps", &maps); |
Brian Carlstrom | aa94cf3 | 2014-03-23 23:47:25 -0700 | [diff] [blame] | 296 | |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 297 | *error_msg = StringPrintf("Failed anonymous mmap(%p, %zd, 0x%x, 0x%x, %d, 0): %s\n%s", |
| 298 | expected, page_aligned_byte_count, prot, flags, fd.get(), |
Brian Carlstrom | aa94cf3 | 2014-03-23 23:47:25 -0700 | [diff] [blame] | 299 | strerror(saved_errno), maps.c_str()); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 300 | return nullptr; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 301 | } |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 302 | std::ostringstream check_map_request_error_msg; |
| 303 | if (!CheckMapRequest(expected, actual, page_aligned_byte_count, &check_map_request_error_msg)) { |
| 304 | *error_msg = check_map_request_error_msg.str(); |
| 305 | return nullptr; |
| 306 | } |
| 307 | return new MemMap(name, reinterpret_cast<byte*>(actual), byte_count, actual, |
| 308 | page_aligned_byte_count, prot); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 311 | 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] | 312 | off_t start, bool reuse, const char* filename, |
| 313 | std::string* error_msg) { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 314 | CHECK_NE(0, prot); |
| 315 | CHECK_NE(0, flags & (MAP_SHARED | MAP_PRIVATE)); |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 316 | if (reuse) { |
| 317 | // reuse means it is okay that it overlaps an existing page mapping. |
| 318 | // Only use this if you actually made the page reservation yourself. |
| 319 | CHECK(expected != nullptr); |
| 320 | flags |= MAP_FIXED; |
| 321 | } else { |
| 322 | CHECK_EQ(0, flags & MAP_FIXED); |
| 323 | } |
| 324 | |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 325 | if (byte_count == 0) { |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 326 | return new MemMap(filename, nullptr, 0, nullptr, 0, prot); |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 327 | } |
Ian Rogers | f8adc60 | 2013-04-18 17:06:19 -0700 | [diff] [blame] | 328 | // Adjust 'offset' to be page-aligned as required by mmap. |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 329 | int page_offset = start % kPageSize; |
| 330 | off_t page_aligned_offset = start - page_offset; |
Ian Rogers | f8adc60 | 2013-04-18 17:06:19 -0700 | [diff] [blame] | 331 | // 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] | 332 | size_t page_aligned_byte_count = RoundUp(byte_count + page_offset, kPageSize); |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 333 | // The 'expected' is modified (if specified, ie non-null) to be page aligned to the file but not |
| 334 | // necessarily to virtual memory. mmap will page align 'expected' for us. |
| 335 | byte* page_aligned_expected = (expected == nullptr) ? nullptr : (expected - page_offset); |
| 336 | |
| 337 | byte* actual = reinterpret_cast<byte*>(mmap(page_aligned_expected, |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 338 | page_aligned_byte_count, |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 339 | prot, |
| 340 | flags, |
| 341 | fd, |
| 342 | page_aligned_offset)); |
| 343 | if (actual == MAP_FAILED) { |
Brian Carlstrom | aa94cf3 | 2014-03-23 23:47:25 -0700 | [diff] [blame] | 344 | auto saved_errno = errno; |
| 345 | |
jeffhao | 8161c03 | 2012-10-31 15:50:00 -0700 | [diff] [blame] | 346 | std::string maps; |
| 347 | ReadFileToString("/proc/self/maps", &maps); |
Brian Carlstrom | aa94cf3 | 2014-03-23 23:47:25 -0700 | [diff] [blame] | 348 | |
Mathieu Chartier | c7cb190 | 2014-03-05 14:41:03 -0800 | [diff] [blame] | 349 | *error_msg = StringPrintf("mmap(%p, %zd, 0x%x, 0x%x, %d, %" PRId64 |
| 350 | ") of file '%s' failed: %s\n%s", |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 351 | page_aligned_expected, page_aligned_byte_count, prot, flags, fd, |
Brian Carlstrom | aa94cf3 | 2014-03-23 23:47:25 -0700 | [diff] [blame] | 352 | static_cast<int64_t>(page_aligned_offset), filename, |
| 353 | strerror(saved_errno), maps.c_str()); |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 354 | return nullptr; |
| 355 | } |
| 356 | std::ostringstream check_map_request_error_msg; |
| 357 | if (!CheckMapRequest(expected, actual, page_aligned_byte_count, &check_map_request_error_msg)) { |
| 358 | *error_msg = check_map_request_error_msg.str(); |
| 359 | return nullptr; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 360 | } |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 361 | 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] | 362 | prot); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | MemMap::~MemMap() { |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 366 | if (base_begin_ == nullptr && base_size_ == 0) { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 367 | return; |
| 368 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 369 | int result = munmap(base_begin_, base_size_); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 370 | if (result == -1) { |
| 371 | PLOG(FATAL) << "munmap failed"; |
| 372 | } |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 373 | |
| 374 | // Remove it from maps_. |
| 375 | MutexLock mu(Thread::Current(), *Locks::mem_maps_lock_); |
| 376 | bool found = false; |
| 377 | for (auto it = maps_.lower_bound(base_begin_), end = maps_.end(); |
| 378 | it != end && it->first == base_begin_; ++it) { |
| 379 | if (it->second == this) { |
| 380 | found = true; |
| 381 | maps_.erase(it); |
| 382 | break; |
| 383 | } |
| 384 | } |
| 385 | CHECK(found) << "MemMap not found"; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 388 | MemMap::MemMap(const std::string& name, byte* begin, size_t size, void* base_begin, |
| 389 | size_t base_size, int prot) |
| 390 | : name_(name), begin_(begin), size_(size), base_begin_(base_begin), base_size_(base_size), |
| 391 | prot_(prot) { |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 392 | if (size_ == 0) { |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 393 | CHECK(begin_ == nullptr); |
| 394 | CHECK(base_begin_ == nullptr); |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 395 | CHECK_EQ(base_size_, 0U); |
| 396 | } else { |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 397 | CHECK(begin_ != nullptr); |
| 398 | CHECK(base_begin_ != nullptr); |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 399 | CHECK_NE(base_size_, 0U); |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 400 | |
| 401 | // Add it to maps_. |
| 402 | MutexLock mu(Thread::Current(), *Locks::mem_maps_lock_); |
| 403 | maps_.insert(std::pair<void*, MemMap*>(base_begin_, this)); |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 404 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 405 | }; |
| 406 | |
Hiroshi Yamauchi | fd7e7f1 | 2013-10-22 14:17:48 -0700 | [diff] [blame] | 407 | MemMap* MemMap::RemapAtEnd(byte* new_end, const char* tail_name, int tail_prot, |
| 408 | std::string* error_msg) { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 409 | DCHECK_GE(new_end, Begin()); |
| 410 | DCHECK_LE(new_end, End()); |
Hiroshi Yamauchi | fd7e7f1 | 2013-10-22 14:17:48 -0700 | [diff] [blame] | 411 | DCHECK_LE(begin_ + size_, reinterpret_cast<byte*>(base_begin_) + base_size_); |
| 412 | DCHECK(IsAligned<kPageSize>(begin_)); |
| 413 | DCHECK(IsAligned<kPageSize>(base_begin_)); |
| 414 | DCHECK(IsAligned<kPageSize>(reinterpret_cast<byte*>(base_begin_) + base_size_)); |
| 415 | DCHECK(IsAligned<kPageSize>(new_end)); |
| 416 | byte* old_end = begin_ + size_; |
| 417 | byte* old_base_end = reinterpret_cast<byte*>(base_begin_) + base_size_; |
| 418 | byte* new_base_end = new_end; |
| 419 | DCHECK_LE(new_base_end, old_base_end); |
| 420 | if (new_base_end == old_base_end) { |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 421 | return new MemMap(tail_name, nullptr, 0, nullptr, 0, tail_prot); |
Hiroshi Yamauchi | fd7e7f1 | 2013-10-22 14:17:48 -0700 | [diff] [blame] | 422 | } |
| 423 | size_ = new_end - reinterpret_cast<byte*>(begin_); |
| 424 | base_size_ = new_base_end - reinterpret_cast<byte*>(base_begin_); |
| 425 | DCHECK_LE(begin_ + size_, reinterpret_cast<byte*>(base_begin_) + base_size_); |
| 426 | size_t tail_size = old_end - new_end; |
| 427 | byte* tail_base_begin = new_base_end; |
| 428 | size_t tail_base_size = old_base_end - new_base_end; |
| 429 | DCHECK_EQ(tail_base_begin + tail_base_size, old_base_end); |
| 430 | DCHECK(IsAligned<kPageSize>(tail_base_size)); |
| 431 | |
| 432 | #ifdef USE_ASHMEM |
| 433 | // android_os_Debug.cpp read_mapinfo assumes all ashmem regions associated with the VM are |
| 434 | // prefixed "dalvik-". |
| 435 | std::string debug_friendly_name("dalvik-"); |
| 436 | debug_friendly_name += tail_name; |
| 437 | 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] | 438 | int flags = MAP_PRIVATE | MAP_FIXED; |
Hiroshi Yamauchi | fd7e7f1 | 2013-10-22 14:17:48 -0700 | [diff] [blame] | 439 | if (fd.get() == -1) { |
| 440 | *error_msg = StringPrintf("ashmem_create_region failed for '%s': %s", |
| 441 | tail_name, strerror(errno)); |
| 442 | return nullptr; |
| 443 | } |
| 444 | #else |
| 445 | ScopedFd fd(-1); |
| 446 | int flags = MAP_PRIVATE | MAP_ANONYMOUS; |
| 447 | #endif |
| 448 | |
| 449 | // Unmap/map the tail region. |
| 450 | int result = munmap(tail_base_begin, tail_base_size); |
| 451 | if (result == -1) { |
| 452 | std::string maps; |
| 453 | ReadFileToString("/proc/self/maps", &maps); |
| 454 | *error_msg = StringPrintf("munmap(%p, %zd) failed for '%s'\n%s", |
| 455 | tail_base_begin, tail_base_size, name_.c_str(), |
| 456 | maps.c_str()); |
| 457 | return nullptr; |
| 458 | } |
| 459 | // Don't cause memory allocation between the munmap and the mmap |
| 460 | // calls. Otherwise, libc (or something else) might take this memory |
| 461 | // region. Note this isn't perfect as there's no way to prevent |
| 462 | // other threads to try to take this memory region here. |
| 463 | byte* actual = reinterpret_cast<byte*>(mmap(tail_base_begin, tail_base_size, tail_prot, |
| 464 | flags, fd.get(), 0)); |
| 465 | if (actual == MAP_FAILED) { |
| 466 | std::string maps; |
| 467 | ReadFileToString("/proc/self/maps", &maps); |
Mathieu Chartier | c7cb190 | 2014-03-05 14:41:03 -0800 | [diff] [blame] | 468 | *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] | 469 | tail_base_begin, tail_base_size, tail_prot, flags, fd.get(), |
| 470 | maps.c_str()); |
| 471 | return nullptr; |
| 472 | } |
| 473 | 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] | 474 | } |
Logan Chien | d88fa26 | 2012-06-06 15:23:32 +0800 | [diff] [blame] | 475 | |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 476 | void MemMap::MadviseDontNeedAndZero() { |
| 477 | if (base_begin_ != nullptr || base_size_ != 0) { |
| 478 | if (!kMadviseZeroes) { |
| 479 | memset(base_begin_, 0, base_size_); |
| 480 | } |
| 481 | int result = madvise(base_begin_, base_size_, MADV_DONTNEED); |
| 482 | if (result == -1) { |
| 483 | PLOG(WARNING) << "madvise failed"; |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
Logan Chien | d88fa26 | 2012-06-06 15:23:32 +0800 | [diff] [blame] | 488 | bool MemMap::Protect(int prot) { |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 489 | if (base_begin_ == nullptr && base_size_ == 0) { |
Ian Rogers | 1c849e5 | 2012-06-28 14:00:33 -0700 | [diff] [blame] | 490 | prot_ = prot; |
Logan Chien | d88fa26 | 2012-06-06 15:23:32 +0800 | [diff] [blame] | 491 | return true; |
| 492 | } |
| 493 | |
| 494 | if (mprotect(base_begin_, base_size_, prot) == 0) { |
Ian Rogers | 1c849e5 | 2012-06-28 14:00:33 -0700 | [diff] [blame] | 495 | prot_ = prot; |
Logan Chien | d88fa26 | 2012-06-06 15:23:32 +0800 | [diff] [blame] | 496 | return true; |
| 497 | } |
| 498 | |
Shih-wei Liao | a060ed9 | 2012-06-07 09:25:28 -0700 | [diff] [blame] | 499 | PLOG(ERROR) << "mprotect(" << reinterpret_cast<void*>(base_begin_) << ", " << base_size_ << ", " |
| 500 | << prot << ") failed"; |
Logan Chien | d88fa26 | 2012-06-06 15:23:32 +0800 | [diff] [blame] | 501 | return false; |
| 502 | } |
| 503 | |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 504 | bool MemMap::CheckNoGaps(MemMap* begin_map, MemMap* end_map) { |
| 505 | MutexLock mu(Thread::Current(), *Locks::mem_maps_lock_); |
| 506 | CHECK(begin_map != nullptr); |
| 507 | CHECK(end_map != nullptr); |
| 508 | CHECK(HasMemMap(begin_map)); |
| 509 | CHECK(HasMemMap(end_map)); |
| 510 | CHECK_LE(begin_map->BaseBegin(), end_map->BaseBegin()); |
| 511 | MemMap* map = begin_map; |
| 512 | while (map->BaseBegin() != end_map->BaseBegin()) { |
| 513 | MemMap* next_map = GetLargestMemMapAt(map->BaseEnd()); |
| 514 | if (next_map == nullptr) { |
| 515 | // Found a gap. |
| 516 | return false; |
| 517 | } |
| 518 | map = next_map; |
| 519 | } |
| 520 | return true; |
| 521 | } |
| 522 | |
| 523 | void MemMap::DumpMaps(std::ostream& os) { |
| 524 | DumpMaps(os, maps_); |
| 525 | } |
| 526 | |
| 527 | void MemMap::DumpMaps(std::ostream& os, const std::multimap<void*, MemMap*>& mem_maps) { |
| 528 | MutexLock mu(Thread::Current(), *Locks::mem_maps_lock_); |
| 529 | DumpMapsLocked(os, mem_maps); |
| 530 | } |
| 531 | |
| 532 | void MemMap::DumpMapsLocked(std::ostream& os, const std::multimap<void*, MemMap*>& mem_maps) { |
| 533 | os << mem_maps; |
| 534 | } |
| 535 | |
| 536 | bool MemMap::HasMemMap(MemMap* map) { |
| 537 | void* base_begin = map->BaseBegin(); |
| 538 | for (auto it = maps_.lower_bound(base_begin), end = maps_.end(); |
| 539 | it != end && it->first == base_begin; ++it) { |
| 540 | if (it->second == map) { |
| 541 | return true; |
| 542 | } |
| 543 | } |
| 544 | return false; |
| 545 | } |
| 546 | |
| 547 | MemMap* MemMap::GetLargestMemMapAt(void* address) { |
| 548 | size_t largest_size = 0; |
| 549 | MemMap* largest_map = nullptr; |
| 550 | for (auto it = maps_.lower_bound(address), end = maps_.end(); |
| 551 | it != end && it->first == address; ++it) { |
| 552 | MemMap* map = it->second; |
| 553 | CHECK(map != nullptr); |
| 554 | if (largest_size < map->BaseSize()) { |
| 555 | largest_size = map->BaseSize(); |
| 556 | largest_map = map; |
| 557 | } |
| 558 | } |
| 559 | return largest_map; |
| 560 | } |
| 561 | |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 562 | std::ostream& operator<<(std::ostream& os, const MemMap& mem_map) { |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 563 | os << StringPrintf("[MemMap: %p-%p prot=0x%x %s]", |
| 564 | mem_map.BaseBegin(), mem_map.BaseEnd(), mem_map.GetProtect(), |
| 565 | mem_map.GetName().c_str()); |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 566 | return os; |
| 567 | } |
| 568 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 569 | } // namespace art |