blob: 39e838f45711996dcb067e27004762b08cbf03e7 [file] [log] [blame]
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001/*
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
Christopher Ferris7b5f0cf2013-11-01 15:18:45 -070019#include <backtrace/backtrace.h>
Elliott Hughesecd3a6f2012-06-06 18:16:37 -070020
Elliott Hughese222ee02012-12-13 14:41:43 -080021#include "base/stringprintf.h"
22#include "ScopedFd.h"
23#include "utils.h"
24
Elliott Hughes6c9c06d2011-11-07 16:43:47 -080025#define USE_ASHMEM 1
26
27#ifdef USE_ASHMEM
28#include <cutils/ashmem.h>
29#endif
30
Brian Carlstrom27ec9612011-09-19 20:20:38 -070031namespace art {
32
Elliott Hughes96970cd2012-03-13 15:46:31 -070033#if !defined(NDEBUG)
34
Christopher Ferris7b5f0cf2013-11-01 15:18:45 -070035static std::ostream& operator<<(std::ostream& os, backtrace_map_info_t* rhs) {
36 for (backtrace_map_info_t* m = rhs; m != NULL; m = m->next) {
Elliott Hughes7bda1582012-06-07 11:00:54 -070037 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 Hughesecd3a6f2012-06-06 18:16:37 -070040 m->is_readable ? 'r' : '-', m->is_executable ? 'x' : '-', m->name);
41 }
42 return os;
Brian Carlstrom27ec9612011-09-19 20:20:38 -070043}
44
Ian Rogers10c5b782013-01-10 10:40:53 -080045static void CheckMapRequest(byte* addr, size_t byte_count) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -070046 if (addr == NULL) {
47 return;
48 }
Elliott Hughesecd3a6f2012-06-06 18:16:37 -070049
Elliott Hughes96970cd2012-03-13 15:46:31 -070050 uint32_t base = reinterpret_cast<size_t>(addr);
Elliott Hughesecd3a6f2012-06-06 18:16:37 -070051 uint32_t limit = base + byte_count;
Brian Carlstrom27ec9612011-09-19 20:20:38 -070052
Christopher Ferris7b5f0cf2013-11-01 15:18:45 -070053 backtrace_map_info_t* map_info_list = backtrace_create_map_info_list(getpid());
54 for (backtrace_map_info_t* m = map_info_list; m != NULL; m = m->next) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -070055 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 Hughes7bda1582012-06-07 11:00:54 -070058 << 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 Hughesecd3a6f2012-06-06 18:16:37 -070061 << map_info_list;
Elliott Hughes96970cd2012-03-13 15:46:31 -070062 }
Christopher Ferris7b5f0cf2013-11-01 15:18:45 -070063 backtrace_destroy_map_info_list(map_info_list);
Brian Carlstrom27ec9612011-09-19 20:20:38 -070064}
65
Elliott Hughes96970cd2012-03-13 15:46:31 -070066#else
Elliott Hughes1bac54f2012-03-16 12:48:31 -070067static void CheckMapRequest(byte*, size_t) { }
Elliott Hughes96970cd2012-03-13 15:46:31 -070068#endif
69
Ian Rogers8d31bbd2013-10-13 10:44:14 -070070MemMap* MemMap::MapAnonymous(const char* name, byte* addr, size_t byte_count, int prot,
71 std::string* error_msg) {
Brian Carlstrom9004cb62013-07-26 15:48:31 -070072 if (byte_count == 0) {
73 return new MemMap(name, NULL, 0, NULL, 0, prot);
74 }
Elliott Hughesecd3a6f2012-06-06 18:16:37 -070075 size_t page_aligned_byte_count = RoundUp(byte_count, kPageSize);
76 CheckMapRequest(addr, page_aligned_byte_count);
Elliott Hughes6c9c06d2011-11-07 16:43:47 -080077
78#ifdef USE_ASHMEM
Ian Rogersa40307e2013-02-22 11:32:44 -080079 // android_os_Debug.cpp read_mapinfo assumes all ashmem regions associated with the VM are
80 // prefixed "dalvik-".
81 std::string debug_friendly_name("dalvik-");
82 debug_friendly_name += name;
83 ScopedFd fd(ashmem_create_region(debug_friendly_name.c_str(), page_aligned_byte_count));
Elliott Hughes6c9c06d2011-11-07 16:43:47 -080084 int flags = MAP_PRIVATE;
85 if (fd.get() == -1) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070086 *error_msg = StringPrintf("ashmem_create_region failed for '%s': %s", name, strerror(errno));
87 return nullptr;
Elliott Hughes6c9c06d2011-11-07 16:43:47 -080088 }
89#else
90 ScopedFd fd(-1);
91 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
92#endif
93
Elliott Hughesecd3a6f2012-06-06 18:16:37 -070094 byte* actual = reinterpret_cast<byte*>(mmap(addr, page_aligned_byte_count, prot, flags, fd.get(), 0));
Brian Carlstrom27ec9612011-09-19 20:20:38 -070095 if (actual == MAP_FAILED) {
jeffhao8161c032012-10-31 15:50:00 -070096 std::string maps;
97 ReadFileToString("/proc/self/maps", &maps);
Ian Rogers8d31bbd2013-10-13 10:44:14 -070098 *error_msg = StringPrintf("anonymous mmap(%p, %zd, %x, %x, %d, 0) failed\n%s",
99 addr, page_aligned_byte_count, prot, flags, fd.get(),
100 maps.c_str());
101 return nullptr;
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700102 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700103 return new MemMap(name, actual, byte_count, actual, page_aligned_byte_count, prot);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700104}
105
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700106MemMap* MemMap::MapFileAtAddress(byte* addr, size_t byte_count, int prot, int flags, int fd,
107 off_t start, bool reuse, const char* filename,
108 std::string* error_msg) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700109 CHECK_NE(0, prot);
110 CHECK_NE(0, flags & (MAP_SHARED | MAP_PRIVATE));
Brian Carlstrom9004cb62013-07-26 15:48:31 -0700111 if (byte_count == 0) {
112 return new MemMap("file", NULL, 0, NULL, 0, prot);
113 }
Ian Rogersf8adc602013-04-18 17:06:19 -0700114 // Adjust 'offset' to be page-aligned as required by mmap.
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700115 int page_offset = start % kPageSize;
116 off_t page_aligned_offset = start - page_offset;
Ian Rogersf8adc602013-04-18 17:06:19 -0700117 // Adjust 'byte_count' to be page-aligned as we will map this anyway.
Elliott Hughesecd3a6f2012-06-06 18:16:37 -0700118 size_t page_aligned_byte_count = RoundUp(byte_count + page_offset, kPageSize);
Ian Rogersf8adc602013-04-18 17:06:19 -0700119 // The 'addr' is modified (if specified, ie non-null) to be page aligned to the file but not
120 // necessarily to virtual memory. mmap will page align 'addr' for us.
121 byte* page_aligned_addr = (addr == NULL) ? NULL : (addr - page_offset);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800122 if (!reuse) {
123 // reuse means it is okay that it overlaps an existing page mapping.
124 // Only use this if you actually made the page reservation yourself.
125 CheckMapRequest(page_aligned_addr, page_aligned_byte_count);
Ian Rogersf8adc602013-04-18 17:06:19 -0700126 } else {
127 CHECK(addr != NULL);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800128 }
129 byte* actual = reinterpret_cast<byte*>(mmap(page_aligned_addr,
Elliott Hughesecd3a6f2012-06-06 18:16:37 -0700130 page_aligned_byte_count,
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700131 prot,
132 flags,
133 fd,
134 page_aligned_offset));
135 if (actual == MAP_FAILED) {
Mathieu Chartier3b6f0fa2013-11-13 13:26:07 -0800136 std::string strerr(strerror(errno));
jeffhao8161c032012-10-31 15:50:00 -0700137 std::string maps;
138 ReadFileToString("/proc/self/maps", &maps);
Mathieu Chartier3b6f0fa2013-11-13 13:26:07 -0800139 *error_msg = StringPrintf("mmap(%p, %zd, %x, %x, %d, %lld) of file '%s' failed: %s\n%s",
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700140 page_aligned_addr, page_aligned_byte_count, prot, flags, fd,
Mathieu Chartier3b6f0fa2013-11-13 13:26:07 -0800141 static_cast<int64_t>(page_aligned_offset), filename, strerr.c_str(),
142 maps.c_str());
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700143 return NULL;
144 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700145 return new MemMap("file", actual + page_offset, byte_count, actual, page_aligned_byte_count,
146 prot);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700147}
148
149MemMap::~MemMap() {
Ian Rogers30fab402012-01-23 15:43:46 -0800150 if (base_begin_ == NULL && base_size_ == 0) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700151 return;
152 }
Ian Rogers30fab402012-01-23 15:43:46 -0800153 int result = munmap(base_begin_, base_size_);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700154 if (result == -1) {
155 PLOG(FATAL) << "munmap failed";
156 }
157}
158
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700159MemMap::MemMap(const std::string& name, byte* begin, size_t size, void* base_begin,
160 size_t base_size, int prot)
161 : name_(name), begin_(begin), size_(size), base_begin_(base_begin), base_size_(base_size),
162 prot_(prot) {
Brian Carlstrom9004cb62013-07-26 15:48:31 -0700163 if (size_ == 0) {
164 CHECK(begin_ == NULL);
165 CHECK(base_begin_ == NULL);
166 CHECK_EQ(base_size_, 0U);
167 } else {
168 CHECK(begin_ != NULL);
169 CHECK(base_begin_ != NULL);
170 CHECK_NE(base_size_, 0U);
171 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700172};
173
Hiroshi Yamauchifd7e7f12013-10-22 14:17:48 -0700174MemMap* MemMap::RemapAtEnd(byte* new_end, const char* tail_name, int tail_prot,
175 std::string* error_msg) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700176 DCHECK_GE(new_end, Begin());
177 DCHECK_LE(new_end, End());
Hiroshi Yamauchifd7e7f12013-10-22 14:17:48 -0700178 DCHECK_LE(begin_ + size_, reinterpret_cast<byte*>(base_begin_) + base_size_);
179 DCHECK(IsAligned<kPageSize>(begin_));
180 DCHECK(IsAligned<kPageSize>(base_begin_));
181 DCHECK(IsAligned<kPageSize>(reinterpret_cast<byte*>(base_begin_) + base_size_));
182 DCHECK(IsAligned<kPageSize>(new_end));
183 byte* old_end = begin_ + size_;
184 byte* old_base_end = reinterpret_cast<byte*>(base_begin_) + base_size_;
185 byte* new_base_end = new_end;
186 DCHECK_LE(new_base_end, old_base_end);
187 if (new_base_end == old_base_end) {
188 return new MemMap(tail_name, NULL, 0, NULL, 0, tail_prot);
189 }
190 size_ = new_end - reinterpret_cast<byte*>(begin_);
191 base_size_ = new_base_end - reinterpret_cast<byte*>(base_begin_);
192 DCHECK_LE(begin_ + size_, reinterpret_cast<byte*>(base_begin_) + base_size_);
193 size_t tail_size = old_end - new_end;
194 byte* tail_base_begin = new_base_end;
195 size_t tail_base_size = old_base_end - new_base_end;
196 DCHECK_EQ(tail_base_begin + tail_base_size, old_base_end);
197 DCHECK(IsAligned<kPageSize>(tail_base_size));
198
199#ifdef 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 += tail_name;
204 ScopedFd fd(ashmem_create_region(debug_friendly_name.c_str(), tail_base_size));
205 int flags = MAP_PRIVATE;
206 if (fd.get() == -1) {
207 *error_msg = StringPrintf("ashmem_create_region failed for '%s': %s",
208 tail_name, strerror(errno));
209 return nullptr;
210 }
211#else
212 ScopedFd fd(-1);
213 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
214#endif
215
216 // Unmap/map the tail region.
217 int result = munmap(tail_base_begin, tail_base_size);
218 if (result == -1) {
219 std::string maps;
220 ReadFileToString("/proc/self/maps", &maps);
221 *error_msg = StringPrintf("munmap(%p, %zd) failed for '%s'\n%s",
222 tail_base_begin, tail_base_size, name_.c_str(),
223 maps.c_str());
224 return nullptr;
225 }
226 // Don't cause memory allocation between the munmap and the mmap
227 // calls. Otherwise, libc (or something else) might take this memory
228 // region. Note this isn't perfect as there's no way to prevent
229 // other threads to try to take this memory region here.
230 byte* actual = reinterpret_cast<byte*>(mmap(tail_base_begin, tail_base_size, tail_prot,
231 flags, fd.get(), 0));
232 if (actual == MAP_FAILED) {
233 std::string maps;
234 ReadFileToString("/proc/self/maps", &maps);
235 *error_msg = StringPrintf("anonymous mmap(%p, %zd, %x, %x, %d, 0) failed\n%s",
236 tail_base_begin, tail_base_size, tail_prot, flags, fd.get(),
237 maps.c_str());
238 return nullptr;
239 }
240 return new MemMap(tail_name, actual, tail_size, actual, tail_base_size, tail_prot);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700241}
Logan Chiend88fa262012-06-06 15:23:32 +0800242
243bool MemMap::Protect(int prot) {
244 if (base_begin_ == NULL && base_size_ == 0) {
Ian Rogers1c849e52012-06-28 14:00:33 -0700245 prot_ = prot;
Logan Chiend88fa262012-06-06 15:23:32 +0800246 return true;
247 }
248
249 if (mprotect(base_begin_, base_size_, prot) == 0) {
Ian Rogers1c849e52012-06-28 14:00:33 -0700250 prot_ = prot;
Logan Chiend88fa262012-06-06 15:23:32 +0800251 return true;
252 }
253
Shih-wei Liaoa060ed92012-06-07 09:25:28 -0700254 PLOG(ERROR) << "mprotect(" << reinterpret_cast<void*>(base_begin_) << ", " << base_size_ << ", "
255 << prot << ") failed";
Logan Chiend88fa262012-06-06 15:23:32 +0800256 return false;
257}
258
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700259} // namespace art