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"
34013fd8073f3ece22b0bba1853d3f3430c8a9e4bdDavid Sehr#include "dex/art_dex_file_loader.h"
359e734c7ab4599d7747a05db0dc73c7b668cb6683David Sehr#include "dex/dex_file-inl.h"
369e734c7ab4599d7747a05db0dc73c7b668cb6683David Sehr#include "dex/dex_file_loader.h"
379e734c7ab4599d7747a05db0dc73c7b668cb6683David Sehr#include "dex/dex_file_tracking_registrar.h"
3861d2b2d353ba4ab952247d2bff2c905598118bb4Mathieu Chartier#include "gc/scoped_gc_critical_section.h"
39f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier#include "gc/space/image_space.h"
40fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier#include "handle_scope-inl.h"
4108883debd927d18c9ecf66683a2a11aa98165656Andreas Gampe#include "jni_internal.h"
42fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier#include "mirror/class_loader.h"
4390b936ddda63139ff46a6755c3b83ad6e4ab4ac5Andreas Gampe#include "mirror/object-inl.h"
442ce6fc80711af9afa3ecda8fdff6f4ed80aa3022Alex Light#include "oat_file.h"
45f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier#include "oat_file_assistant.h"
463398c7874e002beaa6c2b2fadf183e7d1ddad23aMathieu Chartier#include "obj_ptr-inl.h"
470795f23920ee9aabf28e45c63cd592dcccf00216Mathieu Chartier#include "scoped_thread_state_change-inl.h"
48b486a98aadc95d80548953410cf23edba62259faAndreas Gampe#include "thread-current-inl.h"
49a9d82fe8bc6960b565245b920e99107a824ca515Mathieu Chartier#include "thread_list.h"
5090b936ddda63139ff46a6755c3b83ad6e4ab4ac5Andreas Gampe#include "well_known_classes.h"
51f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
52f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartiernamespace art {
53f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
5446ee31b67d7ee1bd085fbc240502053caa3cf8faAndreas Gampeusing android::base::StringPrintf;
5546ee31b67d7ee1bd085fbc240502053caa3cf8faAndreas Gampe
56120aa286ab6adf3e76a31bc61fb4e583e5158d71Mathieu Chartier// If true, we attempt to load the application image if it exists.
57fbc31087932a65e036a153afab3049dc5298656aMathieu Chartierstatic constexpr bool kEnableAppImage = true;
58fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier
59f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartierconst OatFile* OatFileManager::RegisterOatFile(std::unique_ptr<const OatFile> oat_file) {
60e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  WriterMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
6129742602424fe8a30884a00947120bd3407f4da6Nicolas Geoffray  CHECK(!only_use_system_oat_files_ ||
6229742602424fe8a30884a00947120bd3407f4da6Nicolas Geoffray        LocationIsOnSystem(oat_file->GetLocation().c_str()) ||
6329742602424fe8a30884a00947120bd3407f4da6Nicolas Geoffray        !oat_file->IsExecutable())
6468bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray      << "Registering a non /system oat file: " << oat_file->GetLocation();
65f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  DCHECK(oat_file != nullptr);
66f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  if (kIsDebugBuild) {
67e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier    CHECK(oat_files_.find(oat_file) == oat_files_.end());
68f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    for (const std::unique_ptr<const OatFile>& existing : oat_files_) {
69f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      CHECK_NE(oat_file.get(), existing.get()) << oat_file->GetLocation();
70f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      // Check that we don't have an oat file with the same address. Copies of the same oat file
71f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      // should be loaded at different addresses.
72f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      CHECK_NE(oat_file->Begin(), existing->Begin()) << "Oat file already mapped at that location";
73f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    }
74f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
75f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  have_non_pic_oat_file_ = have_non_pic_oat_file_ || !oat_file->IsPic();
76e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  const OatFile* ret = oat_file.get();
77e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  oat_files_.insert(std::move(oat_file));
78e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  return ret;
79e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier}
80e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier
81e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartiervoid OatFileManager::UnRegisterAndDeleteOatFile(const OatFile* oat_file) {
82e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  WriterMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
83e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  DCHECK(oat_file != nullptr);
84e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  std::unique_ptr<const OatFile> compare(oat_file);
85e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  auto it = oat_files_.find(compare);
86e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  CHECK(it != oat_files_.end());
87e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  oat_files_.erase(it);
88e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  compare.release();
89f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier}
90f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
910b791279f67b0f4b72db991707b16b737871bcdeCalin Juravleconst OatFile* OatFileManager::FindOpenedOatFileFromDexLocation(
920b791279f67b0f4b72db991707b16b737871bcdeCalin Juravle    const std::string& dex_base_location) const {
930b791279f67b0f4b72db991707b16b737871bcdeCalin Juravle  ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
940b791279f67b0f4b72db991707b16b737871bcdeCalin Juravle  for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) {
950b791279f67b0f4b72db991707b16b737871bcdeCalin Juravle    const std::vector<const OatDexFile*>& oat_dex_files = oat_file->GetOatDexFiles();
960b791279f67b0f4b72db991707b16b737871bcdeCalin Juravle    for (const OatDexFile* oat_dex_file : oat_dex_files) {
9779c87da9d4698ec58ece65af0065eebd55a1cfe0Mathieu Chartier      if (DexFileLoader::GetBaseLocation(oat_dex_file->GetDexFileLocation()) == dex_base_location) {
980b791279f67b0f4b72db991707b16b737871bcdeCalin Juravle        return oat_file.get();
990b791279f67b0f4b72db991707b16b737871bcdeCalin Juravle      }
1000b791279f67b0f4b72db991707b16b737871bcdeCalin Juravle    }
1010b791279f67b0f4b72db991707b16b737871bcdeCalin Juravle  }
1020b791279f67b0f4b72db991707b16b737871bcdeCalin Juravle  return nullptr;
1030b791279f67b0f4b72db991707b16b737871bcdeCalin Juravle}
1040b791279f67b0f4b72db991707b16b737871bcdeCalin Juravle
105f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartierconst OatFile* OatFileManager::FindOpenedOatFileFromOatLocation(const std::string& oat_location)
106f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    const {
107f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
108e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  return FindOpenedOatFileFromOatLocationLocked(oat_location);
109e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier}
110e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier
111e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartierconst OatFile* OatFileManager::FindOpenedOatFileFromOatLocationLocked(
112e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier    const std::string& oat_location) const {
113f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) {
114f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    if (oat_file->GetLocation() == oat_location) {
115f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      return oat_file.get();
116f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    }
117f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
118f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  return nullptr;
119f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier}
120f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
121dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Haostd::vector<const OatFile*> OatFileManager::GetBootOatFiles() const {
122dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  std::vector<const OatFile*> oat_files;
123dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  std::vector<gc::space::ImageSpace*> image_spaces =
124dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      Runtime::Current()->GetHeap()->GetBootImageSpaces();
125dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  for (gc::space::ImageSpace* image_space : image_spaces) {
126dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao    oat_files.push_back(image_space->GetOatFile());
127dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  }
128dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  return oat_files;
129f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier}
130f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
131f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartierconst OatFile* OatFileManager::GetPrimaryOatFile() const {
132f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
133dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  std::vector<const OatFile*> boot_oat_files = GetBootOatFiles();
134dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  if (!boot_oat_files.empty()) {
135f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) {
136dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      if (std::find(boot_oat_files.begin(), boot_oat_files.end(), oat_file.get()) ==
137dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao          boot_oat_files.end()) {
138f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier        return oat_file.get();
139f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      }
140f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    }
141f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
142f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  return nullptr;
143f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier}
144f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
145b0b68cfc0d2444e2333adcc5a6bc6f670b89fe83Vladimir MarkoOatFileManager::OatFileManager()
146b0b68cfc0d2444e2333adcc5a6bc6f670b89fe83Vladimir Marko    : have_non_pic_oat_file_(false), only_use_system_oat_files_(false) {}
147b0b68cfc0d2444e2333adcc5a6bc6f670b89fe83Vladimir Marko
148f9c6fc610b27887f832e453a0da1789187293408Mathieu ChartierOatFileManager::~OatFileManager() {
149e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  // Explicitly clear oat_files_ since the OatFile destructor calls back into OatFileManager for
150e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  // UnRegisterOatFileLocation.
151e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier  oat_files_.clear();
152f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier}
153f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
154dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Haostd::vector<const OatFile*> OatFileManager::RegisterImageOatFiles(
155dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao    std::vector<gc::space::ImageSpace*> spaces) {
156dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  std::vector<const OatFile*> oat_files;
157dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  for (gc::space::ImageSpace* space : spaces) {
158dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao    oat_files.push_back(RegisterOatFile(space->ReleaseOatFile()));
159dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  }
160dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  return oat_files;
161f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier}
162f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
1638ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Haoclass TypeIndexInfo {
1648ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao public:
1658ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  explicit TypeIndexInfo(const DexFile* dex_file)
1668ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao      : type_indexes_(GenerateTypeIndexes(dex_file)),
1678ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao        iter_(type_indexes_.Indexes().begin()),
1688ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao        end_(type_indexes_.Indexes().end()) { }
1698ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao
1708ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  BitVector& GetTypeIndexes() {
1718ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    return type_indexes_;
1728ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  }
1738ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  BitVector::IndexIterator& GetIterator() {
1748ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    return iter_;
1758ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  }
1768ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  BitVector::IndexIterator& GetIteratorEnd() {
1778ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    return end_;
1788ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  }
1798ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  void AdvanceIterator() {
1808ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    iter_++;
1818ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  }
1828ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao
1838ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao private:
1848ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  static BitVector GenerateTypeIndexes(const DexFile* dex_file) {
1858ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    BitVector type_indexes(/*start_bits*/0, /*expandable*/true, Allocator::GetMallocAllocator());
1868ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    for (uint16_t i = 0; i < dex_file->NumClassDefs(); ++i) {
1878ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao      const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
1888ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao      uint16_t type_idx = class_def.class_idx_.index_;
1898ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao      type_indexes.SetBit(type_idx);
1908ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    }
1918ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    return type_indexes;
1928ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  }
1938ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao
1948ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  // BitVector with bits set for the type indexes of all classes in the input dex file.
1958ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  BitVector type_indexes_;
1968ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  BitVector::IndexIterator iter_;
1978ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  BitVector::IndexIterator end_;
1988ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao};
1998ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao
200f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartierclass DexFileAndClassPair : ValueObject {
201f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier public:
2028ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  DexFileAndClassPair(const DexFile* dex_file, TypeIndexInfo* type_info, bool from_loaded_oat)
2038ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao     : type_info_(type_info),
204f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier       dex_file_(dex_file),
2058ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao       cached_descriptor_(dex_file_->StringByTypeIdx(dex::TypeIndex(*type_info->GetIterator()))),
2068ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao       from_loaded_oat_(from_loaded_oat) {
2078ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    type_info_->AdvanceIterator();
2088ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  }
209f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
21080b37b7e679a530738c9bcbd39873b6dacf177e5Mathieu Chartier  DexFileAndClassPair(const DexFileAndClassPair& rhs) = default;
211f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
21280b37b7e679a530738c9bcbd39873b6dacf177e5Mathieu Chartier  DexFileAndClassPair& operator=(const DexFileAndClassPair& rhs) = default;
213f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
214f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  const char* GetCachedDescriptor() const {
215f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    return cached_descriptor_;
216f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
217f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
218f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  bool operator<(const DexFileAndClassPair& rhs) const {
219f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    const int cmp = strcmp(cached_descriptor_, rhs.cached_descriptor_);
220f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    if (cmp != 0) {
221f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      // Note that the order must be reversed. We want to iterate over the classes in dex files.
222f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      // They are sorted lexicographically. Thus, the priority-queue must be a min-queue.
223f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      return cmp > 0;
224f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    }
225f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    return dex_file_ < rhs.dex_file_;
226f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
227f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
228f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  bool DexFileHasMoreClasses() const {
2298ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    return type_info_->GetIterator() != type_info_->GetIteratorEnd();
230f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
231f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
232f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  void Next() {
2338ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    cached_descriptor_ = dex_file_->StringByTypeIdx(dex::TypeIndex(*type_info_->GetIterator()));
2348ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    type_info_->AdvanceIterator();
235f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
236f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
237f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  bool FromLoadedOat() const {
238f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    return from_loaded_oat_;
239f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
240f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
241f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  const DexFile* GetDexFile() const {
242f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao    return dex_file_;
243f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao  }
244f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao
245f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier private:
2468ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  TypeIndexInfo* type_info_;
247f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao  const DexFile* dex_file_;
2488ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  const char* cached_descriptor_;
249f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  bool from_loaded_oat_;  // We only need to compare mismatches between what we load now
250f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier                          // and what was loaded before. Any old duplicates must have been
251f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier                          // OK, and any new "internal" duplicates are as well (they must
252f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier                          // be from multidex, which resolves correctly).
253f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier};
254f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
2558ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Haostatic void AddDexFilesFromOat(
2568ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    const OatFile* oat_file,
2578ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    /*out*/std::vector<const DexFile*>* dex_files,
2588ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    std::vector<std::unique_ptr<const DexFile>>* opened_dex_files) {
259f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  for (const OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
260f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    std::string error;
261f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error);
262f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    if (dex_file == nullptr) {
263f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      LOG(WARNING) << "Could not create dex file from oat file: " << error;
264f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    } else if (dex_file->NumClassDefs() > 0U) {
2658ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao      dex_files->push_back(dex_file.get());
26614d7b3e37b6129d845d6c8da8ee8d612937c63d4Mathieu Chartier      opened_dex_files->push_back(std::move(dex_file));
267f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    }
268f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
269f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier}
270f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
2718ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Haostatic void AddNext(/*inout*/DexFileAndClassPair& original,
2728ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao                    /*inout*/std::priority_queue<DexFileAndClassPair>& heap) {
2738ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  if (original.DexFileHasMoreClasses()) {
2748ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    original.Next();
2758ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    heap.push(std::move(original));
276f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao  }
277f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao}
278f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao
2798ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Haostatic bool CollisionCheck(std::vector<const DexFile*>& dex_files_loaded,
2808ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao                           std::vector<const DexFile*>& dex_files_unloaded,
2818ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao                           std::string* error_msg /*out*/) {
2828ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  // Generate type index information for each dex file.
2838ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  std::vector<TypeIndexInfo> loaded_types;
2848ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  for (const DexFile* dex_file : dex_files_loaded) {
2858ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    loaded_types.push_back(TypeIndexInfo(dex_file));
2868ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  }
2878ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  std::vector<TypeIndexInfo> unloaded_types;
2888ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  for (const DexFile* dex_file : dex_files_unloaded) {
2898ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    unloaded_types.push_back(TypeIndexInfo(dex_file));
2908ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  }
2918ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao
2928ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  // Populate the queue of dex file and class pairs with the loaded and unloaded dex files.
2938ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  std::priority_queue<DexFileAndClassPair> queue;
2948ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  for (size_t i = 0; i < dex_files_loaded.size(); ++i) {
2958ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    if (loaded_types[i].GetIterator() != loaded_types[i].GetIteratorEnd()) {
2968ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao      queue.emplace(dex_files_loaded[i], &loaded_types[i], /*from_loaded_oat*/true);
2978ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    }
2988ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  }
2998ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  for (size_t i = 0; i < dex_files_unloaded.size(); ++i) {
3008ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    if (unloaded_types[i].GetIterator() != unloaded_types[i].GetIteratorEnd()) {
3018ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao      queue.emplace(dex_files_unloaded[i], &unloaded_types[i], /*from_loaded_oat*/false);
3028ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    }
3038ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  }
3048ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao
3058ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  // Now drain the queue.
3060471ece3d0166097619da19c9f43522200bfda78Jeff Hao  bool has_duplicates = false;
3070471ece3d0166097619da19c9f43522200bfda78Jeff Hao  error_msg->clear();
3088ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  while (!queue.empty()) {
3098ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    // Modifying the top element is only safe if we pop right after.
3108ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    DexFileAndClassPair compare_pop(queue.top());
3118ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    queue.pop();
3128ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao
3138ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    // Compare against the following elements.
3148ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    while (!queue.empty()) {
3158ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao      DexFileAndClassPair top(queue.top());
3168ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao      if (strcmp(compare_pop.GetCachedDescriptor(), top.GetCachedDescriptor()) == 0) {
3178ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao        // Same descriptor. Check whether it's crossing old-oat-files to new-oat-files.
3188ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao        if (compare_pop.FromLoadedOat() != top.FromLoadedOat()) {
3190471ece3d0166097619da19c9f43522200bfda78Jeff Hao          error_msg->append(
3200471ece3d0166097619da19c9f43522200bfda78Jeff Hao              StringPrintf("Found duplicated class when checking oat files: '%s' in %s and %s\n",
3218ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao                           compare_pop.GetCachedDescriptor(),
3228ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao                           compare_pop.GetDexFile()->GetLocation().c_str(),
3230471ece3d0166097619da19c9f43522200bfda78Jeff Hao                           top.GetDexFile()->GetLocation().c_str()));
3240471ece3d0166097619da19c9f43522200bfda78Jeff Hao          if (!VLOG_IS_ON(oat)) {
3250471ece3d0166097619da19c9f43522200bfda78Jeff Hao            return true;
3260471ece3d0166097619da19c9f43522200bfda78Jeff Hao          }
3270471ece3d0166097619da19c9f43522200bfda78Jeff Hao          has_duplicates = true;
3288ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao        }
3298ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao        queue.pop();
3308ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao        AddNext(top, queue);
3318ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao      } else {
3328ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao        // Something else. Done here.
333f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao        break;
334f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao      }
335f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao    }
3368ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao    AddNext(compare_pop, queue);
337f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao  }
3388ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao
3390471ece3d0166097619da19c9f43522200bfda78Jeff Hao  return has_duplicates;
340f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao}
341f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao
342f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier// Check for class-def collisions in dex files.
343f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier//
34427e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle// This first walks the class loader chain present in the given context, getting all the dex files
34527e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle// from the class loader.
346f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao//
34727e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle// If the context is null (which means the initial class loader was null or unsupported)
34827e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle// this returns false. b/37777332.
34927e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle//
35027e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle// This first checks whether all class loaders in the context have the same type and
35127e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle// classpath. If so, we exit early. Otherwise, we do the collision check.
352f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao//
353f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao// The collision check works by maintaining a heap with one class from each dex file, sorted by the
354f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao// class descriptor. Then a dex-file/class pair is continually removed from the heap and compared
355f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier// against the following top element. If the descriptor is the same, it is now checked whether
356f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier// the two elements agree on whether their dex file was from an already-loaded oat-file or the
357f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier// new oat file. Any disagreement indicates a collision.
358f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartierbool OatFileManager::HasCollisions(const OatFile* oat_file,
35927e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle                                   const ClassLoaderContext* context,
360f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier                                   std::string* error_msg /*out*/) const {
361f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  DCHECK(oat_file != nullptr);
362f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  DCHECK(error_msg != nullptr);
363f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao
3643f9186446faa01636475596335240f39472ba9f4Calin Juravle  // The context might be null if there are unrecognized class loaders in the chain or they
3653f9186446faa01636475596335240f39472ba9f4Calin Juravle  // don't meet sensible sanity conditions. In this case we assume that the app knows what it's
3663f9186446faa01636475596335240f39472ba9f4Calin Juravle  // doing and accept the oat file.
3673f9186446faa01636475596335240f39472ba9f4Calin Juravle  // Note that this has correctness implications as we cannot guarantee that the class resolution
3683f9186446faa01636475596335240f39472ba9f4Calin Juravle  // used during compilation is OK (b/37777332).
3693f9186446faa01636475596335240f39472ba9f4Calin Juravle  if (context == nullptr) {
3703f9186446faa01636475596335240f39472ba9f4Calin Juravle      LOG(WARNING) << "Skipping duplicate class check due to unsupported classloader";
3715c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath      return false;
372f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
373f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
3743f9186446faa01636475596335240f39472ba9f4Calin Juravle  // If the pat file loading context matches the context used during compilation then we accept
3753f9186446faa01636475596335240f39472ba9f4Calin Juravle  // the oat file without addition checks
37644e5efa4ae79cf76c65f37fc41c1fa0ed431ec4aCalin Juravle  if (context->VerifyClassLoaderContextMatch(oat_file->GetClassLoaderContext())) {
377f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    return false;
378f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
379f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
3803f9186446faa01636475596335240f39472ba9f4Calin Juravle  // The class loader context does not match. Perform a full duplicate classes check.
3813f9186446faa01636475596335240f39472ba9f4Calin Juravle
3823f9186446faa01636475596335240f39472ba9f4Calin Juravle  std::vector<const DexFile*> dex_files_loaded = context->FlattenOpenedDexFiles();
3833f9186446faa01636475596335240f39472ba9f4Calin Juravle
3845c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath  // Vector that holds the newly opened dex files live, this is done to prevent leaks.
3855c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath  std::vector<std::unique_ptr<const DexFile>> opened_dex_files;
3865c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath
3871fdbe1b034d97b852551d24e70245df64f03ebebAndreas Gampe  ScopedTrace st("Collision check");
388f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  // Add dex files from the oat file to check.
3898ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  std::vector<const DexFile*> dex_files_unloaded;
3908ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  AddDexFilesFromOat(oat_file, &dex_files_unloaded, &opened_dex_files);
3918ec0a20abae9a1e9a708ee02dd2639351933c028Jeff Hao  return CollisionCheck(dex_files_loaded, dex_files_unloaded, error_msg);
392f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier}
393f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
394f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartierstd::vector<std::unique_ptr<const DexFile>> OatFileManager::OpenDexFilesFromOat(
395f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    const char* dex_location,
396fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier    jobject class_loader,
397fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier    jobjectArray dex_elements,
398e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier    const OatFile** out_oat_file,
399f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    std::vector<std::string>* error_msgs) {
40032ce2adefb8a3d0eda59a29f5e87c1eb43eef796Mathieu Chartier  ScopedTrace trace(__FUNCTION__);
401f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  CHECK(dex_location != nullptr);
402f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  CHECK(error_msgs != nullptr);
403f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
404f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  // Verify we aren't holding the mutator lock, which could starve GC if we
405f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  // have to generate or relocate an oat file.
406fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier  Thread* const self = Thread::Current();
407fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier  Locks::mutator_lock_->AssertNotHeld(self);
408fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier  Runtime* const runtime = Runtime::Current();
409b077e15d2d11b7c81aacbcd4a46c2b1e9c9ba20dCalin Juravle
41027e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle  std::unique_ptr<ClassLoaderContext> context;
41127e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle  // If the class_loader is null there's not much we can do. This happens if a dex files is loaded
41227e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle  // directly with DexFile APIs instead of using class loaders.
41327e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle  if (class_loader == nullptr) {
41427e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle    LOG(WARNING) << "Opening an oat file without a class loader. "
41527e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle                 << "Are you using the deprecated DexFile APIs?";
41627e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle    context = nullptr;
41727e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle  } else {
41827e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle    context = ClassLoaderContext::CreateContextForClassLoader(class_loader, dex_elements);
41927e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle  }
42027e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle
421f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  OatFileAssistant oat_file_assistant(dex_location,
422f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier                                      kRuntimeISA,
42329742602424fe8a30884a00947120bd3407f4da6Nicolas Geoffray                                      !runtime->IsAotCompiler(),
42429742602424fe8a30884a00947120bd3407f4da6Nicolas Geoffray                                      only_use_system_oat_files_);
425f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
426f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  // Lock the target oat location to avoid races generating and loading the
427f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  // oat file.
428f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  std::string error_msg;
429f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  if (!oat_file_assistant.Lock(/*out*/&error_msg)) {
430f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    // Don't worry too much if this fails. If it does fail, it's unlikely we
431f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    // can generate an oat file anyway.
432f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    VLOG(class_linker) << "OatFileAssistant::Lock: " << error_msg;
433f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
434f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
435f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  const OatFile* source_oat_file = nullptr;
436f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
43729742602424fe8a30884a00947120bd3407f4da6Nicolas Geoffray  if (!oat_file_assistant.IsUpToDate()) {
43801be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler    // Update the oat file on disk if we can, based on the --compiler-filter
43901be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler    // option derived from the current runtime options.
44001be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler    // This may fail, but that's okay. Best effort is all that matters here.
4418d8d37b80e9f5f2ffd664c54e31337f178c3e62cCalin Juravle    // TODO(calin): b/64530081 b/66984396. Pass a null context to verify and compile
4428d8d37b80e9f5f2ffd664c54e31337f178c3e62cCalin Juravle    // secondary dex files in isolation (and avoid to extract/verify the main apk
4438d8d37b80e9f5f2ffd664c54e31337f178c3e62cCalin Juravle    // if it's in the class path). Note this trades correctness for performance
4448d8d37b80e9f5f2ffd664c54e31337f178c3e62cCalin Juravle    // since the resulting slow down is unacceptable in some cases until b/64530081
4458d8d37b80e9f5f2ffd664c54e31337f178c3e62cCalin Juravle    // is fixed.
44655f39ed15652f2215e0d6369c3628435694e51d4Nicolas Geoffray    // We still pass the class loader context when the classpath string of the runtime
44755f39ed15652f2215e0d6369c3628435694e51d4Nicolas Geoffray    // is not empty, which is the situation when ART is invoked standalone.
44855f39ed15652f2215e0d6369c3628435694e51d4Nicolas Geoffray    ClassLoaderContext* actual_context = Runtime::Current()->GetClassPathString().empty()
44955f39ed15652f2215e0d6369c3628435694e51d4Nicolas Geoffray        ? nullptr
45055f39ed15652f2215e0d6369c3628435694e51d4Nicolas Geoffray        : context.get();
4518d8d37b80e9f5f2ffd664c54e31337f178c3e62cCalin Juravle    switch (oat_file_assistant.MakeUpToDate(/*profile_changed*/ false,
45255f39ed15652f2215e0d6369c3628435694e51d4Nicolas Geoffray                                            actual_context,
45344e5efa4ae79cf76c65f37fc41c1fa0ed431ec4aCalin Juravle                                            /*out*/ &error_msg)) {
45401be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler      case OatFileAssistant::kUpdateFailed:
45501be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler        LOG(WARNING) << error_msg;
45601be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler        break;
45701be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler
45801be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler      case OatFileAssistant::kUpdateNotAttempted:
45901be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler        // Avoid spamming the logs if we decided not to attempt making the oat
46001be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler        // file up to date.
46101be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler        VLOG(oat) << error_msg;
46201be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler        break;
46301be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler
46401be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler      case OatFileAssistant::kUpdateSucceeded:
46501be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler        // Nothing to do.
46601be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler        break;
46701be68100e96a44b4ff8cb4cb3c9baa346ff048dRichard Uhler    }
468f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
469f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
470f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  // Get the oat file on disk.
471f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  std::unique_ptr<const OatFile> oat_file(oat_file_assistant.GetBestOatFile().release());
472ed010d4eebdf12f327856debc2c73694777cccdaAndreas Gampe  VLOG(oat) << "OatFileAssistant(" << dex_location << ").GetBestOatFile()="
473ed010d4eebdf12f327856debc2c73694777cccdaAndreas Gampe            << reinterpret_cast<uintptr_t>(oat_file.get())
474ed010d4eebdf12f327856debc2c73694777cccdaAndreas Gampe            << " (executable=" << (oat_file != nullptr ? oat_file->IsExecutable() : false) << ")";
475fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier
47629742602424fe8a30884a00947120bd3407f4da6Nicolas Geoffray  if ((class_loader != nullptr || dex_elements != nullptr) && oat_file != nullptr) {
47768bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray    // Prevent oat files from being loaded if no class_loader or dex_elements are provided.
47868bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray    // This can happen when the deprecated DexFile.<init>(String) is called directly, and it
47968bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray    // could load oat files without checking the classpath, which would be incorrect.
480f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    // Take the file only if it has no collisions, or we must take it because of preopting.
481f0192c86a58b2f43378c9a2113007538dd38ddbfJeff Hao    bool accept_oat_file =
48227e0d1f3da2e0b7046dacb40794739d739e454a3Calin Juravle        !HasCollisions(oat_file.get(), context.get(), /*out*/ &error_msg);
483f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    if (!accept_oat_file) {
484f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      // Failed the collision check. Print warning.
485f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      if (Runtime::Current()->IsDexFileFallbackEnabled()) {
4865c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath        if (!oat_file_assistant.HasOriginalDexFiles()) {
4875c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath          // We need to fallback but don't have original dex files. We have to
4885c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath          // fallback to opening the existing oat file. This is potentially
4895c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath          // unsafe so we warn about it.
4905c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath          accept_oat_file = true;
4915c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath
4925c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath          LOG(WARNING) << "Dex location " << dex_location << " does not seem to include dex file. "
4935c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath                       << "Allow oat file use. This is potentially dangerous.";
4945c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath        } else {
4955c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath          // We have to fallback and found original dex files - extract them from an APK.
4965c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath          // Also warn about this operation because it's potentially wasteful.
4975c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath          LOG(WARNING) << "Found duplicate classes, falling back to extracting from APK : "
4985c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath                       << dex_location;
4995c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath          LOG(WARNING) << "NOTE: This wastes RAM and hurts startup performance.";
5005c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath        }
501f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      } else {
5025c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath        // TODO: We should remove this. The fact that we're here implies -Xno-dex-file-fallback
5035c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath        // was set, which means that we should never fallback. If we don't have original dex
5045c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath        // files, we should just fail resolution as the flag intended.
5055c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath        if (!oat_file_assistant.HasOriginalDexFiles()) {
5065c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath          accept_oat_file = true;
5075c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath        }
5085c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath
509f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier        LOG(WARNING) << "Found duplicate classes, dex-file-fallback disabled, will be failing to "
510f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier                        " load classes for " << dex_location;
511f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      }
512f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
5135c525746c63f48f11aee787103d023511c54e4e3Narayan Kamath      LOG(WARNING) << error_msg;
514f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    }
515f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
516f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    if (accept_oat_file) {
517f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      VLOG(class_linker) << "Registering " << oat_file->GetLocation();
518f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      source_oat_file = RegisterOatFile(std::move(oat_file));
519e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier      *out_oat_file = source_oat_file;
520f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    }
521f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
522f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
523f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  std::vector<std::unique_ptr<const DexFile>> dex_files;
524f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
525f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  // Load the dex files from the oat file.
526f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  if (source_oat_file != nullptr) {
527fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier    bool added_image_space = false;
528fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier    if (source_oat_file->IsExecutable()) {
5292ce6fc80711af9afa3ecda8fdff6f4ed80aa3022Alex Light      // We need to throw away the image space if we are debuggable but the oat-file source of the
5302ce6fc80711af9afa3ecda8fdff6f4ed80aa3022Alex Light      // image is not otherwise we might get classes with inlined methods or other such things.
5312ce6fc80711af9afa3ecda8fdff6f4ed80aa3022Alex Light      std::unique_ptr<gc::space::ImageSpace> image_space;
5322ce6fc80711af9afa3ecda8fdff6f4ed80aa3022Alex Light      if (kEnableAppImage && (!runtime->IsJavaDebuggable() || source_oat_file->IsDebuggable())) {
5332ce6fc80711af9afa3ecda8fdff6f4ed80aa3022Alex Light        image_space = oat_file_assistant.OpenImageSpace(source_oat_file);
5342ce6fc80711af9afa3ecda8fdff6f4ed80aa3022Alex Light      } else {
5352ce6fc80711af9afa3ecda8fdff6f4ed80aa3022Alex Light        image_space = nullptr;
5362ce6fc80711af9afa3ecda8fdff6f4ed80aa3022Alex Light      }
537fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier      if (image_space != nullptr) {
538fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier        ScopedObjectAccess soa(self);
539fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier        StackHandleScope<1> hs(self);
540fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier        Handle<mirror::ClassLoader> h_loader(
5410795f23920ee9aabf28e45c63cd592dcccf00216Mathieu Chartier            hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader)));
542fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier        // Can not load app image without class loader.
543fa4333dcb481e564f54726b4e6f8153612df835eAndreas Gampe        if (h_loader != nullptr) {
544fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier          std::string temp_error_msg;
545fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier          // Add image space has a race condition since other threads could be reading from the
546fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier          // spaces array.
547a9d82fe8bc6960b565245b920e99107a824ca515Mathieu Chartier          {
548a9d82fe8bc6960b565245b920e99107a824ca515Mathieu Chartier            ScopedThreadSuspension sts(self, kSuspended);
54961d2b2d353ba4ab952247d2bff2c905598118bb4Mathieu Chartier            gc::ScopedGCCriticalSection gcs(self,
55061d2b2d353ba4ab952247d2bff2c905598118bb4Mathieu Chartier                                            gc::kGcCauseAddRemoveAppImageSpace,
55161d2b2d353ba4ab952247d2bff2c905598118bb4Mathieu Chartier                                            gc::kCollectorTypeAddRemoveAppImageSpace);
552a9d82fe8bc6960b565245b920e99107a824ca515Mathieu Chartier            ScopedSuspendAll ssa("Add image space");
553a9d82fe8bc6960b565245b920e99107a824ca515Mathieu Chartier            runtime->GetHeap()->AddSpace(image_space.get());
554a9d82fe8bc6960b565245b920e99107a824ca515Mathieu Chartier          }
55532ce2adefb8a3d0eda59a29f5e87c1eb43eef796Mathieu Chartier          {
55632ce2adefb8a3d0eda59a29f5e87c1eb43eef796Mathieu Chartier            ScopedTrace trace2(StringPrintf("Adding image space for location %s", dex_location));
55732ce2adefb8a3d0eda59a29f5e87c1eb43eef796Mathieu Chartier            added_image_space = runtime->GetClassLinker()->AddImageSpace(image_space.get(),
55832ce2adefb8a3d0eda59a29f5e87c1eb43eef796Mathieu Chartier                                                                         h_loader,
55932ce2adefb8a3d0eda59a29f5e87c1eb43eef796Mathieu Chartier                                                                         dex_elements,
56032ce2adefb8a3d0eda59a29f5e87c1eb43eef796Mathieu Chartier                                                                         dex_location,
56132ce2adefb8a3d0eda59a29f5e87c1eb43eef796Mathieu Chartier                                                                         /*out*/&dex_files,
56232ce2adefb8a3d0eda59a29f5e87c1eb43eef796Mathieu Chartier                                                                         /*out*/&temp_error_msg);
56332ce2adefb8a3d0eda59a29f5e87c1eb43eef796Mathieu Chartier          }
564a6e81ed4c185b7362cd5199ebe5507d00883a9b0Mathieu Chartier          if (added_image_space) {
565bd064ea2269b23360e32e8139c22d5993ddc385bMathieu Chartier            // Successfully added image space to heap, release the map so that it does not get
566bd064ea2269b23360e32e8139c22d5993ddc385bMathieu Chartier            // freed.
567bd064ea2269b23360e32e8139c22d5993ddc385bMathieu Chartier            image_space.release();
5680bb4031b976aeb2bad88db81115fea46b19878f0Bharadwaj Kalandhabhatta
5690bb4031b976aeb2bad88db81115fea46b19878f0Bharadwaj Kalandhabhatta            // Register for tracking.
5700bb4031b976aeb2bad88db81115fea46b19878f0Bharadwaj Kalandhabhatta            for (const auto& dex_file : dex_files) {
5710bb4031b976aeb2bad88db81115fea46b19878f0Bharadwaj Kalandhabhatta              dex::tracking::RegisterDexFile(dex_file.get());
5720bb4031b976aeb2bad88db81115fea46b19878f0Bharadwaj Kalandhabhatta            }
573bd064ea2269b23360e32e8139c22d5993ddc385bMathieu Chartier          } else {
574fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier            LOG(INFO) << "Failed to add image file " << temp_error_msg;
575fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier            dex_files.clear();
576a9d82fe8bc6960b565245b920e99107a824ca515Mathieu Chartier            {
577a9d82fe8bc6960b565245b920e99107a824ca515Mathieu Chartier              ScopedThreadSuspension sts(self, kSuspended);
57861d2b2d353ba4ab952247d2bff2c905598118bb4Mathieu Chartier              gc::ScopedGCCriticalSection gcs(self,
57961d2b2d353ba4ab952247d2bff2c905598118bb4Mathieu Chartier                                              gc::kGcCauseAddRemoveAppImageSpace,
58061d2b2d353ba4ab952247d2bff2c905598118bb4Mathieu Chartier                                              gc::kCollectorTypeAddRemoveAppImageSpace);
581a9d82fe8bc6960b565245b920e99107a824ca515Mathieu Chartier              ScopedSuspendAll ssa("Remove image space");
582a9d82fe8bc6960b565245b920e99107a824ca515Mathieu Chartier              runtime->GetHeap()->RemoveSpace(image_space.get());
583a9d82fe8bc6960b565245b920e99107a824ca515Mathieu Chartier            }
584fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier            // Non-fatal, don't update error_msg.
585fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier          }
586fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier        }
587fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier      }
588fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier    }
589fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier    if (!added_image_space) {
590fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier      DCHECK(dex_files.empty());
591fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier      dex_files = oat_file_assistant.LoadDexFiles(*source_oat_file, dex_location);
5920bb4031b976aeb2bad88db81115fea46b19878f0Bharadwaj Kalandhabhatta
5930bb4031b976aeb2bad88db81115fea46b19878f0Bharadwaj Kalandhabhatta      // Register for tracking.
5940bb4031b976aeb2bad88db81115fea46b19878f0Bharadwaj Kalandhabhatta      for (const auto& dex_file : dex_files) {
5950bb4031b976aeb2bad88db81115fea46b19878f0Bharadwaj Kalandhabhatta        dex::tracking::RegisterDexFile(dex_file.get());
5960bb4031b976aeb2bad88db81115fea46b19878f0Bharadwaj Kalandhabhatta      }
597fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier    }
598f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    if (dex_files.empty()) {
599f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      error_msgs->push_back("Failed to open dex files from " + source_oat_file->GetLocation());
600be8303d3e7afb5a9088b69858cbcf40cf8737d1aMathieu Chartier    } else {
601120aa286ab6adf3e76a31bc61fb4e583e5158d71Mathieu Chartier      // Opened dex files from an oat file, madvise them to their loaded state.
602120aa286ab6adf3e76a31bc61fb4e583e5158d71Mathieu Chartier       for (const std::unique_ptr<const DexFile>& dex_file : dex_files) {
603120aa286ab6adf3e76a31bc61fb4e583e5158d71Mathieu Chartier         OatDexFile::MadviseDexFile(*dex_file, MadviseState::kMadviseStateAtLoad);
604120aa286ab6adf3e76a31bc61fb4e583e5158d71Mathieu Chartier       }
605f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    }
606f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
607f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
608f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  // Fall back to running out of the original dex file if we couldn't load any
609f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  // dex_files from the oat file.
610f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  if (dex_files.empty()) {
611f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    if (oat_file_assistant.HasOriginalDexFiles()) {
612f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      if (Runtime::Current()->IsDexFileFallbackEnabled()) {
61337d6a3bbf5826abb73f872ed0bc4c6ae9553e274Aart Bik        static constexpr bool kVerifyChecksum = true;
614013fd8073f3ece22b0bba1853d3f3430c8a9e4bdDavid Sehr        const ArtDexFileLoader dex_file_loader;
615013fd8073f3ece22b0bba1853d3f3430c8a9e4bdDavid Sehr        if (!dex_file_loader.Open(dex_location,
616013fd8073f3ece22b0bba1853d3f3430c8a9e4bdDavid Sehr                                  dex_location,
617013fd8073f3ece22b0bba1853d3f3430c8a9e4bdDavid Sehr                                  Runtime::Current()->IsVerificationEnabled(),
618013fd8073f3ece22b0bba1853d3f3430c8a9e4bdDavid Sehr                                  kVerifyChecksum,
619013fd8073f3ece22b0bba1853d3f3430c8a9e4bdDavid Sehr                                  /*out*/ &error_msg,
620013fd8073f3ece22b0bba1853d3f3430c8a9e4bdDavid Sehr                                  &dex_files)) {
621f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier          LOG(WARNING) << error_msg;
6223045b66613404fa973aafc8c2aae3728e9c12d9aAlex Light          error_msgs->push_back("Failed to open dex files from " + std::string(dex_location)
6233045b66613404fa973aafc8c2aae3728e9c12d9aAlex Light                                + " because: " + error_msg);
624f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier        }
625f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      } else {
626f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier        error_msgs->push_back("Fallback mode disabled, skipping dex files.");
627f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      }
628f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    } else {
629f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      error_msgs->push_back("No original dex files found for dex location "
630f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier          + std::string(dex_location));
631f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    }
632f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  }
633c90bc92bc577020ff4d3caced4cee1cdf41fa5deCalin Juravle
634f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  return dex_files;
635f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier}
636f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier
63768bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffrayvoid OatFileManager::SetOnlyUseSystemOatFiles() {
63868bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray  ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
63968bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray  CHECK_EQ(oat_files_.size(), GetBootOatFiles().size());
64068bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray  only_use_system_oat_files_ = true;
64168bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray}
64268bf3903250ff0bea11c4705ba331818c5bf5792Nicolas Geoffray
64304680f3d7b021a0afb460266d442f564186a3b6fNicolas Geoffrayvoid OatFileManager::DumpForSigQuit(std::ostream& os) {
64404680f3d7b021a0afb460266d442f564186a3b6fNicolas Geoffray  ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
64504680f3d7b021a0afb460266d442f564186a3b6fNicolas Geoffray  std::vector<const OatFile*> boot_oat_files = GetBootOatFiles();
64604680f3d7b021a0afb460266d442f564186a3b6fNicolas Geoffray  for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) {
64704680f3d7b021a0afb460266d442f564186a3b6fNicolas Geoffray    if (ContainsElement(boot_oat_files, oat_file.get())) {
64804680f3d7b021a0afb460266d442f564186a3b6fNicolas Geoffray      continue;
64904680f3d7b021a0afb460266d442f564186a3b6fNicolas Geoffray    }
65029d38e77c553c6cf71fc4dafe2d22b4e3f814872Andreas Gampe    os << oat_file->GetLocation() << ": " << oat_file->GetCompilerFilter() << "\n";
65104680f3d7b021a0afb460266d442f564186a3b6fNicolas Geoffray  }
65204680f3d7b021a0afb460266d442f564186a3b6fNicolas Geoffray}
65304680f3d7b021a0afb460266d442f564186a3b6fNicolas Geoffray
654f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier}  // namespace art
655