blob: 2481c8ba46f81adcab90957e734203dc5e421b6e [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
19#include <memory>
20
21#include "base/logging.h"
Andreas Gampef7e82232016-09-12 15:55:56 -070022#include "base/unix_file/fd_file.h"
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +000023#include "dex_file.h"
David Brazdil7b49e6c2016-09-01 11:06:18 +010024
25namespace art {
26
27constexpr uint8_t VdexFile::Header::kVdexMagic[4];
28constexpr uint8_t VdexFile::Header::kVdexVersion[4];
29
30bool VdexFile::Header::IsMagicValid() const {
31 return (memcmp(magic_, kVdexMagic, sizeof(kVdexMagic)) == 0);
32}
33
34bool VdexFile::Header::IsVersionValid() const {
35 return (memcmp(version_, kVdexVersion, sizeof(kVdexVersion)) == 0);
36}
37
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +000038VdexFile::Header::Header(uint32_t number_of_dex_files,
39 uint32_t dex_size,
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010040 uint32_t verifier_deps_size,
41 uint32_t quickening_info_size)
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +000042 : number_of_dex_files_(number_of_dex_files),
43 dex_size_(dex_size),
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010044 verifier_deps_size_(verifier_deps_size),
45 quickening_info_size_(quickening_info_size) {
David Brazdil7b49e6c2016-09-01 11:06:18 +010046 memcpy(magic_, kVdexMagic, sizeof(kVdexMagic));
47 memcpy(version_, kVdexVersion, sizeof(kVdexVersion));
48 DCHECK(IsMagicValid());
49 DCHECK(IsVersionValid());
50}
51
Richard Uhlerb8ab63a2017-01-31 11:27:37 +000052std::unique_ptr<VdexFile> VdexFile::Open(const std::string& vdex_filename,
53 bool writable,
54 bool low_4gb,
55 std::string* error_msg) {
David Brazdil7b49e6c2016-09-01 11:06:18 +010056 if (!OS::FileExists(vdex_filename.c_str())) {
57 *error_msg = "File " + vdex_filename + " does not exist.";
58 return nullptr;
59 }
60
61 std::unique_ptr<File> vdex_file;
62 if (writable) {
63 vdex_file.reset(OS::OpenFileReadWrite(vdex_filename.c_str()));
64 } else {
65 vdex_file.reset(OS::OpenFileForReading(vdex_filename.c_str()));
66 }
67 if (vdex_file == nullptr) {
68 *error_msg = "Could not open file " + vdex_filename +
69 (writable ? " for read/write" : "for reading");
70 return nullptr;
71 }
72
73 int64_t vdex_length = vdex_file->GetLength();
74 if (vdex_length == -1) {
75 *error_msg = "Could not read the length of file " + vdex_filename;
76 return nullptr;
77 }
78
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +000079 return Open(vdex_file->Fd(), vdex_length, vdex_filename, writable, low_4gb, error_msg);
80}
81
Richard Uhlerb8ab63a2017-01-31 11:27:37 +000082std::unique_ptr<VdexFile> VdexFile::Open(int file_fd,
83 size_t vdex_length,
84 const std::string& vdex_filename,
85 bool writable,
86 bool low_4gb,
87 std::string* error_msg) {
David Brazdil7b49e6c2016-09-01 11:06:18 +010088 std::unique_ptr<MemMap> mmap(MemMap::MapFile(vdex_length,
89 writable ? PROT_READ | PROT_WRITE : PROT_READ,
90 MAP_SHARED,
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +000091 file_fd,
David Brazdil7b49e6c2016-09-01 11:06:18 +010092 0 /* start offset */,
93 low_4gb,
94 vdex_filename.c_str(),
95 error_msg));
96 if (mmap == nullptr) {
97 *error_msg = "Failed to mmap file " + vdex_filename + " : " + *error_msg;
98 return nullptr;
99 }
100
Richard Uhlerb8ab63a2017-01-31 11:27:37 +0000101 std::unique_ptr<VdexFile> vdex(new VdexFile(mmap.release()));
102 if (!vdex->IsValid()) {
103 *error_msg = "Vdex file is not valid";
104 return nullptr;
105 }
106
David Brazdil7b49e6c2016-09-01 11:06:18 +0100107 *error_msg = "Success";
Richard Uhlerb8ab63a2017-01-31 11:27:37 +0000108 return vdex;
David Brazdil7b49e6c2016-09-01 11:06:18 +0100109}
110
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000111const uint8_t* VdexFile::GetNextDexFileData(const uint8_t* cursor) const {
112 DCHECK(cursor == nullptr || (cursor > Begin() && cursor <= End()));
113 if (cursor == nullptr) {
114 // Beginning of the iteration, return the first dex file if there is one.
115 return HasDexSection() ? DexBegin() : nullptr;
116 } else {
117 // Fetch the next dex file. Return null if there is none.
118 const uint8_t* data = cursor + reinterpret_cast<const DexFile::Header*>(cursor)->file_size_;
119 return (data == DexEnd()) ? nullptr : data;
120 }
121}
122
David Brazdil7b49e6c2016-09-01 11:06:18 +0100123} // namespace art