blob: 9e4650998601fe7192e3f92288498a3306207e27 [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
31#include "Memory.h"
32
33bool Memory::ReadString(uint64_t addr, std::string* string, uint64_t max_read) {
34 string->clear();
35 uint64_t bytes_read = 0;
36 while (bytes_read < max_read) {
37 uint8_t value;
38 if (!Read(addr, &value, sizeof(value))) {
39 return false;
40 }
41 if (value == '\0') {
42 return true;
43 }
44 string->push_back(value);
45 addr++;
46 bytes_read++;
47 }
48 return false;
49}
50
Christopher Ferris3958f802017-02-01 15:44:40 -080051bool MemoryBuffer::Read(uint64_t addr, void* dst, size_t size) {
52 uint64_t last_read_byte;
53 if (__builtin_add_overflow(size, addr, &last_read_byte)) {
54 return false;
55 }
56 if (last_read_byte > raw_.size()) {
57 return false;
58 }
59 memcpy(dst, &raw_[addr], size);
60 return true;
61}
62
63uint8_t* MemoryBuffer::GetPtr(size_t offset) {
64 if (offset < raw_.size()) {
65 return &raw_[offset];
66 }
67 return nullptr;
68}
69
Christopher Ferris723cf9b2017-01-19 20:08:48 -080070MemoryFileAtOffset::~MemoryFileAtOffset() {
Christopher Ferris3958f802017-02-01 15:44:40 -080071 Clear();
72}
73
74void MemoryFileAtOffset::Clear() {
Christopher Ferris723cf9b2017-01-19 20:08:48 -080075 if (data_) {
76 munmap(&data_[-offset_], size_ + offset_);
77 data_ = nullptr;
78 }
79}
80
Christopher Ferris3958f802017-02-01 15:44:40 -080081bool MemoryFileAtOffset::Init(const std::string& file, uint64_t offset, uint64_t size) {
82 // Clear out any previous data if it exists.
83 Clear();
84
Christopher Ferris723cf9b2017-01-19 20:08:48 -080085 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(file.c_str(), O_RDONLY | O_CLOEXEC)));
86 if (fd == -1) {
87 return false;
88 }
89 struct stat buf;
90 if (fstat(fd, &buf) == -1) {
91 return false;
92 }
93 if (offset >= static_cast<uint64_t>(buf.st_size)) {
94 return false;
95 }
96
97 offset_ = offset & (getpagesize() - 1);
98 uint64_t aligned_offset = offset & ~(getpagesize() - 1);
Christopher Ferrisf447c8e2017-04-03 12:39:47 -070099 if (aligned_offset > static_cast<uint64_t>(buf.st_size) ||
100 offset > static_cast<uint64_t>(buf.st_size)) {
101 return false;
102 }
103
Christopher Ferris723cf9b2017-01-19 20:08:48 -0800104 size_ = buf.st_size - aligned_offset;
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700105 uint64_t max_size;
106 if (!__builtin_add_overflow(size, offset_, &max_size) && max_size < size_) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800107 // Truncate the mapped size.
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700108 size_ = max_size;
Christopher Ferris3958f802017-02-01 15:44:40 -0800109 }
Christopher Ferris723cf9b2017-01-19 20:08:48 -0800110 void* map = mmap(nullptr, size_, PROT_READ, MAP_PRIVATE, fd, aligned_offset);
111 if (map == MAP_FAILED) {
112 return false;
113 }
114
115 data_ = &reinterpret_cast<uint8_t*>(map)[offset_];
116 size_ -= offset_;
117
118 return true;
119}
120
121bool MemoryFileAtOffset::Read(uint64_t addr, void* dst, size_t size) {
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700122 uint64_t max_size;
123 if (__builtin_add_overflow(addr, size, &max_size) || max_size > size_) {
Christopher Ferris723cf9b2017-01-19 20:08:48 -0800124 return false;
125 }
126 memcpy(dst, &data_[addr], size);
127 return true;
128}
129
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700130bool MemoryRemote::PtraceRead(uint64_t addr, long* value) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800131#if !defined(__LP64__)
132 // Cannot read an address greater than 32 bits.
133 if (addr > UINT32_MAX) {
134 return false;
135 }
136#endif
Christopher Ferris723cf9b2017-01-19 20:08:48 -0800137 // ptrace() returns -1 and sets errno when the operation fails.
138 // To disambiguate -1 from a valid result, we clear errno beforehand.
139 errno = 0;
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700140 *value = ptrace(PTRACE_PEEKTEXT, pid_, reinterpret_cast<void*>(addr), nullptr);
Christopher Ferris723cf9b2017-01-19 20:08:48 -0800141 if (*value == -1 && errno) {
142 return false;
143 }
144 return true;
145}
146
147bool MemoryRemote::Read(uint64_t addr, void* dst, size_t bytes) {
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700148 // Make sure that there is no overflow.
149 uint64_t max_size;
150 if (__builtin_add_overflow(addr, bytes, &max_size)) {
151 return false;
152 }
153
Christopher Ferris723cf9b2017-01-19 20:08:48 -0800154 size_t bytes_read = 0;
155 long data;
156 size_t align_bytes = addr & (sizeof(long) - 1);
157 if (align_bytes != 0) {
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700158 if (!PtraceRead(addr & ~(sizeof(long) - 1), &data)) {
Christopher Ferris723cf9b2017-01-19 20:08:48 -0800159 return false;
160 }
161 size_t copy_bytes = std::min(sizeof(long) - align_bytes, bytes);
162 memcpy(dst, reinterpret_cast<uint8_t*>(&data) + align_bytes, copy_bytes);
163 addr += copy_bytes;
164 dst = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(dst) + copy_bytes);
165 bytes -= copy_bytes;
166 bytes_read += copy_bytes;
167 }
168
169 for (size_t i = 0; i < bytes / sizeof(long); i++) {
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700170 if (!PtraceRead(addr, &data)) {
Christopher Ferris723cf9b2017-01-19 20:08:48 -0800171 return false;
172 }
173 memcpy(dst, &data, sizeof(long));
174 dst = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(dst) + sizeof(long));
175 addr += sizeof(long);
176 bytes_read += sizeof(long);
177 }
178
179 size_t left_over = bytes & (sizeof(long) - 1);
180 if (left_over) {
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, left_over);
185 bytes_read += left_over;
186 }
187 return true;
188}
189
190bool MemoryLocal::Read(uint64_t addr, void* dst, size_t size) {
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700191 // Make sure that there is no overflow.
192 uint64_t max_size;
193 if (__builtin_add_overflow(addr, size, &max_size)) {
194 return false;
195 }
196
197 // The process_vm_readv call will not always work on remote
Christopher Ferris723cf9b2017-01-19 20:08:48 -0800198 // processes, so only use it for reads from the current pid.
199 // Use this method to avoid crashes if an address is invalid since
200 // unwind data could try to access any part of the address space.
201 struct iovec local_io;
202 local_io.iov_base = dst;
203 local_io.iov_len = size;
204
205 struct iovec remote_io;
206 remote_io.iov_base = reinterpret_cast<void*>(static_cast<uintptr_t>(addr));
207 remote_io.iov_len = size;
208
209 ssize_t bytes_read = process_vm_readv(getpid(), &local_io, 1, &remote_io, 1, 0);
210 if (bytes_read == -1) {
211 return false;
212 }
213 return static_cast<size_t>(bytes_read) == size;
214}
215
216bool MemoryOffline::Init(const std::string& file, uint64_t offset) {
217 if (!MemoryFileAtOffset::Init(file, offset)) {
218 return false;
219 }
220 // The first uint64_t value is the start of memory.
221 if (!MemoryFileAtOffset::Read(0, &start_, sizeof(start_))) {
222 return false;
223 }
224 // Subtract the first 64 bit value from the total size.
225 size_ -= sizeof(start_);
226 return true;
227}
228
229bool MemoryOffline::Read(uint64_t addr, void* dst, size_t size) {
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700230 uint64_t max_size;
231 if (__builtin_add_overflow(addr, size, &max_size)) {
232 return false;
233 }
234
235 uint64_t real_size;
236 if (__builtin_add_overflow(start_, offset_, &real_size) ||
237 __builtin_add_overflow(real_size, size_, &real_size)) {
238 return false;
239 }
240
241 if (addr < start_ || max_size > real_size) {
Christopher Ferris723cf9b2017-01-19 20:08:48 -0800242 return false;
243 }
244 memcpy(dst, &data_[addr + offset_ - start_ + sizeof(start_)], size);
245 return true;
246}
Christopher Ferrisf447c8e2017-04-03 12:39:47 -0700247
248bool MemoryRange::Read(uint64_t addr, void* dst, size_t size) {
249 uint64_t max_read;
250 if (__builtin_add_overflow(addr, size, &max_read) || max_read > length_) {
251 return false;
252 }
253 // The check above guarantees that addr + begin_ will not overflow.
254 return memory_->Read(addr + begin_, dst, size);
255}