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