blob: 72c42b9ee45d88277eab6acd29e30d958c374311 [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"
David Sehr67bf42e2018-02-26 16:43:04 -080027#include "base/leb128.h"
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +010028#include "base/stl_util.h"
Andreas Gampef7e82232016-09-12 15:55:56 -070029#include "base/unix_file/fd_file.h"
David Sehr013fd802018-01-11 22:55:24 -080030#include "dex/art_dex_file_loader.h"
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -070031#include "dex/class_accessor-inl.h"
David Sehr9e734c72018-01-04 17:56:19 -080032#include "dex/dex_file.h"
33#include "dex/dex_file_loader.h"
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +010034#include "dex_to_dex_decompiler.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 Geoffray3a293552018-03-02 10:52:16 +000039constexpr uint8_t VdexFile::VerifierDepsHeader::kVdexInvalidMagic[4];
40constexpr uint8_t VdexFile::VerifierDepsHeader::kVdexMagic[4];
41constexpr uint8_t VdexFile::VerifierDepsHeader::kVerifierDepsVersion[4];
42constexpr uint8_t VdexFile::VerifierDepsHeader::kDexSectionVersion[4];
43constexpr uint8_t VdexFile::VerifierDepsHeader::kDexSectionVersionEmpty[4];
David Brazdil7b49e6c2016-09-01 11:06:18 +010044
Nicolas Geoffray3a293552018-03-02 10:52:16 +000045bool VdexFile::VerifierDepsHeader::IsMagicValid() const {
David Brazdil7b49e6c2016-09-01 11:06:18 +010046 return (memcmp(magic_, kVdexMagic, sizeof(kVdexMagic)) == 0);
47}
48
Nicolas Geoffray3a293552018-03-02 10:52:16 +000049bool VdexFile::VerifierDepsHeader::IsVerifierDepsVersionValid() const {
50 return (memcmp(verifier_deps_version_, kVerifierDepsVersion, sizeof(kVerifierDepsVersion)) == 0);
David Brazdil7b49e6c2016-09-01 11:06:18 +010051}
52
Nicolas Geoffray3a293552018-03-02 10:52:16 +000053bool VdexFile::VerifierDepsHeader::IsDexSectionVersionValid() const {
54 return (memcmp(dex_section_version_, kDexSectionVersion, sizeof(kDexSectionVersion)) == 0) ||
55 (memcmp(dex_section_version_, kDexSectionVersionEmpty, sizeof(kDexSectionVersionEmpty)) == 0);
56}
57
58bool VdexFile::VerifierDepsHeader::HasDexSection() const {
59 return (memcmp(dex_section_version_, kDexSectionVersion, sizeof(kDexSectionVersion)) == 0);
60}
61
62VdexFile::VerifierDepsHeader::VerifierDepsHeader(uint32_t number_of_dex_files,
63 uint32_t verifier_deps_size,
64 bool has_dex_section)
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +000065 : number_of_dex_files_(number_of_dex_files),
Nicolas Geoffray3a293552018-03-02 10:52:16 +000066 verifier_deps_size_(verifier_deps_size) {
David Brazdil7b49e6c2016-09-01 11:06:18 +010067 memcpy(magic_, kVdexMagic, sizeof(kVdexMagic));
Nicolas Geoffray3a293552018-03-02 10:52:16 +000068 memcpy(verifier_deps_version_, kVerifierDepsVersion, sizeof(kVerifierDepsVersion));
69 if (has_dex_section) {
70 memcpy(dex_section_version_, kDexSectionVersion, sizeof(kDexSectionVersion));
71 } else {
72 memcpy(dex_section_version_, kDexSectionVersionEmpty, sizeof(kDexSectionVersionEmpty));
73 }
David Brazdil7b49e6c2016-09-01 11:06:18 +010074 DCHECK(IsMagicValid());
Nicolas Geoffray3a293552018-03-02 10:52:16 +000075 DCHECK(IsVerifierDepsVersionValid());
76 DCHECK(IsDexSectionVersionValid());
77}
78
79VdexFile::DexSectionHeader::DexSectionHeader(uint32_t dex_size,
80 uint32_t dex_shared_data_size,
81 uint32_t quickening_info_size)
82 : dex_size_(dex_size),
83 dex_shared_data_size_(dex_shared_data_size),
84 quickening_info_size_(quickening_info_size) {
David Brazdil7b49e6c2016-09-01 11:06:18 +010085}
86
David Srbeckyec2cdf42017-12-08 16:21:25 +000087std::unique_ptr<VdexFile> VdexFile::OpenAtAddress(uint8_t* mmap_addr,
88 size_t mmap_size,
89 bool mmap_reuse,
90 const std::string& vdex_filename,
91 bool writable,
92 bool low_4gb,
93 bool unquicken,
94 std::string* error_msg) {
David Brazdil7b49e6c2016-09-01 11:06:18 +010095 if (!OS::FileExists(vdex_filename.c_str())) {
96 *error_msg = "File " + vdex_filename + " does not exist.";
97 return nullptr;
98 }
99
100 std::unique_ptr<File> vdex_file;
101 if (writable) {
102 vdex_file.reset(OS::OpenFileReadWrite(vdex_filename.c_str()));
103 } else {
104 vdex_file.reset(OS::OpenFileForReading(vdex_filename.c_str()));
105 }
106 if (vdex_file == nullptr) {
107 *error_msg = "Could not open file " + vdex_filename +
108 (writable ? " for read/write" : "for reading");
109 return nullptr;
110 }
111
112 int64_t vdex_length = vdex_file->GetLength();
113 if (vdex_length == -1) {
114 *error_msg = "Could not read the length of file " + vdex_filename;
115 return nullptr;
116 }
117
David Srbeckyec2cdf42017-12-08 16:21:25 +0000118 return OpenAtAddress(mmap_addr,
119 mmap_size,
120 mmap_reuse,
121 vdex_file->Fd(),
122 vdex_length,
123 vdex_filename,
124 writable,
125 low_4gb,
126 unquicken,
127 error_msg);
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000128}
129
David Srbeckyec2cdf42017-12-08 16:21:25 +0000130std::unique_ptr<VdexFile> VdexFile::OpenAtAddress(uint8_t* mmap_addr,
131 size_t mmap_size,
132 bool mmap_reuse,
133 int file_fd,
134 size_t vdex_length,
135 const std::string& vdex_filename,
136 bool writable,
137 bool low_4gb,
138 bool unquicken,
139 std::string* error_msg) {
David Srbeckyec2cdf42017-12-08 16:21:25 +0000140 if (mmap_addr != nullptr && mmap_size < vdex_length) {
141 LOG(WARNING) << "Insufficient pre-allocated space to mmap vdex.";
142 mmap_addr = nullptr;
143 mmap_reuse = false;
144 }
Andreas Gampec1fc4492018-01-10 14:19:36 -0800145 CHECK(!mmap_reuse || mmap_addr != nullptr);
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100146 MemMap mmap = MemMap::MapFileAtAddress(
David Srbeckyec2cdf42017-12-08 16:21:25 +0000147 mmap_addr,
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100148 vdex_length,
149 (writable || unquicken) ? PROT_READ | PROT_WRITE : PROT_READ,
150 unquicken ? MAP_PRIVATE : MAP_SHARED,
151 file_fd,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700152 /* start= */ 0u,
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100153 low_4gb,
154 vdex_filename.c_str(),
Vladimir Markoc09cd052018-08-23 16:36:36 +0100155 mmap_reuse,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700156 /* reservation= */ nullptr,
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100157 error_msg);
158 if (!mmap.IsValid()) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100159 *error_msg = "Failed to mmap file " + vdex_filename + " : " + *error_msg;
160 return nullptr;
161 }
162
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100163 std::unique_ptr<VdexFile> vdex(new VdexFile(std::move(mmap)));
Richard Uhlerb8ab63a2017-01-31 11:27:37 +0000164 if (!vdex->IsValid()) {
165 *error_msg = "Vdex file is not valid";
166 return nullptr;
167 }
168
Nicolas Geoffray3a293552018-03-02 10:52:16 +0000169 if (unquicken && vdex->HasDexSection()) {
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100170 std::vector<std::unique_ptr<const DexFile>> unique_ptr_dex_files;
171 if (!vdex->OpenAllDexFiles(&unique_ptr_dex_files, error_msg)) {
172 return nullptr;
173 }
Mathieu Chartier210531f2018-01-12 10:15:51 -0800174 vdex->Unquicken(MakeNonOwningPointerVector(unique_ptr_dex_files),
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700175 /* decompile_return_instruction= */ false);
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100176 // Update the quickening info size to pretend there isn't any.
Nicolas Geoffray3a293552018-03-02 10:52:16 +0000177 size_t offset = vdex->GetDexSectionHeaderOffset();
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100178 reinterpret_cast<DexSectionHeader*>(vdex->mmap_.Begin() + offset)->quickening_info_size_ = 0;
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100179 }
180
David Brazdil7b49e6c2016-09-01 11:06:18 +0100181 *error_msg = "Success";
Richard Uhlerb8ab63a2017-01-31 11:27:37 +0000182 return vdex;
David Brazdil7b49e6c2016-09-01 11:06:18 +0100183}
184
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000185const uint8_t* VdexFile::GetNextDexFileData(const uint8_t* cursor) const {
186 DCHECK(cursor == nullptr || (cursor > Begin() && cursor <= End()));
187 if (cursor == nullptr) {
188 // Beginning of the iteration, return the first dex file if there is one.
Mathieu Chartier210531f2018-01-12 10:15:51 -0800189 return HasDexSection() ? DexBegin() + sizeof(QuickeningTableOffsetType) : nullptr;
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000190 } else {
191 // Fetch the next dex file. Return null if there is none.
192 const uint8_t* data = cursor + reinterpret_cast<const DexFile::Header*>(cursor)->file_size_;
Nicolas Geoffray28453cf2017-08-10 15:30:26 +0100193 // Dex files are required to be 4 byte aligned. the OatWriter makes sure they are, see
194 // OatWriter::SeekToDexFiles.
195 data = AlignUp(data, 4);
Mathieu Chartier210531f2018-01-12 10:15:51 -0800196
197 return (data == DexEnd()) ? nullptr : data + sizeof(QuickeningTableOffsetType);
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000198 }
199}
200
David Sehrbeca4fe2017-03-30 17:50:24 -0700201bool VdexFile::OpenAllDexFiles(std::vector<std::unique_ptr<const DexFile>>* dex_files,
202 std::string* error_msg) {
David Sehr013fd802018-01-11 22:55:24 -0800203 const ArtDexFileLoader dex_file_loader;
David Sehrbeca4fe2017-03-30 17:50:24 -0700204 size_t i = 0;
205 for (const uint8_t* dex_file_start = GetNextDexFileData(nullptr);
206 dex_file_start != nullptr;
207 dex_file_start = GetNextDexFileData(dex_file_start), ++i) {
208 size_t size = reinterpret_cast<const DexFile::Header*>(dex_file_start)->file_size_;
209 // TODO: Supply the location information for a vdex file.
210 static constexpr char kVdexLocation[] = "";
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700211 std::string location = DexFileLoader::GetMultiDexLocation(i, kVdexLocation);
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -0800212 std::unique_ptr<const DexFile> dex(dex_file_loader.OpenWithDataSection(
213 dex_file_start,
214 size,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700215 /*data_base=*/ nullptr,
216 /*data_size=*/ 0u,
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -0800217 location,
218 GetLocationChecksum(i),
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700219 /*oat_dex_file=*/ nullptr,
220 /*verify=*/ false,
221 /*verify_checksum=*/ false,
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -0800222 error_msg));
David Sehrbeca4fe2017-03-30 17:50:24 -0700223 if (dex == nullptr) {
224 return false;
225 }
226 dex_files->push_back(std::move(dex));
227 }
228 return true;
229}
230
Mathieu Chartier210531f2018-01-12 10:15:51 -0800231void VdexFile::Unquicken(const std::vector<const DexFile*>& target_dex_files,
232 bool decompile_return_instruction) const {
233 const uint8_t* source_dex = GetNextDexFileData(nullptr);
234 for (const DexFile* target_dex : target_dex_files) {
235 UnquickenDexFile(*target_dex, source_dex, decompile_return_instruction);
236 source_dex = GetNextDexFileData(source_dex);
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100237 }
Mathieu Chartier210531f2018-01-12 10:15:51 -0800238 DCHECK(source_dex == nullptr);
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100239}
240
Mathieu Chartier210531f2018-01-12 10:15:51 -0800241uint32_t VdexFile::GetQuickeningInfoTableOffset(const uint8_t* source_dex_begin) const {
242 DCHECK_GE(source_dex_begin, DexBegin());
243 DCHECK_LT(source_dex_begin, DexEnd());
244 return reinterpret_cast<const QuickeningTableOffsetType*>(source_dex_begin)[-1];
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100245}
246
Mathieu Chartier2daa1342018-02-20 16:19:28 -0800247CompactOffsetTable::Accessor VdexFile::GetQuickenInfoOffsetTable(
Mathieu Chartier210531f2018-01-12 10:15:51 -0800248 const uint8_t* source_dex_begin,
Mathieu Chartier210531f2018-01-12 10:15:51 -0800249 const ArrayRef<const uint8_t>& quickening_info) const {
250 // The offset a is in preheader right before the dex file.
251 const uint32_t offset = GetQuickeningInfoTableOffset(source_dex_begin);
Mathieu Chartier2daa1342018-02-20 16:19:28 -0800252 return CompactOffsetTable::Accessor(quickening_info.SubArray(offset).data());
Mathieu Chartier210531f2018-01-12 10:15:51 -0800253}
Nicolas Geoffrayb4c6acb2017-11-10 12:48:14 +0000254
Mathieu Chartier2daa1342018-02-20 16:19:28 -0800255CompactOffsetTable::Accessor VdexFile::GetQuickenInfoOffsetTable(
Mathieu Chartier210531f2018-01-12 10:15:51 -0800256 const DexFile& dex_file,
257 const ArrayRef<const uint8_t>& quickening_info) const {
Mathieu Chartier2daa1342018-02-20 16:19:28 -0800258 return GetQuickenInfoOffsetTable(dex_file.Begin(), quickening_info);
Nicolas Geoffrayb4c6acb2017-11-10 12:48:14 +0000259}
260
261static ArrayRef<const uint8_t> GetQuickeningInfoAt(const ArrayRef<const uint8_t>& quickening_info,
262 uint32_t quickening_offset) {
Mathieu Chartier2daa1342018-02-20 16:19:28 -0800263 // Subtract offset of one since 0 represents unused and cannot be in the table.
264 ArrayRef<const uint8_t> remaining = quickening_info.SubArray(quickening_offset - 1);
Mathieu Chartier210531f2018-01-12 10:15:51 -0800265 return remaining.SubArray(0u, QuickenInfoTable::SizeInBytes(remaining));
266}
267
Nicolas Geoffrayb4c6acb2017-11-10 12:48:14 +0000268void VdexFile::UnquickenDexFile(const DexFile& target_dex_file,
Mathieu Chartier210531f2018-01-12 10:15:51 -0800269 const DexFile& source_dex_file,
270 bool decompile_return_instruction) const {
271 UnquickenDexFile(target_dex_file, source_dex_file.Begin(), decompile_return_instruction);
272}
273
274void VdexFile::UnquickenDexFile(const DexFile& target_dex_file,
275 const uint8_t* source_dex_begin,
276 bool decompile_return_instruction) const {
277 ArrayRef<const uint8_t> quickening_info = GetQuickeningInfo();
Mathieu Chartier2daa1342018-02-20 16:19:28 -0800278 if (quickening_info.empty()) {
279 // Bail early if there is no quickening info and no need to decompile. This means there is also
280 // no RETURN_VOID to decompile since the empty table takes a non zero amount of space.
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100281 return;
282 }
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800283 // Make sure to not unquicken the same code item multiple times.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800284 std::unordered_set<const dex::CodeItem*> unquickened_code_item;
Mathieu Chartier2daa1342018-02-20 16:19:28 -0800285 CompactOffsetTable::Accessor accessor(GetQuickenInfoOffsetTable(source_dex_begin,
286 quickening_info));
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -0700287 for (ClassAccessor class_accessor : target_dex_file.GetClasses()) {
288 for (const ClassAccessor::Method& method : class_accessor.GetMethods()) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800289 const dex::CodeItem* code_item = method.GetCodeItem();
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -0700290 if (code_item != nullptr && unquickened_code_item.emplace(code_item).second) {
291 const uint32_t offset = accessor.GetOffset(method.GetIndex());
292 // Offset being 0 means not quickened.
293 if (offset != 0u) {
294 ArrayRef<const uint8_t> quicken_data = GetQuickeningInfoAt(quickening_info, offset);
295 optimizer::ArtDecompileDEX(
296 target_dex_file,
297 *code_item,
298 quicken_data,
299 decompile_return_instruction);
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100300 }
301 }
302 }
303 }
304}
305
Mathieu Chartier210531f2018-01-12 10:15:51 -0800306ArrayRef<const uint8_t> VdexFile::GetQuickenedInfoOf(const DexFile& dex_file,
307 uint32_t dex_method_idx) const {
Nicolas Geoffrayb4c6acb2017-11-10 12:48:14 +0000308 ArrayRef<const uint8_t> quickening_info = GetQuickeningInfo();
Mathieu Chartier210531f2018-01-12 10:15:51 -0800309 if (quickening_info.empty()) {
310 return ArrayRef<const uint8_t>();
311 }
Mathieu Chartier1599a662018-04-02 17:31:34 -0700312 CHECK_LT(dex_method_idx, dex_file.NumMethodIds());
Mathieu Chartier2daa1342018-02-20 16:19:28 -0800313 const uint32_t quickening_offset =
314 GetQuickenInfoOffsetTable(dex_file, quickening_info).GetOffset(dex_method_idx);
315 if (quickening_offset == 0u) {
316 return ArrayRef<const uint8_t>();
317 }
Mathieu Chartier210531f2018-01-12 10:15:51 -0800318 return GetQuickeningInfoAt(quickening_info, quickening_offset);
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100319}
320
David Brazdil7b49e6c2016-09-01 11:06:18 +0100321} // namespace art