blob: 5780fbb9cce93ad7d2429a623284114e82f86ac0 [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
36#include "UnwindDexFile.h"
37
38UnwindDexFile* UnwindDexFile::Create(uint64_t dex_file_offset_in_memory,
39 unwindstack::Memory* memory, unwindstack::MapInfo* info) {
40 if (!info->name.empty()) {
41 std::unique_ptr<UnwindDexFileFromFile> dex_file(new UnwindDexFileFromFile);
42 if (dex_file->Open(dex_file_offset_in_memory - info->start + info->offset, info->name)) {
43 return dex_file.release();
44 }
45 }
46
47 std::unique_ptr<UnwindDexFileFromMemory> dex_file(new UnwindDexFileFromMemory);
48 if (dex_file->Open(dex_file_offset_in_memory, memory)) {
49 return dex_file.release();
50 }
51 return nullptr;
52}
53
54void UnwindDexFile::GetMethodInformation(uint64_t dex_offset, std::string* method_name,
55 uint64_t* method_offset) {
56 if (dex_file_ == nullptr) {
57 return;
58 }
59
60 for (uint32_t i = 0; i < dex_file_->NumClassDefs(); ++i) {
61 const art::DexFile::ClassDef& class_def = dex_file_->GetClassDef(i);
62 const uint8_t* class_data = dex_file_->GetClassData(class_def);
63 if (class_data == nullptr) {
64 continue;
65 }
66 for (art::ClassDataItemIterator it(*dex_file_.get(), class_data); it.HasNext(); it.Next()) {
67 if (!it.IsAtMethod()) {
68 continue;
69 }
70 const art::DexFile::CodeItem* code_item = it.GetMethodCodeItem();
71 if (code_item == nullptr) {
72 continue;
73 }
74 art::CodeItemInstructionAccessor code(*dex_file_.get(), code_item);
75 if (!code.HasCodeItem()) {
76 continue;
77 }
78
79 uint64_t offset = reinterpret_cast<const uint8_t*>(code.Insns()) - dex_file_->Begin();
80 size_t size = code.InsnsSizeInCodeUnits() * sizeof(uint16_t);
81 if (offset <= dex_offset && dex_offset < offset + size) {
82 *method_name = dex_file_->PrettyMethod(it.GetMemberIndex(), false);
83 *method_offset = dex_offset - offset;
84 return;
85 }
86 }
87 }
88}
89
90UnwindDexFileFromFile::~UnwindDexFileFromFile() {
91 if (size_ != 0) {
92 munmap(mapped_memory_, size_);
93 }
94}
95
96bool UnwindDexFileFromFile::Open(uint64_t dex_file_offset_in_file, const std::string& file) {
97 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(file.c_str(), O_RDONLY | O_CLOEXEC)));
98 if (fd == -1) {
99 return false;
100 }
101 struct stat buf;
102 if (fstat(fd, &buf) == -1) {
103 return false;
104 }
105 uint64_t length;
106 if (buf.st_size < 0 ||
107 __builtin_add_overflow(dex_file_offset_in_file, sizeof(art::DexFile::Header), &length) ||
108 static_cast<uint64_t>(buf.st_size) < length) {
109 return false;
110 }
111
112 mapped_memory_ = mmap(nullptr, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
113 if (mapped_memory_ == MAP_FAILED) {
114 return false;
115 }
116 size_ = buf.st_size;
117
118 uint8_t* memory = reinterpret_cast<uint8_t*>(mapped_memory_);
119
120 art::DexFile::Header* header =
121 reinterpret_cast<art::DexFile::Header*>(&memory[dex_file_offset_in_file]);
122 if (!art::StandardDexFile::IsMagicValid(header->magic_) &&
123 !art::CompactDexFile::IsMagicValid(header->magic_)) {
124 return false;
125 }
126
127 if (__builtin_add_overflow(dex_file_offset_in_file, header->file_size_, &length) ||
128 static_cast<uint64_t>(buf.st_size) < length) {
129 return false;
130 }
131
132 art::DexFileLoader loader;
133 std::string error_msg;
134 auto dex = loader.Open(&memory[dex_file_offset_in_file], header->file_size_, "", 0, nullptr,
135 false, false, &error_msg);
136 dex_file_.reset(dex.release());
137 return dex_file_ != nullptr;
138}
139
140bool UnwindDexFileFromMemory::Open(uint64_t dex_file_offset_in_memory, unwindstack::Memory* memory) {
141 art::DexFile::Header header;
142 if (!memory->ReadFully(dex_file_offset_in_memory, &header, sizeof(header))) {
143 return false;
144 }
145
146 if (!art::StandardDexFile::IsMagicValid(header.magic_) &&
147 !art::CompactDexFile::IsMagicValid(header.magic_)) {
148 return false;
149 }
150
151 memory_.resize(header.file_size_);
152 if (!memory->ReadFully(dex_file_offset_in_memory, memory_.data(), header.file_size_)) {
153 return false;
154 }
155
156 art::DexFileLoader loader;
157 std::string error_msg;
158 auto dex =
159 loader.Open(memory_.data(), header.file_size_, "", 0, nullptr, false, false, &error_msg);
160 dex_file_.reset(dex.release());
161 return dex_file_ != nullptr;
162}