dex_file.h revision fc0e94bed3f88ed7e50854fd8dfaf5dcb345250f
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_RUNTIME_DEX_FILE_H_
18#define ART_RUNTIME_DEX_FILE_H_
19
20#include <string>
21#include <vector>
22
23#include "base/logging.h"
24#include "base/mutex.h"
25#include "base/stringpiece.h"
26#include "globals.h"
27#include "invoke_type.h"
28#include "jni.h"
29#include "mem_map.h"
30#include "modifiers.h"
31#include "safe_map.h"
32#include "UniquePtr.h"
33
34namespace art {
35
36namespace mirror {
37  class ArtField;
38  class ArtMethod;
39  class ClassLoader;
40  class DexCache;
41}  // namespace mirror
42class ClassLinker;
43class StringPiece;
44class ZipArchive;
45
46// TODO: move all of the macro functionality into the DexCache class.
47class DexFile {
48 public:
49  static const byte kDexMagic[];
50  static const byte kDexMagicVersion[];
51  static const size_t kSha1DigestSize = 20;
52  static const uint32_t kDexEndianConstant = 0x12345678;
53
54  // name of the DexFile entry within a zip archive
55  static const char* kClassesDex;
56
57  // The value of an invalid index.
58  static const uint32_t kDexNoIndex = 0xFFFFFFFF;
59
60  // The value of an invalid index.
61  static const uint16_t kDexNoIndex16 = 0xFFFF;
62
63  // Raw header_item.
64  struct Header {
65    uint8_t magic_[8];
66    uint32_t checksum_;  // See also location_checksum_
67    uint8_t signature_[kSha1DigestSize];
68    uint32_t file_size_;  // size of entire file
69    uint32_t header_size_;  // offset to start of next section
70    uint32_t endian_tag_;
71    uint32_t link_size_;  // unused
72    uint32_t link_off_;  // unused
73    uint32_t map_off_;  // unused
74    uint32_t string_ids_size_;  // number of StringIds
75    uint32_t string_ids_off_;  // file offset of StringIds array
76    uint32_t type_ids_size_;  // number of TypeIds, we don't support more than 65535
77    uint32_t type_ids_off_;  // file offset of TypeIds array
78    uint32_t proto_ids_size_;  // number of ProtoIds, we don't support more than 65535
79    uint32_t proto_ids_off_;  // file offset of ProtoIds array
80    uint32_t field_ids_size_;  // number of FieldIds
81    uint32_t field_ids_off_;  // file offset of FieldIds array
82    uint32_t method_ids_size_;  // number of MethodIds
83    uint32_t method_ids_off_;  // file offset of MethodIds array
84    uint32_t class_defs_size_;  // number of ClassDefs
85    uint32_t class_defs_off_;  // file offset of ClassDef array
86    uint32_t data_size_;  // unused
87    uint32_t data_off_;  // unused
88
89   private:
90    DISALLOW_COPY_AND_ASSIGN(Header);
91  };
92
93  // Map item type codes.
94  enum {
95    kDexTypeHeaderItem               = 0x0000,
96    kDexTypeStringIdItem             = 0x0001,
97    kDexTypeTypeIdItem               = 0x0002,
98    kDexTypeProtoIdItem              = 0x0003,
99    kDexTypeFieldIdItem              = 0x0004,
100    kDexTypeMethodIdItem             = 0x0005,
101    kDexTypeClassDefItem             = 0x0006,
102    kDexTypeMapList                  = 0x1000,
103    kDexTypeTypeList                 = 0x1001,
104    kDexTypeAnnotationSetRefList     = 0x1002,
105    kDexTypeAnnotationSetItem        = 0x1003,
106    kDexTypeClassDataItem            = 0x2000,
107    kDexTypeCodeItem                 = 0x2001,
108    kDexTypeStringDataItem           = 0x2002,
109    kDexTypeDebugInfoItem            = 0x2003,
110    kDexTypeAnnotationItem           = 0x2004,
111    kDexTypeEncodedArrayItem         = 0x2005,
112    kDexTypeAnnotationsDirectoryItem = 0x2006,
113  };
114
115  struct MapItem {
116    uint16_t type_;
117    uint16_t unused_;
118    uint32_t size_;
119    uint32_t offset_;
120
121   private:
122    DISALLOW_COPY_AND_ASSIGN(MapItem);
123  };
124
125  struct MapList {
126    uint32_t size_;
127    MapItem list_[1];
128
129   private:
130    DISALLOW_COPY_AND_ASSIGN(MapList);
131  };
132
133  // Raw string_id_item.
134  struct StringId {
135    uint32_t string_data_off_;  // offset in bytes from the base address
136
137   private:
138    DISALLOW_COPY_AND_ASSIGN(StringId);
139  };
140
141  // Raw type_id_item.
142  struct TypeId {
143    uint32_t descriptor_idx_;  // index into string_ids
144
145   private:
146    DISALLOW_COPY_AND_ASSIGN(TypeId);
147  };
148
149  // Raw field_id_item.
150  struct FieldId {
151    uint16_t class_idx_;  // index into type_ids_ array for defining class
152    uint16_t type_idx_;  // index into type_ids_ array for field type
153    uint32_t name_idx_;  // index into string_ids_ array for field name
154
155   private:
156    DISALLOW_COPY_AND_ASSIGN(FieldId);
157  };
158
159  // Raw method_id_item.
160  struct MethodId {
161    uint16_t class_idx_;  // index into type_ids_ array for defining class
162    uint16_t proto_idx_;  // index into proto_ids_ array for method prototype
163    uint32_t name_idx_;  // index into string_ids_ array for method name
164
165   private:
166    DISALLOW_COPY_AND_ASSIGN(MethodId);
167  };
168
169  // Raw proto_id_item.
170  struct ProtoId {
171    uint32_t shorty_idx_;  // index into string_ids array for shorty descriptor
172    uint16_t return_type_idx_;  // index into type_ids array for return type
173    uint16_t pad_;             // padding = 0
174    uint32_t parameters_off_;  // file offset to type_list for parameter types
175
176   private:
177    DISALLOW_COPY_AND_ASSIGN(ProtoId);
178  };
179
180  // Raw class_def_item.
181  struct ClassDef {
182    uint16_t class_idx_;  // index into type_ids_ array for this class
183    uint16_t pad1_;  // padding = 0
184    uint32_t access_flags_;
185    uint16_t superclass_idx_;  // index into type_ids_ array for superclass
186    uint16_t pad2_;  // padding = 0
187    uint32_t interfaces_off_;  // file offset to TypeList
188    uint32_t source_file_idx_;  // index into string_ids_ for source file name
189    uint32_t annotations_off_;  // file offset to annotations_directory_item
190    uint32_t class_data_off_;  // file offset to class_data_item
191    uint32_t static_values_off_;  // file offset to EncodedArray
192
193   private:
194    DISALLOW_COPY_AND_ASSIGN(ClassDef);
195  };
196
197  // Raw type_item.
198  struct TypeItem {
199    uint16_t type_idx_;  // index into type_ids section
200
201   private:
202    DISALLOW_COPY_AND_ASSIGN(TypeItem);
203  };
204
205  // Raw type_list.
206  class TypeList {
207   public:
208    uint32_t Size() const {
209      return size_;
210    }
211
212    const TypeItem& GetTypeItem(uint32_t idx) const {
213      DCHECK_LT(idx, this->size_);
214      return this->list_[idx];
215    }
216
217   private:
218    uint32_t size_;  // size of the list, in entries
219    TypeItem list_[1];  // elements of the list
220    DISALLOW_COPY_AND_ASSIGN(TypeList);
221  };
222
223  // Raw code_item.
224  struct CodeItem {
225    uint16_t registers_size_;
226    uint16_t ins_size_;
227    uint16_t outs_size_;
228    uint16_t tries_size_;
229    uint32_t debug_info_off_;  // file offset to debug info stream
230    uint32_t insns_size_in_code_units_;  // size of the insns array, in 2 byte code units
231    uint16_t insns_[1];
232
233   private:
234    DISALLOW_COPY_AND_ASSIGN(CodeItem);
235  };
236
237  // Raw try_item.
238  struct TryItem {
239    uint32_t start_addr_;
240    uint16_t insn_count_;
241    uint16_t handler_off_;
242
243   private:
244    DISALLOW_COPY_AND_ASSIGN(TryItem);
245  };
246
247  // Annotation constants.
248  enum {
249    kDexVisibilityBuild         = 0x00,     /* annotation visibility */
250    kDexVisibilityRuntime       = 0x01,
251    kDexVisibilitySystem        = 0x02,
252
253    kDexAnnotationByte          = 0x00,
254    kDexAnnotationShort         = 0x02,
255    kDexAnnotationChar          = 0x03,
256    kDexAnnotationInt           = 0x04,
257    kDexAnnotationLong          = 0x06,
258    kDexAnnotationFloat         = 0x10,
259    kDexAnnotationDouble        = 0x11,
260    kDexAnnotationString        = 0x17,
261    kDexAnnotationType          = 0x18,
262    kDexAnnotationField         = 0x19,
263    kDexAnnotationMethod        = 0x1a,
264    kDexAnnotationEnum          = 0x1b,
265    kDexAnnotationArray         = 0x1c,
266    kDexAnnotationAnnotation    = 0x1d,
267    kDexAnnotationNull          = 0x1e,
268    kDexAnnotationBoolean       = 0x1f,
269
270    kDexAnnotationValueTypeMask = 0x1f,     /* low 5 bits */
271    kDexAnnotationValueArgShift = 5,
272  };
273
274  struct AnnotationsDirectoryItem {
275    uint32_t class_annotations_off_;
276    uint32_t fields_size_;
277    uint32_t methods_size_;
278    uint32_t parameters_size_;
279
280   private:
281    DISALLOW_COPY_AND_ASSIGN(AnnotationsDirectoryItem);
282  };
283
284  struct FieldAnnotationsItem {
285    uint32_t field_idx_;
286    uint32_t annotations_off_;
287
288   private:
289    DISALLOW_COPY_AND_ASSIGN(FieldAnnotationsItem);
290  };
291
292  struct MethodAnnotationsItem {
293    uint32_t method_idx_;
294    uint32_t annotations_off_;
295
296   private:
297    DISALLOW_COPY_AND_ASSIGN(MethodAnnotationsItem);
298  };
299
300  struct ParameterAnnotationsItem {
301    uint32_t method_idx_;
302    uint32_t annotations_off_;
303
304   private:
305    DISALLOW_COPY_AND_ASSIGN(ParameterAnnotationsItem);
306  };
307
308  struct AnnotationSetRefItem {
309    uint32_t annotations_off_;
310
311   private:
312    DISALLOW_COPY_AND_ASSIGN(AnnotationSetRefItem);
313  };
314
315  struct AnnotationSetRefList {
316    uint32_t size_;
317    AnnotationSetRefItem list_[1];
318
319   private:
320    DISALLOW_COPY_AND_ASSIGN(AnnotationSetRefList);
321  };
322
323  struct AnnotationSetItem {
324    uint32_t size_;
325    uint32_t entries_[1];
326
327   private:
328    DISALLOW_COPY_AND_ASSIGN(AnnotationSetItem);
329  };
330
331  struct AnnotationItem {
332    uint8_t visibility_;
333    uint8_t annotation_[1];
334
335   private:
336    DISALLOW_COPY_AND_ASSIGN(AnnotationItem);
337  };
338
339  typedef std::pair<const DexFile*, const DexFile::ClassDef*> ClassPathEntry;
340  typedef std::vector<const DexFile*> ClassPath;
341
342  // Search a collection of DexFiles for a descriptor
343  static ClassPathEntry FindInClassPath(const char* descriptor,
344                                        const ClassPath& class_path);
345
346  // Returns the checksum of a file for comparison with GetLocationChecksum().
347  // For .dex files, this is the header checksum.
348  // For zip files, this is the classes.dex zip entry CRC32 checksum.
349  // Return true if the checksum could be found, false otherwise.
350  static bool GetChecksum(const std::string& filename, uint32_t& checksum)
351      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
352
353  // Opens .dex file, guessing the container format based on file extension
354  static const DexFile* Open(const std::string& filename,
355                             const std::string& location);
356
357  // Opens .dex file, backed by existing memory
358  static const DexFile* Open(const uint8_t* base, size_t size,
359                             const std::string& location,
360                             uint32_t location_checksum) {
361    return OpenMemory(base, size, location, location_checksum, NULL);
362  }
363
364  // Opens .dex file from the classes.dex in a zip archive
365  static const DexFile* Open(const ZipArchive& zip_archive, const std::string& location);
366
367  // Closes a .dex file.
368  virtual ~DexFile();
369
370  const std::string& GetLocation() const {
371    return location_;
372  }
373
374  // For DexFiles directly from .dex files, this is the checksum from the DexFile::Header.
375  // For DexFiles opened from a zip files, this will be the ZipEntry CRC32 of classes.dex.
376  uint32_t GetLocationChecksum() const {
377    return location_checksum_;
378  }
379
380  const Header& GetHeader() const {
381    DCHECK(header_ != NULL) << GetLocation();
382    return *header_;
383  }
384
385  Mutex& GetModificationLock() {
386    return modification_lock;
387  }
388
389  // Decode the dex magic version
390  uint32_t GetVersion() const;
391
392  // Returns true if the byte string points to the magic value.
393  static bool IsMagicValid(const byte* magic);
394
395  // Returns true if the byte string after the magic is the correct value.
396  static bool IsVersionValid(const byte* magic);
397
398  // Returns the number of string identifiers in the .dex file.
399  size_t NumStringIds() const {
400    DCHECK(header_ != NULL) << GetLocation();
401    return header_->string_ids_size_;
402  }
403
404  // Returns the StringId at the specified index.
405  const StringId& GetStringId(uint32_t idx) const {
406    DCHECK_LT(idx, NumStringIds()) << GetLocation();
407    return string_ids_[idx];
408  }
409
410  uint32_t GetIndexForStringId(const StringId& string_id) const {
411    CHECK_GE(&string_id, string_ids_) << GetLocation();
412    CHECK_LT(&string_id, string_ids_ + header_->string_ids_size_) << GetLocation();
413    return &string_id - string_ids_;
414  }
415
416  int32_t GetStringLength(const StringId& string_id) const;
417
418  // Returns a pointer to the UTF-8 string data referred to by the given string_id.
419  const char* GetStringDataAndLength(const StringId& string_id, uint32_t* length) const;
420
421  const char* GetStringData(const StringId& string_id) const {
422    uint32_t length;
423    return GetStringDataAndLength(string_id, &length);
424  }
425
426  // return the UTF-8 encoded string with the specified string_id index
427  const char* StringDataAndLengthByIdx(uint32_t idx, uint32_t* unicode_length) const {
428    if (idx == kDexNoIndex) {
429      *unicode_length = 0;
430      return NULL;
431    }
432    const StringId& string_id = GetStringId(idx);
433    return GetStringDataAndLength(string_id, unicode_length);
434  }
435
436  StringPiece StringDataAsStringPieceByIdx(uint32_t idx) const;
437
438  const char* StringDataByIdx(uint32_t idx) const {
439    uint32_t unicode_length;
440    return StringDataAndLengthByIdx(idx, &unicode_length);
441  }
442
443  // Looks up a string id for a given modified utf8 string.
444  const StringId* FindStringId(const char* string) const;
445
446  // Looks up a string id for a given utf16 string.
447  const StringId* FindStringId(const uint16_t* string) const;
448
449  // Returns the number of type identifiers in the .dex file.
450  size_t NumTypeIds() const {
451    DCHECK(header_ != NULL) << GetLocation();
452    return header_->type_ids_size_;
453  }
454
455  // Returns the TypeId at the specified index.
456  const TypeId& GetTypeId(uint32_t idx) const {
457    DCHECK_LT(idx, NumTypeIds()) << GetLocation();
458    return type_ids_[idx];
459  }
460
461  uint16_t GetIndexForTypeId(const TypeId& type_id) const {
462    CHECK_GE(&type_id, type_ids_) << GetLocation();
463    CHECK_LT(&type_id, type_ids_ + header_->type_ids_size_) << GetLocation();
464    size_t result = &type_id - type_ids_;
465    DCHECK_LT(result, 65536U) << GetLocation();
466    return static_cast<uint16_t>(result);
467  }
468
469  // Get the descriptor string associated with a given type index.
470  const char* StringByTypeIdx(uint32_t idx, uint32_t* unicode_length) const {
471    const TypeId& type_id = GetTypeId(idx);
472    return StringDataAndLengthByIdx(type_id.descriptor_idx_, unicode_length);
473  }
474
475  const char* StringByTypeIdx(uint32_t idx) const {
476    const TypeId& type_id = GetTypeId(idx);
477    return StringDataByIdx(type_id.descriptor_idx_);
478  }
479
480  // Returns the type descriptor string of a type id.
481  const char* GetTypeDescriptor(const TypeId& type_id) const {
482    return StringDataByIdx(type_id.descriptor_idx_);
483  }
484
485  // Looks up a type for the given string index
486  const TypeId* FindTypeId(uint32_t string_idx) const;
487
488  // Returns the number of field identifiers in the .dex file.
489  size_t NumFieldIds() const {
490    DCHECK(header_ != NULL) << GetLocation();
491    return header_->field_ids_size_;
492  }
493
494  // Returns the FieldId at the specified index.
495  const FieldId& GetFieldId(uint32_t idx) const {
496    DCHECK_LT(idx, NumFieldIds()) << GetLocation();
497    return field_ids_[idx];
498  }
499
500  uint32_t GetIndexForFieldId(const FieldId& field_id) const {
501    CHECK_GE(&field_id, field_ids_) << GetLocation();
502    CHECK_LT(&field_id, field_ids_ + header_->field_ids_size_) << GetLocation();
503    return &field_id - field_ids_;
504  }
505
506  // Looks up a field by its declaring class, name and type
507  const FieldId* FindFieldId(const DexFile::TypeId& declaring_klass,
508                             const DexFile::StringId& name,
509                             const DexFile::TypeId& type) const;
510
511  // Returns the declaring class descriptor string of a field id.
512  const char* GetFieldDeclaringClassDescriptor(const FieldId& field_id) const {
513    const DexFile::TypeId& type_id = GetTypeId(field_id.class_idx_);
514    return GetTypeDescriptor(type_id);
515  }
516
517  // Returns the class descriptor string of a field id.
518  const char* GetFieldTypeDescriptor(const FieldId& field_id) const {
519    const DexFile::TypeId& type_id = GetTypeId(field_id.type_idx_);
520    return GetTypeDescriptor(type_id);
521  }
522
523  // Returns the name of a field id.
524  const char* GetFieldName(const FieldId& field_id) const {
525    return StringDataByIdx(field_id.name_idx_);
526  }
527
528  // Returns the number of method identifiers in the .dex file.
529  size_t NumMethodIds() const {
530    DCHECK(header_ != NULL) << GetLocation();
531    return header_->method_ids_size_;
532  }
533
534  // Returns the MethodId at the specified index.
535  const MethodId& GetMethodId(uint32_t idx) const {
536    DCHECK_LT(idx, NumMethodIds()) << GetLocation();
537    return method_ids_[idx];
538  }
539
540  uint32_t GetIndexForMethodId(const MethodId& method_id) const {
541    CHECK_GE(&method_id, method_ids_) << GetLocation();
542    CHECK_LT(&method_id, method_ids_ + header_->method_ids_size_) << GetLocation();
543    return &method_id - method_ids_;
544  }
545
546  // Looks up a method by its declaring class, name and proto_id
547  const MethodId* FindMethodId(const DexFile::TypeId& declaring_klass,
548                               const DexFile::StringId& name,
549                               const DexFile::ProtoId& signature) const;
550
551  // Returns the declaring class descriptor string of a method id.
552  const char* GetMethodDeclaringClassDescriptor(const MethodId& method_id) const {
553    const DexFile::TypeId& type_id = GetTypeId(method_id.class_idx_);
554    return GetTypeDescriptor(type_id);
555  }
556
557  // Returns the prototype of a method id.
558  const ProtoId& GetMethodPrototype(const MethodId& method_id) const {
559    return GetProtoId(method_id.proto_idx_);
560  }
561
562  // Returns the signature of a method id.
563  const std::string GetMethodSignature(const MethodId& method_id) const {
564    return CreateMethodSignature(method_id.proto_idx_, NULL);
565  }
566
567  // Returns the name of a method id.
568  const char* GetMethodName(const MethodId& method_id) const {
569    return StringDataByIdx(method_id.name_idx_);
570  }
571
572  // Returns the shorty of a method id.
573  const char* GetMethodShorty(const MethodId& method_id) const {
574    return StringDataByIdx(GetProtoId(method_id.proto_idx_).shorty_idx_);
575  }
576  const char* GetMethodShorty(const MethodId& method_id, uint32_t* length) const {
577    return StringDataAndLengthByIdx(GetProtoId(method_id.proto_idx_).shorty_idx_, length);
578  }
579  // Returns the number of class definitions in the .dex file.
580  size_t NumClassDefs() const {
581    DCHECK(header_ != NULL) << GetLocation();
582    return header_->class_defs_size_;
583  }
584
585  // Returns the ClassDef at the specified index.
586  const ClassDef& GetClassDef(uint16_t idx) const {
587    DCHECK_LT(idx, NumClassDefs()) << GetLocation();
588    return class_defs_[idx];
589  }
590
591  uint16_t GetIndexForClassDef(const ClassDef& class_def) const {
592    CHECK_GE(&class_def, class_defs_) << GetLocation();
593    CHECK_LT(&class_def, class_defs_ + header_->class_defs_size_) << GetLocation();
594    return &class_def - class_defs_;
595  }
596
597  // Returns the class descriptor string of a class definition.
598  const char* GetClassDescriptor(const ClassDef& class_def) const {
599    return StringByTypeIdx(class_def.class_idx_);
600  }
601
602  // Looks up a class definition by its class descriptor.
603  const ClassDef* FindClassDef(const char* descriptor) const;
604
605  // Looks up a class definition by its type index.
606  const ClassDef* FindClassDef(uint16_t type_idx) const;
607
608  const TypeList* GetInterfacesList(const ClassDef& class_def) const {
609    if (class_def.interfaces_off_ == 0) {
610        return NULL;
611    } else {
612      const byte* addr = begin_ + class_def.interfaces_off_;
613      return reinterpret_cast<const TypeList*>(addr);
614    }
615  }
616
617  // Returns a pointer to the raw memory mapped class_data_item
618  const byte* GetClassData(const ClassDef& class_def) const {
619    if (class_def.class_data_off_ == 0) {
620      return NULL;
621    } else {
622      return begin_ + class_def.class_data_off_;
623    }
624  }
625
626  //
627  const CodeItem* GetCodeItem(const uint32_t code_off) const {
628    if (code_off == 0) {
629      return NULL;  // native or abstract method
630    } else {
631      const byte* addr = begin_ + code_off;
632      return reinterpret_cast<const CodeItem*>(addr);
633    }
634  }
635
636  const char* GetReturnTypeDescriptor(const ProtoId& proto_id) const {
637    return StringByTypeIdx(proto_id.return_type_idx_);
638  }
639
640  // Returns the number of prototype identifiers in the .dex file.
641  size_t NumProtoIds() const {
642    DCHECK(header_ != NULL) << GetLocation();
643    return header_->proto_ids_size_;
644  }
645
646  // Returns the ProtoId at the specified index.
647  const ProtoId& GetProtoId(uint32_t idx) const {
648    DCHECK_LT(idx, NumProtoIds()) << GetLocation();
649    return proto_ids_[idx];
650  }
651
652  uint16_t GetIndexForProtoId(const ProtoId& proto_id) const {
653    CHECK_GE(&proto_id, proto_ids_) << GetLocation();
654    CHECK_LT(&proto_id, proto_ids_ + header_->proto_ids_size_) << GetLocation();
655    return &proto_id - proto_ids_;
656  }
657
658  // Looks up a proto id for a given return type and signature type list
659  const ProtoId* FindProtoId(uint16_t return_type_id,
660                             const std::vector<uint16_t>& signature_type_idxs_) const;
661
662  // Given a signature place the type ids into the given vector, returns true on success
663  bool CreateTypeList(uint16_t* return_type_idx, std::vector<uint16_t>* param_type_idxs,
664                      const std::string& signature) const;
665
666  // Given a proto_idx decode the type list and return type into a method signature
667  std::string CreateMethodSignature(uint32_t proto_idx, int32_t* unicode_length) const;
668
669  // Returns the short form method descriptor for the given prototype.
670  const char* GetShorty(uint32_t proto_idx) const {
671    const ProtoId& proto_id = GetProtoId(proto_idx);
672    return StringDataByIdx(proto_id.shorty_idx_);
673  }
674
675  const TypeList* GetProtoParameters(const ProtoId& proto_id) const {
676    if (proto_id.parameters_off_ == 0) {
677      return NULL;
678    } else {
679      const byte* addr = begin_ + proto_id.parameters_off_;
680      return reinterpret_cast<const TypeList*>(addr);
681    }
682  }
683
684  const byte* GetEncodedStaticFieldValuesArray(const ClassDef& class_def) const {
685    if (class_def.static_values_off_ == 0) {
686      return 0;
687    } else {
688      return begin_ + class_def.static_values_off_;
689    }
690  }
691
692  static const TryItem* GetTryItems(const CodeItem& code_item, uint32_t offset);
693
694  // Get the base of the encoded data for the given DexCode.
695  static const byte* GetCatchHandlerData(const CodeItem& code_item, uint32_t offset) {
696    const byte* handler_data =
697        reinterpret_cast<const byte*>(GetTryItems(code_item, code_item.tries_size_));
698    return handler_data + offset;
699  }
700
701  // Find which try region is associated with the given address (ie dex pc). Returns -1 if none.
702  static int32_t FindTryItem(const CodeItem &code_item, uint32_t address);
703
704  // Find the handler offset associated with the given address (ie dex pc). Returns -1 if none.
705  static int32_t FindCatchHandlerOffset(const CodeItem &code_item, uint32_t address);
706
707  // Get the pointer to the start of the debugging data
708  const byte* GetDebugInfoStream(const CodeItem* code_item) const {
709    if (code_item->debug_info_off_ == 0) {
710      return NULL;
711    } else {
712      return begin_ + code_item->debug_info_off_;
713    }
714  }
715
716  // Callback for "new position table entry".
717  // Returning true causes the decoder to stop early.
718  typedef bool (*DexDebugNewPositionCb)(void* context, uint32_t address, uint32_t line_num);
719
720  // Callback for "new locals table entry". "signature" is an empty string
721  // if no signature is available for an entry.
722  typedef void (*DexDebugNewLocalCb)(void* context, uint16_t reg,
723                                     uint32_t start_address,
724                                     uint32_t end_address,
725                                     const char* name,
726                                     const char* descriptor,
727                                     const char* signature);
728
729  static bool LineNumForPcCb(void* context, uint32_t address, uint32_t line_num);
730
731  // Debug info opcodes and constants
732  enum {
733    DBG_END_SEQUENCE         = 0x00,
734    DBG_ADVANCE_PC           = 0x01,
735    DBG_ADVANCE_LINE         = 0x02,
736    DBG_START_LOCAL          = 0x03,
737    DBG_START_LOCAL_EXTENDED = 0x04,
738    DBG_END_LOCAL            = 0x05,
739    DBG_RESTART_LOCAL        = 0x06,
740    DBG_SET_PROLOGUE_END     = 0x07,
741    DBG_SET_EPILOGUE_BEGIN   = 0x08,
742    DBG_SET_FILE             = 0x09,
743    DBG_FIRST_SPECIAL        = 0x0a,
744    DBG_LINE_BASE            = -4,
745    DBG_LINE_RANGE           = 15,
746  };
747
748  struct LocalInfo {
749    LocalInfo()
750        : name_(NULL), descriptor_(NULL), signature_(NULL), start_address_(0), is_live_(false) {}
751
752    const char* name_;  // E.g., list
753    const char* descriptor_;  // E.g., Ljava/util/LinkedList;
754    const char* signature_;  // E.g., java.util.LinkedList<java.lang.Integer>
755    uint16_t start_address_;  // PC location where the local is first defined.
756    bool is_live_;  // Is the local defined and live.
757
758   private:
759    DISALLOW_COPY_AND_ASSIGN(LocalInfo);
760  };
761
762  struct LineNumFromPcContext {
763    LineNumFromPcContext(uint32_t address, uint32_t line_num)
764        : address_(address), line_num_(line_num) {}
765    uint32_t address_;
766    uint32_t line_num_;
767   private:
768    DISALLOW_COPY_AND_ASSIGN(LineNumFromPcContext);
769  };
770
771  void InvokeLocalCbIfLive(void* context, int reg, uint32_t end_address,
772                           LocalInfo* local_in_reg, DexDebugNewLocalCb local_cb) const {
773    if (local_cb != NULL && local_in_reg[reg].is_live_) {
774      local_cb(context, reg, local_in_reg[reg].start_address_, end_address,
775          local_in_reg[reg].name_, local_in_reg[reg].descriptor_,
776          local_in_reg[reg].signature_ != NULL ? local_in_reg[reg].signature_ : "");
777    }
778  }
779
780  // Determine the source file line number based on the program counter.
781  // "pc" is an offset, in 16-bit units, from the start of the method's code.
782  //
783  // Returns -1 if no match was found (possibly because the source files were
784  // compiled without "-g", so no line number information is present).
785  // Returns -2 for native methods (as expected in exception traces).
786  //
787  // This is used by runtime; therefore use art::Method not art::DexFile::Method.
788  int32_t GetLineNumFromPC(const mirror::ArtMethod* method, uint32_t rel_pc) const
789      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
790
791  void DecodeDebugInfo(const CodeItem* code_item, bool is_static, uint32_t method_idx,
792                       DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb,
793                       void* context) const;
794
795  const char* GetSourceFile(const ClassDef& class_def) const {
796    if (class_def.source_file_idx_ == 0xffffffff) {
797      return NULL;
798    } else {
799      return StringDataByIdx(class_def.source_file_idx_);
800    }
801  }
802
803  int GetPermissions() const;
804
805  bool IsReadOnly() const;
806
807  bool EnableWrite() const;
808
809  bool DisableWrite() const;
810
811  const byte* Begin() const {
812    return begin_;
813  }
814
815  size_t Size() const {
816    return size_;
817  }
818
819 private:
820  // Opens a .dex file
821  static const DexFile* OpenFile(const std::string& filename,
822                                 const std::string& location,
823                                 bool verify);
824
825  // Opens a dex file from within a .jar, .zip, or .apk file
826  static const DexFile* OpenZip(const std::string& filename,
827                                const std::string& location);
828
829  // Opens a .dex file at the given address backed by a MemMap
830  static const DexFile* OpenMemory(const std::string& location,
831                                   uint32_t location_checksum,
832                                   MemMap* mem_map);
833
834  // Opens a .dex file at the given address, optionally backed by a MemMap
835  static const DexFile* OpenMemory(const byte* dex_file,
836                                   size_t size,
837                                   const std::string& location,
838                                   uint32_t location_checksum,
839                                   MemMap* mem_map);
840
841  DexFile(const byte* base, size_t size,
842          const std::string& location,
843          uint32_t location_checksum,
844          MemMap* mem_map)
845      : begin_(base),
846        size_(size),
847        location_(location),
848        location_checksum_(location_checksum),
849        mem_map_(mem_map),
850        modification_lock("DEX modification lock"),
851        header_(0),
852        string_ids_(0),
853        type_ids_(0),
854        field_ids_(0),
855        method_ids_(0),
856        proto_ids_(0),
857        class_defs_(0) {
858    CHECK(begin_ != NULL) << GetLocation();
859    CHECK_GT(size_, 0U) << GetLocation();
860  }
861
862  // Top-level initializer that calls other Init methods.
863  bool Init();
864
865  // Caches pointers into to the various file sections.
866  void InitMembers();
867
868  // Returns true if the header magic and version numbers are of the expected values.
869  bool CheckMagicAndVersion() const;
870
871  void DecodeDebugInfo0(const CodeItem* code_item, bool is_static, uint32_t method_idx,
872      DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb,
873      void* context, const byte* stream, LocalInfo* local_in_reg) const;
874
875  // The base address of the memory mapping.
876  const byte* const begin_;
877
878  // The size of the underlying memory allocation in bytes.
879  const size_t size_;
880
881  // Typically the dex file name when available, alternatively some identifying string.
882  //
883  // The ClassLinker will use this to match DexFiles the boot class
884  // path to DexCache::GetLocation when loading from an image.
885  const std::string location_;
886
887  const uint32_t location_checksum_;
888
889  // Manages the underlying memory allocation.
890  UniquePtr<MemMap> mem_map_;
891
892  // The DEX-to-DEX compiler uses this lock to ensure thread safety when
893  // enabling write access to a read-only DEX file.
894  // TODO: move to Locks::dex_file_modification_lock.
895  Mutex modification_lock;
896
897  // Points to the header section.
898  const Header* header_;
899
900  // Points to the base of the string identifier list.
901  const StringId* string_ids_;
902
903  // Points to the base of the type identifier list.
904  const TypeId* type_ids_;
905
906  // Points to the base of the field identifier list.
907  const FieldId* field_ids_;
908
909  // Points to the base of the method identifier list.
910  const MethodId* method_ids_;
911
912  // Points to the base of the prototype identifier list.
913  const ProtoId* proto_ids_;
914
915  // Points to the base of the class definition list.
916  const ClassDef* class_defs_;
917};
918
919// Iterate over a dex file's ProtoId's paramters
920class DexFileParameterIterator {
921 public:
922  DexFileParameterIterator(const DexFile& dex_file, const DexFile::ProtoId& proto_id)
923      : dex_file_(dex_file), size_(0), pos_(0) {
924    type_list_ = dex_file_.GetProtoParameters(proto_id);
925    if (type_list_ != NULL) {
926      size_ = type_list_->Size();
927    }
928  }
929  bool HasNext() const { return pos_ < size_; }
930  void Next() { ++pos_; }
931  uint16_t GetTypeIdx() {
932    return type_list_->GetTypeItem(pos_).type_idx_;
933  }
934  const char* GetDescriptor() {
935    return dex_file_.StringByTypeIdx(GetTypeIdx());
936  }
937 private:
938  const DexFile& dex_file_;
939  const DexFile::TypeList* type_list_;
940  uint32_t size_;
941  uint32_t pos_;
942  DISALLOW_IMPLICIT_CONSTRUCTORS(DexFileParameterIterator);
943};
944
945// Iterate and decode class_data_item
946class ClassDataItemIterator {
947 public:
948  ClassDataItemIterator(const DexFile& dex_file, const byte* raw_class_data_item)
949      : dex_file_(dex_file), pos_(0), ptr_pos_(raw_class_data_item), last_idx_(0) {
950    ReadClassDataHeader();
951    if (EndOfInstanceFieldsPos() > 0) {
952      ReadClassDataField();
953    } else if (EndOfVirtualMethodsPos() > 0) {
954      ReadClassDataMethod();
955    }
956  }
957  uint32_t NumStaticFields() const {
958    return header_.static_fields_size_;
959  }
960  uint32_t NumInstanceFields() const {
961    return header_.instance_fields_size_;
962  }
963  uint32_t NumDirectMethods() const {
964    return header_.direct_methods_size_;
965  }
966  uint32_t NumVirtualMethods() const {
967    return header_.virtual_methods_size_;
968  }
969  bool HasNextStaticField() const {
970    return pos_ < EndOfStaticFieldsPos();
971  }
972  bool HasNextInstanceField() const {
973    return pos_ >= EndOfStaticFieldsPos() && pos_ < EndOfInstanceFieldsPos();
974  }
975  bool HasNextDirectMethod() const {
976    return pos_ >= EndOfInstanceFieldsPos() && pos_ < EndOfDirectMethodsPos();
977  }
978  bool HasNextVirtualMethod() const {
979    return pos_ >= EndOfDirectMethodsPos() && pos_ < EndOfVirtualMethodsPos();
980  }
981  bool HasNext() const {
982    return pos_ < EndOfVirtualMethodsPos();
983  }
984  inline void Next() {
985    pos_++;
986    if (pos_ < EndOfStaticFieldsPos()) {
987      last_idx_ = GetMemberIndex();
988      ReadClassDataField();
989    } else if (pos_ == EndOfStaticFieldsPos() && NumInstanceFields() > 0) {
990      last_idx_ = 0;  // transition to next array, reset last index
991      ReadClassDataField();
992    } else if (pos_ < EndOfInstanceFieldsPos()) {
993      last_idx_ = GetMemberIndex();
994      ReadClassDataField();
995    } else if (pos_ == EndOfInstanceFieldsPos() && NumDirectMethods() > 0) {
996      last_idx_ = 0;  // transition to next array, reset last index
997      ReadClassDataMethod();
998    } else if (pos_ < EndOfDirectMethodsPos()) {
999      last_idx_ = GetMemberIndex();
1000      ReadClassDataMethod();
1001    } else if (pos_ == EndOfDirectMethodsPos() && NumVirtualMethods() > 0) {
1002      last_idx_ = 0;  // transition to next array, reset last index
1003      ReadClassDataMethod();
1004    } else if (pos_ < EndOfVirtualMethodsPos()) {
1005      last_idx_ = GetMemberIndex();
1006      ReadClassDataMethod();
1007    } else {
1008      DCHECK(!HasNext());
1009    }
1010  }
1011  uint32_t GetMemberIndex() const {
1012    if (pos_ < EndOfInstanceFieldsPos()) {
1013      return last_idx_ + field_.field_idx_delta_;
1014    } else {
1015      DCHECK_LT(pos_, EndOfVirtualMethodsPos());
1016      return last_idx_ + method_.method_idx_delta_;
1017    }
1018  }
1019  uint32_t GetMemberAccessFlags() const {
1020    if (pos_ < EndOfInstanceFieldsPos()) {
1021      return field_.access_flags_;
1022    } else {
1023      DCHECK_LT(pos_, EndOfVirtualMethodsPos());
1024      return method_.access_flags_;
1025    }
1026  }
1027  InvokeType GetMethodInvokeType(const DexFile::ClassDef& class_def) const {
1028    if (HasNextDirectMethod()) {
1029      if ((GetMemberAccessFlags() & kAccStatic) != 0) {
1030        return kStatic;
1031      } else {
1032        return kDirect;
1033      }
1034    } else {
1035      DCHECK_EQ(GetMemberAccessFlags() & kAccStatic, 0U);
1036      if ((class_def.access_flags_ & kAccInterface) != 0) {
1037        return kInterface;
1038      } else if ((GetMemberAccessFlags() & kAccConstructor) != 0) {
1039        return kSuper;
1040      } else {
1041        return kVirtual;
1042      }
1043    }
1044  }
1045  const DexFile::CodeItem* GetMethodCodeItem() const {
1046    return dex_file_.GetCodeItem(method_.code_off_);
1047  }
1048  uint32_t GetMethodCodeItemOffset() const {
1049    return method_.code_off_;
1050  }
1051  const byte* EndDataPointer() const {
1052    CHECK(!HasNext());
1053    return ptr_pos_;
1054  }
1055
1056 private:
1057  // A dex file's class_data_item is leb128 encoded, this structure holds a decoded form of the
1058  // header for a class_data_item
1059  struct ClassDataHeader {
1060    uint32_t static_fields_size_;  // the number of static fields
1061    uint32_t instance_fields_size_;  // the number of instance fields
1062    uint32_t direct_methods_size_;  // the number of direct methods
1063    uint32_t virtual_methods_size_;  // the number of virtual methods
1064  } header_;
1065
1066  // Read and decode header from a class_data_item stream into header
1067  void ReadClassDataHeader();
1068
1069  uint32_t EndOfStaticFieldsPos() const {
1070    return header_.static_fields_size_;
1071  }
1072  uint32_t EndOfInstanceFieldsPos() const {
1073    return EndOfStaticFieldsPos() + header_.instance_fields_size_;
1074  }
1075  uint32_t EndOfDirectMethodsPos() const {
1076    return EndOfInstanceFieldsPos() + header_.direct_methods_size_;
1077  }
1078  uint32_t EndOfVirtualMethodsPos() const {
1079    return EndOfDirectMethodsPos() + header_.virtual_methods_size_;
1080  }
1081
1082  // A decoded version of the field of a class_data_item
1083  struct ClassDataField {
1084    uint32_t field_idx_delta_;  // delta of index into the field_ids array for FieldId
1085    uint32_t access_flags_;  // access flags for the field
1086    ClassDataField() :  field_idx_delta_(0), access_flags_(0) {}
1087
1088   private:
1089    DISALLOW_COPY_AND_ASSIGN(ClassDataField);
1090  };
1091  ClassDataField field_;
1092
1093  // Read and decode a field from a class_data_item stream into field
1094  void ReadClassDataField();
1095
1096  // A decoded version of the method of a class_data_item
1097  struct ClassDataMethod {
1098    uint32_t method_idx_delta_;  // delta of index into the method_ids array for MethodId
1099    uint32_t access_flags_;
1100    uint32_t code_off_;
1101    ClassDataMethod() : method_idx_delta_(0), access_flags_(0), code_off_(0) {}
1102
1103   private:
1104    DISALLOW_COPY_AND_ASSIGN(ClassDataMethod);
1105  };
1106  ClassDataMethod method_;
1107
1108  // Read and decode a method from a class_data_item stream into method
1109  void ReadClassDataMethod();
1110
1111  const DexFile& dex_file_;
1112  size_t pos_;  // integral number of items passed
1113  const byte* ptr_pos_;  // pointer into stream of class_data_item
1114  uint32_t last_idx_;  // last read field or method index to apply delta to
1115  DISALLOW_IMPLICIT_CONSTRUCTORS(ClassDataItemIterator);
1116};
1117
1118class EncodedStaticFieldValueIterator {
1119 public:
1120  EncodedStaticFieldValueIterator(const DexFile& dex_file, mirror::DexCache* dex_cache,
1121                                  mirror::ClassLoader* class_loader,
1122                                  ClassLinker* linker, const DexFile::ClassDef& class_def)
1123      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1124
1125  void ReadValueToField(mirror::ArtField* field) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1126
1127  bool HasNext() { return pos_ < array_size_; }
1128
1129  void Next();
1130
1131  enum ValueType {
1132    kByte = 0x00,
1133    kShort = 0x02,
1134    kChar = 0x03,
1135    kInt = 0x04,
1136    kLong = 0x06,
1137    kFloat = 0x10,
1138    kDouble = 0x11,
1139    kString = 0x17,
1140    kType = 0x18,
1141    kField = 0x19,
1142    kMethod = 0x1a,
1143    kEnum = 0x1b,
1144    kArray = 0x1c,
1145    kAnnotation = 0x1d,
1146    kNull = 0x1e,
1147    kBoolean = 0x1f
1148  };
1149
1150 private:
1151  static const byte kEncodedValueTypeMask = 0x1f;  // 0b11111
1152  static const byte kEncodedValueArgShift = 5;
1153
1154  const DexFile& dex_file_;
1155  mirror::DexCache* dex_cache_;  // Dex cache to resolve literal objects.
1156  mirror::ClassLoader* class_loader_;  // ClassLoader to resolve types.
1157  ClassLinker* linker_;  // Linker to resolve literal objects.
1158  size_t array_size_;  // Size of array.
1159  size_t pos_;  // Current position.
1160  const byte* ptr_;  // Pointer into encoded data array.
1161  ValueType type_;  // Type of current encoded value.
1162  jvalue jval_;  // Value of current encoded value.
1163  DISALLOW_IMPLICIT_CONSTRUCTORS(EncodedStaticFieldValueIterator);
1164};
1165std::ostream& operator<<(std::ostream& os, const EncodedStaticFieldValueIterator::ValueType& code);
1166
1167class CatchHandlerIterator {
1168  public:
1169    CatchHandlerIterator(const DexFile::CodeItem& code_item, uint32_t address);
1170
1171    CatchHandlerIterator(const DexFile::CodeItem& code_item,
1172                         const DexFile::TryItem& try_item);
1173
1174    explicit CatchHandlerIterator(const byte* handler_data) {
1175      Init(handler_data);
1176    }
1177
1178    uint16_t GetHandlerTypeIndex() const {
1179      return handler_.type_idx_;
1180    }
1181    uint32_t GetHandlerAddress() const {
1182      return handler_.address_;
1183    }
1184    void Next();
1185    bool HasNext() const {
1186      return remaining_count_ != -1 || catch_all_;
1187    }
1188    // End of this set of catch blocks, convenience method to locate next set of catch blocks
1189    const byte* EndDataPointer() const {
1190      CHECK(!HasNext());
1191      return current_data_;
1192    }
1193
1194  private:
1195    void Init(const DexFile::CodeItem& code_item, int32_t offset);
1196    void Init(const byte* handler_data);
1197
1198    struct CatchHandlerItem {
1199      uint16_t type_idx_;  // type index of the caught exception type
1200      uint32_t address_;  // handler address
1201    } handler_;
1202    const byte *current_data_;  // the current handler in dex file.
1203    int32_t remaining_count_;   // number of handlers not read.
1204    bool catch_all_;            // is there a handler that will catch all exceptions in case
1205                                // that all typed handler does not match.
1206};
1207
1208}  // namespace art
1209
1210#endif  // ART_RUNTIME_DEX_FILE_H_
1211