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