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_OAT_FILE_H_ 18 #define ART_RUNTIME_OAT_FILE_H_ 19 20 #include <list> 21 #include <string> 22 #include <string_view> 23 #include <vector> 24 25 #include "base/array_ref.h" 26 #include "base/compiler_filter.h" 27 #include "base/mutex.h" 28 #include "base/os.h" 29 #include "base/safe_map.h" 30 #include "base/tracking_safe_map.h" 31 #include "class_status.h" 32 #include "dex/dex_file_layout.h" 33 #include "dex/type_lookup_table.h" 34 #include "dex/utf.h" 35 #include "index_bss_mapping.h" 36 #include "mirror/object.h" 37 38 namespace art { 39 40 class BitVector; 41 class DexFile; 42 class ElfFile; 43 class DexLayoutSections; 44 template <class MirrorType> class GcRoot; 45 class MemMap; 46 class OatDexFile; 47 class OatHeader; 48 class OatMethodOffsets; 49 class OatQuickMethodHeader; 50 class VdexFile; 51 52 namespace dex { 53 struct ClassDef; 54 } // namespace dex 55 56 namespace gc { 57 namespace collector { 58 class FakeOatFile; 59 } // namespace collector 60 } // namespace gc 61 62 // OatMethodOffsets are currently 5x32-bits=160-bits long, so if we can 63 // save even one OatMethodOffsets struct, the more complicated encoding 64 // using a bitmap pays for itself since few classes will have 160 65 // methods. 66 enum class OatClassType : uint8_t { 67 kAllCompiled = 0, // OatClass is followed by an OatMethodOffsets for each method. 68 kSomeCompiled = 1, // A bitmap of OatMethodOffsets that are present follows the OatClass. 69 kNoneCompiled = 2, // All methods are interpreted so no OatMethodOffsets are necessary. 70 kOatClassMax = 3, 71 }; 72 73 std::ostream& operator<<(std::ostream& os, OatClassType rhs); 74 75 class PACKED(4) OatMethodOffsets { 76 public: code_offset_(code_offset)77 explicit OatMethodOffsets(uint32_t code_offset = 0) : code_offset_(code_offset) {} 78 ~OatMethodOffsets()79 ~OatMethodOffsets() {} 80 81 OatMethodOffsets(const OatMethodOffsets&) = default; 82 OatMethodOffsets& operator=(const OatMethodOffsets&) = default; 83 84 uint32_t code_offset_; 85 }; 86 87 // Runtime representation of the OAT file format which holds compiler output. 88 // The class opens an OAT file from storage and maps it to memory, typically with 89 // dlopen and provides access to its internal data structures (see OatWriter for 90 // for more details about the OAT format). 91 // In the process of loading OAT, the class also loads the associated VDEX file 92 // with the input DEX files (see VdexFile for details about the VDEX format). 93 // The raw DEX data are accessible transparently through the OatDexFile objects. 94 95 class OatFile { 96 public: 97 // Open an oat file. Returns null on failure. 98 // The `dex_filenames` argument, if provided, overrides the dex locations 99 // from oat file when opening the dex files if they are not embedded in the 100 // vdex file. These may differ for cross-compilation (the dex file name is 101 // the host path and dex location is the future path on target) and testing. 102 static OatFile* Open(int zip_fd, 103 const std::string& filename, 104 const std::string& location, 105 bool executable, 106 bool low_4gb, 107 ArrayRef<const std::string> dex_filenames, 108 /*inout*/MemMap* reservation, // Where to load if not null. 109 /*out*/std::string* error_msg); 110 // Helper overload that takes a single dex filename and no reservation. Open(int zip_fd,const std::string & filename,const std::string & location,bool executable,bool low_4gb,const std::string & dex_filename,std::string * error_msg)111 static OatFile* Open(int zip_fd, 112 const std::string& filename, 113 const std::string& location, 114 bool executable, 115 bool low_4gb, 116 const std::string& dex_filename, 117 /*out*/std::string* error_msg) { 118 return Open(zip_fd, 119 filename, 120 location, 121 executable, 122 low_4gb, 123 ArrayRef<const std::string>(&dex_filename, /*size=*/ 1u), 124 /*reservation=*/ nullptr, 125 error_msg); 126 } 127 // Helper overload that takes no dex filename and no reservation. Open(int zip_fd,const std::string & filename,const std::string & location,bool executable,bool low_4gb,std::string * error_msg)128 static OatFile* Open(int zip_fd, 129 const std::string& filename, 130 const std::string& location, 131 bool executable, 132 bool low_4gb, 133 /*out*/std::string* error_msg) { 134 return Open(zip_fd, 135 filename, 136 location, 137 executable, 138 low_4gb, 139 ArrayRef<const std::string>(), 140 /*reservation=*/ nullptr, 141 error_msg); 142 } 143 144 // Similar to OatFile::Open(const std::string...), but accepts input vdex and 145 // odex files as file descriptors. We also take zip_fd in case the vdex does not 146 // contain the dex code, and we need to read it from the zip file. 147 static OatFile* Open(int zip_fd, 148 int vdex_fd, 149 int oat_fd, 150 const std::string& oat_location, 151 bool executable, 152 bool low_4gb, 153 ArrayRef<const std::string> dex_filenames, 154 /*inout*/MemMap* reservation, // Where to load if not null. 155 /*out*/std::string* error_msg); 156 157 // Initialize OatFile instance from an already loaded VdexFile. This assumes 158 // the vdex does not have a dex section and accepts a vector of DexFiles separately. 159 static OatFile* OpenFromVdex(const std::vector<const DexFile*>& dex_files, 160 std::unique_ptr<VdexFile>&& vdex_file, 161 const std::string& location); 162 163 // Initialize OatFile instance from an already loaded VdexFile. The dex files 164 // will be opened through `zip_fd` or `dex_location` if `zip_fd` is -1. 165 static OatFile* OpenFromVdex(int zip_fd, 166 std::unique_ptr<VdexFile>&& vdex_file, 167 const std::string& location, 168 std::string* error_msg); 169 170 // Return whether the `OatFile` uses a vdex-only file. 171 bool IsBackedByVdexOnly() const; 172 173 virtual ~OatFile(); 174 IsExecutable()175 bool IsExecutable() const { 176 return is_executable_; 177 } 178 179 // Indicates whether the oat file was compiled with full debugging capability. 180 bool IsDebuggable() const; 181 182 CompilerFilter::Filter GetCompilerFilter() const; 183 184 std::string GetClassLoaderContext() const; 185 186 const char* GetCompilationReason() const; 187 GetLocation()188 const std::string& GetLocation() const { 189 return location_; 190 } 191 192 const OatHeader& GetOatHeader() const; 193 194 class OatMethod final { 195 public: 196 uint32_t GetCodeOffset() const; 197 198 const void* GetQuickCode() const; 199 200 // Returns size of quick code. 201 uint32_t GetQuickCodeSize() const; 202 203 // Returns OatQuickMethodHeader for debugging. Most callers should 204 // use more specific methods such as GetQuickCodeSize. 205 const OatQuickMethodHeader* GetOatQuickMethodHeader() const; 206 uint32_t GetOatQuickMethodHeaderOffset() const; 207 208 size_t GetFrameSizeInBytes() const; 209 uint32_t GetCoreSpillMask() const; 210 uint32_t GetFpSpillMask() const; 211 212 const uint8_t* GetVmapTable() const; 213 uint32_t GetVmapTableOffset() const; 214 215 // Create an OatMethod with offsets relative to the given base address OatMethod(const uint8_t * base,const uint32_t code_offset)216 OatMethod(const uint8_t* base, const uint32_t code_offset) 217 : begin_(base), code_offset_(code_offset) { 218 } 219 OatMethod(const OatMethod&) = default; ~OatMethod()220 ~OatMethod() {} 221 222 OatMethod& operator=(const OatMethod&) = default; 223 224 // A representation of an invalid OatMethod, used when an OatMethod or OatClass can't be found. 225 // See ClassLinker::FindOatMethodFor. Invalid()226 static const OatMethod Invalid() { 227 return OatMethod(nullptr, -1); 228 } 229 230 private: 231 template<class T> GetOatPointer(uint32_t offset)232 T GetOatPointer(uint32_t offset) const { 233 if (offset == 0) { 234 return nullptr; 235 } 236 return reinterpret_cast<T>(begin_ + offset); 237 } 238 239 const uint8_t* begin_; 240 uint32_t code_offset_; 241 242 friend class OatClass; 243 }; 244 245 class OatClass final { 246 public: GetStatus()247 ClassStatus GetStatus() const { 248 return status_; 249 } 250 GetType()251 OatClassType GetType() const { 252 return type_; 253 } 254 255 // Get the OatMethod entry based on its index into the class 256 // defintion. Direct methods come first, followed by virtual 257 // methods. Note that runtime created methods such as miranda 258 // methods are not included. 259 const OatMethod GetOatMethod(uint32_t method_index) const; 260 261 // Return a pointer to the OatMethodOffsets for the requested 262 // method_index, or null if none is present. Note that most 263 // callers should use GetOatMethod. 264 const OatMethodOffsets* GetOatMethodOffsets(uint32_t method_index) const; 265 266 // Return the offset from the start of the OatFile to the 267 // OatMethodOffsets for the requested method_index, or 0 if none 268 // is present. Note that most callers should use GetOatMethod. 269 uint32_t GetOatMethodOffsetsOffset(uint32_t method_index) const; 270 271 // A representation of an invalid OatClass, used when an OatClass can't be found. 272 // See FindOatClass(). Invalid()273 static OatClass Invalid() { 274 return OatClass(/* oat_file= */ nullptr, 275 ClassStatus::kErrorUnresolved, 276 OatClassType::kNoneCompiled, 277 /* bitmap_size= */ 0, 278 /* bitmap_pointer= */ nullptr, 279 /* methods_pointer= */ nullptr); 280 } 281 282 private: 283 OatClass(const OatFile* oat_file, 284 ClassStatus status, 285 OatClassType type, 286 uint32_t num_methods, 287 const uint32_t* bitmap_pointer, 288 const OatMethodOffsets* methods_pointer); 289 290 const OatFile* const oat_file_; 291 const ClassStatus status_; 292 const OatClassType type_; 293 const uint32_t num_methods_; 294 const uint32_t* const bitmap_; 295 const OatMethodOffsets* const methods_pointer_; 296 297 friend class art::OatDexFile; 298 }; 299 300 // Get the OatDexFile for the given dex_location within this oat file. 301 // If dex_location_checksum is non-null, the OatDexFile will only be 302 // returned if it has a matching checksum. 303 // If error_msg is non-null and no OatDexFile is returned, error_msg will 304 // be updated with a description of why no OatDexFile was returned. 305 const OatDexFile* GetOatDexFile(const char* dex_location, 306 const uint32_t* const dex_location_checksum, 307 /*out*/std::string* error_msg = nullptr) const 308 REQUIRES(!secondary_lookup_lock_); 309 GetOatDexFiles()310 const std::vector<const OatDexFile*>& GetOatDexFiles() const { 311 return oat_dex_files_storage_; 312 } 313 Size()314 size_t Size() const { 315 return End() - Begin(); 316 } 317 Contains(const void * p)318 bool Contains(const void* p) const { 319 return p >= Begin() && p < End(); 320 } 321 DataBimgRelRoSize()322 size_t DataBimgRelRoSize() const { 323 return DataBimgRelRoEnd() - DataBimgRelRoBegin(); 324 } 325 BssSize()326 size_t BssSize() const { 327 return BssEnd() - BssBegin(); 328 } 329 VdexSize()330 size_t VdexSize() const { 331 return VdexEnd() - VdexBegin(); 332 } 333 BssMethodsOffset()334 size_t BssMethodsOffset() const { 335 // Note: This is used only for symbolizer and needs to return a valid .bss offset. 336 return (bss_methods_ != nullptr) ? bss_methods_ - BssBegin() : BssRootsOffset(); 337 } 338 BssRootsOffset()339 size_t BssRootsOffset() const { 340 // Note: This is used only for symbolizer and needs to return a valid .bss offset. 341 return (bss_roots_ != nullptr) ? bss_roots_ - BssBegin() : BssSize(); 342 } 343 DexSize()344 size_t DexSize() const { 345 return DexEnd() - DexBegin(); 346 } 347 348 const uint8_t* Begin() const; 349 const uint8_t* End() const; 350 DataBimgRelRoBegin()351 const uint8_t* DataBimgRelRoBegin() const { return data_bimg_rel_ro_begin_; } DataBimgRelRoEnd()352 const uint8_t* DataBimgRelRoEnd() const { return data_bimg_rel_ro_end_; } 353 BssBegin()354 const uint8_t* BssBegin() const { return bss_begin_; } BssEnd()355 const uint8_t* BssEnd() const { return bss_end_; } 356 VdexBegin()357 const uint8_t* VdexBegin() const { return vdex_begin_; } VdexEnd()358 const uint8_t* VdexEnd() const { return vdex_end_; } 359 360 const uint8_t* DexBegin() const; 361 const uint8_t* DexEnd() const; 362 363 ArrayRef<const uint32_t> GetBootImageRelocations() const; 364 ArrayRef<ArtMethod*> GetBssMethods() const; 365 ArrayRef<GcRoot<mirror::Object>> GetBssGcRoots() const; 366 367 // Initialize relocation sections (.data.bimg.rel.ro and .bss). 368 void InitializeRelocations() const; 369 370 // Finds the associated oat class for a dex_file and descriptor. Returns an invalid OatClass on 371 // error and sets found to false. 372 static OatClass FindOatClass(const DexFile& dex_file, uint16_t class_def_idx, bool* found); 373 GetVdexFile()374 VdexFile* GetVdexFile() const { 375 return vdex_.get(); 376 } 377 378 // Whether the OatFile embeds the Dex code. ContainsDexCode()379 bool ContainsDexCode() const { 380 return external_dex_files_.empty(); 381 } 382 383 // Returns whether an image (e.g. app image) is required to safely execute this OAT file. 384 bool RequiresImage() const; 385 386 protected: 387 OatFile(const std::string& filename, bool executable); 388 389 private: 390 // The oat file name. 391 // 392 // The image will embed this to link its associated oat file. 393 const std::string location_; 394 395 // Pointer to the Vdex file with the Dex files for this Oat file. 396 std::unique_ptr<VdexFile> vdex_; 397 398 // Pointer to OatHeader. 399 const uint8_t* begin_; 400 401 // Pointer to end of oat region for bounds checking. 402 const uint8_t* end_; 403 404 // Pointer to the .data.bimg.rel.ro section, if present, otherwise null. 405 const uint8_t* data_bimg_rel_ro_begin_; 406 407 // Pointer to the end of the .data.bimg.rel.ro section, if present, otherwise null. 408 const uint8_t* data_bimg_rel_ro_end_; 409 410 // Pointer to the .bss section, if present, otherwise null. 411 uint8_t* bss_begin_; 412 413 // Pointer to the end of the .bss section, if present, otherwise null. 414 uint8_t* bss_end_; 415 416 // Pointer to the beginning of the ArtMethod*s in .bss section, if present, otherwise null. 417 uint8_t* bss_methods_; 418 419 // Pointer to the beginning of the GC roots in .bss section, if present, otherwise null. 420 uint8_t* bss_roots_; 421 422 // Was this oat_file loaded executable? 423 const bool is_executable_; 424 425 // Pointer to the .vdex section, if present, otherwise null. 426 uint8_t* vdex_begin_; 427 428 // Pointer to the end of the .vdex section, if present, otherwise null. 429 uint8_t* vdex_end_; 430 431 // Owning storage for the OatDexFile objects. 432 std::vector<const OatDexFile*> oat_dex_files_storage_; 433 434 // NOTE: We use a std::string_view as the key type to avoid a memory allocation on every 435 // lookup with a const char* key. The std::string_view doesn't own its backing storage, 436 // therefore we're using the OatFile's stored dex location as the backing storage 437 // for keys in oat_dex_files_ and the string_cache_ entries for the backing storage 438 // of keys in secondary_oat_dex_files_ and oat_dex_files_by_canonical_location_. 439 using Table = 440 AllocationTrackingSafeMap<std::string_view, const OatDexFile*, kAllocatorTagOatFile>; 441 442 // Map each location and canonical location (if different) retrieved from the 443 // oat file to its OatDexFile. This map doesn't change after it's constructed in Setup() 444 // and therefore doesn't need any locking and provides the cheapest dex file lookup 445 // for GetOatDexFile() for a very frequent use case. Never contains a null value. 446 Table oat_dex_files_; 447 448 // Lock guarding all members needed for secondary lookup in GetOatDexFile(). 449 mutable Mutex secondary_lookup_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; 450 451 // If the primary oat_dex_files_ lookup fails, use a secondary map. This map stores 452 // the results of all previous secondary lookups, whether successful (non-null) or 453 // failed (null). If it doesn't contain an entry we need to calculate the canonical 454 // location and use oat_dex_files_by_canonical_location_. 455 mutable Table secondary_oat_dex_files_ GUARDED_BY(secondary_lookup_lock_); 456 457 // Cache of strings. Contains the backing storage for keys in the secondary_oat_dex_files_ 458 // and the lazily initialized oat_dex_files_by_canonical_location_. 459 // NOTE: We're keeping references to contained strings in form of std::string_view and adding 460 // new strings to the end. The adding of a new element must not touch any previously stored 461 // elements. std::list<> and std::deque<> satisfy this requirement, std::vector<> doesn't. 462 mutable std::list<std::string> string_cache_ GUARDED_BY(secondary_lookup_lock_); 463 464 // Dex files opened directly from a file referenced from the oat file or specifed 465 // by the `dex_filenames` parameter, in case the OatFile does not embed the dex code. 466 std::vector<std::unique_ptr<const DexFile>> external_dex_files_; 467 468 friend class gc::collector::FakeOatFile; // For modifying begin_ and end_. 469 friend class OatClass; 470 friend class art::OatDexFile; 471 friend class OatDumper; // For GetBase and GetLimit 472 friend class OatFileBackedByVdex; 473 friend class OatFileBase; 474 DISALLOW_COPY_AND_ASSIGN(OatFile); 475 }; 476 477 // OatDexFile should be an inner class of OatFile. Unfortunately, C++ doesn't 478 // support forward declarations of inner classes, and we want to 479 // forward-declare OatDexFile so that we can store an opaque pointer to an 480 // OatDexFile in DexFile. 481 class OatDexFile final { 482 public: 483 // Opens the DexFile referred to by this OatDexFile from within the containing OatFile. 484 std::unique_ptr<const DexFile> OpenDexFile(std::string* error_msg) const; 485 486 // May return null if the OatDexFile only contains a type lookup table. This case only happens 487 // for the compiler to speed up compilation, or in jitzygote. GetOatFile()488 const OatFile* GetOatFile() const { 489 return oat_file_; 490 } 491 492 // Returns the size of the DexFile refered to by this OatDexFile. 493 size_t FileSize() const; 494 495 // Returns original path of DexFile that was the source of this OatDexFile. GetDexFileLocation()496 const std::string& GetDexFileLocation() const { 497 return dex_file_location_; 498 } 499 500 // Returns the canonical location of DexFile that was the source of this OatDexFile. GetCanonicalDexFileLocation()501 const std::string& GetCanonicalDexFileLocation() const { 502 return canonical_dex_file_location_; 503 } 504 505 // Returns checksum of original DexFile that was the source of this OatDexFile; GetDexFileLocationChecksum()506 uint32_t GetDexFileLocationChecksum() const { 507 return dex_file_location_checksum_; 508 } 509 510 // Returns the OatClass for the class specified by the given DexFile class_def_index. 511 OatFile::OatClass GetOatClass(uint16_t class_def_index) const; 512 513 // Returns the offset to the OatClass information. Most callers should use GetOatClass. 514 uint32_t GetOatClassOffset(uint16_t class_def_index) const; 515 GetLookupTableData()516 const uint8_t* GetLookupTableData() const { 517 return lookup_table_data_; 518 } 519 GetMethodBssMapping()520 const IndexBssMapping* GetMethodBssMapping() const { 521 return method_bss_mapping_; 522 } 523 GetTypeBssMapping()524 const IndexBssMapping* GetTypeBssMapping() const { 525 return type_bss_mapping_; 526 } 527 GetPublicTypeBssMapping()528 const IndexBssMapping* GetPublicTypeBssMapping() const { 529 return public_type_bss_mapping_; 530 } 531 GetPackageTypeBssMapping()532 const IndexBssMapping* GetPackageTypeBssMapping() const { 533 return package_type_bss_mapping_; 534 } 535 GetStringBssMapping()536 const IndexBssMapping* GetStringBssMapping() const { 537 return string_bss_mapping_; 538 } 539 GetDexFilePointer()540 const uint8_t* GetDexFilePointer() const { 541 return dex_file_pointer_; 542 } 543 544 // Looks up a class definition by its class descriptor. Hash must be 545 // ComputeModifiedUtf8Hash(descriptor). 546 static const dex::ClassDef* FindClassDef(const DexFile& dex_file, 547 const char* descriptor, 548 size_t hash); 549 550 // Madvise the dex file based on the state we are moving to. 551 static void MadviseDexFile(const DexFile& dex_file, MadviseState state); 552 GetTypeLookupTable()553 const TypeLookupTable& GetTypeLookupTable() const { 554 return lookup_table_; 555 } 556 557 ~OatDexFile(); 558 559 // Create only with a type lookup table, used by the compiler to speed up compilation. 560 explicit OatDexFile(TypeLookupTable&& lookup_table); 561 562 // Return the dex layout sections. GetDexLayoutSections()563 const DexLayoutSections* GetDexLayoutSections() const { 564 return dex_layout_sections_; 565 } 566 567 private: 568 OatDexFile(const OatFile* oat_file, 569 const std::string& dex_file_location, 570 const std::string& canonical_dex_file_location, 571 uint32_t dex_file_checksum, 572 const uint8_t* dex_file_pointer, 573 const uint8_t* lookup_table_data, 574 const IndexBssMapping* method_bss_mapping, 575 const IndexBssMapping* type_bss_mapping, 576 const IndexBssMapping* public_type_bss_mapping, 577 const IndexBssMapping* package_type_bss_mapping, 578 const IndexBssMapping* string_bss_mapping, 579 const uint32_t* oat_class_offsets_pointer, 580 const DexLayoutSections* dex_layout_sections); 581 582 // Create an OatDexFile wrapping an existing DexFile. Will set the OatDexFile 583 // pointer in the DexFile. 584 OatDexFile(const OatFile* oat_file, 585 const uint8_t* dex_file_pointer, 586 uint32_t dex_file_checksum, 587 const std::string& dex_file_location, 588 const std::string& canonical_dex_file_location, 589 const uint8_t* lookup_table_data); 590 591 bool IsBackedByVdexOnly() const; 592 void InitializeTypeLookupTable(); 593 594 static void AssertAotCompiler(); 595 596 const OatFile* const oat_file_ = nullptr; 597 const std::string dex_file_location_; 598 const std::string canonical_dex_file_location_; 599 const uint32_t dex_file_location_checksum_ = 0u; 600 const uint8_t* const dex_file_pointer_ = nullptr; 601 const uint8_t* const lookup_table_data_ = nullptr; 602 const IndexBssMapping* const method_bss_mapping_ = nullptr; 603 const IndexBssMapping* const type_bss_mapping_ = nullptr; 604 const IndexBssMapping* const public_type_bss_mapping_ = nullptr; 605 const IndexBssMapping* const package_type_bss_mapping_ = nullptr; 606 const IndexBssMapping* const string_bss_mapping_ = nullptr; 607 const uint32_t* const oat_class_offsets_pointer_ = nullptr; 608 TypeLookupTable lookup_table_; 609 const DexLayoutSections* const dex_layout_sections_ = nullptr; 610 611 friend class OatFile; 612 friend class OatFileBase; 613 friend class OatFileBackedByVdex; 614 DISALLOW_COPY_AND_ASSIGN(OatDexFile); 615 }; 616 617 } // namespace art 618 619 #endif // ART_RUNTIME_OAT_FILE_H_ 620