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