blob: e0cce6e6106637e1da77639160443dfef6d61e45 [file] [log] [blame]
Carl Shapiro1fb86202011-06-27 17:43:13 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "dex_file.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07004
5#include <fcntl.h>
Brian Carlstrom1f870082011-08-23 16:02:11 -07006#include <limits.h>
Brian Carlstromb0460ea2011-07-29 10:08:05 -07007#include <stdio.h>
Ian Rogersd81871c2011-10-03 13:57:23 -07008#include <stdlib.h>
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07009#include <string.h>
Brian Carlstromb0460ea2011-07-29 10:08:05 -070010#include <sys/file.h>
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070011#include <sys/mman.h>
12#include <sys/stat.h>
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070013
Elliott Hughes90a33692011-08-30 13:27:07 -070014#include <map>
15
16#include "UniquePtr.h"
Ian Rogers0571d352011-11-03 19:51:38 -070017#include "class_linker.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070018#include "globals.h"
Ian Rogers0571d352011-11-03 19:51:38 -070019#include "leb128.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070020#include "logging.h"
21#include "object.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070022#include "os.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070023#include "stringprintf.h"
24#include "thread.h"
Ian Rogers0571d352011-11-03 19:51:38 -070025#include "utf.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070026#include "utils.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070027#include "zip_archive.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070028
29namespace art {
30
Brian Carlstromf615a612011-07-23 12:50:34 -070031const byte DexFile::kDexMagic[] = { 'd', 'e', 'x', '\n' };
32const byte DexFile::kDexMagicVersion[] = { '0', '3', '5', '\0' };
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070033
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070034DexFile::ClassPathEntry DexFile::FindInClassPath(const StringPiece& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070035 const ClassPath& class_path) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070036 for (size_t i = 0; i != class_path.size(); ++i) {
37 const DexFile* dex_file = class_path[i];
38 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
39 if (dex_class_def != NULL) {
40 return ClassPathEntry(dex_file, dex_class_def);
41 }
42 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070043 // TODO: remove reinterpret_cast when issue with -std=gnu++0x host issue resolved
Brian Carlstrom7e93b502011-08-04 14:16:22 -070044 return ClassPathEntry(reinterpret_cast<const DexFile*>(NULL),
45 reinterpret_cast<const DexFile::ClassDef*>(NULL));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070046}
47
Brian Carlstromae826982011-11-09 01:33:42 -080048void DexFile::OpenDexFiles(const std::vector<const char*>& dex_filenames,
Brian Carlstrom78128a62011-09-15 17:21:19 -070049 std::vector<const DexFile*>& dex_files,
50 const std::string& strip_location_prefix) {
51 for (size_t i = 0; i < dex_filenames.size(); i++) {
52 const char* dex_filename = dex_filenames[i];
53 const DexFile* dex_file = Open(dex_filename, strip_location_prefix);
54 if (dex_file == NULL) {
55 fprintf(stderr, "could not open .dex from file %s\n", dex_filename);
56 exit(EXIT_FAILURE);
57 }
58 dex_files.push_back(dex_file);
59 }
60}
61
Brian Carlstrom16192862011-09-12 17:50:06 -070062const DexFile* DexFile::Open(const std::string& filename,
63 const std::string& strip_location_prefix) {
jeffhao262bf462011-10-20 18:36:32 -070064 if (IsValidZipFilename(filename)) {
Brian Carlstrom16192862011-09-12 17:50:06 -070065 return DexFile::OpenZip(filename, strip_location_prefix);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070066 }
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -070067 if (!IsValidDexFilename(filename)) {
68 LOG(WARNING) << "Attempting to open dex file with unknown extension '" << filename << "'";
69 }
70 return DexFile::OpenFile(filename, filename, strip_location_prefix);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070071}
72
jeffhaob4df5142011-09-19 20:25:32 -070073void DexFile::ChangePermissions(int prot) const {
Brian Carlstrom33f741e2011-10-03 11:24:05 -070074 if (mprotect(mem_map_->GetAddress(), mem_map_->GetLength(), prot) != 0) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -080075 PLOG(FATAL) << "Failed to change dex file permissions to " << prot << " for " << GetLocation();
jeffhaob4df5142011-09-19 20:25:32 -070076 }
77}
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070078
Brian Carlstrom89521892011-12-07 22:05:07 -080079const std::string StripLocationPrefix(const std::string& original_location,
80 const std::string& strip_location_prefix) {
81 StringPiece location = original_location;
82 if (!location.starts_with(strip_location_prefix)) {
83 LOG(ERROR) << location << " does not start with " << strip_location_prefix;
84 return "";
85 }
86 location.remove_prefix(strip_location_prefix.size());
87 return location.ToString();
88}
89
Brian Carlstrom16192862011-09-12 17:50:06 -070090const DexFile* DexFile::OpenFile(const std::string& filename,
91 const std::string& original_location,
92 const std::string& strip_location_prefix) {
Brian Carlstrom89521892011-12-07 22:05:07 -080093 std::string location(StripLocationPrefix(original_location, strip_location_prefix));
94 if (location.empty()) {
Brian Carlstrom16192862011-09-12 17:50:06 -070095 return NULL;
96 }
Brian Carlstrom89521892011-12-07 22:05:07 -080097
Brian Carlstromb0460ea2011-07-29 10:08:05 -070098 int fd = open(filename.c_str(), O_RDONLY); // TODO: scoped_fd
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070099 if (fd == -1) {
100 PLOG(ERROR) << "open(\"" << filename << "\", O_RDONLY) failed";
101 return NULL;
102 }
103 struct stat sbuf;
104 memset(&sbuf, 0, sizeof(sbuf));
105 if (fstat(fd, &sbuf) == -1) {
106 PLOG(ERROR) << "fstat \"" << filename << "\" failed";
107 close(fd);
108 return NULL;
109 }
110 size_t length = sbuf.st_size;
Brian Carlstrom89521892011-12-07 22:05:07 -0800111 UniquePtr<MemMap> map(MemMap::MapFile(length, PROT_READ, MAP_PRIVATE, fd, 0));
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700112 if (map.get() == NULL) {
113 LOG(ERROR) << "mmap \"" << filename << "\" failed";
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700114 close(fd);
115 return NULL;
116 }
117 close(fd);
Brian Carlstrom89521892011-12-07 22:05:07 -0800118 return OpenMemory(location, map.release());
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700119}
120
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700121const char* DexFile::kClassesDex = "classes.dex";
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700122
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700123// Open classes.dex from within a .zip, .jar, .apk, ...
Brian Carlstrom16192862011-09-12 17:50:06 -0700124const DexFile* DexFile::OpenZip(const std::string& filename,
125 const std::string& strip_location_prefix) {
Brian Carlstrom89521892011-12-07 22:05:07 -0800126 std::string location(StripLocationPrefix(filename, strip_location_prefix));
127 if (location.empty()) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700128 return NULL;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700129 }
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700130
Elliott Hughes90a33692011-08-30 13:27:07 -0700131 UniquePtr<ZipArchive> zip_archive(ZipArchive::Open(filename));
132 if (zip_archive.get() == NULL) {
Brian Carlstrom16192862011-09-12 17:50:06 -0700133 LOG(ERROR) << "Failed to open " << filename << " when looking for classes.dex";
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700134 return NULL;
135 }
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800136 return DexFile::Open(*zip_archive.get(), location);
137}
138
139const DexFile* DexFile::Open(const ZipArchive& zip_archive, const std::string& location) {
140 UniquePtr<ZipEntry> zip_entry(zip_archive.Find(kClassesDex));
Elliott Hughes90a33692011-08-30 13:27:07 -0700141 if (zip_entry.get() == NULL) {
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800142 LOG(ERROR) << "Failed to find classes.dex within " << location;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700143 return NULL;
144 }
145
Brian Carlstrom89521892011-12-07 22:05:07 -0800146 uint32_t length = zip_entry->GetUncompressedLength();
147 UniquePtr<MemMap> map(MemMap::MapAnonymous("classes.dex extracted in memory",
148 NULL,
149 length,
150 PROT_READ | PROT_WRITE));
151 if (map.get() == NULL) {
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800152 LOG(ERROR) << "mmap classes.dex for \"" << location << "\" failed";
Brian Carlstrom89521892011-12-07 22:05:07 -0800153 return NULL;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700154 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800155
156 // Extract classes.dex
157 bool success = zip_entry->ExtractToMemory(*map.get());
158 if (!success) {
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800159 LOG(ERROR) << "Failed to extract classes.dex from '" << location << "' to memory";
Brian Carlstrom89521892011-12-07 22:05:07 -0800160 return NULL;
161 }
162
163 return OpenMemory(location, map.release());
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700164}
165
Brian Carlstrom89521892011-12-07 22:05:07 -0800166const DexFile* DexFile::OpenMemory(const byte* base,
167 size_t length,
168 const std::string& location,
169 MemMap* mem_map) {
170 CHECK_ALIGNED(base, 4); // various dex file structures must be word aligned
171 UniquePtr<DexFile> dex_file(new DexFile(base, length, location, mem_map));
Brian Carlstromf615a612011-07-23 12:50:34 -0700172 if (!dex_file->Init()) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700173 return NULL;
174 } else {
Brian Carlstromf615a612011-07-23 12:50:34 -0700175 return dex_file.release();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700176 }
177}
178
Jesse Wilson6bf19152011-09-29 13:12:33 -0400179DexFile::~DexFile() {
Elliott Hughes8cef0b82011-10-11 19:24:00 -0700180 // We don't call DeleteGlobalRef on dex_object_ because we're only called by DestroyJavaVM, and
181 // that's only called after DetachCurrentThread, which means there's no JNIEnv. We could
182 // re-attach, but cleaning up these global references is not obviously useful. It's not as if
183 // the global reference table is otherwise empty!
Jesse Wilson6bf19152011-09-29 13:12:33 -0400184}
185
186jobject DexFile::GetDexObject(JNIEnv* env) const {
187 MutexLock mu(dex_object_lock_);
188 if (dex_object_ != NULL) {
189 return dex_object_;
190 }
191
192 void* address = const_cast<void*>(reinterpret_cast<const void*>(base_));
193 jobject byte_buffer = env->NewDirectByteBuffer(address, length_);
194 if (byte_buffer == NULL) {
195 return NULL;
196 }
197
198 jclass c = env->FindClass("com/android/dex/Dex");
199 if (c == NULL) {
200 return NULL;
201 }
202
203 jmethodID mid = env->GetStaticMethodID(c, "create", "(Ljava/nio/ByteBuffer;)Lcom/android/dex/Dex;");
204 if (mid == NULL) {
205 return NULL;
206 }
207
208 jvalue args[1];
209 args[0].l = byte_buffer;
210 jobject local = env->CallStaticObjectMethodA(c, mid, args);
211 if (local == NULL) {
212 return NULL;
213 }
214
215 dex_object_ = env->NewGlobalRef(local);
216 return dex_object_;
217}
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700218
Brian Carlstromf615a612011-07-23 12:50:34 -0700219bool DexFile::Init() {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700220 InitMembers();
221 if (!IsMagicValid()) {
222 return false;
223 }
224 InitIndex();
225 return true;
226}
227
Brian Carlstromf615a612011-07-23 12:50:34 -0700228void DexFile::InitMembers() {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700229 const byte* b = base_;
230 header_ = reinterpret_cast<const Header*>(b);
231 const Header* h = header_;
232 string_ids_ = reinterpret_cast<const StringId*>(b + h->string_ids_off_);
233 type_ids_ = reinterpret_cast<const TypeId*>(b + h->type_ids_off_);
234 field_ids_ = reinterpret_cast<const FieldId*>(b + h->field_ids_off_);
235 method_ids_ = reinterpret_cast<const MethodId*>(b + h->method_ids_off_);
236 proto_ids_ = reinterpret_cast<const ProtoId*>(b + h->proto_ids_off_);
237 class_defs_ = reinterpret_cast<const ClassDef*>(b + h->class_defs_off_);
Brian Carlstrom89521892011-12-07 22:05:07 -0800238 DCHECK_EQ(length_, header_->file_size_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700239}
240
Brian Carlstromf615a612011-07-23 12:50:34 -0700241bool DexFile::IsMagicValid() {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700242 return CheckMagic(header_->magic_);
243}
244
Brian Carlstromf615a612011-07-23 12:50:34 -0700245bool DexFile::CheckMagic(const byte* magic) {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800246 CHECK(magic != NULL) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700247 if (memcmp(magic, kDexMagic, sizeof(kDexMagic)) != 0) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800248 LOG(ERROR) << "Unrecognized magic number in " << GetLocation() << ":"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700249 << " " << magic[0]
250 << " " << magic[1]
251 << " " << magic[2]
252 << " " << magic[3];
253 return false;
254 }
255 const byte* version = &magic[sizeof(kDexMagic)];
256 if (memcmp(version, kDexMagicVersion, sizeof(kDexMagicVersion)) != 0) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800257 LOG(ERROR) << "Unrecognized version number in " << GetLocation() << ":"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700258 << " " << version[0]
259 << " " << version[1]
260 << " " << version[2]
261 << " " << version[3];
262 return false;
263 }
264 return true;
265}
266
Ian Rogersd81871c2011-10-03 13:57:23 -0700267uint32_t DexFile::GetVersion() const {
268 const char* version = reinterpret_cast<const char*>(&GetHeader().magic_[sizeof(kDexMagic)]);
269 return atoi(version);
270}
271
Ian Rogers0571d352011-11-03 19:51:38 -0700272int32_t DexFile::GetStringLength(const StringId& string_id) const {
273 const byte* ptr = base_ + string_id.string_data_off_;
274 return DecodeUnsignedLeb128(&ptr);
275}
276
277// Returns a pointer to the UTF-8 string data referred to by the given string_id.
278const char* DexFile::GetStringDataAndLength(const StringId& string_id, int32_t* length) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800279 CHECK(length != NULL) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700280 const byte* ptr = base_ + string_id.string_data_off_;
281 *length = DecodeUnsignedLeb128(&ptr);
282 return reinterpret_cast<const char*>(ptr);
283}
284
Brian Carlstromf615a612011-07-23 12:50:34 -0700285void DexFile::InitIndex() {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800286 CHECK_EQ(index_.size(), 0U) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700287 for (size_t i = 0; i < NumClassDefs(); ++i) {
288 const ClassDef& class_def = GetClassDef(i);
289 const char* descriptor = GetClassDescriptor(class_def);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700290 index_[descriptor] = i;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700291 }
292}
293
Brian Carlstrome24fa612011-09-29 00:53:55 -0700294bool DexFile::FindClassDefIndex(const StringPiece& descriptor, uint32_t& idx) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700295 Index::const_iterator it = index_.find(descriptor);
296 if (it == index_.end()) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700297 return false;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700298 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700299 idx = it->second;
300 return true;
301}
302
303const DexFile::ClassDef* DexFile::FindClassDef(const StringPiece& descriptor) const {
304 uint32_t idx;
305 if (FindClassDefIndex(descriptor, idx)) {
306 return &GetClassDef(idx);
307 }
308 return NULL;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700309}
310
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800311const DexFile::FieldId* DexFile::FindFieldId(const DexFile::TypeId& declaring_klass,
312 const DexFile::StringId& name,
313 const DexFile::TypeId& type) const {
314 // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx
315 const uint16_t class_idx = GetIndexForTypeId(declaring_klass);
316 const uint32_t name_idx = GetIndexForStringId(name);
317 const uint16_t type_idx = GetIndexForTypeId(type);
318 uint32_t lo = 0;
319 uint32_t hi = NumFieldIds() - 1;
320 while (hi >= lo) {
321 uint32_t mid = (hi + lo) / 2;
322 const DexFile::FieldId& field = GetFieldId(mid);
323 if (class_idx > field.class_idx_) {
324 lo = mid + 1;
325 } else if (class_idx < field.class_idx_) {
326 hi = mid - 1;
327 } else {
328 if (name_idx > field.name_idx_) {
329 lo = mid + 1;
330 } else if (name_idx < field.name_idx_) {
331 hi = mid - 1;
332 } else {
333 if (type_idx > field.type_idx_) {
334 lo = mid + 1;
335 } else if (type_idx < field.type_idx_) {
336 hi = mid - 1;
337 } else {
338 return &field;
339 }
340 }
341 }
342 }
343 return NULL;
344}
345
346const DexFile::MethodId* DexFile::FindMethodId(const DexFile::TypeId& declaring_klass,
Ian Rogers0571d352011-11-03 19:51:38 -0700347 const DexFile::StringId& name,
348 const DexFile::ProtoId& signature) const {
349 // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800350 const uint16_t class_idx = GetIndexForTypeId(declaring_klass);
Ian Rogers0571d352011-11-03 19:51:38 -0700351 const uint32_t name_idx = GetIndexForStringId(name);
352 const uint16_t proto_idx = GetIndexForProtoId(signature);
353 uint32_t lo = 0;
354 uint32_t hi = NumMethodIds() - 1;
355 while (hi >= lo) {
356 uint32_t mid = (hi + lo) / 2;
357 const DexFile::MethodId& method = GetMethodId(mid);
358 if (class_idx > method.class_idx_) {
359 lo = mid + 1;
360 } else if (class_idx < method.class_idx_) {
361 hi = mid - 1;
362 } else {
363 if (name_idx > method.name_idx_) {
364 lo = mid + 1;
365 } else if (name_idx < method.name_idx_) {
366 hi = mid - 1;
367 } else {
368 if (proto_idx > method.proto_idx_) {
369 lo = mid + 1;
370 } else if (proto_idx < method.proto_idx_) {
371 hi = mid - 1;
372 } else {
373 return &method;
374 }
375 }
376 }
377 }
378 return NULL;
379}
380
381const DexFile::StringId* DexFile::FindStringId(const std::string& string) const {
382 uint32_t lo = 0;
383 uint32_t hi = NumStringIds() - 1;
384 while (hi >= lo) {
385 uint32_t mid = (hi + lo) / 2;
386 int32_t length;
387 const DexFile::StringId& str_id = GetStringId(mid);
388 const char* str = GetStringDataAndLength(str_id, &length);
389 int compare = CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(string.c_str(), str);
390 if (compare > 0) {
391 lo = mid + 1;
392 } else if (compare < 0) {
393 hi = mid - 1;
394 } else {
395 return &str_id;
396 }
397 }
398 return NULL;
399}
400
401const DexFile::TypeId* DexFile::FindTypeId(uint32_t string_idx) const {
402 uint32_t lo = 0;
403 uint32_t hi = NumTypeIds() - 1;
404 while (hi >= lo) {
405 uint32_t mid = (hi + lo) / 2;
406 const TypeId& type_id = GetTypeId(mid);
407 if (string_idx > type_id.descriptor_idx_) {
408 lo = mid + 1;
409 } else if (string_idx < type_id.descriptor_idx_) {
410 hi = mid - 1;
411 } else {
412 return &type_id;
413 }
414 }
415 return NULL;
416}
417
418const DexFile::ProtoId* DexFile::FindProtoId(uint16_t return_type_idx,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800419 const std::vector<uint16_t>& signature_type_idxs) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700420 uint32_t lo = 0;
421 uint32_t hi = NumProtoIds() - 1;
422 while (hi >= lo) {
423 uint32_t mid = (hi + lo) / 2;
424 const DexFile::ProtoId& proto = GetProtoId(mid);
425 int compare = return_type_idx - proto.return_type_idx_;
426 if (compare == 0) {
427 DexFileParameterIterator it(*this, proto);
428 size_t i = 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800429 while (it.HasNext() && i < signature_type_idxs.size() && compare == 0) {
430 compare = signature_type_idxs[i] - it.GetTypeIdx();
Ian Rogers0571d352011-11-03 19:51:38 -0700431 it.Next();
432 i++;
433 }
434 if (compare == 0) {
435 if (it.HasNext()) {
436 compare = -1;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800437 } else if (i < signature_type_idxs.size()) {
Ian Rogers0571d352011-11-03 19:51:38 -0700438 compare = 1;
439 }
440 }
441 }
442 if (compare > 0) {
443 lo = mid + 1;
444 } else if (compare < 0) {
445 hi = mid - 1;
446 } else {
447 return &proto;
448 }
449 }
450 return NULL;
451}
452
453// Given a signature place the type ids into the given vector
454bool DexFile::CreateTypeList(uint16_t* return_type_idx, std::vector<uint16_t>* param_type_idxs,
455 const std::string& signature) const {
456 if (signature[0] != '(') {
457 return false;
458 }
459 size_t offset = 1;
460 size_t end = signature.size();
461 bool process_return = false;
462 while (offset < end) {
463 char c = signature[offset];
464 offset++;
465 if (c == ')') {
466 process_return = true;
467 continue;
468 }
469 std::string descriptor;
470 descriptor += c;
471 while (c == '[') { // process array prefix
472 if (offset >= end) { // expect some descriptor following [
473 return false;
474 }
475 c = signature[offset];
476 offset++;
477 descriptor += c;
478 }
479 if (c == 'L') { // process type descriptors
480 do {
481 if (offset >= end) { // unexpected early termination of descriptor
482 return false;
483 }
484 c = signature[offset];
485 offset++;
486 descriptor += c;
487 } while (c != ';');
488 }
489 const DexFile::StringId* string_id = FindStringId(descriptor);
490 if (string_id == NULL) {
491 return false;
492 }
493 const DexFile::TypeId* type_id = FindTypeId(GetIndexForStringId(*string_id));
494 if (type_id == NULL) {
495 return false;
496 }
497 uint16_t type_idx = GetIndexForTypeId(*type_id);
498 if (!process_return) {
499 param_type_idxs->push_back(type_idx);
500 } else {
501 *return_type_idx = type_idx;
502 return offset == end; // return true if the signature had reached a sensible end
503 }
504 }
505 return false; // failed to correctly parse return type
506}
507
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700508// Materializes the method descriptor for a method prototype. Method
509// descriptors are not stored directly in the dex file. Instead, one
510// must assemble the descriptor from references in the prototype.
Ian Rogers0571d352011-11-03 19:51:38 -0700511std::string DexFile::CreateMethodSignature(uint32_t proto_idx, int32_t* unicode_length) const {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700512 const ProtoId& proto_id = GetProtoId(proto_idx);
513 std::string descriptor;
514 descriptor.push_back('(');
515 const TypeList* type_list = GetProtoParameters(proto_id);
516 size_t parameter_length = 0;
517 if (type_list != NULL) {
518 // A non-zero number of arguments. Append the type names.
519 for (size_t i = 0; i < type_list->Size(); ++i) {
520 const TypeItem& type_item = type_list->GetTypeItem(i);
521 uint32_t type_idx = type_item.type_idx_;
522 int32_t type_length;
Ian Rogers0571d352011-11-03 19:51:38 -0700523 const char* name = StringByTypeIdx(type_idx, &type_length);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700524 parameter_length += type_length;
525 descriptor.append(name);
526 }
527 }
528 descriptor.push_back(')');
529 uint32_t return_type_idx = proto_id.return_type_idx_;
530 int32_t return_type_length;
Ian Rogers0571d352011-11-03 19:51:38 -0700531 const char* name = StringByTypeIdx(return_type_idx, &return_type_length);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700532 descriptor.append(name);
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700533 if (unicode_length != NULL) {
534 *unicode_length = parameter_length + return_type_length + 2; // 2 for ( and )
535 }
Elliott Hughes0c424cb2011-08-26 10:16:25 -0700536 return descriptor;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700537}
538
Carl Shapiro1fb86202011-06-27 17:43:13 -0700539
Shih-wei Liao195487c2011-08-20 13:29:04 -0700540int32_t DexFile::GetLineNumFromPC(const art::Method* method, uint32_t rel_pc) const {
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -0700541 // For native method, lineno should be -2 to indicate it is native. Note that
542 // "line number == -2" is how libcore tells from StackTraceElement.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700543 if (method->GetCodeItemOffset() == 0) {
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -0700544 return -2;
545 }
546
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700547 const CodeItem* code_item = GetCodeItem(method->GetCodeItemOffset());
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800548 DCHECK(code_item != NULL) << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700549
550 // A method with no line number info should return -1
551 LineNumFromPcContext context(rel_pc, -1);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800552 DecodeDebugInfo(code_item, method->IsStatic(), method->GetDexMethodIndex(), LineNumForPcCb,
553 NULL, &context);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700554 return context.line_num_;
555}
556
Ian Rogers0571d352011-11-03 19:51:38 -0700557int32_t DexFile::FindCatchHandlerOffset(const CodeItem &code_item, int32_t tries_size,
558 uint32_t address){
559 // Note: Signed type is important for max and min.
560 int32_t min = 0;
561 int32_t max = tries_size - 1;
562
563 while (max >= min) {
564 int32_t mid = (min + max) / 2;
565 const TryItem* pTry = DexFile::GetTryItems(code_item, mid);
566 uint32_t start = pTry->start_addr_;
567 if (address < start) {
568 max = mid - 1;
569 } else {
570 uint32_t end = start + pTry->insn_count_;
571 if (address >= end) {
572 min = mid + 1;
573 } else { // We have a winner!
574 return (int32_t) pTry->handler_off_;
575 }
576 }
577 }
578 // No match.
579 return -1;
580}
581
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800582void DexFile::DecodeDebugInfo0(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Ian Rogers0571d352011-11-03 19:51:38 -0700583 DexDebugNewPositionCb posCb, DexDebugNewLocalCb local_cb,
584 void* cnxt, const byte* stream, LocalInfo* local_in_reg) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700585 uint32_t line = DecodeUnsignedLeb128(&stream);
586 uint32_t parameters_size = DecodeUnsignedLeb128(&stream);
587 uint16_t arg_reg = code_item->registers_size_ - code_item->ins_size_;
588 uint32_t address = 0;
Elliott Hughes30646832011-10-13 16:59:46 -0700589 bool need_locals = (local_cb != NULL);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700590
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800591 if (!is_static) {
Elliott Hughes30646832011-10-13 16:59:46 -0700592 if (need_locals) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800593 const char* descriptor = GetMethodDeclaringClassDescriptor(GetMethodId(method_idx));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700594 local_in_reg[arg_reg].name_ = "this";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800595 local_in_reg[arg_reg].descriptor_ = descriptor;
Elliott Hughes392b1242011-11-30 13:55:50 -0800596 local_in_reg[arg_reg].signature_ = NULL;
Elliott Hughes30646832011-10-13 16:59:46 -0700597 local_in_reg[arg_reg].start_address_ = 0;
598 local_in_reg[arg_reg].is_live_ = true;
599 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700600 arg_reg++;
601 }
602
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800603 DexFileParameterIterator it(*this, GetMethodPrototype(GetMethodId(method_idx)));
Ian Rogers0571d352011-11-03 19:51:38 -0700604 for (uint32_t i = 0; i < parameters_size && it.HasNext(); ++i, it.Next()) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700605 if (arg_reg >= code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700606 LOG(ERROR) << "invalid stream - arg reg >= reg size (" << arg_reg
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800607 << " >= " << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700608 return;
609 }
Elliott Hughes392b1242011-11-30 13:55:50 -0800610 uint32_t id = DecodeUnsignedLeb128P1(&stream);
Ian Rogers0571d352011-11-03 19:51:38 -0700611 const char* descriptor = it.GetDescriptor();
Elliott Hughes392b1242011-11-30 13:55:50 -0800612 if (need_locals && id != kDexNoIndex) {
Ian Rogers0571d352011-11-03 19:51:38 -0700613 const char* name = StringDataByIdx(id);
Elliott Hughes30646832011-10-13 16:59:46 -0700614 local_in_reg[arg_reg].name_ = name;
615 local_in_reg[arg_reg].descriptor_ = descriptor;
Elliott Hughes392b1242011-11-30 13:55:50 -0800616 local_in_reg[arg_reg].signature_ = NULL;
Elliott Hughes30646832011-10-13 16:59:46 -0700617 local_in_reg[arg_reg].start_address_ = address;
618 local_in_reg[arg_reg].is_live_ = true;
619 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700620 switch (*descriptor) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700621 case 'D':
622 case 'J':
623 arg_reg += 2;
624 break;
625 default:
626 arg_reg += 1;
627 break;
628 }
629 }
630
Ian Rogers0571d352011-11-03 19:51:38 -0700631 if (it.HasNext()) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800632 LOG(ERROR) << "invalid stream - problem with parameter iterator in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700633 return;
634 }
635
636 for (;;) {
637 uint8_t opcode = *stream++;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700638 uint16_t reg;
jeffhaof8728872011-10-28 19:11:13 -0700639 uint16_t name_idx;
640 uint16_t descriptor_idx;
641 uint16_t signature_idx = 0;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700642
Shih-wei Liao195487c2011-08-20 13:29:04 -0700643 switch (opcode) {
644 case DBG_END_SEQUENCE:
645 return;
646
647 case DBG_ADVANCE_PC:
648 address += DecodeUnsignedLeb128(&stream);
649 break;
650
651 case DBG_ADVANCE_LINE:
Shih-wei Liao8a05d272011-10-15 18:45:43 -0700652 line += DecodeSignedLeb128(&stream);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700653 break;
654
655 case DBG_START_LOCAL:
656 case DBG_START_LOCAL_EXTENDED:
657 reg = DecodeUnsignedLeb128(&stream);
658 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700659 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800660 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700661 return;
662 }
663
jeffhaof8728872011-10-28 19:11:13 -0700664 name_idx = DecodeUnsignedLeb128P1(&stream);
665 descriptor_idx = DecodeUnsignedLeb128P1(&stream);
666 if (opcode == DBG_START_LOCAL_EXTENDED) {
667 signature_idx = DecodeUnsignedLeb128P1(&stream);
668 }
669
Shih-wei Liao195487c2011-08-20 13:29:04 -0700670 // Emit what was previously there, if anything
Elliott Hughes30646832011-10-13 16:59:46 -0700671 if (need_locals) {
672 InvokeLocalCbIfLive(cnxt, reg, address, local_in_reg, local_cb);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700673
Ian Rogers0571d352011-11-03 19:51:38 -0700674 local_in_reg[reg].name_ = StringDataByIdx(name_idx);
675 local_in_reg[reg].descriptor_ = StringByTypeIdx(descriptor_idx);
Elliott Hughes30646832011-10-13 16:59:46 -0700676 if (opcode == DBG_START_LOCAL_EXTENDED) {
Ian Rogers0571d352011-11-03 19:51:38 -0700677 local_in_reg[reg].signature_ = StringDataByIdx(signature_idx);
Elliott Hughes30646832011-10-13 16:59:46 -0700678 }
679 local_in_reg[reg].start_address_ = address;
680 local_in_reg[reg].is_live_ = true;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700681 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700682 break;
683
684 case DBG_END_LOCAL:
685 reg = DecodeUnsignedLeb128(&stream);
686 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700687 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800688 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700689 return;
690 }
691
Elliott Hughes30646832011-10-13 16:59:46 -0700692 if (need_locals) {
693 InvokeLocalCbIfLive(cnxt, reg, address, local_in_reg, local_cb);
694 local_in_reg[reg].is_live_ = false;
695 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700696 break;
697
698 case DBG_RESTART_LOCAL:
699 reg = DecodeUnsignedLeb128(&stream);
700 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700701 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800702 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700703 return;
704 }
705
Elliott Hughes30646832011-10-13 16:59:46 -0700706 if (need_locals) {
707 if (local_in_reg[reg].name_ == NULL || local_in_reg[reg].descriptor_ == NULL) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800708 LOG(ERROR) << "invalid stream - no name or descriptor in " << GetLocation();
Elliott Hughes30646832011-10-13 16:59:46 -0700709 return;
710 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700711
Elliott Hughes30646832011-10-13 16:59:46 -0700712 // If the register is live, the "restart" is superfluous,
713 // and we don't want to mess with the existing start address.
714 if (!local_in_reg[reg].is_live_) {
715 local_in_reg[reg].start_address_ = address;
716 local_in_reg[reg].is_live_ = true;
717 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700718 }
719 break;
720
721 case DBG_SET_PROLOGUE_END:
722 case DBG_SET_EPILOGUE_BEGIN:
723 case DBG_SET_FILE:
724 break;
725
Shih-wei Liao8e1b4ff2011-10-15 15:43:51 -0700726 default: {
727 int adjopcode = opcode - DBG_FIRST_SPECIAL;
728
Shih-wei Liao195487c2011-08-20 13:29:04 -0700729 address += adjopcode / DBG_LINE_RANGE;
730 line += DBG_LINE_BASE + (adjopcode % DBG_LINE_RANGE);
731
732 if (posCb != NULL) {
733 if (posCb(cnxt, address, line)) {
734 // early exit
735 return;
736 }
737 }
738 break;
Shih-wei Liao8e1b4ff2011-10-15 15:43:51 -0700739 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700740 }
741 }
742}
743
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800744void DexFile::DecodeDebugInfo(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Ian Rogers0571d352011-11-03 19:51:38 -0700745 DexDebugNewPositionCb posCb, DexDebugNewLocalCb local_cb,
746 void* cnxt) const {
747 const byte* stream = GetDebugInfoStream(code_item);
748 LocalInfo local_in_reg[code_item->registers_size_];
749
750 if (stream != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800751 DecodeDebugInfo0(code_item, is_static, method_idx, posCb, local_cb, cnxt, stream, local_in_reg);
Ian Rogers0571d352011-11-03 19:51:38 -0700752 }
753 for (int reg = 0; reg < code_item->registers_size_; reg++) {
754 InvokeLocalCbIfLive(cnxt, reg, code_item->insns_size_in_code_units_, local_in_reg, local_cb);
755 }
756}
757
758bool DexFile::LineNumForPcCb(void* cnxt, uint32_t address, uint32_t line_num) {
759 LineNumFromPcContext* context = (LineNumFromPcContext*) cnxt;
760
761 // We know that this callback will be called in
762 // ascending address order, so keep going until we find
763 // a match or we've just gone past it.
764 if (address > context->address_) {
765 // The line number from the previous positions callback
766 // wil be the final result.
767 return true;
768 } else {
769 context->line_num_ = line_num;
770 return address == context->address_;
771 }
772}
773
774// Decodes the header section from the class data bytes.
775void ClassDataItemIterator::ReadClassDataHeader() {
776 CHECK(ptr_pos_ != NULL);
777 header_.static_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_);
778 header_.instance_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_);
779 header_.direct_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_);
780 header_.virtual_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_);
781}
782
783void ClassDataItemIterator::ReadClassDataField() {
784 field_.field_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_);
785 field_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_);
786}
787
788void ClassDataItemIterator::ReadClassDataMethod() {
789 method_.method_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_);
790 method_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_);
791 method_.code_off_ = DecodeUnsignedLeb128(&ptr_pos_);
792}
793
794// Read a signed integer. "zwidth" is the zero-based byte count.
795static int32_t ReadSignedInt(const byte* ptr, int zwidth) {
796 int32_t val = 0;
797 for (int i = zwidth; i >= 0; --i) {
798 val = ((uint32_t)val >> 8) | (((int32_t)*ptr++) << 24);
799 }
800 val >>= (3 - zwidth) * 8;
801 return val;
802}
803
804// Read an unsigned integer. "zwidth" is the zero-based byte count,
805// "fill_on_right" indicates which side we want to zero-fill from.
806static uint32_t ReadUnsignedInt(const byte* ptr, int zwidth, bool fill_on_right) {
807 uint32_t val = 0;
808 if (!fill_on_right) {
809 for (int i = zwidth; i >= 0; --i) {
810 val = (val >> 8) | (((uint32_t)*ptr++) << 24);
811 }
812 val >>= (3 - zwidth) * 8;
813 } else {
814 for (int i = zwidth; i >= 0; --i) {
815 val = (val >> 8) | (((uint32_t)*ptr++) << 24);
816 }
817 }
818 return val;
819}
820
821// Read a signed long. "zwidth" is the zero-based byte count.
822static int64_t ReadSignedLong(const byte* ptr, int zwidth) {
823 int64_t val = 0;
824 for (int i = zwidth; i >= 0; --i) {
825 val = ((uint64_t)val >> 8) | (((int64_t)*ptr++) << 56);
826 }
827 val >>= (7 - zwidth) * 8;
828 return val;
829}
830
831// Read an unsigned long. "zwidth" is the zero-based byte count,
832// "fill_on_right" indicates which side we want to zero-fill from.
833static uint64_t ReadUnsignedLong(const byte* ptr, int zwidth, bool fill_on_right) {
834 uint64_t val = 0;
835 if (!fill_on_right) {
836 for (int i = zwidth; i >= 0; --i) {
837 val = (val >> 8) | (((uint64_t)*ptr++) << 56);
838 }
839 val >>= (7 - zwidth) * 8;
840 } else {
841 for (int i = zwidth; i >= 0; --i) {
842 val = (val >> 8) | (((uint64_t)*ptr++) << 56);
843 }
844 }
845 return val;
846}
847
848EncodedStaticFieldValueIterator::EncodedStaticFieldValueIterator(const DexFile& dex_file,
849 DexCache* dex_cache, ClassLinker* linker, const DexFile::ClassDef& class_def) :
850 dex_file_(dex_file), dex_cache_(dex_cache), linker_(linker), array_size_(), pos_(-1), type_(0) {
851 ptr_ = dex_file.GetEncodedStaticFieldValuesArray(class_def);
852 if (ptr_ == NULL) {
853 array_size_ = 0;
854 } else {
855 array_size_ = DecodeUnsignedLeb128(&ptr_);
856 }
857 if (array_size_ > 0) {
858 Next();
859 }
860}
861
862void EncodedStaticFieldValueIterator::Next() {
863 pos_++;
864 if (pos_ >= array_size_) {
865 return;
866 }
867 byte value_type = *ptr_++;
868 byte value_arg = value_type >> kEncodedValueArgShift;
869 size_t width = value_arg + 1; // assume and correct later
870 type_ = value_type & kEncodedValueTypeMask;
871 switch (type_) {
872 case kBoolean:
873 jval_.i = (value_arg != 0) ? 1 : 0;
874 width = 0;
875 break;
876 case kByte:
877 jval_.i = ReadSignedInt(ptr_, value_arg);
878 CHECK(IsInt(8, jval_.i));
879 break;
880 case kShort:
881 jval_.i = ReadSignedInt(ptr_, value_arg);
882 CHECK(IsInt(16, jval_.i));
883 break;
884 case kChar:
885 jval_.i = ReadUnsignedInt(ptr_, value_arg, false);
886 CHECK(IsUint(16, jval_.i));
887 break;
888 case kInt:
889 jval_.i = ReadSignedInt(ptr_, value_arg);
890 break;
891 case kLong:
892 jval_.j = ReadSignedLong(ptr_, value_arg);
893 break;
894 case kFloat:
895 jval_.i = ReadUnsignedInt(ptr_, value_arg, true);
896 break;
897 case kDouble:
898 jval_.j = ReadUnsignedLong(ptr_, value_arg, true);
899 break;
900 case kString:
901 case kType:
902 case kMethod:
903 case kEnum:
904 jval_.i = ReadUnsignedInt(ptr_, value_arg, false);
905 break;
906 case kField:
907 case kArray:
908 case kAnnotation:
909 UNIMPLEMENTED(FATAL) << ": type " << type_;
910 break;
911 case kNull:
912 jval_.l = NULL;
913 width = 0;
914 break;
915 default:
916 LOG(FATAL) << "Unreached";
917 }
918 ptr_ += width;
919}
920
921void EncodedStaticFieldValueIterator::ReadValueToField(Field* field) const {
922 switch (type_) {
923 case kBoolean: field->SetBoolean(NULL, jval_.z); break;
924 case kByte: field->SetByte(NULL, jval_.b); break;
925 case kShort: field->SetShort(NULL, jval_.s); break;
926 case kChar: field->SetChar(NULL, jval_.c); break;
927 case kInt: field->SetInt(NULL, jval_.i); break;
928 case kLong: field->SetLong(NULL, jval_.j); break;
929 case kFloat: field->SetFloat(NULL, jval_.f); break;
930 case kDouble: field->SetDouble(NULL, jval_.d); break;
931 case kNull: field->SetObject(NULL, NULL); break;
932 case kString: {
933 String* resolved = linker_->ResolveString(dex_file_, jval_.i, dex_cache_);
934 field->SetObject(NULL, resolved);
935 break;
936 }
937 default: UNIMPLEMENTED(FATAL) << ": type " << type_;
938 }
939}
940
941CatchHandlerIterator::CatchHandlerIterator(const DexFile::CodeItem& code_item, uint32_t address) {
942 handler_.address_ = -1;
943 int32_t offset = -1;
944
945 // Short-circuit the overwhelmingly common cases.
946 switch (code_item.tries_size_) {
947 case 0:
948 break;
949 case 1: {
950 const DexFile::TryItem* tries = DexFile::GetTryItems(code_item, 0);
951 uint32_t start = tries->start_addr_;
952 if (address >= start) {
953 uint32_t end = start + tries->insn_count_;
954 if (address < end) {
955 offset = tries->handler_off_;
956 }
957 }
958 break;
959 }
960 default:
961 offset = DexFile::FindCatchHandlerOffset(code_item, code_item.tries_size_, address);
962 }
963 if (offset >= 0) {
964 const byte* handler_data = DexFile::GetCatchHandlerData(code_item, offset);
965 Init(handler_data);
966 } else {
967 // Not found, initialize as empty
968 current_data_ = NULL;
969 remaining_count_ = -1;
970 catch_all_ = false;
971 DCHECK(!HasNext());
972 }
973}
974
975void CatchHandlerIterator::Init(const byte* handler_data) {
976 current_data_ = handler_data;
977 remaining_count_ = DecodeSignedLeb128(&current_data_);
978
979 // If remaining_count_ is non-positive, then it is the negative of
980 // the number of catch types, and the catches are followed by a
981 // catch-all handler.
982 if (remaining_count_ <= 0) {
983 catch_all_ = true;
984 remaining_count_ = -remaining_count_;
985 } else {
986 catch_all_ = false;
987 }
988 Next();
989}
990
991void CatchHandlerIterator::Next() {
992 if (remaining_count_ > 0) {
993 handler_.type_idx_ = DecodeUnsignedLeb128(&current_data_);
994 handler_.address_ = DecodeUnsignedLeb128(&current_data_);
995 remaining_count_--;
996 return;
997 }
998
999 if (catch_all_) {
1000 handler_.type_idx_ = DexFile::kDexNoIndex16;
1001 handler_.address_ = DecodeUnsignedLeb128(&current_data_);
1002 catch_all_ = false;
1003 return;
1004 }
1005
1006 // no more handler
1007 remaining_count_ = -1;
1008}
1009
Carl Shapiro1fb86202011-06-27 17:43:13 -07001010} // namespace art