| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2018 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 <stdint.h> | 
|  | 18 | #include <sys/mman.h> | 
|  | 19 | #include <sys/stat.h> | 
|  | 20 | #include <sys/types.h> | 
|  | 21 | #include <unistd.h> | 
|  | 22 |  | 
|  | 23 | #include <memory> | 
|  | 24 |  | 
|  | 25 | #include <android-base/unique_fd.h> | 
|  | 26 |  | 
| Mathieu Chartier | c2277fc | 2018-08-07 18:12:23 -0700 | [diff] [blame] | 27 | #include <dex/class_accessor-inl.h> | 
| David Sehr | 892e675 | 2018-02-07 15:19:22 -0800 | [diff] [blame] | 28 | #include <dex/code_item_accessors-inl.h> | 
| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 29 | #include <dex/compact_dex_file.h> | 
|  | 30 | #include <dex/dex_file-inl.h> | 
|  | 31 | #include <dex/dex_file_loader.h> | 
|  | 32 | #include <dex/standard_dex_file.h> | 
|  | 33 |  | 
|  | 34 | #include <unwindstack/MapInfo.h> | 
|  | 35 | #include <unwindstack/Memory.h> | 
|  | 36 |  | 
| Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 37 | #include "DexFile.h" | 
| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 38 |  | 
| Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 39 | namespace unwindstack { | 
|  | 40 |  | 
|  | 41 | DexFile* DexFile::Create(uint64_t dex_file_offset_in_memory, Memory* memory, MapInfo* info) { | 
| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 42 | if (!info->name.empty()) { | 
| Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 43 | std::unique_ptr<DexFileFromFile> dex_file(new DexFileFromFile); | 
| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 44 | if (dex_file->Open(dex_file_offset_in_memory - info->start + info->offset, info->name)) { | 
|  | 45 | return dex_file.release(); | 
|  | 46 | } | 
|  | 47 | } | 
|  | 48 |  | 
| Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 49 | std::unique_ptr<DexFileFromMemory> dex_file(new DexFileFromMemory); | 
| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 50 | if (dex_file->Open(dex_file_offset_in_memory, memory)) { | 
|  | 51 | return dex_file.release(); | 
|  | 52 | } | 
|  | 53 | return nullptr; | 
|  | 54 | } | 
|  | 55 |  | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 56 | DexFileFromFile::~DexFileFromFile() { | 
|  | 57 | if (size_ != 0) { | 
|  | 58 | munmap(mapped_memory_, size_); | 
|  | 59 | } | 
|  | 60 | } | 
|  | 61 |  | 
|  | 62 | bool DexFile::GetMethodInformation(uint64_t dex_offset, std::string* method_name, | 
| Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 63 | uint64_t* method_offset) { | 
| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 64 | if (dex_file_ == nullptr) { | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 65 | return false; | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | if (!dex_file_->IsInDataSection(dex_file_->Begin() + dex_offset)) { | 
|  | 69 | return false;  // The DEX offset is not within the bytecode of this dex file. | 
| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 70 | } | 
|  | 71 |  | 
| David Srbecky | 02d0f79 | 2018-03-24 00:29:14 +0000 | [diff] [blame] | 72 | if (dex_file_->IsCompactDexFile()) { | 
|  | 73 | // The data section of compact dex files might be shared. | 
|  | 74 | // Check the subrange unique to this compact dex. | 
|  | 75 | const auto& cdex_header = dex_file_->AsCompactDexFile()->GetHeader(); | 
|  | 76 | uint32_t begin = cdex_header.data_off_ + cdex_header.OwnedDataBegin(); | 
|  | 77 | uint32_t end = cdex_header.data_off_ + cdex_header.OwnedDataEnd(); | 
|  | 78 | if (dex_offset < begin || dex_offset >= end) { | 
|  | 79 | return false;  // The DEX offset is not within the bytecode of this dex file. | 
|  | 80 | } | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | // The method data is cached in a std::map indexed by method end offset and | 
|  | 84 | // contains the start offset and the method member index. | 
|  | 85 | // Only cache the method data as it is searched. Do not read the entire | 
|  | 86 | // set of method data into the cache at once. | 
|  | 87 | // This is done because many unwinds only find a single frame with dex file | 
|  | 88 | // info, so reading the entire method data is wasteful. However, still cache | 
|  | 89 | // the data so that anything doing multiple unwinds will have this data | 
|  | 90 | // cached for future use. | 
|  | 91 |  | 
|  | 92 | // First look in the method cache. | 
|  | 93 | auto entry = method_cache_.upper_bound(dex_offset); | 
|  | 94 | if (entry != method_cache_.end() && dex_offset >= entry->second.first) { | 
|  | 95 | *method_name = dex_file_->PrettyMethod(entry->second.second, false); | 
|  | 96 | *method_offset = dex_offset - entry->second.first; | 
|  | 97 | return true; | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | // Check the methods we haven't cached. | 
|  | 101 | for (; class_def_index_ < dex_file_->NumClassDefs(); class_def_index_++) { | 
| Mathieu Chartier | c2277fc | 2018-08-07 18:12:23 -0700 | [diff] [blame] | 102 | art::ClassAccessor accessor(*dex_file_, dex_file_->GetClassDef(class_def_index_)); | 
| David Srbecky | 02d0f79 | 2018-03-24 00:29:14 +0000 | [diff] [blame] | 103 |  | 
| Mathieu Chartier | c2277fc | 2018-08-07 18:12:23 -0700 | [diff] [blame] | 104 | for (const art::ClassAccessor::Method& method : accessor.GetMethods()) { | 
|  | 105 | art::CodeItemInstructionAccessor code = method.GetInstructions(); | 
| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 106 | if (!code.HasCodeItem()) { | 
|  | 107 | continue; | 
|  | 108 | } | 
| David Srbecky | 02d0f79 | 2018-03-24 00:29:14 +0000 | [diff] [blame] | 109 | uint32_t offset = reinterpret_cast<const uint8_t*>(code.Insns()) - dex_file_->Begin(); | 
| Mathieu Chartier | c2277fc | 2018-08-07 18:12:23 -0700 | [diff] [blame] | 110 | uint32_t offset_end = offset + code.InsnsSizeInBytes(); | 
|  | 111 | uint32_t member_index = method.GetIndex(); | 
| David Srbecky | 02d0f79 | 2018-03-24 00:29:14 +0000 | [diff] [blame] | 112 | method_cache_[offset_end] = std::make_pair(offset, member_index); | 
|  | 113 | if (offset <= dex_offset && dex_offset < offset_end) { | 
|  | 114 | *method_name = dex_file_->PrettyMethod(member_index, false); | 
| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 115 | *method_offset = dex_offset - offset; | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 116 | return true; | 
| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 117 | } | 
|  | 118 | } | 
|  | 119 | } | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 120 | return false; | 
| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 121 | } | 
|  | 122 |  | 
| Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 123 | bool DexFileFromFile::Open(uint64_t dex_file_offset_in_file, const std::string& file) { | 
| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 124 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(file.c_str(), O_RDONLY | O_CLOEXEC))); | 
|  | 125 | if (fd == -1) { | 
|  | 126 | return false; | 
|  | 127 | } | 
|  | 128 | struct stat buf; | 
|  | 129 | if (fstat(fd, &buf) == -1) { | 
|  | 130 | return false; | 
|  | 131 | } | 
|  | 132 | uint64_t length; | 
|  | 133 | if (buf.st_size < 0 || | 
|  | 134 | __builtin_add_overflow(dex_file_offset_in_file, sizeof(art::DexFile::Header), &length) || | 
|  | 135 | static_cast<uint64_t>(buf.st_size) < length) { | 
|  | 136 | return false; | 
|  | 137 | } | 
|  | 138 |  | 
|  | 139 | mapped_memory_ = mmap(nullptr, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0); | 
|  | 140 | if (mapped_memory_ == MAP_FAILED) { | 
|  | 141 | return false; | 
|  | 142 | } | 
|  | 143 | size_ = buf.st_size; | 
|  | 144 |  | 
|  | 145 | uint8_t* memory = reinterpret_cast<uint8_t*>(mapped_memory_); | 
|  | 146 |  | 
|  | 147 | art::DexFile::Header* header = | 
|  | 148 | reinterpret_cast<art::DexFile::Header*>(&memory[dex_file_offset_in_file]); | 
|  | 149 | if (!art::StandardDexFile::IsMagicValid(header->magic_) && | 
|  | 150 | !art::CompactDexFile::IsMagicValid(header->magic_)) { | 
|  | 151 | return false; | 
|  | 152 | } | 
|  | 153 |  | 
|  | 154 | if (__builtin_add_overflow(dex_file_offset_in_file, header->file_size_, &length) || | 
|  | 155 | static_cast<uint64_t>(buf.st_size) < length) { | 
|  | 156 | return false; | 
|  | 157 | } | 
|  | 158 |  | 
|  | 159 | art::DexFileLoader loader; | 
|  | 160 | std::string error_msg; | 
|  | 161 | auto dex = loader.Open(&memory[dex_file_offset_in_file], header->file_size_, "", 0, nullptr, | 
|  | 162 | false, false, &error_msg); | 
|  | 163 | dex_file_.reset(dex.release()); | 
|  | 164 | return dex_file_ != nullptr; | 
|  | 165 | } | 
|  | 166 |  | 
| Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 167 | bool DexFileFromMemory::Open(uint64_t dex_file_offset_in_memory, Memory* memory) { | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 168 | memory_.resize(sizeof(art::DexFile::Header)); | 
|  | 169 | if (!memory->ReadFully(dex_file_offset_in_memory, memory_.data(), memory_.size())) { | 
| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 170 | return false; | 
|  | 171 | } | 
|  | 172 |  | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 173 | art::DexFile::Header* header = reinterpret_cast<art::DexFile::Header*>(memory_.data()); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 174 | uint32_t file_size = header->file_size_; | 
|  | 175 | if (art::CompactDexFile::IsMagicValid(header->magic_)) { | 
| David Srbecky | 417f7c3 | 2018-02-05 20:14:48 +0000 | [diff] [blame] | 176 | // Compact dex file store data section separately so that it can be shared. | 
|  | 177 | // Therefore we need to extend the read memory range to include it. | 
|  | 178 | // TODO: This might be wasteful as we might read data in between as well. | 
|  | 179 | //       In practice, this should be fine, as such sharing only happens on disk. | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 180 | uint32_t computed_file_size; | 
|  | 181 | if (__builtin_add_overflow(header->data_off_, header->data_size_, &computed_file_size)) { | 
|  | 182 | return false; | 
|  | 183 | } | 
|  | 184 | if (computed_file_size > file_size) { | 
|  | 185 | file_size = computed_file_size; | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 186 | } | 
|  | 187 | } else if (!art::StandardDexFile::IsMagicValid(header->magic_)) { | 
| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 188 | return false; | 
|  | 189 | } | 
|  | 190 |  | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 191 | memory_.resize(file_size); | 
|  | 192 | if (!memory->ReadFully(dex_file_offset_in_memory, memory_.data(), memory_.size())) { | 
| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 193 | return false; | 
|  | 194 | } | 
|  | 195 |  | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 196 | header = reinterpret_cast<art::DexFile::Header*>(memory_.data()); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 197 |  | 
| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 198 | art::DexFileLoader loader; | 
|  | 199 | std::string error_msg; | 
|  | 200 | auto dex = | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 201 | loader.Open(memory_.data(), header->file_size_, "", 0, nullptr, false, false, &error_msg); | 
| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 202 | dex_file_.reset(dex.release()); | 
|  | 203 | return dex_file_ != nullptr; | 
|  | 204 | } | 
| Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 205 |  | 
|  | 206 | }  // namespace unwindstack |