1 /* 2 * Copyright (C) 2015 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_manager.h" 18 19 #include <memory> 20 #include <queue> 21 #include <vector> 22 23 #include "base/logging.h" 24 #include "base/stl_util.h" 25 #include "base/systrace.h" 26 #include "class_linker.h" 27 #include "dex_file-inl.h" 28 #include "gc/scoped_gc_critical_section.h" 29 #include "gc/space/image_space.h" 30 #include "handle_scope-inl.h" 31 #include "mirror/class_loader.h" 32 #include "oat_file_assistant.h" 33 #include "scoped_thread_state_change.h" 34 #include "thread-inl.h" 35 #include "thread_list.h" 36 37 namespace art { 38 39 // If true, then we attempt to load the application image if it exists. 40 static constexpr bool kEnableAppImage = true; 41 42 CompilerFilter::Filter OatFileManager::filter_ = CompilerFilter::Filter::kSpeed; 43 44 const OatFile* OatFileManager::RegisterOatFile(std::unique_ptr<const OatFile> oat_file) { 45 WriterMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); 46 DCHECK(oat_file != nullptr); 47 if (kIsDebugBuild) { 48 CHECK(oat_files_.find(oat_file) == oat_files_.end()); 49 for (const std::unique_ptr<const OatFile>& existing : oat_files_) { 50 CHECK_NE(oat_file.get(), existing.get()) << oat_file->GetLocation(); 51 // Check that we don't have an oat file with the same address. Copies of the same oat file 52 // should be loaded at different addresses. 53 CHECK_NE(oat_file->Begin(), existing->Begin()) << "Oat file already mapped at that location"; 54 } 55 } 56 have_non_pic_oat_file_ = have_non_pic_oat_file_ || !oat_file->IsPic(); 57 const OatFile* ret = oat_file.get(); 58 oat_files_.insert(std::move(oat_file)); 59 return ret; 60 } 61 62 void OatFileManager::UnRegisterAndDeleteOatFile(const OatFile* oat_file) { 63 WriterMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); 64 DCHECK(oat_file != nullptr); 65 std::unique_ptr<const OatFile> compare(oat_file); 66 auto it = oat_files_.find(compare); 67 CHECK(it != oat_files_.end()); 68 oat_files_.erase(it); 69 compare.release(); 70 } 71 72 const OatFile* OatFileManager::FindOpenedOatFileFromDexLocation( 73 const std::string& dex_base_location) const { 74 ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); 75 for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) { 76 const std::vector<const OatDexFile*>& oat_dex_files = oat_file->GetOatDexFiles(); 77 for (const OatDexFile* oat_dex_file : oat_dex_files) { 78 if (DexFile::GetBaseLocation(oat_dex_file->GetDexFileLocation()) == dex_base_location) { 79 return oat_file.get(); 80 } 81 } 82 } 83 return nullptr; 84 } 85 86 const OatFile* OatFileManager::FindOpenedOatFileFromOatLocation(const std::string& oat_location) 87 const { 88 ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); 89 return FindOpenedOatFileFromOatLocationLocked(oat_location); 90 } 91 92 const OatFile* OatFileManager::FindOpenedOatFileFromOatLocationLocked( 93 const std::string& oat_location) const { 94 for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) { 95 if (oat_file->GetLocation() == oat_location) { 96 return oat_file.get(); 97 } 98 } 99 return nullptr; 100 } 101 102 std::vector<const OatFile*> OatFileManager::GetBootOatFiles() const { 103 std::vector<const OatFile*> oat_files; 104 std::vector<gc::space::ImageSpace*> image_spaces = 105 Runtime::Current()->GetHeap()->GetBootImageSpaces(); 106 for (gc::space::ImageSpace* image_space : image_spaces) { 107 oat_files.push_back(image_space->GetOatFile()); 108 } 109 return oat_files; 110 } 111 112 const OatFile* OatFileManager::GetPrimaryOatFile() const { 113 ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); 114 std::vector<const OatFile*> boot_oat_files = GetBootOatFiles(); 115 if (!boot_oat_files.empty()) { 116 for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) { 117 if (std::find(boot_oat_files.begin(), boot_oat_files.end(), oat_file.get()) == 118 boot_oat_files.end()) { 119 return oat_file.get(); 120 } 121 } 122 } 123 return nullptr; 124 } 125 126 OatFileManager::~OatFileManager() { 127 // Explicitly clear oat_files_ since the OatFile destructor calls back into OatFileManager for 128 // UnRegisterOatFileLocation. 129 oat_files_.clear(); 130 } 131 132 std::vector<const OatFile*> OatFileManager::RegisterImageOatFiles( 133 std::vector<gc::space::ImageSpace*> spaces) { 134 std::vector<const OatFile*> oat_files; 135 for (gc::space::ImageSpace* space : spaces) { 136 oat_files.push_back(RegisterOatFile(space->ReleaseOatFile())); 137 } 138 return oat_files; 139 } 140 141 class DexFileAndClassPair : ValueObject { 142 public: 143 DexFileAndClassPair(const DexFile* dex_file, size_t current_class_index, bool from_loaded_oat) 144 : cached_descriptor_(GetClassDescriptor(dex_file, current_class_index)), 145 dex_file_(dex_file), 146 current_class_index_(current_class_index), 147 from_loaded_oat_(from_loaded_oat) {} 148 149 DexFileAndClassPair(const DexFileAndClassPair& rhs) = default; 150 151 DexFileAndClassPair& operator=(const DexFileAndClassPair& rhs) = default; 152 153 const char* GetCachedDescriptor() const { 154 return cached_descriptor_; 155 } 156 157 bool operator<(const DexFileAndClassPair& rhs) const { 158 const int cmp = strcmp(cached_descriptor_, rhs.cached_descriptor_); 159 if (cmp != 0) { 160 // Note that the order must be reversed. We want to iterate over the classes in dex files. 161 // They are sorted lexicographically. Thus, the priority-queue must be a min-queue. 162 return cmp > 0; 163 } 164 return dex_file_ < rhs.dex_file_; 165 } 166 167 bool DexFileHasMoreClasses() const { 168 return current_class_index_ + 1 < dex_file_->NumClassDefs(); 169 } 170 171 void Next() { 172 ++current_class_index_; 173 cached_descriptor_ = GetClassDescriptor(dex_file_, current_class_index_); 174 } 175 176 size_t GetCurrentClassIndex() const { 177 return current_class_index_; 178 } 179 180 bool FromLoadedOat() const { 181 return from_loaded_oat_; 182 } 183 184 const DexFile* GetDexFile() const { 185 return dex_file_; 186 } 187 188 private: 189 static const char* GetClassDescriptor(const DexFile* dex_file, size_t index) { 190 DCHECK(IsUint<16>(index)); 191 const DexFile::ClassDef& class_def = dex_file->GetClassDef(static_cast<uint16_t>(index)); 192 return dex_file->StringByTypeIdx(class_def.class_idx_); 193 } 194 195 const char* cached_descriptor_; 196 const DexFile* dex_file_; 197 size_t current_class_index_; 198 bool from_loaded_oat_; // We only need to compare mismatches between what we load now 199 // and what was loaded before. Any old duplicates must have been 200 // OK, and any new "internal" duplicates are as well (they must 201 // be from multidex, which resolves correctly). 202 }; 203 204 static void AddDexFilesFromOat(const OatFile* oat_file, 205 bool already_loaded, 206 /*out*/std::priority_queue<DexFileAndClassPair>* heap, 207 std::vector<std::unique_ptr<const DexFile>>* opened_dex_files) { 208 for (const OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) { 209 std::string error; 210 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error); 211 if (dex_file == nullptr) { 212 LOG(WARNING) << "Could not create dex file from oat file: " << error; 213 } else if (dex_file->NumClassDefs() > 0U) { 214 heap->emplace(dex_file.get(), /*current_class_index*/0U, already_loaded); 215 opened_dex_files->push_back(std::move(dex_file)); 216 } 217 } 218 } 219 220 static void AddNext(/*inout*/DexFileAndClassPair* original, 221 /*inout*/std::priority_queue<DexFileAndClassPair>* heap) { 222 if (original->DexFileHasMoreClasses()) { 223 original->Next(); 224 heap->push(std::move(*original)); 225 } 226 } 227 228 static void IterateOverJavaDexFile(mirror::Object* dex_file, 229 ArtField* const cookie_field, 230 std::function<bool(const DexFile*)> fn) 231 SHARED_REQUIRES(Locks::mutator_lock_) { 232 if (dex_file != nullptr) { 233 mirror::LongArray* long_array = cookie_field->GetObject(dex_file)->AsLongArray(); 234 if (long_array == nullptr) { 235 // This should never happen so log a warning. 236 LOG(WARNING) << "Null DexFile::mCookie"; 237 return; 238 } 239 int32_t long_array_size = long_array->GetLength(); 240 // Start from 1 to skip the oat file. 241 for (int32_t j = 1; j < long_array_size; ++j) { 242 const DexFile* cp_dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>( 243 long_array->GetWithoutChecks(j))); 244 if (!fn(cp_dex_file)) { 245 return; 246 } 247 } 248 } 249 } 250 251 static void IterateOverPathClassLoader( 252 ScopedObjectAccessAlreadyRunnable& soa, 253 Handle<mirror::ClassLoader> class_loader, 254 MutableHandle<mirror::ObjectArray<mirror::Object>> dex_elements, 255 std::function<bool(const DexFile*)> fn) SHARED_REQUIRES(Locks::mutator_lock_) { 256 // Handle this step. 257 // Handle as if this is the child PathClassLoader. 258 // The class loader is a PathClassLoader which inherits from BaseDexClassLoader. 259 // We need to get the DexPathList and loop through it. 260 ArtField* const cookie_field = soa.DecodeField(WellKnownClasses::dalvik_system_DexFile_cookie); 261 ArtField* const dex_file_field = 262 soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); 263 mirror::Object* dex_path_list = 264 soa.DecodeField(WellKnownClasses::dalvik_system_PathClassLoader_pathList)-> 265 GetObject(class_loader.Get()); 266 if (dex_path_list != nullptr && dex_file_field != nullptr && cookie_field != nullptr) { 267 // DexPathList has an array dexElements of Elements[] which each contain a dex file. 268 mirror::Object* dex_elements_obj = 269 soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList_dexElements)-> 270 GetObject(dex_path_list); 271 // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look 272 // at the mCookie which is a DexFile vector. 273 if (dex_elements_obj != nullptr) { 274 dex_elements.Assign(dex_elements_obj->AsObjectArray<mirror::Object>()); 275 for (int32_t i = 0; i < dex_elements->GetLength(); ++i) { 276 mirror::Object* element = dex_elements->GetWithoutChecks(i); 277 if (element == nullptr) { 278 // Should never happen, fall back to java code to throw a NPE. 279 break; 280 } 281 mirror::Object* dex_file = dex_file_field->GetObject(element); 282 IterateOverJavaDexFile(dex_file, cookie_field, fn); 283 } 284 } 285 } 286 } 287 288 static bool GetDexFilesFromClassLoader( 289 ScopedObjectAccessAlreadyRunnable& soa, 290 mirror::ClassLoader* class_loader, 291 std::priority_queue<DexFileAndClassPair>* queue) SHARED_REQUIRES(Locks::mutator_lock_) { 292 if (ClassLinker::IsBootClassLoader(soa, class_loader)) { 293 // The boot class loader. We don't load any of these files, as we know we compiled against 294 // them correctly. 295 return true; 296 } 297 298 // Unsupported class-loader? 299 if (class_loader->GetClass() != 300 soa.Decode<mirror::Class*>(WellKnownClasses::dalvik_system_PathClassLoader)) { 301 VLOG(class_linker) << "Unsupported class-loader " << PrettyClass(class_loader->GetClass()); 302 return false; 303 } 304 305 bool recursive_result = GetDexFilesFromClassLoader(soa, class_loader->GetParent(), queue); 306 if (!recursive_result) { 307 // Something wrong up the chain. 308 return false; 309 } 310 311 // Collect all the dex files. 312 auto GetDexFilesFn = [&] (const DexFile* cp_dex_file) 313 SHARED_REQUIRES(Locks::mutator_lock_) { 314 if (cp_dex_file->NumClassDefs() > 0) { 315 queue->emplace(cp_dex_file, 0U, true); 316 } 317 return true; // Continue looking. 318 }; 319 320 // Handle for dex-cache-element. 321 StackHandleScope<3> hs(soa.Self()); 322 MutableHandle<mirror::ObjectArray<mirror::Object>> dex_elements( 323 hs.NewHandle<mirror::ObjectArray<mirror::Object>>(nullptr)); 324 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); 325 326 IterateOverPathClassLoader(soa, h_class_loader, dex_elements, GetDexFilesFn); 327 328 return true; 329 } 330 331 static void GetDexFilesFromDexElementsArray( 332 ScopedObjectAccessAlreadyRunnable& soa, 333 Handle<mirror::ObjectArray<mirror::Object>> dex_elements, 334 std::priority_queue<DexFileAndClassPair>* queue) SHARED_REQUIRES(Locks::mutator_lock_) { 335 if (dex_elements.Get() == nullptr) { 336 // Nothing to do. 337 return; 338 } 339 340 ArtField* const cookie_field = soa.DecodeField(WellKnownClasses::dalvik_system_DexFile_cookie); 341 ArtField* const dex_file_field = 342 soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); 343 const mirror::Class* const element_class = soa.Decode<mirror::Class*>( 344 WellKnownClasses::dalvik_system_DexPathList__Element); 345 const mirror::Class* const dexfile_class = soa.Decode<mirror::Class*>( 346 WellKnownClasses::dalvik_system_DexFile); 347 348 // Collect all the dex files. 349 auto GetDexFilesFn = [&] (const DexFile* cp_dex_file) 350 SHARED_REQUIRES(Locks::mutator_lock_) { 351 if (cp_dex_file != nullptr && cp_dex_file->NumClassDefs() > 0) { 352 queue->emplace(cp_dex_file, 0U, true); 353 } 354 return true; // Continue looking. 355 }; 356 357 for (int32_t i = 0; i < dex_elements->GetLength(); ++i) { 358 mirror::Object* element = dex_elements->GetWithoutChecks(i); 359 if (element == nullptr) { 360 continue; 361 } 362 363 // We support this being dalvik.system.DexPathList$Element and dalvik.system.DexFile. 364 365 mirror::Object* dex_file; 366 if (element->GetClass() == element_class) { 367 dex_file = dex_file_field->GetObject(element); 368 } else if (element->GetClass() == dexfile_class) { 369 dex_file = element; 370 } else { 371 LOG(WARNING) << "Unsupported element in dex_elements: " << PrettyClass(element->GetClass()); 372 continue; 373 } 374 375 IterateOverJavaDexFile(dex_file, cookie_field, GetDexFilesFn); 376 } 377 } 378 379 static bool AreSharedLibrariesOk(const std::string shared_libraries, 380 std::priority_queue<DexFileAndClassPair>& queue) { 381 if (shared_libraries.empty()) { 382 if (queue.empty()) { 383 // No shared libraries or oat files, as expected. 384 return true; 385 } 386 } else { 387 if (shared_libraries.compare(OatFile::kSpecialSharedLibrary) == 0) { 388 // If we find the special shared library, skip the shared libraries check. 389 return true; 390 } 391 // Shared libraries is a series of dex file paths and their checksums, each separated by '*'. 392 std::vector<std::string> shared_libraries_split; 393 Split(shared_libraries, '*', &shared_libraries_split); 394 395 size_t index = 0; 396 std::priority_queue<DexFileAndClassPair> temp = queue; 397 while (!temp.empty() && index < shared_libraries_split.size() - 1) { 398 DexFileAndClassPair pair(temp.top()); 399 const DexFile* dex_file = pair.GetDexFile(); 400 std::string dex_filename(dex_file->GetLocation()); 401 uint32_t dex_checksum = dex_file->GetLocationChecksum(); 402 if (dex_filename != shared_libraries_split[index] || 403 dex_checksum != std::stoul(shared_libraries_split[index + 1])) { 404 break; 405 } 406 temp.pop(); 407 index += 2; 408 } 409 410 // Check is successful if it made it through the queue and all the shared libraries. 411 return temp.empty() && index == shared_libraries_split.size(); 412 } 413 return false; 414 } 415 416 // Check for class-def collisions in dex files. 417 // 418 // This first walks the class loader chain, getting all the dex files from the class loader. If 419 // the class loader is null or one of the class loaders in the chain is unsupported, we collect 420 // dex files from all open non-boot oat files to be safe. 421 // 422 // This first checks whether the shared libraries are in the expected order and the oat files 423 // have the expected checksums. If so, we exit early. Otherwise, we do the collision check. 424 // 425 // The collision check works by maintaining a heap with one class from each dex file, sorted by the 426 // class descriptor. Then a dex-file/class pair is continually removed from the heap and compared 427 // against the following top element. If the descriptor is the same, it is now checked whether 428 // the two elements agree on whether their dex file was from an already-loaded oat-file or the 429 // new oat file. Any disagreement indicates a collision. 430 bool OatFileManager::HasCollisions(const OatFile* oat_file, 431 jobject class_loader, 432 jobjectArray dex_elements, 433 std::string* error_msg /*out*/) const { 434 DCHECK(oat_file != nullptr); 435 DCHECK(error_msg != nullptr); 436 437 std::priority_queue<DexFileAndClassPair> queue; 438 439 // Try to get dex files from the given class loader. If the class loader is null, or we do 440 // not support one of the class loaders in the chain, conservatively compare against all 441 // (non-boot) oat files. 442 bool class_loader_ok = false; 443 { 444 ScopedObjectAccess soa(Thread::Current()); 445 StackHandleScope<2> hs(Thread::Current()); 446 Handle<mirror::ClassLoader> h_class_loader = 447 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(class_loader)); 448 Handle<mirror::ObjectArray<mirror::Object>> h_dex_elements = 449 hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Object>*>(dex_elements)); 450 if (h_class_loader.Get() != nullptr && 451 GetDexFilesFromClassLoader(soa, h_class_loader.Get(), &queue)) { 452 class_loader_ok = true; 453 454 // In this case, also take into account the dex_elements array, if given. We don't need to 455 // read it otherwise, as we'll compare against all open oat files anyways. 456 GetDexFilesFromDexElementsArray(soa, h_dex_elements, &queue); 457 } else if (h_class_loader.Get() != nullptr) { 458 VLOG(class_linker) << "Something unsupported with " 459 << PrettyClass(h_class_loader->GetClass()); 460 } 461 } 462 463 // Dex files are registered late - once a class is actually being loaded. We have to compare 464 // against the open oat files. Take the oat_file_manager_lock_ that protects oat_files_ accesses. 465 ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); 466 467 // Vector that holds the newly opened dex files live, this is done to prevent leaks. 468 std::vector<std::unique_ptr<const DexFile>> opened_dex_files; 469 470 if (!class_loader_ok) { 471 // Add dex files from already loaded oat files, but skip boot. 472 473 // Clean up the queue. 474 while (!queue.empty()) { 475 queue.pop(); 476 } 477 478 std::vector<const OatFile*> boot_oat_files = GetBootOatFiles(); 479 // The same OatFile can be loaded multiple times at different addresses. In this case, we don't 480 // need to check both against each other since they would have resolved the same way at compile 481 // time. 482 std::unordered_set<std::string> unique_locations; 483 for (const std::unique_ptr<const OatFile>& loaded_oat_file : oat_files_) { 484 DCHECK_NE(loaded_oat_file.get(), oat_file); 485 const std::string& location = loaded_oat_file->GetLocation(); 486 if (std::find(boot_oat_files.begin(), boot_oat_files.end(), loaded_oat_file.get()) == 487 boot_oat_files.end() && location != oat_file->GetLocation() && 488 unique_locations.find(location) == unique_locations.end()) { 489 unique_locations.insert(location); 490 AddDexFilesFromOat(loaded_oat_file.get(), 491 /*already_loaded*/true, 492 &queue, 493 /*out*/&opened_dex_files); 494 } 495 } 496 } 497 498 // Exit if shared libraries are ok. Do a full duplicate classes check otherwise. 499 const std::string 500 shared_libraries(oat_file->GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey)); 501 if (AreSharedLibrariesOk(shared_libraries, queue)) { 502 return false; 503 } 504 505 // Add dex files from the oat file to check. 506 AddDexFilesFromOat(oat_file, /*already_loaded*/false, &queue, &opened_dex_files); 507 508 // Now drain the queue. 509 while (!queue.empty()) { 510 // Modifying the top element is only safe if we pop right after. 511 DexFileAndClassPair compare_pop(queue.top()); 512 queue.pop(); 513 514 // Compare against the following elements. 515 while (!queue.empty()) { 516 DexFileAndClassPair top(queue.top()); 517 518 if (strcmp(compare_pop.GetCachedDescriptor(), top.GetCachedDescriptor()) == 0) { 519 // Same descriptor. Check whether it's crossing old-oat-files to new-oat-files. 520 if (compare_pop.FromLoadedOat() != top.FromLoadedOat()) { 521 *error_msg = 522 StringPrintf("Found duplicated class when checking oat files: '%s' in %s and %s", 523 compare_pop.GetCachedDescriptor(), 524 compare_pop.GetDexFile()->GetLocation().c_str(), 525 top.GetDexFile()->GetLocation().c_str()); 526 return true; 527 } 528 queue.pop(); 529 AddNext(&top, &queue); 530 } else { 531 // Something else. Done here. 532 break; 533 } 534 } 535 AddNext(&compare_pop, &queue); 536 } 537 538 return false; 539 } 540 541 std::vector<std::unique_ptr<const DexFile>> OatFileManager::OpenDexFilesFromOat( 542 const char* dex_location, 543 const char* oat_location, 544 jobject class_loader, 545 jobjectArray dex_elements, 546 const OatFile** out_oat_file, 547 std::vector<std::string>* error_msgs) { 548 ScopedTrace trace(__FUNCTION__); 549 CHECK(dex_location != nullptr); 550 CHECK(error_msgs != nullptr); 551 552 // Verify we aren't holding the mutator lock, which could starve GC if we 553 // have to generate or relocate an oat file. 554 Thread* const self = Thread::Current(); 555 Locks::mutator_lock_->AssertNotHeld(self); 556 Runtime* const runtime = Runtime::Current(); 557 558 OatFileAssistant oat_file_assistant(dex_location, 559 oat_location, 560 kRuntimeISA, 561 /*profile_changed*/false, 562 !runtime->IsAotCompiler()); 563 564 // Lock the target oat location to avoid races generating and loading the 565 // oat file. 566 std::string error_msg; 567 if (!oat_file_assistant.Lock(/*out*/&error_msg)) { 568 // Don't worry too much if this fails. If it does fail, it's unlikely we 569 // can generate an oat file anyway. 570 VLOG(class_linker) << "OatFileAssistant::Lock: " << error_msg; 571 } 572 573 const OatFile* source_oat_file = nullptr; 574 575 if (!oat_file_assistant.IsUpToDate()) { 576 // Update the oat file on disk if we can. This may fail, but that's okay. 577 // Best effort is all that matters here. 578 switch (oat_file_assistant.MakeUpToDate(filter_, /*out*/ &error_msg)) { 579 case OatFileAssistant::kUpdateFailed: 580 LOG(WARNING) << error_msg; 581 break; 582 583 case OatFileAssistant::kUpdateNotAttempted: 584 // Avoid spamming the logs if we decided not to attempt making the oat 585 // file up to date. 586 VLOG(oat) << error_msg; 587 break; 588 589 case OatFileAssistant::kUpdateSucceeded: 590 // Nothing to do. 591 break; 592 } 593 } 594 595 // Get the oat file on disk. 596 std::unique_ptr<const OatFile> oat_file(oat_file_assistant.GetBestOatFile().release()); 597 598 if (oat_file != nullptr) { 599 // Take the file only if it has no collisions, or we must take it because of preopting. 600 bool accept_oat_file = 601 !HasCollisions(oat_file.get(), class_loader, dex_elements, /*out*/ &error_msg); 602 if (!accept_oat_file) { 603 // Failed the collision check. Print warning. 604 if (Runtime::Current()->IsDexFileFallbackEnabled()) { 605 LOG(WARNING) << "Found duplicate classes, falling back to interpreter mode for " 606 << dex_location; 607 } else { 608 LOG(WARNING) << "Found duplicate classes, dex-file-fallback disabled, will be failing to " 609 " load classes for " << dex_location; 610 } 611 LOG(WARNING) << error_msg; 612 613 // However, if the app was part of /system and preopted, there is no original dex file 614 // available. In that case grudgingly accept the oat file. 615 if (!oat_file_assistant.HasOriginalDexFiles()) { 616 accept_oat_file = true; 617 LOG(WARNING) << "Dex location " << dex_location << " does not seem to include dex file. " 618 << "Allow oat file use. This is potentially dangerous."; 619 } 620 } 621 622 if (accept_oat_file) { 623 VLOG(class_linker) << "Registering " << oat_file->GetLocation(); 624 source_oat_file = RegisterOatFile(std::move(oat_file)); 625 *out_oat_file = source_oat_file; 626 } 627 } 628 629 std::vector<std::unique_ptr<const DexFile>> dex_files; 630 631 // Load the dex files from the oat file. 632 if (source_oat_file != nullptr) { 633 bool added_image_space = false; 634 if (source_oat_file->IsExecutable()) { 635 std::unique_ptr<gc::space::ImageSpace> image_space( 636 kEnableAppImage ? oat_file_assistant.OpenImageSpace(source_oat_file) : nullptr); 637 if (image_space != nullptr) { 638 ScopedObjectAccess soa(self); 639 StackHandleScope<1> hs(self); 640 Handle<mirror::ClassLoader> h_loader( 641 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(class_loader))); 642 // Can not load app image without class loader. 643 if (h_loader.Get() != nullptr) { 644 std::string temp_error_msg; 645 // Add image space has a race condition since other threads could be reading from the 646 // spaces array. 647 { 648 ScopedThreadSuspension sts(self, kSuspended); 649 gc::ScopedGCCriticalSection gcs(self, 650 gc::kGcCauseAddRemoveAppImageSpace, 651 gc::kCollectorTypeAddRemoveAppImageSpace); 652 ScopedSuspendAll ssa("Add image space"); 653 runtime->GetHeap()->AddSpace(image_space.get()); 654 } 655 { 656 ScopedTrace trace2(StringPrintf("Adding image space for location %s", dex_location)); 657 added_image_space = runtime->GetClassLinker()->AddImageSpace(image_space.get(), 658 h_loader, 659 dex_elements, 660 dex_location, 661 /*out*/&dex_files, 662 /*out*/&temp_error_msg); 663 } 664 if (added_image_space) { 665 // Successfully added image space to heap, release the map so that it does not get 666 // freed. 667 image_space.release(); 668 } else { 669 LOG(INFO) << "Failed to add image file " << temp_error_msg; 670 dex_files.clear(); 671 { 672 ScopedThreadSuspension sts(self, kSuspended); 673 gc::ScopedGCCriticalSection gcs(self, 674 gc::kGcCauseAddRemoveAppImageSpace, 675 gc::kCollectorTypeAddRemoveAppImageSpace); 676 ScopedSuspendAll ssa("Remove image space"); 677 runtime->GetHeap()->RemoveSpace(image_space.get()); 678 } 679 // Non-fatal, don't update error_msg. 680 } 681 } 682 } 683 } 684 if (!added_image_space) { 685 DCHECK(dex_files.empty()); 686 dex_files = oat_file_assistant.LoadDexFiles(*source_oat_file, dex_location); 687 } 688 if (dex_files.empty()) { 689 error_msgs->push_back("Failed to open dex files from " + source_oat_file->GetLocation()); 690 } 691 } 692 693 // Fall back to running out of the original dex file if we couldn't load any 694 // dex_files from the oat file. 695 if (dex_files.empty()) { 696 if (oat_file_assistant.HasOriginalDexFiles()) { 697 if (Runtime::Current()->IsDexFileFallbackEnabled()) { 698 if (!DexFile::Open(dex_location, dex_location, /*out*/ &error_msg, &dex_files)) { 699 LOG(WARNING) << error_msg; 700 error_msgs->push_back("Failed to open dex files from " + std::string(dex_location) 701 + " because: " + error_msg); 702 } 703 } else { 704 error_msgs->push_back("Fallback mode disabled, skipping dex files."); 705 } 706 } else { 707 error_msgs->push_back("No original dex files found for dex location " 708 + std::string(dex_location)); 709 } 710 } 711 712 // TODO(calin): Consider optimizing this knowing that is useless to record the 713 // use of fully compiled apks. 714 Runtime::Current()->NotifyDexLoaded(dex_location); 715 return dex_files; 716 } 717 718 void OatFileManager::DumpForSigQuit(std::ostream& os) { 719 ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); 720 std::vector<const OatFile*> boot_oat_files = GetBootOatFiles(); 721 for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) { 722 if (ContainsElement(boot_oat_files, oat_file.get())) { 723 continue; 724 } 725 os << oat_file->GetLocation() << ": " << oat_file->GetCompilerFilter() << "\n"; 726 } 727 } 728 729 } // namespace art 730