David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1 | /* |
| 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 Gampe | f7e8223 | 2016-09-12 15:55:56 -0700 | [diff] [blame] | 22 | #include "base/unix_file/fd_file.h" |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 23 | #include "dex_file.h" |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | constexpr uint8_t VdexFile::Header::kVdexMagic[4]; |
| 28 | constexpr uint8_t VdexFile::Header::kVdexVersion[4]; |
| 29 | |
| 30 | bool VdexFile::Header::IsMagicValid() const { |
| 31 | return (memcmp(magic_, kVdexMagic, sizeof(kVdexMagic)) == 0); |
| 32 | } |
| 33 | |
| 34 | bool VdexFile::Header::IsVersionValid() const { |
| 35 | return (memcmp(version_, kVdexVersion, sizeof(kVdexVersion)) == 0); |
| 36 | } |
| 37 | |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 38 | VdexFile::Header::Header(uint32_t number_of_dex_files, |
| 39 | uint32_t dex_size, |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 40 | uint32_t verifier_deps_size, |
| 41 | uint32_t quickening_info_size) |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 42 | : number_of_dex_files_(number_of_dex_files), |
| 43 | dex_size_(dex_size), |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 44 | verifier_deps_size_(verifier_deps_size), |
| 45 | quickening_info_size_(quickening_info_size) { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 46 | memcpy(magic_, kVdexMagic, sizeof(kVdexMagic)); |
| 47 | memcpy(version_, kVdexVersion, sizeof(kVdexVersion)); |
| 48 | DCHECK(IsMagicValid()); |
| 49 | DCHECK(IsVersionValid()); |
| 50 | } |
| 51 | |
Richard Uhler | b8ab63a | 2017-01-31 11:27:37 +0000 | [diff] [blame] | 52 | std::unique_ptr<VdexFile> VdexFile::Open(const std::string& vdex_filename, |
| 53 | bool writable, |
| 54 | bool low_4gb, |
| 55 | std::string* error_msg) { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 56 | 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 Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 79 | return Open(vdex_file->Fd(), vdex_length, vdex_filename, writable, low_4gb, error_msg); |
| 80 | } |
| 81 | |
Richard Uhler | b8ab63a | 2017-01-31 11:27:37 +0000 | [diff] [blame] | 82 | std::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 Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 88 | std::unique_ptr<MemMap> mmap(MemMap::MapFile(vdex_length, |
| 89 | writable ? PROT_READ | PROT_WRITE : PROT_READ, |
| 90 | MAP_SHARED, |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 91 | file_fd, |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 92 | 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 Uhler | b8ab63a | 2017-01-31 11:27:37 +0000 | [diff] [blame] | 101 | 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 Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 107 | *error_msg = "Success"; |
Richard Uhler | b8ab63a | 2017-01-31 11:27:37 +0000 | [diff] [blame] | 108 | return vdex; |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 109 | } |
| 110 | |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 111 | const 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 Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame^] | 123 | bool VdexFile::OpenAllDexFiles(std::vector<std::unique_ptr<const DexFile>>* dex_files, |
| 124 | std::string* error_msg) { |
| 125 | size_t i = 0; |
| 126 | for (const uint8_t* dex_file_start = GetNextDexFileData(nullptr); |
| 127 | dex_file_start != nullptr; |
| 128 | dex_file_start = GetNextDexFileData(dex_file_start), ++i) { |
| 129 | size_t size = reinterpret_cast<const DexFile::Header*>(dex_file_start)->file_size_; |
| 130 | // TODO: Supply the location information for a vdex file. |
| 131 | static constexpr char kVdexLocation[] = ""; |
| 132 | std::string location = DexFile::GetMultiDexLocation(i, kVdexLocation); |
| 133 | std::unique_ptr<const DexFile> dex(DexFile::Open(dex_file_start, |
| 134 | size, |
| 135 | location, |
| 136 | GetLocationChecksum(i), |
| 137 | nullptr /*oat_dex_file*/, |
| 138 | false /*verify*/, |
| 139 | false /*verify_checksum*/, |
| 140 | error_msg)); |
| 141 | if (dex == nullptr) { |
| 142 | return false; |
| 143 | } |
| 144 | dex_files->push_back(std::move(dex)); |
| 145 | } |
| 146 | return true; |
| 147 | } |
| 148 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 149 | } // namespace art |