blob: 34b9fcc8a4e0c8de6a376d16653b0cd00bd28d3a [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>
Mathieu Chartiera79efdb2018-01-18 16:31:01 -080022#include <unordered_set>
David Brazdil7b49e6c2016-09-01 11:06:18 +010023
Andreas Gampe57943812017-12-06 21:39:13 -080024#include <android-base/logging.h>
25
Nicolas Geoffray28453cf2017-08-10 15:30:26 +010026#include "base/bit_utils.h"
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +010027#include "base/stl_util.h"
Andreas Gampef7e82232016-09-12 15:55:56 -070028#include "base/unix_file/fd_file.h"
David Sehr013fd802018-01-11 22:55:24 -080029#include "dex/art_dex_file_loader.h"
David Sehr9e734c72018-01-04 17:56:19 -080030#include "dex/dex_file.h"
31#include "dex/dex_file_loader.h"
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +010032#include "dex_to_dex_decompiler.h"
Alex Light1a824a52018-01-26 15:45:30 -080033#include "hidden_api_access_flags.h"
34#include "leb128.h"
Mathieu Chartier210531f2018-01-12 10:15:51 -080035#include "quicken_info.h"
David Brazdil7b49e6c2016-09-01 11:06:18 +010036
37namespace art {
38
Nicolas Geoffray36930ec2017-05-09 13:23:34 +010039constexpr uint8_t VdexFile::Header::kVdexInvalidMagic[4];
David Brazdil7b49e6c2016-09-01 11:06:18 +010040constexpr uint8_t VdexFile::Header::kVdexMagic[4];
41constexpr uint8_t VdexFile::Header::kVdexVersion[4];
42
43bool VdexFile::Header::IsMagicValid() const {
44 return (memcmp(magic_, kVdexMagic, sizeof(kVdexMagic)) == 0);
45}
46
47bool VdexFile::Header::IsVersionValid() const {
48 return (memcmp(version_, kVdexVersion, sizeof(kVdexVersion)) == 0);
49}
50
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +000051VdexFile::Header::Header(uint32_t number_of_dex_files,
52 uint32_t dex_size,
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -080053 uint32_t dex_shared_data_size,
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010054 uint32_t verifier_deps_size,
55 uint32_t quickening_info_size)
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +000056 : number_of_dex_files_(number_of_dex_files),
57 dex_size_(dex_size),
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -080058 dex_shared_data_size_(dex_shared_data_size),
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010059 verifier_deps_size_(verifier_deps_size),
60 quickening_info_size_(quickening_info_size) {
David Brazdil7b49e6c2016-09-01 11:06:18 +010061 memcpy(magic_, kVdexMagic, sizeof(kVdexMagic));
62 memcpy(version_, kVdexVersion, sizeof(kVdexVersion));
63 DCHECK(IsMagicValid());
64 DCHECK(IsVersionValid());
65}
66
David Srbeckyec2cdf42017-12-08 16:21:25 +000067std::unique_ptr<VdexFile> VdexFile::OpenAtAddress(uint8_t* mmap_addr,
68 size_t mmap_size,
69 bool mmap_reuse,
70 const std::string& vdex_filename,
71 bool writable,
72 bool low_4gb,
73 bool unquicken,
74 std::string* error_msg) {
David Brazdil7b49e6c2016-09-01 11:06:18 +010075 if (!OS::FileExists(vdex_filename.c_str())) {
76 *error_msg = "File " + vdex_filename + " does not exist.";
77 return nullptr;
78 }
79
80 std::unique_ptr<File> vdex_file;
81 if (writable) {
82 vdex_file.reset(OS::OpenFileReadWrite(vdex_filename.c_str()));
83 } else {
84 vdex_file.reset(OS::OpenFileForReading(vdex_filename.c_str()));
85 }
86 if (vdex_file == nullptr) {
87 *error_msg = "Could not open file " + vdex_filename +
88 (writable ? " for read/write" : "for reading");
89 return nullptr;
90 }
91
92 int64_t vdex_length = vdex_file->GetLength();
93 if (vdex_length == -1) {
94 *error_msg = "Could not read the length of file " + vdex_filename;
95 return nullptr;
96 }
97
David Srbeckyec2cdf42017-12-08 16:21:25 +000098 return OpenAtAddress(mmap_addr,
99 mmap_size,
100 mmap_reuse,
101 vdex_file->Fd(),
102 vdex_length,
103 vdex_filename,
104 writable,
105 low_4gb,
106 unquicken,
107 error_msg);
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000108}
109
David Srbeckyec2cdf42017-12-08 16:21:25 +0000110std::unique_ptr<VdexFile> VdexFile::OpenAtAddress(uint8_t* mmap_addr,
111 size_t mmap_size,
112 bool mmap_reuse,
113 int file_fd,
114 size_t vdex_length,
115 const std::string& vdex_filename,
116 bool writable,
117 bool low_4gb,
118 bool unquicken,
119 std::string* error_msg) {
David Srbeckyec2cdf42017-12-08 16:21:25 +0000120 if (mmap_addr != nullptr && mmap_size < vdex_length) {
121 LOG(WARNING) << "Insufficient pre-allocated space to mmap vdex.";
122 mmap_addr = nullptr;
123 mmap_reuse = false;
124 }
Andreas Gampec1fc4492018-01-10 14:19:36 -0800125 CHECK(!mmap_reuse || mmap_addr != nullptr);
David Srbeckyec2cdf42017-12-08 16:21:25 +0000126 std::unique_ptr<MemMap> mmap(MemMap::MapFileAtAddress(
127 mmap_addr,
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100128 vdex_length,
129 (writable || unquicken) ? PROT_READ | PROT_WRITE : PROT_READ,
130 unquicken ? MAP_PRIVATE : MAP_SHARED,
131 file_fd,
132 0 /* start offset */,
133 low_4gb,
David Srbeckyec2cdf42017-12-08 16:21:25 +0000134 mmap_reuse,
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100135 vdex_filename.c_str(),
136 error_msg));
David Brazdil7b49e6c2016-09-01 11:06:18 +0100137 if (mmap == nullptr) {
138 *error_msg = "Failed to mmap file " + vdex_filename + " : " + *error_msg;
139 return nullptr;
140 }
141
Richard Uhlerb8ab63a2017-01-31 11:27:37 +0000142 std::unique_ptr<VdexFile> vdex(new VdexFile(mmap.release()));
143 if (!vdex->IsValid()) {
144 *error_msg = "Vdex file is not valid";
145 return nullptr;
146 }
147
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100148 if (unquicken) {
149 std::vector<std::unique_ptr<const DexFile>> unique_ptr_dex_files;
150 if (!vdex->OpenAllDexFiles(&unique_ptr_dex_files, error_msg)) {
151 return nullptr;
152 }
Mathieu Chartier210531f2018-01-12 10:15:51 -0800153 vdex->Unquicken(MakeNonOwningPointerVector(unique_ptr_dex_files),
154 /* decompile_return_instruction */ false);
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100155 // Update the quickening info size to pretend there isn't any.
156 reinterpret_cast<Header*>(vdex->mmap_->Begin())->quickening_info_size_ = 0;
157 }
158
David Brazdil7b49e6c2016-09-01 11:06:18 +0100159 *error_msg = "Success";
Richard Uhlerb8ab63a2017-01-31 11:27:37 +0000160 return vdex;
David Brazdil7b49e6c2016-09-01 11:06:18 +0100161}
162
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000163const uint8_t* VdexFile::GetNextDexFileData(const uint8_t* cursor) const {
164 DCHECK(cursor == nullptr || (cursor > Begin() && cursor <= End()));
165 if (cursor == nullptr) {
166 // Beginning of the iteration, return the first dex file if there is one.
Mathieu Chartier210531f2018-01-12 10:15:51 -0800167 return HasDexSection() ? DexBegin() + sizeof(QuickeningTableOffsetType) : nullptr;
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000168 } else {
169 // Fetch the next dex file. Return null if there is none.
170 const uint8_t* data = cursor + reinterpret_cast<const DexFile::Header*>(cursor)->file_size_;
Nicolas Geoffray28453cf2017-08-10 15:30:26 +0100171 // Dex files are required to be 4 byte aligned. the OatWriter makes sure they are, see
172 // OatWriter::SeekToDexFiles.
173 data = AlignUp(data, 4);
Mathieu Chartier210531f2018-01-12 10:15:51 -0800174
175 return (data == DexEnd()) ? nullptr : data + sizeof(QuickeningTableOffsetType);
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000176 }
177}
178
David Sehrbeca4fe2017-03-30 17:50:24 -0700179bool VdexFile::OpenAllDexFiles(std::vector<std::unique_ptr<const DexFile>>* dex_files,
180 std::string* error_msg) {
David Sehr013fd802018-01-11 22:55:24 -0800181 const ArtDexFileLoader dex_file_loader;
David Sehrbeca4fe2017-03-30 17:50:24 -0700182 size_t i = 0;
183 for (const uint8_t* dex_file_start = GetNextDexFileData(nullptr);
184 dex_file_start != nullptr;
185 dex_file_start = GetNextDexFileData(dex_file_start), ++i) {
186 size_t size = reinterpret_cast<const DexFile::Header*>(dex_file_start)->file_size_;
187 // TODO: Supply the location information for a vdex file.
188 static constexpr char kVdexLocation[] = "";
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700189 std::string location = DexFileLoader::GetMultiDexLocation(i, kVdexLocation);
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -0800190 std::unique_ptr<const DexFile> dex(dex_file_loader.OpenWithDataSection(
191 dex_file_start,
192 size,
193 /*data_base*/ nullptr,
194 /*data_size*/ 0u,
195 location,
196 GetLocationChecksum(i),
197 nullptr /*oat_dex_file*/,
198 false /*verify*/,
199 false /*verify_checksum*/,
200 error_msg));
David Sehrbeca4fe2017-03-30 17:50:24 -0700201 if (dex == nullptr) {
202 return false;
203 }
204 dex_files->push_back(std::move(dex));
205 }
206 return true;
207}
208
Mathieu Chartier210531f2018-01-12 10:15:51 -0800209void VdexFile::Unquicken(const std::vector<const DexFile*>& target_dex_files,
210 bool decompile_return_instruction) const {
211 const uint8_t* source_dex = GetNextDexFileData(nullptr);
212 for (const DexFile* target_dex : target_dex_files) {
213 UnquickenDexFile(*target_dex, source_dex, decompile_return_instruction);
214 source_dex = GetNextDexFileData(source_dex);
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100215 }
Mathieu Chartier210531f2018-01-12 10:15:51 -0800216 DCHECK(source_dex == nullptr);
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100217}
218
Mathieu Chartier210531f2018-01-12 10:15:51 -0800219uint32_t VdexFile::GetQuickeningInfoTableOffset(const uint8_t* source_dex_begin) const {
220 DCHECK_GE(source_dex_begin, DexBegin());
221 DCHECK_LT(source_dex_begin, DexEnd());
222 return reinterpret_cast<const QuickeningTableOffsetType*>(source_dex_begin)[-1];
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100223}
224
Mathieu Chartier2daa1342018-02-20 16:19:28 -0800225CompactOffsetTable::Accessor VdexFile::GetQuickenInfoOffsetTable(
Mathieu Chartier210531f2018-01-12 10:15:51 -0800226 const uint8_t* source_dex_begin,
Mathieu Chartier210531f2018-01-12 10:15:51 -0800227 const ArrayRef<const uint8_t>& quickening_info) const {
228 // The offset a is in preheader right before the dex file.
229 const uint32_t offset = GetQuickeningInfoTableOffset(source_dex_begin);
Mathieu Chartier2daa1342018-02-20 16:19:28 -0800230 return CompactOffsetTable::Accessor(quickening_info.SubArray(offset).data());
Mathieu Chartier210531f2018-01-12 10:15:51 -0800231}
Nicolas Geoffrayb4c6acb2017-11-10 12:48:14 +0000232
Mathieu Chartier2daa1342018-02-20 16:19:28 -0800233CompactOffsetTable::Accessor VdexFile::GetQuickenInfoOffsetTable(
Mathieu Chartier210531f2018-01-12 10:15:51 -0800234 const DexFile& dex_file,
235 const ArrayRef<const uint8_t>& quickening_info) const {
Mathieu Chartier2daa1342018-02-20 16:19:28 -0800236 return GetQuickenInfoOffsetTable(dex_file.Begin(), quickening_info);
Nicolas Geoffrayb4c6acb2017-11-10 12:48:14 +0000237}
238
239static ArrayRef<const uint8_t> GetQuickeningInfoAt(const ArrayRef<const uint8_t>& quickening_info,
240 uint32_t quickening_offset) {
Mathieu Chartier2daa1342018-02-20 16:19:28 -0800241 // Subtract offset of one since 0 represents unused and cannot be in the table.
242 ArrayRef<const uint8_t> remaining = quickening_info.SubArray(quickening_offset - 1);
Mathieu Chartier210531f2018-01-12 10:15:51 -0800243 return remaining.SubArray(0u, QuickenInfoTable::SizeInBytes(remaining));
244}
245
Nicolas Geoffrayb4c6acb2017-11-10 12:48:14 +0000246void VdexFile::UnquickenDexFile(const DexFile& target_dex_file,
Mathieu Chartier210531f2018-01-12 10:15:51 -0800247 const DexFile& source_dex_file,
248 bool decompile_return_instruction) const {
249 UnquickenDexFile(target_dex_file, source_dex_file.Begin(), decompile_return_instruction);
250}
251
252void VdexFile::UnquickenDexFile(const DexFile& target_dex_file,
253 const uint8_t* source_dex_begin,
254 bool decompile_return_instruction) const {
255 ArrayRef<const uint8_t> quickening_info = GetQuickeningInfo();
Mathieu Chartier2daa1342018-02-20 16:19:28 -0800256 if (quickening_info.empty()) {
257 // Bail early if there is no quickening info and no need to decompile. This means there is also
258 // no RETURN_VOID to decompile since the empty table takes a non zero amount of space.
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100259 return;
260 }
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800261 // Make sure to not unquicken the same code item multiple times.
262 std::unordered_set<const DexFile::CodeItem*> unquickened_code_item;
Mathieu Chartier2daa1342018-02-20 16:19:28 -0800263 CompactOffsetTable::Accessor accessor(GetQuickenInfoOffsetTable(source_dex_begin,
264 quickening_info));
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100265 for (uint32_t i = 0; i < target_dex_file.NumClassDefs(); ++i) {
266 const DexFile::ClassDef& class_def = target_dex_file.GetClassDef(i);
267 const uint8_t* class_data = target_dex_file.GetClassData(class_def);
268 if (class_data != nullptr) {
269 for (ClassDataItemIterator class_it(target_dex_file, class_data);
270 class_it.HasNext();
271 class_it.Next()) {
Alex Light1a824a52018-01-26 15:45:30 -0800272 if (class_it.IsAtMethod()) {
Nicolas Geoffrayb4c6acb2017-11-10 12:48:14 +0000273 const DexFile::CodeItem* code_item = class_it.GetMethodCodeItem();
Alex Light1a824a52018-01-26 15:45:30 -0800274 if (code_item != nullptr && unquickened_code_item.emplace(code_item).second) {
Mathieu Chartier2daa1342018-02-20 16:19:28 -0800275 const uint32_t offset = accessor.GetOffset(class_it.GetMemberIndex());
276 // Offset being 0 means not quickened.
277 if (offset != 0u) {
278 ArrayRef<const uint8_t> quicken_data = GetQuickeningInfoAt(quickening_info, offset);
279 optimizer::ArtDecompileDEX(
280 target_dex_file,
281 *code_item,
282 quicken_data,
283 decompile_return_instruction);
Alex Light1a824a52018-01-26 15:45:30 -0800284 }
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800285 }
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100286 }
Alex Lightc88a0082018-02-15 17:08:29 -0800287 DexFile::UnHideAccessFlags(class_it);
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100288 }
289 }
290 }
291}
292
Mathieu Chartier210531f2018-01-12 10:15:51 -0800293ArrayRef<const uint8_t> VdexFile::GetQuickenedInfoOf(const DexFile& dex_file,
294 uint32_t dex_method_idx) const {
Nicolas Geoffrayb4c6acb2017-11-10 12:48:14 +0000295 ArrayRef<const uint8_t> quickening_info = GetQuickeningInfo();
Mathieu Chartier210531f2018-01-12 10:15:51 -0800296 if (quickening_info.empty()) {
297 return ArrayRef<const uint8_t>();
298 }
Mathieu Chartier2daa1342018-02-20 16:19:28 -0800299 const uint32_t quickening_offset =
300 GetQuickenInfoOffsetTable(dex_file, quickening_info).GetOffset(dex_method_idx);
301 if (quickening_offset == 0u) {
302 return ArrayRef<const uint8_t>();
303 }
Mathieu Chartier210531f2018-01-12 10:15:51 -0800304 return GetQuickeningInfoAt(quickening_info, quickening_offset);
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100305}
306
David Brazdil7b49e6c2016-09-01 11:06:18 +0100307} // namespace art