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