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