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