blob: 0f0bed4db945d12ebd296e956e80af35467059a9 [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
Brian Carlstrom16192862011-09-12 17:50:06 -0700111const DexFile* DexFile::OpenFile(const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -0800112 const std::string& location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800113 bool verify) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800114 CHECK(!location.empty()) << filename;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700115 int fd = open(filename.c_str(), O_RDONLY); // TODO: scoped_fd
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700116 if (fd == -1) {
117 PLOG(ERROR) << "open(\"" << filename << "\", O_RDONLY) failed";
118 return NULL;
119 }
120 struct stat sbuf;
121 memset(&sbuf, 0, sizeof(sbuf));
122 if (fstat(fd, &sbuf) == -1) {
123 PLOG(ERROR) << "fstat \"" << filename << "\" failed";
124 close(fd);
125 return NULL;
126 }
Ian Rogers7cfb93e2012-01-17 19:46:36 -0800127 if (S_ISDIR(sbuf.st_mode)) {
128 LOG(ERROR) << "attempt to mmap directory \"" << filename << "\"";
129 return NULL;
130 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700131 size_t length = sbuf.st_size;
Brian Carlstrom89521892011-12-07 22:05:07 -0800132 UniquePtr<MemMap> map(MemMap::MapFile(length, PROT_READ, MAP_PRIVATE, fd, 0));
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700133 if (map.get() == NULL) {
134 LOG(ERROR) << "mmap \"" << filename << "\" failed";
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700135 close(fd);
136 return NULL;
137 }
138 close(fd);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800139
140 if (map->Size() < sizeof(DexFile::Header)) {
141 LOG(ERROR) << "Failed to open dex file '" << filename << "' that is too short to have a header";
142 return NULL;
143 }
144
145 const Header* dex_header = reinterpret_cast<const Header*>(map->Begin());
146
147 const DexFile* dex_file = OpenMemory(location, dex_header->checksum_, map.release());
jeffhao54c1ceb2012-02-01 11:45:32 -0800148 if (dex_file == NULL) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800149 LOG(ERROR) << "Failed to open dex file '" << filename << "' from memory";
jeffhao54c1ceb2012-02-01 11:45:32 -0800150 return NULL;
jeffhaof6174e82012-01-31 16:14:17 -0800151 }
jeffhao54c1ceb2012-02-01 11:45:32 -0800152
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700153 if (verify && !DexFileVerifier::Verify(dex_file, dex_file->Begin(), dex_file->Size())) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800154 LOG(ERROR) << "Failed to verify dex file '" << filename << "'";
jeffhao54c1ceb2012-02-01 11:45:32 -0800155 return NULL;
156 }
157
jeffhaof6174e82012-01-31 16:14:17 -0800158 return dex_file;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700159}
160
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700161const char* DexFile::kClassesDex = "classes.dex";
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700162
Brian Carlstrom16192862011-09-12 17:50:06 -0700163const DexFile* DexFile::OpenZip(const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -0800164 const std::string& location) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700165 UniquePtr<ZipArchive> zip_archive(ZipArchive::Open(filename));
166 if (zip_archive.get() == NULL) {
Brian Carlstrom16192862011-09-12 17:50:06 -0700167 LOG(ERROR) << "Failed to open " << filename << " when looking for classes.dex";
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700168 return NULL;
169 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800170 return DexFile::Open(*zip_archive.get(), location);
171}
172
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800173const DexFile* DexFile::OpenMemory(const std::string& location,
174 uint32_t location_checksum,
175 MemMap* mem_map) {
176 return OpenMemory(mem_map->Begin(),
177 mem_map->Size(),
178 location,
179 location_checksum,
180 mem_map);
181}
182
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800183const DexFile* DexFile::Open(const ZipArchive& zip_archive, const std::string& location) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800184 CHECK(!location.empty());
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800185 UniquePtr<ZipEntry> zip_entry(zip_archive.Find(kClassesDex));
Elliott Hughes90a33692011-08-30 13:27:07 -0700186 if (zip_entry.get() == NULL) {
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800187 LOG(ERROR) << "Failed to find classes.dex within " << location;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700188 return NULL;
189 }
190
Brian Carlstrom89521892011-12-07 22:05:07 -0800191 uint32_t length = zip_entry->GetUncompressedLength();
Elliott Hughes850162c2012-01-12 18:46:43 -0800192 std::string name("classes.dex extracted in memory from ");
193 name += location;
194 UniquePtr<MemMap> map(MemMap::MapAnonymous(name.c_str(), NULL, length, PROT_READ | PROT_WRITE));
Brian Carlstrom89521892011-12-07 22:05:07 -0800195 if (map.get() == NULL) {
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800196 LOG(ERROR) << "mmap classes.dex for \"" << location << "\" failed";
Brian Carlstrom89521892011-12-07 22:05:07 -0800197 return NULL;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700198 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800199
200 // Extract classes.dex
201 bool success = zip_entry->ExtractToMemory(*map.get());
202 if (!success) {
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800203 LOG(ERROR) << "Failed to extract classes.dex from '" << location << "' to memory";
Brian Carlstrom89521892011-12-07 22:05:07 -0800204 return NULL;
205 }
206
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800207 const DexFile* dex_file = OpenMemory(location, zip_entry->GetCrc32(), map.release());
jeffhao54c1ceb2012-02-01 11:45:32 -0800208 if (dex_file == NULL) {
209 LOG(ERROR) << "Failed to open dex file '" << location << "' from memory";
210 return NULL;
jeffhaof6174e82012-01-31 16:14:17 -0800211 }
jeffhao54c1ceb2012-02-01 11:45:32 -0800212
213 if (!DexFileVerifier::Verify(dex_file, dex_file->Begin(), dex_file->Size())) {
214 LOG(ERROR) << "Failed to verify dex file '" << location << "'";
215 return NULL;
216 }
217
jeffhaof6174e82012-01-31 16:14:17 -0800218 return dex_file;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700219}
220
Brian Carlstrom89521892011-12-07 22:05:07 -0800221const DexFile* DexFile::OpenMemory(const byte* base,
jeffhaof6174e82012-01-31 16:14:17 -0800222 size_t size,
Brian Carlstrom89521892011-12-07 22:05:07 -0800223 const std::string& location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800224 uint32_t location_checksum,
Brian Carlstrom89521892011-12-07 22:05:07 -0800225 MemMap* mem_map) {
226 CHECK_ALIGNED(base, 4); // various dex file structures must be word aligned
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800227 UniquePtr<DexFile> dex_file(new DexFile(base, size, location, location_checksum, mem_map));
Brian Carlstromf615a612011-07-23 12:50:34 -0700228 if (!dex_file->Init()) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700229 return NULL;
230 } else {
Brian Carlstromf615a612011-07-23 12:50:34 -0700231 return dex_file.release();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700232 }
233}
234
Jesse Wilson6bf19152011-09-29 13:12:33 -0400235DexFile::~DexFile() {
Elliott Hughes8cef0b82011-10-11 19:24:00 -0700236 // We don't call DeleteGlobalRef on dex_object_ because we're only called by DestroyJavaVM, and
237 // that's only called after DetachCurrentThread, which means there's no JNIEnv. We could
238 // re-attach, but cleaning up these global references is not obviously useful. It's not as if
239 // the global reference table is otherwise empty!
Jesse Wilson6bf19152011-09-29 13:12:33 -0400240}
241
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700242class ScopedJniMonitorLock {
243 public:
244 ScopedJniMonitorLock(JNIEnv* env, jobject locked) : env_(env), locked_(locked){
245 env->MonitorEnter(locked_);
Jesse Wilson6bf19152011-09-29 13:12:33 -0400246 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700247 ~ScopedJniMonitorLock() {
248 env_->MonitorExit(locked_);
249 }
250 private:
251 JNIEnv* const env_;
252 const jobject locked_;
253};
Jesse Wilson6bf19152011-09-29 13:12:33 -0400254
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700255jobject DexFile::GetDexObject(JNIEnv* env) const {
256 {
257 ScopedJniMonitorLock lock(env, WellKnownClasses::com_android_dex_Dex);
258 if (dex_object_ != NULL) {
259 return dex_object_;
260 }
261 }
Ian Rogers30fab402012-01-23 15:43:46 -0800262 void* address = const_cast<void*>(reinterpret_cast<const void*>(begin_));
jeffhaof6174e82012-01-31 16:14:17 -0800263 jobject byte_buffer = env->NewDirectByteBuffer(address, size_);
Jesse Wilson6bf19152011-09-29 13:12:33 -0400264 if (byte_buffer == NULL) {
265 return NULL;
266 }
267
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700268 ScopedJniMonitorLock lock(env, WellKnownClasses::com_android_dex_Dex);
269 // Re-test to see if someone beat us to the creation when we had the lock released.
270 if (dex_object_ != NULL) {
271 return dex_object_;
272 }
Jesse Wilson6bf19152011-09-29 13:12:33 -0400273 jvalue args[1];
274 args[0].l = byte_buffer;
Elliott Hugheseac76672012-05-24 21:56:51 -0700275 jobject local = env->CallStaticObjectMethodA(WellKnownClasses::com_android_dex_Dex,
276 WellKnownClasses::com_android_dex_Dex_create,
277 args);
Jesse Wilson6bf19152011-09-29 13:12:33 -0400278 if (local == NULL) {
279 return NULL;
280 }
281
282 dex_object_ = env->NewGlobalRef(local);
283 return dex_object_;
284}
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700285
Brian Carlstromf615a612011-07-23 12:50:34 -0700286bool DexFile::Init() {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700287 InitMembers();
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800288 if (!CheckMagicAndVersion()) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700289 return false;
290 }
291 InitIndex();
292 return true;
293}
294
Brian Carlstromf615a612011-07-23 12:50:34 -0700295void DexFile::InitMembers() {
Ian Rogers30fab402012-01-23 15:43:46 -0800296 const byte* b = begin_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700297 header_ = reinterpret_cast<const Header*>(b);
298 const Header* h = header_;
299 string_ids_ = reinterpret_cast<const StringId*>(b + h->string_ids_off_);
300 type_ids_ = reinterpret_cast<const TypeId*>(b + h->type_ids_off_);
301 field_ids_ = reinterpret_cast<const FieldId*>(b + h->field_ids_off_);
302 method_ids_ = reinterpret_cast<const MethodId*>(b + h->method_ids_off_);
303 proto_ids_ = reinterpret_cast<const ProtoId*>(b + h->proto_ids_off_);
304 class_defs_ = reinterpret_cast<const ClassDef*>(b + h->class_defs_off_);
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700305 DCHECK_EQ(size_, header_->file_size_) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700306}
307
jeffhao10037c82012-01-23 15:06:23 -0800308bool DexFile::CheckMagicAndVersion() const {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800309 CHECK(header_->magic_ != NULL) << GetLocation();
310 if (!IsMagicValid(header_->magic_)) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800311 LOG(ERROR) << "Unrecognized magic number in " << GetLocation() << ":"
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800312 << " " << header_->magic_[0]
313 << " " << header_->magic_[1]
314 << " " << header_->magic_[2]
315 << " " << header_->magic_[3];
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700316 return false;
317 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800318 if (!IsVersionValid(header_->magic_)) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800319 LOG(ERROR) << "Unrecognized version number in " << GetLocation() << ":"
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800320 << " " << header_->magic_[4]
321 << " " << header_->magic_[5]
322 << " " << header_->magic_[6]
323 << " " << header_->magic_[7];
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700324 return false;
325 }
326 return true;
327}
328
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800329bool DexFile::IsMagicValid(const byte* magic) {
330 return (memcmp(magic, kDexMagic, sizeof(kDexMagic)) == 0);
331}
332
333bool DexFile::IsVersionValid(const byte* magic) {
334 const byte* version = &magic[sizeof(kDexMagic)];
335 return (memcmp(version, kDexMagicVersion, sizeof(kDexMagicVersion)) == 0);
336}
337
Ian Rogersd81871c2011-10-03 13:57:23 -0700338uint32_t DexFile::GetVersion() const {
339 const char* version = reinterpret_cast<const char*>(&GetHeader().magic_[sizeof(kDexMagic)]);
340 return atoi(version);
341}
342
Brian Carlstromf615a612011-07-23 12:50:34 -0700343void DexFile::InitIndex() {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800344 CHECK_EQ(index_.size(), 0U) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700345 for (size_t i = 0; i < NumClassDefs(); ++i) {
346 const ClassDef& class_def = GetClassDef(i);
347 const char* descriptor = GetClassDescriptor(class_def);
Elliott Hughesa0e18062012-04-13 15:59:59 -0700348 index_.Put(descriptor, i);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700349 }
350}
351
Brian Carlstrome24fa612011-09-29 00:53:55 -0700352bool DexFile::FindClassDefIndex(const StringPiece& descriptor, uint32_t& idx) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700353 Index::const_iterator it = index_.find(descriptor);
354 if (it == index_.end()) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700355 return false;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700356 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700357 idx = it->second;
358 return true;
359}
360
361const DexFile::ClassDef* DexFile::FindClassDef(const StringPiece& descriptor) const {
362 uint32_t idx;
363 if (FindClassDefIndex(descriptor, idx)) {
364 return &GetClassDef(idx);
365 }
366 return NULL;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700367}
368
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800369const DexFile::FieldId* DexFile::FindFieldId(const DexFile::TypeId& declaring_klass,
370 const DexFile::StringId& name,
371 const DexFile::TypeId& type) const {
372 // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx
373 const uint16_t class_idx = GetIndexForTypeId(declaring_klass);
374 const uint32_t name_idx = GetIndexForStringId(name);
375 const uint16_t type_idx = GetIndexForTypeId(type);
376 uint32_t lo = 0;
377 uint32_t hi = NumFieldIds() - 1;
378 while (hi >= lo) {
379 uint32_t mid = (hi + lo) / 2;
380 const DexFile::FieldId& field = GetFieldId(mid);
381 if (class_idx > field.class_idx_) {
382 lo = mid + 1;
383 } else if (class_idx < field.class_idx_) {
384 hi = mid - 1;
385 } else {
386 if (name_idx > field.name_idx_) {
387 lo = mid + 1;
388 } else if (name_idx < field.name_idx_) {
389 hi = mid - 1;
390 } else {
391 if (type_idx > field.type_idx_) {
392 lo = mid + 1;
393 } else if (type_idx < field.type_idx_) {
394 hi = mid - 1;
395 } else {
396 return &field;
397 }
398 }
399 }
400 }
401 return NULL;
402}
403
404const DexFile::MethodId* DexFile::FindMethodId(const DexFile::TypeId& declaring_klass,
Ian Rogers0571d352011-11-03 19:51:38 -0700405 const DexFile::StringId& name,
406 const DexFile::ProtoId& signature) const {
407 // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800408 const uint16_t class_idx = GetIndexForTypeId(declaring_klass);
Ian Rogers0571d352011-11-03 19:51:38 -0700409 const uint32_t name_idx = GetIndexForStringId(name);
410 const uint16_t proto_idx = GetIndexForProtoId(signature);
411 uint32_t lo = 0;
412 uint32_t hi = NumMethodIds() - 1;
413 while (hi >= lo) {
414 uint32_t mid = (hi + lo) / 2;
415 const DexFile::MethodId& method = GetMethodId(mid);
416 if (class_idx > method.class_idx_) {
417 lo = mid + 1;
418 } else if (class_idx < method.class_idx_) {
419 hi = mid - 1;
420 } else {
421 if (name_idx > method.name_idx_) {
422 lo = mid + 1;
423 } else if (name_idx < method.name_idx_) {
424 hi = mid - 1;
425 } else {
426 if (proto_idx > method.proto_idx_) {
427 lo = mid + 1;
428 } else if (proto_idx < method.proto_idx_) {
429 hi = mid - 1;
430 } else {
431 return &method;
432 }
433 }
434 }
435 }
436 return NULL;
437}
438
439const DexFile::StringId* DexFile::FindStringId(const std::string& string) const {
440 uint32_t lo = 0;
441 uint32_t hi = NumStringIds() - 1;
442 while (hi >= lo) {
443 uint32_t mid = (hi + lo) / 2;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800444 uint32_t length;
Ian Rogers0571d352011-11-03 19:51:38 -0700445 const DexFile::StringId& str_id = GetStringId(mid);
446 const char* str = GetStringDataAndLength(str_id, &length);
447 int compare = CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(string.c_str(), str);
448 if (compare > 0) {
449 lo = mid + 1;
450 } else if (compare < 0) {
451 hi = mid - 1;
452 } else {
453 return &str_id;
454 }
455 }
456 return NULL;
457}
458
459const DexFile::TypeId* DexFile::FindTypeId(uint32_t string_idx) const {
460 uint32_t lo = 0;
461 uint32_t hi = NumTypeIds() - 1;
462 while (hi >= lo) {
463 uint32_t mid = (hi + lo) / 2;
464 const TypeId& type_id = GetTypeId(mid);
465 if (string_idx > type_id.descriptor_idx_) {
466 lo = mid + 1;
467 } else if (string_idx < type_id.descriptor_idx_) {
468 hi = mid - 1;
469 } else {
470 return &type_id;
471 }
472 }
473 return NULL;
474}
475
476const DexFile::ProtoId* DexFile::FindProtoId(uint16_t return_type_idx,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800477 const std::vector<uint16_t>& signature_type_idxs) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700478 uint32_t lo = 0;
479 uint32_t hi = NumProtoIds() - 1;
480 while (hi >= lo) {
481 uint32_t mid = (hi + lo) / 2;
482 const DexFile::ProtoId& proto = GetProtoId(mid);
483 int compare = return_type_idx - proto.return_type_idx_;
484 if (compare == 0) {
485 DexFileParameterIterator it(*this, proto);
486 size_t i = 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800487 while (it.HasNext() && i < signature_type_idxs.size() && compare == 0) {
488 compare = signature_type_idxs[i] - it.GetTypeIdx();
Ian Rogers0571d352011-11-03 19:51:38 -0700489 it.Next();
490 i++;
491 }
492 if (compare == 0) {
493 if (it.HasNext()) {
494 compare = -1;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800495 } else if (i < signature_type_idxs.size()) {
Ian Rogers0571d352011-11-03 19:51:38 -0700496 compare = 1;
497 }
498 }
499 }
500 if (compare > 0) {
501 lo = mid + 1;
502 } else if (compare < 0) {
503 hi = mid - 1;
504 } else {
505 return &proto;
506 }
507 }
508 return NULL;
509}
510
511// Given a signature place the type ids into the given vector
512bool DexFile::CreateTypeList(uint16_t* return_type_idx, std::vector<uint16_t>* param_type_idxs,
513 const std::string& signature) const {
514 if (signature[0] != '(') {
515 return false;
516 }
517 size_t offset = 1;
518 size_t end = signature.size();
519 bool process_return = false;
520 while (offset < end) {
521 char c = signature[offset];
522 offset++;
523 if (c == ')') {
524 process_return = true;
525 continue;
526 }
527 std::string descriptor;
528 descriptor += c;
529 while (c == '[') { // process array prefix
530 if (offset >= end) { // expect some descriptor following [
531 return false;
532 }
533 c = signature[offset];
534 offset++;
535 descriptor += c;
536 }
537 if (c == 'L') { // process type descriptors
538 do {
539 if (offset >= end) { // unexpected early termination of descriptor
540 return false;
541 }
542 c = signature[offset];
543 offset++;
544 descriptor += c;
545 } while (c != ';');
546 }
547 const DexFile::StringId* string_id = FindStringId(descriptor);
548 if (string_id == NULL) {
549 return false;
550 }
551 const DexFile::TypeId* type_id = FindTypeId(GetIndexForStringId(*string_id));
552 if (type_id == NULL) {
553 return false;
554 }
555 uint16_t type_idx = GetIndexForTypeId(*type_id);
556 if (!process_return) {
557 param_type_idxs->push_back(type_idx);
558 } else {
559 *return_type_idx = type_idx;
560 return offset == end; // return true if the signature had reached a sensible end
561 }
562 }
563 return false; // failed to correctly parse return type
564}
565
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700566// Materializes the method descriptor for a method prototype. Method
567// descriptors are not stored directly in the dex file. Instead, one
568// must assemble the descriptor from references in the prototype.
Ian Rogers0571d352011-11-03 19:51:38 -0700569std::string DexFile::CreateMethodSignature(uint32_t proto_idx, int32_t* unicode_length) const {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700570 const ProtoId& proto_id = GetProtoId(proto_idx);
571 std::string descriptor;
572 descriptor.push_back('(');
573 const TypeList* type_list = GetProtoParameters(proto_id);
574 size_t parameter_length = 0;
575 if (type_list != NULL) {
576 // A non-zero number of arguments. Append the type names.
577 for (size_t i = 0; i < type_list->Size(); ++i) {
578 const TypeItem& type_item = type_list->GetTypeItem(i);
579 uint32_t type_idx = type_item.type_idx_;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800580 uint32_t type_length;
Ian Rogers0571d352011-11-03 19:51:38 -0700581 const char* name = StringByTypeIdx(type_idx, &type_length);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700582 parameter_length += type_length;
583 descriptor.append(name);
584 }
585 }
586 descriptor.push_back(')');
587 uint32_t return_type_idx = proto_id.return_type_idx_;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800588 uint32_t return_type_length;
Ian Rogers0571d352011-11-03 19:51:38 -0700589 const char* name = StringByTypeIdx(return_type_idx, &return_type_length);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700590 descriptor.append(name);
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700591 if (unicode_length != NULL) {
592 *unicode_length = parameter_length + return_type_length + 2; // 2 for ( and )
593 }
Elliott Hughes0c424cb2011-08-26 10:16:25 -0700594 return descriptor;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700595}
596
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800597int32_t DexFile::GetLineNumFromPC(const mirror::AbstractMethod* method, uint32_t rel_pc) const {
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -0700598 // For native method, lineno should be -2 to indicate it is native. Note that
599 // "line number == -2" is how libcore tells from StackTraceElement.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700600 if (method->GetCodeItemOffset() == 0) {
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -0700601 return -2;
602 }
603
TDYa127c8dc1012012-04-19 07:03:33 -0700604 const CodeItem* code_item = GetCodeItem(method->GetCodeItemOffset());
Elliott Hughescaf76542012-06-28 16:08:22 -0700605 DCHECK(code_item != NULL) << PrettyMethod(method) << " " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700606
607 // A method with no line number info should return -1
608 LineNumFromPcContext context(rel_pc, -1);
TDYa127c8dc1012012-04-19 07:03:33 -0700609 DecodeDebugInfo(code_item, method->IsStatic(), method->GetDexMethodIndex(), LineNumForPcCb,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800610 NULL, &context);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700611 return context.line_num_;
612}
613
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700614int32_t DexFile::FindTryItem(const CodeItem &code_item, uint32_t address) {
Ian Rogers0571d352011-11-03 19:51:38 -0700615 // Note: Signed type is important for max and min.
616 int32_t min = 0;
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700617 int32_t max = code_item.tries_size_ - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700618
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700619 while (min <= max) {
620 int32_t mid = min + ((max - min) / 2);
621
622 const art::DexFile::TryItem* ti = GetTryItems(code_item, mid);
623 uint32_t start = ti->start_addr_;
624 uint32_t end = start + ti->insn_count_;
625
Ian Rogers0571d352011-11-03 19:51:38 -0700626 if (address < start) {
627 max = mid - 1;
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700628 } else if (address >= end) {
629 min = mid + 1;
630 } else { // We have a winner!
631 return mid;
Ian Rogers0571d352011-11-03 19:51:38 -0700632 }
633 }
634 // No match.
635 return -1;
636}
637
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700638int32_t DexFile::FindCatchHandlerOffset(const CodeItem &code_item, uint32_t address) {
639 int32_t try_item = FindTryItem(code_item, address);
640 if (try_item == -1) {
641 return -1;
642 } else {
643 return DexFile::GetTryItems(code_item, try_item)->handler_off_;
644 }
645}
646
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800647void DexFile::DecodeDebugInfo0(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes2435a572012-02-17 16:07:41 -0800648 DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb,
649 void* context, const byte* stream, LocalInfo* local_in_reg) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700650 uint32_t line = DecodeUnsignedLeb128(&stream);
651 uint32_t parameters_size = DecodeUnsignedLeb128(&stream);
652 uint16_t arg_reg = code_item->registers_size_ - code_item->ins_size_;
653 uint32_t address = 0;
Elliott Hughes30646832011-10-13 16:59:46 -0700654 bool need_locals = (local_cb != NULL);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700655
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800656 if (!is_static) {
Elliott Hughes30646832011-10-13 16:59:46 -0700657 if (need_locals) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800658 const char* descriptor = GetMethodDeclaringClassDescriptor(GetMethodId(method_idx));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700659 local_in_reg[arg_reg].name_ = "this";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800660 local_in_reg[arg_reg].descriptor_ = descriptor;
Elliott Hughes392b1242011-11-30 13:55:50 -0800661 local_in_reg[arg_reg].signature_ = NULL;
Elliott Hughes30646832011-10-13 16:59:46 -0700662 local_in_reg[arg_reg].start_address_ = 0;
663 local_in_reg[arg_reg].is_live_ = true;
664 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700665 arg_reg++;
666 }
667
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800668 DexFileParameterIterator it(*this, GetMethodPrototype(GetMethodId(method_idx)));
Ian Rogers0571d352011-11-03 19:51:38 -0700669 for (uint32_t i = 0; i < parameters_size && it.HasNext(); ++i, it.Next()) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700670 if (arg_reg >= code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700671 LOG(ERROR) << "invalid stream - arg reg >= reg size (" << arg_reg
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800672 << " >= " << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700673 return;
674 }
Elliott Hughes392b1242011-11-30 13:55:50 -0800675 uint32_t id = DecodeUnsignedLeb128P1(&stream);
Ian Rogers0571d352011-11-03 19:51:38 -0700676 const char* descriptor = it.GetDescriptor();
Elliott Hughes392b1242011-11-30 13:55:50 -0800677 if (need_locals && id != kDexNoIndex) {
Ian Rogers0571d352011-11-03 19:51:38 -0700678 const char* name = StringDataByIdx(id);
Elliott Hughes30646832011-10-13 16:59:46 -0700679 local_in_reg[arg_reg].name_ = name;
680 local_in_reg[arg_reg].descriptor_ = descriptor;
Elliott Hughes392b1242011-11-30 13:55:50 -0800681 local_in_reg[arg_reg].signature_ = NULL;
Elliott Hughes30646832011-10-13 16:59:46 -0700682 local_in_reg[arg_reg].start_address_ = address;
683 local_in_reg[arg_reg].is_live_ = true;
684 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700685 switch (*descriptor) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700686 case 'D':
687 case 'J':
688 arg_reg += 2;
689 break;
690 default:
691 arg_reg += 1;
692 break;
693 }
694 }
695
Ian Rogers0571d352011-11-03 19:51:38 -0700696 if (it.HasNext()) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800697 LOG(ERROR) << "invalid stream - problem with parameter iterator in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700698 return;
699 }
700
701 for (;;) {
702 uint8_t opcode = *stream++;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700703 uint16_t reg;
jeffhaof8728872011-10-28 19:11:13 -0700704 uint16_t name_idx;
705 uint16_t descriptor_idx;
706 uint16_t signature_idx = 0;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700707
Shih-wei Liao195487c2011-08-20 13:29:04 -0700708 switch (opcode) {
709 case DBG_END_SEQUENCE:
710 return;
711
712 case DBG_ADVANCE_PC:
713 address += DecodeUnsignedLeb128(&stream);
714 break;
715
716 case DBG_ADVANCE_LINE:
Shih-wei Liao8a05d272011-10-15 18:45:43 -0700717 line += DecodeSignedLeb128(&stream);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700718 break;
719
720 case DBG_START_LOCAL:
721 case DBG_START_LOCAL_EXTENDED:
722 reg = DecodeUnsignedLeb128(&stream);
723 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700724 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800725 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700726 return;
727 }
728
jeffhaof8728872011-10-28 19:11:13 -0700729 name_idx = DecodeUnsignedLeb128P1(&stream);
730 descriptor_idx = DecodeUnsignedLeb128P1(&stream);
731 if (opcode == DBG_START_LOCAL_EXTENDED) {
732 signature_idx = DecodeUnsignedLeb128P1(&stream);
733 }
734
Shih-wei Liao195487c2011-08-20 13:29:04 -0700735 // Emit what was previously there, if anything
Elliott Hughes30646832011-10-13 16:59:46 -0700736 if (need_locals) {
Elliott Hughes2435a572012-02-17 16:07:41 -0800737 InvokeLocalCbIfLive(context, reg, address, local_in_reg, local_cb);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700738
Ian Rogers0571d352011-11-03 19:51:38 -0700739 local_in_reg[reg].name_ = StringDataByIdx(name_idx);
740 local_in_reg[reg].descriptor_ = StringByTypeIdx(descriptor_idx);
Elliott Hughes30646832011-10-13 16:59:46 -0700741 if (opcode == DBG_START_LOCAL_EXTENDED) {
Ian Rogers0571d352011-11-03 19:51:38 -0700742 local_in_reg[reg].signature_ = StringDataByIdx(signature_idx);
Elliott Hughes30646832011-10-13 16:59:46 -0700743 }
744 local_in_reg[reg].start_address_ = address;
745 local_in_reg[reg].is_live_ = true;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700746 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700747 break;
748
749 case DBG_END_LOCAL:
750 reg = DecodeUnsignedLeb128(&stream);
751 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700752 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800753 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700754 return;
755 }
756
Elliott Hughes30646832011-10-13 16:59:46 -0700757 if (need_locals) {
Elliott Hughes2435a572012-02-17 16:07:41 -0800758 InvokeLocalCbIfLive(context, reg, address, local_in_reg, local_cb);
Elliott Hughes30646832011-10-13 16:59:46 -0700759 local_in_reg[reg].is_live_ = false;
760 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700761 break;
762
763 case DBG_RESTART_LOCAL:
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
Elliott Hughes30646832011-10-13 16:59:46 -0700771 if (need_locals) {
772 if (local_in_reg[reg].name_ == NULL || local_in_reg[reg].descriptor_ == NULL) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800773 LOG(ERROR) << "invalid stream - no name or descriptor in " << GetLocation();
Elliott Hughes30646832011-10-13 16:59:46 -0700774 return;
775 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700776
Elliott Hughes30646832011-10-13 16:59:46 -0700777 // If the register is live, the "restart" is superfluous,
778 // and we don't want to mess with the existing start address.
779 if (!local_in_reg[reg].is_live_) {
780 local_in_reg[reg].start_address_ = address;
781 local_in_reg[reg].is_live_ = true;
782 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700783 }
784 break;
785
786 case DBG_SET_PROLOGUE_END:
787 case DBG_SET_EPILOGUE_BEGIN:
788 case DBG_SET_FILE:
789 break;
790
Shih-wei Liao8e1b4ff2011-10-15 15:43:51 -0700791 default: {
792 int adjopcode = opcode - DBG_FIRST_SPECIAL;
793
Shih-wei Liao195487c2011-08-20 13:29:04 -0700794 address += adjopcode / DBG_LINE_RANGE;
795 line += DBG_LINE_BASE + (adjopcode % DBG_LINE_RANGE);
796
Elliott Hughes2435a572012-02-17 16:07:41 -0800797 if (position_cb != NULL) {
798 if (position_cb(context, address, line)) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700799 // early exit
800 return;
801 }
802 }
803 break;
Shih-wei Liao8e1b4ff2011-10-15 15:43:51 -0700804 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700805 }
806 }
807}
808
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800809void DexFile::DecodeDebugInfo(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes2435a572012-02-17 16:07:41 -0800810 DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb,
811 void* context) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700812 const byte* stream = GetDebugInfoStream(code_item);
Elliott Hughesee0fa762012-03-26 17:12:41 -0700813 UniquePtr<LocalInfo[]> local_in_reg(local_cb != NULL ? new LocalInfo[code_item->registers_size_] : NULL);
Ian Rogers0571d352011-11-03 19:51:38 -0700814 if (stream != NULL) {
Elliott Hughesee0fa762012-03-26 17:12:41 -0700815 DecodeDebugInfo0(code_item, is_static, method_idx, position_cb, local_cb, context, stream, &local_in_reg[0]);
Ian Rogers0571d352011-11-03 19:51:38 -0700816 }
817 for (int reg = 0; reg < code_item->registers_size_; reg++) {
Elliott Hughesee0fa762012-03-26 17:12:41 -0700818 InvokeLocalCbIfLive(context, reg, code_item->insns_size_in_code_units_, &local_in_reg[0], local_cb);
Ian Rogers0571d352011-11-03 19:51:38 -0700819 }
820}
821
Elliott Hughes2435a572012-02-17 16:07:41 -0800822bool DexFile::LineNumForPcCb(void* raw_context, uint32_t address, uint32_t line_num) {
823 LineNumFromPcContext* context = reinterpret_cast<LineNumFromPcContext*>(raw_context);
Ian Rogers0571d352011-11-03 19:51:38 -0700824
825 // We know that this callback will be called in
826 // ascending address order, so keep going until we find
827 // a match or we've just gone past it.
828 if (address > context->address_) {
829 // The line number from the previous positions callback
830 // wil be the final result.
831 return true;
832 } else {
833 context->line_num_ = line_num;
834 return address == context->address_;
835 }
836}
837
838// Decodes the header section from the class data bytes.
839void ClassDataItemIterator::ReadClassDataHeader() {
840 CHECK(ptr_pos_ != NULL);
841 header_.static_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_);
842 header_.instance_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_);
843 header_.direct_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_);
844 header_.virtual_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_);
845}
846
847void ClassDataItemIterator::ReadClassDataField() {
848 field_.field_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_);
849 field_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_);
Brian Carlstrom68adbe42012-05-11 17:18:08 -0700850 if (last_idx_ != 0 && field_.field_idx_delta_ == 0) {
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -0700851 LOG(WARNING) << "Duplicate field " << PrettyField(GetMemberIndex(), dex_file_)
852 << " in " << dex_file_.GetLocation();
853 }
Ian Rogers0571d352011-11-03 19:51:38 -0700854}
855
856void ClassDataItemIterator::ReadClassDataMethod() {
857 method_.method_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_);
858 method_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_);
859 method_.code_off_ = DecodeUnsignedLeb128(&ptr_pos_);
Brian Carlstrom68adbe42012-05-11 17:18:08 -0700860 if (last_idx_ != 0 && method_.method_idx_delta_ == 0) {
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -0700861 LOG(WARNING) << "Duplicate method " << PrettyMethod(GetMemberIndex(), dex_file_)
862 << " in " << dex_file_.GetLocation();
863 }
Ian Rogers0571d352011-11-03 19:51:38 -0700864}
865
866// Read a signed integer. "zwidth" is the zero-based byte count.
867static int32_t ReadSignedInt(const byte* ptr, int zwidth) {
868 int32_t val = 0;
869 for (int i = zwidth; i >= 0; --i) {
870 val = ((uint32_t)val >> 8) | (((int32_t)*ptr++) << 24);
871 }
872 val >>= (3 - zwidth) * 8;
873 return val;
874}
875
876// Read an unsigned integer. "zwidth" is the zero-based byte count,
877// "fill_on_right" indicates which side we want to zero-fill from.
878static uint32_t ReadUnsignedInt(const byte* ptr, int zwidth, bool fill_on_right) {
879 uint32_t val = 0;
880 if (!fill_on_right) {
881 for (int i = zwidth; i >= 0; --i) {
882 val = (val >> 8) | (((uint32_t)*ptr++) << 24);
883 }
884 val >>= (3 - zwidth) * 8;
885 } else {
886 for (int i = zwidth; i >= 0; --i) {
887 val = (val >> 8) | (((uint32_t)*ptr++) << 24);
888 }
889 }
890 return val;
891}
892
893// Read a signed long. "zwidth" is the zero-based byte count.
894static int64_t ReadSignedLong(const byte* ptr, int zwidth) {
895 int64_t val = 0;
896 for (int i = zwidth; i >= 0; --i) {
897 val = ((uint64_t)val >> 8) | (((int64_t)*ptr++) << 56);
898 }
899 val >>= (7 - zwidth) * 8;
900 return val;
901}
902
903// Read an unsigned long. "zwidth" is the zero-based byte count,
904// "fill_on_right" indicates which side we want to zero-fill from.
905static uint64_t ReadUnsignedLong(const byte* ptr, int zwidth, bool fill_on_right) {
906 uint64_t val = 0;
907 if (!fill_on_right) {
908 for (int i = zwidth; i >= 0; --i) {
909 val = (val >> 8) | (((uint64_t)*ptr++) << 56);
910 }
911 val >>= (7 - zwidth) * 8;
912 } else {
913 for (int i = zwidth; i >= 0; --i) {
914 val = (val >> 8) | (((uint64_t)*ptr++) << 56);
915 }
916 }
917 return val;
918}
919
920EncodedStaticFieldValueIterator::EncodedStaticFieldValueIterator(const DexFile& dex_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800921 mirror::DexCache* dex_cache,
922 mirror::ClassLoader* class_loader,
Ian Rogersca190662012-06-26 15:45:57 -0700923 ClassLinker* linker,
924 const DexFile::ClassDef& class_def)
Brian Carlstrom88f36542012-10-16 23:24:21 -0700925 : dex_file_(dex_file), dex_cache_(dex_cache), class_loader_(class_loader), linker_(linker),
926 array_size_(), pos_(-1), type_(kByte) {
Ian Rogers0571d352011-11-03 19:51:38 -0700927 ptr_ = dex_file.GetEncodedStaticFieldValuesArray(class_def);
928 if (ptr_ == NULL) {
929 array_size_ = 0;
930 } else {
931 array_size_ = DecodeUnsignedLeb128(&ptr_);
932 }
933 if (array_size_ > 0) {
934 Next();
935 }
936}
937
938void EncodedStaticFieldValueIterator::Next() {
939 pos_++;
940 if (pos_ >= array_size_) {
941 return;
942 }
943 byte value_type = *ptr_++;
944 byte value_arg = value_type >> kEncodedValueArgShift;
945 size_t width = value_arg + 1; // assume and correct later
Brian Carlstrom88f36542012-10-16 23:24:21 -0700946 type_ = static_cast<ValueType>(value_type & kEncodedValueTypeMask);
Ian Rogers0571d352011-11-03 19:51:38 -0700947 switch (type_) {
948 case kBoolean:
949 jval_.i = (value_arg != 0) ? 1 : 0;
950 width = 0;
951 break;
952 case kByte:
953 jval_.i = ReadSignedInt(ptr_, value_arg);
954 CHECK(IsInt(8, jval_.i));
955 break;
956 case kShort:
957 jval_.i = ReadSignedInt(ptr_, value_arg);
958 CHECK(IsInt(16, jval_.i));
959 break;
960 case kChar:
961 jval_.i = ReadUnsignedInt(ptr_, value_arg, false);
962 CHECK(IsUint(16, jval_.i));
963 break;
964 case kInt:
965 jval_.i = ReadSignedInt(ptr_, value_arg);
966 break;
967 case kLong:
968 jval_.j = ReadSignedLong(ptr_, value_arg);
969 break;
970 case kFloat:
971 jval_.i = ReadUnsignedInt(ptr_, value_arg, true);
972 break;
973 case kDouble:
974 jval_.j = ReadUnsignedLong(ptr_, value_arg, true);
975 break;
976 case kString:
977 case kType:
Ian Rogers0571d352011-11-03 19:51:38 -0700978 jval_.i = ReadUnsignedInt(ptr_, value_arg, false);
979 break;
980 case kField:
Brian Carlstrom88f36542012-10-16 23:24:21 -0700981 case kMethod:
982 case kEnum:
Ian Rogers0571d352011-11-03 19:51:38 -0700983 case kArray:
984 case kAnnotation:
985 UNIMPLEMENTED(FATAL) << ": type " << type_;
986 break;
987 case kNull:
988 jval_.l = NULL;
989 width = 0;
990 break;
991 default:
992 LOG(FATAL) << "Unreached";
993 }
994 ptr_ += width;
995}
996
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800997void EncodedStaticFieldValueIterator::ReadValueToField(mirror::Field* field) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700998 switch (type_) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700999 case kBoolean: field->SetBoolean(field->GetDeclaringClass(), jval_.z); break;
1000 case kByte: field->SetByte(field->GetDeclaringClass(), jval_.b); break;
1001 case kShort: field->SetShort(field->GetDeclaringClass(), jval_.s); break;
1002 case kChar: field->SetChar(field->GetDeclaringClass(), jval_.c); break;
1003 case kInt: field->SetInt(field->GetDeclaringClass(), jval_.i); break;
1004 case kLong: field->SetLong(field->GetDeclaringClass(), jval_.j); break;
1005 case kFloat: field->SetFloat(field->GetDeclaringClass(), jval_.f); break;
1006 case kDouble: field->SetDouble(field->GetDeclaringClass(), jval_.d); break;
1007 case kNull: field->SetObject(field->GetDeclaringClass(), NULL); break;
Ian Rogers0571d352011-11-03 19:51:38 -07001008 case kString: {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001009 mirror::String* resolved = linker_->ResolveString(dex_file_, jval_.i, dex_cache_);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001010 field->SetObject(field->GetDeclaringClass(), resolved);
Ian Rogers0571d352011-11-03 19:51:38 -07001011 break;
1012 }
Brian Carlstrom88f36542012-10-16 23:24:21 -07001013 case kType: {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001014 mirror::Class* resolved = linker_->ResolveType(dex_file_, jval_.i, dex_cache_, class_loader_);
mikaelpeltierebf6aa82012-11-07 14:02:15 +01001015 field->SetObject(field->GetDeclaringClass(), resolved);
Brian Carlstrom88f36542012-10-16 23:24:21 -07001016 break;
1017 }
Ian Rogers0571d352011-11-03 19:51:38 -07001018 default: UNIMPLEMENTED(FATAL) << ": type " << type_;
1019 }
1020}
1021
1022CatchHandlerIterator::CatchHandlerIterator(const DexFile::CodeItem& code_item, uint32_t address) {
1023 handler_.address_ = -1;
1024 int32_t offset = -1;
1025
1026 // Short-circuit the overwhelmingly common cases.
1027 switch (code_item.tries_size_) {
1028 case 0:
1029 break;
1030 case 1: {
1031 const DexFile::TryItem* tries = DexFile::GetTryItems(code_item, 0);
1032 uint32_t start = tries->start_addr_;
1033 if (address >= start) {
1034 uint32_t end = start + tries->insn_count_;
1035 if (address < end) {
1036 offset = tries->handler_off_;
1037 }
1038 }
1039 break;
1040 }
1041 default:
Ian Rogersdbbc99d2013-04-18 16:51:54 -07001042 offset = DexFile::FindCatchHandlerOffset(code_item, address);
Ian Rogers0571d352011-11-03 19:51:38 -07001043 }
Logan Chien736df022012-04-27 16:25:57 +08001044 Init(code_item, offset);
1045}
1046
1047CatchHandlerIterator::CatchHandlerIterator(const DexFile::CodeItem& code_item,
1048 const DexFile::TryItem& try_item) {
1049 handler_.address_ = -1;
1050 Init(code_item, try_item.handler_off_);
1051}
1052
1053void CatchHandlerIterator::Init(const DexFile::CodeItem& code_item,
1054 int32_t offset) {
Ian Rogers0571d352011-11-03 19:51:38 -07001055 if (offset >= 0) {
Logan Chien736df022012-04-27 16:25:57 +08001056 Init(DexFile::GetCatchHandlerData(code_item, offset));
Ian Rogers0571d352011-11-03 19:51:38 -07001057 } else {
1058 // Not found, initialize as empty
1059 current_data_ = NULL;
1060 remaining_count_ = -1;
1061 catch_all_ = false;
1062 DCHECK(!HasNext());
1063 }
1064}
1065
1066void CatchHandlerIterator::Init(const byte* handler_data) {
1067 current_data_ = handler_data;
1068 remaining_count_ = DecodeSignedLeb128(&current_data_);
1069
1070 // If remaining_count_ is non-positive, then it is the negative of
1071 // the number of catch types, and the catches are followed by a
1072 // catch-all handler.
1073 if (remaining_count_ <= 0) {
1074 catch_all_ = true;
1075 remaining_count_ = -remaining_count_;
1076 } else {
1077 catch_all_ = false;
1078 }
1079 Next();
1080}
1081
1082void CatchHandlerIterator::Next() {
1083 if (remaining_count_ > 0) {
1084 handler_.type_idx_ = DecodeUnsignedLeb128(&current_data_);
1085 handler_.address_ = DecodeUnsignedLeb128(&current_data_);
1086 remaining_count_--;
1087 return;
1088 }
1089
1090 if (catch_all_) {
1091 handler_.type_idx_ = DexFile::kDexNoIndex16;
1092 handler_.address_ = DecodeUnsignedLeb128(&current_data_);
1093 catch_all_ = false;
1094 return;
1095 }
1096
1097 // no more handler
1098 remaining_count_ = -1;
1099}
1100
Carl Shapiro1fb86202011-06-27 17:43:13 -07001101} // namespace art