blob: e81c456ccf8d90d7a64b664cb30d4d55fbc99860 [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"
Brian Carlstromea46f952013-07-30 01:26:50 -070034#include "mirror/art_field-inl.h"
35#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "mirror/string.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070037#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070038#include "safe_map.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070039#include "thread.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070040#include "UniquePtr.h"
Ian Rogers0571d352011-11-03 19:51:38 -070041#include "utf.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070042#include "utils.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070043#include "well_known_classes.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070044#include "zip_archive.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070045
46namespace art {
47
Brian Carlstromf615a612011-07-23 12:50:34 -070048const byte DexFile::kDexMagic[] = { 'd', 'e', 'x', '\n' };
49const byte DexFile::kDexMagicVersion[] = { '0', '3', '5', '\0' };
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070050
Ian Rogers8b2c0b92013-09-19 02:56:49 -070051DexFile::ClassPathEntry DexFile::FindInClassPath(const char* descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070052 const ClassPath& class_path) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070053 for (size_t i = 0; i != class_path.size(); ++i) {
54 const DexFile* dex_file = class_path[i];
55 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
56 if (dex_class_def != NULL) {
57 return ClassPathEntry(dex_file, dex_class_def);
58 }
59 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070060 // TODO: remove reinterpret_cast when issue with -std=gnu++0x host issue resolved
Brian Carlstrom7e93b502011-08-04 14:16:22 -070061 return ClassPathEntry(reinterpret_cast<const DexFile*>(NULL),
62 reinterpret_cast<const DexFile::ClassDef*>(NULL));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070063}
64
Brian Carlstrom5b332c82012-02-01 15:02:31 -080065bool DexFile::GetChecksum(const std::string& filename, uint32_t& checksum) {
66 if (IsValidZipFilename(filename)) {
67 UniquePtr<ZipArchive> zip_archive(ZipArchive::Open(filename));
68 if (zip_archive.get() == NULL) {
69 return false;
Brian Carlstrom78128a62011-09-15 17:21:19 -070070 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -080071 UniquePtr<ZipEntry> zip_entry(zip_archive->Find(kClassesDex));
72 if (zip_entry.get() == NULL) {
Ian Rogerse6060102013-05-16 12:01:04 -070073 LOG(ERROR) << "Zip archive '" << filename << "' doesn't contain " << kClassesDex;
Brian Carlstrom5b332c82012-02-01 15:02:31 -080074 return false;
75 }
76 checksum = zip_entry->GetCrc32();
77 return true;
Brian Carlstrom78128a62011-09-15 17:21:19 -070078 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -080079 if (IsValidDexFilename(filename)) {
Brian Carlstrom1db858a2012-03-11 22:44:45 -070080 UniquePtr<const DexFile> dex_file(DexFile::OpenFile(filename, filename, false));
Brian Carlstrom5b332c82012-02-01 15:02:31 -080081 if (dex_file.get() == NULL) {
82 return false;
83 }
84 checksum = dex_file->GetHeader().checksum_;
85 return true;
86 }
Ian Rogerse6060102013-05-16 12:01:04 -070087 LOG(ERROR) << "Expected valid zip or dex file name: " << filename;
Brian Carlstrom5b332c82012-02-01 15:02:31 -080088 return false;
Brian Carlstrom78128a62011-09-15 17:21:19 -070089}
90
Brian Carlstrom16192862011-09-12 17:50:06 -070091const DexFile* DexFile::Open(const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -080092 const std::string& location) {
jeffhao262bf462011-10-20 18:36:32 -070093 if (IsValidZipFilename(filename)) {
Brian Carlstroma004aa92012-02-08 18:05:09 -080094 return DexFile::OpenZip(filename, location);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070095 }
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -070096 if (!IsValidDexFilename(filename)) {
97 LOG(WARNING) << "Attempting to open dex file with unknown extension '" << filename << "'";
98 }
Brian Carlstroma004aa92012-02-08 18:05:09 -080099 return DexFile::OpenFile(filename, location, true);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700100}
101
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800102int DexFile::GetPermissions() const {
103 if (mem_map_.get() == NULL) {
104 return 0;
105 } else {
106 return mem_map_->GetProtect();
107 }
108}
109
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200110bool DexFile::IsReadOnly() const {
111 return GetPermissions() == PROT_READ;
112}
113
Brian Carlstrome0948e12013-08-29 09:36:15 -0700114bool DexFile::EnableWrite() const {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200115 CHECK(IsReadOnly());
116 if (mem_map_.get() == NULL) {
117 return false;
118 } else {
Brian Carlstrome0948e12013-08-29 09:36:15 -0700119 return mem_map_->Protect(PROT_READ | PROT_WRITE);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200120 }
121}
122
Brian Carlstrome0948e12013-08-29 09:36:15 -0700123bool DexFile::DisableWrite() const {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200124 CHECK(!IsReadOnly());
125 if (mem_map_.get() == NULL) {
126 return false;
127 } else {
Brian Carlstrome0948e12013-08-29 09:36:15 -0700128 return mem_map_->Protect(PROT_READ);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200129 }
130}
131
Brian Carlstrom16192862011-09-12 17:50:06 -0700132const DexFile* DexFile::OpenFile(const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -0800133 const std::string& location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800134 bool verify) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800135 CHECK(!location.empty()) << filename;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700136 int fd = open(filename.c_str(), O_RDONLY); // TODO: scoped_fd
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700137 if (fd == -1) {
138 PLOG(ERROR) << "open(\"" << filename << "\", O_RDONLY) failed";
139 return NULL;
140 }
141 struct stat sbuf;
142 memset(&sbuf, 0, sizeof(sbuf));
143 if (fstat(fd, &sbuf) == -1) {
144 PLOG(ERROR) << "fstat \"" << filename << "\" failed";
145 close(fd);
146 return NULL;
147 }
Ian Rogers7cfb93e2012-01-17 19:46:36 -0800148 if (S_ISDIR(sbuf.st_mode)) {
149 LOG(ERROR) << "attempt to mmap directory \"" << filename << "\"";
150 return NULL;
151 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700152 size_t length = sbuf.st_size;
Brian Carlstrom89521892011-12-07 22:05:07 -0800153 UniquePtr<MemMap> map(MemMap::MapFile(length, PROT_READ, MAP_PRIVATE, fd, 0));
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700154 if (map.get() == NULL) {
155 LOG(ERROR) << "mmap \"" << filename << "\" failed";
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700156 close(fd);
157 return NULL;
158 }
159 close(fd);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800160
161 if (map->Size() < sizeof(DexFile::Header)) {
162 LOG(ERROR) << "Failed to open dex file '" << filename << "' that is too short to have a header";
163 return NULL;
164 }
165
166 const Header* dex_header = reinterpret_cast<const Header*>(map->Begin());
167
168 const DexFile* dex_file = OpenMemory(location, dex_header->checksum_, map.release());
jeffhao54c1ceb2012-02-01 11:45:32 -0800169 if (dex_file == NULL) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800170 LOG(ERROR) << "Failed to open dex file '" << filename << "' from memory";
jeffhao54c1ceb2012-02-01 11:45:32 -0800171 return NULL;
jeffhaof6174e82012-01-31 16:14:17 -0800172 }
jeffhao54c1ceb2012-02-01 11:45:32 -0800173
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700174 if (verify && !DexFileVerifier::Verify(dex_file, dex_file->Begin(), dex_file->Size())) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800175 LOG(ERROR) << "Failed to verify dex file '" << filename << "'";
jeffhao54c1ceb2012-02-01 11:45:32 -0800176 return NULL;
177 }
178
jeffhaof6174e82012-01-31 16:14:17 -0800179 return dex_file;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700180}
181
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700182const char* DexFile::kClassesDex = "classes.dex";
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700183
Brian Carlstrom16192862011-09-12 17:50:06 -0700184const DexFile* DexFile::OpenZip(const std::string& filename,
Brian Carlstroma004aa92012-02-08 18:05:09 -0800185 const std::string& location) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700186 UniquePtr<ZipArchive> zip_archive(ZipArchive::Open(filename));
187 if (zip_archive.get() == NULL) {
Brian Carlstrom16192862011-09-12 17:50:06 -0700188 LOG(ERROR) << "Failed to open " << filename << " when looking for classes.dex";
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700189 return NULL;
190 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800191 return DexFile::Open(*zip_archive.get(), location);
192}
193
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800194const DexFile* DexFile::OpenMemory(const std::string& location,
195 uint32_t location_checksum,
196 MemMap* mem_map) {
197 return OpenMemory(mem_map->Begin(),
198 mem_map->Size(),
199 location,
200 location_checksum,
201 mem_map);
202}
203
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800204const DexFile* DexFile::Open(const ZipArchive& zip_archive, const std::string& location) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800205 CHECK(!location.empty());
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800206 UniquePtr<ZipEntry> zip_entry(zip_archive.Find(kClassesDex));
Elliott Hughes90a33692011-08-30 13:27:07 -0700207 if (zip_entry.get() == NULL) {
Brian Carlstrom4922e9d2013-07-09 17:18:47 -0700208 LOG(ERROR) << "Failed to find classes.dex within '" << location << "'";
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700209 return NULL;
210 }
Brian Carlstrom4922e9d2013-07-09 17:18:47 -0700211 UniquePtr<MemMap> map(zip_entry->ExtractToMemMap(kClassesDex));
Brian Carlstrom89521892011-12-07 22:05:07 -0800212 if (map.get() == NULL) {
Brian Carlstrom4922e9d2013-07-09 17:18:47 -0700213 LOG(ERROR) << "Failed to extract '" << kClassesDex << "' from '" << location << "'";
Brian Carlstrom89521892011-12-07 22:05:07 -0800214 return NULL;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700215 }
Brian Carlstrome0948e12013-08-29 09:36:15 -0700216 UniquePtr<const DexFile> dex_file(OpenMemory(location, zip_entry->GetCrc32(), map.release()));
217 if (dex_file.get() == NULL) {
jeffhao54c1ceb2012-02-01 11:45:32 -0800218 LOG(ERROR) << "Failed to open dex file '" << location << "' from memory";
219 return NULL;
jeffhaof6174e82012-01-31 16:14:17 -0800220 }
Brian Carlstrome0948e12013-08-29 09:36:15 -0700221 if (!DexFileVerifier::Verify(dex_file.get(), dex_file->Begin(), dex_file->Size())) {
jeffhao54c1ceb2012-02-01 11:45:32 -0800222 LOG(ERROR) << "Failed to verify dex file '" << location << "'";
223 return NULL;
224 }
Brian Carlstrome0948e12013-08-29 09:36:15 -0700225 if (!dex_file->DisableWrite()) {
226 LOG(ERROR) << "Failed to make dex file read only '" << location << "'";
227 return NULL;
228 }
229 CHECK(dex_file->IsReadOnly()) << location;
230 return dex_file.release();
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700231}
232
Brian Carlstrom89521892011-12-07 22:05:07 -0800233const DexFile* DexFile::OpenMemory(const byte* base,
jeffhaof6174e82012-01-31 16:14:17 -0800234 size_t size,
Brian Carlstrom89521892011-12-07 22:05:07 -0800235 const std::string& location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800236 uint32_t location_checksum,
Brian Carlstrom89521892011-12-07 22:05:07 -0800237 MemMap* mem_map) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700238 CHECK_ALIGNED(base, 4); // various dex file structures must be word aligned
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800239 UniquePtr<DexFile> dex_file(new DexFile(base, size, location, location_checksum, mem_map));
Brian Carlstromf615a612011-07-23 12:50:34 -0700240 if (!dex_file->Init()) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700241 return NULL;
242 } else {
Brian Carlstromf615a612011-07-23 12:50:34 -0700243 return dex_file.release();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700244 }
245}
246
Jesse Wilson6bf19152011-09-29 13:12:33 -0400247DexFile::~DexFile() {
Elliott Hughes8cef0b82011-10-11 19:24:00 -0700248 // We don't call DeleteGlobalRef on dex_object_ because we're only called by DestroyJavaVM, and
249 // that's only called after DetachCurrentThread, which means there's no JNIEnv. We could
250 // re-attach, but cleaning up these global references is not obviously useful. It's not as if
251 // the global reference table is otherwise empty!
Jesse Wilson6bf19152011-09-29 13:12:33 -0400252}
253
Brian Carlstromf615a612011-07-23 12:50:34 -0700254bool DexFile::Init() {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700255 InitMembers();
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800256 if (!CheckMagicAndVersion()) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700257 return false;
258 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700259 return true;
260}
261
Brian Carlstromf615a612011-07-23 12:50:34 -0700262void DexFile::InitMembers() {
Ian Rogers30fab402012-01-23 15:43:46 -0800263 const byte* b = begin_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700264 header_ = reinterpret_cast<const Header*>(b);
265 const Header* h = header_;
266 string_ids_ = reinterpret_cast<const StringId*>(b + h->string_ids_off_);
267 type_ids_ = reinterpret_cast<const TypeId*>(b + h->type_ids_off_);
268 field_ids_ = reinterpret_cast<const FieldId*>(b + h->field_ids_off_);
269 method_ids_ = reinterpret_cast<const MethodId*>(b + h->method_ids_off_);
270 proto_ids_ = reinterpret_cast<const ProtoId*>(b + h->proto_ids_off_);
271 class_defs_ = reinterpret_cast<const ClassDef*>(b + h->class_defs_off_);
272}
273
jeffhao10037c82012-01-23 15:06:23 -0800274bool DexFile::CheckMagicAndVersion() const {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800275 CHECK(header_->magic_ != NULL) << GetLocation();
276 if (!IsMagicValid(header_->magic_)) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800277 LOG(ERROR) << "Unrecognized magic number in " << GetLocation() << ":"
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800278 << " " << header_->magic_[0]
279 << " " << header_->magic_[1]
280 << " " << header_->magic_[2]
281 << " " << header_->magic_[3];
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700282 return false;
283 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800284 if (!IsVersionValid(header_->magic_)) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800285 LOG(ERROR) << "Unrecognized version number in " << GetLocation() << ":"
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800286 << " " << header_->magic_[4]
287 << " " << header_->magic_[5]
288 << " " << header_->magic_[6]
289 << " " << header_->magic_[7];
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700290 return false;
291 }
292 return true;
293}
294
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800295bool DexFile::IsMagicValid(const byte* magic) {
296 return (memcmp(magic, kDexMagic, sizeof(kDexMagic)) == 0);
297}
298
299bool DexFile::IsVersionValid(const byte* magic) {
300 const byte* version = &magic[sizeof(kDexMagic)];
301 return (memcmp(version, kDexMagicVersion, sizeof(kDexMagicVersion)) == 0);
302}
303
Ian Rogersd81871c2011-10-03 13:57:23 -0700304uint32_t DexFile::GetVersion() const {
305 const char* version = reinterpret_cast<const char*>(&GetHeader().magic_[sizeof(kDexMagic)]);
306 return atoi(version);
307}
308
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700309const DexFile::ClassDef* DexFile::FindClassDef(const char* descriptor) const {
310 size_t num_class_defs = NumClassDefs();
311 if (num_class_defs == 0) {
312 return NULL;
313 }
314 const StringId* string_id = FindStringId(descriptor);
315 if (string_id == NULL) {
316 return NULL;
317 }
318 const TypeId* type_id = FindTypeId(GetIndexForStringId(*string_id));
319 if (type_id == NULL) {
320 return NULL;
321 }
322 uint16_t type_idx = GetIndexForTypeId(*type_id);
323 for (size_t i = 0; i < num_class_defs; ++i) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700324 const ClassDef& class_def = GetClassDef(i);
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700325 if (class_def.class_idx_ == type_idx) {
326 return &class_def;
327 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700328 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700329 return NULL;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700330}
331
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700332const DexFile::ClassDef* DexFile::FindClassDef(uint16_t type_idx) const {
333 size_t num_class_defs = NumClassDefs();
334 for (size_t i = 0; i < num_class_defs; ++i) {
335 const ClassDef& class_def = GetClassDef(i);
336 if (class_def.class_idx_ == type_idx) {
337 return &class_def;
338 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700339 }
340 return NULL;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700341}
342
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800343const DexFile::FieldId* DexFile::FindFieldId(const DexFile::TypeId& declaring_klass,
344 const DexFile::StringId& name,
345 const DexFile::TypeId& type) const {
346 // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx
347 const uint16_t class_idx = GetIndexForTypeId(declaring_klass);
348 const uint32_t name_idx = GetIndexForStringId(name);
349 const uint16_t type_idx = GetIndexForTypeId(type);
Ian Rogersf8582c32013-05-29 16:33:03 -0700350 int32_t lo = 0;
351 int32_t hi = NumFieldIds() - 1;
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800352 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700353 int32_t mid = (hi + lo) / 2;
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800354 const DexFile::FieldId& field = GetFieldId(mid);
355 if (class_idx > field.class_idx_) {
356 lo = mid + 1;
357 } else if (class_idx < field.class_idx_) {
358 hi = mid - 1;
359 } else {
360 if (name_idx > field.name_idx_) {
361 lo = mid + 1;
362 } else if (name_idx < field.name_idx_) {
363 hi = mid - 1;
364 } else {
365 if (type_idx > field.type_idx_) {
366 lo = mid + 1;
367 } else if (type_idx < field.type_idx_) {
368 hi = mid - 1;
369 } else {
370 return &field;
371 }
372 }
373 }
374 }
375 return NULL;
376}
377
378const DexFile::MethodId* DexFile::FindMethodId(const DexFile::TypeId& declaring_klass,
Ian Rogers0571d352011-11-03 19:51:38 -0700379 const DexFile::StringId& name,
380 const DexFile::ProtoId& signature) const {
381 // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800382 const uint16_t class_idx = GetIndexForTypeId(declaring_klass);
Ian Rogers0571d352011-11-03 19:51:38 -0700383 const uint32_t name_idx = GetIndexForStringId(name);
384 const uint16_t proto_idx = GetIndexForProtoId(signature);
Ian Rogersf8582c32013-05-29 16:33:03 -0700385 int32_t lo = 0;
386 int32_t hi = NumMethodIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700387 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700388 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700389 const DexFile::MethodId& method = GetMethodId(mid);
390 if (class_idx > method.class_idx_) {
391 lo = mid + 1;
392 } else if (class_idx < method.class_idx_) {
393 hi = mid - 1;
394 } else {
395 if (name_idx > method.name_idx_) {
396 lo = mid + 1;
397 } else if (name_idx < method.name_idx_) {
398 hi = mid - 1;
399 } else {
400 if (proto_idx > method.proto_idx_) {
401 lo = mid + 1;
402 } else if (proto_idx < method.proto_idx_) {
403 hi = mid - 1;
404 } else {
405 return &method;
406 }
407 }
408 }
409 }
410 return NULL;
411}
412
Ian Rogers637c65b2013-05-31 11:46:00 -0700413const DexFile::StringId* DexFile::FindStringId(const char* string) const {
Ian Rogersf8582c32013-05-29 16:33:03 -0700414 int32_t lo = 0;
415 int32_t hi = NumStringIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700416 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700417 int32_t mid = (hi + lo) / 2;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800418 uint32_t length;
Ian Rogers0571d352011-11-03 19:51:38 -0700419 const DexFile::StringId& str_id = GetStringId(mid);
420 const char* str = GetStringDataAndLength(str_id, &length);
Ian Rogers637c65b2013-05-31 11:46:00 -0700421 int compare = CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(string, str);
422 if (compare > 0) {
423 lo = mid + 1;
424 } else if (compare < 0) {
425 hi = mid - 1;
426 } else {
427 return &str_id;
428 }
429 }
430 return NULL;
431}
432
433const DexFile::StringId* DexFile::FindStringId(const uint16_t* string) const {
434 int32_t lo = 0;
435 int32_t hi = NumStringIds() - 1;
436 while (hi >= lo) {
437 int32_t mid = (hi + lo) / 2;
438 uint32_t length;
439 const DexFile::StringId& str_id = GetStringId(mid);
440 const char* str = GetStringDataAndLength(str_id, &length);
441 int compare = CompareModifiedUtf8ToUtf16AsCodePointValues(str, string);
Ian Rogers0571d352011-11-03 19:51:38 -0700442 if (compare > 0) {
443 lo = mid + 1;
444 } else if (compare < 0) {
445 hi = mid - 1;
446 } else {
447 return &str_id;
448 }
449 }
450 return NULL;
451}
452
453const DexFile::TypeId* DexFile::FindTypeId(uint32_t string_idx) const {
Ian Rogersf8582c32013-05-29 16:33:03 -0700454 int32_t lo = 0;
455 int32_t hi = NumTypeIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700456 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700457 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700458 const TypeId& type_id = GetTypeId(mid);
459 if (string_idx > type_id.descriptor_idx_) {
460 lo = mid + 1;
461 } else if (string_idx < type_id.descriptor_idx_) {
462 hi = mid - 1;
463 } else {
464 return &type_id;
465 }
466 }
467 return NULL;
468}
469
470const DexFile::ProtoId* DexFile::FindProtoId(uint16_t return_type_idx,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800471 const std::vector<uint16_t>& signature_type_idxs) const {
Ian Rogersf8582c32013-05-29 16:33:03 -0700472 int32_t lo = 0;
473 int32_t hi = NumProtoIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700474 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700475 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700476 const DexFile::ProtoId& proto = GetProtoId(mid);
477 int compare = return_type_idx - proto.return_type_idx_;
478 if (compare == 0) {
479 DexFileParameterIterator it(*this, proto);
480 size_t i = 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800481 while (it.HasNext() && i < signature_type_idxs.size() && compare == 0) {
482 compare = signature_type_idxs[i] - it.GetTypeIdx();
Ian Rogers0571d352011-11-03 19:51:38 -0700483 it.Next();
484 i++;
485 }
486 if (compare == 0) {
487 if (it.HasNext()) {
488 compare = -1;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800489 } else if (i < signature_type_idxs.size()) {
Ian Rogers0571d352011-11-03 19:51:38 -0700490 compare = 1;
491 }
492 }
493 }
494 if (compare > 0) {
495 lo = mid + 1;
496 } else if (compare < 0) {
497 hi = mid - 1;
498 } else {
499 return &proto;
500 }
501 }
502 return NULL;
503}
504
505// Given a signature place the type ids into the given vector
506bool DexFile::CreateTypeList(uint16_t* return_type_idx, std::vector<uint16_t>* param_type_idxs,
507 const std::string& signature) const {
508 if (signature[0] != '(') {
509 return false;
510 }
511 size_t offset = 1;
512 size_t end = signature.size();
513 bool process_return = false;
514 while (offset < end) {
515 char c = signature[offset];
516 offset++;
517 if (c == ')') {
518 process_return = true;
519 continue;
520 }
521 std::string descriptor;
522 descriptor += c;
523 while (c == '[') { // process array prefix
524 if (offset >= end) { // expect some descriptor following [
525 return false;
526 }
527 c = signature[offset];
528 offset++;
529 descriptor += c;
530 }
531 if (c == 'L') { // process type descriptors
532 do {
533 if (offset >= end) { // unexpected early termination of descriptor
534 return false;
535 }
536 c = signature[offset];
537 offset++;
538 descriptor += c;
539 } while (c != ';');
540 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700541 const DexFile::StringId* string_id = FindStringId(descriptor.c_str());
Ian Rogers0571d352011-11-03 19:51:38 -0700542 if (string_id == NULL) {
543 return false;
544 }
545 const DexFile::TypeId* type_id = FindTypeId(GetIndexForStringId(*string_id));
546 if (type_id == NULL) {
547 return false;
548 }
549 uint16_t type_idx = GetIndexForTypeId(*type_id);
550 if (!process_return) {
551 param_type_idxs->push_back(type_idx);
552 } else {
553 *return_type_idx = type_idx;
554 return offset == end; // return true if the signature had reached a sensible end
555 }
556 }
557 return false; // failed to correctly parse return type
558}
559
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700560// Materializes the method descriptor for a method prototype. Method
561// descriptors are not stored directly in the dex file. Instead, one
562// must assemble the descriptor from references in the prototype.
Ian Rogers0571d352011-11-03 19:51:38 -0700563std::string DexFile::CreateMethodSignature(uint32_t proto_idx, int32_t* unicode_length) const {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700564 const ProtoId& proto_id = GetProtoId(proto_idx);
565 std::string descriptor;
566 descriptor.push_back('(');
567 const TypeList* type_list = GetProtoParameters(proto_id);
568 size_t parameter_length = 0;
569 if (type_list != NULL) {
570 // A non-zero number of arguments. Append the type names.
571 for (size_t i = 0; i < type_list->Size(); ++i) {
572 const TypeItem& type_item = type_list->GetTypeItem(i);
573 uint32_t type_idx = type_item.type_idx_;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800574 uint32_t type_length;
Ian Rogers0571d352011-11-03 19:51:38 -0700575 const char* name = StringByTypeIdx(type_idx, &type_length);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700576 parameter_length += type_length;
577 descriptor.append(name);
578 }
579 }
580 descriptor.push_back(')');
581 uint32_t return_type_idx = proto_id.return_type_idx_;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800582 uint32_t return_type_length;
Ian Rogers0571d352011-11-03 19:51:38 -0700583 const char* name = StringByTypeIdx(return_type_idx, &return_type_length);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700584 descriptor.append(name);
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700585 if (unicode_length != NULL) {
586 *unicode_length = parameter_length + return_type_length + 2; // 2 for ( and )
587 }
Elliott Hughes0c424cb2011-08-26 10:16:25 -0700588 return descriptor;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700589}
590
Brian Carlstromea46f952013-07-30 01:26:50 -0700591int32_t DexFile::GetLineNumFromPC(const mirror::ArtMethod* method, uint32_t rel_pc) const {
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -0700592 // For native method, lineno should be -2 to indicate it is native. Note that
593 // "line number == -2" is how libcore tells from StackTraceElement.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700594 if (method->GetCodeItemOffset() == 0) {
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -0700595 return -2;
596 }
597
TDYa127c8dc1012012-04-19 07:03:33 -0700598 const CodeItem* code_item = GetCodeItem(method->GetCodeItemOffset());
Elliott Hughescaf76542012-06-28 16:08:22 -0700599 DCHECK(code_item != NULL) << PrettyMethod(method) << " " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700600
601 // A method with no line number info should return -1
602 LineNumFromPcContext context(rel_pc, -1);
TDYa127c8dc1012012-04-19 07:03:33 -0700603 DecodeDebugInfo(code_item, method->IsStatic(), method->GetDexMethodIndex(), LineNumForPcCb,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800604 NULL, &context);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700605 return context.line_num_;
606}
607
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700608int32_t DexFile::FindTryItem(const CodeItem &code_item, uint32_t address) {
Ian Rogers0571d352011-11-03 19:51:38 -0700609 // Note: Signed type is important for max and min.
610 int32_t min = 0;
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700611 int32_t max = code_item.tries_size_ - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700612
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700613 while (min <= max) {
614 int32_t mid = min + ((max - min) / 2);
615
616 const art::DexFile::TryItem* ti = GetTryItems(code_item, mid);
617 uint32_t start = ti->start_addr_;
618 uint32_t end = start + ti->insn_count_;
619
Ian Rogers0571d352011-11-03 19:51:38 -0700620 if (address < start) {
621 max = mid - 1;
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700622 } else if (address >= end) {
623 min = mid + 1;
624 } else { // We have a winner!
625 return mid;
Ian Rogers0571d352011-11-03 19:51:38 -0700626 }
627 }
628 // No match.
629 return -1;
630}
631
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700632int32_t DexFile::FindCatchHandlerOffset(const CodeItem &code_item, uint32_t address) {
633 int32_t try_item = FindTryItem(code_item, address);
634 if (try_item == -1) {
635 return -1;
636 } else {
637 return DexFile::GetTryItems(code_item, try_item)->handler_off_;
638 }
639}
640
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800641void DexFile::DecodeDebugInfo0(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes2435a572012-02-17 16:07:41 -0800642 DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb,
643 void* context, const byte* stream, LocalInfo* local_in_reg) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700644 uint32_t line = DecodeUnsignedLeb128(&stream);
645 uint32_t parameters_size = DecodeUnsignedLeb128(&stream);
646 uint16_t arg_reg = code_item->registers_size_ - code_item->ins_size_;
647 uint32_t address = 0;
Elliott Hughes30646832011-10-13 16:59:46 -0700648 bool need_locals = (local_cb != NULL);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700649
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800650 if (!is_static) {
Elliott Hughes30646832011-10-13 16:59:46 -0700651 if (need_locals) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800652 const char* descriptor = GetMethodDeclaringClassDescriptor(GetMethodId(method_idx));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700653 local_in_reg[arg_reg].name_ = "this";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800654 local_in_reg[arg_reg].descriptor_ = descriptor;
Elliott Hughes392b1242011-11-30 13:55:50 -0800655 local_in_reg[arg_reg].signature_ = NULL;
Elliott Hughes30646832011-10-13 16:59:46 -0700656 local_in_reg[arg_reg].start_address_ = 0;
657 local_in_reg[arg_reg].is_live_ = true;
658 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700659 arg_reg++;
660 }
661
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800662 DexFileParameterIterator it(*this, GetMethodPrototype(GetMethodId(method_idx)));
Ian Rogers0571d352011-11-03 19:51:38 -0700663 for (uint32_t i = 0; i < parameters_size && it.HasNext(); ++i, it.Next()) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700664 if (arg_reg >= code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700665 LOG(ERROR) << "invalid stream - arg reg >= reg size (" << arg_reg
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800666 << " >= " << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700667 return;
668 }
Elliott Hughes392b1242011-11-30 13:55:50 -0800669 uint32_t id = DecodeUnsignedLeb128P1(&stream);
Ian Rogers0571d352011-11-03 19:51:38 -0700670 const char* descriptor = it.GetDescriptor();
Elliott Hughes392b1242011-11-30 13:55:50 -0800671 if (need_locals && id != kDexNoIndex) {
Ian Rogers0571d352011-11-03 19:51:38 -0700672 const char* name = StringDataByIdx(id);
Elliott Hughes30646832011-10-13 16:59:46 -0700673 local_in_reg[arg_reg].name_ = name;
674 local_in_reg[arg_reg].descriptor_ = descriptor;
Elliott Hughes392b1242011-11-30 13:55:50 -0800675 local_in_reg[arg_reg].signature_ = NULL;
Elliott Hughes30646832011-10-13 16:59:46 -0700676 local_in_reg[arg_reg].start_address_ = address;
677 local_in_reg[arg_reg].is_live_ = true;
678 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700679 switch (*descriptor) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700680 case 'D':
681 case 'J':
682 arg_reg += 2;
683 break;
684 default:
685 arg_reg += 1;
686 break;
687 }
688 }
689
Ian Rogers0571d352011-11-03 19:51:38 -0700690 if (it.HasNext()) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800691 LOG(ERROR) << "invalid stream - problem with parameter iterator in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700692 return;
693 }
694
695 for (;;) {
696 uint8_t opcode = *stream++;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700697 uint16_t reg;
jeffhaof8728872011-10-28 19:11:13 -0700698 uint16_t name_idx;
699 uint16_t descriptor_idx;
700 uint16_t signature_idx = 0;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700701
Shih-wei Liao195487c2011-08-20 13:29:04 -0700702 switch (opcode) {
703 case DBG_END_SEQUENCE:
704 return;
705
706 case DBG_ADVANCE_PC:
707 address += DecodeUnsignedLeb128(&stream);
708 break;
709
710 case DBG_ADVANCE_LINE:
Shih-wei Liao8a05d272011-10-15 18:45:43 -0700711 line += DecodeSignedLeb128(&stream);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700712 break;
713
714 case DBG_START_LOCAL:
715 case DBG_START_LOCAL_EXTENDED:
716 reg = DecodeUnsignedLeb128(&stream);
717 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700718 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800719 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700720 return;
721 }
722
jeffhaof8728872011-10-28 19:11:13 -0700723 name_idx = DecodeUnsignedLeb128P1(&stream);
724 descriptor_idx = DecodeUnsignedLeb128P1(&stream);
725 if (opcode == DBG_START_LOCAL_EXTENDED) {
726 signature_idx = DecodeUnsignedLeb128P1(&stream);
727 }
728
Shih-wei Liao195487c2011-08-20 13:29:04 -0700729 // Emit what was previously there, if anything
Elliott Hughes30646832011-10-13 16:59:46 -0700730 if (need_locals) {
Elliott Hughes2435a572012-02-17 16:07:41 -0800731 InvokeLocalCbIfLive(context, reg, address, local_in_reg, local_cb);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700732
Ian Rogers0571d352011-11-03 19:51:38 -0700733 local_in_reg[reg].name_ = StringDataByIdx(name_idx);
734 local_in_reg[reg].descriptor_ = StringByTypeIdx(descriptor_idx);
Elliott Hughes30646832011-10-13 16:59:46 -0700735 if (opcode == DBG_START_LOCAL_EXTENDED) {
Ian Rogers0571d352011-11-03 19:51:38 -0700736 local_in_reg[reg].signature_ = StringDataByIdx(signature_idx);
Elliott Hughes30646832011-10-13 16:59:46 -0700737 }
738 local_in_reg[reg].start_address_ = address;
739 local_in_reg[reg].is_live_ = true;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700740 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700741 break;
742
743 case DBG_END_LOCAL:
744 reg = DecodeUnsignedLeb128(&stream);
745 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700746 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800747 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700748 return;
749 }
750
Elliott Hughes30646832011-10-13 16:59:46 -0700751 if (need_locals) {
Elliott Hughes2435a572012-02-17 16:07:41 -0800752 InvokeLocalCbIfLive(context, reg, address, local_in_reg, local_cb);
Elliott Hughes30646832011-10-13 16:59:46 -0700753 local_in_reg[reg].is_live_ = false;
754 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700755 break;
756
757 case DBG_RESTART_LOCAL:
758 reg = DecodeUnsignedLeb128(&stream);
759 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700760 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800761 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700762 return;
763 }
764
Elliott Hughes30646832011-10-13 16:59:46 -0700765 if (need_locals) {
766 if (local_in_reg[reg].name_ == NULL || local_in_reg[reg].descriptor_ == NULL) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800767 LOG(ERROR) << "invalid stream - no name or descriptor in " << GetLocation();
Elliott Hughes30646832011-10-13 16:59:46 -0700768 return;
769 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700770
Elliott Hughes30646832011-10-13 16:59:46 -0700771 // If the register is live, the "restart" is superfluous,
772 // and we don't want to mess with the existing start address.
773 if (!local_in_reg[reg].is_live_) {
774 local_in_reg[reg].start_address_ = address;
775 local_in_reg[reg].is_live_ = true;
776 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700777 }
778 break;
779
780 case DBG_SET_PROLOGUE_END:
781 case DBG_SET_EPILOGUE_BEGIN:
782 case DBG_SET_FILE:
783 break;
784
Shih-wei Liao8e1b4ff2011-10-15 15:43:51 -0700785 default: {
786 int adjopcode = opcode - DBG_FIRST_SPECIAL;
787
Shih-wei Liao195487c2011-08-20 13:29:04 -0700788 address += adjopcode / DBG_LINE_RANGE;
789 line += DBG_LINE_BASE + (adjopcode % DBG_LINE_RANGE);
790
Elliott Hughes2435a572012-02-17 16:07:41 -0800791 if (position_cb != NULL) {
792 if (position_cb(context, address, line)) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700793 // early exit
794 return;
795 }
796 }
797 break;
Shih-wei Liao8e1b4ff2011-10-15 15:43:51 -0700798 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700799 }
800 }
801}
802
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800803void DexFile::DecodeDebugInfo(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes2435a572012-02-17 16:07:41 -0800804 DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb,
805 void* context) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700806 const byte* stream = GetDebugInfoStream(code_item);
Brian Carlstrome0948e12013-08-29 09:36:15 -0700807 UniquePtr<LocalInfo[]> local_in_reg(local_cb != NULL ?
808 new LocalInfo[code_item->registers_size_] :
809 NULL);
Ian Rogers0571d352011-11-03 19:51:38 -0700810 if (stream != NULL) {
Elliott Hughesee0fa762012-03-26 17:12:41 -0700811 DecodeDebugInfo0(code_item, is_static, method_idx, position_cb, local_cb, context, stream, &local_in_reg[0]);
Ian Rogers0571d352011-11-03 19:51:38 -0700812 }
813 for (int reg = 0; reg < code_item->registers_size_; reg++) {
Elliott Hughesee0fa762012-03-26 17:12:41 -0700814 InvokeLocalCbIfLive(context, reg, code_item->insns_size_in_code_units_, &local_in_reg[0], local_cb);
Ian Rogers0571d352011-11-03 19:51:38 -0700815 }
816}
817
Elliott Hughes2435a572012-02-17 16:07:41 -0800818bool DexFile::LineNumForPcCb(void* raw_context, uint32_t address, uint32_t line_num) {
819 LineNumFromPcContext* context = reinterpret_cast<LineNumFromPcContext*>(raw_context);
Ian Rogers0571d352011-11-03 19:51:38 -0700820
821 // We know that this callback will be called in
822 // ascending address order, so keep going until we find
823 // a match or we've just gone past it.
824 if (address > context->address_) {
825 // The line number from the previous positions callback
826 // wil be the final result.
827 return true;
828 } else {
829 context->line_num_ = line_num;
830 return address == context->address_;
831 }
832}
833
834// Decodes the header section from the class data bytes.
835void ClassDataItemIterator::ReadClassDataHeader() {
836 CHECK(ptr_pos_ != NULL);
837 header_.static_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_);
838 header_.instance_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_);
839 header_.direct_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_);
840 header_.virtual_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_);
841}
842
843void ClassDataItemIterator::ReadClassDataField() {
844 field_.field_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_);
845 field_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_);
Brian Carlstrom68adbe42012-05-11 17:18:08 -0700846 if (last_idx_ != 0 && field_.field_idx_delta_ == 0) {
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -0700847 LOG(WARNING) << "Duplicate field " << PrettyField(GetMemberIndex(), dex_file_)
848 << " in " << dex_file_.GetLocation();
849 }
Ian Rogers0571d352011-11-03 19:51:38 -0700850}
851
852void ClassDataItemIterator::ReadClassDataMethod() {
853 method_.method_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_);
854 method_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_);
855 method_.code_off_ = DecodeUnsignedLeb128(&ptr_pos_);
Brian Carlstrom68adbe42012-05-11 17:18:08 -0700856 if (last_idx_ != 0 && method_.method_idx_delta_ == 0) {
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -0700857 LOG(WARNING) << "Duplicate method " << PrettyMethod(GetMemberIndex(), dex_file_)
858 << " in " << dex_file_.GetLocation();
859 }
Ian Rogers0571d352011-11-03 19:51:38 -0700860}
861
862// Read a signed integer. "zwidth" is the zero-based byte count.
863static int32_t ReadSignedInt(const byte* ptr, int zwidth) {
864 int32_t val = 0;
865 for (int i = zwidth; i >= 0; --i) {
866 val = ((uint32_t)val >> 8) | (((int32_t)*ptr++) << 24);
867 }
868 val >>= (3 - zwidth) * 8;
869 return val;
870}
871
872// Read an unsigned integer. "zwidth" is the zero-based byte count,
873// "fill_on_right" indicates which side we want to zero-fill from.
874static uint32_t ReadUnsignedInt(const byte* ptr, int zwidth, bool fill_on_right) {
875 uint32_t val = 0;
876 if (!fill_on_right) {
877 for (int i = zwidth; i >= 0; --i) {
878 val = (val >> 8) | (((uint32_t)*ptr++) << 24);
879 }
880 val >>= (3 - zwidth) * 8;
881 } else {
882 for (int i = zwidth; i >= 0; --i) {
883 val = (val >> 8) | (((uint32_t)*ptr++) << 24);
884 }
885 }
886 return val;
887}
888
889// Read a signed long. "zwidth" is the zero-based byte count.
890static int64_t ReadSignedLong(const byte* ptr, int zwidth) {
891 int64_t val = 0;
892 for (int i = zwidth; i >= 0; --i) {
893 val = ((uint64_t)val >> 8) | (((int64_t)*ptr++) << 56);
894 }
895 val >>= (7 - zwidth) * 8;
896 return val;
897}
898
899// Read an unsigned long. "zwidth" is the zero-based byte count,
900// "fill_on_right" indicates which side we want to zero-fill from.
901static uint64_t ReadUnsignedLong(const byte* ptr, int zwidth, bool fill_on_right) {
902 uint64_t val = 0;
903 if (!fill_on_right) {
904 for (int i = zwidth; i >= 0; --i) {
905 val = (val >> 8) | (((uint64_t)*ptr++) << 56);
906 }
907 val >>= (7 - zwidth) * 8;
908 } else {
909 for (int i = zwidth; i >= 0; --i) {
910 val = (val >> 8) | (((uint64_t)*ptr++) << 56);
911 }
912 }
913 return val;
914}
915
916EncodedStaticFieldValueIterator::EncodedStaticFieldValueIterator(const DexFile& dex_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800917 mirror::DexCache* dex_cache,
918 mirror::ClassLoader* class_loader,
Ian Rogersca190662012-06-26 15:45:57 -0700919 ClassLinker* linker,
920 const DexFile::ClassDef& class_def)
Brian Carlstrom88f36542012-10-16 23:24:21 -0700921 : dex_file_(dex_file), dex_cache_(dex_cache), class_loader_(class_loader), linker_(linker),
922 array_size_(), pos_(-1), type_(kByte) {
Ian Rogers0571d352011-11-03 19:51:38 -0700923 ptr_ = dex_file.GetEncodedStaticFieldValuesArray(class_def);
924 if (ptr_ == NULL) {
925 array_size_ = 0;
926 } else {
927 array_size_ = DecodeUnsignedLeb128(&ptr_);
928 }
929 if (array_size_ > 0) {
930 Next();
931 }
932}
933
934void EncodedStaticFieldValueIterator::Next() {
935 pos_++;
936 if (pos_ >= array_size_) {
937 return;
938 }
939 byte value_type = *ptr_++;
940 byte value_arg = value_type >> kEncodedValueArgShift;
941 size_t width = value_arg + 1; // assume and correct later
Brian Carlstrom88f36542012-10-16 23:24:21 -0700942 type_ = static_cast<ValueType>(value_type & kEncodedValueTypeMask);
Ian Rogers0571d352011-11-03 19:51:38 -0700943 switch (type_) {
944 case kBoolean:
945 jval_.i = (value_arg != 0) ? 1 : 0;
946 width = 0;
947 break;
948 case kByte:
949 jval_.i = ReadSignedInt(ptr_, value_arg);
950 CHECK(IsInt(8, jval_.i));
951 break;
952 case kShort:
953 jval_.i = ReadSignedInt(ptr_, value_arg);
954 CHECK(IsInt(16, jval_.i));
955 break;
956 case kChar:
957 jval_.i = ReadUnsignedInt(ptr_, value_arg, false);
958 CHECK(IsUint(16, jval_.i));
959 break;
960 case kInt:
961 jval_.i = ReadSignedInt(ptr_, value_arg);
962 break;
963 case kLong:
964 jval_.j = ReadSignedLong(ptr_, value_arg);
965 break;
966 case kFloat:
967 jval_.i = ReadUnsignedInt(ptr_, value_arg, true);
968 break;
969 case kDouble:
970 jval_.j = ReadUnsignedLong(ptr_, value_arg, true);
971 break;
972 case kString:
973 case kType:
Ian Rogers0571d352011-11-03 19:51:38 -0700974 jval_.i = ReadUnsignedInt(ptr_, value_arg, false);
975 break;
976 case kField:
Brian Carlstrom88f36542012-10-16 23:24:21 -0700977 case kMethod:
978 case kEnum:
Ian Rogers0571d352011-11-03 19:51:38 -0700979 case kArray:
980 case kAnnotation:
981 UNIMPLEMENTED(FATAL) << ": type " << type_;
982 break;
983 case kNull:
984 jval_.l = NULL;
985 width = 0;
986 break;
987 default:
988 LOG(FATAL) << "Unreached";
989 }
990 ptr_ += width;
991}
992
Brian Carlstromea46f952013-07-30 01:26:50 -0700993void EncodedStaticFieldValueIterator::ReadValueToField(mirror::ArtField* field) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700994 switch (type_) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700995 case kBoolean: field->SetBoolean(field->GetDeclaringClass(), jval_.z); break;
996 case kByte: field->SetByte(field->GetDeclaringClass(), jval_.b); break;
997 case kShort: field->SetShort(field->GetDeclaringClass(), jval_.s); break;
998 case kChar: field->SetChar(field->GetDeclaringClass(), jval_.c); break;
999 case kInt: field->SetInt(field->GetDeclaringClass(), jval_.i); break;
1000 case kLong: field->SetLong(field->GetDeclaringClass(), jval_.j); break;
1001 case kFloat: field->SetFloat(field->GetDeclaringClass(), jval_.f); break;
1002 case kDouble: field->SetDouble(field->GetDeclaringClass(), jval_.d); break;
1003 case kNull: field->SetObject(field->GetDeclaringClass(), NULL); break;
Ian Rogers0571d352011-11-03 19:51:38 -07001004 case kString: {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001005 mirror::String* resolved = linker_->ResolveString(dex_file_, jval_.i, dex_cache_);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001006 field->SetObject(field->GetDeclaringClass(), resolved);
Ian Rogers0571d352011-11-03 19:51:38 -07001007 break;
1008 }
Brian Carlstrom88f36542012-10-16 23:24:21 -07001009 case kType: {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001010 mirror::Class* resolved = linker_->ResolveType(dex_file_, jval_.i, dex_cache_, class_loader_);
mikaelpeltierebf6aa82012-11-07 14:02:15 +01001011 field->SetObject(field->GetDeclaringClass(), resolved);
Brian Carlstrom88f36542012-10-16 23:24:21 -07001012 break;
1013 }
Ian Rogers0571d352011-11-03 19:51:38 -07001014 default: UNIMPLEMENTED(FATAL) << ": type " << type_;
1015 }
1016}
1017
1018CatchHandlerIterator::CatchHandlerIterator(const DexFile::CodeItem& code_item, uint32_t address) {
1019 handler_.address_ = -1;
1020 int32_t offset = -1;
1021
1022 // Short-circuit the overwhelmingly common cases.
1023 switch (code_item.tries_size_) {
1024 case 0:
1025 break;
1026 case 1: {
1027 const DexFile::TryItem* tries = DexFile::GetTryItems(code_item, 0);
1028 uint32_t start = tries->start_addr_;
1029 if (address >= start) {
1030 uint32_t end = start + tries->insn_count_;
1031 if (address < end) {
1032 offset = tries->handler_off_;
1033 }
1034 }
1035 break;
1036 }
1037 default:
Ian Rogersdbbc99d2013-04-18 16:51:54 -07001038 offset = DexFile::FindCatchHandlerOffset(code_item, address);
Ian Rogers0571d352011-11-03 19:51:38 -07001039 }
Logan Chien736df022012-04-27 16:25:57 +08001040 Init(code_item, offset);
1041}
1042
1043CatchHandlerIterator::CatchHandlerIterator(const DexFile::CodeItem& code_item,
1044 const DexFile::TryItem& try_item) {
1045 handler_.address_ = -1;
1046 Init(code_item, try_item.handler_off_);
1047}
1048
1049void CatchHandlerIterator::Init(const DexFile::CodeItem& code_item,
1050 int32_t offset) {
Ian Rogers0571d352011-11-03 19:51:38 -07001051 if (offset >= 0) {
Logan Chien736df022012-04-27 16:25:57 +08001052 Init(DexFile::GetCatchHandlerData(code_item, offset));
Ian Rogers0571d352011-11-03 19:51:38 -07001053 } else {
1054 // Not found, initialize as empty
1055 current_data_ = NULL;
1056 remaining_count_ = -1;
1057 catch_all_ = false;
1058 DCHECK(!HasNext());
1059 }
1060}
1061
1062void CatchHandlerIterator::Init(const byte* handler_data) {
1063 current_data_ = handler_data;
1064 remaining_count_ = DecodeSignedLeb128(&current_data_);
1065
1066 // If remaining_count_ is non-positive, then it is the negative of
1067 // the number of catch types, and the catches are followed by a
1068 // catch-all handler.
1069 if (remaining_count_ <= 0) {
1070 catch_all_ = true;
1071 remaining_count_ = -remaining_count_;
1072 } else {
1073 catch_all_ = false;
1074 }
1075 Next();
1076}
1077
1078void CatchHandlerIterator::Next() {
1079 if (remaining_count_ > 0) {
1080 handler_.type_idx_ = DecodeUnsignedLeb128(&current_data_);
1081 handler_.address_ = DecodeUnsignedLeb128(&current_data_);
1082 remaining_count_--;
1083 return;
1084 }
1085
1086 if (catch_all_) {
1087 handler_.type_idx_ = DexFile::kDexNoIndex16;
1088 handler_.address_ = DecodeUnsignedLeb128(&current_data_);
1089 catch_all_ = false;
1090 return;
1091 }
1092
1093 // no more handler
1094 remaining_count_ = -1;
1095}
1096
Carl Shapiro1fb86202011-06-27 17:43:13 -07001097} // namespace art