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