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 #include "oat_file.h" 18 19 #include <dlfcn.h> 20 #ifndef __APPLE__ 21 #include <link.h> // for dl_iterate_phdr. 22 #endif 23 #include <unistd.h> 24 25 #include <cstdlib> 26 #include <cstring> 27 #include <sstream> 28 #include <type_traits> 29 #include <sys/stat.h> 30 31 // dlopen_ext support from bionic. 32 #ifdef ART_TARGET_ANDROID 33 #include "android/dlext.h" 34 #endif 35 36 #include <android-base/logging.h> 37 #include "android-base/stringprintf.h" 38 39 #include "arch/instruction_set_features.h" 40 #include "art_method.h" 41 #include "base/bit_vector.h" 42 #include "base/enums.h" 43 #include "base/file_utils.h" 44 #include "base/logging.h" // For VLOG_IS_ON. 45 #include "base/mem_map.h" 46 #include "base/os.h" 47 #include "base/stl_util.h" 48 #include "base/systrace.h" 49 #include "base/unix_file/fd_file.h" 50 #include "base/utils.h" 51 #include "dex/art_dex_file_loader.h" 52 #include "dex/dex_file.h" 53 #include "dex/dex_file_loader.h" 54 #include "dex/dex_file_structs.h" 55 #include "dex/dex_file_types.h" 56 #include "dex/standard_dex_file.h" 57 #include "dex/type_lookup_table.h" 58 #include "dex/utf-inl.h" 59 #include "elf/elf_utils.h" 60 #include "elf_file.h" 61 #include "gc_root.h" 62 #include "gc/heap.h" 63 #include "gc/space/image_space.h" 64 #include "mirror/class.h" 65 #include "mirror/object-inl.h" 66 #include "oat.h" 67 #include "oat_file-inl.h" 68 #include "oat_file_manager.h" 69 #include "runtime-inl.h" 70 #include "vdex_file.h" 71 #include "verifier/verifier_deps.h" 72 73 namespace art { 74 75 using android::base::StringPrintf; 76 77 // Whether OatFile::Open will try dlopen. Fallback is our own ELF loader. 78 static constexpr bool kUseDlopen = true; 79 80 // Whether OatFile::Open will try dlopen on the host. On the host we're not linking against 81 // bionic, so cannot take advantage of the support for changed semantics (loading the same soname 82 // multiple times). However, if/when we switch the above, we likely want to switch this, too, 83 // to get test coverage of the code paths. 84 static constexpr bool kUseDlopenOnHost = true; 85 86 // For debugging, Open will print DlOpen error message if set to true. 87 static constexpr bool kPrintDlOpenErrorMessage = false; 88 89 // Note for OatFileBase and descendents: 90 // 91 // These are used in OatFile::Open to try all our loaders. 92 // 93 // The process is simple: 94 // 95 // 1) Allocate an instance through the standard constructor (location, executable) 96 // 2) Load() to try to open the file. 97 // 3) ComputeFields() to populate the OatFile fields like begin_, using FindDynamicSymbolAddress. 98 // 4) PreSetup() for any steps that should be done before the final setup. 99 // 5) Setup() to complete the procedure. 100 101 class OatFileBase : public OatFile { 102 public: 103 virtual ~OatFileBase() {} 104 105 template <typename kOatFileBaseSubType> 106 static OatFileBase* OpenOatFile(int zip_fd, 107 const std::string& vdex_filename, 108 const std::string& elf_filename, 109 const std::string& location, 110 bool writable, 111 bool executable, 112 bool low_4gb, 113 const char* abs_dex_location, 114 /*inout*/MemMap* reservation, // Where to load if not null. 115 /*out*/std::string* error_msg); 116 117 template <typename kOatFileBaseSubType> 118 static OatFileBase* OpenOatFile(int zip_fd, 119 int vdex_fd, 120 int oat_fd, 121 const std::string& vdex_filename, 122 const std::string& oat_filename, 123 bool writable, 124 bool executable, 125 bool low_4gb, 126 const char* abs_dex_location, 127 /*inout*/MemMap* reservation, // Where to load if not null. 128 /*out*/std::string* error_msg); 129 130 protected: 131 OatFileBase(const std::string& filename, bool executable) : OatFile(filename, executable) {} 132 133 virtual const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name, 134 std::string* error_msg) const = 0; 135 136 virtual void PreLoad() = 0; 137 138 bool LoadVdex(const std::string& vdex_filename, 139 bool writable, 140 bool low_4gb, 141 std::string* error_msg); 142 143 bool LoadVdex(int vdex_fd, 144 const std::string& vdex_filename, 145 bool writable, 146 bool low_4gb, 147 std::string* error_msg); 148 149 virtual bool Load(const std::string& elf_filename, 150 bool writable, 151 bool executable, 152 bool low_4gb, 153 /*inout*/MemMap* reservation, // Where to load if not null. 154 /*out*/std::string* error_msg) = 0; 155 156 virtual bool Load(int oat_fd, 157 bool writable, 158 bool executable, 159 bool low_4gb, 160 /*inout*/MemMap* reservation, // Where to load if not null. 161 /*out*/std::string* error_msg) = 0; 162 163 bool ComputeFields(const std::string& file_path, std::string* error_msg); 164 165 virtual void PreSetup(const std::string& elf_filename) = 0; 166 167 bool Setup(int zip_fd, const char* abs_dex_location, std::string* error_msg); 168 bool Setup(const std::vector<const DexFile*>& dex_files); 169 170 // Setters exposed for ElfOatFile. 171 172 void SetBegin(const uint8_t* begin) { 173 begin_ = begin; 174 } 175 176 void SetEnd(const uint8_t* end) { 177 end_ = end; 178 } 179 180 void SetVdex(VdexFile* vdex) { 181 vdex_.reset(vdex); 182 } 183 184 private: 185 DISALLOW_COPY_AND_ASSIGN(OatFileBase); 186 }; 187 188 template <typename kOatFileBaseSubType> 189 OatFileBase* OatFileBase::OpenOatFile(int zip_fd, 190 const std::string& vdex_filename, 191 const std::string& elf_filename, 192 const std::string& location, 193 bool writable, 194 bool executable, 195 bool low_4gb, 196 const char* abs_dex_location, 197 /*inout*/MemMap* reservation, 198 /*out*/std::string* error_msg) { 199 std::unique_ptr<OatFileBase> ret(new kOatFileBaseSubType(location, executable)); 200 201 ret->PreLoad(); 202 203 if (!ret->Load(elf_filename, 204 writable, 205 executable, 206 low_4gb, 207 reservation, 208 error_msg)) { 209 return nullptr; 210 } 211 212 if (!ret->ComputeFields(elf_filename, error_msg)) { 213 return nullptr; 214 } 215 216 ret->PreSetup(elf_filename); 217 218 if (!ret->LoadVdex(vdex_filename, writable, low_4gb, error_msg)) { 219 return nullptr; 220 } 221 222 if (!ret->Setup(zip_fd, abs_dex_location, error_msg)) { 223 return nullptr; 224 } 225 226 return ret.release(); 227 } 228 229 template <typename kOatFileBaseSubType> 230 OatFileBase* OatFileBase::OpenOatFile(int zip_fd, 231 int vdex_fd, 232 int oat_fd, 233 const std::string& vdex_location, 234 const std::string& oat_location, 235 bool writable, 236 bool executable, 237 bool low_4gb, 238 const char* abs_dex_location, 239 /*inout*/MemMap* reservation, 240 /*out*/std::string* error_msg) { 241 std::unique_ptr<OatFileBase> ret(new kOatFileBaseSubType(oat_location, executable)); 242 243 if (!ret->Load(oat_fd, 244 writable, 245 executable, 246 low_4gb, 247 reservation, 248 error_msg)) { 249 return nullptr; 250 } 251 252 if (!ret->ComputeFields(oat_location, error_msg)) { 253 return nullptr; 254 } 255 256 ret->PreSetup(oat_location); 257 258 if (!ret->LoadVdex(vdex_fd, vdex_location, writable, low_4gb, error_msg)) { 259 return nullptr; 260 } 261 262 if (!ret->Setup(zip_fd, abs_dex_location, error_msg)) { 263 return nullptr; 264 } 265 266 return ret.release(); 267 } 268 269 bool OatFileBase::LoadVdex(const std::string& vdex_filename, 270 bool writable, 271 bool low_4gb, 272 std::string* error_msg) { 273 vdex_ = VdexFile::OpenAtAddress(vdex_begin_, 274 vdex_end_ - vdex_begin_, 275 /*mmap_reuse=*/ vdex_begin_ != nullptr, 276 vdex_filename, 277 writable, 278 low_4gb, 279 /* unquicken=*/ false, 280 error_msg); 281 if (vdex_.get() == nullptr) { 282 *error_msg = StringPrintf("Failed to load vdex file '%s' %s", 283 vdex_filename.c_str(), 284 error_msg->c_str()); 285 return false; 286 } 287 return true; 288 } 289 290 bool OatFileBase::LoadVdex(int vdex_fd, 291 const std::string& vdex_filename, 292 bool writable, 293 bool low_4gb, 294 std::string* error_msg) { 295 if (vdex_fd != -1) { 296 struct stat s; 297 int rc = TEMP_FAILURE_RETRY(fstat(vdex_fd, &s)); 298 if (rc == -1) { 299 PLOG(WARNING) << "Failed getting length of vdex file"; 300 } else { 301 vdex_ = VdexFile::OpenAtAddress(vdex_begin_, 302 vdex_end_ - vdex_begin_, 303 /*mmap_reuse=*/ vdex_begin_ != nullptr, 304 vdex_fd, 305 s.st_size, 306 vdex_filename, 307 writable, 308 low_4gb, 309 /*unquicken=*/ false, 310 error_msg); 311 if (vdex_.get() == nullptr) { 312 *error_msg = "Failed opening vdex file."; 313 return false; 314 } 315 } 316 } 317 return true; 318 } 319 320 bool OatFileBase::ComputeFields(const std::string& file_path, std::string* error_msg) { 321 std::string symbol_error_msg; 322 begin_ = FindDynamicSymbolAddress("oatdata", &symbol_error_msg); 323 if (begin_ == nullptr) { 324 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s' %s", 325 file_path.c_str(), 326 symbol_error_msg.c_str()); 327 return false; 328 } 329 end_ = FindDynamicSymbolAddress("oatlastword", &symbol_error_msg); 330 if (end_ == nullptr) { 331 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s' %s", 332 file_path.c_str(), 333 symbol_error_msg.c_str()); 334 return false; 335 } 336 // Readjust to be non-inclusive upper bound. 337 end_ += sizeof(uint32_t); 338 339 data_bimg_rel_ro_begin_ = FindDynamicSymbolAddress("oatdatabimgrelro", &symbol_error_msg); 340 if (data_bimg_rel_ro_begin_ != nullptr) { 341 data_bimg_rel_ro_end_ = 342 FindDynamicSymbolAddress("oatdatabimgrelrolastword", &symbol_error_msg); 343 if (data_bimg_rel_ro_end_ == nullptr) { 344 *error_msg = 345 StringPrintf("Failed to find oatdatabimgrelrolastword symbol in '%s'", file_path.c_str()); 346 return false; 347 } 348 // Readjust to be non-inclusive upper bound. 349 data_bimg_rel_ro_end_ += sizeof(uint32_t); 350 } 351 352 bss_begin_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbss", &symbol_error_msg)); 353 if (bss_begin_ == nullptr) { 354 // No .bss section. 355 bss_end_ = nullptr; 356 } else { 357 bss_end_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbsslastword", &symbol_error_msg)); 358 if (bss_end_ == nullptr) { 359 *error_msg = StringPrintf("Failed to find oatbsslastword symbol in '%s'", file_path.c_str()); 360 return false; 361 } 362 // Readjust to be non-inclusive upper bound. 363 bss_end_ += sizeof(uint32_t); 364 // Find bss methods if present. 365 bss_methods_ = 366 const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbssmethods", &symbol_error_msg)); 367 // Find bss roots if present. 368 bss_roots_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbssroots", &symbol_error_msg)); 369 } 370 371 vdex_begin_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatdex", &symbol_error_msg)); 372 if (vdex_begin_ == nullptr) { 373 // No .vdex section. 374 vdex_end_ = nullptr; 375 } else { 376 vdex_end_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatdexlastword", &symbol_error_msg)); 377 if (vdex_end_ == nullptr) { 378 *error_msg = StringPrintf("Failed to find oatdexlastword symbol in '%s'", file_path.c_str()); 379 return false; 380 } 381 // Readjust to be non-inclusive upper bound. 382 vdex_end_ += sizeof(uint32_t); 383 } 384 385 return true; 386 } 387 388 // Read an unaligned entry from the OatDexFile data in OatFile and advance the read 389 // position by the number of bytes read, i.e. sizeof(T). 390 // Return true on success, false if the read would go beyond the end of the OatFile. 391 template <typename T> 392 inline static bool ReadOatDexFileData(const OatFile& oat_file, 393 /*inout*/const uint8_t** oat, 394 /*out*/T* value) { 395 DCHECK(oat != nullptr); 396 DCHECK(value != nullptr); 397 DCHECK_LE(*oat, oat_file.End()); 398 if (UNLIKELY(static_cast<size_t>(oat_file.End() - *oat) < sizeof(T))) { 399 return false; 400 } 401 static_assert(std::is_trivial<T>::value, "T must be a trivial type"); 402 using unaligned_type __attribute__((__aligned__(1))) = T; 403 *value = *reinterpret_cast<const unaligned_type*>(*oat); 404 *oat += sizeof(T); 405 return true; 406 } 407 408 static bool ReadIndexBssMapping(OatFile* oat_file, 409 /*inout*/const uint8_t** oat, 410 size_t dex_file_index, 411 const std::string& dex_file_location, 412 const char* tag, 413 /*out*/const IndexBssMapping** mapping, 414 std::string* error_msg) { 415 uint32_t index_bss_mapping_offset; 416 if (UNLIKELY(!ReadOatDexFileData(*oat_file, oat, &index_bss_mapping_offset))) { 417 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated " 418 "after %s bss mapping offset", 419 oat_file->GetLocation().c_str(), 420 dex_file_index, 421 dex_file_location.c_str(), 422 tag); 423 return false; 424 } 425 const bool readable_index_bss_mapping_size = 426 index_bss_mapping_offset != 0u && 427 index_bss_mapping_offset <= oat_file->Size() && 428 IsAligned<alignof(IndexBssMapping)>(index_bss_mapping_offset) && 429 oat_file->Size() - index_bss_mapping_offset >= IndexBssMapping::ComputeSize(0); 430 const IndexBssMapping* index_bss_mapping = readable_index_bss_mapping_size 431 ? reinterpret_cast<const IndexBssMapping*>(oat_file->Begin() + index_bss_mapping_offset) 432 : nullptr; 433 if (index_bss_mapping_offset != 0u && 434 (UNLIKELY(index_bss_mapping == nullptr) || 435 UNLIKELY(index_bss_mapping->size() == 0u) || 436 UNLIKELY(oat_file->Size() - index_bss_mapping_offset < 437 IndexBssMapping::ComputeSize(index_bss_mapping->size())))) { 438 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned or " 439 " truncated %s bss mapping, offset %u of %zu, length %zu", 440 oat_file->GetLocation().c_str(), 441 dex_file_index, 442 dex_file_location.c_str(), 443 tag, 444 index_bss_mapping_offset, 445 oat_file->Size(), 446 index_bss_mapping != nullptr ? index_bss_mapping->size() : 0u); 447 return false; 448 } 449 450 *mapping = index_bss_mapping; 451 return true; 452 } 453 454 bool OatFileBase::Setup(const std::vector<const DexFile*>& dex_files) { 455 for (const DexFile* dex_file : dex_files) { 456 std::string dex_location = dex_file->GetLocation(); 457 std::string canonical_location = DexFileLoader::GetDexCanonicalLocation(dex_location.c_str()); 458 459 // Create an OatDexFile and add it to the owning container. 460 OatDexFile* oat_dex_file = new OatDexFile(this, dex_file, dex_location, canonical_location); 461 oat_dex_files_storage_.push_back(oat_dex_file); 462 463 // Add the location and canonical location (if different) to the oat_dex_files_ table. 464 std::string_view key(oat_dex_file->GetDexFileLocation()); 465 oat_dex_files_.Put(key, oat_dex_file); 466 if (canonical_location != dex_location) { 467 std::string_view canonical_key(oat_dex_file->GetCanonicalDexFileLocation()); 468 oat_dex_files_.Put(canonical_key, oat_dex_file); 469 } 470 } 471 472 return true; 473 } 474 475 bool OatFileBase::Setup(int zip_fd, const char* abs_dex_location, std::string* error_msg) { 476 if (!GetOatHeader().IsValid()) { 477 std::string cause = GetOatHeader().GetValidationErrorMessage(); 478 *error_msg = StringPrintf("Invalid oat header for '%s': %s", 479 GetLocation().c_str(), 480 cause.c_str()); 481 return false; 482 } 483 PointerSize pointer_size = GetInstructionSetPointerSize(GetOatHeader().GetInstructionSet()); 484 size_t key_value_store_size = 485 (Size() >= sizeof(OatHeader)) ? GetOatHeader().GetKeyValueStoreSize() : 0u; 486 if (Size() < sizeof(OatHeader) + key_value_store_size) { 487 *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader, " 488 "size = %zu < %zu + %zu", 489 GetLocation().c_str(), 490 Size(), 491 sizeof(OatHeader), 492 key_value_store_size); 493 return false; 494 } 495 496 size_t oat_dex_files_offset = GetOatHeader().GetOatDexFilesOffset(); 497 if (oat_dex_files_offset < GetOatHeader().GetHeaderSize() || oat_dex_files_offset > Size()) { 498 *error_msg = StringPrintf("In oat file '%s' found invalid oat dex files offset: " 499 "%zu is not in [%zu, %zu]", 500 GetLocation().c_str(), 501 oat_dex_files_offset, 502 GetOatHeader().GetHeaderSize(), 503 Size()); 504 return false; 505 } 506 const uint8_t* oat = Begin() + oat_dex_files_offset; // Jump to the OatDexFile records. 507 508 if (!IsAligned<sizeof(uint32_t)>(data_bimg_rel_ro_begin_) || 509 !IsAligned<sizeof(uint32_t)>(data_bimg_rel_ro_end_) || 510 data_bimg_rel_ro_begin_ > data_bimg_rel_ro_end_) { 511 *error_msg = StringPrintf("In oat file '%s' found unaligned or unordered databimgrelro " 512 "symbol(s): begin = %p, end = %p", 513 GetLocation().c_str(), 514 data_bimg_rel_ro_begin_, 515 data_bimg_rel_ro_end_); 516 return false; 517 } 518 519 DCHECK_GE(static_cast<size_t>(pointer_size), alignof(GcRoot<mirror::Object>)); 520 if (!IsAligned<kPageSize>(bss_begin_) || 521 !IsAlignedParam(bss_methods_, static_cast<size_t>(pointer_size)) || 522 !IsAlignedParam(bss_roots_, static_cast<size_t>(pointer_size)) || 523 !IsAligned<alignof(GcRoot<mirror::Object>)>(bss_end_)) { 524 *error_msg = StringPrintf("In oat file '%s' found unaligned bss symbol(s): " 525 "begin = %p, methods_ = %p, roots = %p, end = %p", 526 GetLocation().c_str(), 527 bss_begin_, 528 bss_methods_, 529 bss_roots_, 530 bss_end_); 531 return false; 532 } 533 534 if ((bss_methods_ != nullptr && (bss_methods_ < bss_begin_ || bss_methods_ > bss_end_)) || 535 (bss_roots_ != nullptr && (bss_roots_ < bss_begin_ || bss_roots_ > bss_end_)) || 536 (bss_methods_ != nullptr && bss_roots_ != nullptr && bss_methods_ > bss_roots_)) { 537 *error_msg = StringPrintf("In oat file '%s' found bss symbol(s) outside .bss or unordered: " 538 "begin = %p, methods = %p, roots = %p, end = %p", 539 GetLocation().c_str(), 540 bss_begin_, 541 bss_methods_, 542 bss_roots_, 543 bss_end_); 544 return false; 545 } 546 547 if (bss_methods_ != nullptr && bss_methods_ != bss_begin_) { 548 *error_msg = StringPrintf("In oat file '%s' found unexpected .bss gap before 'oatbssmethods': " 549 "begin = %p, methods = %p", 550 GetLocation().c_str(), 551 bss_begin_, 552 bss_methods_); 553 return false; 554 } 555 556 uint32_t dex_file_count = GetOatHeader().GetDexFileCount(); 557 oat_dex_files_storage_.reserve(dex_file_count); 558 for (size_t i = 0; i < dex_file_count; i++) { 559 uint32_t dex_file_location_size; 560 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_location_size))) { 561 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu truncated after dex file " 562 "location size", 563 GetLocation().c_str(), 564 i); 565 return false; 566 } 567 if (UNLIKELY(dex_file_location_size == 0U)) { 568 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with empty location name", 569 GetLocation().c_str(), 570 i); 571 return false; 572 } 573 if (UNLIKELY(static_cast<size_t>(End() - oat) < dex_file_location_size)) { 574 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with truncated dex file " 575 "location", 576 GetLocation().c_str(), 577 i); 578 return false; 579 } 580 const char* dex_file_location_data = reinterpret_cast<const char*>(oat); 581 oat += dex_file_location_size; 582 583 // Location encoded in the oat file. We will use this for multidex naming, 584 // see ResolveRelativeEncodedDexLocation. 585 std::string oat_dex_file_location(dex_file_location_data, dex_file_location_size); 586 // If `oat_dex_file_location` is relative (so that the oat file can be moved to 587 // a different folder), resolve to absolute location. Also resolve the file name 588 // in case dex files need to be opened from disk. The file name and location 589 // differ when cross-compiling on host for target. 590 std::string dex_file_name; 591 std::string dex_file_location; 592 ResolveRelativeEncodedDexLocation(abs_dex_location, 593 oat_dex_file_location, 594 &dex_file_location, 595 &dex_file_name); 596 597 uint32_t dex_file_checksum; 598 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_checksum))) { 599 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated after " 600 "dex file checksum", 601 GetLocation().c_str(), 602 i, 603 dex_file_location.c_str()); 604 return false; 605 } 606 607 uint32_t dex_file_offset; 608 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_offset))) { 609 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated " 610 "after dex file offsets", 611 GetLocation().c_str(), 612 i, 613 dex_file_location.c_str()); 614 return false; 615 } 616 if (UNLIKELY(dex_file_offset > DexSize())) { 617 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file " 618 "offset %u > %zu", 619 GetLocation().c_str(), 620 i, 621 dex_file_location.c_str(), 622 dex_file_offset, 623 DexSize()); 624 return false; 625 } 626 const uint8_t* dex_file_pointer = nullptr; 627 if (UNLIKELY(dex_file_offset == 0U)) { 628 if (uncompressed_dex_files_ == nullptr) { 629 // Do not support mixed-mode oat files. 630 if (i > 0) { 631 *error_msg = StringPrintf("In oat file '%s', unsupported uncompressed-dex-file for dex " 632 "file %zu (%s)", 633 GetLocation().c_str(), 634 i, 635 dex_file_location.c_str()); 636 return false; 637 } 638 uncompressed_dex_files_.reset(new std::vector<std::unique_ptr<const DexFile>>()); 639 // No dex files, load it from location. 640 const ArtDexFileLoader dex_file_loader; 641 bool loaded = false; 642 if (zip_fd != -1) { 643 loaded = dex_file_loader.OpenZip(zip_fd, 644 dex_file_location, 645 /*verify=*/ false, 646 /*verify_checksum=*/ false, 647 error_msg, 648 uncompressed_dex_files_.get()); 649 } else { 650 loaded = dex_file_loader.Open(dex_file_name.c_str(), 651 dex_file_location, 652 /*verify=*/ false, 653 /*verify_checksum=*/ false, 654 error_msg, 655 uncompressed_dex_files_.get()); 656 } 657 if (!loaded) { 658 if (Runtime::Current() == nullptr) { 659 // If there's no runtime, we're running oatdump, so return 660 // a half constructed oat file that oatdump knows how to deal with. 661 LOG(WARNING) << "Could not find associated dex files of oat file. " 662 << "Oatdump will only dump the header."; 663 return true; 664 } else { 665 return false; 666 } 667 } 668 // The oat file may be out of date wrt/ the dex-file location. We need to be defensive 669 // here and ensure that at least the number of dex files still matches. 670 // Note: actual checksum comparisons are the duty of the OatFileAssistant and will be 671 // done after loading the OatFile. 672 if (uncompressed_dex_files_->size() != dex_file_count) { 673 *error_msg = StringPrintf("In oat file '%s', expected %u uncompressed dex files, but " 674 "found %zu in '%s'", 675 GetLocation().c_str(), 676 dex_file_count, 677 uncompressed_dex_files_->size(), 678 dex_file_location.c_str()); 679 return false; 680 } 681 } 682 dex_file_pointer = (*uncompressed_dex_files_)[i]->Begin(); 683 } else { 684 // Do not support mixed-mode oat files. 685 if (uncompressed_dex_files_ != nullptr) { 686 *error_msg = StringPrintf("In oat file '%s', unsupported embedded dex-file for dex file " 687 "%zu (%s)", 688 GetLocation().c_str(), 689 i, 690 dex_file_location.c_str()); 691 return false; 692 } 693 if (UNLIKELY(DexSize() - dex_file_offset < sizeof(DexFile::Header))) { 694 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file " 695 "offset %u of %zu but the size of dex file header is %zu", 696 GetLocation().c_str(), 697 i, 698 dex_file_location.c_str(), 699 dex_file_offset, 700 DexSize(), 701 sizeof(DexFile::Header)); 702 return false; 703 } 704 dex_file_pointer = DexBegin() + dex_file_offset; 705 } 706 707 const bool valid_magic = DexFileLoader::IsMagicValid(dex_file_pointer); 708 if (UNLIKELY(!valid_magic)) { 709 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid " 710 "dex file magic '%s'", 711 GetLocation().c_str(), 712 i, 713 dex_file_location.c_str(), 714 dex_file_pointer); 715 return false; 716 } 717 if (UNLIKELY(!DexFileLoader::IsVersionAndMagicValid(dex_file_pointer))) { 718 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid " 719 "dex file version '%s'", 720 GetLocation().c_str(), 721 i, 722 dex_file_location.c_str(), 723 dex_file_pointer); 724 return false; 725 } 726 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer); 727 if (dex_file_offset != 0 && (DexSize() - dex_file_offset < header->file_size_)) { 728 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file " 729 "offset %u and size %u truncated at %zu", 730 GetLocation().c_str(), 731 i, 732 dex_file_location.c_str(), 733 dex_file_offset, 734 header->file_size_, 735 DexSize()); 736 return false; 737 } 738 739 uint32_t class_offsets_offset; 740 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &class_offsets_offset))) { 741 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated " 742 "after class offsets offset", 743 GetLocation().c_str(), 744 i, 745 dex_file_location.c_str()); 746 return false; 747 } 748 if (UNLIKELY(class_offsets_offset > Size()) || 749 UNLIKELY((Size() - class_offsets_offset) / sizeof(uint32_t) < header->class_defs_size_)) { 750 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated " 751 "class offsets, offset %u of %zu, class defs %u", 752 GetLocation().c_str(), 753 i, 754 dex_file_location.c_str(), 755 class_offsets_offset, 756 Size(), 757 header->class_defs_size_); 758 return false; 759 } 760 if (UNLIKELY(!IsAligned<alignof(uint32_t)>(class_offsets_offset))) { 761 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned " 762 "class offsets, offset %u", 763 GetLocation().c_str(), 764 i, 765 dex_file_location.c_str(), 766 class_offsets_offset); 767 return false; 768 } 769 const uint32_t* class_offsets_pointer = 770 reinterpret_cast<const uint32_t*>(Begin() + class_offsets_offset); 771 772 uint32_t lookup_table_offset; 773 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &lookup_table_offset))) { 774 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated " 775 "after lookup table offset", 776 GetLocation().c_str(), 777 i, 778 dex_file_location.c_str()); 779 return false; 780 } 781 const uint8_t* lookup_table_data = lookup_table_offset != 0u 782 ? Begin() + lookup_table_offset 783 : nullptr; 784 if (lookup_table_offset != 0u && 785 (UNLIKELY(lookup_table_offset > Size()) || 786 UNLIKELY(Size() - lookup_table_offset < 787 TypeLookupTable::RawDataLength(header->class_defs_size_)))) { 788 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated " 789 "type lookup table, offset %u of %zu, class defs %u", 790 GetLocation().c_str(), 791 i, 792 dex_file_location.c_str(), 793 lookup_table_offset, 794 Size(), 795 header->class_defs_size_); 796 return false; 797 } 798 799 uint32_t dex_layout_sections_offset; 800 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_layout_sections_offset))) { 801 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated " 802 "after dex layout sections offset", 803 GetLocation().c_str(), 804 i, 805 dex_file_location.c_str()); 806 return false; 807 } 808 const DexLayoutSections* const dex_layout_sections = dex_layout_sections_offset != 0 809 ? reinterpret_cast<const DexLayoutSections*>(Begin() + dex_layout_sections_offset) 810 : nullptr; 811 812 const IndexBssMapping* method_bss_mapping; 813 const IndexBssMapping* type_bss_mapping; 814 const IndexBssMapping* string_bss_mapping; 815 if (!ReadIndexBssMapping( 816 this, &oat, i, dex_file_location, "method", &method_bss_mapping, error_msg) || 817 !ReadIndexBssMapping( 818 this, &oat, i, dex_file_location, "type", &type_bss_mapping, error_msg) || 819 !ReadIndexBssMapping( 820 this, &oat, i, dex_file_location, "string", &string_bss_mapping, error_msg)) { 821 return false; 822 } 823 824 // Create the OatDexFile and add it to the owning container. 825 OatDexFile* oat_dex_file = new OatDexFile( 826 this, 827 dex_file_location, 828 DexFileLoader::GetDexCanonicalLocation(dex_file_name.c_str()), 829 dex_file_checksum, 830 dex_file_pointer, 831 lookup_table_data, 832 method_bss_mapping, 833 type_bss_mapping, 834 string_bss_mapping, 835 class_offsets_pointer, 836 dex_layout_sections); 837 oat_dex_files_storage_.push_back(oat_dex_file); 838 839 // Add the location and canonical location (if different) to the oat_dex_files_ table. 840 // Note: we use the dex_file_location_data storage for the view, as oat_dex_file_location 841 // is just a temporary string. 842 std::string_view key(dex_file_location_data, dex_file_location_size); 843 std::string_view canonical_key(oat_dex_file->GetCanonicalDexFileLocation()); 844 oat_dex_files_.Put(key, oat_dex_file); 845 if (canonical_key != key) { 846 oat_dex_files_.Put(canonical_key, oat_dex_file); 847 } 848 } 849 850 Runtime* runtime = Runtime::Current(); 851 852 if (DataBimgRelRoBegin() != nullptr) { 853 // Make .data.bimg.rel.ro read only. ClassLinker shall make it writable for relocation. 854 uint8_t* reloc_begin = const_cast<uint8_t*>(DataBimgRelRoBegin()); 855 CheckedCall(mprotect, "protect relocations", reloc_begin, DataBimgRelRoSize(), PROT_READ); 856 if (UNLIKELY(runtime == nullptr)) { 857 // This must be oatdump without boot image. 858 } else if (!IsExecutable()) { 859 // Do not check whether we have a boot image if the oat file is not executable. 860 } else if (UNLIKELY(runtime->GetHeap()->GetBootImageSpaces().empty())) { 861 *error_msg = StringPrintf("Cannot load oat file '%s' with .data.bimg.rel.ro as executable " 862 "without boot image.", 863 GetLocation().c_str()); 864 return false; 865 } else { 866 // ClassLinker shall perform the relocation when we register a dex file from 867 // this oat file. We do not do the relocation here to avoid dirtying the pages 868 // if the code is never actually ready to be executed. 869 } 870 } 871 872 return true; 873 } 874 875 //////////////////////// 876 // OatFile via dlopen // 877 //////////////////////// 878 879 class DlOpenOatFile final : public OatFileBase { 880 public: 881 DlOpenOatFile(const std::string& filename, bool executable) 882 : OatFileBase(filename, executable), 883 dlopen_handle_(nullptr), 884 shared_objects_before_(0) { 885 } 886 887 ~DlOpenOatFile() { 888 if (dlopen_handle_ != nullptr) { 889 if (!kIsTargetBuild) { 890 MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_); 891 host_dlopen_handles_.erase(dlopen_handle_); 892 dlclose(dlopen_handle_); 893 } else { 894 dlclose(dlopen_handle_); 895 } 896 } 897 } 898 899 protected: 900 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name, 901 std::string* error_msg) const override { 902 const uint8_t* ptr = 903 reinterpret_cast<const uint8_t*>(dlsym(dlopen_handle_, symbol_name.c_str())); 904 if (ptr == nullptr) { 905 *error_msg = dlerror(); 906 } 907 return ptr; 908 } 909 910 void PreLoad() override; 911 912 bool Load(const std::string& elf_filename, 913 bool writable, 914 bool executable, 915 bool low_4gb, 916 /*inout*/MemMap* reservation, // Where to load if not null. 917 /*out*/std::string* error_msg) override; 918 919 bool Load(int oat_fd ATTRIBUTE_UNUSED, 920 bool writable ATTRIBUTE_UNUSED, 921 bool executable ATTRIBUTE_UNUSED, 922 bool low_4gb ATTRIBUTE_UNUSED, 923 /*inout*/MemMap* reservation ATTRIBUTE_UNUSED, 924 /*out*/std::string* error_msg ATTRIBUTE_UNUSED) override { 925 return false; 926 } 927 928 // Ask the linker where it mmaped the file and notify our mmap wrapper of the regions. 929 void PreSetup(const std::string& elf_filename) override; 930 931 private: 932 bool Dlopen(const std::string& elf_filename, 933 /*inout*/MemMap* reservation, // Where to load if not null. 934 /*out*/std::string* error_msg); 935 936 // On the host, if the same library is loaded again with dlopen the same 937 // file handle is returned. This differs from the behavior of dlopen on the 938 // target, where dlopen reloads the library at a different address every 939 // time you load it. The runtime relies on the target behavior to ensure 940 // each instance of the loaded library has a unique dex cache. To avoid 941 // problems, we fall back to our own linker in the case when the same 942 // library is opened multiple times on host. dlopen_handles_ is used to 943 // detect that case. 944 // Guarded by host_dlopen_handles_lock_; 945 static std::unordered_set<void*> host_dlopen_handles_; 946 947 // Reservation and dummy memory map objects corresponding to the regions mapped by dlopen. 948 // Note: Must be destroyed after dlclose() as it can hold the owning reservation. 949 std::vector<MemMap> dlopen_mmaps_; 950 951 // dlopen handle during runtime. 952 void* dlopen_handle_; // TODO: Unique_ptr with custom deleter. 953 954 // The number of shared objects the linker told us about before loading. Used to 955 // (optimistically) optimize the PreSetup stage (see comment there). 956 size_t shared_objects_before_; 957 958 DISALLOW_COPY_AND_ASSIGN(DlOpenOatFile); 959 }; 960 961 std::unordered_set<void*> DlOpenOatFile::host_dlopen_handles_; 962 963 void DlOpenOatFile::PreLoad() { 964 #ifdef __APPLE__ 965 UNUSED(shared_objects_before_); 966 LOG(FATAL) << "Should not reach here."; 967 UNREACHABLE(); 968 #else 969 // Count the entries in dl_iterate_phdr we get at this point in time. 970 struct dl_iterate_context { 971 static int callback(dl_phdr_info* info ATTRIBUTE_UNUSED, 972 size_t size ATTRIBUTE_UNUSED, 973 void* data) { 974 reinterpret_cast<dl_iterate_context*>(data)->count++; 975 return 0; // Continue iteration. 976 } 977 size_t count = 0; 978 } context; 979 980 dl_iterate_phdr(dl_iterate_context::callback, &context); 981 shared_objects_before_ = context.count; 982 #endif 983 } 984 985 bool DlOpenOatFile::Load(const std::string& elf_filename, 986 bool writable, 987 bool executable, 988 bool low_4gb, 989 /*inout*/MemMap* reservation, // Where to load if not null. 990 /*out*/std::string* error_msg) { 991 // Use dlopen only when flagged to do so, and when it's OK to load things executable. 992 // TODO: Also try when not executable? The issue here could be re-mapping as writable (as 993 // !executable is a sign that we may want to patch), which may not be allowed for 994 // various reasons. 995 if (!kUseDlopen) { 996 *error_msg = "DlOpen is disabled."; 997 return false; 998 } 999 if (low_4gb) { 1000 *error_msg = "DlOpen does not support low 4gb loading."; 1001 return false; 1002 } 1003 if (writable) { 1004 *error_msg = "DlOpen does not support writable loading."; 1005 return false; 1006 } 1007 if (!executable) { 1008 *error_msg = "DlOpen does not support non-executable loading."; 1009 return false; 1010 } 1011 1012 // dlopen always returns the same library if it is already opened on the host. For this reason 1013 // we only use dlopen if we are the target or we do not already have the dex file opened. Having 1014 // the same library loaded multiple times at different addresses is required for class unloading 1015 // and for having dex caches arrays in the .bss section. 1016 if (!kIsTargetBuild) { 1017 if (!kUseDlopenOnHost) { 1018 *error_msg = "DlOpen disabled for host."; 1019 return false; 1020 } 1021 } 1022 1023 bool success = Dlopen(elf_filename, reservation, error_msg); 1024 DCHECK(dlopen_handle_ != nullptr || !success); 1025 1026 return success; 1027 } 1028 1029 bool DlOpenOatFile::Dlopen(const std::string& elf_filename, 1030 /*inout*/MemMap* reservation, 1031 /*out*/std::string* error_msg) { 1032 #ifdef __APPLE__ 1033 // The dl_iterate_phdr syscall is missing. There is similar API on OSX, 1034 // but let's fallback to the custom loading code for the time being. 1035 UNUSED(elf_filename, reservation); 1036 *error_msg = "Dlopen unsupported on Mac."; 1037 return false; 1038 #else 1039 { 1040 UniqueCPtr<char> absolute_path(realpath(elf_filename.c_str(), nullptr)); 1041 if (absolute_path == nullptr) { 1042 *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str()); 1043 return false; 1044 } 1045 #ifdef ART_TARGET_ANDROID 1046 android_dlextinfo extinfo = {}; 1047 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD; // Force-load, don't reuse handle 1048 // (open oat files multiple times). 1049 if (reservation != nullptr) { 1050 if (!reservation->IsValid()) { 1051 *error_msg = StringPrintf("Invalid reservation for %s", elf_filename.c_str()); 1052 return false; 1053 } 1054 extinfo.flags |= ANDROID_DLEXT_RESERVED_ADDRESS; // Use the reserved memory range. 1055 extinfo.reserved_addr = reservation->Begin(); 1056 extinfo.reserved_size = reservation->Size(); 1057 } 1058 dlopen_handle_ = android_dlopen_ext(absolute_path.get(), RTLD_NOW, &extinfo); 1059 if (reservation != nullptr && dlopen_handle_ != nullptr) { 1060 // Find used pages from the reservation. 1061 struct dl_iterate_context { 1062 static int callback(dl_phdr_info* info, size_t size ATTRIBUTE_UNUSED, void* data) { 1063 auto* context = reinterpret_cast<dl_iterate_context*>(data); 1064 static_assert(std::is_same<Elf32_Half, Elf64_Half>::value, "Half must match"); 1065 using Elf_Half = Elf64_Half; 1066 1067 // See whether this callback corresponds to the file which we have just loaded. 1068 uint8_t* reservation_begin = context->reservation->Begin(); 1069 bool contained_in_reservation = false; 1070 for (Elf_Half i = 0; i < info->dlpi_phnum; i++) { 1071 if (info->dlpi_phdr[i].p_type == PT_LOAD) { 1072 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr + 1073 info->dlpi_phdr[i].p_vaddr); 1074 size_t memsz = info->dlpi_phdr[i].p_memsz; 1075 size_t offset = static_cast<size_t>(vaddr - reservation_begin); 1076 if (offset < context->reservation->Size()) { 1077 contained_in_reservation = true; 1078 DCHECK_LE(memsz, context->reservation->Size() - offset); 1079 } else if (vaddr < reservation_begin) { 1080 // Check that there's no overlap with the reservation. 1081 DCHECK_LE(memsz, static_cast<size_t>(reservation_begin - vaddr)); 1082 } 1083 break; // It is sufficient to check the first PT_LOAD header. 1084 } 1085 } 1086 1087 if (contained_in_reservation) { 1088 for (Elf_Half i = 0; i < info->dlpi_phnum; i++) { 1089 if (info->dlpi_phdr[i].p_type == PT_LOAD) { 1090 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr + 1091 info->dlpi_phdr[i].p_vaddr); 1092 size_t memsz = info->dlpi_phdr[i].p_memsz; 1093 size_t offset = static_cast<size_t>(vaddr - reservation_begin); 1094 DCHECK_LT(offset, context->reservation->Size()); 1095 DCHECK_LE(memsz, context->reservation->Size() - offset); 1096 context->max_size = std::max(context->max_size, offset + memsz); 1097 } 1098 } 1099 1100 return 1; // Stop iteration and return 1 from dl_iterate_phdr. 1101 } 1102 return 0; // Continue iteration and return 0 from dl_iterate_phdr when finished. 1103 } 1104 1105 const MemMap* const reservation; 1106 size_t max_size = 0u; 1107 }; 1108 dl_iterate_context context = { reservation }; 1109 1110 if (dl_iterate_phdr(dl_iterate_context::callback, &context) == 0) { 1111 LOG(FATAL) << "Could not find the shared object mmapped to the reservation."; 1112 UNREACHABLE(); 1113 } 1114 1115 // Take ownership of the memory used by the shared object. dlopen() does not assume 1116 // full ownership of this memory and dlclose() shall just remap it as zero pages with 1117 // PROT_NONE. We need to unmap the memory when destroying this oat file. 1118 dlopen_mmaps_.push_back(reservation->TakeReservedMemory(context.max_size)); 1119 } 1120 #else 1121 static_assert(!kIsTargetBuild || kIsTargetLinux || kIsTargetFuchsia, 1122 "host_dlopen_handles_ will leak handles"); 1123 if (reservation != nullptr) { 1124 *error_msg = StringPrintf("dlopen() into reserved memory is unsupported on host for '%s'.", 1125 elf_filename.c_str()); 1126 return false; 1127 } 1128 MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_); 1129 dlopen_handle_ = dlopen(absolute_path.get(), RTLD_NOW); 1130 if (dlopen_handle_ != nullptr) { 1131 if (!host_dlopen_handles_.insert(dlopen_handle_).second) { 1132 dlclose(dlopen_handle_); 1133 dlopen_handle_ = nullptr; 1134 *error_msg = StringPrintf("host dlopen re-opened '%s'", elf_filename.c_str()); 1135 return false; 1136 } 1137 } 1138 #endif // ART_TARGET_ANDROID 1139 } 1140 if (dlopen_handle_ == nullptr) { 1141 *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror()); 1142 return false; 1143 } 1144 return true; 1145 #endif 1146 } 1147 1148 void DlOpenOatFile::PreSetup(const std::string& elf_filename) { 1149 #ifdef __APPLE__ 1150 UNUSED(elf_filename); 1151 LOG(FATAL) << "Should not reach here."; 1152 UNREACHABLE(); 1153 #else 1154 struct dl_iterate_context { 1155 static int callback(dl_phdr_info* info, size_t size ATTRIBUTE_UNUSED, void* data) { 1156 auto* context = reinterpret_cast<dl_iterate_context*>(data); 1157 static_assert(std::is_same<Elf32_Half, Elf64_Half>::value, "Half must match"); 1158 using Elf_Half = Elf64_Half; 1159 1160 context->shared_objects_seen++; 1161 if (context->shared_objects_seen < context->shared_objects_before) { 1162 // We haven't been called yet for anything we haven't seen before. Just continue. 1163 // Note: this is aggressively optimistic. If another thread was unloading a library, 1164 // we may miss out here. However, this does not happen often in practice. 1165 return 0; 1166 } 1167 1168 // See whether this callback corresponds to the file which we have just loaded. 1169 bool contains_begin = false; 1170 for (Elf_Half i = 0; i < info->dlpi_phnum; i++) { 1171 if (info->dlpi_phdr[i].p_type == PT_LOAD) { 1172 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr + 1173 info->dlpi_phdr[i].p_vaddr); 1174 size_t memsz = info->dlpi_phdr[i].p_memsz; 1175 if (vaddr <= context->begin_ && context->begin_ < vaddr + memsz) { 1176 contains_begin = true; 1177 break; 1178 } 1179 } 1180 } 1181 // Add dummy mmaps for this file. 1182 if (contains_begin) { 1183 for (Elf_Half i = 0; i < info->dlpi_phnum; i++) { 1184 if (info->dlpi_phdr[i].p_type == PT_LOAD) { 1185 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr + 1186 info->dlpi_phdr[i].p_vaddr); 1187 size_t memsz = info->dlpi_phdr[i].p_memsz; 1188 MemMap mmap = MemMap::MapDummy(info->dlpi_name, vaddr, memsz); 1189 context->dlopen_mmaps_->push_back(std::move(mmap)); 1190 } 1191 } 1192 return 1; // Stop iteration and return 1 from dl_iterate_phdr. 1193 } 1194 return 0; // Continue iteration and return 0 from dl_iterate_phdr when finished. 1195 } 1196 const uint8_t* const begin_; 1197 std::vector<MemMap>* const dlopen_mmaps_; 1198 const size_t shared_objects_before; 1199 size_t shared_objects_seen; 1200 }; 1201 dl_iterate_context context = { Begin(), &dlopen_mmaps_, shared_objects_before_, 0}; 1202 1203 if (dl_iterate_phdr(dl_iterate_context::callback, &context) == 0) { 1204 // Hm. Maybe our optimization went wrong. Try another time with shared_objects_before == 0 1205 // before giving up. This should be unusual. 1206 VLOG(oat) << "Need a second run in PreSetup, didn't find with shared_objects_before=" 1207 << shared_objects_before_; 1208 dl_iterate_context context0 = { Begin(), &dlopen_mmaps_, 0, 0}; 1209 if (dl_iterate_phdr(dl_iterate_context::callback, &context0) == 0) { 1210 // OK, give up and print an error. 1211 PrintFileToLog("/proc/self/maps", android::base::LogSeverity::WARNING); 1212 LOG(ERROR) << "File " << elf_filename << " loaded with dlopen but cannot find its mmaps."; 1213 } 1214 } 1215 #endif 1216 } 1217 1218 //////////////////////////////////////////////// 1219 // OatFile via our own ElfFile implementation // 1220 //////////////////////////////////////////////// 1221 1222 class ElfOatFile final : public OatFileBase { 1223 public: 1224 ElfOatFile(const std::string& filename, bool executable) : OatFileBase(filename, executable) {} 1225 1226 static ElfOatFile* OpenElfFile(int zip_fd, 1227 File* file, 1228 const std::string& location, 1229 bool writable, 1230 bool executable, 1231 bool low_4gb, 1232 const char* abs_dex_location, 1233 /*inout*/MemMap* reservation, // Where to load if not null. 1234 /*out*/std::string* error_msg); 1235 1236 bool InitializeFromElfFile(int zip_fd, 1237 ElfFile* elf_file, 1238 VdexFile* vdex_file, 1239 const char* abs_dex_location, 1240 std::string* error_msg); 1241 1242 protected: 1243 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name, 1244 std::string* error_msg) const override { 1245 const uint8_t* ptr = elf_file_->FindDynamicSymbolAddress(symbol_name); 1246 if (ptr == nullptr) { 1247 *error_msg = "(Internal implementation could not find symbol)"; 1248 } 1249 return ptr; 1250 } 1251 1252 void PreLoad() override { 1253 } 1254 1255 bool Load(const std::string& elf_filename, 1256 bool writable, 1257 bool executable, 1258 bool low_4gb, 1259 /*inout*/MemMap* reservation, // Where to load if not null. 1260 /*out*/std::string* error_msg) override; 1261 1262 bool Load(int oat_fd, 1263 bool writable, 1264 bool executable, 1265 bool low_4gb, 1266 /*inout*/MemMap* reservation, // Where to load if not null. 1267 /*out*/std::string* error_msg) override; 1268 1269 void PreSetup(const std::string& elf_filename ATTRIBUTE_UNUSED) override { 1270 } 1271 1272 private: 1273 bool ElfFileOpen(File* file, 1274 bool writable, 1275 bool executable, 1276 bool low_4gb, 1277 /*inout*/MemMap* reservation, // Where to load if not null. 1278 /*out*/std::string* error_msg); 1279 1280 private: 1281 // Backing memory map for oat file during cross compilation. 1282 std::unique_ptr<ElfFile> elf_file_; 1283 1284 DISALLOW_COPY_AND_ASSIGN(ElfOatFile); 1285 }; 1286 1287 ElfOatFile* ElfOatFile::OpenElfFile(int zip_fd, 1288 File* file, 1289 const std::string& location, 1290 bool writable, 1291 bool executable, 1292 bool low_4gb, 1293 const char* abs_dex_location, 1294 /*inout*/MemMap* reservation, // Where to load if not null. 1295 /*out*/std::string* error_msg) { 1296 ScopedTrace trace("Open elf file " + location); 1297 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, executable)); 1298 bool success = oat_file->ElfFileOpen(file, 1299 writable, 1300 low_4gb, 1301 executable, 1302 reservation, 1303 error_msg); 1304 if (!success) { 1305 CHECK(!error_msg->empty()); 1306 return nullptr; 1307 } 1308 1309 // Complete the setup. 1310 if (!oat_file->ComputeFields(file->GetPath(), error_msg)) { 1311 return nullptr; 1312 } 1313 1314 if (!oat_file->Setup(zip_fd, abs_dex_location, error_msg)) { 1315 return nullptr; 1316 } 1317 1318 return oat_file.release(); 1319 } 1320 1321 bool ElfOatFile::InitializeFromElfFile(int zip_fd, 1322 ElfFile* elf_file, 1323 VdexFile* vdex_file, 1324 const char* abs_dex_location, 1325 std::string* error_msg) { 1326 ScopedTrace trace(__PRETTY_FUNCTION__); 1327 if (IsExecutable()) { 1328 *error_msg = "Cannot initialize from elf file in executable mode."; 1329 return false; 1330 } 1331 elf_file_.reset(elf_file); 1332 SetVdex(vdex_file); 1333 uint64_t offset, size; 1334 bool has_section = elf_file->GetSectionOffsetAndSize(".rodata", &offset, &size); 1335 CHECK(has_section); 1336 SetBegin(elf_file->Begin() + offset); 1337 SetEnd(elf_file->Begin() + size + offset); 1338 // Ignore the optional .bss section when opening non-executable. 1339 return Setup(zip_fd, abs_dex_location, error_msg); 1340 } 1341 1342 bool ElfOatFile::Load(const std::string& elf_filename, 1343 bool writable, 1344 bool executable, 1345 bool low_4gb, 1346 /*inout*/MemMap* reservation, 1347 /*out*/std::string* error_msg) { 1348 ScopedTrace trace(__PRETTY_FUNCTION__); 1349 std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str())); 1350 if (file == nullptr) { 1351 *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno)); 1352 return false; 1353 } 1354 return ElfOatFile::ElfFileOpen(file.get(), 1355 writable, 1356 executable, 1357 low_4gb, 1358 reservation, 1359 error_msg); 1360 } 1361 1362 bool ElfOatFile::Load(int oat_fd, 1363 bool writable, 1364 bool executable, 1365 bool low_4gb, 1366 /*inout*/MemMap* reservation, 1367 /*out*/std::string* error_msg) { 1368 ScopedTrace trace(__PRETTY_FUNCTION__); 1369 if (oat_fd != -1) { 1370 int duped_fd = DupCloexec(oat_fd); 1371 std::unique_ptr<File> file = std::make_unique<File>(duped_fd, false); 1372 if (file == nullptr) { 1373 *error_msg = StringPrintf("Failed to open oat filename for reading: %s", 1374 strerror(errno)); 1375 return false; 1376 } 1377 return ElfOatFile::ElfFileOpen(file.get(), 1378 writable, 1379 executable, 1380 low_4gb, 1381 reservation, 1382 error_msg); 1383 } 1384 return false; 1385 } 1386 1387 bool ElfOatFile::ElfFileOpen(File* file, 1388 bool writable, 1389 bool executable, 1390 bool low_4gb, 1391 /*inout*/MemMap* reservation, 1392 /*out*/std::string* error_msg) { 1393 ScopedTrace trace(__PRETTY_FUNCTION__); 1394 elf_file_.reset(ElfFile::Open(file, 1395 writable, 1396 /*program_header_only=*/ true, 1397 low_4gb, 1398 error_msg)); 1399 if (elf_file_ == nullptr) { 1400 DCHECK(!error_msg->empty()); 1401 return false; 1402 } 1403 bool loaded = elf_file_->Load(file, executable, low_4gb, reservation, error_msg); 1404 DCHECK(loaded || !error_msg->empty()); 1405 return loaded; 1406 } 1407 1408 class OatFileBackedByVdex final : public OatFileBase { 1409 public: 1410 explicit OatFileBackedByVdex(const std::string& filename) 1411 : OatFileBase(filename, /*executable=*/ false) {} 1412 1413 static OatFileBackedByVdex* Open(const std::vector<const DexFile*>& dex_files, 1414 std::unique_ptr<VdexFile>&& vdex_file, 1415 const std::string& location) { 1416 std::unique_ptr<OatFileBackedByVdex> oat_file(new OatFileBackedByVdex(location)); 1417 oat_file->Initialize(dex_files, std::move(vdex_file)); 1418 return oat_file.release(); 1419 } 1420 1421 void Initialize(const std::vector<const DexFile*>& dex_files, 1422 std::unique_ptr<VdexFile>&& vdex_file) { 1423 DCHECK(!IsExecutable()); 1424 1425 // SetVdex will take ownership of the VdexFile. 1426 SetVdex(vdex_file.release()); 1427 1428 // Create a dummy OatHeader. 1429 std::unique_ptr<const InstructionSetFeatures> isa_features = 1430 InstructionSetFeatures::FromCppDefines(); 1431 oat_header_.reset(OatHeader::Create(kRuntimeISA, 1432 isa_features.get(), 1433 dex_files.size(), 1434 nullptr)); 1435 const uint8_t* begin = reinterpret_cast<const uint8_t*>(oat_header_.get()); 1436 SetBegin(begin); 1437 SetEnd(begin + oat_header_->GetHeaderSize()); 1438 1439 // Load VerifierDeps from VDEX and copy bit vectors of verified classes. 1440 ArrayRef<const uint8_t> deps_data = GetVdexFile()->GetVerifierDepsData(); 1441 verified_classes_per_dex_ = verifier::VerifierDeps::ParseVerifiedClasses(dex_files, deps_data); 1442 1443 // Initialize OatDexFiles. 1444 Setup(dex_files); 1445 } 1446 1447 bool IsClassVerifiedInVdex(const OatDexFile& oat_dex_file, uint16_t class_def_index) const { 1448 // Determine the index of the DexFile, assuming the order of OatDexFiles 1449 // in `oat_dex_files_storage_` is the same. 1450 const std::vector<const OatDexFile*>& oat_dex_files = GetOatDexFiles(); 1451 auto oat_dex_file_it = std::find(oat_dex_files.begin(), oat_dex_files.end(), &oat_dex_file); 1452 DCHECK(oat_dex_file_it != oat_dex_files.end()); 1453 size_t dex_index = oat_dex_file_it - oat_dex_files.begin(); 1454 // Check the bitvector of verified classes from the vdex. 1455 return verified_classes_per_dex_[dex_index][class_def_index]; 1456 } 1457 1458 protected: 1459 void PreLoad() override {} 1460 1461 bool Load(const std::string& elf_filename ATTRIBUTE_UNUSED, 1462 bool writable ATTRIBUTE_UNUSED, 1463 bool executable ATTRIBUTE_UNUSED, 1464 bool low_4gb ATTRIBUTE_UNUSED, 1465 MemMap* reservation ATTRIBUTE_UNUSED, 1466 std::string* error_msg ATTRIBUTE_UNUSED) override { 1467 LOG(FATAL) << "Unsupported"; 1468 UNREACHABLE(); 1469 } 1470 1471 bool Load(int oat_fd ATTRIBUTE_UNUSED, 1472 bool writable ATTRIBUTE_UNUSED, 1473 bool executable ATTRIBUTE_UNUSED, 1474 bool low_4gb ATTRIBUTE_UNUSED, 1475 MemMap* reservation ATTRIBUTE_UNUSED, 1476 std::string* error_msg ATTRIBUTE_UNUSED) override { 1477 LOG(FATAL) << "Unsupported"; 1478 UNREACHABLE(); 1479 } 1480 1481 void PreSetup(const std::string& elf_filename ATTRIBUTE_UNUSED) override {} 1482 1483 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name ATTRIBUTE_UNUSED, 1484 std::string* error_msg) const override { 1485 *error_msg = "Unsupported"; 1486 return nullptr; 1487 } 1488 1489 private: 1490 std::unique_ptr<OatHeader> oat_header_; 1491 std::vector<std::vector<bool>> verified_classes_per_dex_; 1492 1493 DISALLOW_COPY_AND_ASSIGN(OatFileBackedByVdex); 1494 }; 1495 1496 ////////////////////////// 1497 // General OatFile code // 1498 ////////////////////////// 1499 1500 static bool IsLocationSuffix(const char* abs_dex_location, const std::string& rel_dex_location) { 1501 std::string_view abs_location(abs_dex_location); 1502 std::string target_suffix = "/" + DexFileLoader::GetBaseLocation(rel_dex_location); 1503 if (abs_location.size() <= target_suffix.size()) { 1504 return false; 1505 } 1506 size_t pos = abs_location.size() - target_suffix.size(); 1507 return abs_location.compare(pos, std::string::npos, target_suffix) == 0; 1508 } 1509 1510 static void MaybeResolveDexPath(const char* abs_dex_location, 1511 const std::string& rel_dex_location, 1512 bool resolve, 1513 /* out */ std::string* out_location) { 1514 DCHECK(!resolve || abs_dex_location != nullptr); 1515 if (out_location != nullptr) { 1516 *out_location = resolve 1517 ? std::string(abs_dex_location) + DexFileLoader::GetMultiDexSuffix(rel_dex_location) 1518 : rel_dex_location; 1519 } 1520 } 1521 1522 void OatFile::ResolveRelativeEncodedDexLocation(const char* abs_dex_location, 1523 const std::string& rel_dex_location, 1524 /* out */ std::string* dex_file_location, 1525 /* out */ std::string* dex_file_name) { 1526 // Note that in this context `abs_dex_location` may not always be absolute 1527 // and `rel_dex_location` may not always be relative. It simply means that 1528 // we will try to resolve `rel_dex_location` into an absolute location using 1529 // `abs_dex_location` for the base directory if needed. 1530 1531 bool resolve_location = false; 1532 bool resolve_filename = false; 1533 1534 if (abs_dex_location != nullptr) { 1535 if (!IsAbsoluteLocation(rel_dex_location) && 1536 IsLocationSuffix(abs_dex_location, rel_dex_location)) { 1537 // The base location (w/o multidex suffix) of the relative `rel_dex_location` is a suffix 1538 // of `abs_dex_location`. This typically happens for oat files which only encode the 1539 // basename() so the oat and dex files can move to different directories. 1540 // Example: 1541 // abs_dex_location = "/data/app/myapp/MyApplication.apk" 1542 // rel_dex_location = "MyApplication.apk!classes2.dex" 1543 resolve_location = true; 1544 resolve_filename = true; 1545 } else { 1546 // Case 1: `rel_dex_location` is absolute 1547 // On target always use `rel_dex_location` for both dex file name and dex location. 1548 // On host assume we're cross-compiling and use `abs_dex_location` as a file name 1549 // (for loading files) and `rel_dex_location` as the dex location. If we're not 1550 // cross-compiling, the two paths should be equal. 1551 // Case 2: `rel_dex_location` is relative and not suffix of `abs_location` 1552 // This should never happen outside of tests. On target always use `rel_dex_location`. On 1553 // host use `abs_dex_location` with the appropriate multidex suffix because 1554 // `rel_dex_location` might be the target path. 1555 resolve_location = false; 1556 resolve_filename = !kIsTargetBuild; 1557 } 1558 } 1559 1560 // Construct dex file location and dex file name if the correspoding out-param pointers 1561 // were provided by the caller. 1562 MaybeResolveDexPath(abs_dex_location, rel_dex_location, resolve_location, dex_file_location); 1563 MaybeResolveDexPath(abs_dex_location, rel_dex_location, resolve_filename, dex_file_name); 1564 } 1565 1566 static void CheckLocation(const std::string& location) { 1567 CHECK(!location.empty()); 1568 } 1569 1570 OatFile* OatFile::OpenWithElfFile(int zip_fd, 1571 ElfFile* elf_file, 1572 VdexFile* vdex_file, 1573 const std::string& location, 1574 const char* abs_dex_location, 1575 std::string* error_msg) { 1576 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, /*executable=*/ false)); 1577 return oat_file->InitializeFromElfFile(zip_fd, elf_file, vdex_file, abs_dex_location, error_msg) 1578 ? oat_file.release() 1579 : nullptr; 1580 } 1581 1582 OatFile* OatFile::Open(int zip_fd, 1583 const std::string& oat_filename, 1584 const std::string& oat_location, 1585 bool executable, 1586 bool low_4gb, 1587 const char* abs_dex_location, 1588 /*inout*/MemMap* reservation, 1589 /*out*/std::string* error_msg) { 1590 ScopedTrace trace("Open oat file " + oat_location); 1591 CHECK(!oat_filename.empty()) << oat_location; 1592 CheckLocation(oat_location); 1593 1594 std::string vdex_filename = GetVdexFilename(oat_filename); 1595 1596 // Check that the files even exist, fast-fail. 1597 if (!OS::FileExists(vdex_filename.c_str())) { 1598 *error_msg = StringPrintf("File %s does not exist.", vdex_filename.c_str()); 1599 return nullptr; 1600 } else if (!OS::FileExists(oat_filename.c_str())) { 1601 *error_msg = StringPrintf("File %s does not exist.", oat_filename.c_str()); 1602 return nullptr; 1603 } 1604 1605 // Try dlopen first, as it is required for native debuggability. This will fail fast if dlopen is 1606 // disabled. 1607 OatFile* with_dlopen = OatFileBase::OpenOatFile<DlOpenOatFile>(zip_fd, 1608 vdex_filename, 1609 oat_filename, 1610 oat_location, 1611 /*writable=*/ false, 1612 executable, 1613 low_4gb, 1614 abs_dex_location, 1615 reservation, 1616 error_msg); 1617 if (with_dlopen != nullptr) { 1618 return with_dlopen; 1619 } 1620 if (kPrintDlOpenErrorMessage) { 1621 LOG(ERROR) << "Failed to dlopen: " << oat_filename << " with error " << *error_msg; 1622 } 1623 // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons: 1624 // 1625 // On target, dlopen may fail when compiling due to selinux restrictions on installd. 1626 // 1627 // We use our own ELF loader for Quick to deal with legacy apps that 1628 // open a generated dex file by name, remove the file, then open 1629 // another generated dex file with the same name. http://b/10614658 1630 // 1631 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile. 1632 // 1633 // 1634 // Another independent reason is the absolute placement of boot.oat. dlopen on the host usually 1635 // does honor the virtual address encoded in the ELF file only for ET_EXEC files, not ET_DYN. 1636 OatFile* with_internal = OatFileBase::OpenOatFile<ElfOatFile>(zip_fd, 1637 vdex_filename, 1638 oat_filename, 1639 oat_location, 1640 /*writable=*/ false, 1641 executable, 1642 low_4gb, 1643 abs_dex_location, 1644 reservation, 1645 error_msg); 1646 return with_internal; 1647 } 1648 1649 OatFile* OatFile::Open(int zip_fd, 1650 int vdex_fd, 1651 int oat_fd, 1652 const std::string& oat_location, 1653 bool executable, 1654 bool low_4gb, 1655 const char* abs_dex_location, 1656 /*inout*/MemMap* reservation, 1657 /*out*/std::string* error_msg) { 1658 CHECK(!oat_location.empty()) << oat_location; 1659 1660 std::string vdex_location = GetVdexFilename(oat_location); 1661 1662 OatFile* with_internal = OatFileBase::OpenOatFile<ElfOatFile>(zip_fd, 1663 vdex_fd, 1664 oat_fd, 1665 vdex_location, 1666 oat_location, 1667 /*writable=*/ false, 1668 executable, 1669 low_4gb, 1670 abs_dex_location, 1671 reservation, 1672 error_msg); 1673 return with_internal; 1674 } 1675 1676 OatFile* OatFile::OpenWritable(int zip_fd, 1677 File* file, 1678 const std::string& location, 1679 const char* abs_dex_location, 1680 std::string* error_msg) { 1681 CheckLocation(location); 1682 return ElfOatFile::OpenElfFile(zip_fd, 1683 file, 1684 location, 1685 /*writable=*/ true, 1686 /*executable=*/ false, 1687 /*low_4gb=*/false, 1688 abs_dex_location, 1689 /*reservation=*/ nullptr, 1690 error_msg); 1691 } 1692 1693 OatFile* OatFile::OpenReadable(int zip_fd, 1694 File* file, 1695 const std::string& location, 1696 const char* abs_dex_location, 1697 std::string* error_msg) { 1698 CheckLocation(location); 1699 return ElfOatFile::OpenElfFile(zip_fd, 1700 file, 1701 location, 1702 /*writable=*/ false, 1703 /*executable=*/ false, 1704 /*low_4gb=*/false, 1705 abs_dex_location, 1706 /*reservation=*/ nullptr, 1707 error_msg); 1708 } 1709 1710 OatFile* OatFile::OpenFromVdex(const std::vector<const DexFile*>& dex_files, 1711 std::unique_ptr<VdexFile>&& vdex_file, 1712 const std::string& location) { 1713 CheckLocation(location); 1714 return OatFileBackedByVdex::Open(dex_files, std::move(vdex_file), location); 1715 } 1716 1717 OatFile::OatFile(const std::string& location, bool is_executable) 1718 : location_(location), 1719 vdex_(nullptr), 1720 begin_(nullptr), 1721 end_(nullptr), 1722 data_bimg_rel_ro_begin_(nullptr), 1723 data_bimg_rel_ro_end_(nullptr), 1724 bss_begin_(nullptr), 1725 bss_end_(nullptr), 1726 bss_methods_(nullptr), 1727 bss_roots_(nullptr), 1728 is_executable_(is_executable), 1729 vdex_begin_(nullptr), 1730 vdex_end_(nullptr), 1731 secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) { 1732 CHECK(!location_.empty()); 1733 } 1734 1735 OatFile::~OatFile() { 1736 STLDeleteElements(&oat_dex_files_storage_); 1737 } 1738 1739 const OatHeader& OatFile::GetOatHeader() const { 1740 return *reinterpret_cast<const OatHeader*>(Begin()); 1741 } 1742 1743 const uint8_t* OatFile::Begin() const { 1744 CHECK(begin_ != nullptr); 1745 return begin_; 1746 } 1747 1748 const uint8_t* OatFile::End() const { 1749 CHECK(end_ != nullptr); 1750 return end_; 1751 } 1752 1753 const uint8_t* OatFile::DexBegin() const { 1754 return vdex_->Begin(); 1755 } 1756 1757 const uint8_t* OatFile::DexEnd() const { 1758 return vdex_->End(); 1759 } 1760 1761 ArrayRef<const uint32_t> OatFile::GetBootImageRelocations() const { 1762 if (data_bimg_rel_ro_begin_ != nullptr) { 1763 const uint32_t* relocations = reinterpret_cast<const uint32_t*>(data_bimg_rel_ro_begin_); 1764 const uint32_t* relocations_end = reinterpret_cast<const uint32_t*>(data_bimg_rel_ro_end_); 1765 return ArrayRef<const uint32_t>(relocations, relocations_end - relocations); 1766 } else { 1767 return ArrayRef<const uint32_t>(); 1768 } 1769 } 1770 1771 ArrayRef<ArtMethod*> OatFile::GetBssMethods() const { 1772 if (bss_methods_ != nullptr) { 1773 ArtMethod** methods = reinterpret_cast<ArtMethod**>(bss_methods_); 1774 ArtMethod** methods_end = 1775 reinterpret_cast<ArtMethod**>(bss_roots_ != nullptr ? bss_roots_ : bss_end_); 1776 return ArrayRef<ArtMethod*>(methods, methods_end - methods); 1777 } else { 1778 return ArrayRef<ArtMethod*>(); 1779 } 1780 } 1781 1782 ArrayRef<GcRoot<mirror::Object>> OatFile::GetBssGcRoots() const { 1783 if (bss_roots_ != nullptr) { 1784 auto* roots = reinterpret_cast<GcRoot<mirror::Object>*>(bss_roots_); 1785 auto* roots_end = reinterpret_cast<GcRoot<mirror::Object>*>(bss_end_); 1786 return ArrayRef<GcRoot<mirror::Object>>(roots, roots_end - roots); 1787 } else { 1788 return ArrayRef<GcRoot<mirror::Object>>(); 1789 } 1790 } 1791 1792 const OatDexFile* OatFile::GetOatDexFile(const char* dex_location, 1793 const uint32_t* dex_location_checksum, 1794 std::string* error_msg) const { 1795 // NOTE: We assume here that the canonical location for a given dex_location never 1796 // changes. If it does (i.e. some symlink used by the filename changes) we may return 1797 // an incorrect OatDexFile. As long as we have a checksum to check, we shall return 1798 // an identical file or fail; otherwise we may see some unpredictable failures. 1799 1800 // TODO: Additional analysis of usage patterns to see if this can be simplified 1801 // without any performance loss, for example by not doing the first lock-free lookup. 1802 1803 const OatDexFile* oat_dex_file = nullptr; 1804 std::string_view key(dex_location); 1805 // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations 1806 // directly mentioned in the oat file and doesn't require locking. 1807 auto primary_it = oat_dex_files_.find(key); 1808 if (primary_it != oat_dex_files_.end()) { 1809 oat_dex_file = primary_it->second; 1810 DCHECK(oat_dex_file != nullptr); 1811 } else { 1812 // This dex_location is not one of the dex locations directly mentioned in the 1813 // oat file. The correct lookup is via the canonical location but first see in 1814 // the secondary_oat_dex_files_ whether we've looked up this location before. 1815 MutexLock mu(Thread::Current(), secondary_lookup_lock_); 1816 auto secondary_lb = secondary_oat_dex_files_.lower_bound(key); 1817 if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) { 1818 oat_dex_file = secondary_lb->second; // May be null. 1819 } else { 1820 // We haven't seen this dex_location before, we must check the canonical location. 1821 std::string dex_canonical_location = DexFileLoader::GetDexCanonicalLocation(dex_location); 1822 if (dex_canonical_location != dex_location) { 1823 std::string_view canonical_key(dex_canonical_location); 1824 auto canonical_it = oat_dex_files_.find(canonical_key); 1825 if (canonical_it != oat_dex_files_.end()) { 1826 oat_dex_file = canonical_it->second; 1827 } // else keep null. 1828 } // else keep null. 1829 1830 // Copy the key to the string_cache_ and store the result in secondary map. 1831 string_cache_.emplace_back(key.data(), key.length()); 1832 std::string_view key_copy(string_cache_.back()); 1833 secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file); 1834 } 1835 } 1836 1837 if (oat_dex_file == nullptr) { 1838 if (error_msg != nullptr) { 1839 std::string dex_canonical_location = DexFileLoader::GetDexCanonicalLocation(dex_location); 1840 *error_msg = "Failed to find OatDexFile for DexFile " + std::string(dex_location) 1841 + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation(); 1842 } 1843 return nullptr; 1844 } 1845 1846 if (dex_location_checksum != nullptr && 1847 oat_dex_file->GetDexFileLocationChecksum() != *dex_location_checksum) { 1848 if (error_msg != nullptr) { 1849 std::string dex_canonical_location = DexFileLoader::GetDexCanonicalLocation(dex_location); 1850 std::string checksum = StringPrintf("0x%08x", oat_dex_file->GetDexFileLocationChecksum()); 1851 std::string required_checksum = StringPrintf("0x%08x", *dex_location_checksum); 1852 *error_msg = "OatDexFile for DexFile " + std::string(dex_location) 1853 + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation() 1854 + " has checksum " + checksum + " but " + required_checksum + " was required"; 1855 } 1856 return nullptr; 1857 } 1858 return oat_dex_file; 1859 } 1860 1861 OatDexFile::OatDexFile(const OatFile* oat_file, 1862 const std::string& dex_file_location, 1863 const std::string& canonical_dex_file_location, 1864 uint32_t dex_file_location_checksum, 1865 const uint8_t* dex_file_pointer, 1866 const uint8_t* lookup_table_data, 1867 const IndexBssMapping* method_bss_mapping_data, 1868 const IndexBssMapping* type_bss_mapping_data, 1869 const IndexBssMapping* string_bss_mapping_data, 1870 const uint32_t* oat_class_offsets_pointer, 1871 const DexLayoutSections* dex_layout_sections) 1872 : oat_file_(oat_file), 1873 dex_file_location_(dex_file_location), 1874 canonical_dex_file_location_(canonical_dex_file_location), 1875 dex_file_location_checksum_(dex_file_location_checksum), 1876 dex_file_pointer_(dex_file_pointer), 1877 lookup_table_data_(lookup_table_data), 1878 method_bss_mapping_(method_bss_mapping_data), 1879 type_bss_mapping_(type_bss_mapping_data), 1880 string_bss_mapping_(string_bss_mapping_data), 1881 oat_class_offsets_pointer_(oat_class_offsets_pointer), 1882 lookup_table_(), 1883 dex_layout_sections_(dex_layout_sections) { 1884 // Initialize TypeLookupTable. 1885 if (lookup_table_data_ != nullptr) { 1886 // Peek the number of classes from the DexFile. 1887 const DexFile::Header* dex_header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer_); 1888 const uint32_t num_class_defs = dex_header->class_defs_size_; 1889 if (lookup_table_data_ + TypeLookupTable::RawDataLength(num_class_defs) > GetOatFile()->End()) { 1890 LOG(WARNING) << "found truncated lookup table in " << dex_file_location_; 1891 } else { 1892 const uint8_t* dex_data = dex_file_pointer_; 1893 // TODO: Clean this up to create the type lookup table after the dex file has been created? 1894 if (CompactDexFile::IsMagicValid(dex_header->magic_)) { 1895 dex_data += dex_header->data_off_; 1896 } 1897 lookup_table_ = TypeLookupTable::Open(dex_data, lookup_table_data_, num_class_defs); 1898 } 1899 } 1900 DCHECK(!IsBackedByVdexOnly()); 1901 } 1902 1903 OatDexFile::OatDexFile(const OatFile* oat_file, 1904 const DexFile* dex_file, 1905 const std::string& dex_file_location, 1906 const std::string& canonical_dex_file_location) 1907 : oat_file_(oat_file), 1908 dex_file_location_(dex_file_location), 1909 canonical_dex_file_location_(canonical_dex_file_location), 1910 dex_file_location_checksum_(dex_file->GetLocationChecksum()), 1911 dex_file_pointer_(reinterpret_cast<const uint8_t*>(dex_file)) { 1912 dex_file->SetOatDexFile(this); 1913 DCHECK(IsBackedByVdexOnly()); 1914 } 1915 1916 OatDexFile::OatDexFile(TypeLookupTable&& lookup_table) : lookup_table_(std::move(lookup_table)) { 1917 // Stripped-down OatDexFile only allowed in the compiler, the zygote, or the system server. 1918 CHECK(Runtime::Current() == nullptr || 1919 Runtime::Current()->IsAotCompiler() || 1920 Runtime::Current()->IsZygote() || 1921 Runtime::Current()->IsSystemServer()); 1922 } 1923 1924 OatDexFile::~OatDexFile() {} 1925 1926 size_t OatDexFile::FileSize() const { 1927 DCHECK(dex_file_pointer_ != nullptr); 1928 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_; 1929 } 1930 1931 std::unique_ptr<const DexFile> OatDexFile::OpenDexFile(std::string* error_msg) const { 1932 ScopedTrace trace(__PRETTY_FUNCTION__); 1933 static constexpr bool kVerify = false; 1934 static constexpr bool kVerifyChecksum = false; 1935 const ArtDexFileLoader dex_file_loader; 1936 return dex_file_loader.Open(dex_file_pointer_, 1937 FileSize(), 1938 dex_file_location_, 1939 dex_file_location_checksum_, 1940 this, 1941 kVerify, 1942 kVerifyChecksum, 1943 error_msg); 1944 } 1945 1946 uint32_t OatDexFile::GetOatClassOffset(uint16_t class_def_index) const { 1947 DCHECK(oat_class_offsets_pointer_ != nullptr); 1948 return oat_class_offsets_pointer_[class_def_index]; 1949 } 1950 1951 bool OatDexFile::IsBackedByVdexOnly() const { 1952 return oat_class_offsets_pointer_ == nullptr; 1953 } 1954 1955 OatFile::OatClass OatDexFile::GetOatClass(uint16_t class_def_index) const { 1956 // If this is an OatFileBackedByVdex, initialize the OatClass using the vdex's VerifierDeps. 1957 if (IsBackedByVdexOnly()) { 1958 bool is_vdex_verified = down_cast<const OatFileBackedByVdex*>(oat_file_)->IsClassVerifiedInVdex( 1959 *this, 1960 class_def_index); 1961 return OatFile::OatClass(oat_file_, 1962 is_vdex_verified ? ClassStatus::kVerified : ClassStatus::kNotReady, 1963 /* type= */ kOatClassNoneCompiled, 1964 /* bitmap_size= */ 0u, 1965 /* bitmap_pointer= */ nullptr, 1966 /* methods_pointer= */ nullptr); 1967 } 1968 1969 uint32_t oat_class_offset = GetOatClassOffset(class_def_index); 1970 1971 const uint8_t* oat_class_pointer = oat_file_->Begin() + oat_class_offset; 1972 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation(); 1973 1974 const uint8_t* status_pointer = oat_class_pointer; 1975 CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation(); 1976 ClassStatus status = enum_cast<ClassStatus>(*reinterpret_cast<const int16_t*>(status_pointer)); 1977 CHECK_LE(status, ClassStatus::kLast); 1978 1979 const uint8_t* type_pointer = status_pointer + sizeof(uint16_t); 1980 CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation(); 1981 OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer)); 1982 CHECK_LT(type, kOatClassMax); 1983 1984 const uint8_t* after_type_pointer = type_pointer + sizeof(int16_t); 1985 CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation(); 1986 1987 uint32_t bitmap_size = 0; 1988 const uint8_t* bitmap_pointer = nullptr; 1989 const uint8_t* methods_pointer = nullptr; 1990 if (type != kOatClassNoneCompiled) { 1991 if (type == kOatClassSomeCompiled) { 1992 bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer)); 1993 bitmap_pointer = after_type_pointer + sizeof(bitmap_size); 1994 CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation(); 1995 methods_pointer = bitmap_pointer + bitmap_size; 1996 } else { 1997 methods_pointer = after_type_pointer; 1998 } 1999 CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation(); 2000 } 2001 2002 return OatFile::OatClass(oat_file_, 2003 status, 2004 type, 2005 bitmap_size, 2006 reinterpret_cast<const uint32_t*>(bitmap_pointer), 2007 reinterpret_cast<const OatMethodOffsets*>(methods_pointer)); 2008 } 2009 2010 ArrayRef<const uint8_t> OatDexFile::GetQuickenedInfoOf(const DexFile& dex_file, 2011 uint32_t dex_method_idx) const { 2012 const OatFile* oat_file = GetOatFile(); 2013 if (oat_file == nullptr) { 2014 return ArrayRef<const uint8_t>(); 2015 } else { 2016 return oat_file->GetVdexFile()->GetQuickenedInfoOf(dex_file, dex_method_idx); 2017 } 2018 } 2019 2020 const dex::ClassDef* OatDexFile::FindClassDef(const DexFile& dex_file, 2021 const char* descriptor, 2022 size_t hash) { 2023 const OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); 2024 DCHECK_EQ(ComputeModifiedUtf8Hash(descriptor), hash); 2025 bool used_lookup_table = false; 2026 const dex::ClassDef* lookup_table_classdef = nullptr; 2027 if (LIKELY((oat_dex_file != nullptr) && oat_dex_file->GetTypeLookupTable().Valid())) { 2028 used_lookup_table = true; 2029 const uint32_t class_def_idx = oat_dex_file->GetTypeLookupTable().Lookup(descriptor, hash); 2030 lookup_table_classdef = (class_def_idx != dex::kDexNoIndex) 2031 ? &dex_file.GetClassDef(class_def_idx) 2032 : nullptr; 2033 if (!kIsDebugBuild) { 2034 return lookup_table_classdef; 2035 } 2036 } 2037 // Fast path for rare no class defs case. 2038 const uint32_t num_class_defs = dex_file.NumClassDefs(); 2039 if (num_class_defs == 0) { 2040 DCHECK(!used_lookup_table); 2041 return nullptr; 2042 } 2043 const dex::TypeId* type_id = dex_file.FindTypeId(descriptor); 2044 if (type_id != nullptr) { 2045 dex::TypeIndex type_idx = dex_file.GetIndexForTypeId(*type_id); 2046 const dex::ClassDef* found_class_def = dex_file.FindClassDef(type_idx); 2047 if (kIsDebugBuild && used_lookup_table) { 2048 DCHECK_EQ(found_class_def, lookup_table_classdef); 2049 } 2050 return found_class_def; 2051 } 2052 return nullptr; 2053 } 2054 2055 // Madvise the dex file based on the state we are moving to. 2056 void OatDexFile::MadviseDexFile(const DexFile& dex_file, MadviseState state) { 2057 Runtime* const runtime = Runtime::Current(); 2058 const bool low_ram = runtime->GetHeap()->IsLowMemoryMode(); 2059 // TODO: Also do madvise hints for non low ram devices. 2060 if (!low_ram) { 2061 return; 2062 } 2063 if (state == MadviseState::kMadviseStateAtLoad && runtime->MAdviseRandomAccess()) { 2064 // Default every dex file to MADV_RANDOM when its loaded by default for low ram devices. 2065 // Other devices have enough page cache to get performance benefits from loading more pages 2066 // into the page cache. 2067 DexLayoutSection::MadviseLargestPageAlignedRegion(dex_file.Begin(), 2068 dex_file.Begin() + dex_file.Size(), 2069 MADV_RANDOM); 2070 } 2071 const OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); 2072 if (oat_dex_file != nullptr) { 2073 // Should always be there. 2074 const DexLayoutSections* const sections = oat_dex_file->GetDexLayoutSections(); 2075 CHECK(sections != nullptr); 2076 sections->Madvise(&dex_file, state); 2077 } 2078 } 2079 2080 OatFile::OatClass::OatClass(const OatFile* oat_file, 2081 ClassStatus status, 2082 OatClassType type, 2083 uint32_t bitmap_size, 2084 const uint32_t* bitmap_pointer, 2085 const OatMethodOffsets* methods_pointer) 2086 : oat_file_(oat_file), status_(status), type_(type), 2087 bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) { 2088 switch (type_) { 2089 case kOatClassAllCompiled: { 2090 CHECK_EQ(0U, bitmap_size); 2091 CHECK(bitmap_pointer == nullptr); 2092 CHECK(methods_pointer != nullptr); 2093 break; 2094 } 2095 case kOatClassSomeCompiled: { 2096 CHECK_NE(0U, bitmap_size); 2097 CHECK(bitmap_pointer != nullptr); 2098 CHECK(methods_pointer != nullptr); 2099 break; 2100 } 2101 case kOatClassNoneCompiled: { 2102 CHECK_EQ(0U, bitmap_size); 2103 CHECK(bitmap_pointer == nullptr); 2104 CHECK(methods_pointer_ == nullptr); 2105 break; 2106 } 2107 case kOatClassMax: { 2108 LOG(FATAL) << "Invalid OatClassType " << type_; 2109 UNREACHABLE(); 2110 } 2111 } 2112 } 2113 2114 uint32_t OatFile::OatClass::GetOatMethodOffsetsOffset(uint32_t method_index) const { 2115 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index); 2116 if (oat_method_offsets == nullptr) { 2117 return 0u; 2118 } 2119 return reinterpret_cast<const uint8_t*>(oat_method_offsets) - oat_file_->Begin(); 2120 } 2121 2122 const OatMethodOffsets* OatFile::OatClass::GetOatMethodOffsets(uint32_t method_index) const { 2123 // NOTE: We don't keep the number of methods and cannot do a bounds check for method_index. 2124 if (methods_pointer_ == nullptr) { 2125 CHECK_EQ(kOatClassNoneCompiled, type_); 2126 return nullptr; 2127 } 2128 size_t methods_pointer_index; 2129 if (bitmap_ == nullptr) { 2130 CHECK_EQ(kOatClassAllCompiled, type_); 2131 methods_pointer_index = method_index; 2132 } else { 2133 CHECK_EQ(kOatClassSomeCompiled, type_); 2134 if (!BitVector::IsBitSet(bitmap_, method_index)) { 2135 return nullptr; 2136 } 2137 size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index); 2138 methods_pointer_index = num_set_bits; 2139 } 2140 const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index]; 2141 return &oat_method_offsets; 2142 } 2143 2144 const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const { 2145 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index); 2146 if (oat_method_offsets == nullptr) { 2147 return OatMethod(nullptr, 0); 2148 } 2149 if (oat_file_->IsExecutable() || 2150 Runtime::Current() == nullptr || // This case applies for oatdump. 2151 Runtime::Current()->IsAotCompiler()) { 2152 return OatMethod(oat_file_->Begin(), oat_method_offsets->code_offset_); 2153 } 2154 // We aren't allowed to use the compiled code. We just force it down the interpreted / jit 2155 // version. 2156 return OatMethod(oat_file_->Begin(), 0); 2157 } 2158 2159 void OatFile::OatMethod::LinkMethod(ArtMethod* method) const { 2160 CHECK(method != nullptr); 2161 method->SetEntryPointFromQuickCompiledCode(GetQuickCode()); 2162 } 2163 2164 bool OatFile::IsDebuggable() const { 2165 return GetOatHeader().IsDebuggable(); 2166 } 2167 2168 CompilerFilter::Filter OatFile::GetCompilerFilter() const { 2169 return GetOatHeader().GetCompilerFilter(); 2170 } 2171 2172 std::string OatFile::GetClassLoaderContext() const { 2173 return GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey); 2174 } 2175 2176 const char* OatFile::GetCompilationReason() const { 2177 return GetOatHeader().GetStoreValueByKey(OatHeader::kCompilationReasonKey); 2178 } 2179 2180 OatFile::OatClass OatFile::FindOatClass(const DexFile& dex_file, 2181 uint16_t class_def_idx, 2182 bool* found) { 2183 DCHECK_NE(class_def_idx, DexFile::kDexNoIndex16); 2184 const OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); 2185 if (oat_dex_file == nullptr || oat_dex_file->GetOatFile() == nullptr) { 2186 *found = false; 2187 return OatFile::OatClass::Invalid(); 2188 } 2189 *found = true; 2190 return oat_dex_file->GetOatClass(class_def_idx); 2191 } 2192 2193 static void DCheckIndexToBssMapping(const OatFile* oat_file, 2194 uint32_t number_of_indexes, 2195 size_t slot_size, 2196 const IndexBssMapping* index_bss_mapping) { 2197 if (kIsDebugBuild && index_bss_mapping != nullptr) { 2198 size_t index_bits = IndexBssMappingEntry::IndexBits(number_of_indexes); 2199 const IndexBssMappingEntry* prev_entry = nullptr; 2200 for (const IndexBssMappingEntry& entry : *index_bss_mapping) { 2201 CHECK_ALIGNED_PARAM(entry.bss_offset, slot_size); 2202 CHECK_LT(entry.bss_offset, oat_file->BssSize()); 2203 uint32_t mask = entry.GetMask(index_bits); 2204 CHECK_LE(POPCOUNT(mask) * slot_size, entry.bss_offset); 2205 size_t index_mask_span = (mask != 0u) ? 32u - index_bits - CTZ(mask) : 0u; 2206 CHECK_LE(index_mask_span, entry.GetIndex(index_bits)); 2207 if (prev_entry != nullptr) { 2208 CHECK_LT(prev_entry->GetIndex(index_bits), entry.GetIndex(index_bits) - index_mask_span); 2209 } 2210 prev_entry = &entry; 2211 } 2212 CHECK(prev_entry != nullptr); 2213 CHECK_LT(prev_entry->GetIndex(index_bits), number_of_indexes); 2214 } 2215 } 2216 2217 void OatFile::InitializeRelocations() const { 2218 DCHECK(IsExecutable()); 2219 2220 // Initialize the .data.bimg.rel.ro section. 2221 if (!GetBootImageRelocations().empty()) { 2222 uint8_t* reloc_begin = const_cast<uint8_t*>(DataBimgRelRoBegin()); 2223 CheckedCall(mprotect, 2224 "un-protect boot image relocations", 2225 reloc_begin, 2226 DataBimgRelRoSize(), 2227 PROT_READ | PROT_WRITE); 2228 uint32_t boot_image_begin = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>( 2229 Runtime::Current()->GetHeap()->GetBootImageSpaces().front()->Begin())); 2230 for (const uint32_t& relocation : GetBootImageRelocations()) { 2231 const_cast<uint32_t&>(relocation) += boot_image_begin; 2232 } 2233 CheckedCall(mprotect, 2234 "protect boot image relocations", 2235 reloc_begin, 2236 DataBimgRelRoSize(), 2237 PROT_READ); 2238 } 2239 2240 // Before initializing .bss, check the .bss mappings in debug mode. 2241 if (kIsDebugBuild) { 2242 PointerSize pointer_size = GetInstructionSetPointerSize(GetOatHeader().GetInstructionSet()); 2243 for (const OatDexFile* odf : GetOatDexFiles()) { 2244 const DexFile::Header* header = 2245 reinterpret_cast<const DexFile::Header*>(odf->GetDexFilePointer()); 2246 DCheckIndexToBssMapping(this, 2247 header->method_ids_size_, 2248 static_cast<size_t>(pointer_size), 2249 odf->GetMethodBssMapping()); 2250 DCheckIndexToBssMapping(this, 2251 header->type_ids_size_, 2252 sizeof(GcRoot<mirror::Class>), 2253 odf->GetTypeBssMapping()); 2254 DCheckIndexToBssMapping(this, 2255 header->string_ids_size_, 2256 sizeof(GcRoot<mirror::String>), 2257 odf->GetStringBssMapping()); 2258 } 2259 } 2260 2261 // Initialize the .bss section. 2262 // TODO: Pre-initialize from boot/app image? 2263 ArtMethod* resolution_method = Runtime::Current()->GetResolutionMethod(); 2264 for (ArtMethod*& entry : GetBssMethods()) { 2265 entry = resolution_method; 2266 } 2267 } 2268 2269 void OatDexFile::AssertAotCompiler() { 2270 CHECK(Runtime::Current()->IsAotCompiler()); 2271 } 2272 2273 } // namespace art 2274