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