blob: 32753df6f1b9783abf0e43c4570419469e2a781a [file] [log] [blame]
Christopher Ferris723cf9b2017-01-19 20:08:48 -08001/*
2 * Copyright (C) 2016 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 <errno.h>
18#include <fcntl.h>
19#include <sys/mman.h>
20#include <sys/ptrace.h>
21#include <sys/stat.h>
22#include <sys/types.h>
23#include <sys/uio.h>
24#include <unistd.h>
25
26#include <algorithm>
27#include <memory>
28
29#include <android-base/unique_fd.h>
30
Christopher Ferrisd226a512017-07-14 10:37:19 -070031#include <unwindstack/Memory.h>
32
33#include "Check.h"
34
35namespace unwindstack {
Christopher Ferris723cf9b2017-01-19 20:08:48 -080036
37bool Memory::ReadString(uint64_t addr, std::string* string, uint64_t max_read) {
38 string->clear();
39 uint64_t bytes_read = 0;
40 while (bytes_read < max_read) {
41 uint8_t value;
42 if (!Read(addr, &value, sizeof(value))) {
43 return false;
44 }
45 if (value == '\0') {
46 return true;
47 }
48 string->push_back(value);
49 addr++;
50 bytes_read++;
51 }
52 return false;
53}
54
Christopher Ferris5f118512017-09-01 11:17:16 -070055std::shared_ptr<Memory> Memory::CreateProcessMemory(pid_t pid) {
56 if (pid == getpid()) {
57 return std::shared_ptr<Memory>(new MemoryLocal());
58 }
59 return std::shared_ptr<Memory>(new MemoryRemote(pid));
60}
61
Christopher Ferris3958f802017-02-01 15:44:40 -080062bool MemoryBuffer::Read(uint64_t addr, void* dst, size_t size) {
63 uint64_t last_read_byte;
64 if (__builtin_add_overflow(size, addr, &last_read_byte)) {
65 return false;
66 }
67 if (last_read_byte > raw_.size()) {
68 return false;
69 }
70 memcpy(dst, &raw_[addr], size);
71 return true;
72}
73
74uint8_t* MemoryBuffer::GetPtr(size_t offset) {
75 if (offset < raw_.size()) {
76 return &raw_[offset];
77 }
78 return nullptr;
79}
80
Christopher Ferris723cf9b2017-01-19 20:08:48 -080081MemoryFileAtOffset::~MemoryFileAtOffset() {
Christopher Ferris3958f802017-02-01 15:44:40 -080082 Clear();
83}
84
85void MemoryFileAtOffset::Clear() {
Christopher Ferris723cf9b2017-01-19 20:08:48 -080086 if (data_) {
87 munmap(&data_[-offset_], size_ + offset_);
88 data_ = nullptr;
89 }
90}
91
Christopher Ferris3958f802017-02-01 15:44:40 -080092bool MemoryFileAtOffset::Init(const std::string& file, uint64_t offset, uint64_t size) {
93 // Clear out any previous data if it exists.
94 Clear();
95
Christopher Ferris723cf9b2017-01-19 20:08:48 -080096 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(file.c_str(), O_RDONLY | O_CLOEXEC)));
97 if (fd == -1) {
98 return false;
99 }
100 struct stat buf;
101 if (fstat(fd, &buf) == -1) {
102 return false;
103 }
104 if (offset >= static_cast<uint64_t>(buf.st_size)) {
105 return false;
106 }
107
108 offset_ = offset & (getpagesize() - 1);
109 uint64_t aligned_offset = offset & ~(getpagesize() - 1);
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700110 if (aligned_offset > static_cast<uint64_t>(buf.st_size) ||
111 offset > static_cast<uint64_t>(buf.st_size)) {
112 return false;
113 }
114
Christopher Ferris723cf9b2017-01-19 20:08:48 -0800115 size_ = buf.st_size - aligned_offset;
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700116 uint64_t max_size;
117 if (!__builtin_add_overflow(size, offset_, &max_size) && max_size < size_) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800118 // Truncate the mapped size.
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700119 size_ = max_size;
Christopher Ferris3958f802017-02-01 15:44:40 -0800120 }
Christopher Ferris723cf9b2017-01-19 20:08:48 -0800121 void* map = mmap(nullptr, size_, PROT_READ, MAP_PRIVATE, fd, aligned_offset);
122 if (map == MAP_FAILED) {
123 return false;
124 }
125
126 data_ = &reinterpret_cast<uint8_t*>(map)[offset_];
127 size_ -= offset_;
128
129 return true;
130}
131
132bool MemoryFileAtOffset::Read(uint64_t addr, void* dst, size_t size) {
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700133 uint64_t max_size;
134 if (__builtin_add_overflow(addr, size, &max_size) || max_size > size_) {
Christopher Ferris723cf9b2017-01-19 20:08:48 -0800135 return false;
136 }
137 memcpy(dst, &data_[addr], size);
138 return true;
139}
140
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700141bool MemoryRemote::PtraceRead(uint64_t addr, long* value) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800142#if !defined(__LP64__)
143 // Cannot read an address greater than 32 bits.
144 if (addr > UINT32_MAX) {
145 return false;
146 }
147#endif
Christopher Ferris723cf9b2017-01-19 20:08:48 -0800148 // ptrace() returns -1 and sets errno when the operation fails.
149 // To disambiguate -1 from a valid result, we clear errno beforehand.
150 errno = 0;
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700151 *value = ptrace(PTRACE_PEEKTEXT, pid_, reinterpret_cast<void*>(addr), nullptr);
Christopher Ferris723cf9b2017-01-19 20:08:48 -0800152 if (*value == -1 && errno) {
153 return false;
154 }
155 return true;
156}
157
158bool MemoryRemote::Read(uint64_t addr, void* dst, size_t bytes) {
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700159 // Make sure that there is no overflow.
160 uint64_t max_size;
161 if (__builtin_add_overflow(addr, bytes, &max_size)) {
162 return false;
163 }
164
Christopher Ferris723cf9b2017-01-19 20:08:48 -0800165 size_t bytes_read = 0;
166 long data;
167 size_t align_bytes = addr & (sizeof(long) - 1);
168 if (align_bytes != 0) {
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700169 if (!PtraceRead(addr & ~(sizeof(long) - 1), &data)) {
Christopher Ferris723cf9b2017-01-19 20:08:48 -0800170 return false;
171 }
172 size_t copy_bytes = std::min(sizeof(long) - align_bytes, bytes);
173 memcpy(dst, reinterpret_cast<uint8_t*>(&data) + align_bytes, copy_bytes);
174 addr += copy_bytes;
175 dst = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(dst) + copy_bytes);
176 bytes -= copy_bytes;
177 bytes_read += copy_bytes;
178 }
179
180 for (size_t i = 0; i < bytes / sizeof(long); i++) {
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700181 if (!PtraceRead(addr, &data)) {
Christopher Ferris723cf9b2017-01-19 20:08:48 -0800182 return false;
183 }
184 memcpy(dst, &data, sizeof(long));
185 dst = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(dst) + sizeof(long));
186 addr += sizeof(long);
187 bytes_read += sizeof(long);
188 }
189
190 size_t left_over = bytes & (sizeof(long) - 1);
191 if (left_over) {
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700192 if (!PtraceRead(addr, &data)) {
Christopher Ferris723cf9b2017-01-19 20:08:48 -0800193 return false;
194 }
195 memcpy(dst, &data, left_over);
196 bytes_read += left_over;
197 }
198 return true;
199}
200
201bool MemoryLocal::Read(uint64_t addr, void* dst, size_t size) {
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700202 // Make sure that there is no overflow.
203 uint64_t max_size;
204 if (__builtin_add_overflow(addr, size, &max_size)) {
205 return false;
206 }
207
208 // The process_vm_readv call will not always work on remote
Christopher Ferris723cf9b2017-01-19 20:08:48 -0800209 // processes, so only use it for reads from the current pid.
210 // Use this method to avoid crashes if an address is invalid since
211 // unwind data could try to access any part of the address space.
212 struct iovec local_io;
213 local_io.iov_base = dst;
214 local_io.iov_len = size;
215
216 struct iovec remote_io;
217 remote_io.iov_base = reinterpret_cast<void*>(static_cast<uintptr_t>(addr));
218 remote_io.iov_len = size;
219
220 ssize_t bytes_read = process_vm_readv(getpid(), &local_io, 1, &remote_io, 1, 0);
221 if (bytes_read == -1) {
222 return false;
223 }
224 return static_cast<size_t>(bytes_read) == size;
225}
226
227bool MemoryOffline::Init(const std::string& file, uint64_t offset) {
228 if (!MemoryFileAtOffset::Init(file, offset)) {
229 return false;
230 }
231 // The first uint64_t value is the start of memory.
232 if (!MemoryFileAtOffset::Read(0, &start_, sizeof(start_))) {
233 return false;
234 }
235 // Subtract the first 64 bit value from the total size.
236 size_ -= sizeof(start_);
237 return true;
238}
239
240bool MemoryOffline::Read(uint64_t addr, void* dst, size_t size) {
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700241 uint64_t max_size;
242 if (__builtin_add_overflow(addr, size, &max_size)) {
243 return false;
244 }
245
246 uint64_t real_size;
247 if (__builtin_add_overflow(start_, offset_, &real_size) ||
248 __builtin_add_overflow(real_size, size_, &real_size)) {
249 return false;
250 }
251
252 if (addr < start_ || max_size > real_size) {
Christopher Ferris723cf9b2017-01-19 20:08:48 -0800253 return false;
254 }
255 memcpy(dst, &data_[addr + offset_ - start_ + sizeof(start_)], size);
256 return true;
257}
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700258
Christopher Ferris5f118512017-09-01 11:17:16 -0700259MemoryRange::MemoryRange(const std::shared_ptr<Memory>& memory, uint64_t begin, uint64_t end)
Christopher Ferrisd226a512017-07-14 10:37:19 -0700260 : memory_(memory), begin_(begin), length_(end - begin) {
261 CHECK(end > begin);
262}
263
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700264bool MemoryRange::Read(uint64_t addr, void* dst, size_t size) {
265 uint64_t max_read;
266 if (__builtin_add_overflow(addr, size, &max_read) || max_read > length_) {
267 return false;
268 }
269 // The check above guarantees that addr + begin_ will not overflow.
270 return memory_->Read(addr + begin_, dst, size);
271}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700272
273} // namespace unwindstack