blob: a897cce2e0a8e0e25b4e1d582e33c49160ef3c59 [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"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070040#include "thread.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070041#include "UniquePtr.h"
Ian Rogersa6724902013-09-23 09:23:37 -070042#include "utf-inl.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070043#include "utils.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070044#include "well_known_classes.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070045#include "zip_archive.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070046
47namespace art {
48
Brian Carlstromf615a612011-07-23 12:50:34 -070049const byte DexFile::kDexMagic[] = { 'd', 'e', 'x', '\n' };
50const byte DexFile::kDexMagicVersion[] = { '0', '3', '5', '\0' };
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070051
Ian Rogers8b2c0b92013-09-19 02:56:49 -070052DexFile::ClassPathEntry DexFile::FindInClassPath(const char* descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070053 const ClassPath& class_path) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070054 for (size_t i = 0; i != class_path.size(); ++i) {
55 const DexFile* dex_file = class_path[i];
56 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
57 if (dex_class_def != NULL) {
58 return ClassPathEntry(dex_file, dex_class_def);
59 }
60 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070061 // TODO: remove reinterpret_cast when issue with -std=gnu++0x host issue resolved
Brian Carlstrom7e93b502011-08-04 14:16:22 -070062 return ClassPathEntry(reinterpret_cast<const DexFile*>(NULL),
63 reinterpret_cast<const DexFile::ClassDef*>(NULL));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070064}
65
Ian Rogers8d31bbd2013-10-13 10:44:14 -070066static int OpenAndReadMagic(const char* filename, uint32_t* magic, std::string* error_msg) {
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070067 CHECK(magic != NULL);
Vladimir Markofd995762013-11-06 16:36:36 +000068 ScopedFd fd(open(filename, O_RDONLY, 0));
69 if (fd.get() == -1) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070070 *error_msg = StringPrintf("Unable to open '%s' : %s", filename, strerror(errno));
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070071 return -1;
72 }
Vladimir Markofd995762013-11-06 16:36:36 +000073 int n = TEMP_FAILURE_RETRY(read(fd.get(), magic, sizeof(*magic)));
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070074 if (n != sizeof(*magic)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070075 *error_msg = StringPrintf("Failed to find magic in '%s'", filename);
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070076 return -1;
77 }
Vladimir Markofd995762013-11-06 16:36:36 +000078 if (lseek(fd.get(), 0, SEEK_SET) != 0) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070079 *error_msg = StringPrintf("Failed to seek to beginning of file '%s' : %s", filename,
80 strerror(errno));
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070081 return -1;
82 }
Vladimir Markofd995762013-11-06 16:36:36 +000083 return fd.release();
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070084}
85
Ian Rogers8d31bbd2013-10-13 10:44:14 -070086bool DexFile::GetChecksum(const char* filename, uint32_t* checksum, std::string* error_msg) {
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070087 CHECK(checksum != NULL);
88 uint32_t magic;
Vladimir Markofd995762013-11-06 16:36:36 +000089 ScopedFd fd(OpenAndReadMagic(filename, &magic, error_msg));
90 if (fd.get() == -1) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070091 DCHECK(!error_msg->empty());
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070092 return false;
93 }
94 if (IsZipMagic(magic)) {
Vladimir Markofd995762013-11-06 16:36:36 +000095 UniquePtr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(fd.release(), filename, error_msg));
Brian Carlstrom5b332c82012-02-01 15:02:31 -080096 if (zip_archive.get() == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070097 *error_msg = StringPrintf("Failed to open zip archive '%s'", filename);
Brian Carlstrom5b332c82012-02-01 15:02:31 -080098 return false;
Brian Carlstrom78128a62011-09-15 17:21:19 -070099 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800100 UniquePtr<ZipEntry> zip_entry(zip_archive->Find(kClassesDex));
101 if (zip_entry.get() == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700102 *error_msg = StringPrintf("Zip archive '%s' doesn\'t contain %s", filename, kClassesDex);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800103 return false;
104 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700105 *checksum = zip_entry->GetCrc32();
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800106 return true;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700107 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700108 if (IsDexMagic(magic)) {
Vladimir Markofd995762013-11-06 16:36:36 +0000109 UniquePtr<const DexFile> dex_file(DexFile::OpenFile(fd.release(), filename, false, error_msg));
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800110 if (dex_file.get() == NULL) {
111 return false;
112 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700113 *checksum = dex_file->GetHeader().checksum_;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800114 return true;
115 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700116 *error_msg = StringPrintf("Expected valid zip or dex file: '%s'", filename);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800117 return false;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700118}
119
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700120const DexFile* DexFile::Open(const char* filename,
121 const char* location,
122 std::string* error_msg) {
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700123 uint32_t magic;
Vladimir Markofd995762013-11-06 16:36:36 +0000124 ScopedFd fd(OpenAndReadMagic(filename, &magic, error_msg));
125 if (fd.get() == -1) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700126 DCHECK(!error_msg->empty());
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700127 return NULL;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700128 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700129 if (IsZipMagic(magic)) {
Vladimir Markofd995762013-11-06 16:36:36 +0000130 return DexFile::OpenZip(fd.release(), location, error_msg);
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700131 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700132 if (IsDexMagic(magic)) {
Vladimir Markofd995762013-11-06 16:36:36 +0000133 return DexFile::OpenFile(fd.release(), location, true, error_msg);
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700134 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700135 *error_msg = StringPrintf("Expected valid zip or dex file: '%s'", filename);
136 return nullptr;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700137}
138
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800139int DexFile::GetPermissions() const {
140 if (mem_map_.get() == NULL) {
141 return 0;
142 } else {
143 return mem_map_->GetProtect();
144 }
145}
146
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200147bool DexFile::IsReadOnly() const {
148 return GetPermissions() == PROT_READ;
149}
150
Brian Carlstrome0948e12013-08-29 09:36:15 -0700151bool DexFile::EnableWrite() const {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200152 CHECK(IsReadOnly());
153 if (mem_map_.get() == NULL) {
154 return false;
155 } else {
Brian Carlstrome0948e12013-08-29 09:36:15 -0700156 return mem_map_->Protect(PROT_READ | PROT_WRITE);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200157 }
158}
159
Brian Carlstrome0948e12013-08-29 09:36:15 -0700160bool DexFile::DisableWrite() const {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200161 CHECK(!IsReadOnly());
162 if (mem_map_.get() == NULL) {
163 return false;
164 } else {
Brian Carlstrome0948e12013-08-29 09:36:15 -0700165 return mem_map_->Protect(PROT_READ);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200166 }
167}
168
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700169const DexFile* DexFile::OpenFile(int fd, const char* location, bool verify,
170 std::string* error_msg) {
171 CHECK(location != nullptr);
Vladimir Markofd995762013-11-06 16:36:36 +0000172 UniquePtr<MemMap> map;
173 {
174 ScopedFd delayed_close(fd);
175 struct stat sbuf;
176 memset(&sbuf, 0, sizeof(sbuf));
177 if (fstat(fd, &sbuf) == -1) {
178 *error_msg = StringPrintf("DexFile: fstat \'%s\' failed: %s", location, strerror(errno));
179 return nullptr;
180 }
181 if (S_ISDIR(sbuf.st_mode)) {
182 *error_msg = StringPrintf("Attempt to mmap directory '%s'", location);
183 return nullptr;
184 }
185 size_t length = sbuf.st_size;
186 map.reset(MemMap::MapFile(length, PROT_READ, MAP_PRIVATE, fd, 0, location, error_msg));
187 if (map.get() == nullptr) {
188 DCHECK(!error_msg->empty());
189 return nullptr;
190 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700191 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800192
193 if (map->Size() < sizeof(DexFile::Header)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700194 *error_msg = StringPrintf(
195 "DexFile: failed to open dex file \'%s\' that is too short to have a header", location);
196 return nullptr;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800197 }
198
199 const Header* dex_header = reinterpret_cast<const Header*>(map->Begin());
200
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700201 const DexFile* dex_file = OpenMemory(location, dex_header->checksum_, map.release(), error_msg);
202 if (dex_file == nullptr) {
203 *error_msg = StringPrintf("Failed to open dex file '%s' from memory: %s", location,
204 error_msg->c_str());
205 return nullptr;
jeffhaof6174e82012-01-31 16:14:17 -0800206 }
jeffhao54c1ceb2012-02-01 11:45:32 -0800207
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700208 if (verify && !DexFileVerifier::Verify(dex_file, dex_file->Begin(), dex_file->Size(), location,
209 error_msg)) {
210 return nullptr;
jeffhao54c1ceb2012-02-01 11:45:32 -0800211 }
212
jeffhaof6174e82012-01-31 16:14:17 -0800213 return dex_file;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700214}
215
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700216const char* DexFile::kClassesDex = "classes.dex";
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700217
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700218const DexFile* DexFile::OpenZip(int fd, const std::string& location, std::string* error_msg) {
219 UniquePtr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(fd, location.c_str(), error_msg));
220 if (zip_archive.get() == nullptr) {
221 DCHECK(!error_msg->empty());
222 return nullptr;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700223 }
Vladimir Markofd995762013-11-06 16:36:36 +0000224 return DexFile::Open(*zip_archive, location, error_msg);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800225}
226
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800227const DexFile* DexFile::OpenMemory(const std::string& location,
228 uint32_t location_checksum,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700229 MemMap* mem_map,
230 std::string* error_msg) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800231 return OpenMemory(mem_map->Begin(),
232 mem_map->Size(),
233 location,
234 location_checksum,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700235 mem_map,
236 error_msg);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800237}
238
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700239const DexFile* DexFile::Open(const ZipArchive& zip_archive, const std::string& location,
240 std::string* error_msg) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800241 CHECK(!location.empty());
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800242 UniquePtr<ZipEntry> zip_entry(zip_archive.Find(kClassesDex));
Elliott Hughes90a33692011-08-30 13:27:07 -0700243 if (zip_entry.get() == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700244 *error_msg = StringPrintf("Failed to find classes.dex within '%s'", location.c_str());
245 return nullptr;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700246 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700247 UniquePtr<MemMap> map(zip_entry->ExtractToMemMap(kClassesDex, error_msg));
Brian Carlstrom89521892011-12-07 22:05:07 -0800248 if (map.get() == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700249 *error_msg = StringPrintf("Failed to extract '%s' from '%s': %s", kClassesDex, location.c_str(),
250 error_msg->c_str());
251 return nullptr;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700252 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700253 UniquePtr<const DexFile> dex_file(OpenMemory(location, zip_entry->GetCrc32(), map.release(),
254 error_msg));
255 if (dex_file.get() == nullptr) {
256 *error_msg = StringPrintf("Failed to open dex file '%s' from memory: %s", location.c_str(),
257 error_msg->c_str());
258 return nullptr;
jeffhaof6174e82012-01-31 16:14:17 -0800259 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700260 if (!DexFileVerifier::Verify(dex_file.get(), dex_file->Begin(), dex_file->Size(),
261 location.c_str(), error_msg)) {
262 return nullptr;
jeffhao54c1ceb2012-02-01 11:45:32 -0800263 }
Brian Carlstrome0948e12013-08-29 09:36:15 -0700264 if (!dex_file->DisableWrite()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700265 *error_msg = StringPrintf("Failed to make dex file '%s' read only", location.c_str());
266 return nullptr;
Brian Carlstrome0948e12013-08-29 09:36:15 -0700267 }
268 CHECK(dex_file->IsReadOnly()) << location;
269 return dex_file.release();
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700270}
271
Brian Carlstrom89521892011-12-07 22:05:07 -0800272const DexFile* DexFile::OpenMemory(const byte* base,
jeffhaof6174e82012-01-31 16:14:17 -0800273 size_t size,
Brian Carlstrom89521892011-12-07 22:05:07 -0800274 const std::string& location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800275 uint32_t location_checksum,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700276 MemMap* mem_map, std::string* error_msg) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700277 CHECK_ALIGNED(base, 4); // various dex file structures must be word aligned
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800278 UniquePtr<DexFile> dex_file(new DexFile(base, size, location, location_checksum, mem_map));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700279 if (!dex_file->Init(error_msg)) {
280 return nullptr;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700281 } else {
Brian Carlstromf615a612011-07-23 12:50:34 -0700282 return dex_file.release();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700283 }
284}
285
Jesse Wilson6bf19152011-09-29 13:12:33 -0400286DexFile::~DexFile() {
Elliott Hughes8cef0b82011-10-11 19:24:00 -0700287 // We don't call DeleteGlobalRef on dex_object_ because we're only called by DestroyJavaVM, and
288 // that's only called after DetachCurrentThread, which means there's no JNIEnv. We could
289 // re-attach, but cleaning up these global references is not obviously useful. It's not as if
290 // the global reference table is otherwise empty!
Jesse Wilson6bf19152011-09-29 13:12:33 -0400291}
292
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700293bool DexFile::Init(std::string* error_msg) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700294 InitMembers();
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700295 if (!CheckMagicAndVersion(error_msg)) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700296 return false;
297 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700298 return true;
299}
300
Brian Carlstromf615a612011-07-23 12:50:34 -0700301void DexFile::InitMembers() {
Ian Rogers30fab402012-01-23 15:43:46 -0800302 const byte* b = begin_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700303 header_ = reinterpret_cast<const Header*>(b);
304 const Header* h = header_;
305 string_ids_ = reinterpret_cast<const StringId*>(b + h->string_ids_off_);
306 type_ids_ = reinterpret_cast<const TypeId*>(b + h->type_ids_off_);
307 field_ids_ = reinterpret_cast<const FieldId*>(b + h->field_ids_off_);
308 method_ids_ = reinterpret_cast<const MethodId*>(b + h->method_ids_off_);
309 proto_ids_ = reinterpret_cast<const ProtoId*>(b + h->proto_ids_off_);
310 class_defs_ = reinterpret_cast<const ClassDef*>(b + h->class_defs_off_);
311}
312
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700313bool DexFile::CheckMagicAndVersion(std::string* error_msg) const {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800314 CHECK(header_->magic_ != NULL) << GetLocation();
315 if (!IsMagicValid(header_->magic_)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700316 std::ostringstream oss;
317 oss << "Unrecognized magic number in " << GetLocation() << ":"
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800318 << " " << header_->magic_[0]
319 << " " << header_->magic_[1]
320 << " " << header_->magic_[2]
321 << " " << header_->magic_[3];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700322 *error_msg = oss.str();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700323 return false;
324 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800325 if (!IsVersionValid(header_->magic_)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700326 std::ostringstream oss;
327 oss << "Unrecognized version number in " << GetLocation() << ":"
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800328 << " " << header_->magic_[4]
329 << " " << header_->magic_[5]
330 << " " << header_->magic_[6]
331 << " " << header_->magic_[7];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700332 *error_msg = oss.str();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700333 return false;
334 }
335 return true;
336}
337
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800338bool DexFile::IsMagicValid(const byte* magic) {
339 return (memcmp(magic, kDexMagic, sizeof(kDexMagic)) == 0);
340}
341
342bool DexFile::IsVersionValid(const byte* magic) {
343 const byte* version = &magic[sizeof(kDexMagic)];
344 return (memcmp(version, kDexMagicVersion, sizeof(kDexMagicVersion)) == 0);
345}
346
Ian Rogersd81871c2011-10-03 13:57:23 -0700347uint32_t DexFile::GetVersion() const {
348 const char* version = reinterpret_cast<const char*>(&GetHeader().magic_[sizeof(kDexMagic)]);
349 return atoi(version);
350}
351
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700352const DexFile::ClassDef* DexFile::FindClassDef(const char* descriptor) const {
353 size_t num_class_defs = NumClassDefs();
354 if (num_class_defs == 0) {
355 return NULL;
356 }
357 const StringId* string_id = FindStringId(descriptor);
358 if (string_id == NULL) {
359 return NULL;
360 }
361 const TypeId* type_id = FindTypeId(GetIndexForStringId(*string_id));
362 if (type_id == NULL) {
363 return NULL;
364 }
365 uint16_t type_idx = GetIndexForTypeId(*type_id);
366 for (size_t i = 0; i < num_class_defs; ++i) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700367 const ClassDef& class_def = GetClassDef(i);
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700368 if (class_def.class_idx_ == type_idx) {
369 return &class_def;
370 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700371 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700372 return NULL;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700373}
374
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700375const DexFile::ClassDef* DexFile::FindClassDef(uint16_t type_idx) const {
376 size_t num_class_defs = NumClassDefs();
377 for (size_t i = 0; i < num_class_defs; ++i) {
378 const ClassDef& class_def = GetClassDef(i);
379 if (class_def.class_idx_ == type_idx) {
380 return &class_def;
381 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700382 }
383 return NULL;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700384}
385
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800386const DexFile::FieldId* DexFile::FindFieldId(const DexFile::TypeId& declaring_klass,
387 const DexFile::StringId& name,
388 const DexFile::TypeId& type) const {
389 // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx
390 const uint16_t class_idx = GetIndexForTypeId(declaring_klass);
391 const uint32_t name_idx = GetIndexForStringId(name);
392 const uint16_t type_idx = GetIndexForTypeId(type);
Ian Rogersf8582c32013-05-29 16:33:03 -0700393 int32_t lo = 0;
394 int32_t hi = NumFieldIds() - 1;
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800395 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700396 int32_t mid = (hi + lo) / 2;
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800397 const DexFile::FieldId& field = GetFieldId(mid);
398 if (class_idx > field.class_idx_) {
399 lo = mid + 1;
400 } else if (class_idx < field.class_idx_) {
401 hi = mid - 1;
402 } else {
403 if (name_idx > field.name_idx_) {
404 lo = mid + 1;
405 } else if (name_idx < field.name_idx_) {
406 hi = mid - 1;
407 } else {
408 if (type_idx > field.type_idx_) {
409 lo = mid + 1;
410 } else if (type_idx < field.type_idx_) {
411 hi = mid - 1;
412 } else {
413 return &field;
414 }
415 }
416 }
417 }
418 return NULL;
419}
420
421const DexFile::MethodId* DexFile::FindMethodId(const DexFile::TypeId& declaring_klass,
Ian Rogers0571d352011-11-03 19:51:38 -0700422 const DexFile::StringId& name,
423 const DexFile::ProtoId& signature) const {
424 // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800425 const uint16_t class_idx = GetIndexForTypeId(declaring_klass);
Ian Rogers0571d352011-11-03 19:51:38 -0700426 const uint32_t name_idx = GetIndexForStringId(name);
427 const uint16_t proto_idx = GetIndexForProtoId(signature);
Ian Rogersf8582c32013-05-29 16:33:03 -0700428 int32_t lo = 0;
429 int32_t hi = NumMethodIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700430 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700431 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700432 const DexFile::MethodId& method = GetMethodId(mid);
433 if (class_idx > method.class_idx_) {
434 lo = mid + 1;
435 } else if (class_idx < method.class_idx_) {
436 hi = mid - 1;
437 } else {
438 if (name_idx > method.name_idx_) {
439 lo = mid + 1;
440 } else if (name_idx < method.name_idx_) {
441 hi = mid - 1;
442 } else {
443 if (proto_idx > method.proto_idx_) {
444 lo = mid + 1;
445 } else if (proto_idx < method.proto_idx_) {
446 hi = mid - 1;
447 } else {
448 return &method;
449 }
450 }
451 }
452 }
453 return NULL;
454}
455
Ian Rogers637c65b2013-05-31 11:46:00 -0700456const DexFile::StringId* DexFile::FindStringId(const char* string) const {
Ian Rogersf8582c32013-05-29 16:33:03 -0700457 int32_t lo = 0;
458 int32_t hi = NumStringIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700459 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700460 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700461 const DexFile::StringId& str_id = GetStringId(mid);
Ian Rogerscf5077a2013-10-31 12:37:54 -0700462 const char* str = GetStringData(str_id);
Ian Rogers637c65b2013-05-31 11:46:00 -0700463 int compare = CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(string, str);
464 if (compare > 0) {
465 lo = mid + 1;
466 } else if (compare < 0) {
467 hi = mid - 1;
468 } else {
469 return &str_id;
470 }
471 }
472 return NULL;
473}
474
475const DexFile::StringId* DexFile::FindStringId(const uint16_t* string) const {
476 int32_t lo = 0;
477 int32_t hi = NumStringIds() - 1;
478 while (hi >= lo) {
479 int32_t mid = (hi + lo) / 2;
Ian Rogers637c65b2013-05-31 11:46:00 -0700480 const DexFile::StringId& str_id = GetStringId(mid);
Ian Rogerscf5077a2013-10-31 12:37:54 -0700481 const char* str = GetStringData(str_id);
Ian Rogers637c65b2013-05-31 11:46:00 -0700482 int compare = CompareModifiedUtf8ToUtf16AsCodePointValues(str, string);
Ian Rogers0571d352011-11-03 19:51:38 -0700483 if (compare > 0) {
484 lo = mid + 1;
485 } else if (compare < 0) {
486 hi = mid - 1;
487 } else {
488 return &str_id;
489 }
490 }
491 return NULL;
492}
493
494const DexFile::TypeId* DexFile::FindTypeId(uint32_t string_idx) const {
Ian Rogersf8582c32013-05-29 16:33:03 -0700495 int32_t lo = 0;
496 int32_t hi = NumTypeIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700497 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700498 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700499 const TypeId& type_id = GetTypeId(mid);
500 if (string_idx > type_id.descriptor_idx_) {
501 lo = mid + 1;
502 } else if (string_idx < type_id.descriptor_idx_) {
503 hi = mid - 1;
504 } else {
505 return &type_id;
506 }
507 }
508 return NULL;
509}
510
511const DexFile::ProtoId* DexFile::FindProtoId(uint16_t return_type_idx,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800512 const std::vector<uint16_t>& signature_type_idxs) const {
Ian Rogersf8582c32013-05-29 16:33:03 -0700513 int32_t lo = 0;
514 int32_t hi = NumProtoIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700515 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700516 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700517 const DexFile::ProtoId& proto = GetProtoId(mid);
518 int compare = return_type_idx - proto.return_type_idx_;
519 if (compare == 0) {
520 DexFileParameterIterator it(*this, proto);
521 size_t i = 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800522 while (it.HasNext() && i < signature_type_idxs.size() && compare == 0) {
523 compare = signature_type_idxs[i] - it.GetTypeIdx();
Ian Rogers0571d352011-11-03 19:51:38 -0700524 it.Next();
525 i++;
526 }
527 if (compare == 0) {
528 if (it.HasNext()) {
529 compare = -1;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800530 } else if (i < signature_type_idxs.size()) {
Ian Rogers0571d352011-11-03 19:51:38 -0700531 compare = 1;
532 }
533 }
534 }
535 if (compare > 0) {
536 lo = mid + 1;
537 } else if (compare < 0) {
538 hi = mid - 1;
539 } else {
540 return &proto;
541 }
542 }
543 return NULL;
544}
545
546// Given a signature place the type ids into the given vector
Ian Rogersd91d6d62013-09-25 20:26:14 -0700547bool DexFile::CreateTypeList(const StringPiece& signature, uint16_t* return_type_idx,
548 std::vector<uint16_t>* param_type_idxs) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700549 if (signature[0] != '(') {
550 return false;
551 }
552 size_t offset = 1;
553 size_t end = signature.size();
554 bool process_return = false;
555 while (offset < end) {
556 char c = signature[offset];
557 offset++;
558 if (c == ')') {
559 process_return = true;
560 continue;
561 }
Ian Rogersd91d6d62013-09-25 20:26:14 -0700562 // TODO: avoid building a string.
Ian Rogers0571d352011-11-03 19:51:38 -0700563 std::string descriptor;
564 descriptor += c;
565 while (c == '[') { // process array prefix
566 if (offset >= end) { // expect some descriptor following [
567 return false;
568 }
569 c = signature[offset];
570 offset++;
571 descriptor += c;
572 }
573 if (c == 'L') { // process type descriptors
574 do {
575 if (offset >= end) { // unexpected early termination of descriptor
576 return false;
577 }
578 c = signature[offset];
579 offset++;
580 descriptor += c;
581 } while (c != ';');
582 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700583 const DexFile::StringId* string_id = FindStringId(descriptor.c_str());
Ian Rogers0571d352011-11-03 19:51:38 -0700584 if (string_id == NULL) {
585 return false;
586 }
587 const DexFile::TypeId* type_id = FindTypeId(GetIndexForStringId(*string_id));
588 if (type_id == NULL) {
589 return false;
590 }
591 uint16_t type_idx = GetIndexForTypeId(*type_id);
592 if (!process_return) {
593 param_type_idxs->push_back(type_idx);
594 } else {
595 *return_type_idx = type_idx;
596 return offset == end; // return true if the signature had reached a sensible end
597 }
598 }
599 return false; // failed to correctly parse return type
600}
601
Ian Rogersd91d6d62013-09-25 20:26:14 -0700602const Signature DexFile::CreateSignature(const StringPiece& signature) const {
603 uint16_t return_type_idx;
604 std::vector<uint16_t> param_type_indices;
605 bool success = CreateTypeList(signature, &return_type_idx, &param_type_indices);
606 if (!success) {
607 return Signature::NoSignature();
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700608 }
Ian Rogersd91d6d62013-09-25 20:26:14 -0700609 const ProtoId* proto_id = FindProtoId(return_type_idx, param_type_indices);
610 if (proto_id == NULL) {
611 return Signature::NoSignature();
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700612 }
Ian Rogersd91d6d62013-09-25 20:26:14 -0700613 return Signature(this, *proto_id);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700614}
615
Brian Carlstromea46f952013-07-30 01:26:50 -0700616int32_t DexFile::GetLineNumFromPC(const mirror::ArtMethod* method, uint32_t rel_pc) const {
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -0700617 // For native method, lineno should be -2 to indicate it is native. Note that
618 // "line number == -2" is how libcore tells from StackTraceElement.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700619 if (method->GetCodeItemOffset() == 0) {
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -0700620 return -2;
621 }
622
TDYa127c8dc1012012-04-19 07:03:33 -0700623 const CodeItem* code_item = GetCodeItem(method->GetCodeItemOffset());
Elliott Hughescaf76542012-06-28 16:08:22 -0700624 DCHECK(code_item != NULL) << PrettyMethod(method) << " " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700625
626 // A method with no line number info should return -1
627 LineNumFromPcContext context(rel_pc, -1);
TDYa127c8dc1012012-04-19 07:03:33 -0700628 DecodeDebugInfo(code_item, method->IsStatic(), method->GetDexMethodIndex(), LineNumForPcCb,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800629 NULL, &context);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700630 return context.line_num_;
631}
632
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700633int32_t DexFile::FindTryItem(const CodeItem &code_item, uint32_t address) {
Ian Rogers0571d352011-11-03 19:51:38 -0700634 // Note: Signed type is important for max and min.
635 int32_t min = 0;
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700636 int32_t max = code_item.tries_size_ - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700637
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700638 while (min <= max) {
639 int32_t mid = min + ((max - min) / 2);
640
641 const art::DexFile::TryItem* ti = GetTryItems(code_item, mid);
642 uint32_t start = ti->start_addr_;
643 uint32_t end = start + ti->insn_count_;
644
Ian Rogers0571d352011-11-03 19:51:38 -0700645 if (address < start) {
646 max = mid - 1;
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700647 } else if (address >= end) {
648 min = mid + 1;
649 } else { // We have a winner!
650 return mid;
Ian Rogers0571d352011-11-03 19:51:38 -0700651 }
652 }
653 // No match.
654 return -1;
655}
656
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700657int32_t DexFile::FindCatchHandlerOffset(const CodeItem &code_item, uint32_t address) {
658 int32_t try_item = FindTryItem(code_item, address);
659 if (try_item == -1) {
660 return -1;
661 } else {
662 return DexFile::GetTryItems(code_item, try_item)->handler_off_;
663 }
664}
665
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800666void DexFile::DecodeDebugInfo0(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes2435a572012-02-17 16:07:41 -0800667 DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb,
668 void* context, const byte* stream, LocalInfo* local_in_reg) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700669 uint32_t line = DecodeUnsignedLeb128(&stream);
670 uint32_t parameters_size = DecodeUnsignedLeb128(&stream);
671 uint16_t arg_reg = code_item->registers_size_ - code_item->ins_size_;
672 uint32_t address = 0;
Elliott Hughes30646832011-10-13 16:59:46 -0700673 bool need_locals = (local_cb != NULL);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700674
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800675 if (!is_static) {
Elliott Hughes30646832011-10-13 16:59:46 -0700676 if (need_locals) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800677 const char* descriptor = GetMethodDeclaringClassDescriptor(GetMethodId(method_idx));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700678 local_in_reg[arg_reg].name_ = "this";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800679 local_in_reg[arg_reg].descriptor_ = descriptor;
Elliott Hughes392b1242011-11-30 13:55:50 -0800680 local_in_reg[arg_reg].signature_ = NULL;
Elliott Hughes30646832011-10-13 16:59:46 -0700681 local_in_reg[arg_reg].start_address_ = 0;
682 local_in_reg[arg_reg].is_live_ = true;
683 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700684 arg_reg++;
685 }
686
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800687 DexFileParameterIterator it(*this, GetMethodPrototype(GetMethodId(method_idx)));
Ian Rogers0571d352011-11-03 19:51:38 -0700688 for (uint32_t i = 0; i < parameters_size && it.HasNext(); ++i, it.Next()) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700689 if (arg_reg >= code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700690 LOG(ERROR) << "invalid stream - arg reg >= reg size (" << arg_reg
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800691 << " >= " << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700692 return;
693 }
Elliott Hughes392b1242011-11-30 13:55:50 -0800694 uint32_t id = DecodeUnsignedLeb128P1(&stream);
Ian Rogers0571d352011-11-03 19:51:38 -0700695 const char* descriptor = it.GetDescriptor();
Elliott Hughes392b1242011-11-30 13:55:50 -0800696 if (need_locals && id != kDexNoIndex) {
Ian Rogers0571d352011-11-03 19:51:38 -0700697 const char* name = StringDataByIdx(id);
Elliott Hughes30646832011-10-13 16:59:46 -0700698 local_in_reg[arg_reg].name_ = name;
699 local_in_reg[arg_reg].descriptor_ = descriptor;
Elliott Hughes392b1242011-11-30 13:55:50 -0800700 local_in_reg[arg_reg].signature_ = NULL;
Elliott Hughes30646832011-10-13 16:59:46 -0700701 local_in_reg[arg_reg].start_address_ = address;
702 local_in_reg[arg_reg].is_live_ = true;
703 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700704 switch (*descriptor) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700705 case 'D':
706 case 'J':
707 arg_reg += 2;
708 break;
709 default:
710 arg_reg += 1;
711 break;
712 }
713 }
714
Ian Rogers0571d352011-11-03 19:51:38 -0700715 if (it.HasNext()) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800716 LOG(ERROR) << "invalid stream - problem with parameter iterator in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700717 return;
718 }
719
720 for (;;) {
721 uint8_t opcode = *stream++;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700722 uint16_t reg;
jeffhaof8728872011-10-28 19:11:13 -0700723 uint16_t name_idx;
724 uint16_t descriptor_idx;
725 uint16_t signature_idx = 0;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700726
Shih-wei Liao195487c2011-08-20 13:29:04 -0700727 switch (opcode) {
728 case DBG_END_SEQUENCE:
729 return;
730
731 case DBG_ADVANCE_PC:
732 address += DecodeUnsignedLeb128(&stream);
733 break;
734
735 case DBG_ADVANCE_LINE:
Shih-wei Liao8a05d272011-10-15 18:45:43 -0700736 line += DecodeSignedLeb128(&stream);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700737 break;
738
739 case DBG_START_LOCAL:
740 case DBG_START_LOCAL_EXTENDED:
741 reg = DecodeUnsignedLeb128(&stream);
742 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700743 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800744 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700745 return;
746 }
747
jeffhaof8728872011-10-28 19:11:13 -0700748 name_idx = DecodeUnsignedLeb128P1(&stream);
749 descriptor_idx = DecodeUnsignedLeb128P1(&stream);
750 if (opcode == DBG_START_LOCAL_EXTENDED) {
751 signature_idx = DecodeUnsignedLeb128P1(&stream);
752 }
753
Shih-wei Liao195487c2011-08-20 13:29:04 -0700754 // Emit what was previously there, if anything
Elliott Hughes30646832011-10-13 16:59:46 -0700755 if (need_locals) {
Elliott Hughes2435a572012-02-17 16:07:41 -0800756 InvokeLocalCbIfLive(context, reg, address, local_in_reg, local_cb);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700757
Ian Rogers0571d352011-11-03 19:51:38 -0700758 local_in_reg[reg].name_ = StringDataByIdx(name_idx);
759 local_in_reg[reg].descriptor_ = StringByTypeIdx(descriptor_idx);
Elliott Hughes30646832011-10-13 16:59:46 -0700760 if (opcode == DBG_START_LOCAL_EXTENDED) {
Ian Rogers0571d352011-11-03 19:51:38 -0700761 local_in_reg[reg].signature_ = StringDataByIdx(signature_idx);
Elliott Hughes30646832011-10-13 16:59:46 -0700762 }
763 local_in_reg[reg].start_address_ = address;
764 local_in_reg[reg].is_live_ = true;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700765 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700766 break;
767
768 case DBG_END_LOCAL:
769 reg = DecodeUnsignedLeb128(&stream);
770 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700771 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800772 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700773 return;
774 }
775
Elliott Hughes30646832011-10-13 16:59:46 -0700776 if (need_locals) {
Elliott Hughes2435a572012-02-17 16:07:41 -0800777 InvokeLocalCbIfLive(context, reg, address, local_in_reg, local_cb);
Elliott Hughes30646832011-10-13 16:59:46 -0700778 local_in_reg[reg].is_live_ = false;
779 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700780 break;
781
782 case DBG_RESTART_LOCAL:
783 reg = DecodeUnsignedLeb128(&stream);
784 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700785 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800786 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700787 return;
788 }
789
Elliott Hughes30646832011-10-13 16:59:46 -0700790 if (need_locals) {
791 if (local_in_reg[reg].name_ == NULL || local_in_reg[reg].descriptor_ == NULL) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800792 LOG(ERROR) << "invalid stream - no name or descriptor in " << GetLocation();
Elliott Hughes30646832011-10-13 16:59:46 -0700793 return;
794 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700795
Elliott Hughes30646832011-10-13 16:59:46 -0700796 // If the register is live, the "restart" is superfluous,
797 // and we don't want to mess with the existing start address.
798 if (!local_in_reg[reg].is_live_) {
799 local_in_reg[reg].start_address_ = address;
800 local_in_reg[reg].is_live_ = true;
801 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700802 }
803 break;
804
805 case DBG_SET_PROLOGUE_END:
806 case DBG_SET_EPILOGUE_BEGIN:
807 case DBG_SET_FILE:
808 break;
809
Shih-wei Liao8e1b4ff2011-10-15 15:43:51 -0700810 default: {
811 int adjopcode = opcode - DBG_FIRST_SPECIAL;
812
Shih-wei Liao195487c2011-08-20 13:29:04 -0700813 address += adjopcode / DBG_LINE_RANGE;
814 line += DBG_LINE_BASE + (adjopcode % DBG_LINE_RANGE);
815
Elliott Hughes2435a572012-02-17 16:07:41 -0800816 if (position_cb != NULL) {
817 if (position_cb(context, address, line)) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700818 // early exit
819 return;
820 }
821 }
822 break;
Shih-wei Liao8e1b4ff2011-10-15 15:43:51 -0700823 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700824 }
825 }
826}
827
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800828void DexFile::DecodeDebugInfo(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes2435a572012-02-17 16:07:41 -0800829 DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb,
830 void* context) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700831 const byte* stream = GetDebugInfoStream(code_item);
Brian Carlstrome0948e12013-08-29 09:36:15 -0700832 UniquePtr<LocalInfo[]> local_in_reg(local_cb != NULL ?
833 new LocalInfo[code_item->registers_size_] :
834 NULL);
Ian Rogers0571d352011-11-03 19:51:38 -0700835 if (stream != NULL) {
Elliott Hughesee0fa762012-03-26 17:12:41 -0700836 DecodeDebugInfo0(code_item, is_static, method_idx, position_cb, local_cb, context, stream, &local_in_reg[0]);
Ian Rogers0571d352011-11-03 19:51:38 -0700837 }
838 for (int reg = 0; reg < code_item->registers_size_; reg++) {
Elliott Hughesee0fa762012-03-26 17:12:41 -0700839 InvokeLocalCbIfLive(context, reg, code_item->insns_size_in_code_units_, &local_in_reg[0], local_cb);
Ian Rogers0571d352011-11-03 19:51:38 -0700840 }
841}
842
Elliott Hughes2435a572012-02-17 16:07:41 -0800843bool DexFile::LineNumForPcCb(void* raw_context, uint32_t address, uint32_t line_num) {
844 LineNumFromPcContext* context = reinterpret_cast<LineNumFromPcContext*>(raw_context);
Ian Rogers0571d352011-11-03 19:51:38 -0700845
846 // We know that this callback will be called in
847 // ascending address order, so keep going until we find
848 // a match or we've just gone past it.
849 if (address > context->address_) {
850 // The line number from the previous positions callback
851 // wil be the final result.
852 return true;
853 } else {
854 context->line_num_ = line_num;
855 return address == context->address_;
856 }
857}
858
Ian Rogersd91d6d62013-09-25 20:26:14 -0700859std::string Signature::ToString() const {
860 if (dex_file_ == nullptr) {
861 CHECK(proto_id_ == nullptr);
862 return "<no signature>";
863 }
864 const DexFile::TypeList* params = dex_file_->GetProtoParameters(*proto_id_);
865 std::string result;
866 if (params == nullptr) {
867 result += "()";
868 } else {
869 result += "(";
870 for (uint32_t i = 0; i < params->Size(); ++i) {
871 result += dex_file_->StringByTypeIdx(params->GetTypeItem(i).type_idx_);
872 }
873 result += ")";
874 }
875 result += dex_file_->StringByTypeIdx(proto_id_->return_type_idx_);
876 return result;
877}
878
879std::ostream& operator<<(std::ostream& os, const Signature& sig) {
880 return os << sig.ToString();
881}
882
Ian Rogers0571d352011-11-03 19:51:38 -0700883// Decodes the header section from the class data bytes.
884void ClassDataItemIterator::ReadClassDataHeader() {
885 CHECK(ptr_pos_ != NULL);
886 header_.static_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_);
887 header_.instance_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_);
888 header_.direct_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_);
889 header_.virtual_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_);
890}
891
892void ClassDataItemIterator::ReadClassDataField() {
893 field_.field_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_);
894 field_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_);
Brian Carlstrom68adbe42012-05-11 17:18:08 -0700895 if (last_idx_ != 0 && field_.field_idx_delta_ == 0) {
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -0700896 LOG(WARNING) << "Duplicate field " << PrettyField(GetMemberIndex(), dex_file_)
897 << " in " << dex_file_.GetLocation();
898 }
Ian Rogers0571d352011-11-03 19:51:38 -0700899}
900
901void ClassDataItemIterator::ReadClassDataMethod() {
902 method_.method_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_);
903 method_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_);
904 method_.code_off_ = DecodeUnsignedLeb128(&ptr_pos_);
Brian Carlstrom68adbe42012-05-11 17:18:08 -0700905 if (last_idx_ != 0 && method_.method_idx_delta_ == 0) {
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -0700906 LOG(WARNING) << "Duplicate method " << PrettyMethod(GetMemberIndex(), dex_file_)
907 << " in " << dex_file_.GetLocation();
908 }
Ian Rogers0571d352011-11-03 19:51:38 -0700909}
910
911// Read a signed integer. "zwidth" is the zero-based byte count.
912static int32_t ReadSignedInt(const byte* ptr, int zwidth) {
913 int32_t val = 0;
914 for (int i = zwidth; i >= 0; --i) {
915 val = ((uint32_t)val >> 8) | (((int32_t)*ptr++) << 24);
916 }
917 val >>= (3 - zwidth) * 8;
918 return val;
919}
920
921// Read an unsigned integer. "zwidth" is the zero-based byte count,
922// "fill_on_right" indicates which side we want to zero-fill from.
923static uint32_t ReadUnsignedInt(const byte* ptr, int zwidth, bool fill_on_right) {
924 uint32_t val = 0;
925 if (!fill_on_right) {
926 for (int i = zwidth; i >= 0; --i) {
927 val = (val >> 8) | (((uint32_t)*ptr++) << 24);
928 }
929 val >>= (3 - zwidth) * 8;
930 } else {
931 for (int i = zwidth; i >= 0; --i) {
932 val = (val >> 8) | (((uint32_t)*ptr++) << 24);
933 }
934 }
935 return val;
936}
937
938// Read a signed long. "zwidth" is the zero-based byte count.
939static int64_t ReadSignedLong(const byte* ptr, int zwidth) {
940 int64_t val = 0;
941 for (int i = zwidth; i >= 0; --i) {
942 val = ((uint64_t)val >> 8) | (((int64_t)*ptr++) << 56);
943 }
944 val >>= (7 - zwidth) * 8;
945 return val;
946}
947
948// Read an unsigned long. "zwidth" is the zero-based byte count,
949// "fill_on_right" indicates which side we want to zero-fill from.
950static uint64_t ReadUnsignedLong(const byte* ptr, int zwidth, bool fill_on_right) {
951 uint64_t val = 0;
952 if (!fill_on_right) {
953 for (int i = zwidth; i >= 0; --i) {
954 val = (val >> 8) | (((uint64_t)*ptr++) << 56);
955 }
956 val >>= (7 - zwidth) * 8;
957 } else {
958 for (int i = zwidth; i >= 0; --i) {
959 val = (val >> 8) | (((uint64_t)*ptr++) << 56);
960 }
961 }
962 return val;
963}
964
965EncodedStaticFieldValueIterator::EncodedStaticFieldValueIterator(const DexFile& dex_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800966 mirror::DexCache* dex_cache,
967 mirror::ClassLoader* class_loader,
Ian Rogersca190662012-06-26 15:45:57 -0700968 ClassLinker* linker,
969 const DexFile::ClassDef& class_def)
Brian Carlstrom88f36542012-10-16 23:24:21 -0700970 : dex_file_(dex_file), dex_cache_(dex_cache), class_loader_(class_loader), linker_(linker),
971 array_size_(), pos_(-1), type_(kByte) {
Ian Rogers0571d352011-11-03 19:51:38 -0700972 ptr_ = dex_file.GetEncodedStaticFieldValuesArray(class_def);
973 if (ptr_ == NULL) {
974 array_size_ = 0;
975 } else {
976 array_size_ = DecodeUnsignedLeb128(&ptr_);
977 }
978 if (array_size_ > 0) {
979 Next();
980 }
981}
982
983void EncodedStaticFieldValueIterator::Next() {
984 pos_++;
985 if (pos_ >= array_size_) {
986 return;
987 }
988 byte value_type = *ptr_++;
989 byte value_arg = value_type >> kEncodedValueArgShift;
990 size_t width = value_arg + 1; // assume and correct later
Brian Carlstrom88f36542012-10-16 23:24:21 -0700991 type_ = static_cast<ValueType>(value_type & kEncodedValueTypeMask);
Ian Rogers0571d352011-11-03 19:51:38 -0700992 switch (type_) {
993 case kBoolean:
994 jval_.i = (value_arg != 0) ? 1 : 0;
995 width = 0;
996 break;
997 case kByte:
998 jval_.i = ReadSignedInt(ptr_, value_arg);
999 CHECK(IsInt(8, jval_.i));
1000 break;
1001 case kShort:
1002 jval_.i = ReadSignedInt(ptr_, value_arg);
1003 CHECK(IsInt(16, jval_.i));
1004 break;
1005 case kChar:
1006 jval_.i = ReadUnsignedInt(ptr_, value_arg, false);
1007 CHECK(IsUint(16, jval_.i));
1008 break;
1009 case kInt:
1010 jval_.i = ReadSignedInt(ptr_, value_arg);
1011 break;
1012 case kLong:
1013 jval_.j = ReadSignedLong(ptr_, value_arg);
1014 break;
1015 case kFloat:
1016 jval_.i = ReadUnsignedInt(ptr_, value_arg, true);
1017 break;
1018 case kDouble:
1019 jval_.j = ReadUnsignedLong(ptr_, value_arg, true);
1020 break;
1021 case kString:
1022 case kType:
Ian Rogers0571d352011-11-03 19:51:38 -07001023 jval_.i = ReadUnsignedInt(ptr_, value_arg, false);
1024 break;
1025 case kField:
Brian Carlstrom88f36542012-10-16 23:24:21 -07001026 case kMethod:
1027 case kEnum:
Ian Rogers0571d352011-11-03 19:51:38 -07001028 case kArray:
1029 case kAnnotation:
1030 UNIMPLEMENTED(FATAL) << ": type " << type_;
1031 break;
1032 case kNull:
1033 jval_.l = NULL;
1034 width = 0;
1035 break;
1036 default:
1037 LOG(FATAL) << "Unreached";
1038 }
1039 ptr_ += width;
1040}
1041
Brian Carlstromea46f952013-07-30 01:26:50 -07001042void EncodedStaticFieldValueIterator::ReadValueToField(mirror::ArtField* field) const {
Ian Rogers0571d352011-11-03 19:51:38 -07001043 switch (type_) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001044 case kBoolean: field->SetBoolean(field->GetDeclaringClass(), jval_.z); break;
1045 case kByte: field->SetByte(field->GetDeclaringClass(), jval_.b); break;
1046 case kShort: field->SetShort(field->GetDeclaringClass(), jval_.s); break;
1047 case kChar: field->SetChar(field->GetDeclaringClass(), jval_.c); break;
1048 case kInt: field->SetInt(field->GetDeclaringClass(), jval_.i); break;
1049 case kLong: field->SetLong(field->GetDeclaringClass(), jval_.j); break;
1050 case kFloat: field->SetFloat(field->GetDeclaringClass(), jval_.f); break;
1051 case kDouble: field->SetDouble(field->GetDeclaringClass(), jval_.d); break;
1052 case kNull: field->SetObject(field->GetDeclaringClass(), NULL); break;
Ian Rogers0571d352011-11-03 19:51:38 -07001053 case kString: {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001054 mirror::String* resolved = linker_->ResolveString(dex_file_, jval_.i, dex_cache_);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001055 field->SetObject(field->GetDeclaringClass(), resolved);
Ian Rogers0571d352011-11-03 19:51:38 -07001056 break;
1057 }
Brian Carlstrom88f36542012-10-16 23:24:21 -07001058 case kType: {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001059 mirror::Class* resolved = linker_->ResolveType(dex_file_, jval_.i, dex_cache_, class_loader_);
mikaelpeltierebf6aa82012-11-07 14:02:15 +01001060 field->SetObject(field->GetDeclaringClass(), resolved);
Brian Carlstrom88f36542012-10-16 23:24:21 -07001061 break;
1062 }
Ian Rogers0571d352011-11-03 19:51:38 -07001063 default: UNIMPLEMENTED(FATAL) << ": type " << type_;
1064 }
1065}
1066
1067CatchHandlerIterator::CatchHandlerIterator(const DexFile::CodeItem& code_item, uint32_t address) {
1068 handler_.address_ = -1;
1069 int32_t offset = -1;
1070
1071 // Short-circuit the overwhelmingly common cases.
1072 switch (code_item.tries_size_) {
1073 case 0:
1074 break;
1075 case 1: {
1076 const DexFile::TryItem* tries = DexFile::GetTryItems(code_item, 0);
1077 uint32_t start = tries->start_addr_;
1078 if (address >= start) {
1079 uint32_t end = start + tries->insn_count_;
1080 if (address < end) {
1081 offset = tries->handler_off_;
1082 }
1083 }
1084 break;
1085 }
1086 default:
Ian Rogersdbbc99d2013-04-18 16:51:54 -07001087 offset = DexFile::FindCatchHandlerOffset(code_item, address);
Ian Rogers0571d352011-11-03 19:51:38 -07001088 }
Logan Chien736df022012-04-27 16:25:57 +08001089 Init(code_item, offset);
1090}
1091
1092CatchHandlerIterator::CatchHandlerIterator(const DexFile::CodeItem& code_item,
1093 const DexFile::TryItem& try_item) {
1094 handler_.address_ = -1;
1095 Init(code_item, try_item.handler_off_);
1096}
1097
1098void CatchHandlerIterator::Init(const DexFile::CodeItem& code_item,
1099 int32_t offset) {
Ian Rogers0571d352011-11-03 19:51:38 -07001100 if (offset >= 0) {
Logan Chien736df022012-04-27 16:25:57 +08001101 Init(DexFile::GetCatchHandlerData(code_item, offset));
Ian Rogers0571d352011-11-03 19:51:38 -07001102 } else {
1103 // Not found, initialize as empty
1104 current_data_ = NULL;
1105 remaining_count_ = -1;
1106 catch_all_ = false;
1107 DCHECK(!HasNext());
1108 }
1109}
1110
1111void CatchHandlerIterator::Init(const byte* handler_data) {
1112 current_data_ = handler_data;
1113 remaining_count_ = DecodeSignedLeb128(&current_data_);
1114
1115 // If remaining_count_ is non-positive, then it is the negative of
1116 // the number of catch types, and the catches are followed by a
1117 // catch-all handler.
1118 if (remaining_count_ <= 0) {
1119 catch_all_ = true;
1120 remaining_count_ = -remaining_count_;
1121 } else {
1122 catch_all_ = false;
1123 }
1124 Next();
1125}
1126
1127void CatchHandlerIterator::Next() {
1128 if (remaining_count_ > 0) {
1129 handler_.type_idx_ = DecodeUnsignedLeb128(&current_data_);
1130 handler_.address_ = DecodeUnsignedLeb128(&current_data_);
1131 remaining_count_--;
1132 return;
1133 }
1134
1135 if (catch_all_) {
1136 handler_.type_idx_ = DexFile::kDexNoIndex16;
1137 handler_.address_ = DecodeUnsignedLeb128(&current_data_);
1138 catch_all_ = false;
1139 return;
1140 }
1141
1142 // no more handler
1143 remaining_count_ = -1;
1144}
1145
Carl Shapiro1fb86202011-06-27 17:43:13 -07001146} // namespace art