dex_file.h revision 9157c0ca6a57fa9f6378aebcfb2e3ff76622499a
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 <vector> 23 24#include "base/logging.h" 25#include "base/mutex.h" // For Locks::mutator_lock_. 26#include "globals.h" 27#include "invoke_type.h" 28#include "jni.h" 29#include "modifiers.h" 30#include "safe_map.h" 31 32namespace art { 33 34// TODO: remove dependencies on mirror classes, primarily by moving 35// EncodedStaticFieldValueIterator to its own file. 36namespace mirror { 37 class ArtField; 38 class ArtMethod; 39 class ClassLoader; 40 class DexCache; 41} // namespace mirror 42class ClassLinker; 43class MemMap; 44class Signature; 45template<class T> class Handle; 46class StringPiece; 47class ZipArchive; 48 49// TODO: move all of the macro functionality into the DexCache class. 50class DexFile { 51 public: 52 static const byte kDexMagic[]; 53 static const byte kDexMagicVersion[]; 54 static const size_t kSha1DigestSize = 20; 55 static const uint32_t kDexEndianConstant = 0x12345678; 56 57 // name of the DexFile entry within a zip archive 58 static const char* kClassesDex; 59 60 // The value of an invalid index. 61 static const uint32_t kDexNoIndex = 0xFFFFFFFF; 62 63 // The value of an invalid index. 64 static const uint16_t kDexNoIndex16 = 0xFFFF; 65 66 // The separator charactor in MultiDex locations. 67 static constexpr char kMultiDexSeparator = ':'; 68 69 // A string version of the previous. This is a define so that we can merge string literals in the 70 // preprocessor. 71 #define kMultiDexSeparatorString ":" 72 73 // Raw header_item. 74 struct Header { 75 uint8_t magic_[8]; 76 uint32_t checksum_; // See also location_checksum_ 77 uint8_t signature_[kSha1DigestSize]; 78 uint32_t file_size_; // size of entire file 79 uint32_t header_size_; // offset to start of next section 80 uint32_t endian_tag_; 81 uint32_t link_size_; // unused 82 uint32_t link_off_; // unused 83 uint32_t map_off_; // unused 84 uint32_t string_ids_size_; // number of StringIds 85 uint32_t string_ids_off_; // file offset of StringIds array 86 uint32_t type_ids_size_; // number of TypeIds, we don't support more than 65535 87 uint32_t type_ids_off_; // file offset of TypeIds array 88 uint32_t proto_ids_size_; // number of ProtoIds, we don't support more than 65535 89 uint32_t proto_ids_off_; // file offset of ProtoIds array 90 uint32_t field_ids_size_; // number of FieldIds 91 uint32_t field_ids_off_; // file offset of FieldIds array 92 uint32_t method_ids_size_; // number of MethodIds 93 uint32_t method_ids_off_; // file offset of MethodIds array 94 uint32_t class_defs_size_; // number of ClassDefs 95 uint32_t class_defs_off_; // file offset of ClassDef array 96 uint32_t data_size_; // unused 97 uint32_t data_off_; // unused 98 99 private: 100 DISALLOW_COPY_AND_ASSIGN(Header); 101 }; 102 103 // Map item type codes. 104 enum { 105 kDexTypeHeaderItem = 0x0000, 106 kDexTypeStringIdItem = 0x0001, 107 kDexTypeTypeIdItem = 0x0002, 108 kDexTypeProtoIdItem = 0x0003, 109 kDexTypeFieldIdItem = 0x0004, 110 kDexTypeMethodIdItem = 0x0005, 111 kDexTypeClassDefItem = 0x0006, 112 kDexTypeMapList = 0x1000, 113 kDexTypeTypeList = 0x1001, 114 kDexTypeAnnotationSetRefList = 0x1002, 115 kDexTypeAnnotationSetItem = 0x1003, 116 kDexTypeClassDataItem = 0x2000, 117 kDexTypeCodeItem = 0x2001, 118 kDexTypeStringDataItem = 0x2002, 119 kDexTypeDebugInfoItem = 0x2003, 120 kDexTypeAnnotationItem = 0x2004, 121 kDexTypeEncodedArrayItem = 0x2005, 122 kDexTypeAnnotationsDirectoryItem = 0x2006, 123 }; 124 125 struct MapItem { 126 uint16_t type_; 127 uint16_t unused_; 128 uint32_t size_; 129 uint32_t offset_; 130 131 private: 132 DISALLOW_COPY_AND_ASSIGN(MapItem); 133 }; 134 135 struct MapList { 136 uint32_t size_; 137 MapItem list_[1]; 138 139 private: 140 DISALLOW_COPY_AND_ASSIGN(MapList); 141 }; 142 143 // Raw string_id_item. 144 struct StringId { 145 uint32_t string_data_off_; // offset in bytes from the base address 146 147 private: 148 DISALLOW_COPY_AND_ASSIGN(StringId); 149 }; 150 151 // Raw type_id_item. 152 struct TypeId { 153 uint32_t descriptor_idx_; // index into string_ids 154 155 private: 156 DISALLOW_COPY_AND_ASSIGN(TypeId); 157 }; 158 159 // Raw field_id_item. 160 struct FieldId { 161 uint16_t class_idx_; // index into type_ids_ array for defining class 162 uint16_t type_idx_; // index into type_ids_ array for field type 163 uint32_t name_idx_; // index into string_ids_ array for field name 164 165 private: 166 DISALLOW_COPY_AND_ASSIGN(FieldId); 167 }; 168 169 // Raw method_id_item. 170 struct MethodId { 171 uint16_t class_idx_; // index into type_ids_ array for defining class 172 uint16_t proto_idx_; // index into proto_ids_ array for method prototype 173 uint32_t name_idx_; // index into string_ids_ array for method name 174 175 private: 176 DISALLOW_COPY_AND_ASSIGN(MethodId); 177 }; 178 179 // Raw proto_id_item. 180 struct ProtoId { 181 uint32_t shorty_idx_; // index into string_ids array for shorty descriptor 182 uint16_t return_type_idx_; // index into type_ids array for return type 183 uint16_t pad_; // padding = 0 184 uint32_t parameters_off_; // file offset to type_list for parameter types 185 186 private: 187 DISALLOW_COPY_AND_ASSIGN(ProtoId); 188 }; 189 190 // Raw class_def_item. 191 struct ClassDef { 192 uint16_t class_idx_; // index into type_ids_ array for this class 193 uint16_t pad1_; // padding = 0 194 uint32_t access_flags_; 195 uint16_t superclass_idx_; // index into type_ids_ array for superclass 196 uint16_t pad2_; // padding = 0 197 uint32_t interfaces_off_; // file offset to TypeList 198 uint32_t source_file_idx_; // index into string_ids_ for source file name 199 uint32_t annotations_off_; // file offset to annotations_directory_item 200 uint32_t class_data_off_; // file offset to class_data_item 201 uint32_t static_values_off_; // file offset to EncodedArray 202 203 private: 204 DISALLOW_COPY_AND_ASSIGN(ClassDef); 205 }; 206 207 // Raw type_item. 208 struct TypeItem { 209 uint16_t type_idx_; // index into type_ids section 210 211 private: 212 DISALLOW_COPY_AND_ASSIGN(TypeItem); 213 }; 214 215 // Raw type_list. 216 class TypeList { 217 public: 218 uint32_t Size() const { 219 return size_; 220 } 221 222 const TypeItem& GetTypeItem(uint32_t idx) const { 223 DCHECK_LT(idx, this->size_); 224 return this->list_[idx]; 225 } 226 227 private: 228 uint32_t size_; // size of the list, in entries 229 TypeItem list_[1]; // elements of the list 230 DISALLOW_COPY_AND_ASSIGN(TypeList); 231 }; 232 233 // Raw code_item. 234 struct CodeItem { 235 uint16_t registers_size_; 236 uint16_t ins_size_; 237 uint16_t outs_size_; 238 uint16_t tries_size_; 239 uint32_t debug_info_off_; // file offset to debug info stream 240 uint32_t insns_size_in_code_units_; // size of the insns array, in 2 byte code units 241 uint16_t insns_[1]; 242 243 private: 244 DISALLOW_COPY_AND_ASSIGN(CodeItem); 245 }; 246 247 // Raw try_item. 248 struct TryItem { 249 uint32_t start_addr_; 250 uint16_t insn_count_; 251 uint16_t handler_off_; 252 253 private: 254 DISALLOW_COPY_AND_ASSIGN(TryItem); 255 }; 256 257 // Annotation constants. 258 enum { 259 kDexVisibilityBuild = 0x00, /* annotation visibility */ 260 kDexVisibilityRuntime = 0x01, 261 kDexVisibilitySystem = 0x02, 262 263 kDexAnnotationByte = 0x00, 264 kDexAnnotationShort = 0x02, 265 kDexAnnotationChar = 0x03, 266 kDexAnnotationInt = 0x04, 267 kDexAnnotationLong = 0x06, 268 kDexAnnotationFloat = 0x10, 269 kDexAnnotationDouble = 0x11, 270 kDexAnnotationString = 0x17, 271 kDexAnnotationType = 0x18, 272 kDexAnnotationField = 0x19, 273 kDexAnnotationMethod = 0x1a, 274 kDexAnnotationEnum = 0x1b, 275 kDexAnnotationArray = 0x1c, 276 kDexAnnotationAnnotation = 0x1d, 277 kDexAnnotationNull = 0x1e, 278 kDexAnnotationBoolean = 0x1f, 279 280 kDexAnnotationValueTypeMask = 0x1f, /* low 5 bits */ 281 kDexAnnotationValueArgShift = 5, 282 }; 283 284 struct AnnotationsDirectoryItem { 285 uint32_t class_annotations_off_; 286 uint32_t fields_size_; 287 uint32_t methods_size_; 288 uint32_t parameters_size_; 289 290 private: 291 DISALLOW_COPY_AND_ASSIGN(AnnotationsDirectoryItem); 292 }; 293 294 struct FieldAnnotationsItem { 295 uint32_t field_idx_; 296 uint32_t annotations_off_; 297 298 private: 299 DISALLOW_COPY_AND_ASSIGN(FieldAnnotationsItem); 300 }; 301 302 struct MethodAnnotationsItem { 303 uint32_t method_idx_; 304 uint32_t annotations_off_; 305 306 private: 307 DISALLOW_COPY_AND_ASSIGN(MethodAnnotationsItem); 308 }; 309 310 struct ParameterAnnotationsItem { 311 uint32_t method_idx_; 312 uint32_t annotations_off_; 313 314 private: 315 DISALLOW_COPY_AND_ASSIGN(ParameterAnnotationsItem); 316 }; 317 318 struct AnnotationSetRefItem { 319 uint32_t annotations_off_; 320 321 private: 322 DISALLOW_COPY_AND_ASSIGN(AnnotationSetRefItem); 323 }; 324 325 struct AnnotationSetRefList { 326 uint32_t size_; 327 AnnotationSetRefItem list_[1]; 328 329 private: 330 DISALLOW_COPY_AND_ASSIGN(AnnotationSetRefList); 331 }; 332 333 struct AnnotationSetItem { 334 uint32_t size_; 335 uint32_t entries_[1]; 336 337 private: 338 DISALLOW_COPY_AND_ASSIGN(AnnotationSetItem); 339 }; 340 341 struct AnnotationItem { 342 uint8_t visibility_; 343 uint8_t annotation_[1]; 344 345 private: 346 DISALLOW_COPY_AND_ASSIGN(AnnotationItem); 347 }; 348 349 typedef std::pair<const DexFile*, const DexFile::ClassDef*> ClassPathEntry; 350 typedef std::vector<const DexFile*> ClassPath; 351 352 // Search a collection of DexFiles for a descriptor 353 static ClassPathEntry FindInClassPath(const char* descriptor, 354 const ClassPath& class_path); 355 356 // Returns the checksum of a file for comparison with GetLocationChecksum(). 357 // For .dex files, this is the header checksum. 358 // For zip files, this is the classes.dex zip entry CRC32 checksum. 359 // Return true if the checksum could be found, false otherwise. 360 static bool GetChecksum(const char* filename, uint32_t* checksum, std::string* error_msg); 361 362 // Opens .dex files found in the container, guessing the container format based on file extension. 363 static bool Open(const char* filename, const char* location, std::string* error_msg, 364 std::vector<const DexFile*>* dex_files); 365 366 // Opens .dex file, backed by existing memory 367 static const DexFile* Open(const uint8_t* base, size_t size, 368 const std::string& location, 369 uint32_t location_checksum, 370 std::string* error_msg) { 371 return OpenMemory(base, size, location, location_checksum, NULL, error_msg); 372 } 373 374 // Open all classesXXX.dex files from a zip archive. 375 static bool OpenFromZip(const ZipArchive& zip_archive, const std::string& location, 376 std::string* error_msg, std::vector<const DexFile*>* dex_files); 377 378 // Closes a .dex file. 379 virtual ~DexFile(); 380 381 const std::string& GetLocation() const { 382 return location_; 383 } 384 385 // For normal dex files, location and base location coincide. If a dex file is part of a multidex 386 // archive, the base location is the name of the originating jar/apk, stripped of any internal 387 // classes*.dex path. 388 const std::string GetBaseLocation() const { 389 if (IsMultiDexLocation(location_.c_str())) { 390 std::pair<const char*, const char*> pair = SplitMultiDexLocation(location_.c_str()); 391 std::string res(pair.first); 392 delete[] pair.first; 393 return res; 394 } else { 395 return location_; 396 } 397 } 398 399 // For DexFiles directly from .dex files, this is the checksum from the DexFile::Header. 400 // For DexFiles opened from a zip files, this will be the ZipEntry CRC32 of classes.dex. 401 uint32_t GetLocationChecksum() const { 402 return location_checksum_; 403 } 404 405 const Header& GetHeader() const { 406 DCHECK(header_ != NULL) << GetLocation(); 407 return *header_; 408 } 409 410 // Decode the dex magic version 411 uint32_t GetVersion() const; 412 413 // Returns true if the byte string points to the magic value. 414 static bool IsMagicValid(const byte* magic); 415 416 // Returns true if the byte string after the magic is the correct value. 417 static bool IsVersionValid(const byte* magic); 418 419 // Returns the number of string identifiers in the .dex file. 420 size_t NumStringIds() const { 421 DCHECK(header_ != NULL) << GetLocation(); 422 return header_->string_ids_size_; 423 } 424 425 // Returns the StringId at the specified index. 426 const StringId& GetStringId(uint32_t idx) const { 427 DCHECK_LT(idx, NumStringIds()) << GetLocation(); 428 return string_ids_[idx]; 429 } 430 431 uint32_t GetIndexForStringId(const StringId& string_id) const { 432 CHECK_GE(&string_id, string_ids_) << GetLocation(); 433 CHECK_LT(&string_id, string_ids_ + header_->string_ids_size_) << GetLocation(); 434 return &string_id - string_ids_; 435 } 436 437 int32_t GetStringLength(const StringId& string_id) const; 438 439 // Returns a pointer to the UTF-8 string data referred to by the given string_id as well as the 440 // length of the string when decoded as a UTF-16 string. Note the UTF-16 length is not the same 441 // as the string length of the string data. 442 const char* GetStringDataAndUtf16Length(const StringId& string_id, uint32_t* utf16_length) const; 443 444 const char* GetStringData(const StringId& string_id) const { 445 uint32_t ignored; 446 return GetStringDataAndUtf16Length(string_id, &ignored); 447 } 448 449 // Index version of GetStringDataAndUtf16Length. 450 const char* StringDataAndUtf16LengthByIdx(uint32_t idx, uint32_t* utf16_length) const { 451 if (idx == kDexNoIndex) { 452 *utf16_length = 0; 453 return NULL; 454 } 455 const StringId& string_id = GetStringId(idx); 456 return GetStringDataAndUtf16Length(string_id, utf16_length); 457 } 458 459 const char* StringDataByIdx(uint32_t idx) const { 460 uint32_t unicode_length; 461 return StringDataAndUtf16LengthByIdx(idx, &unicode_length); 462 } 463 464 // Looks up a string id for a given modified utf8 string. 465 const StringId* FindStringId(const char* string) const; 466 467 // Looks up a string id for a given utf16 string. 468 const StringId* FindStringId(const uint16_t* string) const; 469 470 // Returns the number of type identifiers in the .dex file. 471 size_t NumTypeIds() const { 472 DCHECK(header_ != NULL) << GetLocation(); 473 return header_->type_ids_size_; 474 } 475 476 // Returns the TypeId at the specified index. 477 const TypeId& GetTypeId(uint32_t idx) const { 478 DCHECK_LT(idx, NumTypeIds()) << GetLocation(); 479 return type_ids_[idx]; 480 } 481 482 uint16_t GetIndexForTypeId(const TypeId& type_id) const { 483 CHECK_GE(&type_id, type_ids_) << GetLocation(); 484 CHECK_LT(&type_id, type_ids_ + header_->type_ids_size_) << GetLocation(); 485 size_t result = &type_id - type_ids_; 486 DCHECK_LT(result, 65536U) << GetLocation(); 487 return static_cast<uint16_t>(result); 488 } 489 490 // Get the descriptor string associated with a given type index. 491 const char* StringByTypeIdx(uint32_t idx, uint32_t* unicode_length) const { 492 const TypeId& type_id = GetTypeId(idx); 493 return StringDataAndUtf16LengthByIdx(type_id.descriptor_idx_, unicode_length); 494 } 495 496 const char* StringByTypeIdx(uint32_t idx) const { 497 const TypeId& type_id = GetTypeId(idx); 498 return StringDataByIdx(type_id.descriptor_idx_); 499 } 500 501 // Returns the type descriptor string of a type id. 502 const char* GetTypeDescriptor(const TypeId& type_id) const { 503 return StringDataByIdx(type_id.descriptor_idx_); 504 } 505 506 // Looks up a type for the given string index 507 const TypeId* FindTypeId(uint32_t string_idx) const; 508 509 // Returns the number of field identifiers in the .dex file. 510 size_t NumFieldIds() const { 511 DCHECK(header_ != NULL) << GetLocation(); 512 return header_->field_ids_size_; 513 } 514 515 // Returns the FieldId at the specified index. 516 const FieldId& GetFieldId(uint32_t idx) const { 517 DCHECK_LT(idx, NumFieldIds()) << GetLocation(); 518 return field_ids_[idx]; 519 } 520 521 uint32_t GetIndexForFieldId(const FieldId& field_id) const { 522 CHECK_GE(&field_id, field_ids_) << GetLocation(); 523 CHECK_LT(&field_id, field_ids_ + header_->field_ids_size_) << GetLocation(); 524 return &field_id - field_ids_; 525 } 526 527 // Looks up a field by its declaring class, name and type 528 const FieldId* FindFieldId(const DexFile::TypeId& declaring_klass, 529 const DexFile::StringId& name, 530 const DexFile::TypeId& type) const; 531 532 // Returns the declaring class descriptor string of a field id. 533 const char* GetFieldDeclaringClassDescriptor(const FieldId& field_id) const { 534 const DexFile::TypeId& type_id = GetTypeId(field_id.class_idx_); 535 return GetTypeDescriptor(type_id); 536 } 537 538 // Returns the class descriptor string of a field id. 539 const char* GetFieldTypeDescriptor(const FieldId& field_id) const { 540 const DexFile::TypeId& type_id = GetTypeId(field_id.type_idx_); 541 return GetTypeDescriptor(type_id); 542 } 543 544 // Returns the name of a field id. 545 const char* GetFieldName(const FieldId& field_id) const { 546 return StringDataByIdx(field_id.name_idx_); 547 } 548 549 // Returns the number of method identifiers in the .dex file. 550 size_t NumMethodIds() const { 551 DCHECK(header_ != NULL) << GetLocation(); 552 return header_->method_ids_size_; 553 } 554 555 // Returns the MethodId at the specified index. 556 const MethodId& GetMethodId(uint32_t idx) const { 557 DCHECK_LT(idx, NumMethodIds()) << GetLocation(); 558 return method_ids_[idx]; 559 } 560 561 uint32_t GetIndexForMethodId(const MethodId& method_id) const { 562 CHECK_GE(&method_id, method_ids_) << GetLocation(); 563 CHECK_LT(&method_id, method_ids_ + header_->method_ids_size_) << GetLocation(); 564 return &method_id - method_ids_; 565 } 566 567 // Looks up a method by its declaring class, name and proto_id 568 const MethodId* FindMethodId(const DexFile::TypeId& declaring_klass, 569 const DexFile::StringId& name, 570 const DexFile::ProtoId& signature) const; 571 572 // Returns the declaring class descriptor string of a method id. 573 const char* GetMethodDeclaringClassDescriptor(const MethodId& method_id) const { 574 const DexFile::TypeId& type_id = GetTypeId(method_id.class_idx_); 575 return GetTypeDescriptor(type_id); 576 } 577 578 // Returns the prototype of a method id. 579 const ProtoId& GetMethodPrototype(const MethodId& method_id) const { 580 return GetProtoId(method_id.proto_idx_); 581 } 582 583 // Returns a representation of the signature of a method id. 584 const Signature GetMethodSignature(const MethodId& method_id) const; 585 586 // Returns the name of a method id. 587 const char* GetMethodName(const MethodId& method_id) const { 588 return StringDataByIdx(method_id.name_idx_); 589 } 590 591 // Returns the shorty of a method id. 592 const char* GetMethodShorty(const MethodId& method_id) const { 593 return StringDataByIdx(GetProtoId(method_id.proto_idx_).shorty_idx_); 594 } 595 const char* GetMethodShorty(const MethodId& method_id, uint32_t* length) const { 596 // Using the UTF16 length is safe here as shorties are guaranteed to be ASCII characters. 597 return StringDataAndUtf16LengthByIdx(GetProtoId(method_id.proto_idx_).shorty_idx_, length); 598 } 599 // Returns the number of class definitions in the .dex file. 600 size_t NumClassDefs() const { 601 DCHECK(header_ != NULL) << GetLocation(); 602 return header_->class_defs_size_; 603 } 604 605 // Returns the ClassDef at the specified index. 606 const ClassDef& GetClassDef(uint16_t idx) const { 607 DCHECK_LT(idx, NumClassDefs()) << GetLocation(); 608 return class_defs_[idx]; 609 } 610 611 uint16_t GetIndexForClassDef(const ClassDef& class_def) const { 612 CHECK_GE(&class_def, class_defs_) << GetLocation(); 613 CHECK_LT(&class_def, class_defs_ + header_->class_defs_size_) << GetLocation(); 614 return &class_def - class_defs_; 615 } 616 617 // Returns the class descriptor string of a class definition. 618 const char* GetClassDescriptor(const ClassDef& class_def) const { 619 return StringByTypeIdx(class_def.class_idx_); 620 } 621 622 // Looks up a class definition by its class descriptor. 623 const ClassDef* FindClassDef(const char* descriptor) const; 624 625 // Looks up a class definition by its type index. 626 const ClassDef* FindClassDef(uint16_t type_idx) const; 627 628 const TypeList* GetInterfacesList(const ClassDef& class_def) const { 629 if (class_def.interfaces_off_ == 0) { 630 return NULL; 631 } else { 632 const byte* addr = begin_ + class_def.interfaces_off_; 633 return reinterpret_cast<const TypeList*>(addr); 634 } 635 } 636 637 // Returns a pointer to the raw memory mapped class_data_item 638 const byte* GetClassData(const ClassDef& class_def) const { 639 if (class_def.class_data_off_ == 0) { 640 return NULL; 641 } else { 642 return begin_ + class_def.class_data_off_; 643 } 644 } 645 646 // 647 const CodeItem* GetCodeItem(const uint32_t code_off) const { 648 if (code_off == 0) { 649 return NULL; // native or abstract method 650 } else { 651 const byte* addr = begin_ + code_off; 652 return reinterpret_cast<const CodeItem*>(addr); 653 } 654 } 655 656 const char* GetReturnTypeDescriptor(const ProtoId& proto_id) const { 657 return StringByTypeIdx(proto_id.return_type_idx_); 658 } 659 660 // Returns the number of prototype identifiers in the .dex file. 661 size_t NumProtoIds() const { 662 DCHECK(header_ != NULL) << GetLocation(); 663 return header_->proto_ids_size_; 664 } 665 666 // Returns the ProtoId at the specified index. 667 const ProtoId& GetProtoId(uint32_t idx) const { 668 DCHECK_LT(idx, NumProtoIds()) << GetLocation(); 669 return proto_ids_[idx]; 670 } 671 672 uint16_t GetIndexForProtoId(const ProtoId& proto_id) const { 673 CHECK_GE(&proto_id, proto_ids_) << GetLocation(); 674 CHECK_LT(&proto_id, proto_ids_ + header_->proto_ids_size_) << GetLocation(); 675 return &proto_id - proto_ids_; 676 } 677 678 // Looks up a proto id for a given return type and signature type list 679 const ProtoId* FindProtoId(uint16_t return_type_idx, 680 const uint16_t* signature_type_idxs, uint32_t signature_length) const; 681 const ProtoId* FindProtoId(uint16_t return_type_idx, 682 const std::vector<uint16_t>& signature_type_idxs) const { 683 return FindProtoId(return_type_idx, &signature_type_idxs[0], signature_type_idxs.size()); 684 } 685 686 // Given a signature place the type ids into the given vector, returns true on success 687 bool CreateTypeList(const StringPiece& signature, uint16_t* return_type_idx, 688 std::vector<uint16_t>* param_type_idxs) const; 689 690 // Create a Signature from the given string signature or return Signature::NoSignature if not 691 // possible. 692 const Signature CreateSignature(const StringPiece& signature) const; 693 694 // Returns the short form method descriptor for the given prototype. 695 const char* GetShorty(uint32_t proto_idx) const { 696 const ProtoId& proto_id = GetProtoId(proto_idx); 697 return StringDataByIdx(proto_id.shorty_idx_); 698 } 699 700 const TypeList* GetProtoParameters(const ProtoId& proto_id) const { 701 if (proto_id.parameters_off_ == 0) { 702 return NULL; 703 } else { 704 const byte* addr = begin_ + proto_id.parameters_off_; 705 return reinterpret_cast<const TypeList*>(addr); 706 } 707 } 708 709 const byte* GetEncodedStaticFieldValuesArray(const ClassDef& class_def) const { 710 if (class_def.static_values_off_ == 0) { 711 return 0; 712 } else { 713 return begin_ + class_def.static_values_off_; 714 } 715 } 716 717 static const TryItem* GetTryItems(const CodeItem& code_item, uint32_t offset); 718 719 // Get the base of the encoded data for the given DexCode. 720 static const byte* GetCatchHandlerData(const CodeItem& code_item, uint32_t offset) { 721 const byte* handler_data = 722 reinterpret_cast<const byte*>(GetTryItems(code_item, code_item.tries_size_)); 723 return handler_data + offset; 724 } 725 726 // Find which try region is associated with the given address (ie dex pc). Returns -1 if none. 727 static int32_t FindTryItem(const CodeItem &code_item, uint32_t address); 728 729 // Find the handler offset associated with the given address (ie dex pc). Returns -1 if none. 730 static int32_t FindCatchHandlerOffset(const CodeItem &code_item, uint32_t address); 731 732 // Get the pointer to the start of the debugging data 733 const byte* GetDebugInfoStream(const CodeItem* code_item) const { 734 if (code_item->debug_info_off_ == 0) { 735 return NULL; 736 } else { 737 return begin_ + code_item->debug_info_off_; 738 } 739 } 740 741 // Callback for "new position table entry". 742 // Returning true causes the decoder to stop early. 743 typedef bool (*DexDebugNewPositionCb)(void* context, uint32_t address, uint32_t line_num); 744 745 // Callback for "new locals table entry". "signature" is an empty string 746 // if no signature is available for an entry. 747 typedef void (*DexDebugNewLocalCb)(void* context, uint16_t reg, 748 uint32_t start_address, 749 uint32_t end_address, 750 const char* name, 751 const char* descriptor, 752 const char* signature); 753 754 static bool LineNumForPcCb(void* context, uint32_t address, uint32_t line_num); 755 756 // Debug info opcodes and constants 757 enum { 758 DBG_END_SEQUENCE = 0x00, 759 DBG_ADVANCE_PC = 0x01, 760 DBG_ADVANCE_LINE = 0x02, 761 DBG_START_LOCAL = 0x03, 762 DBG_START_LOCAL_EXTENDED = 0x04, 763 DBG_END_LOCAL = 0x05, 764 DBG_RESTART_LOCAL = 0x06, 765 DBG_SET_PROLOGUE_END = 0x07, 766 DBG_SET_EPILOGUE_BEGIN = 0x08, 767 DBG_SET_FILE = 0x09, 768 DBG_FIRST_SPECIAL = 0x0a, 769 DBG_LINE_BASE = -4, 770 DBG_LINE_RANGE = 15, 771 }; 772 773 struct LocalInfo { 774 LocalInfo() 775 : name_(NULL), descriptor_(NULL), signature_(NULL), start_address_(0), is_live_(false) {} 776 777 const char* name_; // E.g., list 778 const char* descriptor_; // E.g., Ljava/util/LinkedList; 779 const char* signature_; // E.g., java.util.LinkedList<java.lang.Integer> 780 uint16_t start_address_; // PC location where the local is first defined. 781 bool is_live_; // Is the local defined and live. 782 783 private: 784 DISALLOW_COPY_AND_ASSIGN(LocalInfo); 785 }; 786 787 struct LineNumFromPcContext { 788 LineNumFromPcContext(uint32_t address, uint32_t line_num) 789 : address_(address), line_num_(line_num) {} 790 uint32_t address_; 791 uint32_t line_num_; 792 private: 793 DISALLOW_COPY_AND_ASSIGN(LineNumFromPcContext); 794 }; 795 796 void InvokeLocalCbIfLive(void* context, int reg, uint32_t end_address, 797 LocalInfo* local_in_reg, DexDebugNewLocalCb local_cb) const { 798 if (local_cb != NULL && local_in_reg[reg].is_live_) { 799 local_cb(context, reg, local_in_reg[reg].start_address_, end_address, 800 local_in_reg[reg].name_, local_in_reg[reg].descriptor_, 801 local_in_reg[reg].signature_ != NULL ? local_in_reg[reg].signature_ : ""); 802 } 803 } 804 805 // Determine the source file line number based on the program counter. 806 // "pc" is an offset, in 16-bit units, from the start of the method's code. 807 // 808 // Returns -1 if no match was found (possibly because the source files were 809 // compiled without "-g", so no line number information is present). 810 // Returns -2 for native methods (as expected in exception traces). 811 // 812 // This is used by runtime; therefore use art::Method not art::DexFile::Method. 813 int32_t GetLineNumFromPC(mirror::ArtMethod* method, uint32_t rel_pc) const 814 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 815 816 void DecodeDebugInfo(const CodeItem* code_item, bool is_static, uint32_t method_idx, 817 DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb, 818 void* context) const; 819 820 const char* GetSourceFile(const ClassDef& class_def) const { 821 if (class_def.source_file_idx_ == 0xffffffff) { 822 return NULL; 823 } else { 824 return StringDataByIdx(class_def.source_file_idx_); 825 } 826 } 827 828 int GetPermissions() const; 829 830 bool IsReadOnly() const; 831 832 bool EnableWrite() const; 833 834 bool DisableWrite() const; 835 836 const byte* Begin() const { 837 return begin_; 838 } 839 840 size_t Size() const { 841 return size_; 842 } 843 844 private: 845 // Opens a .dex file 846 static const DexFile* OpenFile(int fd, const char* location, bool verify, std::string* error_msg); 847 848 // Opens dex files from within a .jar, .zip, or .apk file 849 static bool OpenZip(int fd, const std::string& location, std::string* error_msg, 850 std::vector<const DexFile*>* dex_files); 851 852 enum class ZipOpenErrorCode { // private 853 kNoError, 854 kEntryNotFound, 855 kExtractToMemoryError, 856 kDexFileError, 857 kMakeReadOnlyError, 858 kVerifyError 859 }; 860 861 // Opens .dex file from the entry_name in a zip archive. error_code is undefined when non-nullptr 862 // return. 863 static const DexFile* Open(const ZipArchive& zip_archive, const char* entry_name, 864 const std::string& location, std::string* error_msg, 865 ZipOpenErrorCode* error_code); 866 867 // Opens a .dex file at the given address backed by a MemMap 868 static const DexFile* OpenMemory(const std::string& location, 869 uint32_t location_checksum, 870 MemMap* mem_map, 871 std::string* error_msg); 872 873 // Opens a .dex file at the given address, optionally backed by a MemMap 874 static const DexFile* OpenMemory(const byte* dex_file, 875 size_t size, 876 const std::string& location, 877 uint32_t location_checksum, 878 MemMap* mem_map, 879 std::string* error_msg); 880 881 DexFile(const byte* base, size_t size, 882 const std::string& location, 883 uint32_t location_checksum, 884 MemMap* mem_map); 885 886 // Top-level initializer that calls other Init methods. 887 bool Init(std::string* error_msg); 888 889 // Returns true if the header magic and version numbers are of the expected values. 890 bool CheckMagicAndVersion(std::string* error_msg) const; 891 892 void DecodeDebugInfo0(const CodeItem* code_item, bool is_static, uint32_t method_idx, 893 DexDebugNewPositionCb position_cb, DexDebugNewLocalCb local_cb, 894 void* context, const byte* stream, LocalInfo* local_in_reg) const; 895 896 // Check whether a location denotes a multidex dex file. This is a very simple check: returns 897 // whether the string contains the separator character. 898 static bool IsMultiDexLocation(const char* location); 899 900 // Splits a multidex location at the last separator character. The second component is a pointer 901 // to the character after the separator. The first is a copy of the substring up to the separator. 902 // 903 // Note: It's the caller's job to free the first component of the returned pair. 904 // Bug 15313523: gcc/libc++ don't allow a unique_ptr for the first component 905 static std::pair<const char*, const char*> SplitMultiDexLocation(const char* location); 906 907 908 // The base address of the memory mapping. 909 const byte* const begin_; 910 911 // The size of the underlying memory allocation in bytes. 912 const size_t size_; 913 914 // Typically the dex file name when available, alternatively some identifying string. 915 // 916 // The ClassLinker will use this to match DexFiles the boot class 917 // path to DexCache::GetLocation when loading from an image. 918 const std::string location_; 919 920 const uint32_t location_checksum_; 921 922 // Manages the underlying memory allocation. 923 std::unique_ptr<MemMap> mem_map_; 924 925 // Points to the header section. 926 const Header* const header_; 927 928 // Points to the base of the string identifier list. 929 const StringId* const string_ids_; 930 931 // Points to the base of the type identifier list. 932 const TypeId* const type_ids_; 933 934 // Points to the base of the field identifier list. 935 const FieldId* const field_ids_; 936 937 // Points to the base of the method identifier list. 938 const MethodId* const method_ids_; 939 940 // Points to the base of the prototype identifier list. 941 const ProtoId* const proto_ids_; 942 943 // Points to the base of the class definition list. 944 const ClassDef* const class_defs_; 945}; 946std::ostream& operator<<(std::ostream& os, const DexFile& dex_file); 947 948// Iterate over a dex file's ProtoId's paramters 949class DexFileParameterIterator { 950 public: 951 DexFileParameterIterator(const DexFile& dex_file, const DexFile::ProtoId& proto_id) 952 : dex_file_(dex_file), size_(0), pos_(0) { 953 type_list_ = dex_file_.GetProtoParameters(proto_id); 954 if (type_list_ != NULL) { 955 size_ = type_list_->Size(); 956 } 957 } 958 bool HasNext() const { return pos_ < size_; } 959 void Next() { ++pos_; } 960 uint16_t GetTypeIdx() { 961 return type_list_->GetTypeItem(pos_).type_idx_; 962 } 963 const char* GetDescriptor() { 964 return dex_file_.StringByTypeIdx(GetTypeIdx()); 965 } 966 private: 967 const DexFile& dex_file_; 968 const DexFile::TypeList* type_list_; 969 uint32_t size_; 970 uint32_t pos_; 971 DISALLOW_IMPLICIT_CONSTRUCTORS(DexFileParameterIterator); 972}; 973 974// Abstract the signature of a method. 975class Signature { 976 public: 977 std::string ToString() const; 978 979 static Signature NoSignature() { 980 return Signature(); 981 } 982 983 bool operator==(const Signature& rhs) const; 984 bool operator!=(const Signature& rhs) const { 985 return !(*this == rhs); 986 } 987 988 bool operator==(const StringPiece& rhs) const; 989 990 private: 991 Signature(const DexFile* dex, const DexFile::ProtoId& proto) : dex_file_(dex), proto_id_(&proto) { 992 } 993 994 Signature() : dex_file_(nullptr), proto_id_(nullptr) { 995 } 996 997 friend class DexFile; 998 999 const DexFile* const dex_file_; 1000 const DexFile::ProtoId* const proto_id_; 1001}; 1002std::ostream& operator<<(std::ostream& os, const Signature& sig); 1003 1004// Iterate and decode class_data_item 1005class ClassDataItemIterator { 1006 public: 1007 ClassDataItemIterator(const DexFile& dex_file, const byte* raw_class_data_item) 1008 : dex_file_(dex_file), pos_(0), ptr_pos_(raw_class_data_item), last_idx_(0) { 1009 ReadClassDataHeader(); 1010 if (EndOfInstanceFieldsPos() > 0) { 1011 ReadClassDataField(); 1012 } else if (EndOfVirtualMethodsPos() > 0) { 1013 ReadClassDataMethod(); 1014 } 1015 } 1016 uint32_t NumStaticFields() const { 1017 return header_.static_fields_size_; 1018 } 1019 uint32_t NumInstanceFields() const { 1020 return header_.instance_fields_size_; 1021 } 1022 uint32_t NumDirectMethods() const { 1023 return header_.direct_methods_size_; 1024 } 1025 uint32_t NumVirtualMethods() const { 1026 return header_.virtual_methods_size_; 1027 } 1028 bool HasNextStaticField() const { 1029 return pos_ < EndOfStaticFieldsPos(); 1030 } 1031 bool HasNextInstanceField() const { 1032 return pos_ >= EndOfStaticFieldsPos() && pos_ < EndOfInstanceFieldsPos(); 1033 } 1034 bool HasNextDirectMethod() const { 1035 return pos_ >= EndOfInstanceFieldsPos() && pos_ < EndOfDirectMethodsPos(); 1036 } 1037 bool HasNextVirtualMethod() const { 1038 return pos_ >= EndOfDirectMethodsPos() && pos_ < EndOfVirtualMethodsPos(); 1039 } 1040 bool HasNext() const { 1041 return pos_ < EndOfVirtualMethodsPos(); 1042 } 1043 inline void Next() { 1044 pos_++; 1045 if (pos_ < EndOfStaticFieldsPos()) { 1046 last_idx_ = GetMemberIndex(); 1047 ReadClassDataField(); 1048 } else if (pos_ == EndOfStaticFieldsPos() && NumInstanceFields() > 0) { 1049 last_idx_ = 0; // transition to next array, reset last index 1050 ReadClassDataField(); 1051 } else if (pos_ < EndOfInstanceFieldsPos()) { 1052 last_idx_ = GetMemberIndex(); 1053 ReadClassDataField(); 1054 } else if (pos_ == EndOfInstanceFieldsPos() && NumDirectMethods() > 0) { 1055 last_idx_ = 0; // transition to next array, reset last index 1056 ReadClassDataMethod(); 1057 } else if (pos_ < EndOfDirectMethodsPos()) { 1058 last_idx_ = GetMemberIndex(); 1059 ReadClassDataMethod(); 1060 } else if (pos_ == EndOfDirectMethodsPos() && NumVirtualMethods() > 0) { 1061 last_idx_ = 0; // transition to next array, reset last index 1062 ReadClassDataMethod(); 1063 } else if (pos_ < EndOfVirtualMethodsPos()) { 1064 last_idx_ = GetMemberIndex(); 1065 ReadClassDataMethod(); 1066 } else { 1067 DCHECK(!HasNext()); 1068 } 1069 } 1070 uint32_t GetMemberIndex() const { 1071 if (pos_ < EndOfInstanceFieldsPos()) { 1072 return last_idx_ + field_.field_idx_delta_; 1073 } else { 1074 DCHECK_LT(pos_, EndOfVirtualMethodsPos()); 1075 return last_idx_ + method_.method_idx_delta_; 1076 } 1077 } 1078 uint32_t GetMemberAccessFlags() const { 1079 if (pos_ < EndOfInstanceFieldsPos()) { 1080 return field_.access_flags_; 1081 } else { 1082 DCHECK_LT(pos_, EndOfVirtualMethodsPos()); 1083 return method_.access_flags_; 1084 } 1085 } 1086 InvokeType GetMethodInvokeType(const DexFile::ClassDef& class_def) const { 1087 if (HasNextDirectMethod()) { 1088 if ((GetMemberAccessFlags() & kAccStatic) != 0) { 1089 return kStatic; 1090 } else { 1091 return kDirect; 1092 } 1093 } else { 1094 DCHECK_EQ(GetMemberAccessFlags() & kAccStatic, 0U); 1095 if ((class_def.access_flags_ & kAccInterface) != 0) { 1096 return kInterface; 1097 } else if ((GetMemberAccessFlags() & kAccConstructor) != 0) { 1098 return kSuper; 1099 } else { 1100 return kVirtual; 1101 } 1102 } 1103 } 1104 const DexFile::CodeItem* GetMethodCodeItem() const { 1105 return dex_file_.GetCodeItem(method_.code_off_); 1106 } 1107 uint32_t GetMethodCodeItemOffset() const { 1108 return method_.code_off_; 1109 } 1110 const byte* EndDataPointer() const { 1111 CHECK(!HasNext()); 1112 return ptr_pos_; 1113 } 1114 1115 private: 1116 // A dex file's class_data_item is leb128 encoded, this structure holds a decoded form of the 1117 // header for a class_data_item 1118 struct ClassDataHeader { 1119 uint32_t static_fields_size_; // the number of static fields 1120 uint32_t instance_fields_size_; // the number of instance fields 1121 uint32_t direct_methods_size_; // the number of direct methods 1122 uint32_t virtual_methods_size_; // the number of virtual methods 1123 } header_; 1124 1125 // Read and decode header from a class_data_item stream into header 1126 void ReadClassDataHeader(); 1127 1128 uint32_t EndOfStaticFieldsPos() const { 1129 return header_.static_fields_size_; 1130 } 1131 uint32_t EndOfInstanceFieldsPos() const { 1132 return EndOfStaticFieldsPos() + header_.instance_fields_size_; 1133 } 1134 uint32_t EndOfDirectMethodsPos() const { 1135 return EndOfInstanceFieldsPos() + header_.direct_methods_size_; 1136 } 1137 uint32_t EndOfVirtualMethodsPos() const { 1138 return EndOfDirectMethodsPos() + header_.virtual_methods_size_; 1139 } 1140 1141 // A decoded version of the field of a class_data_item 1142 struct ClassDataField { 1143 uint32_t field_idx_delta_; // delta of index into the field_ids array for FieldId 1144 uint32_t access_flags_; // access flags for the field 1145 ClassDataField() : field_idx_delta_(0), access_flags_(0) {} 1146 1147 private: 1148 DISALLOW_COPY_AND_ASSIGN(ClassDataField); 1149 }; 1150 ClassDataField field_; 1151 1152 // Read and decode a field from a class_data_item stream into field 1153 void ReadClassDataField(); 1154 1155 // A decoded version of the method of a class_data_item 1156 struct ClassDataMethod { 1157 uint32_t method_idx_delta_; // delta of index into the method_ids array for MethodId 1158 uint32_t access_flags_; 1159 uint32_t code_off_; 1160 ClassDataMethod() : method_idx_delta_(0), access_flags_(0), code_off_(0) {} 1161 1162 private: 1163 DISALLOW_COPY_AND_ASSIGN(ClassDataMethod); 1164 }; 1165 ClassDataMethod method_; 1166 1167 // Read and decode a method from a class_data_item stream into method 1168 void ReadClassDataMethod(); 1169 1170 const DexFile& dex_file_; 1171 size_t pos_; // integral number of items passed 1172 const byte* ptr_pos_; // pointer into stream of class_data_item 1173 uint32_t last_idx_; // last read field or method index to apply delta to 1174 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassDataItemIterator); 1175}; 1176 1177class EncodedStaticFieldValueIterator { 1178 public: 1179 EncodedStaticFieldValueIterator(const DexFile& dex_file, Handle<mirror::DexCache>* dex_cache, 1180 Handle<mirror::ClassLoader>* class_loader, 1181 ClassLinker* linker, const DexFile::ClassDef& class_def) 1182 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 1183 1184 template<bool kTransactionActive> 1185 void ReadValueToField(mirror::ArtField* field) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 1186 1187 bool HasNext() { return pos_ < array_size_; } 1188 1189 void Next(); 1190 1191 enum ValueType { 1192 kByte = 0x00, 1193 kShort = 0x02, 1194 kChar = 0x03, 1195 kInt = 0x04, 1196 kLong = 0x06, 1197 kFloat = 0x10, 1198 kDouble = 0x11, 1199 kString = 0x17, 1200 kType = 0x18, 1201 kField = 0x19, 1202 kMethod = 0x1a, 1203 kEnum = 0x1b, 1204 kArray = 0x1c, 1205 kAnnotation = 0x1d, 1206 kNull = 0x1e, 1207 kBoolean = 0x1f 1208 }; 1209 1210 private: 1211 static const byte kEncodedValueTypeMask = 0x1f; // 0b11111 1212 static const byte kEncodedValueArgShift = 5; 1213 1214 const DexFile& dex_file_; 1215 Handle<mirror::DexCache>* const dex_cache_; // Dex cache to resolve literal objects. 1216 Handle<mirror::ClassLoader>* const class_loader_; // ClassLoader to resolve types. 1217 ClassLinker* linker_; // Linker to resolve literal objects. 1218 size_t array_size_; // Size of array. 1219 size_t pos_; // Current position. 1220 const byte* ptr_; // Pointer into encoded data array. 1221 ValueType type_; // Type of current encoded value. 1222 jvalue jval_; // Value of current encoded value. 1223 DISALLOW_IMPLICIT_CONSTRUCTORS(EncodedStaticFieldValueIterator); 1224}; 1225std::ostream& operator<<(std::ostream& os, const EncodedStaticFieldValueIterator::ValueType& code); 1226 1227class CatchHandlerIterator { 1228 public: 1229 CatchHandlerIterator(const DexFile::CodeItem& code_item, uint32_t address); 1230 1231 CatchHandlerIterator(const DexFile::CodeItem& code_item, 1232 const DexFile::TryItem& try_item); 1233 1234 explicit CatchHandlerIterator(const byte* handler_data) { 1235 Init(handler_data); 1236 } 1237 1238 uint16_t GetHandlerTypeIndex() const { 1239 return handler_.type_idx_; 1240 } 1241 uint32_t GetHandlerAddress() const { 1242 return handler_.address_; 1243 } 1244 void Next(); 1245 bool HasNext() const { 1246 return remaining_count_ != -1 || catch_all_; 1247 } 1248 // End of this set of catch blocks, convenience method to locate next set of catch blocks 1249 const byte* EndDataPointer() const { 1250 CHECK(!HasNext()); 1251 return current_data_; 1252 } 1253 1254 private: 1255 void Init(const DexFile::CodeItem& code_item, int32_t offset); 1256 void Init(const byte* handler_data); 1257 1258 struct CatchHandlerItem { 1259 uint16_t type_idx_; // type index of the caught exception type 1260 uint32_t address_; // handler address 1261 } handler_; 1262 const byte *current_data_; // the current handler in dex file. 1263 int32_t remaining_count_; // number of handlers not read. 1264 bool catch_all_; // is there a handler that will catch all exceptions in case 1265 // that all typed handler does not match. 1266}; 1267 1268} // namespace art 1269 1270#endif // ART_RUNTIME_DEX_FILE_H_ 1271