Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_DEX_FILE_H_ |
| 18 | #define ART_RUNTIME_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 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 24 | #include "base/logging.h" |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 25 | #include "base/value_object.h" |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 26 | #include "dex_file_types.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 27 | #include "globals.h" |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 28 | #include "invoke_type.h" |
Jesse Wilson | 6bf1915 | 2011-09-29 13:12:33 -0400 | [diff] [blame] | 29 | #include "jni.h" |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 30 | #include "modifiers.h" |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 31 | #include "utf.h" |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 32 | |
| 33 | namespace art { |
| 34 | |
Ian Rogers | 576ca0c | 2014-06-06 15:58:22 -0700 | [diff] [blame] | 35 | class MemMap; |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 36 | class OatDexFile; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 37 | class Signature; |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 38 | class StringPiece; |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 39 | class ZipArchive; |
| 40 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 41 | class DexFile { |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 42 | public: |
Roland Levillain | 621b5ea | 2016-05-18 11:41:33 +0100 | [diff] [blame] | 43 | // First Dex format version supporting default methods. |
Alex Light | b55f1ac | 2016-04-12 15:50:55 -0700 | [diff] [blame] | 44 | static const uint32_t kDefaultMethodsVersion = 37; |
Roland Levillain | 621b5ea | 2016-05-18 11:41:33 +0100 | [diff] [blame] | 45 | // First Dex format version enforcing class definition ordering rules. |
| 46 | static const uint32_t kClassDefinitionOrderEnforcedVersion = 37; |
| 47 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 48 | static const uint8_t kDexMagic[]; |
Narayan Kamath | 52e6650 | 2016-08-01 14:20:31 +0100 | [diff] [blame] | 49 | static constexpr size_t kNumDexVersions = 3; |
Alex Light | c496181 | 2016-03-23 10:20:41 -0700 | [diff] [blame] | 50 | static constexpr size_t kDexVersionLen = 4; |
| 51 | static const uint8_t kDexMagicVersions[kNumDexVersions][kDexVersionLen]; |
| 52 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 53 | static constexpr size_t kSha1DigestSize = 20; |
| 54 | static constexpr uint32_t kDexEndianConstant = 0x12345678; |
Carl Shapiro | 80d4dde | 2011-06-28 16:24:07 -0700 | [diff] [blame] | 55 | |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 56 | // name of the DexFile entry within a zip archive |
| 57 | static const char* kClassesDex; |
| 58 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 59 | // The value of an invalid index. |
| 60 | static const uint32_t kDexNoIndex = 0xFFFFFFFF; |
| 61 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 62 | // The value of an invalid index. |
| 63 | static const uint16_t kDexNoIndex16 = 0xFFFF; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 64 | |
Alex Light | c496181 | 2016-03-23 10:20:41 -0700 | [diff] [blame] | 65 | // The separator character in MultiDex locations. |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 66 | static constexpr char kMultiDexSeparator = ':'; |
| 67 | |
| 68 | // A string version of the previous. This is a define so that we can merge string literals in the |
| 69 | // preprocessor. |
| 70 | #define kMultiDexSeparatorString ":" |
| 71 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 72 | // Raw header_item. |
| 73 | struct Header { |
| 74 | uint8_t magic_[8]; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 75 | uint32_t checksum_; // See also location_checksum_ |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 76 | uint8_t signature_[kSha1DigestSize]; |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame] | 77 | uint32_t file_size_; // size of entire file |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 78 | uint32_t header_size_; // offset to start of next section |
| 79 | uint32_t endian_tag_; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 80 | uint32_t link_size_; // unused |
| 81 | uint32_t link_off_; // unused |
| 82 | uint32_t map_off_; // unused |
| 83 | uint32_t string_ids_size_; // number of StringIds |
| 84 | uint32_t string_ids_off_; // file offset of StringIds array |
| 85 | uint32_t type_ids_size_; // number of TypeIds, we don't support more than 65535 |
| 86 | uint32_t type_ids_off_; // file offset of TypeIds array |
| 87 | uint32_t proto_ids_size_; // number of ProtoIds, we don't support more than 65535 |
| 88 | uint32_t proto_ids_off_; // file offset of ProtoIds array |
| 89 | uint32_t field_ids_size_; // number of FieldIds |
| 90 | uint32_t field_ids_off_; // file offset of FieldIds array |
| 91 | uint32_t method_ids_size_; // number of MethodIds |
| 92 | uint32_t method_ids_off_; // file offset of MethodIds array |
| 93 | uint32_t class_defs_size_; // number of ClassDefs |
| 94 | uint32_t class_defs_off_; // file offset of ClassDef array |
| 95 | uint32_t data_size_; // unused |
| 96 | uint32_t data_off_; // unused |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 97 | |
Andreas Gampe | 76ed99d | 2016-03-28 18:31:29 -0700 | [diff] [blame] | 98 | // Decode the dex magic version |
| 99 | uint32_t GetVersion() const; |
| 100 | |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 101 | private: |
| 102 | DISALLOW_COPY_AND_ASSIGN(Header); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 103 | }; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 104 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 105 | // Map item type codes. |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 106 | enum MapItemType : uint16_t { // private |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 107 | kDexTypeHeaderItem = 0x0000, |
| 108 | kDexTypeStringIdItem = 0x0001, |
| 109 | kDexTypeTypeIdItem = 0x0002, |
| 110 | kDexTypeProtoIdItem = 0x0003, |
| 111 | kDexTypeFieldIdItem = 0x0004, |
| 112 | kDexTypeMethodIdItem = 0x0005, |
| 113 | kDexTypeClassDefItem = 0x0006, |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 114 | kDexTypeCallSiteIdItem = 0x0007, |
| 115 | kDexTypeMethodHandleItem = 0x0008, |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 116 | kDexTypeMapList = 0x1000, |
| 117 | kDexTypeTypeList = 0x1001, |
| 118 | kDexTypeAnnotationSetRefList = 0x1002, |
| 119 | kDexTypeAnnotationSetItem = 0x1003, |
| 120 | kDexTypeClassDataItem = 0x2000, |
| 121 | kDexTypeCodeItem = 0x2001, |
| 122 | kDexTypeStringDataItem = 0x2002, |
| 123 | kDexTypeDebugInfoItem = 0x2003, |
| 124 | kDexTypeAnnotationItem = 0x2004, |
| 125 | kDexTypeEncodedArrayItem = 0x2005, |
| 126 | kDexTypeAnnotationsDirectoryItem = 0x2006, |
| 127 | }; |
| 128 | |
| 129 | struct MapItem { |
| 130 | uint16_t type_; |
| 131 | uint16_t unused_; |
| 132 | uint32_t size_; |
| 133 | uint32_t offset_; |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 134 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 135 | private: |
| 136 | DISALLOW_COPY_AND_ASSIGN(MapItem); |
| 137 | }; |
| 138 | |
| 139 | struct MapList { |
| 140 | uint32_t size_; |
| 141 | MapItem list_[1]; |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 142 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 143 | private: |
| 144 | DISALLOW_COPY_AND_ASSIGN(MapList); |
| 145 | }; |
| 146 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 147 | // Raw string_id_item. |
| 148 | struct StringId { |
| 149 | uint32_t string_data_off_; // offset in bytes from the base address |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 150 | |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 151 | private: |
| 152 | DISALLOW_COPY_AND_ASSIGN(StringId); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | // Raw type_id_item. |
| 156 | struct TypeId { |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 157 | dex::StringIndex descriptor_idx_; // index into string_ids |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 158 | |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 159 | private: |
| 160 | DISALLOW_COPY_AND_ASSIGN(TypeId); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | // Raw field_id_item. |
| 164 | struct FieldId { |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 165 | dex::TypeIndex class_idx_; // index into type_ids_ array for defining class |
| 166 | dex::TypeIndex type_idx_; // index into type_ids_ array for field type |
| 167 | dex::StringIndex name_idx_; // index into string_ids_ array for field name |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 168 | |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 169 | private: |
| 170 | DISALLOW_COPY_AND_ASSIGN(FieldId); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 171 | }; |
| 172 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 173 | // Raw proto_id_item. |
| 174 | struct ProtoId { |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 175 | dex::StringIndex shorty_idx_; // index into string_ids array for shorty descriptor |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 176 | 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] | 177 | uint16_t pad_; // padding = 0 |
| 178 | uint32_t parameters_off_; // file offset to type_list for parameter types |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 179 | |
| 180 | private: |
| 181 | DISALLOW_COPY_AND_ASSIGN(ProtoId); |
| 182 | }; |
| 183 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 184 | // Raw method_id_item. |
| 185 | struct MethodId { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 186 | dex::TypeIndex class_idx_; // index into type_ids_ array for defining class |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 187 | uint16_t proto_idx_; // index into proto_ids_ array for method prototype |
| 188 | dex::StringIndex name_idx_; // index into string_ids_ array for method name |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 189 | |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 190 | private: |
| 191 | DISALLOW_COPY_AND_ASSIGN(MethodId); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 192 | }; |
| 193 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 194 | // Raw class_def_item. |
| 195 | struct ClassDef { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 196 | dex::TypeIndex class_idx_; // index into type_ids_ array for this class |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 197 | uint16_t pad1_; // padding = 0 |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 198 | uint32_t access_flags_; |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 199 | dex::TypeIndex superclass_idx_; // index into type_ids_ array for superclass |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 200 | uint16_t pad2_; // padding = 0 |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 201 | uint32_t interfaces_off_; // file offset to TypeList |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 202 | 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] | 203 | uint32_t annotations_off_; // file offset to annotations_directory_item |
| 204 | uint32_t class_data_off_; // file offset to class_data_item |
| 205 | uint32_t static_values_off_; // file offset to EncodedArray |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 206 | |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 207 | // Returns the valid access flags, that is, Java modifier bits relevant to the ClassDef type |
| 208 | // (class or interface). These are all in the lower 16b and do not contain runtime flags. |
| 209 | uint32_t GetJavaAccessFlags() const { |
| 210 | // Make sure that none of our runtime-only flags are set. |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 211 | static_assert((kAccValidClassFlags & kAccJavaFlagsMask) == kAccValidClassFlags, |
| 212 | "Valid class flags not a subset of Java flags"); |
| 213 | static_assert((kAccValidInterfaceFlags & kAccJavaFlagsMask) == kAccValidInterfaceFlags, |
| 214 | "Valid interface flags not a subset of Java flags"); |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 215 | |
| 216 | if ((access_flags_ & kAccInterface) != 0) { |
| 217 | // Interface. |
| 218 | return access_flags_ & kAccValidInterfaceFlags; |
| 219 | } else { |
| 220 | // Class. |
| 221 | return access_flags_ & kAccValidClassFlags; |
| 222 | } |
| 223 | } |
| 224 | |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 225 | private: |
| 226 | DISALLOW_COPY_AND_ASSIGN(ClassDef); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 227 | }; |
| 228 | |
| 229 | // Raw type_item. |
| 230 | struct TypeItem { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 231 | dex::TypeIndex type_idx_; // index into type_ids section |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 232 | |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 233 | private: |
| 234 | DISALLOW_COPY_AND_ASSIGN(TypeItem); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | // Raw type_list. |
| 238 | class TypeList { |
| 239 | public: |
| 240 | uint32_t Size() const { |
| 241 | return size_; |
| 242 | } |
| 243 | |
| 244 | const TypeItem& GetTypeItem(uint32_t idx) const { |
Sebastien Hertz | b24bd99 | 2013-08-02 15:19:09 +0200 | [diff] [blame] | 245 | DCHECK_LT(idx, this->size_); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 246 | return this->list_[idx]; |
| 247 | } |
| 248 | |
Andreas Gampe | 31a7a0c | 2014-08-29 16:07:49 -0700 | [diff] [blame] | 249 | // Size in bytes of the part of the list that is common. |
| 250 | static constexpr size_t GetHeaderSize() { |
| 251 | return 4U; |
| 252 | } |
| 253 | |
| 254 | // Size in bytes of the whole type list including all the stored elements. |
| 255 | static constexpr size_t GetListSize(size_t count) { |
| 256 | return GetHeaderSize() + sizeof(TypeItem) * count; |
| 257 | } |
| 258 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 259 | private: |
| 260 | uint32_t size_; // size of the list, in entries |
| 261 | TypeItem list_[1]; // elements of the list |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 262 | DISALLOW_COPY_AND_ASSIGN(TypeList); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 263 | }; |
| 264 | |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 265 | // MethodHandle Types |
| 266 | enum class MethodHandleType : uint16_t { // private |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 267 | kStaticPut = 0x0000, // a setter for a given static field. |
| 268 | kStaticGet = 0x0001, // a getter for a given static field. |
| 269 | kInstancePut = 0x0002, // a setter for a given instance field. |
| 270 | kInstanceGet = 0x0003, // a getter for a given instance field. |
| 271 | kInvokeStatic = 0x0004, // an invoker for a given static method. |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 272 | kInvokeInstance = 0x0005, // invoke_instance : an invoker for a given instance method. This |
| 273 | // can be any non-static method on any class (or interface) except |
| 274 | // for “<init>”. |
| 275 | kInvokeConstructor = 0x0006, // an invoker for a given constructor. |
| 276 | kLast = kInvokeConstructor |
| 277 | }; |
| 278 | |
| 279 | // raw method_handle_item |
| 280 | struct MethodHandleItem { |
| 281 | uint16_t method_handle_type_; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 282 | uint16_t reserved1_; // Reserved for future use. |
| 283 | uint16_t field_or_method_idx_; // Field index for accessors, method index otherwise. |
| 284 | uint16_t reserved2_; // Reserved for future use. |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 285 | private: |
| 286 | DISALLOW_COPY_AND_ASSIGN(MethodHandleItem); |
| 287 | }; |
| 288 | |
| 289 | // raw call_site_id_item |
| 290 | struct CallSiteIdItem { |
| 291 | uint32_t data_off_; // Offset into data section pointing to encoded array items. |
| 292 | private: |
| 293 | DISALLOW_COPY_AND_ASSIGN(CallSiteIdItem); |
| 294 | }; |
| 295 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 296 | // Raw code_item. |
| 297 | struct CodeItem { |
Igor Murashkin | c449e8b | 2015-06-10 15:56:42 -0700 | [diff] [blame] | 298 | uint16_t registers_size_; // the number of registers used by this code |
| 299 | // (locals + parameters) |
| 300 | uint16_t ins_size_; // the number of words of incoming arguments to the method |
| 301 | // that this code is for |
| 302 | uint16_t outs_size_; // the number of words of outgoing argument space required |
| 303 | // by this code for method invocation |
| 304 | uint16_t tries_size_; // the number of try_items for this instance. If non-zero, |
| 305 | // then these appear as the tries array just after the |
| 306 | // insns in this instance. |
| 307 | uint32_t debug_info_off_; // file offset to debug info stream |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 308 | uint32_t insns_size_in_code_units_; // size of the insns array, in 2 byte code units |
Igor Murashkin | c449e8b | 2015-06-10 15:56:42 -0700 | [diff] [blame] | 309 | uint16_t insns_[1]; // actual array of bytecode. |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 310 | |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 311 | private: |
| 312 | DISALLOW_COPY_AND_ASSIGN(CodeItem); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 313 | }; |
| 314 | |
Carl Shapiro | 2eaa968 | 2011-08-04 19:26:11 -0700 | [diff] [blame] | 315 | // Raw try_item. |
| 316 | struct TryItem { |
| 317 | uint32_t start_addr_; |
| 318 | uint16_t insn_count_; |
| 319 | uint16_t handler_off_; |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 320 | |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 321 | private: |
| 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 | |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 427 | // Returns the checksums of a file for comparison with GetLocationChecksum(). |
| 428 | // For .dex files, this is the single header checksum. |
| 429 | // For zip files, this is the zip entry CRC32 checksum for classes.dex and |
| 430 | // each additional multidex entry classes2.dex, classes3.dex, etc. |
| 431 | // Return true if the checksums could be found, false otherwise. |
| 432 | static bool GetMultiDexChecksums(const char* filename, |
| 433 | std::vector<uint32_t>* checksums, |
| 434 | std::string* error_msg); |
| 435 | |
Richard Uhler | 84f50ae | 2017-02-06 15:12:45 +0000 | [diff] [blame] | 436 | // Check whether a location denotes a multidex dex file. This is a very simple check: returns |
| 437 | // whether the string contains the separator character. |
| 438 | static bool IsMultiDexLocation(const char* location); |
| 439 | |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 440 | // Opens .dex file, backed by existing memory |
David Sehr | 733ddb2 | 2016-09-19 15:02:18 -0700 | [diff] [blame] | 441 | static std::unique_ptr<const DexFile> Open(const uint8_t* base, |
| 442 | size_t size, |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 443 | const std::string& location, |
| 444 | uint32_t location_checksum, |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 445 | const OatDexFile* oat_dex_file, |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 446 | bool verify, |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 447 | bool verify_checksum, |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 448 | std::string* error_msg); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 449 | |
Orion Hodson | a4c2a05 | 2016-08-17 10:51:42 +0100 | [diff] [blame] | 450 | // Opens .dex file that has been memory-mapped by the caller. |
| 451 | static std::unique_ptr<const DexFile> Open(const std::string& location, |
| 452 | uint32_t location_checkum, |
| 453 | std::unique_ptr<MemMap> mem_map, |
| 454 | bool verify, |
| 455 | bool verify_checksum, |
| 456 | std::string* error_msg); |
| 457 | |
David Sehr | 733ddb2 | 2016-09-19 15:02:18 -0700 | [diff] [blame] | 458 | // Opens all .dex files found in the file, guessing the container format based on file extension. |
| 459 | static bool Open(const char* filename, |
| 460 | const std::string& location, |
| 461 | bool verify_checksum, |
| 462 | std::string* error_msg, |
| 463 | std::vector<std::unique_ptr<const DexFile>>* dex_files); |
Orion Hodson | a4c2a05 | 2016-08-17 10:51:42 +0100 | [diff] [blame] | 464 | |
David Sehr | 733ddb2 | 2016-09-19 15:02:18 -0700 | [diff] [blame] | 465 | // Open a single dex file from an fd. |
| 466 | static std::unique_ptr<const DexFile> OpenDex(int fd, |
| 467 | const std::string& location, |
| 468 | bool verify_checksum, |
| 469 | std::string* error_msg); |
| 470 | |
| 471 | // Opens dex files from within a .jar, .zip, or .apk file |
| 472 | static bool OpenZip(int fd, |
| 473 | const std::string& location, |
| 474 | bool verify_checksum, |
| 475 | std::string* error_msg, |
| 476 | std::vector<std::unique_ptr<const DexFile>>* dex_files); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 477 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 478 | // Closes a .dex file. |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 479 | virtual ~DexFile(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 480 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 481 | const std::string& GetLocation() const { |
| 482 | return location_; |
| 483 | } |
| 484 | |
Andreas Gampe | cb8f9e8 | 2014-07-24 15:35:50 -0700 | [diff] [blame] | 485 | // For normal dex files, location and base location coincide. If a dex file is part of a multidex |
| 486 | // archive, the base location is the name of the originating jar/apk, stripped of any internal |
| 487 | // classes*.dex path. |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 488 | static std::string GetBaseLocation(const char* location) { |
| 489 | const char* pos = strrchr(location, kMultiDexSeparator); |
| 490 | if (pos == nullptr) { |
| 491 | return location; |
Andreas Gampe | cb8f9e8 | 2014-07-24 15:35:50 -0700 | [diff] [blame] | 492 | } else { |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 493 | return std::string(location, pos - location); |
| 494 | } |
| 495 | } |
| 496 | |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 497 | static std::string GetBaseLocation(const std::string& location) { |
| 498 | return GetBaseLocation(location.c_str()); |
| 499 | } |
| 500 | |
| 501 | // Returns the ':classes*.dex' part of the dex location. Returns an empty |
| 502 | // string if there is no multidex suffix for the given location. |
| 503 | // The kMultiDexSeparator is included in the returned suffix. |
| 504 | static std::string GetMultiDexSuffix(const std::string& location) { |
| 505 | size_t pos = location.rfind(kMultiDexSeparator); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 506 | if (pos == std::string::npos) { |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 507 | return ""; |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 508 | } else { |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 509 | return location.substr(pos); |
Andreas Gampe | cb8f9e8 | 2014-07-24 15:35:50 -0700 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 513 | std::string GetBaseLocation() const { |
| 514 | return GetBaseLocation(location_); |
| 515 | } |
| 516 | |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 517 | // For DexFiles directly from .dex files, this is the checksum from the DexFile::Header. |
| 518 | // For DexFiles opened from a zip files, this will be the ZipEntry CRC32 of classes.dex. |
| 519 | uint32_t GetLocationChecksum() const { |
| 520 | return location_checksum_; |
| 521 | } |
| 522 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 523 | const Header& GetHeader() const { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 524 | DCHECK(header_ != nullptr) << GetLocation(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 525 | return *header_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 528 | // Decode the dex magic version |
Andreas Gampe | 76ed99d | 2016-03-28 18:31:29 -0700 | [diff] [blame] | 529 | uint32_t GetVersion() const { |
| 530 | return GetHeader().GetVersion(); |
| 531 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 532 | |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 533 | // Returns true if the byte string points to the magic value. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 534 | static bool IsMagicValid(const uint8_t* magic); |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 535 | |
| 536 | // Returns true if the byte string after the magic is the correct value. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 537 | static bool IsVersionValid(const uint8_t* magic); |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 538 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 539 | // Returns the number of string identifiers in the .dex file. |
| 540 | size_t NumStringIds() const { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 541 | DCHECK(header_ != nullptr) << GetLocation(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 542 | return header_->string_ids_size_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 545 | // Returns the StringId at the specified index. |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 546 | const StringId& GetStringId(dex::StringIndex idx) const { |
| 547 | DCHECK_LT(idx.index_, NumStringIds()) << GetLocation(); |
| 548 | return string_ids_[idx.index_]; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 549 | } |
| 550 | |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 551 | dex::StringIndex GetIndexForStringId(const StringId& string_id) const { |
Brian Carlstrom | 61e513c | 2011-12-09 15:30:06 -0800 | [diff] [blame] | 552 | CHECK_GE(&string_id, string_ids_) << GetLocation(); |
| 553 | CHECK_LT(&string_id, string_ids_ + header_->string_ids_size_) << GetLocation(); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 554 | return dex::StringIndex(&string_id - string_ids_); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | int32_t GetStringLength(const StringId& string_id) const; |
| 558 | |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 559 | // Returns a pointer to the UTF-8 string data referred to by the given string_id as well as the |
| 560 | // length of the string when decoded as a UTF-16 string. Note the UTF-16 length is not the same |
| 561 | // as the string length of the string data. |
| 562 | const char* GetStringDataAndUtf16Length(const StringId& string_id, uint32_t* utf16_length) const; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 563 | |
Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 564 | const char* GetStringData(const StringId& string_id) const; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 565 | |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 566 | // Index version of GetStringDataAndUtf16Length. |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 567 | const char* StringDataAndUtf16LengthByIdx(dex::StringIndex idx, uint32_t* utf16_length) const; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 568 | |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 569 | const char* StringDataByIdx(dex::StringIndex idx) const; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 570 | |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 571 | // Looks up a string id for a given modified utf8 string. |
| 572 | const StringId* FindStringId(const char* string) const; |
| 573 | |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 574 | const TypeId* FindTypeId(const char* string) const; |
| 575 | |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 576 | // Looks up a string id for a given utf16 string. |
Vladimir Marko | a48aef4 | 2014-12-03 17:53:53 +0000 | [diff] [blame] | 577 | const StringId* FindStringId(const uint16_t* string, size_t length) const; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 578 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 579 | // Returns the number of type identifiers in the .dex file. |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 580 | uint32_t NumTypeIds() const { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 581 | DCHECK(header_ != nullptr) << GetLocation(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 582 | return header_->type_ids_size_; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 585 | // Returns the TypeId at the specified index. |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 586 | const TypeId& GetTypeId(dex::TypeIndex idx) const { |
| 587 | DCHECK_LT(idx.index_, NumTypeIds()) << GetLocation(); |
| 588 | return type_ids_[idx.index_]; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 589 | } |
| 590 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 591 | dex::TypeIndex GetIndexForTypeId(const TypeId& type_id) const { |
Brian Carlstrom | 61e513c | 2011-12-09 15:30:06 -0800 | [diff] [blame] | 592 | CHECK_GE(&type_id, type_ids_) << GetLocation(); |
| 593 | CHECK_LT(&type_id, type_ids_ + header_->type_ids_size_) << GetLocation(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 594 | size_t result = &type_id - type_ids_; |
Brian Carlstrom | 61e513c | 2011-12-09 15:30:06 -0800 | [diff] [blame] | 595 | DCHECK_LT(result, 65536U) << GetLocation(); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 596 | return dex::TypeIndex(static_cast<uint16_t>(result)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | // Get the descriptor string associated with a given type index. |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 600 | const char* StringByTypeIdx(dex::TypeIndex idx, uint32_t* unicode_length) const; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 601 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 602 | const char* StringByTypeIdx(dex::TypeIndex idx) const; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 603 | |
| 604 | // Returns the type descriptor string of a type id. |
Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 605 | const char* GetTypeDescriptor(const TypeId& type_id) const; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 606 | |
| 607 | // Looks up a type for the given string index |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 608 | const TypeId* FindTypeId(dex::StringIndex string_idx) const; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 609 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 610 | // Returns the number of field identifiers in the .dex file. |
| 611 | size_t NumFieldIds() const { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 612 | DCHECK(header_ != nullptr) << GetLocation(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 613 | return header_->field_ids_size_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 616 | // Returns the FieldId at the specified index. |
| 617 | const FieldId& GetFieldId(uint32_t idx) const { |
Sebastien Hertz | b24bd99 | 2013-08-02 15:19:09 +0200 | [diff] [blame] | 618 | DCHECK_LT(idx, NumFieldIds()) << GetLocation(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 619 | return field_ids_[idx]; |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 620 | } |
| 621 | |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 622 | uint32_t GetIndexForFieldId(const FieldId& field_id) const { |
Brian Carlstrom | 61e513c | 2011-12-09 15:30:06 -0800 | [diff] [blame] | 623 | CHECK_GE(&field_id, field_ids_) << GetLocation(); |
| 624 | CHECK_LT(&field_id, field_ids_ + header_->field_ids_size_) << GetLocation(); |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 625 | return &field_id - field_ids_; |
| 626 | } |
| 627 | |
| 628 | // Looks up a field by its declaring class, name and type |
| 629 | const FieldId* FindFieldId(const DexFile::TypeId& declaring_klass, |
| 630 | const DexFile::StringId& name, |
| 631 | const DexFile::TypeId& type) const; |
| 632 | |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 633 | uint32_t FindCodeItemOffset(const DexFile::ClassDef& class_def, |
| 634 | uint32_t dex_method_idx) const; |
| 635 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 636 | // Returns the declaring class descriptor string of a field id. |
| 637 | const char* GetFieldDeclaringClassDescriptor(const FieldId& field_id) const { |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 638 | const DexFile::TypeId& type_id = GetTypeId(field_id.class_idx_); |
| 639 | return GetTypeDescriptor(type_id); |
| 640 | } |
| 641 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 642 | // Returns the class descriptor string of a field id. |
Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 643 | const char* GetFieldTypeDescriptor(const FieldId& field_id) const; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 644 | |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 645 | // Returns the name of a field id. |
Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 646 | const char* GetFieldName(const FieldId& field_id) const; |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 647 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 648 | // Returns the number of method identifiers in the .dex file. |
| 649 | size_t NumMethodIds() const { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 650 | DCHECK(header_ != nullptr) << GetLocation(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 651 | return header_->method_ids_size_; |
| 652 | } |
| 653 | |
| 654 | // Returns the MethodId at the specified index. |
| 655 | const MethodId& GetMethodId(uint32_t idx) const { |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 656 | DCHECK_LT(idx, NumMethodIds()) << GetLocation(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 657 | return method_ids_[idx]; |
| 658 | } |
| 659 | |
| 660 | uint32_t GetIndexForMethodId(const MethodId& method_id) const { |
Brian Carlstrom | 61e513c | 2011-12-09 15:30:06 -0800 | [diff] [blame] | 661 | CHECK_GE(&method_id, method_ids_) << GetLocation(); |
| 662 | CHECK_LT(&method_id, method_ids_ + header_->method_ids_size_) << GetLocation(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 663 | return &method_id - method_ids_; |
| 664 | } |
| 665 | |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 666 | // Looks up a method by its declaring class, name and proto_id |
| 667 | const MethodId* FindMethodId(const DexFile::TypeId& declaring_klass, |
| 668 | const DexFile::StringId& name, |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 669 | const DexFile::ProtoId& signature) const; |
| 670 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 671 | // Returns the declaring class descriptor string of a method id. |
Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 672 | const char* GetMethodDeclaringClassDescriptor(const MethodId& method_id) const; |
Brian Carlstrom | 7540ff4 | 2011-09-04 16:38:46 -0700 | [diff] [blame] | 673 | |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 674 | // Returns the prototype of a method id. |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 675 | const ProtoId& GetMethodPrototype(const MethodId& method_id) const { |
| 676 | return GetProtoId(method_id.proto_idx_); |
| 677 | } |
| 678 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 679 | // Returns a representation of the signature of a method id. |
| 680 | const Signature GetMethodSignature(const MethodId& method_id) const; |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 681 | |
Orion Hodson | b34bb19 | 2016-10-18 17:02:58 +0100 | [diff] [blame] | 682 | // Returns a representation of the signature of a proto id. |
| 683 | const Signature GetProtoSignature(const ProtoId& proto_id) const; |
| 684 | |
Brian Carlstrom | 7540ff4 | 2011-09-04 16:38:46 -0700 | [diff] [blame] | 685 | // Returns the name of a method id. |
Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 686 | const char* GetMethodName(const MethodId& method_id) const; |
Brian Carlstrom | 7540ff4 | 2011-09-04 16:38:46 -0700 | [diff] [blame] | 687 | |
Calin Juravle | 68ad649 | 2015-08-18 17:08:12 +0100 | [diff] [blame] | 688 | // Returns the shorty of a method by its index. |
Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 689 | const char* GetMethodShorty(uint32_t idx) const; |
Calin Juravle | 68ad649 | 2015-08-18 17:08:12 +0100 | [diff] [blame] | 690 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 691 | // Returns the shorty of a method id. |
Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 692 | const char* GetMethodShorty(const MethodId& method_id) const; |
| 693 | const char* GetMethodShorty(const MethodId& method_id, uint32_t* length) const; |
| 694 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 695 | // Returns the number of class definitions in the .dex file. |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 696 | uint32_t NumClassDefs() const { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 697 | DCHECK(header_ != nullptr) << GetLocation(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 698 | return header_->class_defs_size_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | // Returns the ClassDef at the specified index. |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 702 | const ClassDef& GetClassDef(uint16_t idx) const { |
Sebastien Hertz | b24bd99 | 2013-08-02 15:19:09 +0200 | [diff] [blame] | 703 | DCHECK_LT(idx, NumClassDefs()) << GetLocation(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 704 | return class_defs_[idx]; |
| 705 | } |
| 706 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 707 | uint16_t GetIndexForClassDef(const ClassDef& class_def) const { |
Brian Carlstrom | 61e513c | 2011-12-09 15:30:06 -0800 | [diff] [blame] | 708 | CHECK_GE(&class_def, class_defs_) << GetLocation(); |
| 709 | CHECK_LT(&class_def, class_defs_ + header_->class_defs_size_) << GetLocation(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 710 | return &class_def - class_defs_; |
| 711 | } |
| 712 | |
| 713 | // Returns the class descriptor string of a class definition. |
Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 714 | const char* GetClassDescriptor(const ClassDef& class_def) const; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 715 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 716 | // Looks up a class definition by its type index. |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 717 | const ClassDef* FindClassDef(dex::TypeIndex type_idx) const; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 718 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 719 | const TypeList* GetInterfacesList(const ClassDef& class_def) const { |
| 720 | if (class_def.interfaces_off_ == 0) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 721 | return nullptr; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 722 | } else { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 723 | const uint8_t* addr = begin_ + class_def.interfaces_off_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 724 | return reinterpret_cast<const TypeList*>(addr); |
| 725 | } |
| 726 | } |
| 727 | |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 728 | uint32_t NumMethodHandles() const { |
| 729 | return num_method_handles_; |
| 730 | } |
| 731 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 732 | const MethodHandleItem& GetMethodHandle(uint32_t idx) const { |
| 733 | CHECK_LT(idx, NumMethodHandles()); |
| 734 | return method_handles_[idx]; |
| 735 | } |
| 736 | |
| 737 | uint32_t NumCallSiteIds() const { |
| 738 | return num_call_site_ids_; |
| 739 | } |
| 740 | |
| 741 | const CallSiteIdItem& GetCallSiteId(uint32_t idx) const { |
| 742 | CHECK_LT(idx, NumCallSiteIds()); |
| 743 | return call_site_ids_[idx]; |
| 744 | } |
| 745 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 746 | // Returns a pointer to the raw memory mapped class_data_item |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 747 | const uint8_t* GetClassData(const ClassDef& class_def) const { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 748 | if (class_def.class_data_off_ == 0) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 749 | return nullptr; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 750 | } else { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 751 | return begin_ + class_def.class_data_off_; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 752 | } |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 753 | } |
| 754 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 755 | // |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 756 | const CodeItem* GetCodeItem(const uint32_t code_off) const { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 757 | 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] | 758 | if (code_off == 0) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 759 | return nullptr; // native or abstract method |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 760 | } else { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 761 | const uint8_t* addr = begin_ + code_off; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 762 | return reinterpret_cast<const CodeItem*>(addr); |
| 763 | } |
| 764 | } |
| 765 | |
Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 766 | const char* GetReturnTypeDescriptor(const ProtoId& proto_id) const; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 767 | |
| 768 | // Returns the number of prototype identifiers in the .dex file. |
| 769 | size_t NumProtoIds() const { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 770 | DCHECK(header_ != nullptr) << GetLocation(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 771 | return header_->proto_ids_size_; |
| 772 | } |
| 773 | |
| 774 | // Returns the ProtoId at the specified index. |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 775 | const ProtoId& GetProtoId(uint16_t idx) const { |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 776 | DCHECK_LT(idx, NumProtoIds()) << GetLocation(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 777 | return proto_ids_[idx]; |
| 778 | } |
| 779 | |
| 780 | uint16_t GetIndexForProtoId(const ProtoId& proto_id) const { |
Brian Carlstrom | 61e513c | 2011-12-09 15:30:06 -0800 | [diff] [blame] | 781 | CHECK_GE(&proto_id, proto_ids_) << GetLocation(); |
| 782 | CHECK_LT(&proto_id, proto_ids_ + header_->proto_ids_size_) << GetLocation(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 783 | return &proto_id - proto_ids_; |
| 784 | } |
| 785 | |
| 786 | // 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] | 787 | const ProtoId* FindProtoId(dex::TypeIndex return_type_idx, |
| 788 | const dex::TypeIndex* signature_type_idxs, |
| 789 | uint32_t signature_length) const; |
| 790 | const ProtoId* FindProtoId(dex::TypeIndex return_type_idx, |
| 791 | const std::vector<dex::TypeIndex>& signature_type_idxs) const { |
Vladimir Marko | 5c96e6b | 2013-11-14 15:34:17 +0000 | [diff] [blame] | 792 | return FindProtoId(return_type_idx, &signature_type_idxs[0], signature_type_idxs.size()); |
| 793 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 794 | |
| 795 | // 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] | 796 | bool CreateTypeList(const StringPiece& signature, |
| 797 | dex::TypeIndex* return_type_idx, |
| 798 | std::vector<dex::TypeIndex>* param_type_idxs) const; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 799 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 800 | // Create a Signature from the given string signature or return Signature::NoSignature if not |
| 801 | // possible. |
| 802 | const Signature CreateSignature(const StringPiece& signature) const; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 803 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 804 | // Returns the short form method descriptor for the given prototype. |
Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 805 | const char* GetShorty(uint32_t proto_idx) const; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 806 | |
| 807 | const TypeList* GetProtoParameters(const ProtoId& proto_id) const { |
| 808 | if (proto_id.parameters_off_ == 0) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 809 | return nullptr; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 810 | } else { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 811 | const uint8_t* addr = begin_ + proto_id.parameters_off_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 812 | return reinterpret_cast<const TypeList*>(addr); |
| 813 | } |
| 814 | } |
| 815 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 816 | const uint8_t* GetEncodedStaticFieldValuesArray(const ClassDef& class_def) const { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 817 | if (class_def.static_values_off_ == 0) { |
| 818 | return 0; |
| 819 | } else { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 820 | return begin_ + class_def.static_values_off_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 821 | } |
| 822 | } |
| 823 | |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 824 | const uint8_t* GetCallSiteEncodedValuesArray(const CallSiteIdItem& call_site_id) const { |
| 825 | return begin_ + call_site_id.data_off_; |
| 826 | } |
| 827 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 828 | static const TryItem* GetTryItems(const CodeItem& code_item, uint32_t offset); |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 829 | |
| 830 | // Get the base of the encoded data for the given DexCode. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 831 | static const uint8_t* GetCatchHandlerData(const CodeItem& code_item, uint32_t offset) { |
| 832 | const uint8_t* handler_data = |
| 833 | reinterpret_cast<const uint8_t*>(GetTryItems(code_item, code_item.tries_size_)); |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 834 | return handler_data + offset; |
| 835 | } |
| 836 | |
Ian Rogers | dbbc99d | 2013-04-18 16:51:54 -0700 | [diff] [blame] | 837 | // Find which try region is associated with the given address (ie dex pc). Returns -1 if none. |
| 838 | static int32_t FindTryItem(const CodeItem &code_item, uint32_t address); |
| 839 | |
| 840 | // Find the handler offset associated with the given address (ie dex pc). Returns -1 if none. |
| 841 | static int32_t FindCatchHandlerOffset(const CodeItem &code_item, uint32_t address); |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 842 | |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 843 | // Get the pointer to the start of the debugging data |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 844 | const uint8_t* GetDebugInfoStream(const CodeItem* code_item) const { |
David Srbecky | 6852942 | 2015-07-07 19:13:29 +0100 | [diff] [blame] | 845 | // Check that the offset is in bounds. |
| 846 | // Note that although the specification says that 0 should be used if there |
| 847 | // is no debug information, some applications incorrectly use 0xFFFFFFFF. |
| 848 | if (code_item->debug_info_off_ == 0 || code_item->debug_info_off_ >= size_) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 849 | return nullptr; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 850 | } else { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 851 | return begin_ + code_item->debug_info_off_; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 852 | } |
| 853 | } |
| 854 | |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 855 | struct PositionInfo { |
| 856 | PositionInfo() |
| 857 | : address_(0), |
| 858 | line_(0), |
| 859 | source_file_(nullptr), |
| 860 | prologue_end_(false), |
| 861 | epilogue_begin_(false) { |
| 862 | } |
| 863 | |
| 864 | uint32_t address_; // In 16-bit code units. |
| 865 | uint32_t line_; // Source code line number starting at 1. |
| 866 | const char* source_file_; // nullptr if the file from ClassDef still applies. |
| 867 | bool prologue_end_; |
| 868 | bool epilogue_begin_; |
| 869 | }; |
| 870 | |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 871 | // Callback for "new position table entry". |
| 872 | // Returning true causes the decoder to stop early. |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 873 | typedef bool (*DexDebugNewPositionCb)(void* context, const PositionInfo& entry); |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 874 | |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 875 | struct LocalInfo { |
| 876 | LocalInfo() |
| 877 | : name_(nullptr), |
| 878 | descriptor_(nullptr), |
| 879 | signature_(nullptr), |
| 880 | start_address_(0), |
| 881 | end_address_(0), |
| 882 | reg_(0), |
| 883 | is_live_(false) { |
| 884 | } |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 885 | |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 886 | const char* name_; // E.g., list. It can be nullptr if unknown. |
| 887 | const char* descriptor_; // E.g., Ljava/util/LinkedList; |
| 888 | const char* signature_; // E.g., java.util.LinkedList<java.lang.Integer> |
| 889 | uint32_t start_address_; // PC location where the local is first defined. |
| 890 | uint32_t end_address_; // PC location where the local is no longer defined. |
| 891 | uint16_t reg_; // Dex register which stores the values. |
| 892 | bool is_live_; // Is the local defined and live. |
| 893 | }; |
| 894 | |
| 895 | // Callback for "new locals table entry". |
| 896 | typedef void (*DexDebugNewLocalCb)(void* context, const LocalInfo& entry); |
| 897 | |
| 898 | static bool LineNumForPcCb(void* context, const PositionInfo& entry); |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 899 | |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 900 | const AnnotationsDirectoryItem* GetAnnotationsDirectory(const ClassDef& class_def) const { |
| 901 | if (class_def.annotations_off_ == 0) { |
| 902 | return nullptr; |
| 903 | } else { |
| 904 | return reinterpret_cast<const AnnotationsDirectoryItem*>(begin_ + class_def.annotations_off_); |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | const AnnotationSetItem* GetClassAnnotationSet(const AnnotationsDirectoryItem* anno_dir) const { |
| 909 | if (anno_dir->class_annotations_off_ == 0) { |
| 910 | return nullptr; |
| 911 | } else { |
| 912 | return reinterpret_cast<const AnnotationSetItem*>(begin_ + anno_dir->class_annotations_off_); |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | const FieldAnnotationsItem* GetFieldAnnotations(const AnnotationsDirectoryItem* anno_dir) const { |
| 917 | if (anno_dir->fields_size_ == 0) { |
| 918 | return nullptr; |
| 919 | } else { |
| 920 | return reinterpret_cast<const FieldAnnotationsItem*>(&anno_dir[1]); |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | const MethodAnnotationsItem* GetMethodAnnotations(const AnnotationsDirectoryItem* anno_dir) |
| 925 | const { |
| 926 | if (anno_dir->methods_size_ == 0) { |
| 927 | return nullptr; |
| 928 | } else { |
| 929 | // Skip past the header and field annotations. |
| 930 | const uint8_t* addr = reinterpret_cast<const uint8_t*>(&anno_dir[1]); |
| 931 | addr += anno_dir->fields_size_ * sizeof(FieldAnnotationsItem); |
| 932 | return reinterpret_cast<const MethodAnnotationsItem*>(addr); |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | const ParameterAnnotationsItem* GetParameterAnnotations(const AnnotationsDirectoryItem* anno_dir) |
| 937 | const { |
| 938 | if (anno_dir->parameters_size_ == 0) { |
| 939 | return nullptr; |
| 940 | } else { |
| 941 | // Skip past the header, field annotations, and method annotations. |
| 942 | const uint8_t* addr = reinterpret_cast<const uint8_t*>(&anno_dir[1]); |
| 943 | addr += anno_dir->fields_size_ * sizeof(FieldAnnotationsItem); |
| 944 | addr += anno_dir->methods_size_ * sizeof(MethodAnnotationsItem); |
| 945 | return reinterpret_cast<const ParameterAnnotationsItem*>(addr); |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | const AnnotationSetItem* GetFieldAnnotationSetItem(const FieldAnnotationsItem& anno_item) const { |
| 950 | uint32_t offset = anno_item.annotations_off_; |
| 951 | if (offset == 0) { |
| 952 | return nullptr; |
| 953 | } else { |
| 954 | return reinterpret_cast<const AnnotationSetItem*>(begin_ + offset); |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | const AnnotationSetItem* GetMethodAnnotationSetItem(const MethodAnnotationsItem& anno_item) |
| 959 | const { |
| 960 | uint32_t offset = anno_item.annotations_off_; |
| 961 | if (offset == 0) { |
| 962 | return nullptr; |
| 963 | } else { |
| 964 | return reinterpret_cast<const AnnotationSetItem*>(begin_ + offset); |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | const AnnotationSetRefList* GetParameterAnnotationSetRefList( |
| 969 | const ParameterAnnotationsItem* anno_item) const { |
| 970 | uint32_t offset = anno_item->annotations_off_; |
| 971 | if (offset == 0) { |
| 972 | return nullptr; |
| 973 | } |
| 974 | return reinterpret_cast<const AnnotationSetRefList*>(begin_ + offset); |
| 975 | } |
| 976 | |
| 977 | const AnnotationItem* GetAnnotationItem(const AnnotationSetItem* set_item, uint32_t index) const { |
| 978 | DCHECK_LE(index, set_item->size_); |
| 979 | uint32_t offset = set_item->entries_[index]; |
| 980 | if (offset == 0) { |
| 981 | return nullptr; |
| 982 | } else { |
| 983 | return reinterpret_cast<const AnnotationItem*>(begin_ + offset); |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | const AnnotationSetItem* GetSetRefItemItem(const AnnotationSetRefItem* anno_item) const { |
| 988 | uint32_t offset = anno_item->annotations_off_; |
| 989 | if (offset == 0) { |
| 990 | return nullptr; |
| 991 | } |
| 992 | return reinterpret_cast<const AnnotationSetItem*>(begin_ + offset); |
| 993 | } |
| 994 | |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 995 | // Debug info opcodes and constants |
| 996 | enum { |
| 997 | DBG_END_SEQUENCE = 0x00, |
| 998 | DBG_ADVANCE_PC = 0x01, |
| 999 | DBG_ADVANCE_LINE = 0x02, |
| 1000 | DBG_START_LOCAL = 0x03, |
| 1001 | DBG_START_LOCAL_EXTENDED = 0x04, |
| 1002 | DBG_END_LOCAL = 0x05, |
| 1003 | DBG_RESTART_LOCAL = 0x06, |
| 1004 | DBG_SET_PROLOGUE_END = 0x07, |
| 1005 | DBG_SET_EPILOGUE_BEGIN = 0x08, |
| 1006 | DBG_SET_FILE = 0x09, |
| 1007 | DBG_FIRST_SPECIAL = 0x0a, |
| 1008 | DBG_LINE_BASE = -4, |
| 1009 | DBG_LINE_RANGE = 15, |
| 1010 | }; |
| 1011 | |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 1012 | struct LineNumFromPcContext { |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 1013 | LineNumFromPcContext(uint32_t address, uint32_t line_num) |
| 1014 | : address_(address), line_num_(line_num) {} |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 1015 | uint32_t address_; |
| 1016 | uint32_t line_num_; |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 1017 | private: |
| 1018 | DISALLOW_COPY_AND_ASSIGN(LineNumFromPcContext); |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 1019 | }; |
| 1020 | |
Roland Levillain | 91d65e0 | 2016-01-19 15:59:16 +0000 | [diff] [blame] | 1021 | // Returns false if there is no debugging information or if it cannot be decoded. |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1022 | bool DecodeDebugLocalInfo(const CodeItem* code_item, bool is_static, uint32_t method_idx, |
| 1023 | DexDebugNewLocalCb local_cb, void* context) const; |
| 1024 | |
Roland Levillain | 91d65e0 | 2016-01-19 15:59:16 +0000 | [diff] [blame] | 1025 | // Returns false if there is no debugging information or if it cannot be decoded. |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1026 | bool DecodeDebugPositionInfo(const CodeItem* code_item, DexDebugNewPositionCb position_cb, |
| 1027 | void* context) const; |
Shih-wei Liao | 195487c | 2011-08-20 13:29:04 -0700 | [diff] [blame] | 1028 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1029 | const char* GetSourceFile(const ClassDef& class_def) const { |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 1030 | if (!class_def.source_file_idx_.IsValid()) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1031 | return nullptr; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1032 | } else { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1033 | return StringDataByIdx(class_def.source_file_idx_); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1034 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1037 | int GetPermissions() const; |
Ian Rogers | 1c849e5 | 2012-06-28 14:00:33 -0700 | [diff] [blame] | 1038 | |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 1039 | bool IsReadOnly() const; |
| 1040 | |
Brian Carlstrom | e0948e1 | 2013-08-29 09:36:15 -0700 | [diff] [blame] | 1041 | bool EnableWrite() const; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 1042 | |
Brian Carlstrom | e0948e1 | 2013-08-29 09:36:15 -0700 | [diff] [blame] | 1043 | bool DisableWrite() const; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 1044 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1045 | const uint8_t* Begin() const { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 1046 | return begin_; |
| 1047 | } |
| 1048 | |
| 1049 | size_t Size() const { |
| 1050 | return size_; |
| 1051 | } |
| 1052 | |
Andreas Gampe | 90e3404 | 2015-04-27 20:01:52 -0700 | [diff] [blame] | 1053 | // Return the name of the index-th classes.dex in a multidex zip file. This is classes.dex for |
| 1054 | // index == 0, and classes{index + 1}.dex else. |
| 1055 | static std::string GetMultiDexClassesDexName(size_t index); |
| 1056 | |
| 1057 | // Return the (possibly synthetic) dex location for a multidex entry. This is dex_location for |
| 1058 | // index == 0, and dex_location + multi-dex-separator + GetMultiDexClassesDexName(index) else. |
| 1059 | static std::string GetMultiDexLocation(size_t index, const char* dex_location); |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 1060 | |
| 1061 | // Returns the canonical form of the given dex location. |
| 1062 | // |
| 1063 | // There are different flavors of "dex locations" as follows: |
| 1064 | // the file name of a dex file: |
| 1065 | // The actual file path that the dex file has on disk. |
| 1066 | // dex_location: |
| 1067 | // This acts as a key for the class linker to know which dex file to load. |
| 1068 | // It may correspond to either an old odex file or a particular dex file |
| 1069 | // inside an oat file. In the first case it will also match the file name |
| 1070 | // of the dex file. In the second case (oat) it will include the file name |
| 1071 | // and possibly some multidex annotation to uniquely identify it. |
| 1072 | // canonical_dex_location: |
| 1073 | // the dex_location where it's file name part has been made canonical. |
| 1074 | static std::string GetDexCanonicalLocation(const char* dex_location); |
| 1075 | |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 1076 | const OatDexFile* GetOatDexFile() const { |
| 1077 | return oat_dex_file_; |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 1078 | } |
| 1079 | |
Mathieu Chartier | 1b86849 | 2016-11-16 16:22:37 -0800 | [diff] [blame] | 1080 | // Used by oat writer. |
| 1081 | void SetOatDexFile(OatDexFile* oat_dex_file) const { |
| 1082 | oat_dex_file_ = oat_dex_file; |
| 1083 | } |
| 1084 | |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1085 | // Utility methods for reading integral values from a buffer. |
| 1086 | static int32_t ReadSignedInt(const uint8_t* ptr, int zwidth); |
| 1087 | static uint32_t ReadUnsignedInt(const uint8_t* ptr, int zwidth, bool fill_on_right); |
| 1088 | static int64_t ReadSignedLong(const uint8_t* ptr, int zwidth); |
| 1089 | static uint64_t ReadUnsignedLong(const uint8_t* ptr, int zwidth, bool fill_on_right); |
| 1090 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1091 | // Returns a human-readable form of the method at an index. |
| 1092 | std::string PrettyMethod(uint32_t method_idx, bool with_signature = true) const; |
| 1093 | // Returns a human-readable form of the field at an index. |
| 1094 | std::string PrettyField(uint32_t field_idx, bool with_type = true) const; |
| 1095 | // Returns a human-readable form of the type at an index. |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1096 | std::string PrettyType(dex::TypeIndex type_idx) const; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1097 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1098 | private: |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 1099 | static std::unique_ptr<const DexFile> OpenFile(int fd, |
David Sehr | 733ddb2 | 2016-09-19 15:02:18 -0700 | [diff] [blame] | 1100 | const std::string& location, |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 1101 | bool verify, |
| 1102 | bool verify_checksum, |
| 1103 | std::string* error_msg); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1104 | |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1105 | enum class ZipOpenErrorCode { // private |
| 1106 | kNoError, |
| 1107 | kEntryNotFound, |
| 1108 | kExtractToMemoryError, |
| 1109 | kDexFileError, |
| 1110 | kMakeReadOnlyError, |
| 1111 | kVerifyError |
| 1112 | }; |
| 1113 | |
David Sehr | 733ddb2 | 2016-09-19 15:02:18 -0700 | [diff] [blame] | 1114 | // Open all classesXXX.dex files from a zip archive. |
| 1115 | static bool OpenAllDexFilesFromZip(const ZipArchive& zip_archive, |
| 1116 | const std::string& location, |
| 1117 | bool verify_checksum, |
| 1118 | std::string* error_msg, |
| 1119 | std::vector<std::unique_ptr<const DexFile>>* dex_files); |
| 1120 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1121 | // Opens .dex file from the entry_name in a zip archive. error_code is undefined when non-null |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1122 | // return. |
David Sehr | 733ddb2 | 2016-09-19 15:02:18 -0700 | [diff] [blame] | 1123 | static std::unique_ptr<const DexFile> OpenOneDexFileFromZip(const ZipArchive& zip_archive, |
| 1124 | const char* entry_name, |
| 1125 | const std::string& location, |
| 1126 | bool verify_checksum, |
| 1127 | std::string* error_msg, |
| 1128 | ZipOpenErrorCode* error_code); |
| 1129 | |
| 1130 | enum class VerifyResult { // private |
David Sehr | 9fddd36 | 2016-09-22 14:05:37 -0700 | [diff] [blame] | 1131 | kVerifyNotAttempted, |
David Sehr | 733ddb2 | 2016-09-19 15:02:18 -0700 | [diff] [blame] | 1132 | kVerifySucceeded, |
| 1133 | kVerifyFailed |
| 1134 | }; |
| 1135 | |
| 1136 | static std::unique_ptr<DexFile> OpenCommon(const uint8_t* base, |
| 1137 | size_t size, |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 1138 | const std::string& location, |
David Sehr | 733ddb2 | 2016-09-19 15:02:18 -0700 | [diff] [blame] | 1139 | uint32_t location_checksum, |
| 1140 | const OatDexFile* oat_dex_file, |
| 1141 | bool verify, |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 1142 | bool verify_checksum, |
| 1143 | std::string* error_msg, |
David Sehr | 733ddb2 | 2016-09-19 15:02:18 -0700 | [diff] [blame] | 1144 | VerifyResult* verify_result = nullptr); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1145 | |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 1146 | |
| 1147 | // Opens a .dex file at the given address, optionally backed by a MemMap |
| 1148 | static std::unique_ptr<const DexFile> OpenMemory(const uint8_t* dex_file, |
| 1149 | size_t size, |
| 1150 | const std::string& location, |
| 1151 | uint32_t location_checksum, |
| 1152 | std::unique_ptr<MemMap> mem_map, |
| 1153 | const OatDexFile* oat_dex_file, |
| 1154 | std::string* error_msg); |
| 1155 | |
David Sehr | 733ddb2 | 2016-09-19 15:02:18 -0700 | [diff] [blame] | 1156 | DexFile(const uint8_t* base, |
| 1157 | size_t size, |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 1158 | const std::string& location, |
| 1159 | uint32_t location_checksum, |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 1160 | const OatDexFile* oat_dex_file); |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame] | 1161 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1162 | // Top-level initializer that calls other Init methods. |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1163 | bool Init(std::string* error_msg); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1164 | |
Brian Carlstrom | 6e3b1d9 | 2012-01-11 01:36:32 -0800 | [diff] [blame] | 1165 | // 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] | 1166 | bool CheckMagicAndVersion(std::string* error_msg) const; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1167 | |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 1168 | // Initialize section info for sections only found in map. Returns true on success. |
| 1169 | void InitializeSectionsFromMapList(); |
| 1170 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1171 | // The base address of the memory mapping. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1172 | const uint8_t* const begin_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1173 | |
| 1174 | // The size of the underlying memory allocation in bytes. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1175 | const size_t size_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1176 | |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 1177 | // Typically the dex file name when available, alternatively some identifying string. |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1178 | // |
| 1179 | // The ClassLinker will use this to match DexFiles the boot class |
| 1180 | // path to DexCache::GetLocation when loading from an image. |
| 1181 | const std::string location_; |
| 1182 | |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1183 | const uint32_t location_checksum_; |
| 1184 | |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 1185 | // Manages the underlying memory allocation. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1186 | std::unique_ptr<MemMap> mem_map_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1187 | |
| 1188 | // Points to the header section. |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 1189 | const Header* const header_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1190 | |
| 1191 | // Points to the base of the string identifier list. |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 1192 | const StringId* const string_ids_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1193 | |
| 1194 | // Points to the base of the type identifier list. |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 1195 | const TypeId* const type_ids_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1196 | |
| 1197 | // Points to the base of the field identifier list. |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 1198 | const FieldId* const field_ids_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1199 | |
| 1200 | // Points to the base of the method identifier list. |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 1201 | const MethodId* const method_ids_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1202 | |
| 1203 | // Points to the base of the prototype identifier list. |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 1204 | const ProtoId* const proto_ids_; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1205 | |
| 1206 | // Points to the base of the class definition list. |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 1207 | const ClassDef* const class_defs_; |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 1208 | |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 1209 | // Points to the base of the method handles list. |
| 1210 | const MethodHandleItem* method_handles_; |
| 1211 | |
| 1212 | // Number of elements in the method handles list. |
| 1213 | size_t num_method_handles_; |
| 1214 | |
| 1215 | // Points to the base of the call sites id list. |
| 1216 | const CallSiteIdItem* call_site_ids_; |
| 1217 | |
| 1218 | // Number of elements in the call sites list. |
| 1219 | size_t num_call_site_ids_; |
| 1220 | |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 1221 | // If this dex file was loaded from an oat file, oat_dex_file_ contains a |
| 1222 | // pointer to the OatDexFile it was loaded from. Otherwise oat_dex_file_ is |
| 1223 | // null. |
Mathieu Chartier | 1b86849 | 2016-11-16 16:22:37 -0800 | [diff] [blame] | 1224 | mutable const OatDexFile* oat_dex_file_; |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 1225 | |
| 1226 | friend class DexFileVerifierTest; |
Mathieu Chartier | 1b86849 | 2016-11-16 16:22:37 -0800 | [diff] [blame] | 1227 | friend class OatWriter; |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 1228 | ART_FRIEND_TEST(ClassLinkerTest, RegisterDexFileName); // for constructor |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1229 | }; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1230 | |
| 1231 | struct DexFileReference { |
| 1232 | DexFileReference(const DexFile* file, uint32_t idx) : dex_file(file), index(idx) { } |
| 1233 | const DexFile* dex_file; |
| 1234 | uint32_t index; |
| 1235 | }; |
| 1236 | |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 1237 | std::ostream& operator<<(std::ostream& os, const DexFile& dex_file); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1238 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1239 | // Iterate over a dex file's ProtoId's paramters |
| 1240 | class DexFileParameterIterator { |
| 1241 | public: |
| 1242 | DexFileParameterIterator(const DexFile& dex_file, const DexFile::ProtoId& proto_id) |
| 1243 | : dex_file_(dex_file), size_(0), pos_(0) { |
| 1244 | type_list_ = dex_file_.GetProtoParameters(proto_id); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1245 | if (type_list_ != nullptr) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1246 | size_ = type_list_->Size(); |
| 1247 | } |
| 1248 | } |
| 1249 | bool HasNext() const { return pos_ < size_; } |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1250 | size_t Size() const { return size_; } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1251 | void Next() { ++pos_; } |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1252 | dex::TypeIndex GetTypeIdx() { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1253 | return type_list_->GetTypeItem(pos_).type_idx_; |
| 1254 | } |
| 1255 | const char* GetDescriptor() { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1256 | return dex_file_.StringByTypeIdx(dex::TypeIndex(GetTypeIdx())); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1257 | } |
| 1258 | private: |
| 1259 | const DexFile& dex_file_; |
| 1260 | const DexFile::TypeList* type_list_; |
| 1261 | uint32_t size_; |
| 1262 | uint32_t pos_; |
| 1263 | DISALLOW_IMPLICIT_CONSTRUCTORS(DexFileParameterIterator); |
| 1264 | }; |
| 1265 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 1266 | // Abstract the signature of a method. |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 1267 | class Signature : public ValueObject { |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 1268 | public: |
| 1269 | std::string ToString() const; |
| 1270 | |
| 1271 | static Signature NoSignature() { |
| 1272 | return Signature(); |
| 1273 | } |
| 1274 | |
Orion Hodson | 6c4921b | 2016-09-21 15:41:06 +0100 | [diff] [blame] | 1275 | bool IsVoid() const; |
| 1276 | uint32_t GetNumberOfParameters() const; |
| 1277 | |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 1278 | bool operator==(const Signature& rhs) const; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 1279 | bool operator!=(const Signature& rhs) const { |
| 1280 | return !(*this == rhs); |
| 1281 | } |
| 1282 | |
Vladimir Marko | d9cffea | 2013-11-25 15:08:02 +0000 | [diff] [blame] | 1283 | bool operator==(const StringPiece& rhs) const; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 1284 | |
| 1285 | private: |
| 1286 | Signature(const DexFile* dex, const DexFile::ProtoId& proto) : dex_file_(dex), proto_id_(&proto) { |
| 1287 | } |
| 1288 | |
| 1289 | Signature() : dex_file_(nullptr), proto_id_(nullptr) { |
| 1290 | } |
| 1291 | |
| 1292 | friend class DexFile; |
| 1293 | |
| 1294 | const DexFile* const dex_file_; |
| 1295 | const DexFile::ProtoId* const proto_id_; |
| 1296 | }; |
| 1297 | std::ostream& operator<<(std::ostream& os, const Signature& sig); |
| 1298 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1299 | // Iterate and decode class_data_item |
| 1300 | class ClassDataItemIterator { |
| 1301 | public: |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1302 | ClassDataItemIterator(const DexFile& dex_file, const uint8_t* raw_class_data_item) |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1303 | : dex_file_(dex_file), pos_(0), ptr_pos_(raw_class_data_item), last_idx_(0) { |
| 1304 | ReadClassDataHeader(); |
| 1305 | if (EndOfInstanceFieldsPos() > 0) { |
| 1306 | ReadClassDataField(); |
| 1307 | } else if (EndOfVirtualMethodsPos() > 0) { |
| 1308 | ReadClassDataMethod(); |
| 1309 | } |
| 1310 | } |
| 1311 | uint32_t NumStaticFields() const { |
| 1312 | return header_.static_fields_size_; |
| 1313 | } |
| 1314 | uint32_t NumInstanceFields() const { |
| 1315 | return header_.instance_fields_size_; |
| 1316 | } |
| 1317 | uint32_t NumDirectMethods() const { |
| 1318 | return header_.direct_methods_size_; |
| 1319 | } |
| 1320 | uint32_t NumVirtualMethods() const { |
| 1321 | return header_.virtual_methods_size_; |
| 1322 | } |
| 1323 | bool HasNextStaticField() const { |
| 1324 | return pos_ < EndOfStaticFieldsPos(); |
| 1325 | } |
| 1326 | bool HasNextInstanceField() const { |
| 1327 | return pos_ >= EndOfStaticFieldsPos() && pos_ < EndOfInstanceFieldsPos(); |
| 1328 | } |
| 1329 | bool HasNextDirectMethod() const { |
| 1330 | return pos_ >= EndOfInstanceFieldsPos() && pos_ < EndOfDirectMethodsPos(); |
| 1331 | } |
| 1332 | bool HasNextVirtualMethod() const { |
| 1333 | return pos_ >= EndOfDirectMethodsPos() && pos_ < EndOfVirtualMethodsPos(); |
| 1334 | } |
| 1335 | bool HasNext() const { |
| 1336 | return pos_ < EndOfVirtualMethodsPos(); |
| 1337 | } |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 1338 | inline void Next() { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1339 | pos_++; |
| 1340 | if (pos_ < EndOfStaticFieldsPos()) { |
| 1341 | last_idx_ = GetMemberIndex(); |
| 1342 | ReadClassDataField(); |
| 1343 | } else if (pos_ == EndOfStaticFieldsPos() && NumInstanceFields() > 0) { |
| 1344 | last_idx_ = 0; // transition to next array, reset last index |
| 1345 | ReadClassDataField(); |
| 1346 | } else if (pos_ < EndOfInstanceFieldsPos()) { |
| 1347 | last_idx_ = GetMemberIndex(); |
| 1348 | ReadClassDataField(); |
| 1349 | } else if (pos_ == EndOfInstanceFieldsPos() && NumDirectMethods() > 0) { |
| 1350 | last_idx_ = 0; // transition to next array, reset last index |
| 1351 | ReadClassDataMethod(); |
| 1352 | } else if (pos_ < EndOfDirectMethodsPos()) { |
| 1353 | last_idx_ = GetMemberIndex(); |
| 1354 | ReadClassDataMethod(); |
| 1355 | } else if (pos_ == EndOfDirectMethodsPos() && NumVirtualMethods() > 0) { |
| 1356 | last_idx_ = 0; // transition to next array, reset last index |
| 1357 | ReadClassDataMethod(); |
| 1358 | } else if (pos_ < EndOfVirtualMethodsPos()) { |
| 1359 | last_idx_ = GetMemberIndex(); |
| 1360 | ReadClassDataMethod(); |
| 1361 | } else { |
| 1362 | DCHECK(!HasNext()); |
| 1363 | } |
| 1364 | } |
| 1365 | uint32_t GetMemberIndex() const { |
| 1366 | if (pos_ < EndOfInstanceFieldsPos()) { |
| 1367 | return last_idx_ + field_.field_idx_delta_; |
| 1368 | } else { |
Sebastien Hertz | b24bd99 | 2013-08-02 15:19:09 +0200 | [diff] [blame] | 1369 | DCHECK_LT(pos_, EndOfVirtualMethodsPos()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1370 | return last_idx_ + method_.method_idx_delta_; |
| 1371 | } |
| 1372 | } |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 1373 | uint32_t GetRawMemberAccessFlags() const { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1374 | if (pos_ < EndOfInstanceFieldsPos()) { |
| 1375 | return field_.access_flags_; |
| 1376 | } else { |
Sebastien Hertz | b24bd99 | 2013-08-02 15:19:09 +0200 | [diff] [blame] | 1377 | DCHECK_LT(pos_, EndOfVirtualMethodsPos()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1378 | return method_.access_flags_; |
| 1379 | } |
| 1380 | } |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 1381 | uint32_t GetFieldAccessFlags() const { |
| 1382 | return GetRawMemberAccessFlags() & kAccValidFieldFlags; |
| 1383 | } |
| 1384 | uint32_t GetMethodAccessFlags() const { |
| 1385 | return GetRawMemberAccessFlags() & kAccValidMethodFlags; |
| 1386 | } |
| 1387 | bool MemberIsNative() const { |
| 1388 | return GetRawMemberAccessFlags() & kAccNative; |
| 1389 | } |
| 1390 | bool MemberIsFinal() const { |
| 1391 | return GetRawMemberAccessFlags() & kAccFinal; |
| 1392 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 1393 | InvokeType GetMethodInvokeType(const DexFile::ClassDef& class_def) const { |
| 1394 | if (HasNextDirectMethod()) { |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 1395 | if ((GetRawMemberAccessFlags() & kAccStatic) != 0) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 1396 | return kStatic; |
| 1397 | } else { |
| 1398 | return kDirect; |
| 1399 | } |
| 1400 | } else { |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 1401 | DCHECK_EQ(GetRawMemberAccessFlags() & kAccStatic, 0U); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 1402 | if ((class_def.access_flags_ & kAccInterface) != 0) { |
| 1403 | return kInterface; |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 1404 | } else if ((GetRawMemberAccessFlags() & kAccConstructor) != 0) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 1405 | return kSuper; |
| 1406 | } else { |
| 1407 | return kVirtual; |
| 1408 | } |
| 1409 | } |
| 1410 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1411 | const DexFile::CodeItem* GetMethodCodeItem() const { |
| 1412 | return dex_file_.GetCodeItem(method_.code_off_); |
| 1413 | } |
| 1414 | uint32_t GetMethodCodeItemOffset() const { |
| 1415 | return method_.code_off_; |
| 1416 | } |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 1417 | const uint8_t* DataPointer() const { |
| 1418 | return ptr_pos_; |
| 1419 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1420 | const uint8_t* EndDataPointer() const { |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1421 | CHECK(!HasNext()); |
| 1422 | return ptr_pos_; |
| 1423 | } |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 1424 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1425 | private: |
| 1426 | // A dex file's class_data_item is leb128 encoded, this structure holds a decoded form of the |
| 1427 | // header for a class_data_item |
| 1428 | struct ClassDataHeader { |
| 1429 | uint32_t static_fields_size_; // the number of static fields |
| 1430 | uint32_t instance_fields_size_; // the number of instance fields |
| 1431 | uint32_t direct_methods_size_; // the number of direct methods |
| 1432 | uint32_t virtual_methods_size_; // the number of virtual methods |
| 1433 | } header_; |
| 1434 | |
| 1435 | // Read and decode header from a class_data_item stream into header |
| 1436 | void ReadClassDataHeader(); |
| 1437 | |
| 1438 | uint32_t EndOfStaticFieldsPos() const { |
| 1439 | return header_.static_fields_size_; |
| 1440 | } |
| 1441 | uint32_t EndOfInstanceFieldsPos() const { |
| 1442 | return EndOfStaticFieldsPos() + header_.instance_fields_size_; |
| 1443 | } |
| 1444 | uint32_t EndOfDirectMethodsPos() const { |
| 1445 | return EndOfInstanceFieldsPos() + header_.direct_methods_size_; |
| 1446 | } |
| 1447 | uint32_t EndOfVirtualMethodsPos() const { |
| 1448 | return EndOfDirectMethodsPos() + header_.virtual_methods_size_; |
| 1449 | } |
| 1450 | |
| 1451 | // A decoded version of the field of a class_data_item |
| 1452 | struct ClassDataField { |
| 1453 | uint32_t field_idx_delta_; // delta of index into the field_ids array for FieldId |
| 1454 | uint32_t access_flags_; // access flags for the field |
| 1455 | ClassDataField() : field_idx_delta_(0), access_flags_(0) {} |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 1456 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1457 | private: |
| 1458 | DISALLOW_COPY_AND_ASSIGN(ClassDataField); |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 1459 | }; |
| 1460 | ClassDataField field_; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1461 | |
| 1462 | // Read and decode a field from a class_data_item stream into field |
| 1463 | void ReadClassDataField(); |
| 1464 | |
| 1465 | // A decoded version of the method of a class_data_item |
| 1466 | struct ClassDataMethod { |
| 1467 | uint32_t method_idx_delta_; // delta of index into the method_ids array for MethodId |
| 1468 | uint32_t access_flags_; |
| 1469 | uint32_t code_off_; |
| 1470 | ClassDataMethod() : method_idx_delta_(0), access_flags_(0), code_off_(0) {} |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 1471 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1472 | private: |
| 1473 | DISALLOW_COPY_AND_ASSIGN(ClassDataMethod); |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 1474 | }; |
| 1475 | ClassDataMethod method_; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1476 | |
| 1477 | // Read and decode a method from a class_data_item stream into method |
| 1478 | void ReadClassDataMethod(); |
| 1479 | |
| 1480 | const DexFile& dex_file_; |
| 1481 | size_t pos_; // integral number of items passed |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1482 | const uint8_t* ptr_pos_; // pointer into stream of class_data_item |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1483 | uint32_t last_idx_; // last read field or method index to apply delta to |
| 1484 | DISALLOW_IMPLICIT_CONSTRUCTORS(ClassDataItemIterator); |
| 1485 | }; |
| 1486 | |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 1487 | class EncodedArrayValueIterator { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1488 | public: |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 1489 | EncodedArrayValueIterator(const DexFile& dex_file, const uint8_t* array_data); |
Shinichiro Hamaji | 82863f0 | 2015-11-05 16:51:33 +0900 | [diff] [blame] | 1490 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1491 | bool HasNext() const { return pos_ < array_size_; } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1492 | |
| 1493 | void Next(); |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 1494 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1495 | enum ValueType { |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 1496 | kByte = 0x00, |
| 1497 | kShort = 0x02, |
| 1498 | kChar = 0x03, |
| 1499 | kInt = 0x04, |
| 1500 | kLong = 0x06, |
| 1501 | kFloat = 0x10, |
| 1502 | kDouble = 0x11, |
| 1503 | kMethodType = 0x15, |
| 1504 | kMethodHandle = 0x16, |
| 1505 | kString = 0x17, |
| 1506 | kType = 0x18, |
| 1507 | kField = 0x19, |
| 1508 | kMethod = 0x1a, |
| 1509 | kEnum = 0x1b, |
| 1510 | kArray = 0x1c, |
| 1511 | kAnnotation = 0x1d, |
| 1512 | kNull = 0x1e, |
| 1513 | kBoolean = 0x1f, |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1514 | }; |
| 1515 | |
Shinichiro Hamaji | 82863f0 | 2015-11-05 16:51:33 +0900 | [diff] [blame] | 1516 | ValueType GetValueType() const { return type_; } |
| 1517 | const jvalue& GetJavaValue() const { return jval_; } |
| 1518 | |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1519 | protected: |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1520 | static constexpr uint8_t kEncodedValueTypeMask = 0x1f; // 0b11111 |
| 1521 | static constexpr uint8_t kEncodedValueArgShift = 5; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1522 | |
| 1523 | const DexFile& dex_file_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1524 | size_t array_size_; // Size of array. |
| 1525 | size_t pos_; // Current position. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1526 | const uint8_t* ptr_; // Pointer into encoded data array. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1527 | ValueType type_; // Type of current encoded value. |
| 1528 | jvalue jval_; // Value of current encoded value. |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1529 | |
| 1530 | private: |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 1531 | DISALLOW_IMPLICIT_CONSTRUCTORS(EncodedArrayValueIterator); |
| 1532 | }; |
| 1533 | std::ostream& operator<<(std::ostream& os, const EncodedArrayValueIterator::ValueType& code); |
| 1534 | |
| 1535 | class EncodedStaticFieldValueIterator : public EncodedArrayValueIterator { |
| 1536 | public: |
| 1537 | EncodedStaticFieldValueIterator(const DexFile& dex_file, |
| 1538 | const DexFile::ClassDef& class_def) |
| 1539 | : EncodedArrayValueIterator(dex_file, |
| 1540 | dex_file.GetEncodedStaticFieldValuesArray(class_def)) |
| 1541 | {} |
| 1542 | |
| 1543 | private: |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1544 | DISALLOW_IMPLICIT_CONSTRUCTORS(EncodedStaticFieldValueIterator); |
| 1545 | }; |
Brian Carlstrom | 88f3654 | 2012-10-16 23:24:21 -0700 | [diff] [blame] | 1546 | std::ostream& operator<<(std::ostream& os, const EncodedStaticFieldValueIterator::ValueType& code); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1547 | |
Orion Hodson | 12f4ff4 | 2017-01-13 16:43:12 +0000 | [diff] [blame] | 1548 | class CallSiteArrayValueIterator : public EncodedArrayValueIterator { |
| 1549 | public: |
| 1550 | CallSiteArrayValueIterator(const DexFile& dex_file, |
| 1551 | const DexFile::CallSiteIdItem& call_site_id) |
| 1552 | : EncodedArrayValueIterator(dex_file, |
| 1553 | dex_file.GetCallSiteEncodedValuesArray(call_site_id)) |
| 1554 | {} |
| 1555 | |
| 1556 | uint32_t Size() const { return array_size_; } |
| 1557 | |
| 1558 | private: |
| 1559 | DISALLOW_IMPLICIT_CONSTRUCTORS(CallSiteArrayValueIterator); |
| 1560 | }; |
| 1561 | std::ostream& operator<<(std::ostream& os, const CallSiteArrayValueIterator::ValueType& code); |
| 1562 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1563 | class CatchHandlerIterator { |
| 1564 | public: |
| 1565 | CatchHandlerIterator(const DexFile::CodeItem& code_item, uint32_t address); |
Logan Chien | 736df02 | 2012-04-27 16:25:57 +0800 | [diff] [blame] | 1566 | |
| 1567 | CatchHandlerIterator(const DexFile::CodeItem& code_item, |
| 1568 | const DexFile::TryItem& try_item); |
| 1569 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1570 | explicit CatchHandlerIterator(const uint8_t* handler_data) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1571 | Init(handler_data); |
| 1572 | } |
| 1573 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1574 | dex::TypeIndex GetHandlerTypeIndex() const { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1575 | return handler_.type_idx_; |
| 1576 | } |
| 1577 | uint32_t GetHandlerAddress() const { |
| 1578 | return handler_.address_; |
| 1579 | } |
| 1580 | void Next(); |
| 1581 | bool HasNext() const { |
| 1582 | return remaining_count_ != -1 || catch_all_; |
| 1583 | } |
| 1584 | // End of this set of catch blocks, convenience method to locate next set of catch blocks |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1585 | const uint8_t* EndDataPointer() const { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1586 | CHECK(!HasNext()); |
| 1587 | return current_data_; |
| 1588 | } |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 1589 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1590 | private: |
Logan Chien | 736df02 | 2012-04-27 16:25:57 +0800 | [diff] [blame] | 1591 | void Init(const DexFile::CodeItem& code_item, int32_t offset); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1592 | void Init(const uint8_t* handler_data); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1593 | |
| 1594 | struct CatchHandlerItem { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1595 | dex::TypeIndex type_idx_; // type index of the caught exception type |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1596 | uint32_t address_; // handler address |
| 1597 | } handler_; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1598 | const uint8_t* current_data_; // the current handler in dex file. |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1599 | int32_t remaining_count_; // number of handlers not read. |
| 1600 | bool catch_all_; // is there a handler that will catch all exceptions in case |
| 1601 | // that all typed handler does not match. |
| 1602 | }; |
| 1603 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1604 | } // namespace art |
| 1605 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 1606 | #endif // ART_RUNTIME_DEX_FILE_H_ |