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