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