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