blob: 3bb47d415e21971164750ccb7224fc70ad7a6b1b [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>
Ian Rogers700a4022014-05-19 16:49:03 -070026#include <memory>
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070027
Elliott Hughes07ed66b2012-12-12 18:34:25 -080028#include "base/logging.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080029#include "base/stringprintf.h"
Ian Rogers0571d352011-11-03 19:51:38 -070030#include "class_linker.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070031#include "dex_file-inl.h"
jeffhao10037c82012-01-23 15:06:23 -080032#include "dex_file_verifier.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070033#include "globals.h"
Ian Rogers0571d352011-11-03 19:51:38 -070034#include "leb128.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070035#include "mirror/art_field-inl.h"
36#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "mirror/string.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070038#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070039#include "safe_map.h"
Vladimir Markofd995762013-11-06 16:36:36 +000040#include "ScopedFd.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070041#include "handle_scope-inl.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070042#include "thread.h"
Ian Rogersa6724902013-09-23 09:23:37 -070043#include "utf-inl.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070044#include "utils.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070045#include "well_known_classes.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070046#include "zip_archive.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070047
48namespace art {
49
Brian Carlstromf615a612011-07-23 12:50:34 -070050const byte DexFile::kDexMagic[] = { 'd', 'e', 'x', '\n' };
51const byte DexFile::kDexMagicVersion[] = { '0', '3', '5', '\0' };
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070052
Ian Rogers8d31bbd2013-10-13 10:44:14 -070053static int OpenAndReadMagic(const char* filename, uint32_t* magic, std::string* error_msg) {
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070054 CHECK(magic != NULL);
Vladimir Markofd995762013-11-06 16:36:36 +000055 ScopedFd fd(open(filename, O_RDONLY, 0));
56 if (fd.get() == -1) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070057 *error_msg = StringPrintf("Unable to open '%s' : %s", filename, strerror(errno));
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070058 return -1;
59 }
Vladimir Markofd995762013-11-06 16:36:36 +000060 int n = TEMP_FAILURE_RETRY(read(fd.get(), magic, sizeof(*magic)));
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070061 if (n != sizeof(*magic)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070062 *error_msg = StringPrintf("Failed to find magic in '%s'", filename);
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070063 return -1;
64 }
Vladimir Markofd995762013-11-06 16:36:36 +000065 if (lseek(fd.get(), 0, SEEK_SET) != 0) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070066 *error_msg = StringPrintf("Failed to seek to beginning of file '%s' : %s", filename,
67 strerror(errno));
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070068 return -1;
69 }
Vladimir Markofd995762013-11-06 16:36:36 +000070 return fd.release();
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070071}
72
Ian Rogers8d31bbd2013-10-13 10:44:14 -070073bool DexFile::GetChecksum(const char* filename, uint32_t* checksum, std::string* error_msg) {
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070074 CHECK(checksum != NULL);
75 uint32_t magic;
Andreas Gampe833a4852014-05-21 18:46:59 -070076
77 // Strip ":...", which is the location
78 const char* zip_entry_name = kClassesDex;
79 const char* file_part = filename;
80 std::unique_ptr<const char> file_part_ptr;
81
82
83 if (IsMultiDexLocation(filename)) {
84 std::pair<const char*, const char*> pair = SplitMultiDexLocation(filename);
85 file_part_ptr.reset(pair.first);
86 file_part = pair.first;
87 zip_entry_name = pair.second;
88 }
89
90 ScopedFd fd(OpenAndReadMagic(file_part, &magic, error_msg));
Vladimir Markofd995762013-11-06 16:36:36 +000091 if (fd.get() == -1) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070092 DCHECK(!error_msg->empty());
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070093 return false;
94 }
95 if (IsZipMagic(magic)) {
Ian Rogers700a4022014-05-19 16:49:03 -070096 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(fd.release(), filename, error_msg));
Brian Carlstrom5b332c82012-02-01 15:02:31 -080097 if (zip_archive.get() == NULL) {
Andreas Gampe833a4852014-05-21 18:46:59 -070098 *error_msg = StringPrintf("Failed to open zip archive '%s'", file_part);
Brian Carlstrom5b332c82012-02-01 15:02:31 -080099 return false;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700100 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700101 std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(zip_entry_name, error_msg));
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800102 if (zip_entry.get() == NULL) {
Andreas Gampe833a4852014-05-21 18:46:59 -0700103 *error_msg = StringPrintf("Zip archive '%s' doesn't contain %s (error msg: %s)", file_part,
104 zip_entry_name, error_msg->c_str());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800105 return false;
106 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700107 *checksum = zip_entry->GetCrc32();
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800108 return true;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700109 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700110 if (IsDexMagic(magic)) {
Ian Rogers700a4022014-05-19 16:49:03 -0700111 std::unique_ptr<const DexFile> dex_file(DexFile::OpenFile(fd.release(), filename, false, error_msg));
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800112 if (dex_file.get() == NULL) {
113 return false;
114 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700115 *checksum = dex_file->GetHeader().checksum_;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800116 return true;
117 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700118 *error_msg = StringPrintf("Expected valid zip or dex file: '%s'", filename);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800119 return false;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700120}
121
Andreas Gampe833a4852014-05-21 18:46:59 -0700122bool DexFile::Open(const char* filename, const char* location, std::string* error_msg,
123 std::vector<const DexFile*>* dex_files) {
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700124 uint32_t magic;
Vladimir Markofd995762013-11-06 16:36:36 +0000125 ScopedFd fd(OpenAndReadMagic(filename, &magic, error_msg));
126 if (fd.get() == -1) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700127 DCHECK(!error_msg->empty());
Andreas Gampe833a4852014-05-21 18:46:59 -0700128 return false;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700129 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700130 if (IsZipMagic(magic)) {
Andreas Gampe833a4852014-05-21 18:46:59 -0700131 return DexFile::OpenZip(fd.release(), location, error_msg, dex_files);
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700132 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700133 if (IsDexMagic(magic)) {
Andreas Gampe833a4852014-05-21 18:46:59 -0700134 std::unique_ptr<const DexFile> dex_file(DexFile::OpenFile(fd.release(), location, true,
135 error_msg));
136 if (dex_file.get() != nullptr) {
137 dex_files->push_back(dex_file.release());
138 return true;
139 } else {
140 return false;
141 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700142 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700143 *error_msg = StringPrintf("Expected valid zip or dex file: '%s'", filename);
Alexander Ivchenkobacce5c2014-06-26 16:32:11 +0400144 return false;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700145}
146
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800147int DexFile::GetPermissions() const {
148 if (mem_map_.get() == NULL) {
149 return 0;
150 } else {
151 return mem_map_->GetProtect();
152 }
153}
154
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200155bool DexFile::IsReadOnly() const {
156 return GetPermissions() == PROT_READ;
157}
158
Brian Carlstrome0948e12013-08-29 09:36:15 -0700159bool DexFile::EnableWrite() 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 | PROT_WRITE);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200165 }
166}
167
Brian Carlstrome0948e12013-08-29 09:36:15 -0700168bool DexFile::DisableWrite() const {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200169 CHECK(!IsReadOnly());
170 if (mem_map_.get() == NULL) {
171 return false;
172 } else {
Brian Carlstrome0948e12013-08-29 09:36:15 -0700173 return mem_map_->Protect(PROT_READ);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200174 }
175}
176
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700177const DexFile* DexFile::OpenFile(int fd, const char* location, bool verify,
178 std::string* error_msg) {
179 CHECK(location != nullptr);
Ian Rogers700a4022014-05-19 16:49:03 -0700180 std::unique_ptr<MemMap> map;
Vladimir Markofd995762013-11-06 16:36:36 +0000181 {
182 ScopedFd delayed_close(fd);
183 struct stat sbuf;
184 memset(&sbuf, 0, sizeof(sbuf));
185 if (fstat(fd, &sbuf) == -1) {
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -0800186 *error_msg = StringPrintf("DexFile: fstat '%s' failed: %s", location, strerror(errno));
Vladimir Markofd995762013-11-06 16:36:36 +0000187 return nullptr;
188 }
189 if (S_ISDIR(sbuf.st_mode)) {
190 *error_msg = StringPrintf("Attempt to mmap directory '%s'", location);
191 return nullptr;
192 }
193 size_t length = sbuf.st_size;
194 map.reset(MemMap::MapFile(length, PROT_READ, MAP_PRIVATE, fd, 0, location, error_msg));
195 if (map.get() == nullptr) {
196 DCHECK(!error_msg->empty());
197 return nullptr;
198 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700199 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800200
201 if (map->Size() < sizeof(DexFile::Header)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700202 *error_msg = StringPrintf(
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -0800203 "DexFile: failed to open dex file '%s' that is too short to have a header", location);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700204 return nullptr;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800205 }
206
207 const Header* dex_header = reinterpret_cast<const Header*>(map->Begin());
208
Andreas Gampe928f72b2014-09-09 19:53:48 -0700209 std::unique_ptr<const DexFile> dex_file(OpenMemory(location, dex_header->checksum_, map.release(),
210 error_msg));
211 if (dex_file.get() == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700212 *error_msg = StringPrintf("Failed to open dex file '%s' from memory: %s", location,
213 error_msg->c_str());
214 return nullptr;
jeffhaof6174e82012-01-31 16:14:17 -0800215 }
jeffhao54c1ceb2012-02-01 11:45:32 -0800216
Andreas Gampe928f72b2014-09-09 19:53:48 -0700217 if (verify && !DexFileVerifier::Verify(dex_file.get(), dex_file->Begin(), dex_file->Size(),
218 location, error_msg)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700219 return nullptr;
jeffhao54c1ceb2012-02-01 11:45:32 -0800220 }
221
Andreas Gampe928f72b2014-09-09 19:53:48 -0700222 return dex_file.release();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700223}
224
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700225const char* DexFile::kClassesDex = "classes.dex";
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700226
Andreas Gampe833a4852014-05-21 18:46:59 -0700227bool DexFile::OpenZip(int fd, const std::string& location, std::string* error_msg,
228 std::vector<const DexFile*>* dex_files) {
Ian Rogers700a4022014-05-19 16:49:03 -0700229 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(fd, location.c_str(), error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700230 if (zip_archive.get() == nullptr) {
231 DCHECK(!error_msg->empty());
Andreas Gampe833a4852014-05-21 18:46:59 -0700232 return false;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700233 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700234 return DexFile::OpenFromZip(*zip_archive, location, error_msg, dex_files);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800235}
236
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800237const DexFile* DexFile::OpenMemory(const std::string& location,
238 uint32_t location_checksum,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700239 MemMap* mem_map,
240 std::string* error_msg) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800241 return OpenMemory(mem_map->Begin(),
242 mem_map->Size(),
243 location,
244 location_checksum,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700245 mem_map,
246 error_msg);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800247}
248
Andreas Gampe833a4852014-05-21 18:46:59 -0700249const DexFile* DexFile::Open(const ZipArchive& zip_archive, const char* entry_name,
250 const std::string& location, std::string* error_msg,
251 ZipOpenErrorCode* error_code) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800252 CHECK(!location.empty());
Andreas Gampe833a4852014-05-21 18:46:59 -0700253 std::unique_ptr<ZipEntry> zip_entry(zip_archive.Find(entry_name, error_msg));
Elliott Hughes90a33692011-08-30 13:27:07 -0700254 if (zip_entry.get() == NULL) {
Andreas Gampe833a4852014-05-21 18:46:59 -0700255 *error_code = ZipOpenErrorCode::kEntryNotFound;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700256 return nullptr;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700257 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700258 std::unique_ptr<MemMap> map(zip_entry->ExtractToMemMap(location.c_str(), entry_name, error_msg));
Brian Carlstrom89521892011-12-07 22:05:07 -0800259 if (map.get() == NULL) {
Andreas Gampe833a4852014-05-21 18:46:59 -0700260 *error_msg = StringPrintf("Failed to extract '%s' from '%s': %s", entry_name, location.c_str(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700261 error_msg->c_str());
Andreas Gampe833a4852014-05-21 18:46:59 -0700262 *error_code = ZipOpenErrorCode::kExtractToMemoryError;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700263 return nullptr;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700264 }
Ian Rogers700a4022014-05-19 16:49:03 -0700265 std::unique_ptr<const DexFile> dex_file(OpenMemory(location, zip_entry->GetCrc32(), map.release(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700266 error_msg));
267 if (dex_file.get() == nullptr) {
268 *error_msg = StringPrintf("Failed to open dex file '%s' from memory: %s", location.c_str(),
269 error_msg->c_str());
Andreas Gampe833a4852014-05-21 18:46:59 -0700270 *error_code = ZipOpenErrorCode::kDexFileError;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700271 return nullptr;
jeffhaof6174e82012-01-31 16:14:17 -0800272 }
Brian Carlstrome0948e12013-08-29 09:36:15 -0700273 if (!dex_file->DisableWrite()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700274 *error_msg = StringPrintf("Failed to make dex file '%s' read only", location.c_str());
Andreas Gampe833a4852014-05-21 18:46:59 -0700275 *error_code = ZipOpenErrorCode::kMakeReadOnlyError;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700276 return nullptr;
Brian Carlstrome0948e12013-08-29 09:36:15 -0700277 }
278 CHECK(dex_file->IsReadOnly()) << location;
Brian Carlstromd6cec902014-05-25 16:08:51 -0700279 if (!DexFileVerifier::Verify(dex_file.get(), dex_file->Begin(), dex_file->Size(),
280 location.c_str(), error_msg)) {
Andreas Gampe833a4852014-05-21 18:46:59 -0700281 *error_code = ZipOpenErrorCode::kVerifyError;
Brian Carlstromd6cec902014-05-25 16:08:51 -0700282 return nullptr;
283 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700284 *error_code = ZipOpenErrorCode::kNoError;
Brian Carlstrome0948e12013-08-29 09:36:15 -0700285 return dex_file.release();
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700286}
287
Andreas Gampe833a4852014-05-21 18:46:59 -0700288bool DexFile::OpenFromZip(const ZipArchive& zip_archive, const std::string& location,
289 std::string* error_msg, std::vector<const DexFile*>* dex_files) {
290 ZipOpenErrorCode error_code;
291 std::unique_ptr<const DexFile> dex_file(Open(zip_archive, kClassesDex, location, error_msg,
292 &error_code));
293 if (dex_file.get() == nullptr) {
294 return false;
295 } else {
296 // Had at least classes.dex.
297 dex_files->push_back(dex_file.release());
298
299 // Now try some more.
300 size_t i = 2;
301
302 // We could try to avoid std::string allocations by working on a char array directly. As we
303 // do not expect a lot of iterations, this seems too involved and brittle.
304
305 while (i < 100) {
306 std::string name = StringPrintf("classes%zu.dex", i);
307 std::string fake_location = location + ":" + name;
308 std::unique_ptr<const DexFile> next_dex_file(Open(zip_archive, name.c_str(), fake_location,
309 error_msg, &error_code));
310 if (next_dex_file.get() == nullptr) {
311 if (error_code != ZipOpenErrorCode::kEntryNotFound) {
312 LOG(WARNING) << error_msg;
313 }
314 break;
315 } else {
316 dex_files->push_back(next_dex_file.release());
317 }
318
319 i++;
320 }
321
322 return true;
323 }
324}
325
326
Brian Carlstrom89521892011-12-07 22:05:07 -0800327const DexFile* DexFile::OpenMemory(const byte* base,
jeffhaof6174e82012-01-31 16:14:17 -0800328 size_t size,
Brian Carlstrom89521892011-12-07 22:05:07 -0800329 const std::string& location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800330 uint32_t location_checksum,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700331 MemMap* mem_map, std::string* error_msg) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700332 CHECK_ALIGNED(base, 4); // various dex file structures must be word aligned
Ian Rogers700a4022014-05-19 16:49:03 -0700333 std::unique_ptr<DexFile> dex_file(new DexFile(base, size, location, location_checksum, mem_map));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700334 if (!dex_file->Init(error_msg)) {
335 return nullptr;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700336 } else {
Brian Carlstromf615a612011-07-23 12:50:34 -0700337 return dex_file.release();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700338 }
339}
340
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800341DexFile::DexFile(const byte* base, size_t size,
342 const std::string& location,
343 uint32_t location_checksum,
344 MemMap* mem_map)
345 : begin_(base),
346 size_(size),
347 location_(location),
348 location_checksum_(location_checksum),
349 mem_map_(mem_map),
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800350 header_(reinterpret_cast<const Header*>(base)),
351 string_ids_(reinterpret_cast<const StringId*>(base + header_->string_ids_off_)),
352 type_ids_(reinterpret_cast<const TypeId*>(base + header_->type_ids_off_)),
353 field_ids_(reinterpret_cast<const FieldId*>(base + header_->field_ids_off_)),
354 method_ids_(reinterpret_cast<const MethodId*>(base + header_->method_ids_off_)),
355 proto_ids_(reinterpret_cast<const ProtoId*>(base + header_->proto_ids_off_)),
Ian Rogers68b56852014-08-29 20:19:11 -0700356 class_defs_(reinterpret_cast<const ClassDef*>(base + header_->class_defs_off_)),
357 find_class_def_misses_(0),
358 class_def_index_(nullptr),
359 build_class_def_index_mutex_("DexFile index creation mutex") {
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800360 CHECK(begin_ != NULL) << GetLocation();
361 CHECK_GT(size_, 0U) << GetLocation();
362}
363
Jesse Wilson6bf19152011-09-29 13:12:33 -0400364DexFile::~DexFile() {
Elliott Hughes8cef0b82011-10-11 19:24:00 -0700365 // We don't call DeleteGlobalRef on dex_object_ because we're only called by DestroyJavaVM, and
366 // that's only called after DetachCurrentThread, which means there's no JNIEnv. We could
367 // re-attach, but cleaning up these global references is not obviously useful. It's not as if
368 // the global reference table is otherwise empty!
Ian Rogers68b56852014-08-29 20:19:11 -0700369 // Remove the index if one were created.
370 delete class_def_index_.LoadRelaxed();
Jesse Wilson6bf19152011-09-29 13:12:33 -0400371}
372
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700373bool DexFile::Init(std::string* error_msg) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700374 if (!CheckMagicAndVersion(error_msg)) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700375 return false;
376 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700377 return true;
378}
379
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700380bool DexFile::CheckMagicAndVersion(std::string* error_msg) const {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800381 if (!IsMagicValid(header_->magic_)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700382 std::ostringstream oss;
383 oss << "Unrecognized magic number in " << GetLocation() << ":"
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800384 << " " << header_->magic_[0]
385 << " " << header_->magic_[1]
386 << " " << header_->magic_[2]
387 << " " << header_->magic_[3];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700388 *error_msg = oss.str();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700389 return false;
390 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800391 if (!IsVersionValid(header_->magic_)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700392 std::ostringstream oss;
393 oss << "Unrecognized version number in " << GetLocation() << ":"
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800394 << " " << header_->magic_[4]
395 << " " << header_->magic_[5]
396 << " " << header_->magic_[6]
397 << " " << header_->magic_[7];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700398 *error_msg = oss.str();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700399 return false;
400 }
401 return true;
402}
403
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800404bool DexFile::IsMagicValid(const byte* magic) {
405 return (memcmp(magic, kDexMagic, sizeof(kDexMagic)) == 0);
406}
407
408bool DexFile::IsVersionValid(const byte* magic) {
409 const byte* version = &magic[sizeof(kDexMagic)];
410 return (memcmp(version, kDexMagicVersion, sizeof(kDexMagicVersion)) == 0);
411}
412
Ian Rogersd81871c2011-10-03 13:57:23 -0700413uint32_t DexFile::GetVersion() const {
414 const char* version = reinterpret_cast<const char*>(&GetHeader().magic_[sizeof(kDexMagic)]);
415 return atoi(version);
416}
417
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700418const DexFile::ClassDef* DexFile::FindClassDef(const char* descriptor) const {
Ian Rogers68b56852014-08-29 20:19:11 -0700419 // If we have an index lookup the descriptor via that as its constant time to search.
420 Index* index = class_def_index_.LoadSequentiallyConsistent();
421 if (index != nullptr) {
422 auto it = index->find(descriptor);
423 return (it == index->end()) ? nullptr : it->second;
424 }
425 // Fast path for rate no class defs case.
426 uint32_t num_class_defs = NumClassDefs();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700427 if (num_class_defs == 0) {
Ian Rogers68b56852014-08-29 20:19:11 -0700428 return nullptr;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700429 }
Ian Rogers68b56852014-08-29 20:19:11 -0700430 // Search for class def with 2 binary searches and then a linear search.
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700431 const StringId* string_id = FindStringId(descriptor);
Ian Rogers68b56852014-08-29 20:19:11 -0700432 if (string_id != nullptr) {
433 const TypeId* type_id = FindTypeId(GetIndexForStringId(*string_id));
434 if (type_id != nullptr) {
435 uint16_t type_idx = GetIndexForTypeId(*type_id);
436 for (size_t i = 0; i < num_class_defs; ++i) {
437 const ClassDef& class_def = GetClassDef(i);
438 if (class_def.class_idx_ == type_idx) {
439 return &class_def;
440 }
441 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700442 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700443 }
Ian Rogers68b56852014-08-29 20:19:11 -0700444 // A miss. If we've had kMaxFailedDexClassDefLookups misses then build an index to speed things
445 // up. This isn't done eagerly at construction as construction is not performed in multi-threaded
446 // sections of tools like dex2oat. If we're lazy we hopefully increase the chance of balancing
447 // out which thread builds the index.
448 find_class_def_misses_++;
449 const uint32_t kMaxFailedDexClassDefLookups = 100;
450 if (find_class_def_misses_ > kMaxFailedDexClassDefLookups) {
451 MutexLock mu(Thread::Current(), build_class_def_index_mutex_);
452 // Are we the first ones building the index?
453 if (class_def_index_.LoadSequentiallyConsistent() == nullptr) {
454 index = new Index(num_class_defs);
455 for (uint32_t i = 0; i < num_class_defs; ++i) {
456 const ClassDef& class_def = GetClassDef(i);
457 const char* descriptor = GetClassDescriptor(class_def);
458 index->insert(std::make_pair(descriptor, &class_def));
459 }
460 class_def_index_.StoreSequentiallyConsistent(index);
461 }
462 }
463 return nullptr;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700464}
465
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700466const DexFile::ClassDef* DexFile::FindClassDef(uint16_t type_idx) const {
467 size_t num_class_defs = NumClassDefs();
468 for (size_t i = 0; i < num_class_defs; ++i) {
469 const ClassDef& class_def = GetClassDef(i);
470 if (class_def.class_idx_ == type_idx) {
471 return &class_def;
472 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700473 }
474 return NULL;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700475}
476
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800477const DexFile::FieldId* DexFile::FindFieldId(const DexFile::TypeId& declaring_klass,
478 const DexFile::StringId& name,
479 const DexFile::TypeId& type) const {
480 // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx
481 const uint16_t class_idx = GetIndexForTypeId(declaring_klass);
482 const uint32_t name_idx = GetIndexForStringId(name);
483 const uint16_t type_idx = GetIndexForTypeId(type);
Ian Rogersf8582c32013-05-29 16:33:03 -0700484 int32_t lo = 0;
485 int32_t hi = NumFieldIds() - 1;
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800486 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700487 int32_t mid = (hi + lo) / 2;
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800488 const DexFile::FieldId& field = GetFieldId(mid);
489 if (class_idx > field.class_idx_) {
490 lo = mid + 1;
491 } else if (class_idx < field.class_idx_) {
492 hi = mid - 1;
493 } else {
494 if (name_idx > field.name_idx_) {
495 lo = mid + 1;
496 } else if (name_idx < field.name_idx_) {
497 hi = mid - 1;
498 } else {
499 if (type_idx > field.type_idx_) {
500 lo = mid + 1;
501 } else if (type_idx < field.type_idx_) {
502 hi = mid - 1;
503 } else {
504 return &field;
505 }
506 }
507 }
508 }
509 return NULL;
510}
511
512const DexFile::MethodId* DexFile::FindMethodId(const DexFile::TypeId& declaring_klass,
Ian Rogers0571d352011-11-03 19:51:38 -0700513 const DexFile::StringId& name,
514 const DexFile::ProtoId& signature) const {
515 // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800516 const uint16_t class_idx = GetIndexForTypeId(declaring_klass);
Ian Rogers0571d352011-11-03 19:51:38 -0700517 const uint32_t name_idx = GetIndexForStringId(name);
518 const uint16_t proto_idx = GetIndexForProtoId(signature);
Ian Rogersf8582c32013-05-29 16:33:03 -0700519 int32_t lo = 0;
520 int32_t hi = NumMethodIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700521 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700522 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700523 const DexFile::MethodId& method = GetMethodId(mid);
524 if (class_idx > method.class_idx_) {
525 lo = mid + 1;
526 } else if (class_idx < method.class_idx_) {
527 hi = mid - 1;
528 } else {
529 if (name_idx > method.name_idx_) {
530 lo = mid + 1;
531 } else if (name_idx < method.name_idx_) {
532 hi = mid - 1;
533 } else {
534 if (proto_idx > method.proto_idx_) {
535 lo = mid + 1;
536 } else if (proto_idx < method.proto_idx_) {
537 hi = mid - 1;
538 } else {
539 return &method;
540 }
541 }
542 }
543 }
544 return NULL;
545}
546
Ian Rogers637c65b2013-05-31 11:46:00 -0700547const DexFile::StringId* DexFile::FindStringId(const char* string) const {
Ian Rogersf8582c32013-05-29 16:33:03 -0700548 int32_t lo = 0;
549 int32_t hi = NumStringIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700550 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700551 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700552 const DexFile::StringId& str_id = GetStringId(mid);
Ian Rogerscf5077a2013-10-31 12:37:54 -0700553 const char* str = GetStringData(str_id);
Ian Rogers637c65b2013-05-31 11:46:00 -0700554 int compare = CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(string, str);
555 if (compare > 0) {
556 lo = mid + 1;
557 } else if (compare < 0) {
558 hi = mid - 1;
559 } else {
560 return &str_id;
561 }
562 }
563 return NULL;
564}
565
566const DexFile::StringId* DexFile::FindStringId(const uint16_t* string) const {
567 int32_t lo = 0;
568 int32_t hi = NumStringIds() - 1;
569 while (hi >= lo) {
570 int32_t mid = (hi + lo) / 2;
Ian Rogers637c65b2013-05-31 11:46:00 -0700571 const DexFile::StringId& str_id = GetStringId(mid);
Ian Rogerscf5077a2013-10-31 12:37:54 -0700572 const char* str = GetStringData(str_id);
Ian Rogers637c65b2013-05-31 11:46:00 -0700573 int compare = CompareModifiedUtf8ToUtf16AsCodePointValues(str, string);
Ian Rogers0571d352011-11-03 19:51:38 -0700574 if (compare > 0) {
575 lo = mid + 1;
576 } else if (compare < 0) {
577 hi = mid - 1;
578 } else {
579 return &str_id;
580 }
581 }
582 return NULL;
583}
584
585const DexFile::TypeId* DexFile::FindTypeId(uint32_t string_idx) const {
Ian Rogersf8582c32013-05-29 16:33:03 -0700586 int32_t lo = 0;
587 int32_t hi = NumTypeIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700588 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700589 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700590 const TypeId& type_id = GetTypeId(mid);
591 if (string_idx > type_id.descriptor_idx_) {
592 lo = mid + 1;
593 } else if (string_idx < type_id.descriptor_idx_) {
594 hi = mid - 1;
595 } else {
596 return &type_id;
597 }
598 }
599 return NULL;
600}
601
602const DexFile::ProtoId* DexFile::FindProtoId(uint16_t return_type_idx,
Vladimir Marko5c96e6b2013-11-14 15:34:17 +0000603 const uint16_t* signature_type_idxs,
604 uint32_t signature_length) const {
Ian Rogersf8582c32013-05-29 16:33:03 -0700605 int32_t lo = 0;
606 int32_t hi = NumProtoIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700607 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700608 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700609 const DexFile::ProtoId& proto = GetProtoId(mid);
610 int compare = return_type_idx - proto.return_type_idx_;
611 if (compare == 0) {
612 DexFileParameterIterator it(*this, proto);
613 size_t i = 0;
Vladimir Marko5c96e6b2013-11-14 15:34:17 +0000614 while (it.HasNext() && i < signature_length && compare == 0) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800615 compare = signature_type_idxs[i] - it.GetTypeIdx();
Ian Rogers0571d352011-11-03 19:51:38 -0700616 it.Next();
617 i++;
618 }
619 if (compare == 0) {
620 if (it.HasNext()) {
621 compare = -1;
Vladimir Marko5c96e6b2013-11-14 15:34:17 +0000622 } else if (i < signature_length) {
Ian Rogers0571d352011-11-03 19:51:38 -0700623 compare = 1;
624 }
625 }
626 }
627 if (compare > 0) {
628 lo = mid + 1;
629 } else if (compare < 0) {
630 hi = mid - 1;
631 } else {
632 return &proto;
633 }
634 }
635 return NULL;
636}
637
638// Given a signature place the type ids into the given vector
Ian Rogersd91d6d62013-09-25 20:26:14 -0700639bool DexFile::CreateTypeList(const StringPiece& signature, uint16_t* return_type_idx,
640 std::vector<uint16_t>* param_type_idxs) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700641 if (signature[0] != '(') {
642 return false;
643 }
644 size_t offset = 1;
645 size_t end = signature.size();
646 bool process_return = false;
647 while (offset < end) {
Vladimir Markoe9c36b32013-11-21 15:49:16 +0000648 size_t start_offset = offset;
Ian Rogers0571d352011-11-03 19:51:38 -0700649 char c = signature[offset];
650 offset++;
651 if (c == ')') {
652 process_return = true;
653 continue;
654 }
Ian Rogers0571d352011-11-03 19:51:38 -0700655 while (c == '[') { // process array prefix
656 if (offset >= end) { // expect some descriptor following [
657 return false;
658 }
659 c = signature[offset];
660 offset++;
Ian Rogers0571d352011-11-03 19:51:38 -0700661 }
662 if (c == 'L') { // process type descriptors
663 do {
664 if (offset >= end) { // unexpected early termination of descriptor
665 return false;
666 }
667 c = signature[offset];
668 offset++;
Ian Rogers0571d352011-11-03 19:51:38 -0700669 } while (c != ';');
670 }
Vladimir Markoe9c36b32013-11-21 15:49:16 +0000671 // TODO: avoid creating a std::string just to get a 0-terminated char array
672 std::string descriptor(signature.data() + start_offset, offset - start_offset);
Ian Rogers637c65b2013-05-31 11:46:00 -0700673 const DexFile::StringId* string_id = FindStringId(descriptor.c_str());
Ian Rogers0571d352011-11-03 19:51:38 -0700674 if (string_id == NULL) {
675 return false;
676 }
677 const DexFile::TypeId* type_id = FindTypeId(GetIndexForStringId(*string_id));
678 if (type_id == NULL) {
679 return false;
680 }
681 uint16_t type_idx = GetIndexForTypeId(*type_id);
682 if (!process_return) {
683 param_type_idxs->push_back(type_idx);
684 } else {
685 *return_type_idx = type_idx;
686 return offset == end; // return true if the signature had reached a sensible end
687 }
688 }
689 return false; // failed to correctly parse return type
690}
691
Ian Rogersd91d6d62013-09-25 20:26:14 -0700692const Signature DexFile::CreateSignature(const StringPiece& signature) const {
693 uint16_t return_type_idx;
694 std::vector<uint16_t> param_type_indices;
695 bool success = CreateTypeList(signature, &return_type_idx, &param_type_indices);
696 if (!success) {
697 return Signature::NoSignature();
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700698 }
Ian Rogersd91d6d62013-09-25 20:26:14 -0700699 const ProtoId* proto_id = FindProtoId(return_type_idx, param_type_indices);
700 if (proto_id == NULL) {
701 return Signature::NoSignature();
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700702 }
Ian Rogersd91d6d62013-09-25 20:26:14 -0700703 return Signature(this, *proto_id);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700704}
705
Ian Rogersef7d42f2014-01-06 12:55:46 -0800706int32_t DexFile::GetLineNumFromPC(mirror::ArtMethod* method, uint32_t rel_pc) const {
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -0700707 // For native method, lineno should be -2 to indicate it is native. Note that
708 // "line number == -2" is how libcore tells from StackTraceElement.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700709 if (method->GetCodeItemOffset() == 0) {
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -0700710 return -2;
711 }
712
TDYa127c8dc1012012-04-19 07:03:33 -0700713 const CodeItem* code_item = GetCodeItem(method->GetCodeItemOffset());
Elliott Hughescaf76542012-06-28 16:08:22 -0700714 DCHECK(code_item != NULL) << PrettyMethod(method) << " " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700715
716 // A method with no line number info should return -1
717 LineNumFromPcContext context(rel_pc, -1);
TDYa127c8dc1012012-04-19 07:03:33 -0700718 DecodeDebugInfo(code_item, method->IsStatic(), method->GetDexMethodIndex(), LineNumForPcCb,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800719 NULL, &context);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700720 return context.line_num_;
721}
722
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700723int32_t DexFile::FindTryItem(const CodeItem &code_item, uint32_t address) {
Ian Rogers0571d352011-11-03 19:51:38 -0700724 // Note: Signed type is important for max and min.
725 int32_t min = 0;
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700726 int32_t max = code_item.tries_size_ - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700727
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700728 while (min <= max) {
729 int32_t mid = min + ((max - min) / 2);
730
731 const art::DexFile::TryItem* ti = GetTryItems(code_item, mid);
732 uint32_t start = ti->start_addr_;
733 uint32_t end = start + ti->insn_count_;
734
Ian Rogers0571d352011-11-03 19:51:38 -0700735 if (address < start) {
736 max = mid - 1;
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700737 } else if (address >= end) {
738 min = mid + 1;
739 } else { // We have a winner!
740 return mid;
Ian Rogers0571d352011-11-03 19:51:38 -0700741 }
742 }
743 // No match.
744 return -1;
745}
746
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700747int32_t DexFile::FindCatchHandlerOffset(const CodeItem &code_item, uint32_t address) {
748 int32_t try_item = FindTryItem(code_item, address);
749 if (try_item == -1) {
750 return -1;
751 } else {
752 return DexFile::GetTryItems(code_item, try_item)->handler_off_;
753 }
754}
755
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800756void DexFile::DecodeDebugInfo0(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes2435a572012-02-17 16:07:41 -0800757 DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb,
758 void* context, const byte* stream, LocalInfo* local_in_reg) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700759 uint32_t line = DecodeUnsignedLeb128(&stream);
760 uint32_t parameters_size = DecodeUnsignedLeb128(&stream);
761 uint16_t arg_reg = code_item->registers_size_ - code_item->ins_size_;
762 uint32_t address = 0;
Elliott Hughes30646832011-10-13 16:59:46 -0700763 bool need_locals = (local_cb != NULL);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700764
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800765 if (!is_static) {
Elliott Hughes30646832011-10-13 16:59:46 -0700766 if (need_locals) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800767 const char* descriptor = GetMethodDeclaringClassDescriptor(GetMethodId(method_idx));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700768 local_in_reg[arg_reg].name_ = "this";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800769 local_in_reg[arg_reg].descriptor_ = descriptor;
Elliott Hughes392b1242011-11-30 13:55:50 -0800770 local_in_reg[arg_reg].signature_ = NULL;
Elliott Hughes30646832011-10-13 16:59:46 -0700771 local_in_reg[arg_reg].start_address_ = 0;
772 local_in_reg[arg_reg].is_live_ = true;
773 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700774 arg_reg++;
775 }
776
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800777 DexFileParameterIterator it(*this, GetMethodPrototype(GetMethodId(method_idx)));
Ian Rogers0571d352011-11-03 19:51:38 -0700778 for (uint32_t i = 0; i < parameters_size && it.HasNext(); ++i, it.Next()) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700779 if (arg_reg >= code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700780 LOG(ERROR) << "invalid stream - arg reg >= reg size (" << arg_reg
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800781 << " >= " << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700782 return;
783 }
Elliott Hughes392b1242011-11-30 13:55:50 -0800784 uint32_t id = DecodeUnsignedLeb128P1(&stream);
Ian Rogers0571d352011-11-03 19:51:38 -0700785 const char* descriptor = it.GetDescriptor();
Elliott Hughes392b1242011-11-30 13:55:50 -0800786 if (need_locals && id != kDexNoIndex) {
Ian Rogers0571d352011-11-03 19:51:38 -0700787 const char* name = StringDataByIdx(id);
Elliott Hughes30646832011-10-13 16:59:46 -0700788 local_in_reg[arg_reg].name_ = name;
789 local_in_reg[arg_reg].descriptor_ = descriptor;
Elliott Hughes392b1242011-11-30 13:55:50 -0800790 local_in_reg[arg_reg].signature_ = NULL;
Elliott Hughes30646832011-10-13 16:59:46 -0700791 local_in_reg[arg_reg].start_address_ = address;
792 local_in_reg[arg_reg].is_live_ = true;
793 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700794 switch (*descriptor) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700795 case 'D':
796 case 'J':
797 arg_reg += 2;
798 break;
799 default:
800 arg_reg += 1;
801 break;
802 }
803 }
804
Ian Rogers0571d352011-11-03 19:51:38 -0700805 if (it.HasNext()) {
Brian Carlstromf79fccb2014-02-20 08:55:10 -0800806 LOG(ERROR) << "invalid stream - problem with parameter iterator in " << GetLocation()
807 << " for method " << PrettyMethod(method_idx, *this);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700808 return;
809 }
810
811 for (;;) {
812 uint8_t opcode = *stream++;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700813 uint16_t reg;
Jeff Haob7cefc72013-11-14 14:51:09 -0800814 uint32_t name_idx;
815 uint32_t descriptor_idx;
816 uint32_t signature_idx = 0;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700817
Shih-wei Liao195487c2011-08-20 13:29:04 -0700818 switch (opcode) {
819 case DBG_END_SEQUENCE:
820 return;
821
822 case DBG_ADVANCE_PC:
823 address += DecodeUnsignedLeb128(&stream);
824 break;
825
826 case DBG_ADVANCE_LINE:
Shih-wei Liao8a05d272011-10-15 18:45:43 -0700827 line += DecodeSignedLeb128(&stream);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700828 break;
829
830 case DBG_START_LOCAL:
831 case DBG_START_LOCAL_EXTENDED:
832 reg = DecodeUnsignedLeb128(&stream);
833 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700834 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800835 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700836 return;
837 }
838
jeffhaof8728872011-10-28 19:11:13 -0700839 name_idx = DecodeUnsignedLeb128P1(&stream);
840 descriptor_idx = DecodeUnsignedLeb128P1(&stream);
841 if (opcode == DBG_START_LOCAL_EXTENDED) {
842 signature_idx = DecodeUnsignedLeb128P1(&stream);
843 }
844
Shih-wei Liao195487c2011-08-20 13:29:04 -0700845 // Emit what was previously there, if anything
Elliott Hughes30646832011-10-13 16:59:46 -0700846 if (need_locals) {
Elliott Hughes2435a572012-02-17 16:07:41 -0800847 InvokeLocalCbIfLive(context, reg, address, local_in_reg, local_cb);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700848
Ian Rogers0571d352011-11-03 19:51:38 -0700849 local_in_reg[reg].name_ = StringDataByIdx(name_idx);
850 local_in_reg[reg].descriptor_ = StringByTypeIdx(descriptor_idx);
Elliott Hughes30646832011-10-13 16:59:46 -0700851 if (opcode == DBG_START_LOCAL_EXTENDED) {
Ian Rogers0571d352011-11-03 19:51:38 -0700852 local_in_reg[reg].signature_ = StringDataByIdx(signature_idx);
Elliott Hughes30646832011-10-13 16:59:46 -0700853 }
854 local_in_reg[reg].start_address_ = address;
855 local_in_reg[reg].is_live_ = true;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700856 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700857 break;
858
859 case DBG_END_LOCAL:
860 reg = DecodeUnsignedLeb128(&stream);
861 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700862 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800863 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700864 return;
865 }
866
Elliott Hughes30646832011-10-13 16:59:46 -0700867 if (need_locals) {
Elliott Hughes2435a572012-02-17 16:07:41 -0800868 InvokeLocalCbIfLive(context, reg, address, local_in_reg, local_cb);
Elliott Hughes30646832011-10-13 16:59:46 -0700869 local_in_reg[reg].is_live_ = false;
870 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700871 break;
872
873 case DBG_RESTART_LOCAL:
874 reg = DecodeUnsignedLeb128(&stream);
875 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700876 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800877 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700878 return;
879 }
880
Elliott Hughes30646832011-10-13 16:59:46 -0700881 if (need_locals) {
882 if (local_in_reg[reg].name_ == NULL || local_in_reg[reg].descriptor_ == NULL) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800883 LOG(ERROR) << "invalid stream - no name or descriptor in " << GetLocation();
Elliott Hughes30646832011-10-13 16:59:46 -0700884 return;
885 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700886
Elliott Hughes30646832011-10-13 16:59:46 -0700887 // If the register is live, the "restart" is superfluous,
888 // and we don't want to mess with the existing start address.
889 if (!local_in_reg[reg].is_live_) {
890 local_in_reg[reg].start_address_ = address;
891 local_in_reg[reg].is_live_ = true;
892 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700893 }
894 break;
895
896 case DBG_SET_PROLOGUE_END:
897 case DBG_SET_EPILOGUE_BEGIN:
898 case DBG_SET_FILE:
899 break;
900
Shih-wei Liao8e1b4ff2011-10-15 15:43:51 -0700901 default: {
902 int adjopcode = opcode - DBG_FIRST_SPECIAL;
903
Shih-wei Liao195487c2011-08-20 13:29:04 -0700904 address += adjopcode / DBG_LINE_RANGE;
905 line += DBG_LINE_BASE + (adjopcode % DBG_LINE_RANGE);
906
Elliott Hughes2435a572012-02-17 16:07:41 -0800907 if (position_cb != NULL) {
908 if (position_cb(context, address, line)) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700909 // early exit
910 return;
911 }
912 }
913 break;
Shih-wei Liao8e1b4ff2011-10-15 15:43:51 -0700914 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700915 }
916 }
917}
918
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800919void DexFile::DecodeDebugInfo(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes2435a572012-02-17 16:07:41 -0800920 DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb,
921 void* context) const {
Sebastien Hertzcb19ebf2014-03-11 15:26:35 +0100922 DCHECK(code_item != nullptr);
Ian Rogers0571d352011-11-03 19:51:38 -0700923 const byte* stream = GetDebugInfoStream(code_item);
Ian Rogers700a4022014-05-19 16:49:03 -0700924 std::unique_ptr<LocalInfo[]> local_in_reg(local_cb != NULL ?
Brian Carlstrome0948e12013-08-29 09:36:15 -0700925 new LocalInfo[code_item->registers_size_] :
926 NULL);
Ian Rogers0571d352011-11-03 19:51:38 -0700927 if (stream != NULL) {
Elliott Hughesee0fa762012-03-26 17:12:41 -0700928 DecodeDebugInfo0(code_item, is_static, method_idx, position_cb, local_cb, context, stream, &local_in_reg[0]);
Ian Rogers0571d352011-11-03 19:51:38 -0700929 }
930 for (int reg = 0; reg < code_item->registers_size_; reg++) {
Elliott Hughesee0fa762012-03-26 17:12:41 -0700931 InvokeLocalCbIfLive(context, reg, code_item->insns_size_in_code_units_, &local_in_reg[0], local_cb);
Ian Rogers0571d352011-11-03 19:51:38 -0700932 }
933}
934
Elliott Hughes2435a572012-02-17 16:07:41 -0800935bool DexFile::LineNumForPcCb(void* raw_context, uint32_t address, uint32_t line_num) {
936 LineNumFromPcContext* context = reinterpret_cast<LineNumFromPcContext*>(raw_context);
Ian Rogers0571d352011-11-03 19:51:38 -0700937
938 // We know that this callback will be called in
939 // ascending address order, so keep going until we find
940 // a match or we've just gone past it.
941 if (address > context->address_) {
942 // The line number from the previous positions callback
943 // wil be the final result.
944 return true;
945 } else {
946 context->line_num_ = line_num;
947 return address == context->address_;
948 }
949}
950
Andreas Gampe833a4852014-05-21 18:46:59 -0700951bool DexFile::IsMultiDexLocation(const char* location) {
952 return strrchr(location, kMultiDexSeparator) != nullptr;
953}
954
955std::pair<const char*, const char*> DexFile::SplitMultiDexLocation(
956 const char* location) {
957 const char* colon_ptr = strrchr(location, kMultiDexSeparator);
958
959 // Check it's synthetic.
960 CHECK_NE(colon_ptr, static_cast<const char*>(nullptr));
961
962 size_t colon_index = colon_ptr - location;
963 char* tmp = new char[colon_index + 1];
964 strncpy(tmp, location, colon_index);
965 tmp[colon_index] = 0;
966
967 return std::make_pair(tmp, colon_ptr + 1);
968}
969
Calin Juravle4e1d5792014-07-15 23:56:47 +0100970std::string DexFile::GetMultiDexClassesDexName(size_t number, const char* dex_location) {
971 if (number == 0) {
972 return dex_location;
973 } else {
974 return StringPrintf("%s" kMultiDexSeparatorString "classes%zu.dex", dex_location, number + 1);
975 }
976}
977
978std::string DexFile::GetDexCanonicalLocation(const char* dex_location) {
979 CHECK_NE(dex_location, static_cast<const char*>(nullptr));
980 char* path = nullptr;
981 if (!IsMultiDexLocation(dex_location)) {
982 path = realpath(dex_location, nullptr);
983 } else {
984 std::pair<const char*, const char*> pair = DexFile::SplitMultiDexLocation(dex_location);
985 const char* dex_real_location(realpath(pair.first, nullptr));
986 delete pair.first;
987 if (dex_real_location != nullptr) {
988 int length = strlen(dex_real_location) + strlen(pair.second) + strlen(kMultiDexSeparatorString) + 1;
989 char* multidex_canonical_location = reinterpret_cast<char*>(malloc(sizeof(char) * length));
990 snprintf(multidex_canonical_location, length, "%s" kMultiDexSeparatorString "%s", dex_real_location, pair.second);
991 free(const_cast<char*>(dex_real_location));
992 path = multidex_canonical_location;
993 }
994 }
995
996 // If realpath fails then we just copy the argument.
997 std::string result(path == nullptr ? dex_location : path);
998 free(path);
999 return result;
1000}
1001
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001002std::ostream& operator<<(std::ostream& os, const DexFile& dex_file) {
1003 os << StringPrintf("[DexFile: %s dex-checksum=%08x location-checksum=%08x %p-%p]",
1004 dex_file.GetLocation().c_str(),
1005 dex_file.GetHeader().checksum_, dex_file.GetLocationChecksum(),
1006 dex_file.Begin(), dex_file.Begin() + dex_file.Size());
1007 return os;
1008}
Calin Juravle4e1d5792014-07-15 23:56:47 +01001009
Ian Rogersd91d6d62013-09-25 20:26:14 -07001010std::string Signature::ToString() const {
1011 if (dex_file_ == nullptr) {
1012 CHECK(proto_id_ == nullptr);
1013 return "<no signature>";
1014 }
1015 const DexFile::TypeList* params = dex_file_->GetProtoParameters(*proto_id_);
1016 std::string result;
1017 if (params == nullptr) {
1018 result += "()";
1019 } else {
1020 result += "(";
1021 for (uint32_t i = 0; i < params->Size(); ++i) {
1022 result += dex_file_->StringByTypeIdx(params->GetTypeItem(i).type_idx_);
1023 }
1024 result += ")";
1025 }
1026 result += dex_file_->StringByTypeIdx(proto_id_->return_type_idx_);
1027 return result;
1028}
1029
Vladimir Markod9cffea2013-11-25 15:08:02 +00001030bool Signature::operator==(const StringPiece& rhs) const {
1031 if (dex_file_ == nullptr) {
1032 return false;
1033 }
1034 StringPiece tail(rhs);
1035 if (!tail.starts_with("(")) {
1036 return false; // Invalid signature
1037 }
1038 tail.remove_prefix(1); // "(";
1039 const DexFile::TypeList* params = dex_file_->GetProtoParameters(*proto_id_);
1040 if (params != nullptr) {
1041 for (uint32_t i = 0; i < params->Size(); ++i) {
1042 StringPiece param(dex_file_->StringByTypeIdx(params->GetTypeItem(i).type_idx_));
1043 if (!tail.starts_with(param)) {
1044 return false;
1045 }
1046 tail.remove_prefix(param.length());
1047 }
1048 }
1049 if (!tail.starts_with(")")) {
1050 return false;
1051 }
1052 tail.remove_prefix(1); // ")";
1053 return tail == dex_file_->StringByTypeIdx(proto_id_->return_type_idx_);
1054}
1055
Ian Rogersd91d6d62013-09-25 20:26:14 -07001056std::ostream& operator<<(std::ostream& os, const Signature& sig) {
1057 return os << sig.ToString();
1058}
1059
Ian Rogers0571d352011-11-03 19:51:38 -07001060// Decodes the header section from the class data bytes.
1061void ClassDataItemIterator::ReadClassDataHeader() {
1062 CHECK(ptr_pos_ != NULL);
1063 header_.static_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_);
1064 header_.instance_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_);
1065 header_.direct_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_);
1066 header_.virtual_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_);
1067}
1068
1069void ClassDataItemIterator::ReadClassDataField() {
1070 field_.field_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_);
1071 field_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_);
Brian Carlstrom68adbe42012-05-11 17:18:08 -07001072 if (last_idx_ != 0 && field_.field_idx_delta_ == 0) {
Andreas Gampe4fdbba02014-06-19 20:24:22 -07001073 LOG(WARNING) << "Duplicate field in " << dex_file_.GetLocation();
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -07001074 }
Ian Rogers0571d352011-11-03 19:51:38 -07001075}
1076
1077void ClassDataItemIterator::ReadClassDataMethod() {
1078 method_.method_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_);
1079 method_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_);
1080 method_.code_off_ = DecodeUnsignedLeb128(&ptr_pos_);
Brian Carlstrom68adbe42012-05-11 17:18:08 -07001081 if (last_idx_ != 0 && method_.method_idx_delta_ == 0) {
Andreas Gampe4fdbba02014-06-19 20:24:22 -07001082 LOG(WARNING) << "Duplicate method in " << dex_file_.GetLocation();
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -07001083 }
Ian Rogers0571d352011-11-03 19:51:38 -07001084}
1085
1086// Read a signed integer. "zwidth" is the zero-based byte count.
1087static int32_t ReadSignedInt(const byte* ptr, int zwidth) {
1088 int32_t val = 0;
1089 for (int i = zwidth; i >= 0; --i) {
1090 val = ((uint32_t)val >> 8) | (((int32_t)*ptr++) << 24);
1091 }
1092 val >>= (3 - zwidth) * 8;
1093 return val;
1094}
1095
1096// Read an unsigned integer. "zwidth" is the zero-based byte count,
1097// "fill_on_right" indicates which side we want to zero-fill from.
1098static uint32_t ReadUnsignedInt(const byte* ptr, int zwidth, bool fill_on_right) {
1099 uint32_t val = 0;
1100 if (!fill_on_right) {
1101 for (int i = zwidth; i >= 0; --i) {
1102 val = (val >> 8) | (((uint32_t)*ptr++) << 24);
1103 }
1104 val >>= (3 - zwidth) * 8;
1105 } else {
1106 for (int i = zwidth; i >= 0; --i) {
1107 val = (val >> 8) | (((uint32_t)*ptr++) << 24);
1108 }
1109 }
1110 return val;
1111}
1112
1113// Read a signed long. "zwidth" is the zero-based byte count.
1114static int64_t ReadSignedLong(const byte* ptr, int zwidth) {
1115 int64_t val = 0;
1116 for (int i = zwidth; i >= 0; --i) {
1117 val = ((uint64_t)val >> 8) | (((int64_t)*ptr++) << 56);
1118 }
1119 val >>= (7 - zwidth) * 8;
1120 return val;
1121}
1122
1123// Read an unsigned long. "zwidth" is the zero-based byte count,
1124// "fill_on_right" indicates which side we want to zero-fill from.
1125static uint64_t ReadUnsignedLong(const byte* ptr, int zwidth, bool fill_on_right) {
1126 uint64_t val = 0;
1127 if (!fill_on_right) {
1128 for (int i = zwidth; i >= 0; --i) {
1129 val = (val >> 8) | (((uint64_t)*ptr++) << 56);
1130 }
1131 val >>= (7 - zwidth) * 8;
1132 } else {
1133 for (int i = zwidth; i >= 0; --i) {
1134 val = (val >> 8) | (((uint64_t)*ptr++) << 56);
1135 }
1136 }
1137 return val;
1138}
1139
1140EncodedStaticFieldValueIterator::EncodedStaticFieldValueIterator(const DexFile& dex_file,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001141 Handle<mirror::DexCache>* dex_cache,
1142 Handle<mirror::ClassLoader>* class_loader,
Ian Rogersca190662012-06-26 15:45:57 -07001143 ClassLinker* linker,
1144 const DexFile::ClassDef& class_def)
Brian Carlstrom88f36542012-10-16 23:24:21 -07001145 : dex_file_(dex_file), dex_cache_(dex_cache), class_loader_(class_loader), linker_(linker),
1146 array_size_(), pos_(-1), type_(kByte) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001147 DCHECK(dex_cache != nullptr);
1148 DCHECK(class_loader != nullptr);
Ian Rogers0571d352011-11-03 19:51:38 -07001149 ptr_ = dex_file.GetEncodedStaticFieldValuesArray(class_def);
1150 if (ptr_ == NULL) {
1151 array_size_ = 0;
1152 } else {
1153 array_size_ = DecodeUnsignedLeb128(&ptr_);
1154 }
1155 if (array_size_ > 0) {
1156 Next();
1157 }
1158}
1159
1160void EncodedStaticFieldValueIterator::Next() {
1161 pos_++;
1162 if (pos_ >= array_size_) {
1163 return;
1164 }
1165 byte value_type = *ptr_++;
1166 byte value_arg = value_type >> kEncodedValueArgShift;
1167 size_t width = value_arg + 1; // assume and correct later
Brian Carlstrom88f36542012-10-16 23:24:21 -07001168 type_ = static_cast<ValueType>(value_type & kEncodedValueTypeMask);
Ian Rogers0571d352011-11-03 19:51:38 -07001169 switch (type_) {
1170 case kBoolean:
1171 jval_.i = (value_arg != 0) ? 1 : 0;
1172 width = 0;
1173 break;
1174 case kByte:
1175 jval_.i = ReadSignedInt(ptr_, value_arg);
1176 CHECK(IsInt(8, jval_.i));
1177 break;
1178 case kShort:
1179 jval_.i = ReadSignedInt(ptr_, value_arg);
1180 CHECK(IsInt(16, jval_.i));
1181 break;
1182 case kChar:
1183 jval_.i = ReadUnsignedInt(ptr_, value_arg, false);
1184 CHECK(IsUint(16, jval_.i));
1185 break;
1186 case kInt:
1187 jval_.i = ReadSignedInt(ptr_, value_arg);
1188 break;
1189 case kLong:
1190 jval_.j = ReadSignedLong(ptr_, value_arg);
1191 break;
1192 case kFloat:
1193 jval_.i = ReadUnsignedInt(ptr_, value_arg, true);
1194 break;
1195 case kDouble:
1196 jval_.j = ReadUnsignedLong(ptr_, value_arg, true);
1197 break;
1198 case kString:
1199 case kType:
Ian Rogers0571d352011-11-03 19:51:38 -07001200 jval_.i = ReadUnsignedInt(ptr_, value_arg, false);
1201 break;
1202 case kField:
Brian Carlstrom88f36542012-10-16 23:24:21 -07001203 case kMethod:
1204 case kEnum:
Ian Rogers0571d352011-11-03 19:51:38 -07001205 case kArray:
1206 case kAnnotation:
1207 UNIMPLEMENTED(FATAL) << ": type " << type_;
1208 break;
1209 case kNull:
1210 jval_.l = NULL;
1211 width = 0;
1212 break;
1213 default:
1214 LOG(FATAL) << "Unreached";
1215 }
1216 ptr_ += width;
1217}
1218
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001219template<bool kTransactionActive>
Hiroshi Yamauchi67ef46a2014-08-21 15:59:43 -07001220void EncodedStaticFieldValueIterator::ReadValueToField(Handle<mirror::ArtField> field) const {
Ian Rogers0571d352011-11-03 19:51:38 -07001221 switch (type_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001222 case kBoolean: field->SetBoolean<kTransactionActive>(field->GetDeclaringClass(), jval_.z); break;
1223 case kByte: field->SetByte<kTransactionActive>(field->GetDeclaringClass(), jval_.b); break;
1224 case kShort: field->SetShort<kTransactionActive>(field->GetDeclaringClass(), jval_.s); break;
1225 case kChar: field->SetChar<kTransactionActive>(field->GetDeclaringClass(), jval_.c); break;
1226 case kInt: field->SetInt<kTransactionActive>(field->GetDeclaringClass(), jval_.i); break;
1227 case kLong: field->SetLong<kTransactionActive>(field->GetDeclaringClass(), jval_.j); break;
1228 case kFloat: field->SetFloat<kTransactionActive>(field->GetDeclaringClass(), jval_.f); break;
1229 case kDouble: field->SetDouble<kTransactionActive>(field->GetDeclaringClass(), jval_.d); break;
1230 case kNull: field->SetObject<kTransactionActive>(field->GetDeclaringClass(), NULL); break;
Ian Rogers0571d352011-11-03 19:51:38 -07001231 case kString: {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001232 CHECK(!kMovingFields);
1233 mirror::String* resolved = linker_->ResolveString(dex_file_, jval_.i, *dex_cache_);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001234 field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved);
Ian Rogers0571d352011-11-03 19:51:38 -07001235 break;
1236 }
Brian Carlstrom88f36542012-10-16 23:24:21 -07001237 case kType: {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001238 CHECK(!kMovingFields);
1239 mirror::Class* resolved = linker_->ResolveType(dex_file_, jval_.i, *dex_cache_,
1240 *class_loader_);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001241 field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved);
Brian Carlstrom88f36542012-10-16 23:24:21 -07001242 break;
1243 }
Ian Rogers0571d352011-11-03 19:51:38 -07001244 default: UNIMPLEMENTED(FATAL) << ": type " << type_;
1245 }
1246}
Hiroshi Yamauchi67ef46a2014-08-21 15:59:43 -07001247template void EncodedStaticFieldValueIterator::ReadValueToField<true>(Handle<mirror::ArtField> field) const;
1248template void EncodedStaticFieldValueIterator::ReadValueToField<false>(Handle<mirror::ArtField> field) const;
Ian Rogers0571d352011-11-03 19:51:38 -07001249
1250CatchHandlerIterator::CatchHandlerIterator(const DexFile::CodeItem& code_item, uint32_t address) {
1251 handler_.address_ = -1;
1252 int32_t offset = -1;
1253
1254 // Short-circuit the overwhelmingly common cases.
1255 switch (code_item.tries_size_) {
1256 case 0:
1257 break;
1258 case 1: {
1259 const DexFile::TryItem* tries = DexFile::GetTryItems(code_item, 0);
1260 uint32_t start = tries->start_addr_;
1261 if (address >= start) {
1262 uint32_t end = start + tries->insn_count_;
1263 if (address < end) {
1264 offset = tries->handler_off_;
1265 }
1266 }
1267 break;
1268 }
1269 default:
Ian Rogersdbbc99d2013-04-18 16:51:54 -07001270 offset = DexFile::FindCatchHandlerOffset(code_item, address);
Ian Rogers0571d352011-11-03 19:51:38 -07001271 }
Logan Chien736df022012-04-27 16:25:57 +08001272 Init(code_item, offset);
1273}
1274
1275CatchHandlerIterator::CatchHandlerIterator(const DexFile::CodeItem& code_item,
1276 const DexFile::TryItem& try_item) {
1277 handler_.address_ = -1;
1278 Init(code_item, try_item.handler_off_);
1279}
1280
1281void CatchHandlerIterator::Init(const DexFile::CodeItem& code_item,
1282 int32_t offset) {
Ian Rogers0571d352011-11-03 19:51:38 -07001283 if (offset >= 0) {
Logan Chien736df022012-04-27 16:25:57 +08001284 Init(DexFile::GetCatchHandlerData(code_item, offset));
Ian Rogers0571d352011-11-03 19:51:38 -07001285 } else {
1286 // Not found, initialize as empty
1287 current_data_ = NULL;
1288 remaining_count_ = -1;
1289 catch_all_ = false;
1290 DCHECK(!HasNext());
1291 }
1292}
1293
1294void CatchHandlerIterator::Init(const byte* handler_data) {
1295 current_data_ = handler_data;
1296 remaining_count_ = DecodeSignedLeb128(&current_data_);
1297
1298 // If remaining_count_ is non-positive, then it is the negative of
1299 // the number of catch types, and the catches are followed by a
1300 // catch-all handler.
1301 if (remaining_count_ <= 0) {
1302 catch_all_ = true;
1303 remaining_count_ = -remaining_count_;
1304 } else {
1305 catch_all_ = false;
1306 }
1307 Next();
1308}
1309
1310void CatchHandlerIterator::Next() {
1311 if (remaining_count_ > 0) {
1312 handler_.type_idx_ = DecodeUnsignedLeb128(&current_data_);
1313 handler_.address_ = DecodeUnsignedLeb128(&current_data_);
1314 remaining_count_--;
1315 return;
1316 }
1317
1318 if (catch_all_) {
1319 handler_.type_idx_ = DexFile::kDexNoIndex16;
1320 handler_.address_ = DecodeUnsignedLeb128(&current_data_);
1321 catch_all_ = false;
1322 return;
1323 }
1324
1325 // no more handler
1326 remaining_count_ = -1;
1327}
1328
Carl Shapiro1fb86202011-06-27 17:43:13 -07001329} // namespace art