blob: e49092e0ec19985ca7a57cef7cdce204dafa1359 [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.
David Brazdil35a3f6a2019-03-04 15:59:06 +000020#include <sys/stat.h> // for mkdir()
Andreas Gampe0dfc3152017-04-24 07:58:06 -070021
David Brazdil7b49e6c2016-09-01 11:06:18 +010022#include <memory>
Mathieu Chartiera79efdb2018-01-18 16:31:01 -080023#include <unordered_set>
David Brazdil7b49e6c2016-09-01 11:06:18 +010024
Andreas Gampe57943812017-12-06 21:39:13 -080025#include <android-base/logging.h>
26
Nicolas Geoffray28453cf2017-08-10 15:30:26 +010027#include "base/bit_utils.h"
David Sehr67bf42e2018-02-26 16:43:04 -080028#include "base/leb128.h"
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +010029#include "base/stl_util.h"
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -070030#include "base/systrace.h"
Andreas Gampef7e82232016-09-12 15:55:56 -070031#include "base/unix_file/fd_file.h"
David Brazdil35a3f6a2019-03-04 15:59:06 +000032#include "class_linker.h"
33#include "class_loader_context.h"
David Sehr013fd802018-01-11 22:55:24 -080034#include "dex/art_dex_file_loader.h"
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -070035#include "dex/class_accessor-inl.h"
David Sehr9e734c72018-01-04 17:56:19 -080036#include "dex/dex_file_loader.h"
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +010037#include "dex_to_dex_decompiler.h"
David Brazdil35a3f6a2019-03-04 15:59:06 +000038#include "gc/heap.h"
39#include "gc/space/image_space.h"
Mathieu Chartier210531f2018-01-12 10:15:51 -080040#include "quicken_info.h"
David Brazdil35a3f6a2019-03-04 15:59:06 +000041#include "runtime.h"
42#include "verifier/verifier_deps.h"
David Brazdil7b49e6c2016-09-01 11:06:18 +010043
44namespace art {
45
Nicolas Geoffray3a293552018-03-02 10:52:16 +000046constexpr uint8_t VdexFile::VerifierDepsHeader::kVdexInvalidMagic[4];
47constexpr uint8_t VdexFile::VerifierDepsHeader::kVdexMagic[4];
48constexpr uint8_t VdexFile::VerifierDepsHeader::kVerifierDepsVersion[4];
49constexpr uint8_t VdexFile::VerifierDepsHeader::kDexSectionVersion[4];
50constexpr uint8_t VdexFile::VerifierDepsHeader::kDexSectionVersionEmpty[4];
David Brazdil7b49e6c2016-09-01 11:06:18 +010051
Nicolas Geoffray3a293552018-03-02 10:52:16 +000052bool VdexFile::VerifierDepsHeader::IsMagicValid() const {
David Brazdil7b49e6c2016-09-01 11:06:18 +010053 return (memcmp(magic_, kVdexMagic, sizeof(kVdexMagic)) == 0);
54}
55
Nicolas Geoffray3a293552018-03-02 10:52:16 +000056bool VdexFile::VerifierDepsHeader::IsVerifierDepsVersionValid() const {
57 return (memcmp(verifier_deps_version_, kVerifierDepsVersion, sizeof(kVerifierDepsVersion)) == 0);
David Brazdil7b49e6c2016-09-01 11:06:18 +010058}
59
Nicolas Geoffray3a293552018-03-02 10:52:16 +000060bool VdexFile::VerifierDepsHeader::IsDexSectionVersionValid() const {
61 return (memcmp(dex_section_version_, kDexSectionVersion, sizeof(kDexSectionVersion)) == 0) ||
62 (memcmp(dex_section_version_, kDexSectionVersionEmpty, sizeof(kDexSectionVersionEmpty)) == 0);
63}
64
65bool VdexFile::VerifierDepsHeader::HasDexSection() const {
66 return (memcmp(dex_section_version_, kDexSectionVersion, sizeof(kDexSectionVersion)) == 0);
67}
68
69VdexFile::VerifierDepsHeader::VerifierDepsHeader(uint32_t number_of_dex_files,
70 uint32_t verifier_deps_size,
David Brazdil35a3f6a2019-03-04 15:59:06 +000071 bool has_dex_section,
72 uint32_t bootclasspath_checksums_size,
73 uint32_t class_loader_context_size)
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +000074 : number_of_dex_files_(number_of_dex_files),
David Brazdil35a3f6a2019-03-04 15:59:06 +000075 verifier_deps_size_(verifier_deps_size),
76 bootclasspath_checksums_size_(bootclasspath_checksums_size),
77 class_loader_context_size_(class_loader_context_size) {
David Brazdil7b49e6c2016-09-01 11:06:18 +010078 memcpy(magic_, kVdexMagic, sizeof(kVdexMagic));
Nicolas Geoffray3a293552018-03-02 10:52:16 +000079 memcpy(verifier_deps_version_, kVerifierDepsVersion, sizeof(kVerifierDepsVersion));
80 if (has_dex_section) {
81 memcpy(dex_section_version_, kDexSectionVersion, sizeof(kDexSectionVersion));
82 } else {
83 memcpy(dex_section_version_, kDexSectionVersionEmpty, sizeof(kDexSectionVersionEmpty));
84 }
David Brazdil7b49e6c2016-09-01 11:06:18 +010085 DCHECK(IsMagicValid());
Nicolas Geoffray3a293552018-03-02 10:52:16 +000086 DCHECK(IsVerifierDepsVersionValid());
87 DCHECK(IsDexSectionVersionValid());
88}
89
90VdexFile::DexSectionHeader::DexSectionHeader(uint32_t dex_size,
91 uint32_t dex_shared_data_size,
92 uint32_t quickening_info_size)
93 : dex_size_(dex_size),
94 dex_shared_data_size_(dex_shared_data_size),
95 quickening_info_size_(quickening_info_size) {
David Brazdil7b49e6c2016-09-01 11:06:18 +010096}
97
David Srbeckyec2cdf42017-12-08 16:21:25 +000098std::unique_ptr<VdexFile> VdexFile::OpenAtAddress(uint8_t* mmap_addr,
99 size_t mmap_size,
100 bool mmap_reuse,
101 const std::string& vdex_filename,
102 bool writable,
103 bool low_4gb,
104 bool unquicken,
105 std::string* error_msg) {
Mathieu Chartier4a17f8a2019-05-17 11:03:26 -0700106 ScopedTrace trace(("VdexFile::OpenAtAddress " + vdex_filename).c_str());
David Brazdil7b49e6c2016-09-01 11:06:18 +0100107 if (!OS::FileExists(vdex_filename.c_str())) {
108 *error_msg = "File " + vdex_filename + " does not exist.";
109 return nullptr;
110 }
111
112 std::unique_ptr<File> vdex_file;
113 if (writable) {
114 vdex_file.reset(OS::OpenFileReadWrite(vdex_filename.c_str()));
115 } else {
116 vdex_file.reset(OS::OpenFileForReading(vdex_filename.c_str()));
117 }
118 if (vdex_file == nullptr) {
119 *error_msg = "Could not open file " + vdex_filename +
120 (writable ? " for read/write" : "for reading");
121 return nullptr;
122 }
123
124 int64_t vdex_length = vdex_file->GetLength();
125 if (vdex_length == -1) {
126 *error_msg = "Could not read the length of file " + vdex_filename;
127 return nullptr;
128 }
129
David Srbeckyec2cdf42017-12-08 16:21:25 +0000130 return OpenAtAddress(mmap_addr,
131 mmap_size,
132 mmap_reuse,
133 vdex_file->Fd(),
134 vdex_length,
135 vdex_filename,
136 writable,
137 low_4gb,
138 unquicken,
139 error_msg);
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000140}
141
David Srbeckyec2cdf42017-12-08 16:21:25 +0000142std::unique_ptr<VdexFile> VdexFile::OpenAtAddress(uint8_t* mmap_addr,
143 size_t mmap_size,
144 bool mmap_reuse,
145 int file_fd,
146 size_t vdex_length,
147 const std::string& vdex_filename,
148 bool writable,
149 bool low_4gb,
150 bool unquicken,
151 std::string* error_msg) {
David Srbeckyec2cdf42017-12-08 16:21:25 +0000152 if (mmap_addr != nullptr && mmap_size < vdex_length) {
153 LOG(WARNING) << "Insufficient pre-allocated space to mmap vdex.";
154 mmap_addr = nullptr;
155 mmap_reuse = false;
156 }
Andreas Gampec1fc4492018-01-10 14:19:36 -0800157 CHECK(!mmap_reuse || mmap_addr != nullptr);
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100158 MemMap mmap = MemMap::MapFileAtAddress(
David Srbeckyec2cdf42017-12-08 16:21:25 +0000159 mmap_addr,
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100160 vdex_length,
161 (writable || unquicken) ? PROT_READ | PROT_WRITE : PROT_READ,
162 unquicken ? MAP_PRIVATE : MAP_SHARED,
163 file_fd,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700164 /* start= */ 0u,
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100165 low_4gb,
166 vdex_filename.c_str(),
Vladimir Markoc09cd052018-08-23 16:36:36 +0100167 mmap_reuse,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700168 /* reservation= */ nullptr,
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100169 error_msg);
170 if (!mmap.IsValid()) {
David Brazdil7b49e6c2016-09-01 11:06:18 +0100171 *error_msg = "Failed to mmap file " + vdex_filename + " : " + *error_msg;
172 return nullptr;
173 }
174
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100175 std::unique_ptr<VdexFile> vdex(new VdexFile(std::move(mmap)));
Richard Uhlerb8ab63a2017-01-31 11:27:37 +0000176 if (!vdex->IsValid()) {
177 *error_msg = "Vdex file is not valid";
178 return nullptr;
179 }
180
Nicolas Geoffray3a293552018-03-02 10:52:16 +0000181 if (unquicken && vdex->HasDexSection()) {
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100182 std::vector<std::unique_ptr<const DexFile>> unique_ptr_dex_files;
183 if (!vdex->OpenAllDexFiles(&unique_ptr_dex_files, error_msg)) {
184 return nullptr;
185 }
Mathieu Chartier210531f2018-01-12 10:15:51 -0800186 vdex->Unquicken(MakeNonOwningPointerVector(unique_ptr_dex_files),
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700187 /* decompile_return_instruction= */ false);
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100188 // Update the quickening info size to pretend there isn't any.
Nicolas Geoffray3a293552018-03-02 10:52:16 +0000189 size_t offset = vdex->GetDexSectionHeaderOffset();
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100190 reinterpret_cast<DexSectionHeader*>(vdex->mmap_.Begin() + offset)->quickening_info_size_ = 0;
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100191 }
192
Richard Uhlerb8ab63a2017-01-31 11:27:37 +0000193 return vdex;
David Brazdil7b49e6c2016-09-01 11:06:18 +0100194}
195
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000196const uint8_t* VdexFile::GetNextDexFileData(const uint8_t* cursor) const {
197 DCHECK(cursor == nullptr || (cursor > Begin() && cursor <= End()));
198 if (cursor == nullptr) {
199 // Beginning of the iteration, return the first dex file if there is one.
Mathieu Chartier210531f2018-01-12 10:15:51 -0800200 return HasDexSection() ? DexBegin() + sizeof(QuickeningTableOffsetType) : nullptr;
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000201 } else {
202 // Fetch the next dex file. Return null if there is none.
203 const uint8_t* data = cursor + reinterpret_cast<const DexFile::Header*>(cursor)->file_size_;
Nicolas Geoffray28453cf2017-08-10 15:30:26 +0100204 // Dex files are required to be 4 byte aligned. the OatWriter makes sure they are, see
205 // OatWriter::SeekToDexFiles.
206 data = AlignUp(data, 4);
Mathieu Chartier210531f2018-01-12 10:15:51 -0800207
208 return (data == DexEnd()) ? nullptr : data + sizeof(QuickeningTableOffsetType);
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000209 }
210}
211
David Sehrbeca4fe2017-03-30 17:50:24 -0700212bool VdexFile::OpenAllDexFiles(std::vector<std::unique_ptr<const DexFile>>* dex_files,
213 std::string* error_msg) {
David Sehr013fd802018-01-11 22:55:24 -0800214 const ArtDexFileLoader dex_file_loader;
David Sehrbeca4fe2017-03-30 17:50:24 -0700215 size_t i = 0;
216 for (const uint8_t* dex_file_start = GetNextDexFileData(nullptr);
217 dex_file_start != nullptr;
218 dex_file_start = GetNextDexFileData(dex_file_start), ++i) {
219 size_t size = reinterpret_cast<const DexFile::Header*>(dex_file_start)->file_size_;
220 // TODO: Supply the location information for a vdex file.
221 static constexpr char kVdexLocation[] = "";
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700222 std::string location = DexFileLoader::GetMultiDexLocation(i, kVdexLocation);
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -0800223 std::unique_ptr<const DexFile> dex(dex_file_loader.OpenWithDataSection(
224 dex_file_start,
225 size,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700226 /*data_base=*/ nullptr,
227 /*data_size=*/ 0u,
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -0800228 location,
229 GetLocationChecksum(i),
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700230 /*oat_dex_file=*/ nullptr,
231 /*verify=*/ false,
232 /*verify_checksum=*/ false,
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -0800233 error_msg));
David Sehrbeca4fe2017-03-30 17:50:24 -0700234 if (dex == nullptr) {
235 return false;
236 }
237 dex_files->push_back(std::move(dex));
238 }
239 return true;
240}
241
Mathieu Chartier210531f2018-01-12 10:15:51 -0800242void VdexFile::Unquicken(const std::vector<const DexFile*>& target_dex_files,
243 bool decompile_return_instruction) const {
244 const uint8_t* source_dex = GetNextDexFileData(nullptr);
245 for (const DexFile* target_dex : target_dex_files) {
246 UnquickenDexFile(*target_dex, source_dex, decompile_return_instruction);
247 source_dex = GetNextDexFileData(source_dex);
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100248 }
Mathieu Chartier210531f2018-01-12 10:15:51 -0800249 DCHECK(source_dex == nullptr);
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100250}
251
Mathieu Chartier210531f2018-01-12 10:15:51 -0800252uint32_t VdexFile::GetQuickeningInfoTableOffset(const uint8_t* source_dex_begin) const {
253 DCHECK_GE(source_dex_begin, DexBegin());
254 DCHECK_LT(source_dex_begin, DexEnd());
255 return reinterpret_cast<const QuickeningTableOffsetType*>(source_dex_begin)[-1];
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100256}
257
Mathieu Chartier2daa1342018-02-20 16:19:28 -0800258CompactOffsetTable::Accessor VdexFile::GetQuickenInfoOffsetTable(
Mathieu Chartier210531f2018-01-12 10:15:51 -0800259 const uint8_t* source_dex_begin,
Mathieu Chartier210531f2018-01-12 10:15:51 -0800260 const ArrayRef<const uint8_t>& quickening_info) const {
261 // The offset a is in preheader right before the dex file.
262 const uint32_t offset = GetQuickeningInfoTableOffset(source_dex_begin);
Mathieu Chartier2daa1342018-02-20 16:19:28 -0800263 return CompactOffsetTable::Accessor(quickening_info.SubArray(offset).data());
Mathieu Chartier210531f2018-01-12 10:15:51 -0800264}
Nicolas Geoffrayb4c6acb2017-11-10 12:48:14 +0000265
Mathieu Chartier2daa1342018-02-20 16:19:28 -0800266CompactOffsetTable::Accessor VdexFile::GetQuickenInfoOffsetTable(
Mathieu Chartier210531f2018-01-12 10:15:51 -0800267 const DexFile& dex_file,
268 const ArrayRef<const uint8_t>& quickening_info) const {
Mathieu Chartier2daa1342018-02-20 16:19:28 -0800269 return GetQuickenInfoOffsetTable(dex_file.Begin(), quickening_info);
Nicolas Geoffrayb4c6acb2017-11-10 12:48:14 +0000270}
271
272static ArrayRef<const uint8_t> GetQuickeningInfoAt(const ArrayRef<const uint8_t>& quickening_info,
273 uint32_t quickening_offset) {
Mathieu Chartier2daa1342018-02-20 16:19:28 -0800274 // Subtract offset of one since 0 represents unused and cannot be in the table.
275 ArrayRef<const uint8_t> remaining = quickening_info.SubArray(quickening_offset - 1);
Mathieu Chartier210531f2018-01-12 10:15:51 -0800276 return remaining.SubArray(0u, QuickenInfoTable::SizeInBytes(remaining));
277}
278
Nicolas Geoffrayb4c6acb2017-11-10 12:48:14 +0000279void VdexFile::UnquickenDexFile(const DexFile& target_dex_file,
Mathieu Chartier210531f2018-01-12 10:15:51 -0800280 const DexFile& source_dex_file,
281 bool decompile_return_instruction) const {
282 UnquickenDexFile(target_dex_file, source_dex_file.Begin(), decompile_return_instruction);
283}
284
285void VdexFile::UnquickenDexFile(const DexFile& target_dex_file,
286 const uint8_t* source_dex_begin,
287 bool decompile_return_instruction) const {
288 ArrayRef<const uint8_t> quickening_info = GetQuickeningInfo();
Mathieu Chartier2daa1342018-02-20 16:19:28 -0800289 if (quickening_info.empty()) {
290 // Bail early if there is no quickening info and no need to decompile. This means there is also
291 // no RETURN_VOID to decompile since the empty table takes a non zero amount of space.
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100292 return;
293 }
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800294 // Make sure to not unquicken the same code item multiple times.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800295 std::unordered_set<const dex::CodeItem*> unquickened_code_item;
Mathieu Chartier2daa1342018-02-20 16:19:28 -0800296 CompactOffsetTable::Accessor accessor(GetQuickenInfoOffsetTable(source_dex_begin,
297 quickening_info));
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -0700298 for (ClassAccessor class_accessor : target_dex_file.GetClasses()) {
299 for (const ClassAccessor::Method& method : class_accessor.GetMethods()) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800300 const dex::CodeItem* code_item = method.GetCodeItem();
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -0700301 if (code_item != nullptr && unquickened_code_item.emplace(code_item).second) {
302 const uint32_t offset = accessor.GetOffset(method.GetIndex());
303 // Offset being 0 means not quickened.
304 if (offset != 0u) {
305 ArrayRef<const uint8_t> quicken_data = GetQuickeningInfoAt(quickening_info, offset);
306 optimizer::ArtDecompileDEX(
307 target_dex_file,
308 *code_item,
309 quicken_data,
310 decompile_return_instruction);
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100311 }
312 }
313 }
314 }
315}
316
Mathieu Chartier210531f2018-01-12 10:15:51 -0800317ArrayRef<const uint8_t> VdexFile::GetQuickenedInfoOf(const DexFile& dex_file,
318 uint32_t dex_method_idx) const {
Nicolas Geoffrayb4c6acb2017-11-10 12:48:14 +0000319 ArrayRef<const uint8_t> quickening_info = GetQuickeningInfo();
Mathieu Chartier210531f2018-01-12 10:15:51 -0800320 if (quickening_info.empty()) {
321 return ArrayRef<const uint8_t>();
322 }
Mathieu Chartier1599a662018-04-02 17:31:34 -0700323 CHECK_LT(dex_method_idx, dex_file.NumMethodIds());
Mathieu Chartier2daa1342018-02-20 16:19:28 -0800324 const uint32_t quickening_offset =
325 GetQuickenInfoOffsetTable(dex_file, quickening_info).GetOffset(dex_method_idx);
326 if (quickening_offset == 0u) {
327 return ArrayRef<const uint8_t>();
328 }
Mathieu Chartier210531f2018-01-12 10:15:51 -0800329 return GetQuickeningInfoAt(quickening_info, quickening_offset);
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100330}
331
David Brazdil35a3f6a2019-03-04 15:59:06 +0000332static std::string ComputeBootClassPathChecksumString() {
333 Runtime* const runtime = Runtime::Current();
334 return gc::space::ImageSpace::GetBootClassPathChecksums(
335 runtime->GetHeap()->GetBootImageSpaces(),
336 runtime->GetClassLinker()->GetBootClassPath());
337}
338
339static bool CreateDirectories(const std::string& child_path, /* out */ std::string* error_msg) {
340 size_t last_slash_pos = child_path.find_last_of('/');
341 CHECK_NE(last_slash_pos, std::string::npos) << "Invalid path: " << child_path;
342 std::string parent_path = child_path.substr(0, last_slash_pos);
343 if (OS::DirectoryExists(parent_path.c_str())) {
344 return true;
345 } else if (CreateDirectories(parent_path, error_msg)) {
346 if (mkdir(parent_path.c_str(), 0700) == 0) {
347 return true;
348 }
349 *error_msg = "Could not create directory " + parent_path;
350 return false;
351 } else {
352 return false;
353 }
354}
355
356bool VdexFile::WriteToDisk(const std::string& path,
357 const std::vector<const DexFile*>& dex_files,
358 const verifier::VerifierDeps& verifier_deps,
359 const std::string& class_loader_context,
360 std::string* error_msg) {
361 std::vector<uint8_t> verifier_deps_data;
362 verifier_deps.Encode(dex_files, &verifier_deps_data);
363
364 std::string boot_checksum = ComputeBootClassPathChecksumString();
365 DCHECK_NE(boot_checksum, "");
366
367 VdexFile::VerifierDepsHeader deps_header(dex_files.size(),
368 verifier_deps_data.size(),
369 /* has_dex_section= */ false,
370 boot_checksum.size(),
371 class_loader_context.size());
372
373 if (!CreateDirectories(path, error_msg)) {
374 return false;
375 }
376
377 std::unique_ptr<File> out(OS::CreateEmptyFileWriteOnly(path.c_str()));
378 if (out == nullptr) {
379 *error_msg = "Could not open " + path + " for writing";
380 return false;
381 }
382
383 if (!out->WriteFully(reinterpret_cast<const char*>(&deps_header), sizeof(deps_header))) {
384 *error_msg = "Could not write vdex header to " + path;
385 out->Unlink();
386 return false;
387 }
388
389 for (const DexFile* dex_file : dex_files) {
390 const uint32_t* checksum_ptr = &dex_file->GetHeader().checksum_;
391 static_assert(sizeof(*checksum_ptr) == sizeof(VdexFile::VdexChecksum));
392 if (!out->WriteFully(reinterpret_cast<const char*>(checksum_ptr),
393 sizeof(VdexFile::VdexChecksum))) {
394 *error_msg = "Could not write dex checksums to " + path;
395 out->Unlink();
396 return false;
397 }
398 }
399
400 if (!out->WriteFully(reinterpret_cast<const char*>(verifier_deps_data.data()),
401 verifier_deps_data.size())) {
402 *error_msg = "Could not write verifier deps to " + path;
403 out->Unlink();
404 return false;
405 }
406
407 if (!out->WriteFully(boot_checksum.c_str(), boot_checksum.size())) {
408 *error_msg = "Could not write boot classpath checksum to " + path;
409 out->Unlink();
410 return false;
411 }
412
413 if (!out->WriteFully(class_loader_context.c_str(), class_loader_context.size())) {
414 *error_msg = "Could not write class loader context to " + path;
415 out->Unlink();
416 return false;
417 }
418
419 if (out->FlushClose() != 0) {
420 *error_msg = "Could not flush and close " + path;
421 out->Unlink();
422 return false;
423 }
424
425 return true;
426}
427
David Brazdil7126c5b2019-03-05 00:02:51 +0000428bool VdexFile::MatchesDexFileChecksums(const std::vector<const DexFile::Header*>& dex_headers)
429 const {
430 const VerifierDepsHeader& header = GetVerifierDepsHeader();
431 if (dex_headers.size() != header.GetNumberOfDexFiles()) {
432 LOG(WARNING) << "Mismatch of number of dex files in vdex (expected="
433 << header.GetNumberOfDexFiles() << ", actual=" << dex_headers.size() << ")";
434 return false;
435 }
436 const VdexChecksum* checksums = header.GetDexChecksumsArray();
437 for (size_t i = 0; i < dex_headers.size(); ++i) {
438 if (checksums[i] != dex_headers[i]->checksum_) {
439 LOG(WARNING) << "Mismatch of dex file checksum in vdex (index=" << i << ")";
440 return false;
441 }
442 }
443 return true;
444}
445
446bool VdexFile::MatchesBootClassPathChecksums() const {
447 ArrayRef<const uint8_t> data = GetBootClassPathChecksumData();
448 std::string vdex(reinterpret_cast<const char*>(data.data()), data.size());
449 std::string runtime = ComputeBootClassPathChecksumString();
450 if (vdex == runtime) {
451 return true;
452 } else {
453 LOG(WARNING) << "Mismatch of boot class path checksum in vdex (expected="
454 << vdex << ", actual=" << runtime << ")";
455 return false;
456 }
457}
458
459bool VdexFile::MatchesClassLoaderContext(const ClassLoaderContext& context) const {
460 ArrayRef<const uint8_t> data = GetClassLoaderContextData();
461 std::string spec(reinterpret_cast<const char*>(data.data()), data.size());
462 ClassLoaderContext::VerificationResult result = context.VerifyClassLoaderContextMatch(spec);
463 if (result != ClassLoaderContext::VerificationResult::kMismatch) {
464 return true;
465 } else {
466 LOG(WARNING) << "Mismatch of class loader context in vdex (expected="
467 << spec << ", actual=" << context.EncodeContextForOatFile("") << ")";
468 return false;
469 }
470}
471
David Brazdil7b49e6c2016-09-01 11:06:18 +0100472} // namespace art