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