blob: 7e09a489be5fe7676a3e60b41b520d8e346b2225 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro1fb86202011-06-27 17:43:13 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "dex_file.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070018
19#include <fcntl.h>
Brian Carlstrom1f870082011-08-23 16:02:11 -070020#include <limits.h>
Brian Carlstromb0460ea2011-07-29 10:08:05 -070021#include <stdio.h>
Ian Rogersd81871c2011-10-03 13:57:23 -070022#include <stdlib.h>
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070023#include <string.h>
Brian Carlstromb0460ea2011-07-29 10:08:05 -070024#include <sys/file.h>
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070025#include <sys/stat.h>
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070026
Elliott Hughes07ed66b2012-12-12 18:34:25 -080027#include "base/logging.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080028#include "base/stringprintf.h"
Ian Rogers0571d352011-11-03 19:51:38 -070029#include "class_linker.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070030#include "dex_file-inl.h"
jeffhao10037c82012-01-23 15:06:23 -080031#include "dex_file_verifier.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070032#include "globals.h"
Ian Rogers0571d352011-11-03 19:51:38 -070033#include "leb128.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070034#include "mirror/art_field-inl.h"
35#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "mirror/string.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070037#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070038#include "safe_map.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070039#include "thread.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070040#include "UniquePtr.h"
Ian Rogersa6724902013-09-23 09:23:37 -070041#include "utf-inl.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070042#include "utils.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070043#include "well_known_classes.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070044#include "zip_archive.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070045
46namespace art {
47
Brian Carlstromf615a612011-07-23 12:50:34 -070048const byte DexFile::kDexMagic[] = { 'd', 'e', 'x', '\n' };
49const byte DexFile::kDexMagicVersion[] = { '0', '3', '5', '\0' };
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070050
Ian Rogers8b2c0b92013-09-19 02:56:49 -070051DexFile::ClassPathEntry DexFile::FindInClassPath(const char* descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070052 const ClassPath& class_path) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070053 for (size_t i = 0; i != class_path.size(); ++i) {
54 const DexFile* dex_file = class_path[i];
55 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
56 if (dex_class_def != NULL) {
57 return ClassPathEntry(dex_file, dex_class_def);
58 }
59 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070060 // TODO: remove reinterpret_cast when issue with -std=gnu++0x host issue resolved
Brian Carlstrom7e93b502011-08-04 14:16:22 -070061 return ClassPathEntry(reinterpret_cast<const DexFile*>(NULL),
62 reinterpret_cast<const DexFile::ClassDef*>(NULL));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070063}
64
Ian Rogers8d31bbd2013-10-13 10:44:14 -070065static int OpenAndReadMagic(const char* filename, uint32_t* magic, std::string* error_msg) {
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070066 CHECK(magic != NULL);
Ian Rogers8d31bbd2013-10-13 10:44:14 -070067 int fd = open(filename, O_RDONLY, 0);
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070068 if (fd == -1) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070069 *error_msg = StringPrintf("Unable to open '%s' : %s", filename, strerror(errno));
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070070 return -1;
71 }
72 int n = TEMP_FAILURE_RETRY(read(fd, magic, sizeof(*magic)));
73 if (n != sizeof(*magic)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070074 *error_msg = StringPrintf("Failed to find magic in '%s'", filename);
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070075 return -1;
76 }
77 if (lseek(fd, 0, SEEK_SET) != 0) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070078 *error_msg = StringPrintf("Failed to seek to beginning of file '%s' : %s", filename,
79 strerror(errno));
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070080 return -1;
81 }
82 return fd;
83}
84
Ian Rogers8d31bbd2013-10-13 10:44:14 -070085bool DexFile::GetChecksum(const char* filename, uint32_t* checksum, std::string* error_msg) {
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070086 CHECK(checksum != NULL);
87 uint32_t magic;
Ian Rogers8d31bbd2013-10-13 10:44:14 -070088 int fd = OpenAndReadMagic(filename, &magic, error_msg);
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070089 if (fd == -1) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070090 DCHECK(!error_msg->empty());
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070091 return false;
92 }
93 if (IsZipMagic(magic)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070094 UniquePtr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(fd, filename, error_msg));
Brian Carlstrom5b332c82012-02-01 15:02:31 -080095 if (zip_archive.get() == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070096 *error_msg = StringPrintf("Failed to open zip archive '%s'", filename);
Brian Carlstrom5b332c82012-02-01 15:02:31 -080097 return false;
Brian Carlstrom78128a62011-09-15 17:21:19 -070098 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -080099 UniquePtr<ZipEntry> zip_entry(zip_archive->Find(kClassesDex));
100 if (zip_entry.get() == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700101 *error_msg = StringPrintf("Zip archive '%s' doesn\'t contain %s", filename, kClassesDex);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800102 return false;
103 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700104 *checksum = zip_entry->GetCrc32();
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800105 return true;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700106 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700107 if (IsDexMagic(magic)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700108 UniquePtr<const DexFile> dex_file(DexFile::OpenFile(fd, filename, false, error_msg));
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800109 if (dex_file.get() == NULL) {
110 return false;
111 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700112 *checksum = dex_file->GetHeader().checksum_;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800113 return true;
114 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700115 *error_msg = StringPrintf("Expected valid zip or dex file: '%s'", filename);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800116 return false;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700117}
118
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700119const DexFile* DexFile::Open(const char* filename,
120 const char* location,
121 std::string* error_msg) {
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700122 uint32_t magic;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700123 int fd = OpenAndReadMagic(filename, &magic, error_msg);
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700124 if (fd == -1) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700125 DCHECK(!error_msg->empty());
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700126 return NULL;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700127 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700128 if (IsZipMagic(magic)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700129 return DexFile::OpenZip(fd, location, error_msg);
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700130 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700131 if (IsDexMagic(magic)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700132 return DexFile::OpenFile(fd, location, true, error_msg);
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700133 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700134 *error_msg = StringPrintf("Expected valid zip or dex file: '%s'", filename);
135 return nullptr;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700136}
137
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800138int DexFile::GetPermissions() const {
139 if (mem_map_.get() == NULL) {
140 return 0;
141 } else {
142 return mem_map_->GetProtect();
143 }
144}
145
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200146bool DexFile::IsReadOnly() const {
147 return GetPermissions() == PROT_READ;
148}
149
Brian Carlstrome0948e12013-08-29 09:36:15 -0700150bool DexFile::EnableWrite() const {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200151 CHECK(IsReadOnly());
152 if (mem_map_.get() == NULL) {
153 return false;
154 } else {
Brian Carlstrome0948e12013-08-29 09:36:15 -0700155 return mem_map_->Protect(PROT_READ | PROT_WRITE);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200156 }
157}
158
Brian Carlstrome0948e12013-08-29 09:36:15 -0700159bool DexFile::DisableWrite() const {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200160 CHECK(!IsReadOnly());
161 if (mem_map_.get() == NULL) {
162 return false;
163 } else {
Brian Carlstrome0948e12013-08-29 09:36:15 -0700164 return mem_map_->Protect(PROT_READ);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200165 }
166}
167
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700168const DexFile* DexFile::OpenFile(int fd, const char* location, bool verify,
169 std::string* error_msg) {
170 CHECK(location != nullptr);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700171 struct stat sbuf;
172 memset(&sbuf, 0, sizeof(sbuf));
173 if (fstat(fd, &sbuf) == -1) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700174 *error_msg = StringPrintf("DexFile: fstat \'%s\' failed: %s", location, strerror(errno));
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700175 close(fd);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700176 return nullptr;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700177 }
Ian Rogers7cfb93e2012-01-17 19:46:36 -0800178 if (S_ISDIR(sbuf.st_mode)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700179 *error_msg = StringPrintf("Attempt to mmap directory '%s'", location);
180 return nullptr;
Ian Rogers7cfb93e2012-01-17 19:46:36 -0800181 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700182 size_t length = sbuf.st_size;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700183 UniquePtr<MemMap> map(MemMap::MapFile(length, PROT_READ, MAP_PRIVATE, fd, 0, location,
184 error_msg));
185 if (map.get() == nullptr) {
186 DCHECK(!error_msg->empty());
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700187 close(fd);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700188 return nullptr;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700189 }
190 close(fd);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800191
192 if (map->Size() < sizeof(DexFile::Header)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700193 *error_msg = StringPrintf(
194 "DexFile: failed to open dex file \'%s\' that is too short to have a header", location);
195 return nullptr;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800196 }
197
198 const Header* dex_header = reinterpret_cast<const Header*>(map->Begin());
199
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700200 const DexFile* dex_file = OpenMemory(location, dex_header->checksum_, map.release(), error_msg);
201 if (dex_file == nullptr) {
202 *error_msg = StringPrintf("Failed to open dex file '%s' from memory: %s", location,
203 error_msg->c_str());
204 return nullptr;
jeffhaof6174e82012-01-31 16:14:17 -0800205 }
jeffhao54c1ceb2012-02-01 11:45:32 -0800206
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700207 if (verify && !DexFileVerifier::Verify(dex_file, dex_file->Begin(), dex_file->Size(), location,
208 error_msg)) {
209 return nullptr;
jeffhao54c1ceb2012-02-01 11:45:32 -0800210 }
211
jeffhaof6174e82012-01-31 16:14:17 -0800212 return dex_file;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700213}
214
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700215const char* DexFile::kClassesDex = "classes.dex";
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700216
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700217const DexFile* DexFile::OpenZip(int fd, const std::string& location, std::string* error_msg) {
218 UniquePtr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(fd, location.c_str(), error_msg));
219 if (zip_archive.get() == nullptr) {
220 DCHECK(!error_msg->empty());
221 return nullptr;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700222 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700223 return DexFile::Open(*zip_archive.get(), location, error_msg);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800224}
225
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800226const DexFile* DexFile::OpenMemory(const std::string& location,
227 uint32_t location_checksum,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700228 MemMap* mem_map,
229 std::string* error_msg) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800230 return OpenMemory(mem_map->Begin(),
231 mem_map->Size(),
232 location,
233 location_checksum,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700234 mem_map,
235 error_msg);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800236}
237
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700238const DexFile* DexFile::Open(const ZipArchive& zip_archive, const std::string& location,
239 std::string* error_msg) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800240 CHECK(!location.empty());
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800241 UniquePtr<ZipEntry> zip_entry(zip_archive.Find(kClassesDex));
Elliott Hughes90a33692011-08-30 13:27:07 -0700242 if (zip_entry.get() == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700243 *error_msg = StringPrintf("Failed to find classes.dex within '%s'", location.c_str());
244 return nullptr;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700245 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700246 UniquePtr<MemMap> map(zip_entry->ExtractToMemMap(kClassesDex, error_msg));
Brian Carlstrom89521892011-12-07 22:05:07 -0800247 if (map.get() == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700248 *error_msg = StringPrintf("Failed to extract '%s' from '%s': %s", kClassesDex, location.c_str(),
249 error_msg->c_str());
250 return nullptr;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700251 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700252 UniquePtr<const DexFile> dex_file(OpenMemory(location, zip_entry->GetCrc32(), map.release(),
253 error_msg));
254 if (dex_file.get() == nullptr) {
255 *error_msg = StringPrintf("Failed to open dex file '%s' from memory: %s", location.c_str(),
256 error_msg->c_str());
257 return nullptr;
jeffhaof6174e82012-01-31 16:14:17 -0800258 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700259 if (!DexFileVerifier::Verify(dex_file.get(), dex_file->Begin(), dex_file->Size(),
260 location.c_str(), error_msg)) {
261 return nullptr;
jeffhao54c1ceb2012-02-01 11:45:32 -0800262 }
Brian Carlstrome0948e12013-08-29 09:36:15 -0700263 if (!dex_file->DisableWrite()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700264 *error_msg = StringPrintf("Failed to make dex file '%s' read only", location.c_str());
265 return nullptr;
Brian Carlstrome0948e12013-08-29 09:36:15 -0700266 }
267 CHECK(dex_file->IsReadOnly()) << location;
268 return dex_file.release();
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700269}
270
Brian Carlstrom89521892011-12-07 22:05:07 -0800271const DexFile* DexFile::OpenMemory(const byte* base,
jeffhaof6174e82012-01-31 16:14:17 -0800272 size_t size,
Brian Carlstrom89521892011-12-07 22:05:07 -0800273 const std::string& location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800274 uint32_t location_checksum,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700275 MemMap* mem_map, std::string* error_msg) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700276 CHECK_ALIGNED(base, 4); // various dex file structures must be word aligned
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800277 UniquePtr<DexFile> dex_file(new DexFile(base, size, location, location_checksum, mem_map));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700278 if (!dex_file->Init(error_msg)) {
279 return nullptr;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700280 } else {
Brian Carlstromf615a612011-07-23 12:50:34 -0700281 return dex_file.release();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700282 }
283}
284
Jesse Wilson6bf19152011-09-29 13:12:33 -0400285DexFile::~DexFile() {
Elliott Hughes8cef0b82011-10-11 19:24:00 -0700286 // We don't call DeleteGlobalRef on dex_object_ because we're only called by DestroyJavaVM, and
287 // that's only called after DetachCurrentThread, which means there's no JNIEnv. We could
288 // re-attach, but cleaning up these global references is not obviously useful. It's not as if
289 // the global reference table is otherwise empty!
Jesse Wilson6bf19152011-09-29 13:12:33 -0400290}
291
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700292bool DexFile::Init(std::string* error_msg) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700293 InitMembers();
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700294 if (!CheckMagicAndVersion(error_msg)) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700295 return false;
296 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700297 return true;
298}
299
Brian Carlstromf615a612011-07-23 12:50:34 -0700300void DexFile::InitMembers() {
Ian Rogers30fab402012-01-23 15:43:46 -0800301 const byte* b = begin_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700302 header_ = reinterpret_cast<const Header*>(b);
303 const Header* h = header_;
304 string_ids_ = reinterpret_cast<const StringId*>(b + h->string_ids_off_);
305 type_ids_ = reinterpret_cast<const TypeId*>(b + h->type_ids_off_);
306 field_ids_ = reinterpret_cast<const FieldId*>(b + h->field_ids_off_);
307 method_ids_ = reinterpret_cast<const MethodId*>(b + h->method_ids_off_);
308 proto_ids_ = reinterpret_cast<const ProtoId*>(b + h->proto_ids_off_);
309 class_defs_ = reinterpret_cast<const ClassDef*>(b + h->class_defs_off_);
310}
311
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700312bool DexFile::CheckMagicAndVersion(std::string* error_msg) const {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800313 CHECK(header_->magic_ != NULL) << GetLocation();
314 if (!IsMagicValid(header_->magic_)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700315 std::ostringstream oss;
316 oss << "Unrecognized magic number in " << GetLocation() << ":"
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800317 << " " << header_->magic_[0]
318 << " " << header_->magic_[1]
319 << " " << header_->magic_[2]
320 << " " << header_->magic_[3];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700321 *error_msg = oss.str();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700322 return false;
323 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800324 if (!IsVersionValid(header_->magic_)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700325 std::ostringstream oss;
326 oss << "Unrecognized version number in " << GetLocation() << ":"
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800327 << " " << header_->magic_[4]
328 << " " << header_->magic_[5]
329 << " " << header_->magic_[6]
330 << " " << header_->magic_[7];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700331 *error_msg = oss.str();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700332 return false;
333 }
334 return true;
335}
336
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800337bool DexFile::IsMagicValid(const byte* magic) {
338 return (memcmp(magic, kDexMagic, sizeof(kDexMagic)) == 0);
339}
340
341bool DexFile::IsVersionValid(const byte* magic) {
342 const byte* version = &magic[sizeof(kDexMagic)];
343 return (memcmp(version, kDexMagicVersion, sizeof(kDexMagicVersion)) == 0);
344}
345
Ian Rogersd81871c2011-10-03 13:57:23 -0700346uint32_t DexFile::GetVersion() const {
347 const char* version = reinterpret_cast<const char*>(&GetHeader().magic_[sizeof(kDexMagic)]);
348 return atoi(version);
349}
350
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700351const DexFile::ClassDef* DexFile::FindClassDef(const char* descriptor) const {
352 size_t num_class_defs = NumClassDefs();
353 if (num_class_defs == 0) {
354 return NULL;
355 }
356 const StringId* string_id = FindStringId(descriptor);
357 if (string_id == NULL) {
358 return NULL;
359 }
360 const TypeId* type_id = FindTypeId(GetIndexForStringId(*string_id));
361 if (type_id == NULL) {
362 return NULL;
363 }
364 uint16_t type_idx = GetIndexForTypeId(*type_id);
365 for (size_t i = 0; i < num_class_defs; ++i) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700366 const ClassDef& class_def = GetClassDef(i);
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700367 if (class_def.class_idx_ == type_idx) {
368 return &class_def;
369 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700370 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700371 return NULL;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700372}
373
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700374const DexFile::ClassDef* DexFile::FindClassDef(uint16_t type_idx) const {
375 size_t num_class_defs = NumClassDefs();
376 for (size_t i = 0; i < num_class_defs; ++i) {
377 const ClassDef& class_def = GetClassDef(i);
378 if (class_def.class_idx_ == type_idx) {
379 return &class_def;
380 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700381 }
382 return NULL;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700383}
384
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800385const DexFile::FieldId* DexFile::FindFieldId(const DexFile::TypeId& declaring_klass,
386 const DexFile::StringId& name,
387 const DexFile::TypeId& type) const {
388 // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx
389 const uint16_t class_idx = GetIndexForTypeId(declaring_klass);
390 const uint32_t name_idx = GetIndexForStringId(name);
391 const uint16_t type_idx = GetIndexForTypeId(type);
Ian Rogersf8582c32013-05-29 16:33:03 -0700392 int32_t lo = 0;
393 int32_t hi = NumFieldIds() - 1;
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800394 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700395 int32_t mid = (hi + lo) / 2;
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800396 const DexFile::FieldId& field = GetFieldId(mid);
397 if (class_idx > field.class_idx_) {
398 lo = mid + 1;
399 } else if (class_idx < field.class_idx_) {
400 hi = mid - 1;
401 } else {
402 if (name_idx > field.name_idx_) {
403 lo = mid + 1;
404 } else if (name_idx < field.name_idx_) {
405 hi = mid - 1;
406 } else {
407 if (type_idx > field.type_idx_) {
408 lo = mid + 1;
409 } else if (type_idx < field.type_idx_) {
410 hi = mid - 1;
411 } else {
412 return &field;
413 }
414 }
415 }
416 }
417 return NULL;
418}
419
420const DexFile::MethodId* DexFile::FindMethodId(const DexFile::TypeId& declaring_klass,
Ian Rogers0571d352011-11-03 19:51:38 -0700421 const DexFile::StringId& name,
422 const DexFile::ProtoId& signature) const {
423 // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800424 const uint16_t class_idx = GetIndexForTypeId(declaring_klass);
Ian Rogers0571d352011-11-03 19:51:38 -0700425 const uint32_t name_idx = GetIndexForStringId(name);
426 const uint16_t proto_idx = GetIndexForProtoId(signature);
Ian Rogersf8582c32013-05-29 16:33:03 -0700427 int32_t lo = 0;
428 int32_t hi = NumMethodIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700429 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700430 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700431 const DexFile::MethodId& method = GetMethodId(mid);
432 if (class_idx > method.class_idx_) {
433 lo = mid + 1;
434 } else if (class_idx < method.class_idx_) {
435 hi = mid - 1;
436 } else {
437 if (name_idx > method.name_idx_) {
438 lo = mid + 1;
439 } else if (name_idx < method.name_idx_) {
440 hi = mid - 1;
441 } else {
442 if (proto_idx > method.proto_idx_) {
443 lo = mid + 1;
444 } else if (proto_idx < method.proto_idx_) {
445 hi = mid - 1;
446 } else {
447 return &method;
448 }
449 }
450 }
451 }
452 return NULL;
453}
454
Ian Rogers637c65b2013-05-31 11:46:00 -0700455const DexFile::StringId* DexFile::FindStringId(const char* string) const {
Ian Rogersf8582c32013-05-29 16:33:03 -0700456 int32_t lo = 0;
457 int32_t hi = NumStringIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700458 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700459 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700460 const DexFile::StringId& str_id = GetStringId(mid);
Ian Rogerscf5077a2013-10-31 12:37:54 -0700461 const char* str = GetStringData(str_id);
Ian Rogers637c65b2013-05-31 11:46:00 -0700462 int compare = CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(string, str);
463 if (compare > 0) {
464 lo = mid + 1;
465 } else if (compare < 0) {
466 hi = mid - 1;
467 } else {
468 return &str_id;
469 }
470 }
471 return NULL;
472}
473
474const DexFile::StringId* DexFile::FindStringId(const uint16_t* string) const {
475 int32_t lo = 0;
476 int32_t hi = NumStringIds() - 1;
477 while (hi >= lo) {
478 int32_t mid = (hi + lo) / 2;
Ian Rogers637c65b2013-05-31 11:46:00 -0700479 const DexFile::StringId& str_id = GetStringId(mid);
Ian Rogerscf5077a2013-10-31 12:37:54 -0700480 const char* str = GetStringData(str_id);
Ian Rogers637c65b2013-05-31 11:46:00 -0700481 int compare = CompareModifiedUtf8ToUtf16AsCodePointValues(str, string);
Ian Rogers0571d352011-11-03 19:51:38 -0700482 if (compare > 0) {
483 lo = mid + 1;
484 } else if (compare < 0) {
485 hi = mid - 1;
486 } else {
487 return &str_id;
488 }
489 }
490 return NULL;
491}
492
493const DexFile::TypeId* DexFile::FindTypeId(uint32_t string_idx) const {
Ian Rogersf8582c32013-05-29 16:33:03 -0700494 int32_t lo = 0;
495 int32_t hi = NumTypeIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700496 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700497 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700498 const TypeId& type_id = GetTypeId(mid);
499 if (string_idx > type_id.descriptor_idx_) {
500 lo = mid + 1;
501 } else if (string_idx < type_id.descriptor_idx_) {
502 hi = mid - 1;
503 } else {
504 return &type_id;
505 }
506 }
507 return NULL;
508}
509
510const DexFile::ProtoId* DexFile::FindProtoId(uint16_t return_type_idx,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800511 const std::vector<uint16_t>& signature_type_idxs) const {
Ian Rogersf8582c32013-05-29 16:33:03 -0700512 int32_t lo = 0;
513 int32_t hi = NumProtoIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700514 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700515 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700516 const DexFile::ProtoId& proto = GetProtoId(mid);
517 int compare = return_type_idx - proto.return_type_idx_;
518 if (compare == 0) {
519 DexFileParameterIterator it(*this, proto);
520 size_t i = 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800521 while (it.HasNext() && i < signature_type_idxs.size() && compare == 0) {
522 compare = signature_type_idxs[i] - it.GetTypeIdx();
Ian Rogers0571d352011-11-03 19:51:38 -0700523 it.Next();
524 i++;
525 }
526 if (compare == 0) {
527 if (it.HasNext()) {
528 compare = -1;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800529 } else if (i < signature_type_idxs.size()) {
Ian Rogers0571d352011-11-03 19:51:38 -0700530 compare = 1;
531 }
532 }
533 }
534 if (compare > 0) {
535 lo = mid + 1;
536 } else if (compare < 0) {
537 hi = mid - 1;
538 } else {
539 return &proto;
540 }
541 }
542 return NULL;
543}
544
545// Given a signature place the type ids into the given vector
Ian Rogersd91d6d62013-09-25 20:26:14 -0700546bool DexFile::CreateTypeList(const StringPiece& signature, uint16_t* return_type_idx,
547 std::vector<uint16_t>* param_type_idxs) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700548 if (signature[0] != '(') {
549 return false;
550 }
551 size_t offset = 1;
552 size_t end = signature.size();
553 bool process_return = false;
554 while (offset < end) {
555 char c = signature[offset];
556 offset++;
557 if (c == ')') {
558 process_return = true;
559 continue;
560 }
Ian Rogersd91d6d62013-09-25 20:26:14 -0700561 // TODO: avoid building a string.
Ian Rogers0571d352011-11-03 19:51:38 -0700562 std::string descriptor;
563 descriptor += c;
564 while (c == '[') { // process array prefix
565 if (offset >= end) { // expect some descriptor following [
566 return false;
567 }
568 c = signature[offset];
569 offset++;
570 descriptor += c;
571 }
572 if (c == 'L') { // process type descriptors
573 do {
574 if (offset >= end) { // unexpected early termination of descriptor
575 return false;
576 }
577 c = signature[offset];
578 offset++;
579 descriptor += c;
580 } while (c != ';');
581 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700582 const DexFile::StringId* string_id = FindStringId(descriptor.c_str());
Ian Rogers0571d352011-11-03 19:51:38 -0700583 if (string_id == NULL) {
584 return false;
585 }
586 const DexFile::TypeId* type_id = FindTypeId(GetIndexForStringId(*string_id));
587 if (type_id == NULL) {
588 return false;
589 }
590 uint16_t type_idx = GetIndexForTypeId(*type_id);
591 if (!process_return) {
592 param_type_idxs->push_back(type_idx);
593 } else {
594 *return_type_idx = type_idx;
595 return offset == end; // return true if the signature had reached a sensible end
596 }
597 }
598 return false; // failed to correctly parse return type
599}
600
Ian Rogersd91d6d62013-09-25 20:26:14 -0700601const Signature DexFile::CreateSignature(const StringPiece& signature) const {
602 uint16_t return_type_idx;
603 std::vector<uint16_t> param_type_indices;
604 bool success = CreateTypeList(signature, &return_type_idx, &param_type_indices);
605 if (!success) {
606 return Signature::NoSignature();
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700607 }
Ian Rogersd91d6d62013-09-25 20:26:14 -0700608 const ProtoId* proto_id = FindProtoId(return_type_idx, param_type_indices);
609 if (proto_id == NULL) {
610 return Signature::NoSignature();
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700611 }
Ian Rogersd91d6d62013-09-25 20:26:14 -0700612 return Signature(this, *proto_id);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700613}
614
Brian Carlstromea46f952013-07-30 01:26:50 -0700615int32_t DexFile::GetLineNumFromPC(const mirror::ArtMethod* method, uint32_t rel_pc) const {
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -0700616 // For native method, lineno should be -2 to indicate it is native. Note that
617 // "line number == -2" is how libcore tells from StackTraceElement.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700618 if (method->GetCodeItemOffset() == 0) {
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -0700619 return -2;
620 }
621
TDYa127c8dc1012012-04-19 07:03:33 -0700622 const CodeItem* code_item = GetCodeItem(method->GetCodeItemOffset());
Elliott Hughescaf76542012-06-28 16:08:22 -0700623 DCHECK(code_item != NULL) << PrettyMethod(method) << " " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700624
625 // A method with no line number info should return -1
626 LineNumFromPcContext context(rel_pc, -1);
TDYa127c8dc1012012-04-19 07:03:33 -0700627 DecodeDebugInfo(code_item, method->IsStatic(), method->GetDexMethodIndex(), LineNumForPcCb,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800628 NULL, &context);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700629 return context.line_num_;
630}
631
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700632int32_t DexFile::FindTryItem(const CodeItem &code_item, uint32_t address) {
Ian Rogers0571d352011-11-03 19:51:38 -0700633 // Note: Signed type is important for max and min.
634 int32_t min = 0;
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700635 int32_t max = code_item.tries_size_ - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700636
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700637 while (min <= max) {
638 int32_t mid = min + ((max - min) / 2);
639
640 const art::DexFile::TryItem* ti = GetTryItems(code_item, mid);
641 uint32_t start = ti->start_addr_;
642 uint32_t end = start + ti->insn_count_;
643
Ian Rogers0571d352011-11-03 19:51:38 -0700644 if (address < start) {
645 max = mid - 1;
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700646 } else if (address >= end) {
647 min = mid + 1;
648 } else { // We have a winner!
649 return mid;
Ian Rogers0571d352011-11-03 19:51:38 -0700650 }
651 }
652 // No match.
653 return -1;
654}
655
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700656int32_t DexFile::FindCatchHandlerOffset(const CodeItem &code_item, uint32_t address) {
657 int32_t try_item = FindTryItem(code_item, address);
658 if (try_item == -1) {
659 return -1;
660 } else {
661 return DexFile::GetTryItems(code_item, try_item)->handler_off_;
662 }
663}
664
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800665void DexFile::DecodeDebugInfo0(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes2435a572012-02-17 16:07:41 -0800666 DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb,
667 void* context, const byte* stream, LocalInfo* local_in_reg) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700668 uint32_t line = DecodeUnsignedLeb128(&stream);
669 uint32_t parameters_size = DecodeUnsignedLeb128(&stream);
670 uint16_t arg_reg = code_item->registers_size_ - code_item->ins_size_;
671 uint32_t address = 0;
Elliott Hughes30646832011-10-13 16:59:46 -0700672 bool need_locals = (local_cb != NULL);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700673
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800674 if (!is_static) {
Elliott Hughes30646832011-10-13 16:59:46 -0700675 if (need_locals) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800676 const char* descriptor = GetMethodDeclaringClassDescriptor(GetMethodId(method_idx));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700677 local_in_reg[arg_reg].name_ = "this";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800678 local_in_reg[arg_reg].descriptor_ = descriptor;
Elliott Hughes392b1242011-11-30 13:55:50 -0800679 local_in_reg[arg_reg].signature_ = NULL;
Elliott Hughes30646832011-10-13 16:59:46 -0700680 local_in_reg[arg_reg].start_address_ = 0;
681 local_in_reg[arg_reg].is_live_ = true;
682 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700683 arg_reg++;
684 }
685
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800686 DexFileParameterIterator it(*this, GetMethodPrototype(GetMethodId(method_idx)));
Ian Rogers0571d352011-11-03 19:51:38 -0700687 for (uint32_t i = 0; i < parameters_size && it.HasNext(); ++i, it.Next()) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700688 if (arg_reg >= code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700689 LOG(ERROR) << "invalid stream - arg reg >= reg size (" << arg_reg
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800690 << " >= " << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700691 return;
692 }
Elliott Hughes392b1242011-11-30 13:55:50 -0800693 uint32_t id = DecodeUnsignedLeb128P1(&stream);
Ian Rogers0571d352011-11-03 19:51:38 -0700694 const char* descriptor = it.GetDescriptor();
Elliott Hughes392b1242011-11-30 13:55:50 -0800695 if (need_locals && id != kDexNoIndex) {
Ian Rogers0571d352011-11-03 19:51:38 -0700696 const char* name = StringDataByIdx(id);
Elliott Hughes30646832011-10-13 16:59:46 -0700697 local_in_reg[arg_reg].name_ = name;
698 local_in_reg[arg_reg].descriptor_ = descriptor;
Elliott Hughes392b1242011-11-30 13:55:50 -0800699 local_in_reg[arg_reg].signature_ = NULL;
Elliott Hughes30646832011-10-13 16:59:46 -0700700 local_in_reg[arg_reg].start_address_ = address;
701 local_in_reg[arg_reg].is_live_ = true;
702 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700703 switch (*descriptor) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700704 case 'D':
705 case 'J':
706 arg_reg += 2;
707 break;
708 default:
709 arg_reg += 1;
710 break;
711 }
712 }
713
Ian Rogers0571d352011-11-03 19:51:38 -0700714 if (it.HasNext()) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800715 LOG(ERROR) << "invalid stream - problem with parameter iterator in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700716 return;
717 }
718
719 for (;;) {
720 uint8_t opcode = *stream++;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700721 uint16_t reg;
jeffhaof8728872011-10-28 19:11:13 -0700722 uint16_t name_idx;
723 uint16_t descriptor_idx;
724 uint16_t signature_idx = 0;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700725
Shih-wei Liao195487c2011-08-20 13:29:04 -0700726 switch (opcode) {
727 case DBG_END_SEQUENCE:
728 return;
729
730 case DBG_ADVANCE_PC:
731 address += DecodeUnsignedLeb128(&stream);
732 break;
733
734 case DBG_ADVANCE_LINE:
Shih-wei Liao8a05d272011-10-15 18:45:43 -0700735 line += DecodeSignedLeb128(&stream);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700736 break;
737
738 case DBG_START_LOCAL:
739 case DBG_START_LOCAL_EXTENDED:
740 reg = DecodeUnsignedLeb128(&stream);
741 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700742 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800743 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700744 return;
745 }
746
jeffhaof8728872011-10-28 19:11:13 -0700747 name_idx = DecodeUnsignedLeb128P1(&stream);
748 descriptor_idx = DecodeUnsignedLeb128P1(&stream);
749 if (opcode == DBG_START_LOCAL_EXTENDED) {
750 signature_idx = DecodeUnsignedLeb128P1(&stream);
751 }
752
Shih-wei Liao195487c2011-08-20 13:29:04 -0700753 // Emit what was previously there, if anything
Elliott Hughes30646832011-10-13 16:59:46 -0700754 if (need_locals) {
Elliott Hughes2435a572012-02-17 16:07:41 -0800755 InvokeLocalCbIfLive(context, reg, address, local_in_reg, local_cb);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700756
Ian Rogers0571d352011-11-03 19:51:38 -0700757 local_in_reg[reg].name_ = StringDataByIdx(name_idx);
758 local_in_reg[reg].descriptor_ = StringByTypeIdx(descriptor_idx);
Elliott Hughes30646832011-10-13 16:59:46 -0700759 if (opcode == DBG_START_LOCAL_EXTENDED) {
Ian Rogers0571d352011-11-03 19:51:38 -0700760 local_in_reg[reg].signature_ = StringDataByIdx(signature_idx);
Elliott Hughes30646832011-10-13 16:59:46 -0700761 }
762 local_in_reg[reg].start_address_ = address;
763 local_in_reg[reg].is_live_ = true;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700764 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700765 break;
766
767 case DBG_END_LOCAL:
768 reg = DecodeUnsignedLeb128(&stream);
769 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700770 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800771 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700772 return;
773 }
774
Elliott Hughes30646832011-10-13 16:59:46 -0700775 if (need_locals) {
Elliott Hughes2435a572012-02-17 16:07:41 -0800776 InvokeLocalCbIfLive(context, reg, address, local_in_reg, local_cb);
Elliott Hughes30646832011-10-13 16:59:46 -0700777 local_in_reg[reg].is_live_ = false;
778 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700779 break;
780
781 case DBG_RESTART_LOCAL:
782 reg = DecodeUnsignedLeb128(&stream);
783 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700784 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800785 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700786 return;
787 }
788
Elliott Hughes30646832011-10-13 16:59:46 -0700789 if (need_locals) {
790 if (local_in_reg[reg].name_ == NULL || local_in_reg[reg].descriptor_ == NULL) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800791 LOG(ERROR) << "invalid stream - no name or descriptor in " << GetLocation();
Elliott Hughes30646832011-10-13 16:59:46 -0700792 return;
793 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700794
Elliott Hughes30646832011-10-13 16:59:46 -0700795 // If the register is live, the "restart" is superfluous,
796 // and we don't want to mess with the existing start address.
797 if (!local_in_reg[reg].is_live_) {
798 local_in_reg[reg].start_address_ = address;
799 local_in_reg[reg].is_live_ = true;
800 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700801 }
802 break;
803
804 case DBG_SET_PROLOGUE_END:
805 case DBG_SET_EPILOGUE_BEGIN:
806 case DBG_SET_FILE:
807 break;
808
Shih-wei Liao8e1b4ff2011-10-15 15:43:51 -0700809 default: {
810 int adjopcode = opcode - DBG_FIRST_SPECIAL;
811
Shih-wei Liao195487c2011-08-20 13:29:04 -0700812 address += adjopcode / DBG_LINE_RANGE;
813 line += DBG_LINE_BASE + (adjopcode % DBG_LINE_RANGE);
814
Elliott Hughes2435a572012-02-17 16:07:41 -0800815 if (position_cb != NULL) {
816 if (position_cb(context, address, line)) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700817 // early exit
818 return;
819 }
820 }
821 break;
Shih-wei Liao8e1b4ff2011-10-15 15:43:51 -0700822 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700823 }
824 }
825}
826
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800827void DexFile::DecodeDebugInfo(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes2435a572012-02-17 16:07:41 -0800828 DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb,
829 void* context) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700830 const byte* stream = GetDebugInfoStream(code_item);
Brian Carlstrome0948e12013-08-29 09:36:15 -0700831 UniquePtr<LocalInfo[]> local_in_reg(local_cb != NULL ?
832 new LocalInfo[code_item->registers_size_] :
833 NULL);
Ian Rogers0571d352011-11-03 19:51:38 -0700834 if (stream != NULL) {
Elliott Hughesee0fa762012-03-26 17:12:41 -0700835 DecodeDebugInfo0(code_item, is_static, method_idx, position_cb, local_cb, context, stream, &local_in_reg[0]);
Ian Rogers0571d352011-11-03 19:51:38 -0700836 }
837 for (int reg = 0; reg < code_item->registers_size_; reg++) {
Elliott Hughesee0fa762012-03-26 17:12:41 -0700838 InvokeLocalCbIfLive(context, reg, code_item->insns_size_in_code_units_, &local_in_reg[0], local_cb);
Ian Rogers0571d352011-11-03 19:51:38 -0700839 }
840}
841
Elliott Hughes2435a572012-02-17 16:07:41 -0800842bool DexFile::LineNumForPcCb(void* raw_context, uint32_t address, uint32_t line_num) {
843 LineNumFromPcContext* context = reinterpret_cast<LineNumFromPcContext*>(raw_context);
Ian Rogers0571d352011-11-03 19:51:38 -0700844
845 // We know that this callback will be called in
846 // ascending address order, so keep going until we find
847 // a match or we've just gone past it.
848 if (address > context->address_) {
849 // The line number from the previous positions callback
850 // wil be the final result.
851 return true;
852 } else {
853 context->line_num_ = line_num;
854 return address == context->address_;
855 }
856}
857
Ian Rogersd91d6d62013-09-25 20:26:14 -0700858std::string Signature::ToString() const {
859 if (dex_file_ == nullptr) {
860 CHECK(proto_id_ == nullptr);
861 return "<no signature>";
862 }
863 const DexFile::TypeList* params = dex_file_->GetProtoParameters(*proto_id_);
864 std::string result;
865 if (params == nullptr) {
866 result += "()";
867 } else {
868 result += "(";
869 for (uint32_t i = 0; i < params->Size(); ++i) {
870 result += dex_file_->StringByTypeIdx(params->GetTypeItem(i).type_idx_);
871 }
872 result += ")";
873 }
874 result += dex_file_->StringByTypeIdx(proto_id_->return_type_idx_);
875 return result;
876}
877
878std::ostream& operator<<(std::ostream& os, const Signature& sig) {
879 return os << sig.ToString();
880}
881
Ian Rogers0571d352011-11-03 19:51:38 -0700882// Decodes the header section from the class data bytes.
883void ClassDataItemIterator::ReadClassDataHeader() {
884 CHECK(ptr_pos_ != NULL);
885 header_.static_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_);
886 header_.instance_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_);
887 header_.direct_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_);
888 header_.virtual_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_);
889}
890
891void ClassDataItemIterator::ReadClassDataField() {
892 field_.field_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_);
893 field_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_);
Brian Carlstrom68adbe42012-05-11 17:18:08 -0700894 if (last_idx_ != 0 && field_.field_idx_delta_ == 0) {
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -0700895 LOG(WARNING) << "Duplicate field " << PrettyField(GetMemberIndex(), dex_file_)
896 << " in " << dex_file_.GetLocation();
897 }
Ian Rogers0571d352011-11-03 19:51:38 -0700898}
899
900void ClassDataItemIterator::ReadClassDataMethod() {
901 method_.method_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_);
902 method_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_);
903 method_.code_off_ = DecodeUnsignedLeb128(&ptr_pos_);
Brian Carlstrom68adbe42012-05-11 17:18:08 -0700904 if (last_idx_ != 0 && method_.method_idx_delta_ == 0) {
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -0700905 LOG(WARNING) << "Duplicate method " << PrettyMethod(GetMemberIndex(), dex_file_)
906 << " in " << dex_file_.GetLocation();
907 }
Ian Rogers0571d352011-11-03 19:51:38 -0700908}
909
910// Read a signed integer. "zwidth" is the zero-based byte count.
911static int32_t ReadSignedInt(const byte* ptr, int zwidth) {
912 int32_t val = 0;
913 for (int i = zwidth; i >= 0; --i) {
914 val = ((uint32_t)val >> 8) | (((int32_t)*ptr++) << 24);
915 }
916 val >>= (3 - zwidth) * 8;
917 return val;
918}
919
920// Read an unsigned integer. "zwidth" is the zero-based byte count,
921// "fill_on_right" indicates which side we want to zero-fill from.
922static uint32_t ReadUnsignedInt(const byte* ptr, int zwidth, bool fill_on_right) {
923 uint32_t val = 0;
924 if (!fill_on_right) {
925 for (int i = zwidth; i >= 0; --i) {
926 val = (val >> 8) | (((uint32_t)*ptr++) << 24);
927 }
928 val >>= (3 - zwidth) * 8;
929 } else {
930 for (int i = zwidth; i >= 0; --i) {
931 val = (val >> 8) | (((uint32_t)*ptr++) << 24);
932 }
933 }
934 return val;
935}
936
937// Read a signed long. "zwidth" is the zero-based byte count.
938static int64_t ReadSignedLong(const byte* ptr, int zwidth) {
939 int64_t val = 0;
940 for (int i = zwidth; i >= 0; --i) {
941 val = ((uint64_t)val >> 8) | (((int64_t)*ptr++) << 56);
942 }
943 val >>= (7 - zwidth) * 8;
944 return val;
945}
946
947// Read an unsigned long. "zwidth" is the zero-based byte count,
948// "fill_on_right" indicates which side we want to zero-fill from.
949static uint64_t ReadUnsignedLong(const byte* ptr, int zwidth, bool fill_on_right) {
950 uint64_t val = 0;
951 if (!fill_on_right) {
952 for (int i = zwidth; i >= 0; --i) {
953 val = (val >> 8) | (((uint64_t)*ptr++) << 56);
954 }
955 val >>= (7 - zwidth) * 8;
956 } else {
957 for (int i = zwidth; i >= 0; --i) {
958 val = (val >> 8) | (((uint64_t)*ptr++) << 56);
959 }
960 }
961 return val;
962}
963
964EncodedStaticFieldValueIterator::EncodedStaticFieldValueIterator(const DexFile& dex_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800965 mirror::DexCache* dex_cache,
966 mirror::ClassLoader* class_loader,
Ian Rogersca190662012-06-26 15:45:57 -0700967 ClassLinker* linker,
968 const DexFile::ClassDef& class_def)
Brian Carlstrom88f36542012-10-16 23:24:21 -0700969 : dex_file_(dex_file), dex_cache_(dex_cache), class_loader_(class_loader), linker_(linker),
970 array_size_(), pos_(-1), type_(kByte) {
Ian Rogers0571d352011-11-03 19:51:38 -0700971 ptr_ = dex_file.GetEncodedStaticFieldValuesArray(class_def);
972 if (ptr_ == NULL) {
973 array_size_ = 0;
974 } else {
975 array_size_ = DecodeUnsignedLeb128(&ptr_);
976 }
977 if (array_size_ > 0) {
978 Next();
979 }
980}
981
982void EncodedStaticFieldValueIterator::Next() {
983 pos_++;
984 if (pos_ >= array_size_) {
985 return;
986 }
987 byte value_type = *ptr_++;
988 byte value_arg = value_type >> kEncodedValueArgShift;
989 size_t width = value_arg + 1; // assume and correct later
Brian Carlstrom88f36542012-10-16 23:24:21 -0700990 type_ = static_cast<ValueType>(value_type & kEncodedValueTypeMask);
Ian Rogers0571d352011-11-03 19:51:38 -0700991 switch (type_) {
992 case kBoolean:
993 jval_.i = (value_arg != 0) ? 1 : 0;
994 width = 0;
995 break;
996 case kByte:
997 jval_.i = ReadSignedInt(ptr_, value_arg);
998 CHECK(IsInt(8, jval_.i));
999 break;
1000 case kShort:
1001 jval_.i = ReadSignedInt(ptr_, value_arg);
1002 CHECK(IsInt(16, jval_.i));
1003 break;
1004 case kChar:
1005 jval_.i = ReadUnsignedInt(ptr_, value_arg, false);
1006 CHECK(IsUint(16, jval_.i));
1007 break;
1008 case kInt:
1009 jval_.i = ReadSignedInt(ptr_, value_arg);
1010 break;
1011 case kLong:
1012 jval_.j = ReadSignedLong(ptr_, value_arg);
1013 break;
1014 case kFloat:
1015 jval_.i = ReadUnsignedInt(ptr_, value_arg, true);
1016 break;
1017 case kDouble:
1018 jval_.j = ReadUnsignedLong(ptr_, value_arg, true);
1019 break;
1020 case kString:
1021 case kType:
Ian Rogers0571d352011-11-03 19:51:38 -07001022 jval_.i = ReadUnsignedInt(ptr_, value_arg, false);
1023 break;
1024 case kField:
Brian Carlstrom88f36542012-10-16 23:24:21 -07001025 case kMethod:
1026 case kEnum:
Ian Rogers0571d352011-11-03 19:51:38 -07001027 case kArray:
1028 case kAnnotation:
1029 UNIMPLEMENTED(FATAL) << ": type " << type_;
1030 break;
1031 case kNull:
1032 jval_.l = NULL;
1033 width = 0;
1034 break;
1035 default:
1036 LOG(FATAL) << "Unreached";
1037 }
1038 ptr_ += width;
1039}
1040
Brian Carlstromea46f952013-07-30 01:26:50 -07001041void EncodedStaticFieldValueIterator::ReadValueToField(mirror::ArtField* field) const {
Ian Rogers0571d352011-11-03 19:51:38 -07001042 switch (type_) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001043 case kBoolean: field->SetBoolean(field->GetDeclaringClass(), jval_.z); break;
1044 case kByte: field->SetByte(field->GetDeclaringClass(), jval_.b); break;
1045 case kShort: field->SetShort(field->GetDeclaringClass(), jval_.s); break;
1046 case kChar: field->SetChar(field->GetDeclaringClass(), jval_.c); break;
1047 case kInt: field->SetInt(field->GetDeclaringClass(), jval_.i); break;
1048 case kLong: field->SetLong(field->GetDeclaringClass(), jval_.j); break;
1049 case kFloat: field->SetFloat(field->GetDeclaringClass(), jval_.f); break;
1050 case kDouble: field->SetDouble(field->GetDeclaringClass(), jval_.d); break;
1051 case kNull: field->SetObject(field->GetDeclaringClass(), NULL); break;
Ian Rogers0571d352011-11-03 19:51:38 -07001052 case kString: {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001053 mirror::String* resolved = linker_->ResolveString(dex_file_, jval_.i, dex_cache_);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001054 field->SetObject(field->GetDeclaringClass(), resolved);
Ian Rogers0571d352011-11-03 19:51:38 -07001055 break;
1056 }
Brian Carlstrom88f36542012-10-16 23:24:21 -07001057 case kType: {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001058 mirror::Class* resolved = linker_->ResolveType(dex_file_, jval_.i, dex_cache_, class_loader_);
mikaelpeltierebf6aa82012-11-07 14:02:15 +01001059 field->SetObject(field->GetDeclaringClass(), resolved);
Brian Carlstrom88f36542012-10-16 23:24:21 -07001060 break;
1061 }
Ian Rogers0571d352011-11-03 19:51:38 -07001062 default: UNIMPLEMENTED(FATAL) << ": type " << type_;
1063 }
1064}
1065
1066CatchHandlerIterator::CatchHandlerIterator(const DexFile::CodeItem& code_item, uint32_t address) {
1067 handler_.address_ = -1;
1068 int32_t offset = -1;
1069
1070 // Short-circuit the overwhelmingly common cases.
1071 switch (code_item.tries_size_) {
1072 case 0:
1073 break;
1074 case 1: {
1075 const DexFile::TryItem* tries = DexFile::GetTryItems(code_item, 0);
1076 uint32_t start = tries->start_addr_;
1077 if (address >= start) {
1078 uint32_t end = start + tries->insn_count_;
1079 if (address < end) {
1080 offset = tries->handler_off_;
1081 }
1082 }
1083 break;
1084 }
1085 default:
Ian Rogersdbbc99d2013-04-18 16:51:54 -07001086 offset = DexFile::FindCatchHandlerOffset(code_item, address);
Ian Rogers0571d352011-11-03 19:51:38 -07001087 }
Logan Chien736df022012-04-27 16:25:57 +08001088 Init(code_item, offset);
1089}
1090
1091CatchHandlerIterator::CatchHandlerIterator(const DexFile::CodeItem& code_item,
1092 const DexFile::TryItem& try_item) {
1093 handler_.address_ = -1;
1094 Init(code_item, try_item.handler_off_);
1095}
1096
1097void CatchHandlerIterator::Init(const DexFile::CodeItem& code_item,
1098 int32_t offset) {
Ian Rogers0571d352011-11-03 19:51:38 -07001099 if (offset >= 0) {
Logan Chien736df022012-04-27 16:25:57 +08001100 Init(DexFile::GetCatchHandlerData(code_item, offset));
Ian Rogers0571d352011-11-03 19:51:38 -07001101 } else {
1102 // Not found, initialize as empty
1103 current_data_ = NULL;
1104 remaining_count_ = -1;
1105 catch_all_ = false;
1106 DCHECK(!HasNext());
1107 }
1108}
1109
1110void CatchHandlerIterator::Init(const byte* handler_data) {
1111 current_data_ = handler_data;
1112 remaining_count_ = DecodeSignedLeb128(&current_data_);
1113
1114 // If remaining_count_ is non-positive, then it is the negative of
1115 // the number of catch types, and the catches are followed by a
1116 // catch-all handler.
1117 if (remaining_count_ <= 0) {
1118 catch_all_ = true;
1119 remaining_count_ = -remaining_count_;
1120 } else {
1121 catch_all_ = false;
1122 }
1123 Next();
1124}
1125
1126void CatchHandlerIterator::Next() {
1127 if (remaining_count_ > 0) {
1128 handler_.type_idx_ = DecodeUnsignedLeb128(&current_data_);
1129 handler_.address_ = DecodeUnsignedLeb128(&current_data_);
1130 remaining_count_--;
1131 return;
1132 }
1133
1134 if (catch_all_) {
1135 handler_.type_idx_ = DexFile::kDexNoIndex16;
1136 handler_.address_ = DecodeUnsignedLeb128(&current_data_);
1137 catch_all_ = false;
1138 return;
1139 }
1140
1141 // no more handler
1142 remaining_count_ = -1;
1143}
1144
Carl Shapiro1fb86202011-06-27 17:43:13 -07001145} // namespace art