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 | |
Andreas Gampe | 0dfc315 | 2017-04-24 07:58:06 -0700 | [diff] [blame] | 19 | #include <sys/mman.h> // For the PROT_* and MAP_* constants. |
| 20 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 21 | #include <memory> |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 22 | #include <unordered_set> |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 23 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 24 | #include <android-base/logging.h> |
| 25 | |
Nicolas Geoffray | 28453cf | 2017-08-10 15:30:26 +0100 | [diff] [blame] | 26 | #include "base/bit_utils.h" |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 27 | #include "base/leb128.h" |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 28 | #include "base/stl_util.h" |
Andreas Gampe | f7e8223 | 2016-09-12 15:55:56 -0700 | [diff] [blame] | 29 | #include "base/unix_file/fd_file.h" |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 30 | #include "dex/art_dex_file_loader.h" |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 31 | #include "dex/class_accessor-inl.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 32 | #include "dex/dex_file.h" |
| 33 | #include "dex/dex_file_loader.h" |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 34 | #include "dex_to_dex_decompiler.h" |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 35 | #include "quicken_info.h" |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 36 | |
| 37 | namespace art { |
| 38 | |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 39 | constexpr uint8_t VdexFile::VerifierDepsHeader::kVdexInvalidMagic[4]; |
| 40 | constexpr uint8_t VdexFile::VerifierDepsHeader::kVdexMagic[4]; |
| 41 | constexpr uint8_t VdexFile::VerifierDepsHeader::kVerifierDepsVersion[4]; |
| 42 | constexpr uint8_t VdexFile::VerifierDepsHeader::kDexSectionVersion[4]; |
| 43 | constexpr uint8_t VdexFile::VerifierDepsHeader::kDexSectionVersionEmpty[4]; |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 44 | |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 45 | bool VdexFile::VerifierDepsHeader::IsMagicValid() const { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 46 | return (memcmp(magic_, kVdexMagic, sizeof(kVdexMagic)) == 0); |
| 47 | } |
| 48 | |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 49 | bool VdexFile::VerifierDepsHeader::IsVerifierDepsVersionValid() const { |
| 50 | return (memcmp(verifier_deps_version_, kVerifierDepsVersion, sizeof(kVerifierDepsVersion)) == 0); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 51 | } |
| 52 | |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 53 | bool VdexFile::VerifierDepsHeader::IsDexSectionVersionValid() const { |
| 54 | return (memcmp(dex_section_version_, kDexSectionVersion, sizeof(kDexSectionVersion)) == 0) || |
| 55 | (memcmp(dex_section_version_, kDexSectionVersionEmpty, sizeof(kDexSectionVersionEmpty)) == 0); |
| 56 | } |
| 57 | |
| 58 | bool VdexFile::VerifierDepsHeader::HasDexSection() const { |
| 59 | return (memcmp(dex_section_version_, kDexSectionVersion, sizeof(kDexSectionVersion)) == 0); |
| 60 | } |
| 61 | |
| 62 | VdexFile::VerifierDepsHeader::VerifierDepsHeader(uint32_t number_of_dex_files, |
| 63 | uint32_t verifier_deps_size, |
| 64 | bool has_dex_section) |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 65 | : number_of_dex_files_(number_of_dex_files), |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 66 | verifier_deps_size_(verifier_deps_size) { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 67 | memcpy(magic_, kVdexMagic, sizeof(kVdexMagic)); |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 68 | memcpy(verifier_deps_version_, kVerifierDepsVersion, sizeof(kVerifierDepsVersion)); |
| 69 | if (has_dex_section) { |
| 70 | memcpy(dex_section_version_, kDexSectionVersion, sizeof(kDexSectionVersion)); |
| 71 | } else { |
| 72 | memcpy(dex_section_version_, kDexSectionVersionEmpty, sizeof(kDexSectionVersionEmpty)); |
| 73 | } |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 74 | DCHECK(IsMagicValid()); |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 75 | DCHECK(IsVerifierDepsVersionValid()); |
| 76 | DCHECK(IsDexSectionVersionValid()); |
| 77 | } |
| 78 | |
| 79 | VdexFile::DexSectionHeader::DexSectionHeader(uint32_t dex_size, |
| 80 | uint32_t dex_shared_data_size, |
| 81 | uint32_t quickening_info_size) |
| 82 | : dex_size_(dex_size), |
| 83 | dex_shared_data_size_(dex_shared_data_size), |
| 84 | quickening_info_size_(quickening_info_size) { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 85 | } |
| 86 | |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 87 | std::unique_ptr<VdexFile> VdexFile::OpenAtAddress(uint8_t* mmap_addr, |
| 88 | size_t mmap_size, |
| 89 | bool mmap_reuse, |
| 90 | const std::string& vdex_filename, |
| 91 | bool writable, |
| 92 | bool low_4gb, |
| 93 | bool unquicken, |
| 94 | std::string* error_msg) { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 95 | if (!OS::FileExists(vdex_filename.c_str())) { |
| 96 | *error_msg = "File " + vdex_filename + " does not exist."; |
| 97 | return nullptr; |
| 98 | } |
| 99 | |
| 100 | std::unique_ptr<File> vdex_file; |
| 101 | if (writable) { |
| 102 | vdex_file.reset(OS::OpenFileReadWrite(vdex_filename.c_str())); |
| 103 | } else { |
| 104 | vdex_file.reset(OS::OpenFileForReading(vdex_filename.c_str())); |
| 105 | } |
| 106 | if (vdex_file == nullptr) { |
| 107 | *error_msg = "Could not open file " + vdex_filename + |
| 108 | (writable ? " for read/write" : "for reading"); |
| 109 | return nullptr; |
| 110 | } |
| 111 | |
| 112 | int64_t vdex_length = vdex_file->GetLength(); |
| 113 | if (vdex_length == -1) { |
| 114 | *error_msg = "Could not read the length of file " + vdex_filename; |
| 115 | return nullptr; |
| 116 | } |
| 117 | |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 118 | return OpenAtAddress(mmap_addr, |
| 119 | mmap_size, |
| 120 | mmap_reuse, |
| 121 | vdex_file->Fd(), |
| 122 | vdex_length, |
| 123 | vdex_filename, |
| 124 | writable, |
| 125 | low_4gb, |
| 126 | unquicken, |
| 127 | error_msg); |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 128 | } |
| 129 | |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 130 | std::unique_ptr<VdexFile> VdexFile::OpenAtAddress(uint8_t* mmap_addr, |
| 131 | size_t mmap_size, |
| 132 | bool mmap_reuse, |
| 133 | int file_fd, |
| 134 | size_t vdex_length, |
| 135 | const std::string& vdex_filename, |
| 136 | bool writable, |
| 137 | bool low_4gb, |
| 138 | bool unquicken, |
| 139 | std::string* error_msg) { |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 140 | if (mmap_addr != nullptr && mmap_size < vdex_length) { |
| 141 | LOG(WARNING) << "Insufficient pre-allocated space to mmap vdex."; |
| 142 | mmap_addr = nullptr; |
| 143 | mmap_reuse = false; |
| 144 | } |
Andreas Gampe | c1fc449 | 2018-01-10 14:19:36 -0800 | [diff] [blame] | 145 | CHECK(!mmap_reuse || mmap_addr != nullptr); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 146 | MemMap mmap = MemMap::MapFileAtAddress( |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 147 | mmap_addr, |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 148 | vdex_length, |
| 149 | (writable || unquicken) ? PROT_READ | PROT_WRITE : PROT_READ, |
| 150 | unquicken ? MAP_PRIVATE : MAP_SHARED, |
| 151 | file_fd, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 152 | /* start= */ 0u, |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 153 | low_4gb, |
| 154 | vdex_filename.c_str(), |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 155 | mmap_reuse, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 156 | /* reservation= */ nullptr, |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 157 | error_msg); |
| 158 | if (!mmap.IsValid()) { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 159 | *error_msg = "Failed to mmap file " + vdex_filename + " : " + *error_msg; |
| 160 | return nullptr; |
| 161 | } |
| 162 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 163 | std::unique_ptr<VdexFile> vdex(new VdexFile(std::move(mmap))); |
Richard Uhler | b8ab63a | 2017-01-31 11:27:37 +0000 | [diff] [blame] | 164 | if (!vdex->IsValid()) { |
| 165 | *error_msg = "Vdex file is not valid"; |
| 166 | return nullptr; |
| 167 | } |
| 168 | |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 169 | if (unquicken && vdex->HasDexSection()) { |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 170 | std::vector<std::unique_ptr<const DexFile>> unique_ptr_dex_files; |
| 171 | if (!vdex->OpenAllDexFiles(&unique_ptr_dex_files, error_msg)) { |
| 172 | return nullptr; |
| 173 | } |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 174 | vdex->Unquicken(MakeNonOwningPointerVector(unique_ptr_dex_files), |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 175 | /* decompile_return_instruction= */ false); |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 176 | // Update the quickening info size to pretend there isn't any. |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 177 | size_t offset = vdex->GetDexSectionHeaderOffset(); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 178 | reinterpret_cast<DexSectionHeader*>(vdex->mmap_.Begin() + offset)->quickening_info_size_ = 0; |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 179 | } |
| 180 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 181 | *error_msg = "Success"; |
Richard Uhler | b8ab63a | 2017-01-31 11:27:37 +0000 | [diff] [blame] | 182 | return vdex; |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 183 | } |
| 184 | |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 185 | const uint8_t* VdexFile::GetNextDexFileData(const uint8_t* cursor) const { |
| 186 | DCHECK(cursor == nullptr || (cursor > Begin() && cursor <= End())); |
| 187 | if (cursor == nullptr) { |
| 188 | // Beginning of the iteration, return the first dex file if there is one. |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 189 | return HasDexSection() ? DexBegin() + sizeof(QuickeningTableOffsetType) : nullptr; |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 190 | } else { |
| 191 | // Fetch the next dex file. Return null if there is none. |
| 192 | const uint8_t* data = cursor + reinterpret_cast<const DexFile::Header*>(cursor)->file_size_; |
Nicolas Geoffray | 28453cf | 2017-08-10 15:30:26 +0100 | [diff] [blame] | 193 | // Dex files are required to be 4 byte aligned. the OatWriter makes sure they are, see |
| 194 | // OatWriter::SeekToDexFiles. |
| 195 | data = AlignUp(data, 4); |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 196 | |
| 197 | return (data == DexEnd()) ? nullptr : data + sizeof(QuickeningTableOffsetType); |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 201 | bool VdexFile::OpenAllDexFiles(std::vector<std::unique_ptr<const DexFile>>* dex_files, |
| 202 | std::string* error_msg) { |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 203 | const ArtDexFileLoader dex_file_loader; |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 204 | size_t i = 0; |
| 205 | for (const uint8_t* dex_file_start = GetNextDexFileData(nullptr); |
| 206 | dex_file_start != nullptr; |
| 207 | dex_file_start = GetNextDexFileData(dex_file_start), ++i) { |
| 208 | size_t size = reinterpret_cast<const DexFile::Header*>(dex_file_start)->file_size_; |
| 209 | // TODO: Supply the location information for a vdex file. |
| 210 | static constexpr char kVdexLocation[] = ""; |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 211 | std::string location = DexFileLoader::GetMultiDexLocation(i, kVdexLocation); |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 212 | std::unique_ptr<const DexFile> dex(dex_file_loader.OpenWithDataSection( |
| 213 | dex_file_start, |
| 214 | size, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 215 | /*data_base=*/ nullptr, |
| 216 | /*data_size=*/ 0u, |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 217 | location, |
| 218 | GetLocationChecksum(i), |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 219 | /*oat_dex_file=*/ nullptr, |
| 220 | /*verify=*/ false, |
| 221 | /*verify_checksum=*/ false, |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 222 | error_msg)); |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 223 | if (dex == nullptr) { |
| 224 | return false; |
| 225 | } |
| 226 | dex_files->push_back(std::move(dex)); |
| 227 | } |
| 228 | return true; |
| 229 | } |
| 230 | |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 231 | void VdexFile::Unquicken(const std::vector<const DexFile*>& target_dex_files, |
| 232 | bool decompile_return_instruction) const { |
| 233 | const uint8_t* source_dex = GetNextDexFileData(nullptr); |
| 234 | for (const DexFile* target_dex : target_dex_files) { |
| 235 | UnquickenDexFile(*target_dex, source_dex, decompile_return_instruction); |
| 236 | source_dex = GetNextDexFileData(source_dex); |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 237 | } |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 238 | DCHECK(source_dex == nullptr); |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 239 | } |
| 240 | |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 241 | uint32_t VdexFile::GetQuickeningInfoTableOffset(const uint8_t* source_dex_begin) const { |
| 242 | DCHECK_GE(source_dex_begin, DexBegin()); |
| 243 | DCHECK_LT(source_dex_begin, DexEnd()); |
| 244 | return reinterpret_cast<const QuickeningTableOffsetType*>(source_dex_begin)[-1]; |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 245 | } |
| 246 | |
Mathieu Chartier | 2daa134 | 2018-02-20 16:19:28 -0800 | [diff] [blame] | 247 | CompactOffsetTable::Accessor VdexFile::GetQuickenInfoOffsetTable( |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 248 | const uint8_t* source_dex_begin, |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 249 | const ArrayRef<const uint8_t>& quickening_info) const { |
| 250 | // The offset a is in preheader right before the dex file. |
| 251 | const uint32_t offset = GetQuickeningInfoTableOffset(source_dex_begin); |
Mathieu Chartier | 2daa134 | 2018-02-20 16:19:28 -0800 | [diff] [blame] | 252 | return CompactOffsetTable::Accessor(quickening_info.SubArray(offset).data()); |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 253 | } |
Nicolas Geoffray | b4c6acb | 2017-11-10 12:48:14 +0000 | [diff] [blame] | 254 | |
Mathieu Chartier | 2daa134 | 2018-02-20 16:19:28 -0800 | [diff] [blame] | 255 | CompactOffsetTable::Accessor VdexFile::GetQuickenInfoOffsetTable( |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 256 | const DexFile& dex_file, |
| 257 | const ArrayRef<const uint8_t>& quickening_info) const { |
Mathieu Chartier | 2daa134 | 2018-02-20 16:19:28 -0800 | [diff] [blame] | 258 | return GetQuickenInfoOffsetTable(dex_file.Begin(), quickening_info); |
Nicolas Geoffray | b4c6acb | 2017-11-10 12:48:14 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | static ArrayRef<const uint8_t> GetQuickeningInfoAt(const ArrayRef<const uint8_t>& quickening_info, |
| 262 | uint32_t quickening_offset) { |
Mathieu Chartier | 2daa134 | 2018-02-20 16:19:28 -0800 | [diff] [blame] | 263 | // Subtract offset of one since 0 represents unused and cannot be in the table. |
| 264 | ArrayRef<const uint8_t> remaining = quickening_info.SubArray(quickening_offset - 1); |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 265 | return remaining.SubArray(0u, QuickenInfoTable::SizeInBytes(remaining)); |
| 266 | } |
| 267 | |
Nicolas Geoffray | b4c6acb | 2017-11-10 12:48:14 +0000 | [diff] [blame] | 268 | void VdexFile::UnquickenDexFile(const DexFile& target_dex_file, |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 269 | const DexFile& source_dex_file, |
| 270 | bool decompile_return_instruction) const { |
| 271 | UnquickenDexFile(target_dex_file, source_dex_file.Begin(), decompile_return_instruction); |
| 272 | } |
| 273 | |
| 274 | void VdexFile::UnquickenDexFile(const DexFile& target_dex_file, |
| 275 | const uint8_t* source_dex_begin, |
| 276 | bool decompile_return_instruction) const { |
| 277 | ArrayRef<const uint8_t> quickening_info = GetQuickeningInfo(); |
Mathieu Chartier | 2daa134 | 2018-02-20 16:19:28 -0800 | [diff] [blame] | 278 | if (quickening_info.empty()) { |
| 279 | // Bail early if there is no quickening info and no need to decompile. This means there is also |
| 280 | // no RETURN_VOID to decompile since the empty table takes a non zero amount of space. |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 281 | return; |
| 282 | } |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 283 | // Make sure to not unquicken the same code item multiple times. |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 284 | std::unordered_set<const dex::CodeItem*> unquickened_code_item; |
Mathieu Chartier | 2daa134 | 2018-02-20 16:19:28 -0800 | [diff] [blame] | 285 | CompactOffsetTable::Accessor accessor(GetQuickenInfoOffsetTable(source_dex_begin, |
| 286 | quickening_info)); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 287 | for (ClassAccessor class_accessor : target_dex_file.GetClasses()) { |
| 288 | for (const ClassAccessor::Method& method : class_accessor.GetMethods()) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 289 | const dex::CodeItem* code_item = method.GetCodeItem(); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 290 | if (code_item != nullptr && unquickened_code_item.emplace(code_item).second) { |
| 291 | const uint32_t offset = accessor.GetOffset(method.GetIndex()); |
| 292 | // Offset being 0 means not quickened. |
| 293 | if (offset != 0u) { |
| 294 | ArrayRef<const uint8_t> quicken_data = GetQuickeningInfoAt(quickening_info, offset); |
| 295 | optimizer::ArtDecompileDEX( |
| 296 | target_dex_file, |
| 297 | *code_item, |
| 298 | quicken_data, |
| 299 | decompile_return_instruction); |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 306 | ArrayRef<const uint8_t> VdexFile::GetQuickenedInfoOf(const DexFile& dex_file, |
| 307 | uint32_t dex_method_idx) const { |
Nicolas Geoffray | b4c6acb | 2017-11-10 12:48:14 +0000 | [diff] [blame] | 308 | ArrayRef<const uint8_t> quickening_info = GetQuickeningInfo(); |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 309 | if (quickening_info.empty()) { |
| 310 | return ArrayRef<const uint8_t>(); |
| 311 | } |
Mathieu Chartier | 1599a66 | 2018-04-02 17:31:34 -0700 | [diff] [blame] | 312 | CHECK_LT(dex_method_idx, dex_file.NumMethodIds()); |
Mathieu Chartier | 2daa134 | 2018-02-20 16:19:28 -0800 | [diff] [blame] | 313 | const uint32_t quickening_offset = |
| 314 | GetQuickenInfoOffsetTable(dex_file, quickening_info).GetOffset(dex_method_idx); |
| 315 | if (quickening_offset == 0u) { |
| 316 | return ArrayRef<const uint8_t>(); |
| 317 | } |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 318 | return GetQuickeningInfoAt(quickening_info, quickening_offset); |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 319 | } |
| 320 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 321 | } // namespace art |