blob: d3b823668e421e513a057b4e85877dba62aa6e3d [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
Elliott Hughese222ee02012-12-13 14:41:43 -080022#include "base/stringprintf.h"
23#include "ScopedFd.h"
24#include "utils.h"
25
Elliott Hughes6c9c06d2011-11-07 16:43:47 -080026#define USE_ASHMEM 1
27
28#ifdef USE_ASHMEM
29#include <cutils/ashmem.h>
30#endif
31
Brian Carlstrom27ec9612011-09-19 20:20:38 -070032namespace art {
33
Elliott Hughes96970cd2012-03-13 15:46:31 -070034#if !defined(NDEBUG)
35
Christopher Ferris943af7d2014-01-16 12:41:46 -080036static std::ostream& operator<<(
37 std::ostream& os,
38 std::pair<BacktraceMap::const_iterator, BacktraceMap::const_iterator> iters) {
39 for (BacktraceMap::const_iterator it = iters.first; it != iters.second; ++it) {
40 os << StringPrintf("0x%08x-0x%08x %c%c%c %s\n",
41 static_cast<uint32_t>(it->start),
42 static_cast<uint32_t>(it->end),
43 (it->flags & PROT_READ) ? 'r' : '-',
44 (it->flags & PROT_WRITE) ? 'w' : '-',
45 (it->flags & PROT_EXEC) ? 'x' : '-', it->name.c_str());
Elliott Hughesecd3a6f2012-06-06 18:16:37 -070046 }
47 return os;
Brian Carlstrom27ec9612011-09-19 20:20:38 -070048}
49
Ian Rogers10c5b782013-01-10 10:40:53 -080050static void CheckMapRequest(byte* addr, size_t byte_count) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -070051 if (addr == NULL) {
52 return;
53 }
Elliott Hughesecd3a6f2012-06-06 18:16:37 -070054
Christopher Ferris943af7d2014-01-16 12:41:46 -080055 uintptr_t base = reinterpret_cast<uintptr_t>(addr);
56 uintptr_t limit = base + byte_count;
Brian Carlstrom27ec9612011-09-19 20:20:38 -070057
Christopher Ferris943af7d2014-01-16 12:41:46 -080058 BacktraceMap map(getpid());
59 if (!map.Build()) {
60 PLOG(WARNING) << "Failed to build process map";
61 return;
62 }
63 for (BacktraceMap::const_iterator it = map.begin(); it != map.end(); ++it) {
64 CHECK(!(base >= it->start && base < it->end) // start of new within old
65 && !(limit > it->start && limit < it->end) // end of new within old
66 && !(base <= it->start && limit > it->end)) // start/end of new includes all of old
Ian Rogersdebeb3a2014-01-23 16:54:52 -080067 << StringPrintf("Requested region 0x%08" PRIxPTR "-0x%08" PRIxPTR " overlaps with "
68 "existing map 0x%08" PRIxPTR "-0x%08" PRIxPTR " (%s)\n",
Elliott Hughes7bda1582012-06-07 11:00:54 -070069 base, limit,
Ian Rogersdebeb3a2014-01-23 16:54:52 -080070 static_cast<uintptr_t>(it->start), static_cast<uintptr_t>(it->end),
Christopher Ferris943af7d2014-01-16 12:41:46 -080071 it->name.c_str())
72 << std::make_pair(it, map.end());
Elliott Hughes96970cd2012-03-13 15:46:31 -070073 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -070074}
75
Elliott Hughes96970cd2012-03-13 15:46:31 -070076#else
Elliott Hughes1bac54f2012-03-16 12:48:31 -070077static void CheckMapRequest(byte*, size_t) { }
Elliott Hughes96970cd2012-03-13 15:46:31 -070078#endif
79
Ian Rogers8d31bbd2013-10-13 10:44:14 -070080MemMap* MemMap::MapAnonymous(const char* name, byte* addr, size_t byte_count, int prot,
81 std::string* error_msg) {
Brian Carlstrom9004cb62013-07-26 15:48:31 -070082 if (byte_count == 0) {
83 return new MemMap(name, NULL, 0, NULL, 0, prot);
84 }
Elliott Hughesecd3a6f2012-06-06 18:16:37 -070085 size_t page_aligned_byte_count = RoundUp(byte_count, kPageSize);
86 CheckMapRequest(addr, page_aligned_byte_count);
Elliott Hughes6c9c06d2011-11-07 16:43:47 -080087
88#ifdef USE_ASHMEM
Ian Rogersa40307e2013-02-22 11:32:44 -080089 // android_os_Debug.cpp read_mapinfo assumes all ashmem regions associated with the VM are
90 // prefixed "dalvik-".
91 std::string debug_friendly_name("dalvik-");
92 debug_friendly_name += name;
93 ScopedFd fd(ashmem_create_region(debug_friendly_name.c_str(), page_aligned_byte_count));
Elliott Hughes6c9c06d2011-11-07 16:43:47 -080094 int flags = MAP_PRIVATE;
95 if (fd.get() == -1) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070096 *error_msg = StringPrintf("ashmem_create_region failed for '%s': %s", name, strerror(errno));
97 return nullptr;
Elliott Hughes6c9c06d2011-11-07 16:43:47 -080098 }
99#else
100 ScopedFd fd(-1);
101 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
102#endif
103
Elliott Hughesecd3a6f2012-06-06 18:16:37 -0700104 byte* actual = reinterpret_cast<byte*>(mmap(addr, page_aligned_byte_count, prot, flags, fd.get(), 0));
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700105 if (actual == MAP_FAILED) {
jeffhao8161c032012-10-31 15:50:00 -0700106 std::string maps;
107 ReadFileToString("/proc/self/maps", &maps);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700108 *error_msg = StringPrintf("anonymous mmap(%p, %zd, %x, %x, %d, 0) failed\n%s",
109 addr, page_aligned_byte_count, prot, flags, fd.get(),
110 maps.c_str());
111 return nullptr;
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700112 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700113 return new MemMap(name, actual, byte_count, actual, page_aligned_byte_count, prot);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700114}
115
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700116MemMap* MemMap::MapFileAtAddress(byte* addr, size_t byte_count, int prot, int flags, int fd,
117 off_t start, bool reuse, const char* filename,
118 std::string* error_msg) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700119 CHECK_NE(0, prot);
120 CHECK_NE(0, flags & (MAP_SHARED | MAP_PRIVATE));
Brian Carlstrom9004cb62013-07-26 15:48:31 -0700121 if (byte_count == 0) {
122 return new MemMap("file", NULL, 0, NULL, 0, prot);
123 }
Ian Rogersf8adc602013-04-18 17:06:19 -0700124 // Adjust 'offset' to be page-aligned as required by mmap.
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700125 int page_offset = start % kPageSize;
126 off_t page_aligned_offset = start - page_offset;
Ian Rogersf8adc602013-04-18 17:06:19 -0700127 // Adjust 'byte_count' to be page-aligned as we will map this anyway.
Elliott Hughesecd3a6f2012-06-06 18:16:37 -0700128 size_t page_aligned_byte_count = RoundUp(byte_count + page_offset, kPageSize);
Ian Rogersf8adc602013-04-18 17:06:19 -0700129 // The 'addr' is modified (if specified, ie non-null) to be page aligned to the file but not
130 // necessarily to virtual memory. mmap will page align 'addr' for us.
131 byte* page_aligned_addr = (addr == NULL) ? NULL : (addr - page_offset);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800132 if (!reuse) {
133 // reuse means it is okay that it overlaps an existing page mapping.
134 // Only use this if you actually made the page reservation yourself.
135 CheckMapRequest(page_aligned_addr, page_aligned_byte_count);
Ian Rogersf8adc602013-04-18 17:06:19 -0700136 } else {
137 CHECK(addr != NULL);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800138 }
139 byte* actual = reinterpret_cast<byte*>(mmap(page_aligned_addr,
Elliott Hughesecd3a6f2012-06-06 18:16:37 -0700140 page_aligned_byte_count,
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700141 prot,
142 flags,
143 fd,
144 page_aligned_offset));
145 if (actual == MAP_FAILED) {
Mathieu Chartier3b6f0fa2013-11-13 13:26:07 -0800146 std::string strerr(strerror(errno));
jeffhao8161c032012-10-31 15:50:00 -0700147 std::string maps;
148 ReadFileToString("/proc/self/maps", &maps);
Ian Rogersdebeb3a2014-01-23 16:54:52 -0800149 *error_msg = StringPrintf("mmap(%p, %zd, %x, %x, %d, %" PRId64 ") of file '%s' failed: %s\n%s",
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700150 page_aligned_addr, page_aligned_byte_count, prot, flags, fd,
Mathieu Chartier3b6f0fa2013-11-13 13:26:07 -0800151 static_cast<int64_t>(page_aligned_offset), filename, strerr.c_str(),
152 maps.c_str());
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700153 return NULL;
154 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700155 return new MemMap("file", actual + page_offset, byte_count, actual, page_aligned_byte_count,
156 prot);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700157}
158
159MemMap::~MemMap() {
Ian Rogers30fab402012-01-23 15:43:46 -0800160 if (base_begin_ == NULL && base_size_ == 0) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700161 return;
162 }
Ian Rogers30fab402012-01-23 15:43:46 -0800163 int result = munmap(base_begin_, base_size_);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700164 if (result == -1) {
165 PLOG(FATAL) << "munmap failed";
166 }
167}
168
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700169MemMap::MemMap(const std::string& name, byte* begin, size_t size, void* base_begin,
170 size_t base_size, int prot)
171 : name_(name), begin_(begin), size_(size), base_begin_(base_begin), base_size_(base_size),
172 prot_(prot) {
Brian Carlstrom9004cb62013-07-26 15:48:31 -0700173 if (size_ == 0) {
174 CHECK(begin_ == NULL);
175 CHECK(base_begin_ == NULL);
176 CHECK_EQ(base_size_, 0U);
177 } else {
178 CHECK(begin_ != NULL);
179 CHECK(base_begin_ != NULL);
180 CHECK_NE(base_size_, 0U);
181 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700182};
183
Hiroshi Yamauchifd7e7f12013-10-22 14:17:48 -0700184MemMap* MemMap::RemapAtEnd(byte* new_end, const char* tail_name, int tail_prot,
185 std::string* error_msg) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700186 DCHECK_GE(new_end, Begin());
187 DCHECK_LE(new_end, End());
Hiroshi Yamauchifd7e7f12013-10-22 14:17:48 -0700188 DCHECK_LE(begin_ + size_, reinterpret_cast<byte*>(base_begin_) + base_size_);
189 DCHECK(IsAligned<kPageSize>(begin_));
190 DCHECK(IsAligned<kPageSize>(base_begin_));
191 DCHECK(IsAligned<kPageSize>(reinterpret_cast<byte*>(base_begin_) + base_size_));
192 DCHECK(IsAligned<kPageSize>(new_end));
193 byte* old_end = begin_ + size_;
194 byte* old_base_end = reinterpret_cast<byte*>(base_begin_) + base_size_;
195 byte* new_base_end = new_end;
196 DCHECK_LE(new_base_end, old_base_end);
197 if (new_base_end == old_base_end) {
198 return new MemMap(tail_name, NULL, 0, NULL, 0, tail_prot);
199 }
200 size_ = new_end - reinterpret_cast<byte*>(begin_);
201 base_size_ = new_base_end - reinterpret_cast<byte*>(base_begin_);
202 DCHECK_LE(begin_ + size_, reinterpret_cast<byte*>(base_begin_) + base_size_);
203 size_t tail_size = old_end - new_end;
204 byte* tail_base_begin = new_base_end;
205 size_t tail_base_size = old_base_end - new_base_end;
206 DCHECK_EQ(tail_base_begin + tail_base_size, old_base_end);
207 DCHECK(IsAligned<kPageSize>(tail_base_size));
208
209#ifdef USE_ASHMEM
210 // android_os_Debug.cpp read_mapinfo assumes all ashmem regions associated with the VM are
211 // prefixed "dalvik-".
212 std::string debug_friendly_name("dalvik-");
213 debug_friendly_name += tail_name;
214 ScopedFd fd(ashmem_create_region(debug_friendly_name.c_str(), tail_base_size));
215 int flags = MAP_PRIVATE;
216 if (fd.get() == -1) {
217 *error_msg = StringPrintf("ashmem_create_region failed for '%s': %s",
218 tail_name, strerror(errno));
219 return nullptr;
220 }
221#else
222 ScopedFd fd(-1);
223 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
224#endif
225
226 // Unmap/map the tail region.
227 int result = munmap(tail_base_begin, tail_base_size);
228 if (result == -1) {
229 std::string maps;
230 ReadFileToString("/proc/self/maps", &maps);
231 *error_msg = StringPrintf("munmap(%p, %zd) failed for '%s'\n%s",
232 tail_base_begin, tail_base_size, name_.c_str(),
233 maps.c_str());
234 return nullptr;
235 }
236 // Don't cause memory allocation between the munmap and the mmap
237 // calls. Otherwise, libc (or something else) might take this memory
238 // region. Note this isn't perfect as there's no way to prevent
239 // other threads to try to take this memory region here.
240 byte* actual = reinterpret_cast<byte*>(mmap(tail_base_begin, tail_base_size, tail_prot,
241 flags, fd.get(), 0));
242 if (actual == MAP_FAILED) {
243 std::string maps;
244 ReadFileToString("/proc/self/maps", &maps);
245 *error_msg = StringPrintf("anonymous mmap(%p, %zd, %x, %x, %d, 0) failed\n%s",
246 tail_base_begin, tail_base_size, tail_prot, flags, fd.get(),
247 maps.c_str());
248 return nullptr;
249 }
250 return new MemMap(tail_name, actual, tail_size, actual, tail_base_size, tail_prot);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700251}
Logan Chiend88fa262012-06-06 15:23:32 +0800252
253bool MemMap::Protect(int prot) {
254 if (base_begin_ == NULL && base_size_ == 0) {
Ian Rogers1c849e52012-06-28 14:00:33 -0700255 prot_ = prot;
Logan Chiend88fa262012-06-06 15:23:32 +0800256 return true;
257 }
258
259 if (mprotect(base_begin_, base_size_, prot) == 0) {
Ian Rogers1c849e52012-06-28 14:00:33 -0700260 prot_ = prot;
Logan Chiend88fa262012-06-06 15:23:32 +0800261 return true;
262 }
263
Shih-wei Liaoa060ed92012-06-07 09:25:28 -0700264 PLOG(ERROR) << "mprotect(" << reinterpret_cast<void*>(base_begin_) << ", " << base_size_ << ", "
265 << prot << ") failed";
Logan Chiend88fa262012-06-06 15:23:32 +0800266 return false;
267}
268
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700269} // namespace art