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" |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 27 | #include "quicken_info.h" |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 31 | class DexFile; |
| 32 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 33 | // VDEX files contain extracted DEX files. The VdexFile class maps the file to |
| 34 | // memory and provides tools for accessing its individual sections. |
| 35 | // |
| 36 | // File format: |
| 37 | // VdexFile::Header fixed-length header |
| 38 | // |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 39 | // quicken_table_off[0] offset into QuickeningInfo section for offset table for DEX[0]. |
| 40 | // DEX[0] array of the input DEX files, the bytecode may have been quickened. |
| 41 | // quicken_table_off[1] |
| 42 | // DEX[1] |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 43 | // ... |
| 44 | // DEX[D] |
Nicolas Geoffray | b4c6acb | 2017-11-10 12:48:14 +0000 | [diff] [blame] | 45 | // VerifierDeps |
| 46 | // uint8[D][] verification dependencies |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 47 | // QuickeningInfo |
Nicolas Geoffray | b4c6acb | 2017-11-10 12:48:14 +0000 | [diff] [blame] | 48 | // uint8[D][] quickening data |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 49 | // uint32[D][] quickening data offset tables |
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, |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame^] | 57 | uint32_t dex_shared_data_size, |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 58 | uint32_t verifier_deps_size, |
| 59 | uint32_t quickening_info_size); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 60 | |
Nicolas Geoffray | e70dd56 | 2016-10-30 21:03:35 +0000 | [diff] [blame] | 61 | const char* GetMagic() const { return reinterpret_cast<const char*>(magic_); } |
| 62 | const char* GetVersion() const { return reinterpret_cast<const char*>(version_); } |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 63 | bool IsMagicValid() const; |
| 64 | bool IsVersionValid() const; |
Nicolas Geoffray | e70dd56 | 2016-10-30 21:03:35 +0000 | [diff] [blame] | 65 | bool IsValid() const { return IsMagicValid() && IsVersionValid(); } |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 66 | |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 67 | uint32_t GetDexSize() const { return dex_size_; } |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame^] | 68 | uint32_t GetDexSharedDataSize() const { return dex_shared_data_size_; } |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 69 | uint32_t GetVerifierDepsSize() const { return verifier_deps_size_; } |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 70 | uint32_t GetQuickeningInfoSize() const { return quickening_info_size_; } |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 71 | uint32_t GetNumberOfDexFiles() const { return number_of_dex_files_; } |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 72 | |
David Brazdil | 93592f5 | 2017-12-08 10:53:27 +0000 | [diff] [blame] | 73 | size_t GetComputedFileSize() const { |
| 74 | return sizeof(Header) + |
| 75 | GetSizeOfChecksumsSection() + |
| 76 | GetDexSize() + |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame^] | 77 | GetDexSharedDataSize() + |
David Brazdil | 93592f5 | 2017-12-08 10:53:27 +0000 | [diff] [blame] | 78 | GetVerifierDepsSize() + |
| 79 | GetQuickeningInfoSize(); |
| 80 | } |
| 81 | |
| 82 | size_t GetSizeOfChecksumsSection() const { |
| 83 | return sizeof(VdexChecksum) * GetNumberOfDexFiles(); |
| 84 | } |
| 85 | |
Nicolas Geoffray | 36930ec | 2017-05-09 13:23:34 +0100 | [diff] [blame] | 86 | static constexpr uint8_t kVdexInvalidMagic[] = { 'w', 'd', 'e', 'x' }; |
| 87 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 88 | private: |
| 89 | static constexpr uint8_t kVdexMagic[] = { 'v', 'd', 'e', 'x' }; |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame^] | 90 | // Last update: Separate section for compact dex data. |
| 91 | static constexpr uint8_t kVdexVersion[] = { '0', '1', '6', '\0' }; |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 92 | |
| 93 | uint8_t magic_[4]; |
| 94 | uint8_t version_[4]; |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 95 | uint32_t number_of_dex_files_; |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 96 | uint32_t dex_size_; |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame^] | 97 | uint32_t dex_shared_data_size_; |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 98 | uint32_t verifier_deps_size_; |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 99 | uint32_t quickening_info_size_; |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 100 | |
| 101 | friend class VdexFile; |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 102 | }; |
| 103 | |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 104 | typedef uint32_t VdexChecksum; |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 105 | using QuickeningTableOffsetType = uint32_t; |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 106 | |
Anestis Bechtsoudis | a1f56a8 | 2017-10-08 23:37:10 +0300 | [diff] [blame] | 107 | explicit VdexFile(MemMap* mmap) : mmap_(mmap) {} |
| 108 | |
Richard Uhler | b8ab63a | 2017-01-31 11:27:37 +0000 | [diff] [blame] | 109 | // Returns nullptr if the vdex file cannot be opened or is not valid. |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 110 | // The mmap_* parameters can be left empty (nullptr/0/false) to allocate at random address. |
| 111 | static std::unique_ptr<VdexFile> OpenAtAddress(uint8_t* mmap_addr, |
| 112 | size_t mmap_size, |
| 113 | bool mmap_reuse, |
| 114 | const std::string& vdex_filename, |
| 115 | bool writable, |
| 116 | bool low_4gb, |
| 117 | bool unquicken, |
| 118 | std::string* error_msg); |
| 119 | |
| 120 | // Returns nullptr if the vdex file cannot be opened or is not valid. |
| 121 | // The mmap_* parameters can be left empty (nullptr/0/false) to allocate at random address. |
| 122 | static std::unique_ptr<VdexFile> OpenAtAddress(uint8_t* mmap_addr, |
| 123 | size_t mmap_size, |
| 124 | bool mmap_reuse, |
| 125 | int file_fd, |
| 126 | size_t vdex_length, |
| 127 | const std::string& vdex_filename, |
| 128 | bool writable, |
| 129 | bool low_4gb, |
| 130 | bool unquicken, |
| 131 | std::string* error_msg); |
| 132 | |
| 133 | // Returns nullptr if the vdex file cannot be opened or is not valid. |
Richard Uhler | b8ab63a | 2017-01-31 11:27:37 +0000 | [diff] [blame] | 134 | static std::unique_ptr<VdexFile> Open(const std::string& vdex_filename, |
| 135 | bool writable, |
| 136 | bool low_4gb, |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 137 | bool unquicken, |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 138 | std::string* error_msg) { |
| 139 | return OpenAtAddress(nullptr, |
| 140 | 0, |
| 141 | false, |
| 142 | vdex_filename, |
| 143 | writable, |
| 144 | low_4gb, |
| 145 | unquicken, |
| 146 | error_msg); |
| 147 | } |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 148 | |
Richard Uhler | b8ab63a | 2017-01-31 11:27:37 +0000 | [diff] [blame] | 149 | // Returns nullptr if the vdex file cannot be opened or is not valid. |
| 150 | static std::unique_ptr<VdexFile> Open(int file_fd, |
| 151 | size_t vdex_length, |
| 152 | const std::string& vdex_filename, |
| 153 | bool writable, |
| 154 | bool low_4gb, |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 155 | bool unquicken, |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 156 | std::string* error_msg) { |
| 157 | return OpenAtAddress(nullptr, |
| 158 | 0, |
| 159 | false, |
| 160 | file_fd, |
| 161 | vdex_length, |
| 162 | vdex_filename, |
| 163 | writable, |
| 164 | low_4gb, |
| 165 | unquicken, |
| 166 | error_msg); |
| 167 | } |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 168 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 169 | const uint8_t* Begin() const { return mmap_->Begin(); } |
| 170 | const uint8_t* End() const { return mmap_->End(); } |
| 171 | size_t Size() const { return mmap_->Size(); } |
| 172 | |
Nicolas Geoffray | e70dd56 | 2016-10-30 21:03:35 +0000 | [diff] [blame] | 173 | const Header& GetHeader() const { |
| 174 | return *reinterpret_cast<const Header*>(Begin()); |
| 175 | } |
| 176 | |
| 177 | ArrayRef<const uint8_t> GetVerifierDepsData() const { |
| 178 | return ArrayRef<const uint8_t>( |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame^] | 179 | DexBegin() + GetHeader().GetDexSize() + GetHeader().GetDexSharedDataSize(), |
| 180 | GetHeader().GetVerifierDepsSize()); |
Nicolas Geoffray | e70dd56 | 2016-10-30 21:03:35 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 183 | ArrayRef<const uint8_t> GetQuickeningInfo() const { |
| 184 | return ArrayRef<const uint8_t>( |
| 185 | GetVerifierDepsData().data() + GetHeader().GetVerifierDepsSize(), |
| 186 | GetHeader().GetQuickeningInfoSize()); |
| 187 | } |
| 188 | |
| 189 | bool IsValid() const { |
| 190 | return mmap_->Size() >= sizeof(Header) && GetHeader().IsValid(); |
| 191 | } |
| 192 | |
| 193 | // This method is for iterating over the dex files in the vdex. If `cursor` is null, |
| 194 | // the first dex file is returned. If `cursor` is not null, it must point to a dex |
| 195 | // file and this method returns the next dex file if there is one, or null if there |
| 196 | // is none. |
| 197 | const uint8_t* GetNextDexFileData(const uint8_t* cursor) const; |
| 198 | |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 199 | // Get the location checksum of the dex file number `dex_file_index`. |
| 200 | uint32_t GetLocationChecksum(uint32_t dex_file_index) const { |
| 201 | DCHECK_LT(dex_file_index, GetHeader().GetNumberOfDexFiles()); |
| 202 | return reinterpret_cast<const uint32_t*>(Begin() + sizeof(Header))[dex_file_index]; |
| 203 | } |
| 204 | |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 205 | // Open all the dex files contained in this vdex file. |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 206 | bool OpenAllDexFiles(std::vector<std::unique_ptr<const DexFile>>* dex_files, |
| 207 | std::string* error_msg); |
| 208 | |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 209 | // In-place unquicken the given `dex_files` based on `quickening_info`. |
Anestis Bechtsoudis | a1f56a8 | 2017-10-08 23:37:10 +0300 | [diff] [blame] | 210 | // `decompile_return_instruction` controls if RETURN_VOID_BARRIER instructions are |
| 211 | // decompiled to RETURN_VOID instructions using the slower ClassDataItemIterator |
| 212 | // instead of the faster QuickeningInfoIterator. |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 213 | // Always unquickens using the vdex dex files as the source for quicken tables. |
| 214 | void Unquicken(const std::vector<const DexFile*>& target_dex_files, |
| 215 | bool decompile_return_instruction) const; |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 216 | |
Nicolas Geoffray | b4c6acb | 2017-11-10 12:48:14 +0000 | [diff] [blame] | 217 | // Fully unquicken `target_dex_file` based on `quickening_info`. |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 218 | void UnquickenDexFile(const DexFile& target_dex_file, |
| 219 | const DexFile& source_dex_file, |
| 220 | bool decompile_return_instruction) const; |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 221 | |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 222 | // Return the quickening info of a given method index (or null if it's empty). |
| 223 | ArrayRef<const uint8_t> GetQuickenedInfoOf(const DexFile& dex_file, |
| 224 | uint32_t dex_method_idx) const; |
Nicolas Geoffray | b4c6acb | 2017-11-10 12:48:14 +0000 | [diff] [blame] | 225 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 226 | private: |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 227 | uint32_t GetQuickeningInfoTableOffset(const uint8_t* source_dex_begin) const; |
| 228 | |
| 229 | // Source dex must be the in the vdex file. |
| 230 | void UnquickenDexFile(const DexFile& target_dex_file, |
| 231 | const uint8_t* source_dex_begin, |
| 232 | bool decompile_return_instruction) const; |
| 233 | |
| 234 | QuickenInfoOffsetTableAccessor GetQuickenInfoOffsetTable( |
| 235 | const DexFile& dex_file, |
| 236 | const ArrayRef<const uint8_t>& quickening_info) const; |
| 237 | |
| 238 | QuickenInfoOffsetTableAccessor GetQuickenInfoOffsetTable( |
| 239 | const uint8_t* source_dex_begin, |
| 240 | uint32_t num_method_ids, |
| 241 | const ArrayRef<const uint8_t>& quickening_info) const; |
| 242 | |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 243 | bool HasDexSection() const { |
| 244 | return GetHeader().GetDexSize() != 0; |
| 245 | } |
| 246 | |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 247 | bool ContainsDexFile(const DexFile& dex_file) const; |
| 248 | |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 249 | const uint8_t* DexBegin() const { |
David Brazdil | 93592f5 | 2017-12-08 10:53:27 +0000 | [diff] [blame] | 250 | return Begin() + sizeof(Header) + GetHeader().GetSizeOfChecksumsSection(); |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | const uint8_t* DexEnd() const { |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 254 | return DexBegin() + GetHeader().GetDexSize(); |
| 255 | } |
| 256 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 257 | std::unique_ptr<MemMap> mmap_; |
| 258 | |
| 259 | DISALLOW_COPY_AND_ASSIGN(VdexFile); |
| 260 | }; |
| 261 | |
| 262 | } // namespace art |
| 263 | |
| 264 | #endif // ART_RUNTIME_VDEX_FILE_H_ |