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 | |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 30 | class DexFile; |
| 31 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 32 | // VDEX files contain extracted DEX files. The VdexFile class maps the file to |
| 33 | // memory and provides tools for accessing its individual sections. |
| 34 | // |
| 35 | // File format: |
| 36 | // VdexFile::Header fixed-length header |
| 37 | // |
| 38 | // DEX[0] array of the input DEX files |
| 39 | // DEX[1] the bytecode may have been quickened |
| 40 | // ... |
| 41 | // DEX[D] |
Nicolas Geoffray | b4c6acb | 2017-11-10 12:48:14 +0000 | [diff] [blame] | 42 | // VerifierDeps |
| 43 | // uint8[D][] verification dependencies |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 44 | // QuickeningInfo |
Nicolas Geoffray | b4c6acb | 2017-11-10 12:48:14 +0000 | [diff] [blame] | 45 | // uint8[D][] quickening data |
| 46 | // unaligned_uint32_t[D][2][] table of offsets pair: |
| 47 | // uint32_t[0] contains original CodeItem::debug_info_off_ |
| 48 | // uint32_t[1] contains quickening data offset from the start |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 49 | // of QuickeningInfo |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 50 | |
| 51 | class VdexFile { |
| 52 | public: |
| 53 | struct Header { |
| 54 | public: |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 55 | Header(uint32_t number_of_dex_files_, |
| 56 | uint32_t dex_size, |
| 57 | uint32_t verifier_deps_size, |
| 58 | uint32_t quickening_info_size); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 59 | |
Nicolas Geoffray | e70dd56 | 2016-10-30 21:03:35 +0000 | [diff] [blame] | 60 | const char* GetMagic() const { return reinterpret_cast<const char*>(magic_); } |
| 61 | const char* GetVersion() const { return reinterpret_cast<const char*>(version_); } |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 62 | bool IsMagicValid() const; |
| 63 | bool IsVersionValid() const; |
Nicolas Geoffray | e70dd56 | 2016-10-30 21:03:35 +0000 | [diff] [blame] | 64 | bool IsValid() const { return IsMagicValid() && IsVersionValid(); } |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 65 | |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 66 | uint32_t GetDexSize() const { return dex_size_; } |
| 67 | uint32_t GetVerifierDepsSize() const { return verifier_deps_size_; } |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 68 | uint32_t GetQuickeningInfoSize() const { return quickening_info_size_; } |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 69 | uint32_t GetNumberOfDexFiles() const { return number_of_dex_files_; } |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 70 | |
David Brazdil | 93592f5 | 2017-12-08 10:53:27 +0000 | [diff] [blame^] | 71 | size_t GetComputedFileSize() const { |
| 72 | return sizeof(Header) + |
| 73 | GetSizeOfChecksumsSection() + |
| 74 | GetDexSize() + |
| 75 | GetVerifierDepsSize() + |
| 76 | GetQuickeningInfoSize(); |
| 77 | } |
| 78 | |
| 79 | size_t GetSizeOfChecksumsSection() const { |
| 80 | return sizeof(VdexChecksum) * GetNumberOfDexFiles(); |
| 81 | } |
| 82 | |
Nicolas Geoffray | 36930ec | 2017-05-09 13:23:34 +0100 | [diff] [blame] | 83 | static constexpr uint8_t kVdexInvalidMagic[] = { 'w', 'd', 'e', 'x' }; |
| 84 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 85 | private: |
| 86 | static constexpr uint8_t kVdexMagic[] = { 'v', 'd', 'e', 'x' }; |
Nicolas Geoffray | b4c6acb | 2017-11-10 12:48:14 +0000 | [diff] [blame] | 87 | // Last update: Lookup-friendly encoding for quickening info. |
| 88 | static constexpr uint8_t kVdexVersion[] = { '0', '1', '1', '\0' }; |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 89 | |
| 90 | uint8_t magic_[4]; |
| 91 | uint8_t version_[4]; |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 92 | uint32_t number_of_dex_files_; |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 93 | uint32_t dex_size_; |
| 94 | uint32_t verifier_deps_size_; |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 95 | uint32_t quickening_info_size_; |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 96 | |
| 97 | friend class VdexFile; |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 98 | }; |
| 99 | |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 100 | typedef uint32_t VdexChecksum; |
| 101 | |
Anestis Bechtsoudis | a1f56a8 | 2017-10-08 23:37:10 +0300 | [diff] [blame] | 102 | explicit VdexFile(MemMap* mmap) : mmap_(mmap) {} |
| 103 | |
Richard Uhler | b8ab63a | 2017-01-31 11:27:37 +0000 | [diff] [blame] | 104 | // Returns nullptr if the vdex file cannot be opened or is not valid. |
| 105 | static std::unique_ptr<VdexFile> Open(const std::string& vdex_filename, |
| 106 | bool writable, |
| 107 | bool low_4gb, |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 108 | bool unquicken, |
Richard Uhler | b8ab63a | 2017-01-31 11:27:37 +0000 | [diff] [blame] | 109 | std::string* error_msg); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 110 | |
Richard Uhler | b8ab63a | 2017-01-31 11:27:37 +0000 | [diff] [blame] | 111 | // Returns nullptr if the vdex file cannot be opened or is not valid. |
| 112 | static std::unique_ptr<VdexFile> Open(int file_fd, |
| 113 | size_t vdex_length, |
| 114 | const std::string& vdex_filename, |
| 115 | bool writable, |
| 116 | bool low_4gb, |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 117 | bool unquicken, |
Richard Uhler | b8ab63a | 2017-01-31 11:27:37 +0000 | [diff] [blame] | 118 | std::string* error_msg); |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 119 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 120 | const uint8_t* Begin() const { return mmap_->Begin(); } |
| 121 | const uint8_t* End() const { return mmap_->End(); } |
| 122 | size_t Size() const { return mmap_->Size(); } |
| 123 | |
Nicolas Geoffray | e70dd56 | 2016-10-30 21:03:35 +0000 | [diff] [blame] | 124 | const Header& GetHeader() const { |
| 125 | return *reinterpret_cast<const Header*>(Begin()); |
| 126 | } |
| 127 | |
| 128 | ArrayRef<const uint8_t> GetVerifierDepsData() const { |
| 129 | return ArrayRef<const uint8_t>( |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 130 | DexBegin() + GetHeader().GetDexSize(), GetHeader().GetVerifierDepsSize()); |
Nicolas Geoffray | e70dd56 | 2016-10-30 21:03:35 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 133 | ArrayRef<const uint8_t> GetQuickeningInfo() const { |
| 134 | return ArrayRef<const uint8_t>( |
| 135 | GetVerifierDepsData().data() + GetHeader().GetVerifierDepsSize(), |
| 136 | GetHeader().GetQuickeningInfoSize()); |
| 137 | } |
| 138 | |
| 139 | bool IsValid() const { |
| 140 | return mmap_->Size() >= sizeof(Header) && GetHeader().IsValid(); |
| 141 | } |
| 142 | |
| 143 | // This method is for iterating over the dex files in the vdex. If `cursor` is null, |
| 144 | // the first dex file is returned. If `cursor` is not null, it must point to a dex |
| 145 | // file and this method returns the next dex file if there is one, or null if there |
| 146 | // is none. |
| 147 | const uint8_t* GetNextDexFileData(const uint8_t* cursor) const; |
| 148 | |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 149 | // Get the location checksum of the dex file number `dex_file_index`. |
| 150 | uint32_t GetLocationChecksum(uint32_t dex_file_index) const { |
| 151 | DCHECK_LT(dex_file_index, GetHeader().GetNumberOfDexFiles()); |
| 152 | return reinterpret_cast<const uint32_t*>(Begin() + sizeof(Header))[dex_file_index]; |
| 153 | } |
| 154 | |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 155 | // Open all the dex files contained in this vdex file. |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 156 | bool OpenAllDexFiles(std::vector<std::unique_ptr<const DexFile>>* dex_files, |
| 157 | std::string* error_msg); |
| 158 | |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 159 | // In-place unquicken the given `dex_files` based on `quickening_info`. |
Anestis Bechtsoudis | a1f56a8 | 2017-10-08 23:37:10 +0300 | [diff] [blame] | 160 | // `decompile_return_instruction` controls if RETURN_VOID_BARRIER instructions are |
| 161 | // decompiled to RETURN_VOID instructions using the slower ClassDataItemIterator |
| 162 | // instead of the faster QuickeningInfoIterator. |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 163 | static void Unquicken(const std::vector<const DexFile*>& dex_files, |
Nicolas Geoffray | b4c6acb | 2017-11-10 12:48:14 +0000 | [diff] [blame] | 164 | ArrayRef<const uint8_t> quickening_info, |
Anestis Bechtsoudis | a1f56a8 | 2017-10-08 23:37:10 +0300 | [diff] [blame] | 165 | bool decompile_return_instruction); |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 166 | |
Nicolas Geoffray | b4c6acb | 2017-11-10 12:48:14 +0000 | [diff] [blame] | 167 | // Fully unquicken `target_dex_file` based on `quickening_info`. |
| 168 | static void UnquickenDexFile(const DexFile& target_dex_file, |
| 169 | ArrayRef<const uint8_t> quickening_info, |
| 170 | bool decompile_return_instruction); |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 171 | |
| 172 | // Return the quickening info of the given code item. |
| 173 | const uint8_t* GetQuickenedInfoOf(const DexFile& dex_file, uint32_t code_item_offset) const; |
| 174 | |
Nicolas Geoffray | b4c6acb | 2017-11-10 12:48:14 +0000 | [diff] [blame] | 175 | uint32_t GetDebugInfoOffset(const DexFile& dex_file, uint32_t offset_in_code_item) const; |
| 176 | |
| 177 | static bool CanEncodeQuickenedData(const DexFile& dex_file); |
| 178 | |
| 179 | static constexpr uint32_t kNoQuickeningInfoOffset = -1; |
| 180 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 181 | private: |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 182 | bool HasDexSection() const { |
| 183 | return GetHeader().GetDexSize() != 0; |
| 184 | } |
| 185 | |
| 186 | const uint8_t* DexBegin() const { |
David Brazdil | 93592f5 | 2017-12-08 10:53:27 +0000 | [diff] [blame^] | 187 | return Begin() + sizeof(Header) + GetHeader().GetSizeOfChecksumsSection(); |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | const uint8_t* DexEnd() const { |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 191 | return DexBegin() + GetHeader().GetDexSize(); |
| 192 | } |
| 193 | |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 194 | uint32_t GetDexFileIndex(const DexFile& dex_file) const; |
| 195 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 196 | std::unique_ptr<MemMap> mmap_; |
| 197 | |
| 198 | DISALLOW_COPY_AND_ASSIGN(VdexFile); |
| 199 | }; |
| 200 | |
| 201 | } // namespace art |
| 202 | |
| 203 | #endif // ART_RUNTIME_VDEX_FILE_H_ |