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