blob: 9d835f4abcfd25b5ef5ac410e04ee41db0c97cff [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;
Vladimir Markobe4e6432014-09-05 14:01:17 +010080 std::string file_part_storage;
Andreas Gampe833a4852014-05-21 18:46:59 -070081
Vladimir Markobe4e6432014-09-05 14:01:17 +010082 if (DexFile::IsMultiDexLocation(filename)) {
83 file_part_storage = GetBaseLocation(filename);
84 file_part = file_part_storage.c_str();
85 zip_entry_name = filename + file_part_storage.size() + 1;
86 DCHECK_EQ(zip_entry_name[-1], kMultiDexSeparator);
Andreas Gampe833a4852014-05-21 18:46:59 -070087 }
88
89 ScopedFd fd(OpenAndReadMagic(file_part, &magic, error_msg));
Vladimir Markofd995762013-11-06 16:36:36 +000090 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)) {
Ian Rogers700a4022014-05-19 16:49:03 -070095 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(fd.release(), filename, error_msg));
Brian Carlstrom5b332c82012-02-01 15:02:31 -080096 if (zip_archive.get() == NULL) {
Andreas Gampe833a4852014-05-21 18:46:59 -070097 *error_msg = StringPrintf("Failed to open zip archive '%s'", file_part);
Brian Carlstrom5b332c82012-02-01 15:02:31 -080098 return false;
Brian Carlstrom78128a62011-09-15 17:21:19 -070099 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700100 std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(zip_entry_name, error_msg));
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800101 if (zip_entry.get() == NULL) {
Andreas Gampe833a4852014-05-21 18:46:59 -0700102 *error_msg = StringPrintf("Zip archive '%s' doesn't contain %s (error msg: %s)", file_part,
103 zip_entry_name, error_msg->c_str());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800104 return false;
105 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700106 *checksum = zip_entry->GetCrc32();
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800107 return true;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700108 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700109 if (IsDexMagic(magic)) {
Ian Rogers700a4022014-05-19 16:49:03 -0700110 std::unique_ptr<const DexFile> dex_file(DexFile::OpenFile(fd.release(), filename, false, error_msg));
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800111 if (dex_file.get() == NULL) {
112 return false;
113 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700114 *checksum = dex_file->GetHeader().checksum_;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800115 return true;
116 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700117 *error_msg = StringPrintf("Expected valid zip or dex file: '%s'", filename);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800118 return false;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700119}
120
Andreas Gampe833a4852014-05-21 18:46:59 -0700121bool DexFile::Open(const char* filename, const char* location, std::string* error_msg,
122 std::vector<const DexFile*>* dex_files) {
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());
Andreas Gampe833a4852014-05-21 18:46:59 -0700127 return false;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700128 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700129 if (IsZipMagic(magic)) {
Andreas Gampe833a4852014-05-21 18:46:59 -0700130 return DexFile::OpenZip(fd.release(), location, error_msg, dex_files);
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700131 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700132 if (IsDexMagic(magic)) {
Andreas Gampe833a4852014-05-21 18:46:59 -0700133 std::unique_ptr<const DexFile> dex_file(DexFile::OpenFile(fd.release(), location, true,
134 error_msg));
135 if (dex_file.get() != nullptr) {
136 dex_files->push_back(dex_file.release());
137 return true;
138 } else {
139 return false;
140 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700141 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700142 *error_msg = StringPrintf("Expected valid zip or dex file: '%s'", filename);
Alexander Ivchenkobacce5c2014-06-26 16:32:11 +0400143 return false;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700144}
145
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800146int DexFile::GetPermissions() const {
147 if (mem_map_.get() == NULL) {
148 return 0;
149 } else {
150 return mem_map_->GetProtect();
151 }
152}
153
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200154bool DexFile::IsReadOnly() const {
155 return GetPermissions() == PROT_READ;
156}
157
Brian Carlstrome0948e12013-08-29 09:36:15 -0700158bool DexFile::EnableWrite() const {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200159 CHECK(IsReadOnly());
160 if (mem_map_.get() == NULL) {
161 return false;
162 } else {
Brian Carlstrome0948e12013-08-29 09:36:15 -0700163 return mem_map_->Protect(PROT_READ | PROT_WRITE);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200164 }
165}
166
Brian Carlstrome0948e12013-08-29 09:36:15 -0700167bool DexFile::DisableWrite() const {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200168 CHECK(!IsReadOnly());
169 if (mem_map_.get() == NULL) {
170 return false;
171 } else {
Brian Carlstrome0948e12013-08-29 09:36:15 -0700172 return mem_map_->Protect(PROT_READ);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200173 }
174}
175
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700176const DexFile* DexFile::OpenFile(int fd, const char* location, bool verify,
177 std::string* error_msg) {
178 CHECK(location != nullptr);
Ian Rogers700a4022014-05-19 16:49:03 -0700179 std::unique_ptr<MemMap> map;
Vladimir Markofd995762013-11-06 16:36:36 +0000180 {
181 ScopedFd delayed_close(fd);
182 struct stat sbuf;
183 memset(&sbuf, 0, sizeof(sbuf));
184 if (fstat(fd, &sbuf) == -1) {
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -0800185 *error_msg = StringPrintf("DexFile: fstat '%s' failed: %s", location, strerror(errno));
Vladimir Markofd995762013-11-06 16:36:36 +0000186 return nullptr;
187 }
188 if (S_ISDIR(sbuf.st_mode)) {
189 *error_msg = StringPrintf("Attempt to mmap directory '%s'", location);
190 return nullptr;
191 }
192 size_t length = sbuf.st_size;
193 map.reset(MemMap::MapFile(length, PROT_READ, MAP_PRIVATE, fd, 0, location, error_msg));
194 if (map.get() == nullptr) {
195 DCHECK(!error_msg->empty());
196 return nullptr;
197 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700198 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800199
200 if (map->Size() < sizeof(DexFile::Header)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700201 *error_msg = StringPrintf(
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -0800202 "DexFile: failed to open dex file '%s' that is too short to have a header", location);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700203 return nullptr;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800204 }
205
206 const Header* dex_header = reinterpret_cast<const Header*>(map->Begin());
207
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700208 const DexFile* dex_file = OpenMemory(location, dex_header->checksum_, map.release(), error_msg);
209 if (dex_file == nullptr) {
210 *error_msg = StringPrintf("Failed to open dex file '%s' from memory: %s", location,
211 error_msg->c_str());
212 return nullptr;
jeffhaof6174e82012-01-31 16:14:17 -0800213 }
jeffhao54c1ceb2012-02-01 11:45:32 -0800214
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700215 if (verify && !DexFileVerifier::Verify(dex_file, dex_file->Begin(), dex_file->Size(), location,
216 error_msg)) {
217 return nullptr;
jeffhao54c1ceb2012-02-01 11:45:32 -0800218 }
219
jeffhaof6174e82012-01-31 16:14:17 -0800220 return dex_file;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700221}
222
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700223const char* DexFile::kClassesDex = "classes.dex";
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700224
Andreas Gampe833a4852014-05-21 18:46:59 -0700225bool DexFile::OpenZip(int fd, const std::string& location, std::string* error_msg,
226 std::vector<const DexFile*>* dex_files) {
Ian Rogers700a4022014-05-19 16:49:03 -0700227 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(fd, location.c_str(), error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700228 if (zip_archive.get() == nullptr) {
229 DCHECK(!error_msg->empty());
Andreas Gampe833a4852014-05-21 18:46:59 -0700230 return false;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700231 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700232 return DexFile::OpenFromZip(*zip_archive, location, error_msg, dex_files);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800233}
234
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800235const DexFile* DexFile::OpenMemory(const std::string& location,
236 uint32_t location_checksum,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700237 MemMap* mem_map,
238 std::string* error_msg) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800239 return OpenMemory(mem_map->Begin(),
240 mem_map->Size(),
241 location,
242 location_checksum,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700243 mem_map,
Andreas Gampe15a33b32014-11-06 16:52:58 -0800244 nullptr,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700245 error_msg);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800246}
247
Andreas Gampe833a4852014-05-21 18:46:59 -0700248const DexFile* DexFile::Open(const ZipArchive& zip_archive, const char* entry_name,
249 const std::string& location, std::string* error_msg,
250 ZipOpenErrorCode* error_code) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800251 CHECK(!location.empty());
Andreas Gampe833a4852014-05-21 18:46:59 -0700252 std::unique_ptr<ZipEntry> zip_entry(zip_archive.Find(entry_name, error_msg));
Elliott Hughes90a33692011-08-30 13:27:07 -0700253 if (zip_entry.get() == NULL) {
Andreas Gampe833a4852014-05-21 18:46:59 -0700254 *error_code = ZipOpenErrorCode::kEntryNotFound;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700255 return nullptr;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700256 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700257 std::unique_ptr<MemMap> map(zip_entry->ExtractToMemMap(location.c_str(), entry_name, error_msg));
Brian Carlstrom89521892011-12-07 22:05:07 -0800258 if (map.get() == NULL) {
Andreas Gampe833a4852014-05-21 18:46:59 -0700259 *error_msg = StringPrintf("Failed to extract '%s' from '%s': %s", entry_name, location.c_str(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700260 error_msg->c_str());
Andreas Gampe833a4852014-05-21 18:46:59 -0700261 *error_code = ZipOpenErrorCode::kExtractToMemoryError;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700262 return nullptr;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700263 }
Ian Rogers700a4022014-05-19 16:49:03 -0700264 std::unique_ptr<const DexFile> dex_file(OpenMemory(location, zip_entry->GetCrc32(), map.release(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700265 error_msg));
266 if (dex_file.get() == nullptr) {
267 *error_msg = StringPrintf("Failed to open dex file '%s' from memory: %s", location.c_str(),
268 error_msg->c_str());
Andreas Gampe833a4852014-05-21 18:46:59 -0700269 *error_code = ZipOpenErrorCode::kDexFileError;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700270 return nullptr;
jeffhaof6174e82012-01-31 16:14:17 -0800271 }
Brian Carlstrome0948e12013-08-29 09:36:15 -0700272 if (!dex_file->DisableWrite()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700273 *error_msg = StringPrintf("Failed to make dex file '%s' read only", location.c_str());
Andreas Gampe833a4852014-05-21 18:46:59 -0700274 *error_code = ZipOpenErrorCode::kMakeReadOnlyError;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700275 return nullptr;
Brian Carlstrome0948e12013-08-29 09:36:15 -0700276 }
277 CHECK(dex_file->IsReadOnly()) << location;
Brian Carlstromd6cec902014-05-25 16:08:51 -0700278 if (!DexFileVerifier::Verify(dex_file.get(), dex_file->Begin(), dex_file->Size(),
279 location.c_str(), error_msg)) {
Andreas Gampe833a4852014-05-21 18:46:59 -0700280 *error_code = ZipOpenErrorCode::kVerifyError;
Brian Carlstromd6cec902014-05-25 16:08:51 -0700281 return nullptr;
282 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700283 *error_code = ZipOpenErrorCode::kNoError;
Brian Carlstrome0948e12013-08-29 09:36:15 -0700284 return dex_file.release();
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700285}
286
Andreas Gampe833a4852014-05-21 18:46:59 -0700287bool DexFile::OpenFromZip(const ZipArchive& zip_archive, const std::string& location,
288 std::string* error_msg, std::vector<const DexFile*>* dex_files) {
289 ZipOpenErrorCode error_code;
290 std::unique_ptr<const DexFile> dex_file(Open(zip_archive, kClassesDex, location, error_msg,
291 &error_code));
292 if (dex_file.get() == nullptr) {
293 return false;
294 } else {
295 // Had at least classes.dex.
296 dex_files->push_back(dex_file.release());
297
298 // Now try some more.
299 size_t i = 2;
300
301 // We could try to avoid std::string allocations by working on a char array directly. As we
302 // do not expect a lot of iterations, this seems too involved and brittle.
303
304 while (i < 100) {
305 std::string name = StringPrintf("classes%zu.dex", i);
Vladimir Markobe4e6432014-09-05 14:01:17 +0100306 std::string fake_location = location + kMultiDexSeparator + name;
Andreas Gampe833a4852014-05-21 18:46:59 -0700307 std::unique_ptr<const DexFile> next_dex_file(Open(zip_archive, name.c_str(), fake_location,
308 error_msg, &error_code));
309 if (next_dex_file.get() == nullptr) {
310 if (error_code != ZipOpenErrorCode::kEntryNotFound) {
311 LOG(WARNING) << error_msg;
312 }
313 break;
314 } else {
315 dex_files->push_back(next_dex_file.release());
316 }
317
318 i++;
319 }
320
321 return true;
322 }
323}
324
325
Brian Carlstrom89521892011-12-07 22:05:07 -0800326const DexFile* DexFile::OpenMemory(const byte* base,
jeffhaof6174e82012-01-31 16:14:17 -0800327 size_t size,
Brian Carlstrom89521892011-12-07 22:05:07 -0800328 const std::string& location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800329 uint32_t location_checksum,
Andreas Gampe15a33b32014-11-06 16:52:58 -0800330 MemMap* mem_map,
331 const OatFile* oat_file,
332 std::string* error_msg) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700333 CHECK_ALIGNED(base, 4); // various dex file structures must be word aligned
Andreas Gampe15a33b32014-11-06 16:52:58 -0800334 std::unique_ptr<DexFile> dex_file(
335 new DexFile(base, size, location, location_checksum, mem_map, oat_file));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700336 if (!dex_file->Init(error_msg)) {
337 return nullptr;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700338 } else {
Brian Carlstromf615a612011-07-23 12:50:34 -0700339 return dex_file.release();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700340 }
341}
342
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800343DexFile::DexFile(const byte* base, size_t size,
344 const std::string& location,
345 uint32_t location_checksum,
Andreas Gampe15a33b32014-11-06 16:52:58 -0800346 MemMap* mem_map,
347 const OatFile* oat_file)
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800348 : begin_(base),
349 size_(size),
350 location_(location),
351 location_checksum_(location_checksum),
352 mem_map_(mem_map),
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800353 header_(reinterpret_cast<const Header*>(base)),
354 string_ids_(reinterpret_cast<const StringId*>(base + header_->string_ids_off_)),
355 type_ids_(reinterpret_cast<const TypeId*>(base + header_->type_ids_off_)),
356 field_ids_(reinterpret_cast<const FieldId*>(base + header_->field_ids_off_)),
357 method_ids_(reinterpret_cast<const MethodId*>(base + header_->method_ids_off_)),
358 proto_ids_(reinterpret_cast<const ProtoId*>(base + header_->proto_ids_off_)),
Ian Rogerscc2f2392014-08-29 20:19:11 -0700359 class_defs_(reinterpret_cast<const ClassDef*>(base + header_->class_defs_off_)),
360 find_class_def_misses_(0),
361 class_def_index_(nullptr),
Andreas Gampe15a33b32014-11-06 16:52:58 -0800362 build_class_def_index_mutex_("DexFile index creation mutex"),
363 oat_file_(oat_file) {
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800364 CHECK(begin_ != NULL) << GetLocation();
365 CHECK_GT(size_, 0U) << GetLocation();
366}
367
Jesse Wilson6bf19152011-09-29 13:12:33 -0400368DexFile::~DexFile() {
Elliott Hughes8cef0b82011-10-11 19:24:00 -0700369 // We don't call DeleteGlobalRef on dex_object_ because we're only called by DestroyJavaVM, and
370 // that's only called after DetachCurrentThread, which means there's no JNIEnv. We could
371 // re-attach, but cleaning up these global references is not obviously useful. It's not as if
372 // the global reference table is otherwise empty!
Ian Rogerscc2f2392014-08-29 20:19:11 -0700373 // Remove the index if one were created.
374 delete class_def_index_.LoadRelaxed();
Jesse Wilson6bf19152011-09-29 13:12:33 -0400375}
376
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700377bool DexFile::Init(std::string* error_msg) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700378 if (!CheckMagicAndVersion(error_msg)) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700379 return false;
380 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700381 return true;
382}
383
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700384bool DexFile::CheckMagicAndVersion(std::string* error_msg) const {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800385 if (!IsMagicValid(header_->magic_)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700386 std::ostringstream oss;
387 oss << "Unrecognized magic number in " << GetLocation() << ":"
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800388 << " " << header_->magic_[0]
389 << " " << header_->magic_[1]
390 << " " << header_->magic_[2]
391 << " " << header_->magic_[3];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700392 *error_msg = oss.str();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700393 return false;
394 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800395 if (!IsVersionValid(header_->magic_)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700396 std::ostringstream oss;
397 oss << "Unrecognized version number in " << GetLocation() << ":"
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800398 << " " << header_->magic_[4]
399 << " " << header_->magic_[5]
400 << " " << header_->magic_[6]
401 << " " << header_->magic_[7];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700402 *error_msg = oss.str();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700403 return false;
404 }
405 return true;
406}
407
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800408bool DexFile::IsMagicValid(const byte* magic) {
409 return (memcmp(magic, kDexMagic, sizeof(kDexMagic)) == 0);
410}
411
412bool DexFile::IsVersionValid(const byte* magic) {
413 const byte* version = &magic[sizeof(kDexMagic)];
414 return (memcmp(version, kDexMagicVersion, sizeof(kDexMagicVersion)) == 0);
415}
416
Ian Rogersd81871c2011-10-03 13:57:23 -0700417uint32_t DexFile::GetVersion() const {
418 const char* version = reinterpret_cast<const char*>(&GetHeader().magic_[sizeof(kDexMagic)]);
419 return atoi(version);
420}
421
Mathieu Chartier564ff982014-11-06 16:35:45 -0800422const DexFile::ClassDef* DexFile::FindClassDef(const char* descriptor, size_t hash) const {
423 DCHECK_EQ(ComputeModifiedUtf8Hash(descriptor), hash);
Ian Rogerscc2f2392014-08-29 20:19:11 -0700424 // If we have an index lookup the descriptor via that as its constant time to search.
425 Index* index = class_def_index_.LoadSequentiallyConsistent();
426 if (index != nullptr) {
Mathieu Chartier564ff982014-11-06 16:35:45 -0800427 auto it = index->FindWithHash(descriptor, hash);
Ian Rogerscc2f2392014-08-29 20:19:11 -0700428 return (it == index->end()) ? nullptr : it->second;
429 }
430 // Fast path for rate no class defs case.
431 uint32_t num_class_defs = NumClassDefs();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700432 if (num_class_defs == 0) {
Ian Rogerscc2f2392014-08-29 20:19:11 -0700433 return nullptr;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700434 }
Ian Rogerscc2f2392014-08-29 20:19:11 -0700435 // Search for class def with 2 binary searches and then a linear search.
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700436 const StringId* string_id = FindStringId(descriptor);
Ian Rogerscc2f2392014-08-29 20:19:11 -0700437 if (string_id != nullptr) {
438 const TypeId* type_id = FindTypeId(GetIndexForStringId(*string_id));
439 if (type_id != nullptr) {
440 uint16_t type_idx = GetIndexForTypeId(*type_id);
441 for (size_t i = 0; i < num_class_defs; ++i) {
442 const ClassDef& class_def = GetClassDef(i);
443 if (class_def.class_idx_ == type_idx) {
444 return &class_def;
445 }
446 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700447 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700448 }
Ian Rogerscc2f2392014-08-29 20:19:11 -0700449 // A miss. If we've had kMaxFailedDexClassDefLookups misses then build an index to speed things
450 // up. This isn't done eagerly at construction as construction is not performed in multi-threaded
451 // sections of tools like dex2oat. If we're lazy we hopefully increase the chance of balancing
452 // out which thread builds the index.
453 find_class_def_misses_++;
454 const uint32_t kMaxFailedDexClassDefLookups = 100;
455 if (find_class_def_misses_ > kMaxFailedDexClassDefLookups) {
456 MutexLock mu(Thread::Current(), build_class_def_index_mutex_);
457 // Are we the first ones building the index?
458 if (class_def_index_.LoadSequentiallyConsistent() == nullptr) {
Mathieu Chartier564ff982014-11-06 16:35:45 -0800459 index = new Index;
Ian Rogerscc2f2392014-08-29 20:19:11 -0700460 for (uint32_t i = 0; i < num_class_defs; ++i) {
461 const ClassDef& class_def = GetClassDef(i);
462 const char* descriptor = GetClassDescriptor(class_def);
Mathieu Chartier564ff982014-11-06 16:35:45 -0800463 index->Insert(std::make_pair(descriptor, &class_def));
Ian Rogerscc2f2392014-08-29 20:19:11 -0700464 }
465 class_def_index_.StoreSequentiallyConsistent(index);
466 }
467 }
468 return nullptr;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700469}
470
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700471const DexFile::ClassDef* DexFile::FindClassDef(uint16_t type_idx) const {
472 size_t num_class_defs = NumClassDefs();
473 for (size_t i = 0; i < num_class_defs; ++i) {
474 const ClassDef& class_def = GetClassDef(i);
475 if (class_def.class_idx_ == type_idx) {
476 return &class_def;
477 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700478 }
479 return NULL;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700480}
481
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800482const DexFile::FieldId* DexFile::FindFieldId(const DexFile::TypeId& declaring_klass,
483 const DexFile::StringId& name,
484 const DexFile::TypeId& type) const {
485 // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx
486 const uint16_t class_idx = GetIndexForTypeId(declaring_klass);
487 const uint32_t name_idx = GetIndexForStringId(name);
488 const uint16_t type_idx = GetIndexForTypeId(type);
Ian Rogersf8582c32013-05-29 16:33:03 -0700489 int32_t lo = 0;
490 int32_t hi = NumFieldIds() - 1;
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800491 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700492 int32_t mid = (hi + lo) / 2;
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800493 const DexFile::FieldId& field = GetFieldId(mid);
494 if (class_idx > field.class_idx_) {
495 lo = mid + 1;
496 } else if (class_idx < field.class_idx_) {
497 hi = mid - 1;
498 } else {
499 if (name_idx > field.name_idx_) {
500 lo = mid + 1;
501 } else if (name_idx < field.name_idx_) {
502 hi = mid - 1;
503 } else {
504 if (type_idx > field.type_idx_) {
505 lo = mid + 1;
506 } else if (type_idx < field.type_idx_) {
507 hi = mid - 1;
508 } else {
509 return &field;
510 }
511 }
512 }
513 }
514 return NULL;
515}
516
517const DexFile::MethodId* DexFile::FindMethodId(const DexFile::TypeId& declaring_klass,
Ian Rogers0571d352011-11-03 19:51:38 -0700518 const DexFile::StringId& name,
519 const DexFile::ProtoId& signature) const {
520 // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800521 const uint16_t class_idx = GetIndexForTypeId(declaring_klass);
Ian Rogers0571d352011-11-03 19:51:38 -0700522 const uint32_t name_idx = GetIndexForStringId(name);
523 const uint16_t proto_idx = GetIndexForProtoId(signature);
Ian Rogersf8582c32013-05-29 16:33:03 -0700524 int32_t lo = 0;
525 int32_t hi = NumMethodIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700526 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700527 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700528 const DexFile::MethodId& method = GetMethodId(mid);
529 if (class_idx > method.class_idx_) {
530 lo = mid + 1;
531 } else if (class_idx < method.class_idx_) {
532 hi = mid - 1;
533 } else {
534 if (name_idx > method.name_idx_) {
535 lo = mid + 1;
536 } else if (name_idx < method.name_idx_) {
537 hi = mid - 1;
538 } else {
539 if (proto_idx > method.proto_idx_) {
540 lo = mid + 1;
541 } else if (proto_idx < method.proto_idx_) {
542 hi = mid - 1;
543 } else {
544 return &method;
545 }
546 }
547 }
548 }
549 return NULL;
550}
551
Ian Rogers637c65b2013-05-31 11:46:00 -0700552const DexFile::StringId* DexFile::FindStringId(const char* string) const {
Ian Rogersf8582c32013-05-29 16:33:03 -0700553 int32_t lo = 0;
554 int32_t hi = NumStringIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700555 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700556 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700557 const DexFile::StringId& str_id = GetStringId(mid);
Ian Rogerscf5077a2013-10-31 12:37:54 -0700558 const char* str = GetStringData(str_id);
Ian Rogers637c65b2013-05-31 11:46:00 -0700559 int compare = CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(string, str);
560 if (compare > 0) {
561 lo = mid + 1;
562 } else if (compare < 0) {
563 hi = mid - 1;
564 } else {
565 return &str_id;
566 }
567 }
568 return NULL;
569}
570
571const DexFile::StringId* DexFile::FindStringId(const uint16_t* string) const {
572 int32_t lo = 0;
573 int32_t hi = NumStringIds() - 1;
574 while (hi >= lo) {
575 int32_t mid = (hi + lo) / 2;
Ian Rogers637c65b2013-05-31 11:46:00 -0700576 const DexFile::StringId& str_id = GetStringId(mid);
Ian Rogerscf5077a2013-10-31 12:37:54 -0700577 const char* str = GetStringData(str_id);
Ian Rogers637c65b2013-05-31 11:46:00 -0700578 int compare = CompareModifiedUtf8ToUtf16AsCodePointValues(str, string);
Ian Rogers0571d352011-11-03 19:51:38 -0700579 if (compare > 0) {
580 lo = mid + 1;
581 } else if (compare < 0) {
582 hi = mid - 1;
583 } else {
584 return &str_id;
585 }
586 }
587 return NULL;
588}
589
590const DexFile::TypeId* DexFile::FindTypeId(uint32_t string_idx) const {
Ian Rogersf8582c32013-05-29 16:33:03 -0700591 int32_t lo = 0;
592 int32_t hi = NumTypeIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700593 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700594 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700595 const TypeId& type_id = GetTypeId(mid);
596 if (string_idx > type_id.descriptor_idx_) {
597 lo = mid + 1;
598 } else if (string_idx < type_id.descriptor_idx_) {
599 hi = mid - 1;
600 } else {
601 return &type_id;
602 }
603 }
604 return NULL;
605}
606
607const DexFile::ProtoId* DexFile::FindProtoId(uint16_t return_type_idx,
Vladimir Marko5c96e6b2013-11-14 15:34:17 +0000608 const uint16_t* signature_type_idxs,
609 uint32_t signature_length) const {
Ian Rogersf8582c32013-05-29 16:33:03 -0700610 int32_t lo = 0;
611 int32_t hi = NumProtoIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700612 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700613 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700614 const DexFile::ProtoId& proto = GetProtoId(mid);
615 int compare = return_type_idx - proto.return_type_idx_;
616 if (compare == 0) {
617 DexFileParameterIterator it(*this, proto);
618 size_t i = 0;
Vladimir Marko5c96e6b2013-11-14 15:34:17 +0000619 while (it.HasNext() && i < signature_length && compare == 0) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800620 compare = signature_type_idxs[i] - it.GetTypeIdx();
Ian Rogers0571d352011-11-03 19:51:38 -0700621 it.Next();
622 i++;
623 }
624 if (compare == 0) {
625 if (it.HasNext()) {
626 compare = -1;
Vladimir Marko5c96e6b2013-11-14 15:34:17 +0000627 } else if (i < signature_length) {
Ian Rogers0571d352011-11-03 19:51:38 -0700628 compare = 1;
629 }
630 }
631 }
632 if (compare > 0) {
633 lo = mid + 1;
634 } else if (compare < 0) {
635 hi = mid - 1;
636 } else {
637 return &proto;
638 }
639 }
640 return NULL;
641}
642
643// Given a signature place the type ids into the given vector
Ian Rogersd91d6d62013-09-25 20:26:14 -0700644bool DexFile::CreateTypeList(const StringPiece& signature, uint16_t* return_type_idx,
645 std::vector<uint16_t>* param_type_idxs) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700646 if (signature[0] != '(') {
647 return false;
648 }
649 size_t offset = 1;
650 size_t end = signature.size();
651 bool process_return = false;
652 while (offset < end) {
Vladimir Markoe9c36b32013-11-21 15:49:16 +0000653 size_t start_offset = offset;
Ian Rogers0571d352011-11-03 19:51:38 -0700654 char c = signature[offset];
655 offset++;
656 if (c == ')') {
657 process_return = true;
658 continue;
659 }
Ian Rogers0571d352011-11-03 19:51:38 -0700660 while (c == '[') { // process array prefix
661 if (offset >= end) { // expect some descriptor following [
662 return false;
663 }
664 c = signature[offset];
665 offset++;
Ian Rogers0571d352011-11-03 19:51:38 -0700666 }
667 if (c == 'L') { // process type descriptors
668 do {
669 if (offset >= end) { // unexpected early termination of descriptor
670 return false;
671 }
672 c = signature[offset];
673 offset++;
Ian Rogers0571d352011-11-03 19:51:38 -0700674 } while (c != ';');
675 }
Vladimir Markoe9c36b32013-11-21 15:49:16 +0000676 // TODO: avoid creating a std::string just to get a 0-terminated char array
677 std::string descriptor(signature.data() + start_offset, offset - start_offset);
Ian Rogers637c65b2013-05-31 11:46:00 -0700678 const DexFile::StringId* string_id = FindStringId(descriptor.c_str());
Ian Rogers0571d352011-11-03 19:51:38 -0700679 if (string_id == NULL) {
680 return false;
681 }
682 const DexFile::TypeId* type_id = FindTypeId(GetIndexForStringId(*string_id));
683 if (type_id == NULL) {
684 return false;
685 }
686 uint16_t type_idx = GetIndexForTypeId(*type_id);
687 if (!process_return) {
688 param_type_idxs->push_back(type_idx);
689 } else {
690 *return_type_idx = type_idx;
691 return offset == end; // return true if the signature had reached a sensible end
692 }
693 }
694 return false; // failed to correctly parse return type
695}
696
Ian Rogersd91d6d62013-09-25 20:26:14 -0700697const Signature DexFile::CreateSignature(const StringPiece& signature) const {
698 uint16_t return_type_idx;
699 std::vector<uint16_t> param_type_indices;
700 bool success = CreateTypeList(signature, &return_type_idx, &param_type_indices);
701 if (!success) {
702 return Signature::NoSignature();
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700703 }
Ian Rogersd91d6d62013-09-25 20:26:14 -0700704 const ProtoId* proto_id = FindProtoId(return_type_idx, param_type_indices);
705 if (proto_id == NULL) {
706 return Signature::NoSignature();
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700707 }
Ian Rogersd91d6d62013-09-25 20:26:14 -0700708 return Signature(this, *proto_id);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700709}
710
Ian Rogersef7d42f2014-01-06 12:55:46 -0800711int32_t DexFile::GetLineNumFromPC(mirror::ArtMethod* method, uint32_t rel_pc) const {
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -0700712 // For native method, lineno should be -2 to indicate it is native. Note that
713 // "line number == -2" is how libcore tells from StackTraceElement.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700714 if (method->GetCodeItemOffset() == 0) {
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -0700715 return -2;
716 }
717
TDYa127c8dc1012012-04-19 07:03:33 -0700718 const CodeItem* code_item = GetCodeItem(method->GetCodeItemOffset());
Elliott Hughescaf76542012-06-28 16:08:22 -0700719 DCHECK(code_item != NULL) << PrettyMethod(method) << " " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700720
721 // A method with no line number info should return -1
722 LineNumFromPcContext context(rel_pc, -1);
TDYa127c8dc1012012-04-19 07:03:33 -0700723 DecodeDebugInfo(code_item, method->IsStatic(), method->GetDexMethodIndex(), LineNumForPcCb,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800724 NULL, &context);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700725 return context.line_num_;
726}
727
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700728int32_t DexFile::FindTryItem(const CodeItem &code_item, uint32_t address) {
Ian Rogers0571d352011-11-03 19:51:38 -0700729 // Note: Signed type is important for max and min.
730 int32_t min = 0;
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700731 int32_t max = code_item.tries_size_ - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700732
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700733 while (min <= max) {
734 int32_t mid = min + ((max - min) / 2);
735
736 const art::DexFile::TryItem* ti = GetTryItems(code_item, mid);
737 uint32_t start = ti->start_addr_;
738 uint32_t end = start + ti->insn_count_;
739
Ian Rogers0571d352011-11-03 19:51:38 -0700740 if (address < start) {
741 max = mid - 1;
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700742 } else if (address >= end) {
743 min = mid + 1;
744 } else { // We have a winner!
745 return mid;
Ian Rogers0571d352011-11-03 19:51:38 -0700746 }
747 }
748 // No match.
749 return -1;
750}
751
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700752int32_t DexFile::FindCatchHandlerOffset(const CodeItem &code_item, uint32_t address) {
753 int32_t try_item = FindTryItem(code_item, address);
754 if (try_item == -1) {
755 return -1;
756 } else {
757 return DexFile::GetTryItems(code_item, try_item)->handler_off_;
758 }
759}
760
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800761void DexFile::DecodeDebugInfo0(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes2435a572012-02-17 16:07:41 -0800762 DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb,
763 void* context, const byte* stream, LocalInfo* local_in_reg) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700764 uint32_t line = DecodeUnsignedLeb128(&stream);
765 uint32_t parameters_size = DecodeUnsignedLeb128(&stream);
766 uint16_t arg_reg = code_item->registers_size_ - code_item->ins_size_;
767 uint32_t address = 0;
Elliott Hughes30646832011-10-13 16:59:46 -0700768 bool need_locals = (local_cb != NULL);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700769
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800770 if (!is_static) {
Elliott Hughes30646832011-10-13 16:59:46 -0700771 if (need_locals) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800772 const char* descriptor = GetMethodDeclaringClassDescriptor(GetMethodId(method_idx));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700773 local_in_reg[arg_reg].name_ = "this";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800774 local_in_reg[arg_reg].descriptor_ = descriptor;
Elliott Hughes392b1242011-11-30 13:55:50 -0800775 local_in_reg[arg_reg].signature_ = NULL;
Elliott Hughes30646832011-10-13 16:59:46 -0700776 local_in_reg[arg_reg].start_address_ = 0;
777 local_in_reg[arg_reg].is_live_ = true;
778 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700779 arg_reg++;
780 }
781
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800782 DexFileParameterIterator it(*this, GetMethodPrototype(GetMethodId(method_idx)));
Ian Rogers0571d352011-11-03 19:51:38 -0700783 for (uint32_t i = 0; i < parameters_size && it.HasNext(); ++i, it.Next()) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700784 if (arg_reg >= code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700785 LOG(ERROR) << "invalid stream - arg reg >= reg size (" << arg_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 }
Elliott Hughes392b1242011-11-30 13:55:50 -0800789 uint32_t id = DecodeUnsignedLeb128P1(&stream);
Ian Rogers0571d352011-11-03 19:51:38 -0700790 const char* descriptor = it.GetDescriptor();
Elliott Hughes392b1242011-11-30 13:55:50 -0800791 if (need_locals && id != kDexNoIndex) {
Ian Rogers0571d352011-11-03 19:51:38 -0700792 const char* name = StringDataByIdx(id);
Elliott Hughes30646832011-10-13 16:59:46 -0700793 local_in_reg[arg_reg].name_ = name;
794 local_in_reg[arg_reg].descriptor_ = descriptor;
Elliott Hughes392b1242011-11-30 13:55:50 -0800795 local_in_reg[arg_reg].signature_ = NULL;
Elliott Hughes30646832011-10-13 16:59:46 -0700796 local_in_reg[arg_reg].start_address_ = address;
797 local_in_reg[arg_reg].is_live_ = true;
798 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700799 switch (*descriptor) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700800 case 'D':
801 case 'J':
802 arg_reg += 2;
803 break;
804 default:
805 arg_reg += 1;
806 break;
807 }
808 }
809
Ian Rogers0571d352011-11-03 19:51:38 -0700810 if (it.HasNext()) {
Brian Carlstromf79fccb2014-02-20 08:55:10 -0800811 LOG(ERROR) << "invalid stream - problem with parameter iterator in " << GetLocation()
812 << " for method " << PrettyMethod(method_idx, *this);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700813 return;
814 }
815
816 for (;;) {
817 uint8_t opcode = *stream++;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700818 uint16_t reg;
Jeff Haob7cefc72013-11-14 14:51:09 -0800819 uint32_t name_idx;
820 uint32_t descriptor_idx;
821 uint32_t signature_idx = 0;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700822
Shih-wei Liao195487c2011-08-20 13:29:04 -0700823 switch (opcode) {
824 case DBG_END_SEQUENCE:
825 return;
826
827 case DBG_ADVANCE_PC:
828 address += DecodeUnsignedLeb128(&stream);
829 break;
830
831 case DBG_ADVANCE_LINE:
Shih-wei Liao8a05d272011-10-15 18:45:43 -0700832 line += DecodeSignedLeb128(&stream);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700833 break;
834
835 case DBG_START_LOCAL:
836 case DBG_START_LOCAL_EXTENDED:
837 reg = DecodeUnsignedLeb128(&stream);
838 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700839 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800840 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700841 return;
842 }
843
jeffhaof8728872011-10-28 19:11:13 -0700844 name_idx = DecodeUnsignedLeb128P1(&stream);
845 descriptor_idx = DecodeUnsignedLeb128P1(&stream);
846 if (opcode == DBG_START_LOCAL_EXTENDED) {
847 signature_idx = DecodeUnsignedLeb128P1(&stream);
848 }
849
Shih-wei Liao195487c2011-08-20 13:29:04 -0700850 // Emit what was previously there, if anything
Elliott Hughes30646832011-10-13 16:59:46 -0700851 if (need_locals) {
Elliott Hughes2435a572012-02-17 16:07:41 -0800852 InvokeLocalCbIfLive(context, reg, address, local_in_reg, local_cb);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700853
Ian Rogers0571d352011-11-03 19:51:38 -0700854 local_in_reg[reg].name_ = StringDataByIdx(name_idx);
855 local_in_reg[reg].descriptor_ = StringByTypeIdx(descriptor_idx);
Elliott Hughes30646832011-10-13 16:59:46 -0700856 if (opcode == DBG_START_LOCAL_EXTENDED) {
Ian Rogers0571d352011-11-03 19:51:38 -0700857 local_in_reg[reg].signature_ = StringDataByIdx(signature_idx);
Elliott Hughes30646832011-10-13 16:59:46 -0700858 }
859 local_in_reg[reg].start_address_ = address;
860 local_in_reg[reg].is_live_ = true;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700861 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700862 break;
863
864 case DBG_END_LOCAL:
865 reg = DecodeUnsignedLeb128(&stream);
866 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700867 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800868 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700869 return;
870 }
871
Elliott Hughes30646832011-10-13 16:59:46 -0700872 if (need_locals) {
Elliott Hughes2435a572012-02-17 16:07:41 -0800873 InvokeLocalCbIfLive(context, reg, address, local_in_reg, local_cb);
Elliott Hughes30646832011-10-13 16:59:46 -0700874 local_in_reg[reg].is_live_ = false;
875 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700876 break;
877
878 case DBG_RESTART_LOCAL:
879 reg = DecodeUnsignedLeb128(&stream);
880 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700881 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800882 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700883 return;
884 }
885
Elliott Hughes30646832011-10-13 16:59:46 -0700886 if (need_locals) {
887 if (local_in_reg[reg].name_ == NULL || local_in_reg[reg].descriptor_ == NULL) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800888 LOG(ERROR) << "invalid stream - no name or descriptor in " << GetLocation();
Elliott Hughes30646832011-10-13 16:59:46 -0700889 return;
890 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700891
Elliott Hughes30646832011-10-13 16:59:46 -0700892 // If the register is live, the "restart" is superfluous,
893 // and we don't want to mess with the existing start address.
894 if (!local_in_reg[reg].is_live_) {
895 local_in_reg[reg].start_address_ = address;
896 local_in_reg[reg].is_live_ = true;
897 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700898 }
899 break;
900
901 case DBG_SET_PROLOGUE_END:
902 case DBG_SET_EPILOGUE_BEGIN:
903 case DBG_SET_FILE:
904 break;
905
Shih-wei Liao8e1b4ff2011-10-15 15:43:51 -0700906 default: {
907 int adjopcode = opcode - DBG_FIRST_SPECIAL;
908
Shih-wei Liao195487c2011-08-20 13:29:04 -0700909 address += adjopcode / DBG_LINE_RANGE;
910 line += DBG_LINE_BASE + (adjopcode % DBG_LINE_RANGE);
911
Elliott Hughes2435a572012-02-17 16:07:41 -0800912 if (position_cb != NULL) {
913 if (position_cb(context, address, line)) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700914 // early exit
915 return;
916 }
917 }
918 break;
Shih-wei Liao8e1b4ff2011-10-15 15:43:51 -0700919 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700920 }
921 }
922}
923
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800924void DexFile::DecodeDebugInfo(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes2435a572012-02-17 16:07:41 -0800925 DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb,
926 void* context) const {
Sebastien Hertzcb19ebf2014-03-11 15:26:35 +0100927 DCHECK(code_item != nullptr);
Ian Rogers0571d352011-11-03 19:51:38 -0700928 const byte* stream = GetDebugInfoStream(code_item);
Ian Rogers700a4022014-05-19 16:49:03 -0700929 std::unique_ptr<LocalInfo[]> local_in_reg(local_cb != NULL ?
Brian Carlstrome0948e12013-08-29 09:36:15 -0700930 new LocalInfo[code_item->registers_size_] :
931 NULL);
Ian Rogers0571d352011-11-03 19:51:38 -0700932 if (stream != NULL) {
Elliott Hughesee0fa762012-03-26 17:12:41 -0700933 DecodeDebugInfo0(code_item, is_static, method_idx, position_cb, local_cb, context, stream, &local_in_reg[0]);
Ian Rogers0571d352011-11-03 19:51:38 -0700934 }
935 for (int reg = 0; reg < code_item->registers_size_; reg++) {
Elliott Hughesee0fa762012-03-26 17:12:41 -0700936 InvokeLocalCbIfLive(context, reg, code_item->insns_size_in_code_units_, &local_in_reg[0], local_cb);
Ian Rogers0571d352011-11-03 19:51:38 -0700937 }
938}
939
Elliott Hughes2435a572012-02-17 16:07:41 -0800940bool DexFile::LineNumForPcCb(void* raw_context, uint32_t address, uint32_t line_num) {
941 LineNumFromPcContext* context = reinterpret_cast<LineNumFromPcContext*>(raw_context);
Ian Rogers0571d352011-11-03 19:51:38 -0700942
943 // We know that this callback will be called in
944 // ascending address order, so keep going until we find
945 // a match or we've just gone past it.
946 if (address > context->address_) {
947 // The line number from the previous positions callback
948 // wil be the final result.
949 return true;
950 } else {
951 context->line_num_ = line_num;
952 return address == context->address_;
953 }
954}
955
Andreas Gampe833a4852014-05-21 18:46:59 -0700956bool DexFile::IsMultiDexLocation(const char* location) {
957 return strrchr(location, kMultiDexSeparator) != nullptr;
958}
959
Calin Juravle4e1d5792014-07-15 23:56:47 +0100960std::string DexFile::GetMultiDexClassesDexName(size_t number, const char* dex_location) {
961 if (number == 0) {
962 return dex_location;
963 } else {
964 return StringPrintf("%s" kMultiDexSeparatorString "classes%zu.dex", dex_location, number + 1);
965 }
966}
967
968std::string DexFile::GetDexCanonicalLocation(const char* dex_location) {
969 CHECK_NE(dex_location, static_cast<const char*>(nullptr));
Vladimir Markobe4e6432014-09-05 14:01:17 +0100970 std::string base_location = GetBaseLocation(dex_location);
971 const char* suffix = dex_location + base_location.size();
972 DCHECK(suffix[0] == 0 || suffix[0] == kMultiDexSeparator);
973 UniqueCPtr<const char[]> path(realpath(base_location.c_str(), nullptr));
974 if (path != nullptr && path.get() != base_location) {
975 return std::string(path.get()) + suffix;
976 } else if (suffix[0] == 0) {
977 return base_location;
Calin Juravle4e1d5792014-07-15 23:56:47 +0100978 } else {
Vladimir Markobe4e6432014-09-05 14:01:17 +0100979 return dex_location;
Calin Juravle4e1d5792014-07-15 23:56:47 +0100980 }
Calin Juravle4e1d5792014-07-15 23:56:47 +0100981}
982
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800983std::ostream& operator<<(std::ostream& os, const DexFile& dex_file) {
984 os << StringPrintf("[DexFile: %s dex-checksum=%08x location-checksum=%08x %p-%p]",
985 dex_file.GetLocation().c_str(),
986 dex_file.GetHeader().checksum_, dex_file.GetLocationChecksum(),
987 dex_file.Begin(), dex_file.Begin() + dex_file.Size());
988 return os;
989}
Calin Juravle4e1d5792014-07-15 23:56:47 +0100990
Ian Rogersd91d6d62013-09-25 20:26:14 -0700991std::string Signature::ToString() const {
992 if (dex_file_ == nullptr) {
993 CHECK(proto_id_ == nullptr);
994 return "<no signature>";
995 }
996 const DexFile::TypeList* params = dex_file_->GetProtoParameters(*proto_id_);
997 std::string result;
998 if (params == nullptr) {
999 result += "()";
1000 } else {
1001 result += "(";
1002 for (uint32_t i = 0; i < params->Size(); ++i) {
1003 result += dex_file_->StringByTypeIdx(params->GetTypeItem(i).type_idx_);
1004 }
1005 result += ")";
1006 }
1007 result += dex_file_->StringByTypeIdx(proto_id_->return_type_idx_);
1008 return result;
1009}
1010
Vladimir Markod9cffea2013-11-25 15:08:02 +00001011bool Signature::operator==(const StringPiece& rhs) const {
1012 if (dex_file_ == nullptr) {
1013 return false;
1014 }
1015 StringPiece tail(rhs);
1016 if (!tail.starts_with("(")) {
1017 return false; // Invalid signature
1018 }
1019 tail.remove_prefix(1); // "(";
1020 const DexFile::TypeList* params = dex_file_->GetProtoParameters(*proto_id_);
1021 if (params != nullptr) {
1022 for (uint32_t i = 0; i < params->Size(); ++i) {
1023 StringPiece param(dex_file_->StringByTypeIdx(params->GetTypeItem(i).type_idx_));
1024 if (!tail.starts_with(param)) {
1025 return false;
1026 }
1027 tail.remove_prefix(param.length());
1028 }
1029 }
1030 if (!tail.starts_with(")")) {
1031 return false;
1032 }
1033 tail.remove_prefix(1); // ")";
1034 return tail == dex_file_->StringByTypeIdx(proto_id_->return_type_idx_);
1035}
1036
Ian Rogersd91d6d62013-09-25 20:26:14 -07001037std::ostream& operator<<(std::ostream& os, const Signature& sig) {
1038 return os << sig.ToString();
1039}
1040
Ian Rogers0571d352011-11-03 19:51:38 -07001041// Decodes the header section from the class data bytes.
1042void ClassDataItemIterator::ReadClassDataHeader() {
1043 CHECK(ptr_pos_ != NULL);
1044 header_.static_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_);
1045 header_.instance_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_);
1046 header_.direct_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_);
1047 header_.virtual_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_);
1048}
1049
1050void ClassDataItemIterator::ReadClassDataField() {
1051 field_.field_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_);
1052 field_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_);
Brian Carlstrom68adbe42012-05-11 17:18:08 -07001053 if (last_idx_ != 0 && field_.field_idx_delta_ == 0) {
Andreas Gampe4fdbba02014-06-19 20:24:22 -07001054 LOG(WARNING) << "Duplicate field in " << dex_file_.GetLocation();
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -07001055 }
Ian Rogers0571d352011-11-03 19:51:38 -07001056}
1057
1058void ClassDataItemIterator::ReadClassDataMethod() {
1059 method_.method_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_);
1060 method_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_);
1061 method_.code_off_ = DecodeUnsignedLeb128(&ptr_pos_);
Brian Carlstrom68adbe42012-05-11 17:18:08 -07001062 if (last_idx_ != 0 && method_.method_idx_delta_ == 0) {
Andreas Gampe4fdbba02014-06-19 20:24:22 -07001063 LOG(WARNING) << "Duplicate method in " << dex_file_.GetLocation();
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -07001064 }
Ian Rogers0571d352011-11-03 19:51:38 -07001065}
1066
1067// Read a signed integer. "zwidth" is the zero-based byte count.
1068static int32_t ReadSignedInt(const byte* ptr, int zwidth) {
1069 int32_t val = 0;
1070 for (int i = zwidth; i >= 0; --i) {
1071 val = ((uint32_t)val >> 8) | (((int32_t)*ptr++) << 24);
1072 }
1073 val >>= (3 - zwidth) * 8;
1074 return val;
1075}
1076
1077// Read an unsigned integer. "zwidth" is the zero-based byte count,
1078// "fill_on_right" indicates which side we want to zero-fill from.
1079static uint32_t ReadUnsignedInt(const byte* ptr, int zwidth, bool fill_on_right) {
1080 uint32_t val = 0;
1081 if (!fill_on_right) {
1082 for (int i = zwidth; i >= 0; --i) {
1083 val = (val >> 8) | (((uint32_t)*ptr++) << 24);
1084 }
1085 val >>= (3 - zwidth) * 8;
1086 } else {
1087 for (int i = zwidth; i >= 0; --i) {
1088 val = (val >> 8) | (((uint32_t)*ptr++) << 24);
1089 }
1090 }
1091 return val;
1092}
1093
1094// Read a signed long. "zwidth" is the zero-based byte count.
1095static int64_t ReadSignedLong(const byte* ptr, int zwidth) {
1096 int64_t val = 0;
1097 for (int i = zwidth; i >= 0; --i) {
1098 val = ((uint64_t)val >> 8) | (((int64_t)*ptr++) << 56);
1099 }
1100 val >>= (7 - zwidth) * 8;
1101 return val;
1102}
1103
1104// Read an unsigned long. "zwidth" is the zero-based byte count,
1105// "fill_on_right" indicates which side we want to zero-fill from.
1106static uint64_t ReadUnsignedLong(const byte* ptr, int zwidth, bool fill_on_right) {
1107 uint64_t val = 0;
1108 if (!fill_on_right) {
1109 for (int i = zwidth; i >= 0; --i) {
1110 val = (val >> 8) | (((uint64_t)*ptr++) << 56);
1111 }
1112 val >>= (7 - zwidth) * 8;
1113 } else {
1114 for (int i = zwidth; i >= 0; --i) {
1115 val = (val >> 8) | (((uint64_t)*ptr++) << 56);
1116 }
1117 }
1118 return val;
1119}
1120
1121EncodedStaticFieldValueIterator::EncodedStaticFieldValueIterator(const DexFile& dex_file,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001122 Handle<mirror::DexCache>* dex_cache,
1123 Handle<mirror::ClassLoader>* class_loader,
Ian Rogersca190662012-06-26 15:45:57 -07001124 ClassLinker* linker,
1125 const DexFile::ClassDef& class_def)
Brian Carlstrom88f36542012-10-16 23:24:21 -07001126 : dex_file_(dex_file), dex_cache_(dex_cache), class_loader_(class_loader), linker_(linker),
1127 array_size_(), pos_(-1), type_(kByte) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001128 DCHECK(dex_cache != nullptr);
1129 DCHECK(class_loader != nullptr);
Ian Rogers0571d352011-11-03 19:51:38 -07001130 ptr_ = dex_file.GetEncodedStaticFieldValuesArray(class_def);
1131 if (ptr_ == NULL) {
1132 array_size_ = 0;
1133 } else {
1134 array_size_ = DecodeUnsignedLeb128(&ptr_);
1135 }
1136 if (array_size_ > 0) {
1137 Next();
1138 }
1139}
1140
1141void EncodedStaticFieldValueIterator::Next() {
1142 pos_++;
1143 if (pos_ >= array_size_) {
1144 return;
1145 }
1146 byte value_type = *ptr_++;
1147 byte value_arg = value_type >> kEncodedValueArgShift;
1148 size_t width = value_arg + 1; // assume and correct later
Brian Carlstrom88f36542012-10-16 23:24:21 -07001149 type_ = static_cast<ValueType>(value_type & kEncodedValueTypeMask);
Ian Rogers0571d352011-11-03 19:51:38 -07001150 switch (type_) {
1151 case kBoolean:
1152 jval_.i = (value_arg != 0) ? 1 : 0;
1153 width = 0;
1154 break;
1155 case kByte:
1156 jval_.i = ReadSignedInt(ptr_, value_arg);
1157 CHECK(IsInt(8, jval_.i));
1158 break;
1159 case kShort:
1160 jval_.i = ReadSignedInt(ptr_, value_arg);
1161 CHECK(IsInt(16, jval_.i));
1162 break;
1163 case kChar:
1164 jval_.i = ReadUnsignedInt(ptr_, value_arg, false);
1165 CHECK(IsUint(16, jval_.i));
1166 break;
1167 case kInt:
1168 jval_.i = ReadSignedInt(ptr_, value_arg);
1169 break;
1170 case kLong:
1171 jval_.j = ReadSignedLong(ptr_, value_arg);
1172 break;
1173 case kFloat:
1174 jval_.i = ReadUnsignedInt(ptr_, value_arg, true);
1175 break;
1176 case kDouble:
1177 jval_.j = ReadUnsignedLong(ptr_, value_arg, true);
1178 break;
1179 case kString:
1180 case kType:
Ian Rogers0571d352011-11-03 19:51:38 -07001181 jval_.i = ReadUnsignedInt(ptr_, value_arg, false);
1182 break;
1183 case kField:
Brian Carlstrom88f36542012-10-16 23:24:21 -07001184 case kMethod:
1185 case kEnum:
Ian Rogers0571d352011-11-03 19:51:38 -07001186 case kArray:
1187 case kAnnotation:
1188 UNIMPLEMENTED(FATAL) << ": type " << type_;
1189 break;
1190 case kNull:
1191 jval_.l = NULL;
1192 width = 0;
1193 break;
1194 default:
1195 LOG(FATAL) << "Unreached";
1196 }
1197 ptr_ += width;
1198}
1199
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001200template<bool kTransactionActive>
Brian Carlstromea46f952013-07-30 01:26:50 -07001201void EncodedStaticFieldValueIterator::ReadValueToField(mirror::ArtField* field) const {
Ian Rogers0571d352011-11-03 19:51:38 -07001202 switch (type_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001203 case kBoolean: field->SetBoolean<kTransactionActive>(field->GetDeclaringClass(), jval_.z); break;
1204 case kByte: field->SetByte<kTransactionActive>(field->GetDeclaringClass(), jval_.b); break;
1205 case kShort: field->SetShort<kTransactionActive>(field->GetDeclaringClass(), jval_.s); break;
1206 case kChar: field->SetChar<kTransactionActive>(field->GetDeclaringClass(), jval_.c); break;
1207 case kInt: field->SetInt<kTransactionActive>(field->GetDeclaringClass(), jval_.i); break;
1208 case kLong: field->SetLong<kTransactionActive>(field->GetDeclaringClass(), jval_.j); break;
1209 case kFloat: field->SetFloat<kTransactionActive>(field->GetDeclaringClass(), jval_.f); break;
1210 case kDouble: field->SetDouble<kTransactionActive>(field->GetDeclaringClass(), jval_.d); break;
1211 case kNull: field->SetObject<kTransactionActive>(field->GetDeclaringClass(), NULL); break;
Ian Rogers0571d352011-11-03 19:51:38 -07001212 case kString: {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001213 CHECK(!kMovingFields);
1214 mirror::String* resolved = linker_->ResolveString(dex_file_, jval_.i, *dex_cache_);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001215 field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved);
Ian Rogers0571d352011-11-03 19:51:38 -07001216 break;
1217 }
Brian Carlstrom88f36542012-10-16 23:24:21 -07001218 case kType: {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001219 CHECK(!kMovingFields);
1220 mirror::Class* resolved = linker_->ResolveType(dex_file_, jval_.i, *dex_cache_,
1221 *class_loader_);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001222 field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved);
Brian Carlstrom88f36542012-10-16 23:24:21 -07001223 break;
1224 }
Ian Rogers0571d352011-11-03 19:51:38 -07001225 default: UNIMPLEMENTED(FATAL) << ": type " << type_;
1226 }
1227}
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001228template void EncodedStaticFieldValueIterator::ReadValueToField<true>(mirror::ArtField* field) const;
1229template void EncodedStaticFieldValueIterator::ReadValueToField<false>(mirror::ArtField* field) const;
Ian Rogers0571d352011-11-03 19:51:38 -07001230
1231CatchHandlerIterator::CatchHandlerIterator(const DexFile::CodeItem& code_item, uint32_t address) {
1232 handler_.address_ = -1;
1233 int32_t offset = -1;
1234
1235 // Short-circuit the overwhelmingly common cases.
1236 switch (code_item.tries_size_) {
1237 case 0:
1238 break;
1239 case 1: {
1240 const DexFile::TryItem* tries = DexFile::GetTryItems(code_item, 0);
1241 uint32_t start = tries->start_addr_;
1242 if (address >= start) {
1243 uint32_t end = start + tries->insn_count_;
1244 if (address < end) {
1245 offset = tries->handler_off_;
1246 }
1247 }
1248 break;
1249 }
1250 default:
Ian Rogersdbbc99d2013-04-18 16:51:54 -07001251 offset = DexFile::FindCatchHandlerOffset(code_item, address);
Ian Rogers0571d352011-11-03 19:51:38 -07001252 }
Logan Chien736df022012-04-27 16:25:57 +08001253 Init(code_item, offset);
1254}
1255
1256CatchHandlerIterator::CatchHandlerIterator(const DexFile::CodeItem& code_item,
1257 const DexFile::TryItem& try_item) {
1258 handler_.address_ = -1;
1259 Init(code_item, try_item.handler_off_);
1260}
1261
1262void CatchHandlerIterator::Init(const DexFile::CodeItem& code_item,
1263 int32_t offset) {
Ian Rogers0571d352011-11-03 19:51:38 -07001264 if (offset >= 0) {
Logan Chien736df022012-04-27 16:25:57 +08001265 Init(DexFile::GetCatchHandlerData(code_item, offset));
Ian Rogers0571d352011-11-03 19:51:38 -07001266 } else {
1267 // Not found, initialize as empty
1268 current_data_ = NULL;
1269 remaining_count_ = -1;
1270 catch_all_ = false;
1271 DCHECK(!HasNext());
1272 }
1273}
1274
1275void CatchHandlerIterator::Init(const byte* handler_data) {
1276 current_data_ = handler_data;
1277 remaining_count_ = DecodeSignedLeb128(&current_data_);
1278
1279 // If remaining_count_ is non-positive, then it is the negative of
1280 // the number of catch types, and the catches are followed by a
1281 // catch-all handler.
1282 if (remaining_count_ <= 0) {
1283 catch_all_ = true;
1284 remaining_count_ = -remaining_count_;
1285 } else {
1286 catch_all_ = false;
1287 }
1288 Next();
1289}
1290
1291void CatchHandlerIterator::Next() {
1292 if (remaining_count_ > 0) {
1293 handler_.type_idx_ = DecodeUnsignedLeb128(&current_data_);
1294 handler_.address_ = DecodeUnsignedLeb128(&current_data_);
1295 remaining_count_--;
1296 return;
1297 }
1298
1299 if (catch_all_) {
1300 handler_.type_idx_ = DexFile::kDexNoIndex16;
1301 handler_.address_ = DecodeUnsignedLeb128(&current_data_);
1302 catch_all_ = false;
1303 return;
1304 }
1305
1306 // no more handler
1307 remaining_count_ = -1;
1308}
1309
Carl Shapiro1fb86202011-06-27 17:43:13 -07001310} // namespace art