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