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 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 19 | #include <limits.h> |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 20 | #include <stdio.h> |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 21 | #include <stdlib.h> |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 22 | #include <string.h> |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 23 | #include <zlib.h> |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 24 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 25 | #include <memory> |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 26 | #include <sstream> |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 27 | #include <type_traits> |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 28 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 29 | #include "android-base/stringprintf.h" |
| 30 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 31 | #include "base/enums.h" |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 32 | #include "base/leb128.h" |
Andreas Gampe | 5678db5 | 2017-06-08 14:11:18 -0700 | [diff] [blame] | 33 | #include "base/stl_util.h" |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 34 | #include "descriptors_names.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 35 | #include "dex_file-inl.h" |
Mathieu Chartier | 292567e | 2017-10-12 13:24:38 -0700 | [diff] [blame] | 36 | #include "standard_dex_file.h" |
Ian Rogers | a672490 | 2013-09-23 09:23:37 -0700 | [diff] [blame] | 37 | #include "utf-inl.h" |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 38 | |
| 39 | namespace art { |
| 40 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 41 | using android::base::StringPrintf; |
| 42 | |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 43 | static_assert(sizeof(dex::StringIndex) == sizeof(uint32_t), "StringIndex size is wrong"); |
| 44 | static_assert(std::is_trivially_copyable<dex::StringIndex>::value, "StringIndex not trivial"); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 45 | static_assert(sizeof(dex::TypeIndex) == sizeof(uint16_t), "TypeIndex size is wrong"); |
| 46 | static_assert(std::is_trivially_copyable<dex::TypeIndex>::value, "TypeIndex not trivial"); |
| 47 | |
Alex Light | c88a008 | 2018-02-15 17:08:29 -0800 | [diff] [blame] | 48 | void DexFile::UnHideAccessFlags(ClassDataItemIterator& class_it) { |
| 49 | uint8_t* data = const_cast<uint8_t*>(class_it.DataPointer()); |
| 50 | uint32_t new_flag = class_it.GetMemberAccessFlags(); |
| 51 | bool is_method = class_it.IsAtMethod(); |
| 52 | // Go back 1 uleb to start. |
| 53 | data = ReverseSearchUnsignedLeb128(data); |
| 54 | if (is_method) { |
| 55 | // Methods have another uleb field before the access flags |
| 56 | data = ReverseSearchUnsignedLeb128(data); |
| 57 | } |
| 58 | DCHECK_EQ(HiddenApiAccessFlags::RemoveFromDex(DecodeUnsignedLeb128WithoutMovingCursor(data)), |
| 59 | new_flag); |
| 60 | UpdateUnsignedLeb128(data, new_flag); |
| 61 | } |
| 62 | |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 63 | uint32_t DexFile::CalculateChecksum() const { |
Mathieu Chartier | 2c4b084 | 2017-12-13 11:49:51 -0800 | [diff] [blame] | 64 | return CalculateChecksum(Begin(), Size()); |
| 65 | } |
| 66 | |
| 67 | uint32_t DexFile::CalculateChecksum(const uint8_t* begin, size_t size) { |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 68 | const uint32_t non_sum_bytes = OFFSETOF_MEMBER(DexFile::Header, signature_); |
| 69 | return ChecksumMemoryRange(begin + non_sum_bytes, size - non_sum_bytes); |
| 70 | } |
| 71 | |
| 72 | uint32_t DexFile::ChecksumMemoryRange(const uint8_t* begin, size_t size) { |
| 73 | return adler32(adler32(0L, Z_NULL, 0), begin, size); |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 76 | int DexFile::GetPermissions() const { |
David Sehr | 733bd4d | 2017-10-26 10:39:15 -0700 | [diff] [blame] | 77 | CHECK(container_.get() != nullptr); |
| 78 | return container_->GetPermissions(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 81 | bool DexFile::IsReadOnly() const { |
David Sehr | 733bd4d | 2017-10-26 10:39:15 -0700 | [diff] [blame] | 82 | CHECK(container_.get() != nullptr); |
| 83 | return container_->IsReadOnly(); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Brian Carlstrom | e0948e1 | 2013-08-29 09:36:15 -0700 | [diff] [blame] | 86 | bool DexFile::EnableWrite() const { |
David Sehr | 733bd4d | 2017-10-26 10:39:15 -0700 | [diff] [blame] | 87 | CHECK(container_.get() != nullptr); |
| 88 | return container_->EnableWrite(); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 89 | } |
| 90 | |
Brian Carlstrom | e0948e1 | 2013-08-29 09:36:15 -0700 | [diff] [blame] | 91 | bool DexFile::DisableWrite() const { |
David Sehr | 733bd4d | 2017-10-26 10:39:15 -0700 | [diff] [blame] | 92 | CHECK(container_.get() != nullptr); |
| 93 | return container_->DisableWrite(); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 94 | } |
| 95 | |
David Sehr | 733ddb2 | 2016-09-19 15:02:18 -0700 | [diff] [blame] | 96 | DexFile::DexFile(const uint8_t* base, |
| 97 | size_t size, |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 98 | const uint8_t* data_begin, |
| 99 | size_t data_size, |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 100 | const std::string& location, |
| 101 | uint32_t location_checksum, |
David Sehr | 733bd4d | 2017-10-26 10:39:15 -0700 | [diff] [blame] | 102 | const OatDexFile* oat_dex_file, |
George Burgess IV | ccb0019 | 2018-02-17 23:16:39 -0800 | [diff] [blame] | 103 | std::unique_ptr<DexFileContainer> container, |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 104 | bool is_compact_dex) |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 105 | : begin_(base), |
| 106 | size_(size), |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 107 | data_begin_(data_begin), |
| 108 | data_size_(data_size), |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 109 | location_(location), |
| 110 | location_checksum_(location_checksum), |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 111 | header_(reinterpret_cast<const Header*>(base)), |
| 112 | string_ids_(reinterpret_cast<const StringId*>(base + header_->string_ids_off_)), |
| 113 | type_ids_(reinterpret_cast<const TypeId*>(base + header_->type_ids_off_)), |
| 114 | field_ids_(reinterpret_cast<const FieldId*>(base + header_->field_ids_off_)), |
| 115 | method_ids_(reinterpret_cast<const MethodId*>(base + header_->method_ids_off_)), |
| 116 | proto_ids_(reinterpret_cast<const ProtoId*>(base + header_->proto_ids_off_)), |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 117 | class_defs_(reinterpret_cast<const ClassDef*>(base + header_->class_defs_off_)), |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 118 | method_handles_(nullptr), |
| 119 | num_method_handles_(0), |
| 120 | call_site_ids_(nullptr), |
| 121 | num_call_site_ids_(0), |
David Sehr | 733bd4d | 2017-10-26 10:39:15 -0700 | [diff] [blame] | 122 | oat_dex_file_(oat_dex_file), |
George Burgess IV | ccb0019 | 2018-02-17 23:16:39 -0800 | [diff] [blame] | 123 | container_(std::move(container)), |
David Brazdil | 8e1a7cb | 2018-03-27 08:14:25 +0000 | [diff] [blame] | 124 | is_compact_dex_(is_compact_dex), |
| 125 | is_platform_dex_(false) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 126 | CHECK(begin_ != nullptr) << GetLocation(); |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 127 | CHECK_GT(size_, 0U) << GetLocation(); |
Igor Murashkin | 271a0f8 | 2017-02-14 21:14:17 +0000 | [diff] [blame] | 128 | // Check base (=header) alignment. |
| 129 | // Must be 4-byte aligned to avoid undefined behavior when accessing |
| 130 | // any of the sections via a pointer. |
| 131 | CHECK_ALIGNED(begin_, alignof(Header)); |
| 132 | |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 133 | InitializeSectionsFromMapList(); |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Jesse Wilson | 6bf1915 | 2011-09-29 13:12:33 -0400 | [diff] [blame] | 136 | DexFile::~DexFile() { |
Elliott Hughes | 8cef0b8 | 2011-10-11 19:24:00 -0700 | [diff] [blame] | 137 | // We don't call DeleteGlobalRef on dex_object_ because we're only called by DestroyJavaVM, and |
| 138 | // that's only called after DetachCurrentThread, which means there's no JNIEnv. We could |
| 139 | // re-attach, but cleaning up these global references is not obviously useful. It's not as if |
| 140 | // the global reference table is otherwise empty! |
Jesse Wilson | 6bf1915 | 2011-09-29 13:12:33 -0400 | [diff] [blame] | 141 | } |
| 142 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 143 | bool DexFile::Init(std::string* error_msg) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 144 | if (!CheckMagicAndVersion(error_msg)) { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 145 | return false; |
| 146 | } |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 147 | return true; |
| 148 | } |
| 149 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 150 | bool DexFile::CheckMagicAndVersion(std::string* error_msg) const { |
Mathieu Chartier | 7b074bf | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 151 | if (!IsMagicValid()) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 152 | std::ostringstream oss; |
| 153 | oss << "Unrecognized magic number in " << GetLocation() << ":" |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 154 | << " " << header_->magic_[0] |
| 155 | << " " << header_->magic_[1] |
| 156 | << " " << header_->magic_[2] |
| 157 | << " " << header_->magic_[3]; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 158 | *error_msg = oss.str(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 159 | return false; |
| 160 | } |
Mathieu Chartier | 7b074bf | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 161 | if (!IsVersionValid()) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 162 | std::ostringstream oss; |
| 163 | oss << "Unrecognized version number in " << GetLocation() << ":" |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 164 | << " " << header_->magic_[4] |
| 165 | << " " << header_->magic_[5] |
| 166 | << " " << header_->magic_[6] |
| 167 | << " " << header_->magic_[7]; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 168 | *error_msg = oss.str(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 169 | return false; |
| 170 | } |
| 171 | return true; |
| 172 | } |
| 173 | |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 174 | void DexFile::InitializeSectionsFromMapList() { |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 175 | const MapList* map_list = reinterpret_cast<const MapList*>(DataBegin() + header_->map_off_); |
Mathieu Chartier | 9b302bf | 2018-01-25 13:08:08 -0800 | [diff] [blame] | 176 | if (header_->map_off_ == 0 || header_->map_off_ > DataSize()) { |
Jeff Hao | a4cd677 | 2017-04-13 14:36:29 -0700 | [diff] [blame] | 177 | // Bad offset. The dex file verifier runs after this method and will reject the file. |
| 178 | return; |
| 179 | } |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 180 | const size_t count = map_list->size_; |
| 181 | |
| 182 | size_t map_limit = header_->map_off_ + count * sizeof(MapItem); |
Mathieu Chartier | 9b302bf | 2018-01-25 13:08:08 -0800 | [diff] [blame] | 183 | if (header_->map_off_ >= map_limit || map_limit > DataSize()) { |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 184 | // Overflow or out out of bounds. The dex file verifier runs after |
| 185 | // this method and will reject the file as it is malformed. |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | for (size_t i = 0; i < count; ++i) { |
| 190 | const MapItem& map_item = map_list->list_[i]; |
| 191 | if (map_item.type_ == kDexTypeMethodHandleItem) { |
Mathieu Chartier | 9b302bf | 2018-01-25 13:08:08 -0800 | [diff] [blame] | 192 | method_handles_ = reinterpret_cast<const MethodHandleItem*>(Begin() + map_item.offset_); |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 193 | num_method_handles_ = map_item.size_; |
| 194 | } else if (map_item.type_ == kDexTypeCallSiteIdItem) { |
Mathieu Chartier | 9b302bf | 2018-01-25 13:08:08 -0800 | [diff] [blame] | 195 | call_site_ids_ = reinterpret_cast<const CallSiteIdItem*>(Begin() + map_item.offset_); |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 196 | num_call_site_ids_ = map_item.size_; |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
Andreas Gampe | 76ed99d | 2016-03-28 18:31:29 -0700 | [diff] [blame] | 201 | uint32_t DexFile::Header::GetVersion() const { |
Mathieu Chartier | 7b074bf | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 202 | const char* version = reinterpret_cast<const char*>(&magic_[kDexMagicSize]); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 203 | return atoi(version); |
| 204 | } |
| 205 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 206 | const DexFile::ClassDef* DexFile::FindClassDef(dex::TypeIndex type_idx) const { |
David Sehr | 9aa352e | 2016-09-15 18:13:52 -0700 | [diff] [blame] | 207 | size_t num_class_defs = NumClassDefs(); |
Roland Levillain | ab880f4 | 2016-05-12 16:24:36 +0100 | [diff] [blame] | 208 | // Fast path for rare no class defs case. |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 209 | if (num_class_defs == 0) { |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 210 | return nullptr; |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 211 | } |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 212 | for (size_t i = 0; i < num_class_defs; ++i) { |
| 213 | const ClassDef& class_def = GetClassDef(i); |
| 214 | if (class_def.class_idx_ == type_idx) { |
| 215 | return &class_def; |
| 216 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 217 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 218 | return nullptr; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 221 | uint32_t DexFile::FindCodeItemOffset(const DexFile::ClassDef& class_def, |
| 222 | uint32_t method_idx) const { |
| 223 | const uint8_t* class_data = GetClassData(class_def); |
| 224 | CHECK(class_data != nullptr); |
| 225 | ClassDataItemIterator it(*this, class_data); |
Mathieu Chartier | e17cf24 | 2017-06-19 11:05:51 -0700 | [diff] [blame] | 226 | it.SkipAllFields(); |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 227 | while (it.HasNextDirectMethod()) { |
| 228 | if (it.GetMemberIndex() == method_idx) { |
| 229 | return it.GetMethodCodeItemOffset(); |
| 230 | } |
| 231 | it.Next(); |
| 232 | } |
| 233 | while (it.HasNextVirtualMethod()) { |
| 234 | if (it.GetMemberIndex() == method_idx) { |
| 235 | return it.GetMethodCodeItemOffset(); |
| 236 | } |
| 237 | it.Next(); |
| 238 | } |
| 239 | LOG(FATAL) << "Unable to find method " << method_idx; |
| 240 | UNREACHABLE(); |
| 241 | } |
| 242 | |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 243 | const DexFile::FieldId* DexFile::FindFieldId(const DexFile::TypeId& declaring_klass, |
Roland Levillain | ab880f4 | 2016-05-12 16:24:36 +0100 | [diff] [blame] | 244 | const DexFile::StringId& name, |
| 245 | const DexFile::TypeId& type) const { |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 246 | // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 247 | const dex::TypeIndex class_idx = GetIndexForTypeId(declaring_klass); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 248 | const dex::StringIndex name_idx = GetIndexForStringId(name); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 249 | const dex::TypeIndex type_idx = GetIndexForTypeId(type); |
Ian Rogers | f8582c3 | 2013-05-29 16:33:03 -0700 | [diff] [blame] | 250 | int32_t lo = 0; |
| 251 | int32_t hi = NumFieldIds() - 1; |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 252 | while (hi >= lo) { |
Ian Rogers | f8582c3 | 2013-05-29 16:33:03 -0700 | [diff] [blame] | 253 | int32_t mid = (hi + lo) / 2; |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 254 | const DexFile::FieldId& field = GetFieldId(mid); |
| 255 | if (class_idx > field.class_idx_) { |
| 256 | lo = mid + 1; |
| 257 | } else if (class_idx < field.class_idx_) { |
| 258 | hi = mid - 1; |
| 259 | } else { |
| 260 | if (name_idx > field.name_idx_) { |
| 261 | lo = mid + 1; |
| 262 | } else if (name_idx < field.name_idx_) { |
| 263 | hi = mid - 1; |
| 264 | } else { |
| 265 | if (type_idx > field.type_idx_) { |
| 266 | lo = mid + 1; |
| 267 | } else if (type_idx < field.type_idx_) { |
| 268 | hi = mid - 1; |
| 269 | } else { |
| 270 | return &field; |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 275 | return nullptr; |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | const DexFile::MethodId* DexFile::FindMethodId(const DexFile::TypeId& declaring_klass, |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 279 | const DexFile::StringId& name, |
| 280 | const DexFile::ProtoId& signature) const { |
| 281 | // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 282 | const dex::TypeIndex class_idx = GetIndexForTypeId(declaring_klass); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 283 | const dex::StringIndex name_idx = GetIndexForStringId(name); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 284 | const uint16_t proto_idx = GetIndexForProtoId(signature); |
Ian Rogers | f8582c3 | 2013-05-29 16:33:03 -0700 | [diff] [blame] | 285 | int32_t lo = 0; |
| 286 | int32_t hi = NumMethodIds() - 1; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 287 | while (hi >= lo) { |
Ian Rogers | f8582c3 | 2013-05-29 16:33:03 -0700 | [diff] [blame] | 288 | int32_t mid = (hi + lo) / 2; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 289 | const DexFile::MethodId& method = GetMethodId(mid); |
| 290 | if (class_idx > method.class_idx_) { |
| 291 | lo = mid + 1; |
| 292 | } else if (class_idx < method.class_idx_) { |
| 293 | hi = mid - 1; |
| 294 | } else { |
| 295 | if (name_idx > method.name_idx_) { |
| 296 | lo = mid + 1; |
| 297 | } else if (name_idx < method.name_idx_) { |
| 298 | hi = mid - 1; |
| 299 | } else { |
| 300 | if (proto_idx > method.proto_idx_) { |
| 301 | lo = mid + 1; |
| 302 | } else if (proto_idx < method.proto_idx_) { |
| 303 | hi = mid - 1; |
| 304 | } else { |
| 305 | return &method; |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 310 | return nullptr; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 313 | const DexFile::StringId* DexFile::FindStringId(const char* string) const { |
Ian Rogers | f8582c3 | 2013-05-29 16:33:03 -0700 | [diff] [blame] | 314 | int32_t lo = 0; |
| 315 | int32_t hi = NumStringIds() - 1; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 316 | while (hi >= lo) { |
Ian Rogers | f8582c3 | 2013-05-29 16:33:03 -0700 | [diff] [blame] | 317 | int32_t mid = (hi + lo) / 2; |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 318 | const DexFile::StringId& str_id = GetStringId(dex::StringIndex(mid)); |
Ian Rogers | cf5077a | 2013-10-31 12:37:54 -0700 | [diff] [blame] | 319 | const char* str = GetStringData(str_id); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 320 | int compare = CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(string, str); |
| 321 | if (compare > 0) { |
| 322 | lo = mid + 1; |
| 323 | } else if (compare < 0) { |
| 324 | hi = mid - 1; |
| 325 | } else { |
| 326 | return &str_id; |
| 327 | } |
| 328 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 329 | return nullptr; |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 332 | const DexFile::TypeId* DexFile::FindTypeId(const char* string) const { |
| 333 | int32_t lo = 0; |
| 334 | int32_t hi = NumTypeIds() - 1; |
| 335 | while (hi >= lo) { |
| 336 | int32_t mid = (hi + lo) / 2; |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 337 | const TypeId& type_id = GetTypeId(dex::TypeIndex(mid)); |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 338 | const DexFile::StringId& str_id = GetStringId(type_id.descriptor_idx_); |
| 339 | const char* str = GetStringData(str_id); |
| 340 | int compare = CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(string, str); |
| 341 | if (compare > 0) { |
| 342 | lo = mid + 1; |
| 343 | } else if (compare < 0) { |
| 344 | hi = mid - 1; |
| 345 | } else { |
| 346 | return &type_id; |
| 347 | } |
| 348 | } |
| 349 | return nullptr; |
| 350 | } |
| 351 | |
Vladimir Marko | a48aef4 | 2014-12-03 17:53:53 +0000 | [diff] [blame] | 352 | 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] | 353 | int32_t lo = 0; |
| 354 | int32_t hi = NumStringIds() - 1; |
| 355 | while (hi >= lo) { |
| 356 | int32_t mid = (hi + lo) / 2; |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 357 | const DexFile::StringId& str_id = GetStringId(dex::StringIndex(mid)); |
Ian Rogers | cf5077a | 2013-10-31 12:37:54 -0700 | [diff] [blame] | 358 | const char* str = GetStringData(str_id); |
Vladimir Marko | a48aef4 | 2014-12-03 17:53:53 +0000 | [diff] [blame] | 359 | int compare = CompareModifiedUtf8ToUtf16AsCodePointValues(str, string, length); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 360 | if (compare > 0) { |
| 361 | lo = mid + 1; |
| 362 | } else if (compare < 0) { |
| 363 | hi = mid - 1; |
| 364 | } else { |
| 365 | return &str_id; |
| 366 | } |
| 367 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 368 | return nullptr; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 371 | const DexFile::TypeId* DexFile::FindTypeId(dex::StringIndex string_idx) const { |
Ian Rogers | f8582c3 | 2013-05-29 16:33:03 -0700 | [diff] [blame] | 372 | int32_t lo = 0; |
| 373 | int32_t hi = NumTypeIds() - 1; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 374 | while (hi >= lo) { |
Ian Rogers | f8582c3 | 2013-05-29 16:33:03 -0700 | [diff] [blame] | 375 | int32_t mid = (hi + lo) / 2; |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 376 | const TypeId& type_id = GetTypeId(dex::TypeIndex(mid)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 377 | if (string_idx > type_id.descriptor_idx_) { |
| 378 | lo = mid + 1; |
| 379 | } else if (string_idx < type_id.descriptor_idx_) { |
| 380 | hi = mid - 1; |
| 381 | } else { |
| 382 | return &type_id; |
| 383 | } |
| 384 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 385 | return nullptr; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 388 | const DexFile::ProtoId* DexFile::FindProtoId(dex::TypeIndex return_type_idx, |
| 389 | const dex::TypeIndex* signature_type_idxs, |
Vladimir Marko | 5c96e6b | 2013-11-14 15:34:17 +0000 | [diff] [blame] | 390 | uint32_t signature_length) const { |
Ian Rogers | f8582c3 | 2013-05-29 16:33:03 -0700 | [diff] [blame] | 391 | int32_t lo = 0; |
| 392 | int32_t hi = NumProtoIds() - 1; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 393 | while (hi >= lo) { |
Ian Rogers | f8582c3 | 2013-05-29 16:33:03 -0700 | [diff] [blame] | 394 | int32_t mid = (hi + lo) / 2; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 395 | const DexFile::ProtoId& proto = GetProtoId(mid); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 396 | int compare = return_type_idx.index_ - proto.return_type_idx_.index_; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 397 | if (compare == 0) { |
| 398 | DexFileParameterIterator it(*this, proto); |
| 399 | size_t i = 0; |
Vladimir Marko | 5c96e6b | 2013-11-14 15:34:17 +0000 | [diff] [blame] | 400 | while (it.HasNext() && i < signature_length && compare == 0) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 401 | compare = signature_type_idxs[i].index_ - it.GetTypeIdx().index_; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 402 | it.Next(); |
| 403 | i++; |
| 404 | } |
| 405 | if (compare == 0) { |
| 406 | if (it.HasNext()) { |
| 407 | compare = -1; |
Vladimir Marko | 5c96e6b | 2013-11-14 15:34:17 +0000 | [diff] [blame] | 408 | } else if (i < signature_length) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 409 | compare = 1; |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | if (compare > 0) { |
| 414 | lo = mid + 1; |
| 415 | } else if (compare < 0) { |
| 416 | hi = mid - 1; |
| 417 | } else { |
| 418 | return &proto; |
| 419 | } |
| 420 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 421 | return nullptr; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | // Given a signature place the type ids into the given vector |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 425 | bool DexFile::CreateTypeList(const StringPiece& signature, |
| 426 | dex::TypeIndex* return_type_idx, |
| 427 | std::vector<dex::TypeIndex>* param_type_idxs) const { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 428 | if (signature[0] != '(') { |
| 429 | return false; |
| 430 | } |
| 431 | size_t offset = 1; |
| 432 | size_t end = signature.size(); |
| 433 | bool process_return = false; |
| 434 | while (offset < end) { |
Vladimir Marko | e9c36b3 | 2013-11-21 15:49:16 +0000 | [diff] [blame] | 435 | size_t start_offset = offset; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 436 | char c = signature[offset]; |
| 437 | offset++; |
| 438 | if (c == ')') { |
| 439 | process_return = true; |
| 440 | continue; |
| 441 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 442 | while (c == '[') { // process array prefix |
| 443 | if (offset >= end) { // expect some descriptor following [ |
| 444 | return false; |
| 445 | } |
| 446 | c = signature[offset]; |
| 447 | offset++; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 448 | } |
| 449 | if (c == 'L') { // process type descriptors |
| 450 | do { |
| 451 | if (offset >= end) { // unexpected early termination of descriptor |
| 452 | return false; |
| 453 | } |
| 454 | c = signature[offset]; |
| 455 | offset++; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 456 | } while (c != ';'); |
| 457 | } |
Vladimir Marko | e9c36b3 | 2013-11-21 15:49:16 +0000 | [diff] [blame] | 458 | // TODO: avoid creating a std::string just to get a 0-terminated char array |
| 459 | std::string descriptor(signature.data() + start_offset, offset - start_offset); |
Mathieu Chartier | 9507fa2 | 2015-10-29 15:08:57 -0700 | [diff] [blame] | 460 | const DexFile::TypeId* type_id = FindTypeId(descriptor.c_str()); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 461 | if (type_id == nullptr) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 462 | return false; |
| 463 | } |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 464 | dex::TypeIndex type_idx = GetIndexForTypeId(*type_id); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 465 | if (!process_return) { |
| 466 | param_type_idxs->push_back(type_idx); |
| 467 | } else { |
| 468 | *return_type_idx = type_idx; |
| 469 | return offset == end; // return true if the signature had reached a sensible end |
| 470 | } |
| 471 | } |
| 472 | return false; // failed to correctly parse return type |
| 473 | } |
| 474 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 475 | const Signature DexFile::CreateSignature(const StringPiece& signature) const { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 476 | dex::TypeIndex return_type_idx; |
| 477 | std::vector<dex::TypeIndex> param_type_indices; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 478 | bool success = CreateTypeList(signature, &return_type_idx, ¶m_type_indices); |
| 479 | if (!success) { |
| 480 | return Signature::NoSignature(); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 481 | } |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 482 | const ProtoId* proto_id = FindProtoId(return_type_idx, param_type_indices); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 483 | if (proto_id == nullptr) { |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 484 | return Signature::NoSignature(); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 485 | } |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 486 | return Signature(this, *proto_id); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 489 | int32_t DexFile::FindTryItem(const TryItem* try_items, uint32_t tries_size, uint32_t address) { |
| 490 | uint32_t min = 0; |
| 491 | uint32_t max = tries_size; |
| 492 | while (min < max) { |
| 493 | const uint32_t mid = (min + max) / 2; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 494 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 495 | const art::DexFile::TryItem& ti = try_items[mid]; |
| 496 | const uint32_t start = ti.start_addr_; |
| 497 | const uint32_t end = start + ti.insn_count_; |
Ian Rogers | dbbc99d | 2013-04-18 16:51:54 -0700 | [diff] [blame] | 498 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 499 | if (address < start) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 500 | max = mid; |
Ian Rogers | dbbc99d | 2013-04-18 16:51:54 -0700 | [diff] [blame] | 501 | } else if (address >= end) { |
| 502 | min = mid + 1; |
| 503 | } else { // We have a winner! |
| 504 | return mid; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | // No match. |
| 508 | return -1; |
| 509 | } |
| 510 | |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 511 | bool DexFile::LineNumForPcCb(void* raw_context, const PositionInfo& entry) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 512 | LineNumFromPcContext* context = reinterpret_cast<LineNumFromPcContext*>(raw_context); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 513 | |
| 514 | // We know that this callback will be called in |
| 515 | // ascending address order, so keep going until we find |
| 516 | // a match or we've just gone past it. |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 517 | if (entry.address_ > context->address_) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 518 | // The line number from the previous positions callback |
| 519 | // wil be the final result. |
| 520 | return true; |
| 521 | } else { |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 522 | context->line_num_ = entry.line_; |
| 523 | return entry.address_ == context->address_; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 527 | // Read a signed integer. "zwidth" is the zero-based byte count. |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 528 | int32_t DexFile::ReadSignedInt(const uint8_t* ptr, int zwidth) { |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 529 | int32_t val = 0; |
| 530 | for (int i = zwidth; i >= 0; --i) { |
| 531 | val = ((uint32_t)val >> 8) | (((int32_t)*ptr++) << 24); |
| 532 | } |
| 533 | val >>= (3 - zwidth) * 8; |
| 534 | return val; |
| 535 | } |
| 536 | |
| 537 | // Read an unsigned integer. "zwidth" is the zero-based byte count, |
| 538 | // "fill_on_right" indicates which side we want to zero-fill from. |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 539 | uint32_t DexFile::ReadUnsignedInt(const uint8_t* ptr, int zwidth, bool fill_on_right) { |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 540 | uint32_t val = 0; |
| 541 | for (int i = zwidth; i >= 0; --i) { |
| 542 | val = (val >> 8) | (((uint32_t)*ptr++) << 24); |
| 543 | } |
| 544 | if (!fill_on_right) { |
| 545 | val >>= (3 - zwidth) * 8; |
| 546 | } |
| 547 | return val; |
| 548 | } |
| 549 | |
| 550 | // Read a signed long. "zwidth" is the zero-based byte count. |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 551 | int64_t DexFile::ReadSignedLong(const uint8_t* ptr, int zwidth) { |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 552 | int64_t val = 0; |
| 553 | for (int i = zwidth; i >= 0; --i) { |
| 554 | val = ((uint64_t)val >> 8) | (((int64_t)*ptr++) << 56); |
| 555 | } |
| 556 | val >>= (7 - zwidth) * 8; |
| 557 | return val; |
| 558 | } |
| 559 | |
| 560 | // Read an unsigned long. "zwidth" is the zero-based byte count, |
| 561 | // "fill_on_right" indicates which side we want to zero-fill from. |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 562 | uint64_t DexFile::ReadUnsignedLong(const uint8_t* ptr, int zwidth, bool fill_on_right) { |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 563 | uint64_t val = 0; |
| 564 | for (int i = zwidth; i >= 0; --i) { |
| 565 | val = (val >> 8) | (((uint64_t)*ptr++) << 56); |
| 566 | } |
| 567 | if (!fill_on_right) { |
| 568 | val >>= (7 - zwidth) * 8; |
| 569 | } |
| 570 | return val; |
| 571 | } |
| 572 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 573 | std::string DexFile::PrettyMethod(uint32_t method_idx, bool with_signature) const { |
| 574 | if (method_idx >= NumMethodIds()) { |
| 575 | return StringPrintf("<<invalid-method-idx-%d>>", method_idx); |
| 576 | } |
| 577 | const DexFile::MethodId& method_id = GetMethodId(method_idx); |
Vladimir Marko | b8a55f8 | 2017-09-21 16:21:43 +0100 | [diff] [blame] | 578 | std::string result; |
| 579 | const DexFile::ProtoId* proto_id = with_signature ? &GetProtoId(method_id.proto_idx_) : nullptr; |
| 580 | if (with_signature) { |
| 581 | AppendPrettyDescriptor(StringByTypeIdx(proto_id->return_type_idx_), &result); |
| 582 | result += ' '; |
| 583 | } |
| 584 | AppendPrettyDescriptor(GetMethodDeclaringClassDescriptor(method_id), &result); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 585 | result += '.'; |
| 586 | result += GetMethodName(method_id); |
| 587 | if (with_signature) { |
Vladimir Marko | b8a55f8 | 2017-09-21 16:21:43 +0100 | [diff] [blame] | 588 | result += '('; |
| 589 | const DexFile::TypeList* params = GetProtoParameters(*proto_id); |
| 590 | if (params != nullptr) { |
| 591 | const char* separator = ""; |
| 592 | for (uint32_t i = 0u, size = params->Size(); i != size; ++i) { |
| 593 | result += separator; |
| 594 | separator = ", "; |
| 595 | AppendPrettyDescriptor(StringByTypeIdx(params->GetTypeItem(i).type_idx_), &result); |
| 596 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 597 | } |
Vladimir Marko | b8a55f8 | 2017-09-21 16:21:43 +0100 | [diff] [blame] | 598 | result += ')'; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 599 | } |
| 600 | return result; |
| 601 | } |
| 602 | |
| 603 | std::string DexFile::PrettyField(uint32_t field_idx, bool with_type) const { |
| 604 | if (field_idx >= NumFieldIds()) { |
| 605 | return StringPrintf("<<invalid-field-idx-%d>>", field_idx); |
| 606 | } |
| 607 | const DexFile::FieldId& field_id = GetFieldId(field_idx); |
| 608 | std::string result; |
| 609 | if (with_type) { |
| 610 | result += GetFieldTypeDescriptor(field_id); |
| 611 | result += ' '; |
| 612 | } |
Vladimir Marko | b8a55f8 | 2017-09-21 16:21:43 +0100 | [diff] [blame] | 613 | AppendPrettyDescriptor(GetFieldDeclaringClassDescriptor(field_id), &result); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 614 | result += '.'; |
| 615 | result += GetFieldName(field_id); |
| 616 | return result; |
| 617 | } |
| 618 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 619 | std::string DexFile::PrettyType(dex::TypeIndex type_idx) const { |
| 620 | if (type_idx.index_ >= NumTypeIds()) { |
| 621 | return StringPrintf("<<invalid-type-idx-%d>>", type_idx.index_); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 622 | } |
| 623 | const DexFile::TypeId& type_id = GetTypeId(type_idx); |
| 624 | return PrettyDescriptor(GetTypeDescriptor(type_id)); |
| 625 | } |
| 626 | |
Jeff Hao | 3d08086 | 2016-05-26 18:39:17 -0700 | [diff] [blame] | 627 | // Checks that visibility is as expected. Includes special behavior for M and |
| 628 | // before to allow runtime and build visibility when expecting runtime. |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 629 | std::ostream& operator<<(std::ostream& os, const DexFile& dex_file) { |
| 630 | os << StringPrintf("[DexFile: %s dex-checksum=%08x location-checksum=%08x %p-%p]", |
| 631 | dex_file.GetLocation().c_str(), |
| 632 | dex_file.GetHeader().checksum_, dex_file.GetLocationChecksum(), |
| 633 | dex_file.Begin(), dex_file.Begin() + dex_file.Size()); |
| 634 | return os; |
| 635 | } |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 636 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 637 | std::string Signature::ToString() const { |
| 638 | if (dex_file_ == nullptr) { |
| 639 | CHECK(proto_id_ == nullptr); |
| 640 | return "<no signature>"; |
| 641 | } |
| 642 | const DexFile::TypeList* params = dex_file_->GetProtoParameters(*proto_id_); |
| 643 | std::string result; |
| 644 | if (params == nullptr) { |
| 645 | result += "()"; |
| 646 | } else { |
| 647 | result += "("; |
| 648 | for (uint32_t i = 0; i < params->Size(); ++i) { |
| 649 | result += dex_file_->StringByTypeIdx(params->GetTypeItem(i).type_idx_); |
| 650 | } |
| 651 | result += ")"; |
| 652 | } |
| 653 | result += dex_file_->StringByTypeIdx(proto_id_->return_type_idx_); |
| 654 | return result; |
| 655 | } |
| 656 | |
Orion Hodson | 6c4921b | 2016-09-21 15:41:06 +0100 | [diff] [blame] | 657 | uint32_t Signature::GetNumberOfParameters() const { |
| 658 | const DexFile::TypeList* params = dex_file_->GetProtoParameters(*proto_id_); |
| 659 | return (params != nullptr) ? params->Size() : 0; |
| 660 | } |
| 661 | |
| 662 | bool Signature::IsVoid() const { |
| 663 | const char* return_type = dex_file_->GetReturnTypeDescriptor(*proto_id_); |
| 664 | return strcmp(return_type, "V") == 0; |
| 665 | } |
| 666 | |
Vladimir Marko | d9cffea | 2013-11-25 15:08:02 +0000 | [diff] [blame] | 667 | bool Signature::operator==(const StringPiece& rhs) const { |
| 668 | if (dex_file_ == nullptr) { |
| 669 | return false; |
| 670 | } |
| 671 | StringPiece tail(rhs); |
| 672 | if (!tail.starts_with("(")) { |
| 673 | return false; // Invalid signature |
| 674 | } |
| 675 | tail.remove_prefix(1); // "("; |
| 676 | const DexFile::TypeList* params = dex_file_->GetProtoParameters(*proto_id_); |
| 677 | if (params != nullptr) { |
| 678 | for (uint32_t i = 0; i < params->Size(); ++i) { |
| 679 | StringPiece param(dex_file_->StringByTypeIdx(params->GetTypeItem(i).type_idx_)); |
| 680 | if (!tail.starts_with(param)) { |
| 681 | return false; |
| 682 | } |
| 683 | tail.remove_prefix(param.length()); |
| 684 | } |
| 685 | } |
| 686 | if (!tail.starts_with(")")) { |
| 687 | return false; |
| 688 | } |
| 689 | tail.remove_prefix(1); // ")"; |
| 690 | return tail == dex_file_->StringByTypeIdx(proto_id_->return_type_idx_); |
| 691 | } |
| 692 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 693 | std::ostream& operator<<(std::ostream& os, const Signature& sig) { |
| 694 | return os << sig.ToString(); |
| 695 | } |
| 696 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 697 | // Decodes the header section from the class data bytes. |
| 698 | void ClassDataItemIterator::ReadClassDataHeader() { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 699 | CHECK(ptr_pos_ != nullptr); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 700 | header_.static_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_); |
| 701 | header_.instance_fields_size_ = DecodeUnsignedLeb128(&ptr_pos_); |
| 702 | header_.direct_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_); |
| 703 | header_.virtual_methods_size_ = DecodeUnsignedLeb128(&ptr_pos_); |
| 704 | } |
| 705 | |
| 706 | void ClassDataItemIterator::ReadClassDataField() { |
| 707 | field_.field_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_); |
| 708 | field_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_); |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 709 | // The user of the iterator is responsible for checking if there |
| 710 | // are unordered or duplicate indexes. |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | void ClassDataItemIterator::ReadClassDataMethod() { |
| 714 | method_.method_idx_delta_ = DecodeUnsignedLeb128(&ptr_pos_); |
| 715 | method_.access_flags_ = DecodeUnsignedLeb128(&ptr_pos_); |
| 716 | method_.code_off_ = DecodeUnsignedLeb128(&ptr_pos_); |
Brian Carlstrom | 68adbe4 | 2012-05-11 17:18:08 -0700 | [diff] [blame] | 717 | if (last_idx_ != 0 && method_.method_idx_delta_ == 0) { |
Andreas Gampe | 4fdbba0 | 2014-06-19 20:24:22 -0700 | [diff] [blame] | 718 | LOG(WARNING) << "Duplicate method in " << dex_file_.GetLocation(); |
Brian Carlstrom | 6f29d0e | 2012-05-11 15:50:29 -0700 | [diff] [blame] | 719 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 720 | } |
| 721 | |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 722 | EncodedArrayValueIterator::EncodedArrayValueIterator(const DexFile& dex_file, |
| 723 | const uint8_t* array_data) |
Shinichiro Hamaji | 82863f0 | 2015-11-05 16:51:33 +0900 | [diff] [blame] | 724 | : dex_file_(dex_file), |
Shinichiro Hamaji | 82863f0 | 2015-11-05 16:51:33 +0900 | [diff] [blame] | 725 | array_size_(), |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 726 | pos_(-1), |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 727 | ptr_(array_data), |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 728 | type_(kByte) { |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 729 | array_size_ = (ptr_ != nullptr) ? DecodeUnsignedLeb128(&ptr_) : 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 730 | if (array_size_ > 0) { |
| 731 | Next(); |
| 732 | } |
| 733 | } |
| 734 | |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 735 | void EncodedArrayValueIterator::Next() { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 736 | pos_++; |
| 737 | if (pos_ >= array_size_) { |
| 738 | return; |
| 739 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 740 | uint8_t value_type = *ptr_++; |
| 741 | uint8_t value_arg = value_type >> kEncodedValueArgShift; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 742 | size_t width = value_arg + 1; // assume and correct later |
Brian Carlstrom | 88f3654 | 2012-10-16 23:24:21 -0700 | [diff] [blame] | 743 | type_ = static_cast<ValueType>(value_type & kEncodedValueTypeMask); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 744 | switch (type_) { |
| 745 | case kBoolean: |
| 746 | jval_.i = (value_arg != 0) ? 1 : 0; |
| 747 | width = 0; |
| 748 | break; |
| 749 | case kByte: |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 750 | jval_.i = DexFile::ReadSignedInt(ptr_, value_arg); |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 751 | CHECK(IsInt<8>(jval_.i)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 752 | break; |
| 753 | case kShort: |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 754 | jval_.i = DexFile::ReadSignedInt(ptr_, value_arg); |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 755 | CHECK(IsInt<16>(jval_.i)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 756 | break; |
| 757 | case kChar: |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 758 | jval_.i = DexFile::ReadUnsignedInt(ptr_, value_arg, false); |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 759 | CHECK(IsUint<16>(jval_.i)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 760 | break; |
| 761 | case kInt: |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 762 | jval_.i = DexFile::ReadSignedInt(ptr_, value_arg); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 763 | break; |
| 764 | case kLong: |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 765 | jval_.j = DexFile::ReadSignedLong(ptr_, value_arg); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 766 | break; |
| 767 | case kFloat: |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 768 | jval_.i = DexFile::ReadUnsignedInt(ptr_, value_arg, true); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 769 | break; |
| 770 | case kDouble: |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 771 | jval_.j = DexFile::ReadUnsignedLong(ptr_, value_arg, true); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 772 | break; |
| 773 | case kString: |
| 774 | case kType: |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 775 | case kMethodType: |
| 776 | case kMethodHandle: |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 777 | jval_.i = DexFile::ReadUnsignedInt(ptr_, value_arg, false); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 778 | break; |
| 779 | case kField: |
Brian Carlstrom | 88f3654 | 2012-10-16 23:24:21 -0700 | [diff] [blame] | 780 | case kMethod: |
| 781 | case kEnum: |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 782 | case kArray: |
| 783 | case kAnnotation: |
| 784 | UNIMPLEMENTED(FATAL) << ": type " << type_; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 785 | UNREACHABLE(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 786 | case kNull: |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 787 | jval_.l = nullptr; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 788 | width = 0; |
| 789 | break; |
| 790 | default: |
| 791 | LOG(FATAL) << "Unreached"; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 792 | UNREACHABLE(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 793 | } |
| 794 | ptr_ += width; |
| 795 | } |
| 796 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 797 | namespace dex { |
| 798 | |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 799 | std::ostream& operator<<(std::ostream& os, const StringIndex& index) { |
| 800 | os << "StringIndex[" << index.index_ << "]"; |
| 801 | return os; |
| 802 | } |
| 803 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 804 | std::ostream& operator<<(std::ostream& os, const TypeIndex& index) { |
| 805 | os << "TypeIndex[" << index.index_ << "]"; |
| 806 | return os; |
| 807 | } |
| 808 | |
| 809 | } // namespace dex |
| 810 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 811 | } // namespace art |