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