blob: aaff0fc6dab5b6d6bcc36f5968dbab9eda6b3e85 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro1fb86202011-06-27 17:43:13 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "dex_file.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070018
19#include <fcntl.h>
Brian Carlstrom1f870082011-08-23 16:02:11 -070020#include <limits.h>
Brian Carlstromb0460ea2011-07-29 10:08:05 -070021#include <stdio.h>
Ian Rogersd81871c2011-10-03 13:57:23 -070022#include <stdlib.h>
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070023#include <string.h>
Brian Carlstromb0460ea2011-07-29 10:08:05 -070024#include <sys/file.h>
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070025#include <sys/stat.h>
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070026
Elliott Hughes07ed66b2012-12-12 18:34:25 -080027#include "base/logging.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080028#include "base/stringprintf.h"
Ian Rogers0571d352011-11-03 19:51:38 -070029#include "class_linker.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070030#include "dex_file-inl.h"
jeffhao10037c82012-01-23 15:06:23 -080031#include "dex_file_verifier.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070032#include "globals.h"
Ian Rogers0571d352011-11-03 19:51:38 -070033#include "leb128.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "mirror/abstract_method-inl.h"
35#include "mirror/field.h"
36#include "mirror/field-inl.h"
37#include "mirror/string.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070038#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070039#include "safe_map.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070040#include "thread.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070041#include "UniquePtr.h"
Ian Rogers0571d352011-11-03 19:51:38 -070042#include "utf.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070043#include "utils.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070044#include "well_known_classes.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070045#include "zip_archive.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070046
47namespace art {
48
Brian Carlstromf615a612011-07-23 12:50:34 -070049const byte DexFile::kDexMagic[] = { 'd', 'e', 'x', '\n' };
50const byte DexFile::kDexMagicVersion[] = { '0', '3', '5', '\0' };
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070051
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070052DexFile::ClassPathEntry DexFile::FindInClassPath(const StringPiece& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070053 const ClassPath& class_path) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070054 for (size_t i = 0; i != class_path.size(); ++i) {
55 const DexFile* dex_file = class_path[i];
56 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
57 if (dex_class_def != NULL) {
58 return ClassPathEntry(dex_file, dex_class_def);
59 }
60 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070061 // TODO: remove reinterpret_cast when issue with -std=gnu++0x host issue resolved
Brian Carlstrom7e93b502011-08-04 14:16:22 -070062 return ClassPathEntry(reinterpret_cast<const DexFile*>(NULL),
63 reinterpret_cast<const DexFile::ClassDef*>(NULL));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070064}
65
Brian Carlstrom5b332c82012-02-01 15:02:31 -080066bool DexFile::GetChecksum(const std::string& filename, uint32_t& checksum) {
67 if (IsValidZipFilename(filename)) {
68 UniquePtr<ZipArchive> zip_archive(ZipArchive::Open(filename));
69 if (zip_archive.get() == NULL) {
70 return false;
Brian Carlstrom78128a62011-09-15 17:21:19 -070071 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -080072 UniquePtr<ZipEntry> zip_entry(zip_archive->Find(kClassesDex));
73 if (zip_entry.get() == NULL) {
Ian Rogerse6060102013-05-16 12:01:04 -070074 LOG(ERROR) << "Zip archive '" << filename << "' doesn't contain " << kClassesDex;
Brian Carlstrom5b332c82012-02-01 15:02:31 -080075 return false;
76 }
77 checksum = zip_entry->GetCrc32();
78 return true;
Brian Carlstrom78128a62011-09-15 17:21:19 -070079 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -080080 if (IsValidDexFilename(filename)) {
Brian Carlstrom1db858a2012-03-11 22:44:45 -070081 UniquePtr<const DexFile> dex_file(DexFile::OpenFile(filename, filename, false));
Brian Carlstrom5b332c82012-02-01 15:02:31 -080082 if (dex_file.get() == NULL) {
83 return false;
84 }
85 checksum = dex_file->GetHeader().checksum_;
86 return true;
87 }
Ian Rogerse6060102013-05-16 12:01:04 -070088 LOG(ERROR) << "Expected valid zip or dex file name: " << filename;
Brian Carlstrom5b332c82012-02-01 15:02:31 -080089 return false;
Brian Carlstrom78128a62011-09-15 17:21:19 -070090}
91
Brian Carlstrom16192862011-09-12 17:50:06 -070092const DexFile* DexFile::Open(const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -080093 const std::string& location) {
jeffhao262bf462011-10-20 18:36:32 -070094 if (IsValidZipFilename(filename)) {
Brian Carlstroma004aa92012-02-08 18:05:09 -080095 return DexFile::OpenZip(filename, location);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070096 }
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -070097 if (!IsValidDexFilename(filename)) {
98 LOG(WARNING) << "Attempting to open dex file with unknown extension '" << filename << "'";
99 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800100 return DexFile::OpenFile(filename, location, true);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700101}
102
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800103int DexFile::GetPermissions() const {
104 if (mem_map_.get() == NULL) {
105 return 0;
106 } else {
107 return mem_map_->GetProtect();
108 }
109}
110
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200111bool DexFile::IsReadOnly() const {
112 return GetPermissions() == PROT_READ;
113}
114
115bool DexFile::EnableWrite(uint8_t* addr, size_t length) const {
116 CHECK(IsReadOnly());
117 if (mem_map_.get() == NULL) {
118 return false;
119 } else {
120 return mem_map_->ProtectRegion(addr, length, PROT_READ | PROT_WRITE);
121 }
122}
123
124bool DexFile::DisableWrite(uint8_t* addr, size_t length) const {
125 CHECK(!IsReadOnly());
126 if (mem_map_.get() == NULL) {
127 return false;
128 } else {
129 return mem_map_->ProtectRegion(addr, length, PROT_READ);
130 }
131}
132
Brian Carlstrom16192862011-09-12 17:50:06 -0700133const DexFile* DexFile::OpenFile(const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -0800134 const std::string& location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800135 bool verify) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800136 CHECK(!location.empty()) << filename;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700137 int fd = open(filename.c_str(), O_RDONLY); // TODO: scoped_fd
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700138 if (fd == -1) {
139 PLOG(ERROR) << "open(\"" << filename << "\", O_RDONLY) failed";
140 return NULL;
141 }
142 struct stat sbuf;
143 memset(&sbuf, 0, sizeof(sbuf));
144 if (fstat(fd, &sbuf) == -1) {
145 PLOG(ERROR) << "fstat \"" << filename << "\" failed";
146 close(fd);
147 return NULL;
148 }
Ian Rogers7cfb93e2012-01-17 19:46:36 -0800149 if (S_ISDIR(sbuf.st_mode)) {
150 LOG(ERROR) << "attempt to mmap directory \"" << filename << "\"";
151 return NULL;
152 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700153 size_t length = sbuf.st_size;
Brian Carlstrom89521892011-12-07 22:05:07 -0800154 UniquePtr<MemMap> map(MemMap::MapFile(length, PROT_READ, MAP_PRIVATE, fd, 0));
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700155 if (map.get() == NULL) {
156 LOG(ERROR) << "mmap \"" << filename << "\" failed";
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700157 close(fd);
158 return NULL;
159 }
160 close(fd);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800161
162 if (map->Size() < sizeof(DexFile::Header)) {
163 LOG(ERROR) << "Failed to open dex file '" << filename << "' that is too short to have a header";
164 return NULL;
165 }
166
167 const Header* dex_header = reinterpret_cast<const Header*>(map->Begin());
168
169 const DexFile* dex_file = OpenMemory(location, dex_header->checksum_, map.release());
jeffhao54c1ceb2012-02-01 11:45:32 -0800170 if (dex_file == NULL) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800171 LOG(ERROR) << "Failed to open dex file '" << filename << "' from memory";
jeffhao54c1ceb2012-02-01 11:45:32 -0800172 return NULL;
jeffhaof6174e82012-01-31 16:14:17 -0800173 }
jeffhao54c1ceb2012-02-01 11:45:32 -0800174
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700175 if (verify && !DexFileVerifier::Verify(dex_file, dex_file->Begin(), dex_file->Size())) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800176 LOG(ERROR) << "Failed to verify dex file '" << filename << "'";
jeffhao54c1ceb2012-02-01 11:45:32 -0800177 return NULL;
178 }
179
jeffhaof6174e82012-01-31 16:14:17 -0800180 return dex_file;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700181}
182
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700183const char* DexFile::kClassesDex = "classes.dex";
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700184
Brian Carlstrom16192862011-09-12 17:50:06 -0700185const DexFile* DexFile::OpenZip(const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -0800186 const std::string& location) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700187 UniquePtr<ZipArchive> zip_archive(ZipArchive::Open(filename));
188 if (zip_archive.get() == NULL) {
Brian Carlstrom16192862011-09-12 17:50:06 -0700189 LOG(ERROR) << "Failed to open " << filename << " when looking for classes.dex";
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700190 return NULL;
191 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800192 return DexFile::Open(*zip_archive.get(), location);
193}
194
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800195const DexFile* DexFile::OpenMemory(const std::string& location,
196 uint32_t location_checksum,
197 MemMap* mem_map) {
198 return OpenMemory(mem_map->Begin(),
199 mem_map->Size(),
200 location,
201 location_checksum,
202 mem_map);
203}
204
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800205const DexFile* DexFile::Open(const ZipArchive& zip_archive, const std::string& location) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800206 CHECK(!location.empty());
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800207 UniquePtr<ZipEntry> zip_entry(zip_archive.Find(kClassesDex));
Elliott Hughes90a33692011-08-30 13:27:07 -0700208 if (zip_entry.get() == NULL) {
Brian Carlstrom4922e9d2013-07-09 17:18:47 -0700209 LOG(ERROR) << "Failed to find classes.dex within '" << location << "'";
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700210 return NULL;
211 }
212
Brian Carlstrom4922e9d2013-07-09 17:18:47 -0700213 UniquePtr<MemMap> map(zip_entry->ExtractToMemMap(kClassesDex));
Brian Carlstrom89521892011-12-07 22:05:07 -0800214 if (map.get() == NULL) {
Brian Carlstrom4922e9d2013-07-09 17:18:47 -0700215 LOG(ERROR) << "Failed to extract '" << kClassesDex << "' from '" << location << "'";
Brian Carlstrom89521892011-12-07 22:05:07 -0800216 return NULL;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700217 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800218 const DexFile* dex_file = OpenMemory(location, zip_entry->GetCrc32(), map.release());
jeffhao54c1ceb2012-02-01 11:45:32 -0800219 if (dex_file == NULL) {
220 LOG(ERROR) << "Failed to open dex file '" << location << "' from memory";
221 return NULL;
jeffhaof6174e82012-01-31 16:14:17 -0800222 }
jeffhao54c1ceb2012-02-01 11:45:32 -0800223
224 if (!DexFileVerifier::Verify(dex_file, dex_file->Begin(), dex_file->Size())) {
225 LOG(ERROR) << "Failed to verify dex file '" << location << "'";
226 return NULL;
227 }
228
jeffhaof6174e82012-01-31 16:14:17 -0800229 return dex_file;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700230}
231
Brian Carlstrom89521892011-12-07 22:05:07 -0800232const DexFile* DexFile::OpenMemory(const byte* base,
jeffhaof6174e82012-01-31 16:14:17 -0800233 size_t size,
Brian Carlstrom89521892011-12-07 22:05:07 -0800234 const std::string& location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800235 uint32_t location_checksum,
Brian Carlstrom89521892011-12-07 22:05:07 -0800236 MemMap* mem_map) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700237 CHECK_ALIGNED(base, 4); // various dex file structures must be word aligned
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800238 UniquePtr<DexFile> dex_file(new DexFile(base, size, location, location_checksum, mem_map));
Brian Carlstromf615a612011-07-23 12:50:34 -0700239 if (!dex_file->Init()) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700240 return NULL;
241 } else {
Brian Carlstromf615a612011-07-23 12:50:34 -0700242 return dex_file.release();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700243 }
244}
245
Jesse Wilson6bf19152011-09-29 13:12:33 -0400246DexFile::~DexFile() {
Elliott Hughes8cef0b82011-10-11 19:24:00 -0700247 // We don't call DeleteGlobalRef on dex_object_ because we're only called by DestroyJavaVM, and
248 // that's only called after DetachCurrentThread, which means there's no JNIEnv. We could
249 // re-attach, but cleaning up these global references is not obviously useful. It's not as if
250 // the global reference table is otherwise empty!
Jesse Wilson6bf19152011-09-29 13:12:33 -0400251}
252
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700253class ScopedJniMonitorLock {
254 public:
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700255 ScopedJniMonitorLock(JNIEnv* env, jobject locked) : env_(env), locked_(locked) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700256 env->MonitorEnter(locked_);
Jesse Wilson6bf19152011-09-29 13:12:33 -0400257 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700258 ~ScopedJniMonitorLock() {
259 env_->MonitorExit(locked_);
260 }
261 private:
262 JNIEnv* const env_;
263 const jobject locked_;
264};
Jesse Wilson6bf19152011-09-29 13:12:33 -0400265
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700266jobject DexFile::GetDexObject(JNIEnv* env) const {
267 {
268 ScopedJniMonitorLock lock(env, WellKnownClasses::com_android_dex_Dex);
269 if (dex_object_ != NULL) {
270 return dex_object_;
271 }
272 }
Ian Rogers30fab402012-01-23 15:43:46 -0800273 void* address = const_cast<void*>(reinterpret_cast<const void*>(begin_));
jeffhaof6174e82012-01-31 16:14:17 -0800274 jobject byte_buffer = env->NewDirectByteBuffer(address, size_);
Jesse Wilson6bf19152011-09-29 13:12:33 -0400275 if (byte_buffer == NULL) {
276 return NULL;
277 }
278
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700279 ScopedJniMonitorLock lock(env, WellKnownClasses::com_android_dex_Dex);
280 // Re-test to see if someone beat us to the creation when we had the lock released.
281 if (dex_object_ != NULL) {
282 return dex_object_;
283 }
Jesse Wilson6bf19152011-09-29 13:12:33 -0400284 jvalue args[1];
285 args[0].l = byte_buffer;
Elliott Hugheseac76672012-05-24 21:56:51 -0700286 jobject local = env->CallStaticObjectMethodA(WellKnownClasses::com_android_dex_Dex,
287 WellKnownClasses::com_android_dex_Dex_create,
288 args);
Jesse Wilson6bf19152011-09-29 13:12:33 -0400289 if (local == NULL) {
290 return NULL;
291 }
292
293 dex_object_ = env->NewGlobalRef(local);
294 return dex_object_;
295}
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700296
Brian Carlstromf615a612011-07-23 12:50:34 -0700297bool DexFile::Init() {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700298 InitMembers();
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800299 if (!CheckMagicAndVersion()) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700300 return false;
301 }
302 InitIndex();
303 return true;
304}
305
Brian Carlstromf615a612011-07-23 12:50:34 -0700306void DexFile::InitMembers() {
Ian Rogers30fab402012-01-23 15:43:46 -0800307 const byte* b = begin_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700308 header_ = reinterpret_cast<const Header*>(b);
309 const Header* h = header_;
310 string_ids_ = reinterpret_cast<const StringId*>(b + h->string_ids_off_);
311 type_ids_ = reinterpret_cast<const TypeId*>(b + h->type_ids_off_);
312 field_ids_ = reinterpret_cast<const FieldId*>(b + h->field_ids_off_);
313 method_ids_ = reinterpret_cast<const MethodId*>(b + h->method_ids_off_);
314 proto_ids_ = reinterpret_cast<const ProtoId*>(b + h->proto_ids_off_);
315 class_defs_ = reinterpret_cast<const ClassDef*>(b + h->class_defs_off_);
316}
317
jeffhao10037c82012-01-23 15:06:23 -0800318bool DexFile::CheckMagicAndVersion() const {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800319 CHECK(header_->magic_ != NULL) << GetLocation();
320 if (!IsMagicValid(header_->magic_)) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800321 LOG(ERROR) << "Unrecognized magic number in " << GetLocation() << ":"
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800322 << " " << header_->magic_[0]
323 << " " << header_->magic_[1]
324 << " " << header_->magic_[2]
325 << " " << header_->magic_[3];
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700326 return false;
327 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800328 if (!IsVersionValid(header_->magic_)) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800329 LOG(ERROR) << "Unrecognized version number in " << GetLocation() << ":"
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800330 << " " << header_->magic_[4]
331 << " " << header_->magic_[5]
332 << " " << header_->magic_[6]
333 << " " << header_->magic_[7];
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700334 return false;
335 }
336 return true;
337}
338
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800339bool DexFile::IsMagicValid(const byte* magic) {
340 return (memcmp(magic, kDexMagic, sizeof(kDexMagic)) == 0);
341}
342
343bool DexFile::IsVersionValid(const byte* magic) {
344 const byte* version = &magic[sizeof(kDexMagic)];
345 return (memcmp(version, kDexMagicVersion, sizeof(kDexMagicVersion)) == 0);
346}
347
Ian Rogersd81871c2011-10-03 13:57:23 -0700348uint32_t DexFile::GetVersion() const {
349 const char* version = reinterpret_cast<const char*>(&GetHeader().magic_[sizeof(kDexMagic)]);
350 return atoi(version);
351}
352
Brian Carlstromf615a612011-07-23 12:50:34 -0700353void DexFile::InitIndex() {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800354 CHECK_EQ(index_.size(), 0U) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700355 for (size_t i = 0; i < NumClassDefs(); ++i) {
356 const ClassDef& class_def = GetClassDef(i);
357 const char* descriptor = GetClassDescriptor(class_def);
Elliott Hughesa0e18062012-04-13 15:59:59 -0700358 index_.Put(descriptor, i);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700359 }
360}
361
Brian Carlstrome24fa612011-09-29 00:53:55 -0700362bool DexFile::FindClassDefIndex(const StringPiece& descriptor, uint32_t& idx) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700363 Index::const_iterator it = index_.find(descriptor);
364 if (it == index_.end()) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700365 return false;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700366 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700367 idx = it->second;
368 return true;
369}
370
371const DexFile::ClassDef* DexFile::FindClassDef(const StringPiece& descriptor) const {
372 uint32_t idx;
373 if (FindClassDefIndex(descriptor, idx)) {
374 return &GetClassDef(idx);
375 }
376 return NULL;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700377}
378
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800379const DexFile::FieldId* DexFile::FindFieldId(const DexFile::TypeId& declaring_klass,
380 const DexFile::StringId& name,
381 const DexFile::TypeId& type) const {
382 // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx
383 const uint16_t class_idx = GetIndexForTypeId(declaring_klass);
384 const uint32_t name_idx = GetIndexForStringId(name);
385 const uint16_t type_idx = GetIndexForTypeId(type);
Ian Rogersf8582c32013-05-29 16:33:03 -0700386 int32_t lo = 0;
387 int32_t hi = NumFieldIds() - 1;
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800388 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700389 int32_t mid = (hi + lo) / 2;
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800390 const DexFile::FieldId& field = GetFieldId(mid);
391 if (class_idx > field.class_idx_) {
392 lo = mid + 1;
393 } else if (class_idx < field.class_idx_) {
394 hi = mid - 1;
395 } else {
396 if (name_idx > field.name_idx_) {
397 lo = mid + 1;
398 } else if (name_idx < field.name_idx_) {
399 hi = mid - 1;
400 } else {
401 if (type_idx > field.type_idx_) {
402 lo = mid + 1;
403 } else if (type_idx < field.type_idx_) {
404 hi = mid - 1;
405 } else {
406 return &field;
407 }
408 }
409 }
410 }
411 return NULL;
412}
413
414const DexFile::MethodId* DexFile::FindMethodId(const DexFile::TypeId& declaring_klass,
Ian Rogers0571d352011-11-03 19:51:38 -0700415 const DexFile::StringId& name,
416 const DexFile::ProtoId& signature) const {
417 // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800418 const uint16_t class_idx = GetIndexForTypeId(declaring_klass);
Ian Rogers0571d352011-11-03 19:51:38 -0700419 const uint32_t name_idx = GetIndexForStringId(name);
420 const uint16_t proto_idx = GetIndexForProtoId(signature);
Ian Rogersf8582c32013-05-29 16:33:03 -0700421 int32_t lo = 0;
422 int32_t hi = NumMethodIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700423 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700424 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700425 const DexFile::MethodId& method = GetMethodId(mid);
426 if (class_idx > method.class_idx_) {
427 lo = mid + 1;
428 } else if (class_idx < method.class_idx_) {
429 hi = mid - 1;
430 } else {
431 if (name_idx > method.name_idx_) {
432 lo = mid + 1;
433 } else if (name_idx < method.name_idx_) {
434 hi = mid - 1;
435 } else {
436 if (proto_idx > method.proto_idx_) {
437 lo = mid + 1;
438 } else if (proto_idx < method.proto_idx_) {
439 hi = mid - 1;
440 } else {
441 return &method;
442 }
443 }
444 }
445 }
446 return NULL;
447}
448
Ian Rogers637c65b2013-05-31 11:46:00 -0700449const DexFile::StringId* DexFile::FindStringId(const char* string) const {
Ian Rogersf8582c32013-05-29 16:33:03 -0700450 int32_t lo = 0;
451 int32_t hi = NumStringIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700452 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700453 int32_t mid = (hi + lo) / 2;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800454 uint32_t length;
Ian Rogers0571d352011-11-03 19:51:38 -0700455 const DexFile::StringId& str_id = GetStringId(mid);
456 const char* str = GetStringDataAndLength(str_id, &length);
Ian Rogers637c65b2013-05-31 11:46:00 -0700457 int compare = CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(string, str);
458 if (compare > 0) {
459 lo = mid + 1;
460 } else if (compare < 0) {
461 hi = mid - 1;
462 } else {
463 return &str_id;
464 }
465 }
466 return NULL;
467}
468
469const DexFile::StringId* DexFile::FindStringId(const uint16_t* string) const {
470 int32_t lo = 0;
471 int32_t hi = NumStringIds() - 1;
472 while (hi >= lo) {
473 int32_t mid = (hi + lo) / 2;
474 uint32_t length;
475 const DexFile::StringId& str_id = GetStringId(mid);
476 const char* str = GetStringDataAndLength(str_id, &length);
477 int compare = CompareModifiedUtf8ToUtf16AsCodePointValues(str, string);
Ian Rogers0571d352011-11-03 19:51:38 -0700478 if (compare > 0) {
479 lo = mid + 1;
480 } else if (compare < 0) {
481 hi = mid - 1;
482 } else {
483 return &str_id;
484 }
485 }
486 return NULL;
487}
488
489const DexFile::TypeId* DexFile::FindTypeId(uint32_t string_idx) const {
Ian Rogersf8582c32013-05-29 16:33:03 -0700490 int32_t lo = 0;
491 int32_t hi = NumTypeIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700492 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700493 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700494 const TypeId& type_id = GetTypeId(mid);
495 if (string_idx > type_id.descriptor_idx_) {
496 lo = mid + 1;
497 } else if (string_idx < type_id.descriptor_idx_) {
498 hi = mid - 1;
499 } else {
500 return &type_id;
501 }
502 }
503 return NULL;
504}
505
506const DexFile::ProtoId* DexFile::FindProtoId(uint16_t return_type_idx,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800507 const std::vector<uint16_t>& signature_type_idxs) const {
Ian Rogersf8582c32013-05-29 16:33:03 -0700508 int32_t lo = 0;
509 int32_t hi = NumProtoIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700510 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700511 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700512 const DexFile::ProtoId& proto = GetProtoId(mid);
513 int compare = return_type_idx - proto.return_type_idx_;
514 if (compare == 0) {
515 DexFileParameterIterator it(*this, proto);
516 size_t i = 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800517 while (it.HasNext() && i < signature_type_idxs.size() && compare == 0) {
518 compare = signature_type_idxs[i] - it.GetTypeIdx();
Ian Rogers0571d352011-11-03 19:51:38 -0700519 it.Next();
520 i++;
521 }
522 if (compare == 0) {
523 if (it.HasNext()) {
524 compare = -1;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800525 } else if (i < signature_type_idxs.size()) {
Ian Rogers0571d352011-11-03 19:51:38 -0700526 compare = 1;
527 }
528 }
529 }
530 if (compare > 0) {
531 lo = mid + 1;
532 } else if (compare < 0) {
533 hi = mid - 1;
534 } else {
535 return &proto;
536 }
537 }
538 return NULL;
539}
540
541// Given a signature place the type ids into the given vector
542bool DexFile::CreateTypeList(uint16_t* return_type_idx, std::vector<uint16_t>* param_type_idxs,
543 const std::string& signature) const {
544 if (signature[0] != '(') {
545 return false;
546 }
547 size_t offset = 1;
548 size_t end = signature.size();
549 bool process_return = false;
550 while (offset < end) {
551 char c = signature[offset];
552 offset++;
553 if (c == ')') {
554 process_return = true;
555 continue;
556 }
557 std::string descriptor;
558 descriptor += c;
559 while (c == '[') { // process array prefix
560 if (offset >= end) { // expect some descriptor following [
561 return false;
562 }
563 c = signature[offset];
564 offset++;
565 descriptor += c;
566 }
567 if (c == 'L') { // process type descriptors
568 do {
569 if (offset >= end) { // unexpected early termination of descriptor
570 return false;
571 }
572 c = signature[offset];
573 offset++;
574 descriptor += c;
575 } while (c != ';');
576 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700577 const DexFile::StringId* string_id = FindStringId(descriptor.c_str());
Ian Rogers0571d352011-11-03 19:51:38 -0700578 if (string_id == NULL) {
579 return false;
580 }
581 const DexFile::TypeId* type_id = FindTypeId(GetIndexForStringId(*string_id));
582 if (type_id == NULL) {
583 return false;
584 }
585 uint16_t type_idx = GetIndexForTypeId(*type_id);
586 if (!process_return) {
587 param_type_idxs->push_back(type_idx);
588 } else {
589 *return_type_idx = type_idx;
590 return offset == end; // return true if the signature had reached a sensible end
591 }
592 }
593 return false; // failed to correctly parse return type
594}
595
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700596// Materializes the method descriptor for a method prototype. Method
597// descriptors are not stored directly in the dex file. Instead, one
598// must assemble the descriptor from references in the prototype.
Ian Rogers0571d352011-11-03 19:51:38 -0700599std::string DexFile::CreateMethodSignature(uint32_t proto_idx, int32_t* unicode_length) const {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700600 const ProtoId& proto_id = GetProtoId(proto_idx);
601 std::string descriptor;
602 descriptor.push_back('(');
603 const TypeList* type_list = GetProtoParameters(proto_id);
604 size_t parameter_length = 0;
605 if (type_list != NULL) {
606 // A non-zero number of arguments. Append the type names.
607 for (size_t i = 0; i < type_list->Size(); ++i) {
608 const TypeItem& type_item = type_list->GetTypeItem(i);
609 uint32_t type_idx = type_item.type_idx_;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800610 uint32_t type_length;
Ian Rogers0571d352011-11-03 19:51:38 -0700611 const char* name = StringByTypeIdx(type_idx, &type_length);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700612 parameter_length += type_length;
613 descriptor.append(name);
614 }
615 }
616 descriptor.push_back(')');
617 uint32_t return_type_idx = proto_id.return_type_idx_;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800618 uint32_t return_type_length;
Ian Rogers0571d352011-11-03 19:51:38 -0700619 const char* name = StringByTypeIdx(return_type_idx, &return_type_length);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700620 descriptor.append(name);
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700621 if (unicode_length != NULL) {
622 *unicode_length = parameter_length + return_type_length + 2; // 2 for ( and )
623 }
Elliott Hughes0c424cb2011-08-26 10:16:25 -0700624 return descriptor;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700625}
626
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800627int32_t DexFile::GetLineNumFromPC(const mirror::AbstractMethod* method, uint32_t rel_pc) const {
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -0700628 // For native method, lineno should be -2 to indicate it is native. Note that
629 // "line number == -2" is how libcore tells from StackTraceElement.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700630 if (method->GetCodeItemOffset() == 0) {
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -0700631 return -2;
632 }
633
TDYa127c8dc1012012-04-19 07:03:33 -0700634 const CodeItem* code_item = GetCodeItem(method->GetCodeItemOffset());
Elliott Hughescaf76542012-06-28 16:08:22 -0700635 DCHECK(code_item != NULL) << PrettyMethod(method) << " " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700636
637 // A method with no line number info should return -1
638 LineNumFromPcContext context(rel_pc, -1);
TDYa127c8dc1012012-04-19 07:03:33 -0700639 DecodeDebugInfo(code_item, method->IsStatic(), method->GetDexMethodIndex(), LineNumForPcCb,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800640 NULL, &context);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700641 return context.line_num_;
642}
643
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700644int32_t DexFile::FindTryItem(const CodeItem &code_item, uint32_t address) {
Ian Rogers0571d352011-11-03 19:51:38 -0700645 // Note: Signed type is important for max and min.
646 int32_t min = 0;
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700647 int32_t max = code_item.tries_size_ - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700648
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700649 while (min <= max) {
650 int32_t mid = min + ((max - min) / 2);
651
652 const art::DexFile::TryItem* ti = GetTryItems(code_item, mid);
653 uint32_t start = ti->start_addr_;
654 uint32_t end = start + ti->insn_count_;
655
Ian Rogers0571d352011-11-03 19:51:38 -0700656 if (address < start) {
657 max = mid - 1;
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700658 } else if (address >= end) {
659 min = mid + 1;
660 } else { // We have a winner!
661 return mid;
Ian Rogers0571d352011-11-03 19:51:38 -0700662 }
663 }
664 // No match.
665 return -1;
666}
667
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700668int32_t DexFile::FindCatchHandlerOffset(const CodeItem &code_item, uint32_t address) {
669 int32_t try_item = FindTryItem(code_item, address);
670 if (try_item == -1) {
671 return -1;
672 } else {
673 return DexFile::GetTryItems(code_item, try_item)->handler_off_;
674 }
675}
676
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800677void DexFile::DecodeDebugInfo0(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes2435a572012-02-17 16:07:41 -0800678 DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb,
679 void* context, const byte* stream, LocalInfo* local_in_reg) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700680 uint32_t line = DecodeUnsignedLeb128(&stream);
681 uint32_t parameters_size = DecodeUnsignedLeb128(&stream);
682 uint16_t arg_reg = code_item->registers_size_ - code_item->ins_size_;
683 uint32_t address = 0;
Elliott Hughes30646832011-10-13 16:59:46 -0700684 bool need_locals = (local_cb != NULL);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700685
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800686 if (!is_static) {
Elliott Hughes30646832011-10-13 16:59:46 -0700687 if (need_locals) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800688 const char* descriptor = GetMethodDeclaringClassDescriptor(GetMethodId(method_idx));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700689 local_in_reg[arg_reg].name_ = "this";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800690 local_in_reg[arg_reg].descriptor_ = descriptor;
Elliott Hughes392b1242011-11-30 13:55:50 -0800691 local_in_reg[arg_reg].signature_ = NULL;
Elliott Hughes30646832011-10-13 16:59:46 -0700692 local_in_reg[arg_reg].start_address_ = 0;
693 local_in_reg[arg_reg].is_live_ = true;
694 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700695 arg_reg++;
696 }
697
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800698 DexFileParameterIterator it(*this, GetMethodPrototype(GetMethodId(method_idx)));
Ian Rogers0571d352011-11-03 19:51:38 -0700699 for (uint32_t i = 0; i < parameters_size && it.HasNext(); ++i, it.Next()) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700700 if (arg_reg >= code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700701 LOG(ERROR) << "invalid stream - arg reg >= reg size (" << arg_reg
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800702 << " >= " << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700703 return;
704 }
Elliott Hughes392b1242011-11-30 13:55:50 -0800705 uint32_t id = DecodeUnsignedLeb128P1(&stream);
Ian Rogers0571d352011-11-03 19:51:38 -0700706 const char* descriptor = it.GetDescriptor();
Elliott Hughes392b1242011-11-30 13:55:50 -0800707 if (need_locals && id != kDexNoIndex) {
Ian Rogers0571d352011-11-03 19:51:38 -0700708 const char* name = StringDataByIdx(id);
Elliott Hughes30646832011-10-13 16:59:46 -0700709 local_in_reg[arg_reg].name_ = name;
710 local_in_reg[arg_reg].descriptor_ = descriptor;
Elliott Hughes392b1242011-11-30 13:55:50 -0800711 local_in_reg[arg_reg].signature_ = NULL;
Elliott Hughes30646832011-10-13 16:59:46 -0700712 local_in_reg[arg_reg].start_address_ = address;
713 local_in_reg[arg_reg].is_live_ = true;
714 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700715 switch (*descriptor) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700716 case 'D':
717 case 'J':
718 arg_reg += 2;
719 break;
720 default:
721 arg_reg += 1;
722 break;
723 }
724 }
725
Ian Rogers0571d352011-11-03 19:51:38 -0700726 if (it.HasNext()) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800727 LOG(ERROR) << "invalid stream - problem with parameter iterator in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700728 return;
729 }
730
731 for (;;) {
732 uint8_t opcode = *stream++;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700733 uint16_t reg;
jeffhaof8728872011-10-28 19:11:13 -0700734 uint16_t name_idx;
735 uint16_t descriptor_idx;
736 uint16_t signature_idx = 0;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700737
Shih-wei Liao195487c2011-08-20 13:29:04 -0700738 switch (opcode) {
739 case DBG_END_SEQUENCE:
740 return;
741
742 case DBG_ADVANCE_PC:
743 address += DecodeUnsignedLeb128(&stream);
744 break;
745
746 case DBG_ADVANCE_LINE:
Shih-wei Liao8a05d272011-10-15 18:45:43 -0700747 line += DecodeSignedLeb128(&stream);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700748 break;
749
750 case DBG_START_LOCAL:
751 case DBG_START_LOCAL_EXTENDED:
752 reg = DecodeUnsignedLeb128(&stream);
753 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700754 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800755 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700756 return;
757 }
758
jeffhaof8728872011-10-28 19:11:13 -0700759 name_idx = DecodeUnsignedLeb128P1(&stream);
760 descriptor_idx = DecodeUnsignedLeb128P1(&stream);
761 if (opcode == DBG_START_LOCAL_EXTENDED) {
762 signature_idx = DecodeUnsignedLeb128P1(&stream);
763 }
764
Shih-wei Liao195487c2011-08-20 13:29:04 -0700765 // Emit what was previously there, if anything
Elliott Hughes30646832011-10-13 16:59:46 -0700766 if (need_locals) {
Elliott Hughes2435a572012-02-17 16:07:41 -0800767 InvokeLocalCbIfLive(context, reg, address, local_in_reg, local_cb);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700768
Ian Rogers0571d352011-11-03 19:51:38 -0700769 local_in_reg[reg].name_ = StringDataByIdx(name_idx);
770 local_in_reg[reg].descriptor_ = StringByTypeIdx(descriptor_idx);
Elliott Hughes30646832011-10-13 16:59:46 -0700771 if (opcode == DBG_START_LOCAL_EXTENDED) {
Ian Rogers0571d352011-11-03 19:51:38 -0700772 local_in_reg[reg].signature_ = StringDataByIdx(signature_idx);
Elliott Hughes30646832011-10-13 16:59:46 -0700773 }
774 local_in_reg[reg].start_address_ = address;
775 local_in_reg[reg].is_live_ = true;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700776 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700777 break;
778
779 case DBG_END_LOCAL:
780 reg = DecodeUnsignedLeb128(&stream);
781 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700782 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800783 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700784 return;
785 }
786
Elliott Hughes30646832011-10-13 16:59:46 -0700787 if (need_locals) {
Elliott Hughes2435a572012-02-17 16:07:41 -0800788 InvokeLocalCbIfLive(context, reg, address, local_in_reg, local_cb);
Elliott Hughes30646832011-10-13 16:59:46 -0700789 local_in_reg[reg].is_live_ = false;
790 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700791 break;
792
793 case DBG_RESTART_LOCAL:
794 reg = DecodeUnsignedLeb128(&stream);
795 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700796 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800797 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700798 return;
799 }
800
Elliott Hughes30646832011-10-13 16:59:46 -0700801 if (need_locals) {
802 if (local_in_reg[reg].name_ == NULL || local_in_reg[reg].descriptor_ == NULL) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800803 LOG(ERROR) << "invalid stream - no name or descriptor in " << GetLocation();
Elliott Hughes30646832011-10-13 16:59:46 -0700804 return;
805 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700806
Elliott Hughes30646832011-10-13 16:59:46 -0700807 // If the register is live, the "restart" is superfluous,
808 // and we don't want to mess with the existing start address.
809 if (!local_in_reg[reg].is_live_) {
810 local_in_reg[reg].start_address_ = address;
811 local_in_reg[reg].is_live_ = true;
812 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700813 }
814 break;
815
816 case DBG_SET_PROLOGUE_END:
817 case DBG_SET_EPILOGUE_BEGIN:
818 case DBG_SET_FILE:
819 break;
820
Shih-wei Liao8e1b4ff2011-10-15 15:43:51 -0700821 default: {
822 int adjopcode = opcode - DBG_FIRST_SPECIAL;
823
Shih-wei Liao195487c2011-08-20 13:29:04 -0700824 address += adjopcode / DBG_LINE_RANGE;
825 line += DBG_LINE_BASE + (adjopcode % DBG_LINE_RANGE);
826
Elliott Hughes2435a572012-02-17 16:07:41 -0800827 if (position_cb != NULL) {
828 if (position_cb(context, address, line)) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700829 // early exit
830 return;
831 }
832 }
833 break;
Shih-wei Liao8e1b4ff2011-10-15 15:43:51 -0700834 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700835 }
836 }
837}
838
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800839void DexFile::DecodeDebugInfo(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes2435a572012-02-17 16:07:41 -0800840 DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb,
841 void* context) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700842 const byte* stream = GetDebugInfoStream(code_item);
Elliott Hughesee0fa762012-03-26 17:12:41 -0700843 UniquePtr<LocalInfo[]> local_in_reg(local_cb != NULL ? new LocalInfo[code_item->registers_size_] : NULL);
Ian Rogers0571d352011-11-03 19:51:38 -0700844 if (stream != NULL) {
Elliott Hughesee0fa762012-03-26 17:12:41 -0700845 DecodeDebugInfo0(code_item, is_static, method_idx, position_cb, local_cb, context, stream, &local_in_reg[0]);
Ian Rogers0571d352011-11-03 19:51:38 -0700846 }
847 for (int reg = 0; reg < code_item->registers_size_; reg++) {
Elliott Hughesee0fa762012-03-26 17:12:41 -0700848 InvokeLocalCbIfLive(context, reg, code_item->insns_size_in_code_units_, &local_in_reg[0], local_cb);
Ian Rogers0571d352011-11-03 19:51:38 -0700849 }
850}
851
Elliott Hughes2435a572012-02-17 16:07:41 -0800852bool DexFile::LineNumForPcCb(void* raw_context, uint32_t address, uint32_t line_num) {
853 LineNumFromPcContext* context = reinterpret_cast<LineNumFromPcContext*>(raw_context);
Ian Rogers0571d352011-11-03 19:51:38 -0700854
855 // We know that this callback will be called in
856 // ascending address order, so keep going until we find
857 // a match or we've just gone past it.
858 if (address > context->address_) {
859 // The line number from the previous positions callback
860 // wil be the final result.
861 return true;
862 } else {
863 context->line_num_ = line_num;
864 return address == context->address_;
865 }
866}
867
868// Decodes the header section from the class data bytes.
869void ClassDataItemIterator::ReadClassDataHeader() {
870 CHECK(ptr_pos_ != NULL);
871 header_.static_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_);
872 header_.instance_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_);
873 header_.direct_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_);
874 header_.virtual_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_);
875}
876
877void ClassDataItemIterator::ReadClassDataField() {
878 field_.field_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_);
879 field_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_);
Brian Carlstrom68adbe42012-05-11 17:18:08 -0700880 if (last_idx_ != 0 && field_.field_idx_delta_ == 0) {
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -0700881 LOG(WARNING) << "Duplicate field " << PrettyField(GetMemberIndex(), dex_file_)
882 << " in " << dex_file_.GetLocation();
883 }
Ian Rogers0571d352011-11-03 19:51:38 -0700884}
885
886void ClassDataItemIterator::ReadClassDataMethod() {
887 method_.method_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_);
888 method_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_);
889 method_.code_off_ = DecodeUnsignedLeb128(&ptr_pos_);
Brian Carlstrom68adbe42012-05-11 17:18:08 -0700890 if (last_idx_ != 0 && method_.method_idx_delta_ == 0) {
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -0700891 LOG(WARNING) << "Duplicate method " << PrettyMethod(GetMemberIndex(), dex_file_)
892 << " in " << dex_file_.GetLocation();
893 }
Ian Rogers0571d352011-11-03 19:51:38 -0700894}
895
896// Read a signed integer. "zwidth" is the zero-based byte count.
897static int32_t ReadSignedInt(const byte* ptr, int zwidth) {
898 int32_t val = 0;
899 for (int i = zwidth; i >= 0; --i) {
900 val = ((uint32_t)val >> 8) | (((int32_t)*ptr++) << 24);
901 }
902 val >>= (3 - zwidth) * 8;
903 return val;
904}
905
906// Read an unsigned integer. "zwidth" is the zero-based byte count,
907// "fill_on_right" indicates which side we want to zero-fill from.
908static uint32_t ReadUnsignedInt(const byte* ptr, int zwidth, bool fill_on_right) {
909 uint32_t val = 0;
910 if (!fill_on_right) {
911 for (int i = zwidth; i >= 0; --i) {
912 val = (val >> 8) | (((uint32_t)*ptr++) << 24);
913 }
914 val >>= (3 - zwidth) * 8;
915 } else {
916 for (int i = zwidth; i >= 0; --i) {
917 val = (val >> 8) | (((uint32_t)*ptr++) << 24);
918 }
919 }
920 return val;
921}
922
923// Read a signed long. "zwidth" is the zero-based byte count.
924static int64_t ReadSignedLong(const byte* ptr, int zwidth) {
925 int64_t val = 0;
926 for (int i = zwidth; i >= 0; --i) {
927 val = ((uint64_t)val >> 8) | (((int64_t)*ptr++) << 56);
928 }
929 val >>= (7 - zwidth) * 8;
930 return val;
931}
932
933// Read an unsigned long. "zwidth" is the zero-based byte count,
934// "fill_on_right" indicates which side we want to zero-fill from.
935static uint64_t ReadUnsignedLong(const byte* ptr, int zwidth, bool fill_on_right) {
936 uint64_t val = 0;
937 if (!fill_on_right) {
938 for (int i = zwidth; i >= 0; --i) {
939 val = (val >> 8) | (((uint64_t)*ptr++) << 56);
940 }
941 val >>= (7 - zwidth) * 8;
942 } else {
943 for (int i = zwidth; i >= 0; --i) {
944 val = (val >> 8) | (((uint64_t)*ptr++) << 56);
945 }
946 }
947 return val;
948}
949
950EncodedStaticFieldValueIterator::EncodedStaticFieldValueIterator(const DexFile& dex_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800951 mirror::DexCache* dex_cache,
952 mirror::ClassLoader* class_loader,
Ian Rogersca190662012-06-26 15:45:57 -0700953 ClassLinker* linker,
954 const DexFile::ClassDef& class_def)
Brian Carlstrom88f36542012-10-16 23:24:21 -0700955 : dex_file_(dex_file), dex_cache_(dex_cache), class_loader_(class_loader), linker_(linker),
956 array_size_(), pos_(-1), type_(kByte) {
Ian Rogers0571d352011-11-03 19:51:38 -0700957 ptr_ = dex_file.GetEncodedStaticFieldValuesArray(class_def);
958 if (ptr_ == NULL) {
959 array_size_ = 0;
960 } else {
961 array_size_ = DecodeUnsignedLeb128(&ptr_);
962 }
963 if (array_size_ > 0) {
964 Next();
965 }
966}
967
968void EncodedStaticFieldValueIterator::Next() {
969 pos_++;
970 if (pos_ >= array_size_) {
971 return;
972 }
973 byte value_type = *ptr_++;
974 byte value_arg = value_type >> kEncodedValueArgShift;
975 size_t width = value_arg + 1; // assume and correct later
Brian Carlstrom88f36542012-10-16 23:24:21 -0700976 type_ = static_cast<ValueType>(value_type & kEncodedValueTypeMask);
Ian Rogers0571d352011-11-03 19:51:38 -0700977 switch (type_) {
978 case kBoolean:
979 jval_.i = (value_arg != 0) ? 1 : 0;
980 width = 0;
981 break;
982 case kByte:
983 jval_.i = ReadSignedInt(ptr_, value_arg);
984 CHECK(IsInt(8, jval_.i));
985 break;
986 case kShort:
987 jval_.i = ReadSignedInt(ptr_, value_arg);
988 CHECK(IsInt(16, jval_.i));
989 break;
990 case kChar:
991 jval_.i = ReadUnsignedInt(ptr_, value_arg, false);
992 CHECK(IsUint(16, jval_.i));
993 break;
994 case kInt:
995 jval_.i = ReadSignedInt(ptr_, value_arg);
996 break;
997 case kLong:
998 jval_.j = ReadSignedLong(ptr_, value_arg);
999 break;
1000 case kFloat:
1001 jval_.i = ReadUnsignedInt(ptr_, value_arg, true);
1002 break;
1003 case kDouble:
1004 jval_.j = ReadUnsignedLong(ptr_, value_arg, true);
1005 break;
1006 case kString:
1007 case kType:
Ian Rogers0571d352011-11-03 19:51:38 -07001008 jval_.i = ReadUnsignedInt(ptr_, value_arg, false);
1009 break;
1010 case kField:
Brian Carlstrom88f36542012-10-16 23:24:21 -07001011 case kMethod:
1012 case kEnum:
Ian Rogers0571d352011-11-03 19:51:38 -07001013 case kArray:
1014 case kAnnotation:
1015 UNIMPLEMENTED(FATAL) << ": type " << type_;
1016 break;
1017 case kNull:
1018 jval_.l = NULL;
1019 width = 0;
1020 break;
1021 default:
1022 LOG(FATAL) << "Unreached";
1023 }
1024 ptr_ += width;
1025}
1026
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001027void EncodedStaticFieldValueIterator::ReadValueToField(mirror::Field* field) const {
Ian Rogers0571d352011-11-03 19:51:38 -07001028 switch (type_) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001029 case kBoolean: field->SetBoolean(field->GetDeclaringClass(), jval_.z); break;
1030 case kByte: field->SetByte(field->GetDeclaringClass(), jval_.b); break;
1031 case kShort: field->SetShort(field->GetDeclaringClass(), jval_.s); break;
1032 case kChar: field->SetChar(field->GetDeclaringClass(), jval_.c); break;
1033 case kInt: field->SetInt(field->GetDeclaringClass(), jval_.i); break;
1034 case kLong: field->SetLong(field->GetDeclaringClass(), jval_.j); break;
1035 case kFloat: field->SetFloat(field->GetDeclaringClass(), jval_.f); break;
1036 case kDouble: field->SetDouble(field->GetDeclaringClass(), jval_.d); break;
1037 case kNull: field->SetObject(field->GetDeclaringClass(), NULL); break;
Ian Rogers0571d352011-11-03 19:51:38 -07001038 case kString: {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001039 mirror::String* resolved = linker_->ResolveString(dex_file_, jval_.i, dex_cache_);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001040 field->SetObject(field->GetDeclaringClass(), resolved);
Ian Rogers0571d352011-11-03 19:51:38 -07001041 break;
1042 }
Brian Carlstrom88f36542012-10-16 23:24:21 -07001043 case kType: {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001044 mirror::Class* resolved = linker_->ResolveType(dex_file_, jval_.i, dex_cache_, class_loader_);
mikaelpeltierebf6aa82012-11-07 14:02:15 +01001045 field->SetObject(field->GetDeclaringClass(), resolved);
Brian Carlstrom88f36542012-10-16 23:24:21 -07001046 break;
1047 }
Ian Rogers0571d352011-11-03 19:51:38 -07001048 default: UNIMPLEMENTED(FATAL) << ": type " << type_;
1049 }
1050}
1051
1052CatchHandlerIterator::CatchHandlerIterator(const DexFile::CodeItem& code_item, uint32_t address) {
1053 handler_.address_ = -1;
1054 int32_t offset = -1;
1055
1056 // Short-circuit the overwhelmingly common cases.
1057 switch (code_item.tries_size_) {
1058 case 0:
1059 break;
1060 case 1: {
1061 const DexFile::TryItem* tries = DexFile::GetTryItems(code_item, 0);
1062 uint32_t start = tries->start_addr_;
1063 if (address >= start) {
1064 uint32_t end = start + tries->insn_count_;
1065 if (address < end) {
1066 offset = tries->handler_off_;
1067 }
1068 }
1069 break;
1070 }
1071 default:
Ian Rogersdbbc99d2013-04-18 16:51:54 -07001072 offset = DexFile::FindCatchHandlerOffset(code_item, address);
Ian Rogers0571d352011-11-03 19:51:38 -07001073 }
Logan Chien736df022012-04-27 16:25:57 +08001074 Init(code_item, offset);
1075}
1076
1077CatchHandlerIterator::CatchHandlerIterator(const DexFile::CodeItem& code_item,
1078 const DexFile::TryItem& try_item) {
1079 handler_.address_ = -1;
1080 Init(code_item, try_item.handler_off_);
1081}
1082
1083void CatchHandlerIterator::Init(const DexFile::CodeItem& code_item,
1084 int32_t offset) {
Ian Rogers0571d352011-11-03 19:51:38 -07001085 if (offset >= 0) {
Logan Chien736df022012-04-27 16:25:57 +08001086 Init(DexFile::GetCatchHandlerData(code_item, offset));
Ian Rogers0571d352011-11-03 19:51:38 -07001087 } else {
1088 // Not found, initialize as empty
1089 current_data_ = NULL;
1090 remaining_count_ = -1;
1091 catch_all_ = false;
1092 DCHECK(!HasNext());
1093 }
1094}
1095
1096void CatchHandlerIterator::Init(const byte* handler_data) {
1097 current_data_ = handler_data;
1098 remaining_count_ = DecodeSignedLeb128(&current_data_);
1099
1100 // If remaining_count_ is non-positive, then it is the negative of
1101 // the number of catch types, and the catches are followed by a
1102 // catch-all handler.
1103 if (remaining_count_ <= 0) {
1104 catch_all_ = true;
1105 remaining_count_ = -remaining_count_;
1106 } else {
1107 catch_all_ = false;
1108 }
1109 Next();
1110}
1111
1112void CatchHandlerIterator::Next() {
1113 if (remaining_count_ > 0) {
1114 handler_.type_idx_ = DecodeUnsignedLeb128(&current_data_);
1115 handler_.address_ = DecodeUnsignedLeb128(&current_data_);
1116 remaining_count_--;
1117 return;
1118 }
1119
1120 if (catch_all_) {
1121 handler_.type_idx_ = DexFile::kDexNoIndex16;
1122 handler_.address_ = DecodeUnsignedLeb128(&current_data_);
1123 catch_all_ = false;
1124 return;
1125 }
1126
1127 // no more handler
1128 remaining_count_ = -1;
1129}
1130
Carl Shapiro1fb86202011-06-27 17:43:13 -07001131} // namespace art