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