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