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 
311   // Looks up a type for the given string index
312   const dex::TypeId* FindTypeId(dex::StringIndex string_idx) const;
313 
314   // Returns the number of field identifiers in the .dex file.
NumFieldIds()315   size_t NumFieldIds() const {
316     DCHECK(header_ != nullptr) << GetLocation();
317     return header_->field_ids_size_;
318   }
319 
320   // Returns the FieldId at the specified index.
GetFieldId(uint32_t idx)321   const dex::FieldId& GetFieldId(uint32_t idx) const {
322     DCHECK_LT(idx, NumFieldIds()) << GetLocation();
323     return field_ids_[idx];
324   }
325 
GetIndexForFieldId(const dex::FieldId & field_id)326   uint32_t GetIndexForFieldId(const dex::FieldId& field_id) const {
327     CHECK_GE(&field_id, field_ids_) << GetLocation();
328     CHECK_LT(&field_id, field_ids_ + header_->field_ids_size_) << GetLocation();
329     return &field_id - field_ids_;
330   }
331 
332   // Looks up a field by its declaring class, name and type
333   const dex::FieldId* FindFieldId(const dex::TypeId& declaring_klass,
334                                   const dex::StringId& name,
335                                   const dex::TypeId& type) const;
336 
337   // Return the code-item offset associated with the class and method or nullopt
338   // if the method does not exist or has no code.
339   std::optional<uint32_t> GetCodeItemOffset(const dex::ClassDef& class_def,
340                                             uint32_t dex_method_idx) const;
341 
342   // Return the code-item offset associated with the class and method or
343   // LOG(FATAL) if the method does not exist or has no code.
344   uint32_t FindCodeItemOffset(const dex::ClassDef& class_def,
345                               uint32_t dex_method_idx) const;
346 
347   virtual uint32_t GetCodeItemSize(const dex::CodeItem& disk_code_item) const = 0;
348 
349   // Returns the declaring class descriptor string of a field id.
GetFieldDeclaringClassDescriptor(const dex::FieldId & field_id)350   const char* GetFieldDeclaringClassDescriptor(const dex::FieldId& field_id) const {
351     const dex::TypeId& type_id = GetTypeId(field_id.class_idx_);
352     return GetTypeDescriptor(type_id);
353   }
354 
355   // Returns the class descriptor string of a field id.
356   const char* GetFieldTypeDescriptor(const dex::FieldId& field_id) const;
357 
358   // Returns the name of a field id.
359   const char* GetFieldName(const dex::FieldId& field_id) const;
360 
361   // Returns the number of method identifiers in the .dex file.
NumMethodIds()362   size_t NumMethodIds() const {
363     DCHECK(header_ != nullptr) << GetLocation();
364     return header_->method_ids_size_;
365   }
366 
367   // Returns the MethodId at the specified index.
GetMethodId(uint32_t idx)368   const dex::MethodId& GetMethodId(uint32_t idx) const {
369     DCHECK_LT(idx, NumMethodIds()) << GetLocation();
370     return method_ids_[idx];
371   }
372 
GetIndexForMethodId(const dex::MethodId & method_id)373   uint32_t GetIndexForMethodId(const dex::MethodId& method_id) const {
374     CHECK_GE(&method_id, method_ids_) << GetLocation();
375     CHECK_LT(&method_id, method_ids_ + header_->method_ids_size_) << GetLocation();
376     return &method_id - method_ids_;
377   }
378 
379   // Looks up a method by its declaring class, name and proto_id
380   const dex::MethodId* FindMethodId(const dex::TypeId& declaring_klass,
381                                     const dex::StringId& name,
382                                     const dex::ProtoId& signature) const;
383 
384   const dex::MethodId* FindMethodIdByIndex(dex::TypeIndex declaring_klass,
385                                            dex::StringIndex name,
386                                            dex::ProtoIndex signature) const;
387 
388   // Returns the declaring class descriptor string of a method id.
389   const char* GetMethodDeclaringClassDescriptor(const dex::MethodId& method_id) const;
390 
391   // Returns the prototype of a method id.
GetMethodPrototype(const dex::MethodId & method_id)392   const dex::ProtoId& GetMethodPrototype(const dex::MethodId& method_id) const {
393     return GetProtoId(method_id.proto_idx_);
394   }
395 
396   // Returns a representation of the signature of a method id.
397   const Signature GetMethodSignature(const dex::MethodId& method_id) const;
398 
399   // Returns a representation of the signature of a proto id.
400   const Signature GetProtoSignature(const dex::ProtoId& proto_id) const;
401 
402   // Returns the name of a method id.
403   const char* GetMethodName(const dex::MethodId& method_id) const;
404   const char* GetMethodName(const dex::MethodId& method_id, uint32_t* utf_length) const;
405   const char* GetMethodName(uint32_t idx) const;
406   const char* GetMethodName(uint32_t idx, uint32_t* utf_length) const;
407 
408   // Returns the shorty of a method by its index.
409   const char* GetMethodShorty(uint32_t idx) const;
410 
411   // Returns the shorty of a method id.
412   const char* GetMethodShorty(const dex::MethodId& method_id) const;
413   const char* GetMethodShorty(const dex::MethodId& method_id, uint32_t* length) const;
414 
415   // Returns the number of class definitions in the .dex file.
NumClassDefs()416   uint32_t NumClassDefs() const {
417     DCHECK(header_ != nullptr) << GetLocation();
418     return header_->class_defs_size_;
419   }
420 
421   // Returns the ClassDef at the specified index.
GetClassDef(uint16_t idx)422   const dex::ClassDef& GetClassDef(uint16_t idx) const {
423     DCHECK_LT(idx, NumClassDefs()) << GetLocation();
424     return class_defs_[idx];
425   }
426 
GetIndexForClassDef(const dex::ClassDef & class_def)427   uint16_t GetIndexForClassDef(const dex::ClassDef& class_def) const {
428     CHECK_GE(&class_def, class_defs_) << GetLocation();
429     CHECK_LT(&class_def, class_defs_ + header_->class_defs_size_) << GetLocation();
430     return &class_def - class_defs_;
431   }
432 
433   // Returns the class descriptor string of a class definition.
434   const char* GetClassDescriptor(const dex::ClassDef& class_def) const;
435 
436   // Looks up a class definition by its type index.
437   const dex::ClassDef* FindClassDef(dex::TypeIndex type_idx) const;
438 
GetInterfacesList(const dex::ClassDef & class_def)439   const dex::TypeList* GetInterfacesList(const dex::ClassDef& class_def) const {
440     return DataPointer<dex::TypeList>(class_def.interfaces_off_);
441   }
442 
NumMethodHandles()443   uint32_t NumMethodHandles() const {
444     return num_method_handles_;
445   }
446 
GetMethodHandle(uint32_t idx)447   const dex::MethodHandleItem& GetMethodHandle(uint32_t idx) const {
448     CHECK_LT(idx, NumMethodHandles());
449     return method_handles_[idx];
450   }
451 
NumCallSiteIds()452   uint32_t NumCallSiteIds() const {
453     return num_call_site_ids_;
454   }
455 
GetCallSiteId(uint32_t idx)456   const dex::CallSiteIdItem& GetCallSiteId(uint32_t idx) const {
457     CHECK_LT(idx, NumCallSiteIds());
458     return call_site_ids_[idx];
459   }
460 
461   // Returns a pointer to the raw memory mapped class_data_item
GetClassData(const dex::ClassDef & class_def)462   const uint8_t* GetClassData(const dex::ClassDef& class_def) const {
463     return DataPointer<uint8_t>(class_def.class_data_off_);
464   }
465 
466   // Return the code item for a provided offset.
GetCodeItem(const uint32_t code_off)467   const dex::CodeItem* GetCodeItem(const uint32_t code_off) const {
468     // May be null for native or abstract methods.
469     return DataPointer<dex::CodeItem>(code_off);
470   }
471 
472   const char* GetReturnTypeDescriptor(const dex::ProtoId& proto_id) const;
473 
474   // Returns the number of prototype identifiers in the .dex file.
NumProtoIds()475   size_t NumProtoIds() const {
476     DCHECK(header_ != nullptr) << GetLocation();
477     return header_->proto_ids_size_;
478   }
479 
480   // Returns the ProtoId at the specified index.
GetProtoId(dex::ProtoIndex idx)481   const dex::ProtoId& GetProtoId(dex::ProtoIndex idx) const {
482     DCHECK_LT(idx.index_, NumProtoIds()) << GetLocation();
483     return proto_ids_[idx.index_];
484   }
485 
GetIndexForProtoId(const dex::ProtoId & proto_id)486   dex::ProtoIndex GetIndexForProtoId(const dex::ProtoId& proto_id) const {
487     CHECK_GE(&proto_id, proto_ids_) << GetLocation();
488     CHECK_LT(&proto_id, proto_ids_ + header_->proto_ids_size_) << GetLocation();
489     return dex::ProtoIndex(&proto_id - proto_ids_);
490   }
491 
492   // Looks up a proto id for a given return type and signature type list
493   const dex::ProtoId* FindProtoId(dex::TypeIndex return_type_idx,
494                                   const dex::TypeIndex* signature_type_idxs,
495                              uint32_t signature_length) const;
FindProtoId(dex::TypeIndex return_type_idx,const std::vector<dex::TypeIndex> & signature_type_idxs)496   const dex::ProtoId* FindProtoId(dex::TypeIndex return_type_idx,
497                                   const std::vector<dex::TypeIndex>& signature_type_idxs) const {
498     return FindProtoId(return_type_idx, &signature_type_idxs[0], signature_type_idxs.size());
499   }
500 
501   // Given a signature place the type ids into the given vector, returns true on success
502   bool CreateTypeList(std::string_view signature,
503                       dex::TypeIndex* return_type_idx,
504                       std::vector<dex::TypeIndex>* param_type_idxs) const;
505 
506   // Returns the short form method descriptor for the given prototype.
507   const char* GetShorty(dex::ProtoIndex proto_idx) const;
508 
GetProtoParameters(const dex::ProtoId & proto_id)509   const dex::TypeList* GetProtoParameters(const dex::ProtoId& proto_id) const {
510     return DataPointer<dex::TypeList>(proto_id.parameters_off_);
511   }
512 
GetEncodedStaticFieldValuesArray(const dex::ClassDef & class_def)513   const uint8_t* GetEncodedStaticFieldValuesArray(const dex::ClassDef& class_def) const {
514     return DataPointer<uint8_t>(class_def.static_values_off_);
515   }
516 
GetCallSiteEncodedValuesArray(const dex::CallSiteIdItem & call_site_id)517   const uint8_t* GetCallSiteEncodedValuesArray(const dex::CallSiteIdItem& call_site_id) const {
518     return DataBegin() + call_site_id.data_off_;
519   }
520 
521   dex::ProtoIndex GetProtoIndexForCallSite(uint32_t call_site_idx) const;
522 
523   static const dex::TryItem* GetTryItems(const DexInstructionIterator& code_item_end,
524                                          uint32_t offset);
525 
526   // Get the base of the encoded data for the given DexCode.
527   static const uint8_t* GetCatchHandlerData(const DexInstructionIterator& code_item_end,
528                                             uint32_t tries_size,
529                                             uint32_t offset);
530 
531   // Find which try region is associated with the given address (ie dex pc). Returns -1 if none.
532   static int32_t FindTryItem(const dex::TryItem* try_items, uint32_t tries_size, uint32_t address);
533 
534   // Get the pointer to the start of the debugging data
GetDebugInfoStream(uint32_t debug_info_off)535   const uint8_t* GetDebugInfoStream(uint32_t debug_info_off) const {
536     // Check that the offset is in bounds.
537     // Note that although the specification says that 0 should be used if there
538     // is no debug information, some applications incorrectly use 0xFFFFFFFF.
539     return (debug_info_off == 0 || debug_info_off >= data_size_)
540         ? nullptr
541         : DataBegin() + debug_info_off;
542   }
543 
544   struct PositionInfo {
545     PositionInfo() = default;
546 
547     uint32_t address_ = 0;  // In 16-bit code units.
548     uint32_t line_ = 0;  // Source code line number starting at 1.
549     const char* source_file_ = nullptr;  // nullptr if the file from ClassDef still applies.
550     bool prologue_end_ = false;
551     bool epilogue_begin_ = false;
552   };
553 
554   struct LocalInfo {
555     LocalInfo() = default;
556 
557     const char* name_ = nullptr;  // E.g., list.  It can be nullptr if unknown.
558     const char* descriptor_ = nullptr;  // E.g., Ljava/util/LinkedList;
559     const char* signature_ = nullptr;  // E.g., java.util.LinkedList<java.lang.Integer>
560     uint32_t start_address_ = 0;  // PC location where the local is first defined.
561     uint32_t end_address_ = 0;  // PC location where the local is no longer defined.
562     uint16_t reg_ = 0;  // Dex register which stores the values.
563     bool is_live_ = false;  // Is the local defined and live.
564   };
565 
566   // Callback for "new locals table entry".
567   typedef void (*DexDebugNewLocalCb)(void* context, const LocalInfo& entry);
568 
GetAnnotationsDirectory(const dex::ClassDef & class_def)569   const dex::AnnotationsDirectoryItem* GetAnnotationsDirectory(const dex::ClassDef& class_def)
570       const {
571     return DataPointer<dex::AnnotationsDirectoryItem>(class_def.annotations_off_);
572   }
573 
GetClassAnnotationSet(const dex::AnnotationsDirectoryItem * anno_dir)574   const dex::AnnotationSetItem* GetClassAnnotationSet(const dex::AnnotationsDirectoryItem* anno_dir)
575       const {
576     return DataPointer<dex::AnnotationSetItem>(anno_dir->class_annotations_off_);
577   }
578 
GetFieldAnnotations(const dex::AnnotationsDirectoryItem * anno_dir)579   const dex::FieldAnnotationsItem* GetFieldAnnotations(
580       const dex::AnnotationsDirectoryItem* anno_dir) const {
581     return (anno_dir->fields_size_ == 0)
582          ? nullptr
583          : reinterpret_cast<const dex::FieldAnnotationsItem*>(&anno_dir[1]);
584   }
585 
GetMethodAnnotations(const dex::AnnotationsDirectoryItem * anno_dir)586   const dex::MethodAnnotationsItem* GetMethodAnnotations(
587       const dex::AnnotationsDirectoryItem* anno_dir) const {
588     if (anno_dir->methods_size_ == 0) {
589       return nullptr;
590     }
591     // Skip past the header and field annotations.
592     const uint8_t* addr = reinterpret_cast<const uint8_t*>(&anno_dir[1]);
593     addr += anno_dir->fields_size_ * sizeof(dex::FieldAnnotationsItem);
594     return reinterpret_cast<const dex::MethodAnnotationsItem*>(addr);
595   }
596 
GetParameterAnnotations(const dex::AnnotationsDirectoryItem * anno_dir)597   const dex::ParameterAnnotationsItem* GetParameterAnnotations(
598       const dex::AnnotationsDirectoryItem* anno_dir) const {
599     if (anno_dir->parameters_size_ == 0) {
600       return nullptr;
601     }
602     // Skip past the header, field annotations, and method annotations.
603     const uint8_t* addr = reinterpret_cast<const uint8_t*>(&anno_dir[1]);
604     addr += anno_dir->fields_size_ * sizeof(dex::FieldAnnotationsItem);
605     addr += anno_dir->methods_size_ * sizeof(dex::MethodAnnotationsItem);
606     return reinterpret_cast<const dex::ParameterAnnotationsItem*>(addr);
607   }
608 
GetFieldAnnotationSetItem(const dex::FieldAnnotationsItem & anno_item)609   const dex::AnnotationSetItem* GetFieldAnnotationSetItem(
610       const dex::FieldAnnotationsItem& anno_item) const {
611     return DataPointer<dex::AnnotationSetItem>(anno_item.annotations_off_);
612   }
613 
GetMethodAnnotationSetItem(const dex::MethodAnnotationsItem & anno_item)614   const dex::AnnotationSetItem* GetMethodAnnotationSetItem(
615       const dex::MethodAnnotationsItem& anno_item) const {
616     return DataPointer<dex::AnnotationSetItem>(anno_item.annotations_off_);
617   }
618 
GetParameterAnnotationSetRefList(const dex::ParameterAnnotationsItem * anno_item)619   const dex::AnnotationSetRefList* GetParameterAnnotationSetRefList(
620       const dex::ParameterAnnotationsItem* anno_item) const {
621     return DataPointer<dex::AnnotationSetRefList>(anno_item->annotations_off_);
622   }
623 
GetAnnotationItemAtOffset(uint32_t offset)624   ALWAYS_INLINE const dex::AnnotationItem* GetAnnotationItemAtOffset(uint32_t offset) const {
625     return DataPointer<dex::AnnotationItem>(offset);
626   }
627 
GetHiddenapiClassDataAtOffset(uint32_t offset)628   ALWAYS_INLINE const dex::HiddenapiClassData* GetHiddenapiClassDataAtOffset(uint32_t offset)
629       const {
630     return DataPointer<dex::HiddenapiClassData>(offset);
631   }
632 
GetHiddenapiClassData()633   ALWAYS_INLINE const dex::HiddenapiClassData* GetHiddenapiClassData() const {
634     return hiddenapi_class_data_;
635   }
636 
HasHiddenapiClassData()637   ALWAYS_INLINE bool HasHiddenapiClassData() const {
638     return hiddenapi_class_data_ != nullptr;
639   }
640 
GetAnnotationItem(const dex::AnnotationSetItem * set_item,uint32_t index)641   const dex::AnnotationItem* GetAnnotationItem(const dex::AnnotationSetItem* set_item,
642                                                uint32_t index) const {
643     DCHECK_LE(index, set_item->size_);
644     return GetAnnotationItemAtOffset(set_item->entries_[index]);
645   }
646 
GetSetRefItemItem(const dex::AnnotationSetRefItem * anno_item)647   const dex::AnnotationSetItem* GetSetRefItemItem(const dex::AnnotationSetRefItem* anno_item)
648       const {
649     return DataPointer<dex::AnnotationSetItem>(anno_item->annotations_off_);
650   }
651 
652   // Debug info opcodes and constants
653   enum {
654     DBG_END_SEQUENCE         = 0x00,
655     DBG_ADVANCE_PC           = 0x01,
656     DBG_ADVANCE_LINE         = 0x02,
657     DBG_START_LOCAL          = 0x03,
658     DBG_START_LOCAL_EXTENDED = 0x04,
659     DBG_END_LOCAL            = 0x05,
660     DBG_RESTART_LOCAL        = 0x06,
661     DBG_SET_PROLOGUE_END     = 0x07,
662     DBG_SET_EPILOGUE_BEGIN   = 0x08,
663     DBG_SET_FILE             = 0x09,
664     DBG_FIRST_SPECIAL        = 0x0a,
665     DBG_LINE_BASE            = -4,
666     DBG_LINE_RANGE           = 15,
667   };
668 
669   // Returns false if there is no debugging information or if it cannot be decoded.
670   template<typename NewLocalCallback, typename IndexToStringData, typename TypeIndexToStringData>
671   static bool DecodeDebugLocalInfo(const uint8_t* stream,
672                                    const std::string& location,
673                                    const char* declaring_class_descriptor,
674                                    const std::vector<const char*>& arg_descriptors,
675                                    const std::string& method_name,
676                                    bool is_static,
677                                    uint16_t registers_size,
678                                    uint16_t ins_size,
679                                    uint16_t insns_size_in_code_units,
680                                    const IndexToStringData& index_to_string_data,
681                                    const TypeIndexToStringData& type_index_to_string_data,
682                                    const NewLocalCallback& new_local) NO_THREAD_SAFETY_ANALYSIS;
683   template<typename NewLocalCallback>
684   bool DecodeDebugLocalInfo(uint32_t registers_size,
685                             uint32_t ins_size,
686                             uint32_t insns_size_in_code_units,
687                             uint32_t debug_info_offset,
688                             bool is_static,
689                             uint32_t method_idx,
690                             const NewLocalCallback& new_local) const;
691 
692   // Returns false if there is no debugging information or if it cannot be decoded.
693   template<typename DexDebugNewPosition, typename IndexToStringData>
694   static bool DecodeDebugPositionInfo(const uint8_t* stream,
695                                       const IndexToStringData& index_to_string_data,
696                                       const DexDebugNewPosition& position_functor);
697 
GetSourceFile(const dex::ClassDef & class_def)698   const char* GetSourceFile(const dex::ClassDef& class_def) const {
699     if (!class_def.source_file_idx_.IsValid()) {
700       return nullptr;
701     } else {
702       return StringDataByIdx(class_def.source_file_idx_);
703     }
704   }
705 
706   int GetPermissions() const;
707 
708   bool IsReadOnly() const;
709 
710   bool EnableWrite() const;
711 
712   bool DisableWrite() const;
713 
Begin()714   const uint8_t* Begin() const {
715     return begin_;
716   }
717 
Size()718   size_t Size() const {
719     return size_;
720   }
721 
DataBegin()722   const uint8_t* DataBegin() const {
723     return data_begin_;
724   }
725 
DataSize()726   size_t DataSize() const {
727     return data_size_;
728   }
729 
730   template <typename T>
DataPointer(size_t offset)731   const T* DataPointer(size_t offset) const {
732     DCHECK_LT(offset, DataSize()) << "Offset past end of data section";
733     return (offset != 0u) ? reinterpret_cast<const T*>(DataBegin() + offset) : nullptr;
734   }
735 
GetOatDexFile()736   const OatDexFile* GetOatDexFile() const {
737     return oat_dex_file_;
738   }
739 
740   // Used by oat writer.
SetOatDexFile(const OatDexFile * oat_dex_file)741   void SetOatDexFile(const OatDexFile* oat_dex_file) const {
742     oat_dex_file_ = oat_dex_file;
743   }
744 
745   // Read MapItems and validate/set remaining offsets.
GetMapList()746   const dex::MapList* GetMapList() const {
747     return reinterpret_cast<const dex::MapList*>(DataBegin() + header_->map_off_);
748   }
749 
750   // Utility methods for reading integral values from a buffer.
751   static int32_t ReadSignedInt(const uint8_t* ptr, int zwidth);
752   static uint32_t ReadUnsignedInt(const uint8_t* ptr, int zwidth, bool fill_on_right);
753   static int64_t ReadSignedLong(const uint8_t* ptr, int zwidth);
754   static uint64_t ReadUnsignedLong(const uint8_t* ptr, int zwidth, bool fill_on_right);
755 
756   // Recalculates the checksum of the dex file. Does not use the current value in the header.
757   virtual uint32_t CalculateChecksum() const;
758   static uint32_t CalculateChecksum(const uint8_t* begin, size_t size);
759   static uint32_t ChecksumMemoryRange(const uint8_t* begin, size_t size);
760 
761   // Number of bytes at the beginning of the dex file header which are skipped
762   // when computing the adler32 checksum of the entire file.
763   static constexpr uint32_t kNumNonChecksumBytes = OFFSETOF_MEMBER(DexFile::Header, signature_);
764 
765   // Appends a human-readable form of the method at an index.
766   void AppendPrettyMethod(uint32_t method_idx, bool with_signature, std::string* result) const;
767   // Returns a human-readable form of the field at an index.
768   std::string PrettyField(uint32_t field_idx, bool with_type = true) const;
769   // Returns a human-readable form of the type at an index.
770   std::string PrettyType(dex::TypeIndex type_idx) const;
771 
772   ALWAYS_INLINE std::string PrettyMethod(uint32_t method_idx, bool with_signature = true) const {
773     std::string result;
774     AppendPrettyMethod(method_idx, with_signature, &result);
775     return result;
776   }
777 
778   // Not virtual for performance reasons.
IsCompactDexFile()779   ALWAYS_INLINE bool IsCompactDexFile() const {
780     return is_compact_dex_;
781   }
IsStandardDexFile()782   ALWAYS_INLINE bool IsStandardDexFile() const {
783     return !is_compact_dex_;
784   }
785   ALWAYS_INLINE const StandardDexFile* AsStandardDexFile() const;
786   ALWAYS_INLINE const CompactDexFile* AsCompactDexFile() const;
787 
GetHiddenapiDomain()788   hiddenapi::Domain GetHiddenapiDomain() const { return hiddenapi_domain_; }
SetHiddenapiDomain(hiddenapi::Domain value)789   void SetHiddenapiDomain(hiddenapi::Domain value) const { hiddenapi_domain_ = value; }
790 
IsInMainSection(const void * addr)791   bool IsInMainSection(const void* addr) const {
792     return Begin() <= addr && addr < Begin() + Size();
793   }
794 
IsInDataSection(const void * addr)795   bool IsInDataSection(const void* addr) const {
796     return DataBegin() <= addr && addr < DataBegin() + DataSize();
797   }
798 
GetContainer()799   DexFileContainer* GetContainer() const {
800     return container_.get();
801   }
802 
803   IterationRange<ClassIterator> GetClasses() const;
804 
805   template <typename Visitor>
806   static uint32_t DecodeDebugInfoParameterNames(const uint8_t** debug_info,
807                                                 const Visitor& visitor);
808 
809   static inline bool StringEquals(const DexFile* df1, dex::StringIndex sidx1,
810                                   const DexFile* df2, dex::StringIndex sidx2);
811 
812  protected:
813   // First Dex format version supporting default methods.
814   static constexpr uint32_t kDefaultMethodsVersion = 37;
815 
816   DexFile(const uint8_t* base,
817           size_t size,
818           const uint8_t* data_begin,
819           size_t data_size,
820           const std::string& location,
821           uint32_t location_checksum,
822           const OatDexFile* oat_dex_file,
823           std::unique_ptr<DexFileContainer> container,
824           bool is_compact_dex);
825 
826   // Top-level initializer that calls other Init methods.
827   bool Init(std::string* error_msg);
828 
829   // Returns true if the header magic and version numbers are of the expected values.
830   bool CheckMagicAndVersion(std::string* error_msg) const;
831 
832   // Initialize section info for sections only found in map. Returns true on success.
833   void InitializeSectionsFromMapList();
834 
835   // The base address of the memory mapping.
836   const uint8_t* const begin_;
837 
838   // The size of the underlying memory allocation in bytes.
839   const size_t size_;
840 
841   // The base address of the data section (same as Begin() for standard dex).
842   const uint8_t* const data_begin_;
843 
844   // The size of the data section.
845   const size_t data_size_;
846 
847   // Typically the dex file name when available, alternatively some identifying string.
848   //
849   // The ClassLinker will use this to match DexFiles the boot class
850   // path to DexCache::GetLocation when loading from an image.
851   const std::string location_;
852 
853   const uint32_t location_checksum_;
854 
855   // Points to the header section.
856   const Header* const header_;
857 
858   // Points to the base of the string identifier list.
859   const dex::StringId* const string_ids_;
860 
861   // Points to the base of the type identifier list.
862   const dex::TypeId* const type_ids_;
863 
864   // Points to the base of the field identifier list.
865   const dex::FieldId* const field_ids_;
866 
867   // Points to the base of the method identifier list.
868   const dex::MethodId* const method_ids_;
869 
870   // Points to the base of the prototype identifier list.
871   const dex::ProtoId* const proto_ids_;
872 
873   // Points to the base of the class definition list.
874   const dex::ClassDef* const class_defs_;
875 
876   // Points to the base of the method handles list.
877   const dex::MethodHandleItem* method_handles_;
878 
879   // Number of elements in the method handles list.
880   size_t num_method_handles_;
881 
882   // Points to the base of the call sites id list.
883   const dex::CallSiteIdItem* call_site_ids_;
884 
885   // Number of elements in the call sites list.
886   size_t num_call_site_ids_;
887 
888   // Points to the base of the hiddenapi class data item_, or nullptr if the dex
889   // file does not have one.
890   const dex::HiddenapiClassData* hiddenapi_class_data_;
891 
892   // If this dex file was loaded from an oat file, oat_dex_file_ contains a
893   // pointer to the OatDexFile it was loaded from. Otherwise oat_dex_file_ is
894   // null.
895   mutable const OatDexFile* oat_dex_file_;
896 
897   // Manages the underlying memory allocation.
898   std::unique_ptr<DexFileContainer> container_;
899 
900   // If the dex file is a compact dex file. If false then the dex file is a standard dex file.
901   const bool is_compact_dex_;
902 
903   // The domain this dex file belongs to for hidden API access checks.
904   // It is decleared `mutable` because the domain is assigned after the DexFile
905   // has been created and can be changed later by the runtime.
906   mutable hiddenapi::Domain hiddenapi_domain_;
907 
908   friend class DexFileLoader;
909   friend class DexFileVerifierTest;
910   friend class OatWriter;
911 };
912 
913 std::ostream& operator<<(std::ostream& os, const DexFile& dex_file);
914 
915 // Iterate over a dex file's ProtoId's paramters
916 class DexFileParameterIterator {
917  public:
DexFileParameterIterator(const DexFile & dex_file,const dex::ProtoId & proto_id)918   DexFileParameterIterator(const DexFile& dex_file, const dex::ProtoId& proto_id)
919       : dex_file_(dex_file) {
920     type_list_ = dex_file_.GetProtoParameters(proto_id);
921     if (type_list_ != nullptr) {
922       size_ = type_list_->Size();
923     }
924   }
HasNext()925   bool HasNext() const { return pos_ < size_; }
Size()926   size_t Size() const { return size_; }
Next()927   void Next() { ++pos_; }
GetTypeIdx()928   dex::TypeIndex GetTypeIdx() {
929     return type_list_->GetTypeItem(pos_).type_idx_;
930   }
GetDescriptor()931   const char* GetDescriptor() {
932     return dex_file_.StringByTypeIdx(dex::TypeIndex(GetTypeIdx()));
933   }
934  private:
935   const DexFile& dex_file_;
936   const dex::TypeList* type_list_ = nullptr;
937   uint32_t size_ = 0;
938   uint32_t pos_ = 0;
939   DISALLOW_IMPLICIT_CONSTRUCTORS(DexFileParameterIterator);
940 };
941 
942 class EncodedArrayValueIterator {
943  public:
944   EncodedArrayValueIterator(const DexFile& dex_file, const uint8_t* array_data);
945 
HasNext()946   bool HasNext() const { return pos_ < array_size_; }
947 
948   void Next();
949 
950   enum ValueType {
951     kByte         = 0x00,
952     kShort        = 0x02,
953     kChar         = 0x03,
954     kInt          = 0x04,
955     kLong         = 0x06,
956     kFloat        = 0x10,
957     kDouble       = 0x11,
958     kMethodType   = 0x15,
959     kMethodHandle = 0x16,
960     kString       = 0x17,
961     kType         = 0x18,
962     kField        = 0x19,
963     kMethod       = 0x1a,
964     kEnum         = 0x1b,
965     kArray        = 0x1c,
966     kAnnotation   = 0x1d,
967     kNull         = 0x1e,
968     kBoolean      = 0x1f,
969   };
970 
GetValueType()971   ValueType GetValueType() const { return type_; }
GetJavaValue()972   const jvalue& GetJavaValue() const { return jval_; }
973 
974  protected:
975   static constexpr uint8_t kEncodedValueTypeMask = 0x1f;  // 0b11111
976   static constexpr uint8_t kEncodedValueArgShift = 5;
977 
978   const DexFile& dex_file_;
979   size_t array_size_;  // Size of array.
980   size_t pos_;  // Current position.
981   const uint8_t* ptr_;  // Pointer into encoded data array.
982   ValueType type_;  // Type of current encoded value.
983   jvalue jval_;  // Value of current encoded value.
984 
985  private:
986   DISALLOW_IMPLICIT_CONSTRUCTORS(EncodedArrayValueIterator);
987 };
988 std::ostream& operator<<(std::ostream& os, EncodedArrayValueIterator::ValueType code);
989 
990 class EncodedStaticFieldValueIterator : public EncodedArrayValueIterator {
991  public:
EncodedStaticFieldValueIterator(const DexFile & dex_file,const dex::ClassDef & class_def)992   EncodedStaticFieldValueIterator(const DexFile& dex_file,
993                                   const dex::ClassDef& class_def)
994       : EncodedArrayValueIterator(dex_file,
995                                   dex_file.GetEncodedStaticFieldValuesArray(class_def))
996   {}
997 
998  private:
999   DISALLOW_IMPLICIT_CONSTRUCTORS(EncodedStaticFieldValueIterator);
1000 };
1001 
1002 class CallSiteArrayValueIterator : public EncodedArrayValueIterator {
1003  public:
CallSiteArrayValueIterator(const DexFile & dex_file,const dex::CallSiteIdItem & call_site_id)1004   CallSiteArrayValueIterator(const DexFile& dex_file,
1005                              const dex::CallSiteIdItem& call_site_id)
1006       : EncodedArrayValueIterator(dex_file,
1007                                   dex_file.GetCallSiteEncodedValuesArray(call_site_id))
1008   {}
1009 
Size()1010   uint32_t Size() const { return array_size_; }
1011 
1012  private:
1013   DISALLOW_IMPLICIT_CONSTRUCTORS(CallSiteArrayValueIterator);
1014 };
1015 
1016 }  // namespace art
1017 
1018 #endif  // ART_LIBDEXFILE_DEX_DEX_FILE_H_
1019