| 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 |  | 
|  | 27 | #include <dex/code_item_accessors-no_art-inl.h> | 
|  | 28 | #include <dex/compact_dex_file.h> | 
|  | 29 | #include <dex/dex_file-inl.h> | 
|  | 30 | #include <dex/dex_file_loader.h> | 
|  | 31 | #include <dex/standard_dex_file.h> | 
|  | 32 |  | 
|  | 33 | #include <unwindstack/MapInfo.h> | 
|  | 34 | #include <unwindstack/Memory.h> | 
|  | 35 |  | 
| Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 36 | #include "DexFile.h" | 
| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 37 |  | 
| Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 38 | namespace unwindstack { | 
|  | 39 |  | 
|  | 40 | 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] | 41 | if (!info->name.empty()) { | 
| Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 42 | std::unique_ptr<DexFileFromFile> dex_file(new DexFileFromFile); | 
| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 43 | if (dex_file->Open(dex_file_offset_in_memory - info->start + info->offset, info->name)) { | 
|  | 44 | return dex_file.release(); | 
|  | 45 | } | 
|  | 46 | } | 
|  | 47 |  | 
| Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 48 | std::unique_ptr<DexFileFromMemory> dex_file(new DexFileFromMemory); | 
| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 49 | if (dex_file->Open(dex_file_offset_in_memory, memory)) { | 
|  | 50 | return dex_file.release(); | 
|  | 51 | } | 
|  | 52 | return nullptr; | 
|  | 53 | } | 
|  | 54 |  | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 55 | DexFileFromFile::~DexFileFromFile() { | 
|  | 56 | if (size_ != 0) { | 
|  | 57 | munmap(mapped_memory_, size_); | 
|  | 58 | } | 
|  | 59 | } | 
|  | 60 |  | 
|  | 61 | bool DexFile::GetMethodInformation(uint64_t dex_offset, std::string* method_name, | 
| Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 62 | uint64_t* method_offset) { | 
| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 63 | if (dex_file_ == nullptr) { | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 64 | return false; | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | if (!dex_file_->IsInDataSection(dex_file_->Begin() + dex_offset)) { | 
|  | 68 | 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] | 69 | } | 
|  | 70 |  | 
|  | 71 | for (uint32_t i = 0; i < dex_file_->NumClassDefs(); ++i) { | 
|  | 72 | const art::DexFile::ClassDef& class_def = dex_file_->GetClassDef(i); | 
|  | 73 | const uint8_t* class_data = dex_file_->GetClassData(class_def); | 
|  | 74 | if (class_data == nullptr) { | 
|  | 75 | continue; | 
|  | 76 | } | 
|  | 77 | for (art::ClassDataItemIterator it(*dex_file_.get(), class_data); it.HasNext(); it.Next()) { | 
|  | 78 | if (!it.IsAtMethod()) { | 
|  | 79 | continue; | 
|  | 80 | } | 
|  | 81 | const art::DexFile::CodeItem* code_item = it.GetMethodCodeItem(); | 
|  | 82 | if (code_item == nullptr) { | 
|  | 83 | continue; | 
|  | 84 | } | 
|  | 85 | art::CodeItemInstructionAccessor code(*dex_file_.get(), code_item); | 
|  | 86 | if (!code.HasCodeItem()) { | 
|  | 87 | continue; | 
|  | 88 | } | 
|  | 89 |  | 
|  | 90 | uint64_t offset = reinterpret_cast<const uint8_t*>(code.Insns()) - dex_file_->Begin(); | 
|  | 91 | size_t size = code.InsnsSizeInCodeUnits() * sizeof(uint16_t); | 
|  | 92 | if (offset <= dex_offset && dex_offset < offset + size) { | 
|  | 93 | *method_name = dex_file_->PrettyMethod(it.GetMemberIndex(), false); | 
|  | 94 | *method_offset = dex_offset - offset; | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 95 | return true; | 
| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 96 | } | 
|  | 97 | } | 
|  | 98 | } | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 99 | return false; | 
| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 100 | } | 
|  | 101 |  | 
| Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 102 | 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] | 103 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(file.c_str(), O_RDONLY | O_CLOEXEC))); | 
|  | 104 | if (fd == -1) { | 
|  | 105 | return false; | 
|  | 106 | } | 
|  | 107 | struct stat buf; | 
|  | 108 | if (fstat(fd, &buf) == -1) { | 
|  | 109 | return false; | 
|  | 110 | } | 
|  | 111 | uint64_t length; | 
|  | 112 | if (buf.st_size < 0 || | 
|  | 113 | __builtin_add_overflow(dex_file_offset_in_file, sizeof(art::DexFile::Header), &length) || | 
|  | 114 | static_cast<uint64_t>(buf.st_size) < length) { | 
|  | 115 | return false; | 
|  | 116 | } | 
|  | 117 |  | 
|  | 118 | mapped_memory_ = mmap(nullptr, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0); | 
|  | 119 | if (mapped_memory_ == MAP_FAILED) { | 
|  | 120 | return false; | 
|  | 121 | } | 
|  | 122 | size_ = buf.st_size; | 
|  | 123 |  | 
|  | 124 | uint8_t* memory = reinterpret_cast<uint8_t*>(mapped_memory_); | 
|  | 125 |  | 
|  | 126 | art::DexFile::Header* header = | 
|  | 127 | reinterpret_cast<art::DexFile::Header*>(&memory[dex_file_offset_in_file]); | 
|  | 128 | if (!art::StandardDexFile::IsMagicValid(header->magic_) && | 
|  | 129 | !art::CompactDexFile::IsMagicValid(header->magic_)) { | 
|  | 130 | return false; | 
|  | 131 | } | 
|  | 132 |  | 
|  | 133 | if (__builtin_add_overflow(dex_file_offset_in_file, header->file_size_, &length) || | 
|  | 134 | static_cast<uint64_t>(buf.st_size) < length) { | 
|  | 135 | return false; | 
|  | 136 | } | 
|  | 137 |  | 
|  | 138 | art::DexFileLoader loader; | 
|  | 139 | std::string error_msg; | 
|  | 140 | auto dex = loader.Open(&memory[dex_file_offset_in_file], header->file_size_, "", 0, nullptr, | 
|  | 141 | false, false, &error_msg); | 
|  | 142 | dex_file_.reset(dex.release()); | 
|  | 143 | return dex_file_ != nullptr; | 
|  | 144 | } | 
|  | 145 |  | 
| Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 146 | bool DexFileFromMemory::Open(uint64_t dex_file_offset_in_memory, Memory* memory) { | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 147 | memory_.resize(sizeof(art::DexFile::Header)); | 
|  | 148 | if (!memory->ReadFully(dex_file_offset_in_memory, memory_.data(), memory_.size())) { | 
| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 149 | return false; | 
|  | 150 | } | 
|  | 151 |  | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 152 | art::DexFile::Header* header = reinterpret_cast<art::DexFile::Header*>(memory_.data()); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 153 | uint32_t file_size = header->file_size_; | 
|  | 154 | if (art::CompactDexFile::IsMagicValid(header->magic_)) { | 
| David Srbecky | 417f7c3 | 2018-02-05 20:14:48 +0000 | [diff] [blame] | 155 | // Compact dex file store data section separately so that it can be shared. | 
|  | 156 | // Therefore we need to extend the read memory range to include it. | 
|  | 157 | // TODO: This might be wasteful as we might read data in between as well. | 
|  | 158 | //       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] | 159 | uint32_t computed_file_size; | 
|  | 160 | if (__builtin_add_overflow(header->data_off_, header->data_size_, &computed_file_size)) { | 
|  | 161 | return false; | 
|  | 162 | } | 
|  | 163 | if (computed_file_size > file_size) { | 
|  | 164 | file_size = computed_file_size; | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 165 | } | 
|  | 166 | } else if (!art::StandardDexFile::IsMagicValid(header->magic_)) { | 
| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 167 | return false; | 
|  | 168 | } | 
|  | 169 |  | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 170 | memory_.resize(file_size); | 
|  | 171 | if (!memory->ReadFully(dex_file_offset_in_memory, memory_.data(), memory_.size())) { | 
| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 172 | return false; | 
|  | 173 | } | 
|  | 174 |  | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 175 | header = reinterpret_cast<art::DexFile::Header*>(memory_.data()); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 176 |  | 
| Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 177 | art::DexFileLoader loader; | 
|  | 178 | std::string error_msg; | 
|  | 179 | auto dex = | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 180 | 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] | 181 | dex_file_.reset(dex.release()); | 
|  | 182 | return dex_file_ != nullptr; | 
|  | 183 | } | 
| Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 184 |  | 
|  | 185 | }  // namespace unwindstack |