blob: 759986e183f679553dd21f9abb96fedb129ea531 [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>
Ian Rogers68b56852014-08-29 20:19:11 -070022#include <unordered_map>
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070023#include <vector>
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070024
Elliott Hughes07ed66b2012-12-12 18:34:25 -080025#include "base/logging.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070026#include "base/mutex.h" // For Locks::mutator_lock_.
Ian Rogers03b6eaf2014-10-28 09:34:57 -070027#include "base/value_object.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070028#include "globals.h"
Ian Rogers08f753d2012-08-24 14:35:25 -070029#include "invoke_type.h"
Jesse Wilson6bf19152011-09-29 13:12:33 -040030#include "jni.h"
Jeff Hao13e748b2015-08-25 20:44:19 +000031#include "jvalue.h"
32#include "mirror/object_array.h"
Ian Rogers08f753d2012-08-24 14:35:25 -070033#include "modifiers.h"
Ian Rogers68b56852014-08-29 20:19:11 -070034#include "utf.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070035
36namespace art {
37
Brian Carlstrome8104522013-10-15 21:56:36 -070038// TODO: remove dependencies on mirror classes, primarily by moving
39// EncodedStaticFieldValueIterator to its own file.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040namespace mirror {
Brian Carlstromea46f952013-07-30 01:26:50 -070041 class ClassLoader;
42 class DexCache;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080043} // namespace mirror
Mathieu Chartierc7853442015-03-27 14:35:38 -070044class ArtField;
Mathieu Chartiere401d142015-04-22 13:56:20 -070045class ArtMethod;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046class ClassLinker;
Andreas Gampe2a5c4682015-08-14 08:22:54 -070047template <class Key, class Value, class EmptyFn, class HashFn, class Pred, class Alloc>
48class HashMap;
Ian Rogers576ca0c2014-06-06 15:58:22 -070049class MemMap;
Richard Uhler07b3c232015-03-31 15:57:54 -070050class OatDexFile;
Ian Rogersd91d6d62013-09-25 20:26:14 -070051class Signature;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070052template<class T> class Handle;
Ian Rogersfc0e94b2013-09-23 23:51:32 -070053class StringPiece;
Artem Udovichenkod9786b02015-10-14 16:36:55 +030054class TypeLookupTable;
Brian Carlstroma6cc8932012-01-04 14:44:07 -080055class ZipArchive;
56
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070057// TODO: move all of the macro functionality into the DexCache class.
Brian Carlstromf615a612011-07-23 12:50:34 -070058class DexFile {
Carl Shapiro1fb86202011-06-27 17:43:13 -070059 public:
Roland Levillain621b5ea2016-05-18 11:41:33 +010060 // First Dex format version supporting default methods.
Alex Lightb55f1ac2016-04-12 15:50:55 -070061 static const uint32_t kDefaultMethodsVersion = 37;
Roland Levillain621b5ea2016-05-18 11:41:33 +010062 // First Dex format version enforcing class definition ordering rules.
63 static const uint32_t kClassDefinitionOrderEnforcedVersion = 37;
64
Ian Rogers13735952014-10-08 12:43:28 -070065 static const uint8_t kDexMagic[];
Alex Lightc4961812016-03-23 10:20:41 -070066 static constexpr size_t kNumDexVersions = 2;
67 static constexpr size_t kDexVersionLen = 4;
68 static const uint8_t kDexMagicVersions[kNumDexVersions][kDexVersionLen];
69
Ian Rogers13735952014-10-08 12:43:28 -070070 static constexpr size_t kSha1DigestSize = 20;
71 static constexpr uint32_t kDexEndianConstant = 0x12345678;
Carl Shapiro80d4dde2011-06-28 16:24:07 -070072
Brian Carlstromb7bbba42011-10-13 14:58:47 -070073 // name of the DexFile entry within a zip archive
74 static const char* kClassesDex;
75
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070076 // The value of an invalid index.
77 static const uint32_t kDexNoIndex = 0xFFFFFFFF;
78
Ian Rogers0571d352011-11-03 19:51:38 -070079 // The value of an invalid index.
80 static const uint16_t kDexNoIndex16 = 0xFFFF;
Carl Shapiro1fb86202011-06-27 17:43:13 -070081
Alex Lightc4961812016-03-23 10:20:41 -070082 // The separator character in MultiDex locations.
Andreas Gampe833a4852014-05-21 18:46:59 -070083 static constexpr char kMultiDexSeparator = ':';
84
85 // A string version of the previous. This is a define so that we can merge string literals in the
86 // preprocessor.
87 #define kMultiDexSeparatorString ":"
88
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070089 // Raw header_item.
90 struct Header {
91 uint8_t magic_[8];
Brian Carlstrom7934ac22013-07-26 10:54:15 -070092 uint32_t checksum_; // See also location_checksum_
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070093 uint8_t signature_[kSha1DigestSize];
jeffhaof6174e82012-01-31 16:14:17 -080094 uint32_t file_size_; // size of entire file
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070095 uint32_t header_size_; // offset to start of next section
96 uint32_t endian_tag_;
Ian Rogers0571d352011-11-03 19:51:38 -070097 uint32_t link_size_; // unused
98 uint32_t link_off_; // unused
99 uint32_t map_off_; // unused
100 uint32_t string_ids_size_; // number of StringIds
101 uint32_t string_ids_off_; // file offset of StringIds array
102 uint32_t type_ids_size_; // number of TypeIds, we don't support more than 65535
103 uint32_t type_ids_off_; // file offset of TypeIds array
104 uint32_t proto_ids_size_; // number of ProtoIds, we don't support more than 65535
105 uint32_t proto_ids_off_; // file offset of ProtoIds array
106 uint32_t field_ids_size_; // number of FieldIds
107 uint32_t field_ids_off_; // file offset of FieldIds array
108 uint32_t method_ids_size_; // number of MethodIds
109 uint32_t method_ids_off_; // file offset of MethodIds array
110 uint32_t class_defs_size_; // number of ClassDefs
111 uint32_t class_defs_off_; // file offset of ClassDef array
112 uint32_t data_size_; // unused
113 uint32_t data_off_; // unused
Elliott Hughesa21039c2012-06-21 12:09:25 -0700114
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700115 // Decode the dex magic version
116 uint32_t GetVersion() const;
117
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700118 private:
119 DISALLOW_COPY_AND_ASSIGN(Header);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700120 };
Carl Shapiro1fb86202011-06-27 17:43:13 -0700121
jeffhao10037c82012-01-23 15:06:23 -0800122 // Map item type codes.
123 enum {
124 kDexTypeHeaderItem = 0x0000,
125 kDexTypeStringIdItem = 0x0001,
126 kDexTypeTypeIdItem = 0x0002,
127 kDexTypeProtoIdItem = 0x0003,
128 kDexTypeFieldIdItem = 0x0004,
129 kDexTypeMethodIdItem = 0x0005,
130 kDexTypeClassDefItem = 0x0006,
131 kDexTypeMapList = 0x1000,
132 kDexTypeTypeList = 0x1001,
133 kDexTypeAnnotationSetRefList = 0x1002,
134 kDexTypeAnnotationSetItem = 0x1003,
135 kDexTypeClassDataItem = 0x2000,
136 kDexTypeCodeItem = 0x2001,
137 kDexTypeStringDataItem = 0x2002,
138 kDexTypeDebugInfoItem = 0x2003,
139 kDexTypeAnnotationItem = 0x2004,
140 kDexTypeEncodedArrayItem = 0x2005,
141 kDexTypeAnnotationsDirectoryItem = 0x2006,
142 };
143
144 struct MapItem {
145 uint16_t type_;
146 uint16_t unused_;
147 uint32_t size_;
148 uint32_t offset_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700149
jeffhao10037c82012-01-23 15:06:23 -0800150 private:
151 DISALLOW_COPY_AND_ASSIGN(MapItem);
152 };
153
154 struct MapList {
155 uint32_t size_;
156 MapItem list_[1];
Elliott Hughesa21039c2012-06-21 12:09:25 -0700157
jeffhao10037c82012-01-23 15:06:23 -0800158 private:
159 DISALLOW_COPY_AND_ASSIGN(MapList);
160 };
161
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700162 // Raw string_id_item.
163 struct StringId {
164 uint32_t string_data_off_; // offset in bytes from the base address
Elliott Hughesa21039c2012-06-21 12:09:25 -0700165
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700166 private:
167 DISALLOW_COPY_AND_ASSIGN(StringId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700168 };
169
170 // Raw type_id_item.
171 struct TypeId {
172 uint32_t descriptor_idx_; // index into string_ids
Elliott Hughesa21039c2012-06-21 12:09:25 -0700173
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700174 private:
175 DISALLOW_COPY_AND_ASSIGN(TypeId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700176 };
177
178 // Raw field_id_item.
179 struct FieldId {
Ian Rogers0571d352011-11-03 19:51:38 -0700180 uint16_t class_idx_; // index into type_ids_ array for defining class
181 uint16_t type_idx_; // index into type_ids_ array for field type
182 uint32_t name_idx_; // index into string_ids_ array for field name
Elliott Hughesa21039c2012-06-21 12:09:25 -0700183
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700184 private:
185 DISALLOW_COPY_AND_ASSIGN(FieldId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700186 };
187
188 // Raw method_id_item.
189 struct MethodId {
Ian Rogers0571d352011-11-03 19:51:38 -0700190 uint16_t class_idx_; // index into type_ids_ array for defining class
191 uint16_t proto_idx_; // index into proto_ids_ array for method prototype
192 uint32_t name_idx_; // index into string_ids_ array for method name
Elliott Hughesa21039c2012-06-21 12:09:25 -0700193
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700194 private:
195 DISALLOW_COPY_AND_ASSIGN(MethodId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700196 };
197
198 // Raw proto_id_item.
199 struct ProtoId {
Ian Rogers0571d352011-11-03 19:51:38 -0700200 uint32_t shorty_idx_; // index into string_ids array for shorty descriptor
201 uint16_t return_type_idx_; // index into type_ids array for return type
202 uint16_t pad_; // padding = 0
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700203 uint32_t parameters_off_; // file offset to type_list for parameter types
Elliott Hughesa21039c2012-06-21 12:09:25 -0700204
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700205 private:
206 DISALLOW_COPY_AND_ASSIGN(ProtoId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700207 };
208
209 // Raw class_def_item.
210 struct ClassDef {
Ian Rogers0571d352011-11-03 19:51:38 -0700211 uint16_t class_idx_; // index into type_ids_ array for this class
212 uint16_t pad1_; // padding = 0
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700213 uint32_t access_flags_;
Ian Rogers0571d352011-11-03 19:51:38 -0700214 uint16_t superclass_idx_; // index into type_ids_ array for superclass
215 uint16_t pad2_; // padding = 0
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700216 uint32_t interfaces_off_; // file offset to TypeList
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700217 uint32_t source_file_idx_; // index into string_ids_ for source file name
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700218 uint32_t annotations_off_; // file offset to annotations_directory_item
219 uint32_t class_data_off_; // file offset to class_data_item
220 uint32_t static_values_off_; // file offset to EncodedArray
Elliott Hughesa21039c2012-06-21 12:09:25 -0700221
Andreas Gampe51829322014-08-25 15:05:04 -0700222 // Returns the valid access flags, that is, Java modifier bits relevant to the ClassDef type
223 // (class or interface). These are all in the lower 16b and do not contain runtime flags.
224 uint32_t GetJavaAccessFlags() const {
225 // Make sure that none of our runtime-only flags are set.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800226 static_assert((kAccValidClassFlags & kAccJavaFlagsMask) == kAccValidClassFlags,
227 "Valid class flags not a subset of Java flags");
228 static_assert((kAccValidInterfaceFlags & kAccJavaFlagsMask) == kAccValidInterfaceFlags,
229 "Valid interface flags not a subset of Java flags");
Andreas Gampe51829322014-08-25 15:05:04 -0700230
231 if ((access_flags_ & kAccInterface) != 0) {
232 // Interface.
233 return access_flags_ & kAccValidInterfaceFlags;
234 } else {
235 // Class.
236 return access_flags_ & kAccValidClassFlags;
237 }
238 }
239
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700240 private:
241 DISALLOW_COPY_AND_ASSIGN(ClassDef);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700242 };
243
244 // Raw type_item.
245 struct TypeItem {
246 uint16_t type_idx_; // index into type_ids section
Elliott Hughesa21039c2012-06-21 12:09:25 -0700247
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700248 private:
249 DISALLOW_COPY_AND_ASSIGN(TypeItem);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700250 };
251
252 // Raw type_list.
253 class TypeList {
254 public:
255 uint32_t Size() const {
256 return size_;
257 }
258
259 const TypeItem& GetTypeItem(uint32_t idx) const {
Sebastien Hertzb24bd992013-08-02 15:19:09 +0200260 DCHECK_LT(idx, this->size_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700261 return this->list_[idx];
262 }
263
Andreas Gampe31a7a0c2014-08-29 16:07:49 -0700264 // Size in bytes of the part of the list that is common.
265 static constexpr size_t GetHeaderSize() {
266 return 4U;
267 }
268
269 // Size in bytes of the whole type list including all the stored elements.
270 static constexpr size_t GetListSize(size_t count) {
271 return GetHeaderSize() + sizeof(TypeItem) * count;
272 }
273
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700274 private:
275 uint32_t size_; // size of the list, in entries
276 TypeItem list_[1]; // elements of the list
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700277 DISALLOW_COPY_AND_ASSIGN(TypeList);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700278 };
279
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700280 // Raw code_item.
281 struct CodeItem {
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700282 uint16_t registers_size_; // the number of registers used by this code
283 // (locals + parameters)
284 uint16_t ins_size_; // the number of words of incoming arguments to the method
285 // that this code is for
286 uint16_t outs_size_; // the number of words of outgoing argument space required
287 // by this code for method invocation
288 uint16_t tries_size_; // the number of try_items for this instance. If non-zero,
289 // then these appear as the tries array just after the
290 // insns in this instance.
291 uint32_t debug_info_off_; // file offset to debug info stream
Ian Rogersd81871c2011-10-03 13:57:23 -0700292 uint32_t insns_size_in_code_units_; // size of the insns array, in 2 byte code units
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700293 uint16_t insns_[1]; // actual array of bytecode.
Elliott Hughesa21039c2012-06-21 12:09:25 -0700294
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700295 private:
296 DISALLOW_COPY_AND_ASSIGN(CodeItem);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700297 };
298
Carl Shapiro2eaa9682011-08-04 19:26:11 -0700299 // Raw try_item.
300 struct TryItem {
301 uint32_t start_addr_;
302 uint16_t insn_count_;
303 uint16_t handler_off_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700304
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700305 private:
306 DISALLOW_COPY_AND_ASSIGN(TryItem);
Carl Shapiro2eaa9682011-08-04 19:26:11 -0700307 };
308
jeffhao10037c82012-01-23 15:06:23 -0800309 // Annotation constants.
310 enum {
311 kDexVisibilityBuild = 0x00, /* annotation visibility */
312 kDexVisibilityRuntime = 0x01,
313 kDexVisibilitySystem = 0x02,
314
315 kDexAnnotationByte = 0x00,
316 kDexAnnotationShort = 0x02,
317 kDexAnnotationChar = 0x03,
318 kDexAnnotationInt = 0x04,
319 kDexAnnotationLong = 0x06,
320 kDexAnnotationFloat = 0x10,
321 kDexAnnotationDouble = 0x11,
322 kDexAnnotationString = 0x17,
323 kDexAnnotationType = 0x18,
324 kDexAnnotationField = 0x19,
325 kDexAnnotationMethod = 0x1a,
326 kDexAnnotationEnum = 0x1b,
327 kDexAnnotationArray = 0x1c,
328 kDexAnnotationAnnotation = 0x1d,
329 kDexAnnotationNull = 0x1e,
330 kDexAnnotationBoolean = 0x1f,
331
332 kDexAnnotationValueTypeMask = 0x1f, /* low 5 bits */
333 kDexAnnotationValueArgShift = 5,
334 };
335
336 struct AnnotationsDirectoryItem {
337 uint32_t class_annotations_off_;
338 uint32_t fields_size_;
339 uint32_t methods_size_;
340 uint32_t parameters_size_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700341
jeffhao10037c82012-01-23 15:06:23 -0800342 private:
343 DISALLOW_COPY_AND_ASSIGN(AnnotationsDirectoryItem);
344 };
345
346 struct FieldAnnotationsItem {
347 uint32_t field_idx_;
348 uint32_t annotations_off_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700349
jeffhao10037c82012-01-23 15:06:23 -0800350 private:
351 DISALLOW_COPY_AND_ASSIGN(FieldAnnotationsItem);
352 };
353
354 struct MethodAnnotationsItem {
355 uint32_t method_idx_;
356 uint32_t annotations_off_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700357
jeffhao10037c82012-01-23 15:06:23 -0800358 private:
359 DISALLOW_COPY_AND_ASSIGN(MethodAnnotationsItem);
360 };
361
362 struct ParameterAnnotationsItem {
363 uint32_t method_idx_;
364 uint32_t annotations_off_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700365
jeffhao10037c82012-01-23 15:06:23 -0800366 private:
367 DISALLOW_COPY_AND_ASSIGN(ParameterAnnotationsItem);
368 };
369
370 struct AnnotationSetRefItem {
371 uint32_t annotations_off_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700372
jeffhao10037c82012-01-23 15:06:23 -0800373 private:
374 DISALLOW_COPY_AND_ASSIGN(AnnotationSetRefItem);
375 };
376
377 struct AnnotationSetRefList {
378 uint32_t size_;
379 AnnotationSetRefItem list_[1];
Elliott Hughesa21039c2012-06-21 12:09:25 -0700380
jeffhao10037c82012-01-23 15:06:23 -0800381 private:
382 DISALLOW_COPY_AND_ASSIGN(AnnotationSetRefList);
383 };
384
385 struct AnnotationSetItem {
386 uint32_t size_;
387 uint32_t entries_[1];
Elliott Hughesa21039c2012-06-21 12:09:25 -0700388
jeffhao10037c82012-01-23 15:06:23 -0800389 private:
390 DISALLOW_COPY_AND_ASSIGN(AnnotationSetItem);
391 };
392
393 struct AnnotationItem {
394 uint8_t visibility_;
395 uint8_t annotation_[1];
Elliott Hughesa21039c2012-06-21 12:09:25 -0700396
jeffhao10037c82012-01-23 15:06:23 -0800397 private:
398 DISALLOW_COPY_AND_ASSIGN(AnnotationItem);
399 };
400
Jeff Hao13e748b2015-08-25 20:44:19 +0000401 struct AnnotationValue {
402 JValue value_;
403 uint8_t type_;
404 };
405
406 enum AnnotationResultStyle { // private
407 kAllObjects,
408 kPrimitivesOrObjects,
409 kAllRaw
410 };
411
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800412 // Returns the checksum of a file for comparison with GetLocationChecksum().
413 // For .dex files, this is the header checksum.
414 // For zip files, this is the classes.dex zip entry CRC32 checksum.
415 // Return true if the checksum could be found, false otherwise.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700416 static bool GetChecksum(const char* filename, uint32_t* checksum, std::string* error_msg);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700417
Andreas Gampe833a4852014-05-21 18:46:59 -0700418 // Opens .dex files found in the container, guessing the container format based on file extension.
Aart Bik37d6a3b2016-06-21 18:30:10 -0700419 static bool Open(const char* filename,
420 const char* location,
421 bool verify_checksum,
422 std::string* error_msg,
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800423 std::vector<std::unique_ptr<const DexFile>>* dex_files);
jeffhao262bf462011-10-20 18:36:32 -0700424
Andreas Gampe0cba0042015-04-29 20:47:16 -0700425 // Checks whether the given file has the dex magic, or is a zip file with a classes.dex entry.
426 // If this function returns false, Open will not succeed. The inverse is not true, however.
427 static bool MaybeDex(const char* filename);
428
Brian Carlstrom89521892011-12-07 22:05:07 -0800429 // Opens .dex file, backed by existing memory
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800430 static std::unique_ptr<const DexFile> Open(const uint8_t* base, size_t size,
431 const std::string& location,
432 uint32_t location_checksum,
Richard Uhler07b3c232015-03-31 15:57:54 -0700433 const OatDexFile* oat_dex_file,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800434 bool verify,
Aart Bik37d6a3b2016-06-21 18:30:10 -0700435 bool verify_checksum,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800436 std::string* error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -0800437
Andreas Gampe833a4852014-05-21 18:46:59 -0700438 // Open all classesXXX.dex files from a zip archive.
Aart Bik37d6a3b2016-06-21 18:30:10 -0700439 static bool OpenFromZip(const ZipArchive& zip_archive,
440 const std::string& location,
441 bool verify_checksum,
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800442 std::string* error_msg,
443 std::vector<std::unique_ptr<const DexFile>>* dex_files);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800444
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700445 // Closes a .dex file.
Brian Carlstromf615a612011-07-23 12:50:34 -0700446 virtual ~DexFile();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700447
Brian Carlstroma663ea52011-08-19 23:33:41 -0700448 const std::string& GetLocation() const {
449 return location_;
450 }
451
Andreas Gampecb8f9e82014-07-24 15:35:50 -0700452 // For normal dex files, location and base location coincide. If a dex file is part of a multidex
453 // archive, the base location is the name of the originating jar/apk, stripped of any internal
454 // classes*.dex path.
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100455 static std::string GetBaseLocation(const char* location) {
456 const char* pos = strrchr(location, kMultiDexSeparator);
457 if (pos == nullptr) {
458 return location;
Andreas Gampecb8f9e82014-07-24 15:35:50 -0700459 } else {
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100460 return std::string(location, pos - location);
461 }
462 }
463
Richard Uhlere5fed032015-03-18 08:21:11 -0700464 static std::string GetBaseLocation(const std::string& location) {
465 return GetBaseLocation(location.c_str());
466 }
467
468 // Returns the ':classes*.dex' part of the dex location. Returns an empty
469 // string if there is no multidex suffix for the given location.
470 // The kMultiDexSeparator is included in the returned suffix.
471 static std::string GetMultiDexSuffix(const std::string& location) {
472 size_t pos = location.rfind(kMultiDexSeparator);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100473 if (pos == std::string::npos) {
Richard Uhlere5fed032015-03-18 08:21:11 -0700474 return "";
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100475 } else {
Richard Uhlere5fed032015-03-18 08:21:11 -0700476 return location.substr(pos);
Andreas Gampecb8f9e82014-07-24 15:35:50 -0700477 }
478 }
479
Richard Uhlere5fed032015-03-18 08:21:11 -0700480 std::string GetBaseLocation() const {
481 return GetBaseLocation(location_);
482 }
483
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800484 // For DexFiles directly from .dex files, this is the checksum from the DexFile::Header.
485 // For DexFiles opened from a zip files, this will be the ZipEntry CRC32 of classes.dex.
486 uint32_t GetLocationChecksum() const {
487 return location_checksum_;
488 }
489
Brian Carlstroma663ea52011-08-19 23:33:41 -0700490 const Header& GetHeader() const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700491 DCHECK(header_ != nullptr) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700492 return *header_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700493 }
494
Ian Rogers0571d352011-11-03 19:51:38 -0700495 // Decode the dex magic version
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700496 uint32_t GetVersion() const {
497 return GetHeader().GetVersion();
498 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700499
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800500 // Returns true if the byte string points to the magic value.
Ian Rogers13735952014-10-08 12:43:28 -0700501 static bool IsMagicValid(const uint8_t* magic);
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800502
503 // Returns true if the byte string after the magic is the correct value.
Ian Rogers13735952014-10-08 12:43:28 -0700504 static bool IsVersionValid(const uint8_t* magic);
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800505
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700506 // Returns the number of string identifiers in the .dex file.
507 size_t NumStringIds() const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700508 DCHECK(header_ != nullptr) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700509 return header_->string_ids_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700510 }
511
Ian Rogers0571d352011-11-03 19:51:38 -0700512 // Returns the StringId at the specified index.
513 const StringId& GetStringId(uint32_t idx) const {
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700514 DCHECK_LT(idx, NumStringIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700515 return string_ids_[idx];
516 }
517
518 uint32_t GetIndexForStringId(const StringId& string_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800519 CHECK_GE(&string_id, string_ids_) << GetLocation();
520 CHECK_LT(&string_id, string_ids_ + header_->string_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700521 return &string_id - string_ids_;
522 }
523
524 int32_t GetStringLength(const StringId& string_id) const;
525
Ian Rogersdfb325e2013-10-30 01:00:44 -0700526 // Returns a pointer to the UTF-8 string data referred to by the given string_id as well as the
527 // length of the string when decoded as a UTF-16 string. Note the UTF-16 length is not the same
528 // as the string length of the string data.
529 const char* GetStringDataAndUtf16Length(const StringId& string_id, uint32_t* utf16_length) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700530
531 const char* GetStringData(const StringId& string_id) const {
Ian Rogersdfb325e2013-10-30 01:00:44 -0700532 uint32_t ignored;
533 return GetStringDataAndUtf16Length(string_id, &ignored);
Ian Rogers0571d352011-11-03 19:51:38 -0700534 }
535
Ian Rogersdfb325e2013-10-30 01:00:44 -0700536 // Index version of GetStringDataAndUtf16Length.
537 const char* StringDataAndUtf16LengthByIdx(uint32_t idx, uint32_t* utf16_length) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700538 if (idx == kDexNoIndex) {
Ian Rogersdfb325e2013-10-30 01:00:44 -0700539 *utf16_length = 0;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700540 return nullptr;
Ian Rogers0571d352011-11-03 19:51:38 -0700541 }
542 const StringId& string_id = GetStringId(idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700543 return GetStringDataAndUtf16Length(string_id, utf16_length);
Ian Rogers0571d352011-11-03 19:51:38 -0700544 }
545
546 const char* StringDataByIdx(uint32_t idx) const {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800547 uint32_t unicode_length;
Ian Rogersdfb325e2013-10-30 01:00:44 -0700548 return StringDataAndUtf16LengthByIdx(idx, &unicode_length);
Ian Rogers0571d352011-11-03 19:51:38 -0700549 }
550
Ian Rogers637c65b2013-05-31 11:46:00 -0700551 // Looks up a string id for a given modified utf8 string.
552 const StringId* FindStringId(const char* string) const;
553
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300554 const TypeId* FindTypeId(const char* string) const;
555
Ian Rogers637c65b2013-05-31 11:46:00 -0700556 // Looks up a string id for a given utf16 string.
Vladimir Markoa48aef42014-12-03 17:53:53 +0000557 const StringId* FindStringId(const uint16_t* string, size_t length) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700558
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700559 // Returns the number of type identifiers in the .dex file.
Ian Rogers68b56852014-08-29 20:19:11 -0700560 uint32_t NumTypeIds() const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700561 DCHECK(header_ != nullptr) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700562 return header_->type_ids_size_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700563 }
564
Ian Rogers0571d352011-11-03 19:51:38 -0700565 // Returns the TypeId at the specified index.
566 const TypeId& GetTypeId(uint32_t idx) const {
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700567 DCHECK_LT(idx, NumTypeIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700568 return type_ids_[idx];
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700569 }
570
Ian Rogers0571d352011-11-03 19:51:38 -0700571 uint16_t GetIndexForTypeId(const TypeId& type_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800572 CHECK_GE(&type_id, type_ids_) << GetLocation();
573 CHECK_LT(&type_id, type_ids_ + header_->type_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700574 size_t result = &type_id - type_ids_;
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800575 DCHECK_LT(result, 65536U) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700576 return static_cast<uint16_t>(result);
577 }
578
579 // Get the descriptor string associated with a given type index.
Elliott Hughes45651fd2012-02-21 15:48:20 -0800580 const char* StringByTypeIdx(uint32_t idx, uint32_t* unicode_length) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700581 const TypeId& type_id = GetTypeId(idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700582 return StringDataAndUtf16LengthByIdx(type_id.descriptor_idx_, unicode_length);
Ian Rogers0571d352011-11-03 19:51:38 -0700583 }
584
585 const char* StringByTypeIdx(uint32_t idx) const {
586 const TypeId& type_id = GetTypeId(idx);
587 return StringDataByIdx(type_id.descriptor_idx_);
588 }
589
590 // Returns the type descriptor string of a type id.
591 const char* GetTypeDescriptor(const TypeId& type_id) const {
592 return StringDataByIdx(type_id.descriptor_idx_);
593 }
594
595 // Looks up a type for the given string index
596 const TypeId* FindTypeId(uint32_t string_idx) const;
597
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700598 // Returns the number of field identifiers in the .dex file.
599 size_t NumFieldIds() const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700600 DCHECK(header_ != nullptr) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700601 return header_->field_ids_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700602 }
603
Ian Rogers0571d352011-11-03 19:51:38 -0700604 // Returns the FieldId at the specified index.
605 const FieldId& GetFieldId(uint32_t idx) const {
Sebastien Hertzb24bd992013-08-02 15:19:09 +0200606 DCHECK_LT(idx, NumFieldIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700607 return field_ids_[idx];
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700608 }
609
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800610 uint32_t GetIndexForFieldId(const FieldId& field_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800611 CHECK_GE(&field_id, field_ids_) << GetLocation();
612 CHECK_LT(&field_id, field_ids_ + header_->field_ids_size_) << GetLocation();
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800613 return &field_id - field_ids_;
614 }
615
616 // Looks up a field by its declaring class, name and type
617 const FieldId* FindFieldId(const DexFile::TypeId& declaring_klass,
618 const DexFile::StringId& name,
619 const DexFile::TypeId& type) const;
620
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700621 // Returns the declaring class descriptor string of a field id.
622 const char* GetFieldDeclaringClassDescriptor(const FieldId& field_id) const {
Brian Carlstromb9edb842011-08-28 16:31:06 -0700623 const DexFile::TypeId& type_id = GetTypeId(field_id.class_idx_);
624 return GetTypeDescriptor(type_id);
625 }
626
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700627 // Returns the class descriptor string of a field id.
628 const char* GetFieldTypeDescriptor(const FieldId& field_id) const {
629 const DexFile::TypeId& type_id = GetTypeId(field_id.type_idx_);
630 return GetTypeDescriptor(type_id);
631 }
632
Brian Carlstromb9edb842011-08-28 16:31:06 -0700633 // Returns the name of a field id.
634 const char* GetFieldName(const FieldId& field_id) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700635 return StringDataByIdx(field_id.name_idx_);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700636 }
637
Ian Rogers0571d352011-11-03 19:51:38 -0700638 // Returns the number of method identifiers in the .dex file.
639 size_t NumMethodIds() const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700640 DCHECK(header_ != nullptr) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700641 return header_->method_ids_size_;
642 }
643
644 // Returns the MethodId at the specified index.
645 const MethodId& GetMethodId(uint32_t idx) const {
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700646 DCHECK_LT(idx, NumMethodIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700647 return method_ids_[idx];
648 }
649
650 uint32_t GetIndexForMethodId(const MethodId& method_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800651 CHECK_GE(&method_id, method_ids_) << GetLocation();
652 CHECK_LT(&method_id, method_ids_ + header_->method_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700653 return &method_id - method_ids_;
654 }
655
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800656 // Looks up a method by its declaring class, name and proto_id
657 const MethodId* FindMethodId(const DexFile::TypeId& declaring_klass,
658 const DexFile::StringId& name,
Ian Rogers0571d352011-11-03 19:51:38 -0700659 const DexFile::ProtoId& signature) const;
660
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700661 // Returns the declaring class descriptor string of a method id.
662 const char* GetMethodDeclaringClassDescriptor(const MethodId& method_id) const {
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700663 const DexFile::TypeId& type_id = GetTypeId(method_id.class_idx_);
664 return GetTypeDescriptor(type_id);
665 }
666
jeffhao98eacac2011-09-14 16:11:53 -0700667 // Returns the prototype of a method id.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700668 const ProtoId& GetMethodPrototype(const MethodId& method_id) const {
669 return GetProtoId(method_id.proto_idx_);
670 }
671
Ian Rogersd91d6d62013-09-25 20:26:14 -0700672 // Returns a representation of the signature of a method id.
673 const Signature GetMethodSignature(const MethodId& method_id) const;
jeffhao98eacac2011-09-14 16:11:53 -0700674
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700675 // Returns the name of a method id.
676 const char* GetMethodName(const MethodId& method_id) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700677 return StringDataByIdx(method_id.name_idx_);
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700678 }
679
Calin Juravle68ad6492015-08-18 17:08:12 +0100680 // Returns the shorty of a method by its index.
681 const char* GetMethodShorty(uint32_t idx) const {
682 return StringDataByIdx(GetProtoId(GetMethodId(idx).proto_idx_).shorty_idx_);
683 }
684
Ian Rogers0571d352011-11-03 19:51:38 -0700685 // Returns the shorty of a method id.
686 const char* GetMethodShorty(const MethodId& method_id) const {
687 return StringDataByIdx(GetProtoId(method_id.proto_idx_).shorty_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700688 }
Elliott Hughes45651fd2012-02-21 15:48:20 -0800689 const char* GetMethodShorty(const MethodId& method_id, uint32_t* length) const {
Ian Rogerscf5077a2013-10-31 12:37:54 -0700690 // Using the UTF16 length is safe here as shorties are guaranteed to be ASCII characters.
Ian Rogersdfb325e2013-10-30 01:00:44 -0700691 return StringDataAndUtf16LengthByIdx(GetProtoId(method_id.proto_idx_).shorty_idx_, length);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800692 }
Ian Rogers0571d352011-11-03 19:51:38 -0700693 // Returns the number of class definitions in the .dex file.
Ian Rogers68b56852014-08-29 20:19:11 -0700694 uint32_t NumClassDefs() const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700695 DCHECK(header_ != nullptr) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700696 return header_->class_defs_size_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700697 }
698
699 // Returns the ClassDef at the specified index.
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700700 const ClassDef& GetClassDef(uint16_t idx) const {
Sebastien Hertzb24bd992013-08-02 15:19:09 +0200701 DCHECK_LT(idx, NumClassDefs()) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700702 return class_defs_[idx];
703 }
704
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700705 uint16_t GetIndexForClassDef(const ClassDef& class_def) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800706 CHECK_GE(&class_def, class_defs_) << GetLocation();
707 CHECK_LT(&class_def, class_defs_ + header_->class_defs_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700708 return &class_def - class_defs_;
709 }
710
711 // Returns the class descriptor string of a class definition.
712 const char* GetClassDescriptor(const ClassDef& class_def) const {
713 return StringByTypeIdx(class_def.class_idx_);
714 }
715
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800716 // Looks up a class definition by its class descriptor. Hash must be
717 // ComputeModifiedUtf8Hash(descriptor).
718 const ClassDef* FindClassDef(const char* descriptor, size_t hash) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700719
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700720 // Looks up a class definition by its type index.
721 const ClassDef* FindClassDef(uint16_t type_idx) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700722
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700723 const TypeList* GetInterfacesList(const ClassDef& class_def) const {
724 if (class_def.interfaces_off_ == 0) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700725 return nullptr;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700726 } else {
Ian Rogers13735952014-10-08 12:43:28 -0700727 const uint8_t* addr = begin_ + class_def.interfaces_off_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700728 return reinterpret_cast<const TypeList*>(addr);
729 }
730 }
731
Ian Rogers0571d352011-11-03 19:51:38 -0700732 // Returns a pointer to the raw memory mapped class_data_item
Ian Rogers13735952014-10-08 12:43:28 -0700733 const uint8_t* GetClassData(const ClassDef& class_def) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700734 if (class_def.class_data_off_ == 0) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700735 return nullptr;
Ian Rogers0571d352011-11-03 19:51:38 -0700736 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800737 return begin_ + class_def.class_data_off_;
Ian Rogers0571d352011-11-03 19:51:38 -0700738 }
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700739 }
740
Ian Rogers0571d352011-11-03 19:51:38 -0700741 //
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800742 const CodeItem* GetCodeItem(const uint32_t code_off) const {
Alex Light9139e002015-10-09 15:59:48 -0700743 DCHECK_LT(code_off, size_) << "Code item offset larger then maximum allowed offset";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800744 if (code_off == 0) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700745 return nullptr; // native or abstract method
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700746 } else {
Ian Rogers13735952014-10-08 12:43:28 -0700747 const uint8_t* addr = begin_ + code_off;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700748 return reinterpret_cast<const CodeItem*>(addr);
749 }
750 }
751
Ian Rogers0571d352011-11-03 19:51:38 -0700752 const char* GetReturnTypeDescriptor(const ProtoId& proto_id) const {
753 return StringByTypeIdx(proto_id.return_type_idx_);
754 }
755
756 // Returns the number of prototype identifiers in the .dex file.
757 size_t NumProtoIds() const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700758 DCHECK(header_ != nullptr) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700759 return header_->proto_ids_size_;
760 }
761
762 // Returns the ProtoId at the specified index.
763 const ProtoId& GetProtoId(uint32_t idx) const {
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700764 DCHECK_LT(idx, NumProtoIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700765 return proto_ids_[idx];
766 }
767
768 uint16_t GetIndexForProtoId(const ProtoId& proto_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800769 CHECK_GE(&proto_id, proto_ids_) << GetLocation();
770 CHECK_LT(&proto_id, proto_ids_ + header_->proto_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700771 return &proto_id - proto_ids_;
772 }
773
774 // Looks up a proto id for a given return type and signature type list
Ian Rogersd91d6d62013-09-25 20:26:14 -0700775 const ProtoId* FindProtoId(uint16_t return_type_idx,
Vladimir Marko5c96e6b2013-11-14 15:34:17 +0000776 const uint16_t* signature_type_idxs, uint32_t signature_length) const;
777 const ProtoId* FindProtoId(uint16_t return_type_idx,
778 const std::vector<uint16_t>& signature_type_idxs) const {
779 return FindProtoId(return_type_idx, &signature_type_idxs[0], signature_type_idxs.size());
780 }
Ian Rogers0571d352011-11-03 19:51:38 -0700781
782 // Given a signature place the type ids into the given vector, returns true on success
Ian Rogersd91d6d62013-09-25 20:26:14 -0700783 bool CreateTypeList(const StringPiece& signature, uint16_t* return_type_idx,
784 std::vector<uint16_t>* param_type_idxs) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700785
Ian Rogersd91d6d62013-09-25 20:26:14 -0700786 // Create a Signature from the given string signature or return Signature::NoSignature if not
787 // possible.
788 const Signature CreateSignature(const StringPiece& signature) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700789
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700790 // Returns the short form method descriptor for the given prototype.
791 const char* GetShorty(uint32_t proto_idx) const {
792 const ProtoId& proto_id = GetProtoId(proto_idx);
Ian Rogers0571d352011-11-03 19:51:38 -0700793 return StringDataByIdx(proto_id.shorty_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700794 }
795
796 const TypeList* GetProtoParameters(const ProtoId& proto_id) const {
797 if (proto_id.parameters_off_ == 0) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700798 return nullptr;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700799 } else {
Ian Rogers13735952014-10-08 12:43:28 -0700800 const uint8_t* addr = begin_ + proto_id.parameters_off_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700801 return reinterpret_cast<const TypeList*>(addr);
802 }
803 }
804
Ian Rogers13735952014-10-08 12:43:28 -0700805 const uint8_t* GetEncodedStaticFieldValuesArray(const ClassDef& class_def) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700806 if (class_def.static_values_off_ == 0) {
807 return 0;
808 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800809 return begin_ + class_def.static_values_off_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700810 }
811 }
812
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800813 static const TryItem* GetTryItems(const CodeItem& code_item, uint32_t offset);
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700814
815 // Get the base of the encoded data for the given DexCode.
Ian Rogers13735952014-10-08 12:43:28 -0700816 static const uint8_t* GetCatchHandlerData(const CodeItem& code_item, uint32_t offset) {
817 const uint8_t* handler_data =
818 reinterpret_cast<const uint8_t*>(GetTryItems(code_item, code_item.tries_size_));
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700819 return handler_data + offset;
820 }
821
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700822 // Find which try region is associated with the given address (ie dex pc). Returns -1 if none.
823 static int32_t FindTryItem(const CodeItem &code_item, uint32_t address);
824
825 // Find the handler offset associated with the given address (ie dex pc). Returns -1 if none.
826 static int32_t FindCatchHandlerOffset(const CodeItem &code_item, uint32_t address);
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700827
Shih-wei Liao195487c2011-08-20 13:29:04 -0700828 // Get the pointer to the start of the debugging data
Ian Rogers13735952014-10-08 12:43:28 -0700829 const uint8_t* GetDebugInfoStream(const CodeItem* code_item) const {
David Srbecky68529422015-07-07 19:13:29 +0100830 // Check that the offset is in bounds.
831 // Note that although the specification says that 0 should be used if there
832 // is no debug information, some applications incorrectly use 0xFFFFFFFF.
833 if (code_item->debug_info_off_ == 0 || code_item->debug_info_off_ >= size_) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700834 return nullptr;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700835 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800836 return begin_ + code_item->debug_info_off_;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700837 }
838 }
839
David Srbeckyb06e28e2015-12-10 13:15:00 +0000840 struct PositionInfo {
841 PositionInfo()
842 : address_(0),
843 line_(0),
844 source_file_(nullptr),
845 prologue_end_(false),
846 epilogue_begin_(false) {
847 }
848
849 uint32_t address_; // In 16-bit code units.
850 uint32_t line_; // Source code line number starting at 1.
851 const char* source_file_; // nullptr if the file from ClassDef still applies.
852 bool prologue_end_;
853 bool epilogue_begin_;
854 };
855
Shih-wei Liao195487c2011-08-20 13:29:04 -0700856 // Callback for "new position table entry".
857 // Returning true causes the decoder to stop early.
David Srbeckyb06e28e2015-12-10 13:15:00 +0000858 typedef bool (*DexDebugNewPositionCb)(void* context, const PositionInfo& entry);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700859
David Srbeckyb06e28e2015-12-10 13:15:00 +0000860 struct LocalInfo {
861 LocalInfo()
862 : name_(nullptr),
863 descriptor_(nullptr),
864 signature_(nullptr),
865 start_address_(0),
866 end_address_(0),
867 reg_(0),
868 is_live_(false) {
869 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700870
David Srbeckyb06e28e2015-12-10 13:15:00 +0000871 const char* name_; // E.g., list. It can be nullptr if unknown.
872 const char* descriptor_; // E.g., Ljava/util/LinkedList;
873 const char* signature_; // E.g., java.util.LinkedList<java.lang.Integer>
874 uint32_t start_address_; // PC location where the local is first defined.
875 uint32_t end_address_; // PC location where the local is no longer defined.
876 uint16_t reg_; // Dex register which stores the values.
877 bool is_live_; // Is the local defined and live.
878 };
879
880 // Callback for "new locals table entry".
881 typedef void (*DexDebugNewLocalCb)(void* context, const LocalInfo& entry);
882
883 static bool LineNumForPcCb(void* context, const PositionInfo& entry);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700884
Jeff Hao13e748b2015-08-25 20:44:19 +0000885 const AnnotationsDirectoryItem* GetAnnotationsDirectory(const ClassDef& class_def) const {
886 if (class_def.annotations_off_ == 0) {
887 return nullptr;
888 } else {
889 return reinterpret_cast<const AnnotationsDirectoryItem*>(begin_ + class_def.annotations_off_);
890 }
891 }
892
893 const AnnotationSetItem* GetClassAnnotationSet(const AnnotationsDirectoryItem* anno_dir) const {
894 if (anno_dir->class_annotations_off_ == 0) {
895 return nullptr;
896 } else {
897 return reinterpret_cast<const AnnotationSetItem*>(begin_ + anno_dir->class_annotations_off_);
898 }
899 }
900
901 const FieldAnnotationsItem* GetFieldAnnotations(const AnnotationsDirectoryItem* anno_dir) const {
902 if (anno_dir->fields_size_ == 0) {
903 return nullptr;
904 } else {
905 return reinterpret_cast<const FieldAnnotationsItem*>(&anno_dir[1]);
906 }
907 }
908
909 const MethodAnnotationsItem* GetMethodAnnotations(const AnnotationsDirectoryItem* anno_dir)
910 const {
911 if (anno_dir->methods_size_ == 0) {
912 return nullptr;
913 } else {
914 // Skip past the header and field annotations.
915 const uint8_t* addr = reinterpret_cast<const uint8_t*>(&anno_dir[1]);
916 addr += anno_dir->fields_size_ * sizeof(FieldAnnotationsItem);
917 return reinterpret_cast<const MethodAnnotationsItem*>(addr);
918 }
919 }
920
921 const ParameterAnnotationsItem* GetParameterAnnotations(const AnnotationsDirectoryItem* anno_dir)
922 const {
923 if (anno_dir->parameters_size_ == 0) {
924 return nullptr;
925 } else {
926 // Skip past the header, field annotations, and method annotations.
927 const uint8_t* addr = reinterpret_cast<const uint8_t*>(&anno_dir[1]);
928 addr += anno_dir->fields_size_ * sizeof(FieldAnnotationsItem);
929 addr += anno_dir->methods_size_ * sizeof(MethodAnnotationsItem);
930 return reinterpret_cast<const ParameterAnnotationsItem*>(addr);
931 }
932 }
933
934 const AnnotationSetItem* GetFieldAnnotationSetItem(const FieldAnnotationsItem& anno_item) 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 AnnotationSetItem* GetMethodAnnotationSetItem(const MethodAnnotationsItem& anno_item)
944 const {
945 uint32_t offset = anno_item.annotations_off_;
946 if (offset == 0) {
947 return nullptr;
948 } else {
949 return reinterpret_cast<const AnnotationSetItem*>(begin_ + offset);
950 }
951 }
952
953 const AnnotationSetRefList* GetParameterAnnotationSetRefList(
954 const ParameterAnnotationsItem* anno_item) const {
955 uint32_t offset = anno_item->annotations_off_;
956 if (offset == 0) {
957 return nullptr;
958 }
959 return reinterpret_cast<const AnnotationSetRefList*>(begin_ + offset);
960 }
961
962 const AnnotationItem* GetAnnotationItem(const AnnotationSetItem* set_item, uint32_t index) const {
963 DCHECK_LE(index, set_item->size_);
964 uint32_t offset = set_item->entries_[index];
965 if (offset == 0) {
966 return nullptr;
967 } else {
968 return reinterpret_cast<const AnnotationItem*>(begin_ + offset);
969 }
970 }
971
972 const AnnotationSetItem* GetSetRefItemItem(const AnnotationSetRefItem* anno_item) const {
973 uint32_t offset = anno_item->annotations_off_;
974 if (offset == 0) {
975 return nullptr;
976 }
977 return reinterpret_cast<const AnnotationSetItem*>(begin_ + offset);
978 }
979
980 const AnnotationSetItem* FindAnnotationSetForField(ArtField* field) const
981 SHARED_REQUIRES(Locks::mutator_lock_);
982 mirror::Object* GetAnnotationForField(ArtField* field, Handle<mirror::Class> annotation_class)
983 const SHARED_REQUIRES(Locks::mutator_lock_);
984 mirror::ObjectArray<mirror::Object>* GetAnnotationsForField(ArtField* field) const
985 SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Hao2a5892f2015-08-31 15:00:40 -0700986 mirror::ObjectArray<mirror::String>* GetSignatureAnnotationForField(ArtField* field) const
Jeff Hao13e748b2015-08-25 20:44:19 +0000987 SHARED_REQUIRES(Locks::mutator_lock_);
988 bool IsFieldAnnotationPresent(ArtField* field, Handle<mirror::Class> annotation_class) const
989 SHARED_REQUIRES(Locks::mutator_lock_);
990
991 const AnnotationSetItem* FindAnnotationSetForMethod(ArtMethod* method) const
992 SHARED_REQUIRES(Locks::mutator_lock_);
993 const ParameterAnnotationsItem* FindAnnotationsItemForMethod(ArtMethod* method) const
994 SHARED_REQUIRES(Locks::mutator_lock_);
995 mirror::Object* GetAnnotationDefaultValue(ArtMethod* method) const
996 SHARED_REQUIRES(Locks::mutator_lock_);
997 mirror::Object* GetAnnotationForMethod(ArtMethod* method, Handle<mirror::Class> annotation_class)
998 const SHARED_REQUIRES(Locks::mutator_lock_);
999 mirror::ObjectArray<mirror::Object>* GetAnnotationsForMethod(ArtMethod* method) const
1000 SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Hao2a5892f2015-08-31 15:00:40 -07001001 mirror::ObjectArray<mirror::Class>* GetExceptionTypesForMethod(ArtMethod* method) const
Jeff Hao13e748b2015-08-25 20:44:19 +00001002 SHARED_REQUIRES(Locks::mutator_lock_);
1003 mirror::ObjectArray<mirror::Object>* GetParameterAnnotations(ArtMethod* method) const
1004 SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Hao1133db72016-04-04 19:50:14 -07001005 mirror::ObjectArray<mirror::String>* GetSignatureAnnotationForMethod(ArtMethod* method) const
1006 SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Hao13e748b2015-08-25 20:44:19 +00001007 bool IsMethodAnnotationPresent(ArtMethod* method, Handle<mirror::Class> annotation_class) const
1008 SHARED_REQUIRES(Locks::mutator_lock_);
1009
1010 const AnnotationSetItem* FindAnnotationSetForClass(Handle<mirror::Class> klass) const
1011 SHARED_REQUIRES(Locks::mutator_lock_);
1012 mirror::Object* GetAnnotationForClass(Handle<mirror::Class> klass,
1013 Handle<mirror::Class> annotation_class) const
1014 SHARED_REQUIRES(Locks::mutator_lock_);
1015 mirror::ObjectArray<mirror::Object>* GetAnnotationsForClass(Handle<mirror::Class> klass) const
1016 SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Hao2a5892f2015-08-31 15:00:40 -07001017 mirror::ObjectArray<mirror::Class>* GetDeclaredClasses(Handle<mirror::Class> klass) const
1018 SHARED_REQUIRES(Locks::mutator_lock_);
1019 mirror::Class* GetDeclaringClass(Handle<mirror::Class> klass) const
1020 SHARED_REQUIRES(Locks::mutator_lock_);
1021 mirror::Class* GetEnclosingClass(Handle<mirror::Class> klass) const
1022 SHARED_REQUIRES(Locks::mutator_lock_);
1023 mirror::Object* GetEnclosingMethod(Handle<mirror::Class> klass) const
1024 SHARED_REQUIRES(Locks::mutator_lock_);
1025 bool GetInnerClass(Handle<mirror::Class> klass, mirror::String** name) const
1026 SHARED_REQUIRES(Locks::mutator_lock_);
1027 bool GetInnerClassFlags(Handle<mirror::Class> klass, uint32_t* flags) const
1028 SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Hao1133db72016-04-04 19:50:14 -07001029 mirror::ObjectArray<mirror::String>* GetSignatureAnnotationForClass(Handle<mirror::Class> klass)
1030 const SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Hao13e748b2015-08-25 20:44:19 +00001031 bool IsClassAnnotationPresent(Handle<mirror::Class> klass, Handle<mirror::Class> annotation_class)
1032 const SHARED_REQUIRES(Locks::mutator_lock_);
1033
1034 mirror::Object* CreateAnnotationMember(Handle<mirror::Class> klass,
1035 Handle<mirror::Class> annotation_class,
1036 const uint8_t** annotation) const
1037 SHARED_REQUIRES(Locks::mutator_lock_);
1038 const AnnotationItem* GetAnnotationItemFromAnnotationSet(Handle<mirror::Class> klass,
1039 const AnnotationSetItem* annotation_set,
1040 uint32_t visibility,
1041 Handle<mirror::Class> annotation_class)
1042 const SHARED_REQUIRES(Locks::mutator_lock_);
1043 mirror::Object* GetAnnotationObjectFromAnnotationSet(Handle<mirror::Class> klass,
1044 const AnnotationSetItem* annotation_set,
1045 uint32_t visibility,
1046 Handle<mirror::Class> annotation_class) const
1047 SHARED_REQUIRES(Locks::mutator_lock_);
1048 mirror::Object* GetAnnotationValue(Handle<mirror::Class> klass,
1049 const AnnotationItem* annotation_item,
1050 const char* annotation_name,
1051 Handle<mirror::Class> array_class,
1052 uint32_t expected_type) const
1053 SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Hao2a5892f2015-08-31 15:00:40 -07001054 mirror::ObjectArray<mirror::String>* GetSignatureValue(Handle<mirror::Class> klass,
Jeff Hao13e748b2015-08-25 20:44:19 +00001055 const AnnotationSetItem* annotation_set)
1056 const SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Hao2a5892f2015-08-31 15:00:40 -07001057 mirror::ObjectArray<mirror::Class>* GetThrowsValue(Handle<mirror::Class> klass,
1058 const AnnotationSetItem* annotation_set) const
Jeff Hao13e748b2015-08-25 20:44:19 +00001059 SHARED_REQUIRES(Locks::mutator_lock_);
1060 mirror::ObjectArray<mirror::Object>* ProcessAnnotationSet(Handle<mirror::Class> klass,
1061 const AnnotationSetItem* annotation_set,
1062 uint32_t visibility) const
1063 SHARED_REQUIRES(Locks::mutator_lock_);
1064 mirror::ObjectArray<mirror::Object>* ProcessAnnotationSetRefList(Handle<mirror::Class> klass,
1065 const AnnotationSetRefList* set_ref_list, uint32_t size) const
1066 SHARED_REQUIRES(Locks::mutator_lock_);
1067 bool ProcessAnnotationValue(Handle<mirror::Class> klass, const uint8_t** annotation_ptr,
1068 AnnotationValue* annotation_value, Handle<mirror::Class> return_class,
1069 DexFile::AnnotationResultStyle result_style) const
1070 SHARED_REQUIRES(Locks::mutator_lock_);
1071 mirror::Object* ProcessEncodedAnnotation(Handle<mirror::Class> klass,
1072 const uint8_t** annotation) const
1073 SHARED_REQUIRES(Locks::mutator_lock_);
1074 const AnnotationItem* SearchAnnotationSet(const AnnotationSetItem* annotation_set,
1075 const char* descriptor, uint32_t visibility) const
1076 SHARED_REQUIRES(Locks::mutator_lock_);
1077 const uint8_t* SearchEncodedAnnotation(const uint8_t* annotation, const char* name) const
1078 SHARED_REQUIRES(Locks::mutator_lock_);
1079 bool SkipAnnotationValue(const uint8_t** annotation_ptr) const
1080 SHARED_REQUIRES(Locks::mutator_lock_);
1081
Shih-wei Liao195487c2011-08-20 13:29:04 -07001082 // Debug info opcodes and constants
1083 enum {
1084 DBG_END_SEQUENCE = 0x00,
1085 DBG_ADVANCE_PC = 0x01,
1086 DBG_ADVANCE_LINE = 0x02,
1087 DBG_START_LOCAL = 0x03,
1088 DBG_START_LOCAL_EXTENDED = 0x04,
1089 DBG_END_LOCAL = 0x05,
1090 DBG_RESTART_LOCAL = 0x06,
1091 DBG_SET_PROLOGUE_END = 0x07,
1092 DBG_SET_EPILOGUE_BEGIN = 0x08,
1093 DBG_SET_FILE = 0x09,
1094 DBG_FIRST_SPECIAL = 0x0a,
1095 DBG_LINE_BASE = -4,
1096 DBG_LINE_RANGE = 15,
1097 };
1098
Shih-wei Liao195487c2011-08-20 13:29:04 -07001099 struct LineNumFromPcContext {
Ian Rogersca190662012-06-26 15:45:57 -07001100 LineNumFromPcContext(uint32_t address, uint32_t line_num)
1101 : address_(address), line_num_(line_num) {}
Shih-wei Liao195487c2011-08-20 13:29:04 -07001102 uint32_t address_;
1103 uint32_t line_num_;
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -07001104 private:
1105 DISALLOW_COPY_AND_ASSIGN(LineNumFromPcContext);
Shih-wei Liao195487c2011-08-20 13:29:04 -07001106 };
1107
Shih-wei Liao195487c2011-08-20 13:29:04 -07001108 // Determine the source file line number based on the program counter.
1109 // "pc" is an offset, in 16-bit units, from the start of the method's code.
1110 //
1111 // Returns -1 if no match was found (possibly because the source files were
1112 // compiled without "-g", so no line number information is present).
1113 // Returns -2 for native methods (as expected in exception traces).
1114 //
1115 // This is used by runtime; therefore use art::Method not art::DexFile::Method.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001116 int32_t GetLineNumFromPC(ArtMethod* method, uint32_t rel_pc) const
Mathieu Chartier90443472015-07-16 20:32:27 -07001117 SHARED_REQUIRES(Locks::mutator_lock_);
Shih-wei Liao195487c2011-08-20 13:29:04 -07001118
Roland Levillain91d65e02016-01-19 15:59:16 +00001119 // Returns false if there is no debugging information or if it cannot be decoded.
David Srbeckyb06e28e2015-12-10 13:15:00 +00001120 bool DecodeDebugLocalInfo(const CodeItem* code_item, bool is_static, uint32_t method_idx,
1121 DexDebugNewLocalCb local_cb, void* context) const;
1122
Roland Levillain91d65e02016-01-19 15:59:16 +00001123 // Returns false if there is no debugging information or if it cannot be decoded.
David Srbeckyb06e28e2015-12-10 13:15:00 +00001124 bool DecodeDebugPositionInfo(const CodeItem* code_item, DexDebugNewPositionCb position_cb,
1125 void* context) const;
Shih-wei Liao195487c2011-08-20 13:29:04 -07001126
Ian Rogers0571d352011-11-03 19:51:38 -07001127 const char* GetSourceFile(const ClassDef& class_def) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001128 if (class_def.source_file_idx_ == 0xffffffff) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001129 return nullptr;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001130 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07001131 return StringDataByIdx(class_def.source_file_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001132 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001133 }
1134
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001135 int GetPermissions() const;
Ian Rogers1c849e52012-06-28 14:00:33 -07001136
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02001137 bool IsReadOnly() const;
1138
Brian Carlstrome0948e12013-08-29 09:36:15 -07001139 bool EnableWrite() const;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02001140
Brian Carlstrome0948e12013-08-29 09:36:15 -07001141 bool DisableWrite() const;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02001142
Ian Rogers13735952014-10-08 12:43:28 -07001143 const uint8_t* Begin() const {
Ian Rogers8b2c0b92013-09-19 02:56:49 -07001144 return begin_;
1145 }
1146
1147 size_t Size() const {
1148 return size_;
1149 }
1150
Andreas Gampe90e34042015-04-27 20:01:52 -07001151 // Return the name of the index-th classes.dex in a multidex zip file. This is classes.dex for
1152 // index == 0, and classes{index + 1}.dex else.
1153 static std::string GetMultiDexClassesDexName(size_t index);
1154
1155 // Return the (possibly synthetic) dex location for a multidex entry. This is dex_location for
1156 // index == 0, and dex_location + multi-dex-separator + GetMultiDexClassesDexName(index) else.
1157 static std::string GetMultiDexLocation(size_t index, const char* dex_location);
Calin Juravle4e1d5792014-07-15 23:56:47 +01001158
1159 // Returns the canonical form of the given dex location.
1160 //
1161 // There are different flavors of "dex locations" as follows:
1162 // the file name of a dex file:
1163 // The actual file path that the dex file has on disk.
1164 // dex_location:
1165 // This acts as a key for the class linker to know which dex file to load.
1166 // It may correspond to either an old odex file or a particular dex file
1167 // inside an oat file. In the first case it will also match the file name
1168 // of the dex file. In the second case (oat) it will include the file name
1169 // and possibly some multidex annotation to uniquely identify it.
1170 // canonical_dex_location:
1171 // the dex_location where it's file name part has been made canonical.
1172 static std::string GetDexCanonicalLocation(const char* dex_location);
1173
Richard Uhler07b3c232015-03-31 15:57:54 -07001174 const OatDexFile* GetOatDexFile() const {
1175 return oat_dex_file_;
Andreas Gampefd9eb392014-11-06 16:52:58 -08001176 }
1177
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001178 TypeLookupTable* GetTypeLookupTable() const {
1179 return lookup_table_.get();
1180 }
1181
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001182 void CreateTypeLookupTable(uint8_t* storage = nullptr) const;
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001183
Carl Shapiro1fb86202011-06-27 17:43:13 -07001184 private:
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001185 // Opens a .dex file
Aart Bik37d6a3b2016-06-21 18:30:10 -07001186 static std::unique_ptr<const DexFile> OpenFile(int fd,
1187 const char* location,
1188 bool verify,
1189 bool verify_checksum,
1190 std::string* error_msg);
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001191
Andreas Gampe833a4852014-05-21 18:46:59 -07001192 // Opens dex files from within a .jar, .zip, or .apk file
Aart Bik37d6a3b2016-06-21 18:30:10 -07001193 static bool OpenZip(int fd,
1194 const std::string& location,
1195 bool verify_checksum,
1196 std::string* error_msg,
Richard Uhlerfbef44d2014-12-23 09:48:51 -08001197 std::vector<std::unique_ptr<const DexFile>>* dex_files);
Andreas Gampe833a4852014-05-21 18:46:59 -07001198
1199 enum class ZipOpenErrorCode { // private
1200 kNoError,
1201 kEntryNotFound,
1202 kExtractToMemoryError,
1203 kDexFileError,
1204 kMakeReadOnlyError,
1205 kVerifyError
1206 };
1207
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001208 // 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 -07001209 // return.
Aart Bik37d6a3b2016-06-21 18:30:10 -07001210 static std::unique_ptr<const DexFile> Open(const ZipArchive& zip_archive,
1211 const char* entry_name,
1212 const std::string& location,
1213 bool verify_checksum,
1214 std::string* error_msg,
Richard Uhlerfbef44d2014-12-23 09:48:51 -08001215 ZipOpenErrorCode* error_code);
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001216
Brian Carlstrom89521892011-12-07 22:05:07 -08001217 // Opens a .dex file at the given address backed by a MemMap
Richard Uhlerfbef44d2014-12-23 09:48:51 -08001218 static std::unique_ptr<const DexFile> OpenMemory(const std::string& location,
1219 uint32_t location_checksum,
1220 MemMap* mem_map,
1221 std::string* error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -08001222
1223 // Opens a .dex file at the given address, optionally backed by a MemMap
Richard Uhlerfbef44d2014-12-23 09:48:51 -08001224 static std::unique_ptr<const DexFile> OpenMemory(const uint8_t* dex_file,
1225 size_t size,
1226 const std::string& location,
1227 uint32_t location_checksum,
1228 MemMap* mem_map,
Richard Uhler07b3c232015-03-31 15:57:54 -07001229 const OatDexFile* oat_dex_file,
Richard Uhlerfbef44d2014-12-23 09:48:51 -08001230 std::string* error_msg);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001231
Ian Rogers13735952014-10-08 12:43:28 -07001232 DexFile(const uint8_t* base, size_t size,
Brian Carlstrom28db0122012-10-18 16:20:41 -07001233 const std::string& location,
1234 uint32_t location_checksum,
Andreas Gampefd9eb392014-11-06 16:52:58 -08001235 MemMap* mem_map,
Richard Uhler07b3c232015-03-31 15:57:54 -07001236 const OatDexFile* oat_dex_file);
jeffhaof6174e82012-01-31 16:14:17 -08001237
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001238 // Top-level initializer that calls other Init methods.
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001239 bool Init(std::string* error_msg);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001240
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001241 // Returns true if the header magic and version numbers are of the expected values.
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001242 bool CheckMagicAndVersion(std::string* error_msg) const;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001243
Andreas Gampe833a4852014-05-21 18:46:59 -07001244 // Check whether a location denotes a multidex dex file. This is a very simple check: returns
1245 // whether the string contains the separator character.
1246 static bool IsMultiDexLocation(const char* location);
1247
Andreas Gampe833a4852014-05-21 18:46:59 -07001248
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001249 // The base address of the memory mapping.
Ian Rogers13735952014-10-08 12:43:28 -07001250 const uint8_t* const begin_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001251
1252 // The size of the underlying memory allocation in bytes.
Ian Rogers62d6c772013-02-27 08:32:07 -08001253 const size_t size_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001254
Elliott Hughes64bf5a32011-09-20 14:43:12 -07001255 // Typically the dex file name when available, alternatively some identifying string.
Brian Carlstroma663ea52011-08-19 23:33:41 -07001256 //
1257 // The ClassLinker will use this to match DexFiles the boot class
1258 // path to DexCache::GetLocation when loading from an image.
1259 const std::string location_;
1260
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001261 const uint32_t location_checksum_;
1262
Brian Carlstrom33f741e2011-10-03 11:24:05 -07001263 // Manages the underlying memory allocation.
Ian Rogers700a4022014-05-19 16:49:03 -07001264 std::unique_ptr<MemMap> mem_map_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001265
1266 // Points to the header section.
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001267 const Header* const header_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001268
1269 // Points to the base of the string identifier list.
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001270 const StringId* const string_ids_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001271
1272 // Points to the base of the type identifier list.
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001273 const TypeId* const type_ids_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001274
1275 // Points to the base of the field identifier list.
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001276 const FieldId* const field_ids_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001277
1278 // Points to the base of the method identifier list.
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001279 const MethodId* const method_ids_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001280
1281 // Points to the base of the prototype identifier list.
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001282 const ProtoId* const proto_ids_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001283
1284 // Points to the base of the class definition list.
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001285 const ClassDef* const class_defs_;
Ian Rogers68b56852014-08-29 20:19:11 -07001286
Richard Uhler07b3c232015-03-31 15:57:54 -07001287 // If this dex file was loaded from an oat file, oat_dex_file_ contains a
1288 // pointer to the OatDexFile it was loaded from. Otherwise oat_dex_file_ is
1289 // null.
1290 const OatDexFile* oat_dex_file_;
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001291 mutable std::unique_ptr<TypeLookupTable> lookup_table_;
Andreas Gampee6215c02015-08-31 18:54:38 -07001292
1293 friend class DexFileVerifierTest;
Mathieu Chartier76172162016-01-26 14:54:06 -08001294 ART_FRIEND_TEST(ClassLinkerTest, RegisterDexFileName); // for constructor
Carl Shapiro1fb86202011-06-27 17:43:13 -07001295};
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001296
1297struct DexFileReference {
1298 DexFileReference(const DexFile* file, uint32_t idx) : dex_file(file), index(idx) { }
1299 const DexFile* dex_file;
1300 uint32_t index;
1301};
1302
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001303std::ostream& operator<<(std::ostream& os, const DexFile& dex_file);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001304
Ian Rogers0571d352011-11-03 19:51:38 -07001305// Iterate over a dex file's ProtoId's paramters
1306class DexFileParameterIterator {
1307 public:
1308 DexFileParameterIterator(const DexFile& dex_file, const DexFile::ProtoId& proto_id)
1309 : dex_file_(dex_file), size_(0), pos_(0) {
1310 type_list_ = dex_file_.GetProtoParameters(proto_id);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001311 if (type_list_ != nullptr) {
Ian Rogers0571d352011-11-03 19:51:38 -07001312 size_ = type_list_->Size();
1313 }
1314 }
1315 bool HasNext() const { return pos_ < size_; }
David Srbeckyb06e28e2015-12-10 13:15:00 +00001316 size_t Size() const { return size_; }
Ian Rogers0571d352011-11-03 19:51:38 -07001317 void Next() { ++pos_; }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001318 uint16_t GetTypeIdx() {
Ian Rogers0571d352011-11-03 19:51:38 -07001319 return type_list_->GetTypeItem(pos_).type_idx_;
1320 }
1321 const char* GetDescriptor() {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001322 return dex_file_.StringByTypeIdx(GetTypeIdx());
Ian Rogers0571d352011-11-03 19:51:38 -07001323 }
1324 private:
1325 const DexFile& dex_file_;
1326 const DexFile::TypeList* type_list_;
1327 uint32_t size_;
1328 uint32_t pos_;
1329 DISALLOW_IMPLICIT_CONSTRUCTORS(DexFileParameterIterator);
1330};
1331
Ian Rogersd91d6d62013-09-25 20:26:14 -07001332// Abstract the signature of a method.
Ian Rogers03b6eaf2014-10-28 09:34:57 -07001333class Signature : public ValueObject {
Ian Rogersd91d6d62013-09-25 20:26:14 -07001334 public:
1335 std::string ToString() const;
1336
1337 static Signature NoSignature() {
1338 return Signature();
1339 }
1340
Ian Rogersdfb325e2013-10-30 01:00:44 -07001341 bool operator==(const Signature& rhs) const;
Ian Rogersd91d6d62013-09-25 20:26:14 -07001342 bool operator!=(const Signature& rhs) const {
1343 return !(*this == rhs);
1344 }
1345
Vladimir Markod9cffea2013-11-25 15:08:02 +00001346 bool operator==(const StringPiece& rhs) const;
Ian Rogersd91d6d62013-09-25 20:26:14 -07001347
1348 private:
1349 Signature(const DexFile* dex, const DexFile::ProtoId& proto) : dex_file_(dex), proto_id_(&proto) {
1350 }
1351
1352 Signature() : dex_file_(nullptr), proto_id_(nullptr) {
1353 }
1354
1355 friend class DexFile;
1356
1357 const DexFile* const dex_file_;
1358 const DexFile::ProtoId* const proto_id_;
1359};
1360std::ostream& operator<<(std::ostream& os, const Signature& sig);
1361
Ian Rogers0571d352011-11-03 19:51:38 -07001362// Iterate and decode class_data_item
1363class ClassDataItemIterator {
1364 public:
Ian Rogers13735952014-10-08 12:43:28 -07001365 ClassDataItemIterator(const DexFile& dex_file, const uint8_t* raw_class_data_item)
Ian Rogers0571d352011-11-03 19:51:38 -07001366 : dex_file_(dex_file), pos_(0), ptr_pos_(raw_class_data_item), last_idx_(0) {
1367 ReadClassDataHeader();
1368 if (EndOfInstanceFieldsPos() > 0) {
1369 ReadClassDataField();
1370 } else if (EndOfVirtualMethodsPos() > 0) {
1371 ReadClassDataMethod();
1372 }
1373 }
1374 uint32_t NumStaticFields() const {
1375 return header_.static_fields_size_;
1376 }
1377 uint32_t NumInstanceFields() const {
1378 return header_.instance_fields_size_;
1379 }
1380 uint32_t NumDirectMethods() const {
1381 return header_.direct_methods_size_;
1382 }
1383 uint32_t NumVirtualMethods() const {
1384 return header_.virtual_methods_size_;
1385 }
1386 bool HasNextStaticField() const {
1387 return pos_ < EndOfStaticFieldsPos();
1388 }
1389 bool HasNextInstanceField() const {
1390 return pos_ >= EndOfStaticFieldsPos() && pos_ < EndOfInstanceFieldsPos();
1391 }
1392 bool HasNextDirectMethod() const {
1393 return pos_ >= EndOfInstanceFieldsPos() && pos_ < EndOfDirectMethodsPos();
1394 }
1395 bool HasNextVirtualMethod() const {
1396 return pos_ >= EndOfDirectMethodsPos() && pos_ < EndOfVirtualMethodsPos();
1397 }
1398 bool HasNext() const {
1399 return pos_ < EndOfVirtualMethodsPos();
1400 }
Ian Rogers637c65b2013-05-31 11:46:00 -07001401 inline void Next() {
Ian Rogers0571d352011-11-03 19:51:38 -07001402 pos_++;
1403 if (pos_ < EndOfStaticFieldsPos()) {
1404 last_idx_ = GetMemberIndex();
1405 ReadClassDataField();
1406 } else if (pos_ == EndOfStaticFieldsPos() && NumInstanceFields() > 0) {
1407 last_idx_ = 0; // transition to next array, reset last index
1408 ReadClassDataField();
1409 } else if (pos_ < EndOfInstanceFieldsPos()) {
1410 last_idx_ = GetMemberIndex();
1411 ReadClassDataField();
1412 } else if (pos_ == EndOfInstanceFieldsPos() && NumDirectMethods() > 0) {
1413 last_idx_ = 0; // transition to next array, reset last index
1414 ReadClassDataMethod();
1415 } else if (pos_ < EndOfDirectMethodsPos()) {
1416 last_idx_ = GetMemberIndex();
1417 ReadClassDataMethod();
1418 } else if (pos_ == EndOfDirectMethodsPos() && NumVirtualMethods() > 0) {
1419 last_idx_ = 0; // transition to next array, reset last index
1420 ReadClassDataMethod();
1421 } else if (pos_ < EndOfVirtualMethodsPos()) {
1422 last_idx_ = GetMemberIndex();
1423 ReadClassDataMethod();
1424 } else {
1425 DCHECK(!HasNext());
1426 }
1427 }
1428 uint32_t GetMemberIndex() const {
1429 if (pos_ < EndOfInstanceFieldsPos()) {
1430 return last_idx_ + field_.field_idx_delta_;
1431 } else {
Sebastien Hertzb24bd992013-08-02 15:19:09 +02001432 DCHECK_LT(pos_, EndOfVirtualMethodsPos());
Ian Rogers0571d352011-11-03 19:51:38 -07001433 return last_idx_ + method_.method_idx_delta_;
1434 }
1435 }
Andreas Gampe51829322014-08-25 15:05:04 -07001436 uint32_t GetRawMemberAccessFlags() const {
Ian Rogers0571d352011-11-03 19:51:38 -07001437 if (pos_ < EndOfInstanceFieldsPos()) {
1438 return field_.access_flags_;
1439 } else {
Sebastien Hertzb24bd992013-08-02 15:19:09 +02001440 DCHECK_LT(pos_, EndOfVirtualMethodsPos());
Ian Rogers0571d352011-11-03 19:51:38 -07001441 return method_.access_flags_;
1442 }
1443 }
Andreas Gampe51829322014-08-25 15:05:04 -07001444 uint32_t GetFieldAccessFlags() const {
1445 return GetRawMemberAccessFlags() & kAccValidFieldFlags;
1446 }
1447 uint32_t GetMethodAccessFlags() const {
1448 return GetRawMemberAccessFlags() & kAccValidMethodFlags;
1449 }
1450 bool MemberIsNative() const {
1451 return GetRawMemberAccessFlags() & kAccNative;
1452 }
1453 bool MemberIsFinal() const {
1454 return GetRawMemberAccessFlags() & kAccFinal;
1455 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001456 InvokeType GetMethodInvokeType(const DexFile::ClassDef& class_def) const {
1457 if (HasNextDirectMethod()) {
Andreas Gampe51829322014-08-25 15:05:04 -07001458 if ((GetRawMemberAccessFlags() & kAccStatic) != 0) {
Ian Rogers08f753d2012-08-24 14:35:25 -07001459 return kStatic;
1460 } else {
1461 return kDirect;
1462 }
1463 } else {
Andreas Gampe51829322014-08-25 15:05:04 -07001464 DCHECK_EQ(GetRawMemberAccessFlags() & kAccStatic, 0U);
Ian Rogers08f753d2012-08-24 14:35:25 -07001465 if ((class_def.access_flags_ & kAccInterface) != 0) {
1466 return kInterface;
Andreas Gampe51829322014-08-25 15:05:04 -07001467 } else if ((GetRawMemberAccessFlags() & kAccConstructor) != 0) {
Ian Rogers08f753d2012-08-24 14:35:25 -07001468 return kSuper;
1469 } else {
1470 return kVirtual;
1471 }
1472 }
1473 }
Ian Rogers0571d352011-11-03 19:51:38 -07001474 const DexFile::CodeItem* GetMethodCodeItem() const {
1475 return dex_file_.GetCodeItem(method_.code_off_);
1476 }
1477 uint32_t GetMethodCodeItemOffset() const {
1478 return method_.code_off_;
1479 }
Andreas Gampee6215c02015-08-31 18:54:38 -07001480 const uint8_t* DataPointer() const {
1481 return ptr_pos_;
1482 }
Ian Rogers13735952014-10-08 12:43:28 -07001483 const uint8_t* EndDataPointer() const {
jeffhao10037c82012-01-23 15:06:23 -08001484 CHECK(!HasNext());
1485 return ptr_pos_;
1486 }
Elliott Hughesa21039c2012-06-21 12:09:25 -07001487
Ian Rogers0571d352011-11-03 19:51:38 -07001488 private:
1489 // A dex file's class_data_item is leb128 encoded, this structure holds a decoded form of the
1490 // header for a class_data_item
1491 struct ClassDataHeader {
1492 uint32_t static_fields_size_; // the number of static fields
1493 uint32_t instance_fields_size_; // the number of instance fields
1494 uint32_t direct_methods_size_; // the number of direct methods
1495 uint32_t virtual_methods_size_; // the number of virtual methods
1496 } header_;
1497
1498 // Read and decode header from a class_data_item stream into header
1499 void ReadClassDataHeader();
1500
1501 uint32_t EndOfStaticFieldsPos() const {
1502 return header_.static_fields_size_;
1503 }
1504 uint32_t EndOfInstanceFieldsPos() const {
1505 return EndOfStaticFieldsPos() + header_.instance_fields_size_;
1506 }
1507 uint32_t EndOfDirectMethodsPos() const {
1508 return EndOfInstanceFieldsPos() + header_.direct_methods_size_;
1509 }
1510 uint32_t EndOfVirtualMethodsPos() const {
1511 return EndOfDirectMethodsPos() + header_.virtual_methods_size_;
1512 }
1513
1514 // A decoded version of the field of a class_data_item
1515 struct ClassDataField {
1516 uint32_t field_idx_delta_; // delta of index into the field_ids array for FieldId
1517 uint32_t access_flags_; // access flags for the field
1518 ClassDataField() : field_idx_delta_(0), access_flags_(0) {}
Elliott Hughesa21039c2012-06-21 12:09:25 -07001519
Ian Rogers0571d352011-11-03 19:51:38 -07001520 private:
1521 DISALLOW_COPY_AND_ASSIGN(ClassDataField);
Elliott Hughesee0fa762012-03-26 17:12:41 -07001522 };
1523 ClassDataField field_;
Ian Rogers0571d352011-11-03 19:51:38 -07001524
1525 // Read and decode a field from a class_data_item stream into field
1526 void ReadClassDataField();
1527
1528 // A decoded version of the method of a class_data_item
1529 struct ClassDataMethod {
1530 uint32_t method_idx_delta_; // delta of index into the method_ids array for MethodId
1531 uint32_t access_flags_;
1532 uint32_t code_off_;
1533 ClassDataMethod() : method_idx_delta_(0), access_flags_(0), code_off_(0) {}
Elliott Hughesa21039c2012-06-21 12:09:25 -07001534
Ian Rogers0571d352011-11-03 19:51:38 -07001535 private:
1536 DISALLOW_COPY_AND_ASSIGN(ClassDataMethod);
Elliott Hughesee0fa762012-03-26 17:12:41 -07001537 };
1538 ClassDataMethod method_;
Ian Rogers0571d352011-11-03 19:51:38 -07001539
1540 // Read and decode a method from a class_data_item stream into method
1541 void ReadClassDataMethod();
1542
1543 const DexFile& dex_file_;
1544 size_t pos_; // integral number of items passed
Ian Rogers13735952014-10-08 12:43:28 -07001545 const uint8_t* ptr_pos_; // pointer into stream of class_data_item
Ian Rogers0571d352011-11-03 19:51:38 -07001546 uint32_t last_idx_; // last read field or method index to apply delta to
1547 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassDataItemIterator);
1548};
1549
Ian Rogers0571d352011-11-03 19:51:38 -07001550class EncodedStaticFieldValueIterator {
1551 public:
Shinichiro Hamaji82863f02015-11-05 16:51:33 +09001552 // A constructor for static tools. You cannot call
1553 // ReadValueToField() for an object created by this.
1554 EncodedStaticFieldValueIterator(const DexFile& dex_file,
1555 const DexFile::ClassDef& class_def);
1556
1557 // A constructor meant to be called from runtime code.
Shinichiro Hamaji50a2f8d2015-12-11 09:45:28 +09001558 EncodedStaticFieldValueIterator(const DexFile& dex_file,
1559 Handle<mirror::DexCache>* dex_cache,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001560 Handle<mirror::ClassLoader>* class_loader,
Shinichiro Hamaji50a2f8d2015-12-11 09:45:28 +09001561 ClassLinker* linker,
1562 const DexFile::ClassDef& class_def)
Mathieu Chartier90443472015-07-16 20:32:27 -07001563 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers0571d352011-11-03 19:51:38 -07001564
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001565 template<bool kTransactionActive>
Mathieu Chartier90443472015-07-16 20:32:27 -07001566 void ReadValueToField(ArtField* field) const SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers0571d352011-11-03 19:51:38 -07001567
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001568 bool HasNext() const { return pos_ < array_size_; }
Ian Rogers0571d352011-11-03 19:51:38 -07001569
1570 void Next();
Elliott Hughesa21039c2012-06-21 12:09:25 -07001571
Ian Rogers0571d352011-11-03 19:51:38 -07001572 enum ValueType {
1573 kByte = 0x00,
1574 kShort = 0x02,
1575 kChar = 0x03,
1576 kInt = 0x04,
1577 kLong = 0x06,
1578 kFloat = 0x10,
1579 kDouble = 0x11,
1580 kString = 0x17,
1581 kType = 0x18,
1582 kField = 0x19,
1583 kMethod = 0x1a,
1584 kEnum = 0x1b,
1585 kArray = 0x1c,
1586 kAnnotation = 0x1d,
1587 kNull = 0x1e,
1588 kBoolean = 0x1f
1589 };
1590
Shinichiro Hamaji82863f02015-11-05 16:51:33 +09001591 ValueType GetValueType() const { return type_; }
1592 const jvalue& GetJavaValue() const { return jval_; }
1593
Brian Carlstrom88f36542012-10-16 23:24:21 -07001594 private:
Shinichiro Hamaji50a2f8d2015-12-11 09:45:28 +09001595 EncodedStaticFieldValueIterator(const DexFile& dex_file,
1596 Handle<mirror::DexCache>* dex_cache,
1597 Handle<mirror::ClassLoader>* class_loader,
1598 ClassLinker* linker,
1599 const DexFile::ClassDef& class_def,
1600 size_t pos,
1601 ValueType type);
1602
Ian Rogers13735952014-10-08 12:43:28 -07001603 static constexpr uint8_t kEncodedValueTypeMask = 0x1f; // 0b11111
1604 static constexpr uint8_t kEncodedValueArgShift = 5;
Ian Rogers0571d352011-11-03 19:51:38 -07001605
1606 const DexFile& dex_file_;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001607 Handle<mirror::DexCache>* const dex_cache_; // Dex cache to resolve literal objects.
1608 Handle<mirror::ClassLoader>* const class_loader_; // ClassLoader to resolve types.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001609 ClassLinker* linker_; // Linker to resolve literal objects.
1610 size_t array_size_; // Size of array.
1611 size_t pos_; // Current position.
Ian Rogers13735952014-10-08 12:43:28 -07001612 const uint8_t* ptr_; // Pointer into encoded data array.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001613 ValueType type_; // Type of current encoded value.
1614 jvalue jval_; // Value of current encoded value.
Ian Rogers0571d352011-11-03 19:51:38 -07001615 DISALLOW_IMPLICIT_CONSTRUCTORS(EncodedStaticFieldValueIterator);
1616};
Brian Carlstrom88f36542012-10-16 23:24:21 -07001617std::ostream& operator<<(std::ostream& os, const EncodedStaticFieldValueIterator::ValueType& code);
Ian Rogers0571d352011-11-03 19:51:38 -07001618
1619class CatchHandlerIterator {
1620 public:
1621 CatchHandlerIterator(const DexFile::CodeItem& code_item, uint32_t address);
Logan Chien736df022012-04-27 16:25:57 +08001622
1623 CatchHandlerIterator(const DexFile::CodeItem& code_item,
1624 const DexFile::TryItem& try_item);
1625
Ian Rogers13735952014-10-08 12:43:28 -07001626 explicit CatchHandlerIterator(const uint8_t* handler_data) {
Ian Rogers0571d352011-11-03 19:51:38 -07001627 Init(handler_data);
1628 }
1629
1630 uint16_t GetHandlerTypeIndex() const {
1631 return handler_.type_idx_;
1632 }
1633 uint32_t GetHandlerAddress() const {
1634 return handler_.address_;
1635 }
1636 void Next();
1637 bool HasNext() const {
1638 return remaining_count_ != -1 || catch_all_;
1639 }
1640 // End of this set of catch blocks, convenience method to locate next set of catch blocks
Ian Rogers13735952014-10-08 12:43:28 -07001641 const uint8_t* EndDataPointer() const {
Ian Rogers0571d352011-11-03 19:51:38 -07001642 CHECK(!HasNext());
1643 return current_data_;
1644 }
Elliott Hughesa21039c2012-06-21 12:09:25 -07001645
Ian Rogers0571d352011-11-03 19:51:38 -07001646 private:
Logan Chien736df022012-04-27 16:25:57 +08001647 void Init(const DexFile::CodeItem& code_item, int32_t offset);
Ian Rogers13735952014-10-08 12:43:28 -07001648 void Init(const uint8_t* handler_data);
Ian Rogers0571d352011-11-03 19:51:38 -07001649
1650 struct CatchHandlerItem {
1651 uint16_t type_idx_; // type index of the caught exception type
1652 uint32_t address_; // handler address
1653 } handler_;
Ian Rogers13735952014-10-08 12:43:28 -07001654 const uint8_t* current_data_; // the current handler in dex file.
Ian Rogers0571d352011-11-03 19:51:38 -07001655 int32_t remaining_count_; // number of handlers not read.
1656 bool catch_all_; // is there a handler that will catch all exceptions in case
1657 // that all typed handler does not match.
1658};
1659
Carl Shapiro1fb86202011-06-27 17:43:13 -07001660} // namespace art
1661
Brian Carlstromfc0e3212013-07-17 14:40:12 -07001662#endif // ART_RUNTIME_DEX_FILE_H_