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 Sehr | 79e2607 | 2018-04-06 17:58:50 -0700 | [diff] [blame] | 25 | #include "base/mem_map.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 26 | #include "base/os.h" |
Mathieu Chartier | 2daa134 | 2018-02-20 16:19:28 -0800 | [diff] [blame] | 27 | #include "dex/compact_offset_table.h" |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 28 | #include "dex/dex_file.h" |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 29 | #include "quicken_info.h" |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 30 | |
| 31 | namespace art { |
| 32 | |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 33 | class ClassLoaderContext; |
| 34 | |
| 35 | namespace verifier { |
| 36 | class VerifierDeps; |
| 37 | } // namespace verifier |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 38 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 39 | // VDEX files contain extracted DEX files. The VdexFile class maps the file to |
| 40 | // memory and provides tools for accessing its individual sections. |
| 41 | // |
Nicolas Geoffray | 07b62e3 | 2020-11-07 15:54:08 +0000 | [diff] [blame] | 42 | // In the description below, D is the number of dex files. |
| 43 | // |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 44 | // File format: |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 45 | // VdexFile::VerifierDepsHeader fixed-length header |
| 46 | // Dex file checksums |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 47 | // |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 48 | // Optionally: |
| 49 | // VdexFile::DexSectionHeader fixed-length header |
| 50 | // |
| 51 | // quicken_table_off[0] offset into QuickeningInfo section for offset table for DEX[0]. |
| 52 | // DEX[0] array of the input DEX files, the bytecode may have been quickened. |
| 53 | // quicken_table_off[1] |
| 54 | // DEX[1] |
| 55 | // ... |
Nicolas Geoffray | 07b62e3 | 2020-11-07 15:54:08 +0000 | [diff] [blame] | 56 | // DEX[D-1] |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 57 | // |
Nicolas Geoffray | b4c6acb | 2017-11-10 12:48:14 +0000 | [diff] [blame] | 58 | // VerifierDeps |
Nicolas Geoffray | 07b62e3 | 2020-11-07 15:54:08 +0000 | [diff] [blame] | 59 | // 4-byte alignment |
| 60 | // uint32[D] DexFileDeps offsets for each dex file |
| 61 | // DexFileDeps[D][] verification dependencies |
| 62 | // 4-byte alignment |
| 63 | // uint32[class_def_size] TypeAssignability offsets (kNotVerifiedMarker for a class |
| 64 | // that isn't verified) |
| 65 | // uint32 Offset of end of AssignabilityType sets |
| 66 | // uint8[] AssignabilityType sets |
| 67 | // 4-byte alignment |
| 68 | // uint32 Number of strings |
| 69 | // uint32[] String data offsets for each string |
| 70 | // uint8[] String data |
| 71 | // |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 72 | // |
| 73 | // Optionally: |
| 74 | // QuickeningInfo |
| 75 | // uint8[D][] quickening data |
| 76 | // uint32[D][] quickening data offset tables |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 77 | |
| 78 | class VdexFile { |
| 79 | public: |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 80 | using VdexChecksum = uint32_t; |
| 81 | using QuickeningTableOffsetType = uint32_t; |
| 82 | |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 83 | struct VerifierDepsHeader { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 84 | public: |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 85 | VerifierDepsHeader(uint32_t number_of_dex_files_, |
| 86 | uint32_t verifier_deps_size, |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 87 | bool has_dex_section, |
| 88 | uint32_t bootclasspath_checksums_size = 0, |
| 89 | uint32_t class_loader_context_size = 0); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 90 | |
Nicolas Geoffray | e70dd56 | 2016-10-30 21:03:35 +0000 | [diff] [blame] | 91 | const char* GetMagic() const { return reinterpret_cast<const char*>(magic_); } |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 92 | const char* GetVerifierDepsVersion() const { |
| 93 | return reinterpret_cast<const char*>(verifier_deps_version_); |
David Brazdil | 93592f5 | 2017-12-08 10:53:27 +0000 | [diff] [blame] | 94 | } |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 95 | const char* GetDexSectionVersion() const { |
| 96 | return reinterpret_cast<const char*>(dex_section_version_); |
| 97 | } |
| 98 | bool IsMagicValid() const; |
| 99 | bool IsVerifierDepsVersionValid() const; |
| 100 | bool IsDexSectionVersionValid() const; |
| 101 | bool IsValid() const { |
| 102 | return IsMagicValid() && IsVerifierDepsVersionValid() && IsDexSectionVersionValid(); |
| 103 | } |
| 104 | bool HasDexSection() const; |
| 105 | |
| 106 | uint32_t GetVerifierDepsSize() const { return verifier_deps_size_; } |
| 107 | uint32_t GetNumberOfDexFiles() const { return number_of_dex_files_; } |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 108 | uint32_t GetBootClassPathChecksumStringSize() const { return bootclasspath_checksums_size_; } |
| 109 | uint32_t GetClassLoaderContextStringSize() const { return class_loader_context_size_; } |
David Brazdil | 93592f5 | 2017-12-08 10:53:27 +0000 | [diff] [blame] | 110 | |
| 111 | size_t GetSizeOfChecksumsSection() const { |
| 112 | return sizeof(VdexChecksum) * GetNumberOfDexFiles(); |
| 113 | } |
| 114 | |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 115 | const VdexChecksum* GetDexChecksumsArray() const { |
| 116 | return reinterpret_cast<const VdexChecksum*>( |
| 117 | reinterpret_cast<const uint8_t*>(this) + sizeof(VerifierDepsHeader)); |
| 118 | } |
| 119 | |
| 120 | VdexChecksum GetDexChecksumAtOffset(size_t idx) const { |
| 121 | DCHECK_LT(idx, GetNumberOfDexFiles()); |
| 122 | return GetDexChecksumsArray()[idx]; |
| 123 | } |
| 124 | |
Nicolas Geoffray | 36930ec | 2017-05-09 13:23:34 +0100 | [diff] [blame] | 125 | static constexpr uint8_t kVdexInvalidMagic[] = { 'w', 'd', 'e', 'x' }; |
| 126 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 127 | private: |
| 128 | static constexpr uint8_t kVdexMagic[] = { 'v', 'd', 'e', 'x' }; |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 129 | |
| 130 | // The format version of the verifier deps header and the verifier deps. |
Nicolas Geoffray | 07b62e3 | 2020-11-07 15:54:08 +0000 | [diff] [blame] | 131 | // Last update: Fast per-class access. |
| 132 | static constexpr uint8_t kVerifierDepsVersion[] = { '0', '2', '6', '\0' }; |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 133 | |
| 134 | // The format version of the dex section header and the dex section, containing |
| 135 | // both the dex code and the quickening data. |
Mathieu Chartier | c17b7d8 | 2018-03-14 14:00:04 -0700 | [diff] [blame] | 136 | // Last update: Add owned section for CompactDex. |
| 137 | static constexpr uint8_t kDexSectionVersion[] = { '0', '0', '2', '\0' }; |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 138 | |
| 139 | // If the .vdex file has no dex section (hence no dex code nor quickening data), |
| 140 | // we encode this magic version. |
| 141 | static constexpr uint8_t kDexSectionVersionEmpty[] = { '0', '0', '0', '\0' }; |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 142 | |
| 143 | uint8_t magic_[4]; |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 144 | uint8_t verifier_deps_version_[4]; |
| 145 | uint8_t dex_section_version_[4]; |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 146 | uint32_t number_of_dex_files_; |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 147 | uint32_t verifier_deps_size_; |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 148 | uint32_t bootclasspath_checksums_size_; |
| 149 | uint32_t class_loader_context_size_; |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | struct DexSectionHeader { |
| 153 | public: |
| 154 | DexSectionHeader(uint32_t dex_size, |
| 155 | uint32_t dex_shared_data_size, |
| 156 | uint32_t quickening_info_size); |
| 157 | |
| 158 | uint32_t GetDexSize() const { return dex_size_; } |
| 159 | uint32_t GetDexSharedDataSize() const { return dex_shared_data_size_; } |
| 160 | uint32_t GetQuickeningInfoSize() const { return quickening_info_size_; } |
| 161 | |
| 162 | size_t GetDexSectionSize() const { |
| 163 | return sizeof(DexSectionHeader) + |
| 164 | GetDexSize() + |
| 165 | GetDexSharedDataSize(); |
| 166 | } |
| 167 | |
| 168 | private: |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 169 | uint32_t dex_size_; |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 170 | uint32_t dex_shared_data_size_; |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 171 | uint32_t quickening_info_size_; |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 172 | |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 173 | friend class VdexFile; // For updating quickening_info_size_. |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 174 | }; |
| 175 | |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 176 | size_t GetComputedFileSize() const { |
| 177 | size_t size = sizeof(VerifierDepsHeader); |
| 178 | const VerifierDepsHeader& header = GetVerifierDepsHeader(); |
| 179 | size += header.GetVerifierDepsSize(); |
| 180 | size += header.GetSizeOfChecksumsSection(); |
| 181 | if (header.HasDexSection()) { |
| 182 | size += GetDexSectionHeader().GetDexSectionSize(); |
| 183 | size += GetDexSectionHeader().GetQuickeningInfoSize(); |
| 184 | } |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 185 | size += header.GetBootClassPathChecksumStringSize(); |
| 186 | size += header.GetClassLoaderContextStringSize(); |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 187 | return size; |
| 188 | } |
| 189 | |
Nicolas Geoffray | baeaa9b | 2018-01-26 14:31:17 +0000 | [diff] [blame] | 190 | // Note: The file is called "primary" to match the naming with profiles. |
| 191 | static const constexpr char* kVdexNameInDmFile = "primary.vdex"; |
| 192 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 193 | explicit VdexFile(MemMap&& mmap) : mmap_(std::move(mmap)) {} |
Anestis Bechtsoudis | a1f56a8 | 2017-10-08 23:37:10 +0300 | [diff] [blame] | 194 | |
Richard Uhler | b8ab63a | 2017-01-31 11:27:37 +0000 | [diff] [blame] | 195 | // 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] | 196 | // The mmap_* parameters can be left empty (nullptr/0/false) to allocate at random address. |
| 197 | static std::unique_ptr<VdexFile> OpenAtAddress(uint8_t* mmap_addr, |
| 198 | size_t mmap_size, |
| 199 | bool mmap_reuse, |
| 200 | const std::string& vdex_filename, |
| 201 | bool writable, |
| 202 | bool low_4gb, |
| 203 | bool unquicken, |
| 204 | std::string* error_msg); |
| 205 | |
| 206 | // Returns nullptr if the vdex file cannot be opened or is not valid. |
| 207 | // The mmap_* parameters can be left empty (nullptr/0/false) to allocate at random address. |
| 208 | static std::unique_ptr<VdexFile> OpenAtAddress(uint8_t* mmap_addr, |
| 209 | size_t mmap_size, |
| 210 | bool mmap_reuse, |
| 211 | int file_fd, |
| 212 | size_t vdex_length, |
| 213 | const std::string& vdex_filename, |
| 214 | bool writable, |
| 215 | bool low_4gb, |
| 216 | bool unquicken, |
| 217 | std::string* error_msg); |
| 218 | |
| 219 | // 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] | 220 | static std::unique_ptr<VdexFile> Open(const std::string& vdex_filename, |
| 221 | bool writable, |
| 222 | bool low_4gb, |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 223 | bool unquicken, |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 224 | std::string* error_msg) { |
| 225 | return OpenAtAddress(nullptr, |
| 226 | 0, |
| 227 | false, |
| 228 | vdex_filename, |
| 229 | writable, |
| 230 | low_4gb, |
| 231 | unquicken, |
| 232 | error_msg); |
| 233 | } |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 234 | |
Richard Uhler | b8ab63a | 2017-01-31 11:27:37 +0000 | [diff] [blame] | 235 | // Returns nullptr if the vdex file cannot be opened or is not valid. |
| 236 | static std::unique_ptr<VdexFile> Open(int file_fd, |
| 237 | size_t vdex_length, |
| 238 | const std::string& vdex_filename, |
| 239 | bool writable, |
| 240 | bool low_4gb, |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 241 | bool unquicken, |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 242 | std::string* error_msg) { |
| 243 | return OpenAtAddress(nullptr, |
| 244 | 0, |
| 245 | false, |
| 246 | file_fd, |
| 247 | vdex_length, |
| 248 | vdex_filename, |
| 249 | writable, |
| 250 | low_4gb, |
| 251 | unquicken, |
| 252 | error_msg); |
| 253 | } |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 254 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 255 | const uint8_t* Begin() const { return mmap_.Begin(); } |
| 256 | const uint8_t* End() const { return mmap_.End(); } |
| 257 | size_t Size() const { return mmap_.Size(); } |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 258 | |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 259 | const VerifierDepsHeader& GetVerifierDepsHeader() const { |
| 260 | return *reinterpret_cast<const VerifierDepsHeader*>(Begin()); |
| 261 | } |
| 262 | |
| 263 | uint32_t GetDexSectionHeaderOffset() const { |
| 264 | return sizeof(VerifierDepsHeader) + GetVerifierDepsHeader().GetSizeOfChecksumsSection(); |
| 265 | } |
| 266 | |
| 267 | const DexSectionHeader& GetDexSectionHeader() const { |
| 268 | DCHECK(GetVerifierDepsHeader().HasDexSection()); |
| 269 | return *reinterpret_cast<const DexSectionHeader*>(Begin() + GetDexSectionHeaderOffset()); |
| 270 | } |
| 271 | |
| 272 | const uint8_t* GetVerifierDepsStart() const { |
| 273 | const uint8_t* result = Begin() + GetDexSectionHeaderOffset(); |
| 274 | if (GetVerifierDepsHeader().HasDexSection()) { |
| 275 | // When there is a dex section, the verifier deps are after it, but before the quickening. |
| 276 | return result + GetDexSectionHeader().GetDexSectionSize(); |
| 277 | } else { |
| 278 | // When there is no dex section, the verifier deps are just after the header. |
| 279 | return result; |
| 280 | } |
Nicolas Geoffray | e70dd56 | 2016-10-30 21:03:35 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | ArrayRef<const uint8_t> GetVerifierDepsData() const { |
| 284 | return ArrayRef<const uint8_t>( |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 285 | GetVerifierDepsStart(), |
| 286 | GetVerifierDepsHeader().GetVerifierDepsSize()); |
Nicolas Geoffray | e70dd56 | 2016-10-30 21:03:35 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 289 | ArrayRef<const uint8_t> GetQuickeningInfo() const { |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 290 | return ArrayRef<const uint8_t>( |
| 291 | GetVerifierDepsData().end(), |
| 292 | GetVerifierDepsHeader().HasDexSection() |
| 293 | ? GetDexSectionHeader().GetQuickeningInfoSize() : 0); |
| 294 | } |
| 295 | |
| 296 | ArrayRef<const uint8_t> GetBootClassPathChecksumData() const { |
| 297 | return ArrayRef<const uint8_t>( |
| 298 | GetQuickeningInfo().end(), |
| 299 | GetVerifierDepsHeader().GetBootClassPathChecksumStringSize()); |
| 300 | } |
| 301 | |
| 302 | ArrayRef<const uint8_t> GetClassLoaderContextData() const { |
| 303 | return ArrayRef<const uint8_t>( |
| 304 | GetBootClassPathChecksumData().end(), |
| 305 | GetVerifierDepsHeader().GetClassLoaderContextStringSize()); |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | bool IsValid() const { |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 309 | return mmap_.Size() >= sizeof(VerifierDepsHeader) && GetVerifierDepsHeader().IsValid(); |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | // This method is for iterating over the dex files in the vdex. If `cursor` is null, |
| 313 | // the first dex file is returned. If `cursor` is not null, it must point to a dex |
| 314 | // file and this method returns the next dex file if there is one, or null if there |
| 315 | // is none. |
| 316 | const uint8_t* GetNextDexFileData(const uint8_t* cursor) const; |
| 317 | |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 318 | // Get the location checksum of the dex file number `dex_file_index`. |
| 319 | uint32_t GetLocationChecksum(uint32_t dex_file_index) const { |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 320 | DCHECK_LT(dex_file_index, GetVerifierDepsHeader().GetNumberOfDexFiles()); |
| 321 | return reinterpret_cast<const uint32_t*>(Begin() + sizeof(VerifierDepsHeader))[dex_file_index]; |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 324 | // Open all the dex files contained in this vdex file. |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 325 | bool OpenAllDexFiles(std::vector<std::unique_ptr<const DexFile>>* dex_files, |
Alex Light | abd8f05 | 2019-12-06 10:49:17 -0800 | [diff] [blame] | 326 | std::string* error_msg) const; |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 327 | |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 328 | // In-place unquicken the given `dex_files` based on `quickening_info`. |
Anestis Bechtsoudis | a1f56a8 | 2017-10-08 23:37:10 +0300 | [diff] [blame] | 329 | // `decompile_return_instruction` controls if RETURN_VOID_BARRIER instructions are |
Mathieu Chartier | 4ac9ade | 2018-07-24 10:27:21 -0700 | [diff] [blame] | 330 | // decompiled to RETURN_VOID instructions using the slower ClassAccessor instead of the faster |
| 331 | // QuickeningInfoIterator. |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 332 | // Always unquickens using the vdex dex files as the source for quicken tables. |
| 333 | void Unquicken(const std::vector<const DexFile*>& target_dex_files, |
| 334 | bool decompile_return_instruction) const; |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 335 | |
Alex Light | abd8f05 | 2019-12-06 10:49:17 -0800 | [diff] [blame] | 336 | void UnquickenInPlace(bool decompile_return_instruction) const; |
| 337 | |
Nicolas Geoffray | b4c6acb | 2017-11-10 12:48:14 +0000 | [diff] [blame] | 338 | // Fully unquicken `target_dex_file` based on `quickening_info`. |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 339 | void UnquickenDexFile(const DexFile& target_dex_file, |
| 340 | const DexFile& source_dex_file, |
| 341 | bool decompile_return_instruction) const; |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 342 | |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 343 | // Return the quickening info of a given method index (or null if it's empty). |
| 344 | ArrayRef<const uint8_t> GetQuickenedInfoOf(const DexFile& dex_file, |
| 345 | uint32_t dex_method_idx) const; |
Nicolas Geoffray | b4c6acb | 2017-11-10 12:48:14 +0000 | [diff] [blame] | 346 | |
Nicolas Geoffray | bc17727 | 2018-01-24 14:55:32 +0000 | [diff] [blame] | 347 | bool HasDexSection() const { |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 348 | return GetVerifierDepsHeader().HasDexSection(); |
Nicolas Geoffray | bc17727 | 2018-01-24 14:55:32 +0000 | [diff] [blame] | 349 | } |
| 350 | |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 351 | // Writes a vdex into `path` and returns true on success. |
| 352 | // The vdex will not contain a dex section but will store checksums of `dex_files`, |
| 353 | // encoded `verifier_deps`, as well as the current boot class path cheksum and |
| 354 | // encoded `class_loader_context`. |
| 355 | static bool WriteToDisk(const std::string& path, |
| 356 | const std::vector<const DexFile*>& dex_files, |
| 357 | const verifier::VerifierDeps& verifier_deps, |
| 358 | const std::string& class_loader_context, |
| 359 | std::string* error_msg); |
| 360 | |
David Brazdil | 7126c5b | 2019-03-05 00:02:51 +0000 | [diff] [blame] | 361 | // Returns true if the dex file checksums stored in the vdex header match |
| 362 | // the checksums in `dex_headers`. Both the number of dex files and their |
| 363 | // order must match too. |
| 364 | bool MatchesDexFileChecksums(const std::vector<const DexFile::Header*>& dex_headers) const; |
| 365 | |
| 366 | // Returns true if the boot class path checksum stored in the vdex matches |
| 367 | // the checksum of boot class path in the current runtime. |
| 368 | bool MatchesBootClassPathChecksums() const; |
| 369 | |
| 370 | // Returns true if the class loader context stored in the vdex matches `context`. |
| 371 | bool MatchesClassLoaderContext(const ClassLoaderContext& context) const; |
| 372 | |
Alex Light | abd8f05 | 2019-12-06 10:49:17 -0800 | [diff] [blame] | 373 | // Make the Vdex file & underlying dex-files RW or RO. Should only be used for in-place |
| 374 | // dequickening. |
| 375 | void AllowWriting(bool value) const; |
| 376 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 377 | private: |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 378 | uint32_t GetQuickeningInfoTableOffset(const uint8_t* source_dex_begin) const; |
| 379 | |
| 380 | // Source dex must be the in the vdex file. |
| 381 | void UnquickenDexFile(const DexFile& target_dex_file, |
| 382 | const uint8_t* source_dex_begin, |
| 383 | bool decompile_return_instruction) const; |
| 384 | |
Mathieu Chartier | 2daa134 | 2018-02-20 16:19:28 -0800 | [diff] [blame] | 385 | CompactOffsetTable::Accessor GetQuickenInfoOffsetTable( |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 386 | const DexFile& dex_file, |
| 387 | const ArrayRef<const uint8_t>& quickening_info) const; |
| 388 | |
Mathieu Chartier | 2daa134 | 2018-02-20 16:19:28 -0800 | [diff] [blame] | 389 | CompactOffsetTable::Accessor GetQuickenInfoOffsetTable( |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 390 | const uint8_t* source_dex_begin, |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 391 | const ArrayRef<const uint8_t>& quickening_info) const; |
| 392 | |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 393 | bool ContainsDexFile(const DexFile& dex_file) const; |
| 394 | |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 395 | const uint8_t* DexBegin() const { |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 396 | DCHECK(HasDexSection()); |
| 397 | return Begin() + GetDexSectionHeaderOffset() + sizeof(DexSectionHeader); |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | const uint8_t* DexEnd() const { |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 401 | DCHECK(HasDexSection()); |
| 402 | return DexBegin() + GetDexSectionHeader().GetDexSize(); |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Alex Light | abd8f05 | 2019-12-06 10:49:17 -0800 | [diff] [blame] | 405 | // mutable for AllowWriting() |
| 406 | mutable MemMap mmap_; |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 407 | |
| 408 | DISALLOW_COPY_AND_ASSIGN(VdexFile); |
| 409 | }; |
| 410 | |
| 411 | } // namespace art |
| 412 | |
| 413 | #endif // ART_RUNTIME_VDEX_FILE_H_ |