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