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 | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 38 | VdexFile::Header::Header(uint32_t dex_size, |
| 39 | uint32_t verifier_deps_size, |
| 40 | uint32_t quickening_info_size) |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 41 | : dex_size_(dex_size), |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 42 | verifier_deps_size_(verifier_deps_size), |
| 43 | quickening_info_size_(quickening_info_size) { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 44 | memcpy(magic_, kVdexMagic, sizeof(kVdexMagic)); |
| 45 | memcpy(version_, kVdexVersion, sizeof(kVdexVersion)); |
| 46 | DCHECK(IsMagicValid()); |
| 47 | DCHECK(IsVersionValid()); |
| 48 | } |
| 49 | |
| 50 | VdexFile* 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 Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame^] | 77 | return Open(vdex_file->Fd(), vdex_length, vdex_filename, writable, low_4gb, error_msg); |
| 78 | } |
| 79 | |
| 80 | VdexFile* 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 Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 86 | std::unique_ptr<MemMap> mmap(MemMap::MapFile(vdex_length, |
| 87 | writable ? PROT_READ | PROT_WRITE : PROT_READ, |
| 88 | MAP_SHARED, |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame^] | 89 | file_fd, |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 90 | 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 Gampe | f7e8223 | 2016-09-12 15:55:56 -0700 | [diff] [blame] | 100 | return new VdexFile(mmap.release()); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 101 | } |
| 102 | |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame^] | 103 | const 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 Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 115 | } // namespace art |