blob: 80465f2fed79ff52044f4998dc9d7e61e4d52c62 [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 Carlstroma6cc8932012-01-04 14:44:07 -0800209 LOG(ERROR) << "Failed to find classes.dex within " << location;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700210 return NULL;
211 }
212
Brian Carlstrom89521892011-12-07 22:05:07 -0800213 uint32_t length = zip_entry->GetUncompressedLength();
Elliott Hughes850162c2012-01-12 18:46:43 -0800214 std::string name("classes.dex extracted in memory from ");
215 name += location;
216 UniquePtr<MemMap> map(MemMap::MapAnonymous(name.c_str(), NULL, length, PROT_READ | PROT_WRITE));
Brian Carlstrom89521892011-12-07 22:05:07 -0800217 if (map.get() == NULL) {
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800218 LOG(ERROR) << "mmap classes.dex for \"" << location << "\" failed";
Brian Carlstrom89521892011-12-07 22:05:07 -0800219 return NULL;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700220 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800221
222 // Extract classes.dex
223 bool success = zip_entry->ExtractToMemory(*map.get());
224 if (!success) {
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800225 LOG(ERROR) << "Failed to extract classes.dex from '" << location << "' to memory";
Brian Carlstrom89521892011-12-07 22:05:07 -0800226 return NULL;
227 }
228
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800229 const DexFile* dex_file = OpenMemory(location, zip_entry->GetCrc32(), map.release());
jeffhao54c1ceb2012-02-01 11:45:32 -0800230 if (dex_file == NULL) {
231 LOG(ERROR) << "Failed to open dex file '" << location << "' from memory";
232 return NULL;
jeffhaof6174e82012-01-31 16:14:17 -0800233 }
jeffhao54c1ceb2012-02-01 11:45:32 -0800234
235 if (!DexFileVerifier::Verify(dex_file, dex_file->Begin(), dex_file->Size())) {
236 LOG(ERROR) << "Failed to verify dex file '" << location << "'";
237 return NULL;
238 }
239
jeffhaof6174e82012-01-31 16:14:17 -0800240 return dex_file;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700241}
242
Brian Carlstrom89521892011-12-07 22:05:07 -0800243const DexFile* DexFile::OpenMemory(const byte* base,
jeffhaof6174e82012-01-31 16:14:17 -0800244 size_t size,
Brian Carlstrom89521892011-12-07 22:05:07 -0800245 const std::string& location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800246 uint32_t location_checksum,
Brian Carlstrom89521892011-12-07 22:05:07 -0800247 MemMap* mem_map) {
248 CHECK_ALIGNED(base, 4); // various dex file structures must be word aligned
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800249 UniquePtr<DexFile> dex_file(new DexFile(base, size, location, location_checksum, mem_map));
Brian Carlstromf615a612011-07-23 12:50:34 -0700250 if (!dex_file->Init()) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700251 return NULL;
252 } else {
Brian Carlstromf615a612011-07-23 12:50:34 -0700253 return dex_file.release();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700254 }
255}
256
Jesse Wilson6bf19152011-09-29 13:12:33 -0400257DexFile::~DexFile() {
Elliott Hughes8cef0b82011-10-11 19:24:00 -0700258 // We don't call DeleteGlobalRef on dex_object_ because we're only called by DestroyJavaVM, and
259 // that's only called after DetachCurrentThread, which means there's no JNIEnv. We could
260 // re-attach, but cleaning up these global references is not obviously useful. It's not as if
261 // the global reference table is otherwise empty!
Jesse Wilson6bf19152011-09-29 13:12:33 -0400262}
263
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700264class ScopedJniMonitorLock {
265 public:
266 ScopedJniMonitorLock(JNIEnv* env, jobject locked) : env_(env), locked_(locked){
267 env->MonitorEnter(locked_);
Jesse Wilson6bf19152011-09-29 13:12:33 -0400268 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700269 ~ScopedJniMonitorLock() {
270 env_->MonitorExit(locked_);
271 }
272 private:
273 JNIEnv* const env_;
274 const jobject locked_;
275};
Jesse Wilson6bf19152011-09-29 13:12:33 -0400276
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700277jobject DexFile::GetDexObject(JNIEnv* env) const {
278 {
279 ScopedJniMonitorLock lock(env, WellKnownClasses::com_android_dex_Dex);
280 if (dex_object_ != NULL) {
281 return dex_object_;
282 }
283 }
Ian Rogers30fab402012-01-23 15:43:46 -0800284 void* address = const_cast<void*>(reinterpret_cast<const void*>(begin_));
jeffhaof6174e82012-01-31 16:14:17 -0800285 jobject byte_buffer = env->NewDirectByteBuffer(address, size_);
Jesse Wilson6bf19152011-09-29 13:12:33 -0400286 if (byte_buffer == NULL) {
287 return NULL;
288 }
289
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700290 ScopedJniMonitorLock lock(env, WellKnownClasses::com_android_dex_Dex);
291 // Re-test to see if someone beat us to the creation when we had the lock released.
292 if (dex_object_ != NULL) {
293 return dex_object_;
294 }
Jesse Wilson6bf19152011-09-29 13:12:33 -0400295 jvalue args[1];
296 args[0].l = byte_buffer;
Elliott Hugheseac76672012-05-24 21:56:51 -0700297 jobject local = env->CallStaticObjectMethodA(WellKnownClasses::com_android_dex_Dex,
298 WellKnownClasses::com_android_dex_Dex_create,
299 args);
Jesse Wilson6bf19152011-09-29 13:12:33 -0400300 if (local == NULL) {
301 return NULL;
302 }
303
304 dex_object_ = env->NewGlobalRef(local);
305 return dex_object_;
306}
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700307
Brian Carlstromf615a612011-07-23 12:50:34 -0700308bool DexFile::Init() {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700309 InitMembers();
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800310 if (!CheckMagicAndVersion()) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700311 return false;
312 }
313 InitIndex();
314 return true;
315}
316
Brian Carlstromf615a612011-07-23 12:50:34 -0700317void DexFile::InitMembers() {
Ian Rogers30fab402012-01-23 15:43:46 -0800318 const byte* b = begin_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700319 header_ = reinterpret_cast<const Header*>(b);
320 const Header* h = header_;
321 string_ids_ = reinterpret_cast<const StringId*>(b + h->string_ids_off_);
322 type_ids_ = reinterpret_cast<const TypeId*>(b + h->type_ids_off_);
323 field_ids_ = reinterpret_cast<const FieldId*>(b + h->field_ids_off_);
324 method_ids_ = reinterpret_cast<const MethodId*>(b + h->method_ids_off_);
325 proto_ids_ = reinterpret_cast<const ProtoId*>(b + h->proto_ids_off_);
326 class_defs_ = reinterpret_cast<const ClassDef*>(b + h->class_defs_off_);
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700327 DCHECK_EQ(size_, header_->file_size_) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700328}
329
jeffhao10037c82012-01-23 15:06:23 -0800330bool DexFile::CheckMagicAndVersion() const {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800331 CHECK(header_->magic_ != NULL) << GetLocation();
332 if (!IsMagicValid(header_->magic_)) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800333 LOG(ERROR) << "Unrecognized magic number in " << GetLocation() << ":"
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800334 << " " << header_->magic_[0]
335 << " " << header_->magic_[1]
336 << " " << header_->magic_[2]
337 << " " << header_->magic_[3];
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700338 return false;
339 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800340 if (!IsVersionValid(header_->magic_)) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800341 LOG(ERROR) << "Unrecognized version number in " << GetLocation() << ":"
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800342 << " " << header_->magic_[4]
343 << " " << header_->magic_[5]
344 << " " << header_->magic_[6]
345 << " " << header_->magic_[7];
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700346 return false;
347 }
348 return true;
349}
350
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800351bool DexFile::IsMagicValid(const byte* magic) {
352 return (memcmp(magic, kDexMagic, sizeof(kDexMagic)) == 0);
353}
354
355bool DexFile::IsVersionValid(const byte* magic) {
356 const byte* version = &magic[sizeof(kDexMagic)];
357 return (memcmp(version, kDexMagicVersion, sizeof(kDexMagicVersion)) == 0);
358}
359
Ian Rogersd81871c2011-10-03 13:57:23 -0700360uint32_t DexFile::GetVersion() const {
361 const char* version = reinterpret_cast<const char*>(&GetHeader().magic_[sizeof(kDexMagic)]);
362 return atoi(version);
363}
364
Brian Carlstromf615a612011-07-23 12:50:34 -0700365void DexFile::InitIndex() {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800366 CHECK_EQ(index_.size(), 0U) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700367 for (size_t i = 0; i < NumClassDefs(); ++i) {
368 const ClassDef& class_def = GetClassDef(i);
369 const char* descriptor = GetClassDescriptor(class_def);
Elliott Hughesa0e18062012-04-13 15:59:59 -0700370 index_.Put(descriptor, i);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700371 }
372}
373
Brian Carlstrome24fa612011-09-29 00:53:55 -0700374bool DexFile::FindClassDefIndex(const StringPiece& descriptor, uint32_t& idx) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700375 Index::const_iterator it = index_.find(descriptor);
376 if (it == index_.end()) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700377 return false;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700378 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700379 idx = it->second;
380 return true;
381}
382
383const DexFile::ClassDef* DexFile::FindClassDef(const StringPiece& descriptor) const {
384 uint32_t idx;
385 if (FindClassDefIndex(descriptor, idx)) {
386 return &GetClassDef(idx);
387 }
388 return NULL;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700389}
390
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800391const DexFile::FieldId* DexFile::FindFieldId(const DexFile::TypeId& declaring_klass,
392 const DexFile::StringId& name,
393 const DexFile::TypeId& type) const {
394 // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx
395 const uint16_t class_idx = GetIndexForTypeId(declaring_klass);
396 const uint32_t name_idx = GetIndexForStringId(name);
397 const uint16_t type_idx = GetIndexForTypeId(type);
Ian Rogersf8582c32013-05-29 16:33:03 -0700398 int32_t lo = 0;
399 int32_t hi = NumFieldIds() - 1;
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800400 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700401 int32_t mid = (hi + lo) / 2;
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800402 const DexFile::FieldId& field = GetFieldId(mid);
403 if (class_idx > field.class_idx_) {
404 lo = mid + 1;
405 } else if (class_idx < field.class_idx_) {
406 hi = mid - 1;
407 } else {
408 if (name_idx > field.name_idx_) {
409 lo = mid + 1;
410 } else if (name_idx < field.name_idx_) {
411 hi = mid - 1;
412 } else {
413 if (type_idx > field.type_idx_) {
414 lo = mid + 1;
415 } else if (type_idx < field.type_idx_) {
416 hi = mid - 1;
417 } else {
418 return &field;
419 }
420 }
421 }
422 }
423 return NULL;
424}
425
426const DexFile::MethodId* DexFile::FindMethodId(const DexFile::TypeId& declaring_klass,
Ian Rogers0571d352011-11-03 19:51:38 -0700427 const DexFile::StringId& name,
428 const DexFile::ProtoId& signature) const {
429 // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800430 const uint16_t class_idx = GetIndexForTypeId(declaring_klass);
Ian Rogers0571d352011-11-03 19:51:38 -0700431 const uint32_t name_idx = GetIndexForStringId(name);
432 const uint16_t proto_idx = GetIndexForProtoId(signature);
Ian Rogersf8582c32013-05-29 16:33:03 -0700433 int32_t lo = 0;
434 int32_t hi = NumMethodIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700435 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700436 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700437 const DexFile::MethodId& method = GetMethodId(mid);
438 if (class_idx > method.class_idx_) {
439 lo = mid + 1;
440 } else if (class_idx < method.class_idx_) {
441 hi = mid - 1;
442 } else {
443 if (name_idx > method.name_idx_) {
444 lo = mid + 1;
445 } else if (name_idx < method.name_idx_) {
446 hi = mid - 1;
447 } else {
448 if (proto_idx > method.proto_idx_) {
449 lo = mid + 1;
450 } else if (proto_idx < method.proto_idx_) {
451 hi = mid - 1;
452 } else {
453 return &method;
454 }
455 }
456 }
457 }
458 return NULL;
459}
460
Ian Rogers637c65b2013-05-31 11:46:00 -0700461const DexFile::StringId* DexFile::FindStringId(const char* string) const {
Ian Rogersf8582c32013-05-29 16:33:03 -0700462 int32_t lo = 0;
463 int32_t hi = NumStringIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700464 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700465 int32_t mid = (hi + lo) / 2;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800466 uint32_t length;
Ian Rogers0571d352011-11-03 19:51:38 -0700467 const DexFile::StringId& str_id = GetStringId(mid);
468 const char* str = GetStringDataAndLength(str_id, &length);
Ian Rogers637c65b2013-05-31 11:46:00 -0700469 int compare = CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(string, str);
470 if (compare > 0) {
471 lo = mid + 1;
472 } else if (compare < 0) {
473 hi = mid - 1;
474 } else {
475 return &str_id;
476 }
477 }
478 return NULL;
479}
480
481const DexFile::StringId* DexFile::FindStringId(const uint16_t* string) const {
482 int32_t lo = 0;
483 int32_t hi = NumStringIds() - 1;
484 while (hi >= lo) {
485 int32_t mid = (hi + lo) / 2;
486 uint32_t length;
487 const DexFile::StringId& str_id = GetStringId(mid);
488 const char* str = GetStringDataAndLength(str_id, &length);
489 int compare = CompareModifiedUtf8ToUtf16AsCodePointValues(str, string);
Ian Rogers0571d352011-11-03 19:51:38 -0700490 if (compare > 0) {
491 lo = mid + 1;
492 } else if (compare < 0) {
493 hi = mid - 1;
494 } else {
495 return &str_id;
496 }
497 }
498 return NULL;
499}
500
501const DexFile::TypeId* DexFile::FindTypeId(uint32_t string_idx) const {
Ian Rogersf8582c32013-05-29 16:33:03 -0700502 int32_t lo = 0;
503 int32_t hi = NumTypeIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700504 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700505 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700506 const TypeId& type_id = GetTypeId(mid);
507 if (string_idx > type_id.descriptor_idx_) {
508 lo = mid + 1;
509 } else if (string_idx < type_id.descriptor_idx_) {
510 hi = mid - 1;
511 } else {
512 return &type_id;
513 }
514 }
515 return NULL;
516}
517
518const DexFile::ProtoId* DexFile::FindProtoId(uint16_t return_type_idx,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800519 const std::vector<uint16_t>& signature_type_idxs) const {
Ian Rogersf8582c32013-05-29 16:33:03 -0700520 int32_t lo = 0;
521 int32_t hi = NumProtoIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700522 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700523 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700524 const DexFile::ProtoId& proto = GetProtoId(mid);
525 int compare = return_type_idx - proto.return_type_idx_;
526 if (compare == 0) {
527 DexFileParameterIterator it(*this, proto);
528 size_t i = 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800529 while (it.HasNext() && i < signature_type_idxs.size() && compare == 0) {
530 compare = signature_type_idxs[i] - it.GetTypeIdx();
Ian Rogers0571d352011-11-03 19:51:38 -0700531 it.Next();
532 i++;
533 }
534 if (compare == 0) {
535 if (it.HasNext()) {
536 compare = -1;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800537 } else if (i < signature_type_idxs.size()) {
Ian Rogers0571d352011-11-03 19:51:38 -0700538 compare = 1;
539 }
540 }
541 }
542 if (compare > 0) {
543 lo = mid + 1;
544 } else if (compare < 0) {
545 hi = mid - 1;
546 } else {
547 return &proto;
548 }
549 }
550 return NULL;
551}
552
553// Given a signature place the type ids into the given vector
554bool DexFile::CreateTypeList(uint16_t* return_type_idx, std::vector<uint16_t>* param_type_idxs,
555 const std::string& signature) const {
556 if (signature[0] != '(') {
557 return false;
558 }
559 size_t offset = 1;
560 size_t end = signature.size();
561 bool process_return = false;
562 while (offset < end) {
563 char c = signature[offset];
564 offset++;
565 if (c == ')') {
566 process_return = true;
567 continue;
568 }
569 std::string descriptor;
570 descriptor += c;
571 while (c == '[') { // process array prefix
572 if (offset >= end) { // expect some descriptor following [
573 return false;
574 }
575 c = signature[offset];
576 offset++;
577 descriptor += c;
578 }
579 if (c == 'L') { // process type descriptors
580 do {
581 if (offset >= end) { // unexpected early termination of descriptor
582 return false;
583 }
584 c = signature[offset];
585 offset++;
586 descriptor += c;
587 } while (c != ';');
588 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700589 const DexFile::StringId* string_id = FindStringId(descriptor.c_str());
Ian Rogers0571d352011-11-03 19:51:38 -0700590 if (string_id == NULL) {
591 return false;
592 }
593 const DexFile::TypeId* type_id = FindTypeId(GetIndexForStringId(*string_id));
594 if (type_id == NULL) {
595 return false;
596 }
597 uint16_t type_idx = GetIndexForTypeId(*type_id);
598 if (!process_return) {
599 param_type_idxs->push_back(type_idx);
600 } else {
601 *return_type_idx = type_idx;
602 return offset == end; // return true if the signature had reached a sensible end
603 }
604 }
605 return false; // failed to correctly parse return type
606}
607
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700608// Materializes the method descriptor for a method prototype. Method
609// descriptors are not stored directly in the dex file. Instead, one
610// must assemble the descriptor from references in the prototype.
Ian Rogers0571d352011-11-03 19:51:38 -0700611std::string DexFile::CreateMethodSignature(uint32_t proto_idx, int32_t* unicode_length) const {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700612 const ProtoId& proto_id = GetProtoId(proto_idx);
613 std::string descriptor;
614 descriptor.push_back('(');
615 const TypeList* type_list = GetProtoParameters(proto_id);
616 size_t parameter_length = 0;
617 if (type_list != NULL) {
618 // A non-zero number of arguments. Append the type names.
619 for (size_t i = 0; i < type_list->Size(); ++i) {
620 const TypeItem& type_item = type_list->GetTypeItem(i);
621 uint32_t type_idx = type_item.type_idx_;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800622 uint32_t type_length;
Ian Rogers0571d352011-11-03 19:51:38 -0700623 const char* name = StringByTypeIdx(type_idx, &type_length);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700624 parameter_length += type_length;
625 descriptor.append(name);
626 }
627 }
628 descriptor.push_back(')');
629 uint32_t return_type_idx = proto_id.return_type_idx_;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800630 uint32_t return_type_length;
Ian Rogers0571d352011-11-03 19:51:38 -0700631 const char* name = StringByTypeIdx(return_type_idx, &return_type_length);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700632 descriptor.append(name);
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700633 if (unicode_length != NULL) {
634 *unicode_length = parameter_length + return_type_length + 2; // 2 for ( and )
635 }
Elliott Hughes0c424cb2011-08-26 10:16:25 -0700636 return descriptor;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700637}
638
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800639int32_t DexFile::GetLineNumFromPC(const mirror::AbstractMethod* method, uint32_t rel_pc) const {
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -0700640 // For native method, lineno should be -2 to indicate it is native. Note that
641 // "line number == -2" is how libcore tells from StackTraceElement.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700642 if (method->GetCodeItemOffset() == 0) {
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -0700643 return -2;
644 }
645
TDYa127c8dc1012012-04-19 07:03:33 -0700646 const CodeItem* code_item = GetCodeItem(method->GetCodeItemOffset());
Elliott Hughescaf76542012-06-28 16:08:22 -0700647 DCHECK(code_item != NULL) << PrettyMethod(method) << " " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700648
649 // A method with no line number info should return -1
650 LineNumFromPcContext context(rel_pc, -1);
TDYa127c8dc1012012-04-19 07:03:33 -0700651 DecodeDebugInfo(code_item, method->IsStatic(), method->GetDexMethodIndex(), LineNumForPcCb,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800652 NULL, &context);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700653 return context.line_num_;
654}
655
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700656int32_t DexFile::FindTryItem(const CodeItem &code_item, uint32_t address) {
Ian Rogers0571d352011-11-03 19:51:38 -0700657 // Note: Signed type is important for max and min.
658 int32_t min = 0;
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700659 int32_t max = code_item.tries_size_ - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700660
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700661 while (min <= max) {
662 int32_t mid = min + ((max - min) / 2);
663
664 const art::DexFile::TryItem* ti = GetTryItems(code_item, mid);
665 uint32_t start = ti->start_addr_;
666 uint32_t end = start + ti->insn_count_;
667
Ian Rogers0571d352011-11-03 19:51:38 -0700668 if (address < start) {
669 max = mid - 1;
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700670 } else if (address >= end) {
671 min = mid + 1;
672 } else { // We have a winner!
673 return mid;
Ian Rogers0571d352011-11-03 19:51:38 -0700674 }
675 }
676 // No match.
677 return -1;
678}
679
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700680int32_t DexFile::FindCatchHandlerOffset(const CodeItem &code_item, uint32_t address) {
681 int32_t try_item = FindTryItem(code_item, address);
682 if (try_item == -1) {
683 return -1;
684 } else {
685 return DexFile::GetTryItems(code_item, try_item)->handler_off_;
686 }
687}
688
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800689void DexFile::DecodeDebugInfo0(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes2435a572012-02-17 16:07:41 -0800690 DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb,
691 void* context, const byte* stream, LocalInfo* local_in_reg) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700692 uint32_t line = DecodeUnsignedLeb128(&stream);
693 uint32_t parameters_size = DecodeUnsignedLeb128(&stream);
694 uint16_t arg_reg = code_item->registers_size_ - code_item->ins_size_;
695 uint32_t address = 0;
Elliott Hughes30646832011-10-13 16:59:46 -0700696 bool need_locals = (local_cb != NULL);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700697
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800698 if (!is_static) {
Elliott Hughes30646832011-10-13 16:59:46 -0700699 if (need_locals) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800700 const char* descriptor = GetMethodDeclaringClassDescriptor(GetMethodId(method_idx));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700701 local_in_reg[arg_reg].name_ = "this";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800702 local_in_reg[arg_reg].descriptor_ = descriptor;
Elliott Hughes392b1242011-11-30 13:55:50 -0800703 local_in_reg[arg_reg].signature_ = NULL;
Elliott Hughes30646832011-10-13 16:59:46 -0700704 local_in_reg[arg_reg].start_address_ = 0;
705 local_in_reg[arg_reg].is_live_ = true;
706 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700707 arg_reg++;
708 }
709
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800710 DexFileParameterIterator it(*this, GetMethodPrototype(GetMethodId(method_idx)));
Ian Rogers0571d352011-11-03 19:51:38 -0700711 for (uint32_t i = 0; i < parameters_size && it.HasNext(); ++i, it.Next()) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700712 if (arg_reg >= code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700713 LOG(ERROR) << "invalid stream - arg reg >= reg size (" << arg_reg
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800714 << " >= " << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700715 return;
716 }
Elliott Hughes392b1242011-11-30 13:55:50 -0800717 uint32_t id = DecodeUnsignedLeb128P1(&stream);
Ian Rogers0571d352011-11-03 19:51:38 -0700718 const char* descriptor = it.GetDescriptor();
Elliott Hughes392b1242011-11-30 13:55:50 -0800719 if (need_locals && id != kDexNoIndex) {
Ian Rogers0571d352011-11-03 19:51:38 -0700720 const char* name = StringDataByIdx(id);
Elliott Hughes30646832011-10-13 16:59:46 -0700721 local_in_reg[arg_reg].name_ = name;
722 local_in_reg[arg_reg].descriptor_ = descriptor;
Elliott Hughes392b1242011-11-30 13:55:50 -0800723 local_in_reg[arg_reg].signature_ = NULL;
Elliott Hughes30646832011-10-13 16:59:46 -0700724 local_in_reg[arg_reg].start_address_ = address;
725 local_in_reg[arg_reg].is_live_ = true;
726 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700727 switch (*descriptor) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700728 case 'D':
729 case 'J':
730 arg_reg += 2;
731 break;
732 default:
733 arg_reg += 1;
734 break;
735 }
736 }
737
Ian Rogers0571d352011-11-03 19:51:38 -0700738 if (it.HasNext()) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800739 LOG(ERROR) << "invalid stream - problem with parameter iterator in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700740 return;
741 }
742
743 for (;;) {
744 uint8_t opcode = *stream++;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700745 uint16_t reg;
jeffhaof8728872011-10-28 19:11:13 -0700746 uint16_t name_idx;
747 uint16_t descriptor_idx;
748 uint16_t signature_idx = 0;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700749
Shih-wei Liao195487c2011-08-20 13:29:04 -0700750 switch (opcode) {
751 case DBG_END_SEQUENCE:
752 return;
753
754 case DBG_ADVANCE_PC:
755 address += DecodeUnsignedLeb128(&stream);
756 break;
757
758 case DBG_ADVANCE_LINE:
Shih-wei Liao8a05d272011-10-15 18:45:43 -0700759 line += DecodeSignedLeb128(&stream);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700760 break;
761
762 case DBG_START_LOCAL:
763 case DBG_START_LOCAL_EXTENDED:
764 reg = DecodeUnsignedLeb128(&stream);
765 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700766 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800767 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700768 return;
769 }
770
jeffhaof8728872011-10-28 19:11:13 -0700771 name_idx = DecodeUnsignedLeb128P1(&stream);
772 descriptor_idx = DecodeUnsignedLeb128P1(&stream);
773 if (opcode == DBG_START_LOCAL_EXTENDED) {
774 signature_idx = DecodeUnsignedLeb128P1(&stream);
775 }
776
Shih-wei Liao195487c2011-08-20 13:29:04 -0700777 // Emit what was previously there, if anything
Elliott Hughes30646832011-10-13 16:59:46 -0700778 if (need_locals) {
Elliott Hughes2435a572012-02-17 16:07:41 -0800779 InvokeLocalCbIfLive(context, reg, address, local_in_reg, local_cb);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700780
Ian Rogers0571d352011-11-03 19:51:38 -0700781 local_in_reg[reg].name_ = StringDataByIdx(name_idx);
782 local_in_reg[reg].descriptor_ = StringByTypeIdx(descriptor_idx);
Elliott Hughes30646832011-10-13 16:59:46 -0700783 if (opcode == DBG_START_LOCAL_EXTENDED) {
Ian Rogers0571d352011-11-03 19:51:38 -0700784 local_in_reg[reg].signature_ = StringDataByIdx(signature_idx);
Elliott Hughes30646832011-10-13 16:59:46 -0700785 }
786 local_in_reg[reg].start_address_ = address;
787 local_in_reg[reg].is_live_ = true;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700788 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700789 break;
790
791 case DBG_END_LOCAL:
792 reg = DecodeUnsignedLeb128(&stream);
793 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700794 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800795 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700796 return;
797 }
798
Elliott Hughes30646832011-10-13 16:59:46 -0700799 if (need_locals) {
Elliott Hughes2435a572012-02-17 16:07:41 -0800800 InvokeLocalCbIfLive(context, reg, address, local_in_reg, local_cb);
Elliott Hughes30646832011-10-13 16:59:46 -0700801 local_in_reg[reg].is_live_ = false;
802 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700803 break;
804
805 case DBG_RESTART_LOCAL:
806 reg = DecodeUnsignedLeb128(&stream);
807 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700808 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800809 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700810 return;
811 }
812
Elliott Hughes30646832011-10-13 16:59:46 -0700813 if (need_locals) {
814 if (local_in_reg[reg].name_ == NULL || local_in_reg[reg].descriptor_ == NULL) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800815 LOG(ERROR) << "invalid stream - no name or descriptor in " << GetLocation();
Elliott Hughes30646832011-10-13 16:59:46 -0700816 return;
817 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700818
Elliott Hughes30646832011-10-13 16:59:46 -0700819 // If the register is live, the "restart" is superfluous,
820 // and we don't want to mess with the existing start address.
821 if (!local_in_reg[reg].is_live_) {
822 local_in_reg[reg].start_address_ = address;
823 local_in_reg[reg].is_live_ = true;
824 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700825 }
826 break;
827
828 case DBG_SET_PROLOGUE_END:
829 case DBG_SET_EPILOGUE_BEGIN:
830 case DBG_SET_FILE:
831 break;
832
Shih-wei Liao8e1b4ff2011-10-15 15:43:51 -0700833 default: {
834 int adjopcode = opcode - DBG_FIRST_SPECIAL;
835
Shih-wei Liao195487c2011-08-20 13:29:04 -0700836 address += adjopcode / DBG_LINE_RANGE;
837 line += DBG_LINE_BASE + (adjopcode % DBG_LINE_RANGE);
838
Elliott Hughes2435a572012-02-17 16:07:41 -0800839 if (position_cb != NULL) {
840 if (position_cb(context, address, line)) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700841 // early exit
842 return;
843 }
844 }
845 break;
Shih-wei Liao8e1b4ff2011-10-15 15:43:51 -0700846 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700847 }
848 }
849}
850
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800851void DexFile::DecodeDebugInfo(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes2435a572012-02-17 16:07:41 -0800852 DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb,
853 void* context) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700854 const byte* stream = GetDebugInfoStream(code_item);
Elliott Hughesee0fa762012-03-26 17:12:41 -0700855 UniquePtr<LocalInfo[]> local_in_reg(local_cb != NULL ? new LocalInfo[code_item->registers_size_] : NULL);
Ian Rogers0571d352011-11-03 19:51:38 -0700856 if (stream != NULL) {
Elliott Hughesee0fa762012-03-26 17:12:41 -0700857 DecodeDebugInfo0(code_item, is_static, method_idx, position_cb, local_cb, context, stream, &local_in_reg[0]);
Ian Rogers0571d352011-11-03 19:51:38 -0700858 }
859 for (int reg = 0; reg < code_item->registers_size_; reg++) {
Elliott Hughesee0fa762012-03-26 17:12:41 -0700860 InvokeLocalCbIfLive(context, reg, code_item->insns_size_in_code_units_, &local_in_reg[0], local_cb);
Ian Rogers0571d352011-11-03 19:51:38 -0700861 }
862}
863
Elliott Hughes2435a572012-02-17 16:07:41 -0800864bool DexFile::LineNumForPcCb(void* raw_context, uint32_t address, uint32_t line_num) {
865 LineNumFromPcContext* context = reinterpret_cast<LineNumFromPcContext*>(raw_context);
Ian Rogers0571d352011-11-03 19:51:38 -0700866
867 // We know that this callback will be called in
868 // ascending address order, so keep going until we find
869 // a match or we've just gone past it.
870 if (address > context->address_) {
871 // The line number from the previous positions callback
872 // wil be the final result.
873 return true;
874 } else {
875 context->line_num_ = line_num;
876 return address == context->address_;
877 }
878}
879
880// Decodes the header section from the class data bytes.
881void ClassDataItemIterator::ReadClassDataHeader() {
882 CHECK(ptr_pos_ != NULL);
883 header_.static_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_);
884 header_.instance_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_);
885 header_.direct_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_);
886 header_.virtual_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_);
887}
888
889void ClassDataItemIterator::ReadClassDataField() {
890 field_.field_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_);
891 field_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_);
Brian Carlstrom68adbe42012-05-11 17:18:08 -0700892 if (last_idx_ != 0 && field_.field_idx_delta_ == 0) {
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -0700893 LOG(WARNING) << "Duplicate field " << PrettyField(GetMemberIndex(), dex_file_)
894 << " in " << dex_file_.GetLocation();
895 }
Ian Rogers0571d352011-11-03 19:51:38 -0700896}
897
898void ClassDataItemIterator::ReadClassDataMethod() {
899 method_.method_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_);
900 method_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_);
901 method_.code_off_ = DecodeUnsignedLeb128(&ptr_pos_);
Brian Carlstrom68adbe42012-05-11 17:18:08 -0700902 if (last_idx_ != 0 && method_.method_idx_delta_ == 0) {
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -0700903 LOG(WARNING) << "Duplicate method " << PrettyMethod(GetMemberIndex(), dex_file_)
904 << " in " << dex_file_.GetLocation();
905 }
Ian Rogers0571d352011-11-03 19:51:38 -0700906}
907
908// Read a signed integer. "zwidth" is the zero-based byte count.
909static int32_t ReadSignedInt(const byte* ptr, int zwidth) {
910 int32_t val = 0;
911 for (int i = zwidth; i >= 0; --i) {
912 val = ((uint32_t)val >> 8) | (((int32_t)*ptr++) << 24);
913 }
914 val >>= (3 - zwidth) * 8;
915 return val;
916}
917
918// Read an unsigned integer. "zwidth" is the zero-based byte count,
919// "fill_on_right" indicates which side we want to zero-fill from.
920static uint32_t ReadUnsignedInt(const byte* ptr, int zwidth, bool fill_on_right) {
921 uint32_t val = 0;
922 if (!fill_on_right) {
923 for (int i = zwidth; i >= 0; --i) {
924 val = (val >> 8) | (((uint32_t)*ptr++) << 24);
925 }
926 val >>= (3 - zwidth) * 8;
927 } else {
928 for (int i = zwidth; i >= 0; --i) {
929 val = (val >> 8) | (((uint32_t)*ptr++) << 24);
930 }
931 }
932 return val;
933}
934
935// Read a signed long. "zwidth" is the zero-based byte count.
936static int64_t ReadSignedLong(const byte* ptr, int zwidth) {
937 int64_t val = 0;
938 for (int i = zwidth; i >= 0; --i) {
939 val = ((uint64_t)val >> 8) | (((int64_t)*ptr++) << 56);
940 }
941 val >>= (7 - zwidth) * 8;
942 return val;
943}
944
945// Read an unsigned long. "zwidth" is the zero-based byte count,
946// "fill_on_right" indicates which side we want to zero-fill from.
947static uint64_t ReadUnsignedLong(const byte* ptr, int zwidth, bool fill_on_right) {
948 uint64_t val = 0;
949 if (!fill_on_right) {
950 for (int i = zwidth; i >= 0; --i) {
951 val = (val >> 8) | (((uint64_t)*ptr++) << 56);
952 }
953 val >>= (7 - zwidth) * 8;
954 } else {
955 for (int i = zwidth; i >= 0; --i) {
956 val = (val >> 8) | (((uint64_t)*ptr++) << 56);
957 }
958 }
959 return val;
960}
961
962EncodedStaticFieldValueIterator::EncodedStaticFieldValueIterator(const DexFile& dex_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800963 mirror::DexCache* dex_cache,
964 mirror::ClassLoader* class_loader,
Ian Rogersca190662012-06-26 15:45:57 -0700965 ClassLinker* linker,
966 const DexFile::ClassDef& class_def)
Brian Carlstrom88f36542012-10-16 23:24:21 -0700967 : dex_file_(dex_file), dex_cache_(dex_cache), class_loader_(class_loader), linker_(linker),
968 array_size_(), pos_(-1), type_(kByte) {
Ian Rogers0571d352011-11-03 19:51:38 -0700969 ptr_ = dex_file.GetEncodedStaticFieldValuesArray(class_def);
970 if (ptr_ == NULL) {
971 array_size_ = 0;
972 } else {
973 array_size_ = DecodeUnsignedLeb128(&ptr_);
974 }
975 if (array_size_ > 0) {
976 Next();
977 }
978}
979
980void EncodedStaticFieldValueIterator::Next() {
981 pos_++;
982 if (pos_ >= array_size_) {
983 return;
984 }
985 byte value_type = *ptr_++;
986 byte value_arg = value_type >> kEncodedValueArgShift;
987 size_t width = value_arg + 1; // assume and correct later
Brian Carlstrom88f36542012-10-16 23:24:21 -0700988 type_ = static_cast<ValueType>(value_type & kEncodedValueTypeMask);
Ian Rogers0571d352011-11-03 19:51:38 -0700989 switch (type_) {
990 case kBoolean:
991 jval_.i = (value_arg != 0) ? 1 : 0;
992 width = 0;
993 break;
994 case kByte:
995 jval_.i = ReadSignedInt(ptr_, value_arg);
996 CHECK(IsInt(8, jval_.i));
997 break;
998 case kShort:
999 jval_.i = ReadSignedInt(ptr_, value_arg);
1000 CHECK(IsInt(16, jval_.i));
1001 break;
1002 case kChar:
1003 jval_.i = ReadUnsignedInt(ptr_, value_arg, false);
1004 CHECK(IsUint(16, jval_.i));
1005 break;
1006 case kInt:
1007 jval_.i = ReadSignedInt(ptr_, value_arg);
1008 break;
1009 case kLong:
1010 jval_.j = ReadSignedLong(ptr_, value_arg);
1011 break;
1012 case kFloat:
1013 jval_.i = ReadUnsignedInt(ptr_, value_arg, true);
1014 break;
1015 case kDouble:
1016 jval_.j = ReadUnsignedLong(ptr_, value_arg, true);
1017 break;
1018 case kString:
1019 case kType:
Ian Rogers0571d352011-11-03 19:51:38 -07001020 jval_.i = ReadUnsignedInt(ptr_, value_arg, false);
1021 break;
1022 case kField:
Brian Carlstrom88f36542012-10-16 23:24:21 -07001023 case kMethod:
1024 case kEnum:
Ian Rogers0571d352011-11-03 19:51:38 -07001025 case kArray:
1026 case kAnnotation:
1027 UNIMPLEMENTED(FATAL) << ": type " << type_;
1028 break;
1029 case kNull:
1030 jval_.l = NULL;
1031 width = 0;
1032 break;
1033 default:
1034 LOG(FATAL) << "Unreached";
1035 }
1036 ptr_ += width;
1037}
1038
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001039void EncodedStaticFieldValueIterator::ReadValueToField(mirror::Field* field) const {
Ian Rogers0571d352011-11-03 19:51:38 -07001040 switch (type_) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001041 case kBoolean: field->SetBoolean(field->GetDeclaringClass(), jval_.z); break;
1042 case kByte: field->SetByte(field->GetDeclaringClass(), jval_.b); break;
1043 case kShort: field->SetShort(field->GetDeclaringClass(), jval_.s); break;
1044 case kChar: field->SetChar(field->GetDeclaringClass(), jval_.c); break;
1045 case kInt: field->SetInt(field->GetDeclaringClass(), jval_.i); break;
1046 case kLong: field->SetLong(field->GetDeclaringClass(), jval_.j); break;
1047 case kFloat: field->SetFloat(field->GetDeclaringClass(), jval_.f); break;
1048 case kDouble: field->SetDouble(field->GetDeclaringClass(), jval_.d); break;
1049 case kNull: field->SetObject(field->GetDeclaringClass(), NULL); break;
Ian Rogers0571d352011-11-03 19:51:38 -07001050 case kString: {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001051 mirror::String* resolved = linker_->ResolveString(dex_file_, jval_.i, dex_cache_);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001052 field->SetObject(field->GetDeclaringClass(), resolved);
Ian Rogers0571d352011-11-03 19:51:38 -07001053 break;
1054 }
Brian Carlstrom88f36542012-10-16 23:24:21 -07001055 case kType: {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001056 mirror::Class* resolved = linker_->ResolveType(dex_file_, jval_.i, dex_cache_, class_loader_);
mikaelpeltierebf6aa82012-11-07 14:02:15 +01001057 field->SetObject(field->GetDeclaringClass(), resolved);
Brian Carlstrom88f36542012-10-16 23:24:21 -07001058 break;
1059 }
Ian Rogers0571d352011-11-03 19:51:38 -07001060 default: UNIMPLEMENTED(FATAL) << ": type " << type_;
1061 }
1062}
1063
1064CatchHandlerIterator::CatchHandlerIterator(const DexFile::CodeItem& code_item, uint32_t address) {
1065 handler_.address_ = -1;
1066 int32_t offset = -1;
1067
1068 // Short-circuit the overwhelmingly common cases.
1069 switch (code_item.tries_size_) {
1070 case 0:
1071 break;
1072 case 1: {
1073 const DexFile::TryItem* tries = DexFile::GetTryItems(code_item, 0);
1074 uint32_t start = tries->start_addr_;
1075 if (address >= start) {
1076 uint32_t end = start + tries->insn_count_;
1077 if (address < end) {
1078 offset = tries->handler_off_;
1079 }
1080 }
1081 break;
1082 }
1083 default:
Ian Rogersdbbc99d2013-04-18 16:51:54 -07001084 offset = DexFile::FindCatchHandlerOffset(code_item, address);
Ian Rogers0571d352011-11-03 19:51:38 -07001085 }
Logan Chien736df022012-04-27 16:25:57 +08001086 Init(code_item, offset);
1087}
1088
1089CatchHandlerIterator::CatchHandlerIterator(const DexFile::CodeItem& code_item,
1090 const DexFile::TryItem& try_item) {
1091 handler_.address_ = -1;
1092 Init(code_item, try_item.handler_off_);
1093}
1094
1095void CatchHandlerIterator::Init(const DexFile::CodeItem& code_item,
1096 int32_t offset) {
Ian Rogers0571d352011-11-03 19:51:38 -07001097 if (offset >= 0) {
Logan Chien736df022012-04-27 16:25:57 +08001098 Init(DexFile::GetCatchHandlerData(code_item, offset));
Ian Rogers0571d352011-11-03 19:51:38 -07001099 } else {
1100 // Not found, initialize as empty
1101 current_data_ = NULL;
1102 remaining_count_ = -1;
1103 catch_all_ = false;
1104 DCHECK(!HasNext());
1105 }
1106}
1107
1108void CatchHandlerIterator::Init(const byte* handler_data) {
1109 current_data_ = handler_data;
1110 remaining_count_ = DecodeSignedLeb128(&current_data_);
1111
1112 // If remaining_count_ is non-positive, then it is the negative of
1113 // the number of catch types, and the catches are followed by a
1114 // catch-all handler.
1115 if (remaining_count_ <= 0) {
1116 catch_all_ = true;
1117 remaining_count_ = -remaining_count_;
1118 } else {
1119 catch_all_ = false;
1120 }
1121 Next();
1122}
1123
1124void CatchHandlerIterator::Next() {
1125 if (remaining_count_ > 0) {
1126 handler_.type_idx_ = DecodeUnsignedLeb128(&current_data_);
1127 handler_.address_ = DecodeUnsignedLeb128(&current_data_);
1128 remaining_count_--;
1129 return;
1130 }
1131
1132 if (catch_all_) {
1133 handler_.type_idx_ = DexFile::kDexNoIndex16;
1134 handler_.address_ = DecodeUnsignedLeb128(&current_data_);
1135 catch_all_ = false;
1136 return;
1137 }
1138
1139 // no more handler
1140 remaining_count_ = -1;
1141}
1142
Carl Shapiro1fb86202011-06-27 17:43:13 -07001143} // namespace art