Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | */ |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "dex_file.h" |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 18 | |
| 19 | #include <fcntl.h> |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 20 | #include <limits.h> |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 21 | #include <stdio.h> |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 22 | #include <stdlib.h> |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 23 | #include <string.h> |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 24 | #include <sys/file.h> |
Andreas Gampe | 0dfc315 | 2017-04-24 07:58:06 -0700 | [diff] [blame] | 25 | #include <sys/mman.h> // For the PROT_* and MAP_* constants. |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 26 | #include <sys/stat.h> |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 27 | #include <zlib.h> |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 28 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 29 | #include <memory> |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 30 | #include <sstream> |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 31 | #include <type_traits> |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 32 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 33 | #include "android-base/stringprintf.h" |
| 34 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 35 | #include "base/enums.h" |
Vladimir Marko | 5096e66 | 2015-12-08 19:25:49 +0000 | [diff] [blame] | 36 | #include "base/file_magic.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 37 | #include "base/logging.h" |
Andreas Gampe | 5678db5 | 2017-06-08 14:11:18 -0700 | [diff] [blame^] | 38 | #include "base/stl_util.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 39 | #include "base/systrace.h" |
Andreas Gampe | 43e10b0 | 2016-07-15 17:17:34 -0700 | [diff] [blame] | 40 | #include "base/unix_file/fd_file.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 41 | #include "dex_file-inl.h" |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 42 | #include "dex_file_verifier.h" |
Vladimir Marko | 3a21e38 | 2016-09-02 12:38:38 +0100 | [diff] [blame] | 43 | #include "jvalue.h" |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 44 | #include "leb128.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 45 | #include "os.h" |
Ian Rogers | a672490 | 2013-09-23 09:23:37 -0700 | [diff] [blame] | 46 | #include "utf-inl.h" |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 47 | #include "utils.h" |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 48 | #include "zip_archive.h" |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 49 | |
| 50 | namespace art { |
| 51 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 52 | using android::base::StringPrintf; |
| 53 | |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 54 | static_assert(sizeof(dex::StringIndex) == sizeof(uint32_t), "StringIndex size is wrong"); |
| 55 | static_assert(std::is_trivially_copyable<dex::StringIndex>::value, "StringIndex not trivial"); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 56 | static_assert(sizeof(dex::TypeIndex) == sizeof(uint16_t), "TypeIndex size is wrong"); |
| 57 | static_assert(std::is_trivially_copyable<dex::TypeIndex>::value, "TypeIndex not trivial"); |
| 58 | |
David Sehr | 733ddb2 | 2016-09-19 15:02:18 -0700 | [diff] [blame] | 59 | static constexpr OatDexFile* kNoOatDexFile = nullptr; |
| 60 | |
| 61 | const char* DexFile::kClassesDex = "classes.dex"; |
| 62 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 63 | const uint8_t DexFile::kDexMagic[] = { 'd', 'e', 'x', '\n' }; |
Alex Light | c496181 | 2016-03-23 10:20:41 -0700 | [diff] [blame] | 64 | const uint8_t DexFile::kDexMagicVersions[DexFile::kNumDexVersions][DexFile::kDexVersionLen] = { |
| 65 | {'0', '3', '5', '\0'}, |
| 66 | // Dex version 036 skipped because of an old dalvik bug on some versions of android where dex |
| 67 | // files with that version number would erroneously be accepted and run. |
Narayan Kamath | 52e6650 | 2016-08-01 14:20:31 +0100 | [diff] [blame] | 68 | {'0', '3', '7', '\0'}, |
| 69 | // Dex version 038: Android "O" and beyond. |
| 70 | {'0', '3', '8', '\0'} |
Alex Light | c496181 | 2016-03-23 10:20:41 -0700 | [diff] [blame] | 71 | }; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 72 | |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 73 | uint32_t DexFile::CalculateChecksum() const { |
| 74 | const uint32_t non_sum = OFFSETOF_MEMBER(DexFile::Header, signature_); |
| 75 | const uint8_t* non_sum_ptr = Begin() + non_sum; |
| 76 | return adler32(adler32(0L, Z_NULL, 0), non_sum_ptr, Size() - non_sum); |
| 77 | } |
| 78 | |
Vladimir Marko | 3a21e38 | 2016-09-02 12:38:38 +0100 | [diff] [blame] | 79 | struct DexFile::AnnotationValue { |
| 80 | JValue value_; |
| 81 | uint8_t type_; |
| 82 | }; |
| 83 | |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 84 | bool DexFile::GetMultiDexChecksums(const char* filename, |
| 85 | std::vector<uint32_t>* checksums, |
| 86 | std::string* error_msg) { |
| 87 | CHECK(checksums != nullptr); |
| 88 | uint32_t magic; |
| 89 | |
| 90 | File fd = OpenAndReadMagic(filename, &magic, error_msg); |
Andreas Gampe | 43e10b0 | 2016-07-15 17:17:34 -0700 | [diff] [blame] | 91 | if (fd.Fd() == -1) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 92 | DCHECK(!error_msg->empty()); |
Brian Carlstrom | 7c3d13a | 2013-09-04 17:15:11 -0700 | [diff] [blame] | 93 | return false; |
| 94 | } |
| 95 | if (IsZipMagic(magic)) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 96 | std::unique_ptr<ZipArchive> zip_archive( |
Andreas Gampe | 43e10b0 | 2016-07-15 17:17:34 -0700 | [diff] [blame] | 97 | ZipArchive::OpenFromFd(fd.Release(), filename, error_msg)); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 98 | if (zip_archive.get() == nullptr) { |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 99 | *error_msg = StringPrintf("Failed to open zip archive '%s' (error msg: %s)", filename, |
Andreas Gampe | 0b3ed3d | 2015-03-04 15:38:51 -0800 | [diff] [blame] | 100 | error_msg->c_str()); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 101 | return false; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 102 | } |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 103 | |
| 104 | uint32_t i = 0; |
| 105 | std::string zip_entry_name = GetMultiDexClassesDexName(i++); |
| 106 | std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(zip_entry_name.c_str(), error_msg)); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 107 | if (zip_entry.get() == nullptr) { |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 108 | *error_msg = StringPrintf("Zip archive '%s' doesn't contain %s (error msg: %s)", filename, |
| 109 | zip_entry_name.c_str(), error_msg->c_str()); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 110 | return false; |
| 111 | } |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 112 | |
| 113 | do { |
| 114 | checksums->push_back(zip_entry->GetCrc32()); |
| 115 | zip_entry_name = DexFile::GetMultiDexClassesDexName(i++); |
| 116 | zip_entry.reset(zip_archive->Find(zip_entry_name.c_str(), error_msg)); |
| 117 | } while (zip_entry.get() != nullptr); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 118 | return true; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 119 | } |
Brian Carlstrom | 7c3d13a | 2013-09-04 17:15:11 -0700 | [diff] [blame] | 120 | if (IsDexMagic(magic)) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 121 | std::unique_ptr<const DexFile> dex_file( |
Andreas Gampe | 43e10b0 | 2016-07-15 17:17:34 -0700 | [diff] [blame] | 122 | DexFile::OpenFile(fd.Release(), filename, false, false, error_msg)); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 123 | if (dex_file.get() == nullptr) { |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 124 | return false; |
| 125 | } |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 126 | checksums->push_back(dex_file->GetHeader().checksum_); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 127 | return true; |
| 128 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 129 | *error_msg = StringPrintf("Expected valid zip or dex file: '%s'", filename); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 130 | return false; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 133 | int DexFile::GetPermissions() const { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 134 | if (mem_map_.get() == nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 135 | return 0; |
| 136 | } else { |
| 137 | return mem_map_->GetProtect(); |
| 138 | } |
| 139 | } |
| 140 | |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 141 | bool DexFile::IsReadOnly() const { |
| 142 | return GetPermissions() == PROT_READ; |
| 143 | } |
| 144 | |
Brian Carlstrom | e0948e1 | 2013-08-29 09:36:15 -0700 | [diff] [blame] | 145 | bool DexFile::EnableWrite() const { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 146 | CHECK(IsReadOnly()); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 147 | if (mem_map_.get() == nullptr) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 148 | return false; |
| 149 | } else { |
Brian Carlstrom | e0948e1 | 2013-08-29 09:36:15 -0700 | [diff] [blame] | 150 | return mem_map_->Protect(PROT_READ | PROT_WRITE); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
Brian Carlstrom | e0948e1 | 2013-08-29 09:36:15 -0700 | [diff] [blame] | 154 | bool DexFile::DisableWrite() const { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 155 | CHECK(!IsReadOnly()); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 156 | if (mem_map_.get() == nullptr) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 157 | return false; |
| 158 | } else { |
Brian Carlstrom | e0948e1 | 2013-08-29 09:36:15 -0700 | [diff] [blame] | 159 | return mem_map_->Protect(PROT_READ); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
David Sehr | 733ddb2 | 2016-09-19 15:02:18 -0700 | [diff] [blame] | 163 | |
| 164 | std::unique_ptr<const DexFile> DexFile::Open(const uint8_t* base, |
| 165 | size_t size, |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 166 | const std::string& location, |
| 167 | uint32_t location_checksum, |
| 168 | const OatDexFile* oat_dex_file, |
| 169 | bool verify, |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 170 | bool verify_checksum, |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 171 | std::string* error_msg) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 172 | ScopedTrace trace(std::string("Open dex file from RAM ") + location); |
David Sehr | 733ddb2 | 2016-09-19 15:02:18 -0700 | [diff] [blame] | 173 | return OpenCommon(base, |
| 174 | size, |
| 175 | location, |
| 176 | location_checksum, |
| 177 | oat_dex_file, |
| 178 | verify, |
| 179 | verify_checksum, |
| 180 | error_msg); |
Orion Hodson | a4c2a05 | 2016-08-17 10:51:42 +0100 | [diff] [blame] | 181 | } |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 182 | |
Orion Hodson | a4c2a05 | 2016-08-17 10:51:42 +0100 | [diff] [blame] | 183 | std::unique_ptr<const DexFile> DexFile::Open(const std::string& location, |
| 184 | uint32_t location_checksum, |
David Sehr | 733ddb2 | 2016-09-19 15:02:18 -0700 | [diff] [blame] | 185 | std::unique_ptr<MemMap> map, |
Orion Hodson | a4c2a05 | 2016-08-17 10:51:42 +0100 | [diff] [blame] | 186 | bool verify, |
| 187 | bool verify_checksum, |
| 188 | std::string* error_msg) { |
| 189 | ScopedTrace trace(std::string("Open dex file from mapped-memory ") + location); |
David Sehr | 733ddb2 | 2016-09-19 15:02:18 -0700 | [diff] [blame] | 190 | CHECK(map.get() != nullptr); |
Jeff Hao | 41b2f53 | 2017-03-02 16:36:31 -0800 | [diff] [blame] | 191 | |
| 192 | if (map->Size() < sizeof(DexFile::Header)) { |
| 193 | *error_msg = StringPrintf( |
| 194 | "DexFile: failed to open dex file '%s' that is too short to have a header", |
| 195 | location.c_str()); |
| 196 | return nullptr; |
| 197 | } |
| 198 | |
David Sehr | 733ddb2 | 2016-09-19 15:02:18 -0700 | [diff] [blame] | 199 | std::unique_ptr<DexFile> dex_file = OpenCommon(map->Begin(), |
| 200 | map->Size(), |
| 201 | location, |
| 202 | location_checksum, |
| 203 | kNoOatDexFile, |
| 204 | verify, |
| 205 | verify_checksum, |
| 206 | error_msg); |
| 207 | if (dex_file != nullptr) { |
Andreas Gampe | 8d01c37 | 2017-05-30 13:21:28 -0700 | [diff] [blame] | 208 | dex_file->mem_map_ = std::move(map); |
Orion Hodson | a4c2a05 | 2016-08-17 10:51:42 +0100 | [diff] [blame] | 209 | } |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 210 | return dex_file; |
| 211 | } |
| 212 | |
David Sehr | 733ddb2 | 2016-09-19 15:02:18 -0700 | [diff] [blame] | 213 | bool DexFile::Open(const char* filename, |
| 214 | const std::string& location, |
| 215 | bool verify_checksum, |
| 216 | std::string* error_msg, |
| 217 | std::vector<std::unique_ptr<const DexFile>>* dex_files) { |
| 218 | ScopedTrace trace(std::string("Open dex file ") + std::string(location)); |
| 219 | DCHECK(dex_files != nullptr) << "DexFile::Open: out-param is nullptr"; |
| 220 | uint32_t magic; |
| 221 | File fd = OpenAndReadMagic(filename, &magic, error_msg); |
| 222 | if (fd.Fd() == -1) { |
| 223 | DCHECK(!error_msg->empty()); |
| 224 | return false; |
| 225 | } |
| 226 | if (IsZipMagic(magic)) { |
| 227 | return DexFile::OpenZip(fd.Release(), location, verify_checksum, error_msg, dex_files); |
| 228 | } |
| 229 | if (IsDexMagic(magic)) { |
| 230 | std::unique_ptr<const DexFile> dex_file(DexFile::OpenFile(fd.Release(), |
| 231 | location, |
| 232 | /* verify */ true, |
| 233 | verify_checksum, |
| 234 | error_msg)); |
| 235 | if (dex_file.get() != nullptr) { |
| 236 | dex_files->push_back(std::move(dex_file)); |
| 237 | return true; |
| 238 | } else { |
| 239 | return false; |
Vladimir Marko | fd99576 | 2013-11-06 16:36:36 +0000 | [diff] [blame] | 240 | } |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 241 | } |
David Sehr | 733ddb2 | 2016-09-19 15:02:18 -0700 | [diff] [blame] | 242 | *error_msg = StringPrintf("Expected valid zip or dex file: '%s'", filename); |
| 243 | return false; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 244 | } |
| 245 | |
David Sehr | 733ddb2 | 2016-09-19 15:02:18 -0700 | [diff] [blame] | 246 | std::unique_ptr<const DexFile> DexFile::OpenDex(int fd, |
| 247 | const std::string& location, |
| 248 | bool verify_checksum, |
| 249 | std::string* error_msg) { |
| 250 | ScopedTrace trace("Open dex file " + std::string(location)); |
| 251 | return OpenFile(fd, location, true /* verify */, verify_checksum, error_msg); |
| 252 | } |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 253 | |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 254 | bool DexFile::OpenZip(int fd, |
| 255 | const std::string& location, |
| 256 | bool verify_checksum, |
| 257 | std::string* error_msg, |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 258 | std::vector<std::unique_ptr<const DexFile>>* dex_files) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 259 | ScopedTrace trace("Dex file open Zip " + std::string(location)); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 260 | DCHECK(dex_files != nullptr) << "DexFile::OpenZip: out-param is nullptr"; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 261 | std::unique_ptr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(fd, location.c_str(), error_msg)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 262 | if (zip_archive.get() == nullptr) { |
| 263 | DCHECK(!error_msg->empty()); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 264 | return false; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 265 | } |
David Sehr | 733ddb2 | 2016-09-19 15:02:18 -0700 | [diff] [blame] | 266 | return DexFile::OpenAllDexFilesFromZip(*zip_archive, |
| 267 | location, |
| 268 | verify_checksum, |
| 269 | error_msg, |
| 270 | dex_files); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 271 | } |
| 272 | |
David Sehr | 733ddb2 | 2016-09-19 15:02:18 -0700 | [diff] [blame] | 273 | std::unique_ptr<const DexFile> DexFile::OpenFile(int fd, |
| 274 | const std::string& location, |
| 275 | bool verify, |
| 276 | bool verify_checksum, |
| 277 | std::string* error_msg) { |
| 278 | ScopedTrace trace(std::string("Open dex file ") + std::string(location)); |
| 279 | CHECK(!location.empty()); |
| 280 | std::unique_ptr<MemMap> map; |
| 281 | { |
| 282 | File delayed_close(fd, /* check_usage */ false); |
| 283 | struct stat sbuf; |
| 284 | memset(&sbuf, 0, sizeof(sbuf)); |
| 285 | if (fstat(fd, &sbuf) == -1) { |
| 286 | *error_msg = StringPrintf("DexFile: fstat '%s' failed: %s", location.c_str(), |
| 287 | strerror(errno)); |
| 288 | return nullptr; |
| 289 | } |
| 290 | if (S_ISDIR(sbuf.st_mode)) { |
| 291 | *error_msg = StringPrintf("Attempt to mmap directory '%s'", location.c_str()); |
| 292 | return nullptr; |
| 293 | } |
| 294 | size_t length = sbuf.st_size; |
| 295 | map.reset(MemMap::MapFile(length, |
| 296 | PROT_READ, |
| 297 | MAP_PRIVATE, |
| 298 | fd, |
| 299 | 0, |
| 300 | /*low_4gb*/false, |
| 301 | location.c_str(), |
| 302 | error_msg)); |
| 303 | if (map == nullptr) { |
| 304 | DCHECK(!error_msg->empty()); |
| 305 | return nullptr; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | if (map->Size() < sizeof(DexFile::Header)) { |
| 310 | *error_msg = StringPrintf( |
| 311 | "DexFile: failed to open dex file '%s' that is too short to have a header", |
| 312 | location.c_str()); |
| 313 | return nullptr; |
| 314 | } |
| 315 | |
| 316 | const Header* dex_header = reinterpret_cast<const Header*>(map->Begin()); |
| 317 | |
| 318 | std::unique_ptr<DexFile> dex_file = OpenCommon(map->Begin(), |
| 319 | map->Size(), |
| 320 | location, |
| 321 | dex_header->checksum_, |
| 322 | kNoOatDexFile, |
| 323 | verify, |
| 324 | verify_checksum, |
| 325 | error_msg); |
| 326 | if (dex_file != nullptr) { |
Andreas Gampe | 8d01c37 | 2017-05-30 13:21:28 -0700 | [diff] [blame] | 327 | dex_file->mem_map_ = std::move(map); |
David Sehr | 733ddb2 | 2016-09-19 15:02:18 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | return dex_file; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 331 | } |
| 332 | |
David Sehr | 733ddb2 | 2016-09-19 15:02:18 -0700 | [diff] [blame] | 333 | std::unique_ptr<const DexFile> DexFile::OpenOneDexFileFromZip(const ZipArchive& zip_archive, |
| 334 | const char* entry_name, |
| 335 | const std::string& location, |
| 336 | bool verify_checksum, |
| 337 | std::string* error_msg, |
| 338 | ZipOpenErrorCode* error_code) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 339 | ScopedTrace trace("Dex file open from Zip Archive " + std::string(location)); |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 340 | CHECK(!location.empty()); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 341 | std::unique_ptr<ZipEntry> zip_entry(zip_archive.Find(entry_name, error_msg)); |
David Sehr | 9fddd36 | 2016-09-22 14:05:37 -0700 | [diff] [blame] | 342 | if (zip_entry == nullptr) { |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 343 | *error_code = ZipOpenErrorCode::kEntryNotFound; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 344 | return nullptr; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 345 | } |
ganxiaolin | cd16d0a | 2016-07-18 11:21:44 +0800 | [diff] [blame] | 346 | if (zip_entry->GetUncompressedLength() == 0) { |
| 347 | *error_msg = StringPrintf("Dex file '%s' has zero length", location.c_str()); |
| 348 | *error_code = ZipOpenErrorCode::kDexFileError; |
| 349 | return nullptr; |
| 350 | } |
Igor Murashkin | 271a0f8 | 2017-02-14 21:14:17 +0000 | [diff] [blame] | 351 | |
| 352 | std::unique_ptr<MemMap> map; |
| 353 | if (zip_entry->IsUncompressed()) { |
| 354 | if (!zip_entry->IsAlignedTo(alignof(Header))) { |
| 355 | // Do not mmap unaligned ZIP entries because |
| 356 | // doing so would fail dex verification which requires 4 byte alignment. |
| 357 | LOG(WARNING) << "Can't mmap dex file " << location << "!" << entry_name << " directly; " |
| 358 | << "please zipalign to " << alignof(Header) << " bytes. " |
| 359 | << "Falling back to extracting file."; |
| 360 | } else { |
| 361 | // Map uncompressed files within zip as file-backed to avoid a dirty copy. |
| 362 | map.reset(zip_entry->MapDirectlyFromFile(location.c_str(), /*out*/error_msg)); |
| 363 | if (map == nullptr) { |
| 364 | LOG(WARNING) << "Can't mmap dex file " << location << "!" << entry_name << " directly; " |
| 365 | << "is your ZIP file corrupted? Falling back to extraction."; |
| 366 | // Try again with Extraction which still has a chance of recovery. |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | if (map == nullptr) { |
| 372 | // Default path for compressed ZIP entries, |
| 373 | // and fallback for stored ZIP entries. |
| 374 | map.reset(zip_entry->ExtractToMemMap(location.c_str(), entry_name, error_msg)); |
| 375 | } |
| 376 | |
David Sehr | 9fddd36 | 2016-09-22 14:05:37 -0700 | [diff] [blame] | 377 | if (map == nullptr) { |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 378 | *error_msg = StringPrintf("Failed to extract '%s' from '%s': %s", entry_name, location.c_str(), |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 379 | error_msg->c_str()); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 380 | *error_code = ZipOpenErrorCode::kExtractToMemoryError; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 381 | return nullptr; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 382 | } |
David Sehr | 733ddb2 | 2016-09-19 15:02:18 -0700 | [diff] [blame] | 383 | VerifyResult verify_result; |
| 384 | std::unique_ptr<DexFile> dex_file = OpenCommon(map->Begin(), |
| 385 | map->Size(), |
| 386 | location, |
| 387 | zip_entry->GetCrc32(), |
| 388 | kNoOatDexFile, |
| 389 | /* verify */ true, |
| 390 | verify_checksum, |
| 391 | error_msg, |
| 392 | &verify_result); |
David Sehr | 9fddd36 | 2016-09-22 14:05:37 -0700 | [diff] [blame] | 393 | if (dex_file == nullptr) { |
| 394 | if (verify_result == VerifyResult::kVerifyNotAttempted) { |
| 395 | *error_code = ZipOpenErrorCode::kDexFileError; |
| 396 | } else { |
| 397 | *error_code = ZipOpenErrorCode::kVerifyError; |
| 398 | } |
| 399 | return nullptr; |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame] | 400 | } |
Andreas Gampe | 8d01c37 | 2017-05-30 13:21:28 -0700 | [diff] [blame] | 401 | dex_file->mem_map_ = std::move(map); |
Brian Carlstrom | e0948e1 | 2013-08-29 09:36:15 -0700 | [diff] [blame] | 402 | if (!dex_file->DisableWrite()) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 403 | *error_msg = StringPrintf("Failed to make dex file '%s' read only", location.c_str()); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 404 | *error_code = ZipOpenErrorCode::kMakeReadOnlyError; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 405 | return nullptr; |
Brian Carlstrom | e0948e1 | 2013-08-29 09:36:15 -0700 | [diff] [blame] | 406 | } |
| 407 | CHECK(dex_file->IsReadOnly()) << location; |
David Sehr | 733ddb2 | 2016-09-19 15:02:18 -0700 | [diff] [blame] | 408 | if (verify_result != VerifyResult::kVerifySucceeded) { |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 409 | *error_code = ZipOpenErrorCode::kVerifyError; |
Brian Carlstrom | d6cec90 | 2014-05-25 16:08:51 -0700 | [diff] [blame] | 410 | return nullptr; |
| 411 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 412 | *error_code = ZipOpenErrorCode::kNoError; |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 413 | return dex_file; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Andreas Gampe | 90e3404 | 2015-04-27 20:01:52 -0700 | [diff] [blame] | 416 | // Technically we do not have a limitation with respect to the number of dex files that can be in a |
| 417 | // multidex APK. However, it's bad practice, as each dex file requires its own tables for symbols |
| 418 | // (types, classes, methods, ...) and dex caches. So warn the user that we open a zip with what |
| 419 | // seems an excessive number. |
| 420 | static constexpr size_t kWarnOnManyDexFilesThreshold = 100; |
| 421 | |
David Sehr | 733ddb2 | 2016-09-19 15:02:18 -0700 | [diff] [blame] | 422 | bool DexFile::OpenAllDexFilesFromZip(const ZipArchive& zip_archive, |
| 423 | const std::string& location, |
| 424 | bool verify_checksum, |
| 425 | std::string* error_msg, |
| 426 | std::vector<std::unique_ptr<const DexFile>>* dex_files) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 427 | ScopedTrace trace("Dex file open from Zip " + std::string(location)); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 428 | DCHECK(dex_files != nullptr) << "DexFile::OpenFromZip: out-param is nullptr"; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 429 | ZipOpenErrorCode error_code; |
David Sehr | 733ddb2 | 2016-09-19 15:02:18 -0700 | [diff] [blame] | 430 | std::unique_ptr<const DexFile> dex_file(OpenOneDexFileFromZip(zip_archive, |
| 431 | kClassesDex, |
| 432 | location, |
| 433 | verify_checksum, |
| 434 | error_msg, |
| 435 | &error_code)); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 436 | if (dex_file.get() == nullptr) { |
| 437 | return false; |
| 438 | } else { |
| 439 | // Had at least classes.dex. |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 440 | dex_files->push_back(std::move(dex_file)); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 441 | |
| 442 | // Now try some more. |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 443 | |
| 444 | // We could try to avoid std::string allocations by working on a char array directly. As we |
| 445 | // do not expect a lot of iterations, this seems too involved and brittle. |
| 446 | |
Andreas Gampe | 90e3404 | 2015-04-27 20:01:52 -0700 | [diff] [blame] | 447 | for (size_t i = 1; ; ++i) { |
| 448 | std::string name = GetMultiDexClassesDexName(i); |
| 449 | std::string fake_location = GetMultiDexLocation(i, location.c_str()); |
David Sehr | 733ddb2 | 2016-09-19 15:02:18 -0700 | [diff] [blame] | 450 | std::unique_ptr<const DexFile> next_dex_file(OpenOneDexFileFromZip(zip_archive, |
| 451 | name.c_str(), |
| 452 | fake_location, |
| 453 | verify_checksum, |
| 454 | error_msg, |
| 455 | &error_code)); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 456 | if (next_dex_file.get() == nullptr) { |
| 457 | if (error_code != ZipOpenErrorCode::kEntryNotFound) { |
David Sehr | c922922 | 2017-02-14 10:57:47 -0800 | [diff] [blame] | 458 | LOG(WARNING) << "Zip open failed: " << *error_msg; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 459 | } |
| 460 | break; |
| 461 | } else { |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 462 | dex_files->push_back(std::move(next_dex_file)); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Andreas Gampe | 90e3404 | 2015-04-27 20:01:52 -0700 | [diff] [blame] | 465 | if (i == kWarnOnManyDexFilesThreshold) { |
| 466 | LOG(WARNING) << location << " has in excess of " << kWarnOnManyDexFilesThreshold |
| 467 | << " dex files. Please consider coalescing and shrinking the number to " |
| 468 | " avoid runtime overhead."; |
| 469 | } |
| 470 | |
| 471 | if (i == std::numeric_limits<size_t>::max()) { |
| 472 | LOG(ERROR) << "Overflow in number of dex files!"; |
| 473 | break; |
| 474 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | return true; |
| 478 | } |
| 479 | } |
| 480 | |
David Sehr | 733ddb2 | 2016-09-19 15:02:18 -0700 | [diff] [blame] | 481 | std::unique_ptr<DexFile> DexFile::OpenCommon(const uint8_t* base, |
| 482 | size_t size, |
| 483 | const std::string& location, |
| 484 | uint32_t location_checksum, |
| 485 | const OatDexFile* oat_dex_file, |
| 486 | bool verify, |
| 487 | bool verify_checksum, |
| 488 | std::string* error_msg, |
| 489 | VerifyResult* verify_result) { |
David Sehr | 9fddd36 | 2016-09-22 14:05:37 -0700 | [diff] [blame] | 490 | if (verify_result != nullptr) { |
| 491 | *verify_result = VerifyResult::kVerifyNotAttempted; |
| 492 | } |
David Sehr | 733ddb2 | 2016-09-19 15:02:18 -0700 | [diff] [blame] | 493 | std::unique_ptr<DexFile> dex_file(new DexFile(base, |
| 494 | size, |
| 495 | location, |
| 496 | location_checksum, |
| 497 | oat_dex_file)); |
| 498 | if (dex_file == nullptr) { |
| 499 | *error_msg = StringPrintf("Failed to open dex file '%s' from memory: %s", location.c_str(), |
| 500 | error_msg->c_str()); |
| 501 | return nullptr; |
| 502 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 503 | if (!dex_file->Init(error_msg)) { |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 504 | dex_file.reset(); |
David Sehr | 733ddb2 | 2016-09-19 15:02:18 -0700 | [diff] [blame] | 505 | return nullptr; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 506 | } |
David Sehr | 733ddb2 | 2016-09-19 15:02:18 -0700 | [diff] [blame] | 507 | if (verify && !DexFileVerifier::Verify(dex_file.get(), |
| 508 | dex_file->Begin(), |
| 509 | dex_file->Size(), |
| 510 | location.c_str(), |
| 511 | verify_checksum, |
| 512 | error_msg)) { |
| 513 | if (verify_result != nullptr) { |
| 514 | *verify_result = VerifyResult::kVerifyFailed; |
| 515 | } |
| 516 | return nullptr; |
| 517 | } |
| 518 | if (verify_result != nullptr) { |
| 519 | *verify_result = VerifyResult::kVerifySucceeded; |
| 520 | } |
| 521 | return dex_file; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 522 | } |
| 523 | |
David Sehr | 733ddb2 | 2016-09-19 15:02:18 -0700 | [diff] [blame] | 524 | DexFile::DexFile(const uint8_t* base, |
| 525 | size_t size, |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 526 | const std::string& location, |
| 527 | uint32_t location_checksum, |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 528 | const OatDexFile* oat_dex_file) |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 529 | : begin_(base), |
| 530 | size_(size), |
| 531 | location_(location), |
| 532 | location_checksum_(location_checksum), |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 533 | header_(reinterpret_cast<const Header*>(base)), |
| 534 | string_ids_(reinterpret_cast<const StringId*>(base + header_->string_ids_off_)), |
| 535 | type_ids_(reinterpret_cast<const TypeId*>(base + header_->type_ids_off_)), |
| 536 | field_ids_(reinterpret_cast<const FieldId*>(base + header_->field_ids_off_)), |
| 537 | method_ids_(reinterpret_cast<const MethodId*>(base + header_->method_ids_off_)), |
| 538 | proto_ids_(reinterpret_cast<const ProtoId*>(base + header_->proto_ids_off_)), |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 539 | class_defs_(reinterpret_cast<const ClassDef*>(base + header_->class_defs_off_)), |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 540 | method_handles_(nullptr), |
| 541 | num_method_handles_(0), |
| 542 | call_site_ids_(nullptr), |
| 543 | num_call_site_ids_(0), |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 544 | oat_dex_file_(oat_dex_file) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 545 | CHECK(begin_ != nullptr) << GetLocation(); |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 546 | CHECK_GT(size_, 0U) << GetLocation(); |
Igor Murashkin | 271a0f8 | 2017-02-14 21:14:17 +0000 | [diff] [blame] | 547 | // Check base (=header) alignment. |
| 548 | // Must be 4-byte aligned to avoid undefined behavior when accessing |
| 549 | // any of the sections via a pointer. |
| 550 | CHECK_ALIGNED(begin_, alignof(Header)); |
| 551 | |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 552 | InitializeSectionsFromMapList(); |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 553 | } |
| 554 | |
Jesse Wilson | 6bf1915 | 2011-09-29 13:12:33 -0400 | [diff] [blame] | 555 | DexFile::~DexFile() { |
Elliott Hughes | 8cef0b8 | 2011-10-11 19:24:00 -0700 | [diff] [blame] | 556 | // We don't call DeleteGlobalRef on dex_object_ because we're only called by DestroyJavaVM, and |
| 557 | // that's only called after DetachCurrentThread, which means there's no JNIEnv. We could |
| 558 | // re-attach, but cleaning up these global references is not obviously useful. It's not as if |
| 559 | // the global reference table is otherwise empty! |
Jesse Wilson | 6bf1915 | 2011-09-29 13:12:33 -0400 | [diff] [blame] | 560 | } |
| 561 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 562 | bool DexFile::Init(std::string* error_msg) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 563 | if (!CheckMagicAndVersion(error_msg)) { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 564 | return false; |
| 565 | } |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 566 | return true; |
| 567 | } |
| 568 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 569 | bool DexFile::CheckMagicAndVersion(std::string* error_msg) const { |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 570 | if (!IsMagicValid(header_->magic_)) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 571 | std::ostringstream oss; |
| 572 | oss << "Unrecognized magic number in " << GetLocation() << ":" |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 573 | << " " << header_->magic_[0] |
| 574 | << " " << header_->magic_[1] |
| 575 | << " " << header_->magic_[2] |
| 576 | << " " << header_->magic_[3]; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 577 | *error_msg = oss.str(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 578 | return false; |
| 579 | } |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 580 | if (!IsVersionValid(header_->magic_)) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 581 | std::ostringstream oss; |
| 582 | oss << "Unrecognized version number in " << GetLocation() << ":" |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 583 | << " " << header_->magic_[4] |
| 584 | << " " << header_->magic_[5] |
| 585 | << " " << header_->magic_[6] |
| 586 | << " " << header_->magic_[7]; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 587 | *error_msg = oss.str(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 588 | return false; |
| 589 | } |
| 590 | return true; |
| 591 | } |
| 592 | |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 593 | void DexFile::InitializeSectionsFromMapList() { |
| 594 | const MapList* map_list = reinterpret_cast<const MapList*>(begin_ + header_->map_off_); |
Jeff Hao | a4cd677 | 2017-04-13 14:36:29 -0700 | [diff] [blame] | 595 | if (header_->map_off_ == 0 || header_->map_off_ > size_) { |
| 596 | // Bad offset. The dex file verifier runs after this method and will reject the file. |
| 597 | return; |
| 598 | } |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 599 | const size_t count = map_list->size_; |
| 600 | |
| 601 | size_t map_limit = header_->map_off_ + count * sizeof(MapItem); |
| 602 | if (header_->map_off_ >= map_limit || map_limit > size_) { |
| 603 | // Overflow or out out of bounds. The dex file verifier runs after |
| 604 | // this method and will reject the file as it is malformed. |
| 605 | return; |
| 606 | } |
| 607 | |
| 608 | for (size_t i = 0; i < count; ++i) { |
| 609 | const MapItem& map_item = map_list->list_[i]; |
| 610 | if (map_item.type_ == kDexTypeMethodHandleItem) { |
| 611 | method_handles_ = reinterpret_cast<const MethodHandleItem*>(begin_ + map_item.offset_); |
| 612 | num_method_handles_ = map_item.size_; |
| 613 | } else if (map_item.type_ == kDexTypeCallSiteIdItem) { |
| 614 | call_site_ids_ = reinterpret_cast<const CallSiteIdItem*>(begin_ + map_item.offset_); |
| 615 | num_call_site_ids_ = map_item.size_; |
| 616 | } |
| 617 | } |
| 618 | } |
| 619 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 620 | bool DexFile::IsMagicValid(const uint8_t* magic) { |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 621 | return (memcmp(magic, kDexMagic, sizeof(kDexMagic)) == 0); |
| 622 | } |
| 623 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 624 | bool DexFile::IsVersionValid(const uint8_t* magic) { |
| 625 | const uint8_t* version = &magic[sizeof(kDexMagic)]; |
Alex Light | c496181 | 2016-03-23 10:20:41 -0700 | [diff] [blame] | 626 | for (uint32_t i = 0; i < kNumDexVersions; i++) { |
| 627 | if (memcmp(version, kDexMagicVersions[i], kDexVersionLen) == 0) { |
| 628 | return true; |
| 629 | } |
| 630 | } |
| 631 | return false; |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 632 | } |
| 633 | |
Andreas Gampe | 76ed99d | 2016-03-28 18:31:29 -0700 | [diff] [blame] | 634 | uint32_t DexFile::Header::GetVersion() const { |
| 635 | const char* version = reinterpret_cast<const char*>(&magic_[sizeof(kDexMagic)]); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 636 | return atoi(version); |
| 637 | } |
| 638 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 639 | const DexFile::ClassDef* DexFile::FindClassDef(dex::TypeIndex type_idx) const { |
David Sehr | 9aa352e | 2016-09-15 18:13:52 -0700 | [diff] [blame] | 640 | size_t num_class_defs = NumClassDefs(); |
Roland Levillain | ab880f4 | 2016-05-12 16:24:36 +0100 | [diff] [blame] | 641 | // Fast path for rare no class defs case. |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 642 | if (num_class_defs == 0) { |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 643 | return nullptr; |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 644 | } |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 645 | for (size_t i = 0; i < num_class_defs; ++i) { |
| 646 | const ClassDef& class_def = GetClassDef(i); |
| 647 | if (class_def.class_idx_ == type_idx) { |
| 648 | return &class_def; |
| 649 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 650 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 651 | return nullptr; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 652 | } |
| 653 | |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 654 | uint32_t DexFile::FindCodeItemOffset(const DexFile::ClassDef& class_def, |
| 655 | uint32_t method_idx) const { |
| 656 | const uint8_t* class_data = GetClassData(class_def); |
| 657 | CHECK(class_data != nullptr); |
| 658 | ClassDataItemIterator it(*this, class_data); |
| 659 | // Skip fields |
| 660 | while (it.HasNextStaticField()) { |
| 661 | it.Next(); |
| 662 | } |
| 663 | while (it.HasNextInstanceField()) { |
| 664 | it.Next(); |
| 665 | } |
| 666 | while (it.HasNextDirectMethod()) { |
| 667 | if (it.GetMemberIndex() == method_idx) { |
| 668 | return it.GetMethodCodeItemOffset(); |
| 669 | } |
| 670 | it.Next(); |
| 671 | } |
| 672 | while (it.HasNextVirtualMethod()) { |
| 673 | if (it.GetMemberIndex() == method_idx) { |
| 674 | return it.GetMethodCodeItemOffset(); |
| 675 | } |
| 676 | it.Next(); |
| 677 | } |
| 678 | LOG(FATAL) << "Unable to find method " << method_idx; |
| 679 | UNREACHABLE(); |
| 680 | } |
| 681 | |
Bharadwaj Kalandhabhatta | 043c908 | 2017-06-06 17:14:12 -0700 | [diff] [blame] | 682 | uint32_t DexFile::GetCodeItemSize(const DexFile::CodeItem& code_item) { |
| 683 | uintptr_t code_item_start = reinterpret_cast<uintptr_t>(&code_item); |
| 684 | uint32_t insns_size = code_item.insns_size_in_code_units_; |
| 685 | uint32_t tries_size = code_item.tries_size_; |
| 686 | const uint8_t* handler_data = GetCatchHandlerData(code_item, 0); |
| 687 | |
| 688 | if (tries_size == 0 || handler_data == nullptr) { |
| 689 | uintptr_t insns_end = reinterpret_cast<uintptr_t>(&code_item.insns_[insns_size]); |
| 690 | return insns_end - code_item_start; |
| 691 | } else { |
| 692 | // Get the start of the handler data. |
| 693 | uint32_t handlers_size = DecodeUnsignedLeb128(&handler_data); |
| 694 | // Manually read each handler. |
| 695 | for (uint32_t i = 0; i < handlers_size; ++i) { |
| 696 | int32_t uleb128_count = DecodeSignedLeb128(&handler_data) * 2; |
| 697 | if (uleb128_count <= 0) { |
| 698 | uleb128_count = -uleb128_count + 1; |
| 699 | } |
| 700 | for (int32_t j = 0; j < uleb128_count; ++j) { |
| 701 | DecodeUnsignedLeb128(&handler_data); |
| 702 | } |
| 703 | } |
| 704 | return reinterpret_cast<uintptr_t>(handler_data) - code_item_start; |
| 705 | } |
| 706 | } |
| 707 | |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 708 | const DexFile::FieldId* DexFile::FindFieldId(const DexFile::TypeId& declaring_klass, |
Roland Levillain | ab880f4 | 2016-05-12 16:24:36 +0100 | [diff] [blame] | 709 | const DexFile::StringId& name, |
| 710 | const DexFile::TypeId& type) const { |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 711 | // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 712 | const dex::TypeIndex class_idx = GetIndexForTypeId(declaring_klass); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 713 | const dex::StringIndex name_idx = GetIndexForStringId(name); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 714 | const dex::TypeIndex type_idx = GetIndexForTypeId(type); |
Ian Rogers | f8582c3 | 2013-05-29 16:33:03 -0700 | [diff] [blame] | 715 | int32_t lo = 0; |
| 716 | int32_t hi = NumFieldIds() - 1; |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 717 | while (hi >= lo) { |
Ian Rogers | f8582c3 | 2013-05-29 16:33:03 -0700 | [diff] [blame] | 718 | int32_t mid = (hi + lo) / 2; |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 719 | const DexFile::FieldId& field = GetFieldId(mid); |
| 720 | if (class_idx > field.class_idx_) { |
| 721 | lo = mid + 1; |
| 722 | } else if (class_idx < field.class_idx_) { |
| 723 | hi = mid - 1; |
| 724 | } else { |
| 725 | if (name_idx > field.name_idx_) { |
| 726 | lo = mid + 1; |
| 727 | } else if (name_idx < field.name_idx_) { |
| 728 | hi = mid - 1; |
| 729 | } else { |
| 730 | if (type_idx > field.type_idx_) { |
| 731 | lo = mid + 1; |
| 732 | } else if (type_idx < field.type_idx_) { |
| 733 | hi = mid - 1; |
| 734 | } else { |
| 735 | return &field; |
| 736 | } |
| 737 | } |
| 738 | } |
| 739 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 740 | return nullptr; |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | const DexFile::MethodId* DexFile::FindMethodId(const DexFile::TypeId& declaring_klass, |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 744 | const DexFile::StringId& name, |
| 745 | const DexFile::ProtoId& signature) const { |
| 746 | // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 747 | const dex::TypeIndex class_idx = GetIndexForTypeId(declaring_klass); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 748 | const dex::StringIndex name_idx = GetIndexForStringId(name); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 749 | const uint16_t proto_idx = GetIndexForProtoId(signature); |
Ian Rogers | f8582c3 | 2013-05-29 16:33:03 -0700 | [diff] [blame] | 750 | int32_t lo = 0; |
| 751 | int32_t hi = NumMethodIds() - 1; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 752 | while (hi >= lo) { |
Ian Rogers | f8582c3 | 2013-05-29 16:33:03 -0700 | [diff] [blame] | 753 | int32_t mid = (hi + lo) / 2; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 754 | const DexFile::MethodId& method = GetMethodId(mid); |
| 755 | if (class_idx > method.class_idx_) { |
| 756 | lo = mid + 1; |
| 757 | } else if (class_idx < method.class_idx_) { |
| 758 | hi = mid - 1; |
| 759 | } else { |
| 760 | if (name_idx > method.name_idx_) { |
| 761 | lo = mid + 1; |
| 762 | } else if (name_idx < method.name_idx_) { |
| 763 | hi = mid - 1; |
| 764 | } else { |
| 765 | if (proto_idx > method.proto_idx_) { |
| 766 | lo = mid + 1; |
| 767 | } else if (proto_idx < method.proto_idx_) { |
| 768 | hi = mid - 1; |
| 769 | } else { |
| 770 | return &method; |
| 771 | } |
| 772 | } |
| 773 | } |
| 774 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 775 | return nullptr; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 776 | } |
| 777 | |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 778 | const DexFile::StringId* DexFile::FindStringId(const char* string) const { |
Ian Rogers | f8582c3 | 2013-05-29 16:33:03 -0700 | [diff] [blame] | 779 | int32_t lo = 0; |
| 780 | int32_t hi = NumStringIds() - 1; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 781 | while (hi >= lo) { |
Ian Rogers | f8582c3 | 2013-05-29 16:33:03 -0700 | [diff] [blame] | 782 | int32_t mid = (hi + lo) / 2; |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 783 | const DexFile::StringId& str_id = GetStringId(dex::StringIndex(mid)); |
Ian Rogers | cf5077a | 2013-10-31 12:37:54 -0700 | [diff] [blame] | 784 | const char* str = GetStringData(str_id); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 785 | int compare = CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(string, str); |
| 786 | if (compare > 0) { |
| 787 | lo = mid + 1; |
| 788 | } else if (compare < 0) { |
| 789 | hi = mid - 1; |
| 790 | } else { |
| 791 | return &str_id; |
| 792 | } |
| 793 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 794 | return nullptr; |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 795 | } |
| 796 | |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 797 | const DexFile::TypeId* DexFile::FindTypeId(const char* string) const { |
| 798 | int32_t lo = 0; |
| 799 | int32_t hi = NumTypeIds() - 1; |
| 800 | while (hi >= lo) { |
| 801 | int32_t mid = (hi + lo) / 2; |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 802 | const TypeId& type_id = GetTypeId(dex::TypeIndex(mid)); |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 803 | const DexFile::StringId& str_id = GetStringId(type_id.descriptor_idx_); |
| 804 | const char* str = GetStringData(str_id); |
| 805 | int compare = CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(string, str); |
| 806 | if (compare > 0) { |
| 807 | lo = mid + 1; |
| 808 | } else if (compare < 0) { |
| 809 | hi = mid - 1; |
| 810 | } else { |
| 811 | return &type_id; |
| 812 | } |
| 813 | } |
| 814 | return nullptr; |
| 815 | } |
| 816 | |
Vladimir Marko | a48aef4 | 2014-12-03 17:53:53 +0000 | [diff] [blame] | 817 | const DexFile::StringId* DexFile::FindStringId(const uint16_t* string, size_t length) const { |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 818 | int32_t lo = 0; |
| 819 | int32_t hi = NumStringIds() - 1; |
| 820 | while (hi >= lo) { |
| 821 | int32_t mid = (hi + lo) / 2; |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 822 | const DexFile::StringId& str_id = GetStringId(dex::StringIndex(mid)); |
Ian Rogers | cf5077a | 2013-10-31 12:37:54 -0700 | [diff] [blame] | 823 | const char* str = GetStringData(str_id); |
Vladimir Marko | a48aef4 | 2014-12-03 17:53:53 +0000 | [diff] [blame] | 824 | int compare = CompareModifiedUtf8ToUtf16AsCodePointValues(str, string, length); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 825 | if (compare > 0) { |
| 826 | lo = mid + 1; |
| 827 | } else if (compare < 0) { |
| 828 | hi = mid - 1; |
| 829 | } else { |
| 830 | return &str_id; |
| 831 | } |
| 832 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 833 | return nullptr; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 834 | } |
| 835 | |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 836 | const DexFile::TypeId* DexFile::FindTypeId(dex::StringIndex string_idx) const { |
Ian Rogers | f8582c3 | 2013-05-29 16:33:03 -0700 | [diff] [blame] | 837 | int32_t lo = 0; |
| 838 | int32_t hi = NumTypeIds() - 1; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 839 | while (hi >= lo) { |
Ian Rogers | f8582c3 | 2013-05-29 16:33:03 -0700 | [diff] [blame] | 840 | int32_t mid = (hi + lo) / 2; |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 841 | const TypeId& type_id = GetTypeId(dex::TypeIndex(mid)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 842 | if (string_idx > type_id.descriptor_idx_) { |
| 843 | lo = mid + 1; |
| 844 | } else if (string_idx < type_id.descriptor_idx_) { |
| 845 | hi = mid - 1; |
| 846 | } else { |
| 847 | return &type_id; |
| 848 | } |
| 849 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 850 | return nullptr; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 851 | } |
| 852 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 853 | const DexFile::ProtoId* DexFile::FindProtoId(dex::TypeIndex return_type_idx, |
| 854 | const dex::TypeIndex* signature_type_idxs, |
Vladimir Marko | 5c96e6b | 2013-11-14 15:34:17 +0000 | [diff] [blame] | 855 | uint32_t signature_length) const { |
Ian Rogers | f8582c3 | 2013-05-29 16:33:03 -0700 | [diff] [blame] | 856 | int32_t lo = 0; |
| 857 | int32_t hi = NumProtoIds() - 1; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 858 | while (hi >= lo) { |
Ian Rogers | f8582c3 | 2013-05-29 16:33:03 -0700 | [diff] [blame] | 859 | int32_t mid = (hi + lo) / 2; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 860 | const DexFile::ProtoId& proto = GetProtoId(mid); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 861 | int compare = return_type_idx.index_ - proto.return_type_idx_.index_; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 862 | if (compare == 0) { |
| 863 | DexFileParameterIterator it(*this, proto); |
| 864 | size_t i = 0; |
Vladimir Marko | 5c96e6b | 2013-11-14 15:34:17 +0000 | [diff] [blame] | 865 | while (it.HasNext() && i < signature_length && compare == 0) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 866 | compare = signature_type_idxs[i].index_ - it.GetTypeIdx().index_; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 867 | it.Next(); |
| 868 | i++; |
| 869 | } |
| 870 | if (compare == 0) { |
| 871 | if (it.HasNext()) { |
| 872 | compare = -1; |
Vladimir Marko | 5c96e6b | 2013-11-14 15:34:17 +0000 | [diff] [blame] | 873 | } else if (i < signature_length) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 874 | compare = 1; |
| 875 | } |
| 876 | } |
| 877 | } |
| 878 | if (compare > 0) { |
| 879 | lo = mid + 1; |
| 880 | } else if (compare < 0) { |
| 881 | hi = mid - 1; |
| 882 | } else { |
| 883 | return &proto; |
| 884 | } |
| 885 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 886 | return nullptr; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | // Given a signature place the type ids into the given vector |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 890 | bool DexFile::CreateTypeList(const StringPiece& signature, |
| 891 | dex::TypeIndex* return_type_idx, |
| 892 | std::vector<dex::TypeIndex>* param_type_idxs) const { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 893 | if (signature[0] != '(') { |
| 894 | return false; |
| 895 | } |
| 896 | size_t offset = 1; |
| 897 | size_t end = signature.size(); |
| 898 | bool process_return = false; |
| 899 | while (offset < end) { |
Vladimir Marko | e9c36b3 | 2013-11-21 15:49:16 +0000 | [diff] [blame] | 900 | size_t start_offset = offset; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 901 | char c = signature[offset]; |
| 902 | offset++; |
| 903 | if (c == ')') { |
| 904 | process_return = true; |
| 905 | continue; |
| 906 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 907 | while (c == '[') { // process array prefix |
| 908 | if (offset >= end) { // expect some descriptor following [ |
| 909 | return false; |
| 910 | } |
| 911 | c = signature[offset]; |
| 912 | offset++; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 913 | } |
| 914 | if (c == 'L') { // process type descriptors |
| 915 | do { |
| 916 | if (offset >= end) { // unexpected early termination of descriptor |
| 917 | return false; |
| 918 | } |
| 919 | c = signature[offset]; |
| 920 | offset++; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 921 | } while (c != ';'); |
| 922 | } |
Vladimir Marko | e9c36b3 | 2013-11-21 15:49:16 +0000 | [diff] [blame] | 923 | // TODO: avoid creating a std::string just to get a 0-terminated char array |
| 924 | std::string descriptor(signature.data() + start_offset, offset - start_offset); |
Mathieu Chartier | 9507fa2 | 2015-10-29 15:08:57 -0700 | [diff] [blame] | 925 | const DexFile::TypeId* type_id = FindTypeId(descriptor.c_str()); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 926 | if (type_id == nullptr) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 927 | return false; |
| 928 | } |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 929 | dex::TypeIndex type_idx = GetIndexForTypeId(*type_id); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 930 | if (!process_return) { |
| 931 | param_type_idxs->push_back(type_idx); |
| 932 | } else { |
| 933 | *return_type_idx = type_idx; |
| 934 | return offset == end; // return true if the signature had reached a sensible end |
| 935 | } |
| 936 | } |
| 937 | return false; // failed to correctly parse return type |
| 938 | } |
| 939 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 940 | const Signature DexFile::CreateSignature(const StringPiece& signature) const { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 941 | dex::TypeIndex return_type_idx; |
| 942 | std::vector<dex::TypeIndex> param_type_indices; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 943 | bool success = CreateTypeList(signature, &return_type_idx, ¶m_type_indices); |
| 944 | if (!success) { |
| 945 | return Signature::NoSignature(); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 946 | } |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 947 | const ProtoId* proto_id = FindProtoId(return_type_idx, param_type_indices); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 948 | if (proto_id == nullptr) { |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 949 | return Signature::NoSignature(); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 950 | } |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 951 | return Signature(this, *proto_id); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 952 | } |
| 953 | |
Ian Rogers | dbbc99d | 2013-04-18 16:51:54 -0700 | [diff] [blame] | 954 | int32_t DexFile::FindTryItem(const CodeItem &code_item, uint32_t address) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 955 | // Note: Signed type is important for max and min. |
| 956 | int32_t min = 0; |
Ian Rogers | dbbc99d | 2013-04-18 16:51:54 -0700 | [diff] [blame] | 957 | int32_t max = code_item.tries_size_ - 1; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 958 | |
Ian Rogers | dbbc99d | 2013-04-18 16:51:54 -0700 | [diff] [blame] | 959 | while (min <= max) { |
| 960 | int32_t mid = min + ((max - min) / 2); |
| 961 | |
| 962 | const art::DexFile::TryItem* ti = GetTryItems(code_item, mid); |
| 963 | uint32_t start = ti->start_addr_; |
| 964 | uint32_t end = start + ti->insn_count_; |
| 965 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 966 | if (address < start) { |
| 967 | max = mid - 1; |
Ian Rogers | dbbc99d | 2013-04-18 16:51:54 -0700 | [diff] [blame] | 968 | } else if (address >= end) { |
| 969 | min = mid + 1; |
| 970 | } else { // We have a winner! |
| 971 | return mid; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 972 | } |
| 973 | } |
| 974 | // No match. |
| 975 | return -1; |
| 976 | } |
| 977 | |
Ian Rogers | dbbc99d | 2013-04-18 16:51:54 -0700 | [diff] [blame] | 978 | int32_t DexFile::FindCatchHandlerOffset(const CodeItem &code_item, uint32_t address) { |
| 979 | int32_t try_item = FindTryItem(code_item, address); |
| 980 | if (try_item == -1) { |
| 981 | return -1; |
| 982 | } else { |
| 983 | return DexFile::GetTryItems(code_item, try_item)->handler_off_; |
| 984 | } |
| 985 | } |
| 986 | |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 987 | bool DexFile::DecodeDebugLocalInfo(const CodeItem* code_item, bool is_static, uint32_t method_idx, |
| 988 | DexDebugNewLocalCb local_cb, void* context) const { |
| 989 | DCHECK(local_cb != nullptr); |
| 990 | if (code_item == nullptr) { |
| 991 | return false; |
| 992 | } |
| 993 | const uint8_t* stream = GetDebugInfoStream(code_item); |
| 994 | if (stream == nullptr) { |
| 995 | return false; |
| 996 | } |
| 997 | std::vector<LocalInfo> local_in_reg(code_item->registers_size_); |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 998 | |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 999 | uint16_t arg_reg = code_item->registers_size_ - code_item->ins_size_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1000 | if (!is_static) { |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1001 | const char* descriptor = GetMethodDeclaringClassDescriptor(GetMethodId(method_idx)); |
| 1002 | local_in_reg[arg_reg].name_ = "this"; |
| 1003 | local_in_reg[arg_reg].descriptor_ = descriptor; |
| 1004 | local_in_reg[arg_reg].signature_ = nullptr; |
| 1005 | local_in_reg[arg_reg].start_address_ = 0; |
| 1006 | local_in_reg[arg_reg].reg_ = arg_reg; |
| 1007 | local_in_reg[arg_reg].is_live_ = true; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 1008 | arg_reg++; |
| 1009 | } |
| 1010 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1011 | DexFileParameterIterator it(*this, GetMethodPrototype(GetMethodId(method_idx))); |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1012 | DecodeUnsignedLeb128(&stream); // Line. |
| 1013 | uint32_t parameters_size = DecodeUnsignedLeb128(&stream); |
| 1014 | uint32_t i; |
| 1015 | for (i = 0; i < parameters_size && it.HasNext(); ++i, it.Next()) { |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 1016 | if (arg_reg >= code_item->registers_size_) { |
jeffhao | f872887 | 2011-10-28 19:11:13 -0700 | [diff] [blame] | 1017 | LOG(ERROR) << "invalid stream - arg reg >= reg size (" << arg_reg |
Brian Carlstrom | 2aab947 | 2011-12-12 15:21:43 -0800 | [diff] [blame] | 1018 | << " >= " << code_item->registers_size_ << ") in " << GetLocation(); |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1019 | return false; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 1020 | } |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1021 | uint32_t name_idx = DecodeUnsignedLeb128P1(&stream); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1022 | const char* descriptor = it.GetDescriptor(); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 1023 | local_in_reg[arg_reg].name_ = StringDataByIdx(dex::StringIndex(name_idx)); |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1024 | local_in_reg[arg_reg].descriptor_ = descriptor; |
| 1025 | local_in_reg[arg_reg].signature_ = nullptr; |
| 1026 | local_in_reg[arg_reg].start_address_ = 0; |
| 1027 | local_in_reg[arg_reg].reg_ = arg_reg; |
| 1028 | local_in_reg[arg_reg].is_live_ = true; |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1029 | switch (*descriptor) { |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 1030 | case 'D': |
| 1031 | case 'J': |
| 1032 | arg_reg += 2; |
| 1033 | break; |
| 1034 | default: |
| 1035 | arg_reg += 1; |
| 1036 | break; |
| 1037 | } |
| 1038 | } |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1039 | if (i != parameters_size || it.HasNext()) { |
Brian Carlstrom | f79fccb | 2014-02-20 08:55:10 -0800 | [diff] [blame] | 1040 | LOG(ERROR) << "invalid stream - problem with parameter iterator in " << GetLocation() |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1041 | << " for method " << this->PrettyMethod(method_idx); |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1042 | return false; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1045 | uint32_t address = 0; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 1046 | for (;;) { |
| 1047 | uint8_t opcode = *stream++; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 1048 | switch (opcode) { |
| 1049 | case DBG_END_SEQUENCE: |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1050 | // Emit all variables which are still alive at the end of the method. |
| 1051 | for (uint16_t reg = 0; reg < code_item->registers_size_; reg++) { |
| 1052 | if (local_in_reg[reg].is_live_) { |
| 1053 | local_in_reg[reg].end_address_ = code_item->insns_size_in_code_units_; |
| 1054 | local_cb(context, local_in_reg[reg]); |
| 1055 | } |
| 1056 | } |
| 1057 | return true; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 1058 | case DBG_ADVANCE_PC: |
| 1059 | address += DecodeUnsignedLeb128(&stream); |
| 1060 | break; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 1061 | case DBG_ADVANCE_LINE: |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1062 | DecodeSignedLeb128(&stream); // Line. |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 1063 | break; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 1064 | case DBG_START_LOCAL: |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1065 | case DBG_START_LOCAL_EXTENDED: { |
| 1066 | uint16_t reg = DecodeUnsignedLeb128(&stream); |
| 1067 | if (reg >= code_item->registers_size_) { |
| 1068 | LOG(ERROR) << "invalid stream - reg >= reg size (" << reg << " >= " |
Brian Carlstrom | 2aab947 | 2011-12-12 15:21:43 -0800 | [diff] [blame] | 1069 | << code_item->registers_size_ << ") in " << GetLocation(); |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1070 | return false; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 1071 | } |
| 1072 | |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1073 | uint32_t name_idx = DecodeUnsignedLeb128P1(&stream); |
Jeff Hao | c122536 | 2017-05-01 17:29:35 -0700 | [diff] [blame] | 1074 | uint16_t descriptor_idx = DecodeUnsignedLeb128P1(&stream); |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1075 | uint32_t signature_idx = kDexNoIndex; |
jeffhao | f872887 | 2011-10-28 19:11:13 -0700 | [diff] [blame] | 1076 | if (opcode == DBG_START_LOCAL_EXTENDED) { |
| 1077 | signature_idx = DecodeUnsignedLeb128P1(&stream); |
| 1078 | } |
| 1079 | |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 1080 | // Emit what was previously there, if anything |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1081 | if (local_in_reg[reg].is_live_) { |
| 1082 | local_in_reg[reg].end_address_ = address; |
| 1083 | local_cb(context, local_in_reg[reg]); |
| 1084 | } |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 1085 | |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 1086 | local_in_reg[reg].name_ = StringDataByIdx(dex::StringIndex(name_idx)); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1087 | local_in_reg[reg].descriptor_ = |
| 1088 | StringByTypeIdx(dex::TypeIndex(dchecked_integral_cast<uint16_t>(descriptor_idx)));; |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 1089 | local_in_reg[reg].signature_ = StringDataByIdx(dex::StringIndex(signature_idx)); |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1090 | local_in_reg[reg].start_address_ = address; |
| 1091 | local_in_reg[reg].reg_ = reg; |
| 1092 | local_in_reg[reg].is_live_ = true; |
| 1093 | break; |
| 1094 | } |
| 1095 | case DBG_END_LOCAL: { |
| 1096 | uint16_t reg = DecodeUnsignedLeb128(&stream); |
| 1097 | if (reg >= code_item->registers_size_) { |
| 1098 | LOG(ERROR) << "invalid stream - reg >= reg size (" << reg << " >= " |
| 1099 | << code_item->registers_size_ << ") in " << GetLocation(); |
| 1100 | return false; |
| 1101 | } |
Aart Bik | 2058b1d | 2017-05-17 13:32:26 -0700 | [diff] [blame] | 1102 | // If the register is live, close it properly. Otherwise, closing an already |
| 1103 | // closed register is sloppy, but harmless if no further action is taken. |
| 1104 | if (local_in_reg[reg].is_live_) { |
| 1105 | local_in_reg[reg].end_address_ = address; |
| 1106 | local_cb(context, local_in_reg[reg]); |
| 1107 | local_in_reg[reg].is_live_ = false; |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1108 | } |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1109 | break; |
| 1110 | } |
| 1111 | case DBG_RESTART_LOCAL: { |
| 1112 | uint16_t reg = DecodeUnsignedLeb128(&stream); |
| 1113 | if (reg >= code_item->registers_size_) { |
| 1114 | LOG(ERROR) << "invalid stream - reg >= reg size (" << reg << " >= " |
| 1115 | << code_item->registers_size_ << ") in " << GetLocation(); |
| 1116 | return false; |
| 1117 | } |
| 1118 | // If the register is live, the "restart" is superfluous, |
| 1119 | // and we don't want to mess with the existing start address. |
| 1120 | if (!local_in_reg[reg].is_live_) { |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 1121 | local_in_reg[reg].start_address_ = address; |
| 1122 | local_in_reg[reg].is_live_ = true; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 1123 | } |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 1124 | break; |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1125 | } |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 1126 | case DBG_SET_PROLOGUE_END: |
| 1127 | case DBG_SET_EPILOGUE_BEGIN: |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 1128 | break; |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1129 | case DBG_SET_FILE: |
| 1130 | DecodeUnsignedLeb128P1(&stream); // name. |
| 1131 | break; |
| 1132 | default: |
| 1133 | address += (opcode - DBG_FIRST_SPECIAL) / DBG_LINE_RANGE; |
| 1134 | break; |
| 1135 | } |
| 1136 | } |
| 1137 | } |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 1138 | |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1139 | bool DexFile::DecodeDebugPositionInfo(const CodeItem* code_item, DexDebugNewPositionCb position_cb, |
| 1140 | void* context) const { |
| 1141 | DCHECK(position_cb != nullptr); |
| 1142 | if (code_item == nullptr) { |
| 1143 | return false; |
| 1144 | } |
| 1145 | const uint8_t* stream = GetDebugInfoStream(code_item); |
| 1146 | if (stream == nullptr) { |
| 1147 | return false; |
| 1148 | } |
| 1149 | |
| 1150 | PositionInfo entry = PositionInfo(); |
| 1151 | entry.line_ = DecodeUnsignedLeb128(&stream); |
| 1152 | uint32_t parameters_size = DecodeUnsignedLeb128(&stream); |
| 1153 | for (uint32_t i = 0; i < parameters_size; ++i) { |
| 1154 | DecodeUnsignedLeb128P1(&stream); // Parameter name. |
| 1155 | } |
| 1156 | |
| 1157 | for (;;) { |
| 1158 | uint8_t opcode = *stream++; |
| 1159 | switch (opcode) { |
| 1160 | case DBG_END_SEQUENCE: |
| 1161 | return true; // end of stream. |
| 1162 | case DBG_ADVANCE_PC: |
| 1163 | entry.address_ += DecodeUnsignedLeb128(&stream); |
| 1164 | break; |
| 1165 | case DBG_ADVANCE_LINE: |
| 1166 | entry.line_ += DecodeSignedLeb128(&stream); |
| 1167 | break; |
| 1168 | case DBG_START_LOCAL: |
| 1169 | DecodeUnsignedLeb128(&stream); // reg. |
| 1170 | DecodeUnsignedLeb128P1(&stream); // name. |
| 1171 | DecodeUnsignedLeb128P1(&stream); // descriptor. |
| 1172 | break; |
| 1173 | case DBG_START_LOCAL_EXTENDED: |
| 1174 | DecodeUnsignedLeb128(&stream); // reg. |
| 1175 | DecodeUnsignedLeb128P1(&stream); // name. |
| 1176 | DecodeUnsignedLeb128P1(&stream); // descriptor. |
| 1177 | DecodeUnsignedLeb128P1(&stream); // signature. |
| 1178 | break; |
| 1179 | case DBG_END_LOCAL: |
| 1180 | case DBG_RESTART_LOCAL: |
| 1181 | DecodeUnsignedLeb128(&stream); // reg. |
| 1182 | break; |
| 1183 | case DBG_SET_PROLOGUE_END: |
| 1184 | entry.prologue_end_ = true; |
| 1185 | break; |
| 1186 | case DBG_SET_EPILOGUE_BEGIN: |
| 1187 | entry.epilogue_begin_ = true; |
| 1188 | break; |
| 1189 | case DBG_SET_FILE: { |
| 1190 | uint32_t name_idx = DecodeUnsignedLeb128P1(&stream); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 1191 | entry.source_file_ = StringDataByIdx(dex::StringIndex(name_idx)); |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1192 | break; |
| 1193 | } |
Shih-wei Liao | 8e1b4ff | 2011-10-15 15:43:51 -0700 | [diff] [blame] | 1194 | default: { |
| 1195 | int adjopcode = opcode - DBG_FIRST_SPECIAL; |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1196 | entry.address_ += adjopcode / DBG_LINE_RANGE; |
| 1197 | entry.line_ += DBG_LINE_BASE + (adjopcode % DBG_LINE_RANGE); |
| 1198 | if (position_cb(context, entry)) { |
| 1199 | return true; // early exit. |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 1200 | } |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1201 | entry.prologue_end_ = false; |
| 1202 | entry.epilogue_begin_ = false; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 1203 | break; |
Shih-wei Liao | 8e1b4ff | 2011-10-15 15:43:51 -0700 | [diff] [blame] | 1204 | } |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 1205 | } |
| 1206 | } |
| 1207 | } |
| 1208 | |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1209 | bool DexFile::LineNumForPcCb(void* raw_context, const PositionInfo& entry) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1210 | LineNumFromPcContext* context = reinterpret_cast<LineNumFromPcContext*>(raw_context); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1211 | |
| 1212 | // We know that this callback will be called in |
| 1213 | // ascending address order, so keep going until we find |
| 1214 | // a match or we've just gone past it. |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1215 | if (entry.address_ > context->address_) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1216 | // The line number from the previous positions callback |
| 1217 | // wil be the final result. |
| 1218 | return true; |
| 1219 | } else { |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1220 | context->line_num_ = entry.line_; |
| 1221 | return entry.address_ == context->address_; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1222 | } |
| 1223 | } |
| 1224 | |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1225 | bool DexFile::IsMultiDexLocation(const char* location) { |
| 1226 | return strrchr(location, kMultiDexSeparator) != nullptr; |
| 1227 | } |
| 1228 | |
Andreas Gampe | 90e3404 | 2015-04-27 20:01:52 -0700 | [diff] [blame] | 1229 | std::string DexFile::GetMultiDexClassesDexName(size_t index) { |
| 1230 | if (index == 0) { |
| 1231 | return "classes.dex"; |
| 1232 | } else { |
| 1233 | return StringPrintf("classes%zu.dex", index + 1); |
| 1234 | } |
| 1235 | } |
| 1236 | |
| 1237 | std::string DexFile::GetMultiDexLocation(size_t index, const char* dex_location) { |
| 1238 | if (index == 0) { |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 1239 | return dex_location; |
| 1240 | } else { |
Andreas Gampe | 90e3404 | 2015-04-27 20:01:52 -0700 | [diff] [blame] | 1241 | return StringPrintf("%s" kMultiDexSeparatorString "classes%zu.dex", dex_location, index + 1); |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 1242 | } |
| 1243 | } |
| 1244 | |
| 1245 | std::string DexFile::GetDexCanonicalLocation(const char* dex_location) { |
| 1246 | CHECK_NE(dex_location, static_cast<const char*>(nullptr)); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 1247 | std::string base_location = GetBaseLocation(dex_location); |
| 1248 | const char* suffix = dex_location + base_location.size(); |
| 1249 | DCHECK(suffix[0] == 0 || suffix[0] == kMultiDexSeparator); |
| 1250 | UniqueCPtr<const char[]> path(realpath(base_location.c_str(), nullptr)); |
| 1251 | if (path != nullptr && path.get() != base_location) { |
| 1252 | return std::string(path.get()) + suffix; |
| 1253 | } else if (suffix[0] == 0) { |
| 1254 | return base_location; |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 1255 | } else { |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 1256 | return dex_location; |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 1257 | } |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 1258 | } |
| 1259 | |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 1260 | // Read a signed integer. "zwidth" is the zero-based byte count. |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1261 | int32_t DexFile::ReadSignedInt(const uint8_t* ptr, int zwidth) { |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 1262 | int32_t val = 0; |
| 1263 | for (int i = zwidth; i >= 0; --i) { |
| 1264 | val = ((uint32_t)val >> 8) | (((int32_t)*ptr++) << 24); |
| 1265 | } |
| 1266 | val >>= (3 - zwidth) * 8; |
| 1267 | return val; |
| 1268 | } |
| 1269 | |
| 1270 | // Read an unsigned integer. "zwidth" is the zero-based byte count, |
| 1271 | // "fill_on_right" indicates which side we want to zero-fill from. |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1272 | uint32_t DexFile::ReadUnsignedInt(const uint8_t* ptr, int zwidth, bool fill_on_right) { |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 1273 | uint32_t val = 0; |
| 1274 | for (int i = zwidth; i >= 0; --i) { |
| 1275 | val = (val >> 8) | (((uint32_t)*ptr++) << 24); |
| 1276 | } |
| 1277 | if (!fill_on_right) { |
| 1278 | val >>= (3 - zwidth) * 8; |
| 1279 | } |
| 1280 | return val; |
| 1281 | } |
| 1282 | |
| 1283 | // Read a signed long. "zwidth" is the zero-based byte count. |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1284 | int64_t DexFile::ReadSignedLong(const uint8_t* ptr, int zwidth) { |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 1285 | int64_t val = 0; |
| 1286 | for (int i = zwidth; i >= 0; --i) { |
| 1287 | val = ((uint64_t)val >> 8) | (((int64_t)*ptr++) << 56); |
| 1288 | } |
| 1289 | val >>= (7 - zwidth) * 8; |
| 1290 | return val; |
| 1291 | } |
| 1292 | |
| 1293 | // Read an unsigned long. "zwidth" is the zero-based byte count, |
| 1294 | // "fill_on_right" indicates which side we want to zero-fill from. |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1295 | uint64_t DexFile::ReadUnsignedLong(const uint8_t* ptr, int zwidth, bool fill_on_right) { |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 1296 | uint64_t val = 0; |
| 1297 | for (int i = zwidth; i >= 0; --i) { |
| 1298 | val = (val >> 8) | (((uint64_t)*ptr++) << 56); |
| 1299 | } |
| 1300 | if (!fill_on_right) { |
| 1301 | val >>= (7 - zwidth) * 8; |
| 1302 | } |
| 1303 | return val; |
| 1304 | } |
| 1305 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1306 | std::string DexFile::PrettyMethod(uint32_t method_idx, bool with_signature) const { |
| 1307 | if (method_idx >= NumMethodIds()) { |
| 1308 | return StringPrintf("<<invalid-method-idx-%d>>", method_idx); |
| 1309 | } |
| 1310 | const DexFile::MethodId& method_id = GetMethodId(method_idx); |
| 1311 | std::string result(PrettyDescriptor(GetMethodDeclaringClassDescriptor(method_id))); |
| 1312 | result += '.'; |
| 1313 | result += GetMethodName(method_id); |
| 1314 | if (with_signature) { |
| 1315 | const Signature signature = GetMethodSignature(method_id); |
| 1316 | std::string sig_as_string(signature.ToString()); |
| 1317 | if (signature == Signature::NoSignature()) { |
| 1318 | return result + sig_as_string; |
| 1319 | } |
| 1320 | result = PrettyReturnType(sig_as_string.c_str()) + " " + result + |
| 1321 | PrettyArguments(sig_as_string.c_str()); |
| 1322 | } |
| 1323 | return result; |
| 1324 | } |
| 1325 | |
| 1326 | std::string DexFile::PrettyField(uint32_t field_idx, bool with_type) const { |
| 1327 | if (field_idx >= NumFieldIds()) { |
| 1328 | return StringPrintf("<<invalid-field-idx-%d>>", field_idx); |
| 1329 | } |
| 1330 | const DexFile::FieldId& field_id = GetFieldId(field_idx); |
| 1331 | std::string result; |
| 1332 | if (with_type) { |
| 1333 | result += GetFieldTypeDescriptor(field_id); |
| 1334 | result += ' '; |
| 1335 | } |
| 1336 | result += PrettyDescriptor(GetFieldDeclaringClassDescriptor(field_id)); |
| 1337 | result += '.'; |
| 1338 | result += GetFieldName(field_id); |
| 1339 | return result; |
| 1340 | } |
| 1341 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1342 | std::string DexFile::PrettyType(dex::TypeIndex type_idx) const { |
| 1343 | if (type_idx.index_ >= NumTypeIds()) { |
| 1344 | return StringPrintf("<<invalid-type-idx-%d>>", type_idx.index_); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1345 | } |
| 1346 | const DexFile::TypeId& type_id = GetTypeId(type_idx); |
| 1347 | return PrettyDescriptor(GetTypeDescriptor(type_id)); |
| 1348 | } |
| 1349 | |
Jeff Hao | 3d08086 | 2016-05-26 18:39:17 -0700 | [diff] [blame] | 1350 | // Checks that visibility is as expected. Includes special behavior for M and |
| 1351 | // before to allow runtime and build visibility when expecting runtime. |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 1352 | std::ostream& operator<<(std::ostream& os, const DexFile& dex_file) { |
| 1353 | os << StringPrintf("[DexFile: %s dex-checksum=%08x location-checksum=%08x %p-%p]", |
| 1354 | dex_file.GetLocation().c_str(), |
| 1355 | dex_file.GetHeader().checksum_, dex_file.GetLocationChecksum(), |
| 1356 | dex_file.Begin(), dex_file.Begin() + dex_file.Size()); |
| 1357 | return os; |
| 1358 | } |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 1359 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 1360 | std::string Signature::ToString() const { |
| 1361 | if (dex_file_ == nullptr) { |
| 1362 | CHECK(proto_id_ == nullptr); |
| 1363 | return "<no signature>"; |
| 1364 | } |
| 1365 | const DexFile::TypeList* params = dex_file_->GetProtoParameters(*proto_id_); |
| 1366 | std::string result; |
| 1367 | if (params == nullptr) { |
| 1368 | result += "()"; |
| 1369 | } else { |
| 1370 | result += "("; |
| 1371 | for (uint32_t i = 0; i < params->Size(); ++i) { |
| 1372 | result += dex_file_->StringByTypeIdx(params->GetTypeItem(i).type_idx_); |
| 1373 | } |
| 1374 | result += ")"; |
| 1375 | } |
| 1376 | result += dex_file_->StringByTypeIdx(proto_id_->return_type_idx_); |
| 1377 | return result; |
| 1378 | } |
| 1379 | |
Orion Hodson | 6c4921b | 2016-09-21 15:41:06 +0100 | [diff] [blame] | 1380 | uint32_t Signature::GetNumberOfParameters() const { |
| 1381 | const DexFile::TypeList* params = dex_file_->GetProtoParameters(*proto_id_); |
| 1382 | return (params != nullptr) ? params->Size() : 0; |
| 1383 | } |
| 1384 | |
| 1385 | bool Signature::IsVoid() const { |
| 1386 | const char* return_type = dex_file_->GetReturnTypeDescriptor(*proto_id_); |
| 1387 | return strcmp(return_type, "V") == 0; |
| 1388 | } |
| 1389 | |
Vladimir Marko | d9cffea | 2013-11-25 15:08:02 +0000 | [diff] [blame] | 1390 | bool Signature::operator==(const StringPiece& rhs) const { |
| 1391 | if (dex_file_ == nullptr) { |
| 1392 | return false; |
| 1393 | } |
| 1394 | StringPiece tail(rhs); |
| 1395 | if (!tail.starts_with("(")) { |
| 1396 | return false; // Invalid signature |
| 1397 | } |
| 1398 | tail.remove_prefix(1); // "("; |
| 1399 | const DexFile::TypeList* params = dex_file_->GetProtoParameters(*proto_id_); |
| 1400 | if (params != nullptr) { |
| 1401 | for (uint32_t i = 0; i < params->Size(); ++i) { |
| 1402 | StringPiece param(dex_file_->StringByTypeIdx(params->GetTypeItem(i).type_idx_)); |
| 1403 | if (!tail.starts_with(param)) { |
| 1404 | return false; |
| 1405 | } |
| 1406 | tail.remove_prefix(param.length()); |
| 1407 | } |
| 1408 | } |
| 1409 | if (!tail.starts_with(")")) { |
| 1410 | return false; |
| 1411 | } |
| 1412 | tail.remove_prefix(1); // ")"; |
| 1413 | return tail == dex_file_->StringByTypeIdx(proto_id_->return_type_idx_); |
| 1414 | } |
| 1415 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 1416 | std::ostream& operator<<(std::ostream& os, const Signature& sig) { |
| 1417 | return os << sig.ToString(); |
| 1418 | } |
| 1419 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1420 | // Decodes the header section from the class data bytes. |
| 1421 | void ClassDataItemIterator::ReadClassDataHeader() { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1422 | CHECK(ptr_pos_ != nullptr); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1423 | header_.static_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_); |
| 1424 | header_.instance_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_); |
| 1425 | header_.direct_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_); |
| 1426 | header_.virtual_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_); |
| 1427 | } |
| 1428 | |
| 1429 | void ClassDataItemIterator::ReadClassDataField() { |
| 1430 | field_.field_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_); |
| 1431 | field_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_); |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 1432 | // The user of the iterator is responsible for checking if there |
| 1433 | // are unordered or duplicate indexes. |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | void ClassDataItemIterator::ReadClassDataMethod() { |
| 1437 | method_.method_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_); |
| 1438 | method_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_); |
| 1439 | method_.code_off_ = DecodeUnsignedLeb128(&ptr_pos_); |
Brian Carlstrom | 68adbe4 | 2012-05-11 17:18:08 -0700 | [diff] [blame] | 1440 | if (last_idx_ != 0 && method_.method_idx_delta_ == 0) { |
Andreas Gampe | 4fdbba0 | 2014-06-19 20:24:22 -0700 | [diff] [blame] | 1441 | LOG(WARNING) << "Duplicate method in " << dex_file_.GetLocation(); |
Brian Carlstrom | 6f29d0e | 2012-05-11 15:50:29 -0700 | [diff] [blame] | 1442 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1443 | } |
| 1444 | |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 1445 | EncodedArrayValueIterator::EncodedArrayValueIterator(const DexFile& dex_file, |
| 1446 | const uint8_t* array_data) |
Shinichiro Hamaji | 82863f0 | 2015-11-05 16:51:33 +0900 | [diff] [blame] | 1447 | : dex_file_(dex_file), |
Shinichiro Hamaji | 82863f0 | 2015-11-05 16:51:33 +0900 | [diff] [blame] | 1448 | array_size_(), |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1449 | pos_(-1), |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 1450 | ptr_(array_data), |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1451 | type_(kByte) { |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 1452 | array_size_ = (ptr_ != nullptr) ? DecodeUnsignedLeb128(&ptr_) : 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1453 | if (array_size_ > 0) { |
| 1454 | Next(); |
| 1455 | } |
| 1456 | } |
| 1457 | |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 1458 | void EncodedArrayValueIterator::Next() { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1459 | pos_++; |
| 1460 | if (pos_ >= array_size_) { |
| 1461 | return; |
| 1462 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1463 | uint8_t value_type = *ptr_++; |
| 1464 | uint8_t value_arg = value_type >> kEncodedValueArgShift; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1465 | size_t width = value_arg + 1; // assume and correct later |
Brian Carlstrom | 88f3654 | 2012-10-16 23:24:21 -0700 | [diff] [blame] | 1466 | type_ = static_cast<ValueType>(value_type & kEncodedValueTypeMask); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1467 | switch (type_) { |
| 1468 | case kBoolean: |
| 1469 | jval_.i = (value_arg != 0) ? 1 : 0; |
| 1470 | width = 0; |
| 1471 | break; |
| 1472 | case kByte: |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1473 | jval_.i = DexFile::ReadSignedInt(ptr_, value_arg); |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 1474 | CHECK(IsInt<8>(jval_.i)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1475 | break; |
| 1476 | case kShort: |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1477 | jval_.i = DexFile::ReadSignedInt(ptr_, value_arg); |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 1478 | CHECK(IsInt<16>(jval_.i)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1479 | break; |
| 1480 | case kChar: |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1481 | jval_.i = DexFile::ReadUnsignedInt(ptr_, value_arg, false); |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 1482 | CHECK(IsUint<16>(jval_.i)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1483 | break; |
| 1484 | case kInt: |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1485 | jval_.i = DexFile::ReadSignedInt(ptr_, value_arg); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1486 | break; |
| 1487 | case kLong: |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1488 | jval_.j = DexFile::ReadSignedLong(ptr_, value_arg); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1489 | break; |
| 1490 | case kFloat: |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1491 | jval_.i = DexFile::ReadUnsignedInt(ptr_, value_arg, true); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1492 | break; |
| 1493 | case kDouble: |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1494 | jval_.j = DexFile::ReadUnsignedLong(ptr_, value_arg, true); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1495 | break; |
| 1496 | case kString: |
| 1497 | case kType: |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 1498 | case kMethodType: |
| 1499 | case kMethodHandle: |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1500 | jval_.i = DexFile::ReadUnsignedInt(ptr_, value_arg, false); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1501 | break; |
| 1502 | case kField: |
Brian Carlstrom | 88f3654 | 2012-10-16 23:24:21 -0700 | [diff] [blame] | 1503 | case kMethod: |
| 1504 | case kEnum: |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1505 | case kArray: |
| 1506 | case kAnnotation: |
| 1507 | UNIMPLEMENTED(FATAL) << ": type " << type_; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 1508 | UNREACHABLE(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1509 | case kNull: |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1510 | jval_.l = nullptr; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1511 | width = 0; |
| 1512 | break; |
| 1513 | default: |
| 1514 | LOG(FATAL) << "Unreached"; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 1515 | UNREACHABLE(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1516 | } |
| 1517 | ptr_ += width; |
| 1518 | } |
| 1519 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1520 | CatchHandlerIterator::CatchHandlerIterator(const DexFile::CodeItem& code_item, uint32_t address) { |
| 1521 | handler_.address_ = -1; |
| 1522 | int32_t offset = -1; |
| 1523 | |
| 1524 | // Short-circuit the overwhelmingly common cases. |
| 1525 | switch (code_item.tries_size_) { |
| 1526 | case 0: |
| 1527 | break; |
| 1528 | case 1: { |
| 1529 | const DexFile::TryItem* tries = DexFile::GetTryItems(code_item, 0); |
| 1530 | uint32_t start = tries->start_addr_; |
| 1531 | if (address >= start) { |
| 1532 | uint32_t end = start + tries->insn_count_; |
| 1533 | if (address < end) { |
| 1534 | offset = tries->handler_off_; |
| 1535 | } |
| 1536 | } |
| 1537 | break; |
| 1538 | } |
| 1539 | default: |
Ian Rogers | dbbc99d | 2013-04-18 16:51:54 -0700 | [diff] [blame] | 1540 | offset = DexFile::FindCatchHandlerOffset(code_item, address); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1541 | } |
Logan Chien | 736df02 | 2012-04-27 16:25:57 +0800 | [diff] [blame] | 1542 | Init(code_item, offset); |
| 1543 | } |
| 1544 | |
| 1545 | CatchHandlerIterator::CatchHandlerIterator(const DexFile::CodeItem& code_item, |
| 1546 | const DexFile::TryItem& try_item) { |
| 1547 | handler_.address_ = -1; |
| 1548 | Init(code_item, try_item.handler_off_); |
| 1549 | } |
| 1550 | |
| 1551 | void CatchHandlerIterator::Init(const DexFile::CodeItem& code_item, |
| 1552 | int32_t offset) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1553 | if (offset >= 0) { |
Logan Chien | 736df02 | 2012-04-27 16:25:57 +0800 | [diff] [blame] | 1554 | Init(DexFile::GetCatchHandlerData(code_item, offset)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1555 | } else { |
| 1556 | // Not found, initialize as empty |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1557 | current_data_ = nullptr; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1558 | remaining_count_ = -1; |
| 1559 | catch_all_ = false; |
| 1560 | DCHECK(!HasNext()); |
| 1561 | } |
| 1562 | } |
| 1563 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1564 | void CatchHandlerIterator::Init(const uint8_t* handler_data) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1565 | current_data_ = handler_data; |
| 1566 | remaining_count_ = DecodeSignedLeb128(¤t_data_); |
| 1567 | |
| 1568 | // If remaining_count_ is non-positive, then it is the negative of |
| 1569 | // the number of catch types, and the catches are followed by a |
| 1570 | // catch-all handler. |
| 1571 | if (remaining_count_ <= 0) { |
| 1572 | catch_all_ = true; |
| 1573 | remaining_count_ = -remaining_count_; |
| 1574 | } else { |
| 1575 | catch_all_ = false; |
| 1576 | } |
| 1577 | Next(); |
| 1578 | } |
| 1579 | |
| 1580 | void CatchHandlerIterator::Next() { |
| 1581 | if (remaining_count_ > 0) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1582 | handler_.type_idx_ = dex::TypeIndex(DecodeUnsignedLeb128(¤t_data_)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1583 | handler_.address_ = DecodeUnsignedLeb128(¤t_data_); |
| 1584 | remaining_count_--; |
| 1585 | return; |
| 1586 | } |
| 1587 | |
| 1588 | if (catch_all_) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1589 | handler_.type_idx_ = dex::TypeIndex(DexFile::kDexNoIndex16); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1590 | handler_.address_ = DecodeUnsignedLeb128(¤t_data_); |
| 1591 | catch_all_ = false; |
| 1592 | return; |
| 1593 | } |
| 1594 | |
| 1595 | // no more handler |
| 1596 | remaining_count_ = -1; |
| 1597 | } |
| 1598 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1599 | namespace dex { |
| 1600 | |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 1601 | std::ostream& operator<<(std::ostream& os, const StringIndex& index) { |
| 1602 | os << "StringIndex[" << index.index_ << "]"; |
| 1603 | return os; |
| 1604 | } |
| 1605 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1606 | std::ostream& operator<<(std::ostream& os, const TypeIndex& index) { |
| 1607 | os << "TypeIndex[" << index.index_ << "]"; |
| 1608 | return os; |
| 1609 | } |
| 1610 | |
| 1611 | } // namespace dex |
| 1612 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1613 | } // namespace art |