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