blob: 843be92c0044c98beb9c9a7ac690cb2c6f79b1c5 [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 Geoffray4acefd32016-10-24 13:14:58 +010038VdexFile::Header::Header(uint32_t dex_size,
39 uint32_t verifier_deps_size,
40 uint32_t quickening_info_size)
David Brazdil5d5a36b2016-09-14 15:34:10 +010041 : dex_size_(dex_size),
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010042 verifier_deps_size_(verifier_deps_size),
43 quickening_info_size_(quickening_info_size) {
David Brazdil7b49e6c2016-09-01 11:06:18 +010044 memcpy(magic_, kVdexMagic, sizeof(kVdexMagic));
45 memcpy(version_, kVdexVersion, sizeof(kVdexVersion));
46 DCHECK(IsMagicValid());
47 DCHECK(IsVersionValid());
48}
49
50VdexFile* VdexFile::Open(const std::string& vdex_filename,
51 bool writable,
52 bool low_4gb,
53 std::string* error_msg) {
54 if (!OS::FileExists(vdex_filename.c_str())) {
55 *error_msg = "File " + vdex_filename + " does not exist.";
56 return nullptr;
57 }
58
59 std::unique_ptr<File> vdex_file;
60 if (writable) {
61 vdex_file.reset(OS::OpenFileReadWrite(vdex_filename.c_str()));
62 } else {
63 vdex_file.reset(OS::OpenFileForReading(vdex_filename.c_str()));
64 }
65 if (vdex_file == nullptr) {
66 *error_msg = "Could not open file " + vdex_filename +
67 (writable ? " for read/write" : "for reading");
68 return nullptr;
69 }
70
71 int64_t vdex_length = vdex_file->GetLength();
72 if (vdex_length == -1) {
73 *error_msg = "Could not read the length of file " + vdex_filename;
74 return nullptr;
75 }
76
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +000077 return Open(vdex_file->Fd(), vdex_length, vdex_filename, writable, low_4gb, error_msg);
78}
79
80VdexFile* VdexFile::Open(int file_fd,
81 size_t vdex_length,
82 const std::string& vdex_filename,
83 bool writable,
84 bool low_4gb,
85 std::string* error_msg) {
David Brazdil7b49e6c2016-09-01 11:06:18 +010086 std::unique_ptr<MemMap> mmap(MemMap::MapFile(vdex_length,
87 writable ? PROT_READ | PROT_WRITE : PROT_READ,
88 MAP_SHARED,
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +000089 file_fd,
David Brazdil7b49e6c2016-09-01 11:06:18 +010090 0 /* start offset */,
91 low_4gb,
92 vdex_filename.c_str(),
93 error_msg));
94 if (mmap == nullptr) {
95 *error_msg = "Failed to mmap file " + vdex_filename + " : " + *error_msg;
96 return nullptr;
97 }
98
99 *error_msg = "Success";
Andreas Gampef7e82232016-09-12 15:55:56 -0700100 return new VdexFile(mmap.release());
David Brazdil7b49e6c2016-09-01 11:06:18 +0100101}
102
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000103const uint8_t* VdexFile::GetNextDexFileData(const uint8_t* cursor) const {
104 DCHECK(cursor == nullptr || (cursor > Begin() && cursor <= End()));
105 if (cursor == nullptr) {
106 // Beginning of the iteration, return the first dex file if there is one.
107 return HasDexSection() ? DexBegin() : nullptr;
108 } else {
109 // Fetch the next dex file. Return null if there is none.
110 const uint8_t* data = cursor + reinterpret_cast<const DexFile::Header*>(cursor)->file_size_;
111 return (data == DexEnd()) ? nullptr : data;
112 }
113}
114
David Brazdil7b49e6c2016-09-01 11:06:18 +0100115} // namespace art