blob: b3dab581d57b3aa578928aa662c78c91c83af609 [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"
David Brazdil7b49e6c2016-09-01 11:06:18 +010023
24namespace art {
25
26constexpr uint8_t VdexFile::Header::kVdexMagic[4];
27constexpr uint8_t VdexFile::Header::kVdexVersion[4];
28
29bool VdexFile::Header::IsMagicValid() const {
30 return (memcmp(magic_, kVdexMagic, sizeof(kVdexMagic)) == 0);
31}
32
33bool VdexFile::Header::IsVersionValid() const {
34 return (memcmp(version_, kVdexVersion, sizeof(kVdexVersion)) == 0);
35}
36
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010037VdexFile::Header::Header(uint32_t dex_size,
38 uint32_t verifier_deps_size,
39 uint32_t quickening_info_size)
David Brazdil5d5a36b2016-09-14 15:34:10 +010040 : dex_size_(dex_size),
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010041 verifier_deps_size_(verifier_deps_size),
42 quickening_info_size_(quickening_info_size) {
David Brazdil7b49e6c2016-09-01 11:06:18 +010043 memcpy(magic_, kVdexMagic, sizeof(kVdexMagic));
44 memcpy(version_, kVdexVersion, sizeof(kVdexVersion));
45 DCHECK(IsMagicValid());
46 DCHECK(IsVersionValid());
47}
48
49VdexFile* VdexFile::Open(const std::string& vdex_filename,
50 bool writable,
51 bool low_4gb,
52 std::string* error_msg) {
53 if (!OS::FileExists(vdex_filename.c_str())) {
54 *error_msg = "File " + vdex_filename + " does not exist.";
55 return nullptr;
56 }
57
58 std::unique_ptr<File> vdex_file;
59 if (writable) {
60 vdex_file.reset(OS::OpenFileReadWrite(vdex_filename.c_str()));
61 } else {
62 vdex_file.reset(OS::OpenFileForReading(vdex_filename.c_str()));
63 }
64 if (vdex_file == nullptr) {
65 *error_msg = "Could not open file " + vdex_filename +
66 (writable ? " for read/write" : "for reading");
67 return nullptr;
68 }
69
70 int64_t vdex_length = vdex_file->GetLength();
71 if (vdex_length == -1) {
72 *error_msg = "Could not read the length of file " + vdex_filename;
73 return nullptr;
74 }
75
76 std::unique_ptr<MemMap> mmap(MemMap::MapFile(vdex_length,
77 writable ? PROT_READ | PROT_WRITE : PROT_READ,
78 MAP_SHARED,
79 vdex_file->Fd(),
80 0 /* start offset */,
81 low_4gb,
82 vdex_filename.c_str(),
83 error_msg));
84 if (mmap == nullptr) {
85 *error_msg = "Failed to mmap file " + vdex_filename + " : " + *error_msg;
86 return nullptr;
87 }
88
89 *error_msg = "Success";
Andreas Gampef7e82232016-09-12 15:55:56 -070090 return new VdexFile(mmap.release());
David Brazdil7b49e6c2016-09-01 11:06:18 +010091}
92
93} // namespace art