• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_LIBDEXFILE_DEX_DEX_FILE_H_
18 #define ART_LIBDEXFILE_DEX_DEX_FILE_H_
19 
20 #include <memory>
21 #include <optional>
22 #include <string>
23 #include <string_view>
24 #include <vector>
25 
26 #include <android-base/logging.h>
27 
28 #include "base/globals.h"
29 #include "base/macros.h"
30 #include "base/value_object.h"
31 #include "dex_file_structs.h"
32 #include "dex_file_types.h"
33 #include "jni.h"
34 #include "modifiers.h"
35 
36 namespace art {
37 
38 class ClassDataItemIterator;
39 class ClassIterator;
40 class CompactDexFile;
41 class DexInstructionIterator;
42 enum InvokeType : uint32_t;
43 template <typename Iter> class IterationRange;
44 class MemMap;
45 class OatDexFile;
46 class Signature;
47 class StandardDexFile;
48 class ZipArchive;
49 
50 namespace hiddenapi {
51 enum class Domain : char;
52 }  // namespace hiddenapi
53 
54 // Some instances of DexFile own the storage referred to by DexFile.  Clients who create
55 // such management do so by subclassing Container.
56 class DexFileContainer {
57  public:
DexFileContainer()58   DexFileContainer() { }
~DexFileContainer()59   virtual ~DexFileContainer() { }
60   virtual int GetPermissions() = 0;
61   virtual bool IsReadOnly() = 0;
62   virtual bool EnableWrite() = 0;
63   virtual bool DisableWrite() = 0;
64 
65  private:
66   DISALLOW_COPY_AND_ASSIGN(DexFileContainer);
67 };
68 
69 // Dex file is the API that exposes native dex files (ordinary dex files) and CompactDex.
70 // Originally, the dex file format used by ART was mostly the same as APKs. The only change was
71 // quickened opcodes and layout optimizations.
72 // Since ART needs to support both native dex files and CompactDex files, the DexFile interface
73 // provides an abstraction to facilitate this.
74 class DexFile {
75  public:
76   // Number of bytes in the dex file magic.
77   static constexpr size_t kDexMagicSize = 4;
78   static constexpr size_t kDexVersionLen = 4;
79 
80   // First Dex format version enforcing class definition ordering rules.
81   static constexpr uint32_t kClassDefinitionOrderEnforcedVersion = 37;
82 
83   static constexpr size_t kSha1DigestSize = 20;
84   static constexpr uint32_t kDexEndianConstant = 0x12345678;
85 
86   // The value of an invalid index.
87   static constexpr uint16_t kDexNoIndex16 = 0xFFFF;
88   static constexpr uint32_t kDexNoIndex32 = 0xFFFFFFFF;
89 
90   // Raw header_item.
91   struct Header {
92     uint8_t magic_[8] = {};
93     uint32_t checksum_ = 0;  // See also location_checksum_
94     uint8_t signature_[kSha1DigestSize] = {};
95     uint32_t file_size_ = 0;  // size of entire file
96     uint32_t header_size_ = 0;  // offset to start of next section
97     uint32_t endian_tag_ = 0;
98     uint32_t link_size_ = 0;  // unused
99     uint32_t link_off_ = 0;  // unused
100     uint32_t map_off_ = 0;  // map list offset from data_off_
101     uint32_t string_ids_size_ = 0;  // number of StringIds
102     uint32_t string_ids_off_ = 0;  // file offset of StringIds array
103     uint32_t type_ids_size_ = 0;  // number of TypeIds, we don't support more than 65535
104     uint32_t type_ids_off_ = 0;  // file offset of TypeIds array
105     uint32_t proto_ids_size_ = 0;  // number of ProtoIds, we don't support more than 65535
106     uint32_t proto_ids_off_ = 0;  // file offset of ProtoIds array
107     uint32_t field_ids_size_ = 0;  // number of FieldIds
108     uint32_t field_ids_off_ = 0;  // file offset of FieldIds array
109     uint32_t method_ids_size_ = 0;  // number of MethodIds
110     uint32_t method_ids_off_ = 0;  // file offset of MethodIds array
111     uint32_t class_defs_size_ = 0;  // number of ClassDefs
112     uint32_t class_defs_off_ = 0;  // file offset of ClassDef array
113     uint32_t data_size_ = 0;  // size of data section
114     uint32_t data_off_ = 0;  // file offset of data section
115 
116     // Decode the dex magic version
117     uint32_t GetVersion() const;
118   };
119 
120   // Map item type codes.
121   enum MapItemType : uint16_t {  // private
122     kDexTypeHeaderItem               = 0x0000,
123     kDexTypeStringIdItem             = 0x0001,
124     kDexTypeTypeIdItem               = 0x0002,
125     kDexTypeProtoIdItem              = 0x0003,
126     kDexTypeFieldIdItem              = 0x0004,
127     kDexTypeMethodIdItem             = 0x0005,
128     kDexTypeClassDefItem             = 0x0006,
129     kDexTypeCallSiteIdItem           = 0x0007,
130     kDexTypeMethodHandleItem         = 0x0008,
131     kDexTypeMapList                  = 0x1000,
132     kDexTypeTypeList                 = 0x1001,
133     kDexTypeAnnotationSetRefList     = 0x1002,
134     kDexTypeAnnotationSetItem        = 0x1003,
135     kDexTypeClassDataItem            = 0x2000,
136     kDexTypeCodeItem                 = 0x2001,
137     kDexTypeStringDataItem           = 0x2002,
138     kDexTypeDebugInfoItem            = 0x2003,
139     kDexTypeAnnotationItem           = 0x2004,
140     kDexTypeEncodedArrayItem         = 0x2005,
141     kDexTypeAnnotationsDirectoryItem = 0x2006,
142     kDexTypeHiddenapiClassData       = 0xF000,
143   };
144 
145   // MethodHandle Types
146   enum class MethodHandleType : uint16_t {  // private
147     kStaticPut         = 0x0000,  // a setter for a given static field.
148     kStaticGet         = 0x0001,  // a getter for a given static field.
149     kInstancePut       = 0x0002,  // a setter for a given instance field.
150     kInstanceGet       = 0x0003,  // a getter for a given instance field.
151     kInvokeStatic      = 0x0004,  // an invoker for a given static method.
152     kInvokeInstance    = 0x0005,  // invoke_instance : an invoker for a given instance method. This
153                                   // can be any non-static method on any class (or interface) except
154                                   // for “<init>”.
155     kInvokeConstructor = 0x0006,  // an invoker for a given constructor.
156     kInvokeDirect      = 0x0007,  // an invoker for a direct (special) method.
157     kInvokeInterface   = 0x0008,  // an invoker for an interface method.
158     kLast = kInvokeInterface
159   };
160 
161   // Annotation constants.
162   enum {
163     kDexVisibilityBuild         = 0x00,     /* annotation visibility */
164     kDexVisibilityRuntime       = 0x01,
165     kDexVisibilitySystem        = 0x02,
166 
167     kDexAnnotationByte          = 0x00,
168     kDexAnnotationShort         = 0x02,
169     kDexAnnotationChar          = 0x03,
170     kDexAnnotationInt           = 0x04,
171     kDexAnnotationLong          = 0x06,
172     kDexAnnotationFloat         = 0x10,
173     kDexAnnotationDouble        = 0x11,
174     kDexAnnotationMethodType    = 0x15,
175     kDexAnnotationMethodHandle  = 0x16,
176     kDexAnnotationString        = 0x17,
177     kDexAnnotationType          = 0x18,
178     kDexAnnotationField         = 0x19,
179     kDexAnnotationMethod        = 0x1a,
180     kDexAnnotationEnum          = 0x1b,
181     kDexAnnotationArray         = 0x1c,
182     kDexAnnotationAnnotation    = 0x1d,
183     kDexAnnotationNull          = 0x1e,
184     kDexAnnotationBoolean       = 0x1f,
185 
186     kDexAnnotationValueTypeMask = 0x1f,     /* low 5 bits */
187     kDexAnnotationValueArgShift = 5,
188   };
189 
190   enum AnnotationResultStyle {  // private
191     kAllObjects,
192     kPrimitivesOrObjects,
193     kAllRaw
194   };
195 
196   struct AnnotationValue;
197 
198   // Closes a .dex file.
199   virtual ~DexFile();
200 
GetLocation()201   const std::string& GetLocation() const {
202     return location_;
203   }
204 
205   // For DexFiles directly from .dex files, this is the checksum from the DexFile::Header.
206   // For DexFiles opened from a zip files, this will be the ZipEntry CRC32 of classes.dex.
GetLocationChecksum()207   uint32_t GetLocationChecksum() const {
208     return location_checksum_;
209   }
210 
GetHeader()211   const Header& GetHeader() const {
212     DCHECK(header_ != nullptr) << GetLocation();
213     return *header_;
214   }
215 
216   // Decode the dex magic version
GetDexVersion()217   uint32_t GetDexVersion() const {
218     return GetHeader().GetVersion();
219   }
220 
221   // Returns true if the byte string points to the magic value.
222   virtual bool IsMagicValid() const = 0;
223 
224   // Returns true if the byte string after the magic is the correct value.
225   virtual bool IsVersionValid() const = 0;
226 
227   // Returns true if the dex file supports default methods.
228   virtual bool SupportsDefaultMethods() const = 0;
229 
230   // Returns the maximum size in bytes needed to store an equivalent dex file strictly conforming to
231   // the dex file specification. That is the size if we wanted to get rid of all the
232   // quickening/compact-dexing/etc.
233   //
234   // TODO This should really be an exact size! b/72402467
235   virtual size_t GetDequickenedSize() const = 0;
236 
237   // Returns the number of string identifiers in the .dex file.
NumStringIds()238   size_t NumStringIds() const {
239     DCHECK(header_ != nullptr) << GetLocation();
240     return header_->string_ids_size_;
241   }
242 
243   // Returns the StringId at the specified index.
GetStringId(dex::StringIndex idx)244   const dex::StringId& GetStringId(dex::StringIndex idx) const {
245     DCHECK_LT(idx.index_, NumStringIds()) << GetLocation();
246     return string_ids_[idx.index_];
247   }
248 
GetIndexForStringId(const dex::StringId & string_id)249   dex::StringIndex GetIndexForStringId(const dex::StringId& string_id) const {
250     CHECK_GE(&string_id, string_ids_) << GetLocation();
251     CHECK_LT(&string_id, string_ids_ + header_->string_ids_size_) << GetLocation();
252     return dex::StringIndex(&string_id - string_ids_);
253   }
254 
255   int32_t GetStringLength(const dex::StringId& string_id) const;
256 
257   // Returns a pointer to the UTF-8 string data referred to by the given string_id as well as the
258   // length of the string when decoded as a UTF-16 string. Note the UTF-16 length is not the same
259   // as the string length of the string data.
260   const char* GetStringDataAndUtf16Length(const dex::StringId& string_id,
261                                           uint32_t* utf16_length) const;
262 
263   const char* GetStringData(const dex::StringId& string_id) const;
264 
265   // Index version of GetStringDataAndUtf16Length.
266   const char* StringDataAndUtf16LengthByIdx(dex::StringIndex idx, uint32_t* utf16_length) const;
267 
268   const char* StringDataByIdx(dex::StringIndex idx) const;
269   std::string_view StringViewByIdx(dex::StringIndex idx) const;
270 
271   // Looks up a string id for a given modified utf8 string.
272   const dex::StringId* FindStringId(const char* string) const;
273 
274   const dex::TypeId* FindTypeId(const char* string) const;
FindTypeId(std::string_view string)275   const dex::TypeId* FindTypeId(std::string_view string) const {
276     return FindTypeId(std::string(string).c_str());
277   }
278 
279   // Returns the number of type identifiers in the .dex file.
NumTypeIds()280   uint32_t NumTypeIds() const {
281     DCHECK(header_ != nullptr) << GetLocation();
282     return header_->type_ids_size_;
283   }
284 
IsTypeIndexValid(dex::TypeIndex idx)285   bool IsTypeIndexValid(dex::TypeIndex idx) const {
286     return idx.IsValid() && idx.index_ < NumTypeIds();
287   }
288 
289   // Returns the TypeId at the specified index.
GetTypeId(dex::TypeIndex idx)290   const dex::TypeId& GetTypeId(dex::TypeIndex idx) const {
291     DCHECK_LT(idx.index_, NumTypeIds()) << GetLocation();
292     return type_ids_[idx.index_];
293   }
294 
GetIndexForTypeId(const dex::TypeId & type_id)295   dex::TypeIndex GetIndexForTypeId(const dex::TypeId& type_id) const {
296     CHECK_GE(&type_id, type_ids_) << GetLocation();
297     CHECK_LT(&type_id, type_ids_ + header_->type_ids_size_) << GetLocation();
298     size_t result = &type_id - type_ids_;
299     DCHECK_LT(result, 65536U) << GetLocation();
300     return dex::TypeIndex(static_cast<uint16_t>(result));
301   }
302 
303   // Get the descriptor string associated with a given type index.
304   const char* StringByTypeIdx(dex::TypeIndex idx, uint32_t* unicode_length) const;
305 
306   const char* StringByTypeIdx(dex::TypeIndex idx) const;
307 
308   // Returns the type descriptor string of a type id.
309   const char* GetTypeDescriptor(const dex::TypeId& type_id) const;
310   std::string_view GetTypeDescriptorView(const dex::TypeId& type_id) const;
311 
312   // Looks up a type for the given string index
313   const dex::TypeId* FindTypeId(dex::StringIndex string_idx) const;
314 
315   // Returns the number of field identifiers in the .dex file.
NumFieldIds()316   size_t NumFieldIds() const {
317     DCHECK(header_ != nullptr) << GetLocation();
318     return header_->field_ids_size_;
319   }
320 
321   // Returns the FieldId at the specified index.
GetFieldId(uint32_t idx)322   const dex::FieldId& GetFieldId(uint32_t idx) const {
323     DCHECK_LT(idx, NumFieldIds()) << GetLocation();
324     return field_ids_[idx];
325   }
326 
GetIndexForFieldId(const dex::FieldId & field_id)327   uint32_t GetIndexForFieldId(const dex::FieldId& field_id) const {
328     CHECK_GE(&field_id, field_ids_) << GetLocation();
329     CHECK_LT(&field_id, field_ids_ + header_->field_ids_size_) << GetLocation();
330     return &field_id - field_ids_;
331   }
332 
333   // Looks up a field by its declaring class, name and type
334   const dex::FieldId* FindFieldId(const dex::TypeId& declaring_klass,
335                                   const dex::StringId& name,
336                                   const dex::TypeId& type) const;
337 
338   // Return the code-item offset associated with the class and method or nullopt
339   // if the method does not exist or has no code.
340   std::optional<uint32_t> GetCodeItemOffset(const dex::ClassDef& class_def,
341                                             uint32_t dex_method_idx) const;
342 
343   // Return the code-item offset associated with the class and method or
344   // LOG(FATAL) if the method does not exist or has no code.
345   uint32_t FindCodeItemOffset(const dex::ClassDef& class_def,
346                               uint32_t dex_method_idx) const;
347 
348   virtual uint32_t GetCodeItemSize(const dex::CodeItem& disk_code_item) const = 0;
349 
350   // Returns the declaring class descriptor string of a field id.
GetFieldDeclaringClassDescriptor(const dex::FieldId & field_id)351   const char* GetFieldDeclaringClassDescriptor(const dex::FieldId& field_id) const {
352     const dex::TypeId& type_id = GetTypeId(field_id.class_idx_);
353     return GetTypeDescriptor(type_id);
354   }
355 
356   // Returns the class descriptor string of a field id.
357   const char* GetFieldTypeDescriptor(const dex::FieldId& field_id) const;
358   std::string_view GetFieldTypeDescriptorView(const dex::FieldId& field_id) const;
359 
360   // Returns the name of a field id.
361   const char* GetFieldName(const dex::FieldId& field_id) const;
362   std::string_view GetFieldNameView(const dex::FieldId& field_id) const;
363 
364   // Returns the number of method identifiers in the .dex file.
NumMethodIds()365   size_t NumMethodIds() const {
366     DCHECK(header_ != nullptr) << GetLocation();
367     return header_->method_ids_size_;
368   }
369 
370   // Returns the MethodId at the specified index.
GetMethodId(uint32_t idx)371   const dex::MethodId& GetMethodId(uint32_t idx) const {
372     DCHECK_LT(idx, NumMethodIds()) << GetLocation();
373     return method_ids_[idx];
374   }
375 
GetIndexForMethodId(const dex::MethodId & method_id)376   uint32_t GetIndexForMethodId(const dex::MethodId& method_id) const {
377     CHECK_GE(&method_id, method_ids_) << GetLocation();
378     CHECK_LT(&method_id, method_ids_ + header_->method_ids_size_) << GetLocation();
379     return &method_id - method_ids_;
380   }
381 
382   // Looks up a method by its declaring class, name and proto_id
383   const dex::MethodId* FindMethodId(const dex::TypeId& declaring_klass,
384                                     const dex::StringId& name,
385                                     const dex::ProtoId& signature) const;
386 
387   const dex::MethodId* FindMethodIdByIndex(dex::TypeIndex declaring_klass,
388                                            dex::StringIndex name,
389                                            dex::ProtoIndex signature) const;
390 
391   // Returns the declaring class descriptor string of a method id.
392   const char* GetMethodDeclaringClassDescriptor(const dex::MethodId& method_id) const;
393 
394   // Returns the prototype of a method id.
GetMethodPrototype(const dex::MethodId & method_id)395   const dex::ProtoId& GetMethodPrototype(const dex::MethodId& method_id) const {
396     return GetProtoId(method_id.proto_idx_);
397   }
398 
399   // Returns a representation of the signature of a method id.
400   const Signature GetMethodSignature(const dex::MethodId& method_id) const;
401 
402   // Returns a representation of the signature of a proto id.
403   const Signature GetProtoSignature(const dex::ProtoId& proto_id) const;
404 
405   // Returns the name of a method id.
406   const char* GetMethodName(const dex::MethodId& method_id) const;
407   const char* GetMethodName(const dex::MethodId& method_id, uint32_t* utf_length) const;
408   const char* GetMethodName(uint32_t idx) const;
409   const char* GetMethodName(uint32_t idx, uint32_t* utf_length) const;
410   std::string_view GetMethodNameView(const dex::MethodId& method_id) const;
411   std::string_view GetMethodNameView(uint32_t idx) const;
412 
413   // Returns the shorty of a method by its index.
414   const char* GetMethodShorty(uint32_t idx) const;
415 
416   // Returns the shorty of a method id.
417   const char* GetMethodShorty(const dex::MethodId& method_id) const;
418   const char* GetMethodShorty(const dex::MethodId& method_id, uint32_t* length) const;
419 
420   // Returns the number of class definitions in the .dex file.
NumClassDefs()421   uint32_t NumClassDefs() const {
422     DCHECK(header_ != nullptr) << GetLocation();
423     return header_->class_defs_size_;
424   }
425 
426   // Returns the ClassDef at the specified index.
GetClassDef(uint16_t idx)427   const dex::ClassDef& GetClassDef(uint16_t idx) const {
428     DCHECK_LT(idx, NumClassDefs()) << GetLocation();
429     return class_defs_[idx];
430   }
431 
GetIndexForClassDef(const dex::ClassDef & class_def)432   uint16_t GetIndexForClassDef(const dex::ClassDef& class_def) const {
433     CHECK_GE(&class_def, class_defs_) << GetLocation();
434     CHECK_LT(&class_def, class_defs_ + header_->class_defs_size_) << GetLocation();
435     return &class_def - class_defs_;
436   }
437 
438   // Returns the class descriptor string of a class definition.
439   const char* GetClassDescriptor(const dex::ClassDef& class_def) const;
440 
441   // Looks up a class definition by its type index.
442   const dex::ClassDef* FindClassDef(dex::TypeIndex type_idx) const;
443 
GetInterfacesList(const dex::ClassDef & class_def)444   const dex::TypeList* GetInterfacesList(const dex::ClassDef& class_def) const {
445     return DataPointer<dex::TypeList>(class_def.interfaces_off_);
446   }
447 
NumMethodHandles()448   uint32_t NumMethodHandles() const {
449     return num_method_handles_;
450   }
451 
GetMethodHandle(uint32_t idx)452   const dex::MethodHandleItem& GetMethodHandle(uint32_t idx) const {
453     CHECK_LT(idx, NumMethodHandles());
454     return method_handles_[idx];
455   }
456 
NumCallSiteIds()457   uint32_t NumCallSiteIds() const {
458     return num_call_site_ids_;
459   }
460 
GetCallSiteId(uint32_t idx)461   const dex::CallSiteIdItem& GetCallSiteId(uint32_t idx) const {
462     CHECK_LT(idx, NumCallSiteIds());
463     return call_site_ids_[idx];
464   }
465 
466   // Returns a pointer to the raw memory mapped class_data_item
GetClassData(const dex::ClassDef & class_def)467   const uint8_t* GetClassData(const dex::ClassDef& class_def) const {
468     return DataPointer<uint8_t>(class_def.class_data_off_);
469   }
470 
471   // Return the code item for a provided offset.
GetCodeItem(const uint32_t code_off)472   const dex::CodeItem* GetCodeItem(const uint32_t code_off) const {
473     // May be null for native or abstract methods.
474     return DataPointer<dex::CodeItem>(code_off);
475   }
476 
477   const char* GetReturnTypeDescriptor(const dex::ProtoId& proto_id) const;
478 
479   // Returns the number of prototype identifiers in the .dex file.
NumProtoIds()480   size_t NumProtoIds() const {
481     DCHECK(header_ != nullptr) << GetLocation();
482     return header_->proto_ids_size_;
483   }
484 
485   // Returns the ProtoId at the specified index.
GetProtoId(dex::ProtoIndex idx)486   const dex::ProtoId& GetProtoId(dex::ProtoIndex idx) const {
487     DCHECK_LT(idx.index_, NumProtoIds()) << GetLocation();
488     return proto_ids_[idx.index_];
489   }
490 
GetIndexForProtoId(const dex::ProtoId & proto_id)491   dex::ProtoIndex GetIndexForProtoId(const dex::ProtoId& proto_id) const {
492     CHECK_GE(&proto_id, proto_ids_) << GetLocation();
493     CHECK_LT(&proto_id, proto_ids_ + header_->proto_ids_size_) << GetLocation();
494     return dex::ProtoIndex(&proto_id - proto_ids_);
495   }
496 
497   // Looks up a proto id for a given return type and signature type list
498   const dex::ProtoId* FindProtoId(dex::TypeIndex return_type_idx,
499                                   const dex::TypeIndex* signature_type_idxs,
500                              uint32_t signature_length) const;
FindProtoId(dex::TypeIndex return_type_idx,const std::vector<dex::TypeIndex> & signature_type_idxs)501   const dex::ProtoId* FindProtoId(dex::TypeIndex return_type_idx,
502                                   const std::vector<dex::TypeIndex>& signature_type_idxs) const {
503     return FindProtoId(return_type_idx, &signature_type_idxs[0], signature_type_idxs.size());
504   }
505 
506   // Given a signature place the type ids into the given vector, returns true on success
507   bool CreateTypeList(std::string_view signature,
508                       dex::TypeIndex* return_type_idx,
509                       std::vector<dex::TypeIndex>* param_type_idxs) const;
510 
511   // Returns the short form method descriptor for the given prototype.
512   const char* GetShorty(dex::ProtoIndex proto_idx) const;
513   std::string_view GetShortyView(const dex::ProtoId& proto_id) const;
514 
GetProtoParameters(const dex::ProtoId & proto_id)515   const dex::TypeList* GetProtoParameters(const dex::ProtoId& proto_id) const {
516     return DataPointer<dex::TypeList>(proto_id.parameters_off_);
517   }
518 
GetEncodedStaticFieldValuesArray(const dex::ClassDef & class_def)519   const uint8_t* GetEncodedStaticFieldValuesArray(const dex::ClassDef& class_def) const {
520     return DataPointer<uint8_t>(class_def.static_values_off_);
521   }
522 
GetCallSiteEncodedValuesArray(const dex::CallSiteIdItem & call_site_id)523   const uint8_t* GetCallSiteEncodedValuesArray(const dex::CallSiteIdItem& call_site_id) const {
524     return DataBegin() + call_site_id.data_off_;
525   }
526 
527   dex::ProtoIndex GetProtoIndexForCallSite(uint32_t call_site_idx) const;
528 
529   static const dex::TryItem* GetTryItems(const DexInstructionIterator& code_item_end,
530                                          uint32_t offset);
531 
532   // Get the base of the encoded data for the given DexCode.
533   static const uint8_t* GetCatchHandlerData(const DexInstructionIterator& code_item_end,
534                                             uint32_t tries_size,
535                                             uint32_t offset);
536 
537   // Find which try region is associated with the given address (ie dex pc). Returns -1 if none.
538   static int32_t FindTryItem(const dex::TryItem* try_items, uint32_t tries_size, uint32_t address);
539 
540   // Get the pointer to the start of the debugging data
GetDebugInfoStream(uint32_t debug_info_off)541   const uint8_t* GetDebugInfoStream(uint32_t debug_info_off) const {
542     // Check that the offset is in bounds.
543     // Note that although the specification says that 0 should be used if there
544     // is no debug information, some applications incorrectly use 0xFFFFFFFF.
545     return (debug_info_off == 0 || debug_info_off >= data_size_)
546         ? nullptr
547         : DataBegin() + debug_info_off;
548   }
549 
550   struct PositionInfo {
551     PositionInfo() = default;
552 
553     uint32_t address_ = 0;  // In 16-bit code units.
554     uint32_t line_ = 0;  // Source code line number starting at 1.
555     const char* source_file_ = nullptr;  // nullptr if the file from ClassDef still applies.
556     bool prologue_end_ = false;
557     bool epilogue_begin_ = false;
558   };
559 
560   struct LocalInfo {
561     LocalInfo() = default;
562 
563     const char* name_ = nullptr;  // E.g., list.  It can be nullptr if unknown.
564     const char* descriptor_ = nullptr;  // E.g., Ljava/util/LinkedList;
565     const char* signature_ = nullptr;  // E.g., java.util.LinkedList<java.lang.Integer>
566     uint32_t start_address_ = 0;  // PC location where the local is first defined.
567     uint32_t end_address_ = 0;  // PC location where the local is no longer defined.
568     uint16_t reg_ = 0;  // Dex register which stores the values.
569     bool is_live_ = false;  // Is the local defined and live.
570   };
571 
572   // Callback for "new locals table entry".
573   typedef void (*DexDebugNewLocalCb)(void* context, const LocalInfo& entry);
574 
GetAnnotationsDirectory(const dex::ClassDef & class_def)575   const dex::AnnotationsDirectoryItem* GetAnnotationsDirectory(const dex::ClassDef& class_def)
576       const {
577     return DataPointer<dex::AnnotationsDirectoryItem>(class_def.annotations_off_);
578   }
579 
GetClassAnnotationSet(const dex::AnnotationsDirectoryItem * anno_dir)580   const dex::AnnotationSetItem* GetClassAnnotationSet(const dex::AnnotationsDirectoryItem* anno_dir)
581       const {
582     return DataPointer<dex::AnnotationSetItem>(anno_dir->class_annotations_off_);
583   }
584 
GetFieldAnnotations(const dex::AnnotationsDirectoryItem * anno_dir)585   const dex::FieldAnnotationsItem* GetFieldAnnotations(
586       const dex::AnnotationsDirectoryItem* anno_dir) const {
587     return (anno_dir->fields_size_ == 0)
588          ? nullptr
589          : reinterpret_cast<const dex::FieldAnnotationsItem*>(&anno_dir[1]);
590   }
591 
GetMethodAnnotations(const dex::AnnotationsDirectoryItem * anno_dir)592   const dex::MethodAnnotationsItem* GetMethodAnnotations(
593       const dex::AnnotationsDirectoryItem* anno_dir) const {
594     if (anno_dir->methods_size_ == 0) {
595       return nullptr;
596     }
597     // Skip past the header and field annotations.
598     const uint8_t* addr = reinterpret_cast<const uint8_t*>(&anno_dir[1]);
599     addr += anno_dir->fields_size_ * sizeof(dex::FieldAnnotationsItem);
600     return reinterpret_cast<const dex::MethodAnnotationsItem*>(addr);
601   }
602 
GetParameterAnnotations(const dex::AnnotationsDirectoryItem * anno_dir)603   const dex::ParameterAnnotationsItem* GetParameterAnnotations(
604       const dex::AnnotationsDirectoryItem* anno_dir) const {
605     if (anno_dir->parameters_size_ == 0) {
606       return nullptr;
607     }
608     // Skip past the header, field annotations, and method annotations.
609     const uint8_t* addr = reinterpret_cast<const uint8_t*>(&anno_dir[1]);
610     addr += anno_dir->fields_size_ * sizeof(dex::FieldAnnotationsItem);
611     addr += anno_dir->methods_size_ * sizeof(dex::MethodAnnotationsItem);
612     return reinterpret_cast<const dex::ParameterAnnotationsItem*>(addr);
613   }
614 
GetFieldAnnotationSetItem(const dex::FieldAnnotationsItem & anno_item)615   const dex::AnnotationSetItem* GetFieldAnnotationSetItem(
616       const dex::FieldAnnotationsItem& anno_item) const {
617     return DataPointer<dex::AnnotationSetItem>(anno_item.annotations_off_);
618   }
619 
GetMethodAnnotationSetItem(const dex::MethodAnnotationsItem & anno_item)620   const dex::AnnotationSetItem* GetMethodAnnotationSetItem(
621       const dex::MethodAnnotationsItem& anno_item) const {
622     return DataPointer<dex::AnnotationSetItem>(anno_item.annotations_off_);
623   }
624 
GetParameterAnnotationSetRefList(const dex::ParameterAnnotationsItem * anno_item)625   const dex::AnnotationSetRefList* GetParameterAnnotationSetRefList(
626       const dex::ParameterAnnotationsItem* anno_item) const {
627     return DataPointer<dex::AnnotationSetRefList>(anno_item->annotations_off_);
628   }
629 
GetAnnotationItemAtOffset(uint32_t offset)630   ALWAYS_INLINE const dex::AnnotationItem* GetAnnotationItemAtOffset(uint32_t offset) const {
631     return DataPointer<dex::AnnotationItem>(offset);
632   }
633 
GetHiddenapiClassDataAtOffset(uint32_t offset)634   ALWAYS_INLINE const dex::HiddenapiClassData* GetHiddenapiClassDataAtOffset(uint32_t offset)
635       const {
636     return DataPointer<dex::HiddenapiClassData>(offset);
637   }
638 
GetHiddenapiClassData()639   ALWAYS_INLINE const dex::HiddenapiClassData* GetHiddenapiClassData() const {
640     return hiddenapi_class_data_;
641   }
642 
HasHiddenapiClassData()643   ALWAYS_INLINE bool HasHiddenapiClassData() const {
644     return hiddenapi_class_data_ != nullptr;
645   }
646 
GetAnnotationItem(const dex::AnnotationSetItem * set_item,uint32_t index)647   const dex::AnnotationItem* GetAnnotationItem(const dex::AnnotationSetItem* set_item,
648                                                uint32_t index) const {
649     DCHECK_LE(index, set_item->size_);
650     return GetAnnotationItemAtOffset(set_item->entries_[index]);
651   }
652 
GetSetRefItemItem(const dex::AnnotationSetRefItem * anno_item)653   const dex::AnnotationSetItem* GetSetRefItemItem(const dex::AnnotationSetRefItem* anno_item)
654       const {
655     return DataPointer<dex::AnnotationSetItem>(anno_item->annotations_off_);
656   }
657 
658   // Debug info opcodes and constants
659   enum {
660     DBG_END_SEQUENCE         = 0x00,
661     DBG_ADVANCE_PC           = 0x01,
662     DBG_ADVANCE_LINE         = 0x02,
663     DBG_START_LOCAL          = 0x03,
664     DBG_START_LOCAL_EXTENDED = 0x04,
665     DBG_END_LOCAL            = 0x05,
666     DBG_RESTART_LOCAL        = 0x06,
667     DBG_SET_PROLOGUE_END     = 0x07,
668     DBG_SET_EPILOGUE_BEGIN   = 0x08,
669     DBG_SET_FILE             = 0x09,
670     DBG_FIRST_SPECIAL        = 0x0a,
671     DBG_LINE_BASE            = -4,
672     DBG_LINE_RANGE           = 15,
673   };
674 
675   // Returns false if there is no debugging information or if it cannot be decoded.
676   template<typename NewLocalCallback, typename IndexToStringData, typename TypeIndexToStringData>
677   static bool DecodeDebugLocalInfo(const uint8_t* stream,
678                                    const std::string& location,
679                                    const char* declaring_class_descriptor,
680                                    const std::vector<const char*>& arg_descriptors,
681                                    const std::string& method_name,
682                                    bool is_static,
683                                    uint16_t registers_size,
684                                    uint16_t ins_size,
685                                    uint16_t insns_size_in_code_units,
686                                    const IndexToStringData& index_to_string_data,
687                                    const TypeIndexToStringData& type_index_to_string_data,
688                                    const NewLocalCallback& new_local) NO_THREAD_SAFETY_ANALYSIS;
689   template<typename NewLocalCallback>
690   bool DecodeDebugLocalInfo(uint32_t registers_size,
691                             uint32_t ins_size,
692                             uint32_t insns_size_in_code_units,
693                             uint32_t debug_info_offset,
694                             bool is_static,
695                             uint32_t method_idx,
696                             const NewLocalCallback& new_local) const;
697 
698   // Returns false if there is no debugging information or if it cannot be decoded.
699   template<typename DexDebugNewPosition, typename IndexToStringData>
700   static bool DecodeDebugPositionInfo(const uint8_t* stream,
701                                       const IndexToStringData& index_to_string_data,
702                                       const DexDebugNewPosition& position_functor);
703 
GetSourceFile(const dex::ClassDef & class_def)704   const char* GetSourceFile(const dex::ClassDef& class_def) const {
705     if (!class_def.source_file_idx_.IsValid()) {
706       return nullptr;
707     } else {
708       return StringDataByIdx(class_def.source_file_idx_);
709     }
710   }
711 
712   int GetPermissions() const;
713 
714   bool IsReadOnly() const;
715 
716   bool EnableWrite() const;
717 
718   bool DisableWrite() const;
719 
Begin()720   const uint8_t* Begin() const {
721     return begin_;
722   }
723 
Size()724   size_t Size() const {
725     return size_;
726   }
727 
DataBegin()728   const uint8_t* DataBegin() const {
729     return data_begin_;
730   }
731 
DataSize()732   size_t DataSize() const {
733     return data_size_;
734   }
735 
736   template <typename T>
DataPointer(size_t offset)737   const T* DataPointer(size_t offset) const {
738     DCHECK_LT(offset, DataSize()) << "Offset past end of data section";
739     return (offset != 0u) ? reinterpret_cast<const T*>(DataBegin() + offset) : nullptr;
740   }
741 
GetOatDexFile()742   const OatDexFile* GetOatDexFile() const {
743     return oat_dex_file_;
744   }
745 
746   // Used by oat writer.
SetOatDexFile(const OatDexFile * oat_dex_file)747   void SetOatDexFile(const OatDexFile* oat_dex_file) const {
748     oat_dex_file_ = oat_dex_file;
749   }
750 
751   // Read MapItems and validate/set remaining offsets.
GetMapList()752   const dex::MapList* GetMapList() const {
753     return reinterpret_cast<const dex::MapList*>(DataBegin() + header_->map_off_);
754   }
755 
756   // Utility methods for reading integral values from a buffer.
757   static int32_t ReadSignedInt(const uint8_t* ptr, int zwidth);
758   static uint32_t ReadUnsignedInt(const uint8_t* ptr, int zwidth, bool fill_on_right);
759   static int64_t ReadSignedLong(const uint8_t* ptr, int zwidth);
760   static uint64_t ReadUnsignedLong(const uint8_t* ptr, int zwidth, bool fill_on_right);
761 
762   // Recalculates the checksum of the dex file. Does not use the current value in the header.
763   virtual uint32_t CalculateChecksum() const;
764   static uint32_t CalculateChecksum(const uint8_t* begin, size_t size);
765   static uint32_t ChecksumMemoryRange(const uint8_t* begin, size_t size);
766 
767   // Number of bytes at the beginning of the dex file header which are skipped
768   // when computing the adler32 checksum of the entire file.
769   static constexpr uint32_t kNumNonChecksumBytes = OFFSETOF_MEMBER(DexFile::Header, signature_);
770 
771   // Appends a human-readable form of the method at an index.
772   void AppendPrettyMethod(uint32_t method_idx, bool with_signature, std::string* result) const;
773   // Returns a human-readable form of the field at an index.
774   std::string PrettyField(uint32_t field_idx, bool with_type = true) const;
775   // Returns a human-readable form of the type at an index.
776   std::string PrettyType(dex::TypeIndex type_idx) const;
777 
778   ALWAYS_INLINE std::string PrettyMethod(uint32_t method_idx, bool with_signature = true) const {
779     std::string result;
780     AppendPrettyMethod(method_idx, with_signature, &result);
781     return result;
782   }
783 
784   // Not virtual for performance reasons.
IsCompactDexFile()785   ALWAYS_INLINE bool IsCompactDexFile() const {
786     return is_compact_dex_;
787   }
IsStandardDexFile()788   ALWAYS_INLINE bool IsStandardDexFile() const {
789     return !is_compact_dex_;
790   }
791   ALWAYS_INLINE const StandardDexFile* AsStandardDexFile() const;
792   ALWAYS_INLINE const CompactDexFile* AsCompactDexFile() const;
793 
GetHiddenapiDomain()794   hiddenapi::Domain GetHiddenapiDomain() const { return hiddenapi_domain_; }
SetHiddenapiDomain(hiddenapi::Domain value)795   void SetHiddenapiDomain(hiddenapi::Domain value) const { hiddenapi_domain_ = value; }
796 
IsInMainSection(const void * addr)797   bool IsInMainSection(const void* addr) const {
798     return Begin() <= addr && addr < Begin() + Size();
799   }
800 
IsInDataSection(const void * addr)801   bool IsInDataSection(const void* addr) const {
802     return DataBegin() <= addr && addr < DataBegin() + DataSize();
803   }
804 
GetContainer()805   DexFileContainer* GetContainer() const {
806     return container_.get();
807   }
808 
809   IterationRange<ClassIterator> GetClasses() const;
810 
811   template <typename Visitor>
812   static uint32_t DecodeDebugInfoParameterNames(const uint8_t** debug_info,
813                                                 const Visitor& visitor);
814 
815   static inline bool StringEquals(const DexFile* df1, dex::StringIndex sidx1,
816                                   const DexFile* df2, dex::StringIndex sidx2);
817 
818  protected:
819   // First Dex format version supporting default methods.
820   static constexpr uint32_t kDefaultMethodsVersion = 37;
821 
822   DexFile(const uint8_t* base,
823           size_t size,
824           const uint8_t* data_begin,
825           size_t data_size,
826           const std::string& location,
827           uint32_t location_checksum,
828           const OatDexFile* oat_dex_file,
829           std::unique_ptr<DexFileContainer> container,
830           bool is_compact_dex);
831 
832   // Top-level initializer that calls other Init methods.
833   bool Init(std::string* error_msg);
834 
835   // Returns true if the header magic and version numbers are of the expected values.
836   bool CheckMagicAndVersion(std::string* error_msg) const;
837 
838   // Initialize section info for sections only found in map. Returns true on success.
839   void InitializeSectionsFromMapList();
840 
841   // The base address of the memory mapping.
842   const uint8_t* const begin_;
843 
844   // The size of the underlying memory allocation in bytes.
845   const size_t size_;
846 
847   // The base address of the data section (same as Begin() for standard dex).
848   const uint8_t* const data_begin_;
849 
850   // The size of the data section.
851   const size_t data_size_;
852 
853   // Typically the dex file name when available, alternatively some identifying string.
854   //
855   // The ClassLinker will use this to match DexFiles the boot class
856   // path to DexCache::GetLocation when loading from an image.
857   const std::string location_;
858 
859   const uint32_t location_checksum_;
860 
861   // Points to the header section.
862   const Header* const header_;
863 
864   // Points to the base of the string identifier list.
865   const dex::StringId* const string_ids_;
866 
867   // Points to the base of the type identifier list.
868   const dex::TypeId* const type_ids_;
869 
870   // Points to the base of the field identifier list.
871   const dex::FieldId* const field_ids_;
872 
873   // Points to the base of the method identifier list.
874   const dex::MethodId* const method_ids_;
875 
876   // Points to the base of the prototype identifier list.
877   const dex::ProtoId* const proto_ids_;
878 
879   // Points to the base of the class definition list.
880   const dex::ClassDef* const class_defs_;
881 
882   // Points to the base of the method handles list.
883   const dex::MethodHandleItem* method_handles_;
884 
885   // Number of elements in the method handles list.
886   size_t num_method_handles_;
887 
888   // Points to the base of the call sites id list.
889   const dex::CallSiteIdItem* call_site_ids_;
890 
891   // Number of elements in the call sites list.
892   size_t num_call_site_ids_;
893 
894   // Points to the base of the hiddenapi class data item_, or nullptr if the dex
895   // file does not have one.
896   const dex::HiddenapiClassData* hiddenapi_class_data_;
897 
898   // If this dex file was loaded from an oat file, oat_dex_file_ contains a
899   // pointer to the OatDexFile it was loaded from. Otherwise oat_dex_file_ is
900   // null.
901   mutable const OatDexFile* oat_dex_file_;
902 
903   // Manages the underlying memory allocation.
904   std::unique_ptr<DexFileContainer> container_;
905 
906   // If the dex file is a compact dex file. If false then the dex file is a standard dex file.
907   const bool is_compact_dex_;
908 
909   // The domain this dex file belongs to for hidden API access checks.
910   // It is decleared `mutable` because the domain is assigned after the DexFile
911   // has been created and can be changed later by the runtime.
912   mutable hiddenapi::Domain hiddenapi_domain_;
913 
914   friend class DexFileLoader;
915   friend class DexFileVerifierTest;
916   friend class OatWriter;
917 };
918 
919 std::ostream& operator<<(std::ostream& os, const DexFile& dex_file);
920 
921 // Iterate over a dex file's ProtoId's paramters
922 class DexFileParameterIterator {
923  public:
DexFileParameterIterator(const DexFile & dex_file,const dex::ProtoId & proto_id)924   DexFileParameterIterator(const DexFile& dex_file, const dex::ProtoId& proto_id)
925       : dex_file_(dex_file) {
926     type_list_ = dex_file_.GetProtoParameters(proto_id);
927     if (type_list_ != nullptr) {
928       size_ = type_list_->Size();
929     }
930   }
HasNext()931   bool HasNext() const { return pos_ < size_; }
Size()932   size_t Size() const { return size_; }
Next()933   void Next() { ++pos_; }
GetTypeIdx()934   dex::TypeIndex GetTypeIdx() {
935     return type_list_->GetTypeItem(pos_).type_idx_;
936   }
GetDescriptor()937   const char* GetDescriptor() {
938     return dex_file_.StringByTypeIdx(dex::TypeIndex(GetTypeIdx()));
939   }
940  private:
941   const DexFile& dex_file_;
942   const dex::TypeList* type_list_ = nullptr;
943   uint32_t size_ = 0;
944   uint32_t pos_ = 0;
945   DISALLOW_IMPLICIT_CONSTRUCTORS(DexFileParameterIterator);
946 };
947 
948 class EncodedArrayValueIterator {
949  public:
950   EncodedArrayValueIterator(const DexFile& dex_file, const uint8_t* array_data);
951 
HasNext()952   bool HasNext() const { return pos_ < array_size_; }
953 
954   void Next();
955 
956   enum ValueType {
957     kByte         = 0x00,
958     kShort        = 0x02,
959     kChar         = 0x03,
960     kInt          = 0x04,
961     kLong         = 0x06,
962     kFloat        = 0x10,
963     kDouble       = 0x11,
964     kMethodType   = 0x15,
965     kMethodHandle = 0x16,
966     kString       = 0x17,
967     kType         = 0x18,
968     kField        = 0x19,
969     kMethod       = 0x1a,
970     kEnum         = 0x1b,
971     kArray        = 0x1c,
972     kAnnotation   = 0x1d,
973     kNull         = 0x1e,
974     kBoolean      = 0x1f,
975   };
976 
GetValueType()977   ValueType GetValueType() const { return type_; }
GetJavaValue()978   const jvalue& GetJavaValue() const { return jval_; }
979 
980  protected:
981   static constexpr uint8_t kEncodedValueTypeMask = 0x1f;  // 0b11111
982   static constexpr uint8_t kEncodedValueArgShift = 5;
983 
984   const DexFile& dex_file_;
985   size_t array_size_;  // Size of array.
986   size_t pos_;  // Current position.
987   const uint8_t* ptr_;  // Pointer into encoded data array.
988   ValueType type_;  // Type of current encoded value.
989   jvalue jval_;  // Value of current encoded value.
990 
991  private:
992   DISALLOW_IMPLICIT_CONSTRUCTORS(EncodedArrayValueIterator);
993 };
994 std::ostream& operator<<(std::ostream& os, EncodedArrayValueIterator::ValueType code);
995 
996 class EncodedStaticFieldValueIterator : public EncodedArrayValueIterator {
997  public:
EncodedStaticFieldValueIterator(const DexFile & dex_file,const dex::ClassDef & class_def)998   EncodedStaticFieldValueIterator(const DexFile& dex_file,
999                                   const dex::ClassDef& class_def)
1000       : EncodedArrayValueIterator(dex_file,
1001                                   dex_file.GetEncodedStaticFieldValuesArray(class_def))
1002   {}
1003 
1004  private:
1005   DISALLOW_IMPLICIT_CONSTRUCTORS(EncodedStaticFieldValueIterator);
1006 };
1007 
1008 class CallSiteArrayValueIterator : public EncodedArrayValueIterator {
1009  public:
CallSiteArrayValueIterator(const DexFile & dex_file,const dex::CallSiteIdItem & call_site_id)1010   CallSiteArrayValueIterator(const DexFile& dex_file,
1011                              const dex::CallSiteIdItem& call_site_id)
1012       : EncodedArrayValueIterator(dex_file,
1013                                   dex_file.GetCallSiteEncodedValuesArray(call_site_id))
1014   {}
1015 
Size()1016   uint32_t Size() const { return array_size_; }
1017 
1018  private:
1019   DISALLOW_IMPLICIT_CONSTRUCTORS(CallSiteArrayValueIterator);
1020 };
1021 
1022 }  // namespace art
1023 
1024 #endif  // ART_LIBDEXFILE_DEX_DEX_FILE_H_
1025