blob: be6c2f71efa2424bbea4b3a3deaf6672d555d451 [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
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 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 Ferrisd70ea5e2018-01-30 19:47:24 -080055void DexFile::GetMethodInformation(uint64_t dex_offset, std::string* method_name,
56 uint64_t* method_offset) {
Christopher Ferris0b06a592018-01-19 10:26:36 -080057 if (dex_file_ == nullptr) {
58 return;
59 }
60
61 for (uint32_t i = 0; i < dex_file_->NumClassDefs(); ++i) {
62 const art::DexFile::ClassDef& class_def = dex_file_->GetClassDef(i);
63 const uint8_t* class_data = dex_file_->GetClassData(class_def);
64 if (class_data == nullptr) {
65 continue;
66 }
67 for (art::ClassDataItemIterator it(*dex_file_.get(), class_data); it.HasNext(); it.Next()) {
68 if (!it.IsAtMethod()) {
69 continue;
70 }
71 const art::DexFile::CodeItem* code_item = it.GetMethodCodeItem();
72 if (code_item == nullptr) {
73 continue;
74 }
75 art::CodeItemInstructionAccessor code(*dex_file_.get(), code_item);
76 if (!code.HasCodeItem()) {
77 continue;
78 }
79
80 uint64_t offset = reinterpret_cast<const uint8_t*>(code.Insns()) - dex_file_->Begin();
81 size_t size = code.InsnsSizeInCodeUnits() * sizeof(uint16_t);
82 if (offset <= dex_offset && dex_offset < offset + size) {
83 *method_name = dex_file_->PrettyMethod(it.GetMemberIndex(), false);
84 *method_offset = dex_offset - offset;
85 return;
86 }
87 }
88 }
89}
90
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -080091DexFileFromFile::~DexFileFromFile() {
Christopher Ferris0b06a592018-01-19 10:26:36 -080092 if (size_ != 0) {
93 munmap(mapped_memory_, size_);
94 }
95}
96
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -080097bool DexFileFromFile::Open(uint64_t dex_file_offset_in_file, const std::string& file) {
Christopher Ferris0b06a592018-01-19 10:26:36 -080098 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(file.c_str(), O_RDONLY | O_CLOEXEC)));
99 if (fd == -1) {
100 return false;
101 }
102 struct stat buf;
103 if (fstat(fd, &buf) == -1) {
104 return false;
105 }
106 uint64_t length;
107 if (buf.st_size < 0 ||
108 __builtin_add_overflow(dex_file_offset_in_file, sizeof(art::DexFile::Header), &length) ||
109 static_cast<uint64_t>(buf.st_size) < length) {
110 return false;
111 }
112
113 mapped_memory_ = mmap(nullptr, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
114 if (mapped_memory_ == MAP_FAILED) {
115 return false;
116 }
117 size_ = buf.st_size;
118
119 uint8_t* memory = reinterpret_cast<uint8_t*>(mapped_memory_);
120
121 art::DexFile::Header* header =
122 reinterpret_cast<art::DexFile::Header*>(&memory[dex_file_offset_in_file]);
123 if (!art::StandardDexFile::IsMagicValid(header->magic_) &&
124 !art::CompactDexFile::IsMagicValid(header->magic_)) {
125 return false;
126 }
127
128 if (__builtin_add_overflow(dex_file_offset_in_file, header->file_size_, &length) ||
129 static_cast<uint64_t>(buf.st_size) < length) {
130 return false;
131 }
132
133 art::DexFileLoader loader;
134 std::string error_msg;
135 auto dex = loader.Open(&memory[dex_file_offset_in_file], header->file_size_, "", 0, nullptr,
136 false, false, &error_msg);
137 dex_file_.reset(dex.release());
138 return dex_file_ != nullptr;
139}
140
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800141bool DexFileFromMemory::Open(uint64_t dex_file_offset_in_memory, Memory* memory) {
Christopher Ferris0b06a592018-01-19 10:26:36 -0800142 art::DexFile::Header header;
143 if (!memory->ReadFully(dex_file_offset_in_memory, &header, sizeof(header))) {
144 return false;
145 }
146
147 if (!art::StandardDexFile::IsMagicValid(header.magic_) &&
148 !art::CompactDexFile::IsMagicValid(header.magic_)) {
149 return false;
150 }
151
152 memory_.resize(header.file_size_);
153 if (!memory->ReadFully(dex_file_offset_in_memory, memory_.data(), header.file_size_)) {
154 return false;
155 }
156
157 art::DexFileLoader loader;
158 std::string error_msg;
159 auto dex =
160 loader.Open(memory_.data(), header.file_size_, "", 0, nullptr, false, false, &error_msg);
161 dex_file_.reset(dex.release());
162 return dex_file_ != nullptr;
163}
Christopher Ferrisd70ea5e2018-01-30 19:47:24 -0800164
165} // namespace unwindstack