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