blob: e8f947c95a7f5b33e5b77ac87ca93b5754a9618c [file] [log] [blame]
David Brazdil7b49e6c2016-09-01 11:06:18 +01001/*
2 * Copyright (C) 2016 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 "vdex_file.h"
18
Andreas Gampe0dfc3152017-04-24 07:58:06 -070019#include <sys/mman.h> // For the PROT_* and MAP_* constants.
20
David Brazdil7b49e6c2016-09-01 11:06:18 +010021#include <memory>
22
23#include "base/logging.h"
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +010024#include "base/stl_util.h"
Andreas Gampef7e82232016-09-12 15:55:56 -070025#include "base/unix_file/fd_file.h"
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +000026#include "dex_file.h"
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +010027#include "dex_to_dex_decompiler.h"
David Brazdil7b49e6c2016-09-01 11:06:18 +010028
29namespace art {
30
Nicolas Geoffray36930ec2017-05-09 13:23:34 +010031constexpr uint8_t VdexFile::Header::kVdexInvalidMagic[4];
David Brazdil7b49e6c2016-09-01 11:06:18 +010032constexpr uint8_t VdexFile::Header::kVdexMagic[4];
33constexpr uint8_t VdexFile::Header::kVdexVersion[4];
34
35bool VdexFile::Header::IsMagicValid() const {
36 return (memcmp(magic_, kVdexMagic, sizeof(kVdexMagic)) == 0);
37}
38
39bool VdexFile::Header::IsVersionValid() const {
40 return (memcmp(version_, kVdexVersion, sizeof(kVdexVersion)) == 0);
41}
42
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +000043VdexFile::Header::Header(uint32_t number_of_dex_files,
44 uint32_t dex_size,
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010045 uint32_t verifier_deps_size,
46 uint32_t quickening_info_size)
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +000047 : number_of_dex_files_(number_of_dex_files),
48 dex_size_(dex_size),
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010049 verifier_deps_size_(verifier_deps_size),
50 quickening_info_size_(quickening_info_size) {
David Brazdil7b49e6c2016-09-01 11:06:18 +010051 memcpy(magic_, kVdexMagic, sizeof(kVdexMagic));
52 memcpy(version_, kVdexVersion, sizeof(kVdexVersion));
53 DCHECK(IsMagicValid());
54 DCHECK(IsVersionValid());
55}
56
Richard Uhlerb8ab63a2017-01-31 11:27:37 +000057std::unique_ptr<VdexFile> VdexFile::Open(const std::string& vdex_filename,
58 bool writable,
59 bool low_4gb,
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +010060 bool unquicken,
Richard Uhlerb8ab63a2017-01-31 11:27:37 +000061 std::string* error_msg) {
David Brazdil7b49e6c2016-09-01 11:06:18 +010062 if (!OS::FileExists(vdex_filename.c_str())) {
63 *error_msg = "File " + vdex_filename + " does not exist.";
64 return nullptr;
65 }
66
67 std::unique_ptr<File> vdex_file;
68 if (writable) {
69 vdex_file.reset(OS::OpenFileReadWrite(vdex_filename.c_str()));
70 } else {
71 vdex_file.reset(OS::OpenFileForReading(vdex_filename.c_str()));
72 }
73 if (vdex_file == nullptr) {
74 *error_msg = "Could not open file " + vdex_filename +
75 (writable ? " for read/write" : "for reading");
76 return nullptr;
77 }
78
79 int64_t vdex_length = vdex_file->GetLength();
80 if (vdex_length == -1) {
81 *error_msg = "Could not read the length of file " + vdex_filename;
82 return nullptr;
83 }
84
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +010085 return Open(vdex_file->Fd(), vdex_length, vdex_filename, writable, low_4gb, unquicken, error_msg);
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +000086}
87
Richard Uhlerb8ab63a2017-01-31 11:27:37 +000088std::unique_ptr<VdexFile> VdexFile::Open(int file_fd,
89 size_t vdex_length,
90 const std::string& vdex_filename,
91 bool writable,
92 bool low_4gb,
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +010093 bool unquicken,
Richard Uhlerb8ab63a2017-01-31 11:27:37 +000094 std::string* error_msg) {
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +010095 std::unique_ptr<MemMap> mmap(MemMap::MapFile(
96 vdex_length,
97 (writable || unquicken) ? PROT_READ | PROT_WRITE : PROT_READ,
98 unquicken ? MAP_PRIVATE : MAP_SHARED,
99 file_fd,
100 0 /* start offset */,
101 low_4gb,
102 vdex_filename.c_str(),
103 error_msg));
David Brazdil7b49e6c2016-09-01 11:06:18 +0100104 if (mmap == nullptr) {
105 *error_msg = "Failed to mmap file " + vdex_filename + " : " + *error_msg;
106 return nullptr;
107 }
108
Richard Uhlerb8ab63a2017-01-31 11:27:37 +0000109 std::unique_ptr<VdexFile> vdex(new VdexFile(mmap.release()));
110 if (!vdex->IsValid()) {
111 *error_msg = "Vdex file is not valid";
112 return nullptr;
113 }
114
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100115 if (unquicken) {
116 std::vector<std::unique_ptr<const DexFile>> unique_ptr_dex_files;
117 if (!vdex->OpenAllDexFiles(&unique_ptr_dex_files, error_msg)) {
118 return nullptr;
119 }
120 Unquicken(MakeNonOwningPointerVector(unique_ptr_dex_files), vdex->GetQuickeningInfo());
121 // Update the quickening info size to pretend there isn't any.
122 reinterpret_cast<Header*>(vdex->mmap_->Begin())->quickening_info_size_ = 0;
123 }
124
David Brazdil7b49e6c2016-09-01 11:06:18 +0100125 *error_msg = "Success";
Richard Uhlerb8ab63a2017-01-31 11:27:37 +0000126 return vdex;
David Brazdil7b49e6c2016-09-01 11:06:18 +0100127}
128
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000129const uint8_t* VdexFile::GetNextDexFileData(const uint8_t* cursor) const {
130 DCHECK(cursor == nullptr || (cursor > Begin() && cursor <= End()));
131 if (cursor == nullptr) {
132 // Beginning of the iteration, return the first dex file if there is one.
133 return HasDexSection() ? DexBegin() : nullptr;
134 } else {
135 // Fetch the next dex file. Return null if there is none.
136 const uint8_t* data = cursor + reinterpret_cast<const DexFile::Header*>(cursor)->file_size_;
137 return (data == DexEnd()) ? nullptr : data;
138 }
139}
140
David Sehrbeca4fe2017-03-30 17:50:24 -0700141bool VdexFile::OpenAllDexFiles(std::vector<std::unique_ptr<const DexFile>>* dex_files,
142 std::string* error_msg) {
143 size_t i = 0;
144 for (const uint8_t* dex_file_start = GetNextDexFileData(nullptr);
145 dex_file_start != nullptr;
146 dex_file_start = GetNextDexFileData(dex_file_start), ++i) {
147 size_t size = reinterpret_cast<const DexFile::Header*>(dex_file_start)->file_size_;
148 // TODO: Supply the location information for a vdex file.
149 static constexpr char kVdexLocation[] = "";
150 std::string location = DexFile::GetMultiDexLocation(i, kVdexLocation);
151 std::unique_ptr<const DexFile> dex(DexFile::Open(dex_file_start,
152 size,
153 location,
154 GetLocationChecksum(i),
155 nullptr /*oat_dex_file*/,
156 false /*verify*/,
157 false /*verify_checksum*/,
158 error_msg));
159 if (dex == nullptr) {
160 return false;
161 }
162 dex_files->push_back(std::move(dex));
163 }
164 return true;
165}
166
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100167// Utility class to easily iterate over the quickening data.
168class QuickeningInfoIterator {
169 public:
170 QuickeningInfoIterator(uint32_t dex_file_index,
171 uint32_t number_of_dex_files,
172 const ArrayRef<const uint8_t>& quickening_info)
173 : quickening_info_(quickening_info) {
174 const unaligned_uint32_t* dex_file_indices = reinterpret_cast<const unaligned_uint32_t*>(
175 quickening_info.data() +
176 quickening_info.size() -
177 number_of_dex_files * sizeof(uint32_t));
178 current_code_item_end_ = (dex_file_index == number_of_dex_files - 1)
179 ? dex_file_indices
180 : reinterpret_cast<const unaligned_uint32_t*>(
181 quickening_info_.data() + dex_file_indices[dex_file_index + 1]);
182 current_code_item_ptr_ = reinterpret_cast<const uint32_t*>(
183 quickening_info_.data() + dex_file_indices[dex_file_index]);
184 }
185
186 bool Done() const {
187 return current_code_item_ptr_ == current_code_item_end_;
188 }
189
190 void Advance() {
191 current_code_item_ptr_ += 2;
192 }
193
194 uint32_t GetCurrentCodeItemOffset() const {
195 return current_code_item_ptr_[0];
196 }
197
198 const ArrayRef<const uint8_t> GetCurrentQuickeningInfo() const {
199 return ArrayRef<const uint8_t>(
200 // Add sizeof(uint32_t) to remove the length from the data pointer.
201 quickening_info_.data() + current_code_item_ptr_[1] + sizeof(uint32_t),
202 *reinterpret_cast<const unaligned_uint32_t*>(
203 quickening_info_.data() + current_code_item_ptr_[1]));
204 }
205
206 private:
207 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
208 const ArrayRef<const uint8_t>& quickening_info_;
209 const unaligned_uint32_t* current_code_item_ptr_;
210 const unaligned_uint32_t* current_code_item_end_;
211
212 DISALLOW_COPY_AND_ASSIGN(QuickeningInfoIterator);
213};
214
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100215void VdexFile::Unquicken(const std::vector<const DexFile*>& dex_files,
216 const ArrayRef<const uint8_t>& quickening_info) {
217 if (quickening_info.size() == 0) {
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100218 // Bail early if there is no quickening info.
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100219 return;
220 }
Nicolas Geoffray1cfea7a2017-05-24 14:44:38 +0100221 // We do not decompile a RETURN_VOID_NO_BARRIER into a RETURN_VOID, as the quickening
222 // optimization does not depend on the boot image (the optimization relies on not
223 // having final fields in a class, which does not change for an app).
224 constexpr bool kDecompileReturnInstruction = false;
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100225 for (uint32_t i = 0; i < dex_files.size(); ++i) {
226 for (QuickeningInfoIterator it(i, dex_files.size(), quickening_info);
227 !it.Done();
228 it.Advance()) {
229 optimizer::ArtDecompileDEX(
230 *dex_files[i]->GetCodeItem(it.GetCurrentCodeItemOffset()),
231 it.GetCurrentQuickeningInfo(),
232 kDecompileReturnInstruction);
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100233 }
234 }
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100235}
236
237static constexpr uint32_t kNoDexFile = -1;
238
239uint32_t VdexFile::GetDexFileIndex(const DexFile& dex_file) const {
240 uint32_t dex_index = 0;
241 for (const uint8_t* dex_file_start = GetNextDexFileData(nullptr);
242 dex_file_start != dex_file.Begin();
243 dex_file_start = GetNextDexFileData(dex_file_start)) {
244 if (dex_file_start == nullptr) {
245 return kNoDexFile;
246 }
247 dex_index++;
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100248 }
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100249 return dex_index;
250}
251
252void VdexFile::FullyUnquickenDexFile(const DexFile& target_dex_file,
253 const DexFile& original_dex_file) const {
254 uint32_t dex_index = GetDexFileIndex(original_dex_file);
255 if (dex_index == kNoDexFile) {
256 return;
257 }
258
259 constexpr bool kDecompileReturnInstruction = true;
260 QuickeningInfoIterator it(dex_index, GetHeader().GetNumberOfDexFiles(), GetQuickeningInfo());
261 // Iterate over the class definitions. Even if there is no quickening info,
262 // we want to unquicken RETURN_VOID_NO_BARRIER instruction.
263 for (uint32_t i = 0; i < target_dex_file.NumClassDefs(); ++i) {
264 const DexFile::ClassDef& class_def = target_dex_file.GetClassDef(i);
265 const uint8_t* class_data = target_dex_file.GetClassData(class_def);
266 if (class_data != nullptr) {
267 for (ClassDataItemIterator class_it(target_dex_file, class_data);
268 class_it.HasNext();
269 class_it.Next()) {
270 if (class_it.IsAtMethod() && class_it.GetMethodCodeItem() != nullptr) {
271 uint32_t offset = class_it.GetMethodCodeItemOffset();
272 if (!it.Done() && offset == it.GetCurrentCodeItemOffset()) {
273 optimizer::ArtDecompileDEX(
274 *class_it.GetMethodCodeItem(),
275 it.GetCurrentQuickeningInfo(),
276 kDecompileReturnInstruction);
277 it.Advance();
278 } else {
279 optimizer::ArtDecompileDEX(*class_it.GetMethodCodeItem(),
280 ArrayRef<const uint8_t>(nullptr, 0),
281 kDecompileReturnInstruction);
282 }
283 }
284 }
285 }
286 }
287}
288
289const uint8_t* VdexFile::GetQuickenedInfoOf(const DexFile& dex_file,
290 uint32_t code_item_offset) const {
291 if (GetQuickeningInfo().size() == 0) {
292 // Bail early if there is no quickening info.
293 return nullptr;
294 }
295
296 uint32_t dex_index = GetDexFileIndex(dex_file);
297 if (dex_index == kNoDexFile) {
298 return nullptr;
299 }
300
301 for (QuickeningInfoIterator it(dex_index, GetHeader().GetNumberOfDexFiles(), GetQuickeningInfo());
302 !it.Done();
303 it.Advance()) {
304 if (code_item_offset == it.GetCurrentCodeItemOffset()) {
305 return it.GetCurrentQuickeningInfo().data();
306 }
307 }
308 return nullptr;
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100309}
310
David Brazdil7b49e6c2016-09-01 11:06:18 +0100311} // namespace art