| Christopher Ferris | 723cf9b | 2017-01-19 20:08:48 -0800 | [diff] [blame] | 1 | /* | 
|  | 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 |  | 
|  | 33 | bool 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 |  | 
|  | 51 | MemoryFileAtOffset::~MemoryFileAtOffset() { | 
|  | 52 | if (data_) { | 
|  | 53 | munmap(&data_[-offset_], size_ + offset_); | 
|  | 54 | data_ = nullptr; | 
|  | 55 | } | 
|  | 56 | } | 
|  | 57 |  | 
|  | 58 | bool MemoryFileAtOffset::Init(const std::string& file, uint64_t offset) { | 
|  | 59 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(file.c_str(), O_RDONLY | O_CLOEXEC))); | 
|  | 60 | if (fd == -1) { | 
|  | 61 | return false; | 
|  | 62 | } | 
|  | 63 | struct stat buf; | 
|  | 64 | if (fstat(fd, &buf) == -1) { | 
|  | 65 | return false; | 
|  | 66 | } | 
|  | 67 | if (offset >= static_cast<uint64_t>(buf.st_size)) { | 
|  | 68 | return false; | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | offset_ = offset & (getpagesize() - 1); | 
|  | 72 | uint64_t aligned_offset = offset & ~(getpagesize() - 1); | 
|  | 73 | size_ = buf.st_size - aligned_offset; | 
|  | 74 | void* map = mmap(nullptr, size_, PROT_READ, MAP_PRIVATE, fd, aligned_offset); | 
|  | 75 | if (map == MAP_FAILED) { | 
|  | 76 | return false; | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | data_ = &reinterpret_cast<uint8_t*>(map)[offset_]; | 
|  | 80 | size_ -= offset_; | 
|  | 81 |  | 
|  | 82 | return true; | 
|  | 83 | } | 
|  | 84 |  | 
|  | 85 | bool MemoryFileAtOffset::Read(uint64_t addr, void* dst, size_t size) { | 
|  | 86 | if (addr + size > size_) { | 
|  | 87 | return false; | 
|  | 88 | } | 
|  | 89 | memcpy(dst, &data_[addr], size); | 
|  | 90 | return true; | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | static bool PtraceRead(pid_t pid, uint64_t addr, long* value) { | 
|  | 94 | // ptrace() returns -1 and sets errno when the operation fails. | 
|  | 95 | // To disambiguate -1 from a valid result, we clear errno beforehand. | 
|  | 96 | errno = 0; | 
|  | 97 | *value = ptrace(PTRACE_PEEKTEXT, pid, reinterpret_cast<void*>(addr), nullptr); | 
|  | 98 | if (*value == -1 && errno) { | 
|  | 99 | return false; | 
|  | 100 | } | 
|  | 101 | return true; | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 | bool MemoryRemote::Read(uint64_t addr, void* dst, size_t bytes) { | 
|  | 105 | size_t bytes_read = 0; | 
|  | 106 | long data; | 
|  | 107 | size_t align_bytes = addr & (sizeof(long) - 1); | 
|  | 108 | if (align_bytes != 0) { | 
|  | 109 | if (!PtraceRead(pid_, addr & ~(sizeof(long) - 1), &data)) { | 
|  | 110 | return false; | 
|  | 111 | } | 
|  | 112 | size_t copy_bytes = std::min(sizeof(long) - align_bytes, bytes); | 
|  | 113 | memcpy(dst, reinterpret_cast<uint8_t*>(&data) + align_bytes, copy_bytes); | 
|  | 114 | addr += copy_bytes; | 
|  | 115 | dst = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(dst) + copy_bytes); | 
|  | 116 | bytes -= copy_bytes; | 
|  | 117 | bytes_read += copy_bytes; | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | for (size_t i = 0; i < bytes / sizeof(long); i++) { | 
|  | 121 | if (!PtraceRead(pid_, addr, &data)) { | 
|  | 122 | return false; | 
|  | 123 | } | 
|  | 124 | memcpy(dst, &data, sizeof(long)); | 
|  | 125 | dst = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(dst) + sizeof(long)); | 
|  | 126 | addr += sizeof(long); | 
|  | 127 | bytes_read += sizeof(long); | 
|  | 128 | } | 
|  | 129 |  | 
|  | 130 | size_t left_over = bytes & (sizeof(long) - 1); | 
|  | 131 | if (left_over) { | 
|  | 132 | if (!PtraceRead(pid_, addr, &data)) { | 
|  | 133 | return false; | 
|  | 134 | } | 
|  | 135 | memcpy(dst, &data, left_over); | 
|  | 136 | bytes_read += left_over; | 
|  | 137 | } | 
|  | 138 | return true; | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 | bool MemoryLocal::Read(uint64_t addr, void* dst, size_t size) { | 
|  | 142 | // The process_vm_readv call does will not always work on remote | 
|  | 143 | // processes, so only use it for reads from the current pid. | 
|  | 144 | // Use this method to avoid crashes if an address is invalid since | 
|  | 145 | // unwind data could try to access any part of the address space. | 
|  | 146 | struct iovec local_io; | 
|  | 147 | local_io.iov_base = dst; | 
|  | 148 | local_io.iov_len = size; | 
|  | 149 |  | 
|  | 150 | struct iovec remote_io; | 
|  | 151 | remote_io.iov_base = reinterpret_cast<void*>(static_cast<uintptr_t>(addr)); | 
|  | 152 | remote_io.iov_len = size; | 
|  | 153 |  | 
|  | 154 | ssize_t bytes_read = process_vm_readv(getpid(), &local_io, 1, &remote_io, 1, 0); | 
|  | 155 | if (bytes_read == -1) { | 
|  | 156 | return false; | 
|  | 157 | } | 
|  | 158 | return static_cast<size_t>(bytes_read) == size; | 
|  | 159 | } | 
|  | 160 |  | 
|  | 161 | bool MemoryOffline::Init(const std::string& file, uint64_t offset) { | 
|  | 162 | if (!MemoryFileAtOffset::Init(file, offset)) { | 
|  | 163 | return false; | 
|  | 164 | } | 
|  | 165 | // The first uint64_t value is the start of memory. | 
|  | 166 | if (!MemoryFileAtOffset::Read(0, &start_, sizeof(start_))) { | 
|  | 167 | return false; | 
|  | 168 | } | 
|  | 169 | // Subtract the first 64 bit value from the total size. | 
|  | 170 | size_ -= sizeof(start_); | 
|  | 171 | return true; | 
|  | 172 | } | 
|  | 173 |  | 
|  | 174 | bool MemoryOffline::Read(uint64_t addr, void* dst, size_t size) { | 
|  | 175 | if (addr < start_ || addr + size > start_ + offset_ + size_) { | 
|  | 176 | return false; | 
|  | 177 | } | 
|  | 178 | memcpy(dst, &data_[addr + offset_ - start_ + sizeof(start_)], size); | 
|  | 179 | return true; | 
|  | 180 | } |