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