blob: 945f08b58ac15f247ca1eebcf5e9131a9a5f773d [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"
Andreas Gampef7e82232016-09-12 15:55:56 -070024#include "base/unix_file/fd_file.h"
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +000025#include "dex_file.h"
David Brazdil7b49e6c2016-09-01 11:06:18 +010026
27namespace art {
28
29constexpr uint8_t VdexFile::Header::kVdexMagic[4];
30constexpr uint8_t VdexFile::Header::kVdexVersion[4];
31
32bool VdexFile::Header::IsMagicValid() const {
33 return (memcmp(magic_, kVdexMagic, sizeof(kVdexMagic)) == 0);
34}
35
36bool VdexFile::Header::IsVersionValid() const {
37 return (memcmp(version_, kVdexVersion, sizeof(kVdexVersion)) == 0);
38}
39
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +000040VdexFile::Header::Header(uint32_t number_of_dex_files,
41 uint32_t dex_size,
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010042 uint32_t verifier_deps_size,
43 uint32_t quickening_info_size)
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +000044 : number_of_dex_files_(number_of_dex_files),
45 dex_size_(dex_size),
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010046 verifier_deps_size_(verifier_deps_size),
47 quickening_info_size_(quickening_info_size) {
David Brazdil7b49e6c2016-09-01 11:06:18 +010048 memcpy(magic_, kVdexMagic, sizeof(kVdexMagic));
49 memcpy(version_, kVdexVersion, sizeof(kVdexVersion));
50 DCHECK(IsMagicValid());
51 DCHECK(IsVersionValid());
52}
53
Richard Uhlerb8ab63a2017-01-31 11:27:37 +000054std::unique_ptr<VdexFile> VdexFile::Open(const std::string& vdex_filename,
55 bool writable,
56 bool low_4gb,
57 std::string* error_msg) {
David Brazdil7b49e6c2016-09-01 11:06:18 +010058 if (!OS::FileExists(vdex_filename.c_str())) {
59 *error_msg = "File " + vdex_filename + " does not exist.";
60 return nullptr;
61 }
62
63 std::unique_ptr<File> vdex_file;
64 if (writable) {
65 vdex_file.reset(OS::OpenFileReadWrite(vdex_filename.c_str()));
66 } else {
67 vdex_file.reset(OS::OpenFileForReading(vdex_filename.c_str()));
68 }
69 if (vdex_file == nullptr) {
70 *error_msg = "Could not open file " + vdex_filename +
71 (writable ? " for read/write" : "for reading");
72 return nullptr;
73 }
74
75 int64_t vdex_length = vdex_file->GetLength();
76 if (vdex_length == -1) {
77 *error_msg = "Could not read the length of file " + vdex_filename;
78 return nullptr;
79 }
80
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +000081 return Open(vdex_file->Fd(), vdex_length, vdex_filename, writable, low_4gb, error_msg);
82}
83
Richard Uhlerb8ab63a2017-01-31 11:27:37 +000084std::unique_ptr<VdexFile> VdexFile::Open(int file_fd,
85 size_t vdex_length,
86 const std::string& vdex_filename,
87 bool writable,
88 bool low_4gb,
89 std::string* error_msg) {
David Brazdil7b49e6c2016-09-01 11:06:18 +010090 std::unique_ptr<MemMap> mmap(MemMap::MapFile(vdex_length,
91 writable ? PROT_READ | PROT_WRITE : PROT_READ,
92 MAP_SHARED,
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +000093 file_fd,
David Brazdil7b49e6c2016-09-01 11:06:18 +010094 0 /* start offset */,
95 low_4gb,
96 vdex_filename.c_str(),
97 error_msg));
98 if (mmap == nullptr) {
99 *error_msg = "Failed to mmap file " + vdex_filename + " : " + *error_msg;
100 return nullptr;
101 }
102
Richard Uhlerb8ab63a2017-01-31 11:27:37 +0000103 std::unique_ptr<VdexFile> vdex(new VdexFile(mmap.release()));
104 if (!vdex->IsValid()) {
105 *error_msg = "Vdex file is not valid";
106 return nullptr;
107 }
108
David Brazdil7b49e6c2016-09-01 11:06:18 +0100109 *error_msg = "Success";
Richard Uhlerb8ab63a2017-01-31 11:27:37 +0000110 return vdex;
David Brazdil7b49e6c2016-09-01 11:06:18 +0100111}
112
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000113const uint8_t* VdexFile::GetNextDexFileData(const uint8_t* cursor) const {
114 DCHECK(cursor == nullptr || (cursor > Begin() && cursor <= End()));
115 if (cursor == nullptr) {
116 // Beginning of the iteration, return the first dex file if there is one.
117 return HasDexSection() ? DexBegin() : nullptr;
118 } else {
119 // Fetch the next dex file. Return null if there is none.
120 const uint8_t* data = cursor + reinterpret_cast<const DexFile::Header*>(cursor)->file_size_;
121 return (data == DexEnd()) ? nullptr : data;
122 }
123}
124
David Sehrbeca4fe2017-03-30 17:50:24 -0700125bool VdexFile::OpenAllDexFiles(std::vector<std::unique_ptr<const DexFile>>* dex_files,
126 std::string* error_msg) {
127 size_t i = 0;
128 for (const uint8_t* dex_file_start = GetNextDexFileData(nullptr);
129 dex_file_start != nullptr;
130 dex_file_start = GetNextDexFileData(dex_file_start), ++i) {
131 size_t size = reinterpret_cast<const DexFile::Header*>(dex_file_start)->file_size_;
132 // TODO: Supply the location information for a vdex file.
133 static constexpr char kVdexLocation[] = "";
134 std::string location = DexFile::GetMultiDexLocation(i, kVdexLocation);
135 std::unique_ptr<const DexFile> dex(DexFile::Open(dex_file_start,
136 size,
137 location,
138 GetLocationChecksum(i),
139 nullptr /*oat_dex_file*/,
140 false /*verify*/,
141 false /*verify_checksum*/,
142 error_msg));
143 if (dex == nullptr) {
144 return false;
145 }
146 dex_files->push_back(std::move(dex));
147 }
148 return true;
149}
150
David Brazdil7b49e6c2016-09-01 11:06:18 +0100151} // namespace art