blob: a02823eb90fe8b3d0c2fc576d0ed7f3e5fa2e924 [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"
Vladimir Markofd995762013-11-06 16:36:36 +000039#include "ScopedFd.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070040#include "sirt_ref.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070041#include "thread.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070042#include "UniquePtr.h"
Ian Rogersa6724902013-09-23 09:23:37 -070043#include "utf-inl.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070044#include "utils.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070045#include "well_known_classes.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070046#include "zip_archive.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070047
48namespace art {
49
Brian Carlstromf615a612011-07-23 12:50:34 -070050const byte DexFile::kDexMagic[] = { 'd', 'e', 'x', '\n' };
51const byte DexFile::kDexMagicVersion[] = { '0', '3', '5', '\0' };
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070052
Ian Rogers8b2c0b92013-09-19 02:56:49 -070053DexFile::ClassPathEntry DexFile::FindInClassPath(const char* descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070054 const ClassPath& class_path) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070055 for (size_t i = 0; i != class_path.size(); ++i) {
56 const DexFile* dex_file = class_path[i];
57 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
58 if (dex_class_def != NULL) {
59 return ClassPathEntry(dex_file, dex_class_def);
60 }
61 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070062 // TODO: remove reinterpret_cast when issue with -std=gnu++0x host issue resolved
Brian Carlstrom7e93b502011-08-04 14:16:22 -070063 return ClassPathEntry(reinterpret_cast<const DexFile*>(NULL),
64 reinterpret_cast<const DexFile::ClassDef*>(NULL));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070065}
66
Ian Rogers8d31bbd2013-10-13 10:44:14 -070067static int OpenAndReadMagic(const char* filename, uint32_t* magic, std::string* error_msg) {
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070068 CHECK(magic != NULL);
Vladimir Markofd995762013-11-06 16:36:36 +000069 ScopedFd fd(open(filename, O_RDONLY, 0));
70 if (fd.get() == -1) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070071 *error_msg = StringPrintf("Unable to open '%s' : %s", filename, strerror(errno));
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070072 return -1;
73 }
Vladimir Markofd995762013-11-06 16:36:36 +000074 int n = TEMP_FAILURE_RETRY(read(fd.get(), magic, sizeof(*magic)));
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070075 if (n != sizeof(*magic)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070076 *error_msg = StringPrintf("Failed to find magic in '%s'", filename);
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070077 return -1;
78 }
Vladimir Markofd995762013-11-06 16:36:36 +000079 if (lseek(fd.get(), 0, SEEK_SET) != 0) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070080 *error_msg = StringPrintf("Failed to seek to beginning of file '%s' : %s", filename,
81 strerror(errno));
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070082 return -1;
83 }
Vladimir Markofd995762013-11-06 16:36:36 +000084 return fd.release();
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070085}
86
Ian Rogers8d31bbd2013-10-13 10:44:14 -070087bool DexFile::GetChecksum(const char* filename, uint32_t* checksum, std::string* error_msg) {
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070088 CHECK(checksum != NULL);
89 uint32_t magic;
Vladimir Markofd995762013-11-06 16:36:36 +000090 ScopedFd fd(OpenAndReadMagic(filename, &magic, error_msg));
91 if (fd.get() == -1) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070092 DCHECK(!error_msg->empty());
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070093 return false;
94 }
95 if (IsZipMagic(magic)) {
Vladimir Markofd995762013-11-06 16:36:36 +000096 UniquePtr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(fd.release(), filename, error_msg));
Brian Carlstrom5b332c82012-02-01 15:02:31 -080097 if (zip_archive.get() == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070098 *error_msg = StringPrintf("Failed to open zip archive '%s'", filename);
Brian Carlstrom5b332c82012-02-01 15:02:31 -080099 return false;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700100 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800101 UniquePtr<ZipEntry> zip_entry(zip_archive->Find(kClassesDex));
102 if (zip_entry.get() == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700103 *error_msg = StringPrintf("Zip archive '%s' doesn\'t contain %s", filename, kClassesDex);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800104 return false;
105 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700106 *checksum = zip_entry->GetCrc32();
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800107 return true;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700108 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700109 if (IsDexMagic(magic)) {
Vladimir Markofd995762013-11-06 16:36:36 +0000110 UniquePtr<const DexFile> dex_file(DexFile::OpenFile(fd.release(), filename, false, error_msg));
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800111 if (dex_file.get() == NULL) {
112 return false;
113 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700114 *checksum = dex_file->GetHeader().checksum_;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800115 return true;
116 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700117 *error_msg = StringPrintf("Expected valid zip or dex file: '%s'", filename);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800118 return false;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700119}
120
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700121const DexFile* DexFile::Open(const char* filename,
122 const char* location,
123 std::string* error_msg) {
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700124 uint32_t magic;
Vladimir Markofd995762013-11-06 16:36:36 +0000125 ScopedFd fd(OpenAndReadMagic(filename, &magic, error_msg));
126 if (fd.get() == -1) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700127 DCHECK(!error_msg->empty());
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700128 return NULL;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700129 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700130 if (IsZipMagic(magic)) {
Vladimir Markofd995762013-11-06 16:36:36 +0000131 return DexFile::OpenZip(fd.release(), location, error_msg);
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700132 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700133 if (IsDexMagic(magic)) {
Vladimir Markofd995762013-11-06 16:36:36 +0000134 return DexFile::OpenFile(fd.release(), location, true, error_msg);
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700135 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700136 *error_msg = StringPrintf("Expected valid zip or dex file: '%s'", filename);
137 return nullptr;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700138}
139
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800140int DexFile::GetPermissions() const {
141 if (mem_map_.get() == NULL) {
142 return 0;
143 } else {
144 return mem_map_->GetProtect();
145 }
146}
147
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200148bool DexFile::IsReadOnly() const {
149 return GetPermissions() == PROT_READ;
150}
151
Brian Carlstrome0948e12013-08-29 09:36:15 -0700152bool DexFile::EnableWrite() const {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200153 CHECK(IsReadOnly());
154 if (mem_map_.get() == NULL) {
155 return false;
156 } else {
Brian Carlstrome0948e12013-08-29 09:36:15 -0700157 return mem_map_->Protect(PROT_READ | PROT_WRITE);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200158 }
159}
160
Brian Carlstrome0948e12013-08-29 09:36:15 -0700161bool DexFile::DisableWrite() const {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200162 CHECK(!IsReadOnly());
163 if (mem_map_.get() == NULL) {
164 return false;
165 } else {
Brian Carlstrome0948e12013-08-29 09:36:15 -0700166 return mem_map_->Protect(PROT_READ);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200167 }
168}
169
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700170const DexFile* DexFile::OpenFile(int fd, const char* location, bool verify,
171 std::string* error_msg) {
172 CHECK(location != nullptr);
Vladimir Markofd995762013-11-06 16:36:36 +0000173 UniquePtr<MemMap> map;
174 {
175 ScopedFd delayed_close(fd);
176 struct stat sbuf;
177 memset(&sbuf, 0, sizeof(sbuf));
178 if (fstat(fd, &sbuf) == -1) {
179 *error_msg = StringPrintf("DexFile: fstat \'%s\' failed: %s", location, strerror(errno));
180 return nullptr;
181 }
182 if (S_ISDIR(sbuf.st_mode)) {
183 *error_msg = StringPrintf("Attempt to mmap directory '%s'", location);
184 return nullptr;
185 }
186 size_t length = sbuf.st_size;
187 map.reset(MemMap::MapFile(length, PROT_READ, MAP_PRIVATE, fd, 0, location, error_msg));
188 if (map.get() == nullptr) {
189 DCHECK(!error_msg->empty());
190 return nullptr;
191 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700192 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800193
194 if (map->Size() < sizeof(DexFile::Header)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700195 *error_msg = StringPrintf(
196 "DexFile: failed to open dex file \'%s\' that is too short to have a header", location);
197 return nullptr;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800198 }
199
200 const Header* dex_header = reinterpret_cast<const Header*>(map->Begin());
201
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700202 const DexFile* dex_file = OpenMemory(location, dex_header->checksum_, map.release(), error_msg);
203 if (dex_file == nullptr) {
204 *error_msg = StringPrintf("Failed to open dex file '%s' from memory: %s", location,
205 error_msg->c_str());
206 return nullptr;
jeffhaof6174e82012-01-31 16:14:17 -0800207 }
jeffhao54c1ceb2012-02-01 11:45:32 -0800208
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700209 if (verify && !DexFileVerifier::Verify(dex_file, dex_file->Begin(), dex_file->Size(), location,
210 error_msg)) {
211 return nullptr;
jeffhao54c1ceb2012-02-01 11:45:32 -0800212 }
213
jeffhaof6174e82012-01-31 16:14:17 -0800214 return dex_file;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700215}
216
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700217const char* DexFile::kClassesDex = "classes.dex";
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700218
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700219const DexFile* DexFile::OpenZip(int fd, const std::string& location, std::string* error_msg) {
220 UniquePtr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(fd, location.c_str(), error_msg));
221 if (zip_archive.get() == nullptr) {
222 DCHECK(!error_msg->empty());
223 return nullptr;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700224 }
Vladimir Markofd995762013-11-06 16:36:36 +0000225 return DexFile::Open(*zip_archive, location, error_msg);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800226}
227
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800228const DexFile* DexFile::OpenMemory(const std::string& location,
229 uint32_t location_checksum,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700230 MemMap* mem_map,
231 std::string* error_msg) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800232 return OpenMemory(mem_map->Begin(),
233 mem_map->Size(),
234 location,
235 location_checksum,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700236 mem_map,
237 error_msg);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800238}
239
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700240const DexFile* DexFile::Open(const ZipArchive& zip_archive, const std::string& location,
241 std::string* error_msg) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800242 CHECK(!location.empty());
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800243 UniquePtr<ZipEntry> zip_entry(zip_archive.Find(kClassesDex));
Elliott Hughes90a33692011-08-30 13:27:07 -0700244 if (zip_entry.get() == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700245 *error_msg = StringPrintf("Failed to find classes.dex within '%s'", location.c_str());
246 return nullptr;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700247 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700248 UniquePtr<MemMap> map(zip_entry->ExtractToMemMap(kClassesDex, error_msg));
Brian Carlstrom89521892011-12-07 22:05:07 -0800249 if (map.get() == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700250 *error_msg = StringPrintf("Failed to extract '%s' from '%s': %s", kClassesDex, location.c_str(),
251 error_msg->c_str());
252 return nullptr;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700253 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700254 UniquePtr<const DexFile> dex_file(OpenMemory(location, zip_entry->GetCrc32(), map.release(),
255 error_msg));
256 if (dex_file.get() == nullptr) {
257 *error_msg = StringPrintf("Failed to open dex file '%s' from memory: %s", location.c_str(),
258 error_msg->c_str());
259 return nullptr;
jeffhaof6174e82012-01-31 16:14:17 -0800260 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700261 if (!DexFileVerifier::Verify(dex_file.get(), dex_file->Begin(), dex_file->Size(),
262 location.c_str(), error_msg)) {
263 return nullptr;
jeffhao54c1ceb2012-02-01 11:45:32 -0800264 }
Brian Carlstrome0948e12013-08-29 09:36:15 -0700265 if (!dex_file->DisableWrite()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700266 *error_msg = StringPrintf("Failed to make dex file '%s' read only", location.c_str());
267 return nullptr;
Brian Carlstrome0948e12013-08-29 09:36:15 -0700268 }
269 CHECK(dex_file->IsReadOnly()) << location;
270 return dex_file.release();
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700271}
272
Brian Carlstrom89521892011-12-07 22:05:07 -0800273const DexFile* DexFile::OpenMemory(const byte* base,
jeffhaof6174e82012-01-31 16:14:17 -0800274 size_t size,
Brian Carlstrom89521892011-12-07 22:05:07 -0800275 const std::string& location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800276 uint32_t location_checksum,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700277 MemMap* mem_map, std::string* error_msg) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700278 CHECK_ALIGNED(base, 4); // various dex file structures must be word aligned
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800279 UniquePtr<DexFile> dex_file(new DexFile(base, size, location, location_checksum, mem_map));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700280 if (!dex_file->Init(error_msg)) {
281 return nullptr;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700282 } else {
Brian Carlstromf615a612011-07-23 12:50:34 -0700283 return dex_file.release();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700284 }
285}
286
Jesse Wilson6bf19152011-09-29 13:12:33 -0400287DexFile::~DexFile() {
Elliott Hughes8cef0b82011-10-11 19:24:00 -0700288 // We don't call DeleteGlobalRef on dex_object_ because we're only called by DestroyJavaVM, and
289 // that's only called after DetachCurrentThread, which means there's no JNIEnv. We could
290 // re-attach, but cleaning up these global references is not obviously useful. It's not as if
291 // the global reference table is otherwise empty!
Jesse Wilson6bf19152011-09-29 13:12:33 -0400292}
293
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700294bool DexFile::Init(std::string* error_msg) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700295 InitMembers();
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700296 if (!CheckMagicAndVersion(error_msg)) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700297 return false;
298 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700299 return true;
300}
301
Brian Carlstromf615a612011-07-23 12:50:34 -0700302void DexFile::InitMembers() {
Ian Rogers30fab402012-01-23 15:43:46 -0800303 const byte* b = begin_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700304 header_ = reinterpret_cast<const Header*>(b);
305 const Header* h = header_;
306 string_ids_ = reinterpret_cast<const StringId*>(b + h->string_ids_off_);
307 type_ids_ = reinterpret_cast<const TypeId*>(b + h->type_ids_off_);
308 field_ids_ = reinterpret_cast<const FieldId*>(b + h->field_ids_off_);
309 method_ids_ = reinterpret_cast<const MethodId*>(b + h->method_ids_off_);
310 proto_ids_ = reinterpret_cast<const ProtoId*>(b + h->proto_ids_off_);
311 class_defs_ = reinterpret_cast<const ClassDef*>(b + h->class_defs_off_);
312}
313
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700314bool DexFile::CheckMagicAndVersion(std::string* error_msg) const {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800315 CHECK(header_->magic_ != NULL) << GetLocation();
316 if (!IsMagicValid(header_->magic_)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700317 std::ostringstream oss;
318 oss << "Unrecognized magic number in " << GetLocation() << ":"
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800319 << " " << header_->magic_[0]
320 << " " << header_->magic_[1]
321 << " " << header_->magic_[2]
322 << " " << header_->magic_[3];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700323 *error_msg = oss.str();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700324 return false;
325 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800326 if (!IsVersionValid(header_->magic_)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700327 std::ostringstream oss;
328 oss << "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];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700333 *error_msg = oss.str();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700334 return false;
335 }
336 return true;
337}
338
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800339bool DexFile::IsMagicValid(const byte* magic) {
340 return (memcmp(magic, kDexMagic, sizeof(kDexMagic)) == 0);
341}
342
343bool DexFile::IsVersionValid(const byte* magic) {
344 const byte* version = &magic[sizeof(kDexMagic)];
345 return (memcmp(version, kDexMagicVersion, sizeof(kDexMagicVersion)) == 0);
346}
347
Ian Rogersd81871c2011-10-03 13:57:23 -0700348uint32_t DexFile::GetVersion() const {
349 const char* version = reinterpret_cast<const char*>(&GetHeader().magic_[sizeof(kDexMagic)]);
350 return atoi(version);
351}
352
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700353const DexFile::ClassDef* DexFile::FindClassDef(const char* descriptor) const {
354 size_t num_class_defs = NumClassDefs();
355 if (num_class_defs == 0) {
356 return NULL;
357 }
358 const StringId* string_id = FindStringId(descriptor);
359 if (string_id == NULL) {
360 return NULL;
361 }
362 const TypeId* type_id = FindTypeId(GetIndexForStringId(*string_id));
363 if (type_id == NULL) {
364 return NULL;
365 }
366 uint16_t type_idx = GetIndexForTypeId(*type_id);
367 for (size_t i = 0; i < num_class_defs; ++i) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700368 const ClassDef& class_def = GetClassDef(i);
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700369 if (class_def.class_idx_ == type_idx) {
370 return &class_def;
371 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700372 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700373 return NULL;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700374}
375
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700376const DexFile::ClassDef* DexFile::FindClassDef(uint16_t type_idx) const {
377 size_t num_class_defs = NumClassDefs();
378 for (size_t i = 0; i < num_class_defs; ++i) {
379 const ClassDef& class_def = GetClassDef(i);
380 if (class_def.class_idx_ == type_idx) {
381 return &class_def;
382 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700383 }
384 return NULL;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700385}
386
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800387const DexFile::FieldId* DexFile::FindFieldId(const DexFile::TypeId& declaring_klass,
388 const DexFile::StringId& name,
389 const DexFile::TypeId& type) const {
390 // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx
391 const uint16_t class_idx = GetIndexForTypeId(declaring_klass);
392 const uint32_t name_idx = GetIndexForStringId(name);
393 const uint16_t type_idx = GetIndexForTypeId(type);
Ian Rogersf8582c32013-05-29 16:33:03 -0700394 int32_t lo = 0;
395 int32_t hi = NumFieldIds() - 1;
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800396 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700397 int32_t mid = (hi + lo) / 2;
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800398 const DexFile::FieldId& field = GetFieldId(mid);
399 if (class_idx > field.class_idx_) {
400 lo = mid + 1;
401 } else if (class_idx < field.class_idx_) {
402 hi = mid - 1;
403 } else {
404 if (name_idx > field.name_idx_) {
405 lo = mid + 1;
406 } else if (name_idx < field.name_idx_) {
407 hi = mid - 1;
408 } else {
409 if (type_idx > field.type_idx_) {
410 lo = mid + 1;
411 } else if (type_idx < field.type_idx_) {
412 hi = mid - 1;
413 } else {
414 return &field;
415 }
416 }
417 }
418 }
419 return NULL;
420}
421
422const DexFile::MethodId* DexFile::FindMethodId(const DexFile::TypeId& declaring_klass,
Ian Rogers0571d352011-11-03 19:51:38 -0700423 const DexFile::StringId& name,
424 const DexFile::ProtoId& signature) const {
425 // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800426 const uint16_t class_idx = GetIndexForTypeId(declaring_klass);
Ian Rogers0571d352011-11-03 19:51:38 -0700427 const uint32_t name_idx = GetIndexForStringId(name);
428 const uint16_t proto_idx = GetIndexForProtoId(signature);
Ian Rogersf8582c32013-05-29 16:33:03 -0700429 int32_t lo = 0;
430 int32_t hi = NumMethodIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700431 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700432 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700433 const DexFile::MethodId& method = GetMethodId(mid);
434 if (class_idx > method.class_idx_) {
435 lo = mid + 1;
436 } else if (class_idx < method.class_idx_) {
437 hi = mid - 1;
438 } else {
439 if (name_idx > method.name_idx_) {
440 lo = mid + 1;
441 } else if (name_idx < method.name_idx_) {
442 hi = mid - 1;
443 } else {
444 if (proto_idx > method.proto_idx_) {
445 lo = mid + 1;
446 } else if (proto_idx < method.proto_idx_) {
447 hi = mid - 1;
448 } else {
449 return &method;
450 }
451 }
452 }
453 }
454 return NULL;
455}
456
Ian Rogers637c65b2013-05-31 11:46:00 -0700457const DexFile::StringId* DexFile::FindStringId(const char* string) const {
Ian Rogersf8582c32013-05-29 16:33:03 -0700458 int32_t lo = 0;
459 int32_t hi = NumStringIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700460 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700461 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700462 const DexFile::StringId& str_id = GetStringId(mid);
Ian Rogerscf5077a2013-10-31 12:37:54 -0700463 const char* str = GetStringData(str_id);
Ian Rogers637c65b2013-05-31 11:46:00 -0700464 int compare = CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(string, str);
465 if (compare > 0) {
466 lo = mid + 1;
467 } else if (compare < 0) {
468 hi = mid - 1;
469 } else {
470 return &str_id;
471 }
472 }
473 return NULL;
474}
475
476const DexFile::StringId* DexFile::FindStringId(const uint16_t* string) const {
477 int32_t lo = 0;
478 int32_t hi = NumStringIds() - 1;
479 while (hi >= lo) {
480 int32_t mid = (hi + lo) / 2;
Ian Rogers637c65b2013-05-31 11:46:00 -0700481 const DexFile::StringId& str_id = GetStringId(mid);
Ian Rogerscf5077a2013-10-31 12:37:54 -0700482 const char* str = GetStringData(str_id);
Ian Rogers637c65b2013-05-31 11:46:00 -0700483 int compare = CompareModifiedUtf8ToUtf16AsCodePointValues(str, string);
Ian Rogers0571d352011-11-03 19:51:38 -0700484 if (compare > 0) {
485 lo = mid + 1;
486 } else if (compare < 0) {
487 hi = mid - 1;
488 } else {
489 return &str_id;
490 }
491 }
492 return NULL;
493}
494
495const DexFile::TypeId* DexFile::FindTypeId(uint32_t string_idx) const {
Ian Rogersf8582c32013-05-29 16:33:03 -0700496 int32_t lo = 0;
497 int32_t hi = NumTypeIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700498 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700499 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700500 const TypeId& type_id = GetTypeId(mid);
501 if (string_idx > type_id.descriptor_idx_) {
502 lo = mid + 1;
503 } else if (string_idx < type_id.descriptor_idx_) {
504 hi = mid - 1;
505 } else {
506 return &type_id;
507 }
508 }
509 return NULL;
510}
511
512const DexFile::ProtoId* DexFile::FindProtoId(uint16_t return_type_idx,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800513 const std::vector<uint16_t>& signature_type_idxs) const {
Ian Rogersf8582c32013-05-29 16:33:03 -0700514 int32_t lo = 0;
515 int32_t hi = NumProtoIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700516 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700517 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700518 const DexFile::ProtoId& proto = GetProtoId(mid);
519 int compare = return_type_idx - proto.return_type_idx_;
520 if (compare == 0) {
521 DexFileParameterIterator it(*this, proto);
522 size_t i = 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800523 while (it.HasNext() && i < signature_type_idxs.size() && compare == 0) {
524 compare = signature_type_idxs[i] - it.GetTypeIdx();
Ian Rogers0571d352011-11-03 19:51:38 -0700525 it.Next();
526 i++;
527 }
528 if (compare == 0) {
529 if (it.HasNext()) {
530 compare = -1;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800531 } else if (i < signature_type_idxs.size()) {
Ian Rogers0571d352011-11-03 19:51:38 -0700532 compare = 1;
533 }
534 }
535 }
536 if (compare > 0) {
537 lo = mid + 1;
538 } else if (compare < 0) {
539 hi = mid - 1;
540 } else {
541 return &proto;
542 }
543 }
544 return NULL;
545}
546
547// Given a signature place the type ids into the given vector
Ian Rogersd91d6d62013-09-25 20:26:14 -0700548bool DexFile::CreateTypeList(const StringPiece& signature, uint16_t* return_type_idx,
549 std::vector<uint16_t>* param_type_idxs) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700550 if (signature[0] != '(') {
551 return false;
552 }
553 size_t offset = 1;
554 size_t end = signature.size();
555 bool process_return = false;
556 while (offset < end) {
557 char c = signature[offset];
558 offset++;
559 if (c == ')') {
560 process_return = true;
561 continue;
562 }
Ian Rogersd91d6d62013-09-25 20:26:14 -0700563 // TODO: avoid building a string.
Ian Rogers0571d352011-11-03 19:51:38 -0700564 std::string descriptor;
565 descriptor += c;
566 while (c == '[') { // process array prefix
567 if (offset >= end) { // expect some descriptor following [
568 return false;
569 }
570 c = signature[offset];
571 offset++;
572 descriptor += c;
573 }
574 if (c == 'L') { // process type descriptors
575 do {
576 if (offset >= end) { // unexpected early termination of descriptor
577 return false;
578 }
579 c = signature[offset];
580 offset++;
581 descriptor += c;
582 } while (c != ';');
583 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700584 const DexFile::StringId* string_id = FindStringId(descriptor.c_str());
Ian Rogers0571d352011-11-03 19:51:38 -0700585 if (string_id == NULL) {
586 return false;
587 }
588 const DexFile::TypeId* type_id = FindTypeId(GetIndexForStringId(*string_id));
589 if (type_id == NULL) {
590 return false;
591 }
592 uint16_t type_idx = GetIndexForTypeId(*type_id);
593 if (!process_return) {
594 param_type_idxs->push_back(type_idx);
595 } else {
596 *return_type_idx = type_idx;
597 return offset == end; // return true if the signature had reached a sensible end
598 }
599 }
600 return false; // failed to correctly parse return type
601}
602
Ian Rogersd91d6d62013-09-25 20:26:14 -0700603const Signature DexFile::CreateSignature(const StringPiece& signature) const {
604 uint16_t return_type_idx;
605 std::vector<uint16_t> param_type_indices;
606 bool success = CreateTypeList(signature, &return_type_idx, &param_type_indices);
607 if (!success) {
608 return Signature::NoSignature();
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700609 }
Ian Rogersd91d6d62013-09-25 20:26:14 -0700610 const ProtoId* proto_id = FindProtoId(return_type_idx, param_type_indices);
611 if (proto_id == NULL) {
612 return Signature::NoSignature();
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700613 }
Ian Rogersd91d6d62013-09-25 20:26:14 -0700614 return Signature(this, *proto_id);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700615}
616
Brian Carlstromea46f952013-07-30 01:26:50 -0700617int32_t DexFile::GetLineNumFromPC(const mirror::ArtMethod* method, uint32_t rel_pc) const {
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -0700618 // For native method, lineno should be -2 to indicate it is native. Note that
619 // "line number == -2" is how libcore tells from StackTraceElement.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700620 if (method->GetCodeItemOffset() == 0) {
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -0700621 return -2;
622 }
623
TDYa127c8dc1012012-04-19 07:03:33 -0700624 const CodeItem* code_item = GetCodeItem(method->GetCodeItemOffset());
Elliott Hughescaf76542012-06-28 16:08:22 -0700625 DCHECK(code_item != NULL) << PrettyMethod(method) << " " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700626
627 // A method with no line number info should return -1
628 LineNumFromPcContext context(rel_pc, -1);
TDYa127c8dc1012012-04-19 07:03:33 -0700629 DecodeDebugInfo(code_item, method->IsStatic(), method->GetDexMethodIndex(), LineNumForPcCb,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800630 NULL, &context);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700631 return context.line_num_;
632}
633
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700634int32_t DexFile::FindTryItem(const CodeItem &code_item, uint32_t address) {
Ian Rogers0571d352011-11-03 19:51:38 -0700635 // Note: Signed type is important for max and min.
636 int32_t min = 0;
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700637 int32_t max = code_item.tries_size_ - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700638
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700639 while (min <= max) {
640 int32_t mid = min + ((max - min) / 2);
641
642 const art::DexFile::TryItem* ti = GetTryItems(code_item, mid);
643 uint32_t start = ti->start_addr_;
644 uint32_t end = start + ti->insn_count_;
645
Ian Rogers0571d352011-11-03 19:51:38 -0700646 if (address < start) {
647 max = mid - 1;
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700648 } else if (address >= end) {
649 min = mid + 1;
650 } else { // We have a winner!
651 return mid;
Ian Rogers0571d352011-11-03 19:51:38 -0700652 }
653 }
654 // No match.
655 return -1;
656}
657
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700658int32_t DexFile::FindCatchHandlerOffset(const CodeItem &code_item, uint32_t address) {
659 int32_t try_item = FindTryItem(code_item, address);
660 if (try_item == -1) {
661 return -1;
662 } else {
663 return DexFile::GetTryItems(code_item, try_item)->handler_off_;
664 }
665}
666
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800667void DexFile::DecodeDebugInfo0(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes2435a572012-02-17 16:07:41 -0800668 DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb,
669 void* context, const byte* stream, LocalInfo* local_in_reg) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700670 uint32_t line = DecodeUnsignedLeb128(&stream);
671 uint32_t parameters_size = DecodeUnsignedLeb128(&stream);
672 uint16_t arg_reg = code_item->registers_size_ - code_item->ins_size_;
673 uint32_t address = 0;
Elliott Hughes30646832011-10-13 16:59:46 -0700674 bool need_locals = (local_cb != NULL);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700675
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800676 if (!is_static) {
Elliott Hughes30646832011-10-13 16:59:46 -0700677 if (need_locals) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800678 const char* descriptor = GetMethodDeclaringClassDescriptor(GetMethodId(method_idx));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700679 local_in_reg[arg_reg].name_ = "this";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800680 local_in_reg[arg_reg].descriptor_ = descriptor;
Elliott Hughes392b1242011-11-30 13:55:50 -0800681 local_in_reg[arg_reg].signature_ = NULL;
Elliott Hughes30646832011-10-13 16:59:46 -0700682 local_in_reg[arg_reg].start_address_ = 0;
683 local_in_reg[arg_reg].is_live_ = true;
684 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700685 arg_reg++;
686 }
687
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800688 DexFileParameterIterator it(*this, GetMethodPrototype(GetMethodId(method_idx)));
Ian Rogers0571d352011-11-03 19:51:38 -0700689 for (uint32_t i = 0; i < parameters_size && it.HasNext(); ++i, it.Next()) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700690 if (arg_reg >= code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700691 LOG(ERROR) << "invalid stream - arg reg >= reg size (" << arg_reg
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800692 << " >= " << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700693 return;
694 }
Elliott Hughes392b1242011-11-30 13:55:50 -0800695 uint32_t id = DecodeUnsignedLeb128P1(&stream);
Ian Rogers0571d352011-11-03 19:51:38 -0700696 const char* descriptor = it.GetDescriptor();
Elliott Hughes392b1242011-11-30 13:55:50 -0800697 if (need_locals && id != kDexNoIndex) {
Ian Rogers0571d352011-11-03 19:51:38 -0700698 const char* name = StringDataByIdx(id);
Elliott Hughes30646832011-10-13 16:59:46 -0700699 local_in_reg[arg_reg].name_ = name;
700 local_in_reg[arg_reg].descriptor_ = descriptor;
Elliott Hughes392b1242011-11-30 13:55:50 -0800701 local_in_reg[arg_reg].signature_ = NULL;
Elliott Hughes30646832011-10-13 16:59:46 -0700702 local_in_reg[arg_reg].start_address_ = address;
703 local_in_reg[arg_reg].is_live_ = true;
704 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700705 switch (*descriptor) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700706 case 'D':
707 case 'J':
708 arg_reg += 2;
709 break;
710 default:
711 arg_reg += 1;
712 break;
713 }
714 }
715
Ian Rogers0571d352011-11-03 19:51:38 -0700716 if (it.HasNext()) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800717 LOG(ERROR) << "invalid stream - problem with parameter iterator in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700718 return;
719 }
720
721 for (;;) {
722 uint8_t opcode = *stream++;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700723 uint16_t reg;
jeffhaof8728872011-10-28 19:11:13 -0700724 uint16_t name_idx;
725 uint16_t descriptor_idx;
726 uint16_t signature_idx = 0;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700727
Shih-wei Liao195487c2011-08-20 13:29:04 -0700728 switch (opcode) {
729 case DBG_END_SEQUENCE:
730 return;
731
732 case DBG_ADVANCE_PC:
733 address += DecodeUnsignedLeb128(&stream);
734 break;
735
736 case DBG_ADVANCE_LINE:
Shih-wei Liao8a05d272011-10-15 18:45:43 -0700737 line += DecodeSignedLeb128(&stream);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700738 break;
739
740 case DBG_START_LOCAL:
741 case DBG_START_LOCAL_EXTENDED:
742 reg = DecodeUnsignedLeb128(&stream);
743 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700744 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800745 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700746 return;
747 }
748
jeffhaof8728872011-10-28 19:11:13 -0700749 name_idx = DecodeUnsignedLeb128P1(&stream);
750 descriptor_idx = DecodeUnsignedLeb128P1(&stream);
751 if (opcode == DBG_START_LOCAL_EXTENDED) {
752 signature_idx = DecodeUnsignedLeb128P1(&stream);
753 }
754
Shih-wei Liao195487c2011-08-20 13:29:04 -0700755 // Emit what was previously there, if anything
Elliott Hughes30646832011-10-13 16:59:46 -0700756 if (need_locals) {
Elliott Hughes2435a572012-02-17 16:07:41 -0800757 InvokeLocalCbIfLive(context, reg, address, local_in_reg, local_cb);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700758
Ian Rogers0571d352011-11-03 19:51:38 -0700759 local_in_reg[reg].name_ = StringDataByIdx(name_idx);
760 local_in_reg[reg].descriptor_ = StringByTypeIdx(descriptor_idx);
Elliott Hughes30646832011-10-13 16:59:46 -0700761 if (opcode == DBG_START_LOCAL_EXTENDED) {
Ian Rogers0571d352011-11-03 19:51:38 -0700762 local_in_reg[reg].signature_ = StringDataByIdx(signature_idx);
Elliott Hughes30646832011-10-13 16:59:46 -0700763 }
764 local_in_reg[reg].start_address_ = address;
765 local_in_reg[reg].is_live_ = true;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700766 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700767 break;
768
769 case DBG_END_LOCAL:
770 reg = DecodeUnsignedLeb128(&stream);
771 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700772 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800773 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700774 return;
775 }
776
Elliott Hughes30646832011-10-13 16:59:46 -0700777 if (need_locals) {
Elliott Hughes2435a572012-02-17 16:07:41 -0800778 InvokeLocalCbIfLive(context, reg, address, local_in_reg, local_cb);
Elliott Hughes30646832011-10-13 16:59:46 -0700779 local_in_reg[reg].is_live_ = false;
780 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700781 break;
782
783 case DBG_RESTART_LOCAL:
784 reg = DecodeUnsignedLeb128(&stream);
785 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700786 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800787 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700788 return;
789 }
790
Elliott Hughes30646832011-10-13 16:59:46 -0700791 if (need_locals) {
792 if (local_in_reg[reg].name_ == NULL || local_in_reg[reg].descriptor_ == NULL) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800793 LOG(ERROR) << "invalid stream - no name or descriptor in " << GetLocation();
Elliott Hughes30646832011-10-13 16:59:46 -0700794 return;
795 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700796
Elliott Hughes30646832011-10-13 16:59:46 -0700797 // If the register is live, the "restart" is superfluous,
798 // and we don't want to mess with the existing start address.
799 if (!local_in_reg[reg].is_live_) {
800 local_in_reg[reg].start_address_ = address;
801 local_in_reg[reg].is_live_ = true;
802 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700803 }
804 break;
805
806 case DBG_SET_PROLOGUE_END:
807 case DBG_SET_EPILOGUE_BEGIN:
808 case DBG_SET_FILE:
809 break;
810
Shih-wei Liao8e1b4ff2011-10-15 15:43:51 -0700811 default: {
812 int adjopcode = opcode - DBG_FIRST_SPECIAL;
813
Shih-wei Liao195487c2011-08-20 13:29:04 -0700814 address += adjopcode / DBG_LINE_RANGE;
815 line += DBG_LINE_BASE + (adjopcode % DBG_LINE_RANGE);
816
Elliott Hughes2435a572012-02-17 16:07:41 -0800817 if (position_cb != NULL) {
818 if (position_cb(context, address, line)) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700819 // early exit
820 return;
821 }
822 }
823 break;
Shih-wei Liao8e1b4ff2011-10-15 15:43:51 -0700824 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700825 }
826 }
827}
828
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800829void DexFile::DecodeDebugInfo(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes2435a572012-02-17 16:07:41 -0800830 DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb,
831 void* context) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700832 const byte* stream = GetDebugInfoStream(code_item);
Brian Carlstrome0948e12013-08-29 09:36:15 -0700833 UniquePtr<LocalInfo[]> local_in_reg(local_cb != NULL ?
834 new LocalInfo[code_item->registers_size_] :
835 NULL);
Ian Rogers0571d352011-11-03 19:51:38 -0700836 if (stream != NULL) {
Elliott Hughesee0fa762012-03-26 17:12:41 -0700837 DecodeDebugInfo0(code_item, is_static, method_idx, position_cb, local_cb, context, stream, &local_in_reg[0]);
Ian Rogers0571d352011-11-03 19:51:38 -0700838 }
839 for (int reg = 0; reg < code_item->registers_size_; reg++) {
Elliott Hughesee0fa762012-03-26 17:12:41 -0700840 InvokeLocalCbIfLive(context, reg, code_item->insns_size_in_code_units_, &local_in_reg[0], local_cb);
Ian Rogers0571d352011-11-03 19:51:38 -0700841 }
842}
843
Elliott Hughes2435a572012-02-17 16:07:41 -0800844bool DexFile::LineNumForPcCb(void* raw_context, uint32_t address, uint32_t line_num) {
845 LineNumFromPcContext* context = reinterpret_cast<LineNumFromPcContext*>(raw_context);
Ian Rogers0571d352011-11-03 19:51:38 -0700846
847 // We know that this callback will be called in
848 // ascending address order, so keep going until we find
849 // a match or we've just gone past it.
850 if (address > context->address_) {
851 // The line number from the previous positions callback
852 // wil be the final result.
853 return true;
854 } else {
855 context->line_num_ = line_num;
856 return address == context->address_;
857 }
858}
859
Ian Rogersd91d6d62013-09-25 20:26:14 -0700860std::string Signature::ToString() const {
861 if (dex_file_ == nullptr) {
862 CHECK(proto_id_ == nullptr);
863 return "<no signature>";
864 }
865 const DexFile::TypeList* params = dex_file_->GetProtoParameters(*proto_id_);
866 std::string result;
867 if (params == nullptr) {
868 result += "()";
869 } else {
870 result += "(";
871 for (uint32_t i = 0; i < params->Size(); ++i) {
872 result += dex_file_->StringByTypeIdx(params->GetTypeItem(i).type_idx_);
873 }
874 result += ")";
875 }
876 result += dex_file_->StringByTypeIdx(proto_id_->return_type_idx_);
877 return result;
878}
879
880std::ostream& operator<<(std::ostream& os, const Signature& sig) {
881 return os << sig.ToString();
882}
883
Ian Rogers0571d352011-11-03 19:51:38 -0700884// Decodes the header section from the class data bytes.
885void ClassDataItemIterator::ReadClassDataHeader() {
886 CHECK(ptr_pos_ != NULL);
887 header_.static_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_);
888 header_.instance_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_);
889 header_.direct_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_);
890 header_.virtual_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_);
891}
892
893void ClassDataItemIterator::ReadClassDataField() {
894 field_.field_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_);
895 field_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_);
Brian Carlstrom68adbe42012-05-11 17:18:08 -0700896 if (last_idx_ != 0 && field_.field_idx_delta_ == 0) {
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -0700897 LOG(WARNING) << "Duplicate field " << PrettyField(GetMemberIndex(), dex_file_)
898 << " in " << dex_file_.GetLocation();
899 }
Ian Rogers0571d352011-11-03 19:51:38 -0700900}
901
902void ClassDataItemIterator::ReadClassDataMethod() {
903 method_.method_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_);
904 method_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_);
905 method_.code_off_ = DecodeUnsignedLeb128(&ptr_pos_);
Brian Carlstrom68adbe42012-05-11 17:18:08 -0700906 if (last_idx_ != 0 && method_.method_idx_delta_ == 0) {
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -0700907 LOG(WARNING) << "Duplicate method " << PrettyMethod(GetMemberIndex(), dex_file_)
908 << " in " << dex_file_.GetLocation();
909 }
Ian Rogers0571d352011-11-03 19:51:38 -0700910}
911
912// Read a signed integer. "zwidth" is the zero-based byte count.
913static int32_t ReadSignedInt(const byte* ptr, int zwidth) {
914 int32_t val = 0;
915 for (int i = zwidth; i >= 0; --i) {
916 val = ((uint32_t)val >> 8) | (((int32_t)*ptr++) << 24);
917 }
918 val >>= (3 - zwidth) * 8;
919 return val;
920}
921
922// Read an unsigned integer. "zwidth" is the zero-based byte count,
923// "fill_on_right" indicates which side we want to zero-fill from.
924static uint32_t ReadUnsignedInt(const byte* ptr, int zwidth, bool fill_on_right) {
925 uint32_t val = 0;
926 if (!fill_on_right) {
927 for (int i = zwidth; i >= 0; --i) {
928 val = (val >> 8) | (((uint32_t)*ptr++) << 24);
929 }
930 val >>= (3 - zwidth) * 8;
931 } else {
932 for (int i = zwidth; i >= 0; --i) {
933 val = (val >> 8) | (((uint32_t)*ptr++) << 24);
934 }
935 }
936 return val;
937}
938
939// Read a signed long. "zwidth" is the zero-based byte count.
940static int64_t ReadSignedLong(const byte* ptr, int zwidth) {
941 int64_t val = 0;
942 for (int i = zwidth; i >= 0; --i) {
943 val = ((uint64_t)val >> 8) | (((int64_t)*ptr++) << 56);
944 }
945 val >>= (7 - zwidth) * 8;
946 return val;
947}
948
949// Read an unsigned long. "zwidth" is the zero-based byte count,
950// "fill_on_right" indicates which side we want to zero-fill from.
951static uint64_t ReadUnsignedLong(const byte* ptr, int zwidth, bool fill_on_right) {
952 uint64_t val = 0;
953 if (!fill_on_right) {
954 for (int i = zwidth; i >= 0; --i) {
955 val = (val >> 8) | (((uint64_t)*ptr++) << 56);
956 }
957 val >>= (7 - zwidth) * 8;
958 } else {
959 for (int i = zwidth; i >= 0; --i) {
960 val = (val >> 8) | (((uint64_t)*ptr++) << 56);
961 }
962 }
963 return val;
964}
965
966EncodedStaticFieldValueIterator::EncodedStaticFieldValueIterator(const DexFile& dex_file,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700967 SirtRef<mirror::DexCache>* dex_cache,
968 SirtRef<mirror::ClassLoader>* class_loader,
Ian Rogersca190662012-06-26 15:45:57 -0700969 ClassLinker* linker,
970 const DexFile::ClassDef& class_def)
Brian Carlstrom88f36542012-10-16 23:24:21 -0700971 : dex_file_(dex_file), dex_cache_(dex_cache), class_loader_(class_loader), linker_(linker),
972 array_size_(), pos_(-1), type_(kByte) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700973 DCHECK(dex_cache != nullptr);
974 DCHECK(class_loader != nullptr);
Ian Rogers0571d352011-11-03 19:51:38 -0700975 ptr_ = dex_file.GetEncodedStaticFieldValuesArray(class_def);
976 if (ptr_ == NULL) {
977 array_size_ = 0;
978 } else {
979 array_size_ = DecodeUnsignedLeb128(&ptr_);
980 }
981 if (array_size_ > 0) {
982 Next();
983 }
984}
985
986void EncodedStaticFieldValueIterator::Next() {
987 pos_++;
988 if (pos_ >= array_size_) {
989 return;
990 }
991 byte value_type = *ptr_++;
992 byte value_arg = value_type >> kEncodedValueArgShift;
993 size_t width = value_arg + 1; // assume and correct later
Brian Carlstrom88f36542012-10-16 23:24:21 -0700994 type_ = static_cast<ValueType>(value_type & kEncodedValueTypeMask);
Ian Rogers0571d352011-11-03 19:51:38 -0700995 switch (type_) {
996 case kBoolean:
997 jval_.i = (value_arg != 0) ? 1 : 0;
998 width = 0;
999 break;
1000 case kByte:
1001 jval_.i = ReadSignedInt(ptr_, value_arg);
1002 CHECK(IsInt(8, jval_.i));
1003 break;
1004 case kShort:
1005 jval_.i = ReadSignedInt(ptr_, value_arg);
1006 CHECK(IsInt(16, jval_.i));
1007 break;
1008 case kChar:
1009 jval_.i = ReadUnsignedInt(ptr_, value_arg, false);
1010 CHECK(IsUint(16, jval_.i));
1011 break;
1012 case kInt:
1013 jval_.i = ReadSignedInt(ptr_, value_arg);
1014 break;
1015 case kLong:
1016 jval_.j = ReadSignedLong(ptr_, value_arg);
1017 break;
1018 case kFloat:
1019 jval_.i = ReadUnsignedInt(ptr_, value_arg, true);
1020 break;
1021 case kDouble:
1022 jval_.j = ReadUnsignedLong(ptr_, value_arg, true);
1023 break;
1024 case kString:
1025 case kType:
Ian Rogers0571d352011-11-03 19:51:38 -07001026 jval_.i = ReadUnsignedInt(ptr_, value_arg, false);
1027 break;
1028 case kField:
Brian Carlstrom88f36542012-10-16 23:24:21 -07001029 case kMethod:
1030 case kEnum:
Ian Rogers0571d352011-11-03 19:51:38 -07001031 case kArray:
1032 case kAnnotation:
1033 UNIMPLEMENTED(FATAL) << ": type " << type_;
1034 break;
1035 case kNull:
1036 jval_.l = NULL;
1037 width = 0;
1038 break;
1039 default:
1040 LOG(FATAL) << "Unreached";
1041 }
1042 ptr_ += width;
1043}
1044
Brian Carlstromea46f952013-07-30 01:26:50 -07001045void EncodedStaticFieldValueIterator::ReadValueToField(mirror::ArtField* field) const {
Ian Rogers0571d352011-11-03 19:51:38 -07001046 switch (type_) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001047 case kBoolean: field->SetBoolean(field->GetDeclaringClass(), jval_.z); break;
1048 case kByte: field->SetByte(field->GetDeclaringClass(), jval_.b); break;
1049 case kShort: field->SetShort(field->GetDeclaringClass(), jval_.s); break;
1050 case kChar: field->SetChar(field->GetDeclaringClass(), jval_.c); break;
1051 case kInt: field->SetInt(field->GetDeclaringClass(), jval_.i); break;
1052 case kLong: field->SetLong(field->GetDeclaringClass(), jval_.j); break;
1053 case kFloat: field->SetFloat(field->GetDeclaringClass(), jval_.f); break;
1054 case kDouble: field->SetDouble(field->GetDeclaringClass(), jval_.d); break;
1055 case kNull: field->SetObject(field->GetDeclaringClass(), NULL); break;
Ian Rogers0571d352011-11-03 19:51:38 -07001056 case kString: {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001057 CHECK(!kMovingFields);
1058 mirror::String* resolved = linker_->ResolveString(dex_file_, jval_.i, *dex_cache_);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001059 field->SetObject(field->GetDeclaringClass(), resolved);
Ian Rogers0571d352011-11-03 19:51:38 -07001060 break;
1061 }
Brian Carlstrom88f36542012-10-16 23:24:21 -07001062 case kType: {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001063 CHECK(!kMovingFields);
1064 mirror::Class* resolved = linker_->ResolveType(dex_file_, jval_.i, *dex_cache_,
1065 *class_loader_);
mikaelpeltierebf6aa82012-11-07 14:02:15 +01001066 field->SetObject(field->GetDeclaringClass(), resolved);
Brian Carlstrom88f36542012-10-16 23:24:21 -07001067 break;
1068 }
Ian Rogers0571d352011-11-03 19:51:38 -07001069 default: UNIMPLEMENTED(FATAL) << ": type " << type_;
1070 }
1071}
1072
1073CatchHandlerIterator::CatchHandlerIterator(const DexFile::CodeItem& code_item, uint32_t address) {
1074 handler_.address_ = -1;
1075 int32_t offset = -1;
1076
1077 // Short-circuit the overwhelmingly common cases.
1078 switch (code_item.tries_size_) {
1079 case 0:
1080 break;
1081 case 1: {
1082 const DexFile::TryItem* tries = DexFile::GetTryItems(code_item, 0);
1083 uint32_t start = tries->start_addr_;
1084 if (address >= start) {
1085 uint32_t end = start + tries->insn_count_;
1086 if (address < end) {
1087 offset = tries->handler_off_;
1088 }
1089 }
1090 break;
1091 }
1092 default:
Ian Rogersdbbc99d2013-04-18 16:51:54 -07001093 offset = DexFile::FindCatchHandlerOffset(code_item, address);
Ian Rogers0571d352011-11-03 19:51:38 -07001094 }
Logan Chien736df022012-04-27 16:25:57 +08001095 Init(code_item, offset);
1096}
1097
1098CatchHandlerIterator::CatchHandlerIterator(const DexFile::CodeItem& code_item,
1099 const DexFile::TryItem& try_item) {
1100 handler_.address_ = -1;
1101 Init(code_item, try_item.handler_off_);
1102}
1103
1104void CatchHandlerIterator::Init(const DexFile::CodeItem& code_item,
1105 int32_t offset) {
Ian Rogers0571d352011-11-03 19:51:38 -07001106 if (offset >= 0) {
Logan Chien736df022012-04-27 16:25:57 +08001107 Init(DexFile::GetCatchHandlerData(code_item, offset));
Ian Rogers0571d352011-11-03 19:51:38 -07001108 } else {
1109 // Not found, initialize as empty
1110 current_data_ = NULL;
1111 remaining_count_ = -1;
1112 catch_all_ = false;
1113 DCHECK(!HasNext());
1114 }
1115}
1116
1117void CatchHandlerIterator::Init(const byte* handler_data) {
1118 current_data_ = handler_data;
1119 remaining_count_ = DecodeSignedLeb128(&current_data_);
1120
1121 // If remaining_count_ is non-positive, then it is the negative of
1122 // the number of catch types, and the catches are followed by a
1123 // catch-all handler.
1124 if (remaining_count_ <= 0) {
1125 catch_all_ = true;
1126 remaining_count_ = -remaining_count_;
1127 } else {
1128 catch_all_ = false;
1129 }
1130 Next();
1131}
1132
1133void CatchHandlerIterator::Next() {
1134 if (remaining_count_ > 0) {
1135 handler_.type_idx_ = DecodeUnsignedLeb128(&current_data_);
1136 handler_.address_ = DecodeUnsignedLeb128(&current_data_);
1137 remaining_count_--;
1138 return;
1139 }
1140
1141 if (catch_all_) {
1142 handler_.type_idx_ = DexFile::kDexNoIndex16;
1143 handler_.address_ = DecodeUnsignedLeb128(&current_data_);
1144 catch_all_ = false;
1145 return;
1146 }
1147
1148 // no more handler
1149 remaining_count_ = -1;
1150}
1151
Carl Shapiro1fb86202011-06-27 17:43:13 -07001152} // namespace art