blob: 62096b1097f701ddd9b5e19bca057fa1b9f378ff [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
19#include <sys/mman.h>
20
Elliott Hughes6c9c06d2011-11-07 16:43:47 -080021#include "ScopedFd.h"
22#include "utils.h"
23
24#define USE_ASHMEM 1
25
26#ifdef USE_ASHMEM
27#include <cutils/ashmem.h>
28#endif
29
Brian Carlstrom27ec9612011-09-19 20:20:38 -070030namespace art {
31
Elliott Hughes96970cd2012-03-13 15:46:31 -070032#if !defined(NDEBUG)
33
34static size_t ParseHex(const std::string& string) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -070035 CHECK_EQ(8U, string.size());
36 const char* str = string.c_str();
37 char* end;
38 size_t value = strtoul(str, &end, 16);
39 CHECK(end != str) << "Failed to parse hexadecimal value from " << string;
Elliott Hughescc607472011-10-17 15:34:11 -070040 CHECK_EQ(*end, '\0') << "Failed to parse hexadecimal value from " << string;
Brian Carlstrom27ec9612011-09-19 20:20:38 -070041 return value;
42}
43
Elliott Hughes96970cd2012-03-13 15:46:31 -070044static void CheckMapRegion(uint32_t base, uint32_t limit, uint32_t start, uint32_t end, const std::string& maps) {
45 CHECK(!(base >= start && base < end) // start of new within old
46 && !(limit > start && limit < end) // end of new within old
47 && !(base <= start && limit > end)) // start/end of new includes all of old
48 << StringPrintf("Requested region %08x-%08x overlaps with existing map %08x-%08x\n",
49 base, limit, start, end)
50 << maps;
51}
52
Brian Carlstrom27ec9612011-09-19 20:20:38 -070053void CheckMapRequest(byte* addr, size_t length) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -070054 if (addr == NULL) {
55 return;
56 }
Elliott Hughes96970cd2012-03-13 15:46:31 -070057 uint32_t base = reinterpret_cast<size_t>(addr);
58 uint32_t limit = base + length;
Brian Carlstrom27ec9612011-09-19 20:20:38 -070059
Elliott Hughes96970cd2012-03-13 15:46:31 -070060#if defined(__APPLE__)
61 // Mac OS vmmap(1) output currently looks something like this:
62
63 // Virtual Memory Map of process 51036 (dex2oatd)
64 // Output report format: 2.2 -- 32-bit process
65 //
66 // ==== regions for process 51036 (non-writable and writable regions are interleaved)
67 // __PAGEZERO 00000000-00001000 [ 4K 0K 0K] ---/--- SM=NUL out/host/darwin-x86/bin/dex2oatd
68 // __TEXT 00001000-00015000 [ 80K 80K 0K] r-x/rwx SM=COW out/host/darwin-x86/bin/dex2oatd
69 // __DATA 00015000-00016000 [ 4K 4K 4K] rw-/rwx SM=PRV out/host/darwin-x86/bin/dex2oatd
70 // __LINKEDIT 00016000-00044000 [ 184K 184K 0K] r--/rwx SM=COW out/host/darwin-x86/bin/dex2oatd
71 // __TEXT 00044000-00046000 [ 8K 8K 4K] r-x/rwx SM=COW out/host/darwin-x86/obj/lib/libnativehelper.dylib
72 // __DATA 00046000-00047000 [ 4K 4K 4K] rw-/rwx SM=ZER out/host/darwin-x86/obj/lib/libnativehelper.dylib
73 // __LINKEDIT 00047000-0004a000 [ 12K 12K 0K] r--/rwx SM=COW out/host/darwin-x86/obj/lib/libnativehelper.dylib
74 // ...
75
76 std::string command(StringPrintf("vmmap -v -interleaved %d", getpid()));
77 FILE* fp = popen(command.c_str(), "r");
78 if (fp == NULL) {
79 PLOG(FATAL) << "popen failed";
80 }
81 std::vector<char> chars(512);
82 std::string maps;
83 while (fgets(&chars[0], chars.size(), fp) != NULL) {
84 std::string line(&chars[0]);
85 maps += line;
86 if (line.size() < 40 || line[31] != '-') {
87 continue;
88 }
89
Elliott Hughes448e93c2012-03-28 22:30:06 -070090 std::string start_str(line.substr(23, 8));
91 std::string end_str(line.substr(32, 8));
Elliott Hughes96970cd2012-03-13 15:46:31 -070092 uint32_t start = ParseHex(start_str);
93 uint32_t end = ParseHex(end_str);
94 CheckMapRegion(base, limit, start, end, maps);
95 }
96 if (ferror(fp)) {
97 PLOG(FATAL) << "fgets failed";
98 }
99 if (pclose(fp) == -1) {
100 PLOG(FATAL) << "pclose failed";
101 }
102#else // Linux
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700103 std::string maps;
104 bool read = ReadFileToString("/proc/self/maps", &maps);
105 if (!read) {
106 PLOG(FATAL) << "Failed to read /proc/self/maps";
107 }
108 // Quick and dirty parse of output like shown below. We only focus
109 // on grabbing the two 32-bit hex values at the start of each line
110 // and will fail on wider addresses found on 64-bit systems.
111
112 // 00008000-0001f000 r-xp 00000000 b3:01 273 /system/bin/toolbox
113 // 0001f000-00021000 rw-p 00017000 b3:01 273 /system/bin/toolbox
114 // 00021000-00029000 rw-p 00000000 00:00 0 [heap]
115 // 40011000-40053000 r-xp 00000000 b3:01 1050 /system/lib/libc.so
116 // 40053000-40056000 rw-p 00042000 b3:01 1050 /system/lib/libc.so
117 // 40056000-40061000 rw-p 00000000 00:00 0
118 // 40061000-40063000 r-xp 00000000 b3:01 1107 /system/lib/libusbhost.so
119 // 40063000-40064000 rw-p 00002000 b3:01 1107 /system/lib/libusbhost.so
120 // 4009d000-400a0000 r-xp 00000000 b3:01 1022 /system/lib/liblog.so
121 // 400a0000-400a1000 rw-p 00003000 b3:01 1022 /system/lib/liblog.so
122 // 400b7000-400cc000 r-xp 00000000 b3:01 932 /system/lib/libm.so
123 // 400cc000-400cd000 rw-p 00015000 b3:01 932 /system/lib/libm.so
124 // 400cf000-400d0000 r--p 00000000 00:00 0
125 // 400e4000-400ec000 r--s 00000000 00:0b 388 /dev/__properties__ (deleted)
126 // 400ec000-400fa000 r-xp 00000000 b3:01 1101 /system/lib/libcutils.so
127 // 400fa000-400fb000 rw-p 0000e000 b3:01 1101 /system/lib/libcutils.so
128 // 400fb000-4010a000 rw-p 00000000 00:00 0
129 // 4010d000-4010e000 r-xp 00000000 b3:01 929 /system/lib/libstdc++.so
130 // 4010e000-4010f000 rw-p 00001000 b3:01 929 /system/lib/libstdc++.so
131 // b0001000-b0009000 r-xp 00001000 b3:01 1098 /system/bin/linker
132 // b0009000-b000a000 rw-p 00009000 b3:01 1098 /system/bin/linker
133 // b000a000-b0015000 rw-p 00000000 00:00 0
134 // bee35000-bee56000 rw-p 00000000 00:00 0 [stack]
135 // ffff0000-ffff1000 r-xp 00000000 00:00 0 [vectors]
136
137 for (size_t i = 0; i < maps.size(); i++) {
138 size_t remaining = maps.size() - i;
139 if (remaining < 8+1+8) { // 00008000-0001f000
140 LOG(FATAL) << "Failed to parse at pos " << i << "\n" << maps;
141 }
Elliott Hughes95572412011-12-13 18:14:20 -0800142 std::string start_str(maps.substr(i, 8));
143 std::string end_str(maps.substr(i+1+8, 8));
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700144 uint32_t start = ParseHex(start_str);
145 uint32_t end = ParseHex(end_str);
Elliott Hughes96970cd2012-03-13 15:46:31 -0700146 CheckMapRegion(base, limit, start, end, maps);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700147 i += 8+1+8;
148 i = maps.find('\n', i);
149 CHECK(i != std::string::npos) << "Failed to find newline from pos " << i << "\n" << maps;
150 }
151#endif
152}
153
Elliott Hughes96970cd2012-03-13 15:46:31 -0700154#else
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700155static void CheckMapRequest(byte*, size_t) { }
Elliott Hughes96970cd2012-03-13 15:46:31 -0700156#endif
157
Brian Carlstrom89521892011-12-07 22:05:07 -0800158MemMap* MemMap::MapAnonymous(const char* name, byte* addr, size_t length, int prot) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700159 CHECK_NE(0U, length);
160 CHECK_NE(0, prot);
161 size_t page_aligned_size = RoundUp(length, kPageSize);
162 CheckMapRequest(addr, page_aligned_size);
Elliott Hughes6c9c06d2011-11-07 16:43:47 -0800163
164#ifdef USE_ASHMEM
165 ScopedFd fd(ashmem_create_region(name, page_aligned_size));
166 int flags = MAP_PRIVATE;
167 if (fd.get() == -1) {
168 PLOG(ERROR) << "ashmem_create_region failed (" << name << ")";
169 return NULL;
170 }
171#else
172 ScopedFd fd(-1);
173 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
174#endif
175
176 byte* actual = reinterpret_cast<byte*>(mmap(addr, page_aligned_size, prot, flags, fd.get(), 0));
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700177 if (actual == MAP_FAILED) {
Elliott Hughes6c9c06d2011-11-07 16:43:47 -0800178 PLOG(ERROR) << "mmap failed (" << name << ")";
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700179 return NULL;
180 }
181 return new MemMap(actual, length, actual, page_aligned_size);
182}
183
Brian Carlstrom89521892011-12-07 22:05:07 -0800184MemMap* MemMap::MapFileAtAddress(byte* addr, size_t length, int prot, int flags, int fd, off_t start) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700185 CHECK_NE(0U, length);
186 CHECK_NE(0, prot);
187 CHECK_NE(0, flags & (MAP_SHARED | MAP_PRIVATE));
188 // adjust to be page-aligned
189 int page_offset = start % kPageSize;
190 off_t page_aligned_offset = start - page_offset;
191 size_t page_aligned_size = RoundUp(length + page_offset, kPageSize);
192 CheckMapRequest(addr, page_aligned_size);
193 byte* actual = reinterpret_cast<byte*>(mmap(addr,
194 page_aligned_size,
195 prot,
196 flags,
197 fd,
198 page_aligned_offset));
199 if (actual == MAP_FAILED) {
200 PLOG(ERROR) << "mmap failed";
201 return NULL;
202 }
203 return new MemMap(actual + page_offset, length, actual, page_aligned_size);
204}
205
206MemMap::~MemMap() {
Ian Rogers30fab402012-01-23 15:43:46 -0800207 if (base_begin_ == NULL && base_size_ == 0) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700208 return;
209 }
Ian Rogers30fab402012-01-23 15:43:46 -0800210 int result = munmap(base_begin_, base_size_);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700211 if (result == -1) {
212 PLOG(FATAL) << "munmap failed";
213 }
214}
215
Ian Rogers30fab402012-01-23 15:43:46 -0800216MemMap::MemMap(byte* begin, size_t size, void* base_begin, size_t base_size)
217 : begin_(begin), size_(size), base_begin_(base_begin), base_size_(base_size) {
218 CHECK(begin_ != NULL);
219 CHECK_NE(size_, 0U);
220 CHECK(base_begin_ != NULL);
221 CHECK_NE(base_size_, 0U);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700222};
223
224} // namespace art