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 | |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame^] | 49 | static bool CheckMapRequest(byte* expected_ptr, void* actual_ptr, size_t byte_count, |
| 50 | std::ostringstream* error_msg) { |
| 51 | // Handled first by caller for more specific error messages. |
| 52 | CHECK(actual_ptr != MAP_FAILED); |
| 53 | |
| 54 | if (expected_ptr == nullptr) { |
| 55 | return true; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 56 | } |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 57 | |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame^] | 58 | if (expected_ptr == actual_ptr) { |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | // We asked for an address but didn't get what we wanted, all paths below here should fail. |
| 63 | int result = munmap(actual_ptr, byte_count); |
| 64 | if (result == -1) { |
| 65 | PLOG(WARNING) << StringPrintf("munmap(%p, %zd) failed", actual_ptr, byte_count); |
| 66 | } |
| 67 | |
| 68 | uintptr_t actual = reinterpret_cast<uintptr_t>(actual_ptr); |
| 69 | uintptr_t expected = reinterpret_cast<uintptr_t>(expected_ptr); |
| 70 | uintptr_t limit = expected + byte_count; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 71 | |
Christopher Ferris | caf22ac | 2014-01-27 18:32:14 -0800 | [diff] [blame] | 72 | UniquePtr<BacktraceMap> map(BacktraceMap::Create(getpid())); |
| 73 | if (!map->Build()) { |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame^] | 74 | *error_msg << StringPrintf("Failed to build process map to determine why mmap returned " |
| 75 | "0x%08" PRIxPTR " instead of 0x%08" PRIxPTR, actual, expected); |
| 76 | |
| 77 | return false; |
Christopher Ferris | 943af7d | 2014-01-16 12:41:46 -0800 | [diff] [blame] | 78 | } |
Christopher Ferris | caf22ac | 2014-01-27 18:32:14 -0800 | [diff] [blame] | 79 | for (BacktraceMap::const_iterator it = map->begin(); it != map->end(); ++it) { |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame^] | 80 | if ((expected >= it->start && expected < it->end) // start of new within old |
| 81 | || (limit > it->start && limit < it->end) // end of new within old |
| 82 | || (expected <= it->start && limit > it->end)) { // start/end of new includes all of old |
| 83 | *error_msg |
| 84 | << StringPrintf("Requested region 0x%08" PRIxPTR "-0x%08" PRIxPTR " overlaps with " |
| 85 | "existing map 0x%08" PRIxPTR "-0x%08" PRIxPTR " (%s)\n", |
| 86 | expected, limit, |
| 87 | static_cast<uintptr_t>(it->start), static_cast<uintptr_t>(it->end), |
| 88 | it->name.c_str()) |
| 89 | << std::make_pair(it, map->end()); |
| 90 | return false; |
| 91 | } |
Elliott Hughes | 96970cd | 2012-03-13 15:46:31 -0700 | [diff] [blame] | 92 | } |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame^] | 93 | *error_msg << StringPrintf("Failed to mmap at expected address, mapped at " |
| 94 | "0x%08" PRIxPTR " instead of 0x%08" PRIxPTR, actual, expected); |
| 95 | return false; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame^] | 98 | 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] | 99 | bool low_4gb, std::string* error_msg) { |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 100 | if (byte_count == 0) { |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame^] | 101 | return new MemMap(name, nullptr, 0, nullptr, 0, prot); |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 102 | } |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 103 | size_t page_aligned_byte_count = RoundUp(byte_count, kPageSize); |
Elliott Hughes | 6c9c06d | 2011-11-07 16:43:47 -0800 | [diff] [blame] | 104 | |
| 105 | #ifdef USE_ASHMEM |
Ian Rogers | a40307e | 2013-02-22 11:32:44 -0800 | [diff] [blame] | 106 | // android_os_Debug.cpp read_mapinfo assumes all ashmem regions associated with the VM are |
| 107 | // prefixed "dalvik-". |
| 108 | std::string debug_friendly_name("dalvik-"); |
| 109 | debug_friendly_name += name; |
| 110 | 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] | 111 | if (fd.get() == -1) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 112 | *error_msg = StringPrintf("ashmem_create_region failed for '%s': %s", name, strerror(errno)); |
| 113 | return nullptr; |
Elliott Hughes | 6c9c06d | 2011-11-07 16:43:47 -0800 | [diff] [blame] | 114 | } |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame^] | 115 | int flags = MAP_PRIVATE; |
Elliott Hughes | 6c9c06d | 2011-11-07 16:43:47 -0800 | [diff] [blame] | 116 | #else |
| 117 | ScopedFd fd(-1); |
| 118 | int flags = MAP_PRIVATE | MAP_ANONYMOUS; |
| 119 | #endif |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 120 | #ifdef __LP64__ |
| 121 | if (low_4gb) { |
| 122 | flags |= MAP_32BIT; |
| 123 | } |
| 124 | #endif |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame^] | 125 | |
| 126 | void* actual = mmap(expected, page_aligned_byte_count, prot, flags, fd.get(), 0); |
| 127 | std::string strerr(strerror(errno)); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 128 | if (actual == MAP_FAILED) { |
jeffhao | 8161c03 | 2012-10-31 15:50:00 -0700 | [diff] [blame] | 129 | std::string maps; |
| 130 | ReadFileToString("/proc/self/maps", &maps); |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame^] | 131 | *error_msg = StringPrintf("Failed anonymous mmap(%p, %zd, 0x%x, 0x%x, %d, 0): %s\n%s", |
| 132 | expected, page_aligned_byte_count, prot, flags, fd.get(), |
| 133 | strerr.c_str(), maps.c_str()); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 134 | return nullptr; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 135 | } |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame^] | 136 | std::ostringstream check_map_request_error_msg; |
| 137 | if (!CheckMapRequest(expected, actual, page_aligned_byte_count, &check_map_request_error_msg)) { |
| 138 | *error_msg = check_map_request_error_msg.str(); |
| 139 | return nullptr; |
| 140 | } |
| 141 | return new MemMap(name, reinterpret_cast<byte*>(actual), byte_count, actual, |
| 142 | page_aligned_byte_count, prot); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame^] | 145 | 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] | 146 | off_t start, bool reuse, const char* filename, |
| 147 | std::string* error_msg) { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 148 | CHECK_NE(0, prot); |
| 149 | CHECK_NE(0, flags & (MAP_SHARED | MAP_PRIVATE)); |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame^] | 150 | if (reuse) { |
| 151 | // reuse means it is okay that it overlaps an existing page mapping. |
| 152 | // Only use this if you actually made the page reservation yourself. |
| 153 | CHECK(expected != nullptr); |
| 154 | flags |= MAP_FIXED; |
| 155 | } else { |
| 156 | CHECK_EQ(0, flags & MAP_FIXED); |
| 157 | } |
| 158 | |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 159 | if (byte_count == 0) { |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame^] | 160 | return new MemMap(filename, nullptr, 0, nullptr, 0, prot); |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 161 | } |
Ian Rogers | f8adc60 | 2013-04-18 17:06:19 -0700 | [diff] [blame] | 162 | // Adjust 'offset' to be page-aligned as required by mmap. |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 163 | int page_offset = start % kPageSize; |
| 164 | off_t page_aligned_offset = start - page_offset; |
Ian Rogers | f8adc60 | 2013-04-18 17:06:19 -0700 | [diff] [blame] | 165 | // 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] | 166 | size_t page_aligned_byte_count = RoundUp(byte_count + page_offset, kPageSize); |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame^] | 167 | // The 'expected' is modified (if specified, ie non-null) to be page aligned to the file but not |
| 168 | // necessarily to virtual memory. mmap will page align 'expected' for us. |
| 169 | byte* page_aligned_expected = (expected == nullptr) ? nullptr : (expected - page_offset); |
| 170 | |
| 171 | byte* actual = reinterpret_cast<byte*>(mmap(page_aligned_expected, |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 172 | page_aligned_byte_count, |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 173 | prot, |
| 174 | flags, |
| 175 | fd, |
| 176 | page_aligned_offset)); |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame^] | 177 | std::string strerr(strerror(errno)); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 178 | if (actual == MAP_FAILED) { |
jeffhao | 8161c03 | 2012-10-31 15:50:00 -0700 | [diff] [blame] | 179 | std::string maps; |
| 180 | ReadFileToString("/proc/self/maps", &maps); |
Mathieu Chartier | c7cb190 | 2014-03-05 14:41:03 -0800 | [diff] [blame] | 181 | *error_msg = StringPrintf("mmap(%p, %zd, 0x%x, 0x%x, %d, %" PRId64 |
| 182 | ") of file '%s' failed: %s\n%s", |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame^] | 183 | page_aligned_expected, page_aligned_byte_count, prot, flags, fd, |
Mathieu Chartier | 3b6f0fa | 2013-11-13 13:26:07 -0800 | [diff] [blame] | 184 | static_cast<int64_t>(page_aligned_offset), filename, strerr.c_str(), |
| 185 | maps.c_str()); |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame^] | 186 | return nullptr; |
| 187 | } |
| 188 | std::ostringstream check_map_request_error_msg; |
| 189 | if (!CheckMapRequest(expected, actual, page_aligned_byte_count, &check_map_request_error_msg)) { |
| 190 | *error_msg = check_map_request_error_msg.str(); |
| 191 | return nullptr; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 192 | } |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 193 | 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] | 194 | prot); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | MemMap::~MemMap() { |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame^] | 198 | if (base_begin_ == nullptr && base_size_ == 0) { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 199 | return; |
| 200 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 201 | int result = munmap(base_begin_, base_size_); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 202 | if (result == -1) { |
| 203 | PLOG(FATAL) << "munmap failed"; |
| 204 | } |
| 205 | } |
| 206 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 207 | MemMap::MemMap(const std::string& name, byte* begin, size_t size, void* base_begin, |
| 208 | size_t base_size, int prot) |
| 209 | : name_(name), begin_(begin), size_(size), base_begin_(base_begin), base_size_(base_size), |
| 210 | prot_(prot) { |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 211 | if (size_ == 0) { |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame^] | 212 | CHECK(begin_ == nullptr); |
| 213 | CHECK(base_begin_ == nullptr); |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 214 | CHECK_EQ(base_size_, 0U); |
| 215 | } else { |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame^] | 216 | CHECK(begin_ != nullptr); |
| 217 | CHECK(base_begin_ != nullptr); |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 218 | CHECK_NE(base_size_, 0U); |
| 219 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 220 | }; |
| 221 | |
Hiroshi Yamauchi | fd7e7f1 | 2013-10-22 14:17:48 -0700 | [diff] [blame] | 222 | MemMap* MemMap::RemapAtEnd(byte* new_end, const char* tail_name, int tail_prot, |
| 223 | std::string* error_msg) { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 224 | DCHECK_GE(new_end, Begin()); |
| 225 | DCHECK_LE(new_end, End()); |
Hiroshi Yamauchi | fd7e7f1 | 2013-10-22 14:17:48 -0700 | [diff] [blame] | 226 | DCHECK_LE(begin_ + size_, reinterpret_cast<byte*>(base_begin_) + base_size_); |
| 227 | DCHECK(IsAligned<kPageSize>(begin_)); |
| 228 | DCHECK(IsAligned<kPageSize>(base_begin_)); |
| 229 | DCHECK(IsAligned<kPageSize>(reinterpret_cast<byte*>(base_begin_) + base_size_)); |
| 230 | DCHECK(IsAligned<kPageSize>(new_end)); |
| 231 | byte* old_end = begin_ + size_; |
| 232 | byte* old_base_end = reinterpret_cast<byte*>(base_begin_) + base_size_; |
| 233 | byte* new_base_end = new_end; |
| 234 | DCHECK_LE(new_base_end, old_base_end); |
| 235 | if (new_base_end == old_base_end) { |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame^] | 236 | return new MemMap(tail_name, nullptr, 0, nullptr, 0, tail_prot); |
Hiroshi Yamauchi | fd7e7f1 | 2013-10-22 14:17:48 -0700 | [diff] [blame] | 237 | } |
| 238 | size_ = new_end - reinterpret_cast<byte*>(begin_); |
| 239 | base_size_ = new_base_end - reinterpret_cast<byte*>(base_begin_); |
| 240 | DCHECK_LE(begin_ + size_, reinterpret_cast<byte*>(base_begin_) + base_size_); |
| 241 | size_t tail_size = old_end - new_end; |
| 242 | byte* tail_base_begin = new_base_end; |
| 243 | size_t tail_base_size = old_base_end - new_base_end; |
| 244 | DCHECK_EQ(tail_base_begin + tail_base_size, old_base_end); |
| 245 | DCHECK(IsAligned<kPageSize>(tail_base_size)); |
| 246 | |
| 247 | #ifdef USE_ASHMEM |
| 248 | // android_os_Debug.cpp read_mapinfo assumes all ashmem regions associated with the VM are |
| 249 | // prefixed "dalvik-". |
| 250 | std::string debug_friendly_name("dalvik-"); |
| 251 | debug_friendly_name += tail_name; |
| 252 | ScopedFd fd(ashmem_create_region(debug_friendly_name.c_str(), tail_base_size)); |
| 253 | int flags = MAP_PRIVATE; |
| 254 | if (fd.get() == -1) { |
| 255 | *error_msg = StringPrintf("ashmem_create_region failed for '%s': %s", |
| 256 | tail_name, strerror(errno)); |
| 257 | return nullptr; |
| 258 | } |
| 259 | #else |
| 260 | ScopedFd fd(-1); |
| 261 | int flags = MAP_PRIVATE | MAP_ANONYMOUS; |
| 262 | #endif |
| 263 | |
| 264 | // Unmap/map the tail region. |
| 265 | int result = munmap(tail_base_begin, tail_base_size); |
| 266 | if (result == -1) { |
| 267 | std::string maps; |
| 268 | ReadFileToString("/proc/self/maps", &maps); |
| 269 | *error_msg = StringPrintf("munmap(%p, %zd) failed for '%s'\n%s", |
| 270 | tail_base_begin, tail_base_size, name_.c_str(), |
| 271 | maps.c_str()); |
| 272 | return nullptr; |
| 273 | } |
| 274 | // Don't cause memory allocation between the munmap and the mmap |
| 275 | // calls. Otherwise, libc (or something else) might take this memory |
| 276 | // region. Note this isn't perfect as there's no way to prevent |
| 277 | // other threads to try to take this memory region here. |
| 278 | byte* actual = reinterpret_cast<byte*>(mmap(tail_base_begin, tail_base_size, tail_prot, |
| 279 | flags, fd.get(), 0)); |
| 280 | if (actual == MAP_FAILED) { |
| 281 | std::string maps; |
| 282 | ReadFileToString("/proc/self/maps", &maps); |
Mathieu Chartier | c7cb190 | 2014-03-05 14:41:03 -0800 | [diff] [blame] | 283 | *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] | 284 | tail_base_begin, tail_base_size, tail_prot, flags, fd.get(), |
| 285 | maps.c_str()); |
| 286 | return nullptr; |
| 287 | } |
| 288 | 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] | 289 | } |
Logan Chien | d88fa26 | 2012-06-06 15:23:32 +0800 | [diff] [blame] | 290 | |
| 291 | bool MemMap::Protect(int prot) { |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame^] | 292 | if (base_begin_ == nullptr && base_size_ == 0) { |
Ian Rogers | 1c849e5 | 2012-06-28 14:00:33 -0700 | [diff] [blame] | 293 | prot_ = prot; |
Logan Chien | d88fa26 | 2012-06-06 15:23:32 +0800 | [diff] [blame] | 294 | return true; |
| 295 | } |
| 296 | |
| 297 | if (mprotect(base_begin_, base_size_, prot) == 0) { |
Ian Rogers | 1c849e5 | 2012-06-28 14:00:33 -0700 | [diff] [blame] | 298 | prot_ = prot; |
Logan Chien | d88fa26 | 2012-06-06 15:23:32 +0800 | [diff] [blame] | 299 | return true; |
| 300 | } |
| 301 | |
Shih-wei Liao | a060ed9 | 2012-06-07 09:25:28 -0700 | [diff] [blame] | 302 | PLOG(ERROR) << "mprotect(" << reinterpret_cast<void*>(base_begin_) << ", " << base_size_ << ", " |
| 303 | << prot << ") failed"; |
Logan Chien | d88fa26 | 2012-06-06 15:23:32 +0800 | [diff] [blame] | 304 | return false; |
| 305 | } |
| 306 | |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 307 | std::ostream& operator<<(std::ostream& os, const MemMap& mem_map) { |
Mathieu Chartier | c7cb190 | 2014-03-05 14:41:03 -0800 | [diff] [blame] | 308 | os << StringPrintf("[MemMap: %s prot=0x%x %p-%p]", |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 309 | mem_map.GetName().c_str(), mem_map.GetProtect(), |
| 310 | mem_map.BaseBegin(), mem_map.BaseEnd()); |
| 311 | return os; |
| 312 | } |
| 313 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 314 | } // namespace art |