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 | |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_DEX_DEX_FILE_H_ |
| 18 | #define ART_RUNTIME_DEX_DEX_FILE_H_ |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 19 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 20 | #include <memory> |
Elliott Hughes | 0c424cb | 2011-08-26 10:16:25 -0700 | [diff] [blame] | 21 | #include <string> |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 22 | #include <vector> |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 23 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 24 | #include <android-base/logging.h> |
| 25 | |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 26 | #include "base/iteration_range.h" |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 27 | #include "base/macros.h" |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 28 | #include "base/value_object.h" |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 29 | #include "dex_file_types.h" |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 30 | #include "dex_instruction_iterator.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 31 | #include "globals.h" |
Jesse Wilson | 6bf1915 | 2011-09-29 13:12:33 -0400 | [diff] [blame] | 32 | #include "jni.h" |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 33 | #include "modifiers.h" |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 34 | |
| 35 | namespace art { |
| 36 | |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 37 | class CompactDexFile; |
Andreas Gampe | 04c6ab9 | 2017-06-08 21:49:14 -0700 | [diff] [blame] | 38 | enum InvokeType : uint32_t; |
Ian Rogers | 576ca0c | 2014-06-06 15:58:22 -0700 | [diff] [blame] | 39 | class MemMap; |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 40 | class OatDexFile; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 41 | class Signature; |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 42 | class StandardDexFile; |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 43 | class StringPiece; |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 44 | class ZipArchive; |
| 45 | |
David Sehr | 733bd4d | 2017-10-26 10:39:15 -0700 | [diff] [blame] | 46 | // Some instances of DexFile own the storage referred to by DexFile. Clients who create |
| 47 | // such management do so by subclassing Container. |
| 48 | class DexFileContainer { |
| 49 | public: |
| 50 | DexFileContainer() { } |
| 51 | virtual ~DexFileContainer() { } |
| 52 | virtual int GetPermissions() = 0; |
| 53 | virtual bool IsReadOnly() = 0; |
| 54 | virtual bool EnableWrite() = 0; |
| 55 | virtual bool DisableWrite() = 0; |
| 56 | |
| 57 | private: |
| 58 | DISALLOW_COPY_AND_ASSIGN(DexFileContainer); |
| 59 | }; |
| 60 | |
Mathieu Chartier | 7b074bf | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 61 | // Dex file is the API that exposes native dex files (ordinary dex files) and CompactDex. |
| 62 | // Originally, the dex file format used by ART was mostly the same as APKs. The only change was |
| 63 | // quickened opcodes and layout optimizations. |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 64 | // Since ART needs to support both native dex files and CompactDex files, the DexFile interface |
| 65 | // provides an abstraction to facilitate this. |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 66 | class DexFile { |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 67 | public: |
Mathieu Chartier | 7b074bf | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 68 | // Number of bytes in the dex file magic. |
| 69 | static constexpr size_t kDexMagicSize = 4; |
| 70 | static constexpr size_t kDexVersionLen = 4; |
| 71 | |
Roland Levillain | 621b5ea | 2016-05-18 11:41:33 +0100 | [diff] [blame] | 72 | // First Dex format version enforcing class definition ordering rules. |
| 73 | static const uint32_t kClassDefinitionOrderEnforcedVersion = 37; |
| 74 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 75 | static constexpr size_t kSha1DigestSize = 20; |
| 76 | static constexpr uint32_t kDexEndianConstant = 0x12345678; |
Carl Shapiro | 80d4dde | 2011-06-28 16:24:07 -0700 | [diff] [blame] | 77 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 78 | // The value of an invalid index. |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 79 | static const uint16_t kDexNoIndex16 = 0xFFFF; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 80 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 81 | // Raw header_item. |
| 82 | struct Header { |
Mathieu Chartier | f95a75e | 2017-11-03 15:25:52 -0700 | [diff] [blame] | 83 | uint8_t magic_[8] = {}; |
| 84 | uint32_t checksum_ = 0; // See also location_checksum_ |
| 85 | uint8_t signature_[kSha1DigestSize] = {}; |
| 86 | uint32_t file_size_ = 0; // size of entire file |
| 87 | uint32_t header_size_ = 0; // offset to start of next section |
| 88 | uint32_t endian_tag_ = 0; |
| 89 | uint32_t link_size_ = 0; // unused |
| 90 | uint32_t link_off_ = 0; // unused |
| 91 | uint32_t map_off_ = 0; // unused |
| 92 | uint32_t string_ids_size_ = 0; // number of StringIds |
| 93 | uint32_t string_ids_off_ = 0; // file offset of StringIds array |
| 94 | uint32_t type_ids_size_ = 0; // number of TypeIds, we don't support more than 65535 |
| 95 | uint32_t type_ids_off_ = 0; // file offset of TypeIds array |
| 96 | uint32_t proto_ids_size_ = 0; // number of ProtoIds, we don't support more than 65535 |
| 97 | uint32_t proto_ids_off_ = 0; // file offset of ProtoIds array |
| 98 | uint32_t field_ids_size_ = 0; // number of FieldIds |
| 99 | uint32_t field_ids_off_ = 0; // file offset of FieldIds array |
| 100 | uint32_t method_ids_size_ = 0; // number of MethodIds |
| 101 | uint32_t method_ids_off_ = 0; // file offset of MethodIds array |
| 102 | uint32_t class_defs_size_ = 0; // number of ClassDefs |
| 103 | uint32_t class_defs_off_ = 0; // file offset of ClassDef array |
| 104 | uint32_t data_size_ = 0; // size of data section |
| 105 | uint32_t data_off_ = 0; // file offset of data section |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 106 | |
Andreas Gampe | 76ed99d | 2016-03-28 18:31:29 -0700 | [diff] [blame] | 107 | // Decode the dex magic version |
| 108 | uint32_t GetVersion() const; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 109 | }; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 110 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 111 | // Map item type codes. |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 112 | enum MapItemType : uint16_t { // private |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 113 | kDexTypeHeaderItem = 0x0000, |
| 114 | kDexTypeStringIdItem = 0x0001, |
| 115 | kDexTypeTypeIdItem = 0x0002, |
| 116 | kDexTypeProtoIdItem = 0x0003, |
| 117 | kDexTypeFieldIdItem = 0x0004, |
| 118 | kDexTypeMethodIdItem = 0x0005, |
| 119 | kDexTypeClassDefItem = 0x0006, |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 120 | kDexTypeCallSiteIdItem = 0x0007, |
| 121 | kDexTypeMethodHandleItem = 0x0008, |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 122 | kDexTypeMapList = 0x1000, |
| 123 | kDexTypeTypeList = 0x1001, |
| 124 | kDexTypeAnnotationSetRefList = 0x1002, |
| 125 | kDexTypeAnnotationSetItem = 0x1003, |
| 126 | kDexTypeClassDataItem = 0x2000, |
| 127 | kDexTypeCodeItem = 0x2001, |
| 128 | kDexTypeStringDataItem = 0x2002, |
| 129 | kDexTypeDebugInfoItem = 0x2003, |
| 130 | kDexTypeAnnotationItem = 0x2004, |
| 131 | kDexTypeEncodedArrayItem = 0x2005, |
| 132 | kDexTypeAnnotationsDirectoryItem = 0x2006, |
| 133 | }; |
| 134 | |
| 135 | struct MapItem { |
| 136 | uint16_t type_; |
| 137 | uint16_t unused_; |
| 138 | uint32_t size_; |
| 139 | uint32_t offset_; |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 140 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 141 | private: |
| 142 | DISALLOW_COPY_AND_ASSIGN(MapItem); |
| 143 | }; |
| 144 | |
| 145 | struct MapList { |
| 146 | uint32_t size_; |
| 147 | MapItem list_[1]; |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 148 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 149 | private: |
| 150 | DISALLOW_COPY_AND_ASSIGN(MapList); |
| 151 | }; |
| 152 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 153 | // Raw string_id_item. |
| 154 | struct StringId { |
| 155 | uint32_t string_data_off_; // offset in bytes from the base address |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 156 | |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 157 | private: |
| 158 | DISALLOW_COPY_AND_ASSIGN(StringId); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | // Raw type_id_item. |
| 162 | struct TypeId { |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 163 | dex::StringIndex descriptor_idx_; // index into string_ids |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 164 | |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 165 | private: |
| 166 | DISALLOW_COPY_AND_ASSIGN(TypeId); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | // Raw field_id_item. |
| 170 | struct FieldId { |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 171 | dex::TypeIndex class_idx_; // index into type_ids_ array for defining class |
| 172 | dex::TypeIndex type_idx_; // index into type_ids_ array for field type |
| 173 | dex::StringIndex name_idx_; // index into string_ids_ array for field name |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 174 | |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 175 | private: |
| 176 | DISALLOW_COPY_AND_ASSIGN(FieldId); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 177 | }; |
| 178 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 179 | // Raw proto_id_item. |
| 180 | struct ProtoId { |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 181 | dex::StringIndex shorty_idx_; // index into string_ids array for shorty descriptor |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 182 | dex::TypeIndex return_type_idx_; // index into type_ids array for return type |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 183 | uint16_t pad_; // padding = 0 |
| 184 | uint32_t parameters_off_; // file offset to type_list for parameter types |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 185 | |
| 186 | private: |
| 187 | DISALLOW_COPY_AND_ASSIGN(ProtoId); |
| 188 | }; |
| 189 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 190 | // Raw method_id_item. |
| 191 | struct MethodId { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 192 | dex::TypeIndex class_idx_; // index into type_ids_ array for defining class |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 193 | uint16_t proto_idx_; // index into proto_ids_ array for method prototype |
| 194 | dex::StringIndex name_idx_; // index into string_ids_ array for method name |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 195 | |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 196 | private: |
| 197 | DISALLOW_COPY_AND_ASSIGN(MethodId); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 198 | }; |
| 199 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 200 | // Raw class_def_item. |
| 201 | struct ClassDef { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 202 | dex::TypeIndex class_idx_; // index into type_ids_ array for this class |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 203 | uint16_t pad1_; // padding = 0 |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 204 | uint32_t access_flags_; |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 205 | dex::TypeIndex superclass_idx_; // index into type_ids_ array for superclass |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 206 | uint16_t pad2_; // padding = 0 |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 207 | uint32_t interfaces_off_; // file offset to TypeList |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 208 | dex::StringIndex source_file_idx_; // index into string_ids_ for source file name |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 209 | uint32_t annotations_off_; // file offset to annotations_directory_item |
| 210 | uint32_t class_data_off_; // file offset to class_data_item |
| 211 | uint32_t static_values_off_; // file offset to EncodedArray |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 212 | |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 213 | // Returns the valid access flags, that is, Java modifier bits relevant to the ClassDef type |
| 214 | // (class or interface). These are all in the lower 16b and do not contain runtime flags. |
| 215 | uint32_t GetJavaAccessFlags() const { |
| 216 | // Make sure that none of our runtime-only flags are set. |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 217 | static_assert((kAccValidClassFlags & kAccJavaFlagsMask) == kAccValidClassFlags, |
| 218 | "Valid class flags not a subset of Java flags"); |
| 219 | static_assert((kAccValidInterfaceFlags & kAccJavaFlagsMask) == kAccValidInterfaceFlags, |
| 220 | "Valid interface flags not a subset of Java flags"); |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 221 | |
| 222 | if ((access_flags_ & kAccInterface) != 0) { |
| 223 | // Interface. |
| 224 | return access_flags_ & kAccValidInterfaceFlags; |
| 225 | } else { |
| 226 | // Class. |
| 227 | return access_flags_ & kAccValidClassFlags; |
| 228 | } |
| 229 | } |
| 230 | |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 231 | private: |
| 232 | DISALLOW_COPY_AND_ASSIGN(ClassDef); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 233 | }; |
| 234 | |
| 235 | // Raw type_item. |
| 236 | struct TypeItem { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 237 | dex::TypeIndex type_idx_; // index into type_ids section |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 238 | |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 239 | private: |
| 240 | DISALLOW_COPY_AND_ASSIGN(TypeItem); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 241 | }; |
| 242 | |
| 243 | // Raw type_list. |
| 244 | class TypeList { |
| 245 | public: |
| 246 | uint32_t Size() const { |
| 247 | return size_; |
| 248 | } |
| 249 | |
| 250 | const TypeItem& GetTypeItem(uint32_t idx) const { |
Sebastien Hertz | b24bd99 | 2013-08-02 15:19:09 +0200 | [diff] [blame] | 251 | DCHECK_LT(idx, this->size_); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 252 | return this->list_[idx]; |
| 253 | } |
| 254 | |
Andreas Gampe | 31a7a0c | 2014-08-29 16:07:49 -0700 | [diff] [blame] | 255 | // Size in bytes of the part of the list that is common. |
| 256 | static constexpr size_t GetHeaderSize() { |
| 257 | return 4U; |
| 258 | } |
| 259 | |
| 260 | // Size in bytes of the whole type list including all the stored elements. |
| 261 | static constexpr size_t GetListSize(size_t count) { |
| 262 | return GetHeaderSize() + sizeof(TypeItem) * count; |
| 263 | } |
| 264 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 265 | private: |
| 266 | uint32_t size_; // size of the list, in entries |
| 267 | TypeItem list_[1]; // elements of the list |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 268 | DISALLOW_COPY_AND_ASSIGN(TypeList); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 269 | }; |
| 270 | |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 271 | // MethodHandle Types |
| 272 | enum class MethodHandleType : uint16_t { // private |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 273 | kStaticPut = 0x0000, // a setter for a given static field. |
| 274 | kStaticGet = 0x0001, // a getter for a given static field. |
| 275 | kInstancePut = 0x0002, // a setter for a given instance field. |
| 276 | kInstanceGet = 0x0003, // a getter for a given instance field. |
| 277 | kInvokeStatic = 0x0004, // an invoker for a given static method. |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 278 | kInvokeInstance = 0x0005, // invoke_instance : an invoker for a given instance method. This |
| 279 | // can be any non-static method on any class (or interface) except |
| 280 | // for “<init>”. |
| 281 | kInvokeConstructor = 0x0006, // an invoker for a given constructor. |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 282 | kInvokeDirect = 0x0007, // an invoker for a direct (special) method. |
| 283 | kInvokeInterface = 0x0008, // an invoker for an interface method. |
| 284 | kLast = kInvokeInterface |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 285 | }; |
| 286 | |
| 287 | // raw method_handle_item |
| 288 | struct MethodHandleItem { |
| 289 | uint16_t method_handle_type_; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 290 | uint16_t reserved1_; // Reserved for future use. |
| 291 | uint16_t field_or_method_idx_; // Field index for accessors, method index otherwise. |
| 292 | uint16_t reserved2_; // Reserved for future use. |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 293 | private: |
| 294 | DISALLOW_COPY_AND_ASSIGN(MethodHandleItem); |
| 295 | }; |
| 296 | |
| 297 | // raw call_site_id_item |
| 298 | struct CallSiteIdItem { |
| 299 | uint32_t data_off_; // Offset into data section pointing to encoded array items. |
| 300 | private: |
| 301 | DISALLOW_COPY_AND_ASSIGN(CallSiteIdItem); |
| 302 | }; |
| 303 | |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 304 | // Base code_item, compact dex and standard dex have different code item layouts. |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 305 | struct CodeItem { |
Mathieu Chartier | 6238c83 | 2018-01-04 09:55:13 -0800 | [diff] [blame] | 306 | protected: |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 307 | CodeItem() = default; |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 308 | |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 309 | private: |
| 310 | DISALLOW_COPY_AND_ASSIGN(CodeItem); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 311 | }; |
| 312 | |
Carl Shapiro | 2eaa968 | 2011-08-04 19:26:11 -0700 | [diff] [blame] | 313 | // Raw try_item. |
| 314 | struct TryItem { |
| 315 | uint32_t start_addr_; |
| 316 | uint16_t insn_count_; |
| 317 | uint16_t handler_off_; |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 318 | |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 319 | private: |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 320 | TryItem() = default; |
| 321 | friend class DexWriter; |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 322 | DISALLOW_COPY_AND_ASSIGN(TryItem); |
Carl Shapiro | 2eaa968 | 2011-08-04 19:26:11 -0700 | [diff] [blame] | 323 | }; |
| 324 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 325 | // Annotation constants. |
| 326 | enum { |
| 327 | kDexVisibilityBuild = 0x00, /* annotation visibility */ |
| 328 | kDexVisibilityRuntime = 0x01, |
| 329 | kDexVisibilitySystem = 0x02, |
| 330 | |
| 331 | kDexAnnotationByte = 0x00, |
| 332 | kDexAnnotationShort = 0x02, |
| 333 | kDexAnnotationChar = 0x03, |
| 334 | kDexAnnotationInt = 0x04, |
| 335 | kDexAnnotationLong = 0x06, |
| 336 | kDexAnnotationFloat = 0x10, |
| 337 | kDexAnnotationDouble = 0x11, |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 338 | kDexAnnotationMethodType = 0x15, |
| 339 | kDexAnnotationMethodHandle = 0x16, |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 340 | kDexAnnotationString = 0x17, |
| 341 | kDexAnnotationType = 0x18, |
| 342 | kDexAnnotationField = 0x19, |
| 343 | kDexAnnotationMethod = 0x1a, |
| 344 | kDexAnnotationEnum = 0x1b, |
| 345 | kDexAnnotationArray = 0x1c, |
| 346 | kDexAnnotationAnnotation = 0x1d, |
| 347 | kDexAnnotationNull = 0x1e, |
| 348 | kDexAnnotationBoolean = 0x1f, |
| 349 | |
| 350 | kDexAnnotationValueTypeMask = 0x1f, /* low 5 bits */ |
| 351 | kDexAnnotationValueArgShift = 5, |
| 352 | }; |
| 353 | |
| 354 | struct AnnotationsDirectoryItem { |
| 355 | uint32_t class_annotations_off_; |
| 356 | uint32_t fields_size_; |
| 357 | uint32_t methods_size_; |
| 358 | uint32_t parameters_size_; |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 359 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 360 | private: |
| 361 | DISALLOW_COPY_AND_ASSIGN(AnnotationsDirectoryItem); |
| 362 | }; |
| 363 | |
| 364 | struct FieldAnnotationsItem { |
| 365 | uint32_t field_idx_; |
| 366 | uint32_t annotations_off_; |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 367 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 368 | private: |
| 369 | DISALLOW_COPY_AND_ASSIGN(FieldAnnotationsItem); |
| 370 | }; |
| 371 | |
| 372 | struct MethodAnnotationsItem { |
| 373 | uint32_t method_idx_; |
| 374 | uint32_t annotations_off_; |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 375 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 376 | private: |
| 377 | DISALLOW_COPY_AND_ASSIGN(MethodAnnotationsItem); |
| 378 | }; |
| 379 | |
| 380 | struct ParameterAnnotationsItem { |
| 381 | uint32_t method_idx_; |
| 382 | uint32_t annotations_off_; |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 383 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 384 | private: |
| 385 | DISALLOW_COPY_AND_ASSIGN(ParameterAnnotationsItem); |
| 386 | }; |
| 387 | |
| 388 | struct AnnotationSetRefItem { |
| 389 | uint32_t annotations_off_; |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 390 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 391 | private: |
| 392 | DISALLOW_COPY_AND_ASSIGN(AnnotationSetRefItem); |
| 393 | }; |
| 394 | |
| 395 | struct AnnotationSetRefList { |
| 396 | uint32_t size_; |
| 397 | AnnotationSetRefItem list_[1]; |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 398 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 399 | private: |
| 400 | DISALLOW_COPY_AND_ASSIGN(AnnotationSetRefList); |
| 401 | }; |
| 402 | |
| 403 | struct AnnotationSetItem { |
| 404 | uint32_t size_; |
| 405 | uint32_t entries_[1]; |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 406 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 407 | private: |
| 408 | DISALLOW_COPY_AND_ASSIGN(AnnotationSetItem); |
| 409 | }; |
| 410 | |
| 411 | struct AnnotationItem { |
| 412 | uint8_t visibility_; |
| 413 | uint8_t annotation_[1]; |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 414 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 415 | private: |
| 416 | DISALLOW_COPY_AND_ASSIGN(AnnotationItem); |
| 417 | }; |
| 418 | |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 419 | enum AnnotationResultStyle { // private |
| 420 | kAllObjects, |
| 421 | kPrimitivesOrObjects, |
| 422 | kAllRaw |
| 423 | }; |
| 424 | |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 425 | struct AnnotationValue; |
| 426 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 427 | // Closes a .dex file. |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 428 | virtual ~DexFile(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 429 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 430 | const std::string& GetLocation() const { |
| 431 | return location_; |
| 432 | } |
| 433 | |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 434 | // For DexFiles directly from .dex files, this is the checksum from the DexFile::Header. |
| 435 | // For DexFiles opened from a zip files, this will be the ZipEntry CRC32 of classes.dex. |
| 436 | uint32_t GetLocationChecksum() const { |
| 437 | return location_checksum_; |
| 438 | } |
| 439 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 440 | const Header& GetHeader() const { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 441 | DCHECK(header_ != nullptr) << GetLocation(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 442 | return *header_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 445 | // Decode the dex magic version |
Mathieu Chartier | f6e3147 | 2017-12-28 13:32:08 -0800 | [diff] [blame] | 446 | uint32_t GetDexVersion() const { |
Andreas Gampe | 76ed99d | 2016-03-28 18:31:29 -0700 | [diff] [blame] | 447 | return GetHeader().GetVersion(); |
| 448 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 449 | |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 450 | // Returns true if the byte string points to the magic value. |
Mathieu Chartier | 7b074bf | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 451 | virtual bool IsMagicValid() const = 0; |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 452 | |
| 453 | // Returns true if the byte string after the magic is the correct value. |
Mathieu Chartier | 7b074bf | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 454 | virtual bool IsVersionValid() const = 0; |
| 455 | |
Mathieu Chartier | f6e3147 | 2017-12-28 13:32:08 -0800 | [diff] [blame] | 456 | // Returns true if the dex file supports default methods. |
| 457 | virtual bool SupportsDefaultMethods() const = 0; |
| 458 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 459 | // Returns the number of string identifiers in the .dex file. |
| 460 | size_t NumStringIds() const { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 461 | DCHECK(header_ != nullptr) << GetLocation(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 462 | return header_->string_ids_size_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 465 | // Returns the StringId at the specified index. |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 466 | const StringId& GetStringId(dex::StringIndex idx) const { |
| 467 | DCHECK_LT(idx.index_, NumStringIds()) << GetLocation(); |
| 468 | return string_ids_[idx.index_]; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 469 | } |
| 470 | |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 471 | dex::StringIndex GetIndexForStringId(const StringId& string_id) const { |
Brian Carlstrom | 61e513c | 2011-12-09 15:30:06 -0800 | [diff] [blame] | 472 | CHECK_GE(&string_id, string_ids_) << GetLocation(); |
| 473 | CHECK_LT(&string_id, string_ids_ + header_->string_ids_size_) << GetLocation(); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 474 | return dex::StringIndex(&string_id - string_ids_); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | int32_t GetStringLength(const StringId& string_id) const; |
| 478 | |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 479 | // Returns a pointer to the UTF-8 string data referred to by the given string_id as well as the |
| 480 | // length of the string when decoded as a UTF-16 string. Note the UTF-16 length is not the same |
| 481 | // as the string length of the string data. |
| 482 | const char* GetStringDataAndUtf16Length(const StringId& string_id, uint32_t* utf16_length) const; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 483 | |
Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 484 | const char* GetStringData(const StringId& string_id) const; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 485 | |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 486 | // Index version of GetStringDataAndUtf16Length. |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 487 | const char* StringDataAndUtf16LengthByIdx(dex::StringIndex idx, uint32_t* utf16_length) const; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 488 | |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 489 | const char* StringDataByIdx(dex::StringIndex idx) const; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 490 | |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 491 | // Looks up a string id for a given modified utf8 string. |
| 492 | const StringId* FindStringId(const char* string) const; |
| 493 | |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 494 | const TypeId* FindTypeId(const char* string) const; |
| 495 | |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 496 | // Looks up a string id for a given utf16 string. |
Vladimir Marko | a48aef4 | 2014-12-03 17:53:53 +0000 | [diff] [blame] | 497 | const StringId* FindStringId(const uint16_t* string, size_t length) const; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 498 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 499 | // Returns the number of type identifiers in the .dex file. |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 500 | uint32_t NumTypeIds() const { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 501 | DCHECK(header_ != nullptr) << GetLocation(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 502 | return header_->type_ids_size_; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Calin Juravle | 0855688 | 2017-05-26 16:40:45 -0700 | [diff] [blame] | 505 | bool IsTypeIndexValid(dex::TypeIndex idx) const { |
| 506 | return idx.IsValid() && idx.index_ < NumTypeIds(); |
| 507 | } |
| 508 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 509 | // Returns the TypeId at the specified index. |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 510 | const TypeId& GetTypeId(dex::TypeIndex idx) const { |
| 511 | DCHECK_LT(idx.index_, NumTypeIds()) << GetLocation(); |
| 512 | return type_ids_[idx.index_]; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 515 | dex::TypeIndex GetIndexForTypeId(const TypeId& type_id) const { |
Brian Carlstrom | 61e513c | 2011-12-09 15:30:06 -0800 | [diff] [blame] | 516 | CHECK_GE(&type_id, type_ids_) << GetLocation(); |
| 517 | CHECK_LT(&type_id, type_ids_ + header_->type_ids_size_) << GetLocation(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 518 | size_t result = &type_id - type_ids_; |
Brian Carlstrom | 61e513c | 2011-12-09 15:30:06 -0800 | [diff] [blame] | 519 | DCHECK_LT(result, 65536U) << GetLocation(); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 520 | return dex::TypeIndex(static_cast<uint16_t>(result)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | // Get the descriptor string associated with a given type index. |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 524 | const char* StringByTypeIdx(dex::TypeIndex idx, uint32_t* unicode_length) const; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 525 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 526 | const char* StringByTypeIdx(dex::TypeIndex idx) const; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 527 | |
| 528 | // Returns the type descriptor string of a type id. |
Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 529 | const char* GetTypeDescriptor(const TypeId& type_id) const; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 530 | |
| 531 | // Looks up a type for the given string index |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 532 | const TypeId* FindTypeId(dex::StringIndex string_idx) const; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 533 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 534 | // Returns the number of field identifiers in the .dex file. |
| 535 | size_t NumFieldIds() const { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 536 | DCHECK(header_ != nullptr) << GetLocation(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 537 | return header_->field_ids_size_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 540 | // Returns the FieldId at the specified index. |
| 541 | const FieldId& GetFieldId(uint32_t idx) const { |
Sebastien Hertz | b24bd99 | 2013-08-02 15:19:09 +0200 | [diff] [blame] | 542 | DCHECK_LT(idx, NumFieldIds()) << GetLocation(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 543 | return field_ids_[idx]; |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 544 | } |
| 545 | |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 546 | uint32_t GetIndexForFieldId(const FieldId& field_id) const { |
Brian Carlstrom | 61e513c | 2011-12-09 15:30:06 -0800 | [diff] [blame] | 547 | CHECK_GE(&field_id, field_ids_) << GetLocation(); |
| 548 | CHECK_LT(&field_id, field_ids_ + header_->field_ids_size_) << GetLocation(); |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 549 | return &field_id - field_ids_; |
| 550 | } |
| 551 | |
| 552 | // Looks up a field by its declaring class, name and type |
| 553 | const FieldId* FindFieldId(const DexFile::TypeId& declaring_klass, |
| 554 | const DexFile::StringId& name, |
| 555 | const DexFile::TypeId& type) const; |
| 556 | |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 557 | uint32_t FindCodeItemOffset(const DexFile::ClassDef& class_def, |
| 558 | uint32_t dex_method_idx) const; |
| 559 | |
Mathieu Chartier | 6238c83 | 2018-01-04 09:55:13 -0800 | [diff] [blame] | 560 | virtual uint32_t GetCodeItemSize(const DexFile::CodeItem& disk_code_item) const = 0; |
Bharadwaj Kalandhabhatta | 043c908 | 2017-06-06 17:14:12 -0700 | [diff] [blame] | 561 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 562 | // Returns the declaring class descriptor string of a field id. |
| 563 | const char* GetFieldDeclaringClassDescriptor(const FieldId& field_id) const { |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 564 | const DexFile::TypeId& type_id = GetTypeId(field_id.class_idx_); |
| 565 | return GetTypeDescriptor(type_id); |
| 566 | } |
| 567 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 568 | // Returns the class descriptor string of a field id. |
Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 569 | const char* GetFieldTypeDescriptor(const FieldId& field_id) const; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 570 | |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 571 | // Returns the name of a field id. |
Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 572 | const char* GetFieldName(const FieldId& field_id) const; |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 573 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 574 | // Returns the number of method identifiers in the .dex file. |
| 575 | size_t NumMethodIds() const { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 576 | DCHECK(header_ != nullptr) << GetLocation(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 577 | return header_->method_ids_size_; |
| 578 | } |
| 579 | |
| 580 | // Returns the MethodId at the specified index. |
| 581 | const MethodId& GetMethodId(uint32_t idx) const { |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 582 | DCHECK_LT(idx, NumMethodIds()) << GetLocation(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 583 | return method_ids_[idx]; |
| 584 | } |
| 585 | |
| 586 | uint32_t GetIndexForMethodId(const MethodId& method_id) const { |
Brian Carlstrom | 61e513c | 2011-12-09 15:30:06 -0800 | [diff] [blame] | 587 | CHECK_GE(&method_id, method_ids_) << GetLocation(); |
| 588 | CHECK_LT(&method_id, method_ids_ + header_->method_ids_size_) << GetLocation(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 589 | return &method_id - method_ids_; |
| 590 | } |
| 591 | |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 592 | // Looks up a method by its declaring class, name and proto_id |
| 593 | const MethodId* FindMethodId(const DexFile::TypeId& declaring_klass, |
| 594 | const DexFile::StringId& name, |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 595 | const DexFile::ProtoId& signature) const; |
| 596 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 597 | // Returns the declaring class descriptor string of a method id. |
Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 598 | const char* GetMethodDeclaringClassDescriptor(const MethodId& method_id) const; |
Brian Carlstrom | 7540ff4 | 2011-09-04 16:38:46 -0700 | [diff] [blame] | 599 | |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 600 | // Returns the prototype of a method id. |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 601 | const ProtoId& GetMethodPrototype(const MethodId& method_id) const { |
| 602 | return GetProtoId(method_id.proto_idx_); |
| 603 | } |
| 604 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 605 | // Returns a representation of the signature of a method id. |
| 606 | const Signature GetMethodSignature(const MethodId& method_id) const; |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 607 | |
Orion Hodson | b34bb19 | 2016-10-18 17:02:58 +0100 | [diff] [blame] | 608 | // Returns a representation of the signature of a proto id. |
| 609 | const Signature GetProtoSignature(const ProtoId& proto_id) const; |
| 610 | |
Brian Carlstrom | 7540ff4 | 2011-09-04 16:38:46 -0700 | [diff] [blame] | 611 | // Returns the name of a method id. |
Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 612 | const char* GetMethodName(const MethodId& method_id) const; |
Brian Carlstrom | 7540ff4 | 2011-09-04 16:38:46 -0700 | [diff] [blame] | 613 | |
Calin Juravle | 68ad649 | 2015-08-18 17:08:12 +0100 | [diff] [blame] | 614 | // Returns the shorty of a method by its index. |
Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 615 | const char* GetMethodShorty(uint32_t idx) const; |
Calin Juravle | 68ad649 | 2015-08-18 17:08:12 +0100 | [diff] [blame] | 616 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 617 | // Returns the shorty of a method id. |
Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 618 | const char* GetMethodShorty(const MethodId& method_id) const; |
| 619 | const char* GetMethodShorty(const MethodId& method_id, uint32_t* length) const; |
| 620 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 621 | // Returns the number of class definitions in the .dex file. |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 622 | uint32_t NumClassDefs() const { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 623 | DCHECK(header_ != nullptr) << GetLocation(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 624 | return header_->class_defs_size_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | // Returns the ClassDef at the specified index. |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 628 | const ClassDef& GetClassDef(uint16_t idx) const { |
Sebastien Hertz | b24bd99 | 2013-08-02 15:19:09 +0200 | [diff] [blame] | 629 | DCHECK_LT(idx, NumClassDefs()) << GetLocation(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 630 | return class_defs_[idx]; |
| 631 | } |
| 632 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 633 | uint16_t GetIndexForClassDef(const ClassDef& class_def) const { |
Brian Carlstrom | 61e513c | 2011-12-09 15:30:06 -0800 | [diff] [blame] | 634 | CHECK_GE(&class_def, class_defs_) << GetLocation(); |
| 635 | CHECK_LT(&class_def, class_defs_ + header_->class_defs_size_) << GetLocation(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 636 | return &class_def - class_defs_; |
| 637 | } |
| 638 | |
| 639 | // Returns the class descriptor string of a class definition. |
Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 640 | const char* GetClassDescriptor(const ClassDef& class_def) const; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 641 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 642 | // Looks up a class definition by its type index. |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 643 | const ClassDef* FindClassDef(dex::TypeIndex type_idx) const; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 644 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 645 | const TypeList* GetInterfacesList(const ClassDef& class_def) const { |
| 646 | if (class_def.interfaces_off_ == 0) { |
Mathieu Chartier | 73d7b2d | 2017-10-09 16:41:33 -0700 | [diff] [blame] | 647 | return nullptr; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 648 | } |
Mathieu Chartier | 73d7b2d | 2017-10-09 16:41:33 -0700 | [diff] [blame] | 649 | const uint8_t* addr = begin_ + class_def.interfaces_off_; |
| 650 | return reinterpret_cast<const TypeList*>(addr); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 651 | } |
| 652 | |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 653 | uint32_t NumMethodHandles() const { |
| 654 | return num_method_handles_; |
| 655 | } |
| 656 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 657 | const MethodHandleItem& GetMethodHandle(uint32_t idx) const { |
| 658 | CHECK_LT(idx, NumMethodHandles()); |
| 659 | return method_handles_[idx]; |
| 660 | } |
| 661 | |
| 662 | uint32_t NumCallSiteIds() const { |
| 663 | return num_call_site_ids_; |
| 664 | } |
| 665 | |
| 666 | const CallSiteIdItem& GetCallSiteId(uint32_t idx) const { |
| 667 | CHECK_LT(idx, NumCallSiteIds()); |
| 668 | return call_site_ids_[idx]; |
| 669 | } |
| 670 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 671 | // Returns a pointer to the raw memory mapped class_data_item |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 672 | const uint8_t* GetClassData(const ClassDef& class_def) const { |
Mathieu Chartier | 73d7b2d | 2017-10-09 16:41:33 -0700 | [diff] [blame] | 673 | return (class_def.class_data_off_ == 0) ? nullptr : begin_ + class_def.class_data_off_; |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 674 | } |
| 675 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 676 | // |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 677 | const CodeItem* GetCodeItem(const uint32_t code_off) const { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 678 | DCHECK_LT(code_off, size_) << "Code item offset larger then maximum allowed offset"; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 679 | if (code_off == 0) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 680 | return nullptr; // native or abstract method |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 681 | } |
Mathieu Chartier | 73d7b2d | 2017-10-09 16:41:33 -0700 | [diff] [blame] | 682 | const uint8_t* addr = begin_ + code_off; |
| 683 | return reinterpret_cast<const CodeItem*>(addr); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 686 | const char* GetReturnTypeDescriptor(const ProtoId& proto_id) const; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 687 | |
| 688 | // Returns the number of prototype identifiers in the .dex file. |
| 689 | size_t NumProtoIds() const { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 690 | DCHECK(header_ != nullptr) << GetLocation(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 691 | return header_->proto_ids_size_; |
| 692 | } |
| 693 | |
| 694 | // Returns the ProtoId at the specified index. |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 695 | const ProtoId& GetProtoId(uint16_t idx) const { |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 696 | DCHECK_LT(idx, NumProtoIds()) << GetLocation(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 697 | return proto_ids_[idx]; |
| 698 | } |
| 699 | |
| 700 | uint16_t GetIndexForProtoId(const ProtoId& proto_id) const { |
Brian Carlstrom | 61e513c | 2011-12-09 15:30:06 -0800 | [diff] [blame] | 701 | CHECK_GE(&proto_id, proto_ids_) << GetLocation(); |
| 702 | CHECK_LT(&proto_id, proto_ids_ + header_->proto_ids_size_) << GetLocation(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 703 | return &proto_id - proto_ids_; |
| 704 | } |
| 705 | |
| 706 | // Looks up a proto id for a given return type and signature type list |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 707 | const ProtoId* FindProtoId(dex::TypeIndex return_type_idx, |
| 708 | const dex::TypeIndex* signature_type_idxs, |
| 709 | uint32_t signature_length) const; |
| 710 | const ProtoId* FindProtoId(dex::TypeIndex return_type_idx, |
| 711 | const std::vector<dex::TypeIndex>& signature_type_idxs) const { |
Vladimir Marko | 5c96e6b | 2013-11-14 15:34:17 +0000 | [diff] [blame] | 712 | return FindProtoId(return_type_idx, &signature_type_idxs[0], signature_type_idxs.size()); |
| 713 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 714 | |
| 715 | // Given a signature place the type ids into the given vector, returns true on success |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 716 | bool CreateTypeList(const StringPiece& signature, |
| 717 | dex::TypeIndex* return_type_idx, |
| 718 | std::vector<dex::TypeIndex>* param_type_idxs) const; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 719 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 720 | // Create a Signature from the given string signature or return Signature::NoSignature if not |
| 721 | // possible. |
| 722 | const Signature CreateSignature(const StringPiece& signature) const; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 723 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 724 | // Returns the short form method descriptor for the given prototype. |
Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 725 | const char* GetShorty(uint32_t proto_idx) const; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 726 | |
| 727 | const TypeList* GetProtoParameters(const ProtoId& proto_id) const { |
Mathieu Chartier | 73d7b2d | 2017-10-09 16:41:33 -0700 | [diff] [blame] | 728 | return (proto_id.parameters_off_ == 0) |
| 729 | ? nullptr |
| 730 | : reinterpret_cast<const TypeList*>(begin_ + proto_id.parameters_off_); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 731 | } |
| 732 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 733 | const uint8_t* GetEncodedStaticFieldValuesArray(const ClassDef& class_def) const { |
Mathieu Chartier | 73d7b2d | 2017-10-09 16:41:33 -0700 | [diff] [blame] | 734 | return (class_def.static_values_off_ == 0) ? 0 : begin_ + class_def.static_values_off_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 735 | } |
| 736 | |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 737 | const uint8_t* GetCallSiteEncodedValuesArray(const CallSiteIdItem& call_site_id) const { |
| 738 | return begin_ + call_site_id.data_off_; |
| 739 | } |
| 740 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 741 | static const TryItem* GetTryItems(const DexInstructionIterator& code_item_end, uint32_t offset); |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 742 | |
| 743 | // Get the base of the encoded data for the given DexCode. |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 744 | static const uint8_t* GetCatchHandlerData(const DexInstructionIterator& code_item_end, |
| 745 | uint32_t tries_size, |
Mathieu Chartier | 6238c83 | 2018-01-04 09:55:13 -0800 | [diff] [blame] | 746 | uint32_t offset); |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 747 | |
Ian Rogers | dbbc99d | 2013-04-18 16:51:54 -0700 | [diff] [blame] | 748 | // Find which try region is associated with the given address (ie dex pc). Returns -1 if none. |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 749 | static int32_t FindTryItem(const TryItem* try_items, uint32_t tries_size, uint32_t address); |
Ian Rogers | dbbc99d | 2013-04-18 16:51:54 -0700 | [diff] [blame] | 750 | |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 751 | // Get the pointer to the start of the debugging data |
Nicolas Geoffray | 58cc1cb | 2017-11-20 13:27:29 +0000 | [diff] [blame] | 752 | const uint8_t* GetDebugInfoStream(uint32_t debug_info_off) const { |
David Srbecky | 6852942 | 2015-07-07 19:13:29 +0100 | [diff] [blame] | 753 | // Check that the offset is in bounds. |
| 754 | // Note that although the specification says that 0 should be used if there |
| 755 | // is no debug information, some applications incorrectly use 0xFFFFFFFF. |
Mathieu Chartier | 73d7b2d | 2017-10-09 16:41:33 -0700 | [diff] [blame] | 756 | return (debug_info_off == 0 || debug_info_off >= size_) ? nullptr : begin_ + debug_info_off; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 757 | } |
| 758 | |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 759 | struct PositionInfo { |
Mathieu Chartier | 73d7b2d | 2017-10-09 16:41:33 -0700 | [diff] [blame] | 760 | PositionInfo() = default; |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 761 | |
Mathieu Chartier | 73d7b2d | 2017-10-09 16:41:33 -0700 | [diff] [blame] | 762 | uint32_t address_ = 0; // In 16-bit code units. |
| 763 | uint32_t line_ = 0; // Source code line number starting at 1. |
| 764 | const char* source_file_ = nullptr; // nullptr if the file from ClassDef still applies. |
| 765 | bool prologue_end_ = false; |
| 766 | bool epilogue_begin_ = false; |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 767 | }; |
| 768 | |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 769 | struct LocalInfo { |
Mathieu Chartier | 73d7b2d | 2017-10-09 16:41:33 -0700 | [diff] [blame] | 770 | LocalInfo() = default; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 771 | |
Mathieu Chartier | 73d7b2d | 2017-10-09 16:41:33 -0700 | [diff] [blame] | 772 | const char* name_ = nullptr; // E.g., list. It can be nullptr if unknown. |
| 773 | const char* descriptor_ = nullptr; // E.g., Ljava/util/LinkedList; |
| 774 | const char* signature_ = nullptr; // E.g., java.util.LinkedList<java.lang.Integer> |
| 775 | uint32_t start_address_ = 0; // PC location where the local is first defined. |
| 776 | uint32_t end_address_ = 0; // PC location where the local is no longer defined. |
| 777 | uint16_t reg_ = 0; // Dex register which stores the values. |
| 778 | bool is_live_ = false; // Is the local defined and live. |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 779 | }; |
| 780 | |
| 781 | // Callback for "new locals table entry". |
| 782 | typedef void (*DexDebugNewLocalCb)(void* context, const LocalInfo& entry); |
| 783 | |
| 784 | static bool LineNumForPcCb(void* context, const PositionInfo& entry); |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 785 | |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 786 | const AnnotationsDirectoryItem* GetAnnotationsDirectory(const ClassDef& class_def) const { |
Mathieu Chartier | 73d7b2d | 2017-10-09 16:41:33 -0700 | [diff] [blame] | 787 | return (class_def.annotations_off_ == 0) |
| 788 | ? nullptr |
| 789 | : reinterpret_cast<const AnnotationsDirectoryItem*>(begin_ + class_def.annotations_off_); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | const AnnotationSetItem* GetClassAnnotationSet(const AnnotationsDirectoryItem* anno_dir) const { |
Mathieu Chartier | 73d7b2d | 2017-10-09 16:41:33 -0700 | [diff] [blame] | 793 | return (anno_dir->class_annotations_off_ == 0) |
| 794 | ? nullptr |
| 795 | : reinterpret_cast<const AnnotationSetItem*>(begin_ + anno_dir->class_annotations_off_); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | const FieldAnnotationsItem* GetFieldAnnotations(const AnnotationsDirectoryItem* anno_dir) const { |
Mathieu Chartier | 73d7b2d | 2017-10-09 16:41:33 -0700 | [diff] [blame] | 799 | return (anno_dir->fields_size_ == 0) |
| 800 | ? nullptr |
| 801 | : reinterpret_cast<const FieldAnnotationsItem*>(&anno_dir[1]); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | const MethodAnnotationsItem* GetMethodAnnotations(const AnnotationsDirectoryItem* anno_dir) |
| 805 | const { |
| 806 | if (anno_dir->methods_size_ == 0) { |
| 807 | return nullptr; |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 808 | } |
Mathieu Chartier | 73d7b2d | 2017-10-09 16:41:33 -0700 | [diff] [blame] | 809 | // Skip past the header and field annotations. |
| 810 | const uint8_t* addr = reinterpret_cast<const uint8_t*>(&anno_dir[1]); |
| 811 | addr += anno_dir->fields_size_ * sizeof(FieldAnnotationsItem); |
| 812 | return reinterpret_cast<const MethodAnnotationsItem*>(addr); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | const ParameterAnnotationsItem* GetParameterAnnotations(const AnnotationsDirectoryItem* anno_dir) |
| 816 | const { |
| 817 | if (anno_dir->parameters_size_ == 0) { |
| 818 | return nullptr; |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 819 | } |
Mathieu Chartier | 73d7b2d | 2017-10-09 16:41:33 -0700 | [diff] [blame] | 820 | // Skip past the header, field annotations, and method annotations. |
| 821 | const uint8_t* addr = reinterpret_cast<const uint8_t*>(&anno_dir[1]); |
| 822 | addr += anno_dir->fields_size_ * sizeof(FieldAnnotationsItem); |
| 823 | addr += anno_dir->methods_size_ * sizeof(MethodAnnotationsItem); |
| 824 | return reinterpret_cast<const ParameterAnnotationsItem*>(addr); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | const AnnotationSetItem* GetFieldAnnotationSetItem(const FieldAnnotationsItem& anno_item) const { |
| 828 | uint32_t offset = anno_item.annotations_off_; |
Mathieu Chartier | 73d7b2d | 2017-10-09 16:41:33 -0700 | [diff] [blame] | 829 | return (offset == 0) |
| 830 | ? nullptr |
| 831 | : reinterpret_cast<const AnnotationSetItem*>(begin_ + offset); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | const AnnotationSetItem* GetMethodAnnotationSetItem(const MethodAnnotationsItem& anno_item) |
| 835 | const { |
| 836 | uint32_t offset = anno_item.annotations_off_; |
Mathieu Chartier | 73d7b2d | 2017-10-09 16:41:33 -0700 | [diff] [blame] | 837 | return (offset == 0) |
| 838 | ? nullptr |
| 839 | : reinterpret_cast<const AnnotationSetItem*>(begin_ + offset); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | const AnnotationSetRefList* GetParameterAnnotationSetRefList( |
| 843 | const ParameterAnnotationsItem* anno_item) const { |
| 844 | uint32_t offset = anno_item->annotations_off_; |
Mathieu Chartier | 73d7b2d | 2017-10-09 16:41:33 -0700 | [diff] [blame] | 845 | return (offset == 0) |
| 846 | ? nullptr |
| 847 | : reinterpret_cast<const AnnotationSetRefList*>(begin_ + offset); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Mathieu Chartier | 24066ec | 2017-10-21 16:01:08 -0700 | [diff] [blame] | 850 | ALWAYS_INLINE const AnnotationItem* GetAnnotationItemAtOffset(uint32_t offset) const { |
| 851 | DCHECK_LE(offset, Size()); |
Mathieu Chartier | 73d7b2d | 2017-10-09 16:41:33 -0700 | [diff] [blame] | 852 | return (offset == 0) |
| 853 | ? nullptr |
| 854 | : reinterpret_cast<const AnnotationItem*>(begin_ + offset); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Mathieu Chartier | 24066ec | 2017-10-21 16:01:08 -0700 | [diff] [blame] | 857 | const AnnotationItem* GetAnnotationItem(const AnnotationSetItem* set_item, uint32_t index) const { |
| 858 | DCHECK_LE(index, set_item->size_); |
| 859 | return GetAnnotationItemAtOffset(set_item->entries_[index]); |
| 860 | } |
| 861 | |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 862 | const AnnotationSetItem* GetSetRefItemItem(const AnnotationSetRefItem* anno_item) const { |
| 863 | uint32_t offset = anno_item->annotations_off_; |
Mathieu Chartier | 73d7b2d | 2017-10-09 16:41:33 -0700 | [diff] [blame] | 864 | return (offset == 0) |
| 865 | ? nullptr |
| 866 | : reinterpret_cast<const AnnotationSetItem*>(begin_ + offset); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 867 | } |
| 868 | |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 869 | // Debug info opcodes and constants |
| 870 | enum { |
| 871 | DBG_END_SEQUENCE = 0x00, |
| 872 | DBG_ADVANCE_PC = 0x01, |
| 873 | DBG_ADVANCE_LINE = 0x02, |
| 874 | DBG_START_LOCAL = 0x03, |
| 875 | DBG_START_LOCAL_EXTENDED = 0x04, |
| 876 | DBG_END_LOCAL = 0x05, |
| 877 | DBG_RESTART_LOCAL = 0x06, |
| 878 | DBG_SET_PROLOGUE_END = 0x07, |
| 879 | DBG_SET_EPILOGUE_BEGIN = 0x08, |
| 880 | DBG_SET_FILE = 0x09, |
| 881 | DBG_FIRST_SPECIAL = 0x0a, |
| 882 | DBG_LINE_BASE = -4, |
| 883 | DBG_LINE_RANGE = 15, |
| 884 | }; |
| 885 | |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 886 | struct LineNumFromPcContext { |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 887 | LineNumFromPcContext(uint32_t address, uint32_t line_num) |
| 888 | : address_(address), line_num_(line_num) {} |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 889 | uint32_t address_; |
| 890 | uint32_t line_num_; |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 891 | private: |
| 892 | DISALLOW_COPY_AND_ASSIGN(LineNumFromPcContext); |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 893 | }; |
| 894 | |
Roland Levillain | 91d65e0 | 2016-01-19 15:59:16 +0000 | [diff] [blame] | 895 | // Returns false if there is no debugging information or if it cannot be decoded. |
David Sehr | aa6abb0 | 2017-10-12 08:25:11 -0700 | [diff] [blame] | 896 | template<typename NewLocalCallback, typename IndexToStringData, typename TypeIndexToStringData> |
| 897 | static bool DecodeDebugLocalInfo(const uint8_t* stream, |
| 898 | const std::string& location, |
| 899 | const char* declaring_class_descriptor, |
| 900 | const std::vector<const char*>& arg_descriptors, |
| 901 | const std::string& method_name, |
| 902 | bool is_static, |
| 903 | uint16_t registers_size, |
| 904 | uint16_t ins_size, |
| 905 | uint16_t insns_size_in_code_units, |
| 906 | IndexToStringData index_to_string_data, |
| 907 | TypeIndexToStringData type_index_to_string_data, |
| 908 | NewLocalCallback new_local, |
| 909 | void* context); |
| 910 | template<typename NewLocalCallback> |
Mathieu Chartier | 31f4c9f | 2017-12-08 15:46:11 -0800 | [diff] [blame] | 911 | bool DecodeDebugLocalInfo(uint32_t registers_size, |
| 912 | uint32_t ins_size, |
| 913 | uint32_t insns_size_in_code_units, |
Nicolas Geoffray | 58cc1cb | 2017-11-20 13:27:29 +0000 | [diff] [blame] | 914 | uint32_t debug_info_offset, |
David Sehr | aa6abb0 | 2017-10-12 08:25:11 -0700 | [diff] [blame] | 915 | bool is_static, |
| 916 | uint32_t method_idx, |
| 917 | NewLocalCallback new_local, |
| 918 | void* context) const; |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 919 | |
Roland Levillain | 91d65e0 | 2016-01-19 15:59:16 +0000 | [diff] [blame] | 920 | // Returns false if there is no debugging information or if it cannot be decoded. |
David Sehr | aa6abb0 | 2017-10-12 08:25:11 -0700 | [diff] [blame] | 921 | template<typename DexDebugNewPosition, typename IndexToStringData> |
| 922 | static bool DecodeDebugPositionInfo(const uint8_t* stream, |
| 923 | IndexToStringData index_to_string_data, |
| 924 | DexDebugNewPosition position_functor, |
| 925 | void* context); |
| 926 | template<typename DexDebugNewPosition> |
Mathieu Chartier | 31f4c9f | 2017-12-08 15:46:11 -0800 | [diff] [blame] | 927 | bool DecodeDebugPositionInfo(uint32_t debug_info_offset, |
David Sehr | aa6abb0 | 2017-10-12 08:25:11 -0700 | [diff] [blame] | 928 | DexDebugNewPosition position_functor, |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 929 | void* context) const; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 930 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 931 | const char* GetSourceFile(const ClassDef& class_def) const { |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 932 | if (!class_def.source_file_idx_.IsValid()) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 933 | return nullptr; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 934 | } else { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 935 | return StringDataByIdx(class_def.source_file_idx_); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 936 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 937 | } |
| 938 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 939 | int GetPermissions() const; |
Ian Rogers | 1c849e5 | 2012-06-28 14:00:33 -0700 | [diff] [blame] | 940 | |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 941 | bool IsReadOnly() const; |
| 942 | |
Brian Carlstrom | e0948e1 | 2013-08-29 09:36:15 -0700 | [diff] [blame] | 943 | bool EnableWrite() const; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 944 | |
Brian Carlstrom | e0948e1 | 2013-08-29 09:36:15 -0700 | [diff] [blame] | 945 | bool DisableWrite() const; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 946 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 947 | const uint8_t* Begin() const { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 948 | return begin_; |
| 949 | } |
| 950 | |
| 951 | size_t Size() const { |
| 952 | return size_; |
| 953 | } |
| 954 | |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 955 | const OatDexFile* GetOatDexFile() const { |
| 956 | return oat_dex_file_; |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 957 | } |
| 958 | |
Mathieu Chartier | 1b86849 | 2016-11-16 16:22:37 -0800 | [diff] [blame] | 959 | // Used by oat writer. |
| 960 | void SetOatDexFile(OatDexFile* oat_dex_file) const { |
| 961 | oat_dex_file_ = oat_dex_file; |
| 962 | } |
| 963 | |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 964 | // Utility methods for reading integral values from a buffer. |
| 965 | static int32_t ReadSignedInt(const uint8_t* ptr, int zwidth); |
| 966 | static uint32_t ReadUnsignedInt(const uint8_t* ptr, int zwidth, bool fill_on_right); |
| 967 | static int64_t ReadSignedLong(const uint8_t* ptr, int zwidth); |
| 968 | static uint64_t ReadUnsignedLong(const uint8_t* ptr, int zwidth, bool fill_on_right); |
| 969 | |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 970 | // Recalculates the checksum of the dex file. Does not use the current value in the header. |
| 971 | uint32_t CalculateChecksum() const; |
Mathieu Chartier | 2c4b084 | 2017-12-13 11:49:51 -0800 | [diff] [blame] | 972 | static uint32_t CalculateChecksum(const uint8_t* begin, size_t size); |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 973 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 974 | // Returns a human-readable form of the method at an index. |
| 975 | std::string PrettyMethod(uint32_t method_idx, bool with_signature = true) const; |
| 976 | // Returns a human-readable form of the field at an index. |
| 977 | std::string PrettyField(uint32_t field_idx, bool with_type = true) const; |
| 978 | // Returns a human-readable form of the type at an index. |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 979 | std::string PrettyType(dex::TypeIndex type_idx) const; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 980 | |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 981 | // Not virtual for performance reasons. |
| 982 | ALWAYS_INLINE bool IsCompactDexFile() const { |
| 983 | return is_compact_dex_; |
Mathieu Chartier | 603ccab | 2017-10-20 14:34:28 -0700 | [diff] [blame] | 984 | } |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 985 | ALWAYS_INLINE bool IsStandardDexFile() const { |
| 986 | return !is_compact_dex_; |
Mathieu Chartier | 603ccab | 2017-10-20 14:34:28 -0700 | [diff] [blame] | 987 | } |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 988 | ALWAYS_INLINE const StandardDexFile* AsStandardDexFile() const; |
| 989 | ALWAYS_INLINE const CompactDexFile* AsCompactDexFile() const; |
Mathieu Chartier | 603ccab | 2017-10-20 14:34:28 -0700 | [diff] [blame] | 990 | |
Mathieu Chartier | 73f21d4 | 2018-01-02 14:26:50 -0800 | [diff] [blame] | 991 | bool HasAddress(const void* addr) const { |
| 992 | return Begin() <= addr && addr < Begin() + Size(); |
| 993 | } |
| 994 | |
Mathieu Chartier | 7b074bf | 2017-09-25 16:22:36 -0700 | [diff] [blame] | 995 | protected: |
Mathieu Chartier | f6e3147 | 2017-12-28 13:32:08 -0800 | [diff] [blame] | 996 | // First Dex format version supporting default methods. |
| 997 | static const uint32_t kDefaultMethodsVersion = 37; |
| 998 | |
David Sehr | 733ddb2 | 2016-09-19 15:02:18 -0700 | [diff] [blame] | 999 | DexFile(const uint8_t* base, |
| 1000 | size_t size, |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 1001 | const std::string& location, |
| 1002 | uint32_t location_checksum, |
David Sehr | 733bd4d | 2017-10-26 10:39:15 -0700 | [diff] [blame] | 1003 | const OatDexFile* oat_dex_file, |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1004 | DexFileContainer* container, |
| 1005 | bool is_compact_dex); |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame] | 1006 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1007 | // Top-level initializer that calls other Init methods. |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1008 | bool Init(std::string* error_msg); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1009 | |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 1010 | // Returns true if the header magic and version numbers are of the expected values. |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1011 | bool CheckMagicAndVersion(std::string* error_msg) const; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1012 | |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 1013 | // Initialize section info for sections only found in map. Returns true on success. |
| 1014 | void InitializeSectionsFromMapList(); |
| 1015 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1016 | // The base address of the memory mapping. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1017 | const uint8_t* const begin_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1018 | |
| 1019 | // The size of the underlying memory allocation in bytes. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1020 | const size_t size_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1021 | |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 1022 | // Typically the dex file name when available, alternatively some identifying string. |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1023 | // |
| 1024 | // The ClassLinker will use this to match DexFiles the boot class |
| 1025 | // path to DexCache::GetLocation when loading from an image. |
| 1026 | const std::string location_; |
| 1027 | |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1028 | const uint32_t location_checksum_; |
| 1029 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1030 | // Points to the header section. |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 1031 | const Header* const header_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1032 | |
| 1033 | // Points to the base of the string identifier list. |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 1034 | const StringId* const string_ids_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1035 | |
| 1036 | // Points to the base of the type identifier list. |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 1037 | const TypeId* const type_ids_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1038 | |
| 1039 | // Points to the base of the field identifier list. |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 1040 | const FieldId* const field_ids_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1041 | |
| 1042 | // Points to the base of the method identifier list. |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 1043 | const MethodId* const method_ids_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1044 | |
| 1045 | // Points to the base of the prototype identifier list. |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 1046 | const ProtoId* const proto_ids_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1047 | |
| 1048 | // Points to the base of the class definition list. |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 1049 | const ClassDef* const class_defs_; |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 1050 | |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 1051 | // Points to the base of the method handles list. |
| 1052 | const MethodHandleItem* method_handles_; |
| 1053 | |
| 1054 | // Number of elements in the method handles list. |
| 1055 | size_t num_method_handles_; |
| 1056 | |
| 1057 | // Points to the base of the call sites id list. |
| 1058 | const CallSiteIdItem* call_site_ids_; |
| 1059 | |
| 1060 | // Number of elements in the call sites list. |
| 1061 | size_t num_call_site_ids_; |
| 1062 | |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 1063 | // If this dex file was loaded from an oat file, oat_dex_file_ contains a |
| 1064 | // pointer to the OatDexFile it was loaded from. Otherwise oat_dex_file_ is |
| 1065 | // null. |
Mathieu Chartier | 1b86849 | 2016-11-16 16:22:37 -0800 | [diff] [blame] | 1066 | mutable const OatDexFile* oat_dex_file_; |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 1067 | |
David Sehr | 733bd4d | 2017-10-26 10:39:15 -0700 | [diff] [blame] | 1068 | // Manages the underlying memory allocation. |
| 1069 | std::unique_ptr<DexFileContainer> container_; |
| 1070 | |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1071 | // If the dex file is a compact dex file. If false then the dex file is a standard dex file. |
| 1072 | const bool is_compact_dex_; |
| 1073 | |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 1074 | friend class DexFileLoader; |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 1075 | friend class DexFileVerifierTest; |
Mathieu Chartier | 1b86849 | 2016-11-16 16:22:37 -0800 | [diff] [blame] | 1076 | friend class OatWriter; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1077 | }; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1078 | |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 1079 | std::ostream& operator<<(std::ostream& os, const DexFile& dex_file); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1080 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1081 | // Iterate over a dex file's ProtoId's paramters |
| 1082 | class DexFileParameterIterator { |
| 1083 | public: |
| 1084 | DexFileParameterIterator(const DexFile& dex_file, const DexFile::ProtoId& proto_id) |
Mathieu Chartier | 73d7b2d | 2017-10-09 16:41:33 -0700 | [diff] [blame] | 1085 | : dex_file_(dex_file) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1086 | type_list_ = dex_file_.GetProtoParameters(proto_id); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1087 | if (type_list_ != nullptr) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1088 | size_ = type_list_->Size(); |
| 1089 | } |
| 1090 | } |
| 1091 | bool HasNext() const { return pos_ < size_; } |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1092 | size_t Size() const { return size_; } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1093 | void Next() { ++pos_; } |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1094 | dex::TypeIndex GetTypeIdx() { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1095 | return type_list_->GetTypeItem(pos_).type_idx_; |
| 1096 | } |
| 1097 | const char* GetDescriptor() { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1098 | return dex_file_.StringByTypeIdx(dex::TypeIndex(GetTypeIdx())); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1099 | } |
| 1100 | private: |
| 1101 | const DexFile& dex_file_; |
Mathieu Chartier | 73d7b2d | 2017-10-09 16:41:33 -0700 | [diff] [blame] | 1102 | const DexFile::TypeList* type_list_ = nullptr; |
| 1103 | uint32_t size_ = 0; |
| 1104 | uint32_t pos_ = 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1105 | DISALLOW_IMPLICIT_CONSTRUCTORS(DexFileParameterIterator); |
| 1106 | }; |
| 1107 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 1108 | // Abstract the signature of a method. |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 1109 | class Signature : public ValueObject { |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 1110 | public: |
| 1111 | std::string ToString() const; |
| 1112 | |
| 1113 | static Signature NoSignature() { |
| 1114 | return Signature(); |
| 1115 | } |
| 1116 | |
Orion Hodson | 6c4921b | 2016-09-21 15:41:06 +0100 | [diff] [blame] | 1117 | bool IsVoid() const; |
| 1118 | uint32_t GetNumberOfParameters() const; |
| 1119 | |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 1120 | bool operator==(const Signature& rhs) const; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 1121 | bool operator!=(const Signature& rhs) const { |
| 1122 | return !(*this == rhs); |
| 1123 | } |
| 1124 | |
Vladimir Marko | d9cffea | 2013-11-25 15:08:02 +0000 | [diff] [blame] | 1125 | bool operator==(const StringPiece& rhs) const; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 1126 | |
| 1127 | private: |
| 1128 | Signature(const DexFile* dex, const DexFile::ProtoId& proto) : dex_file_(dex), proto_id_(&proto) { |
| 1129 | } |
| 1130 | |
Mathieu Chartier | 73d7b2d | 2017-10-09 16:41:33 -0700 | [diff] [blame] | 1131 | Signature() = default; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 1132 | |
| 1133 | friend class DexFile; |
| 1134 | |
Mathieu Chartier | 73d7b2d | 2017-10-09 16:41:33 -0700 | [diff] [blame] | 1135 | const DexFile* const dex_file_ = nullptr; |
| 1136 | const DexFile::ProtoId* const proto_id_ = nullptr; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 1137 | }; |
| 1138 | std::ostream& operator<<(std::ostream& os, const Signature& sig); |
| 1139 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1140 | // Iterate and decode class_data_item |
| 1141 | class ClassDataItemIterator { |
| 1142 | public: |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1143 | ClassDataItemIterator(const DexFile& dex_file, const uint8_t* raw_class_data_item) |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1144 | : dex_file_(dex_file), pos_(0), ptr_pos_(raw_class_data_item), last_idx_(0) { |
| 1145 | ReadClassDataHeader(); |
| 1146 | if (EndOfInstanceFieldsPos() > 0) { |
| 1147 | ReadClassDataField(); |
| 1148 | } else if (EndOfVirtualMethodsPos() > 0) { |
| 1149 | ReadClassDataMethod(); |
| 1150 | } |
| 1151 | } |
| 1152 | uint32_t NumStaticFields() const { |
| 1153 | return header_.static_fields_size_; |
| 1154 | } |
| 1155 | uint32_t NumInstanceFields() const { |
| 1156 | return header_.instance_fields_size_; |
| 1157 | } |
| 1158 | uint32_t NumDirectMethods() const { |
| 1159 | return header_.direct_methods_size_; |
| 1160 | } |
| 1161 | uint32_t NumVirtualMethods() const { |
| 1162 | return header_.virtual_methods_size_; |
| 1163 | } |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 1164 | bool IsAtMethod() const { |
| 1165 | return pos_ >= EndOfInstanceFieldsPos(); |
| 1166 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1167 | bool HasNextStaticField() const { |
| 1168 | return pos_ < EndOfStaticFieldsPos(); |
| 1169 | } |
| 1170 | bool HasNextInstanceField() const { |
| 1171 | return pos_ >= EndOfStaticFieldsPos() && pos_ < EndOfInstanceFieldsPos(); |
| 1172 | } |
| 1173 | bool HasNextDirectMethod() const { |
| 1174 | return pos_ >= EndOfInstanceFieldsPos() && pos_ < EndOfDirectMethodsPos(); |
| 1175 | } |
| 1176 | bool HasNextVirtualMethod() const { |
| 1177 | return pos_ >= EndOfDirectMethodsPos() && pos_ < EndOfVirtualMethodsPos(); |
| 1178 | } |
Mathieu Chartier | b7c273c | 2017-11-10 18:07:56 -0800 | [diff] [blame] | 1179 | bool HasNextMethod() const { |
| 1180 | const bool result = pos_ >= EndOfInstanceFieldsPos() && pos_ < EndOfVirtualMethodsPos(); |
| 1181 | DCHECK_EQ(result, HasNextDirectMethod() || HasNextVirtualMethod()); |
| 1182 | return result; |
| 1183 | } |
Mathieu Chartier | e17cf24 | 2017-06-19 11:05:51 -0700 | [diff] [blame] | 1184 | void SkipStaticFields() { |
| 1185 | while (HasNextStaticField()) { |
| 1186 | Next(); |
| 1187 | } |
| 1188 | } |
| 1189 | void SkipInstanceFields() { |
| 1190 | while (HasNextInstanceField()) { |
| 1191 | Next(); |
| 1192 | } |
| 1193 | } |
| 1194 | void SkipAllFields() { |
| 1195 | SkipStaticFields(); |
| 1196 | SkipInstanceFields(); |
| 1197 | } |
| 1198 | void SkipDirectMethods() { |
| 1199 | while (HasNextDirectMethod()) { |
| 1200 | Next(); |
| 1201 | } |
| 1202 | } |
| 1203 | void SkipVirtualMethods() { |
| 1204 | while (HasNextVirtualMethod()) { |
| 1205 | Next(); |
| 1206 | } |
| 1207 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1208 | bool HasNext() const { |
| 1209 | return pos_ < EndOfVirtualMethodsPos(); |
| 1210 | } |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 1211 | inline void Next() { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1212 | pos_++; |
| 1213 | if (pos_ < EndOfStaticFieldsPos()) { |
| 1214 | last_idx_ = GetMemberIndex(); |
| 1215 | ReadClassDataField(); |
| 1216 | } else if (pos_ == EndOfStaticFieldsPos() && NumInstanceFields() > 0) { |
| 1217 | last_idx_ = 0; // transition to next array, reset last index |
| 1218 | ReadClassDataField(); |
| 1219 | } else if (pos_ < EndOfInstanceFieldsPos()) { |
| 1220 | last_idx_ = GetMemberIndex(); |
| 1221 | ReadClassDataField(); |
| 1222 | } else if (pos_ == EndOfInstanceFieldsPos() && NumDirectMethods() > 0) { |
| 1223 | last_idx_ = 0; // transition to next array, reset last index |
| 1224 | ReadClassDataMethod(); |
| 1225 | } else if (pos_ < EndOfDirectMethodsPos()) { |
| 1226 | last_idx_ = GetMemberIndex(); |
| 1227 | ReadClassDataMethod(); |
| 1228 | } else if (pos_ == EndOfDirectMethodsPos() && NumVirtualMethods() > 0) { |
| 1229 | last_idx_ = 0; // transition to next array, reset last index |
| 1230 | ReadClassDataMethod(); |
| 1231 | } else if (pos_ < EndOfVirtualMethodsPos()) { |
| 1232 | last_idx_ = GetMemberIndex(); |
| 1233 | ReadClassDataMethod(); |
| 1234 | } else { |
| 1235 | DCHECK(!HasNext()); |
| 1236 | } |
| 1237 | } |
| 1238 | uint32_t GetMemberIndex() const { |
| 1239 | if (pos_ < EndOfInstanceFieldsPos()) { |
| 1240 | return last_idx_ + field_.field_idx_delta_; |
| 1241 | } else { |
Sebastien Hertz | b24bd99 | 2013-08-02 15:19:09 +0200 | [diff] [blame] | 1242 | DCHECK_LT(pos_, EndOfVirtualMethodsPos()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1243 | return last_idx_ + method_.method_idx_delta_; |
| 1244 | } |
| 1245 | } |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 1246 | uint32_t GetRawMemberAccessFlags() const { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1247 | if (pos_ < EndOfInstanceFieldsPos()) { |
| 1248 | return field_.access_flags_; |
| 1249 | } else { |
Sebastien Hertz | b24bd99 | 2013-08-02 15:19:09 +0200 | [diff] [blame] | 1250 | DCHECK_LT(pos_, EndOfVirtualMethodsPos()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1251 | return method_.access_flags_; |
| 1252 | } |
| 1253 | } |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 1254 | uint32_t GetFieldAccessFlags() const { |
| 1255 | return GetRawMemberAccessFlags() & kAccValidFieldFlags; |
| 1256 | } |
| 1257 | uint32_t GetMethodAccessFlags() const { |
| 1258 | return GetRawMemberAccessFlags() & kAccValidMethodFlags; |
| 1259 | } |
| 1260 | bool MemberIsNative() const { |
| 1261 | return GetRawMemberAccessFlags() & kAccNative; |
| 1262 | } |
| 1263 | bool MemberIsFinal() const { |
| 1264 | return GetRawMemberAccessFlags() & kAccFinal; |
| 1265 | } |
Andreas Gampe | 04c6ab9 | 2017-06-08 21:49:14 -0700 | [diff] [blame] | 1266 | ALWAYS_INLINE InvokeType GetMethodInvokeType(const DexFile::ClassDef& class_def) const; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1267 | const DexFile::CodeItem* GetMethodCodeItem() const { |
| 1268 | return dex_file_.GetCodeItem(method_.code_off_); |
| 1269 | } |
| 1270 | uint32_t GetMethodCodeItemOffset() const { |
| 1271 | return method_.code_off_; |
| 1272 | } |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 1273 | const uint8_t* DataPointer() const { |
| 1274 | return ptr_pos_; |
| 1275 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1276 | const uint8_t* EndDataPointer() const { |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1277 | CHECK(!HasNext()); |
| 1278 | return ptr_pos_; |
| 1279 | } |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 1280 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1281 | private: |
| 1282 | // A dex file's class_data_item is leb128 encoded, this structure holds a decoded form of the |
| 1283 | // header for a class_data_item |
| 1284 | struct ClassDataHeader { |
| 1285 | uint32_t static_fields_size_; // the number of static fields |
| 1286 | uint32_t instance_fields_size_; // the number of instance fields |
| 1287 | uint32_t direct_methods_size_; // the number of direct methods |
| 1288 | uint32_t virtual_methods_size_; // the number of virtual methods |
| 1289 | } header_; |
| 1290 | |
| 1291 | // Read and decode header from a class_data_item stream into header |
| 1292 | void ReadClassDataHeader(); |
| 1293 | |
| 1294 | uint32_t EndOfStaticFieldsPos() const { |
| 1295 | return header_.static_fields_size_; |
| 1296 | } |
| 1297 | uint32_t EndOfInstanceFieldsPos() const { |
| 1298 | return EndOfStaticFieldsPos() + header_.instance_fields_size_; |
| 1299 | } |
| 1300 | uint32_t EndOfDirectMethodsPos() const { |
| 1301 | return EndOfInstanceFieldsPos() + header_.direct_methods_size_; |
| 1302 | } |
| 1303 | uint32_t EndOfVirtualMethodsPos() const { |
| 1304 | return EndOfDirectMethodsPos() + header_.virtual_methods_size_; |
| 1305 | } |
| 1306 | |
| 1307 | // A decoded version of the field of a class_data_item |
| 1308 | struct ClassDataField { |
| 1309 | uint32_t field_idx_delta_; // delta of index into the field_ids array for FieldId |
| 1310 | uint32_t access_flags_; // access flags for the field |
| 1311 | ClassDataField() : field_idx_delta_(0), access_flags_(0) {} |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 1312 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1313 | private: |
| 1314 | DISALLOW_COPY_AND_ASSIGN(ClassDataField); |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 1315 | }; |
| 1316 | ClassDataField field_; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1317 | |
| 1318 | // Read and decode a field from a class_data_item stream into field |
| 1319 | void ReadClassDataField(); |
| 1320 | |
| 1321 | // A decoded version of the method of a class_data_item |
| 1322 | struct ClassDataMethod { |
| 1323 | uint32_t method_idx_delta_; // delta of index into the method_ids array for MethodId |
| 1324 | uint32_t access_flags_; |
| 1325 | uint32_t code_off_; |
| 1326 | ClassDataMethod() : method_idx_delta_(0), access_flags_(0), code_off_(0) {} |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 1327 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1328 | private: |
| 1329 | DISALLOW_COPY_AND_ASSIGN(ClassDataMethod); |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 1330 | }; |
| 1331 | ClassDataMethod method_; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1332 | |
| 1333 | // Read and decode a method from a class_data_item stream into method |
| 1334 | void ReadClassDataMethod(); |
| 1335 | |
| 1336 | const DexFile& dex_file_; |
| 1337 | size_t pos_; // integral number of items passed |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1338 | const uint8_t* ptr_pos_; // pointer into stream of class_data_item |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1339 | uint32_t last_idx_; // last read field or method index to apply delta to |
| 1340 | DISALLOW_IMPLICIT_CONSTRUCTORS(ClassDataItemIterator); |
| 1341 | }; |
| 1342 | |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 1343 | class EncodedArrayValueIterator { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1344 | public: |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 1345 | EncodedArrayValueIterator(const DexFile& dex_file, const uint8_t* array_data); |
Shinichiro Hamaji | 82863f0 | 2015-11-05 16:51:33 +0900 | [diff] [blame] | 1346 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1347 | bool HasNext() const { return pos_ < array_size_; } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1348 | |
| 1349 | void Next(); |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 1350 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1351 | enum ValueType { |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 1352 | kByte = 0x00, |
| 1353 | kShort = 0x02, |
| 1354 | kChar = 0x03, |
| 1355 | kInt = 0x04, |
| 1356 | kLong = 0x06, |
| 1357 | kFloat = 0x10, |
| 1358 | kDouble = 0x11, |
| 1359 | kMethodType = 0x15, |
| 1360 | kMethodHandle = 0x16, |
| 1361 | kString = 0x17, |
| 1362 | kType = 0x18, |
| 1363 | kField = 0x19, |
| 1364 | kMethod = 0x1a, |
| 1365 | kEnum = 0x1b, |
| 1366 | kArray = 0x1c, |
| 1367 | kAnnotation = 0x1d, |
| 1368 | kNull = 0x1e, |
| 1369 | kBoolean = 0x1f, |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1370 | }; |
| 1371 | |
Shinichiro Hamaji | 82863f0 | 2015-11-05 16:51:33 +0900 | [diff] [blame] | 1372 | ValueType GetValueType() const { return type_; } |
| 1373 | const jvalue& GetJavaValue() const { return jval_; } |
| 1374 | |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1375 | protected: |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1376 | static constexpr uint8_t kEncodedValueTypeMask = 0x1f; // 0b11111 |
| 1377 | static constexpr uint8_t kEncodedValueArgShift = 5; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1378 | |
| 1379 | const DexFile& dex_file_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1380 | size_t array_size_; // Size of array. |
| 1381 | size_t pos_; // Current position. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1382 | const uint8_t* ptr_; // Pointer into encoded data array. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1383 | ValueType type_; // Type of current encoded value. |
| 1384 | jvalue jval_; // Value of current encoded value. |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1385 | |
| 1386 | private: |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 1387 | DISALLOW_IMPLICIT_CONSTRUCTORS(EncodedArrayValueIterator); |
| 1388 | }; |
| 1389 | std::ostream& operator<<(std::ostream& os, const EncodedArrayValueIterator::ValueType& code); |
| 1390 | |
| 1391 | class EncodedStaticFieldValueIterator : public EncodedArrayValueIterator { |
| 1392 | public: |
| 1393 | EncodedStaticFieldValueIterator(const DexFile& dex_file, |
| 1394 | const DexFile::ClassDef& class_def) |
| 1395 | : EncodedArrayValueIterator(dex_file, |
| 1396 | dex_file.GetEncodedStaticFieldValuesArray(class_def)) |
| 1397 | {} |
| 1398 | |
| 1399 | private: |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1400 | DISALLOW_IMPLICIT_CONSTRUCTORS(EncodedStaticFieldValueIterator); |
| 1401 | }; |
Brian Carlstrom | 88f3654 | 2012-10-16 23:24:21 -0700 | [diff] [blame] | 1402 | std::ostream& operator<<(std::ostream& os, const EncodedStaticFieldValueIterator::ValueType& code); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1403 | |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 1404 | class CallSiteArrayValueIterator : public EncodedArrayValueIterator { |
| 1405 | public: |
| 1406 | CallSiteArrayValueIterator(const DexFile& dex_file, |
| 1407 | const DexFile::CallSiteIdItem& call_site_id) |
| 1408 | : EncodedArrayValueIterator(dex_file, |
| 1409 | dex_file.GetCallSiteEncodedValuesArray(call_site_id)) |
| 1410 | {} |
| 1411 | |
| 1412 | uint32_t Size() const { return array_size_; } |
| 1413 | |
| 1414 | private: |
| 1415 | DISALLOW_IMPLICIT_CONSTRUCTORS(CallSiteArrayValueIterator); |
| 1416 | }; |
| 1417 | std::ostream& operator<<(std::ostream& os, const CallSiteArrayValueIterator::ValueType& code); |
| 1418 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1419 | } // namespace art |
| 1420 | |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 1421 | #endif // ART_RUNTIME_DEX_DEX_FILE_H_ |