blob: 990ab118e7daeb39ccc0e3669e57248da01f8601 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 Shapiro1fb86202011-06-27 17:43:13 -070016
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_DEX_FILE_H_
18#define ART_RUNTIME_DEX_FILE_H_
Carl Shapiro1fb86202011-06-27 17:43:13 -070019
Ian Rogers700a4022014-05-19 16:49:03 -070020#include <memory>
Elliott Hughes0c424cb2011-08-26 10:16:25 -070021#include <string>
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070022#include <vector>
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070023
Elliott Hughes07ed66b2012-12-12 18:34:25 -080024#include "base/logging.h"
Ian Rogers03b6eaf2014-10-28 09:34:57 -070025#include "base/value_object.h"
Andreas Gampea5b09a62016-11-17 15:21:22 -080026#include "dex_file_types.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070027#include "globals.h"
Ian Rogers08f753d2012-08-24 14:35:25 -070028#include "invoke_type.h"
Jesse Wilson6bf19152011-09-29 13:12:33 -040029#include "jni.h"
Ian Rogers08f753d2012-08-24 14:35:25 -070030#include "modifiers.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070031
32namespace art {
33
Ian Rogers576ca0c2014-06-06 15:58:22 -070034class MemMap;
Richard Uhler07b3c232015-03-31 15:57:54 -070035class OatDexFile;
Ian Rogersd91d6d62013-09-25 20:26:14 -070036class Signature;
Ian Rogersfc0e94b2013-09-23 23:51:32 -070037class StringPiece;
Brian Carlstroma6cc8932012-01-04 14:44:07 -080038class ZipArchive;
39
Brian Carlstromf615a612011-07-23 12:50:34 -070040class DexFile {
Carl Shapiro1fb86202011-06-27 17:43:13 -070041 public:
Roland Levillain621b5ea2016-05-18 11:41:33 +010042 // First Dex format version supporting default methods.
Alex Lightb55f1ac2016-04-12 15:50:55 -070043 static const uint32_t kDefaultMethodsVersion = 37;
Roland Levillain621b5ea2016-05-18 11:41:33 +010044 // First Dex format version enforcing class definition ordering rules.
45 static const uint32_t kClassDefinitionOrderEnforcedVersion = 37;
46
Ian Rogers13735952014-10-08 12:43:28 -070047 static const uint8_t kDexMagic[];
Narayan Kamath52e66502016-08-01 14:20:31 +010048 static constexpr size_t kNumDexVersions = 3;
Alex Lightc4961812016-03-23 10:20:41 -070049 static constexpr size_t kDexVersionLen = 4;
50 static const uint8_t kDexMagicVersions[kNumDexVersions][kDexVersionLen];
51
Ian Rogers13735952014-10-08 12:43:28 -070052 static constexpr size_t kSha1DigestSize = 20;
53 static constexpr uint32_t kDexEndianConstant = 0x12345678;
Carl Shapiro80d4dde2011-06-28 16:24:07 -070054
Brian Carlstromb7bbba42011-10-13 14:58:47 -070055 // name of the DexFile entry within a zip archive
56 static const char* kClassesDex;
57
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070058 // The value of an invalid index.
59 static const uint32_t kDexNoIndex = 0xFFFFFFFF;
60
Ian Rogers0571d352011-11-03 19:51:38 -070061 // The value of an invalid index.
62 static const uint16_t kDexNoIndex16 = 0xFFFF;
Carl Shapiro1fb86202011-06-27 17:43:13 -070063
Alex Lightc4961812016-03-23 10:20:41 -070064 // The separator character in MultiDex locations.
Calin Juravlea308a322017-07-18 16:51:51 -070065 static constexpr char kMultiDexSeparator = '!';
Andreas Gampe833a4852014-05-21 18:46:59 -070066
67 // A string version of the previous. This is a define so that we can merge string literals in the
68 // preprocessor.
Calin Juravlea308a322017-07-18 16:51:51 -070069 #define kMultiDexSeparatorString "!"
Andreas Gampe833a4852014-05-21 18:46:59 -070070
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070071 // Raw header_item.
72 struct Header {
73 uint8_t magic_[8];
Brian Carlstrom7934ac22013-07-26 10:54:15 -070074 uint32_t checksum_; // See also location_checksum_
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070075 uint8_t signature_[kSha1DigestSize];
jeffhaof6174e82012-01-31 16:14:17 -080076 uint32_t file_size_; // size of entire file
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070077 uint32_t header_size_; // offset to start of next section
78 uint32_t endian_tag_;
Ian Rogers0571d352011-11-03 19:51:38 -070079 uint32_t link_size_; // unused
80 uint32_t link_off_; // unused
81 uint32_t map_off_; // unused
82 uint32_t string_ids_size_; // number of StringIds
83 uint32_t string_ids_off_; // file offset of StringIds array
84 uint32_t type_ids_size_; // number of TypeIds, we don't support more than 65535
85 uint32_t type_ids_off_; // file offset of TypeIds array
86 uint32_t proto_ids_size_; // number of ProtoIds, we don't support more than 65535
87 uint32_t proto_ids_off_; // file offset of ProtoIds array
88 uint32_t field_ids_size_; // number of FieldIds
89 uint32_t field_ids_off_; // file offset of FieldIds array
90 uint32_t method_ids_size_; // number of MethodIds
91 uint32_t method_ids_off_; // file offset of MethodIds array
92 uint32_t class_defs_size_; // number of ClassDefs
93 uint32_t class_defs_off_; // file offset of ClassDef array
Jeff Hao5daee902017-04-27 18:00:38 -070094 uint32_t data_size_; // size of data section
95 uint32_t data_off_; // file offset of data section
Elliott Hughesa21039c2012-06-21 12:09:25 -070096
Andreas Gampe76ed99d2016-03-28 18:31:29 -070097 // Decode the dex magic version
98 uint32_t GetVersion() const;
99
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700100 private:
101 DISALLOW_COPY_AND_ASSIGN(Header);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700102 };
Carl Shapiro1fb86202011-06-27 17:43:13 -0700103
jeffhao10037c82012-01-23 15:06:23 -0800104 // Map item type codes.
Orion Hodson12f4ff42017-01-13 16:43:12 +0000105 enum MapItemType : uint16_t { // private
jeffhao10037c82012-01-23 15:06:23 -0800106 kDexTypeHeaderItem = 0x0000,
107 kDexTypeStringIdItem = 0x0001,
108 kDexTypeTypeIdItem = 0x0002,
109 kDexTypeProtoIdItem = 0x0003,
110 kDexTypeFieldIdItem = 0x0004,
111 kDexTypeMethodIdItem = 0x0005,
112 kDexTypeClassDefItem = 0x0006,
Orion Hodson12f4ff42017-01-13 16:43:12 +0000113 kDexTypeCallSiteIdItem = 0x0007,
114 kDexTypeMethodHandleItem = 0x0008,
jeffhao10037c82012-01-23 15:06:23 -0800115 kDexTypeMapList = 0x1000,
116 kDexTypeTypeList = 0x1001,
117 kDexTypeAnnotationSetRefList = 0x1002,
118 kDexTypeAnnotationSetItem = 0x1003,
119 kDexTypeClassDataItem = 0x2000,
120 kDexTypeCodeItem = 0x2001,
121 kDexTypeStringDataItem = 0x2002,
122 kDexTypeDebugInfoItem = 0x2003,
123 kDexTypeAnnotationItem = 0x2004,
124 kDexTypeEncodedArrayItem = 0x2005,
125 kDexTypeAnnotationsDirectoryItem = 0x2006,
126 };
127
128 struct MapItem {
129 uint16_t type_;
130 uint16_t unused_;
131 uint32_t size_;
132 uint32_t offset_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700133
jeffhao10037c82012-01-23 15:06:23 -0800134 private:
135 DISALLOW_COPY_AND_ASSIGN(MapItem);
136 };
137
138 struct MapList {
139 uint32_t size_;
140 MapItem list_[1];
Elliott Hughesa21039c2012-06-21 12:09:25 -0700141
jeffhao10037c82012-01-23 15:06:23 -0800142 private:
143 DISALLOW_COPY_AND_ASSIGN(MapList);
144 };
145
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700146 // Raw string_id_item.
147 struct StringId {
148 uint32_t string_data_off_; // offset in bytes from the base address
Elliott Hughesa21039c2012-06-21 12:09:25 -0700149
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700150 private:
151 DISALLOW_COPY_AND_ASSIGN(StringId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700152 };
153
154 // Raw type_id_item.
155 struct TypeId {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800156 dex::StringIndex descriptor_idx_; // index into string_ids
Elliott Hughesa21039c2012-06-21 12:09:25 -0700157
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700158 private:
159 DISALLOW_COPY_AND_ASSIGN(TypeId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700160 };
161
162 // Raw field_id_item.
163 struct FieldId {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800164 dex::TypeIndex class_idx_; // index into type_ids_ array for defining class
165 dex::TypeIndex type_idx_; // index into type_ids_ array for field type
166 dex::StringIndex name_idx_; // index into string_ids_ array for field name
Elliott Hughesa21039c2012-06-21 12:09:25 -0700167
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700168 private:
169 DISALLOW_COPY_AND_ASSIGN(FieldId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700170 };
171
Andreas Gampea5b09a62016-11-17 15:21:22 -0800172 // Raw proto_id_item.
173 struct ProtoId {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800174 dex::StringIndex shorty_idx_; // index into string_ids array for shorty descriptor
Andreas Gampea5b09a62016-11-17 15:21:22 -0800175 dex::TypeIndex return_type_idx_; // index into type_ids array for return type
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800176 uint16_t pad_; // padding = 0
177 uint32_t parameters_off_; // file offset to type_list for parameter types
Andreas Gampea5b09a62016-11-17 15:21:22 -0800178
179 private:
180 DISALLOW_COPY_AND_ASSIGN(ProtoId);
181 };
182
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700183 // Raw method_id_item.
184 struct MethodId {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800185 dex::TypeIndex class_idx_; // index into type_ids_ array for defining class
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800186 uint16_t proto_idx_; // index into proto_ids_ array for method prototype
187 dex::StringIndex name_idx_; // index into string_ids_ array for method name
Elliott Hughesa21039c2012-06-21 12:09:25 -0700188
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700189 private:
190 DISALLOW_COPY_AND_ASSIGN(MethodId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700191 };
192
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700193 // Raw class_def_item.
194 struct ClassDef {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800195 dex::TypeIndex class_idx_; // index into type_ids_ array for this class
Ian Rogers0571d352011-11-03 19:51:38 -0700196 uint16_t pad1_; // padding = 0
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700197 uint32_t access_flags_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800198 dex::TypeIndex superclass_idx_; // index into type_ids_ array for superclass
Ian Rogers0571d352011-11-03 19:51:38 -0700199 uint16_t pad2_; // padding = 0
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700200 uint32_t interfaces_off_; // file offset to TypeList
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800201 dex::StringIndex source_file_idx_; // index into string_ids_ for source file name
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700202 uint32_t annotations_off_; // file offset to annotations_directory_item
203 uint32_t class_data_off_; // file offset to class_data_item
204 uint32_t static_values_off_; // file offset to EncodedArray
Elliott Hughesa21039c2012-06-21 12:09:25 -0700205
Andreas Gampe51829322014-08-25 15:05:04 -0700206 // Returns the valid access flags, that is, Java modifier bits relevant to the ClassDef type
207 // (class or interface). These are all in the lower 16b and do not contain runtime flags.
208 uint32_t GetJavaAccessFlags() const {
209 // Make sure that none of our runtime-only flags are set.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800210 static_assert((kAccValidClassFlags & kAccJavaFlagsMask) == kAccValidClassFlags,
211 "Valid class flags not a subset of Java flags");
212 static_assert((kAccValidInterfaceFlags & kAccJavaFlagsMask) == kAccValidInterfaceFlags,
213 "Valid interface flags not a subset of Java flags");
Andreas Gampe51829322014-08-25 15:05:04 -0700214
215 if ((access_flags_ & kAccInterface) != 0) {
216 // Interface.
217 return access_flags_ & kAccValidInterfaceFlags;
218 } else {
219 // Class.
220 return access_flags_ & kAccValidClassFlags;
221 }
222 }
223
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700224 private:
225 DISALLOW_COPY_AND_ASSIGN(ClassDef);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700226 };
227
228 // Raw type_item.
229 struct TypeItem {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800230 dex::TypeIndex type_idx_; // index into type_ids section
Elliott Hughesa21039c2012-06-21 12:09:25 -0700231
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700232 private:
233 DISALLOW_COPY_AND_ASSIGN(TypeItem);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700234 };
235
236 // Raw type_list.
237 class TypeList {
238 public:
239 uint32_t Size() const {
240 return size_;
241 }
242
243 const TypeItem& GetTypeItem(uint32_t idx) const {
Sebastien Hertzb24bd992013-08-02 15:19:09 +0200244 DCHECK_LT(idx, this->size_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700245 return this->list_[idx];
246 }
247
Andreas Gampe31a7a0c2014-08-29 16:07:49 -0700248 // Size in bytes of the part of the list that is common.
249 static constexpr size_t GetHeaderSize() {
250 return 4U;
251 }
252
253 // Size in bytes of the whole type list including all the stored elements.
254 static constexpr size_t GetListSize(size_t count) {
255 return GetHeaderSize() + sizeof(TypeItem) * count;
256 }
257
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700258 private:
259 uint32_t size_; // size of the list, in entries
260 TypeItem list_[1]; // elements of the list
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700261 DISALLOW_COPY_AND_ASSIGN(TypeList);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700262 };
263
Orion Hodson12f4ff42017-01-13 16:43:12 +0000264 // MethodHandle Types
265 enum class MethodHandleType : uint16_t { // private
Orion Hodsonc069a302017-01-18 09:23:12 +0000266 kStaticPut = 0x0000, // a setter for a given static field.
267 kStaticGet = 0x0001, // a getter for a given static field.
268 kInstancePut = 0x0002, // a setter for a given instance field.
269 kInstanceGet = 0x0003, // a getter for a given instance field.
270 kInvokeStatic = 0x0004, // an invoker for a given static method.
Orion Hodson12f4ff42017-01-13 16:43:12 +0000271 kInvokeInstance = 0x0005, // invoke_instance : an invoker for a given instance method. This
272 // can be any non-static method on any class (or interface) except
273 // for “<init>”.
274 kInvokeConstructor = 0x0006, // an invoker for a given constructor.
Orion Hodson631827d2017-04-10 14:53:47 +0100275 kInvokeDirect = 0x0007, // an invoker for a direct (special) method.
276 kInvokeInterface = 0x0008, // an invoker for an interface method.
277 kLast = kInvokeInterface
Orion Hodson12f4ff42017-01-13 16:43:12 +0000278 };
279
280 // raw method_handle_item
281 struct MethodHandleItem {
282 uint16_t method_handle_type_;
Orion Hodsonc069a302017-01-18 09:23:12 +0000283 uint16_t reserved1_; // Reserved for future use.
284 uint16_t field_or_method_idx_; // Field index for accessors, method index otherwise.
285 uint16_t reserved2_; // Reserved for future use.
Orion Hodson12f4ff42017-01-13 16:43:12 +0000286 private:
287 DISALLOW_COPY_AND_ASSIGN(MethodHandleItem);
288 };
289
290 // raw call_site_id_item
291 struct CallSiteIdItem {
292 uint32_t data_off_; // Offset into data section pointing to encoded array items.
293 private:
294 DISALLOW_COPY_AND_ASSIGN(CallSiteIdItem);
295 };
296
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700297 // Raw code_item.
298 struct CodeItem {
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700299 uint16_t registers_size_; // the number of registers used by this code
300 // (locals + parameters)
301 uint16_t ins_size_; // the number of words of incoming arguments to the method
302 // that this code is for
303 uint16_t outs_size_; // the number of words of outgoing argument space required
304 // by this code for method invocation
305 uint16_t tries_size_; // the number of try_items for this instance. If non-zero,
306 // then these appear as the tries array just after the
307 // insns in this instance.
308 uint32_t debug_info_off_; // file offset to debug info stream
Ian Rogersd81871c2011-10-03 13:57:23 -0700309 uint32_t insns_size_in_code_units_; // size of the insns array, in 2 byte code units
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700310 uint16_t insns_[1]; // actual array of bytecode.
Elliott Hughesa21039c2012-06-21 12:09:25 -0700311
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700312 private:
313 DISALLOW_COPY_AND_ASSIGN(CodeItem);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700314 };
315
Carl Shapiro2eaa9682011-08-04 19:26:11 -0700316 // Raw try_item.
317 struct TryItem {
318 uint32_t start_addr_;
319 uint16_t insn_count_;
320 uint16_t handler_off_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700321
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700322 private:
323 DISALLOW_COPY_AND_ASSIGN(TryItem);
Carl Shapiro2eaa9682011-08-04 19:26:11 -0700324 };
325
jeffhao10037c82012-01-23 15:06:23 -0800326 // Annotation constants.
327 enum {
328 kDexVisibilityBuild = 0x00, /* annotation visibility */
329 kDexVisibilityRuntime = 0x01,
330 kDexVisibilitySystem = 0x02,
331
332 kDexAnnotationByte = 0x00,
333 kDexAnnotationShort = 0x02,
334 kDexAnnotationChar = 0x03,
335 kDexAnnotationInt = 0x04,
336 kDexAnnotationLong = 0x06,
337 kDexAnnotationFloat = 0x10,
338 kDexAnnotationDouble = 0x11,
Orion Hodson12f4ff42017-01-13 16:43:12 +0000339 kDexAnnotationMethodType = 0x15,
340 kDexAnnotationMethodHandle = 0x16,
jeffhao10037c82012-01-23 15:06:23 -0800341 kDexAnnotationString = 0x17,
342 kDexAnnotationType = 0x18,
343 kDexAnnotationField = 0x19,
344 kDexAnnotationMethod = 0x1a,
345 kDexAnnotationEnum = 0x1b,
346 kDexAnnotationArray = 0x1c,
347 kDexAnnotationAnnotation = 0x1d,
348 kDexAnnotationNull = 0x1e,
349 kDexAnnotationBoolean = 0x1f,
350
351 kDexAnnotationValueTypeMask = 0x1f, /* low 5 bits */
352 kDexAnnotationValueArgShift = 5,
353 };
354
355 struct AnnotationsDirectoryItem {
356 uint32_t class_annotations_off_;
357 uint32_t fields_size_;
358 uint32_t methods_size_;
359 uint32_t parameters_size_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700360
jeffhao10037c82012-01-23 15:06:23 -0800361 private:
362 DISALLOW_COPY_AND_ASSIGN(AnnotationsDirectoryItem);
363 };
364
365 struct FieldAnnotationsItem {
366 uint32_t field_idx_;
367 uint32_t annotations_off_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700368
jeffhao10037c82012-01-23 15:06:23 -0800369 private:
370 DISALLOW_COPY_AND_ASSIGN(FieldAnnotationsItem);
371 };
372
373 struct MethodAnnotationsItem {
374 uint32_t method_idx_;
375 uint32_t annotations_off_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700376
jeffhao10037c82012-01-23 15:06:23 -0800377 private:
378 DISALLOW_COPY_AND_ASSIGN(MethodAnnotationsItem);
379 };
380
381 struct ParameterAnnotationsItem {
382 uint32_t method_idx_;
383 uint32_t annotations_off_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700384
jeffhao10037c82012-01-23 15:06:23 -0800385 private:
386 DISALLOW_COPY_AND_ASSIGN(ParameterAnnotationsItem);
387 };
388
389 struct AnnotationSetRefItem {
390 uint32_t annotations_off_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700391
jeffhao10037c82012-01-23 15:06:23 -0800392 private:
393 DISALLOW_COPY_AND_ASSIGN(AnnotationSetRefItem);
394 };
395
396 struct AnnotationSetRefList {
397 uint32_t size_;
398 AnnotationSetRefItem list_[1];
Elliott Hughesa21039c2012-06-21 12:09:25 -0700399
jeffhao10037c82012-01-23 15:06:23 -0800400 private:
401 DISALLOW_COPY_AND_ASSIGN(AnnotationSetRefList);
402 };
403
404 struct AnnotationSetItem {
405 uint32_t size_;
406 uint32_t entries_[1];
Elliott Hughesa21039c2012-06-21 12:09:25 -0700407
jeffhao10037c82012-01-23 15:06:23 -0800408 private:
409 DISALLOW_COPY_AND_ASSIGN(AnnotationSetItem);
410 };
411
412 struct AnnotationItem {
413 uint8_t visibility_;
414 uint8_t annotation_[1];
Elliott Hughesa21039c2012-06-21 12:09:25 -0700415
jeffhao10037c82012-01-23 15:06:23 -0800416 private:
417 DISALLOW_COPY_AND_ASSIGN(AnnotationItem);
418 };
419
Jeff Hao13e748b2015-08-25 20:44:19 +0000420 enum AnnotationResultStyle { // private
421 kAllObjects,
422 kPrimitivesOrObjects,
423 kAllRaw
424 };
425
David Sehr9323e6e2016-09-13 08:58:35 -0700426 struct AnnotationValue;
427
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000428 // Returns the checksums of a file for comparison with GetLocationChecksum().
429 // For .dex files, this is the single header checksum.
430 // For zip files, this is the zip entry CRC32 checksum for classes.dex and
431 // each additional multidex entry classes2.dex, classes3.dex, etc.
432 // Return true if the checksums could be found, false otherwise.
433 static bool GetMultiDexChecksums(const char* filename,
434 std::vector<uint32_t>* checksums,
435 std::string* error_msg);
436
Richard Uhler84f50ae2017-02-06 15:12:45 +0000437 // Check whether a location denotes a multidex dex file. This is a very simple check: returns
438 // whether the string contains the separator character.
439 static bool IsMultiDexLocation(const char* location);
440
Brian Carlstrom89521892011-12-07 22:05:07 -0800441 // Opens .dex file, backed by existing memory
David Sehr733ddb22016-09-19 15:02:18 -0700442 static std::unique_ptr<const DexFile> Open(const uint8_t* base,
443 size_t size,
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800444 const std::string& location,
445 uint32_t location_checksum,
Richard Uhler07b3c232015-03-31 15:57:54 -0700446 const OatDexFile* oat_dex_file,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800447 bool verify,
Aart Bik37d6a3b2016-06-21 18:30:10 -0700448 bool verify_checksum,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800449 std::string* error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -0800450
Orion Hodsona4c2a052016-08-17 10:51:42 +0100451 // Opens .dex file that has been memory-mapped by the caller.
452 static std::unique_ptr<const DexFile> Open(const std::string& location,
453 uint32_t location_checkum,
454 std::unique_ptr<MemMap> mem_map,
455 bool verify,
456 bool verify_checksum,
457 std::string* error_msg);
458
David Sehr733ddb22016-09-19 15:02:18 -0700459 // Opens all .dex files found in the file, guessing the container format based on file extension.
460 static bool Open(const char* filename,
461 const std::string& location,
462 bool verify_checksum,
463 std::string* error_msg,
464 std::vector<std::unique_ptr<const DexFile>>* dex_files);
Orion Hodsona4c2a052016-08-17 10:51:42 +0100465
David Sehr733ddb22016-09-19 15:02:18 -0700466 // Open a single dex file from an fd.
467 static std::unique_ptr<const DexFile> OpenDex(int fd,
468 const std::string& location,
469 bool verify_checksum,
470 std::string* error_msg);
471
472 // Opens dex files from within a .jar, .zip, or .apk file
473 static bool OpenZip(int fd,
474 const std::string& location,
475 bool verify_checksum,
476 std::string* error_msg,
477 std::vector<std::unique_ptr<const DexFile>>* dex_files);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800478
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700479 // Closes a .dex file.
Brian Carlstromf615a612011-07-23 12:50:34 -0700480 virtual ~DexFile();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700481
Brian Carlstroma663ea52011-08-19 23:33:41 -0700482 const std::string& GetLocation() const {
483 return location_;
484 }
485
Andreas Gampecb8f9e82014-07-24 15:35:50 -0700486 // For normal dex files, location and base location coincide. If a dex file is part of a multidex
487 // archive, the base location is the name of the originating jar/apk, stripped of any internal
488 // classes*.dex path.
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100489 static std::string GetBaseLocation(const char* location) {
490 const char* pos = strrchr(location, kMultiDexSeparator);
491 if (pos == nullptr) {
492 return location;
Andreas Gampecb8f9e82014-07-24 15:35:50 -0700493 } else {
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100494 return std::string(location, pos - location);
495 }
496 }
497
Richard Uhlere5fed032015-03-18 08:21:11 -0700498 static std::string GetBaseLocation(const std::string& location) {
499 return GetBaseLocation(location.c_str());
500 }
501
Calin Juravlea308a322017-07-18 16:51:51 -0700502 // Returns the '!classes*.dex' part of the dex location. Returns an empty
Richard Uhlere5fed032015-03-18 08:21:11 -0700503 // string if there is no multidex suffix for the given location.
504 // The kMultiDexSeparator is included in the returned suffix.
505 static std::string GetMultiDexSuffix(const std::string& location) {
506 size_t pos = location.rfind(kMultiDexSeparator);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100507 if (pos == std::string::npos) {
Richard Uhlere5fed032015-03-18 08:21:11 -0700508 return "";
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100509 } else {
Richard Uhlere5fed032015-03-18 08:21:11 -0700510 return location.substr(pos);
Andreas Gampecb8f9e82014-07-24 15:35:50 -0700511 }
512 }
513
Richard Uhlere5fed032015-03-18 08:21:11 -0700514 std::string GetBaseLocation() const {
515 return GetBaseLocation(location_);
516 }
517
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800518 // For DexFiles directly from .dex files, this is the checksum from the DexFile::Header.
519 // For DexFiles opened from a zip files, this will be the ZipEntry CRC32 of classes.dex.
520 uint32_t GetLocationChecksum() const {
521 return location_checksum_;
522 }
523
Brian Carlstroma663ea52011-08-19 23:33:41 -0700524 const Header& GetHeader() const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700525 DCHECK(header_ != nullptr) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700526 return *header_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700527 }
528
Ian Rogers0571d352011-11-03 19:51:38 -0700529 // Decode the dex magic version
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700530 uint32_t GetVersion() const {
531 return GetHeader().GetVersion();
532 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700533
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800534 // Returns true if the byte string points to the magic value.
Ian Rogers13735952014-10-08 12:43:28 -0700535 static bool IsMagicValid(const uint8_t* magic);
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800536
537 // Returns true if the byte string after the magic is the correct value.
Ian Rogers13735952014-10-08 12:43:28 -0700538 static bool IsVersionValid(const uint8_t* magic);
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800539
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700540 // Returns the number of string identifiers in the .dex file.
541 size_t NumStringIds() const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700542 DCHECK(header_ != nullptr) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700543 return header_->string_ids_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700544 }
545
Ian Rogers0571d352011-11-03 19:51:38 -0700546 // Returns the StringId at the specified index.
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800547 const StringId& GetStringId(dex::StringIndex idx) const {
548 DCHECK_LT(idx.index_, NumStringIds()) << GetLocation();
549 return string_ids_[idx.index_];
Ian Rogers0571d352011-11-03 19:51:38 -0700550 }
551
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800552 dex::StringIndex GetIndexForStringId(const StringId& string_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800553 CHECK_GE(&string_id, string_ids_) << GetLocation();
554 CHECK_LT(&string_id, string_ids_ + header_->string_ids_size_) << GetLocation();
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800555 return dex::StringIndex(&string_id - string_ids_);
Ian Rogers0571d352011-11-03 19:51:38 -0700556 }
557
558 int32_t GetStringLength(const StringId& string_id) const;
559
Ian Rogersdfb325e2013-10-30 01:00:44 -0700560 // Returns a pointer to the UTF-8 string data referred to by the given string_id as well as the
561 // length of the string when decoded as a UTF-16 string. Note the UTF-16 length is not the same
562 // as the string length of the string data.
563 const char* GetStringDataAndUtf16Length(const StringId& string_id, uint32_t* utf16_length) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700564
Vladimir Marko5c6a5872016-06-27 13:50:16 +0100565 const char* GetStringData(const StringId& string_id) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700566
Ian Rogersdfb325e2013-10-30 01:00:44 -0700567 // Index version of GetStringDataAndUtf16Length.
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800568 const char* StringDataAndUtf16LengthByIdx(dex::StringIndex idx, uint32_t* utf16_length) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700569
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800570 const char* StringDataByIdx(dex::StringIndex idx) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700571
Ian Rogers637c65b2013-05-31 11:46:00 -0700572 // Looks up a string id for a given modified utf8 string.
573 const StringId* FindStringId(const char* string) const;
574
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300575 const TypeId* FindTypeId(const char* string) const;
576
Ian Rogers637c65b2013-05-31 11:46:00 -0700577 // Looks up a string id for a given utf16 string.
Vladimir Markoa48aef42014-12-03 17:53:53 +0000578 const StringId* FindStringId(const uint16_t* string, size_t length) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700579
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700580 // Returns the number of type identifiers in the .dex file.
Ian Rogers68b56852014-08-29 20:19:11 -0700581 uint32_t NumTypeIds() const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700582 DCHECK(header_ != nullptr) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700583 return header_->type_ids_size_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700584 }
585
Calin Juravle08556882017-05-26 16:40:45 -0700586 bool IsTypeIndexValid(dex::TypeIndex idx) const {
587 return idx.IsValid() && idx.index_ < NumTypeIds();
588 }
589
Ian Rogers0571d352011-11-03 19:51:38 -0700590 // Returns the TypeId at the specified index.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800591 const TypeId& GetTypeId(dex::TypeIndex idx) const {
592 DCHECK_LT(idx.index_, NumTypeIds()) << GetLocation();
593 return type_ids_[idx.index_];
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700594 }
595
Andreas Gampea5b09a62016-11-17 15:21:22 -0800596 dex::TypeIndex GetIndexForTypeId(const TypeId& type_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800597 CHECK_GE(&type_id, type_ids_) << GetLocation();
598 CHECK_LT(&type_id, type_ids_ + header_->type_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700599 size_t result = &type_id - type_ids_;
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800600 DCHECK_LT(result, 65536U) << GetLocation();
Andreas Gampea5b09a62016-11-17 15:21:22 -0800601 return dex::TypeIndex(static_cast<uint16_t>(result));
Ian Rogers0571d352011-11-03 19:51:38 -0700602 }
603
604 // Get the descriptor string associated with a given type index.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800605 const char* StringByTypeIdx(dex::TypeIndex idx, uint32_t* unicode_length) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700606
Andreas Gampea5b09a62016-11-17 15:21:22 -0800607 const char* StringByTypeIdx(dex::TypeIndex idx) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700608
609 // Returns the type descriptor string of a type id.
Vladimir Marko5c6a5872016-06-27 13:50:16 +0100610 const char* GetTypeDescriptor(const TypeId& type_id) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700611
612 // Looks up a type for the given string index
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800613 const TypeId* FindTypeId(dex::StringIndex string_idx) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700614
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700615 // Returns the number of field identifiers in the .dex file.
616 size_t NumFieldIds() const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700617 DCHECK(header_ != nullptr) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700618 return header_->field_ids_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700619 }
620
Ian Rogers0571d352011-11-03 19:51:38 -0700621 // Returns the FieldId at the specified index.
622 const FieldId& GetFieldId(uint32_t idx) const {
Sebastien Hertzb24bd992013-08-02 15:19:09 +0200623 DCHECK_LT(idx, NumFieldIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700624 return field_ids_[idx];
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700625 }
626
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800627 uint32_t GetIndexForFieldId(const FieldId& field_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800628 CHECK_GE(&field_id, field_ids_) << GetLocation();
629 CHECK_LT(&field_id, field_ids_ + header_->field_ids_size_) << GetLocation();
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800630 return &field_id - field_ids_;
631 }
632
633 // Looks up a field by its declaring class, name and type
634 const FieldId* FindFieldId(const DexFile::TypeId& declaring_klass,
635 const DexFile::StringId& name,
636 const DexFile::TypeId& type) const;
637
Alex Light9c20a142016-08-23 15:05:12 -0700638 uint32_t FindCodeItemOffset(const DexFile::ClassDef& class_def,
639 uint32_t dex_method_idx) const;
640
Bharadwaj Kalandhabhatta043c9082017-06-06 17:14:12 -0700641 static uint32_t GetCodeItemSize(const DexFile::CodeItem& disk_code_item);
642
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700643 // Returns the declaring class descriptor string of a field id.
644 const char* GetFieldDeclaringClassDescriptor(const FieldId& field_id) const {
Brian Carlstromb9edb842011-08-28 16:31:06 -0700645 const DexFile::TypeId& type_id = GetTypeId(field_id.class_idx_);
646 return GetTypeDescriptor(type_id);
647 }
648
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700649 // Returns the class descriptor string of a field id.
Vladimir Marko5c6a5872016-06-27 13:50:16 +0100650 const char* GetFieldTypeDescriptor(const FieldId& field_id) const;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700651
Brian Carlstromb9edb842011-08-28 16:31:06 -0700652 // Returns the name of a field id.
Vladimir Marko5c6a5872016-06-27 13:50:16 +0100653 const char* GetFieldName(const FieldId& field_id) const;
Brian Carlstromb9edb842011-08-28 16:31:06 -0700654
Ian Rogers0571d352011-11-03 19:51:38 -0700655 // Returns the number of method identifiers in the .dex file.
656 size_t NumMethodIds() const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700657 DCHECK(header_ != nullptr) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700658 return header_->method_ids_size_;
659 }
660
661 // Returns the MethodId at the specified index.
662 const MethodId& GetMethodId(uint32_t idx) const {
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700663 DCHECK_LT(idx, NumMethodIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700664 return method_ids_[idx];
665 }
666
667 uint32_t GetIndexForMethodId(const MethodId& method_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800668 CHECK_GE(&method_id, method_ids_) << GetLocation();
669 CHECK_LT(&method_id, method_ids_ + header_->method_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700670 return &method_id - method_ids_;
671 }
672
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800673 // Looks up a method by its declaring class, name and proto_id
674 const MethodId* FindMethodId(const DexFile::TypeId& declaring_klass,
675 const DexFile::StringId& name,
Ian Rogers0571d352011-11-03 19:51:38 -0700676 const DexFile::ProtoId& signature) const;
677
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700678 // Returns the declaring class descriptor string of a method id.
Vladimir Marko5c6a5872016-06-27 13:50:16 +0100679 const char* GetMethodDeclaringClassDescriptor(const MethodId& method_id) const;
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700680
jeffhao98eacac2011-09-14 16:11:53 -0700681 // Returns the prototype of a method id.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700682 const ProtoId& GetMethodPrototype(const MethodId& method_id) const {
683 return GetProtoId(method_id.proto_idx_);
684 }
685
Ian Rogersd91d6d62013-09-25 20:26:14 -0700686 // Returns a representation of the signature of a method id.
687 const Signature GetMethodSignature(const MethodId& method_id) const;
jeffhao98eacac2011-09-14 16:11:53 -0700688
Orion Hodsonb34bb192016-10-18 17:02:58 +0100689 // Returns a representation of the signature of a proto id.
690 const Signature GetProtoSignature(const ProtoId& proto_id) const;
691
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700692 // Returns the name of a method id.
Vladimir Marko5c6a5872016-06-27 13:50:16 +0100693 const char* GetMethodName(const MethodId& method_id) const;
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700694
Calin Juravle68ad6492015-08-18 17:08:12 +0100695 // Returns the shorty of a method by its index.
Vladimir Marko5c6a5872016-06-27 13:50:16 +0100696 const char* GetMethodShorty(uint32_t idx) const;
Calin Juravle68ad6492015-08-18 17:08:12 +0100697
Ian Rogers0571d352011-11-03 19:51:38 -0700698 // Returns the shorty of a method id.
Vladimir Marko5c6a5872016-06-27 13:50:16 +0100699 const char* GetMethodShorty(const MethodId& method_id) const;
700 const char* GetMethodShorty(const MethodId& method_id, uint32_t* length) const;
701
Ian Rogers0571d352011-11-03 19:51:38 -0700702 // Returns the number of class definitions in the .dex file.
Ian Rogers68b56852014-08-29 20:19:11 -0700703 uint32_t NumClassDefs() const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700704 DCHECK(header_ != nullptr) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700705 return header_->class_defs_size_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700706 }
707
708 // Returns the ClassDef at the specified index.
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700709 const ClassDef& GetClassDef(uint16_t idx) const {
Sebastien Hertzb24bd992013-08-02 15:19:09 +0200710 DCHECK_LT(idx, NumClassDefs()) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700711 return class_defs_[idx];
712 }
713
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700714 uint16_t GetIndexForClassDef(const ClassDef& class_def) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800715 CHECK_GE(&class_def, class_defs_) << GetLocation();
716 CHECK_LT(&class_def, class_defs_ + header_->class_defs_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700717 return &class_def - class_defs_;
718 }
719
720 // Returns the class descriptor string of a class definition.
Vladimir Marko5c6a5872016-06-27 13:50:16 +0100721 const char* GetClassDescriptor(const ClassDef& class_def) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700722
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700723 // Looks up a class definition by its type index.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800724 const ClassDef* FindClassDef(dex::TypeIndex type_idx) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700725
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700726 const TypeList* GetInterfacesList(const ClassDef& class_def) const {
727 if (class_def.interfaces_off_ == 0) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700728 return nullptr;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700729 } else {
Ian Rogers13735952014-10-08 12:43:28 -0700730 const uint8_t* addr = begin_ + class_def.interfaces_off_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700731 return reinterpret_cast<const TypeList*>(addr);
732 }
733 }
734
Orion Hodson12f4ff42017-01-13 16:43:12 +0000735 uint32_t NumMethodHandles() const {
736 return num_method_handles_;
737 }
738
Orion Hodsonc069a302017-01-18 09:23:12 +0000739 const MethodHandleItem& GetMethodHandle(uint32_t idx) const {
740 CHECK_LT(idx, NumMethodHandles());
741 return method_handles_[idx];
742 }
743
744 uint32_t NumCallSiteIds() const {
745 return num_call_site_ids_;
746 }
747
748 const CallSiteIdItem& GetCallSiteId(uint32_t idx) const {
749 CHECK_LT(idx, NumCallSiteIds());
750 return call_site_ids_[idx];
751 }
752
Ian Rogers0571d352011-11-03 19:51:38 -0700753 // Returns a pointer to the raw memory mapped class_data_item
Ian Rogers13735952014-10-08 12:43:28 -0700754 const uint8_t* GetClassData(const ClassDef& class_def) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700755 if (class_def.class_data_off_ == 0) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700756 return nullptr;
Ian Rogers0571d352011-11-03 19:51:38 -0700757 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800758 return begin_ + class_def.class_data_off_;
Ian Rogers0571d352011-11-03 19:51:38 -0700759 }
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700760 }
761
Ian Rogers0571d352011-11-03 19:51:38 -0700762 //
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800763 const CodeItem* GetCodeItem(const uint32_t code_off) const {
Alex Light9139e002015-10-09 15:59:48 -0700764 DCHECK_LT(code_off, size_) << "Code item offset larger then maximum allowed offset";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800765 if (code_off == 0) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700766 return nullptr; // native or abstract method
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700767 } else {
Ian Rogers13735952014-10-08 12:43:28 -0700768 const uint8_t* addr = begin_ + code_off;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700769 return reinterpret_cast<const CodeItem*>(addr);
770 }
771 }
772
Vladimir Marko5c6a5872016-06-27 13:50:16 +0100773 const char* GetReturnTypeDescriptor(const ProtoId& proto_id) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700774
775 // Returns the number of prototype identifiers in the .dex file.
776 size_t NumProtoIds() const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700777 DCHECK(header_ != nullptr) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700778 return header_->proto_ids_size_;
779 }
780
781 // Returns the ProtoId at the specified index.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800782 const ProtoId& GetProtoId(uint16_t idx) const {
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700783 DCHECK_LT(idx, NumProtoIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700784 return proto_ids_[idx];
785 }
786
787 uint16_t GetIndexForProtoId(const ProtoId& proto_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800788 CHECK_GE(&proto_id, proto_ids_) << GetLocation();
789 CHECK_LT(&proto_id, proto_ids_ + header_->proto_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700790 return &proto_id - proto_ids_;
791 }
792
793 // Looks up a proto id for a given return type and signature type list
Andreas Gampea5b09a62016-11-17 15:21:22 -0800794 const ProtoId* FindProtoId(dex::TypeIndex return_type_idx,
795 const dex::TypeIndex* signature_type_idxs,
796 uint32_t signature_length) const;
797 const ProtoId* FindProtoId(dex::TypeIndex return_type_idx,
798 const std::vector<dex::TypeIndex>& signature_type_idxs) const {
Vladimir Marko5c96e6b2013-11-14 15:34:17 +0000799 return FindProtoId(return_type_idx, &signature_type_idxs[0], signature_type_idxs.size());
800 }
Ian Rogers0571d352011-11-03 19:51:38 -0700801
802 // Given a signature place the type ids into the given vector, returns true on success
Andreas Gampea5b09a62016-11-17 15:21:22 -0800803 bool CreateTypeList(const StringPiece& signature,
804 dex::TypeIndex* return_type_idx,
805 std::vector<dex::TypeIndex>* param_type_idxs) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700806
Ian Rogersd91d6d62013-09-25 20:26:14 -0700807 // Create a Signature from the given string signature or return Signature::NoSignature if not
808 // possible.
809 const Signature CreateSignature(const StringPiece& signature) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700810
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700811 // Returns the short form method descriptor for the given prototype.
Vladimir Marko5c6a5872016-06-27 13:50:16 +0100812 const char* GetShorty(uint32_t proto_idx) const;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700813
814 const TypeList* GetProtoParameters(const ProtoId& proto_id) const {
815 if (proto_id.parameters_off_ == 0) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700816 return nullptr;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700817 } else {
Ian Rogers13735952014-10-08 12:43:28 -0700818 const uint8_t* addr = begin_ + proto_id.parameters_off_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700819 return reinterpret_cast<const TypeList*>(addr);
820 }
821 }
822
Ian Rogers13735952014-10-08 12:43:28 -0700823 const uint8_t* GetEncodedStaticFieldValuesArray(const ClassDef& class_def) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700824 if (class_def.static_values_off_ == 0) {
825 return 0;
826 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800827 return begin_ + class_def.static_values_off_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700828 }
829 }
830
Orion Hodson12f4ff42017-01-13 16:43:12 +0000831 const uint8_t* GetCallSiteEncodedValuesArray(const CallSiteIdItem& call_site_id) const {
832 return begin_ + call_site_id.data_off_;
833 }
834
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800835 static const TryItem* GetTryItems(const CodeItem& code_item, uint32_t offset);
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700836
837 // Get the base of the encoded data for the given DexCode.
Ian Rogers13735952014-10-08 12:43:28 -0700838 static const uint8_t* GetCatchHandlerData(const CodeItem& code_item, uint32_t offset) {
839 const uint8_t* handler_data =
840 reinterpret_cast<const uint8_t*>(GetTryItems(code_item, code_item.tries_size_));
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700841 return handler_data + offset;
842 }
843
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700844 // Find which try region is associated with the given address (ie dex pc). Returns -1 if none.
845 static int32_t FindTryItem(const CodeItem &code_item, uint32_t address);
846
847 // Find the handler offset associated with the given address (ie dex pc). Returns -1 if none.
848 static int32_t FindCatchHandlerOffset(const CodeItem &code_item, uint32_t address);
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700849
Shih-wei Liao195487c2011-08-20 13:29:04 -0700850 // Get the pointer to the start of the debugging data
Ian Rogers13735952014-10-08 12:43:28 -0700851 const uint8_t* GetDebugInfoStream(const CodeItem* code_item) const {
David Srbecky68529422015-07-07 19:13:29 +0100852 // Check that the offset is in bounds.
853 // Note that although the specification says that 0 should be used if there
854 // is no debug information, some applications incorrectly use 0xFFFFFFFF.
855 if (code_item->debug_info_off_ == 0 || code_item->debug_info_off_ >= size_) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700856 return nullptr;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700857 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800858 return begin_ + code_item->debug_info_off_;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700859 }
860 }
861
David Srbeckyb06e28e2015-12-10 13:15:00 +0000862 struct PositionInfo {
863 PositionInfo()
864 : address_(0),
865 line_(0),
866 source_file_(nullptr),
867 prologue_end_(false),
868 epilogue_begin_(false) {
869 }
870
871 uint32_t address_; // In 16-bit code units.
872 uint32_t line_; // Source code line number starting at 1.
873 const char* source_file_; // nullptr if the file from ClassDef still applies.
874 bool prologue_end_;
875 bool epilogue_begin_;
876 };
877
Shih-wei Liao195487c2011-08-20 13:29:04 -0700878 // Callback for "new position table entry".
879 // Returning true causes the decoder to stop early.
David Srbeckyb06e28e2015-12-10 13:15:00 +0000880 typedef bool (*DexDebugNewPositionCb)(void* context, const PositionInfo& entry);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700881
David Srbeckyb06e28e2015-12-10 13:15:00 +0000882 struct LocalInfo {
883 LocalInfo()
884 : name_(nullptr),
885 descriptor_(nullptr),
886 signature_(nullptr),
887 start_address_(0),
888 end_address_(0),
889 reg_(0),
890 is_live_(false) {
891 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700892
David Srbeckyb06e28e2015-12-10 13:15:00 +0000893 const char* name_; // E.g., list. It can be nullptr if unknown.
894 const char* descriptor_; // E.g., Ljava/util/LinkedList;
895 const char* signature_; // E.g., java.util.LinkedList<java.lang.Integer>
896 uint32_t start_address_; // PC location where the local is first defined.
897 uint32_t end_address_; // PC location where the local is no longer defined.
898 uint16_t reg_; // Dex register which stores the values.
899 bool is_live_; // Is the local defined and live.
900 };
901
902 // Callback for "new locals table entry".
903 typedef void (*DexDebugNewLocalCb)(void* context, const LocalInfo& entry);
904
905 static bool LineNumForPcCb(void* context, const PositionInfo& entry);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700906
Jeff Hao13e748b2015-08-25 20:44:19 +0000907 const AnnotationsDirectoryItem* GetAnnotationsDirectory(const ClassDef& class_def) const {
908 if (class_def.annotations_off_ == 0) {
909 return nullptr;
910 } else {
911 return reinterpret_cast<const AnnotationsDirectoryItem*>(begin_ + class_def.annotations_off_);
912 }
913 }
914
915 const AnnotationSetItem* GetClassAnnotationSet(const AnnotationsDirectoryItem* anno_dir) const {
916 if (anno_dir->class_annotations_off_ == 0) {
917 return nullptr;
918 } else {
919 return reinterpret_cast<const AnnotationSetItem*>(begin_ + anno_dir->class_annotations_off_);
920 }
921 }
922
923 const FieldAnnotationsItem* GetFieldAnnotations(const AnnotationsDirectoryItem* anno_dir) const {
924 if (anno_dir->fields_size_ == 0) {
925 return nullptr;
926 } else {
927 return reinterpret_cast<const FieldAnnotationsItem*>(&anno_dir[1]);
928 }
929 }
930
931 const MethodAnnotationsItem* GetMethodAnnotations(const AnnotationsDirectoryItem* anno_dir)
932 const {
933 if (anno_dir->methods_size_ == 0) {
934 return nullptr;
935 } else {
936 // Skip past the header and field annotations.
937 const uint8_t* addr = reinterpret_cast<const uint8_t*>(&anno_dir[1]);
938 addr += anno_dir->fields_size_ * sizeof(FieldAnnotationsItem);
939 return reinterpret_cast<const MethodAnnotationsItem*>(addr);
940 }
941 }
942
943 const ParameterAnnotationsItem* GetParameterAnnotations(const AnnotationsDirectoryItem* anno_dir)
944 const {
945 if (anno_dir->parameters_size_ == 0) {
946 return nullptr;
947 } else {
948 // Skip past the header, field annotations, and method annotations.
949 const uint8_t* addr = reinterpret_cast<const uint8_t*>(&anno_dir[1]);
950 addr += anno_dir->fields_size_ * sizeof(FieldAnnotationsItem);
951 addr += anno_dir->methods_size_ * sizeof(MethodAnnotationsItem);
952 return reinterpret_cast<const ParameterAnnotationsItem*>(addr);
953 }
954 }
955
956 const AnnotationSetItem* GetFieldAnnotationSetItem(const FieldAnnotationsItem& anno_item) const {
957 uint32_t offset = anno_item.annotations_off_;
958 if (offset == 0) {
959 return nullptr;
960 } else {
961 return reinterpret_cast<const AnnotationSetItem*>(begin_ + offset);
962 }
963 }
964
965 const AnnotationSetItem* GetMethodAnnotationSetItem(const MethodAnnotationsItem& anno_item)
966 const {
967 uint32_t offset = anno_item.annotations_off_;
968 if (offset == 0) {
969 return nullptr;
970 } else {
971 return reinterpret_cast<const AnnotationSetItem*>(begin_ + offset);
972 }
973 }
974
975 const AnnotationSetRefList* GetParameterAnnotationSetRefList(
976 const ParameterAnnotationsItem* anno_item) const {
977 uint32_t offset = anno_item->annotations_off_;
978 if (offset == 0) {
979 return nullptr;
980 }
981 return reinterpret_cast<const AnnotationSetRefList*>(begin_ + offset);
982 }
983
984 const AnnotationItem* GetAnnotationItem(const AnnotationSetItem* set_item, uint32_t index) const {
985 DCHECK_LE(index, set_item->size_);
986 uint32_t offset = set_item->entries_[index];
987 if (offset == 0) {
988 return nullptr;
989 } else {
990 return reinterpret_cast<const AnnotationItem*>(begin_ + offset);
991 }
992 }
993
994 const AnnotationSetItem* GetSetRefItemItem(const AnnotationSetRefItem* anno_item) const {
995 uint32_t offset = anno_item->annotations_off_;
996 if (offset == 0) {
997 return nullptr;
998 }
999 return reinterpret_cast<const AnnotationSetItem*>(begin_ + offset);
1000 }
1001
Shih-wei Liao195487c2011-08-20 13:29:04 -07001002 // Debug info opcodes and constants
1003 enum {
1004 DBG_END_SEQUENCE = 0x00,
1005 DBG_ADVANCE_PC = 0x01,
1006 DBG_ADVANCE_LINE = 0x02,
1007 DBG_START_LOCAL = 0x03,
1008 DBG_START_LOCAL_EXTENDED = 0x04,
1009 DBG_END_LOCAL = 0x05,
1010 DBG_RESTART_LOCAL = 0x06,
1011 DBG_SET_PROLOGUE_END = 0x07,
1012 DBG_SET_EPILOGUE_BEGIN = 0x08,
1013 DBG_SET_FILE = 0x09,
1014 DBG_FIRST_SPECIAL = 0x0a,
1015 DBG_LINE_BASE = -4,
1016 DBG_LINE_RANGE = 15,
1017 };
1018
Shih-wei Liao195487c2011-08-20 13:29:04 -07001019 struct LineNumFromPcContext {
Ian Rogersca190662012-06-26 15:45:57 -07001020 LineNumFromPcContext(uint32_t address, uint32_t line_num)
1021 : address_(address), line_num_(line_num) {}
Shih-wei Liao195487c2011-08-20 13:29:04 -07001022 uint32_t address_;
1023 uint32_t line_num_;
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -07001024 private:
1025 DISALLOW_COPY_AND_ASSIGN(LineNumFromPcContext);
Shih-wei Liao195487c2011-08-20 13:29:04 -07001026 };
1027
Roland Levillain91d65e02016-01-19 15:59:16 +00001028 // Returns false if there is no debugging information or if it cannot be decoded.
David Srbeckyb06e28e2015-12-10 13:15:00 +00001029 bool DecodeDebugLocalInfo(const CodeItem* code_item, bool is_static, uint32_t method_idx,
1030 DexDebugNewLocalCb local_cb, void* context) const;
1031
Roland Levillain91d65e02016-01-19 15:59:16 +00001032 // Returns false if there is no debugging information or if it cannot be decoded.
David Srbeckyb06e28e2015-12-10 13:15:00 +00001033 bool DecodeDebugPositionInfo(const CodeItem* code_item, DexDebugNewPositionCb position_cb,
1034 void* context) const;
Shih-wei Liao195487c2011-08-20 13:29:04 -07001035
Ian Rogers0571d352011-11-03 19:51:38 -07001036 const char* GetSourceFile(const ClassDef& class_def) const {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001037 if (!class_def.source_file_idx_.IsValid()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001038 return nullptr;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001039 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07001040 return StringDataByIdx(class_def.source_file_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001041 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001042 }
1043
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001044 int GetPermissions() const;
Ian Rogers1c849e52012-06-28 14:00:33 -07001045
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02001046 bool IsReadOnly() const;
1047
Brian Carlstrome0948e12013-08-29 09:36:15 -07001048 bool EnableWrite() const;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02001049
Brian Carlstrome0948e12013-08-29 09:36:15 -07001050 bool DisableWrite() const;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02001051
Ian Rogers13735952014-10-08 12:43:28 -07001052 const uint8_t* Begin() const {
Ian Rogers8b2c0b92013-09-19 02:56:49 -07001053 return begin_;
1054 }
1055
1056 size_t Size() const {
1057 return size_;
1058 }
1059
Andreas Gampe90e34042015-04-27 20:01:52 -07001060 // Return the name of the index-th classes.dex in a multidex zip file. This is classes.dex for
1061 // index == 0, and classes{index + 1}.dex else.
1062 static std::string GetMultiDexClassesDexName(size_t index);
1063
1064 // Return the (possibly synthetic) dex location for a multidex entry. This is dex_location for
1065 // index == 0, and dex_location + multi-dex-separator + GetMultiDexClassesDexName(index) else.
1066 static std::string GetMultiDexLocation(size_t index, const char* dex_location);
Calin Juravle4e1d5792014-07-15 23:56:47 +01001067
1068 // Returns the canonical form of the given dex location.
1069 //
1070 // There are different flavors of "dex locations" as follows:
1071 // the file name of a dex file:
1072 // The actual file path that the dex file has on disk.
1073 // dex_location:
1074 // This acts as a key for the class linker to know which dex file to load.
1075 // It may correspond to either an old odex file or a particular dex file
1076 // inside an oat file. In the first case it will also match the file name
1077 // of the dex file. In the second case (oat) it will include the file name
1078 // and possibly some multidex annotation to uniquely identify it.
1079 // canonical_dex_location:
1080 // the dex_location where it's file name part has been made canonical.
1081 static std::string GetDexCanonicalLocation(const char* dex_location);
1082
Richard Uhler07b3c232015-03-31 15:57:54 -07001083 const OatDexFile* GetOatDexFile() const {
1084 return oat_dex_file_;
Andreas Gampefd9eb392014-11-06 16:52:58 -08001085 }
1086
Mathieu Chartier1b868492016-11-16 16:22:37 -08001087 // Used by oat writer.
1088 void SetOatDexFile(OatDexFile* oat_dex_file) const {
1089 oat_dex_file_ = oat_dex_file;
1090 }
1091
David Sehr9323e6e2016-09-13 08:58:35 -07001092 // Utility methods for reading integral values from a buffer.
1093 static int32_t ReadSignedInt(const uint8_t* ptr, int zwidth);
1094 static uint32_t ReadUnsignedInt(const uint8_t* ptr, int zwidth, bool fill_on_right);
1095 static int64_t ReadSignedLong(const uint8_t* ptr, int zwidth);
1096 static uint64_t ReadUnsignedLong(const uint8_t* ptr, int zwidth, bool fill_on_right);
1097
Alex Light40528472017-03-28 09:07:36 -07001098 // Recalculates the checksum of the dex file. Does not use the current value in the header.
1099 uint32_t CalculateChecksum() const;
1100
David Sehr709b0702016-10-13 09:12:37 -07001101 // Returns a human-readable form of the method at an index.
1102 std::string PrettyMethod(uint32_t method_idx, bool with_signature = true) const;
1103 // Returns a human-readable form of the field at an index.
1104 std::string PrettyField(uint32_t field_idx, bool with_type = true) const;
1105 // Returns a human-readable form of the type at an index.
Andreas Gampea5b09a62016-11-17 15:21:22 -08001106 std::string PrettyType(dex::TypeIndex type_idx) const;
David Sehr709b0702016-10-13 09:12:37 -07001107
Carl Shapiro1fb86202011-06-27 17:43:13 -07001108 private:
Aart Bik37d6a3b2016-06-21 18:30:10 -07001109 static std::unique_ptr<const DexFile> OpenFile(int fd,
David Sehr733ddb22016-09-19 15:02:18 -07001110 const std::string& location,
Aart Bik37d6a3b2016-06-21 18:30:10 -07001111 bool verify,
1112 bool verify_checksum,
1113 std::string* error_msg);
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001114
Andreas Gampe833a4852014-05-21 18:46:59 -07001115 enum class ZipOpenErrorCode { // private
1116 kNoError,
1117 kEntryNotFound,
1118 kExtractToMemoryError,
1119 kDexFileError,
1120 kMakeReadOnlyError,
1121 kVerifyError
1122 };
1123
David Sehr733ddb22016-09-19 15:02:18 -07001124 // Open all classesXXX.dex files from a zip archive.
1125 static bool OpenAllDexFilesFromZip(const ZipArchive& zip_archive,
1126 const std::string& location,
1127 bool verify_checksum,
1128 std::string* error_msg,
1129 std::vector<std::unique_ptr<const DexFile>>* dex_files);
1130
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001131 // Opens .dex file from the entry_name in a zip archive. error_code is undefined when non-null
Andreas Gampe833a4852014-05-21 18:46:59 -07001132 // return.
David Sehr733ddb22016-09-19 15:02:18 -07001133 static std::unique_ptr<const DexFile> OpenOneDexFileFromZip(const ZipArchive& zip_archive,
1134 const char* entry_name,
1135 const std::string& location,
1136 bool verify_checksum,
1137 std::string* error_msg,
1138 ZipOpenErrorCode* error_code);
1139
1140 enum class VerifyResult { // private
David Sehr9fddd362016-09-22 14:05:37 -07001141 kVerifyNotAttempted,
David Sehr733ddb22016-09-19 15:02:18 -07001142 kVerifySucceeded,
1143 kVerifyFailed
1144 };
1145
1146 static std::unique_ptr<DexFile> OpenCommon(const uint8_t* base,
1147 size_t size,
Aart Bik37d6a3b2016-06-21 18:30:10 -07001148 const std::string& location,
David Sehr733ddb22016-09-19 15:02:18 -07001149 uint32_t location_checksum,
1150 const OatDexFile* oat_dex_file,
1151 bool verify,
Aart Bik37d6a3b2016-06-21 18:30:10 -07001152 bool verify_checksum,
1153 std::string* error_msg,
David Sehr733ddb22016-09-19 15:02:18 -07001154 VerifyResult* verify_result = nullptr);
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001155
Alex Light9c20a142016-08-23 15:05:12 -07001156
1157 // Opens a .dex file at the given address, optionally backed by a MemMap
1158 static std::unique_ptr<const DexFile> OpenMemory(const uint8_t* dex_file,
1159 size_t size,
1160 const std::string& location,
1161 uint32_t location_checksum,
1162 std::unique_ptr<MemMap> mem_map,
1163 const OatDexFile* oat_dex_file,
1164 std::string* error_msg);
1165
David Sehr733ddb22016-09-19 15:02:18 -07001166 DexFile(const uint8_t* base,
1167 size_t size,
Brian Carlstrom28db0122012-10-18 16:20:41 -07001168 const std::string& location,
1169 uint32_t location_checksum,
Richard Uhler07b3c232015-03-31 15:57:54 -07001170 const OatDexFile* oat_dex_file);
jeffhaof6174e82012-01-31 16:14:17 -08001171
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001172 // Top-level initializer that calls other Init methods.
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001173 bool Init(std::string* error_msg);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001174
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001175 // Returns true if the header magic and version numbers are of the expected values.
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001176 bool CheckMagicAndVersion(std::string* error_msg) const;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001177
Orion Hodson12f4ff42017-01-13 16:43:12 +00001178 // Initialize section info for sections only found in map. Returns true on success.
1179 void InitializeSectionsFromMapList();
1180
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001181 // The base address of the memory mapping.
Ian Rogers13735952014-10-08 12:43:28 -07001182 const uint8_t* const begin_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001183
1184 // The size of the underlying memory allocation in bytes.
Ian Rogers62d6c772013-02-27 08:32:07 -08001185 const size_t size_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001186
Elliott Hughes64bf5a32011-09-20 14:43:12 -07001187 // Typically the dex file name when available, alternatively some identifying string.
Brian Carlstroma663ea52011-08-19 23:33:41 -07001188 //
1189 // The ClassLinker will use this to match DexFiles the boot class
1190 // path to DexCache::GetLocation when loading from an image.
1191 const std::string location_;
1192
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001193 const uint32_t location_checksum_;
1194
Brian Carlstrom33f741e2011-10-03 11:24:05 -07001195 // Manages the underlying memory allocation.
Ian Rogers700a4022014-05-19 16:49:03 -07001196 std::unique_ptr<MemMap> mem_map_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001197
1198 // Points to the header section.
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001199 const Header* const header_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001200
1201 // Points to the base of the string identifier list.
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001202 const StringId* const string_ids_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001203
1204 // Points to the base of the type identifier list.
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001205 const TypeId* const type_ids_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001206
1207 // Points to the base of the field identifier list.
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001208 const FieldId* const field_ids_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001209
1210 // Points to the base of the method identifier list.
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001211 const MethodId* const method_ids_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001212
1213 // Points to the base of the prototype identifier list.
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001214 const ProtoId* const proto_ids_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001215
1216 // Points to the base of the class definition list.
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001217 const ClassDef* const class_defs_;
Ian Rogers68b56852014-08-29 20:19:11 -07001218
Orion Hodson12f4ff42017-01-13 16:43:12 +00001219 // Points to the base of the method handles list.
1220 const MethodHandleItem* method_handles_;
1221
1222 // Number of elements in the method handles list.
1223 size_t num_method_handles_;
1224
1225 // Points to the base of the call sites id list.
1226 const CallSiteIdItem* call_site_ids_;
1227
1228 // Number of elements in the call sites list.
1229 size_t num_call_site_ids_;
1230
Richard Uhler07b3c232015-03-31 15:57:54 -07001231 // If this dex file was loaded from an oat file, oat_dex_file_ contains a
1232 // pointer to the OatDexFile it was loaded from. Otherwise oat_dex_file_ is
1233 // null.
Mathieu Chartier1b868492016-11-16 16:22:37 -08001234 mutable const OatDexFile* oat_dex_file_;
Andreas Gampee6215c02015-08-31 18:54:38 -07001235
1236 friend class DexFileVerifierTest;
Mathieu Chartier1b868492016-11-16 16:22:37 -08001237 friend class OatWriter;
Mathieu Chartier76172162016-01-26 14:54:06 -08001238 ART_FRIEND_TEST(ClassLinkerTest, RegisterDexFileName); // for constructor
Carl Shapiro1fb86202011-06-27 17:43:13 -07001239};
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001240
1241struct DexFileReference {
1242 DexFileReference(const DexFile* file, uint32_t idx) : dex_file(file), index(idx) { }
1243 const DexFile* dex_file;
1244 uint32_t index;
1245};
1246
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001247std::ostream& operator<<(std::ostream& os, const DexFile& dex_file);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001248
Ian Rogers0571d352011-11-03 19:51:38 -07001249// Iterate over a dex file's ProtoId's paramters
1250class DexFileParameterIterator {
1251 public:
1252 DexFileParameterIterator(const DexFile& dex_file, const DexFile::ProtoId& proto_id)
1253 : dex_file_(dex_file), size_(0), pos_(0) {
1254 type_list_ = dex_file_.GetProtoParameters(proto_id);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001255 if (type_list_ != nullptr) {
Ian Rogers0571d352011-11-03 19:51:38 -07001256 size_ = type_list_->Size();
1257 }
1258 }
1259 bool HasNext() const { return pos_ < size_; }
David Srbeckyb06e28e2015-12-10 13:15:00 +00001260 size_t Size() const { return size_; }
Ian Rogers0571d352011-11-03 19:51:38 -07001261 void Next() { ++pos_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08001262 dex::TypeIndex GetTypeIdx() {
Ian Rogers0571d352011-11-03 19:51:38 -07001263 return type_list_->GetTypeItem(pos_).type_idx_;
1264 }
1265 const char* GetDescriptor() {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001266 return dex_file_.StringByTypeIdx(dex::TypeIndex(GetTypeIdx()));
Ian Rogers0571d352011-11-03 19:51:38 -07001267 }
1268 private:
1269 const DexFile& dex_file_;
1270 const DexFile::TypeList* type_list_;
1271 uint32_t size_;
1272 uint32_t pos_;
1273 DISALLOW_IMPLICIT_CONSTRUCTORS(DexFileParameterIterator);
1274};
1275
Ian Rogersd91d6d62013-09-25 20:26:14 -07001276// Abstract the signature of a method.
Ian Rogers03b6eaf2014-10-28 09:34:57 -07001277class Signature : public ValueObject {
Ian Rogersd91d6d62013-09-25 20:26:14 -07001278 public:
1279 std::string ToString() const;
1280
1281 static Signature NoSignature() {
1282 return Signature();
1283 }
1284
Orion Hodson6c4921b2016-09-21 15:41:06 +01001285 bool IsVoid() const;
1286 uint32_t GetNumberOfParameters() const;
1287
Ian Rogersdfb325e2013-10-30 01:00:44 -07001288 bool operator==(const Signature& rhs) const;
Ian Rogersd91d6d62013-09-25 20:26:14 -07001289 bool operator!=(const Signature& rhs) const {
1290 return !(*this == rhs);
1291 }
1292
Vladimir Markod9cffea2013-11-25 15:08:02 +00001293 bool operator==(const StringPiece& rhs) const;
Ian Rogersd91d6d62013-09-25 20:26:14 -07001294
1295 private:
1296 Signature(const DexFile* dex, const DexFile::ProtoId& proto) : dex_file_(dex), proto_id_(&proto) {
1297 }
1298
1299 Signature() : dex_file_(nullptr), proto_id_(nullptr) {
1300 }
1301
1302 friend class DexFile;
1303
1304 const DexFile* const dex_file_;
1305 const DexFile::ProtoId* const proto_id_;
1306};
1307std::ostream& operator<<(std::ostream& os, const Signature& sig);
1308
Ian Rogers0571d352011-11-03 19:51:38 -07001309// Iterate and decode class_data_item
1310class ClassDataItemIterator {
1311 public:
Ian Rogers13735952014-10-08 12:43:28 -07001312 ClassDataItemIterator(const DexFile& dex_file, const uint8_t* raw_class_data_item)
Ian Rogers0571d352011-11-03 19:51:38 -07001313 : dex_file_(dex_file), pos_(0), ptr_pos_(raw_class_data_item), last_idx_(0) {
1314 ReadClassDataHeader();
1315 if (EndOfInstanceFieldsPos() > 0) {
1316 ReadClassDataField();
1317 } else if (EndOfVirtualMethodsPos() > 0) {
1318 ReadClassDataMethod();
1319 }
1320 }
1321 uint32_t NumStaticFields() const {
1322 return header_.static_fields_size_;
1323 }
1324 uint32_t NumInstanceFields() const {
1325 return header_.instance_fields_size_;
1326 }
1327 uint32_t NumDirectMethods() const {
1328 return header_.direct_methods_size_;
1329 }
1330 uint32_t NumVirtualMethods() const {
1331 return header_.virtual_methods_size_;
1332 }
Alex Light40528472017-03-28 09:07:36 -07001333 bool IsAtMethod() const {
1334 return pos_ >= EndOfInstanceFieldsPos();
1335 }
Ian Rogers0571d352011-11-03 19:51:38 -07001336 bool HasNextStaticField() const {
1337 return pos_ < EndOfStaticFieldsPos();
1338 }
1339 bool HasNextInstanceField() const {
1340 return pos_ >= EndOfStaticFieldsPos() && pos_ < EndOfInstanceFieldsPos();
1341 }
1342 bool HasNextDirectMethod() const {
1343 return pos_ >= EndOfInstanceFieldsPos() && pos_ < EndOfDirectMethodsPos();
1344 }
1345 bool HasNextVirtualMethod() const {
1346 return pos_ >= EndOfDirectMethodsPos() && pos_ < EndOfVirtualMethodsPos();
1347 }
Mathieu Chartiere17cf242017-06-19 11:05:51 -07001348 void SkipStaticFields() {
1349 while (HasNextStaticField()) {
1350 Next();
1351 }
1352 }
1353 void SkipInstanceFields() {
1354 while (HasNextInstanceField()) {
1355 Next();
1356 }
1357 }
1358 void SkipAllFields() {
1359 SkipStaticFields();
1360 SkipInstanceFields();
1361 }
1362 void SkipDirectMethods() {
1363 while (HasNextDirectMethod()) {
1364 Next();
1365 }
1366 }
1367 void SkipVirtualMethods() {
1368 while (HasNextVirtualMethod()) {
1369 Next();
1370 }
1371 }
Ian Rogers0571d352011-11-03 19:51:38 -07001372 bool HasNext() const {
1373 return pos_ < EndOfVirtualMethodsPos();
1374 }
Ian Rogers637c65b2013-05-31 11:46:00 -07001375 inline void Next() {
Ian Rogers0571d352011-11-03 19:51:38 -07001376 pos_++;
1377 if (pos_ < EndOfStaticFieldsPos()) {
1378 last_idx_ = GetMemberIndex();
1379 ReadClassDataField();
1380 } else if (pos_ == EndOfStaticFieldsPos() && NumInstanceFields() > 0) {
1381 last_idx_ = 0; // transition to next array, reset last index
1382 ReadClassDataField();
1383 } else if (pos_ < EndOfInstanceFieldsPos()) {
1384 last_idx_ = GetMemberIndex();
1385 ReadClassDataField();
1386 } else if (pos_ == EndOfInstanceFieldsPos() && NumDirectMethods() > 0) {
1387 last_idx_ = 0; // transition to next array, reset last index
1388 ReadClassDataMethod();
1389 } else if (pos_ < EndOfDirectMethodsPos()) {
1390 last_idx_ = GetMemberIndex();
1391 ReadClassDataMethod();
1392 } else if (pos_ == EndOfDirectMethodsPos() && NumVirtualMethods() > 0) {
1393 last_idx_ = 0; // transition to next array, reset last index
1394 ReadClassDataMethod();
1395 } else if (pos_ < EndOfVirtualMethodsPos()) {
1396 last_idx_ = GetMemberIndex();
1397 ReadClassDataMethod();
1398 } else {
1399 DCHECK(!HasNext());
1400 }
1401 }
1402 uint32_t GetMemberIndex() const {
1403 if (pos_ < EndOfInstanceFieldsPos()) {
1404 return last_idx_ + field_.field_idx_delta_;
1405 } else {
Sebastien Hertzb24bd992013-08-02 15:19:09 +02001406 DCHECK_LT(pos_, EndOfVirtualMethodsPos());
Ian Rogers0571d352011-11-03 19:51:38 -07001407 return last_idx_ + method_.method_idx_delta_;
1408 }
1409 }
Andreas Gampe51829322014-08-25 15:05:04 -07001410 uint32_t GetRawMemberAccessFlags() const {
Ian Rogers0571d352011-11-03 19:51:38 -07001411 if (pos_ < EndOfInstanceFieldsPos()) {
1412 return field_.access_flags_;
1413 } else {
Sebastien Hertzb24bd992013-08-02 15:19:09 +02001414 DCHECK_LT(pos_, EndOfVirtualMethodsPos());
Ian Rogers0571d352011-11-03 19:51:38 -07001415 return method_.access_flags_;
1416 }
1417 }
Andreas Gampe51829322014-08-25 15:05:04 -07001418 uint32_t GetFieldAccessFlags() const {
1419 return GetRawMemberAccessFlags() & kAccValidFieldFlags;
1420 }
1421 uint32_t GetMethodAccessFlags() const {
1422 return GetRawMemberAccessFlags() & kAccValidMethodFlags;
1423 }
1424 bool MemberIsNative() const {
1425 return GetRawMemberAccessFlags() & kAccNative;
1426 }
1427 bool MemberIsFinal() const {
1428 return GetRawMemberAccessFlags() & kAccFinal;
1429 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001430 InvokeType GetMethodInvokeType(const DexFile::ClassDef& class_def) const {
1431 if (HasNextDirectMethod()) {
Andreas Gampe51829322014-08-25 15:05:04 -07001432 if ((GetRawMemberAccessFlags() & kAccStatic) != 0) {
Ian Rogers08f753d2012-08-24 14:35:25 -07001433 return kStatic;
1434 } else {
1435 return kDirect;
1436 }
1437 } else {
Andreas Gampe51829322014-08-25 15:05:04 -07001438 DCHECK_EQ(GetRawMemberAccessFlags() & kAccStatic, 0U);
Ian Rogers08f753d2012-08-24 14:35:25 -07001439 if ((class_def.access_flags_ & kAccInterface) != 0) {
1440 return kInterface;
Andreas Gampe51829322014-08-25 15:05:04 -07001441 } else if ((GetRawMemberAccessFlags() & kAccConstructor) != 0) {
Ian Rogers08f753d2012-08-24 14:35:25 -07001442 return kSuper;
1443 } else {
1444 return kVirtual;
1445 }
1446 }
1447 }
Ian Rogers0571d352011-11-03 19:51:38 -07001448 const DexFile::CodeItem* GetMethodCodeItem() const {
1449 return dex_file_.GetCodeItem(method_.code_off_);
1450 }
1451 uint32_t GetMethodCodeItemOffset() const {
1452 return method_.code_off_;
1453 }
Andreas Gampee6215c02015-08-31 18:54:38 -07001454 const uint8_t* DataPointer() const {
1455 return ptr_pos_;
1456 }
Ian Rogers13735952014-10-08 12:43:28 -07001457 const uint8_t* EndDataPointer() const {
jeffhao10037c82012-01-23 15:06:23 -08001458 CHECK(!HasNext());
1459 return ptr_pos_;
1460 }
Elliott Hughesa21039c2012-06-21 12:09:25 -07001461
Ian Rogers0571d352011-11-03 19:51:38 -07001462 private:
1463 // A dex file's class_data_item is leb128 encoded, this structure holds a decoded form of the
1464 // header for a class_data_item
1465 struct ClassDataHeader {
1466 uint32_t static_fields_size_; // the number of static fields
1467 uint32_t instance_fields_size_; // the number of instance fields
1468 uint32_t direct_methods_size_; // the number of direct methods
1469 uint32_t virtual_methods_size_; // the number of virtual methods
1470 } header_;
1471
1472 // Read and decode header from a class_data_item stream into header
1473 void ReadClassDataHeader();
1474
1475 uint32_t EndOfStaticFieldsPos() const {
1476 return header_.static_fields_size_;
1477 }
1478 uint32_t EndOfInstanceFieldsPos() const {
1479 return EndOfStaticFieldsPos() + header_.instance_fields_size_;
1480 }
1481 uint32_t EndOfDirectMethodsPos() const {
1482 return EndOfInstanceFieldsPos() + header_.direct_methods_size_;
1483 }
1484 uint32_t EndOfVirtualMethodsPos() const {
1485 return EndOfDirectMethodsPos() + header_.virtual_methods_size_;
1486 }
1487
1488 // A decoded version of the field of a class_data_item
1489 struct ClassDataField {
1490 uint32_t field_idx_delta_; // delta of index into the field_ids array for FieldId
1491 uint32_t access_flags_; // access flags for the field
1492 ClassDataField() : field_idx_delta_(0), access_flags_(0) {}
Elliott Hughesa21039c2012-06-21 12:09:25 -07001493
Ian Rogers0571d352011-11-03 19:51:38 -07001494 private:
1495 DISALLOW_COPY_AND_ASSIGN(ClassDataField);
Elliott Hughesee0fa762012-03-26 17:12:41 -07001496 };
1497 ClassDataField field_;
Ian Rogers0571d352011-11-03 19:51:38 -07001498
1499 // Read and decode a field from a class_data_item stream into field
1500 void ReadClassDataField();
1501
1502 // A decoded version of the method of a class_data_item
1503 struct ClassDataMethod {
1504 uint32_t method_idx_delta_; // delta of index into the method_ids array for MethodId
1505 uint32_t access_flags_;
1506 uint32_t code_off_;
1507 ClassDataMethod() : method_idx_delta_(0), access_flags_(0), code_off_(0) {}
Elliott Hughesa21039c2012-06-21 12:09:25 -07001508
Ian Rogers0571d352011-11-03 19:51:38 -07001509 private:
1510 DISALLOW_COPY_AND_ASSIGN(ClassDataMethod);
Elliott Hughesee0fa762012-03-26 17:12:41 -07001511 };
1512 ClassDataMethod method_;
Ian Rogers0571d352011-11-03 19:51:38 -07001513
1514 // Read and decode a method from a class_data_item stream into method
1515 void ReadClassDataMethod();
1516
1517 const DexFile& dex_file_;
1518 size_t pos_; // integral number of items passed
Ian Rogers13735952014-10-08 12:43:28 -07001519 const uint8_t* ptr_pos_; // pointer into stream of class_data_item
Ian Rogers0571d352011-11-03 19:51:38 -07001520 uint32_t last_idx_; // last read field or method index to apply delta to
1521 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassDataItemIterator);
1522};
1523
Orion Hodson12f4ff42017-01-13 16:43:12 +00001524class EncodedArrayValueIterator {
Ian Rogers0571d352011-11-03 19:51:38 -07001525 public:
Orion Hodson12f4ff42017-01-13 16:43:12 +00001526 EncodedArrayValueIterator(const DexFile& dex_file, const uint8_t* array_data);
Shinichiro Hamaji82863f02015-11-05 16:51:33 +09001527
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001528 bool HasNext() const { return pos_ < array_size_; }
Ian Rogers0571d352011-11-03 19:51:38 -07001529
1530 void Next();
Elliott Hughesa21039c2012-06-21 12:09:25 -07001531
Ian Rogers0571d352011-11-03 19:51:38 -07001532 enum ValueType {
Orion Hodson12f4ff42017-01-13 16:43:12 +00001533 kByte = 0x00,
1534 kShort = 0x02,
1535 kChar = 0x03,
1536 kInt = 0x04,
1537 kLong = 0x06,
1538 kFloat = 0x10,
1539 kDouble = 0x11,
1540 kMethodType = 0x15,
1541 kMethodHandle = 0x16,
1542 kString = 0x17,
1543 kType = 0x18,
1544 kField = 0x19,
1545 kMethod = 0x1a,
1546 kEnum = 0x1b,
1547 kArray = 0x1c,
1548 kAnnotation = 0x1d,
1549 kNull = 0x1e,
1550 kBoolean = 0x1f,
Ian Rogers0571d352011-11-03 19:51:38 -07001551 };
1552
Shinichiro Hamaji82863f02015-11-05 16:51:33 +09001553 ValueType GetValueType() const { return type_; }
1554 const jvalue& GetJavaValue() const { return jval_; }
1555
David Sehr9323e6e2016-09-13 08:58:35 -07001556 protected:
Ian Rogers13735952014-10-08 12:43:28 -07001557 static constexpr uint8_t kEncodedValueTypeMask = 0x1f; // 0b11111
1558 static constexpr uint8_t kEncodedValueArgShift = 5;
Ian Rogers0571d352011-11-03 19:51:38 -07001559
1560 const DexFile& dex_file_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001561 size_t array_size_; // Size of array.
1562 size_t pos_; // Current position.
Ian Rogers13735952014-10-08 12:43:28 -07001563 const uint8_t* ptr_; // Pointer into encoded data array.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001564 ValueType type_; // Type of current encoded value.
1565 jvalue jval_; // Value of current encoded value.
David Sehr9323e6e2016-09-13 08:58:35 -07001566
1567 private:
Orion Hodson12f4ff42017-01-13 16:43:12 +00001568 DISALLOW_IMPLICIT_CONSTRUCTORS(EncodedArrayValueIterator);
1569};
1570std::ostream& operator<<(std::ostream& os, const EncodedArrayValueIterator::ValueType& code);
1571
1572class EncodedStaticFieldValueIterator : public EncodedArrayValueIterator {
1573 public:
1574 EncodedStaticFieldValueIterator(const DexFile& dex_file,
1575 const DexFile::ClassDef& class_def)
1576 : EncodedArrayValueIterator(dex_file,
1577 dex_file.GetEncodedStaticFieldValuesArray(class_def))
1578 {}
1579
1580 private:
Ian Rogers0571d352011-11-03 19:51:38 -07001581 DISALLOW_IMPLICIT_CONSTRUCTORS(EncodedStaticFieldValueIterator);
1582};
Brian Carlstrom88f36542012-10-16 23:24:21 -07001583std::ostream& operator<<(std::ostream& os, const EncodedStaticFieldValueIterator::ValueType& code);
Ian Rogers0571d352011-11-03 19:51:38 -07001584
Orion Hodson12f4ff42017-01-13 16:43:12 +00001585class CallSiteArrayValueIterator : public EncodedArrayValueIterator {
1586 public:
1587 CallSiteArrayValueIterator(const DexFile& dex_file,
1588 const DexFile::CallSiteIdItem& call_site_id)
1589 : EncodedArrayValueIterator(dex_file,
1590 dex_file.GetCallSiteEncodedValuesArray(call_site_id))
1591 {}
1592
1593 uint32_t Size() const { return array_size_; }
1594
1595 private:
1596 DISALLOW_IMPLICIT_CONSTRUCTORS(CallSiteArrayValueIterator);
1597};
1598std::ostream& operator<<(std::ostream& os, const CallSiteArrayValueIterator::ValueType& code);
1599
Ian Rogers0571d352011-11-03 19:51:38 -07001600class CatchHandlerIterator {
1601 public:
1602 CatchHandlerIterator(const DexFile::CodeItem& code_item, uint32_t address);
Logan Chien736df022012-04-27 16:25:57 +08001603
1604 CatchHandlerIterator(const DexFile::CodeItem& code_item,
1605 const DexFile::TryItem& try_item);
1606
Ian Rogers13735952014-10-08 12:43:28 -07001607 explicit CatchHandlerIterator(const uint8_t* handler_data) {
Ian Rogers0571d352011-11-03 19:51:38 -07001608 Init(handler_data);
1609 }
1610
Andreas Gampea5b09a62016-11-17 15:21:22 -08001611 dex::TypeIndex GetHandlerTypeIndex() const {
Ian Rogers0571d352011-11-03 19:51:38 -07001612 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 Rogers13735952014-10-08 12:43:28 -07001622 const uint8_t* EndDataPointer() const {
Ian Rogers0571d352011-11-03 19:51:38 -07001623 CHECK(!HasNext());
1624 return current_data_;
1625 }
Elliott Hughesa21039c2012-06-21 12:09:25 -07001626
Ian Rogers0571d352011-11-03 19:51:38 -07001627 private:
Logan Chien736df022012-04-27 16:25:57 +08001628 void Init(const DexFile::CodeItem& code_item, int32_t offset);
Ian Rogers13735952014-10-08 12:43:28 -07001629 void Init(const uint8_t* handler_data);
Ian Rogers0571d352011-11-03 19:51:38 -07001630
1631 struct CatchHandlerItem {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001632 dex::TypeIndex type_idx_; // type index of the caught exception type
Ian Rogers0571d352011-11-03 19:51:38 -07001633 uint32_t address_; // handler address
1634 } handler_;
Ian Rogers13735952014-10-08 12:43:28 -07001635 const uint8_t* current_data_; // the current handler in dex file.
Ian Rogers0571d352011-11-03 19:51:38 -07001636 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 Shapiro1fb86202011-06-27 17:43:13 -07001641} // namespace art
1642
Brian Carlstromfc0e3212013-07-17 14:40:12 -07001643#endif // ART_RUNTIME_DEX_FILE_H_