blob: 19a4bd0b6f41c62757f32b0cc4955577d169068b [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"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070042#include "handle_scope-inl.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070043#include "thread.h"
Ian Rogersa6724902013-09-23 09:23:37 -070044#include "utf-inl.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070045#include "utils.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070046#include "well_known_classes.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070047#include "zip_archive.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070048
Andreas Gampe277ccbd2014-11-03 21:36:10 -080049#pragma GCC diagnostic push
50#pragma GCC diagnostic ignored "-Wshadow"
51#include "ScopedFd.h"
52#pragma GCC diagnostic pop
53
Carl Shapiro1fb86202011-06-27 17:43:13 -070054namespace art {
55
Ian Rogers13735952014-10-08 12:43:28 -070056const uint8_t DexFile::kDexMagic[] = { 'd', 'e', 'x', '\n' };
57const uint8_t DexFile::kDexMagicVersion[] = { '0', '3', '5', '\0' };
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070058
Ian Rogers8d31bbd2013-10-13 10:44:14 -070059static int OpenAndReadMagic(const char* filename, uint32_t* magic, std::string* error_msg) {
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070060 CHECK(magic != NULL);
Vladimir Markofd995762013-11-06 16:36:36 +000061 ScopedFd fd(open(filename, O_RDONLY, 0));
62 if (fd.get() == -1) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070063 *error_msg = StringPrintf("Unable to open '%s' : %s", filename, strerror(errno));
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070064 return -1;
65 }
Vladimir Markofd995762013-11-06 16:36:36 +000066 int n = TEMP_FAILURE_RETRY(read(fd.get(), magic, sizeof(*magic)));
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070067 if (n != sizeof(*magic)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070068 *error_msg = StringPrintf("Failed to find magic in '%s'", filename);
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070069 return -1;
70 }
Vladimir Markofd995762013-11-06 16:36:36 +000071 if (lseek(fd.get(), 0, SEEK_SET) != 0) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070072 *error_msg = StringPrintf("Failed to seek to beginning of file '%s' : %s", filename,
73 strerror(errno));
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070074 return -1;
75 }
Vladimir Markofd995762013-11-06 16:36:36 +000076 return fd.release();
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070077}
78
Ian Rogers8d31bbd2013-10-13 10:44:14 -070079bool DexFile::GetChecksum(const char* filename, uint32_t* checksum, std::string* error_msg) {
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070080 CHECK(checksum != NULL);
81 uint32_t magic;
Andreas Gampe833a4852014-05-21 18:46:59 -070082
83 // Strip ":...", which is the location
84 const char* zip_entry_name = kClassesDex;
85 const char* file_part = filename;
Vladimir Markoaa4497d2014-09-05 14:01:17 +010086 std::string file_part_storage;
Andreas Gampe833a4852014-05-21 18:46:59 -070087
Vladimir Markoaa4497d2014-09-05 14:01:17 +010088 if (DexFile::IsMultiDexLocation(filename)) {
89 file_part_storage = GetBaseLocation(filename);
90 file_part = file_part_storage.c_str();
91 zip_entry_name = filename + file_part_storage.size() + 1;
92 DCHECK_EQ(zip_entry_name[-1], kMultiDexSeparator);
Andreas Gampe833a4852014-05-21 18:46:59 -070093 }
94
95 ScopedFd fd(OpenAndReadMagic(file_part, &magic, error_msg));
Vladimir Markofd995762013-11-06 16:36:36 +000096 if (fd.get() == -1) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070097 DCHECK(!error_msg->empty());
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -070098 return false;
99 }
100 if (IsZipMagic(magic)) {
Ian Rogers700a4022014-05-19 16:49:03 -0700101 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(fd.release(), filename, error_msg));
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800102 if (zip_archive.get() == NULL) {
Andreas Gampe833a4852014-05-21 18:46:59 -0700103 *error_msg = StringPrintf("Failed to open zip archive '%s'", file_part);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800104 return false;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700105 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700106 std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(zip_entry_name, error_msg));
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800107 if (zip_entry.get() == NULL) {
Andreas Gampe833a4852014-05-21 18:46:59 -0700108 *error_msg = StringPrintf("Zip archive '%s' doesn't contain %s (error msg: %s)", file_part,
109 zip_entry_name, error_msg->c_str());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800110 return false;
111 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700112 *checksum = zip_entry->GetCrc32();
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800113 return true;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700114 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700115 if (IsDexMagic(magic)) {
Ian Rogers700a4022014-05-19 16:49:03 -0700116 std::unique_ptr<const DexFile> dex_file(DexFile::OpenFile(fd.release(), filename, false, error_msg));
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800117 if (dex_file.get() == NULL) {
118 return false;
119 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700120 *checksum = dex_file->GetHeader().checksum_;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800121 return true;
122 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700123 *error_msg = StringPrintf("Expected valid zip or dex file: '%s'", filename);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800124 return false;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700125}
126
Andreas Gampe833a4852014-05-21 18:46:59 -0700127bool DexFile::Open(const char* filename, const char* location, std::string* error_msg,
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800128 std::vector<std::unique_ptr<const DexFile>>* dex_files) {
129 DCHECK(dex_files != nullptr) << "DexFile::Open: out-param is NULL";
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700130 uint32_t magic;
Vladimir Markofd995762013-11-06 16:36:36 +0000131 ScopedFd fd(OpenAndReadMagic(filename, &magic, error_msg));
132 if (fd.get() == -1) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700133 DCHECK(!error_msg->empty());
Andreas Gampe833a4852014-05-21 18:46:59 -0700134 return false;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700135 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700136 if (IsZipMagic(magic)) {
Andreas Gampe833a4852014-05-21 18:46:59 -0700137 return DexFile::OpenZip(fd.release(), location, error_msg, dex_files);
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700138 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700139 if (IsDexMagic(magic)) {
Andreas Gampe833a4852014-05-21 18:46:59 -0700140 std::unique_ptr<const DexFile> dex_file(DexFile::OpenFile(fd.release(), location, true,
141 error_msg));
142 if (dex_file.get() != nullptr) {
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800143 dex_files->push_back(std::move(dex_file));
Andreas Gampe833a4852014-05-21 18:46:59 -0700144 return true;
145 } else {
146 return false;
147 }
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700148 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700149 *error_msg = StringPrintf("Expected valid zip or dex file: '%s'", filename);
Alexander Ivchenkobacce5c2014-06-26 16:32:11 +0400150 return false;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700151}
152
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800153int DexFile::GetPermissions() const {
154 if (mem_map_.get() == NULL) {
155 return 0;
156 } else {
157 return mem_map_->GetProtect();
158 }
159}
160
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200161bool DexFile::IsReadOnly() const {
162 return GetPermissions() == PROT_READ;
163}
164
Brian Carlstrome0948e12013-08-29 09:36:15 -0700165bool DexFile::EnableWrite() const {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200166 CHECK(IsReadOnly());
167 if (mem_map_.get() == NULL) {
168 return false;
169 } else {
Brian Carlstrome0948e12013-08-29 09:36:15 -0700170 return mem_map_->Protect(PROT_READ | PROT_WRITE);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200171 }
172}
173
Brian Carlstrome0948e12013-08-29 09:36:15 -0700174bool DexFile::DisableWrite() const {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200175 CHECK(!IsReadOnly());
176 if (mem_map_.get() == NULL) {
177 return false;
178 } else {
Brian Carlstrome0948e12013-08-29 09:36:15 -0700179 return mem_map_->Protect(PROT_READ);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200180 }
181}
182
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800183std::unique_ptr<const DexFile> DexFile::OpenFile(int fd, const char* location, bool verify,
184 std::string* error_msg) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700185 CHECK(location != nullptr);
Ian Rogers700a4022014-05-19 16:49:03 -0700186 std::unique_ptr<MemMap> map;
Vladimir Markofd995762013-11-06 16:36:36 +0000187 {
188 ScopedFd delayed_close(fd);
189 struct stat sbuf;
190 memset(&sbuf, 0, sizeof(sbuf));
191 if (fstat(fd, &sbuf) == -1) {
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -0800192 *error_msg = StringPrintf("DexFile: fstat '%s' failed: %s", location, strerror(errno));
Vladimir Markofd995762013-11-06 16:36:36 +0000193 return nullptr;
194 }
195 if (S_ISDIR(sbuf.st_mode)) {
196 *error_msg = StringPrintf("Attempt to mmap directory '%s'", location);
197 return nullptr;
198 }
199 size_t length = sbuf.st_size;
200 map.reset(MemMap::MapFile(length, PROT_READ, MAP_PRIVATE, fd, 0, location, error_msg));
201 if (map.get() == nullptr) {
202 DCHECK(!error_msg->empty());
203 return nullptr;
204 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700205 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800206
207 if (map->Size() < sizeof(DexFile::Header)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700208 *error_msg = StringPrintf(
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -0800209 "DexFile: failed to open dex file '%s' that is too short to have a header", location);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700210 return nullptr;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800211 }
212
213 const Header* dex_header = reinterpret_cast<const Header*>(map->Begin());
214
Andreas Gampe928f72b2014-09-09 19:53:48 -0700215 std::unique_ptr<const DexFile> dex_file(OpenMemory(location, dex_header->checksum_, map.release(),
216 error_msg));
217 if (dex_file.get() == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700218 *error_msg = StringPrintf("Failed to open dex file '%s' from memory: %s", location,
219 error_msg->c_str());
220 return nullptr;
jeffhaof6174e82012-01-31 16:14:17 -0800221 }
jeffhao54c1ceb2012-02-01 11:45:32 -0800222
Andreas Gampe928f72b2014-09-09 19:53:48 -0700223 if (verify && !DexFileVerifier::Verify(dex_file.get(), dex_file->Begin(), dex_file->Size(),
224 location, error_msg)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700225 return nullptr;
jeffhao54c1ceb2012-02-01 11:45:32 -0800226 }
227
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800228 return dex_file;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700229}
230
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700231const char* DexFile::kClassesDex = "classes.dex";
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700232
Andreas Gampe833a4852014-05-21 18:46:59 -0700233bool DexFile::OpenZip(int fd, const std::string& location, std::string* error_msg,
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800234 std::vector<std::unique_ptr<const DexFile>>* dex_files) {
235 DCHECK(dex_files != nullptr) << "DexFile::OpenZip: out-param is NULL";
Ian Rogers700a4022014-05-19 16:49:03 -0700236 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(fd, location.c_str(), error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700237 if (zip_archive.get() == nullptr) {
238 DCHECK(!error_msg->empty());
Andreas Gampe833a4852014-05-21 18:46:59 -0700239 return false;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700240 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700241 return DexFile::OpenFromZip(*zip_archive, location, error_msg, dex_files);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800242}
243
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800244std::unique_ptr<const DexFile> DexFile::OpenMemory(const std::string& location,
245 uint32_t location_checksum,
246 MemMap* mem_map,
247 std::string* error_msg) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800248 return OpenMemory(mem_map->Begin(),
249 mem_map->Size(),
250 location,
251 location_checksum,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700252 mem_map,
Andreas Gampefd9eb392014-11-06 16:52:58 -0800253 nullptr,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700254 error_msg);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800255}
256
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800257std::unique_ptr<const DexFile> DexFile::Open(const ZipArchive& zip_archive, const char* entry_name,
258 const std::string& location, std::string* error_msg,
259 ZipOpenErrorCode* error_code) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800260 CHECK(!location.empty());
Andreas Gampe833a4852014-05-21 18:46:59 -0700261 std::unique_ptr<ZipEntry> zip_entry(zip_archive.Find(entry_name, error_msg));
Elliott Hughes90a33692011-08-30 13:27:07 -0700262 if (zip_entry.get() == NULL) {
Andreas Gampe833a4852014-05-21 18:46:59 -0700263 *error_code = ZipOpenErrorCode::kEntryNotFound;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700264 return nullptr;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700265 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700266 std::unique_ptr<MemMap> map(zip_entry->ExtractToMemMap(location.c_str(), entry_name, error_msg));
Brian Carlstrom89521892011-12-07 22:05:07 -0800267 if (map.get() == NULL) {
Andreas Gampe833a4852014-05-21 18:46:59 -0700268 *error_msg = StringPrintf("Failed to extract '%s' from '%s': %s", entry_name, location.c_str(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700269 error_msg->c_str());
Andreas Gampe833a4852014-05-21 18:46:59 -0700270 *error_code = ZipOpenErrorCode::kExtractToMemoryError;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700271 return nullptr;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700272 }
Ian Rogers700a4022014-05-19 16:49:03 -0700273 std::unique_ptr<const DexFile> dex_file(OpenMemory(location, zip_entry->GetCrc32(), map.release(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700274 error_msg));
275 if (dex_file.get() == nullptr) {
276 *error_msg = StringPrintf("Failed to open dex file '%s' from memory: %s", location.c_str(),
277 error_msg->c_str());
Andreas Gampe833a4852014-05-21 18:46:59 -0700278 *error_code = ZipOpenErrorCode::kDexFileError;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700279 return nullptr;
jeffhaof6174e82012-01-31 16:14:17 -0800280 }
Brian Carlstrome0948e12013-08-29 09:36:15 -0700281 if (!dex_file->DisableWrite()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700282 *error_msg = StringPrintf("Failed to make dex file '%s' read only", location.c_str());
Andreas Gampe833a4852014-05-21 18:46:59 -0700283 *error_code = ZipOpenErrorCode::kMakeReadOnlyError;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700284 return nullptr;
Brian Carlstrome0948e12013-08-29 09:36:15 -0700285 }
286 CHECK(dex_file->IsReadOnly()) << location;
Brian Carlstromd6cec902014-05-25 16:08:51 -0700287 if (!DexFileVerifier::Verify(dex_file.get(), dex_file->Begin(), dex_file->Size(),
288 location.c_str(), error_msg)) {
Andreas Gampe833a4852014-05-21 18:46:59 -0700289 *error_code = ZipOpenErrorCode::kVerifyError;
Brian Carlstromd6cec902014-05-25 16:08:51 -0700290 return nullptr;
291 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700292 *error_code = ZipOpenErrorCode::kNoError;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800293 return dex_file;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700294}
295
Andreas Gampe833a4852014-05-21 18:46:59 -0700296bool DexFile::OpenFromZip(const ZipArchive& zip_archive, const std::string& location,
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800297 std::string* error_msg,
298 std::vector<std::unique_ptr<const DexFile>>* dex_files) {
299 DCHECK(dex_files != nullptr) << "DexFile::OpenFromZip: out-param is NULL";
Andreas Gampe833a4852014-05-21 18:46:59 -0700300 ZipOpenErrorCode error_code;
301 std::unique_ptr<const DexFile> dex_file(Open(zip_archive, kClassesDex, location, error_msg,
302 &error_code));
303 if (dex_file.get() == nullptr) {
304 return false;
305 } else {
306 // Had at least classes.dex.
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800307 dex_files->push_back(std::move(dex_file));
Andreas Gampe833a4852014-05-21 18:46:59 -0700308
309 // Now try some more.
310 size_t i = 2;
311
312 // We could try to avoid std::string allocations by working on a char array directly. As we
313 // do not expect a lot of iterations, this seems too involved and brittle.
314
315 while (i < 100) {
316 std::string name = StringPrintf("classes%zu.dex", i);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100317 std::string fake_location = location + kMultiDexSeparator + name;
Andreas Gampe833a4852014-05-21 18:46:59 -0700318 std::unique_ptr<const DexFile> next_dex_file(Open(zip_archive, name.c_str(), fake_location,
319 error_msg, &error_code));
320 if (next_dex_file.get() == nullptr) {
321 if (error_code != ZipOpenErrorCode::kEntryNotFound) {
322 LOG(WARNING) << error_msg;
323 }
324 break;
325 } else {
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800326 dex_files->push_back(std::move(next_dex_file));
Andreas Gampe833a4852014-05-21 18:46:59 -0700327 }
328
329 i++;
330 }
331
332 return true;
333 }
334}
335
336
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800337std::unique_ptr<const DexFile> DexFile::OpenMemory(const uint8_t* base,
338 size_t size,
339 const std::string& location,
340 uint32_t location_checksum,
Andreas Gampefd9eb392014-11-06 16:52:58 -0800341 MemMap* mem_map,
342 const OatFile* oat_file,
343 std::string* error_msg) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700344 CHECK_ALIGNED(base, 4); // various dex file structures must be word aligned
Andreas Gampefd9eb392014-11-06 16:52:58 -0800345 std::unique_ptr<DexFile> dex_file(
346 new DexFile(base, size, location, location_checksum, mem_map, oat_file));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700347 if (!dex_file->Init(error_msg)) {
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800348 dex_file.reset();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700349 }
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800350 return std::unique_ptr<const DexFile>(dex_file.release());
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700351}
352
Ian Rogers13735952014-10-08 12:43:28 -0700353DexFile::DexFile(const uint8_t* base, size_t size,
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800354 const std::string& location,
355 uint32_t location_checksum,
Andreas Gampefd9eb392014-11-06 16:52:58 -0800356 MemMap* mem_map,
357 const OatFile* oat_file)
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800358 : begin_(base),
359 size_(size),
360 location_(location),
361 location_checksum_(location_checksum),
362 mem_map_(mem_map),
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800363 header_(reinterpret_cast<const Header*>(base)),
364 string_ids_(reinterpret_cast<const StringId*>(base + header_->string_ids_off_)),
365 type_ids_(reinterpret_cast<const TypeId*>(base + header_->type_ids_off_)),
366 field_ids_(reinterpret_cast<const FieldId*>(base + header_->field_ids_off_)),
367 method_ids_(reinterpret_cast<const MethodId*>(base + header_->method_ids_off_)),
368 proto_ids_(reinterpret_cast<const ProtoId*>(base + header_->proto_ids_off_)),
Ian Rogers68b56852014-08-29 20:19:11 -0700369 class_defs_(reinterpret_cast<const ClassDef*>(base + header_->class_defs_off_)),
370 find_class_def_misses_(0),
Andreas Gampefd9eb392014-11-06 16:52:58 -0800371 class_def_index_(nullptr),
372 oat_file_(oat_file) {
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800373 CHECK(begin_ != NULL) << GetLocation();
374 CHECK_GT(size_, 0U) << GetLocation();
375}
376
Jesse Wilson6bf19152011-09-29 13:12:33 -0400377DexFile::~DexFile() {
Elliott Hughes8cef0b82011-10-11 19:24:00 -0700378 // We don't call DeleteGlobalRef on dex_object_ because we're only called by DestroyJavaVM, and
379 // that's only called after DetachCurrentThread, which means there's no JNIEnv. We could
380 // re-attach, but cleaning up these global references is not obviously useful. It's not as if
381 // the global reference table is otherwise empty!
Ian Rogers68b56852014-08-29 20:19:11 -0700382 // Remove the index if one were created.
383 delete class_def_index_.LoadRelaxed();
Jesse Wilson6bf19152011-09-29 13:12:33 -0400384}
385
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700386bool DexFile::Init(std::string* error_msg) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700387 if (!CheckMagicAndVersion(error_msg)) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700388 return false;
389 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700390 return true;
391}
392
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700393bool DexFile::CheckMagicAndVersion(std::string* error_msg) const {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800394 if (!IsMagicValid(header_->magic_)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700395 std::ostringstream oss;
396 oss << "Unrecognized magic number in " << GetLocation() << ":"
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800397 << " " << header_->magic_[0]
398 << " " << header_->magic_[1]
399 << " " << header_->magic_[2]
400 << " " << header_->magic_[3];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700401 *error_msg = oss.str();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700402 return false;
403 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800404 if (!IsVersionValid(header_->magic_)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700405 std::ostringstream oss;
406 oss << "Unrecognized version number in " << GetLocation() << ":"
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800407 << " " << header_->magic_[4]
408 << " " << header_->magic_[5]
409 << " " << header_->magic_[6]
410 << " " << header_->magic_[7];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700411 *error_msg = oss.str();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700412 return false;
413 }
414 return true;
415}
416
Ian Rogers13735952014-10-08 12:43:28 -0700417bool DexFile::IsMagicValid(const uint8_t* magic) {
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800418 return (memcmp(magic, kDexMagic, sizeof(kDexMagic)) == 0);
419}
420
Ian Rogers13735952014-10-08 12:43:28 -0700421bool DexFile::IsVersionValid(const uint8_t* magic) {
422 const uint8_t* version = &magic[sizeof(kDexMagic)];
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800423 return (memcmp(version, kDexMagicVersion, sizeof(kDexMagicVersion)) == 0);
424}
425
Ian Rogersd81871c2011-10-03 13:57:23 -0700426uint32_t DexFile::GetVersion() const {
427 const char* version = reinterpret_cast<const char*>(&GetHeader().magic_[sizeof(kDexMagic)]);
428 return atoi(version);
429}
430
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800431const DexFile::ClassDef* DexFile::FindClassDef(const char* descriptor, size_t hash) const {
432 DCHECK_EQ(ComputeModifiedUtf8Hash(descriptor), hash);
Ian Rogers68b56852014-08-29 20:19:11 -0700433 // If we have an index lookup the descriptor via that as its constant time to search.
434 Index* index = class_def_index_.LoadSequentiallyConsistent();
435 if (index != nullptr) {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800436 auto it = index->FindWithHash(descriptor, hash);
Ian Rogers68b56852014-08-29 20:19:11 -0700437 return (it == index->end()) ? nullptr : it->second;
438 }
439 // Fast path for rate no class defs case.
440 uint32_t num_class_defs = NumClassDefs();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700441 if (num_class_defs == 0) {
Ian Rogers68b56852014-08-29 20:19:11 -0700442 return nullptr;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700443 }
Ian Rogers68b56852014-08-29 20:19:11 -0700444 // Search for class def with 2 binary searches and then a linear search.
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700445 const StringId* string_id = FindStringId(descriptor);
Ian Rogers68b56852014-08-29 20:19:11 -0700446 if (string_id != nullptr) {
447 const TypeId* type_id = FindTypeId(GetIndexForStringId(*string_id));
448 if (type_id != nullptr) {
449 uint16_t type_idx = GetIndexForTypeId(*type_id);
450 for (size_t i = 0; i < num_class_defs; ++i) {
451 const ClassDef& class_def = GetClassDef(i);
452 if (class_def.class_idx_ == type_idx) {
453 return &class_def;
454 }
455 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700456 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700457 }
Ian Rogers68b56852014-08-29 20:19:11 -0700458 // A miss. If we've had kMaxFailedDexClassDefLookups misses then build an index to speed things
459 // up. This isn't done eagerly at construction as construction is not performed in multi-threaded
460 // sections of tools like dex2oat. If we're lazy we hopefully increase the chance of balancing
461 // out which thread builds the index.
Ian Rogers68b56852014-08-29 20:19:11 -0700462 const uint32_t kMaxFailedDexClassDefLookups = 100;
Ian Rogersecaebd32014-09-12 23:10:21 -0700463 uint32_t old_misses = find_class_def_misses_.FetchAndAddSequentiallyConsistent(1);
464 if (old_misses == kMaxFailedDexClassDefLookups) {
465 // Are we the ones moving the miss count past the max? Sanity check the index doesn't exist.
466 CHECK(class_def_index_.LoadSequentiallyConsistent() == nullptr);
467 // Build the index.
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800468 index = new Index();
Ian Rogersecaebd32014-09-12 23:10:21 -0700469 for (uint32_t i = 0; i < num_class_defs; ++i) {
470 const ClassDef& class_def = GetClassDef(i);
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800471 const char* class_descriptor = GetClassDescriptor(class_def);
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800472 index->Insert(std::make_pair(class_descriptor, &class_def));
Ian Rogers68b56852014-08-29 20:19:11 -0700473 }
Ian Rogersecaebd32014-09-12 23:10:21 -0700474 // Sanity check the index still doesn't exist, only 1 thread should build it.
475 CHECK(class_def_index_.LoadSequentiallyConsistent() == nullptr);
476 class_def_index_.StoreSequentiallyConsistent(index);
Ian Rogers68b56852014-08-29 20:19:11 -0700477 }
478 return nullptr;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700479}
480
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700481const DexFile::ClassDef* DexFile::FindClassDef(uint16_t type_idx) const {
482 size_t num_class_defs = NumClassDefs();
483 for (size_t i = 0; i < num_class_defs; ++i) {
484 const ClassDef& class_def = GetClassDef(i);
485 if (class_def.class_idx_ == type_idx) {
486 return &class_def;
487 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700488 }
489 return NULL;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700490}
491
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800492const DexFile::FieldId* DexFile::FindFieldId(const DexFile::TypeId& declaring_klass,
493 const DexFile::StringId& name,
494 const DexFile::TypeId& type) const {
495 // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx
496 const uint16_t class_idx = GetIndexForTypeId(declaring_klass);
497 const uint32_t name_idx = GetIndexForStringId(name);
498 const uint16_t type_idx = GetIndexForTypeId(type);
Ian Rogersf8582c32013-05-29 16:33:03 -0700499 int32_t lo = 0;
500 int32_t hi = NumFieldIds() - 1;
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800501 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700502 int32_t mid = (hi + lo) / 2;
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800503 const DexFile::FieldId& field = GetFieldId(mid);
504 if (class_idx > field.class_idx_) {
505 lo = mid + 1;
506 } else if (class_idx < field.class_idx_) {
507 hi = mid - 1;
508 } else {
509 if (name_idx > field.name_idx_) {
510 lo = mid + 1;
511 } else if (name_idx < field.name_idx_) {
512 hi = mid - 1;
513 } else {
514 if (type_idx > field.type_idx_) {
515 lo = mid + 1;
516 } else if (type_idx < field.type_idx_) {
517 hi = mid - 1;
518 } else {
519 return &field;
520 }
521 }
522 }
523 }
524 return NULL;
525}
526
527const DexFile::MethodId* DexFile::FindMethodId(const DexFile::TypeId& declaring_klass,
Ian Rogers0571d352011-11-03 19:51:38 -0700528 const DexFile::StringId& name,
529 const DexFile::ProtoId& signature) const {
530 // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800531 const uint16_t class_idx = GetIndexForTypeId(declaring_klass);
Ian Rogers0571d352011-11-03 19:51:38 -0700532 const uint32_t name_idx = GetIndexForStringId(name);
533 const uint16_t proto_idx = GetIndexForProtoId(signature);
Ian Rogersf8582c32013-05-29 16:33:03 -0700534 int32_t lo = 0;
535 int32_t hi = NumMethodIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700536 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700537 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700538 const DexFile::MethodId& method = GetMethodId(mid);
539 if (class_idx > method.class_idx_) {
540 lo = mid + 1;
541 } else if (class_idx < method.class_idx_) {
542 hi = mid - 1;
543 } else {
544 if (name_idx > method.name_idx_) {
545 lo = mid + 1;
546 } else if (name_idx < method.name_idx_) {
547 hi = mid - 1;
548 } else {
549 if (proto_idx > method.proto_idx_) {
550 lo = mid + 1;
551 } else if (proto_idx < method.proto_idx_) {
552 hi = mid - 1;
553 } else {
554 return &method;
555 }
556 }
557 }
558 }
559 return NULL;
560}
561
Ian Rogers637c65b2013-05-31 11:46:00 -0700562const DexFile::StringId* DexFile::FindStringId(const char* string) const {
Ian Rogersf8582c32013-05-29 16:33:03 -0700563 int32_t lo = 0;
564 int32_t hi = NumStringIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700565 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700566 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700567 const DexFile::StringId& str_id = GetStringId(mid);
Ian Rogerscf5077a2013-10-31 12:37:54 -0700568 const char* str = GetStringData(str_id);
Ian Rogers637c65b2013-05-31 11:46:00 -0700569 int compare = CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(string, str);
570 if (compare > 0) {
571 lo = mid + 1;
572 } else if (compare < 0) {
573 hi = mid - 1;
574 } else {
575 return &str_id;
576 }
577 }
578 return NULL;
579}
580
Vladimir Markoa48aef42014-12-03 17:53:53 +0000581const DexFile::StringId* DexFile::FindStringId(const uint16_t* string, size_t length) const {
Ian Rogers637c65b2013-05-31 11:46:00 -0700582 int32_t lo = 0;
583 int32_t hi = NumStringIds() - 1;
584 while (hi >= lo) {
585 int32_t mid = (hi + lo) / 2;
Ian Rogers637c65b2013-05-31 11:46:00 -0700586 const DexFile::StringId& str_id = GetStringId(mid);
Ian Rogerscf5077a2013-10-31 12:37:54 -0700587 const char* str = GetStringData(str_id);
Vladimir Markoa48aef42014-12-03 17:53:53 +0000588 int compare = CompareModifiedUtf8ToUtf16AsCodePointValues(str, string, length);
Ian Rogers0571d352011-11-03 19:51:38 -0700589 if (compare > 0) {
590 lo = mid + 1;
591 } else if (compare < 0) {
592 hi = mid - 1;
593 } else {
594 return &str_id;
595 }
596 }
597 return NULL;
598}
599
600const DexFile::TypeId* DexFile::FindTypeId(uint32_t string_idx) const {
Ian Rogersf8582c32013-05-29 16:33:03 -0700601 int32_t lo = 0;
602 int32_t hi = NumTypeIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700603 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700604 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700605 const TypeId& type_id = GetTypeId(mid);
606 if (string_idx > type_id.descriptor_idx_) {
607 lo = mid + 1;
608 } else if (string_idx < type_id.descriptor_idx_) {
609 hi = mid - 1;
610 } else {
611 return &type_id;
612 }
613 }
614 return NULL;
615}
616
617const DexFile::ProtoId* DexFile::FindProtoId(uint16_t return_type_idx,
Vladimir Marko5c96e6b2013-11-14 15:34:17 +0000618 const uint16_t* signature_type_idxs,
619 uint32_t signature_length) const {
Ian Rogersf8582c32013-05-29 16:33:03 -0700620 int32_t lo = 0;
621 int32_t hi = NumProtoIds() - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700622 while (hi >= lo) {
Ian Rogersf8582c32013-05-29 16:33:03 -0700623 int32_t mid = (hi + lo) / 2;
Ian Rogers0571d352011-11-03 19:51:38 -0700624 const DexFile::ProtoId& proto = GetProtoId(mid);
625 int compare = return_type_idx - proto.return_type_idx_;
626 if (compare == 0) {
627 DexFileParameterIterator it(*this, proto);
628 size_t i = 0;
Vladimir Marko5c96e6b2013-11-14 15:34:17 +0000629 while (it.HasNext() && i < signature_length && compare == 0) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800630 compare = signature_type_idxs[i] - it.GetTypeIdx();
Ian Rogers0571d352011-11-03 19:51:38 -0700631 it.Next();
632 i++;
633 }
634 if (compare == 0) {
635 if (it.HasNext()) {
636 compare = -1;
Vladimir Marko5c96e6b2013-11-14 15:34:17 +0000637 } else if (i < signature_length) {
Ian Rogers0571d352011-11-03 19:51:38 -0700638 compare = 1;
639 }
640 }
641 }
642 if (compare > 0) {
643 lo = mid + 1;
644 } else if (compare < 0) {
645 hi = mid - 1;
646 } else {
647 return &proto;
648 }
649 }
650 return NULL;
651}
652
653// Given a signature place the type ids into the given vector
Ian Rogersd91d6d62013-09-25 20:26:14 -0700654bool DexFile::CreateTypeList(const StringPiece& signature, uint16_t* return_type_idx,
655 std::vector<uint16_t>* param_type_idxs) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700656 if (signature[0] != '(') {
657 return false;
658 }
659 size_t offset = 1;
660 size_t end = signature.size();
661 bool process_return = false;
662 while (offset < end) {
Vladimir Markoe9c36b32013-11-21 15:49:16 +0000663 size_t start_offset = offset;
Ian Rogers0571d352011-11-03 19:51:38 -0700664 char c = signature[offset];
665 offset++;
666 if (c == ')') {
667 process_return = true;
668 continue;
669 }
Ian Rogers0571d352011-11-03 19:51:38 -0700670 while (c == '[') { // process array prefix
671 if (offset >= end) { // expect some descriptor following [
672 return false;
673 }
674 c = signature[offset];
675 offset++;
Ian Rogers0571d352011-11-03 19:51:38 -0700676 }
677 if (c == 'L') { // process type descriptors
678 do {
679 if (offset >= end) { // unexpected early termination of descriptor
680 return false;
681 }
682 c = signature[offset];
683 offset++;
Ian Rogers0571d352011-11-03 19:51:38 -0700684 } while (c != ';');
685 }
Vladimir Markoe9c36b32013-11-21 15:49:16 +0000686 // TODO: avoid creating a std::string just to get a 0-terminated char array
687 std::string descriptor(signature.data() + start_offset, offset - start_offset);
Ian Rogers637c65b2013-05-31 11:46:00 -0700688 const DexFile::StringId* string_id = FindStringId(descriptor.c_str());
Ian Rogers0571d352011-11-03 19:51:38 -0700689 if (string_id == NULL) {
690 return false;
691 }
692 const DexFile::TypeId* type_id = FindTypeId(GetIndexForStringId(*string_id));
693 if (type_id == NULL) {
694 return false;
695 }
696 uint16_t type_idx = GetIndexForTypeId(*type_id);
697 if (!process_return) {
698 param_type_idxs->push_back(type_idx);
699 } else {
700 *return_type_idx = type_idx;
701 return offset == end; // return true if the signature had reached a sensible end
702 }
703 }
704 return false; // failed to correctly parse return type
705}
706
Ian Rogersd91d6d62013-09-25 20:26:14 -0700707const Signature DexFile::CreateSignature(const StringPiece& signature) const {
708 uint16_t return_type_idx;
709 std::vector<uint16_t> param_type_indices;
710 bool success = CreateTypeList(signature, &return_type_idx, &param_type_indices);
711 if (!success) {
712 return Signature::NoSignature();
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700713 }
Ian Rogersd91d6d62013-09-25 20:26:14 -0700714 const ProtoId* proto_id = FindProtoId(return_type_idx, param_type_indices);
715 if (proto_id == NULL) {
716 return Signature::NoSignature();
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700717 }
Ian Rogersd91d6d62013-09-25 20:26:14 -0700718 return Signature(this, *proto_id);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700719}
720
Ian Rogersef7d42f2014-01-06 12:55:46 -0800721int32_t DexFile::GetLineNumFromPC(mirror::ArtMethod* method, uint32_t rel_pc) const {
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -0700722 // For native method, lineno should be -2 to indicate it is native. Note that
723 // "line number == -2" is how libcore tells from StackTraceElement.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700724 if (method->GetCodeItemOffset() == 0) {
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -0700725 return -2;
726 }
727
TDYa127c8dc1012012-04-19 07:03:33 -0700728 const CodeItem* code_item = GetCodeItem(method->GetCodeItemOffset());
Elliott Hughescaf76542012-06-28 16:08:22 -0700729 DCHECK(code_item != NULL) << PrettyMethod(method) << " " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700730
731 // A method with no line number info should return -1
732 LineNumFromPcContext context(rel_pc, -1);
TDYa127c8dc1012012-04-19 07:03:33 -0700733 DecodeDebugInfo(code_item, method->IsStatic(), method->GetDexMethodIndex(), LineNumForPcCb,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800734 NULL, &context);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700735 return context.line_num_;
736}
737
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700738int32_t DexFile::FindTryItem(const CodeItem &code_item, uint32_t address) {
Ian Rogers0571d352011-11-03 19:51:38 -0700739 // Note: Signed type is important for max and min.
740 int32_t min = 0;
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700741 int32_t max = code_item.tries_size_ - 1;
Ian Rogers0571d352011-11-03 19:51:38 -0700742
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700743 while (min <= max) {
744 int32_t mid = min + ((max - min) / 2);
745
746 const art::DexFile::TryItem* ti = GetTryItems(code_item, mid);
747 uint32_t start = ti->start_addr_;
748 uint32_t end = start + ti->insn_count_;
749
Ian Rogers0571d352011-11-03 19:51:38 -0700750 if (address < start) {
751 max = mid - 1;
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700752 } else if (address >= end) {
753 min = mid + 1;
754 } else { // We have a winner!
755 return mid;
Ian Rogers0571d352011-11-03 19:51:38 -0700756 }
757 }
758 // No match.
759 return -1;
760}
761
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700762int32_t DexFile::FindCatchHandlerOffset(const CodeItem &code_item, uint32_t address) {
763 int32_t try_item = FindTryItem(code_item, address);
764 if (try_item == -1) {
765 return -1;
766 } else {
767 return DexFile::GetTryItems(code_item, try_item)->handler_off_;
768 }
769}
770
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800771void DexFile::DecodeDebugInfo0(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes2435a572012-02-17 16:07:41 -0800772 DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb,
Ian Rogers13735952014-10-08 12:43:28 -0700773 void* context, const uint8_t* stream, LocalInfo* local_in_reg) const {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700774 uint32_t line = DecodeUnsignedLeb128(&stream);
775 uint32_t parameters_size = DecodeUnsignedLeb128(&stream);
776 uint16_t arg_reg = code_item->registers_size_ - code_item->ins_size_;
777 uint32_t address = 0;
Elliott Hughes30646832011-10-13 16:59:46 -0700778 bool need_locals = (local_cb != NULL);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700779
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800780 if (!is_static) {
Elliott Hughes30646832011-10-13 16:59:46 -0700781 if (need_locals) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800782 const char* descriptor = GetMethodDeclaringClassDescriptor(GetMethodId(method_idx));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700783 local_in_reg[arg_reg].name_ = "this";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800784 local_in_reg[arg_reg].descriptor_ = descriptor;
Elliott Hughes392b1242011-11-30 13:55:50 -0800785 local_in_reg[arg_reg].signature_ = NULL;
Elliott Hughes30646832011-10-13 16:59:46 -0700786 local_in_reg[arg_reg].start_address_ = 0;
787 local_in_reg[arg_reg].is_live_ = true;
788 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700789 arg_reg++;
790 }
791
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800792 DexFileParameterIterator it(*this, GetMethodPrototype(GetMethodId(method_idx)));
Ian Rogers0571d352011-11-03 19:51:38 -0700793 for (uint32_t i = 0; i < parameters_size && it.HasNext(); ++i, it.Next()) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700794 if (arg_reg >= code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700795 LOG(ERROR) << "invalid stream - arg reg >= reg size (" << arg_reg
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800796 << " >= " << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700797 return;
798 }
Elliott Hughes392b1242011-11-30 13:55:50 -0800799 uint32_t id = DecodeUnsignedLeb128P1(&stream);
Ian Rogers0571d352011-11-03 19:51:38 -0700800 const char* descriptor = it.GetDescriptor();
Elliott Hughes392b1242011-11-30 13:55:50 -0800801 if (need_locals && id != kDexNoIndex) {
Ian Rogers0571d352011-11-03 19:51:38 -0700802 const char* name = StringDataByIdx(id);
Elliott Hughes30646832011-10-13 16:59:46 -0700803 local_in_reg[arg_reg].name_ = name;
804 local_in_reg[arg_reg].descriptor_ = descriptor;
Elliott Hughes392b1242011-11-30 13:55:50 -0800805 local_in_reg[arg_reg].signature_ = NULL;
Elliott Hughes30646832011-10-13 16:59:46 -0700806 local_in_reg[arg_reg].start_address_ = address;
807 local_in_reg[arg_reg].is_live_ = true;
808 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700809 switch (*descriptor) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700810 case 'D':
811 case 'J':
812 arg_reg += 2;
813 break;
814 default:
815 arg_reg += 1;
816 break;
817 }
818 }
819
Ian Rogers0571d352011-11-03 19:51:38 -0700820 if (it.HasNext()) {
Brian Carlstromf79fccb2014-02-20 08:55:10 -0800821 LOG(ERROR) << "invalid stream - problem with parameter iterator in " << GetLocation()
822 << " for method " << PrettyMethod(method_idx, *this);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700823 return;
824 }
825
826 for (;;) {
827 uint8_t opcode = *stream++;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700828 uint16_t reg;
Jeff Haob7cefc72013-11-14 14:51:09 -0800829 uint32_t name_idx;
830 uint32_t descriptor_idx;
831 uint32_t signature_idx = 0;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700832
Shih-wei Liao195487c2011-08-20 13:29:04 -0700833 switch (opcode) {
834 case DBG_END_SEQUENCE:
835 return;
836
837 case DBG_ADVANCE_PC:
838 address += DecodeUnsignedLeb128(&stream);
839 break;
840
841 case DBG_ADVANCE_LINE:
Shih-wei Liao8a05d272011-10-15 18:45:43 -0700842 line += DecodeSignedLeb128(&stream);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700843 break;
844
845 case DBG_START_LOCAL:
846 case DBG_START_LOCAL_EXTENDED:
847 reg = DecodeUnsignedLeb128(&stream);
848 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700849 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800850 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700851 return;
852 }
853
jeffhaof8728872011-10-28 19:11:13 -0700854 name_idx = DecodeUnsignedLeb128P1(&stream);
855 descriptor_idx = DecodeUnsignedLeb128P1(&stream);
856 if (opcode == DBG_START_LOCAL_EXTENDED) {
857 signature_idx = DecodeUnsignedLeb128P1(&stream);
858 }
859
Shih-wei Liao195487c2011-08-20 13:29:04 -0700860 // Emit what was previously there, if anything
Elliott Hughes30646832011-10-13 16:59:46 -0700861 if (need_locals) {
Elliott Hughes2435a572012-02-17 16:07:41 -0800862 InvokeLocalCbIfLive(context, reg, address, local_in_reg, local_cb);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700863
Ian Rogers0571d352011-11-03 19:51:38 -0700864 local_in_reg[reg].name_ = StringDataByIdx(name_idx);
865 local_in_reg[reg].descriptor_ = StringByTypeIdx(descriptor_idx);
Elliott Hughes30646832011-10-13 16:59:46 -0700866 if (opcode == DBG_START_LOCAL_EXTENDED) {
Ian Rogers0571d352011-11-03 19:51:38 -0700867 local_in_reg[reg].signature_ = StringDataByIdx(signature_idx);
Elliott Hughes30646832011-10-13 16:59:46 -0700868 }
869 local_in_reg[reg].start_address_ = address;
870 local_in_reg[reg].is_live_ = true;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700871 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700872 break;
873
874 case DBG_END_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) {
Elliott Hughes2435a572012-02-17 16:07:41 -0800883 InvokeLocalCbIfLive(context, reg, address, local_in_reg, local_cb);
Elliott Hughes30646832011-10-13 16:59:46 -0700884 local_in_reg[reg].is_live_ = false;
885 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700886 break;
887
888 case DBG_RESTART_LOCAL:
889 reg = DecodeUnsignedLeb128(&stream);
890 if (reg > code_item->registers_size_) {
jeffhaof8728872011-10-28 19:11:13 -0700891 LOG(ERROR) << "invalid stream - reg > reg size (" << reg << " > "
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800892 << code_item->registers_size_ << ") in " << GetLocation();
Shih-wei Liao195487c2011-08-20 13:29:04 -0700893 return;
894 }
895
Elliott Hughes30646832011-10-13 16:59:46 -0700896 if (need_locals) {
897 if (local_in_reg[reg].name_ == NULL || local_in_reg[reg].descriptor_ == NULL) {
Brian Carlstrom2aab9472011-12-12 15:21:43 -0800898 LOG(ERROR) << "invalid stream - no name or descriptor in " << GetLocation();
Elliott Hughes30646832011-10-13 16:59:46 -0700899 return;
900 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700901
Elliott Hughes30646832011-10-13 16:59:46 -0700902 // If the register is live, the "restart" is superfluous,
903 // and we don't want to mess with the existing start address.
904 if (!local_in_reg[reg].is_live_) {
905 local_in_reg[reg].start_address_ = address;
906 local_in_reg[reg].is_live_ = true;
907 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700908 }
909 break;
910
911 case DBG_SET_PROLOGUE_END:
912 case DBG_SET_EPILOGUE_BEGIN:
913 case DBG_SET_FILE:
914 break;
915
Shih-wei Liao8e1b4ff2011-10-15 15:43:51 -0700916 default: {
917 int adjopcode = opcode - DBG_FIRST_SPECIAL;
918
Shih-wei Liao195487c2011-08-20 13:29:04 -0700919 address += adjopcode / DBG_LINE_RANGE;
920 line += DBG_LINE_BASE + (adjopcode % DBG_LINE_RANGE);
921
Elliott Hughes2435a572012-02-17 16:07:41 -0800922 if (position_cb != NULL) {
923 if (position_cb(context, address, line)) {
Shih-wei Liao195487c2011-08-20 13:29:04 -0700924 // early exit
925 return;
926 }
927 }
928 break;
Shih-wei Liao8e1b4ff2011-10-15 15:43:51 -0700929 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700930 }
931 }
932}
933
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800934void DexFile::DecodeDebugInfo(const CodeItem* code_item, bool is_static, uint32_t method_idx,
Elliott Hughes2435a572012-02-17 16:07:41 -0800935 DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb,
936 void* context) const {
Sebastien Hertzcb19ebf2014-03-11 15:26:35 +0100937 DCHECK(code_item != nullptr);
Ian Rogers13735952014-10-08 12:43:28 -0700938 const uint8_t* stream = GetDebugInfoStream(code_item);
Ian Rogers700a4022014-05-19 16:49:03 -0700939 std::unique_ptr<LocalInfo[]> local_in_reg(local_cb != NULL ?
Brian Carlstrome0948e12013-08-29 09:36:15 -0700940 new LocalInfo[code_item->registers_size_] :
941 NULL);
Ian Rogers0571d352011-11-03 19:51:38 -0700942 if (stream != NULL) {
Elliott Hughesee0fa762012-03-26 17:12:41 -0700943 DecodeDebugInfo0(code_item, is_static, method_idx, position_cb, local_cb, context, stream, &local_in_reg[0]);
Ian Rogers0571d352011-11-03 19:51:38 -0700944 }
945 for (int reg = 0; reg < code_item->registers_size_; reg++) {
Elliott Hughesee0fa762012-03-26 17:12:41 -0700946 InvokeLocalCbIfLive(context, reg, code_item->insns_size_in_code_units_, &local_in_reg[0], local_cb);
Ian Rogers0571d352011-11-03 19:51:38 -0700947 }
948}
949
Elliott Hughes2435a572012-02-17 16:07:41 -0800950bool DexFile::LineNumForPcCb(void* raw_context, uint32_t address, uint32_t line_num) {
951 LineNumFromPcContext* context = reinterpret_cast<LineNumFromPcContext*>(raw_context);
Ian Rogers0571d352011-11-03 19:51:38 -0700952
953 // We know that this callback will be called in
954 // ascending address order, so keep going until we find
955 // a match or we've just gone past it.
956 if (address > context->address_) {
957 // The line number from the previous positions callback
958 // wil be the final result.
959 return true;
960 } else {
961 context->line_num_ = line_num;
962 return address == context->address_;
963 }
964}
965
Andreas Gampe833a4852014-05-21 18:46:59 -0700966bool DexFile::IsMultiDexLocation(const char* location) {
967 return strrchr(location, kMultiDexSeparator) != nullptr;
968}
969
Calin Juravle4e1d5792014-07-15 23:56:47 +0100970std::string DexFile::GetMultiDexClassesDexName(size_t number, const char* dex_location) {
971 if (number == 0) {
972 return dex_location;
973 } else {
974 return StringPrintf("%s" kMultiDexSeparatorString "classes%zu.dex", dex_location, number + 1);
975 }
976}
977
978std::string DexFile::GetDexCanonicalLocation(const char* dex_location) {
979 CHECK_NE(dex_location, static_cast<const char*>(nullptr));
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100980 std::string base_location = GetBaseLocation(dex_location);
981 const char* suffix = dex_location + base_location.size();
982 DCHECK(suffix[0] == 0 || suffix[0] == kMultiDexSeparator);
983 UniqueCPtr<const char[]> path(realpath(base_location.c_str(), nullptr));
984 if (path != nullptr && path.get() != base_location) {
985 return std::string(path.get()) + suffix;
986 } else if (suffix[0] == 0) {
987 return base_location;
Calin Juravle4e1d5792014-07-15 23:56:47 +0100988 } else {
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100989 return dex_location;
Calin Juravle4e1d5792014-07-15 23:56:47 +0100990 }
Calin Juravle4e1d5792014-07-15 23:56:47 +0100991}
992
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800993std::ostream& operator<<(std::ostream& os, const DexFile& dex_file) {
994 os << StringPrintf("[DexFile: %s dex-checksum=%08x location-checksum=%08x %p-%p]",
995 dex_file.GetLocation().c_str(),
996 dex_file.GetHeader().checksum_, dex_file.GetLocationChecksum(),
997 dex_file.Begin(), dex_file.Begin() + dex_file.Size());
998 return os;
999}
Calin Juravle4e1d5792014-07-15 23:56:47 +01001000
Ian Rogersd91d6d62013-09-25 20:26:14 -07001001std::string Signature::ToString() const {
1002 if (dex_file_ == nullptr) {
1003 CHECK(proto_id_ == nullptr);
1004 return "<no signature>";
1005 }
1006 const DexFile::TypeList* params = dex_file_->GetProtoParameters(*proto_id_);
1007 std::string result;
1008 if (params == nullptr) {
1009 result += "()";
1010 } else {
1011 result += "(";
1012 for (uint32_t i = 0; i < params->Size(); ++i) {
1013 result += dex_file_->StringByTypeIdx(params->GetTypeItem(i).type_idx_);
1014 }
1015 result += ")";
1016 }
1017 result += dex_file_->StringByTypeIdx(proto_id_->return_type_idx_);
1018 return result;
1019}
1020
Vladimir Markod9cffea2013-11-25 15:08:02 +00001021bool Signature::operator==(const StringPiece& rhs) const {
1022 if (dex_file_ == nullptr) {
1023 return false;
1024 }
1025 StringPiece tail(rhs);
1026 if (!tail.starts_with("(")) {
1027 return false; // Invalid signature
1028 }
1029 tail.remove_prefix(1); // "(";
1030 const DexFile::TypeList* params = dex_file_->GetProtoParameters(*proto_id_);
1031 if (params != nullptr) {
1032 for (uint32_t i = 0; i < params->Size(); ++i) {
1033 StringPiece param(dex_file_->StringByTypeIdx(params->GetTypeItem(i).type_idx_));
1034 if (!tail.starts_with(param)) {
1035 return false;
1036 }
1037 tail.remove_prefix(param.length());
1038 }
1039 }
1040 if (!tail.starts_with(")")) {
1041 return false;
1042 }
1043 tail.remove_prefix(1); // ")";
1044 return tail == dex_file_->StringByTypeIdx(proto_id_->return_type_idx_);
1045}
1046
Ian Rogersd91d6d62013-09-25 20:26:14 -07001047std::ostream& operator<<(std::ostream& os, const Signature& sig) {
1048 return os << sig.ToString();
1049}
1050
Ian Rogers0571d352011-11-03 19:51:38 -07001051// Decodes the header section from the class data bytes.
1052void ClassDataItemIterator::ReadClassDataHeader() {
1053 CHECK(ptr_pos_ != NULL);
1054 header_.static_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_);
1055 header_.instance_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_);
1056 header_.direct_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_);
1057 header_.virtual_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_);
1058}
1059
1060void ClassDataItemIterator::ReadClassDataField() {
1061 field_.field_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_);
1062 field_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_);
Brian Carlstrom68adbe42012-05-11 17:18:08 -07001063 if (last_idx_ != 0 && field_.field_idx_delta_ == 0) {
Andreas Gampe4fdbba02014-06-19 20:24:22 -07001064 LOG(WARNING) << "Duplicate field in " << dex_file_.GetLocation();
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -07001065 }
Ian Rogers0571d352011-11-03 19:51:38 -07001066}
1067
1068void ClassDataItemIterator::ReadClassDataMethod() {
1069 method_.method_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_);
1070 method_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_);
1071 method_.code_off_ = DecodeUnsignedLeb128(&ptr_pos_);
Brian Carlstrom68adbe42012-05-11 17:18:08 -07001072 if (last_idx_ != 0 && method_.method_idx_delta_ == 0) {
Andreas Gampe4fdbba02014-06-19 20:24:22 -07001073 LOG(WARNING) << "Duplicate method in " << dex_file_.GetLocation();
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -07001074 }
Ian Rogers0571d352011-11-03 19:51:38 -07001075}
1076
1077// Read a signed integer. "zwidth" is the zero-based byte count.
Ian Rogers13735952014-10-08 12:43:28 -07001078static int32_t ReadSignedInt(const uint8_t* ptr, int zwidth) {
Ian Rogers0571d352011-11-03 19:51:38 -07001079 int32_t val = 0;
1080 for (int i = zwidth; i >= 0; --i) {
1081 val = ((uint32_t)val >> 8) | (((int32_t)*ptr++) << 24);
1082 }
1083 val >>= (3 - zwidth) * 8;
1084 return val;
1085}
1086
1087// Read an unsigned integer. "zwidth" is the zero-based byte count,
1088// "fill_on_right" indicates which side we want to zero-fill from.
Ian Rogers13735952014-10-08 12:43:28 -07001089static uint32_t ReadUnsignedInt(const uint8_t* ptr, int zwidth, bool fill_on_right) {
Ian Rogers0571d352011-11-03 19:51:38 -07001090 uint32_t val = 0;
1091 if (!fill_on_right) {
1092 for (int i = zwidth; i >= 0; --i) {
1093 val = (val >> 8) | (((uint32_t)*ptr++) << 24);
1094 }
1095 val >>= (3 - zwidth) * 8;
1096 } else {
1097 for (int i = zwidth; i >= 0; --i) {
1098 val = (val >> 8) | (((uint32_t)*ptr++) << 24);
1099 }
1100 }
1101 return val;
1102}
1103
1104// Read a signed long. "zwidth" is the zero-based byte count.
Ian Rogers13735952014-10-08 12:43:28 -07001105static int64_t ReadSignedLong(const uint8_t* ptr, int zwidth) {
Ian Rogers0571d352011-11-03 19:51:38 -07001106 int64_t val = 0;
1107 for (int i = zwidth; i >= 0; --i) {
1108 val = ((uint64_t)val >> 8) | (((int64_t)*ptr++) << 56);
1109 }
1110 val >>= (7 - zwidth) * 8;
1111 return val;
1112}
1113
1114// Read an unsigned long. "zwidth" is the zero-based byte count,
1115// "fill_on_right" indicates which side we want to zero-fill from.
Ian Rogers13735952014-10-08 12:43:28 -07001116static uint64_t ReadUnsignedLong(const uint8_t* ptr, int zwidth, bool fill_on_right) {
Ian Rogers0571d352011-11-03 19:51:38 -07001117 uint64_t val = 0;
1118 if (!fill_on_right) {
1119 for (int i = zwidth; i >= 0; --i) {
1120 val = (val >> 8) | (((uint64_t)*ptr++) << 56);
1121 }
1122 val >>= (7 - zwidth) * 8;
1123 } else {
1124 for (int i = zwidth; i >= 0; --i) {
1125 val = (val >> 8) | (((uint64_t)*ptr++) << 56);
1126 }
1127 }
1128 return val;
1129}
1130
1131EncodedStaticFieldValueIterator::EncodedStaticFieldValueIterator(const DexFile& dex_file,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001132 Handle<mirror::DexCache>* dex_cache,
1133 Handle<mirror::ClassLoader>* class_loader,
Ian Rogersca190662012-06-26 15:45:57 -07001134 ClassLinker* linker,
1135 const DexFile::ClassDef& class_def)
Brian Carlstrom88f36542012-10-16 23:24:21 -07001136 : dex_file_(dex_file), dex_cache_(dex_cache), class_loader_(class_loader), linker_(linker),
1137 array_size_(), pos_(-1), type_(kByte) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001138 DCHECK(dex_cache != nullptr);
1139 DCHECK(class_loader != nullptr);
Ian Rogers0571d352011-11-03 19:51:38 -07001140 ptr_ = dex_file.GetEncodedStaticFieldValuesArray(class_def);
1141 if (ptr_ == NULL) {
1142 array_size_ = 0;
1143 } else {
1144 array_size_ = DecodeUnsignedLeb128(&ptr_);
1145 }
1146 if (array_size_ > 0) {
1147 Next();
1148 }
1149}
1150
1151void EncodedStaticFieldValueIterator::Next() {
1152 pos_++;
1153 if (pos_ >= array_size_) {
1154 return;
1155 }
Ian Rogers13735952014-10-08 12:43:28 -07001156 uint8_t value_type = *ptr_++;
1157 uint8_t value_arg = value_type >> kEncodedValueArgShift;
Ian Rogers0571d352011-11-03 19:51:38 -07001158 size_t width = value_arg + 1; // assume and correct later
Brian Carlstrom88f36542012-10-16 23:24:21 -07001159 type_ = static_cast<ValueType>(value_type & kEncodedValueTypeMask);
Ian Rogers0571d352011-11-03 19:51:38 -07001160 switch (type_) {
1161 case kBoolean:
1162 jval_.i = (value_arg != 0) ? 1 : 0;
1163 width = 0;
1164 break;
1165 case kByte:
1166 jval_.i = ReadSignedInt(ptr_, value_arg);
Andreas Gampeab1eb0d2015-02-13 19:23:55 -08001167 CHECK(IsInt<8>(jval_.i));
Ian Rogers0571d352011-11-03 19:51:38 -07001168 break;
1169 case kShort:
1170 jval_.i = ReadSignedInt(ptr_, value_arg);
Andreas Gampeab1eb0d2015-02-13 19:23:55 -08001171 CHECK(IsInt<16>(jval_.i));
Ian Rogers0571d352011-11-03 19:51:38 -07001172 break;
1173 case kChar:
1174 jval_.i = ReadUnsignedInt(ptr_, value_arg, false);
Andreas Gampeab1eb0d2015-02-13 19:23:55 -08001175 CHECK(IsUint<16>(jval_.i));
Ian Rogers0571d352011-11-03 19:51:38 -07001176 break;
1177 case kInt:
1178 jval_.i = ReadSignedInt(ptr_, value_arg);
1179 break;
1180 case kLong:
1181 jval_.j = ReadSignedLong(ptr_, value_arg);
1182 break;
1183 case kFloat:
1184 jval_.i = ReadUnsignedInt(ptr_, value_arg, true);
1185 break;
1186 case kDouble:
1187 jval_.j = ReadUnsignedLong(ptr_, value_arg, true);
1188 break;
1189 case kString:
1190 case kType:
Ian Rogers0571d352011-11-03 19:51:38 -07001191 jval_.i = ReadUnsignedInt(ptr_, value_arg, false);
1192 break;
1193 case kField:
Brian Carlstrom88f36542012-10-16 23:24:21 -07001194 case kMethod:
1195 case kEnum:
Ian Rogers0571d352011-11-03 19:51:38 -07001196 case kArray:
1197 case kAnnotation:
1198 UNIMPLEMENTED(FATAL) << ": type " << type_;
Ian Rogers2c4257b2014-10-24 14:20:06 -07001199 UNREACHABLE();
Ian Rogers0571d352011-11-03 19:51:38 -07001200 case kNull:
1201 jval_.l = NULL;
1202 width = 0;
1203 break;
1204 default:
1205 LOG(FATAL) << "Unreached";
Ian Rogers2c4257b2014-10-24 14:20:06 -07001206 UNREACHABLE();
Ian Rogers0571d352011-11-03 19:51:38 -07001207 }
1208 ptr_ += width;
1209}
1210
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001211template<bool kTransactionActive>
Hiroshi Yamauchi67ef46a2014-08-21 15:59:43 -07001212void EncodedStaticFieldValueIterator::ReadValueToField(Handle<mirror::ArtField> field) const {
Ian Rogers0571d352011-11-03 19:51:38 -07001213 switch (type_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001214 case kBoolean: field->SetBoolean<kTransactionActive>(field->GetDeclaringClass(), jval_.z); break;
1215 case kByte: field->SetByte<kTransactionActive>(field->GetDeclaringClass(), jval_.b); break;
1216 case kShort: field->SetShort<kTransactionActive>(field->GetDeclaringClass(), jval_.s); break;
1217 case kChar: field->SetChar<kTransactionActive>(field->GetDeclaringClass(), jval_.c); break;
1218 case kInt: field->SetInt<kTransactionActive>(field->GetDeclaringClass(), jval_.i); break;
1219 case kLong: field->SetLong<kTransactionActive>(field->GetDeclaringClass(), jval_.j); break;
1220 case kFloat: field->SetFloat<kTransactionActive>(field->GetDeclaringClass(), jval_.f); break;
1221 case kDouble: field->SetDouble<kTransactionActive>(field->GetDeclaringClass(), jval_.d); break;
1222 case kNull: field->SetObject<kTransactionActive>(field->GetDeclaringClass(), NULL); break;
Ian Rogers0571d352011-11-03 19:51:38 -07001223 case kString: {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001224 CHECK(!kMovingFields);
1225 mirror::String* resolved = linker_->ResolveString(dex_file_, jval_.i, *dex_cache_);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001226 field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved);
Ian Rogers0571d352011-11-03 19:51:38 -07001227 break;
1228 }
Brian Carlstrom88f36542012-10-16 23:24:21 -07001229 case kType: {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001230 CHECK(!kMovingFields);
1231 mirror::Class* resolved = linker_->ResolveType(dex_file_, jval_.i, *dex_cache_,
1232 *class_loader_);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001233 field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved);
Brian Carlstrom88f36542012-10-16 23:24:21 -07001234 break;
1235 }
Ian Rogers0571d352011-11-03 19:51:38 -07001236 default: UNIMPLEMENTED(FATAL) << ": type " << type_;
1237 }
1238}
Hiroshi Yamauchi67ef46a2014-08-21 15:59:43 -07001239template void EncodedStaticFieldValueIterator::ReadValueToField<true>(Handle<mirror::ArtField> field) const;
1240template void EncodedStaticFieldValueIterator::ReadValueToField<false>(Handle<mirror::ArtField> field) const;
Ian Rogers0571d352011-11-03 19:51:38 -07001241
1242CatchHandlerIterator::CatchHandlerIterator(const DexFile::CodeItem& code_item, uint32_t address) {
1243 handler_.address_ = -1;
1244 int32_t offset = -1;
1245
1246 // Short-circuit the overwhelmingly common cases.
1247 switch (code_item.tries_size_) {
1248 case 0:
1249 break;
1250 case 1: {
1251 const DexFile::TryItem* tries = DexFile::GetTryItems(code_item, 0);
1252 uint32_t start = tries->start_addr_;
1253 if (address >= start) {
1254 uint32_t end = start + tries->insn_count_;
1255 if (address < end) {
1256 offset = tries->handler_off_;
1257 }
1258 }
1259 break;
1260 }
1261 default:
Ian Rogersdbbc99d2013-04-18 16:51:54 -07001262 offset = DexFile::FindCatchHandlerOffset(code_item, address);
Ian Rogers0571d352011-11-03 19:51:38 -07001263 }
Logan Chien736df022012-04-27 16:25:57 +08001264 Init(code_item, offset);
1265}
1266
1267CatchHandlerIterator::CatchHandlerIterator(const DexFile::CodeItem& code_item,
1268 const DexFile::TryItem& try_item) {
1269 handler_.address_ = -1;
1270 Init(code_item, try_item.handler_off_);
1271}
1272
1273void CatchHandlerIterator::Init(const DexFile::CodeItem& code_item,
1274 int32_t offset) {
Ian Rogers0571d352011-11-03 19:51:38 -07001275 if (offset >= 0) {
Logan Chien736df022012-04-27 16:25:57 +08001276 Init(DexFile::GetCatchHandlerData(code_item, offset));
Ian Rogers0571d352011-11-03 19:51:38 -07001277 } else {
1278 // Not found, initialize as empty
1279 current_data_ = NULL;
1280 remaining_count_ = -1;
1281 catch_all_ = false;
1282 DCHECK(!HasNext());
1283 }
1284}
1285
Ian Rogers13735952014-10-08 12:43:28 -07001286void CatchHandlerIterator::Init(const uint8_t* handler_data) {
Ian Rogers0571d352011-11-03 19:51:38 -07001287 current_data_ = handler_data;
1288 remaining_count_ = DecodeSignedLeb128(&current_data_);
1289
1290 // If remaining_count_ is non-positive, then it is the negative of
1291 // the number of catch types, and the catches are followed by a
1292 // catch-all handler.
1293 if (remaining_count_ <= 0) {
1294 catch_all_ = true;
1295 remaining_count_ = -remaining_count_;
1296 } else {
1297 catch_all_ = false;
1298 }
1299 Next();
1300}
1301
1302void CatchHandlerIterator::Next() {
1303 if (remaining_count_ > 0) {
1304 handler_.type_idx_ = DecodeUnsignedLeb128(&current_data_);
1305 handler_.address_ = DecodeUnsignedLeb128(&current_data_);
1306 remaining_count_--;
1307 return;
1308 }
1309
1310 if (catch_all_) {
1311 handler_.type_idx_ = DexFile::kDexNoIndex16;
1312 handler_.address_ = DecodeUnsignedLeb128(&current_data_);
1313 catch_all_ = false;
1314 return;
1315 }
1316
1317 // no more handler
1318 remaining_count_ = -1;
1319}
1320
Carl Shapiro1fb86202011-06-27 17:43:13 -07001321} // namespace art