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