blob: 761441eae689214614fad9460bf251ef80c6249c [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 Rogersc7dd2952014-10-21 23:31:19 -070026
Ian Rogers700a4022014-05-19 16:49:03 -070027#include <memory>
Ian Rogersc7dd2952014-10-21 23:31:19 -070028#include <sstream>
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070029
Elliott Hughes07ed66b2012-12-12 18:34:25 -080030#include "base/logging.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080031#include "base/stringprintf.h"
Ian Rogers0571d352011-11-03 19:51:38 -070032#include "class_linker.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070033#include "dex_file-inl.h"
jeffhao10037c82012-01-23 15:06:23 -080034#include "dex_file_verifier.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070035#include "globals.h"
Ian Rogers0571d352011-11-03 19:51:38 -070036#include "leb128.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070037#include "mirror/art_field-inl.h"
38#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "mirror/string.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070040#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070041#include "safe_map.h"
Vladimir Markofd995762013-11-06 16:36:36 +000042#include "ScopedFd.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070043#include "handle_scope-inl.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070044#include "thread.h"
Ian Rogersa6724902013-09-23 09:23:37 -070045#include "utf-inl.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070046#include "utils.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070047#include "well_known_classes.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070048#include "zip_archive.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070049
50namespace art {
51
Ian Rogers13735952014-10-08 12:43:28 -070052const uint8_t DexFile::kDexMagic[] = { 'd', 'e', 'x', '\n' };
53const uint8_t DexFile::kDexMagicVersion[] = { '0', '3', '5', '\0' };
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070054
Ian Rogers8d31bbd2013-10-13 10:44:14 -070055static int OpenAndReadMagic(const char* filename, uint32_t* magic, std::string* error_msg) {
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070056 CHECK(magic != NULL);
Vladimir Markofd995762013-11-06 16:36:36 +000057 ScopedFd fd(open(filename, O_RDONLY, 0));
58 if (fd.get() == -1) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070059 *error_msg = StringPrintf("Unable to open '%s' : %s", filename, strerror(errno));
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070060 return -1;
61 }
Vladimir Markofd995762013-11-06 16:36:36 +000062 int n = TEMP_FAILURE_RETRY(read(fd.get(), magic, sizeof(*magic)));
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070063 if (n != sizeof(*magic)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070064 *error_msg = StringPrintf("Failed to find magic in '%s'", filename);
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070065 return -1;
66 }
Vladimir Markofd995762013-11-06 16:36:36 +000067 if (lseek(fd.get(), 0, SEEK_SET) != 0) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070068 *error_msg = StringPrintf("Failed to seek to beginning of file '%s' : %s", filename,
69 strerror(errno));
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070070 return -1;
71 }
Vladimir Markofd995762013-11-06 16:36:36 +000072 return fd.release();
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070073}
74
Ian Rogers8d31bbd2013-10-13 10:44:14 -070075bool DexFile::GetChecksum(const char* filename, uint32_t* checksum, std::string* error_msg) {
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070076 CHECK(checksum != NULL);
77 uint32_t magic;
Andreas Gampe833a4852014-05-21 18:46:59 -070078
79 // Strip ":...", which is the location
80 const char* zip_entry_name = kClassesDex;
81 const char* file_part = filename;
Vladimir Markoaa4497d2014-09-05 14:01:17 +010082 std::string file_part_storage;
Andreas Gampe833a4852014-05-21 18:46:59 -070083
Vladimir Markoaa4497d2014-09-05 14:01:17 +010084 if (DexFile::IsMultiDexLocation(filename)) {
85 file_part_storage = GetBaseLocation(filename);
86 file_part = file_part_storage.c_str();
87 zip_entry_name = filename + file_part_storage.size() + 1;
88 DCHECK_EQ(zip_entry_name[-1], kMultiDexSeparator);
Andreas Gampe833a4852014-05-21 18:46:59 -070089 }
90
91 ScopedFd fd(OpenAndReadMagic(file_part, &magic, error_msg));
Vladimir Markofd995762013-11-06 16:36:36 +000092 if (fd.get() == -1) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070093 DCHECK(!error_msg->empty());
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070094 return false;
95 }
96 if (IsZipMagic(magic)) {
Ian Rogers700a4022014-05-19 16:49:03 -070097 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(fd.release(), filename, error_msg));
Brian Carlstrom5b332c82012-02-01 15:02:31 -080098 if (zip_archive.get() == NULL) {
Andreas Gampe833a4852014-05-21 18:46:59 -070099 *error_msg = StringPrintf("Failed to open zip archive '%s'", file_part);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800100 return false;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700101 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700102 std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(zip_entry_name, error_msg));
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800103 if (zip_entry.get() == NULL) {
Andreas Gampe833a4852014-05-21 18:46:59 -0700104 *error_msg = StringPrintf("Zip archive '%s' doesn't contain %s (error msg: %s)", file_part,
105 zip_entry_name, error_msg->c_str());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800106 return false;
107 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700108 *checksum = zip_entry->GetCrc32();
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800109 return true;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700110 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700111 if (IsDexMagic(magic)) {
Ian Rogers700a4022014-05-19 16:49:03 -0700112 std::unique_ptr<const DexFile> dex_file(DexFile::OpenFile(fd.release(), filename, false, error_msg));
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800113 if (dex_file.get() == NULL) {
114 return false;
115 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700116 *checksum = dex_file->GetHeader().checksum_;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800117 return true;
118 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700119 *error_msg = StringPrintf("Expected valid zip or dex file: '%s'", filename);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800120 return false;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700121}
122
Andreas Gampe833a4852014-05-21 18:46:59 -0700123bool DexFile::Open(const char* filename, const char* location, std::string* error_msg,
124 std::vector<const DexFile*>* dex_files) {
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700125 uint32_t magic;
Vladimir Markofd995762013-11-06 16:36:36 +0000126 ScopedFd fd(OpenAndReadMagic(filename, &magic, error_msg));
127 if (fd.get() == -1) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700128 DCHECK(!error_msg->empty());
Andreas Gampe833a4852014-05-21 18:46:59 -0700129 return false;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700130 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700131 if (IsZipMagic(magic)) {
Andreas Gampe833a4852014-05-21 18:46:59 -0700132 return DexFile::OpenZip(fd.release(), location, error_msg, dex_files);
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700133 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700134 if (IsDexMagic(magic)) {
Andreas Gampe833a4852014-05-21 18:46:59 -0700135 std::unique_ptr<const DexFile> dex_file(DexFile::OpenFile(fd.release(), location, true,
136 error_msg));
137 if (dex_file.get() != nullptr) {
138 dex_files->push_back(dex_file.release());
139 return true;
140 } else {
141 return false;
142 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700143 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700144 *error_msg = StringPrintf("Expected valid zip or dex file: '%s'", filename);
Alexander Ivchenkobacce5c2014-06-26 16:32:11 +0400145 return false;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700146}
147
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800148int DexFile::GetPermissions() const {
149 if (mem_map_.get() == NULL) {
150 return 0;
151 } else {
152 return mem_map_->GetProtect();
153 }
154}
155
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200156bool DexFile::IsReadOnly() const {
157 return GetPermissions() == PROT_READ;
158}
159
Brian Carlstrome0948e12013-08-29 09:36:15 -0700160bool DexFile::EnableWrite() const {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200161 CHECK(IsReadOnly());
162 if (mem_map_.get() == NULL) {
163 return false;
164 } else {
Brian Carlstrome0948e12013-08-29 09:36:15 -0700165 return mem_map_->Protect(PROT_READ | PROT_WRITE);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200166 }
167}
168
Brian Carlstrome0948e12013-08-29 09:36:15 -0700169bool DexFile::DisableWrite() const {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200170 CHECK(!IsReadOnly());
171 if (mem_map_.get() == NULL) {
172 return false;
173 } else {
Brian Carlstrome0948e12013-08-29 09:36:15 -0700174 return mem_map_->Protect(PROT_READ);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200175 }
176}
177
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700178const DexFile* DexFile::OpenFile(int fd, const char* location, bool verify,
179 std::string* error_msg) {
180 CHECK(location != nullptr);
Ian Rogers700a4022014-05-19 16:49:03 -0700181 std::unique_ptr<MemMap> map;
Vladimir Markofd995762013-11-06 16:36:36 +0000182 {
183 ScopedFd delayed_close(fd);
184 struct stat sbuf;
185 memset(&sbuf, 0, sizeof(sbuf));
186 if (fstat(fd, &sbuf) == -1) {
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -0800187 *error_msg = StringPrintf("DexFile: fstat '%s' failed: %s", location, strerror(errno));
Vladimir Markofd995762013-11-06 16:36:36 +0000188 return nullptr;
189 }
190 if (S_ISDIR(sbuf.st_mode)) {
191 *error_msg = StringPrintf("Attempt to mmap directory '%s'", location);
192 return nullptr;
193 }
194 size_t length = sbuf.st_size;
195 map.reset(MemMap::MapFile(length, PROT_READ, MAP_PRIVATE, fd, 0, location, error_msg));
196 if (map.get() == nullptr) {
197 DCHECK(!error_msg->empty());
198 return nullptr;
199 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700200 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800201
202 if (map->Size() < sizeof(DexFile::Header)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700203 *error_msg = StringPrintf(
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -0800204 "DexFile: failed to open dex file '%s' that is too short to have a header", location);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700205 return nullptr;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800206 }
207
208 const Header* dex_header = reinterpret_cast<const Header*>(map->Begin());
209
Andreas Gampe928f72b2014-09-09 19:53:48 -0700210 std::unique_ptr<const DexFile> dex_file(OpenMemory(location, dex_header->checksum_, map.release(),
211 error_msg));
212 if (dex_file.get() == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700213 *error_msg = StringPrintf("Failed to open dex file '%s' from memory: %s", location,
214 error_msg->c_str());
215 return nullptr;
jeffhaof6174e82012-01-31 16:14:17 -0800216 }
jeffhao54c1ceb2012-02-01 11:45:32 -0800217
Andreas Gampe928f72b2014-09-09 19:53:48 -0700218 if (verify && !DexFileVerifier::Verify(dex_file.get(), dex_file->Begin(), dex_file->Size(),
219 location, error_msg)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700220 return nullptr;
jeffhao54c1ceb2012-02-01 11:45:32 -0800221 }
222
Andreas Gampe928f72b2014-09-09 19:53:48 -0700223 return dex_file.release();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700224}
225
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700226const char* DexFile::kClassesDex = "classes.dex";
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700227
Andreas Gampe833a4852014-05-21 18:46:59 -0700228bool DexFile::OpenZip(int fd, const std::string& location, std::string* error_msg,
229 std::vector<const DexFile*>* dex_files) {
Ian Rogers700a4022014-05-19 16:49:03 -0700230 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(fd, location.c_str(), error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700231 if (zip_archive.get() == nullptr) {
232 DCHECK(!error_msg->empty());
Andreas Gampe833a4852014-05-21 18:46:59 -0700233 return false;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700234 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700235 return DexFile::OpenFromZip(*zip_archive, location, error_msg, dex_files);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800236}
237
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800238const DexFile* DexFile::OpenMemory(const std::string& location,
239 uint32_t location_checksum,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700240 MemMap* mem_map,
241 std::string* error_msg) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800242 return OpenMemory(mem_map->Begin(),
243 mem_map->Size(),
244 location,
245 location_checksum,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700246 mem_map,
247 error_msg);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800248}
249
Andreas Gampe833a4852014-05-21 18:46:59 -0700250const DexFile* DexFile::Open(const ZipArchive& zip_archive, const char* entry_name,
251 const std::string& location, std::string* error_msg,
252 ZipOpenErrorCode* error_code) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800253 CHECK(!location.empty());
Andreas Gampe833a4852014-05-21 18:46:59 -0700254 std::unique_ptr<ZipEntry> zip_entry(zip_archive.Find(entry_name, error_msg));
Elliott Hughes90a33692011-08-30 13:27:07 -0700255 if (zip_entry.get() == NULL) {
Andreas Gampe833a4852014-05-21 18:46:59 -0700256 *error_code = ZipOpenErrorCode::kEntryNotFound;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700257 return nullptr;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700258 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700259 std::unique_ptr<MemMap> map(zip_entry->ExtractToMemMap(location.c_str(), entry_name, error_msg));
Brian Carlstrom89521892011-12-07 22:05:07 -0800260 if (map.get() == NULL) {
Andreas Gampe833a4852014-05-21 18:46:59 -0700261 *error_msg = StringPrintf("Failed to extract '%s' from '%s': %s", entry_name, location.c_str(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700262 error_msg->c_str());
Andreas Gampe833a4852014-05-21 18:46:59 -0700263 *error_code = ZipOpenErrorCode::kExtractToMemoryError;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700264 return nullptr;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700265 }
Ian Rogers700a4022014-05-19 16:49:03 -0700266 std::unique_ptr<const DexFile> dex_file(OpenMemory(location, zip_entry->GetCrc32(), map.release(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700267 error_msg));
268 if (dex_file.get() == nullptr) {
269 *error_msg = StringPrintf("Failed to open dex file '%s' from memory: %s", location.c_str(),
270 error_msg->c_str());
Andreas Gampe833a4852014-05-21 18:46:59 -0700271 *error_code = ZipOpenErrorCode::kDexFileError;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700272 return nullptr;
jeffhaof6174e82012-01-31 16:14:17 -0800273 }
Brian Carlstrome0948e12013-08-29 09:36:15 -0700274 if (!dex_file->DisableWrite()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700275 *error_msg = StringPrintf("Failed to make dex file '%s' read only", location.c_str());
Andreas Gampe833a4852014-05-21 18:46:59 -0700276 *error_code = ZipOpenErrorCode::kMakeReadOnlyError;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700277 return nullptr;
Brian Carlstrome0948e12013-08-29 09:36:15 -0700278 }
279 CHECK(dex_file->IsReadOnly()) << location;
Brian Carlstromd6cec902014-05-25 16:08:51 -0700280 if (!DexFileVerifier::Verify(dex_file.get(), dex_file->Begin(), dex_file->Size(),
281 location.c_str(), error_msg)) {
Andreas Gampe833a4852014-05-21 18:46:59 -0700282 *error_code = ZipOpenErrorCode::kVerifyError;
Brian Carlstromd6cec902014-05-25 16:08:51 -0700283 return nullptr;
284 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700285 *error_code = ZipOpenErrorCode::kNoError;
Brian Carlstrome0948e12013-08-29 09:36:15 -0700286 return dex_file.release();
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700287}
288
Andreas Gampe833a4852014-05-21 18:46:59 -0700289bool DexFile::OpenFromZip(const ZipArchive& zip_archive, const std::string& location,
290 std::string* error_msg, std::vector<const DexFile*>* dex_files) {
291 ZipOpenErrorCode error_code;
292 std::unique_ptr<const DexFile> dex_file(Open(zip_archive, kClassesDex, location, error_msg,
293 &error_code));
294 if (dex_file.get() == nullptr) {
295 return false;
296 } else {
297 // Had at least classes.dex.
298 dex_files->push_back(dex_file.release());
299
300 // Now try some more.
301 size_t i = 2;
302
303 // We could try to avoid std::string allocations by working on a char array directly. As we
304 // do not expect a lot of iterations, this seems too involved and brittle.
305
306 while (i < 100) {
307 std::string name = StringPrintf("classes%zu.dex", i);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100308 std::string fake_location = location + kMultiDexSeparator + name;
Andreas Gampe833a4852014-05-21 18:46:59 -0700309 std::unique_ptr<const DexFile> next_dex_file(Open(zip_archive, name.c_str(), fake_location,
310 error_msg, &error_code));
311 if (next_dex_file.get() == nullptr) {
312 if (error_code != ZipOpenErrorCode::kEntryNotFound) {
313 LOG(WARNING) << error_msg;
314 }
315 break;
316 } else {
317 dex_files->push_back(next_dex_file.release());
318 }
319
320 i++;
321 }
322
323 return true;
324 }
325}
326
327
Ian Rogers13735952014-10-08 12:43:28 -0700328const DexFile* DexFile::OpenMemory(const uint8_t* base,
jeffhaof6174e82012-01-31 16:14:17 -0800329 size_t size,
Brian Carlstrom89521892011-12-07 22:05:07 -0800330 const std::string& location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800331 uint32_t location_checksum,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700332 MemMap* mem_map, std::string* error_msg) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700333 CHECK_ALIGNED(base, 4); // various dex file structures must be word aligned
Ian Rogers700a4022014-05-19 16:49:03 -0700334 std::unique_ptr<DexFile> dex_file(new DexFile(base, size, location, location_checksum, mem_map));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700335 if (!dex_file->Init(error_msg)) {
336 return nullptr;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700337 } else {
Brian Carlstromf615a612011-07-23 12:50:34 -0700338 return dex_file.release();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700339 }
340}
341
Ian Rogers13735952014-10-08 12:43:28 -0700342DexFile::DexFile(const uint8_t* base, size_t size,
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800343 const std::string& location,
344 uint32_t location_checksum,
345 MemMap* mem_map)
346 : begin_(base),
347 size_(size),
348 location_(location),
349 location_checksum_(location_checksum),
350 mem_map_(mem_map),
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800351 header_(reinterpret_cast<const Header*>(base)),
352 string_ids_(reinterpret_cast<const StringId*>(base + header_->string_ids_off_)),
353 type_ids_(reinterpret_cast<const TypeId*>(base + header_->type_ids_off_)),
354 field_ids_(reinterpret_cast<const FieldId*>(base + header_->field_ids_off_)),
355 method_ids_(reinterpret_cast<const MethodId*>(base + header_->method_ids_off_)),
356 proto_ids_(reinterpret_cast<const ProtoId*>(base + header_->proto_ids_off_)),
Ian Rogers68b56852014-08-29 20:19:11 -0700357 class_defs_(reinterpret_cast<const ClassDef*>(base + header_->class_defs_off_)),
358 find_class_def_misses_(0),
Ian Rogersecaebd32014-09-12 23:10:21 -0700359 class_def_index_(nullptr) {
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
Ian Rogers13735952014-10-08 12:43:28 -0700404bool DexFile::IsMagicValid(const uint8_t* magic) {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800405 return (memcmp(magic, kDexMagic, sizeof(kDexMagic)) == 0);
406}
407
Ian Rogers13735952014-10-08 12:43:28 -0700408bool DexFile::IsVersionValid(const uint8_t* magic) {
409 const uint8_t* version = &magic[sizeof(kDexMagic)];
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800410 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.
Ian Rogers68b56852014-08-29 20:19:11 -0700448 const uint32_t kMaxFailedDexClassDefLookups = 100;
Ian Rogersecaebd32014-09-12 23:10:21 -0700449 uint32_t old_misses = find_class_def_misses_.FetchAndAddSequentiallyConsistent(1);
450 if (old_misses == kMaxFailedDexClassDefLookups) {
451 // Are we the ones moving the miss count past the max? Sanity check the index doesn't exist.
452 CHECK(class_def_index_.LoadSequentiallyConsistent() == nullptr);
453 // Build the index.
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));
Ian Rogers68b56852014-08-29 20:19:11 -0700459 }
Ian Rogersecaebd32014-09-12 23:10:21 -0700460 // Sanity check the index still doesn't exist, only 1 thread should build it.
461 CHECK(class_def_index_.LoadSequentiallyConsistent() == nullptr);
462 class_def_index_.StoreSequentiallyConsistent(index);
Ian Rogers68b56852014-08-29 20:19:11 -0700463 }
464 return nullptr;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700465}
466
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700467const DexFile::ClassDef* DexFile::FindClassDef(uint16_t type_idx) const {
468 size_t num_class_defs = NumClassDefs();
469 for (size_t i = 0; i < num_class_defs; ++i) {
470 const ClassDef& class_def = GetClassDef(i);
471 if (class_def.class_idx_ == type_idx) {
472 return &class_def;
473 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700474 }
475 return NULL;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700476}
477
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800478const DexFile::FieldId* DexFile::FindFieldId(const DexFile::TypeId& declaring_klass,
479 const DexFile::StringId& name,
480 const DexFile::TypeId& type) const {
481 // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx
482 const uint16_t class_idx = GetIndexForTypeId(declaring_klass);
483 const uint32_t name_idx = GetIndexForStringId(name);
484 const uint16_t type_idx = GetIndexForTypeId(type);
Ian Rogersf8582c32013-05-29 16:33:03 -0700485 int32_t lo = 0;
486 int32_t hi = NumFieldIds() - 1;
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800487 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700488 int32_t mid = (hi + lo) / 2;
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800489 const DexFile::FieldId& field = GetFieldId(mid);
490 if (class_idx > field.class_idx_) {
491 lo = mid + 1;
492 } else if (class_idx < field.class_idx_) {
493 hi = mid - 1;
494 } else {
495 if (name_idx > field.name_idx_) {
496 lo = mid + 1;
497 } else if (name_idx < field.name_idx_) {
498 hi = mid - 1;
499 } else {
500 if (type_idx > field.type_idx_) {
501 lo = mid + 1;
502 } else if (type_idx < field.type_idx_) {
503 hi = mid - 1;
504 } else {
505 return &field;
506 }
507 }
508 }
509 }
510 return NULL;
511}
512
513const DexFile::MethodId* DexFile::FindMethodId(const DexFile::TypeId& declaring_klass,
Ian Rogers0571d352011-11-03 19:51:38 -0700514 const DexFile::StringId& name,
515 const DexFile::ProtoId& signature) const {
516 // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800517 const uint16_t class_idx = GetIndexForTypeId(declaring_klass);
Ian Rogers0571d352011-11-03 19:51:38 -0700518 const uint32_t name_idx = GetIndexForStringId(name);
519 const uint16_t proto_idx = GetIndexForProtoId(signature);
Ian Rogersf8582c32013-05-29 16:33:03 -0700520 int32_t lo = 0;
521 int32_t hi = NumMethodIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700522 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700523 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700524 const DexFile::MethodId& method = GetMethodId(mid);
525 if (class_idx > method.class_idx_) {
526 lo = mid + 1;
527 } else if (class_idx < method.class_idx_) {
528 hi = mid - 1;
529 } else {
530 if (name_idx > method.name_idx_) {
531 lo = mid + 1;
532 } else if (name_idx < method.name_idx_) {
533 hi = mid - 1;
534 } else {
535 if (proto_idx > method.proto_idx_) {
536 lo = mid + 1;
537 } else if (proto_idx < method.proto_idx_) {
538 hi = mid - 1;
539 } else {
540 return &method;
541 }
542 }
543 }
544 }
545 return NULL;
546}
547
Ian Rogers637c65b2013-05-31 11:46:00 -0700548const DexFile::StringId* DexFile::FindStringId(const char* string) const {
Ian Rogersf8582c32013-05-29 16:33:03 -0700549 int32_t lo = 0;
550 int32_t hi = NumStringIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700551 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700552 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700553 const DexFile::StringId& str_id = GetStringId(mid);
Ian Rogerscf5077a2013-10-31 12:37:54 -0700554 const char* str = GetStringData(str_id);
Ian Rogers637c65b2013-05-31 11:46:00 -0700555 int compare = CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(string, str);
556 if (compare > 0) {
557 lo = mid + 1;
558 } else if (compare < 0) {
559 hi = mid - 1;
560 } else {
561 return &str_id;
562 }
563 }
564 return NULL;
565}
566
567const DexFile::StringId* DexFile::FindStringId(const uint16_t* string) const {
568 int32_t lo = 0;
569 int32_t hi = NumStringIds() - 1;
570 while (hi >= lo) {
571 int32_t mid = (hi + lo) / 2;
Ian Rogers637c65b2013-05-31 11:46:00 -0700572 const DexFile::StringId& str_id = GetStringId(mid);
Ian Rogerscf5077a2013-10-31 12:37:54 -0700573 const char* str = GetStringData(str_id);
Ian Rogers637c65b2013-05-31 11:46:00 -0700574 int compare = CompareModifiedUtf8ToUtf16AsCodePointValues(str, string);
Ian Rogers0571d352011-11-03 19:51:38 -0700575 if (compare > 0) {
576 lo = mid + 1;
577 } else if (compare < 0) {
578 hi = mid - 1;
579 } else {
580 return &str_id;
581 }
582 }
583 return NULL;
584}
585
586const DexFile::TypeId* DexFile::FindTypeId(uint32_t string_idx) const {
Ian Rogersf8582c32013-05-29 16:33:03 -0700587 int32_t lo = 0;
588 int32_t hi = NumTypeIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700589 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700590 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700591 const TypeId& type_id = GetTypeId(mid);
592 if (string_idx > type_id.descriptor_idx_) {
593 lo = mid + 1;
594 } else if (string_idx < type_id.descriptor_idx_) {
595 hi = mid - 1;
596 } else {
597 return &type_id;
598 }
599 }
600 return NULL;
601}
602
603const DexFile::ProtoId* DexFile::FindProtoId(uint16_t return_type_idx,
Vladimir Marko5c96e6b2013-11-14 15:34:17 +0000604 const uint16_t* signature_type_idxs,
605 uint32_t signature_length) const {
Ian Rogersf8582c32013-05-29 16:33:03 -0700606 int32_t lo = 0;
607 int32_t hi = NumProtoIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700608 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700609 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700610 const DexFile::ProtoId& proto = GetProtoId(mid);
611 int compare = return_type_idx - proto.return_type_idx_;
612 if (compare == 0) {
613 DexFileParameterIterator it(*this, proto);
614 size_t i = 0;
Vladimir Marko5c96e6b2013-11-14 15:34:17 +0000615 while (it.HasNext() && i < signature_length && compare == 0) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800616 compare = signature_type_idxs[i] - it.GetTypeIdx();
Ian Rogers0571d352011-11-03 19:51:38 -0700617 it.Next();
618 i++;
619 }
620 if (compare == 0) {
621 if (it.HasNext()) {
622 compare = -1;
Vladimir Marko5c96e6b2013-11-14 15:34:17 +0000623 } else if (i < signature_length) {
Ian Rogers0571d352011-11-03 19:51:38 -0700624 compare = 1;
625 }
626 }
627 }
628 if (compare > 0) {
629 lo = mid + 1;
630 } else if (compare < 0) {
631 hi = mid - 1;
632 } else {
633 return &proto;
634 }
635 }
636 return NULL;
637}
638
639// Given a signature place the type ids into the given vector
Ian Rogersd91d6d62013-09-25 20:26:14 -0700640bool DexFile::CreateTypeList(const StringPiece& signature, uint16_t* return_type_idx,
641 std::vector<uint16_t>* param_type_idxs) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700642 if (signature[0] != '(') {
643 return false;
644 }
645 size_t offset = 1;
646 size_t end = signature.size();
647 bool process_return = false;
648 while (offset < end) {
Vladimir Markoe9c36b32013-11-21 15:49:16 +0000649 size_t start_offset = offset;
Ian Rogers0571d352011-11-03 19:51:38 -0700650 char c = signature[offset];
651 offset++;
652 if (c == ')') {
653 process_return = true;
654 continue;
655 }
Ian Rogers0571d352011-11-03 19:51:38 -0700656 while (c == '[') { // process array prefix
657 if (offset >= end) { // expect some descriptor following [
658 return false;
659 }
660 c = signature[offset];
661 offset++;
Ian Rogers0571d352011-11-03 19:51:38 -0700662 }
663 if (c == 'L') { // process type descriptors
664 do {
665 if (offset >= end) { // unexpected early termination of descriptor
666 return false;
667 }
668 c = signature[offset];
669 offset++;
Ian Rogers0571d352011-11-03 19:51:38 -0700670 } while (c != ';');
671 }
Vladimir Markoe9c36b32013-11-21 15:49:16 +0000672 // TODO: avoid creating a std::string just to get a 0-terminated char array
673 std::string descriptor(signature.data() + start_offset, offset - start_offset);
Ian Rogers637c65b2013-05-31 11:46:00 -0700674 const DexFile::StringId* string_id = FindStringId(descriptor.c_str());
Ian Rogers0571d352011-11-03 19:51:38 -0700675 if (string_id == NULL) {
676 return false;
677 }
678 const DexFile::TypeId* type_id = FindTypeId(GetIndexForStringId(*string_id));
679 if (type_id == NULL) {
680 return false;
681 }
682 uint16_t type_idx = GetIndexForTypeId(*type_id);
683 if (!process_return) {
684 param_type_idxs->push_back(type_idx);
685 } else {
686 *return_type_idx = type_idx;
687 return offset == end; // return true if the signature had reached a sensible end
688 }
689 }
690 return false; // failed to correctly parse return type
691}
692
Ian Rogersd91d6d62013-09-25 20:26:14 -0700693const Signature DexFile::CreateSignature(const StringPiece& signature) const {
694 uint16_t return_type_idx;
695 std::vector<uint16_t> param_type_indices;
696 bool success = CreateTypeList(signature, &return_type_idx, &param_type_indices);
697 if (!success) {
698 return Signature::NoSignature();
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700699 }
Ian Rogersd91d6d62013-09-25 20:26:14 -0700700 const ProtoId* proto_id = FindProtoId(return_type_idx, param_type_indices);
701 if (proto_id == NULL) {
702 return Signature::NoSignature();
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700703 }
Ian Rogersd91d6d62013-09-25 20:26:14 -0700704 return Signature(this, *proto_id);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700705}
706
Ian Rogersef7d42f2014-01-06 12:55:46 -0800707int32_t DexFile::GetLineNumFromPC(mirror::ArtMethod* method, uint32_t rel_pc) const {
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -0700708 // For native method, lineno should be -2 to indicate it is native. Note that
709 // "line number == -2" is how libcore tells from StackTraceElement.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700710 if (method->GetCodeItemOffset() == 0) {
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -0700711 return -2;
712 }
713
TDYa127c8dc1012012-04-19 07:03:33 -0700714 const CodeItem* code_item = GetCodeItem(method->GetCodeItemOffset());
Elliott Hughescaf76542012-06-28 16:08:22 -0700715 DCHECK(code_item != NULL) << PrettyMethod(method) << " " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700716
717 // A method with no line number info should return -1
718 LineNumFromPcContext context(rel_pc, -1);
TDYa127c8dc1012012-04-19 07:03:33 -0700719 DecodeDebugInfo(code_item, method->IsStatic(), method->GetDexMethodIndex(), LineNumForPcCb,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800720 NULL, &context);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700721 return context.line_num_;
722}
723
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700724int32_t DexFile::FindTryItem(const CodeItem &code_item, uint32_t address) {
Ian Rogers0571d352011-11-03 19:51:38 -0700725 // Note: Signed type is important for max and min.
726 int32_t min = 0;
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700727 int32_t max = code_item.tries_size_ - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700728
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700729 while (min <= max) {
730 int32_t mid = min + ((max - min) / 2);
731
732 const art::DexFile::TryItem* ti = GetTryItems(code_item, mid);
733 uint32_t start = ti->start_addr_;
734 uint32_t end = start + ti->insn_count_;
735
Ian Rogers0571d352011-11-03 19:51:38 -0700736 if (address < start) {
737 max = mid - 1;
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700738 } else if (address >= end) {
739 min = mid + 1;
740 } else { // We have a winner!
741 return mid;
Ian Rogers0571d352011-11-03 19:51:38 -0700742 }
743 }
744 // No match.
745 return -1;
746}
747
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700748int32_t DexFile::FindCatchHandlerOffset(const CodeItem &code_item, uint32_t address) {
749 int32_t try_item = FindTryItem(code_item, address);
750 if (try_item == -1) {
751 return -1;
752 } else {
753 return DexFile::GetTryItems(code_item, try_item)->handler_off_;
754 }
755}
756
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800757void DexFile::DecodeDebugInfo0(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes2435a572012-02-17 16:07:41 -0800758 DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb,
Ian Rogers13735952014-10-08 12:43:28 -0700759 void* context, const uint8_t* stream, LocalInfo* local_in_reg) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700760 uint32_t line = DecodeUnsignedLeb128(&stream);
761 uint32_t parameters_size = DecodeUnsignedLeb128(&stream);
762 uint16_t arg_reg = code_item->registers_size_ - code_item->ins_size_;
763 uint32_t address = 0;
Elliott Hughes30646832011-10-13 16:59:46 -0700764 bool need_locals = (local_cb != NULL);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700765
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800766 if (!is_static) {
Elliott Hughes30646832011-10-13 16:59:46 -0700767 if (need_locals) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800768 const char* descriptor = GetMethodDeclaringClassDescriptor(GetMethodId(method_idx));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700769 local_in_reg[arg_reg].name_ = "this";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800770 local_in_reg[arg_reg].descriptor_ = descriptor;
Elliott Hughes392b1242011-11-30 13:55:50 -0800771 local_in_reg[arg_reg].signature_ = NULL;
Elliott Hughes30646832011-10-13 16:59:46 -0700772 local_in_reg[arg_reg].start_address_ = 0;
773 local_in_reg[arg_reg].is_live_ = true;
774 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700775 arg_reg++;
776 }
777
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800778 DexFileParameterIterator it(*this, GetMethodPrototype(GetMethodId(method_idx)));
Ian Rogers0571d352011-11-03 19:51:38 -0700779 for (uint32_t i = 0; i < parameters_size && it.HasNext(); ++i, it.Next()) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700780 if (arg_reg >= code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700781 LOG(ERROR) << "invalid stream - arg reg >= reg size (" << arg_reg
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800782 << " >= " << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700783 return;
784 }
Elliott Hughes392b1242011-11-30 13:55:50 -0800785 uint32_t id = DecodeUnsignedLeb128P1(&stream);
Ian Rogers0571d352011-11-03 19:51:38 -0700786 const char* descriptor = it.GetDescriptor();
Elliott Hughes392b1242011-11-30 13:55:50 -0800787 if (need_locals && id != kDexNoIndex) {
Ian Rogers0571d352011-11-03 19:51:38 -0700788 const char* name = StringDataByIdx(id);
Elliott Hughes30646832011-10-13 16:59:46 -0700789 local_in_reg[arg_reg].name_ = name;
790 local_in_reg[arg_reg].descriptor_ = descriptor;
Elliott Hughes392b1242011-11-30 13:55:50 -0800791 local_in_reg[arg_reg].signature_ = NULL;
Elliott Hughes30646832011-10-13 16:59:46 -0700792 local_in_reg[arg_reg].start_address_ = address;
793 local_in_reg[arg_reg].is_live_ = true;
794 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700795 switch (*descriptor) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700796 case 'D':
797 case 'J':
798 arg_reg += 2;
799 break;
800 default:
801 arg_reg += 1;
802 break;
803 }
804 }
805
Ian Rogers0571d352011-11-03 19:51:38 -0700806 if (it.HasNext()) {
Brian Carlstromf79fccb2014-02-20 08:55:10 -0800807 LOG(ERROR) << "invalid stream - problem with parameter iterator in " << GetLocation()
808 << " for method " << PrettyMethod(method_idx, *this);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700809 return;
810 }
811
812 for (;;) {
813 uint8_t opcode = *stream++;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700814 uint16_t reg;
Jeff Haob7cefc72013-11-14 14:51:09 -0800815 uint32_t name_idx;
816 uint32_t descriptor_idx;
817 uint32_t signature_idx = 0;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700818
Shih-wei Liao195487c2011-08-20 13:29:04 -0700819 switch (opcode) {
820 case DBG_END_SEQUENCE:
821 return;
822
823 case DBG_ADVANCE_PC:
824 address += DecodeUnsignedLeb128(&stream);
825 break;
826
827 case DBG_ADVANCE_LINE:
Shih-wei Liao8a05d272011-10-15 18:45:43 -0700828 line += DecodeSignedLeb128(&stream);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700829 break;
830
831 case DBG_START_LOCAL:
832 case DBG_START_LOCAL_EXTENDED:
833 reg = DecodeUnsignedLeb128(&stream);
834 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700835 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800836 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700837 return;
838 }
839
jeffhaof8728872011-10-28 19:11:13 -0700840 name_idx = DecodeUnsignedLeb128P1(&stream);
841 descriptor_idx = DecodeUnsignedLeb128P1(&stream);
842 if (opcode == DBG_START_LOCAL_EXTENDED) {
843 signature_idx = DecodeUnsignedLeb128P1(&stream);
844 }
845
Shih-wei Liao195487c2011-08-20 13:29:04 -0700846 // Emit what was previously there, if anything
Elliott Hughes30646832011-10-13 16:59:46 -0700847 if (need_locals) {
Elliott Hughes2435a572012-02-17 16:07:41 -0800848 InvokeLocalCbIfLive(context, reg, address, local_in_reg, local_cb);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700849
Ian Rogers0571d352011-11-03 19:51:38 -0700850 local_in_reg[reg].name_ = StringDataByIdx(name_idx);
851 local_in_reg[reg].descriptor_ = StringByTypeIdx(descriptor_idx);
Elliott Hughes30646832011-10-13 16:59:46 -0700852 if (opcode == DBG_START_LOCAL_EXTENDED) {
Ian Rogers0571d352011-11-03 19:51:38 -0700853 local_in_reg[reg].signature_ = StringDataByIdx(signature_idx);
Elliott Hughes30646832011-10-13 16:59:46 -0700854 }
855 local_in_reg[reg].start_address_ = address;
856 local_in_reg[reg].is_live_ = true;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700857 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700858 break;
859
860 case DBG_END_LOCAL:
861 reg = DecodeUnsignedLeb128(&stream);
862 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700863 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800864 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700865 return;
866 }
867
Elliott Hughes30646832011-10-13 16:59:46 -0700868 if (need_locals) {
Elliott Hughes2435a572012-02-17 16:07:41 -0800869 InvokeLocalCbIfLive(context, reg, address, local_in_reg, local_cb);
Elliott Hughes30646832011-10-13 16:59:46 -0700870 local_in_reg[reg].is_live_ = false;
871 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700872 break;
873
874 case DBG_RESTART_LOCAL:
875 reg = DecodeUnsignedLeb128(&stream);
876 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700877 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800878 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700879 return;
880 }
881
Elliott Hughes30646832011-10-13 16:59:46 -0700882 if (need_locals) {
883 if (local_in_reg[reg].name_ == NULL || local_in_reg[reg].descriptor_ == NULL) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800884 LOG(ERROR) << "invalid stream - no name or descriptor in " << GetLocation();
Elliott Hughes30646832011-10-13 16:59:46 -0700885 return;
886 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700887
Elliott Hughes30646832011-10-13 16:59:46 -0700888 // If the register is live, the "restart" is superfluous,
889 // and we don't want to mess with the existing start address.
890 if (!local_in_reg[reg].is_live_) {
891 local_in_reg[reg].start_address_ = address;
892 local_in_reg[reg].is_live_ = true;
893 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700894 }
895 break;
896
897 case DBG_SET_PROLOGUE_END:
898 case DBG_SET_EPILOGUE_BEGIN:
899 case DBG_SET_FILE:
900 break;
901
Shih-wei Liao8e1b4ff2011-10-15 15:43:51 -0700902 default: {
903 int adjopcode = opcode - DBG_FIRST_SPECIAL;
904
Shih-wei Liao195487c2011-08-20 13:29:04 -0700905 address += adjopcode / DBG_LINE_RANGE;
906 line += DBG_LINE_BASE + (adjopcode % DBG_LINE_RANGE);
907
Elliott Hughes2435a572012-02-17 16:07:41 -0800908 if (position_cb != NULL) {
909 if (position_cb(context, address, line)) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700910 // early exit
911 return;
912 }
913 }
914 break;
Shih-wei Liao8e1b4ff2011-10-15 15:43:51 -0700915 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700916 }
917 }
918}
919
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800920void DexFile::DecodeDebugInfo(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes2435a572012-02-17 16:07:41 -0800921 DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb,
922 void* context) const {
Sebastien Hertzcb19ebf2014-03-11 15:26:35 +0100923 DCHECK(code_item != nullptr);
Ian Rogers13735952014-10-08 12:43:28 -0700924 const uint8_t* stream = GetDebugInfoStream(code_item);
Ian Rogers700a4022014-05-19 16:49:03 -0700925 std::unique_ptr<LocalInfo[]> local_in_reg(local_cb != NULL ?
Brian Carlstrome0948e12013-08-29 09:36:15 -0700926 new LocalInfo[code_item->registers_size_] :
927 NULL);
Ian Rogers0571d352011-11-03 19:51:38 -0700928 if (stream != NULL) {
Elliott Hughesee0fa762012-03-26 17:12:41 -0700929 DecodeDebugInfo0(code_item, is_static, method_idx, position_cb, local_cb, context, stream, &local_in_reg[0]);
Ian Rogers0571d352011-11-03 19:51:38 -0700930 }
931 for (int reg = 0; reg < code_item->registers_size_; reg++) {
Elliott Hughesee0fa762012-03-26 17:12:41 -0700932 InvokeLocalCbIfLive(context, reg, code_item->insns_size_in_code_units_, &local_in_reg[0], local_cb);
Ian Rogers0571d352011-11-03 19:51:38 -0700933 }
934}
935
Elliott Hughes2435a572012-02-17 16:07:41 -0800936bool DexFile::LineNumForPcCb(void* raw_context, uint32_t address, uint32_t line_num) {
937 LineNumFromPcContext* context = reinterpret_cast<LineNumFromPcContext*>(raw_context);
Ian Rogers0571d352011-11-03 19:51:38 -0700938
939 // We know that this callback will be called in
940 // ascending address order, so keep going until we find
941 // a match or we've just gone past it.
942 if (address > context->address_) {
943 // The line number from the previous positions callback
944 // wil be the final result.
945 return true;
946 } else {
947 context->line_num_ = line_num;
948 return address == context->address_;
949 }
950}
951
Andreas Gampe833a4852014-05-21 18:46:59 -0700952bool DexFile::IsMultiDexLocation(const char* location) {
953 return strrchr(location, kMultiDexSeparator) != nullptr;
954}
955
Calin Juravle4e1d5792014-07-15 23:56:47 +0100956std::string DexFile::GetMultiDexClassesDexName(size_t number, const char* dex_location) {
957 if (number == 0) {
958 return dex_location;
959 } else {
960 return StringPrintf("%s" kMultiDexSeparatorString "classes%zu.dex", dex_location, number + 1);
961 }
962}
963
964std::string DexFile::GetDexCanonicalLocation(const char* dex_location) {
965 CHECK_NE(dex_location, static_cast<const char*>(nullptr));
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100966 std::string base_location = GetBaseLocation(dex_location);
967 const char* suffix = dex_location + base_location.size();
968 DCHECK(suffix[0] == 0 || suffix[0] == kMultiDexSeparator);
969 UniqueCPtr<const char[]> path(realpath(base_location.c_str(), nullptr));
970 if (path != nullptr && path.get() != base_location) {
971 return std::string(path.get()) + suffix;
972 } else if (suffix[0] == 0) {
973 return base_location;
Calin Juravle4e1d5792014-07-15 23:56:47 +0100974 } else {
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100975 return dex_location;
Calin Juravle4e1d5792014-07-15 23:56:47 +0100976 }
Calin Juravle4e1d5792014-07-15 23:56:47 +0100977}
978
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800979std::ostream& operator<<(std::ostream& os, const DexFile& dex_file) {
980 os << StringPrintf("[DexFile: %s dex-checksum=%08x location-checksum=%08x %p-%p]",
981 dex_file.GetLocation().c_str(),
982 dex_file.GetHeader().checksum_, dex_file.GetLocationChecksum(),
983 dex_file.Begin(), dex_file.Begin() + dex_file.Size());
984 return os;
985}
Calin Juravle4e1d5792014-07-15 23:56:47 +0100986
Ian Rogersd91d6d62013-09-25 20:26:14 -0700987std::string Signature::ToString() const {
988 if (dex_file_ == nullptr) {
989 CHECK(proto_id_ == nullptr);
990 return "<no signature>";
991 }
992 const DexFile::TypeList* params = dex_file_->GetProtoParameters(*proto_id_);
993 std::string result;
994 if (params == nullptr) {
995 result += "()";
996 } else {
997 result += "(";
998 for (uint32_t i = 0; i < params->Size(); ++i) {
999 result += dex_file_->StringByTypeIdx(params->GetTypeItem(i).type_idx_);
1000 }
1001 result += ")";
1002 }
1003 result += dex_file_->StringByTypeIdx(proto_id_->return_type_idx_);
1004 return result;
1005}
1006
Vladimir Markod9cffea2013-11-25 15:08:02 +00001007bool Signature::operator==(const StringPiece& rhs) const {
1008 if (dex_file_ == nullptr) {
1009 return false;
1010 }
1011 StringPiece tail(rhs);
1012 if (!tail.starts_with("(")) {
1013 return false; // Invalid signature
1014 }
1015 tail.remove_prefix(1); // "(";
1016 const DexFile::TypeList* params = dex_file_->GetProtoParameters(*proto_id_);
1017 if (params != nullptr) {
1018 for (uint32_t i = 0; i < params->Size(); ++i) {
1019 StringPiece param(dex_file_->StringByTypeIdx(params->GetTypeItem(i).type_idx_));
1020 if (!tail.starts_with(param)) {
1021 return false;
1022 }
1023 tail.remove_prefix(param.length());
1024 }
1025 }
1026 if (!tail.starts_with(")")) {
1027 return false;
1028 }
1029 tail.remove_prefix(1); // ")";
1030 return tail == dex_file_->StringByTypeIdx(proto_id_->return_type_idx_);
1031}
1032
Ian Rogersd91d6d62013-09-25 20:26:14 -07001033std::ostream& operator<<(std::ostream& os, const Signature& sig) {
1034 return os << sig.ToString();
1035}
1036
Ian Rogers0571d352011-11-03 19:51:38 -07001037// Decodes the header section from the class data bytes.
1038void ClassDataItemIterator::ReadClassDataHeader() {
1039 CHECK(ptr_pos_ != NULL);
1040 header_.static_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_);
1041 header_.instance_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_);
1042 header_.direct_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_);
1043 header_.virtual_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_);
1044}
1045
1046void ClassDataItemIterator::ReadClassDataField() {
1047 field_.field_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_);
1048 field_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_);
Brian Carlstrom68adbe42012-05-11 17:18:08 -07001049 if (last_idx_ != 0 && field_.field_idx_delta_ == 0) {
Andreas Gampe4fdbba02014-06-19 20:24:22 -07001050 LOG(WARNING) << "Duplicate field in " << dex_file_.GetLocation();
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -07001051 }
Ian Rogers0571d352011-11-03 19:51:38 -07001052}
1053
1054void ClassDataItemIterator::ReadClassDataMethod() {
1055 method_.method_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_);
1056 method_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_);
1057 method_.code_off_ = DecodeUnsignedLeb128(&ptr_pos_);
Brian Carlstrom68adbe42012-05-11 17:18:08 -07001058 if (last_idx_ != 0 && method_.method_idx_delta_ == 0) {
Andreas Gampe4fdbba02014-06-19 20:24:22 -07001059 LOG(WARNING) << "Duplicate method in " << dex_file_.GetLocation();
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -07001060 }
Ian Rogers0571d352011-11-03 19:51:38 -07001061}
1062
1063// Read a signed integer. "zwidth" is the zero-based byte count.
Ian Rogers13735952014-10-08 12:43:28 -07001064static int32_t ReadSignedInt(const uint8_t* ptr, int zwidth) {
Ian Rogers0571d352011-11-03 19:51:38 -07001065 int32_t val = 0;
1066 for (int i = zwidth; i >= 0; --i) {
1067 val = ((uint32_t)val >> 8) | (((int32_t)*ptr++) << 24);
1068 }
1069 val >>= (3 - zwidth) * 8;
1070 return val;
1071}
1072
1073// Read an unsigned integer. "zwidth" is the zero-based byte count,
1074// "fill_on_right" indicates which side we want to zero-fill from.
Ian Rogers13735952014-10-08 12:43:28 -07001075static uint32_t ReadUnsignedInt(const uint8_t* ptr, int zwidth, bool fill_on_right) {
Ian Rogers0571d352011-11-03 19:51:38 -07001076 uint32_t val = 0;
1077 if (!fill_on_right) {
1078 for (int i = zwidth; i >= 0; --i) {
1079 val = (val >> 8) | (((uint32_t)*ptr++) << 24);
1080 }
1081 val >>= (3 - zwidth) * 8;
1082 } else {
1083 for (int i = zwidth; i >= 0; --i) {
1084 val = (val >> 8) | (((uint32_t)*ptr++) << 24);
1085 }
1086 }
1087 return val;
1088}
1089
1090// Read a signed long. "zwidth" is the zero-based byte count.
Ian Rogers13735952014-10-08 12:43:28 -07001091static int64_t ReadSignedLong(const uint8_t* ptr, int zwidth) {
Ian Rogers0571d352011-11-03 19:51:38 -07001092 int64_t val = 0;
1093 for (int i = zwidth; i >= 0; --i) {
1094 val = ((uint64_t)val >> 8) | (((int64_t)*ptr++) << 56);
1095 }
1096 val >>= (7 - zwidth) * 8;
1097 return val;
1098}
1099
1100// Read an unsigned long. "zwidth" is the zero-based byte count,
1101// "fill_on_right" indicates which side we want to zero-fill from.
Ian Rogers13735952014-10-08 12:43:28 -07001102static uint64_t ReadUnsignedLong(const uint8_t* ptr, int zwidth, bool fill_on_right) {
Ian Rogers0571d352011-11-03 19:51:38 -07001103 uint64_t val = 0;
1104 if (!fill_on_right) {
1105 for (int i = zwidth; i >= 0; --i) {
1106 val = (val >> 8) | (((uint64_t)*ptr++) << 56);
1107 }
1108 val >>= (7 - zwidth) * 8;
1109 } else {
1110 for (int i = zwidth; i >= 0; --i) {
1111 val = (val >> 8) | (((uint64_t)*ptr++) << 56);
1112 }
1113 }
1114 return val;
1115}
1116
1117EncodedStaticFieldValueIterator::EncodedStaticFieldValueIterator(const DexFile& dex_file,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001118 Handle<mirror::DexCache>* dex_cache,
1119 Handle<mirror::ClassLoader>* class_loader,
Ian Rogersca190662012-06-26 15:45:57 -07001120 ClassLinker* linker,
1121 const DexFile::ClassDef& class_def)
Brian Carlstrom88f36542012-10-16 23:24:21 -07001122 : dex_file_(dex_file), dex_cache_(dex_cache), class_loader_(class_loader), linker_(linker),
1123 array_size_(), pos_(-1), type_(kByte) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001124 DCHECK(dex_cache != nullptr);
1125 DCHECK(class_loader != nullptr);
Ian Rogers0571d352011-11-03 19:51:38 -07001126 ptr_ = dex_file.GetEncodedStaticFieldValuesArray(class_def);
1127 if (ptr_ == NULL) {
1128 array_size_ = 0;
1129 } else {
1130 array_size_ = DecodeUnsignedLeb128(&ptr_);
1131 }
1132 if (array_size_ > 0) {
1133 Next();
1134 }
1135}
1136
1137void EncodedStaticFieldValueIterator::Next() {
1138 pos_++;
1139 if (pos_ >= array_size_) {
1140 return;
1141 }
Ian Rogers13735952014-10-08 12:43:28 -07001142 uint8_t value_type = *ptr_++;
1143 uint8_t value_arg = value_type >> kEncodedValueArgShift;
Ian Rogers0571d352011-11-03 19:51:38 -07001144 size_t width = value_arg + 1; // assume and correct later
Brian Carlstrom88f36542012-10-16 23:24:21 -07001145 type_ = static_cast<ValueType>(value_type & kEncodedValueTypeMask);
Ian Rogers0571d352011-11-03 19:51:38 -07001146 switch (type_) {
1147 case kBoolean:
1148 jval_.i = (value_arg != 0) ? 1 : 0;
1149 width = 0;
1150 break;
1151 case kByte:
1152 jval_.i = ReadSignedInt(ptr_, value_arg);
1153 CHECK(IsInt(8, jval_.i));
1154 break;
1155 case kShort:
1156 jval_.i = ReadSignedInt(ptr_, value_arg);
1157 CHECK(IsInt(16, jval_.i));
1158 break;
1159 case kChar:
1160 jval_.i = ReadUnsignedInt(ptr_, value_arg, false);
1161 CHECK(IsUint(16, jval_.i));
1162 break;
1163 case kInt:
1164 jval_.i = ReadSignedInt(ptr_, value_arg);
1165 break;
1166 case kLong:
1167 jval_.j = ReadSignedLong(ptr_, value_arg);
1168 break;
1169 case kFloat:
1170 jval_.i = ReadUnsignedInt(ptr_, value_arg, true);
1171 break;
1172 case kDouble:
1173 jval_.j = ReadUnsignedLong(ptr_, value_arg, true);
1174 break;
1175 case kString:
1176 case kType:
Ian Rogers0571d352011-11-03 19:51:38 -07001177 jval_.i = ReadUnsignedInt(ptr_, value_arg, false);
1178 break;
1179 case kField:
Brian Carlstrom88f36542012-10-16 23:24:21 -07001180 case kMethod:
1181 case kEnum:
Ian Rogers0571d352011-11-03 19:51:38 -07001182 case kArray:
1183 case kAnnotation:
1184 UNIMPLEMENTED(FATAL) << ": type " << type_;
Ian Rogers2c4257b2014-10-24 14:20:06 -07001185 UNREACHABLE();
Ian Rogers0571d352011-11-03 19:51:38 -07001186 case kNull:
1187 jval_.l = NULL;
1188 width = 0;
1189 break;
1190 default:
1191 LOG(FATAL) << "Unreached";
Ian Rogers2c4257b2014-10-24 14:20:06 -07001192 UNREACHABLE();
Ian Rogers0571d352011-11-03 19:51:38 -07001193 }
1194 ptr_ += width;
1195}
1196
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001197template<bool kTransactionActive>
Hiroshi Yamauchi67ef46a2014-08-21 15:59:43 -07001198void EncodedStaticFieldValueIterator::ReadValueToField(Handle<mirror::ArtField> field) const {
Ian Rogers0571d352011-11-03 19:51:38 -07001199 switch (type_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001200 case kBoolean: field->SetBoolean<kTransactionActive>(field->GetDeclaringClass(), jval_.z); break;
1201 case kByte: field->SetByte<kTransactionActive>(field->GetDeclaringClass(), jval_.b); break;
1202 case kShort: field->SetShort<kTransactionActive>(field->GetDeclaringClass(), jval_.s); break;
1203 case kChar: field->SetChar<kTransactionActive>(field->GetDeclaringClass(), jval_.c); break;
1204 case kInt: field->SetInt<kTransactionActive>(field->GetDeclaringClass(), jval_.i); break;
1205 case kLong: field->SetLong<kTransactionActive>(field->GetDeclaringClass(), jval_.j); break;
1206 case kFloat: field->SetFloat<kTransactionActive>(field->GetDeclaringClass(), jval_.f); break;
1207 case kDouble: field->SetDouble<kTransactionActive>(field->GetDeclaringClass(), jval_.d); break;
1208 case kNull: field->SetObject<kTransactionActive>(field->GetDeclaringClass(), NULL); break;
Ian Rogers0571d352011-11-03 19:51:38 -07001209 case kString: {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001210 CHECK(!kMovingFields);
1211 mirror::String* resolved = linker_->ResolveString(dex_file_, jval_.i, *dex_cache_);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001212 field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved);
Ian Rogers0571d352011-11-03 19:51:38 -07001213 break;
1214 }
Brian Carlstrom88f36542012-10-16 23:24:21 -07001215 case kType: {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001216 CHECK(!kMovingFields);
1217 mirror::Class* resolved = linker_->ResolveType(dex_file_, jval_.i, *dex_cache_,
1218 *class_loader_);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001219 field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved);
Brian Carlstrom88f36542012-10-16 23:24:21 -07001220 break;
1221 }
Ian Rogers0571d352011-11-03 19:51:38 -07001222 default: UNIMPLEMENTED(FATAL) << ": type " << type_;
1223 }
1224}
Hiroshi Yamauchi67ef46a2014-08-21 15:59:43 -07001225template void EncodedStaticFieldValueIterator::ReadValueToField<true>(Handle<mirror::ArtField> field) const;
1226template void EncodedStaticFieldValueIterator::ReadValueToField<false>(Handle<mirror::ArtField> field) const;
Ian Rogers0571d352011-11-03 19:51:38 -07001227
1228CatchHandlerIterator::CatchHandlerIterator(const DexFile::CodeItem& code_item, uint32_t address) {
1229 handler_.address_ = -1;
1230 int32_t offset = -1;
1231
1232 // Short-circuit the overwhelmingly common cases.
1233 switch (code_item.tries_size_) {
1234 case 0:
1235 break;
1236 case 1: {
1237 const DexFile::TryItem* tries = DexFile::GetTryItems(code_item, 0);
1238 uint32_t start = tries->start_addr_;
1239 if (address >= start) {
1240 uint32_t end = start + tries->insn_count_;
1241 if (address < end) {
1242 offset = tries->handler_off_;
1243 }
1244 }
1245 break;
1246 }
1247 default:
Ian Rogersdbbc99d2013-04-18 16:51:54 -07001248 offset = DexFile::FindCatchHandlerOffset(code_item, address);
Ian Rogers0571d352011-11-03 19:51:38 -07001249 }
Logan Chien736df022012-04-27 16:25:57 +08001250 Init(code_item, offset);
1251}
1252
1253CatchHandlerIterator::CatchHandlerIterator(const DexFile::CodeItem& code_item,
1254 const DexFile::TryItem& try_item) {
1255 handler_.address_ = -1;
1256 Init(code_item, try_item.handler_off_);
1257}
1258
1259void CatchHandlerIterator::Init(const DexFile::CodeItem& code_item,
1260 int32_t offset) {
Ian Rogers0571d352011-11-03 19:51:38 -07001261 if (offset >= 0) {
Logan Chien736df022012-04-27 16:25:57 +08001262 Init(DexFile::GetCatchHandlerData(code_item, offset));
Ian Rogers0571d352011-11-03 19:51:38 -07001263 } else {
1264 // Not found, initialize as empty
1265 current_data_ = NULL;
1266 remaining_count_ = -1;
1267 catch_all_ = false;
1268 DCHECK(!HasNext());
1269 }
1270}
1271
Ian Rogers13735952014-10-08 12:43:28 -07001272void CatchHandlerIterator::Init(const uint8_t* handler_data) {
Ian Rogers0571d352011-11-03 19:51:38 -07001273 current_data_ = handler_data;
1274 remaining_count_ = DecodeSignedLeb128(&current_data_);
1275
1276 // If remaining_count_ is non-positive, then it is the negative of
1277 // the number of catch types, and the catches are followed by a
1278 // catch-all handler.
1279 if (remaining_count_ <= 0) {
1280 catch_all_ = true;
1281 remaining_count_ = -remaining_count_;
1282 } else {
1283 catch_all_ = false;
1284 }
1285 Next();
1286}
1287
1288void CatchHandlerIterator::Next() {
1289 if (remaining_count_ > 0) {
1290 handler_.type_idx_ = DecodeUnsignedLeb128(&current_data_);
1291 handler_.address_ = DecodeUnsignedLeb128(&current_data_);
1292 remaining_count_--;
1293 return;
1294 }
1295
1296 if (catch_all_) {
1297 handler_.type_idx_ = DexFile::kDexNoIndex16;
1298 handler_.address_ = DecodeUnsignedLeb128(&current_data_);
1299 catch_all_ = false;
1300 return;
1301 }
1302
1303 // no more handler
1304 remaining_count_ = -1;
1305}
1306
Carl Shapiro1fb86202011-06-27 17:43:13 -07001307} // namespace art