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