Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 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 Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "dex_file.h" |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 18 | |
| 19 | #include <fcntl.h> |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 20 | #include <limits.h> |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 21 | #include <stdio.h> |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 22 | #include <stdlib.h> |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 23 | #include <string.h> |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 24 | #include <sys/file.h> |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 25 | #include <sys/stat.h> |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 26 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 27 | #include <memory> |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 28 | #include <sstream> |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 29 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 30 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 31 | #include "art_method-inl.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame^] | 32 | #include "base/enums.h" |
Vladimir Marko | 5096e66 | 2015-12-08 19:25:49 +0000 | [diff] [blame] | 33 | #include "base/file_magic.h" |
Andreas Gampe | 2a5c468 | 2015-08-14 08:22:54 -0700 | [diff] [blame] | 34 | #include "base/hash_map.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 35 | #include "base/logging.h" |
Vladimir Marko | 637ee0b | 2015-09-04 12:47:41 +0100 | [diff] [blame] | 36 | #include "base/stl_util.h" |
Elliott Hughes | e222ee0 | 2012-12-13 14:41:43 -0800 | [diff] [blame] | 37 | #include "base/stringprintf.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 38 | #include "base/systrace.h" |
Andreas Gampe | 43e10b0 | 2016-07-15 17:17:34 -0700 | [diff] [blame] | 39 | #include "base/unix_file/fd_file.h" |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 40 | #include "class_linker-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 41 | #include "dex_file-inl.h" |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 42 | #include "dex_file_verifier.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 43 | #include "globals.h" |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 44 | #include "handle_scope-inl.h" |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 45 | #include "leb128.h" |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 46 | #include "mirror/field.h" |
| 47 | #include "mirror/method.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 48 | #include "mirror/string.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 49 | #include "os.h" |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 50 | #include "reflection.h" |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 51 | #include "safe_map.h" |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 52 | #include "thread.h" |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 53 | #include "type_lookup_table.h" |
Ian Rogers | a672490 | 2013-09-23 09:23:37 -0700 | [diff] [blame] | 54 | #include "utf-inl.h" |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 55 | #include "utils.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 56 | #include "well_known_classes.h" |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 57 | #include "zip_archive.h" |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 58 | |
| 59 | namespace art { |
| 60 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 61 | const uint8_t DexFile::kDexMagic[] = { 'd', 'e', 'x', '\n' }; |
Alex Light | c496181 | 2016-03-23 10:20:41 -0700 | [diff] [blame] | 62 | const uint8_t DexFile::kDexMagicVersions[DexFile::kNumDexVersions][DexFile::kDexVersionLen] = { |
| 63 | {'0', '3', '5', '\0'}, |
| 64 | // Dex version 036 skipped because of an old dalvik bug on some versions of android where dex |
| 65 | // files with that version number would erroneously be accepted and run. |
| 66 | {'0', '3', '7', '\0'} |
| 67 | }; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 68 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 69 | bool DexFile::GetChecksum(const char* filename, uint32_t* checksum, std::string* error_msg) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 70 | CHECK(checksum != nullptr); |
Brian Carlstrom | 7c3d13a | 2013-09-04 17:15:11 -0700 | [diff] [blame] | 71 | uint32_t magic; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 72 | |
| 73 | // Strip ":...", which is the location |
| 74 | const char* zip_entry_name = kClassesDex; |
| 75 | const char* file_part = filename; |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 76 | std::string file_part_storage; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 77 | |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 78 | if (DexFile::IsMultiDexLocation(filename)) { |
| 79 | file_part_storage = GetBaseLocation(filename); |
| 80 | file_part = file_part_storage.c_str(); |
| 81 | zip_entry_name = filename + file_part_storage.size() + 1; |
| 82 | DCHECK_EQ(zip_entry_name[-1], kMultiDexSeparator); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Andreas Gampe | 43e10b0 | 2016-07-15 17:17:34 -0700 | [diff] [blame] | 85 | File fd = OpenAndReadMagic(file_part, &magic, error_msg); |
| 86 | if (fd.Fd() == -1) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 87 | DCHECK(!error_msg->empty()); |
Brian Carlstrom | 7c3d13a | 2013-09-04 17:15:11 -0700 | [diff] [blame] | 88 | return false; |
| 89 | } |
| 90 | if (IsZipMagic(magic)) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 91 | std::unique_ptr<ZipArchive> zip_archive( |
Andreas Gampe | 43e10b0 | 2016-07-15 17:17:34 -0700 | [diff] [blame] | 92 | ZipArchive::OpenFromFd(fd.Release(), filename, error_msg)); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 93 | if (zip_archive.get() == nullptr) { |
Andreas Gampe | 0b3ed3d | 2015-03-04 15:38:51 -0800 | [diff] [blame] | 94 | *error_msg = StringPrintf("Failed to open zip archive '%s' (error msg: %s)", file_part, |
| 95 | error_msg->c_str()); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 96 | return false; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 97 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 98 | std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(zip_entry_name, error_msg)); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 99 | if (zip_entry.get() == nullptr) { |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 100 | *error_msg = StringPrintf("Zip archive '%s' doesn't contain %s (error msg: %s)", file_part, |
| 101 | zip_entry_name, error_msg->c_str()); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 102 | return false; |
| 103 | } |
Brian Carlstrom | 7c3d13a | 2013-09-04 17:15:11 -0700 | [diff] [blame] | 104 | *checksum = zip_entry->GetCrc32(); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 105 | return true; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 106 | } |
Brian Carlstrom | 7c3d13a | 2013-09-04 17:15:11 -0700 | [diff] [blame] | 107 | if (IsDexMagic(magic)) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 108 | std::unique_ptr<const DexFile> dex_file( |
Andreas Gampe | 43e10b0 | 2016-07-15 17:17:34 -0700 | [diff] [blame] | 109 | DexFile::OpenFile(fd.Release(), filename, false, false, error_msg)); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 110 | if (dex_file.get() == nullptr) { |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 111 | return false; |
| 112 | } |
Brian Carlstrom | 7c3d13a | 2013-09-04 17:15:11 -0700 | [diff] [blame] | 113 | *checksum = dex_file->GetHeader().checksum_; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 114 | return true; |
| 115 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 116 | *error_msg = StringPrintf("Expected valid zip or dex file: '%s'", filename); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 117 | return false; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 120 | bool DexFile::Open(const char* filename, |
| 121 | const char* location, |
| 122 | bool verify_checksum, |
| 123 | std::string* error_msg, |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 124 | std::vector<std::unique_ptr<const DexFile>>* dex_files) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 125 | ScopedTrace trace(std::string("Open dex file ") + location); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 126 | DCHECK(dex_files != nullptr) << "DexFile::Open: out-param is nullptr"; |
Brian Carlstrom | 7c3d13a | 2013-09-04 17:15:11 -0700 | [diff] [blame] | 127 | uint32_t magic; |
Andreas Gampe | 43e10b0 | 2016-07-15 17:17:34 -0700 | [diff] [blame] | 128 | File fd = OpenAndReadMagic(filename, &magic, error_msg); |
| 129 | if (fd.Fd() == -1) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 130 | DCHECK(!error_msg->empty()); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 131 | return false; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 132 | } |
Brian Carlstrom | 7c3d13a | 2013-09-04 17:15:11 -0700 | [diff] [blame] | 133 | if (IsZipMagic(magic)) { |
Andreas Gampe | 43e10b0 | 2016-07-15 17:17:34 -0700 | [diff] [blame] | 134 | return DexFile::OpenZip(fd.Release(), location, verify_checksum, error_msg, dex_files); |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 135 | } |
Brian Carlstrom | 7c3d13a | 2013-09-04 17:15:11 -0700 | [diff] [blame] | 136 | if (IsDexMagic(magic)) { |
Andreas Gampe | 43e10b0 | 2016-07-15 17:17:34 -0700 | [diff] [blame] | 137 | std::unique_ptr<const DexFile> dex_file(DexFile::OpenFile(fd.Release(), |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 138 | location, |
| 139 | /* verify */ true, |
| 140 | verify_checksum, |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 141 | error_msg)); |
| 142 | if (dex_file.get() != nullptr) { |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 143 | dex_files->push_back(std::move(dex_file)); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 144 | return true; |
| 145 | } else { |
| 146 | return false; |
| 147 | } |
Brian Carlstrom | 7c3d13a | 2013-09-04 17:15:11 -0700 | [diff] [blame] | 148 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 149 | *error_msg = StringPrintf("Expected valid zip or dex file: '%s'", filename); |
Alexander Ivchenko | bacce5c | 2014-06-26 16:32:11 +0400 | [diff] [blame] | 150 | return false; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Andreas Gampe | 0cba004 | 2015-04-29 20:47:16 -0700 | [diff] [blame] | 153 | static bool ContainsClassesDex(int fd, const char* filename) { |
| 154 | std::string error_msg; |
| 155 | std::unique_ptr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(fd, filename, &error_msg)); |
| 156 | if (zip_archive.get() == nullptr) { |
| 157 | return false; |
| 158 | } |
| 159 | std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(DexFile::kClassesDex, &error_msg)); |
| 160 | return (zip_entry.get() != nullptr); |
| 161 | } |
| 162 | |
| 163 | bool DexFile::MaybeDex(const char* filename) { |
| 164 | uint32_t magic; |
| 165 | std::string error_msg; |
Andreas Gampe | 43e10b0 | 2016-07-15 17:17:34 -0700 | [diff] [blame] | 166 | File fd = OpenAndReadMagic(filename, &magic, &error_msg); |
| 167 | if (fd.Fd() == -1) { |
Andreas Gampe | 0cba004 | 2015-04-29 20:47:16 -0700 | [diff] [blame] | 168 | return false; |
| 169 | } |
| 170 | if (IsZipMagic(magic)) { |
Andreas Gampe | 43e10b0 | 2016-07-15 17:17:34 -0700 | [diff] [blame] | 171 | return ContainsClassesDex(fd.Release(), filename); |
Andreas Gampe | 0cba004 | 2015-04-29 20:47:16 -0700 | [diff] [blame] | 172 | } else if (IsDexMagic(magic)) { |
| 173 | return true; |
| 174 | } |
| 175 | return false; |
| 176 | } |
| 177 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 178 | int DexFile::GetPermissions() const { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 179 | if (mem_map_.get() == nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 180 | return 0; |
| 181 | } else { |
| 182 | return mem_map_->GetProtect(); |
| 183 | } |
| 184 | } |
| 185 | |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 186 | bool DexFile::IsReadOnly() const { |
| 187 | return GetPermissions() == PROT_READ; |
| 188 | } |
| 189 | |
Brian Carlstrom | e0948e1 | 2013-08-29 09:36:15 -0700 | [diff] [blame] | 190 | bool DexFile::EnableWrite() const { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 191 | CHECK(IsReadOnly()); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 192 | if (mem_map_.get() == nullptr) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 193 | return false; |
| 194 | } else { |
Brian Carlstrom | e0948e1 | 2013-08-29 09:36:15 -0700 | [diff] [blame] | 195 | return mem_map_->Protect(PROT_READ | PROT_WRITE); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
Brian Carlstrom | e0948e1 | 2013-08-29 09:36:15 -0700 | [diff] [blame] | 199 | bool DexFile::DisableWrite() const { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 200 | CHECK(!IsReadOnly()); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 201 | if (mem_map_.get() == nullptr) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 202 | return false; |
| 203 | } else { |
Brian Carlstrom | e0948e1 | 2013-08-29 09:36:15 -0700 | [diff] [blame] | 204 | return mem_map_->Protect(PROT_READ); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 208 | std::unique_ptr<const DexFile> DexFile::Open(const uint8_t* base, size_t size, |
| 209 | const std::string& location, |
| 210 | uint32_t location_checksum, |
| 211 | const OatDexFile* oat_dex_file, |
| 212 | bool verify, |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 213 | bool verify_checksum, |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 214 | std::string* error_msg) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 215 | ScopedTrace trace(std::string("Open dex file from RAM ") + location); |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 216 | std::unique_ptr<const DexFile> dex_file = OpenMemory(base, |
| 217 | size, |
| 218 | location, |
| 219 | location_checksum, |
| 220 | nullptr, |
| 221 | oat_dex_file, |
| 222 | error_msg); |
| 223 | if (verify && !DexFileVerifier::Verify(dex_file.get(), |
| 224 | dex_file->Begin(), |
| 225 | dex_file->Size(), |
| 226 | location.c_str(), |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 227 | verify_checksum, |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 228 | error_msg)) { |
| 229 | return nullptr; |
| 230 | } |
| 231 | |
| 232 | return dex_file; |
| 233 | } |
| 234 | |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 235 | std::unique_ptr<const DexFile> DexFile::OpenFile(int fd, |
| 236 | const char* location, |
| 237 | bool verify, |
| 238 | bool verify_checksum, |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 239 | std::string* error_msg) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 240 | ScopedTrace trace(std::string("Open dex file ") + location); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 241 | CHECK(location != nullptr); |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 242 | std::unique_ptr<MemMap> map; |
Vladimir Marko | fd99576 | 2013-11-06 16:36:36 +0000 | [diff] [blame] | 243 | { |
Andreas Gampe | 43e10b0 | 2016-07-15 17:17:34 -0700 | [diff] [blame] | 244 | File delayed_close(fd, /* check_usage */ false); |
Vladimir Marko | fd99576 | 2013-11-06 16:36:36 +0000 | [diff] [blame] | 245 | struct stat sbuf; |
| 246 | memset(&sbuf, 0, sizeof(sbuf)); |
| 247 | if (fstat(fd, &sbuf) == -1) { |
Brian Carlstrom | 4fa0bcd | 2013-12-10 11:24:21 -0800 | [diff] [blame] | 248 | *error_msg = StringPrintf("DexFile: fstat '%s' failed: %s", location, strerror(errno)); |
Vladimir Marko | fd99576 | 2013-11-06 16:36:36 +0000 | [diff] [blame] | 249 | return nullptr; |
| 250 | } |
| 251 | if (S_ISDIR(sbuf.st_mode)) { |
| 252 | *error_msg = StringPrintf("Attempt to mmap directory '%s'", location); |
| 253 | return nullptr; |
| 254 | } |
| 255 | size_t length = sbuf.st_size; |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 256 | map.reset(MemMap::MapFile(length, |
| 257 | PROT_READ, |
| 258 | MAP_PRIVATE, |
| 259 | fd, |
| 260 | 0, |
| 261 | /*low_4gb*/false, |
| 262 | location, |
| 263 | error_msg)); |
Vladimir Marko | fd99576 | 2013-11-06 16:36:36 +0000 | [diff] [blame] | 264 | if (map.get() == nullptr) { |
| 265 | DCHECK(!error_msg->empty()); |
| 266 | return nullptr; |
| 267 | } |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 268 | } |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 269 | |
| 270 | if (map->Size() < sizeof(DexFile::Header)) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 271 | *error_msg = StringPrintf( |
Brian Carlstrom | 4fa0bcd | 2013-12-10 11:24:21 -0800 | [diff] [blame] | 272 | "DexFile: failed to open dex file '%s' that is too short to have a header", location); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 273 | return nullptr; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | const Header* dex_header = reinterpret_cast<const Header*>(map->Begin()); |
| 277 | |
Andreas Gampe | 928f72b | 2014-09-09 19:53:48 -0700 | [diff] [blame] | 278 | std::unique_ptr<const DexFile> dex_file(OpenMemory(location, dex_header->checksum_, map.release(), |
| 279 | error_msg)); |
| 280 | if (dex_file.get() == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 281 | *error_msg = StringPrintf("Failed to open dex file '%s' from memory: %s", location, |
| 282 | error_msg->c_str()); |
| 283 | return nullptr; |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame] | 284 | } |
jeffhao | 54c1ceb | 2012-02-01 11:45:32 -0800 | [diff] [blame] | 285 | |
Andreas Gampe | 928f72b | 2014-09-09 19:53:48 -0700 | [diff] [blame] | 286 | if (verify && !DexFileVerifier::Verify(dex_file.get(), dex_file->Begin(), dex_file->Size(), |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 287 | location, |
| 288 | verify_checksum, |
| 289 | error_msg)) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 290 | return nullptr; |
jeffhao | 54c1ceb | 2012-02-01 11:45:32 -0800 | [diff] [blame] | 291 | } |
| 292 | |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 293 | return dex_file; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 296 | const char* DexFile::kClassesDex = "classes.dex"; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 297 | |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 298 | bool DexFile::OpenZip(int fd, |
| 299 | const std::string& location, |
| 300 | bool verify_checksum, |
| 301 | std::string* error_msg, |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 302 | std::vector<std::unique_ptr<const DexFile>>* dex_files) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 303 | ScopedTrace trace("Dex file open Zip " + std::string(location)); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 304 | DCHECK(dex_files != nullptr) << "DexFile::OpenZip: out-param is nullptr"; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 305 | std::unique_ptr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(fd, location.c_str(), error_msg)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 306 | if (zip_archive.get() == nullptr) { |
| 307 | DCHECK(!error_msg->empty()); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 308 | return false; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 309 | } |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 310 | return DexFile::OpenFromZip(*zip_archive, location, verify_checksum, error_msg, dex_files); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 311 | } |
| 312 | |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 313 | std::unique_ptr<const DexFile> DexFile::OpenMemory(const std::string& location, |
| 314 | uint32_t location_checksum, |
| 315 | MemMap* mem_map, |
| 316 | std::string* error_msg) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 317 | return OpenMemory(mem_map->Begin(), |
| 318 | mem_map->Size(), |
| 319 | location, |
| 320 | location_checksum, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 321 | mem_map, |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 322 | nullptr, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 323 | error_msg); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 324 | } |
| 325 | |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 326 | std::unique_ptr<const DexFile> DexFile::Open(const ZipArchive& zip_archive, |
| 327 | const char* entry_name, |
| 328 | const std::string& location, |
| 329 | bool verify_checksum, |
| 330 | std::string* error_msg, |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 331 | ZipOpenErrorCode* error_code) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 332 | ScopedTrace trace("Dex file open from Zip Archive " + std::string(location)); |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 333 | CHECK(!location.empty()); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 334 | std::unique_ptr<ZipEntry> zip_entry(zip_archive.Find(entry_name, error_msg)); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 335 | if (zip_entry.get() == nullptr) { |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 336 | *error_code = ZipOpenErrorCode::kEntryNotFound; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 337 | return nullptr; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 338 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 339 | std::unique_ptr<MemMap> map(zip_entry->ExtractToMemMap(location.c_str(), entry_name, error_msg)); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 340 | if (map.get() == nullptr) { |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 341 | *error_msg = StringPrintf("Failed to extract '%s' from '%s': %s", entry_name, location.c_str(), |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 342 | error_msg->c_str()); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 343 | *error_code = ZipOpenErrorCode::kExtractToMemoryError; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 344 | return nullptr; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 345 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 346 | std::unique_ptr<const DexFile> dex_file(OpenMemory(location, zip_entry->GetCrc32(), map.release(), |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 347 | error_msg)); |
| 348 | if (dex_file.get() == nullptr) { |
| 349 | *error_msg = StringPrintf("Failed to open dex file '%s' from memory: %s", location.c_str(), |
| 350 | error_msg->c_str()); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 351 | *error_code = ZipOpenErrorCode::kDexFileError; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 352 | return nullptr; |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame] | 353 | } |
Brian Carlstrom | e0948e1 | 2013-08-29 09:36:15 -0700 | [diff] [blame] | 354 | if (!dex_file->DisableWrite()) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 355 | *error_msg = StringPrintf("Failed to make dex file '%s' read only", location.c_str()); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 356 | *error_code = ZipOpenErrorCode::kMakeReadOnlyError; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 357 | return nullptr; |
Brian Carlstrom | e0948e1 | 2013-08-29 09:36:15 -0700 | [diff] [blame] | 358 | } |
| 359 | CHECK(dex_file->IsReadOnly()) << location; |
Brian Carlstrom | d6cec90 | 2014-05-25 16:08:51 -0700 | [diff] [blame] | 360 | if (!DexFileVerifier::Verify(dex_file.get(), dex_file->Begin(), dex_file->Size(), |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 361 | location.c_str(), |
| 362 | verify_checksum, |
| 363 | error_msg)) { |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 364 | *error_code = ZipOpenErrorCode::kVerifyError; |
Brian Carlstrom | d6cec90 | 2014-05-25 16:08:51 -0700 | [diff] [blame] | 365 | return nullptr; |
| 366 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 367 | *error_code = ZipOpenErrorCode::kNoError; |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 368 | return dex_file; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Andreas Gampe | 90e3404 | 2015-04-27 20:01:52 -0700 | [diff] [blame] | 371 | // Technically we do not have a limitation with respect to the number of dex files that can be in a |
| 372 | // multidex APK. However, it's bad practice, as each dex file requires its own tables for symbols |
| 373 | // (types, classes, methods, ...) and dex caches. So warn the user that we open a zip with what |
| 374 | // seems an excessive number. |
| 375 | static constexpr size_t kWarnOnManyDexFilesThreshold = 100; |
| 376 | |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 377 | bool DexFile::OpenFromZip(const ZipArchive& zip_archive, |
| 378 | const std::string& location, |
| 379 | bool verify_checksum, |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 380 | std::string* error_msg, |
| 381 | std::vector<std::unique_ptr<const DexFile>>* dex_files) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 382 | ScopedTrace trace("Dex file open from Zip " + std::string(location)); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 383 | DCHECK(dex_files != nullptr) << "DexFile::OpenFromZip: out-param is nullptr"; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 384 | ZipOpenErrorCode error_code; |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 385 | std::unique_ptr<const DexFile> dex_file( |
| 386 | Open(zip_archive, kClassesDex, location, verify_checksum, error_msg, &error_code)); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 387 | if (dex_file.get() == nullptr) { |
| 388 | return false; |
| 389 | } else { |
| 390 | // Had at least classes.dex. |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 391 | dex_files->push_back(std::move(dex_file)); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 392 | |
| 393 | // Now try some more. |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 394 | |
| 395 | // We could try to avoid std::string allocations by working on a char array directly. As we |
| 396 | // do not expect a lot of iterations, this seems too involved and brittle. |
| 397 | |
Andreas Gampe | 90e3404 | 2015-04-27 20:01:52 -0700 | [diff] [blame] | 398 | for (size_t i = 1; ; ++i) { |
| 399 | std::string name = GetMultiDexClassesDexName(i); |
| 400 | std::string fake_location = GetMultiDexLocation(i, location.c_str()); |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 401 | std::unique_ptr<const DexFile> next_dex_file( |
| 402 | Open(zip_archive, name.c_str(), fake_location, verify_checksum, error_msg, &error_code)); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 403 | if (next_dex_file.get() == nullptr) { |
| 404 | if (error_code != ZipOpenErrorCode::kEntryNotFound) { |
| 405 | LOG(WARNING) << error_msg; |
| 406 | } |
| 407 | break; |
| 408 | } else { |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 409 | dex_files->push_back(std::move(next_dex_file)); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Andreas Gampe | 90e3404 | 2015-04-27 20:01:52 -0700 | [diff] [blame] | 412 | if (i == kWarnOnManyDexFilesThreshold) { |
| 413 | LOG(WARNING) << location << " has in excess of " << kWarnOnManyDexFilesThreshold |
| 414 | << " dex files. Please consider coalescing and shrinking the number to " |
| 415 | " avoid runtime overhead."; |
| 416 | } |
| 417 | |
| 418 | if (i == std::numeric_limits<size_t>::max()) { |
| 419 | LOG(ERROR) << "Overflow in number of dex files!"; |
| 420 | break; |
| 421 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | return true; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 429 | std::unique_ptr<const DexFile> DexFile::OpenMemory(const uint8_t* base, |
| 430 | size_t size, |
| 431 | const std::string& location, |
| 432 | uint32_t location_checksum, |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 433 | MemMap* mem_map, |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 434 | const OatDexFile* oat_dex_file, |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 435 | std::string* error_msg) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 436 | CHECK_ALIGNED(base, 4); // various dex file structures must be word aligned |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 437 | std::unique_ptr<DexFile> dex_file( |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 438 | new DexFile(base, size, location, location_checksum, mem_map, oat_dex_file)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 439 | if (!dex_file->Init(error_msg)) { |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 440 | dex_file.reset(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 441 | } |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 442 | return std::unique_ptr<const DexFile>(dex_file.release()); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 445 | DexFile::DexFile(const uint8_t* base, size_t size, |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 446 | const std::string& location, |
| 447 | uint32_t location_checksum, |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 448 | MemMap* mem_map, |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 449 | const OatDexFile* oat_dex_file) |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 450 | : begin_(base), |
| 451 | size_(size), |
| 452 | location_(location), |
| 453 | location_checksum_(location_checksum), |
| 454 | mem_map_(mem_map), |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 455 | header_(reinterpret_cast<const Header*>(base)), |
| 456 | string_ids_(reinterpret_cast<const StringId*>(base + header_->string_ids_off_)), |
| 457 | type_ids_(reinterpret_cast<const TypeId*>(base + header_->type_ids_off_)), |
| 458 | field_ids_(reinterpret_cast<const FieldId*>(base + header_->field_ids_off_)), |
| 459 | method_ids_(reinterpret_cast<const MethodId*>(base + header_->method_ids_off_)), |
| 460 | proto_ids_(reinterpret_cast<const ProtoId*>(base + header_->proto_ids_off_)), |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 461 | class_defs_(reinterpret_cast<const ClassDef*>(base + header_->class_defs_off_)), |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 462 | oat_dex_file_(oat_dex_file) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 463 | CHECK(begin_ != nullptr) << GetLocation(); |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 464 | CHECK_GT(size_, 0U) << GetLocation(); |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 465 | const uint8_t* lookup_data = (oat_dex_file != nullptr) |
| 466 | ? oat_dex_file->GetLookupTableData() |
| 467 | : nullptr; |
| 468 | if (lookup_data != nullptr) { |
| 469 | if (lookup_data + TypeLookupTable::RawDataLength(*this) > oat_dex_file->GetOatFile()->End()) { |
| 470 | LOG(WARNING) << "found truncated lookup table in " << GetLocation(); |
| 471 | } else { |
| 472 | lookup_table_.reset(TypeLookupTable::Open(lookup_data, *this)); |
| 473 | } |
| 474 | } |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 475 | } |
| 476 | |
Jesse Wilson | 6bf1915 | 2011-09-29 13:12:33 -0400 | [diff] [blame] | 477 | DexFile::~DexFile() { |
Elliott Hughes | 8cef0b8 | 2011-10-11 19:24:00 -0700 | [diff] [blame] | 478 | // We don't call DeleteGlobalRef on dex_object_ because we're only called by DestroyJavaVM, and |
| 479 | // that's only called after DetachCurrentThread, which means there's no JNIEnv. We could |
| 480 | // re-attach, but cleaning up these global references is not obviously useful. It's not as if |
| 481 | // the global reference table is otherwise empty! |
Jesse Wilson | 6bf1915 | 2011-09-29 13:12:33 -0400 | [diff] [blame] | 482 | } |
| 483 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 484 | bool DexFile::Init(std::string* error_msg) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 485 | if (!CheckMagicAndVersion(error_msg)) { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 486 | return false; |
| 487 | } |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 488 | return true; |
| 489 | } |
| 490 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 491 | bool DexFile::CheckMagicAndVersion(std::string* error_msg) const { |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 492 | if (!IsMagicValid(header_->magic_)) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 493 | std::ostringstream oss; |
| 494 | oss << "Unrecognized magic number in " << GetLocation() << ":" |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 495 | << " " << header_->magic_[0] |
| 496 | << " " << header_->magic_[1] |
| 497 | << " " << header_->magic_[2] |
| 498 | << " " << header_->magic_[3]; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 499 | *error_msg = oss.str(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 500 | return false; |
| 501 | } |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 502 | if (!IsVersionValid(header_->magic_)) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 503 | std::ostringstream oss; |
| 504 | oss << "Unrecognized version number in " << GetLocation() << ":" |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 505 | << " " << header_->magic_[4] |
| 506 | << " " << header_->magic_[5] |
| 507 | << " " << header_->magic_[6] |
| 508 | << " " << header_->magic_[7]; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 509 | *error_msg = oss.str(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 510 | return false; |
| 511 | } |
| 512 | return true; |
| 513 | } |
| 514 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 515 | bool DexFile::IsMagicValid(const uint8_t* magic) { |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 516 | return (memcmp(magic, kDexMagic, sizeof(kDexMagic)) == 0); |
| 517 | } |
| 518 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 519 | bool DexFile::IsVersionValid(const uint8_t* magic) { |
| 520 | const uint8_t* version = &magic[sizeof(kDexMagic)]; |
Alex Light | c496181 | 2016-03-23 10:20:41 -0700 | [diff] [blame] | 521 | for (uint32_t i = 0; i < kNumDexVersions; i++) { |
| 522 | if (memcmp(version, kDexMagicVersions[i], kDexVersionLen) == 0) { |
| 523 | return true; |
| 524 | } |
| 525 | } |
| 526 | return false; |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 527 | } |
| 528 | |
Andreas Gampe | 76ed99d | 2016-03-28 18:31:29 -0700 | [diff] [blame] | 529 | uint32_t DexFile::Header::GetVersion() const { |
| 530 | const char* version = reinterpret_cast<const char*>(&magic_[sizeof(kDexMagic)]); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 531 | return atoi(version); |
| 532 | } |
| 533 | |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 534 | const DexFile::ClassDef* DexFile::FindClassDef(const char* descriptor, size_t hash) const { |
| 535 | DCHECK_EQ(ComputeModifiedUtf8Hash(descriptor), hash); |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 536 | if (LIKELY(lookup_table_ != nullptr)) { |
| 537 | const uint32_t class_def_idx = lookup_table_->Lookup(descriptor, hash); |
| 538 | return (class_def_idx != DexFile::kDexNoIndex) ? &GetClassDef(class_def_idx) : nullptr; |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 539 | } |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 540 | |
Roland Levillain | ab880f4 | 2016-05-12 16:24:36 +0100 | [diff] [blame] | 541 | // Fast path for rare no class defs case. |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 542 | const uint32_t num_class_defs = NumClassDefs(); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 543 | if (num_class_defs == 0) { |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 544 | return nullptr; |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 545 | } |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 546 | const TypeId* type_id = FindTypeId(descriptor); |
| 547 | if (type_id != nullptr) { |
| 548 | uint16_t type_idx = GetIndexForTypeId(*type_id); |
| 549 | for (size_t i = 0; i < num_class_defs; ++i) { |
| 550 | const ClassDef& class_def = GetClassDef(i); |
| 551 | if (class_def.class_idx_ == type_idx) { |
| 552 | return &class_def; |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 553 | } |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 554 | } |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 555 | } |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 556 | return nullptr; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 559 | const DexFile::ClassDef* DexFile::FindClassDef(uint16_t type_idx) const { |
| 560 | size_t num_class_defs = NumClassDefs(); |
| 561 | for (size_t i = 0; i < num_class_defs; ++i) { |
| 562 | const ClassDef& class_def = GetClassDef(i); |
| 563 | if (class_def.class_idx_ == type_idx) { |
| 564 | return &class_def; |
| 565 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 566 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 567 | return nullptr; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 568 | } |
| 569 | |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 570 | const DexFile::FieldId* DexFile::FindFieldId(const DexFile::TypeId& declaring_klass, |
Roland Levillain | ab880f4 | 2016-05-12 16:24:36 +0100 | [diff] [blame] | 571 | const DexFile::StringId& name, |
| 572 | const DexFile::TypeId& type) const { |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 573 | // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx |
| 574 | const uint16_t class_idx = GetIndexForTypeId(declaring_klass); |
| 575 | const uint32_t name_idx = GetIndexForStringId(name); |
| 576 | const uint16_t type_idx = GetIndexForTypeId(type); |
Ian Rogers | f8582c3 | 2013-05-29 16:33:03 -0700 | [diff] [blame] | 577 | int32_t lo = 0; |
| 578 | int32_t hi = NumFieldIds() - 1; |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 579 | while (hi >= lo) { |
Ian Rogers | f8582c3 | 2013-05-29 16:33:03 -0700 | [diff] [blame] | 580 | int32_t mid = (hi + lo) / 2; |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 581 | const DexFile::FieldId& field = GetFieldId(mid); |
| 582 | if (class_idx > field.class_idx_) { |
| 583 | lo = mid + 1; |
| 584 | } else if (class_idx < field.class_idx_) { |
| 585 | hi = mid - 1; |
| 586 | } else { |
| 587 | if (name_idx > field.name_idx_) { |
| 588 | lo = mid + 1; |
| 589 | } else if (name_idx < field.name_idx_) { |
| 590 | hi = mid - 1; |
| 591 | } else { |
| 592 | if (type_idx > field.type_idx_) { |
| 593 | lo = mid + 1; |
| 594 | } else if (type_idx < field.type_idx_) { |
| 595 | hi = mid - 1; |
| 596 | } else { |
| 597 | return &field; |
| 598 | } |
| 599 | } |
| 600 | } |
| 601 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 602 | return nullptr; |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | const DexFile::MethodId* DexFile::FindMethodId(const DexFile::TypeId& declaring_klass, |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 606 | const DexFile::StringId& name, |
| 607 | const DexFile::ProtoId& signature) const { |
| 608 | // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 609 | const uint16_t class_idx = GetIndexForTypeId(declaring_klass); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 610 | const uint32_t name_idx = GetIndexForStringId(name); |
| 611 | const uint16_t proto_idx = GetIndexForProtoId(signature); |
Ian Rogers | f8582c3 | 2013-05-29 16:33:03 -0700 | [diff] [blame] | 612 | int32_t lo = 0; |
| 613 | int32_t hi = NumMethodIds() - 1; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 614 | while (hi >= lo) { |
Ian Rogers | f8582c3 | 2013-05-29 16:33:03 -0700 | [diff] [blame] | 615 | int32_t mid = (hi + lo) / 2; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 616 | const DexFile::MethodId& method = GetMethodId(mid); |
| 617 | if (class_idx > method.class_idx_) { |
| 618 | lo = mid + 1; |
| 619 | } else if (class_idx < method.class_idx_) { |
| 620 | hi = mid - 1; |
| 621 | } else { |
| 622 | if (name_idx > method.name_idx_) { |
| 623 | lo = mid + 1; |
| 624 | } else if (name_idx < method.name_idx_) { |
| 625 | hi = mid - 1; |
| 626 | } else { |
| 627 | if (proto_idx > method.proto_idx_) { |
| 628 | lo = mid + 1; |
| 629 | } else if (proto_idx < method.proto_idx_) { |
| 630 | hi = mid - 1; |
| 631 | } else { |
| 632 | return &method; |
| 633 | } |
| 634 | } |
| 635 | } |
| 636 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 637 | return nullptr; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 638 | } |
| 639 | |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 640 | const DexFile::StringId* DexFile::FindStringId(const char* string) const { |
Ian Rogers | f8582c3 | 2013-05-29 16:33:03 -0700 | [diff] [blame] | 641 | int32_t lo = 0; |
| 642 | int32_t hi = NumStringIds() - 1; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 643 | while (hi >= lo) { |
Ian Rogers | f8582c3 | 2013-05-29 16:33:03 -0700 | [diff] [blame] | 644 | int32_t mid = (hi + lo) / 2; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 645 | const DexFile::StringId& str_id = GetStringId(mid); |
Ian Rogers | cf5077a | 2013-10-31 12:37:54 -0700 | [diff] [blame] | 646 | const char* str = GetStringData(str_id); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 647 | int compare = CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(string, str); |
| 648 | if (compare > 0) { |
| 649 | lo = mid + 1; |
| 650 | } else if (compare < 0) { |
| 651 | hi = mid - 1; |
| 652 | } else { |
| 653 | return &str_id; |
| 654 | } |
| 655 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 656 | return nullptr; |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 657 | } |
| 658 | |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 659 | const DexFile::TypeId* DexFile::FindTypeId(const char* string) const { |
| 660 | int32_t lo = 0; |
| 661 | int32_t hi = NumTypeIds() - 1; |
| 662 | while (hi >= lo) { |
| 663 | int32_t mid = (hi + lo) / 2; |
| 664 | const TypeId& type_id = GetTypeId(mid); |
| 665 | const DexFile::StringId& str_id = GetStringId(type_id.descriptor_idx_); |
| 666 | const char* str = GetStringData(str_id); |
| 667 | int compare = CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(string, str); |
| 668 | if (compare > 0) { |
| 669 | lo = mid + 1; |
| 670 | } else if (compare < 0) { |
| 671 | hi = mid - 1; |
| 672 | } else { |
| 673 | return &type_id; |
| 674 | } |
| 675 | } |
| 676 | return nullptr; |
| 677 | } |
| 678 | |
Vladimir Marko | a48aef4 | 2014-12-03 17:53:53 +0000 | [diff] [blame] | 679 | const DexFile::StringId* DexFile::FindStringId(const uint16_t* string, size_t length) const { |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 680 | int32_t lo = 0; |
| 681 | int32_t hi = NumStringIds() - 1; |
| 682 | while (hi >= lo) { |
| 683 | int32_t mid = (hi + lo) / 2; |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 684 | const DexFile::StringId& str_id = GetStringId(mid); |
Ian Rogers | cf5077a | 2013-10-31 12:37:54 -0700 | [diff] [blame] | 685 | const char* str = GetStringData(str_id); |
Vladimir Marko | a48aef4 | 2014-12-03 17:53:53 +0000 | [diff] [blame] | 686 | int compare = CompareModifiedUtf8ToUtf16AsCodePointValues(str, string, length); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 687 | if (compare > 0) { |
| 688 | lo = mid + 1; |
| 689 | } else if (compare < 0) { |
| 690 | hi = mid - 1; |
| 691 | } else { |
| 692 | return &str_id; |
| 693 | } |
| 694 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 695 | return nullptr; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | const DexFile::TypeId* DexFile::FindTypeId(uint32_t string_idx) const { |
Ian Rogers | f8582c3 | 2013-05-29 16:33:03 -0700 | [diff] [blame] | 699 | int32_t lo = 0; |
| 700 | int32_t hi = NumTypeIds() - 1; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 701 | while (hi >= lo) { |
Ian Rogers | f8582c3 | 2013-05-29 16:33:03 -0700 | [diff] [blame] | 702 | int32_t mid = (hi + lo) / 2; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 703 | const TypeId& type_id = GetTypeId(mid); |
| 704 | if (string_idx > type_id.descriptor_idx_) { |
| 705 | lo = mid + 1; |
| 706 | } else if (string_idx < type_id.descriptor_idx_) { |
| 707 | hi = mid - 1; |
| 708 | } else { |
| 709 | return &type_id; |
| 710 | } |
| 711 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 712 | return nullptr; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | const DexFile::ProtoId* DexFile::FindProtoId(uint16_t return_type_idx, |
Vladimir Marko | 5c96e6b | 2013-11-14 15:34:17 +0000 | [diff] [blame] | 716 | const uint16_t* signature_type_idxs, |
| 717 | uint32_t signature_length) const { |
Ian Rogers | f8582c3 | 2013-05-29 16:33:03 -0700 | [diff] [blame] | 718 | int32_t lo = 0; |
| 719 | int32_t hi = NumProtoIds() - 1; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 720 | while (hi >= lo) { |
Ian Rogers | f8582c3 | 2013-05-29 16:33:03 -0700 | [diff] [blame] | 721 | int32_t mid = (hi + lo) / 2; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 722 | const DexFile::ProtoId& proto = GetProtoId(mid); |
| 723 | int compare = return_type_idx - proto.return_type_idx_; |
| 724 | if (compare == 0) { |
| 725 | DexFileParameterIterator it(*this, proto); |
| 726 | size_t i = 0; |
Vladimir Marko | 5c96e6b | 2013-11-14 15:34:17 +0000 | [diff] [blame] | 727 | while (it.HasNext() && i < signature_length && compare == 0) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 728 | compare = signature_type_idxs[i] - it.GetTypeIdx(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 729 | it.Next(); |
| 730 | i++; |
| 731 | } |
| 732 | if (compare == 0) { |
| 733 | if (it.HasNext()) { |
| 734 | compare = -1; |
Vladimir Marko | 5c96e6b | 2013-11-14 15:34:17 +0000 | [diff] [blame] | 735 | } else if (i < signature_length) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 736 | compare = 1; |
| 737 | } |
| 738 | } |
| 739 | } |
| 740 | if (compare > 0) { |
| 741 | lo = mid + 1; |
| 742 | } else if (compare < 0) { |
| 743 | hi = mid - 1; |
| 744 | } else { |
| 745 | return &proto; |
| 746 | } |
| 747 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 748 | return nullptr; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 749 | } |
| 750 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 751 | void DexFile::CreateTypeLookupTable(uint8_t* storage) const { |
| 752 | lookup_table_.reset(TypeLookupTable::Create(*this, storage)); |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 753 | } |
| 754 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 755 | // Given a signature place the type ids into the given vector |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 756 | bool DexFile::CreateTypeList(const StringPiece& signature, uint16_t* return_type_idx, |
| 757 | std::vector<uint16_t>* param_type_idxs) const { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 758 | if (signature[0] != '(') { |
| 759 | return false; |
| 760 | } |
| 761 | size_t offset = 1; |
| 762 | size_t end = signature.size(); |
| 763 | bool process_return = false; |
| 764 | while (offset < end) { |
Vladimir Marko | e9c36b3 | 2013-11-21 15:49:16 +0000 | [diff] [blame] | 765 | size_t start_offset = offset; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 766 | char c = signature[offset]; |
| 767 | offset++; |
| 768 | if (c == ')') { |
| 769 | process_return = true; |
| 770 | continue; |
| 771 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 772 | while (c == '[') { // process array prefix |
| 773 | if (offset >= end) { // expect some descriptor following [ |
| 774 | return false; |
| 775 | } |
| 776 | c = signature[offset]; |
| 777 | offset++; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 778 | } |
| 779 | if (c == 'L') { // process type descriptors |
| 780 | do { |
| 781 | if (offset >= end) { // unexpected early termination of descriptor |
| 782 | return false; |
| 783 | } |
| 784 | c = signature[offset]; |
| 785 | offset++; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 786 | } while (c != ';'); |
| 787 | } |
Vladimir Marko | e9c36b3 | 2013-11-21 15:49:16 +0000 | [diff] [blame] | 788 | // TODO: avoid creating a std::string just to get a 0-terminated char array |
| 789 | std::string descriptor(signature.data() + start_offset, offset - start_offset); |
Mathieu Chartier | 9507fa2 | 2015-10-29 15:08:57 -0700 | [diff] [blame] | 790 | const DexFile::TypeId* type_id = FindTypeId(descriptor.c_str()); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 791 | if (type_id == nullptr) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 792 | return false; |
| 793 | } |
| 794 | uint16_t type_idx = GetIndexForTypeId(*type_id); |
| 795 | if (!process_return) { |
| 796 | param_type_idxs->push_back(type_idx); |
| 797 | } else { |
| 798 | *return_type_idx = type_idx; |
| 799 | return offset == end; // return true if the signature had reached a sensible end |
| 800 | } |
| 801 | } |
| 802 | return false; // failed to correctly parse return type |
| 803 | } |
| 804 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 805 | const Signature DexFile::CreateSignature(const StringPiece& signature) const { |
| 806 | uint16_t return_type_idx; |
| 807 | std::vector<uint16_t> param_type_indices; |
| 808 | bool success = CreateTypeList(signature, &return_type_idx, ¶m_type_indices); |
| 809 | if (!success) { |
| 810 | return Signature::NoSignature(); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 811 | } |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 812 | const ProtoId* proto_id = FindProtoId(return_type_idx, param_type_indices); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 813 | if (proto_id == nullptr) { |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 814 | return Signature::NoSignature(); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 815 | } |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 816 | return Signature(this, *proto_id); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 817 | } |
| 818 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 819 | int32_t DexFile::GetLineNumFromPC(ArtMethod* method, uint32_t rel_pc) const { |
Shih-wei Liao | ff0f9be | 2011-08-29 15:43:53 -0700 | [diff] [blame] | 820 | // For native method, lineno should be -2 to indicate it is native. Note that |
| 821 | // "line number == -2" is how libcore tells from StackTraceElement. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 822 | if (method->GetCodeItemOffset() == 0) { |
Shih-wei Liao | ff0f9be | 2011-08-29 15:43:53 -0700 | [diff] [blame] | 823 | return -2; |
| 824 | } |
| 825 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 826 | const CodeItem* code_item = GetCodeItem(method->GetCodeItemOffset()); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 827 | DCHECK(code_item != nullptr) << PrettyMethod(method) << " " << GetLocation(); |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 828 | |
| 829 | // A method with no line number info should return -1 |
| 830 | LineNumFromPcContext context(rel_pc, -1); |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 831 | DecodeDebugPositionInfo(code_item, LineNumForPcCb, &context); |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 832 | return context.line_num_; |
| 833 | } |
| 834 | |
Ian Rogers | dbbc99d | 2013-04-18 16:51:54 -0700 | [diff] [blame] | 835 | int32_t DexFile::FindTryItem(const CodeItem &code_item, uint32_t address) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 836 | // Note: Signed type is important for max and min. |
| 837 | int32_t min = 0; |
Ian Rogers | dbbc99d | 2013-04-18 16:51:54 -0700 | [diff] [blame] | 838 | int32_t max = code_item.tries_size_ - 1; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 839 | |
Ian Rogers | dbbc99d | 2013-04-18 16:51:54 -0700 | [diff] [blame] | 840 | while (min <= max) { |
| 841 | int32_t mid = min + ((max - min) / 2); |
| 842 | |
| 843 | const art::DexFile::TryItem* ti = GetTryItems(code_item, mid); |
| 844 | uint32_t start = ti->start_addr_; |
| 845 | uint32_t end = start + ti->insn_count_; |
| 846 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 847 | if (address < start) { |
| 848 | max = mid - 1; |
Ian Rogers | dbbc99d | 2013-04-18 16:51:54 -0700 | [diff] [blame] | 849 | } else if (address >= end) { |
| 850 | min = mid + 1; |
| 851 | } else { // We have a winner! |
| 852 | return mid; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 853 | } |
| 854 | } |
| 855 | // No match. |
| 856 | return -1; |
| 857 | } |
| 858 | |
Ian Rogers | dbbc99d | 2013-04-18 16:51:54 -0700 | [diff] [blame] | 859 | int32_t DexFile::FindCatchHandlerOffset(const CodeItem &code_item, uint32_t address) { |
| 860 | int32_t try_item = FindTryItem(code_item, address); |
| 861 | if (try_item == -1) { |
| 862 | return -1; |
| 863 | } else { |
| 864 | return DexFile::GetTryItems(code_item, try_item)->handler_off_; |
| 865 | } |
| 866 | } |
| 867 | |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 868 | bool DexFile::DecodeDebugLocalInfo(const CodeItem* code_item, bool is_static, uint32_t method_idx, |
| 869 | DexDebugNewLocalCb local_cb, void* context) const { |
| 870 | DCHECK(local_cb != nullptr); |
| 871 | if (code_item == nullptr) { |
| 872 | return false; |
| 873 | } |
| 874 | const uint8_t* stream = GetDebugInfoStream(code_item); |
| 875 | if (stream == nullptr) { |
| 876 | return false; |
| 877 | } |
| 878 | std::vector<LocalInfo> local_in_reg(code_item->registers_size_); |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 879 | |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 880 | uint16_t arg_reg = code_item->registers_size_ - code_item->ins_size_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 881 | if (!is_static) { |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 882 | const char* descriptor = GetMethodDeclaringClassDescriptor(GetMethodId(method_idx)); |
| 883 | local_in_reg[arg_reg].name_ = "this"; |
| 884 | local_in_reg[arg_reg].descriptor_ = descriptor; |
| 885 | local_in_reg[arg_reg].signature_ = nullptr; |
| 886 | local_in_reg[arg_reg].start_address_ = 0; |
| 887 | local_in_reg[arg_reg].reg_ = arg_reg; |
| 888 | local_in_reg[arg_reg].is_live_ = true; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 889 | arg_reg++; |
| 890 | } |
| 891 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 892 | DexFileParameterIterator it(*this, GetMethodPrototype(GetMethodId(method_idx))); |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 893 | DecodeUnsignedLeb128(&stream); // Line. |
| 894 | uint32_t parameters_size = DecodeUnsignedLeb128(&stream); |
| 895 | uint32_t i; |
| 896 | for (i = 0; i < parameters_size && it.HasNext(); ++i, it.Next()) { |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 897 | if (arg_reg >= code_item->registers_size_) { |
jeffhao | f872887 | 2011-10-28 19:11:13 -0700 | [diff] [blame] | 898 | LOG(ERROR) << "invalid stream - arg reg >= reg size (" << arg_reg |
Brian Carlstrom | 2aab947 | 2011-12-12 15:21:43 -0800 | [diff] [blame] | 899 | << " >= " << code_item->registers_size_ << ") in " << GetLocation(); |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 900 | return false; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 901 | } |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 902 | uint32_t name_idx = DecodeUnsignedLeb128P1(&stream); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 903 | const char* descriptor = it.GetDescriptor(); |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 904 | local_in_reg[arg_reg].name_ = StringDataByIdx(name_idx); |
| 905 | local_in_reg[arg_reg].descriptor_ = descriptor; |
| 906 | local_in_reg[arg_reg].signature_ = nullptr; |
| 907 | local_in_reg[arg_reg].start_address_ = 0; |
| 908 | local_in_reg[arg_reg].reg_ = arg_reg; |
| 909 | local_in_reg[arg_reg].is_live_ = true; |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 910 | switch (*descriptor) { |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 911 | case 'D': |
| 912 | case 'J': |
| 913 | arg_reg += 2; |
| 914 | break; |
| 915 | default: |
| 916 | arg_reg += 1; |
| 917 | break; |
| 918 | } |
| 919 | } |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 920 | if (i != parameters_size || it.HasNext()) { |
Brian Carlstrom | f79fccb | 2014-02-20 08:55:10 -0800 | [diff] [blame] | 921 | LOG(ERROR) << "invalid stream - problem with parameter iterator in " << GetLocation() |
| 922 | << " for method " << PrettyMethod(method_idx, *this); |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 923 | return false; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 924 | } |
| 925 | |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 926 | uint32_t address = 0; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 927 | for (;;) { |
| 928 | uint8_t opcode = *stream++; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 929 | switch (opcode) { |
| 930 | case DBG_END_SEQUENCE: |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 931 | // Emit all variables which are still alive at the end of the method. |
| 932 | for (uint16_t reg = 0; reg < code_item->registers_size_; reg++) { |
| 933 | if (local_in_reg[reg].is_live_) { |
| 934 | local_in_reg[reg].end_address_ = code_item->insns_size_in_code_units_; |
| 935 | local_cb(context, local_in_reg[reg]); |
| 936 | } |
| 937 | } |
| 938 | return true; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 939 | case DBG_ADVANCE_PC: |
| 940 | address += DecodeUnsignedLeb128(&stream); |
| 941 | break; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 942 | case DBG_ADVANCE_LINE: |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 943 | DecodeSignedLeb128(&stream); // Line. |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 944 | break; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 945 | case DBG_START_LOCAL: |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 946 | case DBG_START_LOCAL_EXTENDED: { |
| 947 | uint16_t reg = DecodeUnsignedLeb128(&stream); |
| 948 | if (reg >= code_item->registers_size_) { |
| 949 | LOG(ERROR) << "invalid stream - reg >= reg size (" << reg << " >= " |
Brian Carlstrom | 2aab947 | 2011-12-12 15:21:43 -0800 | [diff] [blame] | 950 | << code_item->registers_size_ << ") in " << GetLocation(); |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 951 | return false; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 952 | } |
| 953 | |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 954 | uint32_t name_idx = DecodeUnsignedLeb128P1(&stream); |
| 955 | uint32_t descriptor_idx = DecodeUnsignedLeb128P1(&stream); |
| 956 | uint32_t signature_idx = kDexNoIndex; |
jeffhao | f872887 | 2011-10-28 19:11:13 -0700 | [diff] [blame] | 957 | if (opcode == DBG_START_LOCAL_EXTENDED) { |
| 958 | signature_idx = DecodeUnsignedLeb128P1(&stream); |
| 959 | } |
| 960 | |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 961 | // Emit what was previously there, if anything |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 962 | if (local_in_reg[reg].is_live_) { |
| 963 | local_in_reg[reg].end_address_ = address; |
| 964 | local_cb(context, local_in_reg[reg]); |
| 965 | } |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 966 | |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 967 | local_in_reg[reg].name_ = StringDataByIdx(name_idx); |
| 968 | local_in_reg[reg].descriptor_ = StringByTypeIdx(descriptor_idx); |
| 969 | local_in_reg[reg].signature_ = StringDataByIdx(signature_idx); |
| 970 | local_in_reg[reg].start_address_ = address; |
| 971 | local_in_reg[reg].reg_ = reg; |
| 972 | local_in_reg[reg].is_live_ = true; |
| 973 | break; |
| 974 | } |
| 975 | case DBG_END_LOCAL: { |
| 976 | uint16_t reg = DecodeUnsignedLeb128(&stream); |
| 977 | if (reg >= code_item->registers_size_) { |
| 978 | LOG(ERROR) << "invalid stream - reg >= reg size (" << reg << " >= " |
| 979 | << code_item->registers_size_ << ") in " << GetLocation(); |
| 980 | return false; |
| 981 | } |
| 982 | if (!local_in_reg[reg].is_live_) { |
| 983 | LOG(ERROR) << "invalid stream - end without start in " << GetLocation(); |
| 984 | return false; |
| 985 | } |
| 986 | local_in_reg[reg].end_address_ = address; |
| 987 | local_cb(context, local_in_reg[reg]); |
| 988 | local_in_reg[reg].is_live_ = false; |
| 989 | break; |
| 990 | } |
| 991 | case DBG_RESTART_LOCAL: { |
| 992 | uint16_t reg = DecodeUnsignedLeb128(&stream); |
| 993 | if (reg >= code_item->registers_size_) { |
| 994 | LOG(ERROR) << "invalid stream - reg >= reg size (" << reg << " >= " |
| 995 | << code_item->registers_size_ << ") in " << GetLocation(); |
| 996 | return false; |
| 997 | } |
| 998 | // If the register is live, the "restart" is superfluous, |
| 999 | // and we don't want to mess with the existing start address. |
| 1000 | if (!local_in_reg[reg].is_live_) { |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 1001 | local_in_reg[reg].start_address_ = address; |
| 1002 | local_in_reg[reg].is_live_ = true; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 1003 | } |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 1004 | break; |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1005 | } |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 1006 | case DBG_SET_PROLOGUE_END: |
| 1007 | case DBG_SET_EPILOGUE_BEGIN: |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 1008 | break; |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1009 | case DBG_SET_FILE: |
| 1010 | DecodeUnsignedLeb128P1(&stream); // name. |
| 1011 | break; |
| 1012 | default: |
| 1013 | address += (opcode - DBG_FIRST_SPECIAL) / DBG_LINE_RANGE; |
| 1014 | break; |
| 1015 | } |
| 1016 | } |
| 1017 | } |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 1018 | |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1019 | bool DexFile::DecodeDebugPositionInfo(const CodeItem* code_item, DexDebugNewPositionCb position_cb, |
| 1020 | void* context) const { |
| 1021 | DCHECK(position_cb != nullptr); |
| 1022 | if (code_item == nullptr) { |
| 1023 | return false; |
| 1024 | } |
| 1025 | const uint8_t* stream = GetDebugInfoStream(code_item); |
| 1026 | if (stream == nullptr) { |
| 1027 | return false; |
| 1028 | } |
| 1029 | |
| 1030 | PositionInfo entry = PositionInfo(); |
| 1031 | entry.line_ = DecodeUnsignedLeb128(&stream); |
| 1032 | uint32_t parameters_size = DecodeUnsignedLeb128(&stream); |
| 1033 | for (uint32_t i = 0; i < parameters_size; ++i) { |
| 1034 | DecodeUnsignedLeb128P1(&stream); // Parameter name. |
| 1035 | } |
| 1036 | |
| 1037 | for (;;) { |
| 1038 | uint8_t opcode = *stream++; |
| 1039 | switch (opcode) { |
| 1040 | case DBG_END_SEQUENCE: |
| 1041 | return true; // end of stream. |
| 1042 | case DBG_ADVANCE_PC: |
| 1043 | entry.address_ += DecodeUnsignedLeb128(&stream); |
| 1044 | break; |
| 1045 | case DBG_ADVANCE_LINE: |
| 1046 | entry.line_ += DecodeSignedLeb128(&stream); |
| 1047 | break; |
| 1048 | case DBG_START_LOCAL: |
| 1049 | DecodeUnsignedLeb128(&stream); // reg. |
| 1050 | DecodeUnsignedLeb128P1(&stream); // name. |
| 1051 | DecodeUnsignedLeb128P1(&stream); // descriptor. |
| 1052 | break; |
| 1053 | case DBG_START_LOCAL_EXTENDED: |
| 1054 | DecodeUnsignedLeb128(&stream); // reg. |
| 1055 | DecodeUnsignedLeb128P1(&stream); // name. |
| 1056 | DecodeUnsignedLeb128P1(&stream); // descriptor. |
| 1057 | DecodeUnsignedLeb128P1(&stream); // signature. |
| 1058 | break; |
| 1059 | case DBG_END_LOCAL: |
| 1060 | case DBG_RESTART_LOCAL: |
| 1061 | DecodeUnsignedLeb128(&stream); // reg. |
| 1062 | break; |
| 1063 | case DBG_SET_PROLOGUE_END: |
| 1064 | entry.prologue_end_ = true; |
| 1065 | break; |
| 1066 | case DBG_SET_EPILOGUE_BEGIN: |
| 1067 | entry.epilogue_begin_ = true; |
| 1068 | break; |
| 1069 | case DBG_SET_FILE: { |
| 1070 | uint32_t name_idx = DecodeUnsignedLeb128P1(&stream); |
| 1071 | entry.source_file_ = StringDataByIdx(name_idx); |
| 1072 | break; |
| 1073 | } |
Shih-wei Liao | 8e1b4ff | 2011-10-15 15:43:51 -0700 | [diff] [blame] | 1074 | default: { |
| 1075 | int adjopcode = opcode - DBG_FIRST_SPECIAL; |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1076 | entry.address_ += adjopcode / DBG_LINE_RANGE; |
| 1077 | entry.line_ += DBG_LINE_BASE + (adjopcode % DBG_LINE_RANGE); |
| 1078 | if (position_cb(context, entry)) { |
| 1079 | return true; // early exit. |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 1080 | } |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1081 | entry.prologue_end_ = false; |
| 1082 | entry.epilogue_begin_ = false; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 1083 | break; |
Shih-wei Liao | 8e1b4ff | 2011-10-15 15:43:51 -0700 | [diff] [blame] | 1084 | } |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 1085 | } |
| 1086 | } |
| 1087 | } |
| 1088 | |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1089 | bool DexFile::LineNumForPcCb(void* raw_context, const PositionInfo& entry) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 1090 | LineNumFromPcContext* context = reinterpret_cast<LineNumFromPcContext*>(raw_context); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1091 | |
| 1092 | // We know that this callback will be called in |
| 1093 | // ascending address order, so keep going until we find |
| 1094 | // a match or we've just gone past it. |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1095 | if (entry.address_ > context->address_) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1096 | // The line number from the previous positions callback |
| 1097 | // wil be the final result. |
| 1098 | return true; |
| 1099 | } else { |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1100 | context->line_num_ = entry.line_; |
| 1101 | return entry.address_ == context->address_; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1102 | } |
| 1103 | } |
| 1104 | |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1105 | bool DexFile::IsMultiDexLocation(const char* location) { |
| 1106 | return strrchr(location, kMultiDexSeparator) != nullptr; |
| 1107 | } |
| 1108 | |
Andreas Gampe | 90e3404 | 2015-04-27 20:01:52 -0700 | [diff] [blame] | 1109 | std::string DexFile::GetMultiDexClassesDexName(size_t index) { |
| 1110 | if (index == 0) { |
| 1111 | return "classes.dex"; |
| 1112 | } else { |
| 1113 | return StringPrintf("classes%zu.dex", index + 1); |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | std::string DexFile::GetMultiDexLocation(size_t index, const char* dex_location) { |
| 1118 | if (index == 0) { |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 1119 | return dex_location; |
| 1120 | } else { |
Andreas Gampe | 90e3404 | 2015-04-27 20:01:52 -0700 | [diff] [blame] | 1121 | return StringPrintf("%s" kMultiDexSeparatorString "classes%zu.dex", dex_location, index + 1); |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | std::string DexFile::GetDexCanonicalLocation(const char* dex_location) { |
| 1126 | CHECK_NE(dex_location, static_cast<const char*>(nullptr)); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 1127 | std::string base_location = GetBaseLocation(dex_location); |
| 1128 | const char* suffix = dex_location + base_location.size(); |
| 1129 | DCHECK(suffix[0] == 0 || suffix[0] == kMultiDexSeparator); |
| 1130 | UniqueCPtr<const char[]> path(realpath(base_location.c_str(), nullptr)); |
| 1131 | if (path != nullptr && path.get() != base_location) { |
| 1132 | return std::string(path.get()) + suffix; |
| 1133 | } else if (suffix[0] == 0) { |
| 1134 | return base_location; |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 1135 | } else { |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 1136 | return dex_location; |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 1137 | } |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 1138 | } |
| 1139 | |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 1140 | // Read a signed integer. "zwidth" is the zero-based byte count. |
| 1141 | static int32_t ReadSignedInt(const uint8_t* ptr, int zwidth) { |
| 1142 | int32_t val = 0; |
| 1143 | for (int i = zwidth; i >= 0; --i) { |
| 1144 | val = ((uint32_t)val >> 8) | (((int32_t)*ptr++) << 24); |
| 1145 | } |
| 1146 | val >>= (3 - zwidth) * 8; |
| 1147 | return val; |
| 1148 | } |
| 1149 | |
| 1150 | // Read an unsigned integer. "zwidth" is the zero-based byte count, |
| 1151 | // "fill_on_right" indicates which side we want to zero-fill from. |
| 1152 | static uint32_t ReadUnsignedInt(const uint8_t* ptr, int zwidth, bool fill_on_right) { |
| 1153 | uint32_t val = 0; |
| 1154 | for (int i = zwidth; i >= 0; --i) { |
| 1155 | val = (val >> 8) | (((uint32_t)*ptr++) << 24); |
| 1156 | } |
| 1157 | if (!fill_on_right) { |
| 1158 | val >>= (3 - zwidth) * 8; |
| 1159 | } |
| 1160 | return val; |
| 1161 | } |
| 1162 | |
| 1163 | // Read a signed long. "zwidth" is the zero-based byte count. |
| 1164 | static int64_t ReadSignedLong(const uint8_t* ptr, int zwidth) { |
| 1165 | int64_t val = 0; |
| 1166 | for (int i = zwidth; i >= 0; --i) { |
| 1167 | val = ((uint64_t)val >> 8) | (((int64_t)*ptr++) << 56); |
| 1168 | } |
| 1169 | val >>= (7 - zwidth) * 8; |
| 1170 | return val; |
| 1171 | } |
| 1172 | |
| 1173 | // Read an unsigned long. "zwidth" is the zero-based byte count, |
| 1174 | // "fill_on_right" indicates which side we want to zero-fill from. |
| 1175 | static uint64_t ReadUnsignedLong(const uint8_t* ptr, int zwidth, bool fill_on_right) { |
| 1176 | uint64_t val = 0; |
| 1177 | for (int i = zwidth; i >= 0; --i) { |
| 1178 | val = (val >> 8) | (((uint64_t)*ptr++) << 56); |
| 1179 | } |
| 1180 | if (!fill_on_right) { |
| 1181 | val >>= (7 - zwidth) * 8; |
| 1182 | } |
| 1183 | return val; |
| 1184 | } |
| 1185 | |
Jeff Hao | 3d08086 | 2016-05-26 18:39:17 -0700 | [diff] [blame] | 1186 | // Checks that visibility is as expected. Includes special behavior for M and |
| 1187 | // before to allow runtime and build visibility when expecting runtime. |
| 1188 | static bool IsVisibilityCompatible(uint32_t actual, uint32_t expected) { |
| 1189 | if (expected == DexFile::kDexVisibilityRuntime) { |
| 1190 | int32_t sdk_version = Runtime::Current()->GetTargetSdkVersion(); |
| 1191 | if (sdk_version > 0 && sdk_version <= 23) { |
| 1192 | return actual == DexFile::kDexVisibilityRuntime || actual == DexFile::kDexVisibilityBuild; |
| 1193 | } |
| 1194 | } |
| 1195 | return actual == expected; |
| 1196 | } |
| 1197 | |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 1198 | const DexFile::AnnotationSetItem* DexFile::FindAnnotationSetForField(ArtField* field) const { |
| 1199 | mirror::Class* klass = field->GetDeclaringClass(); |
| 1200 | const AnnotationsDirectoryItem* annotations_dir = GetAnnotationsDirectory(*klass->GetClassDef()); |
| 1201 | if (annotations_dir == nullptr) { |
| 1202 | return nullptr; |
| 1203 | } |
| 1204 | const FieldAnnotationsItem* field_annotations = GetFieldAnnotations(annotations_dir); |
| 1205 | if (field_annotations == nullptr) { |
| 1206 | return nullptr; |
| 1207 | } |
| 1208 | uint32_t field_index = field->GetDexFieldIndex(); |
| 1209 | uint32_t field_count = annotations_dir->fields_size_; |
| 1210 | for (uint32_t i = 0; i < field_count; ++i) { |
| 1211 | if (field_annotations[i].field_idx_ == field_index) { |
| 1212 | return GetFieldAnnotationSetItem(field_annotations[i]); |
| 1213 | } |
| 1214 | } |
| 1215 | return nullptr; |
| 1216 | } |
| 1217 | |
| 1218 | mirror::Object* DexFile::GetAnnotationForField(ArtField* field, |
| 1219 | Handle<mirror::Class> annotation_class) const { |
| 1220 | const AnnotationSetItem* annotation_set = FindAnnotationSetForField(field); |
| 1221 | if (annotation_set == nullptr) { |
| 1222 | return nullptr; |
| 1223 | } |
| 1224 | StackHandleScope<1> hs(Thread::Current()); |
| 1225 | Handle<mirror::Class> field_class(hs.NewHandle(field->GetDeclaringClass())); |
| 1226 | return GetAnnotationObjectFromAnnotationSet( |
| 1227 | field_class, annotation_set, kDexVisibilityRuntime, annotation_class); |
| 1228 | } |
| 1229 | |
| 1230 | mirror::ObjectArray<mirror::Object>* DexFile::GetAnnotationsForField(ArtField* field) const { |
| 1231 | const AnnotationSetItem* annotation_set = FindAnnotationSetForField(field); |
| 1232 | StackHandleScope<1> hs(Thread::Current()); |
| 1233 | Handle<mirror::Class> field_class(hs.NewHandle(field->GetDeclaringClass())); |
| 1234 | return ProcessAnnotationSet(field_class, annotation_set, kDexVisibilityRuntime); |
| 1235 | } |
| 1236 | |
Jeff Hao | 2a5892f | 2015-08-31 15:00:40 -0700 | [diff] [blame] | 1237 | mirror::ObjectArray<mirror::String>* DexFile::GetSignatureAnnotationForField(ArtField* field) |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 1238 | const { |
| 1239 | const AnnotationSetItem* annotation_set = FindAnnotationSetForField(field); |
| 1240 | if (annotation_set == nullptr) { |
| 1241 | return nullptr; |
| 1242 | } |
| 1243 | StackHandleScope<1> hs(Thread::Current()); |
| 1244 | Handle<mirror::Class> field_class(hs.NewHandle(field->GetDeclaringClass())); |
| 1245 | return GetSignatureValue(field_class, annotation_set); |
| 1246 | } |
| 1247 | |
| 1248 | bool DexFile::IsFieldAnnotationPresent(ArtField* field, Handle<mirror::Class> annotation_class) |
| 1249 | const { |
| 1250 | const AnnotationSetItem* annotation_set = FindAnnotationSetForField(field); |
| 1251 | if (annotation_set == nullptr) { |
| 1252 | return false; |
| 1253 | } |
| 1254 | StackHandleScope<1> hs(Thread::Current()); |
| 1255 | Handle<mirror::Class> field_class(hs.NewHandle(field->GetDeclaringClass())); |
| 1256 | const AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet( |
| 1257 | field_class, annotation_set, kDexVisibilityRuntime, annotation_class); |
| 1258 | return annotation_item != nullptr; |
| 1259 | } |
| 1260 | |
| 1261 | const DexFile::AnnotationSetItem* DexFile::FindAnnotationSetForMethod(ArtMethod* method) const { |
| 1262 | mirror::Class* klass = method->GetDeclaringClass(); |
| 1263 | const AnnotationsDirectoryItem* annotations_dir = GetAnnotationsDirectory(*klass->GetClassDef()); |
| 1264 | if (annotations_dir == nullptr) { |
| 1265 | return nullptr; |
| 1266 | } |
| 1267 | const MethodAnnotationsItem* method_annotations = GetMethodAnnotations(annotations_dir); |
| 1268 | if (method_annotations == nullptr) { |
| 1269 | return nullptr; |
| 1270 | } |
| 1271 | uint32_t method_index = method->GetDexMethodIndex(); |
| 1272 | uint32_t method_count = annotations_dir->methods_size_; |
| 1273 | for (uint32_t i = 0; i < method_count; ++i) { |
| 1274 | if (method_annotations[i].method_idx_ == method_index) { |
| 1275 | return GetMethodAnnotationSetItem(method_annotations[i]); |
| 1276 | } |
| 1277 | } |
| 1278 | return nullptr; |
| 1279 | } |
| 1280 | |
| 1281 | const DexFile::ParameterAnnotationsItem* DexFile::FindAnnotationsItemForMethod(ArtMethod* method) |
| 1282 | const { |
| 1283 | mirror::Class* klass = method->GetDeclaringClass(); |
| 1284 | const AnnotationsDirectoryItem* annotations_dir = GetAnnotationsDirectory(*klass->GetClassDef()); |
| 1285 | if (annotations_dir == nullptr) { |
| 1286 | return nullptr; |
| 1287 | } |
| 1288 | const ParameterAnnotationsItem* parameter_annotations = GetParameterAnnotations(annotations_dir); |
| 1289 | if (parameter_annotations == nullptr) { |
| 1290 | return nullptr; |
| 1291 | } |
| 1292 | uint32_t method_index = method->GetDexMethodIndex(); |
| 1293 | uint32_t parameter_count = annotations_dir->parameters_size_; |
| 1294 | for (uint32_t i = 0; i < parameter_count; ++i) { |
| 1295 | if (parameter_annotations[i].method_idx_ == method_index) { |
| 1296 | return ¶meter_annotations[i]; |
| 1297 | } |
| 1298 | } |
| 1299 | return nullptr; |
| 1300 | } |
| 1301 | |
| 1302 | mirror::Object* DexFile::GetAnnotationDefaultValue(ArtMethod* method) const { |
| 1303 | mirror::Class* klass = method->GetDeclaringClass(); |
| 1304 | const AnnotationsDirectoryItem* annotations_dir = GetAnnotationsDirectory(*klass->GetClassDef()); |
| 1305 | if (annotations_dir == nullptr) { |
| 1306 | return nullptr; |
| 1307 | } |
| 1308 | const AnnotationSetItem* annotation_set = GetClassAnnotationSet(annotations_dir); |
| 1309 | if (annotation_set == nullptr) { |
| 1310 | return nullptr; |
| 1311 | } |
| 1312 | const AnnotationItem* annotation_item = SearchAnnotationSet(annotation_set, |
| 1313 | "Ldalvik/annotation/AnnotationDefault;", kDexVisibilitySystem); |
| 1314 | if (annotation_item == nullptr) { |
| 1315 | return nullptr; |
| 1316 | } |
| 1317 | const uint8_t* annotation = SearchEncodedAnnotation(annotation_item->annotation_, "value"); |
| 1318 | if (annotation == nullptr) { |
| 1319 | return nullptr; |
| 1320 | } |
| 1321 | uint8_t header_byte = *(annotation++); |
| 1322 | if ((header_byte & kDexAnnotationValueTypeMask) != kDexAnnotationAnnotation) { |
| 1323 | return nullptr; |
| 1324 | } |
| 1325 | annotation = SearchEncodedAnnotation(annotation, method->GetName()); |
| 1326 | if (annotation == nullptr) { |
| 1327 | return nullptr; |
| 1328 | } |
| 1329 | AnnotationValue annotation_value; |
| 1330 | StackHandleScope<2> hs(Thread::Current()); |
| 1331 | Handle<mirror::Class> h_klass(hs.NewHandle(klass)); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame^] | 1332 | PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1333 | Handle<mirror::Class> return_type(hs.NewHandle( |
| 1334 | method->GetReturnType(true /* resolve */, pointer_size))); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 1335 | if (!ProcessAnnotationValue(h_klass, &annotation, &annotation_value, return_type, kAllObjects)) { |
| 1336 | return nullptr; |
| 1337 | } |
| 1338 | return annotation_value.value_.GetL(); |
| 1339 | } |
| 1340 | |
| 1341 | mirror::Object* DexFile::GetAnnotationForMethod(ArtMethod* method, |
| 1342 | Handle<mirror::Class> annotation_class) const { |
| 1343 | const AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method); |
| 1344 | if (annotation_set == nullptr) { |
| 1345 | return nullptr; |
| 1346 | } |
| 1347 | StackHandleScope<1> hs(Thread::Current()); |
| 1348 | Handle<mirror::Class> method_class(hs.NewHandle(method->GetDeclaringClass())); |
| 1349 | return GetAnnotationObjectFromAnnotationSet(method_class, annotation_set, |
| 1350 | kDexVisibilityRuntime, annotation_class); |
| 1351 | } |
| 1352 | |
| 1353 | mirror::ObjectArray<mirror::Object>* DexFile::GetAnnotationsForMethod(ArtMethod* method) const { |
| 1354 | const AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method); |
| 1355 | StackHandleScope<1> hs(Thread::Current()); |
| 1356 | Handle<mirror::Class> method_class(hs.NewHandle(method->GetDeclaringClass())); |
| 1357 | return ProcessAnnotationSet(method_class, annotation_set, kDexVisibilityRuntime); |
| 1358 | } |
| 1359 | |
Jeff Hao | 2a5892f | 2015-08-31 15:00:40 -0700 | [diff] [blame] | 1360 | mirror::ObjectArray<mirror::Class>* DexFile::GetExceptionTypesForMethod(ArtMethod* method) const { |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 1361 | const AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method); |
| 1362 | if (annotation_set == nullptr) { |
| 1363 | return nullptr; |
| 1364 | } |
| 1365 | StackHandleScope<1> hs(Thread::Current()); |
| 1366 | Handle<mirror::Class> method_class(hs.NewHandle(method->GetDeclaringClass())); |
| 1367 | return GetThrowsValue(method_class, annotation_set); |
| 1368 | } |
| 1369 | |
| 1370 | mirror::ObjectArray<mirror::Object>* DexFile::GetParameterAnnotations(ArtMethod* method) const { |
| 1371 | const ParameterAnnotationsItem* parameter_annotations = FindAnnotationsItemForMethod(method); |
| 1372 | if (parameter_annotations == nullptr) { |
| 1373 | return nullptr; |
| 1374 | } |
| 1375 | const AnnotationSetRefList* set_ref_list = |
| 1376 | GetParameterAnnotationSetRefList(parameter_annotations); |
| 1377 | if (set_ref_list == nullptr) { |
| 1378 | return nullptr; |
| 1379 | } |
| 1380 | uint32_t size = set_ref_list->size_; |
| 1381 | StackHandleScope<1> hs(Thread::Current()); |
| 1382 | Handle<mirror::Class> method_class(hs.NewHandle(method->GetDeclaringClass())); |
| 1383 | return ProcessAnnotationSetRefList(method_class, set_ref_list, size); |
| 1384 | } |
| 1385 | |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 1386 | mirror::ObjectArray<mirror::String>* DexFile::GetSignatureAnnotationForMethod(ArtMethod* method) |
| 1387 | const { |
| 1388 | const AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method); |
| 1389 | if (annotation_set == nullptr) { |
| 1390 | return nullptr; |
| 1391 | } |
| 1392 | StackHandleScope<1> hs(Thread::Current()); |
| 1393 | Handle<mirror::Class> method_class(hs.NewHandle(method->GetDeclaringClass())); |
| 1394 | return GetSignatureValue(method_class, annotation_set); |
| 1395 | } |
| 1396 | |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 1397 | bool DexFile::IsMethodAnnotationPresent(ArtMethod* method, Handle<mirror::Class> annotation_class) |
| 1398 | const { |
| 1399 | const AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method); |
| 1400 | if (annotation_set == nullptr) { |
| 1401 | return false; |
| 1402 | } |
| 1403 | StackHandleScope<1> hs(Thread::Current()); |
| 1404 | Handle<mirror::Class> method_class(hs.NewHandle(method->GetDeclaringClass())); |
| 1405 | const AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet( |
| 1406 | method_class, annotation_set, kDexVisibilityRuntime, annotation_class); |
Jeff Hao | 2a5892f | 2015-08-31 15:00:40 -0700 | [diff] [blame] | 1407 | return annotation_item != nullptr; |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | const DexFile::AnnotationSetItem* DexFile::FindAnnotationSetForClass(Handle<mirror::Class> klass) |
| 1411 | const { |
| 1412 | const AnnotationsDirectoryItem* annotations_dir = GetAnnotationsDirectory(*klass->GetClassDef()); |
| 1413 | if (annotations_dir == nullptr) { |
| 1414 | return nullptr; |
| 1415 | } |
| 1416 | return GetClassAnnotationSet(annotations_dir); |
| 1417 | } |
| 1418 | |
| 1419 | mirror::Object* DexFile::GetAnnotationForClass(Handle<mirror::Class> klass, |
| 1420 | Handle<mirror::Class> annotation_class) const { |
| 1421 | const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass); |
| 1422 | if (annotation_set == nullptr) { |
| 1423 | return nullptr; |
| 1424 | } |
| 1425 | return GetAnnotationObjectFromAnnotationSet(klass, annotation_set, kDexVisibilityRuntime, |
| 1426 | annotation_class); |
| 1427 | } |
| 1428 | |
| 1429 | mirror::ObjectArray<mirror::Object>* DexFile::GetAnnotationsForClass(Handle<mirror::Class> klass) |
| 1430 | const { |
| 1431 | const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass); |
| 1432 | return ProcessAnnotationSet(klass, annotation_set, kDexVisibilityRuntime); |
| 1433 | } |
| 1434 | |
Jeff Hao | 2a5892f | 2015-08-31 15:00:40 -0700 | [diff] [blame] | 1435 | mirror::ObjectArray<mirror::Class>* DexFile::GetDeclaredClasses(Handle<mirror::Class> klass) const { |
| 1436 | const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass); |
| 1437 | if (annotation_set == nullptr) { |
| 1438 | return nullptr; |
| 1439 | } |
| 1440 | const AnnotationItem* annotation_item = SearchAnnotationSet( |
| 1441 | annotation_set, "Ldalvik/annotation/MemberClasses;", kDexVisibilitySystem); |
| 1442 | if (annotation_item == nullptr) { |
| 1443 | return nullptr; |
| 1444 | } |
| 1445 | StackHandleScope<1> hs(Thread::Current()); |
| 1446 | mirror::Class* class_class = mirror::Class::GetJavaLangClass(); |
| 1447 | Handle<mirror::Class> class_array_class(hs.NewHandle( |
| 1448 | Runtime::Current()->GetClassLinker()->FindArrayClass(hs.Self(), &class_class))); |
| 1449 | if (class_array_class.Get() == nullptr) { |
| 1450 | return nullptr; |
| 1451 | } |
| 1452 | mirror::Object* obj = GetAnnotationValue( |
| 1453 | klass, annotation_item, "value", class_array_class, kDexAnnotationArray); |
| 1454 | if (obj == nullptr) { |
| 1455 | return nullptr; |
| 1456 | } |
| 1457 | return obj->AsObjectArray<mirror::Class>(); |
| 1458 | } |
| 1459 | |
| 1460 | mirror::Class* DexFile::GetDeclaringClass(Handle<mirror::Class> klass) const { |
| 1461 | const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass); |
| 1462 | if (annotation_set == nullptr) { |
| 1463 | return nullptr; |
| 1464 | } |
| 1465 | const AnnotationItem* annotation_item = SearchAnnotationSet( |
| 1466 | annotation_set, "Ldalvik/annotation/EnclosingClass;", kDexVisibilitySystem); |
| 1467 | if (annotation_item == nullptr) { |
| 1468 | return nullptr; |
| 1469 | } |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 1470 | mirror::Object* obj = GetAnnotationValue(klass, |
| 1471 | annotation_item, |
| 1472 | "value", |
| 1473 | ScopedNullHandle<mirror::Class>(), |
| 1474 | kDexAnnotationType); |
Jeff Hao | 2a5892f | 2015-08-31 15:00:40 -0700 | [diff] [blame] | 1475 | if (obj == nullptr) { |
| 1476 | return nullptr; |
| 1477 | } |
| 1478 | return obj->AsClass(); |
| 1479 | } |
| 1480 | |
| 1481 | mirror::Class* DexFile::GetEnclosingClass(Handle<mirror::Class> klass) const { |
| 1482 | mirror::Class* declaring_class = GetDeclaringClass(klass); |
| 1483 | if (declaring_class != nullptr) { |
| 1484 | return declaring_class; |
| 1485 | } |
| 1486 | const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass); |
| 1487 | if (annotation_set == nullptr) { |
| 1488 | return nullptr; |
| 1489 | } |
| 1490 | const AnnotationItem* annotation_item = SearchAnnotationSet( |
| 1491 | annotation_set, "Ldalvik/annotation/EnclosingMethod;", kDexVisibilitySystem); |
| 1492 | if (annotation_item == nullptr) { |
| 1493 | return nullptr; |
| 1494 | } |
| 1495 | const uint8_t* annotation = SearchEncodedAnnotation(annotation_item->annotation_, "value"); |
| 1496 | if (annotation == nullptr) { |
| 1497 | return nullptr; |
| 1498 | } |
| 1499 | AnnotationValue annotation_value; |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 1500 | if (!ProcessAnnotationValue(klass, |
| 1501 | &annotation, |
| 1502 | &annotation_value, |
| 1503 | ScopedNullHandle<mirror::Class>(), |
| 1504 | kAllRaw)) { |
Jeff Hao | 2a5892f | 2015-08-31 15:00:40 -0700 | [diff] [blame] | 1505 | return nullptr; |
| 1506 | } |
| 1507 | if (annotation_value.type_ != kDexAnnotationMethod) { |
| 1508 | return nullptr; |
| 1509 | } |
| 1510 | StackHandleScope<2> hs(Thread::Current()); |
| 1511 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache())); |
| 1512 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader())); |
| 1513 | ArtMethod* method = Runtime::Current()->GetClassLinker()->ResolveMethodWithoutInvokeType( |
| 1514 | klass->GetDexFile(), annotation_value.value_.GetI(), dex_cache, class_loader); |
| 1515 | if (method == nullptr) { |
| 1516 | return nullptr; |
| 1517 | } |
| 1518 | return method->GetDeclaringClass(); |
| 1519 | } |
| 1520 | |
| 1521 | mirror::Object* DexFile::GetEnclosingMethod(Handle<mirror::Class> klass) const { |
| 1522 | const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass); |
| 1523 | if (annotation_set == nullptr) { |
| 1524 | return nullptr; |
| 1525 | } |
| 1526 | const AnnotationItem* annotation_item = SearchAnnotationSet( |
| 1527 | annotation_set, "Ldalvik/annotation/EnclosingMethod;", kDexVisibilitySystem); |
| 1528 | if (annotation_item == nullptr) { |
| 1529 | return nullptr; |
| 1530 | } |
| 1531 | return GetAnnotationValue( |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 1532 | klass, annotation_item, "value", ScopedNullHandle<mirror::Class>(), kDexAnnotationMethod); |
Jeff Hao | 2a5892f | 2015-08-31 15:00:40 -0700 | [diff] [blame] | 1533 | } |
| 1534 | |
| 1535 | bool DexFile::GetInnerClass(Handle<mirror::Class> klass, mirror::String** name) const { |
| 1536 | const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass); |
| 1537 | if (annotation_set == nullptr) { |
| 1538 | return false; |
| 1539 | } |
| 1540 | const AnnotationItem* annotation_item = SearchAnnotationSet( |
| 1541 | annotation_set, "Ldalvik/annotation/InnerClass;", kDexVisibilitySystem); |
| 1542 | if (annotation_item == nullptr) { |
| 1543 | return false; |
| 1544 | } |
| 1545 | const uint8_t* annotation = SearchEncodedAnnotation(annotation_item->annotation_, "name"); |
| 1546 | if (annotation == nullptr) { |
| 1547 | return false; |
| 1548 | } |
| 1549 | AnnotationValue annotation_value; |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 1550 | if (!ProcessAnnotationValue(klass, |
| 1551 | &annotation, |
| 1552 | &annotation_value, |
| 1553 | ScopedNullHandle<mirror::Class>(), |
| 1554 | kAllObjects)) { |
Jeff Hao | 2a5892f | 2015-08-31 15:00:40 -0700 | [diff] [blame] | 1555 | return false; |
| 1556 | } |
| 1557 | if (annotation_value.type_ != kDexAnnotationNull && |
| 1558 | annotation_value.type_ != kDexAnnotationString) { |
| 1559 | return false; |
| 1560 | } |
| 1561 | *name = down_cast<mirror::String*>(annotation_value.value_.GetL()); |
| 1562 | return true; |
| 1563 | } |
| 1564 | |
| 1565 | bool DexFile::GetInnerClassFlags(Handle<mirror::Class> klass, uint32_t* flags) const { |
| 1566 | const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass); |
| 1567 | if (annotation_set == nullptr) { |
| 1568 | return false; |
| 1569 | } |
| 1570 | const AnnotationItem* annotation_item = SearchAnnotationSet( |
| 1571 | annotation_set, "Ldalvik/annotation/InnerClass;", kDexVisibilitySystem); |
| 1572 | if (annotation_item == nullptr) { |
| 1573 | return false; |
| 1574 | } |
| 1575 | const uint8_t* annotation = SearchEncodedAnnotation(annotation_item->annotation_, "accessFlags"); |
| 1576 | if (annotation == nullptr) { |
| 1577 | return false; |
| 1578 | } |
| 1579 | AnnotationValue annotation_value; |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 1580 | if (!ProcessAnnotationValue(klass, |
| 1581 | &annotation, |
| 1582 | &annotation_value, |
| 1583 | ScopedNullHandle<mirror::Class>(), |
| 1584 | kAllRaw)) { |
Jeff Hao | 2a5892f | 2015-08-31 15:00:40 -0700 | [diff] [blame] | 1585 | return false; |
| 1586 | } |
| 1587 | if (annotation_value.type_ != kDexAnnotationInt) { |
| 1588 | return false; |
| 1589 | } |
| 1590 | *flags = annotation_value.value_.GetI(); |
| 1591 | return true; |
| 1592 | } |
| 1593 | |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 1594 | mirror::ObjectArray<mirror::String>* DexFile::GetSignatureAnnotationForClass( |
| 1595 | Handle<mirror::Class> klass) const { |
| 1596 | const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass); |
| 1597 | if (annotation_set == nullptr) { |
| 1598 | return nullptr; |
| 1599 | } |
| 1600 | return GetSignatureValue(klass, annotation_set); |
| 1601 | } |
| 1602 | |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 1603 | bool DexFile::IsClassAnnotationPresent(Handle<mirror::Class> klass, |
| 1604 | Handle<mirror::Class> annotation_class) const { |
| 1605 | const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass); |
| 1606 | if (annotation_set == nullptr) { |
| 1607 | return false; |
| 1608 | } |
| 1609 | const AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet( |
| 1610 | klass, annotation_set, kDexVisibilityRuntime, annotation_class); |
Jeff Hao | 2a5892f | 2015-08-31 15:00:40 -0700 | [diff] [blame] | 1611 | return annotation_item != nullptr; |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
| 1614 | mirror::Object* DexFile::CreateAnnotationMember(Handle<mirror::Class> klass, |
| 1615 | Handle<mirror::Class> annotation_class, const uint8_t** annotation) const { |
| 1616 | Thread* self = Thread::Current(); |
| 1617 | ScopedObjectAccessUnchecked soa(self); |
| 1618 | StackHandleScope<5> hs(self); |
| 1619 | uint32_t element_name_index = DecodeUnsignedLeb128(annotation); |
| 1620 | const char* name = StringDataByIdx(element_name_index); |
| 1621 | Handle<mirror::String> string_name( |
| 1622 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(self, name))); |
| 1623 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame^] | 1624 | PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 1625 | ArtMethod* annotation_method = |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame^] | 1626 | annotation_class->FindDeclaredVirtualMethodByName(name, pointer_size); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 1627 | if (annotation_method == nullptr) { |
| 1628 | return nullptr; |
| 1629 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1630 | Handle<mirror::Class> method_return(hs.NewHandle( |
| 1631 | annotation_method->GetReturnType(true /* resolve */, pointer_size))); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 1632 | |
| 1633 | AnnotationValue annotation_value; |
| 1634 | if (!ProcessAnnotationValue(klass, annotation, &annotation_value, method_return, kAllObjects)) { |
| 1635 | return nullptr; |
| 1636 | } |
| 1637 | Handle<mirror::Object> value_object(hs.NewHandle(annotation_value.value_.GetL())); |
| 1638 | |
| 1639 | mirror::Class* annotation_member_class = |
| 1640 | WellKnownClasses::ToClass(WellKnownClasses::libcore_reflect_AnnotationMember); |
| 1641 | Handle<mirror::Object> new_member(hs.NewHandle(annotation_member_class->AllocObject(self))); |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 1642 | mirror::Method* method_obj_ptr; |
| 1643 | DCHECK(!Runtime::Current()->IsActiveTransaction()); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame^] | 1644 | if (pointer_size == PointerSize::k64) { |
| 1645 | method_obj_ptr = mirror::Method::CreateFromArtMethod<PointerSize::k64, false>( |
| 1646 | self, annotation_method); |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 1647 | } else { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame^] | 1648 | method_obj_ptr = mirror::Method::CreateFromArtMethod<PointerSize::k32, false>( |
| 1649 | self, annotation_method); |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 1650 | } |
| 1651 | Handle<mirror::Method> method_object(hs.NewHandle(method_obj_ptr)); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 1652 | |
| 1653 | if (new_member.Get() == nullptr || string_name.Get() == nullptr || |
| 1654 | method_object.Get() == nullptr || method_return.Get() == nullptr) { |
| 1655 | LOG(ERROR) << StringPrintf("Failed creating annotation element (m=%p n=%p a=%p r=%p", |
| 1656 | new_member.Get(), string_name.Get(), method_object.Get(), method_return.Get()); |
| 1657 | return nullptr; |
| 1658 | } |
| 1659 | |
| 1660 | JValue result; |
| 1661 | ArtMethod* annotation_member_init = |
| 1662 | soa.DecodeMethod(WellKnownClasses::libcore_reflect_AnnotationMember_init); |
| 1663 | uint32_t args[5] = { static_cast<uint32_t>(reinterpret_cast<uintptr_t>(new_member.Get())), |
| 1664 | static_cast<uint32_t>(reinterpret_cast<uintptr_t>(string_name.Get())), |
| 1665 | static_cast<uint32_t>(reinterpret_cast<uintptr_t>(value_object.Get())), |
| 1666 | static_cast<uint32_t>(reinterpret_cast<uintptr_t>(method_return.Get())), |
| 1667 | static_cast<uint32_t>(reinterpret_cast<uintptr_t>(method_object.Get())) |
| 1668 | }; |
| 1669 | annotation_member_init->Invoke(self, args, sizeof(args), &result, "VLLLL"); |
| 1670 | if (self->IsExceptionPending()) { |
| 1671 | LOG(INFO) << "Exception in AnnotationMember.<init>"; |
| 1672 | return nullptr; |
| 1673 | } |
| 1674 | |
| 1675 | return new_member.Get(); |
| 1676 | } |
| 1677 | |
| 1678 | const DexFile::AnnotationItem* DexFile::GetAnnotationItemFromAnnotationSet( |
| 1679 | Handle<mirror::Class> klass, const AnnotationSetItem* annotation_set, uint32_t visibility, |
| 1680 | Handle<mirror::Class> annotation_class) const { |
| 1681 | for (uint32_t i = 0; i < annotation_set->size_; ++i) { |
| 1682 | const AnnotationItem* annotation_item = GetAnnotationItem(annotation_set, i); |
Jeff Hao | 3d08086 | 2016-05-26 18:39:17 -0700 | [diff] [blame] | 1683 | if (!IsVisibilityCompatible(annotation_item->visibility_, visibility)) { |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 1684 | continue; |
| 1685 | } |
| 1686 | const uint8_t* annotation = annotation_item->annotation_; |
| 1687 | uint32_t type_index = DecodeUnsignedLeb128(&annotation); |
| 1688 | mirror::Class* resolved_class = Runtime::Current()->GetClassLinker()->ResolveType( |
| 1689 | klass->GetDexFile(), type_index, klass.Get()); |
| 1690 | if (resolved_class == nullptr) { |
| 1691 | std::string temp; |
| 1692 | LOG(WARNING) << StringPrintf("Unable to resolve %s annotation class %d", |
| 1693 | klass->GetDescriptor(&temp), type_index); |
| 1694 | CHECK(Thread::Current()->IsExceptionPending()); |
| 1695 | Thread::Current()->ClearException(); |
| 1696 | continue; |
| 1697 | } |
| 1698 | if (resolved_class == annotation_class.Get()) { |
| 1699 | return annotation_item; |
| 1700 | } |
| 1701 | } |
| 1702 | |
| 1703 | return nullptr; |
| 1704 | } |
| 1705 | |
| 1706 | mirror::Object* DexFile::GetAnnotationObjectFromAnnotationSet(Handle<mirror::Class> klass, |
| 1707 | const AnnotationSetItem* annotation_set, uint32_t visibility, |
| 1708 | Handle<mirror::Class> annotation_class) const { |
| 1709 | const AnnotationItem* annotation_item = |
| 1710 | GetAnnotationItemFromAnnotationSet(klass, annotation_set, visibility, annotation_class); |
| 1711 | if (annotation_item == nullptr) { |
| 1712 | return nullptr; |
| 1713 | } |
| 1714 | const uint8_t* annotation = annotation_item->annotation_; |
| 1715 | return ProcessEncodedAnnotation(klass, &annotation); |
| 1716 | } |
| 1717 | |
| 1718 | mirror::Object* DexFile::GetAnnotationValue(Handle<mirror::Class> klass, |
| 1719 | const AnnotationItem* annotation_item, const char* annotation_name, |
| 1720 | Handle<mirror::Class> array_class, uint32_t expected_type) const { |
| 1721 | const uint8_t* annotation = |
| 1722 | SearchEncodedAnnotation(annotation_item->annotation_, annotation_name); |
| 1723 | if (annotation == nullptr) { |
| 1724 | return nullptr; |
| 1725 | } |
| 1726 | AnnotationValue annotation_value; |
| 1727 | if (!ProcessAnnotationValue(klass, &annotation, &annotation_value, array_class, kAllObjects)) { |
| 1728 | return nullptr; |
| 1729 | } |
| 1730 | if (annotation_value.type_ != expected_type) { |
| 1731 | return nullptr; |
| 1732 | } |
| 1733 | return annotation_value.value_.GetL(); |
| 1734 | } |
| 1735 | |
Jeff Hao | 2a5892f | 2015-08-31 15:00:40 -0700 | [diff] [blame] | 1736 | mirror::ObjectArray<mirror::String>* DexFile::GetSignatureValue(Handle<mirror::Class> klass, |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 1737 | const AnnotationSetItem* annotation_set) const { |
| 1738 | StackHandleScope<1> hs(Thread::Current()); |
| 1739 | const AnnotationItem* annotation_item = |
| 1740 | SearchAnnotationSet(annotation_set, "Ldalvik/annotation/Signature;", kDexVisibilitySystem); |
| 1741 | if (annotation_item == nullptr) { |
| 1742 | return nullptr; |
| 1743 | } |
| 1744 | mirror::Class* string_class = mirror::String::GetJavaLangString(); |
| 1745 | Handle<mirror::Class> string_array_class(hs.NewHandle( |
| 1746 | Runtime::Current()->GetClassLinker()->FindArrayClass(Thread::Current(), &string_class))); |
Jeff Hao | 2a5892f | 2015-08-31 15:00:40 -0700 | [diff] [blame] | 1747 | if (string_array_class.Get() == nullptr) { |
| 1748 | return nullptr; |
| 1749 | } |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 1750 | mirror::Object* obj = |
| 1751 | GetAnnotationValue(klass, annotation_item, "value", string_array_class, kDexAnnotationArray); |
| 1752 | if (obj == nullptr) { |
| 1753 | return nullptr; |
| 1754 | } |
Jeff Hao | 2a5892f | 2015-08-31 15:00:40 -0700 | [diff] [blame] | 1755 | return obj->AsObjectArray<mirror::String>(); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
Jeff Hao | 2a5892f | 2015-08-31 15:00:40 -0700 | [diff] [blame] | 1758 | mirror::ObjectArray<mirror::Class>* DexFile::GetThrowsValue(Handle<mirror::Class> klass, |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 1759 | const AnnotationSetItem* annotation_set) const { |
| 1760 | StackHandleScope<1> hs(Thread::Current()); |
| 1761 | const AnnotationItem* annotation_item = |
| 1762 | SearchAnnotationSet(annotation_set, "Ldalvik/annotation/Throws;", kDexVisibilitySystem); |
| 1763 | if (annotation_item == nullptr) { |
| 1764 | return nullptr; |
| 1765 | } |
| 1766 | mirror::Class* class_class = mirror::Class::GetJavaLangClass(); |
| 1767 | Handle<mirror::Class> class_array_class(hs.NewHandle( |
| 1768 | Runtime::Current()->GetClassLinker()->FindArrayClass(Thread::Current(), &class_class))); |
Jeff Hao | 2a5892f | 2015-08-31 15:00:40 -0700 | [diff] [blame] | 1769 | if (class_array_class.Get() == nullptr) { |
| 1770 | return nullptr; |
| 1771 | } |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 1772 | mirror::Object* obj = |
| 1773 | GetAnnotationValue(klass, annotation_item, "value", class_array_class, kDexAnnotationArray); |
| 1774 | if (obj == nullptr) { |
| 1775 | return nullptr; |
| 1776 | } |
Jeff Hao | 2a5892f | 2015-08-31 15:00:40 -0700 | [diff] [blame] | 1777 | return obj->AsObjectArray<mirror::Class>(); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 1778 | } |
| 1779 | |
| 1780 | mirror::ObjectArray<mirror::Object>* DexFile::ProcessAnnotationSet(Handle<mirror::Class> klass, |
| 1781 | const AnnotationSetItem* annotation_set, uint32_t visibility) const { |
| 1782 | Thread* self = Thread::Current(); |
| 1783 | ScopedObjectAccessUnchecked soa(self); |
| 1784 | StackHandleScope<2> hs(self); |
| 1785 | Handle<mirror::Class> annotation_array_class(hs.NewHandle( |
| 1786 | soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_annotation_Annotation__array))); |
| 1787 | if (annotation_set == nullptr) { |
| 1788 | return mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), 0); |
| 1789 | } |
| 1790 | |
| 1791 | uint32_t size = annotation_set->size_; |
| 1792 | Handle<mirror::ObjectArray<mirror::Object>> result(hs.NewHandle( |
| 1793 | mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), size))); |
| 1794 | if (result.Get() == nullptr) { |
| 1795 | return nullptr; |
| 1796 | } |
| 1797 | |
| 1798 | uint32_t dest_index = 0; |
| 1799 | for (uint32_t i = 0; i < size; ++i) { |
| 1800 | const AnnotationItem* annotation_item = GetAnnotationItem(annotation_set, i); |
Jeff Hao | 3d08086 | 2016-05-26 18:39:17 -0700 | [diff] [blame] | 1801 | // Note that we do not use IsVisibilityCompatible here because older code |
| 1802 | // was correct for this case. |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 1803 | if (annotation_item->visibility_ != visibility) { |
| 1804 | continue; |
| 1805 | } |
| 1806 | const uint8_t* annotation = annotation_item->annotation_; |
| 1807 | mirror::Object* annotation_obj = ProcessEncodedAnnotation(klass, &annotation); |
| 1808 | if (annotation_obj != nullptr) { |
| 1809 | result->SetWithoutChecks<false>(dest_index, annotation_obj); |
| 1810 | ++dest_index; |
Jeff Hao | 2a5892f | 2015-08-31 15:00:40 -0700 | [diff] [blame] | 1811 | } else if (self->IsExceptionPending()) { |
| 1812 | return nullptr; |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 1813 | } |
| 1814 | } |
| 1815 | |
| 1816 | if (dest_index == size) { |
| 1817 | return result.Get(); |
| 1818 | } |
| 1819 | |
| 1820 | mirror::ObjectArray<mirror::Object>* trimmed_result = |
| 1821 | mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), dest_index); |
Jeff Hao | 2a5892f | 2015-08-31 15:00:40 -0700 | [diff] [blame] | 1822 | if (trimmed_result == nullptr) { |
| 1823 | return nullptr; |
| 1824 | } |
| 1825 | |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 1826 | for (uint32_t i = 0; i < dest_index; ++i) { |
| 1827 | mirror::Object* obj = result->GetWithoutChecks(i); |
| 1828 | trimmed_result->SetWithoutChecks<false>(i, obj); |
| 1829 | } |
| 1830 | |
| 1831 | return trimmed_result; |
| 1832 | } |
| 1833 | |
| 1834 | mirror::ObjectArray<mirror::Object>* DexFile::ProcessAnnotationSetRefList( |
| 1835 | Handle<mirror::Class> klass, const AnnotationSetRefList* set_ref_list, uint32_t size) const { |
| 1836 | Thread* self = Thread::Current(); |
| 1837 | ScopedObjectAccessUnchecked soa(self); |
| 1838 | StackHandleScope<1> hs(self); |
| 1839 | mirror::Class* annotation_array_class = |
| 1840 | soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_annotation_Annotation__array); |
| 1841 | mirror::Class* annotation_array_array_class = |
| 1842 | Runtime::Current()->GetClassLinker()->FindArrayClass(self, &annotation_array_class); |
Jeff Hao | 2a5892f | 2015-08-31 15:00:40 -0700 | [diff] [blame] | 1843 | if (annotation_array_array_class == nullptr) { |
| 1844 | return nullptr; |
| 1845 | } |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 1846 | Handle<mirror::ObjectArray<mirror::Object>> annotation_array_array(hs.NewHandle( |
| 1847 | mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_array_class, size))); |
| 1848 | if (annotation_array_array.Get() == nullptr) { |
| 1849 | LOG(ERROR) << "Annotation set ref array allocation failed"; |
| 1850 | return nullptr; |
| 1851 | } |
| 1852 | for (uint32_t index = 0; index < size; ++index) { |
| 1853 | const AnnotationSetRefItem* set_ref_item = &set_ref_list->list_[index]; |
| 1854 | const AnnotationSetItem* set_item = GetSetRefItemItem(set_ref_item); |
| 1855 | mirror::Object* annotation_set = ProcessAnnotationSet(klass, set_item, kDexVisibilityRuntime); |
| 1856 | if (annotation_set == nullptr) { |
| 1857 | return nullptr; |
| 1858 | } |
| 1859 | annotation_array_array->SetWithoutChecks<false>(index, annotation_set); |
| 1860 | } |
| 1861 | return annotation_array_array.Get(); |
| 1862 | } |
| 1863 | |
| 1864 | bool DexFile::ProcessAnnotationValue(Handle<mirror::Class> klass, const uint8_t** annotation_ptr, |
| 1865 | AnnotationValue* annotation_value, Handle<mirror::Class> array_class, |
| 1866 | DexFile::AnnotationResultStyle result_style) const { |
| 1867 | Thread* self = Thread::Current(); |
| 1868 | mirror::Object* element_object = nullptr; |
| 1869 | bool set_object = false; |
| 1870 | Primitive::Type primitive_type = Primitive::kPrimVoid; |
| 1871 | const uint8_t* annotation = *annotation_ptr; |
| 1872 | uint8_t header_byte = *(annotation++); |
| 1873 | uint8_t value_type = header_byte & kDexAnnotationValueTypeMask; |
| 1874 | uint8_t value_arg = header_byte >> kDexAnnotationValueArgShift; |
| 1875 | int32_t width = value_arg + 1; |
| 1876 | annotation_value->type_ = value_type; |
| 1877 | |
| 1878 | switch (value_type) { |
| 1879 | case kDexAnnotationByte: |
| 1880 | annotation_value->value_.SetB(static_cast<int8_t>(ReadSignedInt(annotation, value_arg))); |
| 1881 | primitive_type = Primitive::kPrimByte; |
| 1882 | break; |
| 1883 | case kDexAnnotationShort: |
| 1884 | annotation_value->value_.SetS(static_cast<int16_t>(ReadSignedInt(annotation, value_arg))); |
| 1885 | primitive_type = Primitive::kPrimShort; |
| 1886 | break; |
| 1887 | case kDexAnnotationChar: |
| 1888 | annotation_value->value_.SetC(static_cast<uint16_t>(ReadUnsignedInt(annotation, value_arg, |
| 1889 | false))); |
| 1890 | primitive_type = Primitive::kPrimChar; |
| 1891 | break; |
| 1892 | case kDexAnnotationInt: |
| 1893 | annotation_value->value_.SetI(ReadSignedInt(annotation, value_arg)); |
| 1894 | primitive_type = Primitive::kPrimInt; |
| 1895 | break; |
| 1896 | case kDexAnnotationLong: |
| 1897 | annotation_value->value_.SetJ(ReadSignedLong(annotation, value_arg)); |
| 1898 | primitive_type = Primitive::kPrimLong; |
| 1899 | break; |
| 1900 | case kDexAnnotationFloat: |
| 1901 | annotation_value->value_.SetI(ReadUnsignedInt(annotation, value_arg, true)); |
| 1902 | primitive_type = Primitive::kPrimFloat; |
| 1903 | break; |
| 1904 | case kDexAnnotationDouble: |
| 1905 | annotation_value->value_.SetJ(ReadUnsignedLong(annotation, value_arg, true)); |
| 1906 | primitive_type = Primitive::kPrimDouble; |
| 1907 | break; |
| 1908 | case kDexAnnotationBoolean: |
| 1909 | annotation_value->value_.SetZ(value_arg != 0); |
| 1910 | primitive_type = Primitive::kPrimBoolean; |
| 1911 | width = 0; |
| 1912 | break; |
| 1913 | case kDexAnnotationString: { |
| 1914 | uint32_t index = ReadUnsignedInt(annotation, value_arg, false); |
| 1915 | if (result_style == kAllRaw) { |
| 1916 | annotation_value->value_.SetI(index); |
| 1917 | } else { |
| 1918 | StackHandleScope<1> hs(self); |
| 1919 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache())); |
| 1920 | element_object = Runtime::Current()->GetClassLinker()->ResolveString( |
| 1921 | klass->GetDexFile(), index, dex_cache); |
| 1922 | set_object = true; |
| 1923 | if (element_object == nullptr) { |
| 1924 | return false; |
| 1925 | } |
| 1926 | } |
| 1927 | break; |
| 1928 | } |
| 1929 | case kDexAnnotationType: { |
| 1930 | uint32_t index = ReadUnsignedInt(annotation, value_arg, false); |
| 1931 | if (result_style == kAllRaw) { |
| 1932 | annotation_value->value_.SetI(index); |
| 1933 | } else { |
| 1934 | element_object = Runtime::Current()->GetClassLinker()->ResolveType( |
| 1935 | klass->GetDexFile(), index, klass.Get()); |
| 1936 | set_object = true; |
| 1937 | if (element_object == nullptr) { |
Jeff Hao | fc8d247 | 2015-09-02 13:52:20 -0700 | [diff] [blame] | 1938 | CHECK(self->IsExceptionPending()); |
| 1939 | if (result_style == kAllObjects) { |
| 1940 | const char* msg = StringByTypeIdx(index); |
| 1941 | self->ThrowNewWrappedException("Ljava/lang/TypeNotPresentException;", msg); |
| 1942 | element_object = self->GetException(); |
| 1943 | self->ClearException(); |
| 1944 | } else { |
| 1945 | return false; |
| 1946 | } |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 1947 | } |
| 1948 | } |
| 1949 | break; |
| 1950 | } |
| 1951 | case kDexAnnotationMethod: { |
| 1952 | uint32_t index = ReadUnsignedInt(annotation, value_arg, false); |
| 1953 | if (result_style == kAllRaw) { |
| 1954 | annotation_value->value_.SetI(index); |
| 1955 | } else { |
| 1956 | StackHandleScope<2> hs(self); |
| 1957 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache())); |
| 1958 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader())); |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 1959 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 1960 | ArtMethod* method = class_linker->ResolveMethodWithoutInvokeType( |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 1961 | klass->GetDexFile(), index, dex_cache, class_loader); |
| 1962 | if (method == nullptr) { |
| 1963 | return false; |
| 1964 | } |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame^] | 1965 | PointerSize pointer_size = class_linker->GetImagePointerSize(); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 1966 | set_object = true; |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 1967 | DCHECK(!Runtime::Current()->IsActiveTransaction()); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 1968 | if (method->IsConstructor()) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame^] | 1969 | if (pointer_size == PointerSize::k64) { |
| 1970 | element_object = mirror::Constructor::CreateFromArtMethod<PointerSize::k64, |
| 1971 | false>(self, method); |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 1972 | } else { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame^] | 1973 | element_object = mirror::Constructor::CreateFromArtMethod<PointerSize::k32, |
| 1974 | false>(self, method); |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 1975 | } |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 1976 | } else { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame^] | 1977 | if (pointer_size == PointerSize::k64) { |
| 1978 | element_object = mirror::Method::CreateFromArtMethod<PointerSize::k64, |
| 1979 | false>(self, method); |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 1980 | } else { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame^] | 1981 | element_object = mirror::Method::CreateFromArtMethod<PointerSize::k32, |
| 1982 | false>(self, method); |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 1983 | } |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 1984 | } |
| 1985 | if (element_object == nullptr) { |
| 1986 | return false; |
| 1987 | } |
| 1988 | } |
| 1989 | break; |
| 1990 | } |
| 1991 | case kDexAnnotationField: { |
| 1992 | uint32_t index = ReadUnsignedInt(annotation, value_arg, false); |
| 1993 | if (result_style == kAllRaw) { |
| 1994 | annotation_value->value_.SetI(index); |
| 1995 | } else { |
| 1996 | StackHandleScope<2> hs(self); |
| 1997 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache())); |
| 1998 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader())); |
| 1999 | ArtField* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS( |
| 2000 | klass->GetDexFile(), index, dex_cache, class_loader); |
| 2001 | if (field == nullptr) { |
| 2002 | return false; |
| 2003 | } |
| 2004 | set_object = true; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame^] | 2005 | PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
| 2006 | if (pointer_size == PointerSize::k64) { |
| 2007 | element_object = mirror::Field::CreateFromArtField<PointerSize::k64>(self, field, true); |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 2008 | } else { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame^] | 2009 | element_object = mirror::Field::CreateFromArtField<PointerSize::k32>(self, field, true); |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 2010 | } |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 2011 | if (element_object == nullptr) { |
| 2012 | return false; |
| 2013 | } |
| 2014 | } |
| 2015 | break; |
| 2016 | } |
| 2017 | case kDexAnnotationEnum: { |
| 2018 | uint32_t index = ReadUnsignedInt(annotation, value_arg, false); |
| 2019 | if (result_style == kAllRaw) { |
| 2020 | annotation_value->value_.SetI(index); |
| 2021 | } else { |
| 2022 | StackHandleScope<3> hs(self); |
| 2023 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache())); |
| 2024 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader())); |
| 2025 | ArtField* enum_field = Runtime::Current()->GetClassLinker()->ResolveField( |
| 2026 | klass->GetDexFile(), index, dex_cache, class_loader, true); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 2027 | if (enum_field == nullptr) { |
| 2028 | return false; |
| 2029 | } else { |
Jeff Hao | d297b55 | 2015-11-20 14:56:09 -0800 | [diff] [blame] | 2030 | Handle<mirror::Class> field_class(hs.NewHandle(enum_field->GetDeclaringClass())); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 2031 | Runtime::Current()->GetClassLinker()->EnsureInitialized(self, field_class, true, true); |
| 2032 | element_object = enum_field->GetObject(field_class.Get()); |
| 2033 | set_object = true; |
| 2034 | } |
| 2035 | } |
| 2036 | break; |
| 2037 | } |
| 2038 | case kDexAnnotationArray: |
| 2039 | if (result_style == kAllRaw || array_class.Get() == nullptr) { |
| 2040 | return false; |
| 2041 | } else { |
| 2042 | ScopedObjectAccessUnchecked soa(self); |
| 2043 | StackHandleScope<2> hs(self); |
| 2044 | uint32_t size = DecodeUnsignedLeb128(&annotation); |
| 2045 | Handle<mirror::Class> component_type(hs.NewHandle(array_class->GetComponentType())); |
| 2046 | Handle<mirror::Array> new_array(hs.NewHandle(mirror::Array::Alloc<true>( |
| 2047 | self, array_class.Get(), size, array_class->GetComponentSizeShift(), |
| 2048 | Runtime::Current()->GetHeap()->GetCurrentAllocator()))); |
| 2049 | if (new_array.Get() == nullptr) { |
| 2050 | LOG(ERROR) << "Annotation element array allocation failed with size " << size; |
| 2051 | return false; |
| 2052 | } |
| 2053 | AnnotationValue new_annotation_value; |
| 2054 | for (uint32_t i = 0; i < size; ++i) { |
| 2055 | if (!ProcessAnnotationValue(klass, &annotation, &new_annotation_value, component_type, |
| 2056 | kPrimitivesOrObjects)) { |
| 2057 | return false; |
| 2058 | } |
| 2059 | if (!component_type->IsPrimitive()) { |
| 2060 | mirror::Object* obj = new_annotation_value.value_.GetL(); |
| 2061 | new_array->AsObjectArray<mirror::Object>()->SetWithoutChecks<false>(i, obj); |
| 2062 | } else { |
| 2063 | switch (new_annotation_value.type_) { |
| 2064 | case kDexAnnotationByte: |
| 2065 | new_array->AsByteArray()->SetWithoutChecks<false>( |
| 2066 | i, new_annotation_value.value_.GetB()); |
| 2067 | break; |
| 2068 | case kDexAnnotationShort: |
| 2069 | new_array->AsShortArray()->SetWithoutChecks<false>( |
| 2070 | i, new_annotation_value.value_.GetS()); |
| 2071 | break; |
| 2072 | case kDexAnnotationChar: |
| 2073 | new_array->AsCharArray()->SetWithoutChecks<false>( |
| 2074 | i, new_annotation_value.value_.GetC()); |
| 2075 | break; |
| 2076 | case kDexAnnotationInt: |
| 2077 | new_array->AsIntArray()->SetWithoutChecks<false>( |
| 2078 | i, new_annotation_value.value_.GetI()); |
| 2079 | break; |
| 2080 | case kDexAnnotationLong: |
| 2081 | new_array->AsLongArray()->SetWithoutChecks<false>( |
| 2082 | i, new_annotation_value.value_.GetJ()); |
| 2083 | break; |
| 2084 | case kDexAnnotationFloat: |
| 2085 | new_array->AsFloatArray()->SetWithoutChecks<false>( |
| 2086 | i, new_annotation_value.value_.GetF()); |
| 2087 | break; |
| 2088 | case kDexAnnotationDouble: |
| 2089 | new_array->AsDoubleArray()->SetWithoutChecks<false>( |
| 2090 | i, new_annotation_value.value_.GetD()); |
| 2091 | break; |
| 2092 | case kDexAnnotationBoolean: |
| 2093 | new_array->AsBooleanArray()->SetWithoutChecks<false>( |
| 2094 | i, new_annotation_value.value_.GetZ()); |
| 2095 | break; |
| 2096 | default: |
| 2097 | LOG(FATAL) << "Found invalid annotation value type while building annotation array"; |
| 2098 | return false; |
| 2099 | } |
| 2100 | } |
| 2101 | } |
| 2102 | element_object = new_array.Get(); |
| 2103 | set_object = true; |
| 2104 | width = 0; |
| 2105 | } |
| 2106 | break; |
| 2107 | case kDexAnnotationAnnotation: |
| 2108 | if (result_style == kAllRaw) { |
| 2109 | return false; |
| 2110 | } |
| 2111 | element_object = ProcessEncodedAnnotation(klass, &annotation); |
| 2112 | if (element_object == nullptr) { |
| 2113 | return false; |
| 2114 | } |
| 2115 | set_object = true; |
| 2116 | width = 0; |
| 2117 | break; |
| 2118 | case kDexAnnotationNull: |
| 2119 | if (result_style == kAllRaw) { |
| 2120 | annotation_value->value_.SetI(0); |
| 2121 | } else { |
| 2122 | CHECK(element_object == nullptr); |
| 2123 | set_object = true; |
| 2124 | } |
| 2125 | width = 0; |
| 2126 | break; |
| 2127 | default: |
| 2128 | LOG(ERROR) << StringPrintf("Bad annotation element value type 0x%02x", value_type); |
| 2129 | return false; |
| 2130 | } |
| 2131 | |
| 2132 | annotation += width; |
| 2133 | *annotation_ptr = annotation; |
| 2134 | |
| 2135 | if (result_style == kAllObjects && primitive_type != Primitive::kPrimVoid) { |
| 2136 | element_object = BoxPrimitive(primitive_type, annotation_value->value_); |
| 2137 | set_object = true; |
| 2138 | } |
| 2139 | |
| 2140 | if (set_object) { |
| 2141 | annotation_value->value_.SetL(element_object); |
| 2142 | } |
| 2143 | |
| 2144 | return true; |
| 2145 | } |
| 2146 | |
| 2147 | mirror::Object* DexFile::ProcessEncodedAnnotation(Handle<mirror::Class> klass, |
| 2148 | const uint8_t** annotation) const { |
| 2149 | uint32_t type_index = DecodeUnsignedLeb128(annotation); |
| 2150 | uint32_t size = DecodeUnsignedLeb128(annotation); |
| 2151 | |
| 2152 | Thread* self = Thread::Current(); |
| 2153 | ScopedObjectAccessUnchecked soa(self); |
| 2154 | StackHandleScope<2> hs(self); |
| 2155 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 2156 | Handle<mirror::Class> annotation_class(hs.NewHandle( |
| 2157 | class_linker->ResolveType(klass->GetDexFile(), type_index, klass.Get()))); |
| 2158 | if (annotation_class.Get() == nullptr) { |
| 2159 | LOG(INFO) << "Unable to resolve " << PrettyClass(klass.Get()) << " annotation class " |
| 2160 | << type_index; |
| 2161 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 2162 | Thread::Current()->ClearException(); |
| 2163 | return nullptr; |
| 2164 | } |
| 2165 | |
| 2166 | mirror::Class* annotation_member_class = |
| 2167 | soa.Decode<mirror::Class*>(WellKnownClasses::libcore_reflect_AnnotationMember); |
| 2168 | mirror::Class* annotation_member_array_class = |
| 2169 | class_linker->FindArrayClass(self, &annotation_member_class); |
Jeff Hao | 2a5892f | 2015-08-31 15:00:40 -0700 | [diff] [blame] | 2170 | if (annotation_member_array_class == nullptr) { |
| 2171 | return nullptr; |
| 2172 | } |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 2173 | mirror::ObjectArray<mirror::Object>* element_array = nullptr; |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 2174 | if (size > 0) { |
| 2175 | element_array = |
| 2176 | mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_member_array_class, size); |
| 2177 | if (element_array == nullptr) { |
| 2178 | LOG(ERROR) << "Failed to allocate annotation member array (" << size << " elements)"; |
| 2179 | return nullptr; |
| 2180 | } |
| 2181 | } |
| 2182 | |
| 2183 | Handle<mirror::ObjectArray<mirror::Object>> h_element_array(hs.NewHandle(element_array)); |
| 2184 | for (uint32_t i = 0; i < size; ++i) { |
| 2185 | mirror::Object* new_member = CreateAnnotationMember(klass, annotation_class, annotation); |
| 2186 | if (new_member == nullptr) { |
| 2187 | return nullptr; |
| 2188 | } |
| 2189 | h_element_array->SetWithoutChecks<false>(i, new_member); |
| 2190 | } |
| 2191 | |
| 2192 | JValue result; |
| 2193 | ArtMethod* create_annotation_method = |
| 2194 | soa.DecodeMethod(WellKnownClasses::libcore_reflect_AnnotationFactory_createAnnotation); |
| 2195 | uint32_t args[2] = { static_cast<uint32_t>(reinterpret_cast<uintptr_t>(annotation_class.Get())), |
| 2196 | static_cast<uint32_t>(reinterpret_cast<uintptr_t>(h_element_array.Get())) }; |
| 2197 | create_annotation_method->Invoke(self, args, sizeof(args), &result, "LLL"); |
| 2198 | if (self->IsExceptionPending()) { |
| 2199 | LOG(INFO) << "Exception in AnnotationFactory.createAnnotation"; |
| 2200 | return nullptr; |
| 2201 | } |
| 2202 | |
| 2203 | return result.GetL(); |
| 2204 | } |
| 2205 | |
| 2206 | const DexFile::AnnotationItem* DexFile::SearchAnnotationSet(const AnnotationSetItem* annotation_set, |
| 2207 | const char* descriptor, uint32_t visibility) const { |
| 2208 | const AnnotationItem* result = nullptr; |
| 2209 | for (uint32_t i = 0; i < annotation_set->size_; ++i) { |
| 2210 | const AnnotationItem* annotation_item = GetAnnotationItem(annotation_set, i); |
Jeff Hao | 3d08086 | 2016-05-26 18:39:17 -0700 | [diff] [blame] | 2211 | if (!IsVisibilityCompatible(annotation_item->visibility_, visibility)) { |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 2212 | continue; |
| 2213 | } |
| 2214 | const uint8_t* annotation = annotation_item->annotation_; |
| 2215 | uint32_t type_index = DecodeUnsignedLeb128(&annotation); |
| 2216 | |
| 2217 | if (strcmp(descriptor, StringByTypeIdx(type_index)) == 0) { |
| 2218 | result = annotation_item; |
| 2219 | break; |
| 2220 | } |
| 2221 | } |
| 2222 | return result; |
| 2223 | } |
| 2224 | |
| 2225 | const uint8_t* DexFile::SearchEncodedAnnotation(const uint8_t* annotation, const char* name) const { |
| 2226 | DecodeUnsignedLeb128(&annotation); // unused type_index |
| 2227 | uint32_t size = DecodeUnsignedLeb128(&annotation); |
| 2228 | |
| 2229 | while (size != 0) { |
| 2230 | uint32_t element_name_index = DecodeUnsignedLeb128(&annotation); |
| 2231 | const char* element_name = GetStringData(GetStringId(element_name_index)); |
| 2232 | if (strcmp(name, element_name) == 0) { |
| 2233 | return annotation; |
| 2234 | } |
| 2235 | SkipAnnotationValue(&annotation); |
| 2236 | size--; |
| 2237 | } |
| 2238 | return nullptr; |
| 2239 | } |
| 2240 | |
| 2241 | bool DexFile::SkipAnnotationValue(const uint8_t** annotation_ptr) const { |
| 2242 | const uint8_t* annotation = *annotation_ptr; |
| 2243 | uint8_t header_byte = *(annotation++); |
| 2244 | uint8_t value_type = header_byte & kDexAnnotationValueTypeMask; |
| 2245 | uint8_t value_arg = header_byte >> kDexAnnotationValueArgShift; |
| 2246 | int32_t width = value_arg + 1; |
| 2247 | |
| 2248 | switch (value_type) { |
| 2249 | case kDexAnnotationByte: |
| 2250 | case kDexAnnotationShort: |
| 2251 | case kDexAnnotationChar: |
| 2252 | case kDexAnnotationInt: |
| 2253 | case kDexAnnotationLong: |
| 2254 | case kDexAnnotationFloat: |
| 2255 | case kDexAnnotationDouble: |
| 2256 | case kDexAnnotationString: |
| 2257 | case kDexAnnotationType: |
| 2258 | case kDexAnnotationMethod: |
| 2259 | case kDexAnnotationField: |
| 2260 | case kDexAnnotationEnum: |
| 2261 | break; |
| 2262 | case kDexAnnotationArray: |
| 2263 | { |
| 2264 | uint32_t size = DecodeUnsignedLeb128(&annotation); |
| 2265 | while (size--) { |
| 2266 | if (!SkipAnnotationValue(&annotation)) { |
| 2267 | return false; |
| 2268 | } |
| 2269 | } |
| 2270 | width = 0; |
| 2271 | break; |
| 2272 | } |
| 2273 | case kDexAnnotationAnnotation: |
| 2274 | { |
| 2275 | DecodeUnsignedLeb128(&annotation); // unused type_index |
| 2276 | uint32_t size = DecodeUnsignedLeb128(&annotation); |
| 2277 | while (size--) { |
| 2278 | DecodeUnsignedLeb128(&annotation); // unused element_name_index |
| 2279 | if (!SkipAnnotationValue(&annotation)) { |
| 2280 | return false; |
| 2281 | } |
| 2282 | } |
| 2283 | width = 0; |
| 2284 | break; |
| 2285 | } |
| 2286 | case kDexAnnotationBoolean: |
| 2287 | case kDexAnnotationNull: |
| 2288 | width = 0; |
| 2289 | break; |
| 2290 | default: |
| 2291 | LOG(FATAL) << StringPrintf("Bad annotation element value byte 0x%02x", value_type); |
| 2292 | return false; |
| 2293 | } |
| 2294 | |
| 2295 | annotation += width; |
| 2296 | *annotation_ptr = annotation; |
| 2297 | return true; |
| 2298 | } |
| 2299 | |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 2300 | std::ostream& operator<<(std::ostream& os, const DexFile& dex_file) { |
| 2301 | os << StringPrintf("[DexFile: %s dex-checksum=%08x location-checksum=%08x %p-%p]", |
| 2302 | dex_file.GetLocation().c_str(), |
| 2303 | dex_file.GetHeader().checksum_, dex_file.GetLocationChecksum(), |
| 2304 | dex_file.Begin(), dex_file.Begin() + dex_file.Size()); |
| 2305 | return os; |
| 2306 | } |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 2307 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 2308 | std::string Signature::ToString() const { |
| 2309 | if (dex_file_ == nullptr) { |
| 2310 | CHECK(proto_id_ == nullptr); |
| 2311 | return "<no signature>"; |
| 2312 | } |
| 2313 | const DexFile::TypeList* params = dex_file_->GetProtoParameters(*proto_id_); |
| 2314 | std::string result; |
| 2315 | if (params == nullptr) { |
| 2316 | result += "()"; |
| 2317 | } else { |
| 2318 | result += "("; |
| 2319 | for (uint32_t i = 0; i < params->Size(); ++i) { |
| 2320 | result += dex_file_->StringByTypeIdx(params->GetTypeItem(i).type_idx_); |
| 2321 | } |
| 2322 | result += ")"; |
| 2323 | } |
| 2324 | result += dex_file_->StringByTypeIdx(proto_id_->return_type_idx_); |
| 2325 | return result; |
| 2326 | } |
| 2327 | |
Vladimir Marko | d9cffea | 2013-11-25 15:08:02 +0000 | [diff] [blame] | 2328 | bool Signature::operator==(const StringPiece& rhs) const { |
| 2329 | if (dex_file_ == nullptr) { |
| 2330 | return false; |
| 2331 | } |
| 2332 | StringPiece tail(rhs); |
| 2333 | if (!tail.starts_with("(")) { |
| 2334 | return false; // Invalid signature |
| 2335 | } |
| 2336 | tail.remove_prefix(1); // "("; |
| 2337 | const DexFile::TypeList* params = dex_file_->GetProtoParameters(*proto_id_); |
| 2338 | if (params != nullptr) { |
| 2339 | for (uint32_t i = 0; i < params->Size(); ++i) { |
| 2340 | StringPiece param(dex_file_->StringByTypeIdx(params->GetTypeItem(i).type_idx_)); |
| 2341 | if (!tail.starts_with(param)) { |
| 2342 | return false; |
| 2343 | } |
| 2344 | tail.remove_prefix(param.length()); |
| 2345 | } |
| 2346 | } |
| 2347 | if (!tail.starts_with(")")) { |
| 2348 | return false; |
| 2349 | } |
| 2350 | tail.remove_prefix(1); // ")"; |
| 2351 | return tail == dex_file_->StringByTypeIdx(proto_id_->return_type_idx_); |
| 2352 | } |
| 2353 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 2354 | std::ostream& operator<<(std::ostream& os, const Signature& sig) { |
| 2355 | return os << sig.ToString(); |
| 2356 | } |
| 2357 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2358 | // Decodes the header section from the class data bytes. |
| 2359 | void ClassDataItemIterator::ReadClassDataHeader() { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 2360 | CHECK(ptr_pos_ != nullptr); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2361 | header_.static_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_); |
| 2362 | header_.instance_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_); |
| 2363 | header_.direct_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_); |
| 2364 | header_.virtual_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_); |
| 2365 | } |
| 2366 | |
| 2367 | void ClassDataItemIterator::ReadClassDataField() { |
| 2368 | field_.field_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_); |
| 2369 | field_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_); |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 2370 | // The user of the iterator is responsible for checking if there |
| 2371 | // are unordered or duplicate indexes. |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2372 | } |
| 2373 | |
| 2374 | void ClassDataItemIterator::ReadClassDataMethod() { |
| 2375 | method_.method_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_); |
| 2376 | method_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_); |
| 2377 | method_.code_off_ = DecodeUnsignedLeb128(&ptr_pos_); |
Brian Carlstrom | 68adbe4 | 2012-05-11 17:18:08 -0700 | [diff] [blame] | 2378 | if (last_idx_ != 0 && method_.method_idx_delta_ == 0) { |
Andreas Gampe | 4fdbba0 | 2014-06-19 20:24:22 -0700 | [diff] [blame] | 2379 | LOG(WARNING) << "Duplicate method in " << dex_file_.GetLocation(); |
Brian Carlstrom | 6f29d0e | 2012-05-11 15:50:29 -0700 | [diff] [blame] | 2380 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2381 | } |
| 2382 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 2383 | EncodedStaticFieldValueIterator::EncodedStaticFieldValueIterator( |
Shinichiro Hamaji | 82863f0 | 2015-11-05 16:51:33 +0900 | [diff] [blame] | 2384 | const DexFile& dex_file, |
| 2385 | const DexFile::ClassDef& class_def) |
Shinichiro Hamaji | 50a2f8d | 2015-12-11 09:45:28 +0900 | [diff] [blame] | 2386 | : EncodedStaticFieldValueIterator(dex_file, |
| 2387 | nullptr, |
| 2388 | nullptr, |
| 2389 | nullptr, |
| 2390 | class_def, |
| 2391 | -1, |
| 2392 | kByte) { |
Shinichiro Hamaji | 82863f0 | 2015-11-05 16:51:33 +0900 | [diff] [blame] | 2393 | } |
| 2394 | |
| 2395 | EncodedStaticFieldValueIterator::EncodedStaticFieldValueIterator( |
Shinichiro Hamaji | 50a2f8d | 2015-12-11 09:45:28 +0900 | [diff] [blame] | 2396 | const DexFile& dex_file, |
| 2397 | Handle<mirror::DexCache>* dex_cache, |
| 2398 | Handle<mirror::ClassLoader>* class_loader, |
| 2399 | ClassLinker* linker, |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 2400 | const DexFile::ClassDef& class_def) |
Shinichiro Hamaji | 50a2f8d | 2015-12-11 09:45:28 +0900 | [diff] [blame] | 2401 | : EncodedStaticFieldValueIterator(dex_file, |
| 2402 | dex_cache, class_loader, |
| 2403 | linker, |
| 2404 | class_def, |
| 2405 | -1, |
| 2406 | kByte) { |
| 2407 | DCHECK(dex_cache_ != nullptr); |
| 2408 | DCHECK(class_loader_ != nullptr); |
| 2409 | } |
| 2410 | |
| 2411 | EncodedStaticFieldValueIterator::EncodedStaticFieldValueIterator( |
| 2412 | const DexFile& dex_file, |
| 2413 | Handle<mirror::DexCache>* dex_cache, |
| 2414 | Handle<mirror::ClassLoader>* class_loader, |
| 2415 | ClassLinker* linker, |
| 2416 | const DexFile::ClassDef& class_def, |
| 2417 | size_t pos, |
| 2418 | ValueType type) |
Shinichiro Hamaji | 82863f0 | 2015-11-05 16:51:33 +0900 | [diff] [blame] | 2419 | : dex_file_(dex_file), |
| 2420 | dex_cache_(dex_cache), |
| 2421 | class_loader_(class_loader), |
| 2422 | linker_(linker), |
| 2423 | array_size_(), |
Shinichiro Hamaji | 50a2f8d | 2015-12-11 09:45:28 +0900 | [diff] [blame] | 2424 | pos_(pos), |
| 2425 | type_(type) { |
| 2426 | ptr_ = dex_file.GetEncodedStaticFieldValuesArray(class_def); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 2427 | if (ptr_ == nullptr) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2428 | array_size_ = 0; |
| 2429 | } else { |
| 2430 | array_size_ = DecodeUnsignedLeb128(&ptr_); |
| 2431 | } |
| 2432 | if (array_size_ > 0) { |
| 2433 | Next(); |
| 2434 | } |
| 2435 | } |
| 2436 | |
| 2437 | void EncodedStaticFieldValueIterator::Next() { |
| 2438 | pos_++; |
| 2439 | if (pos_ >= array_size_) { |
| 2440 | return; |
| 2441 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2442 | uint8_t value_type = *ptr_++; |
| 2443 | uint8_t value_arg = value_type >> kEncodedValueArgShift; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2444 | size_t width = value_arg + 1; // assume and correct later |
Brian Carlstrom | 88f3654 | 2012-10-16 23:24:21 -0700 | [diff] [blame] | 2445 | type_ = static_cast<ValueType>(value_type & kEncodedValueTypeMask); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2446 | switch (type_) { |
| 2447 | case kBoolean: |
| 2448 | jval_.i = (value_arg != 0) ? 1 : 0; |
| 2449 | width = 0; |
| 2450 | break; |
| 2451 | case kByte: |
| 2452 | jval_.i = ReadSignedInt(ptr_, value_arg); |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 2453 | CHECK(IsInt<8>(jval_.i)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2454 | break; |
| 2455 | case kShort: |
| 2456 | jval_.i = ReadSignedInt(ptr_, value_arg); |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 2457 | CHECK(IsInt<16>(jval_.i)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2458 | break; |
| 2459 | case kChar: |
| 2460 | jval_.i = ReadUnsignedInt(ptr_, value_arg, false); |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 2461 | CHECK(IsUint<16>(jval_.i)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2462 | break; |
| 2463 | case kInt: |
| 2464 | jval_.i = ReadSignedInt(ptr_, value_arg); |
| 2465 | break; |
| 2466 | case kLong: |
| 2467 | jval_.j = ReadSignedLong(ptr_, value_arg); |
| 2468 | break; |
| 2469 | case kFloat: |
| 2470 | jval_.i = ReadUnsignedInt(ptr_, value_arg, true); |
| 2471 | break; |
| 2472 | case kDouble: |
| 2473 | jval_.j = ReadUnsignedLong(ptr_, value_arg, true); |
| 2474 | break; |
| 2475 | case kString: |
| 2476 | case kType: |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2477 | jval_.i = ReadUnsignedInt(ptr_, value_arg, false); |
| 2478 | break; |
| 2479 | case kField: |
Brian Carlstrom | 88f3654 | 2012-10-16 23:24:21 -0700 | [diff] [blame] | 2480 | case kMethod: |
| 2481 | case kEnum: |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2482 | case kArray: |
| 2483 | case kAnnotation: |
| 2484 | UNIMPLEMENTED(FATAL) << ": type " << type_; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 2485 | UNREACHABLE(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2486 | case kNull: |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 2487 | jval_.l = nullptr; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2488 | width = 0; |
| 2489 | break; |
| 2490 | default: |
| 2491 | LOG(FATAL) << "Unreached"; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 2492 | UNREACHABLE(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2493 | } |
| 2494 | ptr_ += width; |
| 2495 | } |
| 2496 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 2497 | template<bool kTransactionActive> |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2498 | void EncodedStaticFieldValueIterator::ReadValueToField(ArtField* field) const { |
Shinichiro Hamaji | 82863f0 | 2015-11-05 16:51:33 +0900 | [diff] [blame] | 2499 | DCHECK(dex_cache_ != nullptr); |
| 2500 | DCHECK(class_loader_ != nullptr); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2501 | switch (type_) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 2502 | case kBoolean: field->SetBoolean<kTransactionActive>(field->GetDeclaringClass(), jval_.z); |
| 2503 | break; |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 2504 | case kByte: field->SetByte<kTransactionActive>(field->GetDeclaringClass(), jval_.b); break; |
| 2505 | case kShort: field->SetShort<kTransactionActive>(field->GetDeclaringClass(), jval_.s); break; |
| 2506 | case kChar: field->SetChar<kTransactionActive>(field->GetDeclaringClass(), jval_.c); break; |
| 2507 | case kInt: field->SetInt<kTransactionActive>(field->GetDeclaringClass(), jval_.i); break; |
| 2508 | case kLong: field->SetLong<kTransactionActive>(field->GetDeclaringClass(), jval_.j); break; |
| 2509 | case kFloat: field->SetFloat<kTransactionActive>(field->GetDeclaringClass(), jval_.f); break; |
| 2510 | case kDouble: field->SetDouble<kTransactionActive>(field->GetDeclaringClass(), jval_.d); break; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 2511 | case kNull: field->SetObject<kTransactionActive>(field->GetDeclaringClass(), nullptr); break; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2512 | case kString: { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2513 | mirror::String* resolved = linker_->ResolveString(dex_file_, jval_.i, *dex_cache_); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 2514 | field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2515 | break; |
| 2516 | } |
Brian Carlstrom | 88f3654 | 2012-10-16 23:24:21 -0700 | [diff] [blame] | 2517 | case kType: { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2518 | mirror::Class* resolved = linker_->ResolveType(dex_file_, jval_.i, *dex_cache_, |
| 2519 | *class_loader_); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 2520 | field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved); |
Brian Carlstrom | 88f3654 | 2012-10-16 23:24:21 -0700 | [diff] [blame] | 2521 | break; |
| 2522 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2523 | default: UNIMPLEMENTED(FATAL) << ": type " << type_; |
| 2524 | } |
| 2525 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2526 | template void EncodedStaticFieldValueIterator::ReadValueToField<true>(ArtField* field) const; |
| 2527 | template void EncodedStaticFieldValueIterator::ReadValueToField<false>(ArtField* field) const; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2528 | |
| 2529 | CatchHandlerIterator::CatchHandlerIterator(const DexFile::CodeItem& code_item, uint32_t address) { |
| 2530 | handler_.address_ = -1; |
| 2531 | int32_t offset = -1; |
| 2532 | |
| 2533 | // Short-circuit the overwhelmingly common cases. |
| 2534 | switch (code_item.tries_size_) { |
| 2535 | case 0: |
| 2536 | break; |
| 2537 | case 1: { |
| 2538 | const DexFile::TryItem* tries = DexFile::GetTryItems(code_item, 0); |
| 2539 | uint32_t start = tries->start_addr_; |
| 2540 | if (address >= start) { |
| 2541 | uint32_t end = start + tries->insn_count_; |
| 2542 | if (address < end) { |
| 2543 | offset = tries->handler_off_; |
| 2544 | } |
| 2545 | } |
| 2546 | break; |
| 2547 | } |
| 2548 | default: |
Ian Rogers | dbbc99d | 2013-04-18 16:51:54 -0700 | [diff] [blame] | 2549 | offset = DexFile::FindCatchHandlerOffset(code_item, address); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2550 | } |
Logan Chien | 736df02 | 2012-04-27 16:25:57 +0800 | [diff] [blame] | 2551 | Init(code_item, offset); |
| 2552 | } |
| 2553 | |
| 2554 | CatchHandlerIterator::CatchHandlerIterator(const DexFile::CodeItem& code_item, |
| 2555 | const DexFile::TryItem& try_item) { |
| 2556 | handler_.address_ = -1; |
| 2557 | Init(code_item, try_item.handler_off_); |
| 2558 | } |
| 2559 | |
| 2560 | void CatchHandlerIterator::Init(const DexFile::CodeItem& code_item, |
| 2561 | int32_t offset) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2562 | if (offset >= 0) { |
Logan Chien | 736df02 | 2012-04-27 16:25:57 +0800 | [diff] [blame] | 2563 | Init(DexFile::GetCatchHandlerData(code_item, offset)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2564 | } else { |
| 2565 | // Not found, initialize as empty |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 2566 | current_data_ = nullptr; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2567 | remaining_count_ = -1; |
| 2568 | catch_all_ = false; |
| 2569 | DCHECK(!HasNext()); |
| 2570 | } |
| 2571 | } |
| 2572 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2573 | void CatchHandlerIterator::Init(const uint8_t* handler_data) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2574 | current_data_ = handler_data; |
| 2575 | remaining_count_ = DecodeSignedLeb128(¤t_data_); |
| 2576 | |
| 2577 | // If remaining_count_ is non-positive, then it is the negative of |
| 2578 | // the number of catch types, and the catches are followed by a |
| 2579 | // catch-all handler. |
| 2580 | if (remaining_count_ <= 0) { |
| 2581 | catch_all_ = true; |
| 2582 | remaining_count_ = -remaining_count_; |
| 2583 | } else { |
| 2584 | catch_all_ = false; |
| 2585 | } |
| 2586 | Next(); |
| 2587 | } |
| 2588 | |
| 2589 | void CatchHandlerIterator::Next() { |
| 2590 | if (remaining_count_ > 0) { |
| 2591 | handler_.type_idx_ = DecodeUnsignedLeb128(¤t_data_); |
| 2592 | handler_.address_ = DecodeUnsignedLeb128(¤t_data_); |
| 2593 | remaining_count_--; |
| 2594 | return; |
| 2595 | } |
| 2596 | |
| 2597 | if (catch_all_) { |
| 2598 | handler_.type_idx_ = DexFile::kDexNoIndex16; |
| 2599 | handler_.address_ = DecodeUnsignedLeb128(¤t_data_); |
| 2600 | catch_all_ = false; |
| 2601 | return; |
| 2602 | } |
| 2603 | |
| 2604 | // no more handler |
| 2605 | remaining_count_ = -1; |
| 2606 | } |
| 2607 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 2608 | } // namespace art |