blob: 393ea686f4b77de1f8a10f13d8fba302d0254dd3 [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
Ian Rogersdebeb3a2014-01-23 16:54:52 -080019#include <inttypes.h>
Christopher Ferris943af7d2014-01-16 12:41:46 -080020#include <backtrace/BacktraceMap.h>
Elliott Hughesecd3a6f2012-06-06 18:16:37 -070021
Christopher Ferriscaf22ac2014-01-27 18:32:14 -080022#include "UniquePtr.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080023#include "base/stringprintf.h"
24#include "ScopedFd.h"
25#include "utils.h"
26
Elliott Hughes6c9c06d2011-11-07 16:43:47 -080027#define USE_ASHMEM 1
28
29#ifdef USE_ASHMEM
30#include <cutils/ashmem.h>
31#endif
32
Brian Carlstrom27ec9612011-09-19 20:20:38 -070033namespace art {
34
Elliott Hughes96970cd2012-03-13 15:46:31 -070035#if !defined(NDEBUG)
36
Christopher Ferris943af7d2014-01-16 12:41:46 -080037static std::ostream& operator<<(
38 std::ostream& os,
39 std::pair<BacktraceMap::const_iterator, BacktraceMap::const_iterator> iters) {
40 for (BacktraceMap::const_iterator it = iters.first; it != iters.second; ++it) {
41 os << StringPrintf("0x%08x-0x%08x %c%c%c %s\n",
42 static_cast<uint32_t>(it->start),
43 static_cast<uint32_t>(it->end),
44 (it->flags & PROT_READ) ? 'r' : '-',
45 (it->flags & PROT_WRITE) ? 'w' : '-',
46 (it->flags & PROT_EXEC) ? 'x' : '-', it->name.c_str());
Elliott Hughesecd3a6f2012-06-06 18:16:37 -070047 }
48 return os;
Brian Carlstrom27ec9612011-09-19 20:20:38 -070049}
50
Ian Rogers10c5b782013-01-10 10:40:53 -080051static void CheckMapRequest(byte* addr, size_t byte_count) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -070052 if (addr == NULL) {
53 return;
54 }
Elliott Hughesecd3a6f2012-06-06 18:16:37 -070055
Christopher Ferris943af7d2014-01-16 12:41:46 -080056 uintptr_t base = reinterpret_cast<uintptr_t>(addr);
57 uintptr_t limit = base + byte_count;
Brian Carlstrom27ec9612011-09-19 20:20:38 -070058
Christopher Ferriscaf22ac2014-01-27 18:32:14 -080059 UniquePtr<BacktraceMap> map(BacktraceMap::Create(getpid()));
60 if (!map->Build()) {
Christopher Ferris943af7d2014-01-16 12:41:46 -080061 PLOG(WARNING) << "Failed to build process map";
62 return;
63 }
Christopher Ferriscaf22ac2014-01-27 18:32:14 -080064 for (BacktraceMap::const_iterator it = map->begin(); it != map->end(); ++it) {
Christopher Ferris943af7d2014-01-16 12:41:46 -080065 CHECK(!(base >= it->start && base < it->end) // start of new within old
66 && !(limit > it->start && limit < it->end) // end of new within old
67 && !(base <= it->start && limit > it->end)) // start/end of new includes all of old
Ian Rogersdebeb3a2014-01-23 16:54:52 -080068 << StringPrintf("Requested region 0x%08" PRIxPTR "-0x%08" PRIxPTR " overlaps with "
69 "existing map 0x%08" PRIxPTR "-0x%08" PRIxPTR " (%s)\n",
Elliott Hughes7bda1582012-06-07 11:00:54 -070070 base, limit,
Ian Rogersdebeb3a2014-01-23 16:54:52 -080071 static_cast<uintptr_t>(it->start), static_cast<uintptr_t>(it->end),
Christopher Ferris943af7d2014-01-16 12:41:46 -080072 it->name.c_str())
Christopher Ferriscaf22ac2014-01-27 18:32:14 -080073 << std::make_pair(it, map->end());
Elliott Hughes96970cd2012-03-13 15:46:31 -070074 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -070075}
76
Elliott Hughes96970cd2012-03-13 15:46:31 -070077#else
Elliott Hughes1bac54f2012-03-16 12:48:31 -070078static void CheckMapRequest(byte*, size_t) { }
Elliott Hughes96970cd2012-03-13 15:46:31 -070079#endif
80
Ian Rogers8d31bbd2013-10-13 10:44:14 -070081MemMap* MemMap::MapAnonymous(const char* name, byte* addr, size_t byte_count, int prot,
Ian Rogersef7d42f2014-01-06 12:55:46 -080082 bool low_4gb, std::string* error_msg) {
Brian Carlstrom9004cb62013-07-26 15:48:31 -070083 if (byte_count == 0) {
84 return new MemMap(name, NULL, 0, NULL, 0, prot);
85 }
Elliott Hughesecd3a6f2012-06-06 18:16:37 -070086 size_t page_aligned_byte_count = RoundUp(byte_count, kPageSize);
87 CheckMapRequest(addr, page_aligned_byte_count);
Elliott Hughes6c9c06d2011-11-07 16:43:47 -080088
89#ifdef USE_ASHMEM
Ian Rogersa40307e2013-02-22 11:32:44 -080090 // android_os_Debug.cpp read_mapinfo assumes all ashmem regions associated with the VM are
91 // prefixed "dalvik-".
92 std::string debug_friendly_name("dalvik-");
93 debug_friendly_name += name;
94 ScopedFd fd(ashmem_create_region(debug_friendly_name.c_str(), page_aligned_byte_count));
Elliott Hughes6c9c06d2011-11-07 16:43:47 -080095 int flags = MAP_PRIVATE;
96 if (fd.get() == -1) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070097 *error_msg = StringPrintf("ashmem_create_region failed for '%s': %s", name, strerror(errno));
98 return nullptr;
Elliott Hughes6c9c06d2011-11-07 16:43:47 -080099 }
100#else
101 ScopedFd fd(-1);
102 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
103#endif
Ian Rogersef7d42f2014-01-06 12:55:46 -0800104#ifdef __LP64__
105 if (low_4gb) {
106 flags |= MAP_32BIT;
107 }
108#endif
Elliott Hughesecd3a6f2012-06-06 18:16:37 -0700109 byte* actual = reinterpret_cast<byte*>(mmap(addr, page_aligned_byte_count, prot, flags, fd.get(), 0));
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700110 if (actual == MAP_FAILED) {
jeffhao8161c032012-10-31 15:50:00 -0700111 std::string maps;
112 ReadFileToString("/proc/self/maps", &maps);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700113 *error_msg = StringPrintf("anonymous mmap(%p, %zd, %x, %x, %d, 0) failed\n%s",
114 addr, page_aligned_byte_count, prot, flags, fd.get(),
115 maps.c_str());
116 return nullptr;
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700117 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700118 return new MemMap(name, actual, byte_count, actual, page_aligned_byte_count, prot);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700119}
120
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700121MemMap* MemMap::MapFileAtAddress(byte* addr, size_t byte_count, int prot, int flags, int fd,
122 off_t start, bool reuse, const char* filename,
123 std::string* error_msg) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700124 CHECK_NE(0, prot);
125 CHECK_NE(0, flags & (MAP_SHARED | MAP_PRIVATE));
Brian Carlstrom9004cb62013-07-26 15:48:31 -0700126 if (byte_count == 0) {
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800127 return new MemMap(filename, NULL, 0, NULL, 0, prot);
Brian Carlstrom9004cb62013-07-26 15:48:31 -0700128 }
Ian Rogersf8adc602013-04-18 17:06:19 -0700129 // Adjust 'offset' to be page-aligned as required by mmap.
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700130 int page_offset = start % kPageSize;
131 off_t page_aligned_offset = start - page_offset;
Ian Rogersf8adc602013-04-18 17:06:19 -0700132 // Adjust 'byte_count' to be page-aligned as we will map this anyway.
Elliott Hughesecd3a6f2012-06-06 18:16:37 -0700133 size_t page_aligned_byte_count = RoundUp(byte_count + page_offset, kPageSize);
Ian Rogersf8adc602013-04-18 17:06:19 -0700134 // The 'addr' is modified (if specified, ie non-null) to be page aligned to the file but not
135 // necessarily to virtual memory. mmap will page align 'addr' for us.
136 byte* page_aligned_addr = (addr == NULL) ? NULL : (addr - page_offset);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800137 if (!reuse) {
138 // reuse means it is okay that it overlaps an existing page mapping.
139 // Only use this if you actually made the page reservation yourself.
140 CheckMapRequest(page_aligned_addr, page_aligned_byte_count);
Ian Rogersf8adc602013-04-18 17:06:19 -0700141 } else {
142 CHECK(addr != NULL);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800143 }
144 byte* actual = reinterpret_cast<byte*>(mmap(page_aligned_addr,
Elliott Hughesecd3a6f2012-06-06 18:16:37 -0700145 page_aligned_byte_count,
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700146 prot,
147 flags,
148 fd,
149 page_aligned_offset));
150 if (actual == MAP_FAILED) {
Mathieu Chartier3b6f0fa2013-11-13 13:26:07 -0800151 std::string strerr(strerror(errno));
jeffhao8161c032012-10-31 15:50:00 -0700152 std::string maps;
153 ReadFileToString("/proc/self/maps", &maps);
Ian Rogersdebeb3a2014-01-23 16:54:52 -0800154 *error_msg = StringPrintf("mmap(%p, %zd, %x, %x, %d, %" PRId64 ") of file '%s' failed: %s\n%s",
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700155 page_aligned_addr, page_aligned_byte_count, prot, flags, fd,
Mathieu Chartier3b6f0fa2013-11-13 13:26:07 -0800156 static_cast<int64_t>(page_aligned_offset), filename, strerr.c_str(),
157 maps.c_str());
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700158 return NULL;
159 }
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800160 return new MemMap(filename, actual + page_offset, byte_count, actual, page_aligned_byte_count,
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700161 prot);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700162}
163
164MemMap::~MemMap() {
Ian Rogers30fab402012-01-23 15:43:46 -0800165 if (base_begin_ == NULL && base_size_ == 0) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700166 return;
167 }
Ian Rogers30fab402012-01-23 15:43:46 -0800168 int result = munmap(base_begin_, base_size_);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700169 if (result == -1) {
170 PLOG(FATAL) << "munmap failed";
171 }
172}
173
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700174MemMap::MemMap(const std::string& name, byte* begin, size_t size, void* base_begin,
175 size_t base_size, int prot)
176 : name_(name), begin_(begin), size_(size), base_begin_(base_begin), base_size_(base_size),
177 prot_(prot) {
Brian Carlstrom9004cb62013-07-26 15:48:31 -0700178 if (size_ == 0) {
179 CHECK(begin_ == NULL);
180 CHECK(base_begin_ == NULL);
181 CHECK_EQ(base_size_, 0U);
182 } else {
183 CHECK(begin_ != NULL);
184 CHECK(base_begin_ != NULL);
185 CHECK_NE(base_size_, 0U);
186 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700187};
188
Hiroshi Yamauchifd7e7f12013-10-22 14:17:48 -0700189MemMap* MemMap::RemapAtEnd(byte* new_end, const char* tail_name, int tail_prot,
190 std::string* error_msg) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700191 DCHECK_GE(new_end, Begin());
192 DCHECK_LE(new_end, End());
Hiroshi Yamauchifd7e7f12013-10-22 14:17:48 -0700193 DCHECK_LE(begin_ + size_, reinterpret_cast<byte*>(base_begin_) + base_size_);
194 DCHECK(IsAligned<kPageSize>(begin_));
195 DCHECK(IsAligned<kPageSize>(base_begin_));
196 DCHECK(IsAligned<kPageSize>(reinterpret_cast<byte*>(base_begin_) + base_size_));
197 DCHECK(IsAligned<kPageSize>(new_end));
198 byte* old_end = begin_ + size_;
199 byte* old_base_end = reinterpret_cast<byte*>(base_begin_) + base_size_;
200 byte* new_base_end = new_end;
201 DCHECK_LE(new_base_end, old_base_end);
202 if (new_base_end == old_base_end) {
203 return new MemMap(tail_name, NULL, 0, NULL, 0, tail_prot);
204 }
205 size_ = new_end - reinterpret_cast<byte*>(begin_);
206 base_size_ = new_base_end - reinterpret_cast<byte*>(base_begin_);
207 DCHECK_LE(begin_ + size_, reinterpret_cast<byte*>(base_begin_) + base_size_);
208 size_t tail_size = old_end - new_end;
209 byte* tail_base_begin = new_base_end;
210 size_t tail_base_size = old_base_end - new_base_end;
211 DCHECK_EQ(tail_base_begin + tail_base_size, old_base_end);
212 DCHECK(IsAligned<kPageSize>(tail_base_size));
213
214#ifdef USE_ASHMEM
215 // android_os_Debug.cpp read_mapinfo assumes all ashmem regions associated with the VM are
216 // prefixed "dalvik-".
217 std::string debug_friendly_name("dalvik-");
218 debug_friendly_name += tail_name;
219 ScopedFd fd(ashmem_create_region(debug_friendly_name.c_str(), tail_base_size));
220 int flags = MAP_PRIVATE;
221 if (fd.get() == -1) {
222 *error_msg = StringPrintf("ashmem_create_region failed for '%s': %s",
223 tail_name, strerror(errno));
224 return nullptr;
225 }
226#else
227 ScopedFd fd(-1);
228 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
229#endif
230
231 // Unmap/map the tail region.
232 int result = munmap(tail_base_begin, tail_base_size);
233 if (result == -1) {
234 std::string maps;
235 ReadFileToString("/proc/self/maps", &maps);
236 *error_msg = StringPrintf("munmap(%p, %zd) failed for '%s'\n%s",
237 tail_base_begin, tail_base_size, name_.c_str(),
238 maps.c_str());
239 return nullptr;
240 }
241 // Don't cause memory allocation between the munmap and the mmap
242 // calls. Otherwise, libc (or something else) might take this memory
243 // region. Note this isn't perfect as there's no way to prevent
244 // other threads to try to take this memory region here.
245 byte* actual = reinterpret_cast<byte*>(mmap(tail_base_begin, tail_base_size, tail_prot,
246 flags, fd.get(), 0));
247 if (actual == MAP_FAILED) {
248 std::string maps;
249 ReadFileToString("/proc/self/maps", &maps);
250 *error_msg = StringPrintf("anonymous mmap(%p, %zd, %x, %x, %d, 0) failed\n%s",
251 tail_base_begin, tail_base_size, tail_prot, flags, fd.get(),
252 maps.c_str());
253 return nullptr;
254 }
255 return new MemMap(tail_name, actual, tail_size, actual, tail_base_size, tail_prot);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700256}
Logan Chiend88fa262012-06-06 15:23:32 +0800257
258bool MemMap::Protect(int prot) {
259 if (base_begin_ == NULL && base_size_ == 0) {
Ian Rogers1c849e52012-06-28 14:00:33 -0700260 prot_ = prot;
Logan Chiend88fa262012-06-06 15:23:32 +0800261 return true;
262 }
263
264 if (mprotect(base_begin_, base_size_, prot) == 0) {
Ian Rogers1c849e52012-06-28 14:00:33 -0700265 prot_ = prot;
Logan Chiend88fa262012-06-06 15:23:32 +0800266 return true;
267 }
268
Shih-wei Liaoa060ed92012-06-07 09:25:28 -0700269 PLOG(ERROR) << "mprotect(" << reinterpret_cast<void*>(base_begin_) << ", " << base_size_ << ", "
270 << prot << ") failed";
Logan Chiend88fa262012-06-06 15:23:32 +0800271 return false;
272}
273
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800274std::ostream& operator<<(std::ostream& os, const MemMap& mem_map) {
275 os << StringPrintf("[MemMap: %s prot=%x %p-%p]",
276 mem_map.GetName().c_str(), mem_map.GetProtect(),
277 mem_map.BaseBegin(), mem_map.BaseEnd());
278 return os;
279}
280
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700281} // namespace art