blob: 3a28422067ed9b1495794a71df85efa7dd40a54f [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:
Ian Rogers13735952014-10-08 12:43:28 -070060 static const uint8_t kDexMagic[];
Alex Lightc4961812016-03-23 10:20:41 -070061 static constexpr size_t kNumDexVersions = 2;
62 static constexpr size_t kDexVersionLen = 4;
63 static const uint8_t kDexMagicVersions[kNumDexVersions][kDexVersionLen];
64
Ian Rogers13735952014-10-08 12:43:28 -070065 static constexpr size_t kSha1DigestSize = 20;
66 static constexpr uint32_t kDexEndianConstant = 0x12345678;
Carl Shapiro80d4dde2011-06-28 16:24:07 -070067
Brian Carlstromb7bbba42011-10-13 14:58:47 -070068 // name of the DexFile entry within a zip archive
69 static const char* kClassesDex;
70
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070071 // The value of an invalid index.
72 static const uint32_t kDexNoIndex = 0xFFFFFFFF;
73
Ian Rogers0571d352011-11-03 19:51:38 -070074 // The value of an invalid index.
75 static const uint16_t kDexNoIndex16 = 0xFFFF;
Carl Shapiro1fb86202011-06-27 17:43:13 -070076
Alex Lightc4961812016-03-23 10:20:41 -070077 // The separator character in MultiDex locations.
Andreas Gampe833a4852014-05-21 18:46:59 -070078 static constexpr char kMultiDexSeparator = ':';
79
80 // A string version of the previous. This is a define so that we can merge string literals in the
81 // preprocessor.
82 #define kMultiDexSeparatorString ":"
83
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070084 // Raw header_item.
85 struct Header {
86 uint8_t magic_[8];
Brian Carlstrom7934ac22013-07-26 10:54:15 -070087 uint32_t checksum_; // See also location_checksum_
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070088 uint8_t signature_[kSha1DigestSize];
jeffhaof6174e82012-01-31 16:14:17 -080089 uint32_t file_size_; // size of entire file
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070090 uint32_t header_size_; // offset to start of next section
91 uint32_t endian_tag_;
Ian Rogers0571d352011-11-03 19:51:38 -070092 uint32_t link_size_; // unused
93 uint32_t link_off_; // unused
94 uint32_t map_off_; // unused
95 uint32_t string_ids_size_; // number of StringIds
96 uint32_t string_ids_off_; // file offset of StringIds array
97 uint32_t type_ids_size_; // number of TypeIds, we don't support more than 65535
98 uint32_t type_ids_off_; // file offset of TypeIds array
99 uint32_t proto_ids_size_; // number of ProtoIds, we don't support more than 65535
100 uint32_t proto_ids_off_; // file offset of ProtoIds array
101 uint32_t field_ids_size_; // number of FieldIds
102 uint32_t field_ids_off_; // file offset of FieldIds array
103 uint32_t method_ids_size_; // number of MethodIds
104 uint32_t method_ids_off_; // file offset of MethodIds array
105 uint32_t class_defs_size_; // number of ClassDefs
106 uint32_t class_defs_off_; // file offset of ClassDef array
107 uint32_t data_size_; // unused
108 uint32_t data_off_; // unused
Elliott Hughesa21039c2012-06-21 12:09:25 -0700109
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700110 // Decode the dex magic version
111 uint32_t GetVersion() const;
112
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700113 private:
114 DISALLOW_COPY_AND_ASSIGN(Header);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700115 };
Carl Shapiro1fb86202011-06-27 17:43:13 -0700116
jeffhao10037c82012-01-23 15:06:23 -0800117 // Map item type codes.
118 enum {
119 kDexTypeHeaderItem = 0x0000,
120 kDexTypeStringIdItem = 0x0001,
121 kDexTypeTypeIdItem = 0x0002,
122 kDexTypeProtoIdItem = 0x0003,
123 kDexTypeFieldIdItem = 0x0004,
124 kDexTypeMethodIdItem = 0x0005,
125 kDexTypeClassDefItem = 0x0006,
126 kDexTypeMapList = 0x1000,
127 kDexTypeTypeList = 0x1001,
128 kDexTypeAnnotationSetRefList = 0x1002,
129 kDexTypeAnnotationSetItem = 0x1003,
130 kDexTypeClassDataItem = 0x2000,
131 kDexTypeCodeItem = 0x2001,
132 kDexTypeStringDataItem = 0x2002,
133 kDexTypeDebugInfoItem = 0x2003,
134 kDexTypeAnnotationItem = 0x2004,
135 kDexTypeEncodedArrayItem = 0x2005,
136 kDexTypeAnnotationsDirectoryItem = 0x2006,
137 };
138
139 struct MapItem {
140 uint16_t type_;
141 uint16_t unused_;
142 uint32_t size_;
143 uint32_t offset_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700144
jeffhao10037c82012-01-23 15:06:23 -0800145 private:
146 DISALLOW_COPY_AND_ASSIGN(MapItem);
147 };
148
149 struct MapList {
150 uint32_t size_;
151 MapItem list_[1];
Elliott Hughesa21039c2012-06-21 12:09:25 -0700152
jeffhao10037c82012-01-23 15:06:23 -0800153 private:
154 DISALLOW_COPY_AND_ASSIGN(MapList);
155 };
156
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700157 // Raw string_id_item.
158 struct StringId {
159 uint32_t string_data_off_; // offset in bytes from the base address
Elliott Hughesa21039c2012-06-21 12:09:25 -0700160
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700161 private:
162 DISALLOW_COPY_AND_ASSIGN(StringId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700163 };
164
165 // Raw type_id_item.
166 struct TypeId {
167 uint32_t descriptor_idx_; // index into string_ids
Elliott Hughesa21039c2012-06-21 12:09:25 -0700168
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700169 private:
170 DISALLOW_COPY_AND_ASSIGN(TypeId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700171 };
172
173 // Raw field_id_item.
174 struct FieldId {
Ian Rogers0571d352011-11-03 19:51:38 -0700175 uint16_t class_idx_; // index into type_ids_ array for defining class
176 uint16_t type_idx_; // index into type_ids_ array for field type
177 uint32_t name_idx_; // index into string_ids_ array for field name
Elliott Hughesa21039c2012-06-21 12:09:25 -0700178
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700179 private:
180 DISALLOW_COPY_AND_ASSIGN(FieldId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700181 };
182
183 // Raw method_id_item.
184 struct MethodId {
Ian Rogers0571d352011-11-03 19:51:38 -0700185 uint16_t class_idx_; // index into type_ids_ array for defining class
186 uint16_t proto_idx_; // index into proto_ids_ array for method prototype
187 uint32_t 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
193 // Raw proto_id_item.
194 struct ProtoId {
Ian Rogers0571d352011-11-03 19:51:38 -0700195 uint32_t shorty_idx_; // index into string_ids array for shorty descriptor
196 uint16_t return_type_idx_; // index into type_ids array for return type
197 uint16_t pad_; // padding = 0
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700198 uint32_t parameters_off_; // file offset to type_list for parameter types
Elliott Hughesa21039c2012-06-21 12:09:25 -0700199
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700200 private:
201 DISALLOW_COPY_AND_ASSIGN(ProtoId);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700202 };
203
204 // Raw class_def_item.
205 struct ClassDef {
Ian Rogers0571d352011-11-03 19:51:38 -0700206 uint16_t class_idx_; // index into type_ids_ array for this class
207 uint16_t pad1_; // padding = 0
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700208 uint32_t access_flags_;
Ian Rogers0571d352011-11-03 19:51:38 -0700209 uint16_t superclass_idx_; // index into type_ids_ array for superclass
210 uint16_t pad2_; // padding = 0
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700211 uint32_t interfaces_off_; // file offset to TypeList
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700212 uint32_t source_file_idx_; // index into string_ids_ for source file name
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700213 uint32_t annotations_off_; // file offset to annotations_directory_item
214 uint32_t class_data_off_; // file offset to class_data_item
215 uint32_t static_values_off_; // file offset to EncodedArray
Elliott Hughesa21039c2012-06-21 12:09:25 -0700216
Andreas Gampe51829322014-08-25 15:05:04 -0700217 // Returns the valid access flags, that is, Java modifier bits relevant to the ClassDef type
218 // (class or interface). These are all in the lower 16b and do not contain runtime flags.
219 uint32_t GetJavaAccessFlags() const {
220 // Make sure that none of our runtime-only flags are set.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800221 static_assert((kAccValidClassFlags & kAccJavaFlagsMask) == kAccValidClassFlags,
222 "Valid class flags not a subset of Java flags");
223 static_assert((kAccValidInterfaceFlags & kAccJavaFlagsMask) == kAccValidInterfaceFlags,
224 "Valid interface flags not a subset of Java flags");
Andreas Gampe51829322014-08-25 15:05:04 -0700225
226 if ((access_flags_ & kAccInterface) != 0) {
227 // Interface.
228 return access_flags_ & kAccValidInterfaceFlags;
229 } else {
230 // Class.
231 return access_flags_ & kAccValidClassFlags;
232 }
233 }
234
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700235 private:
236 DISALLOW_COPY_AND_ASSIGN(ClassDef);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700237 };
238
239 // Raw type_item.
240 struct TypeItem {
241 uint16_t type_idx_; // index into type_ids section
Elliott Hughesa21039c2012-06-21 12:09:25 -0700242
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700243 private:
244 DISALLOW_COPY_AND_ASSIGN(TypeItem);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700245 };
246
247 // Raw type_list.
248 class TypeList {
249 public:
250 uint32_t Size() const {
251 return size_;
252 }
253
254 const TypeItem& GetTypeItem(uint32_t idx) const {
Sebastien Hertzb24bd992013-08-02 15:19:09 +0200255 DCHECK_LT(idx, this->size_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700256 return this->list_[idx];
257 }
258
Andreas Gampe31a7a0c2014-08-29 16:07:49 -0700259 // Size in bytes of the part of the list that is common.
260 static constexpr size_t GetHeaderSize() {
261 return 4U;
262 }
263
264 // Size in bytes of the whole type list including all the stored elements.
265 static constexpr size_t GetListSize(size_t count) {
266 return GetHeaderSize() + sizeof(TypeItem) * count;
267 }
268
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700269 private:
270 uint32_t size_; // size of the list, in entries
271 TypeItem list_[1]; // elements of the list
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700272 DISALLOW_COPY_AND_ASSIGN(TypeList);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700273 };
274
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700275 // Raw code_item.
276 struct CodeItem {
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700277 uint16_t registers_size_; // the number of registers used by this code
278 // (locals + parameters)
279 uint16_t ins_size_; // the number of words of incoming arguments to the method
280 // that this code is for
281 uint16_t outs_size_; // the number of words of outgoing argument space required
282 // by this code for method invocation
283 uint16_t tries_size_; // the number of try_items for this instance. If non-zero,
284 // then these appear as the tries array just after the
285 // insns in this instance.
286 uint32_t debug_info_off_; // file offset to debug info stream
Ian Rogersd81871c2011-10-03 13:57:23 -0700287 uint32_t insns_size_in_code_units_; // size of the insns array, in 2 byte code units
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700288 uint16_t insns_[1]; // actual array of bytecode.
Elliott Hughesa21039c2012-06-21 12:09:25 -0700289
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700290 private:
291 DISALLOW_COPY_AND_ASSIGN(CodeItem);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700292 };
293
Carl Shapiro2eaa9682011-08-04 19:26:11 -0700294 // Raw try_item.
295 struct TryItem {
296 uint32_t start_addr_;
297 uint16_t insn_count_;
298 uint16_t handler_off_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700299
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700300 private:
301 DISALLOW_COPY_AND_ASSIGN(TryItem);
Carl Shapiro2eaa9682011-08-04 19:26:11 -0700302 };
303
jeffhao10037c82012-01-23 15:06:23 -0800304 // Annotation constants.
305 enum {
306 kDexVisibilityBuild = 0x00, /* annotation visibility */
307 kDexVisibilityRuntime = 0x01,
308 kDexVisibilitySystem = 0x02,
309
310 kDexAnnotationByte = 0x00,
311 kDexAnnotationShort = 0x02,
312 kDexAnnotationChar = 0x03,
313 kDexAnnotationInt = 0x04,
314 kDexAnnotationLong = 0x06,
315 kDexAnnotationFloat = 0x10,
316 kDexAnnotationDouble = 0x11,
317 kDexAnnotationString = 0x17,
318 kDexAnnotationType = 0x18,
319 kDexAnnotationField = 0x19,
320 kDexAnnotationMethod = 0x1a,
321 kDexAnnotationEnum = 0x1b,
322 kDexAnnotationArray = 0x1c,
323 kDexAnnotationAnnotation = 0x1d,
324 kDexAnnotationNull = 0x1e,
325 kDexAnnotationBoolean = 0x1f,
326
327 kDexAnnotationValueTypeMask = 0x1f, /* low 5 bits */
328 kDexAnnotationValueArgShift = 5,
329 };
330
331 struct AnnotationsDirectoryItem {
332 uint32_t class_annotations_off_;
333 uint32_t fields_size_;
334 uint32_t methods_size_;
335 uint32_t parameters_size_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700336
jeffhao10037c82012-01-23 15:06:23 -0800337 private:
338 DISALLOW_COPY_AND_ASSIGN(AnnotationsDirectoryItem);
339 };
340
341 struct FieldAnnotationsItem {
342 uint32_t field_idx_;
343 uint32_t annotations_off_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700344
jeffhao10037c82012-01-23 15:06:23 -0800345 private:
346 DISALLOW_COPY_AND_ASSIGN(FieldAnnotationsItem);
347 };
348
349 struct MethodAnnotationsItem {
350 uint32_t method_idx_;
351 uint32_t annotations_off_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700352
jeffhao10037c82012-01-23 15:06:23 -0800353 private:
354 DISALLOW_COPY_AND_ASSIGN(MethodAnnotationsItem);
355 };
356
357 struct ParameterAnnotationsItem {
358 uint32_t method_idx_;
359 uint32_t annotations_off_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700360
jeffhao10037c82012-01-23 15:06:23 -0800361 private:
362 DISALLOW_COPY_AND_ASSIGN(ParameterAnnotationsItem);
363 };
364
365 struct AnnotationSetRefItem {
366 uint32_t annotations_off_;
Elliott Hughesa21039c2012-06-21 12:09:25 -0700367
jeffhao10037c82012-01-23 15:06:23 -0800368 private:
369 DISALLOW_COPY_AND_ASSIGN(AnnotationSetRefItem);
370 };
371
372 struct AnnotationSetRefList {
373 uint32_t size_;
374 AnnotationSetRefItem list_[1];
Elliott Hughesa21039c2012-06-21 12:09:25 -0700375
jeffhao10037c82012-01-23 15:06:23 -0800376 private:
377 DISALLOW_COPY_AND_ASSIGN(AnnotationSetRefList);
378 };
379
380 struct AnnotationSetItem {
381 uint32_t size_;
382 uint32_t entries_[1];
Elliott Hughesa21039c2012-06-21 12:09:25 -0700383
jeffhao10037c82012-01-23 15:06:23 -0800384 private:
385 DISALLOW_COPY_AND_ASSIGN(AnnotationSetItem);
386 };
387
388 struct AnnotationItem {
389 uint8_t visibility_;
390 uint8_t annotation_[1];
Elliott Hughesa21039c2012-06-21 12:09:25 -0700391
jeffhao10037c82012-01-23 15:06:23 -0800392 private:
393 DISALLOW_COPY_AND_ASSIGN(AnnotationItem);
394 };
395
Jeff Hao13e748b2015-08-25 20:44:19 +0000396 struct AnnotationValue {
397 JValue value_;
398 uint8_t type_;
399 };
400
401 enum AnnotationResultStyle { // private
402 kAllObjects,
403 kPrimitivesOrObjects,
404 kAllRaw
405 };
406
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800407 // Returns the checksum of a file for comparison with GetLocationChecksum().
408 // For .dex files, this is the header checksum.
409 // For zip files, this is the classes.dex zip entry CRC32 checksum.
410 // Return true if the checksum could be found, false otherwise.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700411 static bool GetChecksum(const char* filename, uint32_t* checksum, std::string* error_msg);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700412
Andreas Gampe833a4852014-05-21 18:46:59 -0700413 // Opens .dex files found in the container, guessing the container format based on file extension.
414 static bool Open(const char* filename, const char* location, std::string* error_msg,
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800415 std::vector<std::unique_ptr<const DexFile>>* dex_files);
jeffhao262bf462011-10-20 18:36:32 -0700416
Andreas Gampe0cba0042015-04-29 20:47:16 -0700417 // Checks whether the given file has the dex magic, or is a zip file with a classes.dex entry.
418 // If this function returns false, Open will not succeed. The inverse is not true, however.
419 static bool MaybeDex(const char* filename);
420
Brian Carlstrom89521892011-12-07 22:05:07 -0800421 // Opens .dex file, backed by existing memory
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800422 static std::unique_ptr<const DexFile> Open(const uint8_t* base, size_t size,
423 const std::string& location,
424 uint32_t location_checksum,
Richard Uhler07b3c232015-03-31 15:57:54 -0700425 const OatDexFile* oat_dex_file,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800426 bool verify,
427 std::string* error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -0800428
Andreas Gampe833a4852014-05-21 18:46:59 -0700429 // Open all classesXXX.dex files from a zip archive.
430 static bool OpenFromZip(const ZipArchive& zip_archive, const std::string& location,
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800431 std::string* error_msg,
432 std::vector<std::unique_ptr<const DexFile>>* dex_files);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800433
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700434 // Closes a .dex file.
Brian Carlstromf615a612011-07-23 12:50:34 -0700435 virtual ~DexFile();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700436
Brian Carlstroma663ea52011-08-19 23:33:41 -0700437 const std::string& GetLocation() const {
438 return location_;
439 }
440
Andreas Gampecb8f9e82014-07-24 15:35:50 -0700441 // For normal dex files, location and base location coincide. If a dex file is part of a multidex
442 // archive, the base location is the name of the originating jar/apk, stripped of any internal
443 // classes*.dex path.
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100444 static std::string GetBaseLocation(const char* location) {
445 const char* pos = strrchr(location, kMultiDexSeparator);
446 if (pos == nullptr) {
447 return location;
Andreas Gampecb8f9e82014-07-24 15:35:50 -0700448 } else {
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100449 return std::string(location, pos - location);
450 }
451 }
452
Richard Uhlere5fed032015-03-18 08:21:11 -0700453 static std::string GetBaseLocation(const std::string& location) {
454 return GetBaseLocation(location.c_str());
455 }
456
457 // Returns the ':classes*.dex' part of the dex location. Returns an empty
458 // string if there is no multidex suffix for the given location.
459 // The kMultiDexSeparator is included in the returned suffix.
460 static std::string GetMultiDexSuffix(const std::string& location) {
461 size_t pos = location.rfind(kMultiDexSeparator);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100462 if (pos == std::string::npos) {
Richard Uhlere5fed032015-03-18 08:21:11 -0700463 return "";
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100464 } else {
Richard Uhlere5fed032015-03-18 08:21:11 -0700465 return location.substr(pos);
Andreas Gampecb8f9e82014-07-24 15:35:50 -0700466 }
467 }
468
Richard Uhlere5fed032015-03-18 08:21:11 -0700469 std::string GetBaseLocation() const {
470 return GetBaseLocation(location_);
471 }
472
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800473 // For DexFiles directly from .dex files, this is the checksum from the DexFile::Header.
474 // For DexFiles opened from a zip files, this will be the ZipEntry CRC32 of classes.dex.
475 uint32_t GetLocationChecksum() const {
476 return location_checksum_;
477 }
478
Brian Carlstroma663ea52011-08-19 23:33:41 -0700479 const Header& GetHeader() const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700480 DCHECK(header_ != nullptr) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700481 return *header_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700482 }
483
Ian Rogers0571d352011-11-03 19:51:38 -0700484 // Decode the dex magic version
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700485 uint32_t GetVersion() const {
486 return GetHeader().GetVersion();
487 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700488
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800489 // Returns true if the byte string points to the magic value.
Ian Rogers13735952014-10-08 12:43:28 -0700490 static bool IsMagicValid(const uint8_t* magic);
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800491
492 // Returns true if the byte string after the magic is the correct value.
Ian Rogers13735952014-10-08 12:43:28 -0700493 static bool IsVersionValid(const uint8_t* magic);
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800494
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700495 // Returns the number of string identifiers in the .dex file.
496 size_t NumStringIds() const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700497 DCHECK(header_ != nullptr) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700498 return header_->string_ids_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700499 }
500
Ian Rogers0571d352011-11-03 19:51:38 -0700501 // Returns the StringId at the specified index.
502 const StringId& GetStringId(uint32_t idx) const {
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700503 DCHECK_LT(idx, NumStringIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700504 return string_ids_[idx];
505 }
506
507 uint32_t GetIndexForStringId(const StringId& string_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800508 CHECK_GE(&string_id, string_ids_) << GetLocation();
509 CHECK_LT(&string_id, string_ids_ + header_->string_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700510 return &string_id - string_ids_;
511 }
512
513 int32_t GetStringLength(const StringId& string_id) const;
514
Ian Rogersdfb325e2013-10-30 01:00:44 -0700515 // Returns a pointer to the UTF-8 string data referred to by the given string_id as well as the
516 // length of the string when decoded as a UTF-16 string. Note the UTF-16 length is not the same
517 // as the string length of the string data.
518 const char* GetStringDataAndUtf16Length(const StringId& string_id, uint32_t* utf16_length) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700519
520 const char* GetStringData(const StringId& string_id) const {
Ian Rogersdfb325e2013-10-30 01:00:44 -0700521 uint32_t ignored;
522 return GetStringDataAndUtf16Length(string_id, &ignored);
Ian Rogers0571d352011-11-03 19:51:38 -0700523 }
524
Ian Rogersdfb325e2013-10-30 01:00:44 -0700525 // Index version of GetStringDataAndUtf16Length.
526 const char* StringDataAndUtf16LengthByIdx(uint32_t idx, uint32_t* utf16_length) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700527 if (idx == kDexNoIndex) {
Ian Rogersdfb325e2013-10-30 01:00:44 -0700528 *utf16_length = 0;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700529 return nullptr;
Ian Rogers0571d352011-11-03 19:51:38 -0700530 }
531 const StringId& string_id = GetStringId(idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700532 return GetStringDataAndUtf16Length(string_id, utf16_length);
Ian Rogers0571d352011-11-03 19:51:38 -0700533 }
534
535 const char* StringDataByIdx(uint32_t idx) const {
Elliott Hughes45651fd2012-02-21 15:48:20 -0800536 uint32_t unicode_length;
Ian Rogersdfb325e2013-10-30 01:00:44 -0700537 return StringDataAndUtf16LengthByIdx(idx, &unicode_length);
Ian Rogers0571d352011-11-03 19:51:38 -0700538 }
539
Ian Rogers637c65b2013-05-31 11:46:00 -0700540 // Looks up a string id for a given modified utf8 string.
541 const StringId* FindStringId(const char* string) const;
542
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300543 const TypeId* FindTypeId(const char* string) const;
544
Ian Rogers637c65b2013-05-31 11:46:00 -0700545 // Looks up a string id for a given utf16 string.
Vladimir Markoa48aef42014-12-03 17:53:53 +0000546 const StringId* FindStringId(const uint16_t* string, size_t length) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700547
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700548 // Returns the number of type identifiers in the .dex file.
Ian Rogers68b56852014-08-29 20:19:11 -0700549 uint32_t NumTypeIds() const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700550 DCHECK(header_ != nullptr) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700551 return header_->type_ids_size_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700552 }
553
Ian Rogers0571d352011-11-03 19:51:38 -0700554 // Returns the TypeId at the specified index.
555 const TypeId& GetTypeId(uint32_t idx) const {
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700556 DCHECK_LT(idx, NumTypeIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700557 return type_ids_[idx];
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700558 }
559
Ian Rogers0571d352011-11-03 19:51:38 -0700560 uint16_t GetIndexForTypeId(const TypeId& type_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800561 CHECK_GE(&type_id, type_ids_) << GetLocation();
562 CHECK_LT(&type_id, type_ids_ + header_->type_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700563 size_t result = &type_id - type_ids_;
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800564 DCHECK_LT(result, 65536U) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700565 return static_cast<uint16_t>(result);
566 }
567
568 // Get the descriptor string associated with a given type index.
Elliott Hughes45651fd2012-02-21 15:48:20 -0800569 const char* StringByTypeIdx(uint32_t idx, uint32_t* unicode_length) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700570 const TypeId& type_id = GetTypeId(idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700571 return StringDataAndUtf16LengthByIdx(type_id.descriptor_idx_, unicode_length);
Ian Rogers0571d352011-11-03 19:51:38 -0700572 }
573
574 const char* StringByTypeIdx(uint32_t idx) const {
575 const TypeId& type_id = GetTypeId(idx);
576 return StringDataByIdx(type_id.descriptor_idx_);
577 }
578
579 // Returns the type descriptor string of a type id.
580 const char* GetTypeDescriptor(const TypeId& type_id) const {
581 return StringDataByIdx(type_id.descriptor_idx_);
582 }
583
584 // Looks up a type for the given string index
585 const TypeId* FindTypeId(uint32_t string_idx) const;
586
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700587 // Returns the number of field identifiers in the .dex file.
588 size_t NumFieldIds() const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700589 DCHECK(header_ != nullptr) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700590 return header_->field_ids_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700591 }
592
Ian Rogers0571d352011-11-03 19:51:38 -0700593 // Returns the FieldId at the specified index.
594 const FieldId& GetFieldId(uint32_t idx) const {
Sebastien Hertzb24bd992013-08-02 15:19:09 +0200595 DCHECK_LT(idx, NumFieldIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700596 return field_ids_[idx];
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700597 }
598
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800599 uint32_t GetIndexForFieldId(const FieldId& field_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800600 CHECK_GE(&field_id, field_ids_) << GetLocation();
601 CHECK_LT(&field_id, field_ids_ + header_->field_ids_size_) << GetLocation();
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800602 return &field_id - field_ids_;
603 }
604
605 // Looks up a field by its declaring class, name and type
606 const FieldId* FindFieldId(const DexFile::TypeId& declaring_klass,
607 const DexFile::StringId& name,
608 const DexFile::TypeId& type) const;
609
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700610 // Returns the declaring class descriptor string of a field id.
611 const char* GetFieldDeclaringClassDescriptor(const FieldId& field_id) const {
Brian Carlstromb9edb842011-08-28 16:31:06 -0700612 const DexFile::TypeId& type_id = GetTypeId(field_id.class_idx_);
613 return GetTypeDescriptor(type_id);
614 }
615
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700616 // Returns the class descriptor string of a field id.
617 const char* GetFieldTypeDescriptor(const FieldId& field_id) const {
618 const DexFile::TypeId& type_id = GetTypeId(field_id.type_idx_);
619 return GetTypeDescriptor(type_id);
620 }
621
Brian Carlstromb9edb842011-08-28 16:31:06 -0700622 // Returns the name of a field id.
623 const char* GetFieldName(const FieldId& field_id) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700624 return StringDataByIdx(field_id.name_idx_);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700625 }
626
Ian Rogers0571d352011-11-03 19:51:38 -0700627 // Returns the number of method identifiers in the .dex file.
628 size_t NumMethodIds() const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700629 DCHECK(header_ != nullptr) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700630 return header_->method_ids_size_;
631 }
632
633 // Returns the MethodId at the specified index.
634 const MethodId& GetMethodId(uint32_t idx) const {
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700635 DCHECK_LT(idx, NumMethodIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700636 return method_ids_[idx];
637 }
638
639 uint32_t GetIndexForMethodId(const MethodId& method_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800640 CHECK_GE(&method_id, method_ids_) << GetLocation();
641 CHECK_LT(&method_id, method_ids_ + header_->method_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700642 return &method_id - method_ids_;
643 }
644
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800645 // Looks up a method by its declaring class, name and proto_id
646 const MethodId* FindMethodId(const DexFile::TypeId& declaring_klass,
647 const DexFile::StringId& name,
Ian Rogers0571d352011-11-03 19:51:38 -0700648 const DexFile::ProtoId& signature) const;
649
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700650 // Returns the declaring class descriptor string of a method id.
651 const char* GetMethodDeclaringClassDescriptor(const MethodId& method_id) const {
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700652 const DexFile::TypeId& type_id = GetTypeId(method_id.class_idx_);
653 return GetTypeDescriptor(type_id);
654 }
655
jeffhao98eacac2011-09-14 16:11:53 -0700656 // Returns the prototype of a method id.
Brian Carlstromaded5f72011-10-07 17:15:04 -0700657 const ProtoId& GetMethodPrototype(const MethodId& method_id) const {
658 return GetProtoId(method_id.proto_idx_);
659 }
660
Ian Rogersd91d6d62013-09-25 20:26:14 -0700661 // Returns a representation of the signature of a method id.
662 const Signature GetMethodSignature(const MethodId& method_id) const;
jeffhao98eacac2011-09-14 16:11:53 -0700663
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700664 // Returns the name of a method id.
665 const char* GetMethodName(const MethodId& method_id) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700666 return StringDataByIdx(method_id.name_idx_);
Brian Carlstrom7540ff42011-09-04 16:38:46 -0700667 }
668
Calin Juravle68ad6492015-08-18 17:08:12 +0100669 // Returns the shorty of a method by its index.
670 const char* GetMethodShorty(uint32_t idx) const {
671 return StringDataByIdx(GetProtoId(GetMethodId(idx).proto_idx_).shorty_idx_);
672 }
673
Ian Rogers0571d352011-11-03 19:51:38 -0700674 // Returns the shorty of a method id.
675 const char* GetMethodShorty(const MethodId& method_id) const {
676 return StringDataByIdx(GetProtoId(method_id.proto_idx_).shorty_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700677 }
Elliott Hughes45651fd2012-02-21 15:48:20 -0800678 const char* GetMethodShorty(const MethodId& method_id, uint32_t* length) const {
Ian Rogerscf5077a2013-10-31 12:37:54 -0700679 // Using the UTF16 length is safe here as shorties are guaranteed to be ASCII characters.
Ian Rogersdfb325e2013-10-30 01:00:44 -0700680 return StringDataAndUtf16LengthByIdx(GetProtoId(method_id.proto_idx_).shorty_idx_, length);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800681 }
Ian Rogers0571d352011-11-03 19:51:38 -0700682 // Returns the number of class definitions in the .dex file.
Ian Rogers68b56852014-08-29 20:19:11 -0700683 uint32_t NumClassDefs() const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700684 DCHECK(header_ != nullptr) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700685 return header_->class_defs_size_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700686 }
687
688 // Returns the ClassDef at the specified index.
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700689 const ClassDef& GetClassDef(uint16_t idx) const {
Sebastien Hertzb24bd992013-08-02 15:19:09 +0200690 DCHECK_LT(idx, NumClassDefs()) << GetLocation();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700691 return class_defs_[idx];
692 }
693
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700694 uint16_t GetIndexForClassDef(const ClassDef& class_def) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800695 CHECK_GE(&class_def, class_defs_) << GetLocation();
696 CHECK_LT(&class_def, class_defs_ + header_->class_defs_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700697 return &class_def - class_defs_;
698 }
699
700 // Returns the class descriptor string of a class definition.
701 const char* GetClassDescriptor(const ClassDef& class_def) const {
702 return StringByTypeIdx(class_def.class_idx_);
703 }
704
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800705 // Looks up a class definition by its class descriptor. Hash must be
706 // ComputeModifiedUtf8Hash(descriptor).
707 const ClassDef* FindClassDef(const char* descriptor, size_t hash) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700708
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700709 // Looks up a class definition by its type index.
710 const ClassDef* FindClassDef(uint16_t type_idx) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700711
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700712 const TypeList* GetInterfacesList(const ClassDef& class_def) const {
713 if (class_def.interfaces_off_ == 0) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700714 return nullptr;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700715 } else {
Ian Rogers13735952014-10-08 12:43:28 -0700716 const uint8_t* addr = begin_ + class_def.interfaces_off_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700717 return reinterpret_cast<const TypeList*>(addr);
718 }
719 }
720
Ian Rogers0571d352011-11-03 19:51:38 -0700721 // Returns a pointer to the raw memory mapped class_data_item
Ian Rogers13735952014-10-08 12:43:28 -0700722 const uint8_t* GetClassData(const ClassDef& class_def) const {
Ian Rogers0571d352011-11-03 19:51:38 -0700723 if (class_def.class_data_off_ == 0) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700724 return nullptr;
Ian Rogers0571d352011-11-03 19:51:38 -0700725 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800726 return begin_ + class_def.class_data_off_;
Ian Rogers0571d352011-11-03 19:51:38 -0700727 }
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700728 }
729
Ian Rogers0571d352011-11-03 19:51:38 -0700730 //
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800731 const CodeItem* GetCodeItem(const uint32_t code_off) const {
Alex Light9139e002015-10-09 15:59:48 -0700732 DCHECK_LT(code_off, size_) << "Code item offset larger then maximum allowed offset";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800733 if (code_off == 0) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700734 return nullptr; // native or abstract method
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700735 } else {
Ian Rogers13735952014-10-08 12:43:28 -0700736 const uint8_t* addr = begin_ + code_off;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700737 return reinterpret_cast<const CodeItem*>(addr);
738 }
739 }
740
Ian Rogers0571d352011-11-03 19:51:38 -0700741 const char* GetReturnTypeDescriptor(const ProtoId& proto_id) const {
742 return StringByTypeIdx(proto_id.return_type_idx_);
743 }
744
745 // Returns the number of prototype identifiers in the .dex file.
746 size_t NumProtoIds() const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700747 DCHECK(header_ != nullptr) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700748 return header_->proto_ids_size_;
749 }
750
751 // Returns the ProtoId at the specified index.
752 const ProtoId& GetProtoId(uint32_t idx) const {
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700753 DCHECK_LT(idx, NumProtoIds()) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700754 return proto_ids_[idx];
755 }
756
757 uint16_t GetIndexForProtoId(const ProtoId& proto_id) const {
Brian Carlstrom61e513c2011-12-09 15:30:06 -0800758 CHECK_GE(&proto_id, proto_ids_) << GetLocation();
759 CHECK_LT(&proto_id, proto_ids_ + header_->proto_ids_size_) << GetLocation();
Ian Rogers0571d352011-11-03 19:51:38 -0700760 return &proto_id - proto_ids_;
761 }
762
763 // Looks up a proto id for a given return type and signature type list
Ian Rogersd91d6d62013-09-25 20:26:14 -0700764 const ProtoId* FindProtoId(uint16_t return_type_idx,
Vladimir Marko5c96e6b2013-11-14 15:34:17 +0000765 const uint16_t* signature_type_idxs, uint32_t signature_length) const;
766 const ProtoId* FindProtoId(uint16_t return_type_idx,
767 const std::vector<uint16_t>& signature_type_idxs) const {
768 return FindProtoId(return_type_idx, &signature_type_idxs[0], signature_type_idxs.size());
769 }
Ian Rogers0571d352011-11-03 19:51:38 -0700770
771 // Given a signature place the type ids into the given vector, returns true on success
Ian Rogersd91d6d62013-09-25 20:26:14 -0700772 bool CreateTypeList(const StringPiece& signature, uint16_t* return_type_idx,
773 std::vector<uint16_t>* param_type_idxs) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700774
Ian Rogersd91d6d62013-09-25 20:26:14 -0700775 // Create a Signature from the given string signature or return Signature::NoSignature if not
776 // possible.
777 const Signature CreateSignature(const StringPiece& signature) const;
Ian Rogers0571d352011-11-03 19:51:38 -0700778
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700779 // Returns the short form method descriptor for the given prototype.
780 const char* GetShorty(uint32_t proto_idx) const {
781 const ProtoId& proto_id = GetProtoId(proto_idx);
Ian Rogers0571d352011-11-03 19:51:38 -0700782 return StringDataByIdx(proto_id.shorty_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700783 }
784
785 const TypeList* GetProtoParameters(const ProtoId& proto_id) const {
786 if (proto_id.parameters_off_ == 0) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700787 return nullptr;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700788 } else {
Ian Rogers13735952014-10-08 12:43:28 -0700789 const uint8_t* addr = begin_ + proto_id.parameters_off_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700790 return reinterpret_cast<const TypeList*>(addr);
791 }
792 }
793
Ian Rogers13735952014-10-08 12:43:28 -0700794 const uint8_t* GetEncodedStaticFieldValuesArray(const ClassDef& class_def) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700795 if (class_def.static_values_off_ == 0) {
796 return 0;
797 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800798 return begin_ + class_def.static_values_off_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700799 }
800 }
801
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800802 static const TryItem* GetTryItems(const CodeItem& code_item, uint32_t offset);
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700803
804 // Get the base of the encoded data for the given DexCode.
Ian Rogers13735952014-10-08 12:43:28 -0700805 static const uint8_t* GetCatchHandlerData(const CodeItem& code_item, uint32_t offset) {
806 const uint8_t* handler_data =
807 reinterpret_cast<const uint8_t*>(GetTryItems(code_item, code_item.tries_size_));
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700808 return handler_data + offset;
809 }
810
Ian Rogersdbbc99d2013-04-18 16:51:54 -0700811 // Find which try region is associated with the given address (ie dex pc). Returns -1 if none.
812 static int32_t FindTryItem(const CodeItem &code_item, uint32_t address);
813
814 // Find the handler offset associated with the given address (ie dex pc). Returns -1 if none.
815 static int32_t FindCatchHandlerOffset(const CodeItem &code_item, uint32_t address);
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700816
Shih-wei Liao195487c2011-08-20 13:29:04 -0700817 // Get the pointer to the start of the debugging data
Ian Rogers13735952014-10-08 12:43:28 -0700818 const uint8_t* GetDebugInfoStream(const CodeItem* code_item) const {
David Srbecky68529422015-07-07 19:13:29 +0100819 // Check that the offset is in bounds.
820 // Note that although the specification says that 0 should be used if there
821 // is no debug information, some applications incorrectly use 0xFFFFFFFF.
822 if (code_item->debug_info_off_ == 0 || code_item->debug_info_off_ >= size_) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700823 return nullptr;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700824 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800825 return begin_ + code_item->debug_info_off_;
Shih-wei Liao195487c2011-08-20 13:29:04 -0700826 }
827 }
828
David Srbeckyb06e28e2015-12-10 13:15:00 +0000829 struct PositionInfo {
830 PositionInfo()
831 : address_(0),
832 line_(0),
833 source_file_(nullptr),
834 prologue_end_(false),
835 epilogue_begin_(false) {
836 }
837
838 uint32_t address_; // In 16-bit code units.
839 uint32_t line_; // Source code line number starting at 1.
840 const char* source_file_; // nullptr if the file from ClassDef still applies.
841 bool prologue_end_;
842 bool epilogue_begin_;
843 };
844
Shih-wei Liao195487c2011-08-20 13:29:04 -0700845 // Callback for "new position table entry".
846 // Returning true causes the decoder to stop early.
David Srbeckyb06e28e2015-12-10 13:15:00 +0000847 typedef bool (*DexDebugNewPositionCb)(void* context, const PositionInfo& entry);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700848
David Srbeckyb06e28e2015-12-10 13:15:00 +0000849 struct LocalInfo {
850 LocalInfo()
851 : name_(nullptr),
852 descriptor_(nullptr),
853 signature_(nullptr),
854 start_address_(0),
855 end_address_(0),
856 reg_(0),
857 is_live_(false) {
858 }
Shih-wei Liao195487c2011-08-20 13:29:04 -0700859
David Srbeckyb06e28e2015-12-10 13:15:00 +0000860 const char* name_; // E.g., list. It can be nullptr if unknown.
861 const char* descriptor_; // E.g., Ljava/util/LinkedList;
862 const char* signature_; // E.g., java.util.LinkedList<java.lang.Integer>
863 uint32_t start_address_; // PC location where the local is first defined.
864 uint32_t end_address_; // PC location where the local is no longer defined.
865 uint16_t reg_; // Dex register which stores the values.
866 bool is_live_; // Is the local defined and live.
867 };
868
869 // Callback for "new locals table entry".
870 typedef void (*DexDebugNewLocalCb)(void* context, const LocalInfo& entry);
871
872 static bool LineNumForPcCb(void* context, const PositionInfo& entry);
Shih-wei Liao195487c2011-08-20 13:29:04 -0700873
Jeff Hao13e748b2015-08-25 20:44:19 +0000874 const AnnotationsDirectoryItem* GetAnnotationsDirectory(const ClassDef& class_def) const {
875 if (class_def.annotations_off_ == 0) {
876 return nullptr;
877 } else {
878 return reinterpret_cast<const AnnotationsDirectoryItem*>(begin_ + class_def.annotations_off_);
879 }
880 }
881
882 const AnnotationSetItem* GetClassAnnotationSet(const AnnotationsDirectoryItem* anno_dir) const {
883 if (anno_dir->class_annotations_off_ == 0) {
884 return nullptr;
885 } else {
886 return reinterpret_cast<const AnnotationSetItem*>(begin_ + anno_dir->class_annotations_off_);
887 }
888 }
889
890 const FieldAnnotationsItem* GetFieldAnnotations(const AnnotationsDirectoryItem* anno_dir) const {
891 if (anno_dir->fields_size_ == 0) {
892 return nullptr;
893 } else {
894 return reinterpret_cast<const FieldAnnotationsItem*>(&anno_dir[1]);
895 }
896 }
897
898 const MethodAnnotationsItem* GetMethodAnnotations(const AnnotationsDirectoryItem* anno_dir)
899 const {
900 if (anno_dir->methods_size_ == 0) {
901 return nullptr;
902 } else {
903 // Skip past the header and field annotations.
904 const uint8_t* addr = reinterpret_cast<const uint8_t*>(&anno_dir[1]);
905 addr += anno_dir->fields_size_ * sizeof(FieldAnnotationsItem);
906 return reinterpret_cast<const MethodAnnotationsItem*>(addr);
907 }
908 }
909
910 const ParameterAnnotationsItem* GetParameterAnnotations(const AnnotationsDirectoryItem* anno_dir)
911 const {
912 if (anno_dir->parameters_size_ == 0) {
913 return nullptr;
914 } else {
915 // Skip past the header, field annotations, and method annotations.
916 const uint8_t* addr = reinterpret_cast<const uint8_t*>(&anno_dir[1]);
917 addr += anno_dir->fields_size_ * sizeof(FieldAnnotationsItem);
918 addr += anno_dir->methods_size_ * sizeof(MethodAnnotationsItem);
919 return reinterpret_cast<const ParameterAnnotationsItem*>(addr);
920 }
921 }
922
923 const AnnotationSetItem* GetFieldAnnotationSetItem(const FieldAnnotationsItem& anno_item) const {
924 uint32_t offset = anno_item.annotations_off_;
925 if (offset == 0) {
926 return nullptr;
927 } else {
928 return reinterpret_cast<const AnnotationSetItem*>(begin_ + offset);
929 }
930 }
931
932 const AnnotationSetItem* GetMethodAnnotationSetItem(const MethodAnnotationsItem& anno_item)
933 const {
934 uint32_t offset = anno_item.annotations_off_;
935 if (offset == 0) {
936 return nullptr;
937 } else {
938 return reinterpret_cast<const AnnotationSetItem*>(begin_ + offset);
939 }
940 }
941
942 const AnnotationSetRefList* GetParameterAnnotationSetRefList(
943 const ParameterAnnotationsItem* anno_item) const {
944 uint32_t offset = anno_item->annotations_off_;
945 if (offset == 0) {
946 return nullptr;
947 }
948 return reinterpret_cast<const AnnotationSetRefList*>(begin_ + offset);
949 }
950
951 const AnnotationItem* GetAnnotationItem(const AnnotationSetItem* set_item, uint32_t index) const {
952 DCHECK_LE(index, set_item->size_);
953 uint32_t offset = set_item->entries_[index];
954 if (offset == 0) {
955 return nullptr;
956 } else {
957 return reinterpret_cast<const AnnotationItem*>(begin_ + offset);
958 }
959 }
960
961 const AnnotationSetItem* GetSetRefItemItem(const AnnotationSetRefItem* anno_item) const {
962 uint32_t offset = anno_item->annotations_off_;
963 if (offset == 0) {
964 return nullptr;
965 }
966 return reinterpret_cast<const AnnotationSetItem*>(begin_ + offset);
967 }
968
969 const AnnotationSetItem* FindAnnotationSetForField(ArtField* field) const
970 SHARED_REQUIRES(Locks::mutator_lock_);
971 mirror::Object* GetAnnotationForField(ArtField* field, Handle<mirror::Class> annotation_class)
972 const SHARED_REQUIRES(Locks::mutator_lock_);
973 mirror::ObjectArray<mirror::Object>* GetAnnotationsForField(ArtField* field) const
974 SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Hao2a5892f2015-08-31 15:00:40 -0700975 mirror::ObjectArray<mirror::String>* GetSignatureAnnotationForField(ArtField* field) const
Jeff Hao13e748b2015-08-25 20:44:19 +0000976 SHARED_REQUIRES(Locks::mutator_lock_);
977 bool IsFieldAnnotationPresent(ArtField* field, Handle<mirror::Class> annotation_class) const
978 SHARED_REQUIRES(Locks::mutator_lock_);
979
980 const AnnotationSetItem* FindAnnotationSetForMethod(ArtMethod* method) const
981 SHARED_REQUIRES(Locks::mutator_lock_);
982 const ParameterAnnotationsItem* FindAnnotationsItemForMethod(ArtMethod* method) const
983 SHARED_REQUIRES(Locks::mutator_lock_);
984 mirror::Object* GetAnnotationDefaultValue(ArtMethod* method) const
985 SHARED_REQUIRES(Locks::mutator_lock_);
986 mirror::Object* GetAnnotationForMethod(ArtMethod* method, Handle<mirror::Class> annotation_class)
987 const SHARED_REQUIRES(Locks::mutator_lock_);
988 mirror::ObjectArray<mirror::Object>* GetAnnotationsForMethod(ArtMethod* method) const
989 SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Hao2a5892f2015-08-31 15:00:40 -0700990 mirror::ObjectArray<mirror::Class>* GetExceptionTypesForMethod(ArtMethod* method) const
Jeff Hao13e748b2015-08-25 20:44:19 +0000991 SHARED_REQUIRES(Locks::mutator_lock_);
992 mirror::ObjectArray<mirror::Object>* GetParameterAnnotations(ArtMethod* method) const
993 SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Hao1133db72016-04-04 19:50:14 -0700994 mirror::ObjectArray<mirror::String>* GetSignatureAnnotationForMethod(ArtMethod* method) const
995 SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Hao13e748b2015-08-25 20:44:19 +0000996 bool IsMethodAnnotationPresent(ArtMethod* method, Handle<mirror::Class> annotation_class) const
997 SHARED_REQUIRES(Locks::mutator_lock_);
998
999 const AnnotationSetItem* FindAnnotationSetForClass(Handle<mirror::Class> klass) const
1000 SHARED_REQUIRES(Locks::mutator_lock_);
1001 mirror::Object* GetAnnotationForClass(Handle<mirror::Class> klass,
1002 Handle<mirror::Class> annotation_class) const
1003 SHARED_REQUIRES(Locks::mutator_lock_);
1004 mirror::ObjectArray<mirror::Object>* GetAnnotationsForClass(Handle<mirror::Class> klass) const
1005 SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Hao2a5892f2015-08-31 15:00:40 -07001006 mirror::ObjectArray<mirror::Class>* GetDeclaredClasses(Handle<mirror::Class> klass) const
1007 SHARED_REQUIRES(Locks::mutator_lock_);
1008 mirror::Class* GetDeclaringClass(Handle<mirror::Class> klass) const
1009 SHARED_REQUIRES(Locks::mutator_lock_);
1010 mirror::Class* GetEnclosingClass(Handle<mirror::Class> klass) const
1011 SHARED_REQUIRES(Locks::mutator_lock_);
1012 mirror::Object* GetEnclosingMethod(Handle<mirror::Class> klass) const
1013 SHARED_REQUIRES(Locks::mutator_lock_);
1014 bool GetInnerClass(Handle<mirror::Class> klass, mirror::String** name) const
1015 SHARED_REQUIRES(Locks::mutator_lock_);
1016 bool GetInnerClassFlags(Handle<mirror::Class> klass, uint32_t* flags) const
1017 SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Hao1133db72016-04-04 19:50:14 -07001018 mirror::ObjectArray<mirror::String>* GetSignatureAnnotationForClass(Handle<mirror::Class> klass)
1019 const SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Hao13e748b2015-08-25 20:44:19 +00001020 bool IsClassAnnotationPresent(Handle<mirror::Class> klass, Handle<mirror::Class> annotation_class)
1021 const SHARED_REQUIRES(Locks::mutator_lock_);
1022
1023 mirror::Object* CreateAnnotationMember(Handle<mirror::Class> klass,
1024 Handle<mirror::Class> annotation_class,
1025 const uint8_t** annotation) const
1026 SHARED_REQUIRES(Locks::mutator_lock_);
1027 const AnnotationItem* GetAnnotationItemFromAnnotationSet(Handle<mirror::Class> klass,
1028 const AnnotationSetItem* annotation_set,
1029 uint32_t visibility,
1030 Handle<mirror::Class> annotation_class)
1031 const SHARED_REQUIRES(Locks::mutator_lock_);
1032 mirror::Object* GetAnnotationObjectFromAnnotationSet(Handle<mirror::Class> klass,
1033 const AnnotationSetItem* annotation_set,
1034 uint32_t visibility,
1035 Handle<mirror::Class> annotation_class) const
1036 SHARED_REQUIRES(Locks::mutator_lock_);
1037 mirror::Object* GetAnnotationValue(Handle<mirror::Class> klass,
1038 const AnnotationItem* annotation_item,
1039 const char* annotation_name,
1040 Handle<mirror::Class> array_class,
1041 uint32_t expected_type) const
1042 SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Hao2a5892f2015-08-31 15:00:40 -07001043 mirror::ObjectArray<mirror::String>* GetSignatureValue(Handle<mirror::Class> klass,
Jeff Hao13e748b2015-08-25 20:44:19 +00001044 const AnnotationSetItem* annotation_set)
1045 const SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Hao2a5892f2015-08-31 15:00:40 -07001046 mirror::ObjectArray<mirror::Class>* GetThrowsValue(Handle<mirror::Class> klass,
1047 const AnnotationSetItem* annotation_set) const
Jeff Hao13e748b2015-08-25 20:44:19 +00001048 SHARED_REQUIRES(Locks::mutator_lock_);
1049 mirror::ObjectArray<mirror::Object>* ProcessAnnotationSet(Handle<mirror::Class> klass,
1050 const AnnotationSetItem* annotation_set,
1051 uint32_t visibility) const
1052 SHARED_REQUIRES(Locks::mutator_lock_);
1053 mirror::ObjectArray<mirror::Object>* ProcessAnnotationSetRefList(Handle<mirror::Class> klass,
1054 const AnnotationSetRefList* set_ref_list, uint32_t size) const
1055 SHARED_REQUIRES(Locks::mutator_lock_);
1056 bool ProcessAnnotationValue(Handle<mirror::Class> klass, const uint8_t** annotation_ptr,
1057 AnnotationValue* annotation_value, Handle<mirror::Class> return_class,
1058 DexFile::AnnotationResultStyle result_style) const
1059 SHARED_REQUIRES(Locks::mutator_lock_);
1060 mirror::Object* ProcessEncodedAnnotation(Handle<mirror::Class> klass,
1061 const uint8_t** annotation) const
1062 SHARED_REQUIRES(Locks::mutator_lock_);
1063 const AnnotationItem* SearchAnnotationSet(const AnnotationSetItem* annotation_set,
1064 const char* descriptor, uint32_t visibility) const
1065 SHARED_REQUIRES(Locks::mutator_lock_);
1066 const uint8_t* SearchEncodedAnnotation(const uint8_t* annotation, const char* name) const
1067 SHARED_REQUIRES(Locks::mutator_lock_);
1068 bool SkipAnnotationValue(const uint8_t** annotation_ptr) const
1069 SHARED_REQUIRES(Locks::mutator_lock_);
1070
Shih-wei Liao195487c2011-08-20 13:29:04 -07001071 // Debug info opcodes and constants
1072 enum {
1073 DBG_END_SEQUENCE = 0x00,
1074 DBG_ADVANCE_PC = 0x01,
1075 DBG_ADVANCE_LINE = 0x02,
1076 DBG_START_LOCAL = 0x03,
1077 DBG_START_LOCAL_EXTENDED = 0x04,
1078 DBG_END_LOCAL = 0x05,
1079 DBG_RESTART_LOCAL = 0x06,
1080 DBG_SET_PROLOGUE_END = 0x07,
1081 DBG_SET_EPILOGUE_BEGIN = 0x08,
1082 DBG_SET_FILE = 0x09,
1083 DBG_FIRST_SPECIAL = 0x0a,
1084 DBG_LINE_BASE = -4,
1085 DBG_LINE_RANGE = 15,
1086 };
1087
Shih-wei Liao195487c2011-08-20 13:29:04 -07001088 struct LineNumFromPcContext {
Ian Rogersca190662012-06-26 15:45:57 -07001089 LineNumFromPcContext(uint32_t address, uint32_t line_num)
1090 : address_(address), line_num_(line_num) {}
Shih-wei Liao195487c2011-08-20 13:29:04 -07001091 uint32_t address_;
1092 uint32_t line_num_;
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -07001093 private:
1094 DISALLOW_COPY_AND_ASSIGN(LineNumFromPcContext);
Shih-wei Liao195487c2011-08-20 13:29:04 -07001095 };
1096
Shih-wei Liao195487c2011-08-20 13:29:04 -07001097 // Determine the source file line number based on the program counter.
1098 // "pc" is an offset, in 16-bit units, from the start of the method's code.
1099 //
1100 // Returns -1 if no match was found (possibly because the source files were
1101 // compiled without "-g", so no line number information is present).
1102 // Returns -2 for native methods (as expected in exception traces).
1103 //
1104 // This is used by runtime; therefore use art::Method not art::DexFile::Method.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001105 int32_t GetLineNumFromPC(ArtMethod* method, uint32_t rel_pc) const
Mathieu Chartier90443472015-07-16 20:32:27 -07001106 SHARED_REQUIRES(Locks::mutator_lock_);
Shih-wei Liao195487c2011-08-20 13:29:04 -07001107
Roland Levillain91d65e02016-01-19 15:59:16 +00001108 // Returns false if there is no debugging information or if it cannot be decoded.
David Srbeckyb06e28e2015-12-10 13:15:00 +00001109 bool DecodeDebugLocalInfo(const CodeItem* code_item, bool is_static, uint32_t method_idx,
1110 DexDebugNewLocalCb local_cb, void* context) const;
1111
Roland Levillain91d65e02016-01-19 15:59:16 +00001112 // Returns false if there is no debugging information or if it cannot be decoded.
David Srbeckyb06e28e2015-12-10 13:15:00 +00001113 bool DecodeDebugPositionInfo(const CodeItem* code_item, DexDebugNewPositionCb position_cb,
1114 void* context) const;
Shih-wei Liao195487c2011-08-20 13:29:04 -07001115
Ian Rogers0571d352011-11-03 19:51:38 -07001116 const char* GetSourceFile(const ClassDef& class_def) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001117 if (class_def.source_file_idx_ == 0xffffffff) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001118 return nullptr;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001119 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07001120 return StringDataByIdx(class_def.source_file_idx_);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001121 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001122 }
1123
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001124 int GetPermissions() const;
Ian Rogers1c849e52012-06-28 14:00:33 -07001125
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02001126 bool IsReadOnly() const;
1127
Brian Carlstrome0948e12013-08-29 09:36:15 -07001128 bool EnableWrite() const;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02001129
Brian Carlstrome0948e12013-08-29 09:36:15 -07001130 bool DisableWrite() const;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02001131
Ian Rogers13735952014-10-08 12:43:28 -07001132 const uint8_t* Begin() const {
Ian Rogers8b2c0b92013-09-19 02:56:49 -07001133 return begin_;
1134 }
1135
1136 size_t Size() const {
1137 return size_;
1138 }
1139
Andreas Gampe90e34042015-04-27 20:01:52 -07001140 // Return the name of the index-th classes.dex in a multidex zip file. This is classes.dex for
1141 // index == 0, and classes{index + 1}.dex else.
1142 static std::string GetMultiDexClassesDexName(size_t index);
1143
1144 // Return the (possibly synthetic) dex location for a multidex entry. This is dex_location for
1145 // index == 0, and dex_location + multi-dex-separator + GetMultiDexClassesDexName(index) else.
1146 static std::string GetMultiDexLocation(size_t index, const char* dex_location);
Calin Juravle4e1d5792014-07-15 23:56:47 +01001147
1148 // Returns the canonical form of the given dex location.
1149 //
1150 // There are different flavors of "dex locations" as follows:
1151 // the file name of a dex file:
1152 // The actual file path that the dex file has on disk.
1153 // dex_location:
1154 // This acts as a key for the class linker to know which dex file to load.
1155 // It may correspond to either an old odex file or a particular dex file
1156 // inside an oat file. In the first case it will also match the file name
1157 // of the dex file. In the second case (oat) it will include the file name
1158 // and possibly some multidex annotation to uniquely identify it.
1159 // canonical_dex_location:
1160 // the dex_location where it's file name part has been made canonical.
1161 static std::string GetDexCanonicalLocation(const char* dex_location);
1162
Richard Uhler07b3c232015-03-31 15:57:54 -07001163 const OatDexFile* GetOatDexFile() const {
1164 return oat_dex_file_;
Andreas Gampefd9eb392014-11-06 16:52:58 -08001165 }
1166
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001167 TypeLookupTable* GetTypeLookupTable() const {
1168 return lookup_table_.get();
1169 }
1170
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001171 void CreateTypeLookupTable(uint8_t* storage = nullptr) const;
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001172
Carl Shapiro1fb86202011-06-27 17:43:13 -07001173 private:
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001174 // Opens a .dex file
Richard Uhlerfbef44d2014-12-23 09:48:51 -08001175 static std::unique_ptr<const DexFile> OpenFile(int fd, const char* location,
1176 bool verify, std::string* error_msg);
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001177
Andreas Gampe833a4852014-05-21 18:46:59 -07001178 // Opens dex files from within a .jar, .zip, or .apk file
1179 static bool OpenZip(int fd, const std::string& location, std::string* error_msg,
Richard Uhlerfbef44d2014-12-23 09:48:51 -08001180 std::vector<std::unique_ptr<const DexFile>>* dex_files);
Andreas Gampe833a4852014-05-21 18:46:59 -07001181
1182 enum class ZipOpenErrorCode { // private
1183 kNoError,
1184 kEntryNotFound,
1185 kExtractToMemoryError,
1186 kDexFileError,
1187 kMakeReadOnlyError,
1188 kVerifyError
1189 };
1190
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001191 // 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 -07001192 // return.
Richard Uhlerfbef44d2014-12-23 09:48:51 -08001193 static std::unique_ptr<const DexFile> Open(const ZipArchive& zip_archive, const char* entry_name,
1194 const std::string& location, std::string* error_msg,
1195 ZipOpenErrorCode* error_code);
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001196
Brian Carlstrom89521892011-12-07 22:05:07 -08001197 // Opens a .dex file at the given address backed by a MemMap
Richard Uhlerfbef44d2014-12-23 09:48:51 -08001198 static std::unique_ptr<const DexFile> OpenMemory(const std::string& location,
1199 uint32_t location_checksum,
1200 MemMap* mem_map,
1201 std::string* error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -08001202
1203 // Opens a .dex file at the given address, optionally backed by a MemMap
Richard Uhlerfbef44d2014-12-23 09:48:51 -08001204 static std::unique_ptr<const DexFile> OpenMemory(const uint8_t* dex_file,
1205 size_t size,
1206 const std::string& location,
1207 uint32_t location_checksum,
1208 MemMap* mem_map,
Richard Uhler07b3c232015-03-31 15:57:54 -07001209 const OatDexFile* oat_dex_file,
Richard Uhlerfbef44d2014-12-23 09:48:51 -08001210 std::string* error_msg);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001211
Ian Rogers13735952014-10-08 12:43:28 -07001212 DexFile(const uint8_t* base, size_t size,
Brian Carlstrom28db0122012-10-18 16:20:41 -07001213 const std::string& location,
1214 uint32_t location_checksum,
Andreas Gampefd9eb392014-11-06 16:52:58 -08001215 MemMap* mem_map,
Richard Uhler07b3c232015-03-31 15:57:54 -07001216 const OatDexFile* oat_dex_file);
jeffhaof6174e82012-01-31 16:14:17 -08001217
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001218 // Top-level initializer that calls other Init methods.
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001219 bool Init(std::string* error_msg);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001220
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -08001221 // Returns true if the header magic and version numbers are of the expected values.
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001222 bool CheckMagicAndVersion(std::string* error_msg) const;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001223
Andreas Gampe833a4852014-05-21 18:46:59 -07001224 // Check whether a location denotes a multidex dex file. This is a very simple check: returns
1225 // whether the string contains the separator character.
1226 static bool IsMultiDexLocation(const char* location);
1227
Andreas Gampe833a4852014-05-21 18:46:59 -07001228
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001229 // The base address of the memory mapping.
Ian Rogers13735952014-10-08 12:43:28 -07001230 const uint8_t* const begin_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001231
1232 // The size of the underlying memory allocation in bytes.
Ian Rogers62d6c772013-02-27 08:32:07 -08001233 const size_t size_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001234
Elliott Hughes64bf5a32011-09-20 14:43:12 -07001235 // Typically the dex file name when available, alternatively some identifying string.
Brian Carlstroma663ea52011-08-19 23:33:41 -07001236 //
1237 // The ClassLinker will use this to match DexFiles the boot class
1238 // path to DexCache::GetLocation when loading from an image.
1239 const std::string location_;
1240
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001241 const uint32_t location_checksum_;
1242
Brian Carlstrom33f741e2011-10-03 11:24:05 -07001243 // Manages the underlying memory allocation.
Ian Rogers700a4022014-05-19 16:49:03 -07001244 std::unique_ptr<MemMap> mem_map_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001245
1246 // Points to the header section.
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001247 const Header* const header_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001248
1249 // Points to the base of the string identifier list.
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001250 const StringId* const string_ids_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001251
1252 // Points to the base of the type identifier list.
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001253 const TypeId* const type_ids_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001254
1255 // Points to the base of the field identifier list.
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001256 const FieldId* const field_ids_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001257
1258 // Points to the base of the method identifier list.
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001259 const MethodId* const method_ids_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001260
1261 // Points to the base of the prototype identifier list.
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001262 const ProtoId* const proto_ids_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001263
1264 // Points to the base of the class definition list.
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001265 const ClassDef* const class_defs_;
Ian Rogers68b56852014-08-29 20:19:11 -07001266
Richard Uhler07b3c232015-03-31 15:57:54 -07001267 // If this dex file was loaded from an oat file, oat_dex_file_ contains a
1268 // pointer to the OatDexFile it was loaded from. Otherwise oat_dex_file_ is
1269 // null.
1270 const OatDexFile* oat_dex_file_;
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001271 mutable std::unique_ptr<TypeLookupTable> lookup_table_;
Andreas Gampee6215c02015-08-31 18:54:38 -07001272
1273 friend class DexFileVerifierTest;
Mathieu Chartier76172162016-01-26 14:54:06 -08001274 ART_FRIEND_TEST(ClassLinkerTest, RegisterDexFileName); // for constructor
Carl Shapiro1fb86202011-06-27 17:43:13 -07001275};
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001276
1277struct DexFileReference {
1278 DexFileReference(const DexFile* file, uint32_t idx) : dex_file(file), index(idx) { }
1279 const DexFile* dex_file;
1280 uint32_t index;
1281};
1282
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001283std::ostream& operator<<(std::ostream& os, const DexFile& dex_file);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001284
Ian Rogers0571d352011-11-03 19:51:38 -07001285// Iterate over a dex file's ProtoId's paramters
1286class DexFileParameterIterator {
1287 public:
1288 DexFileParameterIterator(const DexFile& dex_file, const DexFile::ProtoId& proto_id)
1289 : dex_file_(dex_file), size_(0), pos_(0) {
1290 type_list_ = dex_file_.GetProtoParameters(proto_id);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001291 if (type_list_ != nullptr) {
Ian Rogers0571d352011-11-03 19:51:38 -07001292 size_ = type_list_->Size();
1293 }
1294 }
1295 bool HasNext() const { return pos_ < size_; }
David Srbeckyb06e28e2015-12-10 13:15:00 +00001296 size_t Size() const { return size_; }
Ian Rogers0571d352011-11-03 19:51:38 -07001297 void Next() { ++pos_; }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001298 uint16_t GetTypeIdx() {
Ian Rogers0571d352011-11-03 19:51:38 -07001299 return type_list_->GetTypeItem(pos_).type_idx_;
1300 }
1301 const char* GetDescriptor() {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001302 return dex_file_.StringByTypeIdx(GetTypeIdx());
Ian Rogers0571d352011-11-03 19:51:38 -07001303 }
1304 private:
1305 const DexFile& dex_file_;
1306 const DexFile::TypeList* type_list_;
1307 uint32_t size_;
1308 uint32_t pos_;
1309 DISALLOW_IMPLICIT_CONSTRUCTORS(DexFileParameterIterator);
1310};
1311
Ian Rogersd91d6d62013-09-25 20:26:14 -07001312// Abstract the signature of a method.
Ian Rogers03b6eaf2014-10-28 09:34:57 -07001313class Signature : public ValueObject {
Ian Rogersd91d6d62013-09-25 20:26:14 -07001314 public:
1315 std::string ToString() const;
1316
1317 static Signature NoSignature() {
1318 return Signature();
1319 }
1320
Ian Rogersdfb325e2013-10-30 01:00:44 -07001321 bool operator==(const Signature& rhs) const;
Ian Rogersd91d6d62013-09-25 20:26:14 -07001322 bool operator!=(const Signature& rhs) const {
1323 return !(*this == rhs);
1324 }
1325
Vladimir Markod9cffea2013-11-25 15:08:02 +00001326 bool operator==(const StringPiece& rhs) const;
Ian Rogersd91d6d62013-09-25 20:26:14 -07001327
1328 private:
1329 Signature(const DexFile* dex, const DexFile::ProtoId& proto) : dex_file_(dex), proto_id_(&proto) {
1330 }
1331
1332 Signature() : dex_file_(nullptr), proto_id_(nullptr) {
1333 }
1334
1335 friend class DexFile;
1336
1337 const DexFile* const dex_file_;
1338 const DexFile::ProtoId* const proto_id_;
1339};
1340std::ostream& operator<<(std::ostream& os, const Signature& sig);
1341
Ian Rogers0571d352011-11-03 19:51:38 -07001342// Iterate and decode class_data_item
1343class ClassDataItemIterator {
1344 public:
Ian Rogers13735952014-10-08 12:43:28 -07001345 ClassDataItemIterator(const DexFile& dex_file, const uint8_t* raw_class_data_item)
Ian Rogers0571d352011-11-03 19:51:38 -07001346 : dex_file_(dex_file), pos_(0), ptr_pos_(raw_class_data_item), last_idx_(0) {
1347 ReadClassDataHeader();
1348 if (EndOfInstanceFieldsPos() > 0) {
1349 ReadClassDataField();
1350 } else if (EndOfVirtualMethodsPos() > 0) {
1351 ReadClassDataMethod();
1352 }
1353 }
1354 uint32_t NumStaticFields() const {
1355 return header_.static_fields_size_;
1356 }
1357 uint32_t NumInstanceFields() const {
1358 return header_.instance_fields_size_;
1359 }
1360 uint32_t NumDirectMethods() const {
1361 return header_.direct_methods_size_;
1362 }
1363 uint32_t NumVirtualMethods() const {
1364 return header_.virtual_methods_size_;
1365 }
1366 bool HasNextStaticField() const {
1367 return pos_ < EndOfStaticFieldsPos();
1368 }
1369 bool HasNextInstanceField() const {
1370 return pos_ >= EndOfStaticFieldsPos() && pos_ < EndOfInstanceFieldsPos();
1371 }
1372 bool HasNextDirectMethod() const {
1373 return pos_ >= EndOfInstanceFieldsPos() && pos_ < EndOfDirectMethodsPos();
1374 }
1375 bool HasNextVirtualMethod() const {
1376 return pos_ >= EndOfDirectMethodsPos() && pos_ < EndOfVirtualMethodsPos();
1377 }
1378 bool HasNext() const {
1379 return pos_ < EndOfVirtualMethodsPos();
1380 }
Ian Rogers637c65b2013-05-31 11:46:00 -07001381 inline void Next() {
Ian Rogers0571d352011-11-03 19:51:38 -07001382 pos_++;
1383 if (pos_ < EndOfStaticFieldsPos()) {
1384 last_idx_ = GetMemberIndex();
1385 ReadClassDataField();
1386 } else if (pos_ == EndOfStaticFieldsPos() && NumInstanceFields() > 0) {
1387 last_idx_ = 0; // transition to next array, reset last index
1388 ReadClassDataField();
1389 } else if (pos_ < EndOfInstanceFieldsPos()) {
1390 last_idx_ = GetMemberIndex();
1391 ReadClassDataField();
1392 } else if (pos_ == EndOfInstanceFieldsPos() && NumDirectMethods() > 0) {
1393 last_idx_ = 0; // transition to next array, reset last index
1394 ReadClassDataMethod();
1395 } else if (pos_ < EndOfDirectMethodsPos()) {
1396 last_idx_ = GetMemberIndex();
1397 ReadClassDataMethod();
1398 } else if (pos_ == EndOfDirectMethodsPos() && NumVirtualMethods() > 0) {
1399 last_idx_ = 0; // transition to next array, reset last index
1400 ReadClassDataMethod();
1401 } else if (pos_ < EndOfVirtualMethodsPos()) {
1402 last_idx_ = GetMemberIndex();
1403 ReadClassDataMethod();
1404 } else {
1405 DCHECK(!HasNext());
1406 }
1407 }
1408 uint32_t GetMemberIndex() const {
1409 if (pos_ < EndOfInstanceFieldsPos()) {
1410 return last_idx_ + field_.field_idx_delta_;
1411 } else {
Sebastien Hertzb24bd992013-08-02 15:19:09 +02001412 DCHECK_LT(pos_, EndOfVirtualMethodsPos());
Ian Rogers0571d352011-11-03 19:51:38 -07001413 return last_idx_ + method_.method_idx_delta_;
1414 }
1415 }
Andreas Gampe51829322014-08-25 15:05:04 -07001416 uint32_t GetRawMemberAccessFlags() const {
Ian Rogers0571d352011-11-03 19:51:38 -07001417 if (pos_ < EndOfInstanceFieldsPos()) {
1418 return field_.access_flags_;
1419 } else {
Sebastien Hertzb24bd992013-08-02 15:19:09 +02001420 DCHECK_LT(pos_, EndOfVirtualMethodsPos());
Ian Rogers0571d352011-11-03 19:51:38 -07001421 return method_.access_flags_;
1422 }
1423 }
Andreas Gampe51829322014-08-25 15:05:04 -07001424 uint32_t GetFieldAccessFlags() const {
1425 return GetRawMemberAccessFlags() & kAccValidFieldFlags;
1426 }
1427 uint32_t GetMethodAccessFlags() const {
1428 return GetRawMemberAccessFlags() & kAccValidMethodFlags;
1429 }
1430 bool MemberIsNative() const {
1431 return GetRawMemberAccessFlags() & kAccNative;
1432 }
1433 bool MemberIsFinal() const {
1434 return GetRawMemberAccessFlags() & kAccFinal;
1435 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001436 InvokeType GetMethodInvokeType(const DexFile::ClassDef& class_def) const {
1437 if (HasNextDirectMethod()) {
Andreas Gampe51829322014-08-25 15:05:04 -07001438 if ((GetRawMemberAccessFlags() & kAccStatic) != 0) {
Ian Rogers08f753d2012-08-24 14:35:25 -07001439 return kStatic;
1440 } else {
1441 return kDirect;
1442 }
1443 } else {
Andreas Gampe51829322014-08-25 15:05:04 -07001444 DCHECK_EQ(GetRawMemberAccessFlags() & kAccStatic, 0U);
Ian Rogers08f753d2012-08-24 14:35:25 -07001445 if ((class_def.access_flags_ & kAccInterface) != 0) {
1446 return kInterface;
Andreas Gampe51829322014-08-25 15:05:04 -07001447 } else if ((GetRawMemberAccessFlags() & kAccConstructor) != 0) {
Ian Rogers08f753d2012-08-24 14:35:25 -07001448 return kSuper;
1449 } else {
1450 return kVirtual;
1451 }
1452 }
1453 }
Ian Rogers0571d352011-11-03 19:51:38 -07001454 const DexFile::CodeItem* GetMethodCodeItem() const {
1455 return dex_file_.GetCodeItem(method_.code_off_);
1456 }
1457 uint32_t GetMethodCodeItemOffset() const {
1458 return method_.code_off_;
1459 }
Andreas Gampee6215c02015-08-31 18:54:38 -07001460 const uint8_t* DataPointer() const {
1461 return ptr_pos_;
1462 }
Ian Rogers13735952014-10-08 12:43:28 -07001463 const uint8_t* EndDataPointer() const {
jeffhao10037c82012-01-23 15:06:23 -08001464 CHECK(!HasNext());
1465 return ptr_pos_;
1466 }
Elliott Hughesa21039c2012-06-21 12:09:25 -07001467
Ian Rogers0571d352011-11-03 19:51:38 -07001468 private:
1469 // A dex file's class_data_item is leb128 encoded, this structure holds a decoded form of the
1470 // header for a class_data_item
1471 struct ClassDataHeader {
1472 uint32_t static_fields_size_; // the number of static fields
1473 uint32_t instance_fields_size_; // the number of instance fields
1474 uint32_t direct_methods_size_; // the number of direct methods
1475 uint32_t virtual_methods_size_; // the number of virtual methods
1476 } header_;
1477
1478 // Read and decode header from a class_data_item stream into header
1479 void ReadClassDataHeader();
1480
1481 uint32_t EndOfStaticFieldsPos() const {
1482 return header_.static_fields_size_;
1483 }
1484 uint32_t EndOfInstanceFieldsPos() const {
1485 return EndOfStaticFieldsPos() + header_.instance_fields_size_;
1486 }
1487 uint32_t EndOfDirectMethodsPos() const {
1488 return EndOfInstanceFieldsPos() + header_.direct_methods_size_;
1489 }
1490 uint32_t EndOfVirtualMethodsPos() const {
1491 return EndOfDirectMethodsPos() + header_.virtual_methods_size_;
1492 }
1493
1494 // A decoded version of the field of a class_data_item
1495 struct ClassDataField {
1496 uint32_t field_idx_delta_; // delta of index into the field_ids array for FieldId
1497 uint32_t access_flags_; // access flags for the field
1498 ClassDataField() : field_idx_delta_(0), access_flags_(0) {}
Elliott Hughesa21039c2012-06-21 12:09:25 -07001499
Ian Rogers0571d352011-11-03 19:51:38 -07001500 private:
1501 DISALLOW_COPY_AND_ASSIGN(ClassDataField);
Elliott Hughesee0fa762012-03-26 17:12:41 -07001502 };
1503 ClassDataField field_;
Ian Rogers0571d352011-11-03 19:51:38 -07001504
1505 // Read and decode a field from a class_data_item stream into field
1506 void ReadClassDataField();
1507
1508 // A decoded version of the method of a class_data_item
1509 struct ClassDataMethod {
1510 uint32_t method_idx_delta_; // delta of index into the method_ids array for MethodId
1511 uint32_t access_flags_;
1512 uint32_t code_off_;
1513 ClassDataMethod() : method_idx_delta_(0), access_flags_(0), code_off_(0) {}
Elliott Hughesa21039c2012-06-21 12:09:25 -07001514
Ian Rogers0571d352011-11-03 19:51:38 -07001515 private:
1516 DISALLOW_COPY_AND_ASSIGN(ClassDataMethod);
Elliott Hughesee0fa762012-03-26 17:12:41 -07001517 };
1518 ClassDataMethod method_;
Ian Rogers0571d352011-11-03 19:51:38 -07001519
1520 // Read and decode a method from a class_data_item stream into method
1521 void ReadClassDataMethod();
1522
1523 const DexFile& dex_file_;
1524 size_t pos_; // integral number of items passed
Ian Rogers13735952014-10-08 12:43:28 -07001525 const uint8_t* ptr_pos_; // pointer into stream of class_data_item
Ian Rogers0571d352011-11-03 19:51:38 -07001526 uint32_t last_idx_; // last read field or method index to apply delta to
1527 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassDataItemIterator);
1528};
1529
Ian Rogers0571d352011-11-03 19:51:38 -07001530class EncodedStaticFieldValueIterator {
1531 public:
Shinichiro Hamaji82863f02015-11-05 16:51:33 +09001532 // A constructor for static tools. You cannot call
1533 // ReadValueToField() for an object created by this.
1534 EncodedStaticFieldValueIterator(const DexFile& dex_file,
1535 const DexFile::ClassDef& class_def);
1536
1537 // A constructor meant to be called from runtime code.
Shinichiro Hamaji50a2f8d2015-12-11 09:45:28 +09001538 EncodedStaticFieldValueIterator(const DexFile& dex_file,
1539 Handle<mirror::DexCache>* dex_cache,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001540 Handle<mirror::ClassLoader>* class_loader,
Shinichiro Hamaji50a2f8d2015-12-11 09:45:28 +09001541 ClassLinker* linker,
1542 const DexFile::ClassDef& class_def)
Mathieu Chartier90443472015-07-16 20:32:27 -07001543 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers0571d352011-11-03 19:51:38 -07001544
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001545 template<bool kTransactionActive>
Mathieu Chartier90443472015-07-16 20:32:27 -07001546 void ReadValueToField(ArtField* field) const SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers0571d352011-11-03 19:51:38 -07001547
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001548 bool HasNext() const { return pos_ < array_size_; }
Ian Rogers0571d352011-11-03 19:51:38 -07001549
1550 void Next();
Elliott Hughesa21039c2012-06-21 12:09:25 -07001551
Ian Rogers0571d352011-11-03 19:51:38 -07001552 enum ValueType {
1553 kByte = 0x00,
1554 kShort = 0x02,
1555 kChar = 0x03,
1556 kInt = 0x04,
1557 kLong = 0x06,
1558 kFloat = 0x10,
1559 kDouble = 0x11,
1560 kString = 0x17,
1561 kType = 0x18,
1562 kField = 0x19,
1563 kMethod = 0x1a,
1564 kEnum = 0x1b,
1565 kArray = 0x1c,
1566 kAnnotation = 0x1d,
1567 kNull = 0x1e,
1568 kBoolean = 0x1f
1569 };
1570
Shinichiro Hamaji82863f02015-11-05 16:51:33 +09001571 ValueType GetValueType() const { return type_; }
1572 const jvalue& GetJavaValue() const { return jval_; }
1573
Brian Carlstrom88f36542012-10-16 23:24:21 -07001574 private:
Shinichiro Hamaji50a2f8d2015-12-11 09:45:28 +09001575 EncodedStaticFieldValueIterator(const DexFile& dex_file,
1576 Handle<mirror::DexCache>* dex_cache,
1577 Handle<mirror::ClassLoader>* class_loader,
1578 ClassLinker* linker,
1579 const DexFile::ClassDef& class_def,
1580 size_t pos,
1581 ValueType type);
1582
Ian Rogers13735952014-10-08 12:43:28 -07001583 static constexpr uint8_t kEncodedValueTypeMask = 0x1f; // 0b11111
1584 static constexpr uint8_t kEncodedValueArgShift = 5;
Ian Rogers0571d352011-11-03 19:51:38 -07001585
1586 const DexFile& dex_file_;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001587 Handle<mirror::DexCache>* const dex_cache_; // Dex cache to resolve literal objects.
1588 Handle<mirror::ClassLoader>* const class_loader_; // ClassLoader to resolve types.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001589 ClassLinker* linker_; // Linker to resolve literal objects.
1590 size_t array_size_; // Size of array.
1591 size_t pos_; // Current position.
Ian Rogers13735952014-10-08 12:43:28 -07001592 const uint8_t* ptr_; // Pointer into encoded data array.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001593 ValueType type_; // Type of current encoded value.
1594 jvalue jval_; // Value of current encoded value.
Ian Rogers0571d352011-11-03 19:51:38 -07001595 DISALLOW_IMPLICIT_CONSTRUCTORS(EncodedStaticFieldValueIterator);
1596};
Brian Carlstrom88f36542012-10-16 23:24:21 -07001597std::ostream& operator<<(std::ostream& os, const EncodedStaticFieldValueIterator::ValueType& code);
Ian Rogers0571d352011-11-03 19:51:38 -07001598
1599class CatchHandlerIterator {
1600 public:
1601 CatchHandlerIterator(const DexFile::CodeItem& code_item, uint32_t address);
Logan Chien736df022012-04-27 16:25:57 +08001602
1603 CatchHandlerIterator(const DexFile::CodeItem& code_item,
1604 const DexFile::TryItem& try_item);
1605
Ian Rogers13735952014-10-08 12:43:28 -07001606 explicit CatchHandlerIterator(const uint8_t* handler_data) {
Ian Rogers0571d352011-11-03 19:51:38 -07001607 Init(handler_data);
1608 }
1609
1610 uint16_t GetHandlerTypeIndex() const {
1611 return handler_.type_idx_;
1612 }
1613 uint32_t GetHandlerAddress() const {
1614 return handler_.address_;
1615 }
1616 void Next();
1617 bool HasNext() const {
1618 return remaining_count_ != -1 || catch_all_;
1619 }
1620 // End of this set of catch blocks, convenience method to locate next set of catch blocks
Ian Rogers13735952014-10-08 12:43:28 -07001621 const uint8_t* EndDataPointer() const {
Ian Rogers0571d352011-11-03 19:51:38 -07001622 CHECK(!HasNext());
1623 return current_data_;
1624 }
Elliott Hughesa21039c2012-06-21 12:09:25 -07001625
Ian Rogers0571d352011-11-03 19:51:38 -07001626 private:
Logan Chien736df022012-04-27 16:25:57 +08001627 void Init(const DexFile::CodeItem& code_item, int32_t offset);
Ian Rogers13735952014-10-08 12:43:28 -07001628 void Init(const uint8_t* handler_data);
Ian Rogers0571d352011-11-03 19:51:38 -07001629
1630 struct CatchHandlerItem {
1631 uint16_t type_idx_; // type index of the caught exception type
1632 uint32_t address_; // handler address
1633 } handler_;
Ian Rogers13735952014-10-08 12:43:28 -07001634 const uint8_t* current_data_; // the current handler in dex file.
Ian Rogers0571d352011-11-03 19:51:38 -07001635 int32_t remaining_count_; // number of handlers not read.
1636 bool catch_all_; // is there a handler that will catch all exceptions in case
1637 // that all typed handler does not match.
1638};
1639
Carl Shapiro1fb86202011-06-27 17:43:13 -07001640} // namespace art
1641
Brian Carlstromfc0e3212013-07-17 14:40:12 -07001642#endif // ART_RUNTIME_DEX_FILE_H_