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 | #ifndef ART_RUNTIME_VDEX_FILE_H_ |
| 18 | #define ART_RUNTIME_VDEX_FILE_H_ |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | #include <string> |
| 22 | |
Nicolas Geoffray | e70dd56 | 2016-10-30 21:03:35 +0000 | [diff] [blame] | 23 | #include "base/array_ref.h" |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 24 | #include "base/macros.h" |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 25 | #include "mem_map.h" |
| 26 | #include "os.h" |
| 27 | |
| 28 | namespace art { |
| 29 | |
| 30 | // VDEX files contain extracted DEX files. The VdexFile class maps the file to |
| 31 | // memory and provides tools for accessing its individual sections. |
| 32 | // |
| 33 | // File format: |
| 34 | // VdexFile::Header fixed-length header |
| 35 | // |
| 36 | // DEX[0] array of the input DEX files |
| 37 | // DEX[1] the bytecode may have been quickened |
| 38 | // ... |
| 39 | // DEX[D] |
| 40 | // |
| 41 | |
| 42 | class VdexFile { |
| 43 | public: |
| 44 | struct Header { |
| 45 | public: |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 46 | Header(uint32_t number_of_dex_files_, |
| 47 | uint32_t dex_size, |
| 48 | uint32_t verifier_deps_size, |
| 49 | uint32_t quickening_info_size); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 50 | |
Nicolas Geoffray | e70dd56 | 2016-10-30 21:03:35 +0000 | [diff] [blame] | 51 | const char* GetMagic() const { return reinterpret_cast<const char*>(magic_); } |
| 52 | const char* GetVersion() const { return reinterpret_cast<const char*>(version_); } |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 53 | bool IsMagicValid() const; |
| 54 | bool IsVersionValid() const; |
Nicolas Geoffray | e70dd56 | 2016-10-30 21:03:35 +0000 | [diff] [blame] | 55 | bool IsValid() const { return IsMagicValid() && IsVersionValid(); } |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 56 | |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 57 | uint32_t GetDexSize() const { return dex_size_; } |
| 58 | uint32_t GetVerifierDepsSize() const { return verifier_deps_size_; } |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 59 | uint32_t GetQuickeningInfoSize() const { return quickening_info_size_; } |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 60 | uint32_t GetNumberOfDexFiles() const { return number_of_dex_files_; } |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 61 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 62 | private: |
| 63 | static constexpr uint8_t kVdexMagic[] = { 'v', 'd', 'e', 'x' }; |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 64 | static constexpr uint8_t kVdexVersion[] = { '0', '0', '1', '\0' }; |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 65 | |
| 66 | uint8_t magic_[4]; |
| 67 | uint8_t version_[4]; |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 68 | uint32_t number_of_dex_files_; |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 69 | uint32_t dex_size_; |
| 70 | uint32_t verifier_deps_size_; |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 71 | uint32_t quickening_info_size_; |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 72 | }; |
| 73 | |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 74 | typedef uint32_t VdexChecksum; |
| 75 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 76 | static VdexFile* Open(const std::string& vdex_filename, |
| 77 | bool writable, |
| 78 | bool low_4gb, |
| 79 | std::string* error_msg); |
| 80 | |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 81 | static VdexFile* Open(int file_fd, |
| 82 | size_t vdex_length, |
| 83 | const std::string& vdex_filename, |
| 84 | bool writable, |
| 85 | bool low_4gb, |
| 86 | std::string* error_msg); |
| 87 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 88 | const uint8_t* Begin() const { return mmap_->Begin(); } |
| 89 | const uint8_t* End() const { return mmap_->End(); } |
| 90 | size_t Size() const { return mmap_->Size(); } |
| 91 | |
Nicolas Geoffray | e70dd56 | 2016-10-30 21:03:35 +0000 | [diff] [blame] | 92 | const Header& GetHeader() const { |
| 93 | return *reinterpret_cast<const Header*>(Begin()); |
| 94 | } |
| 95 | |
| 96 | ArrayRef<const uint8_t> GetVerifierDepsData() const { |
| 97 | return ArrayRef<const uint8_t>( |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 98 | DexBegin() + GetHeader().GetDexSize(), GetHeader().GetVerifierDepsSize()); |
Nicolas Geoffray | e70dd56 | 2016-10-30 21:03:35 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 101 | ArrayRef<const uint8_t> GetQuickeningInfo() const { |
| 102 | return ArrayRef<const uint8_t>( |
| 103 | GetVerifierDepsData().data() + GetHeader().GetVerifierDepsSize(), |
| 104 | GetHeader().GetQuickeningInfoSize()); |
| 105 | } |
| 106 | |
| 107 | bool IsValid() const { |
| 108 | return mmap_->Size() >= sizeof(Header) && GetHeader().IsValid(); |
| 109 | } |
| 110 | |
| 111 | // This method is for iterating over the dex files in the vdex. If `cursor` is null, |
| 112 | // the first dex file is returned. If `cursor` is not null, it must point to a dex |
| 113 | // file and this method returns the next dex file if there is one, or null if there |
| 114 | // is none. |
| 115 | const uint8_t* GetNextDexFileData(const uint8_t* cursor) const; |
| 116 | |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 117 | // Get the location checksum of the dex file number `dex_file_index`. |
| 118 | uint32_t GetLocationChecksum(uint32_t dex_file_index) const { |
| 119 | DCHECK_LT(dex_file_index, GetHeader().GetNumberOfDexFiles()); |
| 120 | return reinterpret_cast<const uint32_t*>(Begin() + sizeof(Header))[dex_file_index]; |
| 121 | } |
| 122 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 123 | private: |
Andreas Gampe | f7e8223 | 2016-09-12 15:55:56 -0700 | [diff] [blame] | 124 | explicit VdexFile(MemMap* mmap) : mmap_(mmap) {} |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 125 | |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 126 | bool HasDexSection() const { |
| 127 | return GetHeader().GetDexSize() != 0; |
| 128 | } |
| 129 | |
| 130 | const uint8_t* DexBegin() const { |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 131 | return Begin() + sizeof(Header) + GetSizeOfChecksumsSection(); |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | const uint8_t* DexEnd() const { |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 135 | return DexBegin() + GetHeader().GetDexSize(); |
| 136 | } |
| 137 | |
| 138 | size_t GetSizeOfChecksumsSection() const { |
| 139 | return sizeof(VdexChecksum) * GetHeader().GetNumberOfDexFiles(); |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 140 | } |
| 141 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 142 | std::unique_ptr<MemMap> mmap_; |
| 143 | |
| 144 | DISALLOW_COPY_AND_ASSIGN(VdexFile); |
| 145 | }; |
| 146 | |
| 147 | } // namespace art |
| 148 | |
| 149 | #endif // ART_RUNTIME_VDEX_FILE_H_ |