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