blob: 118cffeda6dc7d785b0fbb7c298dd9f2c3fb22ea [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
Andreas Gampe57943812017-12-06 21:39:13 -080023#include <android-base/logging.h>
24
Nicolas Geoffray28453cf2017-08-10 15:30:26 +010025#include "base/bit_utils.h"
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +010026#include "base/stl_util.h"
Andreas Gampef7e82232016-09-12 15:55:56 -070027#include "base/unix_file/fd_file.h"
David Sehr013fd802018-01-11 22:55:24 -080028#include "dex/art_dex_file_loader.h"
David Sehr9e734c72018-01-04 17:56:19 -080029#include "dex/dex_file.h"
30#include "dex/dex_file_loader.h"
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +010031#include "dex_to_dex_decompiler.h"
Mathieu Chartier210531f2018-01-12 10:15:51 -080032#include "quicken_info.h"
David Brazdil7b49e6c2016-09-01 11:06:18 +010033
34namespace art {
35
Nicolas Geoffray36930ec2017-05-09 13:23:34 +010036constexpr uint8_t VdexFile::Header::kVdexInvalidMagic[4];
David Brazdil7b49e6c2016-09-01 11:06:18 +010037constexpr uint8_t VdexFile::Header::kVdexMagic[4];
38constexpr uint8_t VdexFile::Header::kVdexVersion[4];
39
40bool VdexFile::Header::IsMagicValid() const {
41 return (memcmp(magic_, kVdexMagic, sizeof(kVdexMagic)) == 0);
42}
43
44bool VdexFile::Header::IsVersionValid() const {
45 return (memcmp(version_, kVdexVersion, sizeof(kVdexVersion)) == 0);
46}
47
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +000048VdexFile::Header::Header(uint32_t number_of_dex_files,
49 uint32_t dex_size,
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010050 uint32_t verifier_deps_size,
51 uint32_t quickening_info_size)
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +000052 : number_of_dex_files_(number_of_dex_files),
53 dex_size_(dex_size),
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010054 verifier_deps_size_(verifier_deps_size),
55 quickening_info_size_(quickening_info_size) {
David Brazdil7b49e6c2016-09-01 11:06:18 +010056 memcpy(magic_, kVdexMagic, sizeof(kVdexMagic));
57 memcpy(version_, kVdexVersion, sizeof(kVdexVersion));
58 DCHECK(IsMagicValid());
59 DCHECK(IsVersionValid());
60}
61
David Srbeckyec2cdf42017-12-08 16:21:25 +000062std::unique_ptr<VdexFile> VdexFile::OpenAtAddress(uint8_t* mmap_addr,
63 size_t mmap_size,
64 bool mmap_reuse,
65 const std::string& vdex_filename,
66 bool writable,
67 bool low_4gb,
68 bool unquicken,
69 std::string* error_msg) {
David Brazdil7b49e6c2016-09-01 11:06:18 +010070 if (!OS::FileExists(vdex_filename.c_str())) {
71 *error_msg = "File " + vdex_filename + " does not exist.";
72 return nullptr;
73 }
74
75 std::unique_ptr<File> vdex_file;
76 if (writable) {
77 vdex_file.reset(OS::OpenFileReadWrite(vdex_filename.c_str()));
78 } else {
79 vdex_file.reset(OS::OpenFileForReading(vdex_filename.c_str()));
80 }
81 if (vdex_file == nullptr) {
82 *error_msg = "Could not open file " + vdex_filename +
83 (writable ? " for read/write" : "for reading");
84 return nullptr;
85 }
86
87 int64_t vdex_length = vdex_file->GetLength();
88 if (vdex_length == -1) {
89 *error_msg = "Could not read the length of file " + vdex_filename;
90 return nullptr;
91 }
92
David Srbeckyec2cdf42017-12-08 16:21:25 +000093 return OpenAtAddress(mmap_addr,
94 mmap_size,
95 mmap_reuse,
96 vdex_file->Fd(),
97 vdex_length,
98 vdex_filename,
99 writable,
100 low_4gb,
101 unquicken,
102 error_msg);
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000103}
104
David Srbeckyec2cdf42017-12-08 16:21:25 +0000105std::unique_ptr<VdexFile> VdexFile::OpenAtAddress(uint8_t* mmap_addr,
106 size_t mmap_size,
107 bool mmap_reuse,
108 int file_fd,
109 size_t vdex_length,
110 const std::string& vdex_filename,
111 bool writable,
112 bool low_4gb,
113 bool unquicken,
114 std::string* error_msg) {
David Srbeckyec2cdf42017-12-08 16:21:25 +0000115 if (mmap_addr != nullptr && mmap_size < vdex_length) {
116 LOG(WARNING) << "Insufficient pre-allocated space to mmap vdex.";
117 mmap_addr = nullptr;
118 mmap_reuse = false;
119 }
Andreas Gampec1fc4492018-01-10 14:19:36 -0800120 CHECK(!mmap_reuse || mmap_addr != nullptr);
David Srbeckyec2cdf42017-12-08 16:21:25 +0000121 std::unique_ptr<MemMap> mmap(MemMap::MapFileAtAddress(
122 mmap_addr,
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100123 vdex_length,
124 (writable || unquicken) ? PROT_READ | PROT_WRITE : PROT_READ,
125 unquicken ? MAP_PRIVATE : MAP_SHARED,
126 file_fd,
127 0 /* start offset */,
128 low_4gb,
David Srbeckyec2cdf42017-12-08 16:21:25 +0000129 mmap_reuse,
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100130 vdex_filename.c_str(),
131 error_msg));
David Brazdil7b49e6c2016-09-01 11:06:18 +0100132 if (mmap == nullptr) {
133 *error_msg = "Failed to mmap file " + vdex_filename + " : " + *error_msg;
134 return nullptr;
135 }
136
Richard Uhlerb8ab63a2017-01-31 11:27:37 +0000137 std::unique_ptr<VdexFile> vdex(new VdexFile(mmap.release()));
138 if (!vdex->IsValid()) {
139 *error_msg = "Vdex file is not valid";
140 return nullptr;
141 }
142
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100143 if (unquicken) {
144 std::vector<std::unique_ptr<const DexFile>> unique_ptr_dex_files;
145 if (!vdex->OpenAllDexFiles(&unique_ptr_dex_files, error_msg)) {
146 return nullptr;
147 }
Mathieu Chartier210531f2018-01-12 10:15:51 -0800148 vdex->Unquicken(MakeNonOwningPointerVector(unique_ptr_dex_files),
149 /* decompile_return_instruction */ false);
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100150 // Update the quickening info size to pretend there isn't any.
151 reinterpret_cast<Header*>(vdex->mmap_->Begin())->quickening_info_size_ = 0;
152 }
153
David Brazdil7b49e6c2016-09-01 11:06:18 +0100154 *error_msg = "Success";
Richard Uhlerb8ab63a2017-01-31 11:27:37 +0000155 return vdex;
David Brazdil7b49e6c2016-09-01 11:06:18 +0100156}
157
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000158const uint8_t* VdexFile::GetNextDexFileData(const uint8_t* cursor) const {
159 DCHECK(cursor == nullptr || (cursor > Begin() && cursor <= End()));
160 if (cursor == nullptr) {
161 // Beginning of the iteration, return the first dex file if there is one.
Mathieu Chartier210531f2018-01-12 10:15:51 -0800162 return HasDexSection() ? DexBegin() + sizeof(QuickeningTableOffsetType) : nullptr;
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000163 } else {
164 // Fetch the next dex file. Return null if there is none.
165 const uint8_t* data = cursor + reinterpret_cast<const DexFile::Header*>(cursor)->file_size_;
Nicolas Geoffray28453cf2017-08-10 15:30:26 +0100166 // Dex files are required to be 4 byte aligned. the OatWriter makes sure they are, see
167 // OatWriter::SeekToDexFiles.
168 data = AlignUp(data, 4);
Mathieu Chartier210531f2018-01-12 10:15:51 -0800169
170 return (data == DexEnd()) ? nullptr : data + sizeof(QuickeningTableOffsetType);
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000171 }
172}
173
David Sehrbeca4fe2017-03-30 17:50:24 -0700174bool VdexFile::OpenAllDexFiles(std::vector<std::unique_ptr<const DexFile>>* dex_files,
175 std::string* error_msg) {
David Sehr013fd802018-01-11 22:55:24 -0800176 const ArtDexFileLoader dex_file_loader;
David Sehrbeca4fe2017-03-30 17:50:24 -0700177 size_t i = 0;
178 for (const uint8_t* dex_file_start = GetNextDexFileData(nullptr);
179 dex_file_start != nullptr;
180 dex_file_start = GetNextDexFileData(dex_file_start), ++i) {
181 size_t size = reinterpret_cast<const DexFile::Header*>(dex_file_start)->file_size_;
182 // TODO: Supply the location information for a vdex file.
183 static constexpr char kVdexLocation[] = "";
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700184 std::string location = DexFileLoader::GetMultiDexLocation(i, kVdexLocation);
David Sehr013fd802018-01-11 22:55:24 -0800185 std::unique_ptr<const DexFile> dex(dex_file_loader.Open(dex_file_start,
186 size,
187 location,
188 GetLocationChecksum(i),
189 nullptr /*oat_dex_file*/,
190 false /*verify*/,
191 false /*verify_checksum*/,
192 error_msg));
David Sehrbeca4fe2017-03-30 17:50:24 -0700193 if (dex == nullptr) {
194 return false;
195 }
196 dex_files->push_back(std::move(dex));
197 }
198 return true;
199}
200
Mathieu Chartier210531f2018-01-12 10:15:51 -0800201void VdexFile::Unquicken(const std::vector<const DexFile*>& target_dex_files,
202 bool decompile_return_instruction) const {
203 const uint8_t* source_dex = GetNextDexFileData(nullptr);
204 for (const DexFile* target_dex : target_dex_files) {
205 UnquickenDexFile(*target_dex, source_dex, decompile_return_instruction);
206 source_dex = GetNextDexFileData(source_dex);
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100207 }
Mathieu Chartier210531f2018-01-12 10:15:51 -0800208 DCHECK(source_dex == nullptr);
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100209}
210
Mathieu Chartier210531f2018-01-12 10:15:51 -0800211uint32_t VdexFile::GetQuickeningInfoTableOffset(const uint8_t* source_dex_begin) const {
212 DCHECK_GE(source_dex_begin, DexBegin());
213 DCHECK_LT(source_dex_begin, DexEnd());
214 return reinterpret_cast<const QuickeningTableOffsetType*>(source_dex_begin)[-1];
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100215}
216
Mathieu Chartier210531f2018-01-12 10:15:51 -0800217QuickenInfoOffsetTableAccessor VdexFile::GetQuickenInfoOffsetTable(
218 const uint8_t* source_dex_begin,
219 uint32_t num_method_ids,
220 const ArrayRef<const uint8_t>& quickening_info) const {
221 // The offset a is in preheader right before the dex file.
222 const uint32_t offset = GetQuickeningInfoTableOffset(source_dex_begin);
223 const uint8_t* data_ptr = quickening_info.data() + offset;
224 return QuickenInfoOffsetTableAccessor(data_ptr, num_method_ids);
225}
Nicolas Geoffrayb4c6acb2017-11-10 12:48:14 +0000226
Mathieu Chartier210531f2018-01-12 10:15:51 -0800227QuickenInfoOffsetTableAccessor VdexFile::GetQuickenInfoOffsetTable(
228 const DexFile& dex_file,
229 const ArrayRef<const uint8_t>& quickening_info) const {
230 return GetQuickenInfoOffsetTable(dex_file.Begin(), dex_file.NumMethodIds(), quickening_info);
Nicolas Geoffrayb4c6acb2017-11-10 12:48:14 +0000231}
232
233static ArrayRef<const uint8_t> GetQuickeningInfoAt(const ArrayRef<const uint8_t>& quickening_info,
234 uint32_t quickening_offset) {
Mathieu Chartier210531f2018-01-12 10:15:51 -0800235 ArrayRef<const uint8_t> remaining = quickening_info.SubArray(quickening_offset);
236 return remaining.SubArray(0u, QuickenInfoTable::SizeInBytes(remaining));
237}
238
239static uint32_t GetQuickeningInfoOffset(const QuickenInfoOffsetTableAccessor& table,
240 uint32_t dex_method_index,
241 const ArrayRef<const uint8_t>& quickening_info) {
242 DCHECK(!quickening_info.empty());
243 uint32_t remainder;
244 uint32_t offset = table.ElementOffset(dex_method_index, &remainder);
245 // Decode the sizes for the remainder offsets (not covered by the table).
246 while (remainder != 0) {
247 offset += GetQuickeningInfoAt(quickening_info, offset).size();
248 --remainder;
249 }
250 return offset;
Nicolas Geoffrayb4c6acb2017-11-10 12:48:14 +0000251}
252
253void VdexFile::UnquickenDexFile(const DexFile& target_dex_file,
Mathieu Chartier210531f2018-01-12 10:15:51 -0800254 const DexFile& source_dex_file,
255 bool decompile_return_instruction) const {
256 UnquickenDexFile(target_dex_file, source_dex_file.Begin(), decompile_return_instruction);
257}
258
259void VdexFile::UnquickenDexFile(const DexFile& target_dex_file,
260 const uint8_t* source_dex_begin,
261 bool decompile_return_instruction) const {
262 ArrayRef<const uint8_t> quickening_info = GetQuickeningInfo();
Nicolas Geoffrayb4c6acb2017-11-10 12:48:14 +0000263 if (quickening_info.size() == 0 && !decompile_return_instruction) {
264 // Bail early if there is no quickening info and no need to decompile
265 // RETURN_VOID_NO_BARRIER instructions to RETURN_VOID instructions.
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100266 return;
267 }
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100268 for (uint32_t i = 0; i < target_dex_file.NumClassDefs(); ++i) {
269 const DexFile::ClassDef& class_def = target_dex_file.GetClassDef(i);
270 const uint8_t* class_data = target_dex_file.GetClassData(class_def);
271 if (class_data != nullptr) {
272 for (ClassDataItemIterator class_it(target_dex_file, class_data);
273 class_it.HasNext();
274 class_it.Next()) {
275 if (class_it.IsAtMethod() && class_it.GetMethodCodeItem() != nullptr) {
Nicolas Geoffrayb4c6acb2017-11-10 12:48:14 +0000276 const DexFile::CodeItem* code_item = class_it.GetMethodCodeItem();
Mathieu Chartier210531f2018-01-12 10:15:51 -0800277 ArrayRef<const uint8_t> quicken_data;
278 if (!quickening_info.empty()) {
279 const uint32_t quickening_offset = GetQuickeningInfoOffset(
280 GetQuickenInfoOffsetTable(source_dex_begin,
281 target_dex_file.NumMethodIds(),
282 quickening_info),
283 class_it.GetMemberIndex(),
284 quickening_info);
285 quicken_data = GetQuickeningInfoAt(quickening_info, quickening_offset);
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100286 }
Nicolas Geoffrayb4c6acb2017-11-10 12:48:14 +0000287 optimizer::ArtDecompileDEX(
Mathieu Chartier6238c832018-01-04 09:55:13 -0800288 target_dex_file,
Nicolas Geoffrayb4c6acb2017-11-10 12:48:14 +0000289 *code_item,
Mathieu Chartier210531f2018-01-12 10:15:51 -0800290 quicken_data,
Nicolas Geoffrayb4c6acb2017-11-10 12:48:14 +0000291 decompile_return_instruction);
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100292 }
293 }
294 }
295 }
296}
297
Mathieu Chartier210531f2018-01-12 10:15:51 -0800298ArrayRef<const uint8_t> VdexFile::GetQuickenedInfoOf(const DexFile& dex_file,
299 uint32_t dex_method_idx) const {
Nicolas Geoffrayb4c6acb2017-11-10 12:48:14 +0000300 ArrayRef<const uint8_t> quickening_info = GetQuickeningInfo();
Mathieu Chartier210531f2018-01-12 10:15:51 -0800301 if (quickening_info.empty()) {
302 return ArrayRef<const uint8_t>();
303 }
304 const uint32_t quickening_offset = GetQuickeningInfoOffset(
305 GetQuickenInfoOffsetTable(dex_file, quickening_info),
306 dex_method_idx,
307 quickening_info);
308 return GetQuickeningInfoAt(quickening_info, quickening_offset);
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100309}
310
David Brazdil7b49e6c2016-09-01 11:06:18 +0100311} // namespace art