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