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