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