blob: 45b34270f916201206f06b7e1ab491da21b43c6e [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
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070051DexFile::ClassPathEntry DexFile::FindInClassPath(const StringPiece& 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
114bool DexFile::EnableWrite(uint8_t* addr, size_t length) const {
115 CHECK(IsReadOnly());
116 if (mem_map_.get() == NULL) {
117 return false;
118 } else {
119 return mem_map_->ProtectRegion(addr, length, PROT_READ | PROT_WRITE);
120 }
121}
122
123bool DexFile::DisableWrite(uint8_t* addr, size_t length) const {
124 CHECK(!IsReadOnly());
125 if (mem_map_.get() == NULL) {
126 return false;
127 } else {
128 return mem_map_->ProtectRegion(addr, length, PROT_READ);
129 }
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 }
211
Brian Carlstrom4922e9d2013-07-09 17:18:47 -0700212 UniquePtr<MemMap> map(zip_entry->ExtractToMemMap(kClassesDex));
Brian Carlstrom89521892011-12-07 22:05:07 -0800213 if (map.get() == NULL) {
Brian Carlstrom4922e9d2013-07-09 17:18:47 -0700214 LOG(ERROR) << "Failed to extract '" << kClassesDex << "' from '" << location << "'";
Brian Carlstrom89521892011-12-07 22:05:07 -0800215 return NULL;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700216 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800217 const DexFile* dex_file = OpenMemory(location, zip_entry->GetCrc32(), map.release());
jeffhao54c1ceb2012-02-01 11:45:32 -0800218 if (dex_file == NULL) {
219 LOG(ERROR) << "Failed to open dex file '" << location << "' from memory";
220 return NULL;
jeffhaof6174e82012-01-31 16:14:17 -0800221 }
jeffhao54c1ceb2012-02-01 11:45:32 -0800222
223 if (!DexFileVerifier::Verify(dex_file, dex_file->Begin(), dex_file->Size())) {
224 LOG(ERROR) << "Failed to verify dex file '" << location << "'";
225 return NULL;
226 }
227
jeffhaof6174e82012-01-31 16:14:17 -0800228 return dex_file;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700229}
230
Brian Carlstrom89521892011-12-07 22:05:07 -0800231const DexFile* DexFile::OpenMemory(const byte* base,
jeffhaof6174e82012-01-31 16:14:17 -0800232 size_t size,
Brian Carlstrom89521892011-12-07 22:05:07 -0800233 const std::string& location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800234 uint32_t location_checksum,
Brian Carlstrom89521892011-12-07 22:05:07 -0800235 MemMap* mem_map) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700236 CHECK_ALIGNED(base, 4); // various dex file structures must be word aligned
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800237 UniquePtr<DexFile> dex_file(new DexFile(base, size, location, location_checksum, mem_map));
Brian Carlstromf615a612011-07-23 12:50:34 -0700238 if (!dex_file->Init()) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700239 return NULL;
240 } else {
Brian Carlstromf615a612011-07-23 12:50:34 -0700241 return dex_file.release();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700242 }
243}
244
Jesse Wilson6bf19152011-09-29 13:12:33 -0400245DexFile::~DexFile() {
Elliott Hughes8cef0b82011-10-11 19:24:00 -0700246 // We don't call DeleteGlobalRef on dex_object_ because we're only called by DestroyJavaVM, and
247 // that's only called after DetachCurrentThread, which means there's no JNIEnv. We could
248 // re-attach, but cleaning up these global references is not obviously useful. It's not as if
249 // the global reference table is otherwise empty!
Jesse Wilson6bf19152011-09-29 13:12:33 -0400250}
251
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700252class ScopedJniMonitorLock {
253 public:
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700254 ScopedJniMonitorLock(JNIEnv* env, jobject locked) : env_(env), locked_(locked) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700255 env->MonitorEnter(locked_);
Jesse Wilson6bf19152011-09-29 13:12:33 -0400256 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700257 ~ScopedJniMonitorLock() {
258 env_->MonitorExit(locked_);
259 }
260 private:
261 JNIEnv* const env_;
262 const jobject locked_;
263};
Jesse Wilson6bf19152011-09-29 13:12:33 -0400264
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700265jobject DexFile::GetDexObject(JNIEnv* env) const {
266 {
267 ScopedJniMonitorLock lock(env, WellKnownClasses::com_android_dex_Dex);
268 if (dex_object_ != NULL) {
269 return dex_object_;
270 }
271 }
Ian Rogers30fab402012-01-23 15:43:46 -0800272 void* address = const_cast<void*>(reinterpret_cast<const void*>(begin_));
jeffhaof6174e82012-01-31 16:14:17 -0800273 jobject byte_buffer = env->NewDirectByteBuffer(address, size_);
Jesse Wilson6bf19152011-09-29 13:12:33 -0400274 if (byte_buffer == NULL) {
275 return NULL;
276 }
277
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700278 ScopedJniMonitorLock lock(env, WellKnownClasses::com_android_dex_Dex);
279 // Re-test to see if someone beat us to the creation when we had the lock released.
280 if (dex_object_ != NULL) {
281 return dex_object_;
282 }
Jesse Wilson6bf19152011-09-29 13:12:33 -0400283 jvalue args[1];
284 args[0].l = byte_buffer;
Elliott Hugheseac76672012-05-24 21:56:51 -0700285 jobject local = env->CallStaticObjectMethodA(WellKnownClasses::com_android_dex_Dex,
286 WellKnownClasses::com_android_dex_Dex_create,
287 args);
Jesse Wilson6bf19152011-09-29 13:12:33 -0400288 if (local == NULL) {
289 return NULL;
290 }
291
292 dex_object_ = env->NewGlobalRef(local);
293 return dex_object_;
294}
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700295
Brian Carlstromf615a612011-07-23 12:50:34 -0700296bool DexFile::Init() {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700297 InitMembers();
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800298 if (!CheckMagicAndVersion()) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700299 return false;
300 }
301 InitIndex();
302 return true;
303}
304
Brian Carlstromf615a612011-07-23 12:50:34 -0700305void DexFile::InitMembers() {
Ian Rogers30fab402012-01-23 15:43:46 -0800306 const byte* b = begin_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700307 header_ = reinterpret_cast<const Header*>(b);
308 const Header* h = header_;
309 string_ids_ = reinterpret_cast<const StringId*>(b + h->string_ids_off_);
310 type_ids_ = reinterpret_cast<const TypeId*>(b + h->type_ids_off_);
311 field_ids_ = reinterpret_cast<const FieldId*>(b + h->field_ids_off_);
312 method_ids_ = reinterpret_cast<const MethodId*>(b + h->method_ids_off_);
313 proto_ids_ = reinterpret_cast<const ProtoId*>(b + h->proto_ids_off_);
314 class_defs_ = reinterpret_cast<const ClassDef*>(b + h->class_defs_off_);
315}
316
jeffhao10037c82012-01-23 15:06:23 -0800317bool DexFile::CheckMagicAndVersion() const {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800318 CHECK(header_->magic_ != NULL) << GetLocation();
319 if (!IsMagicValid(header_->magic_)) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800320 LOG(ERROR) << "Unrecognized magic number in " << GetLocation() << ":"
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800321 << " " << header_->magic_[0]
322 << " " << header_->magic_[1]
323 << " " << header_->magic_[2]
324 << " " << header_->magic_[3];
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700325 return false;
326 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800327 if (!IsVersionValid(header_->magic_)) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800328 LOG(ERROR) << "Unrecognized version number in " << GetLocation() << ":"
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800329 << " " << header_->magic_[4]
330 << " " << header_->magic_[5]
331 << " " << header_->magic_[6]
332 << " " << header_->magic_[7];
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700333 return false;
334 }
335 return true;
336}
337
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800338bool DexFile::IsMagicValid(const byte* magic) {
339 return (memcmp(magic, kDexMagic, sizeof(kDexMagic)) == 0);
340}
341
342bool DexFile::IsVersionValid(const byte* magic) {
343 const byte* version = &magic[sizeof(kDexMagic)];
344 return (memcmp(version, kDexMagicVersion, sizeof(kDexMagicVersion)) == 0);
345}
346
Ian Rogersd81871c2011-10-03 13:57:23 -0700347uint32_t DexFile::GetVersion() const {
348 const char* version = reinterpret_cast<const char*>(&GetHeader().magic_[sizeof(kDexMagic)]);
349 return atoi(version);
350}
351
Brian Carlstromf615a612011-07-23 12:50:34 -0700352void DexFile::InitIndex() {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800353 CHECK_EQ(index_.size(), 0U) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700354 for (size_t i = 0; i < NumClassDefs(); ++i) {
355 const ClassDef& class_def = GetClassDef(i);
356 const char* descriptor = GetClassDescriptor(class_def);
Elliott Hughesa0e18062012-04-13 15:59:59 -0700357 index_.Put(descriptor, i);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700358 }
359}
360
Brian Carlstrome24fa612011-09-29 00:53:55 -0700361bool DexFile::FindClassDefIndex(const StringPiece& descriptor, uint32_t& idx) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700362 Index::const_iterator it = index_.find(descriptor);
363 if (it == index_.end()) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700364 return false;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700365 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700366 idx = it->second;
367 return true;
368}
369
370const DexFile::ClassDef* DexFile::FindClassDef(const StringPiece& descriptor) const {
371 uint32_t idx;
372 if (FindClassDefIndex(descriptor, idx)) {
373 return &GetClassDef(idx);
374 }
375 return NULL;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700376}
377
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800378const DexFile::FieldId* DexFile::FindFieldId(const DexFile::TypeId& declaring_klass,
379 const DexFile::StringId& name,
380 const DexFile::TypeId& type) const {
381 // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx
382 const uint16_t class_idx = GetIndexForTypeId(declaring_klass);
383 const uint32_t name_idx = GetIndexForStringId(name);
384 const uint16_t type_idx = GetIndexForTypeId(type);
Ian Rogersf8582c32013-05-29 16:33:03 -0700385 int32_t lo = 0;
386 int32_t hi = NumFieldIds() - 1;
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800387 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700388 int32_t mid = (hi + lo) / 2;
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800389 const DexFile::FieldId& field = GetFieldId(mid);
390 if (class_idx > field.class_idx_) {
391 lo = mid + 1;
392 } else if (class_idx < field.class_idx_) {
393 hi = mid - 1;
394 } else {
395 if (name_idx > field.name_idx_) {
396 lo = mid + 1;
397 } else if (name_idx < field.name_idx_) {
398 hi = mid - 1;
399 } else {
400 if (type_idx > field.type_idx_) {
401 lo = mid + 1;
402 } else if (type_idx < field.type_idx_) {
403 hi = mid - 1;
404 } else {
405 return &field;
406 }
407 }
408 }
409 }
410 return NULL;
411}
412
413const DexFile::MethodId* DexFile::FindMethodId(const DexFile::TypeId& declaring_klass,
Ian Rogers0571d352011-11-03 19:51:38 -0700414 const DexFile::StringId& name,
415 const DexFile::ProtoId& signature) const {
416 // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800417 const uint16_t class_idx = GetIndexForTypeId(declaring_klass);
Ian Rogers0571d352011-11-03 19:51:38 -0700418 const uint32_t name_idx = GetIndexForStringId(name);
419 const uint16_t proto_idx = GetIndexForProtoId(signature);
Ian Rogersf8582c32013-05-29 16:33:03 -0700420 int32_t lo = 0;
421 int32_t hi = NumMethodIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700422 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700423 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700424 const DexFile::MethodId& method = GetMethodId(mid);
425 if (class_idx > method.class_idx_) {
426 lo = mid + 1;
427 } else if (class_idx < method.class_idx_) {
428 hi = mid - 1;
429 } else {
430 if (name_idx > method.name_idx_) {
431 lo = mid + 1;
432 } else if (name_idx < method.name_idx_) {
433 hi = mid - 1;
434 } else {
435 if (proto_idx > method.proto_idx_) {
436 lo = mid + 1;
437 } else if (proto_idx < method.proto_idx_) {
438 hi = mid - 1;
439 } else {
440 return &method;
441 }
442 }
443 }
444 }
445 return NULL;
446}
447
Ian Rogers637c65b2013-05-31 11:46:00 -0700448const DexFile::StringId* DexFile::FindStringId(const char* string) const {
Ian Rogersf8582c32013-05-29 16:33:03 -0700449 int32_t lo = 0;
450 int32_t hi = NumStringIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700451 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700452 int32_t mid = (hi + lo) / 2;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800453 uint32_t length;
Ian Rogers0571d352011-11-03 19:51:38 -0700454 const DexFile::StringId& str_id = GetStringId(mid);
455 const char* str = GetStringDataAndLength(str_id, &length);
Ian Rogers637c65b2013-05-31 11:46:00 -0700456 int compare = CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(string, str);
457 if (compare > 0) {
458 lo = mid + 1;
459 } else if (compare < 0) {
460 hi = mid - 1;
461 } else {
462 return &str_id;
463 }
464 }
465 return NULL;
466}
467
468const DexFile::StringId* DexFile::FindStringId(const uint16_t* string) const {
469 int32_t lo = 0;
470 int32_t hi = NumStringIds() - 1;
471 while (hi >= lo) {
472 int32_t mid = (hi + lo) / 2;
473 uint32_t length;
474 const DexFile::StringId& str_id = GetStringId(mid);
475 const char* str = GetStringDataAndLength(str_id, &length);
476 int compare = CompareModifiedUtf8ToUtf16AsCodePointValues(str, string);
Ian Rogers0571d352011-11-03 19:51:38 -0700477 if (compare > 0) {
478 lo = mid + 1;
479 } else if (compare < 0) {
480 hi = mid - 1;
481 } else {
482 return &str_id;
483 }
484 }
485 return NULL;
486}
487
488const DexFile::TypeId* DexFile::FindTypeId(uint32_t string_idx) const {
Ian Rogersf8582c32013-05-29 16:33:03 -0700489 int32_t lo = 0;
490 int32_t hi = NumTypeIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700491 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700492 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700493 const TypeId& type_id = GetTypeId(mid);
494 if (string_idx > type_id.descriptor_idx_) {
495 lo = mid + 1;
496 } else if (string_idx < type_id.descriptor_idx_) {
497 hi = mid - 1;
498 } else {
499 return &type_id;
500 }
501 }
502 return NULL;
503}
504
505const DexFile::ProtoId* DexFile::FindProtoId(uint16_t return_type_idx,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800506 const std::vector<uint16_t>& signature_type_idxs) const {
Ian Rogersf8582c32013-05-29 16:33:03 -0700507 int32_t lo = 0;
508 int32_t hi = NumProtoIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700509 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700510 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700511 const DexFile::ProtoId& proto = GetProtoId(mid);
512 int compare = return_type_idx - proto.return_type_idx_;
513 if (compare == 0) {
514 DexFileParameterIterator it(*this, proto);
515 size_t i = 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800516 while (it.HasNext() && i < signature_type_idxs.size() && compare == 0) {
517 compare = signature_type_idxs[i] - it.GetTypeIdx();
Ian Rogers0571d352011-11-03 19:51:38 -0700518 it.Next();
519 i++;
520 }
521 if (compare == 0) {
522 if (it.HasNext()) {
523 compare = -1;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800524 } else if (i < signature_type_idxs.size()) {
Ian Rogers0571d352011-11-03 19:51:38 -0700525 compare = 1;
526 }
527 }
528 }
529 if (compare > 0) {
530 lo = mid + 1;
531 } else if (compare < 0) {
532 hi = mid - 1;
533 } else {
534 return &proto;
535 }
536 }
537 return NULL;
538}
539
540// Given a signature place the type ids into the given vector
541bool DexFile::CreateTypeList(uint16_t* return_type_idx, std::vector<uint16_t>* param_type_idxs,
542 const std::string& signature) const {
543 if (signature[0] != '(') {
544 return false;
545 }
546 size_t offset = 1;
547 size_t end = signature.size();
548 bool process_return = false;
549 while (offset < end) {
550 char c = signature[offset];
551 offset++;
552 if (c == ')') {
553 process_return = true;
554 continue;
555 }
556 std::string descriptor;
557 descriptor += c;
558 while (c == '[') { // process array prefix
559 if (offset >= end) { // expect some descriptor following [
560 return false;
561 }
562 c = signature[offset];
563 offset++;
564 descriptor += c;
565 }
566 if (c == 'L') { // process type descriptors
567 do {
568 if (offset >= end) { // unexpected early termination of descriptor
569 return false;
570 }
571 c = signature[offset];
572 offset++;
573 descriptor += c;
574 } while (c != ';');
575 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700576 const DexFile::StringId* string_id = FindStringId(descriptor.c_str());
Ian Rogers0571d352011-11-03 19:51:38 -0700577 if (string_id == NULL) {
578 return false;
579 }
580 const DexFile::TypeId* type_id = FindTypeId(GetIndexForStringId(*string_id));
581 if (type_id == NULL) {
582 return false;
583 }
584 uint16_t type_idx = GetIndexForTypeId(*type_id);
585 if (!process_return) {
586 param_type_idxs->push_back(type_idx);
587 } else {
588 *return_type_idx = type_idx;
589 return offset == end; // return true if the signature had reached a sensible end
590 }
591 }
592 return false; // failed to correctly parse return type
593}
594
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700595// Materializes the method descriptor for a method prototype. Method
596// descriptors are not stored directly in the dex file. Instead, one
597// must assemble the descriptor from references in the prototype.
Ian Rogers0571d352011-11-03 19:51:38 -0700598std::string DexFile::CreateMethodSignature(uint32_t proto_idx, int32_t* unicode_length) const {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700599 const ProtoId& proto_id = GetProtoId(proto_idx);
600 std::string descriptor;
601 descriptor.push_back('(');
602 const TypeList* type_list = GetProtoParameters(proto_id);
603 size_t parameter_length = 0;
604 if (type_list != NULL) {
605 // A non-zero number of arguments. Append the type names.
606 for (size_t i = 0; i < type_list->Size(); ++i) {
607 const TypeItem& type_item = type_list->GetTypeItem(i);
608 uint32_t type_idx = type_item.type_idx_;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800609 uint32_t type_length;
Ian Rogers0571d352011-11-03 19:51:38 -0700610 const char* name = StringByTypeIdx(type_idx, &type_length);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700611 parameter_length += type_length;
612 descriptor.append(name);
613 }
614 }
615 descriptor.push_back(')');
616 uint32_t return_type_idx = proto_id.return_type_idx_;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800617 uint32_t return_type_length;
Ian Rogers0571d352011-11-03 19:51:38 -0700618 const char* name = StringByTypeIdx(return_type_idx, &return_type_length);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700619 descriptor.append(name);
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700620 if (unicode_length != NULL) {
621 *unicode_length = parameter_length + return_type_length + 2; // 2 for ( and )
622 }
Elliott Hughes0c424cb2011-08-26 10:16:25 -0700623 return descriptor;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700624}
625
Brian Carlstromea46f952013-07-30 01:26:50 -0700626int32_t DexFile::GetLineNumFromPC(const mirror::ArtMethod* method, uint32_t rel_pc) const {
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -0700627 // For native method, lineno should be -2 to indicate it is native. Note that
628 // "line number == -2" is how libcore tells from StackTraceElement.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700629 if (method->GetCodeItemOffset() == 0) {
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -0700630 return -2;
631 }
632
TDYa127c8dc1012012-04-19 07:03:33 -0700633 const CodeItem* code_item = GetCodeItem(method->GetCodeItemOffset());
Elliott Hughescaf76542012-06-28 16:08:22 -0700634 DCHECK(code_item != NULL) << PrettyMethod(method) << " " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700635
636 // A method with no line number info should return -1
637 LineNumFromPcContext context(rel_pc, -1);
TDYa127c8dc1012012-04-19 07:03:33 -0700638 DecodeDebugInfo(code_item, method->IsStatic(), method->GetDexMethodIndex(), LineNumForPcCb,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800639 NULL, &context);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700640 return context.line_num_;
641}
642
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700643int32_t DexFile::FindTryItem(const CodeItem &code_item, uint32_t address) {
Ian Rogers0571d352011-11-03 19:51:38 -0700644 // Note: Signed type is important for max and min.
645 int32_t min = 0;
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700646 int32_t max = code_item.tries_size_ - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700647
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700648 while (min <= max) {
649 int32_t mid = min + ((max - min) / 2);
650
651 const art::DexFile::TryItem* ti = GetTryItems(code_item, mid);
652 uint32_t start = ti->start_addr_;
653 uint32_t end = start + ti->insn_count_;
654
Ian Rogers0571d352011-11-03 19:51:38 -0700655 if (address < start) {
656 max = mid - 1;
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700657 } else if (address >= end) {
658 min = mid + 1;
659 } else { // We have a winner!
660 return mid;
Ian Rogers0571d352011-11-03 19:51:38 -0700661 }
662 }
663 // No match.
664 return -1;
665}
666
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700667int32_t DexFile::FindCatchHandlerOffset(const CodeItem &code_item, uint32_t address) {
668 int32_t try_item = FindTryItem(code_item, address);
669 if (try_item == -1) {
670 return -1;
671 } else {
672 return DexFile::GetTryItems(code_item, try_item)->handler_off_;
673 }
674}
675
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800676void DexFile::DecodeDebugInfo0(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes2435a572012-02-17 16:07:41 -0800677 DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb,
678 void* context, const byte* stream, LocalInfo* local_in_reg) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700679 uint32_t line = DecodeUnsignedLeb128(&stream);
680 uint32_t parameters_size = DecodeUnsignedLeb128(&stream);
681 uint16_t arg_reg = code_item->registers_size_ - code_item->ins_size_;
682 uint32_t address = 0;
Elliott Hughes30646832011-10-13 16:59:46 -0700683 bool need_locals = (local_cb != NULL);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700684
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800685 if (!is_static) {
Elliott Hughes30646832011-10-13 16:59:46 -0700686 if (need_locals) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800687 const char* descriptor = GetMethodDeclaringClassDescriptor(GetMethodId(method_idx));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700688 local_in_reg[arg_reg].name_ = "this";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800689 local_in_reg[arg_reg].descriptor_ = descriptor;
Elliott Hughes392b1242011-11-30 13:55:50 -0800690 local_in_reg[arg_reg].signature_ = NULL;
Elliott Hughes30646832011-10-13 16:59:46 -0700691 local_in_reg[arg_reg].start_address_ = 0;
692 local_in_reg[arg_reg].is_live_ = true;
693 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700694 arg_reg++;
695 }
696
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800697 DexFileParameterIterator it(*this, GetMethodPrototype(GetMethodId(method_idx)));
Ian Rogers0571d352011-11-03 19:51:38 -0700698 for (uint32_t i = 0; i < parameters_size && it.HasNext(); ++i, it.Next()) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700699 if (arg_reg >= code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700700 LOG(ERROR) << "invalid stream - arg reg >= reg size (" << arg_reg
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800701 << " >= " << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700702 return;
703 }
Elliott Hughes392b1242011-11-30 13:55:50 -0800704 uint32_t id = DecodeUnsignedLeb128P1(&stream);
Ian Rogers0571d352011-11-03 19:51:38 -0700705 const char* descriptor = it.GetDescriptor();
Elliott Hughes392b1242011-11-30 13:55:50 -0800706 if (need_locals && id != kDexNoIndex) {
Ian Rogers0571d352011-11-03 19:51:38 -0700707 const char* name = StringDataByIdx(id);
Elliott Hughes30646832011-10-13 16:59:46 -0700708 local_in_reg[arg_reg].name_ = name;
709 local_in_reg[arg_reg].descriptor_ = descriptor;
Elliott Hughes392b1242011-11-30 13:55:50 -0800710 local_in_reg[arg_reg].signature_ = NULL;
Elliott Hughes30646832011-10-13 16:59:46 -0700711 local_in_reg[arg_reg].start_address_ = address;
712 local_in_reg[arg_reg].is_live_ = true;
713 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700714 switch (*descriptor) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700715 case 'D':
716 case 'J':
717 arg_reg += 2;
718 break;
719 default:
720 arg_reg += 1;
721 break;
722 }
723 }
724
Ian Rogers0571d352011-11-03 19:51:38 -0700725 if (it.HasNext()) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800726 LOG(ERROR) << "invalid stream - problem with parameter iterator in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700727 return;
728 }
729
730 for (;;) {
731 uint8_t opcode = *stream++;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700732 uint16_t reg;
jeffhaof8728872011-10-28 19:11:13 -0700733 uint16_t name_idx;
734 uint16_t descriptor_idx;
735 uint16_t signature_idx = 0;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700736
Shih-wei Liao195487c2011-08-20 13:29:04 -0700737 switch (opcode) {
738 case DBG_END_SEQUENCE:
739 return;
740
741 case DBG_ADVANCE_PC:
742 address += DecodeUnsignedLeb128(&stream);
743 break;
744
745 case DBG_ADVANCE_LINE:
Shih-wei Liao8a05d272011-10-15 18:45:43 -0700746 line += DecodeSignedLeb128(&stream);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700747 break;
748
749 case DBG_START_LOCAL:
750 case DBG_START_LOCAL_EXTENDED:
751 reg = DecodeUnsignedLeb128(&stream);
752 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700753 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800754 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700755 return;
756 }
757
jeffhaof8728872011-10-28 19:11:13 -0700758 name_idx = DecodeUnsignedLeb128P1(&stream);
759 descriptor_idx = DecodeUnsignedLeb128P1(&stream);
760 if (opcode == DBG_START_LOCAL_EXTENDED) {
761 signature_idx = DecodeUnsignedLeb128P1(&stream);
762 }
763
Shih-wei Liao195487c2011-08-20 13:29:04 -0700764 // Emit what was previously there, if anything
Elliott Hughes30646832011-10-13 16:59:46 -0700765 if (need_locals) {
Elliott Hughes2435a572012-02-17 16:07:41 -0800766 InvokeLocalCbIfLive(context, reg, address, local_in_reg, local_cb);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700767
Ian Rogers0571d352011-11-03 19:51:38 -0700768 local_in_reg[reg].name_ = StringDataByIdx(name_idx);
769 local_in_reg[reg].descriptor_ = StringByTypeIdx(descriptor_idx);
Elliott Hughes30646832011-10-13 16:59:46 -0700770 if (opcode == DBG_START_LOCAL_EXTENDED) {
Ian Rogers0571d352011-11-03 19:51:38 -0700771 local_in_reg[reg].signature_ = StringDataByIdx(signature_idx);
Elliott Hughes30646832011-10-13 16:59:46 -0700772 }
773 local_in_reg[reg].start_address_ = address;
774 local_in_reg[reg].is_live_ = true;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700775 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700776 break;
777
778 case DBG_END_LOCAL:
779 reg = DecodeUnsignedLeb128(&stream);
780 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700781 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800782 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700783 return;
784 }
785
Elliott Hughes30646832011-10-13 16:59:46 -0700786 if (need_locals) {
Elliott Hughes2435a572012-02-17 16:07:41 -0800787 InvokeLocalCbIfLive(context, reg, address, local_in_reg, local_cb);
Elliott Hughes30646832011-10-13 16:59:46 -0700788 local_in_reg[reg].is_live_ = false;
789 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700790 break;
791
792 case DBG_RESTART_LOCAL:
793 reg = DecodeUnsignedLeb128(&stream);
794 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700795 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800796 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700797 return;
798 }
799
Elliott Hughes30646832011-10-13 16:59:46 -0700800 if (need_locals) {
801 if (local_in_reg[reg].name_ == NULL || local_in_reg[reg].descriptor_ == NULL) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800802 LOG(ERROR) << "invalid stream - no name or descriptor in " << GetLocation();
Elliott Hughes30646832011-10-13 16:59:46 -0700803 return;
804 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700805
Elliott Hughes30646832011-10-13 16:59:46 -0700806 // If the register is live, the "restart" is superfluous,
807 // and we don't want to mess with the existing start address.
808 if (!local_in_reg[reg].is_live_) {
809 local_in_reg[reg].start_address_ = address;
810 local_in_reg[reg].is_live_ = true;
811 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700812 }
813 break;
814
815 case DBG_SET_PROLOGUE_END:
816 case DBG_SET_EPILOGUE_BEGIN:
817 case DBG_SET_FILE:
818 break;
819
Shih-wei Liao8e1b4ff2011-10-15 15:43:51 -0700820 default: {
821 int adjopcode = opcode - DBG_FIRST_SPECIAL;
822
Shih-wei Liao195487c2011-08-20 13:29:04 -0700823 address += adjopcode / DBG_LINE_RANGE;
824 line += DBG_LINE_BASE + (adjopcode % DBG_LINE_RANGE);
825
Elliott Hughes2435a572012-02-17 16:07:41 -0800826 if (position_cb != NULL) {
827 if (position_cb(context, address, line)) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700828 // early exit
829 return;
830 }
831 }
832 break;
Shih-wei Liao8e1b4ff2011-10-15 15:43:51 -0700833 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700834 }
835 }
836}
837
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800838void DexFile::DecodeDebugInfo(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes2435a572012-02-17 16:07:41 -0800839 DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb,
840 void* context) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700841 const byte* stream = GetDebugInfoStream(code_item);
Elliott Hughesee0fa762012-03-26 17:12:41 -0700842 UniquePtr<LocalInfo[]> local_in_reg(local_cb != NULL ? new LocalInfo[code_item->registers_size_] : NULL);
Ian Rogers0571d352011-11-03 19:51:38 -0700843 if (stream != NULL) {
Elliott Hughesee0fa762012-03-26 17:12:41 -0700844 DecodeDebugInfo0(code_item, is_static, method_idx, position_cb, local_cb, context, stream, &local_in_reg[0]);
Ian Rogers0571d352011-11-03 19:51:38 -0700845 }
846 for (int reg = 0; reg < code_item->registers_size_; reg++) {
Elliott Hughesee0fa762012-03-26 17:12:41 -0700847 InvokeLocalCbIfLive(context, reg, code_item->insns_size_in_code_units_, &local_in_reg[0], local_cb);
Ian Rogers0571d352011-11-03 19:51:38 -0700848 }
849}
850
Elliott Hughes2435a572012-02-17 16:07:41 -0800851bool DexFile::LineNumForPcCb(void* raw_context, uint32_t address, uint32_t line_num) {
852 LineNumFromPcContext* context = reinterpret_cast<LineNumFromPcContext*>(raw_context);
Ian Rogers0571d352011-11-03 19:51:38 -0700853
854 // We know that this callback will be called in
855 // ascending address order, so keep going until we find
856 // a match or we've just gone past it.
857 if (address > context->address_) {
858 // The line number from the previous positions callback
859 // wil be the final result.
860 return true;
861 } else {
862 context->line_num_ = line_num;
863 return address == context->address_;
864 }
865}
866
867// Decodes the header section from the class data bytes.
868void ClassDataItemIterator::ReadClassDataHeader() {
869 CHECK(ptr_pos_ != NULL);
870 header_.static_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_);
871 header_.instance_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_);
872 header_.direct_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_);
873 header_.virtual_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_);
874}
875
876void ClassDataItemIterator::ReadClassDataField() {
877 field_.field_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_);
878 field_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_);
Brian Carlstrom68adbe42012-05-11 17:18:08 -0700879 if (last_idx_ != 0 && field_.field_idx_delta_ == 0) {
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -0700880 LOG(WARNING) << "Duplicate field " << PrettyField(GetMemberIndex(), dex_file_)
881 << " in " << dex_file_.GetLocation();
882 }
Ian Rogers0571d352011-11-03 19:51:38 -0700883}
884
885void ClassDataItemIterator::ReadClassDataMethod() {
886 method_.method_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_);
887 method_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_);
888 method_.code_off_ = DecodeUnsignedLeb128(&ptr_pos_);
Brian Carlstrom68adbe42012-05-11 17:18:08 -0700889 if (last_idx_ != 0 && method_.method_idx_delta_ == 0) {
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -0700890 LOG(WARNING) << "Duplicate method " << PrettyMethod(GetMemberIndex(), dex_file_)
891 << " in " << dex_file_.GetLocation();
892 }
Ian Rogers0571d352011-11-03 19:51:38 -0700893}
894
895// Read a signed integer. "zwidth" is the zero-based byte count.
896static int32_t ReadSignedInt(const byte* ptr, int zwidth) {
897 int32_t val = 0;
898 for (int i = zwidth; i >= 0; --i) {
899 val = ((uint32_t)val >> 8) | (((int32_t)*ptr++) << 24);
900 }
901 val >>= (3 - zwidth) * 8;
902 return val;
903}
904
905// Read an unsigned integer. "zwidth" is the zero-based byte count,
906// "fill_on_right" indicates which side we want to zero-fill from.
907static uint32_t ReadUnsignedInt(const byte* ptr, int zwidth, bool fill_on_right) {
908 uint32_t val = 0;
909 if (!fill_on_right) {
910 for (int i = zwidth; i >= 0; --i) {
911 val = (val >> 8) | (((uint32_t)*ptr++) << 24);
912 }
913 val >>= (3 - zwidth) * 8;
914 } else {
915 for (int i = zwidth; i >= 0; --i) {
916 val = (val >> 8) | (((uint32_t)*ptr++) << 24);
917 }
918 }
919 return val;
920}
921
922// Read a signed long. "zwidth" is the zero-based byte count.
923static int64_t ReadSignedLong(const byte* ptr, int zwidth) {
924 int64_t val = 0;
925 for (int i = zwidth; i >= 0; --i) {
926 val = ((uint64_t)val >> 8) | (((int64_t)*ptr++) << 56);
927 }
928 val >>= (7 - zwidth) * 8;
929 return val;
930}
931
932// Read an unsigned long. "zwidth" is the zero-based byte count,
933// "fill_on_right" indicates which side we want to zero-fill from.
934static uint64_t ReadUnsignedLong(const byte* ptr, int zwidth, bool fill_on_right) {
935 uint64_t val = 0;
936 if (!fill_on_right) {
937 for (int i = zwidth; i >= 0; --i) {
938 val = (val >> 8) | (((uint64_t)*ptr++) << 56);
939 }
940 val >>= (7 - zwidth) * 8;
941 } else {
942 for (int i = zwidth; i >= 0; --i) {
943 val = (val >> 8) | (((uint64_t)*ptr++) << 56);
944 }
945 }
946 return val;
947}
948
949EncodedStaticFieldValueIterator::EncodedStaticFieldValueIterator(const DexFile& dex_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800950 mirror::DexCache* dex_cache,
951 mirror::ClassLoader* class_loader,
Ian Rogersca190662012-06-26 15:45:57 -0700952 ClassLinker* linker,
953 const DexFile::ClassDef& class_def)
Brian Carlstrom88f36542012-10-16 23:24:21 -0700954 : dex_file_(dex_file), dex_cache_(dex_cache), class_loader_(class_loader), linker_(linker),
955 array_size_(), pos_(-1), type_(kByte) {
Ian Rogers0571d352011-11-03 19:51:38 -0700956 ptr_ = dex_file.GetEncodedStaticFieldValuesArray(class_def);
957 if (ptr_ == NULL) {
958 array_size_ = 0;
959 } else {
960 array_size_ = DecodeUnsignedLeb128(&ptr_);
961 }
962 if (array_size_ > 0) {
963 Next();
964 }
965}
966
967void EncodedStaticFieldValueIterator::Next() {
968 pos_++;
969 if (pos_ >= array_size_) {
970 return;
971 }
972 byte value_type = *ptr_++;
973 byte value_arg = value_type >> kEncodedValueArgShift;
974 size_t width = value_arg + 1; // assume and correct later
Brian Carlstrom88f36542012-10-16 23:24:21 -0700975 type_ = static_cast<ValueType>(value_type & kEncodedValueTypeMask);
Ian Rogers0571d352011-11-03 19:51:38 -0700976 switch (type_) {
977 case kBoolean:
978 jval_.i = (value_arg != 0) ? 1 : 0;
979 width = 0;
980 break;
981 case kByte:
982 jval_.i = ReadSignedInt(ptr_, value_arg);
983 CHECK(IsInt(8, jval_.i));
984 break;
985 case kShort:
986 jval_.i = ReadSignedInt(ptr_, value_arg);
987 CHECK(IsInt(16, jval_.i));
988 break;
989 case kChar:
990 jval_.i = ReadUnsignedInt(ptr_, value_arg, false);
991 CHECK(IsUint(16, jval_.i));
992 break;
993 case kInt:
994 jval_.i = ReadSignedInt(ptr_, value_arg);
995 break;
996 case kLong:
997 jval_.j = ReadSignedLong(ptr_, value_arg);
998 break;
999 case kFloat:
1000 jval_.i = ReadUnsignedInt(ptr_, value_arg, true);
1001 break;
1002 case kDouble:
1003 jval_.j = ReadUnsignedLong(ptr_, value_arg, true);
1004 break;
1005 case kString:
1006 case kType:
Ian Rogers0571d352011-11-03 19:51:38 -07001007 jval_.i = ReadUnsignedInt(ptr_, value_arg, false);
1008 break;
1009 case kField:
Brian Carlstrom88f36542012-10-16 23:24:21 -07001010 case kMethod:
1011 case kEnum:
Ian Rogers0571d352011-11-03 19:51:38 -07001012 case kArray:
1013 case kAnnotation:
1014 UNIMPLEMENTED(FATAL) << ": type " << type_;
1015 break;
1016 case kNull:
1017 jval_.l = NULL;
1018 width = 0;
1019 break;
1020 default:
1021 LOG(FATAL) << "Unreached";
1022 }
1023 ptr_ += width;
1024}
1025
Brian Carlstromea46f952013-07-30 01:26:50 -07001026void EncodedStaticFieldValueIterator::ReadValueToField(mirror::ArtField* field) const {
Ian Rogers0571d352011-11-03 19:51:38 -07001027 switch (type_) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001028 case kBoolean: field->SetBoolean(field->GetDeclaringClass(), jval_.z); break;
1029 case kByte: field->SetByte(field->GetDeclaringClass(), jval_.b); break;
1030 case kShort: field->SetShort(field->GetDeclaringClass(), jval_.s); break;
1031 case kChar: field->SetChar(field->GetDeclaringClass(), jval_.c); break;
1032 case kInt: field->SetInt(field->GetDeclaringClass(), jval_.i); break;
1033 case kLong: field->SetLong(field->GetDeclaringClass(), jval_.j); break;
1034 case kFloat: field->SetFloat(field->GetDeclaringClass(), jval_.f); break;
1035 case kDouble: field->SetDouble(field->GetDeclaringClass(), jval_.d); break;
1036 case kNull: field->SetObject(field->GetDeclaringClass(), NULL); break;
Ian Rogers0571d352011-11-03 19:51:38 -07001037 case kString: {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001038 mirror::String* resolved = linker_->ResolveString(dex_file_, jval_.i, dex_cache_);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001039 field->SetObject(field->GetDeclaringClass(), resolved);
Ian Rogers0571d352011-11-03 19:51:38 -07001040 break;
1041 }
Brian Carlstrom88f36542012-10-16 23:24:21 -07001042 case kType: {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001043 mirror::Class* resolved = linker_->ResolveType(dex_file_, jval_.i, dex_cache_, class_loader_);
mikaelpeltierebf6aa82012-11-07 14:02:15 +01001044 field->SetObject(field->GetDeclaringClass(), resolved);
Brian Carlstrom88f36542012-10-16 23:24:21 -07001045 break;
1046 }
Ian Rogers0571d352011-11-03 19:51:38 -07001047 default: UNIMPLEMENTED(FATAL) << ": type " << type_;
1048 }
1049}
1050
1051CatchHandlerIterator::CatchHandlerIterator(const DexFile::CodeItem& code_item, uint32_t address) {
1052 handler_.address_ = -1;
1053 int32_t offset = -1;
1054
1055 // Short-circuit the overwhelmingly common cases.
1056 switch (code_item.tries_size_) {
1057 case 0:
1058 break;
1059 case 1: {
1060 const DexFile::TryItem* tries = DexFile::GetTryItems(code_item, 0);
1061 uint32_t start = tries->start_addr_;
1062 if (address >= start) {
1063 uint32_t end = start + tries->insn_count_;
1064 if (address < end) {
1065 offset = tries->handler_off_;
1066 }
1067 }
1068 break;
1069 }
1070 default:
Ian Rogersdbbc99d2013-04-18 16:51:54 -07001071 offset = DexFile::FindCatchHandlerOffset(code_item, address);
Ian Rogers0571d352011-11-03 19:51:38 -07001072 }
Logan Chien736df022012-04-27 16:25:57 +08001073 Init(code_item, offset);
1074}
1075
1076CatchHandlerIterator::CatchHandlerIterator(const DexFile::CodeItem& code_item,
1077 const DexFile::TryItem& try_item) {
1078 handler_.address_ = -1;
1079 Init(code_item, try_item.handler_off_);
1080}
1081
1082void CatchHandlerIterator::Init(const DexFile::CodeItem& code_item,
1083 int32_t offset) {
Ian Rogers0571d352011-11-03 19:51:38 -07001084 if (offset >= 0) {
Logan Chien736df022012-04-27 16:25:57 +08001085 Init(DexFile::GetCatchHandlerData(code_item, offset));
Ian Rogers0571d352011-11-03 19:51:38 -07001086 } else {
1087 // Not found, initialize as empty
1088 current_data_ = NULL;
1089 remaining_count_ = -1;
1090 catch_all_ = false;
1091 DCHECK(!HasNext());
1092 }
1093}
1094
1095void CatchHandlerIterator::Init(const byte* handler_data) {
1096 current_data_ = handler_data;
1097 remaining_count_ = DecodeSignedLeb128(&current_data_);
1098
1099 // If remaining_count_ is non-positive, then it is the negative of
1100 // the number of catch types, and the catches are followed by a
1101 // catch-all handler.
1102 if (remaining_count_ <= 0) {
1103 catch_all_ = true;
1104 remaining_count_ = -remaining_count_;
1105 } else {
1106 catch_all_ = false;
1107 }
1108 Next();
1109}
1110
1111void CatchHandlerIterator::Next() {
1112 if (remaining_count_ > 0) {
1113 handler_.type_idx_ = DecodeUnsignedLeb128(&current_data_);
1114 handler_.address_ = DecodeUnsignedLeb128(&current_data_);
1115 remaining_count_--;
1116 return;
1117 }
1118
1119 if (catch_all_) {
1120 handler_.type_idx_ = DexFile::kDexNoIndex16;
1121 handler_.address_ = DecodeUnsignedLeb128(&current_data_);
1122 catch_all_ = false;
1123 return;
1124 }
1125
1126 // no more handler
1127 remaining_count_ = -1;
1128}
1129
Carl Shapiro1fb86202011-06-27 17:43:13 -07001130} // namespace art