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