blob: 3d982f6e92cc8cee134252bdeb77261caff50719 [file] [log] [blame]
Christopher Ferris0b06a592018-01-19 10:26:36 -08001/*
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
David Sehr892e6752018-02-07 15:19:22 -080027#include <dex/code_item_accessors-inl.h>
Christopher Ferris0b06a592018-01-19 10:26:36 -080028#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 Ferrisd70ea5e2018-01-30 19:47:24 -080036#include "DexFile.h"
Christopher Ferris0b06a592018-01-19 10:26:36 -080037
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -080038namespace unwindstack {
39
40DexFile* DexFile::Create(uint64_t dex_file_offset_in_memory, Memory* memory, MapInfo* info) {
Christopher Ferris0b06a592018-01-19 10:26:36 -080041 if (!info->name.empty()) {
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -080042 std::unique_ptr<DexFileFromFile> dex_file(new DexFileFromFile);
Christopher Ferris0b06a592018-01-19 10:26:36 -080043 if (dex_file->Open(dex_file_offset_in_memory - info->start + info->offset, info->name)) {
44 return dex_file.release();
45 }
46 }
47
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -080048 std::unique_ptr<DexFileFromMemory> dex_file(new DexFileFromMemory);
Christopher Ferris0b06a592018-01-19 10:26:36 -080049 if (dex_file->Open(dex_file_offset_in_memory, memory)) {
50 return dex_file.release();
51 }
52 return nullptr;
53}
54
Christopher Ferris7747b602018-01-31 19:05:19 -080055DexFileFromFile::~DexFileFromFile() {
56 if (size_ != 0) {
57 munmap(mapped_memory_, size_);
58 }
59}
60
61bool DexFile::GetMethodInformation(uint64_t dex_offset, std::string* method_name,
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -080062 uint64_t* method_offset) {
Christopher Ferris0b06a592018-01-19 10:26:36 -080063 if (dex_file_ == nullptr) {
Christopher Ferris7747b602018-01-31 19:05:19 -080064 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 Ferris0b06a592018-01-19 10:26:36 -080069 }
70
David Srbecky02d0f792018-03-24 00:29:14 +000071 if (dex_file_->IsCompactDexFile()) {
72 // The data section of compact dex files might be shared.
73 // Check the subrange unique to this compact dex.
74 const auto& cdex_header = dex_file_->AsCompactDexFile()->GetHeader();
75 uint32_t begin = cdex_header.data_off_ + cdex_header.OwnedDataBegin();
76 uint32_t end = cdex_header.data_off_ + cdex_header.OwnedDataEnd();
77 if (dex_offset < begin || dex_offset >= end) {
78 return false; // The DEX offset is not within the bytecode of this dex file.
79 }
80 }
81
82 // The method data is cached in a std::map indexed by method end offset and
83 // contains the start offset and the method member index.
84 // Only cache the method data as it is searched. Do not read the entire
85 // set of method data into the cache at once.
86 // This is done because many unwinds only find a single frame with dex file
87 // info, so reading the entire method data is wasteful. However, still cache
88 // the data so that anything doing multiple unwinds will have this data
89 // cached for future use.
90
91 // First look in the method cache.
92 auto entry = method_cache_.upper_bound(dex_offset);
93 if (entry != method_cache_.end() && dex_offset >= entry->second.first) {
94 *method_name = dex_file_->PrettyMethod(entry->second.second, false);
95 *method_offset = dex_offset - entry->second.first;
96 return true;
97 }
98
99 // Check the methods we haven't cached.
100 for (; class_def_index_ < dex_file_->NumClassDefs(); class_def_index_++) {
101 const art::DexFile::ClassDef& class_def = dex_file_->GetClassDef(class_def_index_);
Christopher Ferris0b06a592018-01-19 10:26:36 -0800102 const uint8_t* class_data = dex_file_->GetClassData(class_def);
103 if (class_data == nullptr) {
104 continue;
105 }
David Srbecky02d0f792018-03-24 00:29:14 +0000106
107 if (class_it_.get() == nullptr || !class_it_->HasNext()) {
108 class_it_.reset(new art::ClassDataItemIterator(*dex_file_.get(), class_data));
109 }
110
111 for (; class_it_->HasNext(); class_it_->Next()) {
112 if (!class_it_->IsAtMethod()) {
Christopher Ferris0b06a592018-01-19 10:26:36 -0800113 continue;
114 }
David Srbecky02d0f792018-03-24 00:29:14 +0000115 const art::DexFile::CodeItem* code_item = class_it_->GetMethodCodeItem();
Christopher Ferris0b06a592018-01-19 10:26:36 -0800116 if (code_item == nullptr) {
117 continue;
118 }
119 art::CodeItemInstructionAccessor code(*dex_file_.get(), code_item);
120 if (!code.HasCodeItem()) {
121 continue;
122 }
123
David Srbecky02d0f792018-03-24 00:29:14 +0000124 uint32_t offset = reinterpret_cast<const uint8_t*>(code.Insns()) - dex_file_->Begin();
125 uint32_t offset_end = offset + code.InsnsSizeInCodeUnits() * sizeof(uint16_t);
126 uint32_t member_index = class_it_->GetMemberIndex();
127 method_cache_[offset_end] = std::make_pair(offset, member_index);
128 if (offset <= dex_offset && dex_offset < offset_end) {
129 *method_name = dex_file_->PrettyMethod(member_index, false);
Christopher Ferris0b06a592018-01-19 10:26:36 -0800130 *method_offset = dex_offset - offset;
David Srbecky02d0f792018-03-24 00:29:14 +0000131 // Move past this element.
132 class_it_->Next();
Christopher Ferris7747b602018-01-31 19:05:19 -0800133 return true;
Christopher Ferris0b06a592018-01-19 10:26:36 -0800134 }
135 }
136 }
Christopher Ferris7747b602018-01-31 19:05:19 -0800137 return false;
Christopher Ferris0b06a592018-01-19 10:26:36 -0800138}
139
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800140bool DexFileFromFile::Open(uint64_t dex_file_offset_in_file, const std::string& file) {
Christopher Ferris0b06a592018-01-19 10:26:36 -0800141 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(file.c_str(), O_RDONLY | O_CLOEXEC)));
142 if (fd == -1) {
143 return false;
144 }
145 struct stat buf;
146 if (fstat(fd, &buf) == -1) {
147 return false;
148 }
149 uint64_t length;
150 if (buf.st_size < 0 ||
151 __builtin_add_overflow(dex_file_offset_in_file, sizeof(art::DexFile::Header), &length) ||
152 static_cast<uint64_t>(buf.st_size) < length) {
153 return false;
154 }
155
156 mapped_memory_ = mmap(nullptr, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
157 if (mapped_memory_ == MAP_FAILED) {
158 return false;
159 }
160 size_ = buf.st_size;
161
162 uint8_t* memory = reinterpret_cast<uint8_t*>(mapped_memory_);
163
164 art::DexFile::Header* header =
165 reinterpret_cast<art::DexFile::Header*>(&memory[dex_file_offset_in_file]);
166 if (!art::StandardDexFile::IsMagicValid(header->magic_) &&
167 !art::CompactDexFile::IsMagicValid(header->magic_)) {
168 return false;
169 }
170
171 if (__builtin_add_overflow(dex_file_offset_in_file, header->file_size_, &length) ||
172 static_cast<uint64_t>(buf.st_size) < length) {
173 return false;
174 }
175
176 art::DexFileLoader loader;
177 std::string error_msg;
178 auto dex = loader.Open(&memory[dex_file_offset_in_file], header->file_size_, "", 0, nullptr,
179 false, false, &error_msg);
180 dex_file_.reset(dex.release());
181 return dex_file_ != nullptr;
182}
183
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800184bool DexFileFromMemory::Open(uint64_t dex_file_offset_in_memory, Memory* memory) {
Christopher Ferris7747b602018-01-31 19:05:19 -0800185 memory_.resize(sizeof(art::DexFile::Header));
186 if (!memory->ReadFully(dex_file_offset_in_memory, memory_.data(), memory_.size())) {
Christopher Ferris0b06a592018-01-19 10:26:36 -0800187 return false;
188 }
189
Christopher Ferris7747b602018-01-31 19:05:19 -0800190 art::DexFile::Header* header = reinterpret_cast<art::DexFile::Header*>(memory_.data());
Christopher Ferris7747b602018-01-31 19:05:19 -0800191 uint32_t file_size = header->file_size_;
192 if (art::CompactDexFile::IsMagicValid(header->magic_)) {
David Srbecky417f7c32018-02-05 20:14:48 +0000193 // Compact dex file store data section separately so that it can be shared.
194 // Therefore we need to extend the read memory range to include it.
195 // TODO: This might be wasteful as we might read data in between as well.
196 // In practice, this should be fine, as such sharing only happens on disk.
Christopher Ferris7747b602018-01-31 19:05:19 -0800197 uint32_t computed_file_size;
198 if (__builtin_add_overflow(header->data_off_, header->data_size_, &computed_file_size)) {
199 return false;
200 }
201 if (computed_file_size > file_size) {
202 file_size = computed_file_size;
Christopher Ferris7747b602018-01-31 19:05:19 -0800203 }
204 } else if (!art::StandardDexFile::IsMagicValid(header->magic_)) {
Christopher Ferris0b06a592018-01-19 10:26:36 -0800205 return false;
206 }
207
Christopher Ferris7747b602018-01-31 19:05:19 -0800208 memory_.resize(file_size);
209 if (!memory->ReadFully(dex_file_offset_in_memory, memory_.data(), memory_.size())) {
Christopher Ferris0b06a592018-01-19 10:26:36 -0800210 return false;
211 }
212
Christopher Ferris7747b602018-01-31 19:05:19 -0800213 header = reinterpret_cast<art::DexFile::Header*>(memory_.data());
Christopher Ferris7747b602018-01-31 19:05:19 -0800214
Christopher Ferris0b06a592018-01-19 10:26:36 -0800215 art::DexFileLoader loader;
216 std::string error_msg;
217 auto dex =
Christopher Ferris7747b602018-01-31 19:05:19 -0800218 loader.Open(memory_.data(), header->file_size_, "", 0, nullptr, false, false, &error_msg);
Christopher Ferris0b06a592018-01-19 10:26:36 -0800219 dex_file_.reset(dex.release());
220 return dex_file_ != nullptr;
221}
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800222
223} // namespace unwindstack