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