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