oat_file_manager.cc revision 57943810cfc789da890d73621741729da5feaaf8
1f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier/*
2f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier * Copyright (C) 2015 The Android Open Source Project
3f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier *
4f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier * Licensed under the Apache License, Version 2.0 (the "License");
5f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier * you may not use this file except in compliance with the License.
6f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier * You may obtain a copy of the License at
7f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier *
8f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier *      http://www.apache.org/licenses/LICENSE-2.0
9f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier *
10f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier * Unless required by applicable law or agreed to in writing, software
11f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier * distributed under the License is distributed on an "AS IS" BASIS,
12f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier * See the License for the specific language governing permissions and
14f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier * limitations under the License.
15f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier */
16f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
17f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier#include "oat_file_manager.h"
18f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
19f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier#include <memory>
20f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier#include <queue>
21f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier#include <vector>
22f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
2346ee31b67d7ee1bd085fbc240502053caa3cf8faAndreas Gampe#include "android-base/stringprintf.h"
2468bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray#include "android-base/strings.h"
2546ee31b67d7ee1bd085fbc240502053caa3cf8faAndreas Gampe
2690b936ddda63139ff46a6755c3b83ad6e4ab4ac5Andreas Gampe#include "art_field-inl.h"
278ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao#include "base/bit_vector-inl.h"
28891a50e9dc0f02ee34bd7348f80b621249153af5David Sehr#include "base/file_utils.h"
2957943810cfc789da890d73621741729da5feaaf8Andreas Gampe#include "base/logging.h"  // For VLOG.
30f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier#include "base/stl_util.h"
3132ce2adefb8a3d0eda59a29f5e87c1eb43eef796Mathieu Chartier#include "base/systrace.h"
32fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier#include "class_linker.h"
337b0648aa7cb4b7a58e73bf353e031dfe4553d9d7Calin Juravle#include "class_loader_context.h"
3480b37b7e679a530738c9bcbd39873b6dacf177e5Mathieu Chartier#include "dex_file-inl.h"
3579c87da9d4698ec58ece65af0065eebd55a1cfe0Mathieu Chartier#include "dex_file_loader.h"
360bb4031b976aeb2bad88db81115fea46b19878f0Bharadwaj Kalandhabhatta#include "dex_file_tracking_registrar.h"
3761d2b2d353ba4ab952247d2bff2c905598118bb4Mathieu Chartier#include "gc/scoped_gc_critical_section.h"
38f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier#include "gc/space/image_space.h"
39fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier#include "handle_scope-inl.h"
4008883debd927d18c9ecf66683a2a11aa98165656Andreas Gampe#include "jni_internal.h"
41fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier#include "mirror/class_loader.h"
4290b936ddda63139ff46a6755c3b83ad6e4ab4ac5Andreas Gampe#include "mirror/object-inl.h"
43f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier#include "oat_file_assistant.h"
443398c7874e002beaa6c2b2fadf183e7d1ddad23aMathieu Chartier#include "obj_ptr-inl.h"
450795f23920ee9aabf28e45c63cd592dcccf00216Mathieu Chartier#include "scoped_thread_state_change-inl.h"
46b486a98aadc95d80548953410cf23edba62259faAndreas Gampe#include "thread-current-inl.h"
47a9d82fe8bc6960b565245b920e99107a824ca515Mathieu Chartier#include "thread_list.h"
4890b936ddda63139ff46a6755c3b83ad6e4ab4ac5Andreas Gampe#include "well_known_classes.h"
49f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
50f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartiernamespace art {
51f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
5246ee31b67d7ee1bd085fbc240502053caa3cf8faAndreas Gampeusing android::base::StringPrintf;
5346ee31b67d7ee1bd085fbc240502053caa3cf8faAndreas Gampe
54120aa286ab6adf3e76a31bc61fb4e583e5158d71Mathieu Chartier// If true, we attempt to load the application image if it exists.
55fbc31087932a65e036a153afab3049dc5298656aMathieu Chartierstatic constexpr bool kEnableAppImage = true;
56fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier
5768bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffraystatic bool OatFileIsOnSystem(const std::unique_ptr<const OatFile>& oat_file) {
5868bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray  UniqueCPtr<const char[]> path(realpath(oat_file->GetLocation().c_str(), nullptr));
593f093891e55383055eecc940b724ee1f24f50123Andreas Gampe  return path != nullptr && android::base::StartsWith(oat_file->GetLocation(),
603f093891e55383055eecc940b724ee1f24f50123Andreas Gampe                                                      GetAndroidRoot().c_str());
6168bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray}
6268bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray
63f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartierconst OatFile* OatFileManager::RegisterOatFile(std::unique_ptr<const OatFile> oat_file) {
64e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  WriterMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
6568bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray  CHECK(!only_use_system_oat_files_ || OatFileIsOnSystem(oat_file))
6668bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray      << "Registering a non /system oat file: " << oat_file->GetLocation();
67f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  DCHECK(oat_file != nullptr);
68f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  if (kIsDebugBuild) {
69e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier    CHECK(oat_files_.find(oat_file) == oat_files_.end());
70f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    for (const std::unique_ptr<const OatFile>& existing : oat_files_) {
71f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      CHECK_NE(oat_file.get(), existing.get()) << oat_file->GetLocation();
72f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      // Check that we don't have an oat file with the same address. Copies of the same oat file
73f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      // should be loaded at different addresses.
74f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      CHECK_NE(oat_file->Begin(), existing->Begin()) << "Oat file already mapped at that location";
75f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    }
76f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
77f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  have_non_pic_oat_file_ = have_non_pic_oat_file_ || !oat_file->IsPic();
78e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  const OatFile* ret = oat_file.get();
79e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  oat_files_.insert(std::move(oat_file));
80e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  return ret;
81e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier}
82e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier
83e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartiervoid OatFileManager::UnRegisterAndDeleteOatFile(const OatFile* oat_file) {
84e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  WriterMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
85e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  DCHECK(oat_file != nullptr);
86e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  std::unique_ptr<const OatFile> compare(oat_file);
87e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  auto it = oat_files_.find(compare);
88e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  CHECK(it != oat_files_.end());
89e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  oat_files_.erase(it);
90e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  compare.release();
91f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier}
92f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
930b791279f67b0f4b72db991707b16b737871bcdeCalin Juravleconst OatFile* OatFileManager::FindOpenedOatFileFromDexLocation(
940b791279f67b0f4b72db991707b16b737871bcdeCalin Juravle    const std::string& dex_base_location) const {
950b791279f67b0f4b72db991707b16b737871bcdeCalin Juravle  ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
960b791279f67b0f4b72db991707b16b737871bcdeCalin Juravle  for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) {
970b791279f67b0f4b72db991707b16b737871bcdeCalin Juravle    const std::vector<const OatDexFile*>& oat_dex_files = oat_file->GetOatDexFiles();
980b791279f67b0f4b72db991707b16b737871bcdeCalin Juravle    for (const OatDexFile* oat_dex_file : oat_dex_files) {
9979c87da9d4698ec58ece65af0065eebd55a1cfe0Mathieu Chartier      if (DexFileLoader::GetBaseLocation(oat_dex_file->GetDexFileLocation()) == dex_base_location) {
1000b791279f67b0f4b72db991707b16b737871bcdeCalin Juravle        return oat_file.get();
1010b791279f67b0f4b72db991707b16b737871bcdeCalin Juravle      }
1020b791279f67b0f4b72db991707b16b737871bcdeCalin Juravle    }
1030b791279f67b0f4b72db991707b16b737871bcdeCalin Juravle  }
1040b791279f67b0f4b72db991707b16b737871bcdeCalin Juravle  return nullptr;
1050b791279f67b0f4b72db991707b16b737871bcdeCalin Juravle}
1060b791279f67b0f4b72db991707b16b737871bcdeCalin Juravle
107f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartierconst OatFile* OatFileManager::FindOpenedOatFileFromOatLocation(const std::string& oat_location)
108f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    const {
109f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
110e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  return FindOpenedOatFileFromOatLocationLocked(oat_location);
111e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier}
112e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier
113e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartierconst OatFile* OatFileManager::FindOpenedOatFileFromOatLocationLocked(
114e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier    const std::string& oat_location) const {
115f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) {
116f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    if (oat_file->GetLocation() == oat_location) {
117f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      return oat_file.get();
118f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    }
119f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
120f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  return nullptr;
121f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier}
122f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
123dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Haostd::vector<const OatFile*> OatFileManager::GetBootOatFiles() const {
124dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  std::vector<const OatFile*> oat_files;
125dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  std::vector<gc::space::ImageSpace*> image_spaces =
126dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      Runtime::Current()->GetHeap()->GetBootImageSpaces();
127dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  for (gc::space::ImageSpace* image_space : image_spaces) {
128dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao    oat_files.push_back(image_space->GetOatFile());
129dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  }
130dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  return oat_files;
131f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier}
132f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
133f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartierconst OatFile* OatFileManager::GetPrimaryOatFile() const {
134f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
135dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  std::vector<const OatFile*> boot_oat_files = GetBootOatFiles();
136dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  if (!boot_oat_files.empty()) {
137f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) {
138dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      if (std::find(boot_oat_files.begin(), boot_oat_files.end(), oat_file.get()) ==
139dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao          boot_oat_files.end()) {
140f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier        return oat_file.get();
141f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      }
142f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    }
143f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
144f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  return nullptr;
145f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier}
146f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
147b0b68cfc0d2444e2333adcc5a6bc6f670b89fe83Vladimir MarkoOatFileManager::OatFileManager()
148b0b68cfc0d2444e2333adcc5a6bc6f670b89fe83Vladimir Marko    : have_non_pic_oat_file_(false), only_use_system_oat_files_(false) {}
149b0b68cfc0d2444e2333adcc5a6bc6f670b89fe83Vladimir Marko
150f9c6fc610b27887f832e453a0da1789187293408Mathieu ChartierOatFileManager::~OatFileManager() {
151e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  // Explicitly clear oat_files_ since the OatFile destructor calls back into OatFileManager for
152e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  // UnRegisterOatFileLocation.
153e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  oat_files_.clear();
154f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier}
155f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
156dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Haostd::vector<const OatFile*> OatFileManager::RegisterImageOatFiles(
157dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao    std::vector<gc::space::ImageSpace*> spaces) {
158dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  std::vector<const OatFile*> oat_files;
159dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  for (gc::space::ImageSpace* space : spaces) {
160dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao    oat_files.push_back(RegisterOatFile(space->ReleaseOatFile()));
161dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  }
162dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  return oat_files;
163f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier}
164f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
1658ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Haoclass TypeIndexInfo {
1668ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao public:
1678ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  explicit TypeIndexInfo(const DexFile* dex_file)
1688ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao      : type_indexes_(GenerateTypeIndexes(dex_file)),
1698ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao        iter_(type_indexes_.Indexes().begin()),
1708ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao        end_(type_indexes_.Indexes().end()) { }
1718ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao
1728ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  BitVector& GetTypeIndexes() {
1738ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    return type_indexes_;
1748ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  }
1758ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  BitVector::IndexIterator& GetIterator() {
1768ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    return iter_;
1778ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  }
1788ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  BitVector::IndexIterator& GetIteratorEnd() {
1798ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    return end_;
1808ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  }
1818ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  void AdvanceIterator() {
1828ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    iter_++;
1838ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  }
1848ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao
1858ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao private:
1868ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  static BitVector GenerateTypeIndexes(const DexFile* dex_file) {
1878ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    BitVector type_indexes(/*start_bits*/0, /*expandable*/true, Allocator::GetMallocAllocator());
1888ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    for (uint16_t i = 0; i < dex_file->NumClassDefs(); ++i) {
1898ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao      const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
1908ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao      uint16_t type_idx = class_def.class_idx_.index_;
1918ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao      type_indexes.SetBit(type_idx);
1928ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    }
1938ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    return type_indexes;
1948ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  }
1958ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao
1968ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  // BitVector with bits set for the type indexes of all classes in the input dex file.
1978ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  BitVector type_indexes_;
1988ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  BitVector::IndexIterator iter_;
1998ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  BitVector::IndexIterator end_;
2008ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao};
2018ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao
202f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartierclass DexFileAndClassPair : ValueObject {
203f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier public:
2048ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  DexFileAndClassPair(const DexFile* dex_file, TypeIndexInfo* type_info, bool from_loaded_oat)
2058ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao     : type_info_(type_info),
206f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier       dex_file_(dex_file),
2078ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao       cached_descriptor_(dex_file_->StringByTypeIdx(dex::TypeIndex(*type_info->GetIterator()))),
2088ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao       from_loaded_oat_(from_loaded_oat) {
2098ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    type_info_->AdvanceIterator();
2108ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  }
211f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
21280b37b7e679a530738c9bcbd39873b6dacf177e5Mathieu Chartier  DexFileAndClassPair(const DexFileAndClassPair& rhs) = default;
213f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
21480b37b7e679a530738c9bcbd39873b6dacf177e5Mathieu Chartier  DexFileAndClassPair& operator=(const DexFileAndClassPair& rhs) = default;
215f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
216f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  const char* GetCachedDescriptor() const {
217f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    return cached_descriptor_;
218f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
219f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
220f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  bool operator<(const DexFileAndClassPair& rhs) const {
221f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    const int cmp = strcmp(cached_descriptor_, rhs.cached_descriptor_);
222f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    if (cmp != 0) {
223f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      // Note that the order must be reversed. We want to iterate over the classes in dex files.
224f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      // They are sorted lexicographically. Thus, the priority-queue must be a min-queue.
225f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      return cmp > 0;
226f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    }
227f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    return dex_file_ < rhs.dex_file_;
228f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
229f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
230f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  bool DexFileHasMoreClasses() const {
2318ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    return type_info_->GetIterator() != type_info_->GetIteratorEnd();
232f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
233f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
234f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  void Next() {
2358ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    cached_descriptor_ = dex_file_->StringByTypeIdx(dex::TypeIndex(*type_info_->GetIterator()));
2368ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    type_info_->AdvanceIterator();
237f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
238f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
239f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  bool FromLoadedOat() const {
240f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    return from_loaded_oat_;
241f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
242f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
243f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  const DexFile* GetDexFile() const {
244f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao    return dex_file_;
245f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao  }
246f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao
247f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier private:
2488ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  TypeIndexInfo* type_info_;
249f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao  const DexFile* dex_file_;
2508ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  const char* cached_descriptor_;
251f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  bool from_loaded_oat_;  // We only need to compare mismatches between what we load now
252f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier                          // and what was loaded before. Any old duplicates must have been
253f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier                          // OK, and any new "internal" duplicates are as well (they must
254f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier                          // be from multidex, which resolves correctly).
255f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier};
256f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
2578ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Haostatic void AddDexFilesFromOat(
2588ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    const OatFile* oat_file,
2598ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    /*out*/std::vector<const DexFile*>* dex_files,
2608ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    std::vector<std::unique_ptr<const DexFile>>* opened_dex_files) {
261f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  for (const OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
262f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    std::string error;
263f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error);
264f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    if (dex_file == nullptr) {
265f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      LOG(WARNING) << "Could not create dex file from oat file: " << error;
266f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    } else if (dex_file->NumClassDefs() > 0U) {
2678ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao      dex_files->push_back(dex_file.get());
26814d7b3e37b6129d845d6c8da8ee8d612937c63d4Mathieu Chartier      opened_dex_files->push_back(std::move(dex_file));
269f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    }
270f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
271f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier}
272f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
2738ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Haostatic void AddNext(/*inout*/DexFileAndClassPair& original,
2748ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao                    /*inout*/std::priority_queue<DexFileAndClassPair>& heap) {
2758ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  if (original.DexFileHasMoreClasses()) {
2768ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    original.Next();
2778ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    heap.push(std::move(original));
278f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao  }
279f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao}
280f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao
2818ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Haostatic bool CollisionCheck(std::vector<const DexFile*>& dex_files_loaded,
2828ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao                           std::vector<const DexFile*>& dex_files_unloaded,
2838ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao                           std::string* error_msg /*out*/) {
2848ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  // Generate type index information for each dex file.
2858ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  std::vector<TypeIndexInfo> loaded_types;
2868ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  for (const DexFile* dex_file : dex_files_loaded) {
2878ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    loaded_types.push_back(TypeIndexInfo(dex_file));
2888ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  }
2898ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  std::vector<TypeIndexInfo> unloaded_types;
2908ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  for (const DexFile* dex_file : dex_files_unloaded) {
2918ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    unloaded_types.push_back(TypeIndexInfo(dex_file));
2928ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  }
2938ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao
2948ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  // Populate the queue of dex file and class pairs with the loaded and unloaded dex files.
2958ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  std::priority_queue<DexFileAndClassPair> queue;
2968ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  for (size_t i = 0; i < dex_files_loaded.size(); ++i) {
2978ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    if (loaded_types[i].GetIterator() != loaded_types[i].GetIteratorEnd()) {
2988ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao      queue.emplace(dex_files_loaded[i], &loaded_types[i], /*from_loaded_oat*/true);
2998ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    }
3008ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  }
3018ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  for (size_t i = 0; i < dex_files_unloaded.size(); ++i) {
3028ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    if (unloaded_types[i].GetIterator() != unloaded_types[i].GetIteratorEnd()) {
3038ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao      queue.emplace(dex_files_unloaded[i], &unloaded_types[i], /*from_loaded_oat*/false);
3048ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    }
3058ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  }
3068ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao
3078ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  // Now drain the queue.
3080471ece3d0166097619da19c9f43522200bfda78Jeff Hao  bool has_duplicates = false;
3090471ece3d0166097619da19c9f43522200bfda78Jeff Hao  error_msg->clear();
3108ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  while (!queue.empty()) {
3118ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    // Modifying the top element is only safe if we pop right after.
3128ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    DexFileAndClassPair compare_pop(queue.top());
3138ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    queue.pop();
3148ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao
3158ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    // Compare against the following elements.
3168ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    while (!queue.empty()) {
3178ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao      DexFileAndClassPair top(queue.top());
3188ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao      if (strcmp(compare_pop.GetCachedDescriptor(), top.GetCachedDescriptor()) == 0) {
3198ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao        // Same descriptor. Check whether it's crossing old-oat-files to new-oat-files.
3208ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao        if (compare_pop.FromLoadedOat() != top.FromLoadedOat()) {
3210471ece3d0166097619da19c9f43522200bfda78Jeff Hao          error_msg->append(
3220471ece3d0166097619da19c9f43522200bfda78Jeff Hao              StringPrintf("Found duplicated class when checking oat files: '%s' in %s and %s\n",
3238ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao                           compare_pop.GetCachedDescriptor(),
3248ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao                           compare_pop.GetDexFile()->GetLocation().c_str(),
3250471ece3d0166097619da19c9f43522200bfda78Jeff Hao                           top.GetDexFile()->GetLocation().c_str()));
3260471ece3d0166097619da19c9f43522200bfda78Jeff Hao          if (!VLOG_IS_ON(oat)) {
3270471ece3d0166097619da19c9f43522200bfda78Jeff Hao            return true;
3280471ece3d0166097619da19c9f43522200bfda78Jeff Hao          }
3290471ece3d0166097619da19c9f43522200bfda78Jeff Hao          has_duplicates = true;
3308ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao        }
3318ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao        queue.pop();
3328ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao        AddNext(top, queue);
3338ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao      } else {
3348ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao        // Something else. Done here.
335f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao        break;
336f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao      }
337f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao    }
3388ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    AddNext(compare_pop, queue);
339f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao  }
3408ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao
3410471ece3d0166097619da19c9f43522200bfda78Jeff Hao  return has_duplicates;
342f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao}
343f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao
344f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier// Check for class-def collisions in dex files.
345f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier//
34627e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle// This first walks the class loader chain present in the given context, getting all the dex files
34727e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle// from the class loader.
348f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao//
34927e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle// If the context is null (which means the initial class loader was null or unsupported)
35027e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle// this returns false. b/37777332.
35127e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle//
35227e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle// This first checks whether all class loaders in the context have the same type and
35327e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle// classpath. If so, we exit early. Otherwise, we do the collision check.
354f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao//
355f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao// The collision check works by maintaining a heap with one class from each dex file, sorted by the
356f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao// class descriptor. Then a dex-file/class pair is continually removed from the heap and compared
357f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier// against the following top element. If the descriptor is the same, it is now checked whether
358f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier// the two elements agree on whether their dex file was from an already-loaded oat-file or the
359f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier// new oat file. Any disagreement indicates a collision.
360f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartierbool OatFileManager::HasCollisions(const OatFile* oat_file,
36127e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle                                   const ClassLoaderContext* context,
362f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier                                   std::string* error_msg /*out*/) const {
363f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  DCHECK(oat_file != nullptr);
364f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  DCHECK(error_msg != nullptr);
365f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao
3663f9186446faa01636475596335240f39472ba9f4Calin Juravle  // The context might be null if there are unrecognized class loaders in the chain or they
3673f9186446faa01636475596335240f39472ba9f4Calin Juravle  // don't meet sensible sanity conditions. In this case we assume that the app knows what it's
3683f9186446faa01636475596335240f39472ba9f4Calin Juravle  // doing and accept the oat file.
3693f9186446faa01636475596335240f39472ba9f4Calin Juravle  // Note that this has correctness implications as we cannot guarantee that the class resolution
3703f9186446faa01636475596335240f39472ba9f4Calin Juravle  // used during compilation is OK (b/37777332).
3713f9186446faa01636475596335240f39472ba9f4Calin Juravle  if (context == nullptr) {
3723f9186446faa01636475596335240f39472ba9f4Calin Juravle      LOG(WARNING) << "Skipping duplicate class check due to unsupported classloader";
3735c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath      return false;
374f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
375f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
3763f9186446faa01636475596335240f39472ba9f4Calin Juravle  // If the pat file loading context matches the context used during compilation then we accept
3773f9186446faa01636475596335240f39472ba9f4Calin Juravle  // the oat file without addition checks
37844e5efa4ae79cf76c65f37fc41c1fa0ed431ec4aCalin Juravle  if (context->VerifyClassLoaderContextMatch(oat_file->GetClassLoaderContext())) {
379f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    return false;
380f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
381f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
3823f9186446faa01636475596335240f39472ba9f4Calin Juravle  // The class loader context does not match. Perform a full duplicate classes check.
3833f9186446faa01636475596335240f39472ba9f4Calin Juravle
3843f9186446faa01636475596335240f39472ba9f4Calin Juravle  std::vector<const DexFile*> dex_files_loaded = context->FlattenOpenedDexFiles();
3853f9186446faa01636475596335240f39472ba9f4Calin Juravle
3865c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath  // Vector that holds the newly opened dex files live, this is done to prevent leaks.
3875c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath  std::vector<std::unique_ptr<const DexFile>> opened_dex_files;
3885c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath
3891fdbe1b034d97b852551d24e70245df64f03ebebAndreas Gampe  ScopedTrace st("Collision check");
390f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  // Add dex files from the oat file to check.
3918ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  std::vector<const DexFile*> dex_files_unloaded;
3928ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  AddDexFilesFromOat(oat_file, &dex_files_unloaded, &opened_dex_files);
3938ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  return CollisionCheck(dex_files_loaded, dex_files_unloaded, error_msg);
394f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier}
395f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
396f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartierstd::vector<std::unique_ptr<const DexFile>> OatFileManager::OpenDexFilesFromOat(
397f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    const char* dex_location,
398fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier    jobject class_loader,
399fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier    jobjectArray dex_elements,
400e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier    const OatFile** out_oat_file,
401f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    std::vector<std::string>* error_msgs) {
40232ce2adefb8a3d0eda59a29f5e87c1eb43eef796Mathieu Chartier  ScopedTrace trace(__FUNCTION__);
403f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  CHECK(dex_location != nullptr);
404f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  CHECK(error_msgs != nullptr);
405f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
406f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  // Verify we aren't holding the mutator lock, which could starve GC if we
407f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  // have to generate or relocate an oat file.
408fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier  Thread* const self = Thread::Current();
409fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier  Locks::mutator_lock_->AssertNotHeld(self);
410fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier  Runtime* const runtime = Runtime::Current();
411b077e15d2d11b7c81aacbcd4a46c2b1e9c9ba20dCalin Juravle
41227e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle  std::unique_ptr<ClassLoaderContext> context;
41327e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle  // If the class_loader is null there's not much we can do. This happens if a dex files is loaded
41427e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle  // directly with DexFile APIs instead of using class loaders.
41527e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle  if (class_loader == nullptr) {
41627e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle    LOG(WARNING) << "Opening an oat file without a class loader. "
41727e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle                 << "Are you using the deprecated DexFile APIs?";
41827e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle    context = nullptr;
41927e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle  } else {
42027e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle    context = ClassLoaderContext::CreateContextForClassLoader(class_loader, dex_elements);
42127e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle  }
42227e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle
423f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  OatFileAssistant oat_file_assistant(dex_location,
424f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier                                      kRuntimeISA,
425fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier                                      !runtime->IsAotCompiler());
426f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
427f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  // Lock the target oat location to avoid races generating and loading the
428f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  // oat file.
429f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  std::string error_msg;
430f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  if (!oat_file_assistant.Lock(/*out*/&error_msg)) {
431f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    // Don't worry too much if this fails. If it does fail, it's unlikely we
432f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    // can generate an oat file anyway.
433f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    VLOG(class_linker) << "OatFileAssistant::Lock: " << error_msg;
434f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
435f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
436f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  const OatFile* source_oat_file = nullptr;
437f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
43868bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray  // No point in trying to make up-to-date if we can only use system oat files.
43968bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray  if (!only_use_system_oat_files_ && !oat_file_assistant.IsUpToDate()) {
44001be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler    // Update the oat file on disk if we can, based on the --compiler-filter
44101be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler    // option derived from the current runtime options.
44201be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler    // This may fail, but that's okay. Best effort is all that matters here.
4438d8d37b80e9f5f2ffd664c54e31337f178c3e62cCalin Juravle    // TODO(calin): b/64530081 b/66984396. Pass a null context to verify and compile
4448d8d37b80e9f5f2ffd664c54e31337f178c3e62cCalin Juravle    // secondary dex files in isolation (and avoid to extract/verify the main apk
4458d8d37b80e9f5f2ffd664c54e31337f178c3e62cCalin Juravle    // if it's in the class path). Note this trades correctness for performance
4468d8d37b80e9f5f2ffd664c54e31337f178c3e62cCalin Juravle    // since the resulting slow down is unacceptable in some cases until b/64530081
4478d8d37b80e9f5f2ffd664c54e31337f178c3e62cCalin Juravle    // is fixed.
44855f39ed15652f2215e0d6369c3628435694e51d4Nicolas Geoffray    // We still pass the class loader context when the classpath string of the runtime
44955f39ed15652f2215e0d6369c3628435694e51d4Nicolas Geoffray    // is not empty, which is the situation when ART is invoked standalone.
45055f39ed15652f2215e0d6369c3628435694e51d4Nicolas Geoffray    ClassLoaderContext* actual_context = Runtime::Current()->GetClassPathString().empty()
45155f39ed15652f2215e0d6369c3628435694e51d4Nicolas Geoffray        ? nullptr
45255f39ed15652f2215e0d6369c3628435694e51d4Nicolas Geoffray        : context.get();
4538d8d37b80e9f5f2ffd664c54e31337f178c3e62cCalin Juravle    switch (oat_file_assistant.MakeUpToDate(/*profile_changed*/ false,
45455f39ed15652f2215e0d6369c3628435694e51d4Nicolas Geoffray                                            actual_context,
45544e5efa4ae79cf76c65f37fc41c1fa0ed431ec4aCalin Juravle                                            /*out*/ &error_msg)) {
45601be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler      case OatFileAssistant::kUpdateFailed:
45701be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler        LOG(WARNING) << error_msg;
45801be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler        break;
45901be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler
46001be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler      case OatFileAssistant::kUpdateNotAttempted:
46101be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler        // Avoid spamming the logs if we decided not to attempt making the oat
46201be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler        // file up to date.
46301be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler        VLOG(oat) << error_msg;
46401be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler        break;
46501be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler
46601be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler      case OatFileAssistant::kUpdateSucceeded:
46701be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler        // Nothing to do.
46801be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler        break;
46901be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler    }
470f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
471f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
472f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  // Get the oat file on disk.
473f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  std::unique_ptr<const OatFile> oat_file(oat_file_assistant.GetBestOatFile().release());
474fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier
47568bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray  if (oat_file != nullptr && only_use_system_oat_files_ && !OatFileIsOnSystem(oat_file)) {
47668bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray    // If the oat file is not on /system, don't use it.
47768bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray  } else  if ((class_loader != nullptr || dex_elements != nullptr) && oat_file != nullptr) {
47868bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray    // Prevent oat files from being loaded if no class_loader or dex_elements are provided.
47968bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray    // This can happen when the deprecated DexFile.<init>(String) is called directly, and it
48068bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray    // could load oat files without checking the classpath, which would be incorrect.
481f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    // Take the file only if it has no collisions, or we must take it because of preopting.
482f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao    bool accept_oat_file =
48327e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle        !HasCollisions(oat_file.get(), context.get(), /*out*/ &error_msg);
484f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    if (!accept_oat_file) {
485f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      // Failed the collision check. Print warning.
486f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      if (Runtime::Current()->IsDexFileFallbackEnabled()) {
4875c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath        if (!oat_file_assistant.HasOriginalDexFiles()) {
4885c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath          // We need to fallback but don't have original dex files. We have to
4895c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath          // fallback to opening the existing oat file. This is potentially
4905c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath          // unsafe so we warn about it.
4915c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath          accept_oat_file = true;
4925c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath
4935c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath          LOG(WARNING) << "Dex location " << dex_location << " does not seem to include dex file. "
4945c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath                       << "Allow oat file use. This is potentially dangerous.";
4955c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath        } else {
4965c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath          // We have to fallback and found original dex files - extract them from an APK.
4975c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath          // Also warn about this operation because it's potentially wasteful.
4985c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath          LOG(WARNING) << "Found duplicate classes, falling back to extracting from APK : "
4995c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath                       << dex_location;
5005c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath          LOG(WARNING) << "NOTE: This wastes RAM and hurts startup performance.";
5015c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath        }
502f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      } else {
5035c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath        // TODO: We should remove this. The fact that we're here implies -Xno-dex-file-fallback
5045c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath        // was set, which means that we should never fallback. If we don't have original dex
5055c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath        // files, we should just fail resolution as the flag intended.
5065c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath        if (!oat_file_assistant.HasOriginalDexFiles()) {
5075c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath          accept_oat_file = true;
5085c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath        }
5095c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath
510f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier        LOG(WARNING) << "Found duplicate classes, dex-file-fallback disabled, will be failing to "
511f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier                        " load classes for " << dex_location;
512f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      }
513f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
5145c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath      LOG(WARNING) << error_msg;
515f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    }
516f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
517f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    if (accept_oat_file) {
518f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      VLOG(class_linker) << "Registering " << oat_file->GetLocation();
519f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      source_oat_file = RegisterOatFile(std::move(oat_file));
520e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier      *out_oat_file = source_oat_file;
521f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    }
522f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
523f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
524f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  std::vector<std::unique_ptr<const DexFile>> dex_files;
525f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
526f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  // Load the dex files from the oat file.
527f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  if (source_oat_file != nullptr) {
528fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier    bool added_image_space = false;
529fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier    if (source_oat_file->IsExecutable()) {
530a463b6a920a2a0bf14f9cca20a561b412b9349d1Andreas Gampe      std::unique_ptr<gc::space::ImageSpace> image_space =
531a463b6a920a2a0bf14f9cca20a561b412b9349d1Andreas Gampe          kEnableAppImage ? oat_file_assistant.OpenImageSpace(source_oat_file) : nullptr;
532fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier      if (image_space != nullptr) {
533fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier        ScopedObjectAccess soa(self);
534fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier        StackHandleScope<1> hs(self);
535fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier        Handle<mirror::ClassLoader> h_loader(
5360795f23920ee9aabf28e45c63cd592dcccf00216Mathieu Chartier            hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader)));
537fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier        // Can not load app image without class loader.
538fa4333dcb481e564f54726b4e6f8153612df835eAndreas Gampe        if (h_loader != nullptr) {
539fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier          std::string temp_error_msg;
540fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier          // Add image space has a race condition since other threads could be reading from the
541fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier          // spaces array.
542a9d82fe8bc6960b565245b920e99107a824ca515Mathieu Chartier          {
543a9d82fe8bc6960b565245b920e99107a824ca515Mathieu Chartier            ScopedThreadSuspension sts(self, kSuspended);
54461d2b2d353ba4ab952247d2bff2c905598118bb4Mathieu Chartier            gc::ScopedGCCriticalSection gcs(self,
54561d2b2d353ba4ab952247d2bff2c905598118bb4Mathieu Chartier                                            gc::kGcCauseAddRemoveAppImageSpace,
54661d2b2d353ba4ab952247d2bff2c905598118bb4Mathieu Chartier                                            gc::kCollectorTypeAddRemoveAppImageSpace);
547a9d82fe8bc6960b565245b920e99107a824ca515Mathieu Chartier            ScopedSuspendAll ssa("Add image space");
548a9d82fe8bc6960b565245b920e99107a824ca515Mathieu Chartier            runtime->GetHeap()->AddSpace(image_space.get());
549a9d82fe8bc6960b565245b920e99107a824ca515Mathieu Chartier          }
55032ce2adefb8a3d0eda59a29f5e87c1eb43eef796Mathieu Chartier          {
55132ce2adefb8a3d0eda59a29f5e87c1eb43eef796Mathieu Chartier            ScopedTrace trace2(StringPrintf("Adding image space for location %s", dex_location));
55232ce2adefb8a3d0eda59a29f5e87c1eb43eef796Mathieu Chartier            added_image_space = runtime->GetClassLinker()->AddImageSpace(image_space.get(),
55332ce2adefb8a3d0eda59a29f5e87c1eb43eef796Mathieu Chartier                                                                         h_loader,
55432ce2adefb8a3d0eda59a29f5e87c1eb43eef796Mathieu Chartier                                                                         dex_elements,
55532ce2adefb8a3d0eda59a29f5e87c1eb43eef796Mathieu Chartier                                                                         dex_location,
55632ce2adefb8a3d0eda59a29f5e87c1eb43eef796Mathieu Chartier                                                                         /*out*/&dex_files,
55732ce2adefb8a3d0eda59a29f5e87c1eb43eef796Mathieu Chartier                                                                         /*out*/&temp_error_msg);
55832ce2adefb8a3d0eda59a29f5e87c1eb43eef796Mathieu Chartier          }
559a6e81ed4c185b7362cd5199ebe5507d00883a9b0Mathieu Chartier          if (added_image_space) {
560bd064ea2269b23360e32e8139c22d5993ddc385bMathieu Chartier            // Successfully added image space to heap, release the map so that it does not get
561bd064ea2269b23360e32e8139c22d5993ddc385bMathieu Chartier            // freed.
562bd064ea2269b23360e32e8139c22d5993ddc385bMathieu Chartier            image_space.release();
5630bb4031b976aeb2bad88db81115fea46b19878f0Bharadwaj Kalandhabhatta
5640bb4031b976aeb2bad88db81115fea46b19878f0Bharadwaj Kalandhabhatta            // Register for tracking.
5650bb4031b976aeb2bad88db81115fea46b19878f0Bharadwaj Kalandhabhatta            for (const auto& dex_file : dex_files) {
5660bb4031b976aeb2bad88db81115fea46b19878f0Bharadwaj Kalandhabhatta              dex::tracking::RegisterDexFile(dex_file.get());
5670bb4031b976aeb2bad88db81115fea46b19878f0Bharadwaj Kalandhabhatta            }
568bd064ea2269b23360e32e8139c22d5993ddc385bMathieu Chartier          } else {
569fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier            LOG(INFO) << "Failed to add image file " << temp_error_msg;
570fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier            dex_files.clear();
571a9d82fe8bc6960b565245b920e99107a824ca515Mathieu Chartier            {
572a9d82fe8bc6960b565245b920e99107a824ca515Mathieu Chartier              ScopedThreadSuspension sts(self, kSuspended);
57361d2b2d353ba4ab952247d2bff2c905598118bb4Mathieu Chartier              gc::ScopedGCCriticalSection gcs(self,
57461d2b2d353ba4ab952247d2bff2c905598118bb4Mathieu Chartier                                              gc::kGcCauseAddRemoveAppImageSpace,
57561d2b2d353ba4ab952247d2bff2c905598118bb4Mathieu Chartier                                              gc::kCollectorTypeAddRemoveAppImageSpace);
576a9d82fe8bc6960b565245b920e99107a824ca515Mathieu Chartier              ScopedSuspendAll ssa("Remove image space");
577a9d82fe8bc6960b565245b920e99107a824ca515Mathieu Chartier              runtime->GetHeap()->RemoveSpace(image_space.get());
578a9d82fe8bc6960b565245b920e99107a824ca515Mathieu Chartier            }
579fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier            // Non-fatal, don't update error_msg.
580fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier          }
581fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier        }
582fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier      }
583fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier    }
584fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier    if (!added_image_space) {
585fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier      DCHECK(dex_files.empty());
586fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier      dex_files = oat_file_assistant.LoadDexFiles(*source_oat_file, dex_location);
5870bb4031b976aeb2bad88db81115fea46b19878f0Bharadwaj Kalandhabhatta
5880bb4031b976aeb2bad88db81115fea46b19878f0Bharadwaj Kalandhabhatta      // Register for tracking.
5890bb4031b976aeb2bad88db81115fea46b19878f0Bharadwaj Kalandhabhatta      for (const auto& dex_file : dex_files) {
5900bb4031b976aeb2bad88db81115fea46b19878f0Bharadwaj Kalandhabhatta        dex::tracking::RegisterDexFile(dex_file.get());
5910bb4031b976aeb2bad88db81115fea46b19878f0Bharadwaj Kalandhabhatta      }
592fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier    }
593f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    if (dex_files.empty()) {
594f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      error_msgs->push_back("Failed to open dex files from " + source_oat_file->GetLocation());
595be8303d3e7afb5a9088b69858cbcf40cf8737d1aMathieu Chartier    } else {
596120aa286ab6adf3e76a31bc61fb4e583e5158d71Mathieu Chartier      // Opened dex files from an oat file, madvise them to their loaded state.
597120aa286ab6adf3e76a31bc61fb4e583e5158d71Mathieu Chartier       for (const std::unique_ptr<const DexFile>& dex_file : dex_files) {
598120aa286ab6adf3e76a31bc61fb4e583e5158d71Mathieu Chartier         OatDexFile::MadviseDexFile(*dex_file, MadviseState::kMadviseStateAtLoad);
599120aa286ab6adf3e76a31bc61fb4e583e5158d71Mathieu Chartier       }
600f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    }
601f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
602f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
603f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  // Fall back to running out of the original dex file if we couldn't load any
604f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  // dex_files from the oat file.
605f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  if (dex_files.empty()) {
606f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    if (oat_file_assistant.HasOriginalDexFiles()) {
607f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      if (Runtime::Current()->IsDexFileFallbackEnabled()) {
60837d6a3bbf5826abb73f872ed0bc4c6ae9553e274Aart Bik        static constexpr bool kVerifyChecksum = true;
609095c6c96236476b605b3ac672f6d2b8c151e9479Nicolas Geoffray        if (!DexFileLoader::Open(dex_location,
610095c6c96236476b605b3ac672f6d2b8c151e9479Nicolas Geoffray                                 dex_location,
611e875f4c210ce0598e47b6d66ec12cd7c60208844Nicolas Geoffray                                 Runtime::Current()->IsVerificationEnabled(),
612095c6c96236476b605b3ac672f6d2b8c151e9479Nicolas Geoffray                                 kVerifyChecksum,
613095c6c96236476b605b3ac672f6d2b8c151e9479Nicolas Geoffray                                 /*out*/ &error_msg,
614095c6c96236476b605b3ac672f6d2b8c151e9479Nicolas Geoffray                                 &dex_files)) {
615f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier          LOG(WARNING) << error_msg;
6163045b66613404fa973aafc8c2aae3728e9c12d9aAlex Light          error_msgs->push_back("Failed to open dex files from " + std::string(dex_location)
6173045b66613404fa973aafc8c2aae3728e9c12d9aAlex Light                                + " because: " + error_msg);
618f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier        }
619f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      } else {
620f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier        error_msgs->push_back("Fallback mode disabled, skipping dex files.");
621f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      }
622f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    } else {
623f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      error_msgs->push_back("No original dex files found for dex location "
624f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier          + std::string(dex_location));
625f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    }
626f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
627c90bc92bc577020ff4d3caced4cee1cdf41fa5deCalin Juravle
628f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  return dex_files;
629f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier}
630f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
63168bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffrayvoid OatFileManager::SetOnlyUseSystemOatFiles() {
63268bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray  ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
63368bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray  CHECK_EQ(oat_files_.size(), GetBootOatFiles().size());
63468bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray  only_use_system_oat_files_ = true;
63568bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray}
63668bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray
63704680f3d7b021a0afb460266d442f564186a3b6fNicolas Geoffrayvoid OatFileManager::DumpForSigQuit(std::ostream& os) {
63804680f3d7b021a0afb460266d442f564186a3b6fNicolas Geoffray  ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
63904680f3d7b021a0afb460266d442f564186a3b6fNicolas Geoffray  std::vector<const OatFile*> boot_oat_files = GetBootOatFiles();
64004680f3d7b021a0afb460266d442f564186a3b6fNicolas Geoffray  for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) {
64104680f3d7b021a0afb460266d442f564186a3b6fNicolas Geoffray    if (ContainsElement(boot_oat_files, oat_file.get())) {
64204680f3d7b021a0afb460266d442f564186a3b6fNicolas Geoffray      continue;
64304680f3d7b021a0afb460266d442f564186a3b6fNicolas Geoffray    }
64429d38e77c553c6cf71fc4dafe2d22b4e3f814872Andreas Gampe    os << oat_file->GetLocation() << ": " << oat_file->GetCompilerFilter() << "\n";
64504680f3d7b021a0afb460266d442f564186a3b6fNicolas Geoffray  }
64604680f3d7b021a0afb460266d442f564186a3b6fNicolas Geoffray}
64704680f3d7b021a0afb460266d442f564186a3b6fNicolas Geoffray
648f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier}  // namespace art
649