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. |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 20 | #include <sys/stat.h> // for mkdir() |
Andreas Gampe | 0dfc315 | 2017-04-24 07:58:06 -0700 | [diff] [blame] | 21 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 22 | #include <memory> |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 23 | #include <unordered_set> |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 24 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 25 | #include <android-base/logging.h> |
| 26 | |
Nicolas Geoffray | 28453cf | 2017-08-10 15:30:26 +0100 | [diff] [blame] | 27 | #include "base/bit_utils.h" |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 28 | #include "base/leb128.h" |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 29 | #include "base/stl_util.h" |
Andreas Gampe | f7e8223 | 2016-09-12 15:55:56 -0700 | [diff] [blame] | 30 | #include "base/unix_file/fd_file.h" |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 31 | #include "class_linker.h" |
| 32 | #include "class_loader_context.h" |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 33 | #include "dex/art_dex_file_loader.h" |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 34 | #include "dex/class_accessor-inl.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 35 | #include "dex/dex_file_loader.h" |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 36 | #include "dex_to_dex_decompiler.h" |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 37 | #include "gc/heap.h" |
| 38 | #include "gc/space/image_space.h" |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 39 | #include "quicken_info.h" |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 40 | #include "runtime.h" |
| 41 | #include "verifier/verifier_deps.h" |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 42 | |
| 43 | namespace art { |
| 44 | |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 45 | constexpr uint8_t VdexFile::VerifierDepsHeader::kVdexInvalidMagic[4]; |
| 46 | constexpr uint8_t VdexFile::VerifierDepsHeader::kVdexMagic[4]; |
| 47 | constexpr uint8_t VdexFile::VerifierDepsHeader::kVerifierDepsVersion[4]; |
| 48 | constexpr uint8_t VdexFile::VerifierDepsHeader::kDexSectionVersion[4]; |
| 49 | constexpr uint8_t VdexFile::VerifierDepsHeader::kDexSectionVersionEmpty[4]; |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 50 | |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 51 | bool VdexFile::VerifierDepsHeader::IsMagicValid() const { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 52 | return (memcmp(magic_, kVdexMagic, sizeof(kVdexMagic)) == 0); |
| 53 | } |
| 54 | |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 55 | bool VdexFile::VerifierDepsHeader::IsVerifierDepsVersionValid() const { |
| 56 | return (memcmp(verifier_deps_version_, kVerifierDepsVersion, sizeof(kVerifierDepsVersion)) == 0); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 57 | } |
| 58 | |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 59 | bool VdexFile::VerifierDepsHeader::IsDexSectionVersionValid() const { |
| 60 | return (memcmp(dex_section_version_, kDexSectionVersion, sizeof(kDexSectionVersion)) == 0) || |
| 61 | (memcmp(dex_section_version_, kDexSectionVersionEmpty, sizeof(kDexSectionVersionEmpty)) == 0); |
| 62 | } |
| 63 | |
| 64 | bool VdexFile::VerifierDepsHeader::HasDexSection() const { |
| 65 | return (memcmp(dex_section_version_, kDexSectionVersion, sizeof(kDexSectionVersion)) == 0); |
| 66 | } |
| 67 | |
| 68 | VdexFile::VerifierDepsHeader::VerifierDepsHeader(uint32_t number_of_dex_files, |
| 69 | uint32_t verifier_deps_size, |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 70 | bool has_dex_section, |
| 71 | uint32_t bootclasspath_checksums_size, |
| 72 | uint32_t class_loader_context_size) |
Nicolas Geoffray | f54e5df | 2016-12-01 10:45:08 +0000 | [diff] [blame] | 73 | : number_of_dex_files_(number_of_dex_files), |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 74 | verifier_deps_size_(verifier_deps_size), |
| 75 | bootclasspath_checksums_size_(bootclasspath_checksums_size), |
| 76 | class_loader_context_size_(class_loader_context_size) { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 77 | memcpy(magic_, kVdexMagic, sizeof(kVdexMagic)); |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 78 | memcpy(verifier_deps_version_, kVerifierDepsVersion, sizeof(kVerifierDepsVersion)); |
| 79 | if (has_dex_section) { |
| 80 | memcpy(dex_section_version_, kDexSectionVersion, sizeof(kDexSectionVersion)); |
| 81 | } else { |
| 82 | memcpy(dex_section_version_, kDexSectionVersionEmpty, sizeof(kDexSectionVersionEmpty)); |
| 83 | } |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 84 | DCHECK(IsMagicValid()); |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 85 | DCHECK(IsVerifierDepsVersionValid()); |
| 86 | DCHECK(IsDexSectionVersionValid()); |
| 87 | } |
| 88 | |
| 89 | VdexFile::DexSectionHeader::DexSectionHeader(uint32_t dex_size, |
| 90 | uint32_t dex_shared_data_size, |
| 91 | uint32_t quickening_info_size) |
| 92 | : dex_size_(dex_size), |
| 93 | dex_shared_data_size_(dex_shared_data_size), |
| 94 | quickening_info_size_(quickening_info_size) { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 95 | } |
| 96 | |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 97 | std::unique_ptr<VdexFile> VdexFile::OpenAtAddress(uint8_t* mmap_addr, |
| 98 | size_t mmap_size, |
| 99 | bool mmap_reuse, |
| 100 | const std::string& vdex_filename, |
| 101 | bool writable, |
| 102 | bool low_4gb, |
| 103 | bool unquicken, |
| 104 | std::string* error_msg) { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 105 | if (!OS::FileExists(vdex_filename.c_str())) { |
| 106 | *error_msg = "File " + vdex_filename + " does not exist."; |
| 107 | return nullptr; |
| 108 | } |
| 109 | |
| 110 | std::unique_ptr<File> vdex_file; |
| 111 | if (writable) { |
| 112 | vdex_file.reset(OS::OpenFileReadWrite(vdex_filename.c_str())); |
| 113 | } else { |
| 114 | vdex_file.reset(OS::OpenFileForReading(vdex_filename.c_str())); |
| 115 | } |
| 116 | if (vdex_file == nullptr) { |
| 117 | *error_msg = "Could not open file " + vdex_filename + |
| 118 | (writable ? " for read/write" : "for reading"); |
| 119 | return nullptr; |
| 120 | } |
| 121 | |
| 122 | int64_t vdex_length = vdex_file->GetLength(); |
| 123 | if (vdex_length == -1) { |
| 124 | *error_msg = "Could not read the length of file " + vdex_filename; |
| 125 | return nullptr; |
| 126 | } |
| 127 | |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 128 | return OpenAtAddress(mmap_addr, |
| 129 | mmap_size, |
| 130 | mmap_reuse, |
| 131 | vdex_file->Fd(), |
| 132 | vdex_length, |
| 133 | vdex_filename, |
| 134 | writable, |
| 135 | low_4gb, |
| 136 | unquicken, |
| 137 | error_msg); |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 138 | } |
| 139 | |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 140 | std::unique_ptr<VdexFile> VdexFile::OpenAtAddress(uint8_t* mmap_addr, |
| 141 | size_t mmap_size, |
| 142 | bool mmap_reuse, |
| 143 | int file_fd, |
| 144 | size_t vdex_length, |
| 145 | const std::string& vdex_filename, |
| 146 | bool writable, |
| 147 | bool low_4gb, |
| 148 | bool unquicken, |
| 149 | std::string* error_msg) { |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 150 | if (mmap_addr != nullptr && mmap_size < vdex_length) { |
| 151 | LOG(WARNING) << "Insufficient pre-allocated space to mmap vdex."; |
| 152 | mmap_addr = nullptr; |
| 153 | mmap_reuse = false; |
| 154 | } |
Andreas Gampe | c1fc449 | 2018-01-10 14:19:36 -0800 | [diff] [blame] | 155 | CHECK(!mmap_reuse || mmap_addr != nullptr); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 156 | MemMap mmap = MemMap::MapFileAtAddress( |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 157 | mmap_addr, |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 158 | vdex_length, |
| 159 | (writable || unquicken) ? PROT_READ | PROT_WRITE : PROT_READ, |
| 160 | unquicken ? MAP_PRIVATE : MAP_SHARED, |
| 161 | file_fd, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 162 | /* start= */ 0u, |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 163 | low_4gb, |
| 164 | vdex_filename.c_str(), |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 165 | mmap_reuse, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 166 | /* reservation= */ nullptr, |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 167 | error_msg); |
| 168 | if (!mmap.IsValid()) { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 169 | *error_msg = "Failed to mmap file " + vdex_filename + " : " + *error_msg; |
| 170 | return nullptr; |
| 171 | } |
| 172 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 173 | std::unique_ptr<VdexFile> vdex(new VdexFile(std::move(mmap))); |
Richard Uhler | b8ab63a | 2017-01-31 11:27:37 +0000 | [diff] [blame] | 174 | if (!vdex->IsValid()) { |
| 175 | *error_msg = "Vdex file is not valid"; |
| 176 | return nullptr; |
| 177 | } |
| 178 | |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 179 | if (unquicken && vdex->HasDexSection()) { |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 180 | std::vector<std::unique_ptr<const DexFile>> unique_ptr_dex_files; |
| 181 | if (!vdex->OpenAllDexFiles(&unique_ptr_dex_files, error_msg)) { |
| 182 | return nullptr; |
| 183 | } |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 184 | vdex->Unquicken(MakeNonOwningPointerVector(unique_ptr_dex_files), |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 185 | /* decompile_return_instruction= */ false); |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 186 | // Update the quickening info size to pretend there isn't any. |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame] | 187 | size_t offset = vdex->GetDexSectionHeaderOffset(); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 188 | reinterpret_cast<DexSectionHeader*>(vdex->mmap_.Begin() + offset)->quickening_info_size_ = 0; |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 189 | } |
| 190 | |
Richard Uhler | b8ab63a | 2017-01-31 11:27:37 +0000 | [diff] [blame] | 191 | return vdex; |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 192 | } |
| 193 | |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 194 | const uint8_t* VdexFile::GetNextDexFileData(const uint8_t* cursor) const { |
| 195 | DCHECK(cursor == nullptr || (cursor > Begin() && cursor <= End())); |
| 196 | if (cursor == nullptr) { |
| 197 | // 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] | 198 | return HasDexSection() ? DexBegin() + sizeof(QuickeningTableOffsetType) : nullptr; |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 199 | } else { |
| 200 | // Fetch the next dex file. Return null if there is none. |
| 201 | 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] | 202 | // Dex files are required to be 4 byte aligned. the OatWriter makes sure they are, see |
| 203 | // OatWriter::SeekToDexFiles. |
| 204 | data = AlignUp(data, 4); |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 205 | |
| 206 | return (data == DexEnd()) ? nullptr : data + sizeof(QuickeningTableOffsetType); |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 210 | bool VdexFile::OpenAllDexFiles(std::vector<std::unique_ptr<const DexFile>>* dex_files, |
| 211 | std::string* error_msg) { |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 212 | const ArtDexFileLoader dex_file_loader; |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 213 | size_t i = 0; |
| 214 | for (const uint8_t* dex_file_start = GetNextDexFileData(nullptr); |
| 215 | dex_file_start != nullptr; |
| 216 | dex_file_start = GetNextDexFileData(dex_file_start), ++i) { |
| 217 | size_t size = reinterpret_cast<const DexFile::Header*>(dex_file_start)->file_size_; |
| 218 | // TODO: Supply the location information for a vdex file. |
| 219 | static constexpr char kVdexLocation[] = ""; |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 220 | std::string location = DexFileLoader::GetMultiDexLocation(i, kVdexLocation); |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 221 | std::unique_ptr<const DexFile> dex(dex_file_loader.OpenWithDataSection( |
| 222 | dex_file_start, |
| 223 | size, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 224 | /*data_base=*/ nullptr, |
| 225 | /*data_size=*/ 0u, |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 226 | location, |
| 227 | GetLocationChecksum(i), |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 228 | /*oat_dex_file=*/ nullptr, |
| 229 | /*verify=*/ false, |
| 230 | /*verify_checksum=*/ false, |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 231 | error_msg)); |
David Sehr | beca4fe | 2017-03-30 17:50:24 -0700 | [diff] [blame] | 232 | if (dex == nullptr) { |
| 233 | return false; |
| 234 | } |
| 235 | dex_files->push_back(std::move(dex)); |
| 236 | } |
| 237 | return true; |
| 238 | } |
| 239 | |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 240 | void VdexFile::Unquicken(const std::vector<const DexFile*>& target_dex_files, |
| 241 | bool decompile_return_instruction) const { |
| 242 | const uint8_t* source_dex = GetNextDexFileData(nullptr); |
| 243 | for (const DexFile* target_dex : target_dex_files) { |
| 244 | UnquickenDexFile(*target_dex, source_dex, decompile_return_instruction); |
| 245 | source_dex = GetNextDexFileData(source_dex); |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 246 | } |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 247 | DCHECK(source_dex == nullptr); |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 248 | } |
| 249 | |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 250 | uint32_t VdexFile::GetQuickeningInfoTableOffset(const uint8_t* source_dex_begin) const { |
| 251 | DCHECK_GE(source_dex_begin, DexBegin()); |
| 252 | DCHECK_LT(source_dex_begin, DexEnd()); |
| 253 | return reinterpret_cast<const QuickeningTableOffsetType*>(source_dex_begin)[-1]; |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 254 | } |
| 255 | |
Mathieu Chartier | 2daa134 | 2018-02-20 16:19:28 -0800 | [diff] [blame] | 256 | CompactOffsetTable::Accessor VdexFile::GetQuickenInfoOffsetTable( |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 257 | const uint8_t* source_dex_begin, |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 258 | const ArrayRef<const uint8_t>& quickening_info) const { |
| 259 | // The offset a is in preheader right before the dex file. |
| 260 | const uint32_t offset = GetQuickeningInfoTableOffset(source_dex_begin); |
Mathieu Chartier | 2daa134 | 2018-02-20 16:19:28 -0800 | [diff] [blame] | 261 | return CompactOffsetTable::Accessor(quickening_info.SubArray(offset).data()); |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 262 | } |
Nicolas Geoffray | b4c6acb | 2017-11-10 12:48:14 +0000 | [diff] [blame] | 263 | |
Mathieu Chartier | 2daa134 | 2018-02-20 16:19:28 -0800 | [diff] [blame] | 264 | CompactOffsetTable::Accessor VdexFile::GetQuickenInfoOffsetTable( |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 265 | const DexFile& dex_file, |
| 266 | const ArrayRef<const uint8_t>& quickening_info) const { |
Mathieu Chartier | 2daa134 | 2018-02-20 16:19:28 -0800 | [diff] [blame] | 267 | return GetQuickenInfoOffsetTable(dex_file.Begin(), quickening_info); |
Nicolas Geoffray | b4c6acb | 2017-11-10 12:48:14 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | static ArrayRef<const uint8_t> GetQuickeningInfoAt(const ArrayRef<const uint8_t>& quickening_info, |
| 271 | uint32_t quickening_offset) { |
Mathieu Chartier | 2daa134 | 2018-02-20 16:19:28 -0800 | [diff] [blame] | 272 | // Subtract offset of one since 0 represents unused and cannot be in the table. |
| 273 | ArrayRef<const uint8_t> remaining = quickening_info.SubArray(quickening_offset - 1); |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 274 | return remaining.SubArray(0u, QuickenInfoTable::SizeInBytes(remaining)); |
| 275 | } |
| 276 | |
Nicolas Geoffray | b4c6acb | 2017-11-10 12:48:14 +0000 | [diff] [blame] | 277 | void VdexFile::UnquickenDexFile(const DexFile& target_dex_file, |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 278 | const DexFile& source_dex_file, |
| 279 | bool decompile_return_instruction) const { |
| 280 | UnquickenDexFile(target_dex_file, source_dex_file.Begin(), decompile_return_instruction); |
| 281 | } |
| 282 | |
| 283 | void VdexFile::UnquickenDexFile(const DexFile& target_dex_file, |
| 284 | const uint8_t* source_dex_begin, |
| 285 | bool decompile_return_instruction) const { |
| 286 | ArrayRef<const uint8_t> quickening_info = GetQuickeningInfo(); |
Mathieu Chartier | 2daa134 | 2018-02-20 16:19:28 -0800 | [diff] [blame] | 287 | if (quickening_info.empty()) { |
| 288 | // Bail early if there is no quickening info and no need to decompile. This means there is also |
| 289 | // 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] | 290 | return; |
| 291 | } |
Mathieu Chartier | a79efdb | 2018-01-18 16:31:01 -0800 | [diff] [blame] | 292 | // Make sure to not unquicken the same code item multiple times. |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 293 | std::unordered_set<const dex::CodeItem*> unquickened_code_item; |
Mathieu Chartier | 2daa134 | 2018-02-20 16:19:28 -0800 | [diff] [blame] | 294 | CompactOffsetTable::Accessor accessor(GetQuickenInfoOffsetTable(source_dex_begin, |
| 295 | quickening_info)); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 296 | for (ClassAccessor class_accessor : target_dex_file.GetClasses()) { |
| 297 | for (const ClassAccessor::Method& method : class_accessor.GetMethods()) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 298 | const dex::CodeItem* code_item = method.GetCodeItem(); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 299 | if (code_item != nullptr && unquickened_code_item.emplace(code_item).second) { |
| 300 | const uint32_t offset = accessor.GetOffset(method.GetIndex()); |
| 301 | // Offset being 0 means not quickened. |
| 302 | if (offset != 0u) { |
| 303 | ArrayRef<const uint8_t> quicken_data = GetQuickeningInfoAt(quickening_info, offset); |
| 304 | optimizer::ArtDecompileDEX( |
| 305 | target_dex_file, |
| 306 | *code_item, |
| 307 | quicken_data, |
| 308 | decompile_return_instruction); |
Nicolas Geoffray | b02ba93 | 2017-07-13 15:53:54 +0100 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 315 | ArrayRef<const uint8_t> VdexFile::GetQuickenedInfoOf(const DexFile& dex_file, |
| 316 | uint32_t dex_method_idx) const { |
Nicolas Geoffray | b4c6acb | 2017-11-10 12:48:14 +0000 | [diff] [blame] | 317 | ArrayRef<const uint8_t> quickening_info = GetQuickeningInfo(); |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 318 | if (quickening_info.empty()) { |
| 319 | return ArrayRef<const uint8_t>(); |
| 320 | } |
Mathieu Chartier | 1599a66 | 2018-04-02 17:31:34 -0700 | [diff] [blame] | 321 | CHECK_LT(dex_method_idx, dex_file.NumMethodIds()); |
Mathieu Chartier | 2daa134 | 2018-02-20 16:19:28 -0800 | [diff] [blame] | 322 | const uint32_t quickening_offset = |
| 323 | GetQuickenInfoOffsetTable(dex_file, quickening_info).GetOffset(dex_method_idx); |
| 324 | if (quickening_offset == 0u) { |
| 325 | return ArrayRef<const uint8_t>(); |
| 326 | } |
Mathieu Chartier | 210531f | 2018-01-12 10:15:51 -0800 | [diff] [blame] | 327 | return GetQuickeningInfoAt(quickening_info, quickening_offset); |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 328 | } |
| 329 | |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 330 | static std::string ComputeBootClassPathChecksumString() { |
| 331 | Runtime* const runtime = Runtime::Current(); |
| 332 | return gc::space::ImageSpace::GetBootClassPathChecksums( |
| 333 | runtime->GetHeap()->GetBootImageSpaces(), |
| 334 | runtime->GetClassLinker()->GetBootClassPath()); |
| 335 | } |
| 336 | |
| 337 | static bool CreateDirectories(const std::string& child_path, /* out */ std::string* error_msg) { |
| 338 | size_t last_slash_pos = child_path.find_last_of('/'); |
| 339 | CHECK_NE(last_slash_pos, std::string::npos) << "Invalid path: " << child_path; |
| 340 | std::string parent_path = child_path.substr(0, last_slash_pos); |
| 341 | if (OS::DirectoryExists(parent_path.c_str())) { |
| 342 | return true; |
| 343 | } else if (CreateDirectories(parent_path, error_msg)) { |
| 344 | if (mkdir(parent_path.c_str(), 0700) == 0) { |
| 345 | return true; |
| 346 | } |
| 347 | *error_msg = "Could not create directory " + parent_path; |
| 348 | return false; |
| 349 | } else { |
| 350 | return false; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | bool VdexFile::WriteToDisk(const std::string& path, |
| 355 | const std::vector<const DexFile*>& dex_files, |
| 356 | const verifier::VerifierDeps& verifier_deps, |
| 357 | const std::string& class_loader_context, |
| 358 | std::string* error_msg) { |
| 359 | std::vector<uint8_t> verifier_deps_data; |
| 360 | verifier_deps.Encode(dex_files, &verifier_deps_data); |
| 361 | |
| 362 | std::string boot_checksum = ComputeBootClassPathChecksumString(); |
| 363 | DCHECK_NE(boot_checksum, ""); |
| 364 | |
| 365 | VdexFile::VerifierDepsHeader deps_header(dex_files.size(), |
| 366 | verifier_deps_data.size(), |
| 367 | /* has_dex_section= */ false, |
| 368 | boot_checksum.size(), |
| 369 | class_loader_context.size()); |
| 370 | |
| 371 | if (!CreateDirectories(path, error_msg)) { |
| 372 | return false; |
| 373 | } |
| 374 | |
| 375 | std::unique_ptr<File> out(OS::CreateEmptyFileWriteOnly(path.c_str())); |
| 376 | if (out == nullptr) { |
| 377 | *error_msg = "Could not open " + path + " for writing"; |
| 378 | return false; |
| 379 | } |
| 380 | |
| 381 | if (!out->WriteFully(reinterpret_cast<const char*>(&deps_header), sizeof(deps_header))) { |
| 382 | *error_msg = "Could not write vdex header to " + path; |
| 383 | out->Unlink(); |
| 384 | return false; |
| 385 | } |
| 386 | |
| 387 | for (const DexFile* dex_file : dex_files) { |
| 388 | const uint32_t* checksum_ptr = &dex_file->GetHeader().checksum_; |
| 389 | static_assert(sizeof(*checksum_ptr) == sizeof(VdexFile::VdexChecksum)); |
| 390 | if (!out->WriteFully(reinterpret_cast<const char*>(checksum_ptr), |
| 391 | sizeof(VdexFile::VdexChecksum))) { |
| 392 | *error_msg = "Could not write dex checksums to " + path; |
| 393 | out->Unlink(); |
| 394 | return false; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | if (!out->WriteFully(reinterpret_cast<const char*>(verifier_deps_data.data()), |
| 399 | verifier_deps_data.size())) { |
| 400 | *error_msg = "Could not write verifier deps to " + path; |
| 401 | out->Unlink(); |
| 402 | return false; |
| 403 | } |
| 404 | |
| 405 | if (!out->WriteFully(boot_checksum.c_str(), boot_checksum.size())) { |
| 406 | *error_msg = "Could not write boot classpath checksum to " + path; |
| 407 | out->Unlink(); |
| 408 | return false; |
| 409 | } |
| 410 | |
| 411 | if (!out->WriteFully(class_loader_context.c_str(), class_loader_context.size())) { |
| 412 | *error_msg = "Could not write class loader context to " + path; |
| 413 | out->Unlink(); |
| 414 | return false; |
| 415 | } |
| 416 | |
| 417 | if (out->FlushClose() != 0) { |
| 418 | *error_msg = "Could not flush and close " + path; |
| 419 | out->Unlink(); |
| 420 | return false; |
| 421 | } |
| 422 | |
| 423 | return true; |
| 424 | } |
| 425 | |
David Brazdil | 7126c5b | 2019-03-05 00:02:51 +0000 | [diff] [blame] | 426 | bool VdexFile::MatchesDexFileChecksums(const std::vector<const DexFile::Header*>& dex_headers) |
| 427 | const { |
| 428 | const VerifierDepsHeader& header = GetVerifierDepsHeader(); |
| 429 | if (dex_headers.size() != header.GetNumberOfDexFiles()) { |
| 430 | LOG(WARNING) << "Mismatch of number of dex files in vdex (expected=" |
| 431 | << header.GetNumberOfDexFiles() << ", actual=" << dex_headers.size() << ")"; |
| 432 | return false; |
| 433 | } |
| 434 | const VdexChecksum* checksums = header.GetDexChecksumsArray(); |
| 435 | for (size_t i = 0; i < dex_headers.size(); ++i) { |
| 436 | if (checksums[i] != dex_headers[i]->checksum_) { |
| 437 | LOG(WARNING) << "Mismatch of dex file checksum in vdex (index=" << i << ")"; |
| 438 | return false; |
| 439 | } |
| 440 | } |
| 441 | return true; |
| 442 | } |
| 443 | |
| 444 | bool VdexFile::MatchesBootClassPathChecksums() const { |
| 445 | ArrayRef<const uint8_t> data = GetBootClassPathChecksumData(); |
| 446 | std::string vdex(reinterpret_cast<const char*>(data.data()), data.size()); |
| 447 | std::string runtime = ComputeBootClassPathChecksumString(); |
| 448 | if (vdex == runtime) { |
| 449 | return true; |
| 450 | } else { |
| 451 | LOG(WARNING) << "Mismatch of boot class path checksum in vdex (expected=" |
| 452 | << vdex << ", actual=" << runtime << ")"; |
| 453 | return false; |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | bool VdexFile::MatchesClassLoaderContext(const ClassLoaderContext& context) const { |
| 458 | ArrayRef<const uint8_t> data = GetClassLoaderContextData(); |
| 459 | std::string spec(reinterpret_cast<const char*>(data.data()), data.size()); |
| 460 | ClassLoaderContext::VerificationResult result = context.VerifyClassLoaderContextMatch(spec); |
| 461 | if (result != ClassLoaderContext::VerificationResult::kMismatch) { |
| 462 | return true; |
| 463 | } else { |
| 464 | LOG(WARNING) << "Mismatch of class loader context in vdex (expected=" |
| 465 | << spec << ", actual=" << context.EncodeContextForOatFile("") << ")"; |
| 466 | return false; |
| 467 | } |
| 468 | } |
| 469 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 470 | } // namespace art |