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 | |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 19 | #include <corkscrew/map_info.h> |
| 20 | |
Elliott Hughes | e222ee0 | 2012-12-13 14:41:43 -0800 | [diff] [blame] | 21 | #include "base/stringprintf.h" |
| 22 | #include "ScopedFd.h" |
| 23 | #include "utils.h" |
| 24 | |
Elliott Hughes | 6c9c06d | 2011-11-07 16:43:47 -0800 | [diff] [blame] | 25 | #define USE_ASHMEM 1 |
| 26 | |
| 27 | #ifdef USE_ASHMEM |
| 28 | #include <cutils/ashmem.h> |
| 29 | #endif |
| 30 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 31 | namespace art { |
| 32 | |
Elliott Hughes | 96970cd | 2012-03-13 15:46:31 -0700 | [diff] [blame] | 33 | #if !defined(NDEBUG) |
| 34 | |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 35 | static std::ostream& operator<<(std::ostream& os, map_info_t* rhs) { |
| 36 | for (map_info_t* m = rhs; m != NULL; m = m->next) { |
Elliott Hughes | 7bda158 | 2012-06-07 11:00:54 -0700 | [diff] [blame] | 37 | os << StringPrintf("0x%08x-0x%08x %c%c %s\n", |
| 38 | static_cast<uint32_t>(m->start), |
| 39 | static_cast<uint32_t>(m->end), |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 40 | m->is_readable ? 'r' : '-', m->is_executable ? 'x' : '-', m->name); |
| 41 | } |
| 42 | return os; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Ian Rogers | 10c5b78 | 2013-01-10 10:40:53 -0800 | [diff] [blame] | 45 | static void CheckMapRequest(byte* addr, size_t byte_count) { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 46 | if (addr == NULL) { |
| 47 | return; |
| 48 | } |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 49 | |
Elliott Hughes | 96970cd | 2012-03-13 15:46:31 -0700 | [diff] [blame] | 50 | uint32_t base = reinterpret_cast<size_t>(addr); |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 51 | uint32_t limit = base + byte_count; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 52 | |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 53 | map_info_t* map_info_list = load_map_info_list(getpid()); |
| 54 | for (map_info_t* m = map_info_list; m != NULL; m = m->next) { |
| 55 | CHECK(!(base >= m->start && base < m->end) // start of new within old |
| 56 | && !(limit > m->start && limit < m->end) // end of new within old |
| 57 | && !(base <= m->start && limit > m->end)) // start/end of new includes all of old |
Elliott Hughes | 7bda158 | 2012-06-07 11:00:54 -0700 | [diff] [blame] | 58 | << StringPrintf("Requested region 0x%08x-0x%08x overlaps with existing map 0x%08x-0x%08x (%s)\n", |
| 59 | base, limit, |
| 60 | static_cast<uint32_t>(m->start), static_cast<uint32_t>(m->end), m->name) |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 61 | << map_info_list; |
Elliott Hughes | 96970cd | 2012-03-13 15:46:31 -0700 | [diff] [blame] | 62 | } |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 63 | free_map_info_list(map_info_list); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Elliott Hughes | 96970cd | 2012-03-13 15:46:31 -0700 | [diff] [blame] | 66 | #else |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 67 | static void CheckMapRequest(byte*, size_t) { } |
Elliott Hughes | 96970cd | 2012-03-13 15:46:31 -0700 | [diff] [blame] | 68 | #endif |
| 69 | |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 70 | MemMap* MemMap::MapAnonymous(const char* name, byte* addr, size_t byte_count, int prot) { |
| 71 | CHECK_NE(0U, byte_count); |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 72 | size_t page_aligned_byte_count = RoundUp(byte_count, kPageSize); |
| 73 | CheckMapRequest(addr, page_aligned_byte_count); |
Elliott Hughes | 6c9c06d | 2011-11-07 16:43:47 -0800 | [diff] [blame] | 74 | |
| 75 | #ifdef USE_ASHMEM |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 76 | ScopedFd fd(ashmem_create_region(name, page_aligned_byte_count)); |
Elliott Hughes | 6c9c06d | 2011-11-07 16:43:47 -0800 | [diff] [blame] | 77 | int flags = MAP_PRIVATE; |
| 78 | if (fd.get() == -1) { |
| 79 | PLOG(ERROR) << "ashmem_create_region failed (" << name << ")"; |
| 80 | return NULL; |
| 81 | } |
| 82 | #else |
| 83 | ScopedFd fd(-1); |
| 84 | int flags = MAP_PRIVATE | MAP_ANONYMOUS; |
| 85 | #endif |
| 86 | |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 87 | byte* actual = reinterpret_cast<byte*>(mmap(addr, page_aligned_byte_count, prot, flags, fd.get(), 0)); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 88 | if (actual == MAP_FAILED) { |
jeffhao | 8161c03 | 2012-10-31 15:50:00 -0700 | [diff] [blame] | 89 | std::string maps; |
| 90 | ReadFileToString("/proc/self/maps", &maps); |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 91 | PLOG(ERROR) << "mmap(" << reinterpret_cast<void*>(addr) << ", " << page_aligned_byte_count |
jeffhao | 8161c03 | 2012-10-31 15:50:00 -0700 | [diff] [blame] | 92 | << ", " << prot << ", " << flags << ", " << fd.get() << ", 0) failed for " << name |
| 93 | << "\n" << maps; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 94 | return NULL; |
| 95 | } |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 96 | return new MemMap(name, actual, byte_count, actual, page_aligned_byte_count, prot); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 99 | MemMap* MemMap::MapFileAtAddress(byte* addr, size_t byte_count, int prot, int flags, int fd, off_t start) { |
| 100 | CHECK_NE(0U, byte_count); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 101 | CHECK_NE(0, prot); |
| 102 | CHECK_NE(0, flags & (MAP_SHARED | MAP_PRIVATE)); |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 103 | // Adjust 'offset' and 'byte_count' to be page-aligned. |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 104 | int page_offset = start % kPageSize; |
| 105 | off_t page_aligned_offset = start - page_offset; |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 106 | size_t page_aligned_byte_count = RoundUp(byte_count + page_offset, kPageSize); |
| 107 | CheckMapRequest(addr, page_aligned_byte_count); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 108 | byte* actual = reinterpret_cast<byte*>(mmap(addr, |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 109 | page_aligned_byte_count, |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 110 | prot, |
| 111 | flags, |
| 112 | fd, |
| 113 | page_aligned_offset)); |
| 114 | if (actual == MAP_FAILED) { |
jeffhao | 8161c03 | 2012-10-31 15:50:00 -0700 | [diff] [blame] | 115 | std::string maps; |
| 116 | ReadFileToString("/proc/self/maps", &maps); |
| 117 | PLOG(ERROR) << "mmap(" << reinterpret_cast<void*>(addr) << ", " << page_aligned_byte_count |
| 118 | << ", " << prot << ", " << flags << ", " << fd << ", " << page_aligned_offset |
| 119 | << ") failed\n" << maps; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 120 | return NULL; |
| 121 | } |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 122 | return new MemMap("file", actual + page_offset, byte_count, actual, page_aligned_byte_count, |
| 123 | prot); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | MemMap::~MemMap() { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 127 | if (base_begin_ == NULL && base_size_ == 0) { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 128 | return; |
| 129 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 130 | int result = munmap(base_begin_, base_size_); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 131 | if (result == -1) { |
| 132 | PLOG(FATAL) << "munmap failed"; |
| 133 | } |
| 134 | } |
| 135 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 136 | MemMap::MemMap(const std::string& name, byte* begin, size_t size, void* base_begin, |
| 137 | size_t base_size, int prot) |
| 138 | : name_(name), begin_(begin), size_(size), base_begin_(base_begin), base_size_(base_size), |
| 139 | prot_(prot) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 140 | CHECK(begin_ != NULL); |
| 141 | CHECK_NE(size_, 0U); |
| 142 | CHECK(base_begin_ != NULL); |
| 143 | CHECK_NE(base_size_, 0U); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 144 | }; |
| 145 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 146 | void MemMap::UnMapAtEnd(byte* new_end) { |
| 147 | DCHECK_GE(new_end, Begin()); |
| 148 | DCHECK_LE(new_end, End()); |
| 149 | size_t unmap_size = End() - new_end; |
| 150 | munmap(new_end, unmap_size); |
| 151 | size_ -= unmap_size; |
| 152 | } |
Logan Chien | d88fa26 | 2012-06-06 15:23:32 +0800 | [diff] [blame] | 153 | |
| 154 | bool MemMap::Protect(int prot) { |
| 155 | if (base_begin_ == NULL && base_size_ == 0) { |
Ian Rogers | 1c849e5 | 2012-06-28 14:00:33 -0700 | [diff] [blame] | 156 | prot_ = prot; |
Logan Chien | d88fa26 | 2012-06-06 15:23:32 +0800 | [diff] [blame] | 157 | return true; |
| 158 | } |
| 159 | |
| 160 | if (mprotect(base_begin_, base_size_, prot) == 0) { |
Ian Rogers | 1c849e5 | 2012-06-28 14:00:33 -0700 | [diff] [blame] | 161 | prot_ = prot; |
Logan Chien | d88fa26 | 2012-06-06 15:23:32 +0800 | [diff] [blame] | 162 | return true; |
| 163 | } |
| 164 | |
Shih-wei Liao | a060ed9 | 2012-06-07 09:25:28 -0700 | [diff] [blame] | 165 | PLOG(ERROR) << "mprotect(" << reinterpret_cast<void*>(base_begin_) << ", " << base_size_ << ", " |
| 166 | << prot << ") failed"; |
Logan Chien | d88fa26 | 2012-06-06 15:23:32 +0800 | [diff] [blame] | 167 | return false; |
| 168 | } |
| 169 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 170 | } // namespace art |