oat_file_manager.cc revision e875f4c210ce0598e47b6d66ec12cd7c60208844
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"
28f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier#include "base/logging.h"
29f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier#include "base/stl_util.h"
3032ce2adefb8a3d0eda59a29f5e87c1eb43eef796Mathieu Chartier#include "base/systrace.h"
31fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier#include "class_linker.h"
327b0648aa7cb4b7a58e73bf353e031dfe4553d9d7Calin Juravle#include "class_loader_context.h"
3380b37b7e679a530738c9bcbd39873b6dacf177e5Mathieu Chartier#include "dex_file-inl.h"
3479c87da9d4698ec58ece65af0065eebd55a1cfe0Mathieu Chartier#include "dex_file_loader.h"
350bb4031b976aeb2bad88db81115fea46b19878f0Bharadwaj Kalandhabhatta#include "dex_file_tracking_registrar.h"
3661d2b2d353ba4ab952247d2bff2c905598118bb4Mathieu Chartier#include "gc/scoped_gc_critical_section.h"
37f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier#include "gc/space/image_space.h"
38fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier#include "handle_scope-inl.h"
3908883debd927d18c9ecf66683a2a11aa98165656Andreas Gampe#include "jni_internal.h"
40fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier#include "mirror/class_loader.h"
4190b936ddda63139ff46a6755c3b83ad6e4ab4ac5Andreas Gampe#include "mirror/object-inl.h"
42f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier#include "oat_file_assistant.h"
433398c7874e002beaa6c2b2fadf183e7d1ddad23aMathieu Chartier#include "obj_ptr-inl.h"
440795f23920ee9aabf28e45c63cd592dcccf00216Mathieu Chartier#include "scoped_thread_state_change-inl.h"
45b486a98aadc95d80548953410cf23edba62259faAndreas Gampe#include "thread-current-inl.h"
46a9d82fe8bc6960b565245b920e99107a824ca515Mathieu Chartier#include "thread_list.h"
4790b936ddda63139ff46a6755c3b83ad6e4ab4ac5Andreas Gampe#include "well_known_classes.h"
48f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
49f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartiernamespace art {
50f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
5146ee31b67d7ee1bd085fbc240502053caa3cf8faAndreas Gampeusing android::base::StringPrintf;
5246ee31b67d7ee1bd085fbc240502053caa3cf8faAndreas Gampe
53120aa286ab6adf3e76a31bc61fb4e583e5158d71Mathieu Chartier// If true, we attempt to load the application image if it exists.
54fbc31087932a65e036a153afab3049dc5298656aMathieu Chartierstatic constexpr bool kEnableAppImage = true;
55fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier
5668bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffraystatic bool OatFileIsOnSystem(const std::unique_ptr<const OatFile>& oat_file) {
5768bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray  UniqueCPtr<const char[]> path(realpath(oat_file->GetLocation().c_str(), nullptr));
583f093891e55383055eecc940b724ee1f24f50123Andreas Gampe  return path != nullptr && android::base::StartsWith(oat_file->GetLocation(),
593f093891e55383055eecc940b724ee1f24f50123Andreas Gampe                                                      GetAndroidRoot().c_str());
6068bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray}
6168bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray
62f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartierconst OatFile* OatFileManager::RegisterOatFile(std::unique_ptr<const OatFile> oat_file) {
63e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  WriterMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
6468bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray  CHECK(!only_use_system_oat_files_ || OatFileIsOnSystem(oat_file))
6568bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray      << "Registering a non /system oat file: " << oat_file->GetLocation();
66f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  DCHECK(oat_file != nullptr);
67f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  if (kIsDebugBuild) {
68e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier    CHECK(oat_files_.find(oat_file) == oat_files_.end());
69f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    for (const std::unique_ptr<const OatFile>& existing : oat_files_) {
70f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      CHECK_NE(oat_file.get(), existing.get()) << oat_file->GetLocation();
71f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      // Check that we don't have an oat file with the same address. Copies of the same oat file
72f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      // should be loaded at different addresses.
73f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      CHECK_NE(oat_file->Begin(), existing->Begin()) << "Oat file already mapped at that location";
74f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    }
75f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
76f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  have_non_pic_oat_file_ = have_non_pic_oat_file_ || !oat_file->IsPic();
77e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  const OatFile* ret = oat_file.get();
78e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  oat_files_.insert(std::move(oat_file));
79e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  return ret;
80e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier}
81e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier
82e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartiervoid OatFileManager::UnRegisterAndDeleteOatFile(const OatFile* oat_file) {
83e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  WriterMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
84e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  DCHECK(oat_file != nullptr);
85e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  std::unique_ptr<const OatFile> compare(oat_file);
86e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  auto it = oat_files_.find(compare);
87e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  CHECK(it != oat_files_.end());
88e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  oat_files_.erase(it);
89e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  compare.release();
90f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier}
91f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
920b791279f67b0f4b72db991707b16b737871bcdeCalin Juravleconst OatFile* OatFileManager::FindOpenedOatFileFromDexLocation(
930b791279f67b0f4b72db991707b16b737871bcdeCalin Juravle    const std::string& dex_base_location) const {
940b791279f67b0f4b72db991707b16b737871bcdeCalin Juravle  ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
950b791279f67b0f4b72db991707b16b737871bcdeCalin Juravle  for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) {
960b791279f67b0f4b72db991707b16b737871bcdeCalin Juravle    const std::vector<const OatDexFile*>& oat_dex_files = oat_file->GetOatDexFiles();
970b791279f67b0f4b72db991707b16b737871bcdeCalin Juravle    for (const OatDexFile* oat_dex_file : oat_dex_files) {
9879c87da9d4698ec58ece65af0065eebd55a1cfe0Mathieu Chartier      if (DexFileLoader::GetBaseLocation(oat_dex_file->GetDexFileLocation()) == dex_base_location) {
990b791279f67b0f4b72db991707b16b737871bcdeCalin Juravle        return oat_file.get();
1000b791279f67b0f4b72db991707b16b737871bcdeCalin Juravle      }
1010b791279f67b0f4b72db991707b16b737871bcdeCalin Juravle    }
1020b791279f67b0f4b72db991707b16b737871bcdeCalin Juravle  }
1030b791279f67b0f4b72db991707b16b737871bcdeCalin Juravle  return nullptr;
1040b791279f67b0f4b72db991707b16b737871bcdeCalin Juravle}
1050b791279f67b0f4b72db991707b16b737871bcdeCalin Juravle
106f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartierconst OatFile* OatFileManager::FindOpenedOatFileFromOatLocation(const std::string& oat_location)
107f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    const {
108f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
109e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  return FindOpenedOatFileFromOatLocationLocked(oat_location);
110e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier}
111e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier
112e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartierconst OatFile* OatFileManager::FindOpenedOatFileFromOatLocationLocked(
113e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier    const std::string& oat_location) const {
114f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) {
115f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    if (oat_file->GetLocation() == oat_location) {
116f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      return oat_file.get();
117f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    }
118f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
119f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  return nullptr;
120f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier}
121f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
122dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Haostd::vector<const OatFile*> OatFileManager::GetBootOatFiles() const {
123dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  std::vector<const OatFile*> oat_files;
124dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  std::vector<gc::space::ImageSpace*> image_spaces =
125dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      Runtime::Current()->GetHeap()->GetBootImageSpaces();
126dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  for (gc::space::ImageSpace* image_space : image_spaces) {
127dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao    oat_files.push_back(image_space->GetOatFile());
128dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  }
129dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  return oat_files;
130f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier}
131f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
132f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartierconst OatFile* OatFileManager::GetPrimaryOatFile() const {
133f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
134dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  std::vector<const OatFile*> boot_oat_files = GetBootOatFiles();
135dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  if (!boot_oat_files.empty()) {
136f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) {
137dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      if (std::find(boot_oat_files.begin(), boot_oat_files.end(), oat_file.get()) ==
138dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao          boot_oat_files.end()) {
139f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier        return oat_file.get();
140f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      }
141f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    }
142f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
143f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  return nullptr;
144f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier}
145f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
146f9c6fc610b27887f832e453a0da1789187293408Mathieu ChartierOatFileManager::~OatFileManager() {
147e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  // Explicitly clear oat_files_ since the OatFile destructor calls back into OatFileManager for
148e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  // UnRegisterOatFileLocation.
149e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  oat_files_.clear();
150f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier}
151f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
152dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Haostd::vector<const OatFile*> OatFileManager::RegisterImageOatFiles(
153dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao    std::vector<gc::space::ImageSpace*> spaces) {
154dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  std::vector<const OatFile*> oat_files;
155dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  for (gc::space::ImageSpace* space : spaces) {
156dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao    oat_files.push_back(RegisterOatFile(space->ReleaseOatFile()));
157dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  }
158dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  return oat_files;
159f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier}
160f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
1618ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Haoclass TypeIndexInfo {
1628ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao public:
1638ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  explicit TypeIndexInfo(const DexFile* dex_file)
1648ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao      : type_indexes_(GenerateTypeIndexes(dex_file)),
1658ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao        iter_(type_indexes_.Indexes().begin()),
1668ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao        end_(type_indexes_.Indexes().end()) { }
1678ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao
1688ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  BitVector& GetTypeIndexes() {
1698ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    return type_indexes_;
1708ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  }
1718ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  BitVector::IndexIterator& GetIterator() {
1728ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    return iter_;
1738ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  }
1748ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  BitVector::IndexIterator& GetIteratorEnd() {
1758ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    return end_;
1768ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  }
1778ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  void AdvanceIterator() {
1788ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    iter_++;
1798ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  }
1808ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao
1818ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao private:
1828ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  static BitVector GenerateTypeIndexes(const DexFile* dex_file) {
1838ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    BitVector type_indexes(/*start_bits*/0, /*expandable*/true, Allocator::GetMallocAllocator());
1848ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    for (uint16_t i = 0; i < dex_file->NumClassDefs(); ++i) {
1858ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao      const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
1868ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao      uint16_t type_idx = class_def.class_idx_.index_;
1878ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao      type_indexes.SetBit(type_idx);
1888ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    }
1898ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    return type_indexes;
1908ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  }
1918ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao
1928ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  // BitVector with bits set for the type indexes of all classes in the input dex file.
1938ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  BitVector type_indexes_;
1948ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  BitVector::IndexIterator iter_;
1958ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  BitVector::IndexIterator end_;
1968ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao};
1978ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao
198f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartierclass DexFileAndClassPair : ValueObject {
199f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier public:
2008ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  DexFileAndClassPair(const DexFile* dex_file, TypeIndexInfo* type_info, bool from_loaded_oat)
2018ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao     : type_info_(type_info),
202f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier       dex_file_(dex_file),
2038ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao       cached_descriptor_(dex_file_->StringByTypeIdx(dex::TypeIndex(*type_info->GetIterator()))),
2048ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao       from_loaded_oat_(from_loaded_oat) {
2058ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    type_info_->AdvanceIterator();
2068ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  }
207f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
20880b37b7e679a530738c9bcbd39873b6dacf177e5Mathieu Chartier  DexFileAndClassPair(const DexFileAndClassPair& rhs) = default;
209f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
21080b37b7e679a530738c9bcbd39873b6dacf177e5Mathieu Chartier  DexFileAndClassPair& operator=(const DexFileAndClassPair& rhs) = default;
211f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
212f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  const char* GetCachedDescriptor() const {
213f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    return cached_descriptor_;
214f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
215f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
216f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  bool operator<(const DexFileAndClassPair& rhs) const {
217f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    const int cmp = strcmp(cached_descriptor_, rhs.cached_descriptor_);
218f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    if (cmp != 0) {
219f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      // Note that the order must be reversed. We want to iterate over the classes in dex files.
220f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      // They are sorted lexicographically. Thus, the priority-queue must be a min-queue.
221f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      return cmp > 0;
222f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    }
223f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    return dex_file_ < rhs.dex_file_;
224f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
225f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
226f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  bool DexFileHasMoreClasses() const {
2278ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    return type_info_->GetIterator() != type_info_->GetIteratorEnd();
228f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
229f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
230f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  void Next() {
2318ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    cached_descriptor_ = dex_file_->StringByTypeIdx(dex::TypeIndex(*type_info_->GetIterator()));
2328ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    type_info_->AdvanceIterator();
233f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
234f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
235f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  bool FromLoadedOat() const {
236f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    return from_loaded_oat_;
237f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
238f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
239f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  const DexFile* GetDexFile() const {
240f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao    return dex_file_;
241f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao  }
242f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao
243f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier private:
2448ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  TypeIndexInfo* type_info_;
245f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao  const DexFile* dex_file_;
2468ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  const char* cached_descriptor_;
247f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  bool from_loaded_oat_;  // We only need to compare mismatches between what we load now
248f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier                          // and what was loaded before. Any old duplicates must have been
249f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier                          // OK, and any new "internal" duplicates are as well (they must
250f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier                          // be from multidex, which resolves correctly).
251f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier};
252f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
2538ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Haostatic void AddDexFilesFromOat(
2548ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    const OatFile* oat_file,
2558ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    /*out*/std::vector<const DexFile*>* dex_files,
2568ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    std::vector<std::unique_ptr<const DexFile>>* opened_dex_files) {
257f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  for (const OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
258f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    std::string error;
259f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error);
260f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    if (dex_file == nullptr) {
261f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      LOG(WARNING) << "Could not create dex file from oat file: " << error;
262f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    } else if (dex_file->NumClassDefs() > 0U) {
2638ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao      dex_files->push_back(dex_file.get());
26414d7b3e37b6129d845d6c8da8ee8d612937c63d4Mathieu Chartier      opened_dex_files->push_back(std::move(dex_file));
265f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    }
266f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
267f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier}
268f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
2698ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Haostatic void AddNext(/*inout*/DexFileAndClassPair& original,
2708ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao                    /*inout*/std::priority_queue<DexFileAndClassPair>& heap) {
2718ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  if (original.DexFileHasMoreClasses()) {
2728ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    original.Next();
2738ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    heap.push(std::move(original));
274f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao  }
275f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao}
276f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao
2778ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Haostatic bool CollisionCheck(std::vector<const DexFile*>& dex_files_loaded,
2788ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao                           std::vector<const DexFile*>& dex_files_unloaded,
2798ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao                           std::string* error_msg /*out*/) {
2808ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  // Generate type index information for each dex file.
2818ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  std::vector<TypeIndexInfo> loaded_types;
2828ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  for (const DexFile* dex_file : dex_files_loaded) {
2838ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    loaded_types.push_back(TypeIndexInfo(dex_file));
2848ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  }
2858ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  std::vector<TypeIndexInfo> unloaded_types;
2868ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  for (const DexFile* dex_file : dex_files_unloaded) {
2878ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    unloaded_types.push_back(TypeIndexInfo(dex_file));
2888ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  }
2898ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao
2908ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  // Populate the queue of dex file and class pairs with the loaded and unloaded dex files.
2918ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  std::priority_queue<DexFileAndClassPair> queue;
2928ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  for (size_t i = 0; i < dex_files_loaded.size(); ++i) {
2938ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    if (loaded_types[i].GetIterator() != loaded_types[i].GetIteratorEnd()) {
2948ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao      queue.emplace(dex_files_loaded[i], &loaded_types[i], /*from_loaded_oat*/true);
2958ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    }
2968ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  }
2978ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  for (size_t i = 0; i < dex_files_unloaded.size(); ++i) {
2988ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    if (unloaded_types[i].GetIterator() != unloaded_types[i].GetIteratorEnd()) {
2998ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao      queue.emplace(dex_files_unloaded[i], &unloaded_types[i], /*from_loaded_oat*/false);
3008ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    }
3018ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  }
3028ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao
3038ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  // Now drain the queue.
3040471ece3d0166097619da19c9f43522200bfda78Jeff Hao  bool has_duplicates = false;
3050471ece3d0166097619da19c9f43522200bfda78Jeff Hao  error_msg->clear();
3068ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  while (!queue.empty()) {
3078ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    // Modifying the top element is only safe if we pop right after.
3088ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    DexFileAndClassPair compare_pop(queue.top());
3098ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    queue.pop();
3108ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao
3118ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    // Compare against the following elements.
3128ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    while (!queue.empty()) {
3138ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao      DexFileAndClassPair top(queue.top());
3148ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao      if (strcmp(compare_pop.GetCachedDescriptor(), top.GetCachedDescriptor()) == 0) {
3158ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao        // Same descriptor. Check whether it's crossing old-oat-files to new-oat-files.
3168ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao        if (compare_pop.FromLoadedOat() != top.FromLoadedOat()) {
3170471ece3d0166097619da19c9f43522200bfda78Jeff Hao          error_msg->append(
3180471ece3d0166097619da19c9f43522200bfda78Jeff Hao              StringPrintf("Found duplicated class when checking oat files: '%s' in %s and %s\n",
3198ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao                           compare_pop.GetCachedDescriptor(),
3208ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao                           compare_pop.GetDexFile()->GetLocation().c_str(),
3210471ece3d0166097619da19c9f43522200bfda78Jeff Hao                           top.GetDexFile()->GetLocation().c_str()));
3220471ece3d0166097619da19c9f43522200bfda78Jeff Hao          if (!VLOG_IS_ON(oat)) {
3230471ece3d0166097619da19c9f43522200bfda78Jeff Hao            return true;
3240471ece3d0166097619da19c9f43522200bfda78Jeff Hao          }
3250471ece3d0166097619da19c9f43522200bfda78Jeff Hao          has_duplicates = true;
3268ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao        }
3278ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao        queue.pop();
3288ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao        AddNext(top, queue);
3298ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao      } else {
3308ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao        // Something else. Done here.
331f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao        break;
332f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao      }
333f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao    }
3348ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    AddNext(compare_pop, queue);
335f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao  }
3368ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao
3370471ece3d0166097619da19c9f43522200bfda78Jeff Hao  return has_duplicates;
338f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao}
339f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao
340f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier// Check for class-def collisions in dex files.
341f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier//
34227e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle// This first walks the class loader chain present in the given context, getting all the dex files
34327e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle// from the class loader.
344f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao//
34527e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle// If the context is null (which means the initial class loader was null or unsupported)
34627e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle// this returns false. b/37777332.
34727e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle//
34827e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle// This first checks whether all class loaders in the context have the same type and
34927e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle// classpath. If so, we exit early. Otherwise, we do the collision check.
350f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao//
351f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao// The collision check works by maintaining a heap with one class from each dex file, sorted by the
352f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao// class descriptor. Then a dex-file/class pair is continually removed from the heap and compared
353f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier// against the following top element. If the descriptor is the same, it is now checked whether
354f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier// the two elements agree on whether their dex file was from an already-loaded oat-file or the
355f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier// new oat file. Any disagreement indicates a collision.
356f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartierbool OatFileManager::HasCollisions(const OatFile* oat_file,
35727e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle                                   const ClassLoaderContext* context,
358f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier                                   std::string* error_msg /*out*/) const {
359f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  DCHECK(oat_file != nullptr);
360f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  DCHECK(error_msg != nullptr);
361f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao
3623f9186446faa01636475596335240f39472ba9f4Calin Juravle  // The context might be null if there are unrecognized class loaders in the chain or they
3633f9186446faa01636475596335240f39472ba9f4Calin Juravle  // don't meet sensible sanity conditions. In this case we assume that the app knows what it's
3643f9186446faa01636475596335240f39472ba9f4Calin Juravle  // doing and accept the oat file.
3653f9186446faa01636475596335240f39472ba9f4Calin Juravle  // Note that this has correctness implications as we cannot guarantee that the class resolution
3663f9186446faa01636475596335240f39472ba9f4Calin Juravle  // used during compilation is OK (b/37777332).
3673f9186446faa01636475596335240f39472ba9f4Calin Juravle  if (context == nullptr) {
3683f9186446faa01636475596335240f39472ba9f4Calin Juravle      LOG(WARNING) << "Skipping duplicate class check due to unsupported classloader";
3695c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath      return false;
370f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
371f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
3723f9186446faa01636475596335240f39472ba9f4Calin Juravle  // If the pat file loading context matches the context used during compilation then we accept
3733f9186446faa01636475596335240f39472ba9f4Calin Juravle  // the oat file without addition checks
37444e5efa4ae79cf76c65f37fc41c1fa0ed431ec4aCalin Juravle  if (context->VerifyClassLoaderContextMatch(oat_file->GetClassLoaderContext())) {
375f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    return false;
376f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
377f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
3783f9186446faa01636475596335240f39472ba9f4Calin Juravle  // The class loader context does not match. Perform a full duplicate classes check.
3793f9186446faa01636475596335240f39472ba9f4Calin Juravle
3803f9186446faa01636475596335240f39472ba9f4Calin Juravle  std::vector<const DexFile*> dex_files_loaded = context->FlattenOpenedDexFiles();
3813f9186446faa01636475596335240f39472ba9f4Calin Juravle
3825c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath  // Vector that holds the newly opened dex files live, this is done to prevent leaks.
3835c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath  std::vector<std::unique_ptr<const DexFile>> opened_dex_files;
3845c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath
3851fdbe1b034d97b852551d24e70245df64f03ebebAndreas Gampe  ScopedTrace st("Collision check");
386f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  // Add dex files from the oat file to check.
3878ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  std::vector<const DexFile*> dex_files_unloaded;
3888ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  AddDexFilesFromOat(oat_file, &dex_files_unloaded, &opened_dex_files);
3898ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  return CollisionCheck(dex_files_loaded, dex_files_unloaded, error_msg);
390f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier}
391f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
392f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartierstd::vector<std::unique_ptr<const DexFile>> OatFileManager::OpenDexFilesFromOat(
393f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    const char* dex_location,
394fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier    jobject class_loader,
395fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier    jobjectArray dex_elements,
396e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier    const OatFile** out_oat_file,
397f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    std::vector<std::string>* error_msgs) {
39832ce2adefb8a3d0eda59a29f5e87c1eb43eef796Mathieu Chartier  ScopedTrace trace(__FUNCTION__);
399f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  CHECK(dex_location != nullptr);
400f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  CHECK(error_msgs != nullptr);
401f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
402f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  // Verify we aren't holding the mutator lock, which could starve GC if we
403f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  // have to generate or relocate an oat file.
404fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier  Thread* const self = Thread::Current();
405fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier  Locks::mutator_lock_->AssertNotHeld(self);
406fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier  Runtime* const runtime = Runtime::Current();
407b077e15d2d11b7c81aacbcd4a46c2b1e9c9ba20dCalin Juravle
40827e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle  std::unique_ptr<ClassLoaderContext> context;
40927e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle  // If the class_loader is null there's not much we can do. This happens if a dex files is loaded
41027e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle  // directly with DexFile APIs instead of using class loaders.
41127e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle  if (class_loader == nullptr) {
41227e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle    LOG(WARNING) << "Opening an oat file without a class loader. "
41327e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle                 << "Are you using the deprecated DexFile APIs?";
41427e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle    context = nullptr;
41527e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle  } else {
41627e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle    context = ClassLoaderContext::CreateContextForClassLoader(class_loader, dex_elements);
41727e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle  }
41827e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle
419f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  OatFileAssistant oat_file_assistant(dex_location,
420f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier                                      kRuntimeISA,
421fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier                                      !runtime->IsAotCompiler());
422f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
423f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  // Lock the target oat location to avoid races generating and loading the
424f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  // oat file.
425f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  std::string error_msg;
426f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  if (!oat_file_assistant.Lock(/*out*/&error_msg)) {
427f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    // Don't worry too much if this fails. If it does fail, it's unlikely we
428f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    // can generate an oat file anyway.
429f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    VLOG(class_linker) << "OatFileAssistant::Lock: " << error_msg;
430f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
431f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
432f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  const OatFile* source_oat_file = nullptr;
433f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
43468bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray  // No point in trying to make up-to-date if we can only use system oat files.
43568bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray  if (!only_use_system_oat_files_ && !oat_file_assistant.IsUpToDate()) {
43601be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler    // Update the oat file on disk if we can, based on the --compiler-filter
43701be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler    // option derived from the current runtime options.
43801be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler    // This may fail, but that's okay. Best effort is all that matters here.
4398d8d37b80e9f5f2ffd664c54e31337f178c3e62cCalin Juravle    // TODO(calin): b/64530081 b/66984396. Pass a null context to verify and compile
4408d8d37b80e9f5f2ffd664c54e31337f178c3e62cCalin Juravle    // secondary dex files in isolation (and avoid to extract/verify the main apk
4418d8d37b80e9f5f2ffd664c54e31337f178c3e62cCalin Juravle    // if it's in the class path). Note this trades correctness for performance
4428d8d37b80e9f5f2ffd664c54e31337f178c3e62cCalin Juravle    // since the resulting slow down is unacceptable in some cases until b/64530081
4438d8d37b80e9f5f2ffd664c54e31337f178c3e62cCalin Juravle    // is fixed.
4448d8d37b80e9f5f2ffd664c54e31337f178c3e62cCalin Juravle    switch (oat_file_assistant.MakeUpToDate(/*profile_changed*/ false,
4458d8d37b80e9f5f2ffd664c54e31337f178c3e62cCalin Juravle                                            /*class_loader_context*/ nullptr,
44644e5efa4ae79cf76c65f37fc41c1fa0ed431ec4aCalin Juravle                                            /*out*/ &error_msg)) {
44701be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler      case OatFileAssistant::kUpdateFailed:
44801be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler        LOG(WARNING) << error_msg;
44901be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler        break;
45001be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler
45101be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler      case OatFileAssistant::kUpdateNotAttempted:
45201be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler        // Avoid spamming the logs if we decided not to attempt making the oat
45301be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler        // file up to date.
45401be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler        VLOG(oat) << error_msg;
45501be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler        break;
45601be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler
45701be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler      case OatFileAssistant::kUpdateSucceeded:
45801be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler        // Nothing to do.
45901be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler        break;
46001be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler    }
461f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
462f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
463f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  // Get the oat file on disk.
464f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  std::unique_ptr<const OatFile> oat_file(oat_file_assistant.GetBestOatFile().release());
465fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier
46668bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray  if (oat_file != nullptr && only_use_system_oat_files_ && !OatFileIsOnSystem(oat_file)) {
46768bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray    // If the oat file is not on /system, don't use it.
46868bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray  } else  if ((class_loader != nullptr || dex_elements != nullptr) && oat_file != nullptr) {
46968bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray    // Prevent oat files from being loaded if no class_loader or dex_elements are provided.
47068bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray    // This can happen when the deprecated DexFile.<init>(String) is called directly, and it
47168bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray    // could load oat files without checking the classpath, which would be incorrect.
472f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    // Take the file only if it has no collisions, or we must take it because of preopting.
473f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao    bool accept_oat_file =
47427e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle        !HasCollisions(oat_file.get(), context.get(), /*out*/ &error_msg);
475f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    if (!accept_oat_file) {
476f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      // Failed the collision check. Print warning.
477f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      if (Runtime::Current()->IsDexFileFallbackEnabled()) {
4785c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath        if (!oat_file_assistant.HasOriginalDexFiles()) {
4795c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath          // We need to fallback but don't have original dex files. We have to
4805c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath          // fallback to opening the existing oat file. This is potentially
4815c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath          // unsafe so we warn about it.
4825c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath          accept_oat_file = true;
4835c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath
4845c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath          LOG(WARNING) << "Dex location " << dex_location << " does not seem to include dex file. "
4855c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath                       << "Allow oat file use. This is potentially dangerous.";
4865c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath        } else {
4875c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath          // We have to fallback and found original dex files - extract them from an APK.
4885c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath          // Also warn about this operation because it's potentially wasteful.
4895c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath          LOG(WARNING) << "Found duplicate classes, falling back to extracting from APK : "
4905c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath                       << dex_location;
4915c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath          LOG(WARNING) << "NOTE: This wastes RAM and hurts startup performance.";
4925c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath        }
493f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      } else {
4945c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath        // TODO: We should remove this. The fact that we're here implies -Xno-dex-file-fallback
4955c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath        // was set, which means that we should never fallback. If we don't have original dex
4965c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath        // files, we should just fail resolution as the flag intended.
4975c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath        if (!oat_file_assistant.HasOriginalDexFiles()) {
4985c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath          accept_oat_file = true;
4995c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath        }
5005c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath
501f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier        LOG(WARNING) << "Found duplicate classes, dex-file-fallback disabled, will be failing to "
502f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier                        " load classes for " << dex_location;
503f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      }
504f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
5055c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath      LOG(WARNING) << error_msg;
506f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    }
507f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
508f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    if (accept_oat_file) {
509f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      VLOG(class_linker) << "Registering " << oat_file->GetLocation();
510f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      source_oat_file = RegisterOatFile(std::move(oat_file));
511e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier      *out_oat_file = source_oat_file;
512f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    }
513f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
514f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
515f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  std::vector<std::unique_ptr<const DexFile>> dex_files;
516f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
517f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  // Load the dex files from the oat file.
518f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  if (source_oat_file != nullptr) {
519fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier    bool added_image_space = false;
520fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier    if (source_oat_file->IsExecutable()) {
521a463b6a920a2a0bf14f9cca20a561b412b9349d1Andreas Gampe      std::unique_ptr<gc::space::ImageSpace> image_space =
522a463b6a920a2a0bf14f9cca20a561b412b9349d1Andreas Gampe          kEnableAppImage ? oat_file_assistant.OpenImageSpace(source_oat_file) : nullptr;
523fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier      if (image_space != nullptr) {
524fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier        ScopedObjectAccess soa(self);
525fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier        StackHandleScope<1> hs(self);
526fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier        Handle<mirror::ClassLoader> h_loader(
5270795f23920ee9aabf28e45c63cd592dcccf00216Mathieu Chartier            hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader)));
528fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier        // Can not load app image without class loader.
529fa4333dcb481e564f54726b4e6f8153612df835eAndreas Gampe        if (h_loader != nullptr) {
530fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier          std::string temp_error_msg;
531fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier          // Add image space has a race condition since other threads could be reading from the
532fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier          // spaces array.
533a9d82fe8bc6960b565245b920e99107a824ca515Mathieu Chartier          {
534a9d82fe8bc6960b565245b920e99107a824ca515Mathieu Chartier            ScopedThreadSuspension sts(self, kSuspended);
53561d2b2d353ba4ab952247d2bff2c905598118bb4Mathieu Chartier            gc::ScopedGCCriticalSection gcs(self,
53661d2b2d353ba4ab952247d2bff2c905598118bb4Mathieu Chartier                                            gc::kGcCauseAddRemoveAppImageSpace,
53761d2b2d353ba4ab952247d2bff2c905598118bb4Mathieu Chartier                                            gc::kCollectorTypeAddRemoveAppImageSpace);
538a9d82fe8bc6960b565245b920e99107a824ca515Mathieu Chartier            ScopedSuspendAll ssa("Add image space");
539a9d82fe8bc6960b565245b920e99107a824ca515Mathieu Chartier            runtime->GetHeap()->AddSpace(image_space.get());
540a9d82fe8bc6960b565245b920e99107a824ca515Mathieu Chartier          }
54132ce2adefb8a3d0eda59a29f5e87c1eb43eef796Mathieu Chartier          {
54232ce2adefb8a3d0eda59a29f5e87c1eb43eef796Mathieu Chartier            ScopedTrace trace2(StringPrintf("Adding image space for location %s", dex_location));
54332ce2adefb8a3d0eda59a29f5e87c1eb43eef796Mathieu Chartier            added_image_space = runtime->GetClassLinker()->AddImageSpace(image_space.get(),
54432ce2adefb8a3d0eda59a29f5e87c1eb43eef796Mathieu Chartier                                                                         h_loader,
54532ce2adefb8a3d0eda59a29f5e87c1eb43eef796Mathieu Chartier                                                                         dex_elements,
54632ce2adefb8a3d0eda59a29f5e87c1eb43eef796Mathieu Chartier                                                                         dex_location,
54732ce2adefb8a3d0eda59a29f5e87c1eb43eef796Mathieu Chartier                                                                         /*out*/&dex_files,
54832ce2adefb8a3d0eda59a29f5e87c1eb43eef796Mathieu Chartier                                                                         /*out*/&temp_error_msg);
54932ce2adefb8a3d0eda59a29f5e87c1eb43eef796Mathieu Chartier          }
550a6e81ed4c185b7362cd5199ebe5507d00883a9b0Mathieu Chartier          if (added_image_space) {
551bd064ea2269b23360e32e8139c22d5993ddc385bMathieu Chartier            // Successfully added image space to heap, release the map so that it does not get
552bd064ea2269b23360e32e8139c22d5993ddc385bMathieu Chartier            // freed.
553bd064ea2269b23360e32e8139c22d5993ddc385bMathieu Chartier            image_space.release();
5540bb4031b976aeb2bad88db81115fea46b19878f0Bharadwaj Kalandhabhatta
5550bb4031b976aeb2bad88db81115fea46b19878f0Bharadwaj Kalandhabhatta            // Register for tracking.
5560bb4031b976aeb2bad88db81115fea46b19878f0Bharadwaj Kalandhabhatta            for (const auto& dex_file : dex_files) {
5570bb4031b976aeb2bad88db81115fea46b19878f0Bharadwaj Kalandhabhatta              dex::tracking::RegisterDexFile(dex_file.get());
5580bb4031b976aeb2bad88db81115fea46b19878f0Bharadwaj Kalandhabhatta            }
559bd064ea2269b23360e32e8139c22d5993ddc385bMathieu Chartier          } else {
560fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier            LOG(INFO) << "Failed to add image file " << temp_error_msg;
561fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier            dex_files.clear();
562a9d82fe8bc6960b565245b920e99107a824ca515Mathieu Chartier            {
563a9d82fe8bc6960b565245b920e99107a824ca515Mathieu Chartier              ScopedThreadSuspension sts(self, kSuspended);
56461d2b2d353ba4ab952247d2bff2c905598118bb4Mathieu Chartier              gc::ScopedGCCriticalSection gcs(self,
56561d2b2d353ba4ab952247d2bff2c905598118bb4Mathieu Chartier                                              gc::kGcCauseAddRemoveAppImageSpace,
56661d2b2d353ba4ab952247d2bff2c905598118bb4Mathieu Chartier                                              gc::kCollectorTypeAddRemoveAppImageSpace);
567a9d82fe8bc6960b565245b920e99107a824ca515Mathieu Chartier              ScopedSuspendAll ssa("Remove image space");
568a9d82fe8bc6960b565245b920e99107a824ca515Mathieu Chartier              runtime->GetHeap()->RemoveSpace(image_space.get());
569a9d82fe8bc6960b565245b920e99107a824ca515Mathieu Chartier            }
570fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier            // Non-fatal, don't update error_msg.
571fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier          }
572fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier        }
573fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier      }
574fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier    }
575fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier    if (!added_image_space) {
576fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier      DCHECK(dex_files.empty());
577fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier      dex_files = oat_file_assistant.LoadDexFiles(*source_oat_file, dex_location);
5780bb4031b976aeb2bad88db81115fea46b19878f0Bharadwaj Kalandhabhatta
5790bb4031b976aeb2bad88db81115fea46b19878f0Bharadwaj Kalandhabhatta      // Register for tracking.
5800bb4031b976aeb2bad88db81115fea46b19878f0Bharadwaj Kalandhabhatta      for (const auto& dex_file : dex_files) {
5810bb4031b976aeb2bad88db81115fea46b19878f0Bharadwaj Kalandhabhatta        dex::tracking::RegisterDexFile(dex_file.get());
5820bb4031b976aeb2bad88db81115fea46b19878f0Bharadwaj Kalandhabhatta      }
583fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier    }
584f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    if (dex_files.empty()) {
585f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      error_msgs->push_back("Failed to open dex files from " + source_oat_file->GetLocation());
586be8303d3e7afb5a9088b69858cbcf40cf8737d1aMathieu Chartier    } else {
587120aa286ab6adf3e76a31bc61fb4e583e5158d71Mathieu Chartier      // Opened dex files from an oat file, madvise them to their loaded state.
588120aa286ab6adf3e76a31bc61fb4e583e5158d71Mathieu Chartier       for (const std::unique_ptr<const DexFile>& dex_file : dex_files) {
589120aa286ab6adf3e76a31bc61fb4e583e5158d71Mathieu Chartier         OatDexFile::MadviseDexFile(*dex_file, MadviseState::kMadviseStateAtLoad);
590120aa286ab6adf3e76a31bc61fb4e583e5158d71Mathieu Chartier       }
591f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    }
592f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
593f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
594f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  // Fall back to running out of the original dex file if we couldn't load any
595f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  // dex_files from the oat file.
596f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  if (dex_files.empty()) {
597f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    if (oat_file_assistant.HasOriginalDexFiles()) {
598f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      if (Runtime::Current()->IsDexFileFallbackEnabled()) {
59937d6a3bbf5826abb73f872ed0bc4c6ae9553e274Aart Bik        static constexpr bool kVerifyChecksum = true;
600095c6c96236476b605b3ac672f6d2b8c151e9479Nicolas Geoffray        if (!DexFileLoader::Open(dex_location,
601095c6c96236476b605b3ac672f6d2b8c151e9479Nicolas Geoffray                                 dex_location,
602e875f4c210ce0598e47b6d66ec12cd7c60208844Nicolas Geoffray                                 Runtime::Current()->IsVerificationEnabled(),
603095c6c96236476b605b3ac672f6d2b8c151e9479Nicolas Geoffray                                 kVerifyChecksum,
604095c6c96236476b605b3ac672f6d2b8c151e9479Nicolas Geoffray                                 /*out*/ &error_msg,
605095c6c96236476b605b3ac672f6d2b8c151e9479Nicolas Geoffray                                 &dex_files)) {
606f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier          LOG(WARNING) << error_msg;
6073045b66613404fa973aafc8c2aae3728e9c12d9aAlex Light          error_msgs->push_back("Failed to open dex files from " + std::string(dex_location)
6083045b66613404fa973aafc8c2aae3728e9c12d9aAlex Light                                + " because: " + error_msg);
609f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier        }
610f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      } else {
611f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier        error_msgs->push_back("Fallback mode disabled, skipping dex files.");
612f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      }
613f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    } else {
614f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      error_msgs->push_back("No original dex files found for dex location "
615f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier          + std::string(dex_location));
616f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    }
617f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
618c90bc92bc577020ff4d3caced4cee1cdf41fa5deCalin Juravle
619f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  return dex_files;
620f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier}
621f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
62268bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffrayvoid OatFileManager::SetOnlyUseSystemOatFiles() {
62368bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray  ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
62468bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray  CHECK_EQ(oat_files_.size(), GetBootOatFiles().size());
62568bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray  only_use_system_oat_files_ = true;
62668bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray}
62768bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray
62804680f3d7b021a0afb460266d442f564186a3b6fNicolas Geoffrayvoid OatFileManager::DumpForSigQuit(std::ostream& os) {
62904680f3d7b021a0afb460266d442f564186a3b6fNicolas Geoffray  ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
63004680f3d7b021a0afb460266d442f564186a3b6fNicolas Geoffray  std::vector<const OatFile*> boot_oat_files = GetBootOatFiles();
63104680f3d7b021a0afb460266d442f564186a3b6fNicolas Geoffray  for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) {
63204680f3d7b021a0afb460266d442f564186a3b6fNicolas Geoffray    if (ContainsElement(boot_oat_files, oat_file.get())) {
63304680f3d7b021a0afb460266d442f564186a3b6fNicolas Geoffray      continue;
63404680f3d7b021a0afb460266d442f564186a3b6fNicolas Geoffray    }
63529d38e77c553c6cf71fc4dafe2d22b4e3f814872Andreas Gampe    os << oat_file->GetLocation() << ": " << oat_file->GetCompilerFilter() << "\n";
63604680f3d7b021a0afb460266d442f564186a3b6fNicolas Geoffray  }
63704680f3d7b021a0afb460266d442f564186a3b6fNicolas Geoffray}
63804680f3d7b021a0afb460266d442f564186a3b6fNicolas Geoffray
639f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier}  // namespace art
640