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