Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "oat_file_manager.h" |
| 18 | |
| 19 | #include <memory> |
| 20 | #include <queue> |
| 21 | #include <vector> |
| 22 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 23 | #include "android-base/stringprintf.h" |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 24 | #include "android-base/strings.h" |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 25 | |
Andreas Gampe | 90b936d | 2017-01-31 08:58:55 -0800 | [diff] [blame] | 26 | #include "art_field-inl.h" |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 27 | #include "base/bit_vector-inl.h" |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 28 | #include "base/file_utils.h" |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 29 | #include "base/logging.h" // For VLOG. |
Andreas Gampe | 7fbc4a5 | 2018-11-28 08:26:47 -0800 | [diff] [blame] | 30 | #include "base/mutex-inl.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 31 | #include "base/stl_util.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 32 | #include "base/systrace.h" |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 33 | #include "class_linker.h" |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 34 | #include "class_loader_context.h" |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 35 | #include "dex/art_dex_file_loader.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 36 | #include "dex/dex_file-inl.h" |
| 37 | #include "dex/dex_file_loader.h" |
| 38 | #include "dex/dex_file_tracking_registrar.h" |
Mathieu Chartier | 61d2b2d | 2016-02-04 13:31:46 -0800 | [diff] [blame] | 39 | #include "gc/scoped_gc_critical_section.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 40 | #include "gc/space/image_space.h" |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 41 | #include "handle_scope-inl.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 42 | #include "jni/jni_internal.h" |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 43 | #include "mirror/class_loader.h" |
Andreas Gampe | 90b936d | 2017-01-31 08:58:55 -0800 | [diff] [blame] | 44 | #include "mirror/object-inl.h" |
Alex Light | 2ce6fc8 | 2017-12-18 16:42:36 -0800 | [diff] [blame] | 45 | #include "oat_file.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 46 | #include "oat_file_assistant.h" |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 47 | #include "obj_ptr-inl.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 48 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 49 | #include "thread-current-inl.h" |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 50 | #include "thread_list.h" |
Andreas Gampe | 90b936d | 2017-01-31 08:58:55 -0800 | [diff] [blame] | 51 | #include "well_known_classes.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 52 | |
| 53 | namespace art { |
| 54 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 55 | using android::base::StringPrintf; |
| 56 | |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 57 | // If true, we attempt to load the application image if it exists. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 58 | static constexpr bool kEnableAppImage = true; |
| 59 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 60 | const OatFile* OatFileManager::RegisterOatFile(std::unique_ptr<const OatFile> oat_file) { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 61 | WriterMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 62 | CHECK(!only_use_system_oat_files_ || |
| 63 | LocationIsOnSystem(oat_file->GetLocation().c_str()) || |
| 64 | !oat_file->IsExecutable()) |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 65 | << "Registering a non /system oat file: " << oat_file->GetLocation(); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 66 | DCHECK(oat_file != nullptr); |
| 67 | if (kIsDebugBuild) { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 68 | CHECK(oat_files_.find(oat_file) == oat_files_.end()); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 69 | for (const std::unique_ptr<const OatFile>& existing : oat_files_) { |
| 70 | CHECK_NE(oat_file.get(), existing.get()) << oat_file->GetLocation(); |
| 71 | // Check that we don't have an oat file with the same address. Copies of the same oat file |
| 72 | // should be loaded at different addresses. |
| 73 | CHECK_NE(oat_file->Begin(), existing->Begin()) << "Oat file already mapped at that location"; |
| 74 | } |
| 75 | } |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 76 | const OatFile* ret = oat_file.get(); |
| 77 | oat_files_.insert(std::move(oat_file)); |
| 78 | return ret; |
| 79 | } |
| 80 | |
| 81 | void OatFileManager::UnRegisterAndDeleteOatFile(const OatFile* oat_file) { |
| 82 | WriterMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
| 83 | DCHECK(oat_file != nullptr); |
| 84 | std::unique_ptr<const OatFile> compare(oat_file); |
| 85 | auto it = oat_files_.find(compare); |
| 86 | CHECK(it != oat_files_.end()); |
| 87 | oat_files_.erase(it); |
Andreas Gampe | afaf7f8 | 2018-10-16 11:32:38 -0700 | [diff] [blame] | 88 | compare.release(); // NOLINT b/117926937 |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Calin Juravle | 0b79127 | 2016-04-18 16:38:27 +0100 | [diff] [blame] | 91 | const OatFile* OatFileManager::FindOpenedOatFileFromDexLocation( |
| 92 | const std::string& dex_base_location) const { |
| 93 | ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
| 94 | for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) { |
| 95 | const std::vector<const OatDexFile*>& oat_dex_files = oat_file->GetOatDexFiles(); |
| 96 | for (const OatDexFile* oat_dex_file : oat_dex_files) { |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 97 | if (DexFileLoader::GetBaseLocation(oat_dex_file->GetDexFileLocation()) == dex_base_location) { |
Calin Juravle | 0b79127 | 2016-04-18 16:38:27 +0100 | [diff] [blame] | 98 | return oat_file.get(); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | return nullptr; |
| 103 | } |
| 104 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 105 | const OatFile* OatFileManager::FindOpenedOatFileFromOatLocation(const std::string& oat_location) |
| 106 | const { |
| 107 | ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 108 | return FindOpenedOatFileFromOatLocationLocked(oat_location); |
| 109 | } |
| 110 | |
| 111 | const OatFile* OatFileManager::FindOpenedOatFileFromOatLocationLocked( |
| 112 | const std::string& oat_location) const { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 113 | for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) { |
| 114 | if (oat_file->GetLocation() == oat_location) { |
| 115 | return oat_file.get(); |
| 116 | } |
| 117 | } |
| 118 | return nullptr; |
| 119 | } |
| 120 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 121 | std::vector<const OatFile*> OatFileManager::GetBootOatFiles() const { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 122 | std::vector<gc::space::ImageSpace*> image_spaces = |
| 123 | Runtime::Current()->GetHeap()->GetBootImageSpaces(); |
Andreas Gampe | 2a487eb | 2018-11-19 11:41:22 -0800 | [diff] [blame] | 124 | std::vector<const OatFile*> oat_files; |
| 125 | oat_files.reserve(image_spaces.size()); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 126 | for (gc::space::ImageSpace* image_space : image_spaces) { |
| 127 | oat_files.push_back(image_space->GetOatFile()); |
| 128 | } |
| 129 | return oat_files; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | const OatFile* OatFileManager::GetPrimaryOatFile() const { |
| 133 | ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 134 | std::vector<const OatFile*> boot_oat_files = GetBootOatFiles(); |
| 135 | if (!boot_oat_files.empty()) { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 136 | for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 137 | if (std::find(boot_oat_files.begin(), boot_oat_files.end(), oat_file.get()) == |
| 138 | boot_oat_files.end()) { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 139 | return oat_file.get(); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | return nullptr; |
| 144 | } |
| 145 | |
Vladimir Marko | b0b68cf | 2017-11-14 18:11:50 +0000 | [diff] [blame] | 146 | OatFileManager::OatFileManager() |
Vladimir Marko | e066932 | 2018-09-03 15:44:54 +0100 | [diff] [blame] | 147 | : only_use_system_oat_files_(false) {} |
Vladimir Marko | b0b68cf | 2017-11-14 18:11:50 +0000 | [diff] [blame] | 148 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 149 | OatFileManager::~OatFileManager() { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 150 | // Explicitly clear oat_files_ since the OatFile destructor calls back into OatFileManager for |
| 151 | // UnRegisterOatFileLocation. |
| 152 | oat_files_.clear(); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 155 | std::vector<const OatFile*> OatFileManager::RegisterImageOatFiles( |
Stephen Hines | 48ba197 | 2018-09-24 13:35:54 -0700 | [diff] [blame] | 156 | const std::vector<gc::space::ImageSpace*>& spaces) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 157 | std::vector<const OatFile*> oat_files; |
Andreas Gampe | 2a487eb | 2018-11-19 11:41:22 -0800 | [diff] [blame] | 158 | oat_files.reserve(spaces.size()); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 159 | for (gc::space::ImageSpace* space : spaces) { |
| 160 | oat_files.push_back(RegisterOatFile(space->ReleaseOatFile())); |
| 161 | } |
| 162 | return oat_files; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 165 | class TypeIndexInfo { |
| 166 | public: |
| 167 | explicit TypeIndexInfo(const DexFile* dex_file) |
| 168 | : type_indexes_(GenerateTypeIndexes(dex_file)), |
| 169 | iter_(type_indexes_.Indexes().begin()), |
| 170 | end_(type_indexes_.Indexes().end()) { } |
| 171 | |
| 172 | BitVector& GetTypeIndexes() { |
| 173 | return type_indexes_; |
| 174 | } |
| 175 | BitVector::IndexIterator& GetIterator() { |
| 176 | return iter_; |
| 177 | } |
| 178 | BitVector::IndexIterator& GetIteratorEnd() { |
| 179 | return end_; |
| 180 | } |
| 181 | void AdvanceIterator() { |
| 182 | iter_++; |
| 183 | } |
| 184 | |
| 185 | private: |
| 186 | static BitVector GenerateTypeIndexes(const DexFile* dex_file) { |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 187 | BitVector type_indexes(/*start_bits=*/0, /*expandable=*/true, Allocator::GetMallocAllocator()); |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 188 | for (uint16_t i = 0; i < dex_file->NumClassDefs(); ++i) { |
| 189 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(i); |
| 190 | uint16_t type_idx = class_def.class_idx_.index_; |
| 191 | type_indexes.SetBit(type_idx); |
| 192 | } |
| 193 | return type_indexes; |
| 194 | } |
| 195 | |
| 196 | // BitVector with bits set for the type indexes of all classes in the input dex file. |
| 197 | BitVector type_indexes_; |
| 198 | BitVector::IndexIterator iter_; |
| 199 | BitVector::IndexIterator end_; |
| 200 | }; |
| 201 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 202 | class DexFileAndClassPair : ValueObject { |
| 203 | public: |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 204 | DexFileAndClassPair(const DexFile* dex_file, TypeIndexInfo* type_info, bool from_loaded_oat) |
| 205 | : type_info_(type_info), |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 206 | dex_file_(dex_file), |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 207 | cached_descriptor_(dex_file_->StringByTypeIdx(dex::TypeIndex(*type_info->GetIterator()))), |
| 208 | from_loaded_oat_(from_loaded_oat) { |
| 209 | type_info_->AdvanceIterator(); |
| 210 | } |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 211 | |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 212 | DexFileAndClassPair(const DexFileAndClassPair& rhs) = default; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 213 | |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 214 | DexFileAndClassPair& operator=(const DexFileAndClassPair& rhs) = default; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 215 | |
| 216 | const char* GetCachedDescriptor() const { |
| 217 | return cached_descriptor_; |
| 218 | } |
| 219 | |
| 220 | bool operator<(const DexFileAndClassPair& rhs) const { |
| 221 | const int cmp = strcmp(cached_descriptor_, rhs.cached_descriptor_); |
| 222 | if (cmp != 0) { |
| 223 | // Note that the order must be reversed. We want to iterate over the classes in dex files. |
| 224 | // They are sorted lexicographically. Thus, the priority-queue must be a min-queue. |
| 225 | return cmp > 0; |
| 226 | } |
| 227 | return dex_file_ < rhs.dex_file_; |
| 228 | } |
| 229 | |
| 230 | bool DexFileHasMoreClasses() const { |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 231 | return type_info_->GetIterator() != type_info_->GetIteratorEnd(); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | void Next() { |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 235 | cached_descriptor_ = dex_file_->StringByTypeIdx(dex::TypeIndex(*type_info_->GetIterator())); |
| 236 | type_info_->AdvanceIterator(); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | bool FromLoadedOat() const { |
| 240 | return from_loaded_oat_; |
| 241 | } |
| 242 | |
| 243 | const DexFile* GetDexFile() const { |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 244 | return dex_file_; |
| 245 | } |
| 246 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 247 | private: |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 248 | TypeIndexInfo* type_info_; |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 249 | const DexFile* dex_file_; |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 250 | const char* cached_descriptor_; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 251 | bool from_loaded_oat_; // We only need to compare mismatches between what we load now |
| 252 | // and what was loaded before. Any old duplicates must have been |
| 253 | // OK, and any new "internal" duplicates are as well (they must |
| 254 | // be from multidex, which resolves correctly). |
| 255 | }; |
| 256 | |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 257 | static void AddDexFilesFromOat( |
| 258 | const OatFile* oat_file, |
| 259 | /*out*/std::vector<const DexFile*>* dex_files, |
| 260 | std::vector<std::unique_ptr<const DexFile>>* opened_dex_files) { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 261 | for (const OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) { |
| 262 | std::string error; |
| 263 | std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error); |
| 264 | if (dex_file == nullptr) { |
| 265 | LOG(WARNING) << "Could not create dex file from oat file: " << error; |
| 266 | } else if (dex_file->NumClassDefs() > 0U) { |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 267 | dex_files->push_back(dex_file.get()); |
Mathieu Chartier | 14d7b3e | 2016-06-09 16:18:04 -0700 | [diff] [blame] | 268 | opened_dex_files->push_back(std::move(dex_file)); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 273 | static void AddNext(/*inout*/DexFileAndClassPair& original, |
| 274 | /*inout*/std::priority_queue<DexFileAndClassPair>& heap) { |
| 275 | if (original.DexFileHasMoreClasses()) { |
| 276 | original.Next(); |
| 277 | heap.push(std::move(original)); |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 281 | static bool CheckClassCollision(const OatFile* oat_file, |
| 282 | const ClassLoaderContext* context, |
| 283 | std::string* error_msg /*out*/) { |
| 284 | std::vector<const DexFile*> dex_files_loaded = context->FlattenOpenedDexFiles(); |
| 285 | |
| 286 | // Vector that holds the newly opened dex files live, this is done to prevent leaks. |
| 287 | std::vector<std::unique_ptr<const DexFile>> opened_dex_files; |
| 288 | |
| 289 | ScopedTrace st("Collision check"); |
| 290 | // Add dex files from the oat file to check. |
| 291 | std::vector<const DexFile*> dex_files_unloaded; |
| 292 | AddDexFilesFromOat(oat_file, &dex_files_unloaded, &opened_dex_files); |
| 293 | |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 294 | // Generate type index information for each dex file. |
| 295 | std::vector<TypeIndexInfo> loaded_types; |
Andreas Gampe | 2a487eb | 2018-11-19 11:41:22 -0800 | [diff] [blame] | 296 | loaded_types.reserve(dex_files_loaded.size()); |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 297 | for (const DexFile* dex_file : dex_files_loaded) { |
| 298 | loaded_types.push_back(TypeIndexInfo(dex_file)); |
| 299 | } |
| 300 | std::vector<TypeIndexInfo> unloaded_types; |
Andreas Gampe | 2a487eb | 2018-11-19 11:41:22 -0800 | [diff] [blame] | 301 | unloaded_types.reserve(dex_files_unloaded.size()); |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 302 | for (const DexFile* dex_file : dex_files_unloaded) { |
| 303 | unloaded_types.push_back(TypeIndexInfo(dex_file)); |
| 304 | } |
| 305 | |
| 306 | // Populate the queue of dex file and class pairs with the loaded and unloaded dex files. |
| 307 | std::priority_queue<DexFileAndClassPair> queue; |
| 308 | for (size_t i = 0; i < dex_files_loaded.size(); ++i) { |
| 309 | if (loaded_types[i].GetIterator() != loaded_types[i].GetIteratorEnd()) { |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 310 | queue.emplace(dex_files_loaded[i], &loaded_types[i], /*from_loaded_oat=*/true); |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | for (size_t i = 0; i < dex_files_unloaded.size(); ++i) { |
| 314 | if (unloaded_types[i].GetIterator() != unloaded_types[i].GetIteratorEnd()) { |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 315 | queue.emplace(dex_files_unloaded[i], &unloaded_types[i], /*from_loaded_oat=*/false); |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | |
| 319 | // Now drain the queue. |
Jeff Hao | 0471ece | 2017-04-07 16:28:12 -0700 | [diff] [blame] | 320 | bool has_duplicates = false; |
| 321 | error_msg->clear(); |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 322 | while (!queue.empty()) { |
| 323 | // Modifying the top element is only safe if we pop right after. |
| 324 | DexFileAndClassPair compare_pop(queue.top()); |
| 325 | queue.pop(); |
| 326 | |
| 327 | // Compare against the following elements. |
| 328 | while (!queue.empty()) { |
| 329 | DexFileAndClassPair top(queue.top()); |
| 330 | if (strcmp(compare_pop.GetCachedDescriptor(), top.GetCachedDescriptor()) == 0) { |
| 331 | // Same descriptor. Check whether it's crossing old-oat-files to new-oat-files. |
| 332 | if (compare_pop.FromLoadedOat() != top.FromLoadedOat()) { |
Jeff Hao | 0471ece | 2017-04-07 16:28:12 -0700 | [diff] [blame] | 333 | error_msg->append( |
| 334 | StringPrintf("Found duplicated class when checking oat files: '%s' in %s and %s\n", |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 335 | compare_pop.GetCachedDescriptor(), |
| 336 | compare_pop.GetDexFile()->GetLocation().c_str(), |
Jeff Hao | 0471ece | 2017-04-07 16:28:12 -0700 | [diff] [blame] | 337 | top.GetDexFile()->GetLocation().c_str())); |
| 338 | if (!VLOG_IS_ON(oat)) { |
| 339 | return true; |
| 340 | } |
| 341 | has_duplicates = true; |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 342 | } |
| 343 | queue.pop(); |
| 344 | AddNext(top, queue); |
| 345 | } else { |
| 346 | // Something else. Done here. |
| 347 | break; |
| 348 | } |
| 349 | } |
| 350 | AddNext(compare_pop, queue); |
| 351 | } |
| 352 | |
Jeff Hao | 0471ece | 2017-04-07 16:28:12 -0700 | [diff] [blame] | 353 | return has_duplicates; |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 356 | // Check for class-def collisions in dex files. |
| 357 | // |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 358 | // This first walks the class loader chain present in the given context, getting all the dex files |
| 359 | // from the class loader. |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 360 | // |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 361 | // If the context is null (which means the initial class loader was null or unsupported) |
| 362 | // this returns false. b/37777332. |
| 363 | // |
| 364 | // This first checks whether all class loaders in the context have the same type and |
| 365 | // classpath. If so, we exit early. Otherwise, we do the collision check. |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 366 | // |
| 367 | // The collision check works by maintaining a heap with one class from each dex file, sorted by the |
| 368 | // class descriptor. Then a dex-file/class pair is continually removed from the heap and compared |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 369 | // against the following top element. If the descriptor is the same, it is now checked whether |
| 370 | // the two elements agree on whether their dex file was from an already-loaded oat-file or the |
| 371 | // new oat file. Any disagreement indicates a collision. |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 372 | OatFileManager::CheckCollisionResult OatFileManager::CheckCollision( |
| 373 | const OatFile* oat_file, |
| 374 | const ClassLoaderContext* context, |
| 375 | /*out*/ std::string* error_msg) const { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 376 | DCHECK(oat_file != nullptr); |
| 377 | DCHECK(error_msg != nullptr); |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 378 | |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 379 | // The context might be null if there are unrecognized class loaders in the chain or they |
| 380 | // don't meet sensible sanity conditions. In this case we assume that the app knows what it's |
| 381 | // doing and accept the oat file. |
| 382 | // Note that this has correctness implications as we cannot guarantee that the class resolution |
| 383 | // used during compilation is OK (b/37777332). |
| 384 | if (context == nullptr) { |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 385 | LOG(WARNING) << "Skipping duplicate class check due to unsupported classloader"; |
| 386 | return CheckCollisionResult::kSkippedUnsupportedClassLoader; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 389 | // If the oat file loading context matches the context used during compilation then we accept |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 390 | // the oat file without addition checks |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 391 | ClassLoaderContext::VerificationResult result = context->VerifyClassLoaderContextMatch( |
| 392 | oat_file->GetClassLoaderContext(), |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 393 | /*verify_names=*/ true, |
| 394 | /*verify_checksums=*/ true); |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 395 | switch (result) { |
| 396 | case ClassLoaderContext::VerificationResult::kForcedToSkipChecks: |
| 397 | return CheckCollisionResult::kSkippedClassLoaderContextSharedLibrary; |
| 398 | case ClassLoaderContext::VerificationResult::kMismatch: |
| 399 | // Mismatched context, do the actual collision check. |
| 400 | break; |
| 401 | case ClassLoaderContext::VerificationResult::kVerifies: |
| 402 | return CheckCollisionResult::kNoCollisions; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 405 | // The class loader context does not match. Perform a full duplicate classes check. |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 406 | return CheckClassCollision(oat_file, context, error_msg) |
| 407 | ? CheckCollisionResult::kPerformedHasCollisions : CheckCollisionResult::kNoCollisions; |
| 408 | } |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 409 | |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 410 | bool OatFileManager::AcceptOatFile(CheckCollisionResult result) const { |
| 411 | // Take the file only if it has no collisions, or we must take it because of preopting. |
| 412 | // Also accept oat files for shared libraries and unsupported class loaders. |
| 413 | return result != CheckCollisionResult::kPerformedHasCollisions; |
| 414 | } |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 415 | |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 416 | bool OatFileManager::ShouldLoadAppImage(CheckCollisionResult check_collision_result, |
| 417 | const OatFile* source_oat_file, |
| 418 | ClassLoaderContext* context, |
| 419 | std::string* error_msg) { |
| 420 | Runtime* const runtime = Runtime::Current(); |
| 421 | if (kEnableAppImage && (!runtime->IsJavaDebuggable() || source_oat_file->IsDebuggable())) { |
| 422 | // If we verified the class loader context (skipping due to the special marker doesn't |
| 423 | // count), then also avoid the collision check. |
| 424 | bool load_image = check_collision_result == CheckCollisionResult::kNoCollisions; |
| 425 | // If we skipped the collision check, we need to reverify to be sure its OK to load the |
| 426 | // image. |
| 427 | if (!load_image && |
| 428 | check_collision_result == |
| 429 | CheckCollisionResult::kSkippedClassLoaderContextSharedLibrary) { |
| 430 | // We can load the app image only if there are no collisions. If we know the |
| 431 | // class loader but didn't do the full collision check in HasCollisions(), |
| 432 | // do it now. b/77342775 |
| 433 | load_image = !CheckClassCollision(source_oat_file, context, error_msg); |
| 434 | } |
| 435 | return load_image; |
| 436 | } |
| 437 | return false; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | std::vector<std::unique_ptr<const DexFile>> OatFileManager::OpenDexFilesFromOat( |
| 441 | const char* dex_location, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 442 | jobject class_loader, |
| 443 | jobjectArray dex_elements, |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 444 | const OatFile** out_oat_file, |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 445 | std::vector<std::string>* error_msgs) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 446 | ScopedTrace trace(__FUNCTION__); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 447 | CHECK(dex_location != nullptr); |
| 448 | CHECK(error_msgs != nullptr); |
| 449 | |
| 450 | // Verify we aren't holding the mutator lock, which could starve GC if we |
| 451 | // have to generate or relocate an oat file. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 452 | Thread* const self = Thread::Current(); |
| 453 | Locks::mutator_lock_->AssertNotHeld(self); |
| 454 | Runtime* const runtime = Runtime::Current(); |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 455 | |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 456 | std::unique_ptr<ClassLoaderContext> context; |
| 457 | // If the class_loader is null there's not much we can do. This happens if a dex files is loaded |
| 458 | // directly with DexFile APIs instead of using class loaders. |
| 459 | if (class_loader == nullptr) { |
| 460 | LOG(WARNING) << "Opening an oat file without a class loader. " |
| 461 | << "Are you using the deprecated DexFile APIs?"; |
| 462 | context = nullptr; |
| 463 | } else { |
| 464 | context = ClassLoaderContext::CreateContextForClassLoader(class_loader, dex_elements); |
| 465 | } |
| 466 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 467 | OatFileAssistant oat_file_assistant(dex_location, |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 468 | kRuntimeISA, |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 469 | !runtime->IsAotCompiler(), |
| 470 | only_use_system_oat_files_); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 471 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 472 | // Get the oat file on disk. |
| 473 | std::unique_ptr<const OatFile> oat_file(oat_file_assistant.GetBestOatFile().release()); |
Andreas Gampe | 9ea84d0 | 2018-03-02 09:32:03 -0800 | [diff] [blame] | 474 | VLOG(oat) << "OatFileAssistant(" << dex_location << ").GetBestOatFile()=" |
| 475 | << reinterpret_cast<uintptr_t>(oat_file.get()) |
| 476 | << " (executable=" << (oat_file != nullptr ? oat_file->IsExecutable() : false) << ")"; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 477 | |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame] | 478 | const OatFile* source_oat_file = nullptr; |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 479 | CheckCollisionResult check_collision_result = CheckCollisionResult::kPerformedHasCollisions; |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame] | 480 | std::string error_msg; |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 481 | if ((class_loader != nullptr || dex_elements != nullptr) && oat_file != nullptr) { |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 482 | // Prevent oat files from being loaded if no class_loader or dex_elements are provided. |
| 483 | // This can happen when the deprecated DexFile.<init>(String) is called directly, and it |
| 484 | // could load oat files without checking the classpath, which would be incorrect. |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 485 | // Take the file only if it has no collisions, or we must take it because of preopting. |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 486 | check_collision_result = CheckCollision(oat_file.get(), context.get(), /*out*/ &error_msg); |
| 487 | bool accept_oat_file = AcceptOatFile(check_collision_result); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 488 | if (!accept_oat_file) { |
| 489 | // Failed the collision check. Print warning. |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 490 | if (runtime->IsDexFileFallbackEnabled()) { |
Narayan Kamath | 5c52574 | 2017-04-28 10:19:29 +0100 | [diff] [blame] | 491 | if (!oat_file_assistant.HasOriginalDexFiles()) { |
| 492 | // We need to fallback but don't have original dex files. We have to |
| 493 | // fallback to opening the existing oat file. This is potentially |
| 494 | // unsafe so we warn about it. |
| 495 | accept_oat_file = true; |
| 496 | |
| 497 | LOG(WARNING) << "Dex location " << dex_location << " does not seem to include dex file. " |
| 498 | << "Allow oat file use. This is potentially dangerous."; |
| 499 | } else { |
| 500 | // We have to fallback and found original dex files - extract them from an APK. |
| 501 | // Also warn about this operation because it's potentially wasteful. |
| 502 | LOG(WARNING) << "Found duplicate classes, falling back to extracting from APK : " |
| 503 | << dex_location; |
| 504 | LOG(WARNING) << "NOTE: This wastes RAM and hurts startup performance."; |
| 505 | } |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 506 | } else { |
Narayan Kamath | 5c52574 | 2017-04-28 10:19:29 +0100 | [diff] [blame] | 507 | // TODO: We should remove this. The fact that we're here implies -Xno-dex-file-fallback |
| 508 | // was set, which means that we should never fallback. If we don't have original dex |
| 509 | // files, we should just fail resolution as the flag intended. |
| 510 | if (!oat_file_assistant.HasOriginalDexFiles()) { |
| 511 | accept_oat_file = true; |
| 512 | } |
| 513 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 514 | LOG(WARNING) << "Found duplicate classes, dex-file-fallback disabled, will be failing to " |
| 515 | " load classes for " << dex_location; |
| 516 | } |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 517 | |
Narayan Kamath | 5c52574 | 2017-04-28 10:19:29 +0100 | [diff] [blame] | 518 | LOG(WARNING) << error_msg; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | if (accept_oat_file) { |
| 522 | VLOG(class_linker) << "Registering " << oat_file->GetLocation(); |
| 523 | source_oat_file = RegisterOatFile(std::move(oat_file)); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 524 | *out_oat_file = source_oat_file; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | |
| 528 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 529 | |
| 530 | // Load the dex files from the oat file. |
| 531 | if (source_oat_file != nullptr) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 532 | bool added_image_space = false; |
| 533 | if (source_oat_file->IsExecutable()) { |
Chris Wailes | 2386636 | 2018-08-22 16:16:58 -0700 | [diff] [blame] | 534 | ScopedTrace app_image_timing("AppImage:Loading"); |
| 535 | |
Alex Light | 2ce6fc8 | 2017-12-18 16:42:36 -0800 | [diff] [blame] | 536 | // We need to throw away the image space if we are debuggable but the oat-file source of the |
| 537 | // image is not otherwise we might get classes with inlined methods or other such things. |
| 538 | std::unique_ptr<gc::space::ImageSpace> image_space; |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 539 | if (ShouldLoadAppImage(check_collision_result, |
| 540 | source_oat_file, |
| 541 | context.get(), |
| 542 | &error_msg)) { |
Alex Light | 2ce6fc8 | 2017-12-18 16:42:36 -0800 | [diff] [blame] | 543 | image_space = oat_file_assistant.OpenImageSpace(source_oat_file); |
Alex Light | 2ce6fc8 | 2017-12-18 16:42:36 -0800 | [diff] [blame] | 544 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 545 | if (image_space != nullptr) { |
| 546 | ScopedObjectAccess soa(self); |
| 547 | StackHandleScope<1> hs(self); |
| 548 | Handle<mirror::ClassLoader> h_loader( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 549 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader))); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 550 | // Can not load app image without class loader. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 551 | if (h_loader != nullptr) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 552 | std::string temp_error_msg; |
| 553 | // Add image space has a race condition since other threads could be reading from the |
| 554 | // spaces array. |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 555 | { |
| 556 | ScopedThreadSuspension sts(self, kSuspended); |
Mathieu Chartier | 61d2b2d | 2016-02-04 13:31:46 -0800 | [diff] [blame] | 557 | gc::ScopedGCCriticalSection gcs(self, |
| 558 | gc::kGcCauseAddRemoveAppImageSpace, |
| 559 | gc::kCollectorTypeAddRemoveAppImageSpace); |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 560 | ScopedSuspendAll ssa("Add image space"); |
| 561 | runtime->GetHeap()->AddSpace(image_space.get()); |
| 562 | } |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 563 | { |
| 564 | ScopedTrace trace2(StringPrintf("Adding image space for location %s", dex_location)); |
| 565 | added_image_space = runtime->GetClassLinker()->AddImageSpace(image_space.get(), |
| 566 | h_loader, |
| 567 | dex_elements, |
| 568 | dex_location, |
| 569 | /*out*/&dex_files, |
| 570 | /*out*/&temp_error_msg); |
| 571 | } |
Mathieu Chartier | a6e81ed | 2016-02-25 13:52:10 -0800 | [diff] [blame] | 572 | if (added_image_space) { |
Mathieu Chartier | bd064ea | 2016-02-11 16:27:18 -0800 | [diff] [blame] | 573 | // Successfully added image space to heap, release the map so that it does not get |
| 574 | // freed. |
Andreas Gampe | afaf7f8 | 2018-10-16 11:32:38 -0700 | [diff] [blame] | 575 | image_space.release(); // NOLINT b/117926937 |
Bharadwaj Kalandhabhatta | 0bb4031 | 2017-06-01 10:47:00 -0700 | [diff] [blame] | 576 | |
| 577 | // Register for tracking. |
| 578 | for (const auto& dex_file : dex_files) { |
| 579 | dex::tracking::RegisterDexFile(dex_file.get()); |
| 580 | } |
Mathieu Chartier | bd064ea | 2016-02-11 16:27:18 -0800 | [diff] [blame] | 581 | } else { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 582 | LOG(INFO) << "Failed to add image file " << temp_error_msg; |
| 583 | dex_files.clear(); |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 584 | { |
| 585 | ScopedThreadSuspension sts(self, kSuspended); |
Mathieu Chartier | 61d2b2d | 2016-02-04 13:31:46 -0800 | [diff] [blame] | 586 | gc::ScopedGCCriticalSection gcs(self, |
| 587 | gc::kGcCauseAddRemoveAppImageSpace, |
| 588 | gc::kCollectorTypeAddRemoveAppImageSpace); |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 589 | ScopedSuspendAll ssa("Remove image space"); |
| 590 | runtime->GetHeap()->RemoveSpace(image_space.get()); |
| 591 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 592 | // Non-fatal, don't update error_msg. |
| 593 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 594 | } |
| 595 | } |
| 596 | } |
| 597 | if (!added_image_space) { |
| 598 | DCHECK(dex_files.empty()); |
| 599 | dex_files = oat_file_assistant.LoadDexFiles(*source_oat_file, dex_location); |
Bharadwaj Kalandhabhatta | 0bb4031 | 2017-06-01 10:47:00 -0700 | [diff] [blame] | 600 | |
| 601 | // Register for tracking. |
| 602 | for (const auto& dex_file : dex_files) { |
| 603 | dex::tracking::RegisterDexFile(dex_file.get()); |
| 604 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 605 | } |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 606 | if (dex_files.empty()) { |
| 607 | error_msgs->push_back("Failed to open dex files from " + source_oat_file->GetLocation()); |
Mathieu Chartier | be8303d | 2017-08-17 17:39:39 -0700 | [diff] [blame] | 608 | } else { |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 609 | // Opened dex files from an oat file, madvise them to their loaded state. |
| 610 | for (const std::unique_ptr<const DexFile>& dex_file : dex_files) { |
| 611 | OatDexFile::MadviseDexFile(*dex_file, MadviseState::kMadviseStateAtLoad); |
| 612 | } |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 613 | } |
| 614 | } |
| 615 | |
| 616 | // Fall back to running out of the original dex file if we couldn't load any |
| 617 | // dex_files from the oat file. |
| 618 | if (dex_files.empty()) { |
| 619 | if (oat_file_assistant.HasOriginalDexFiles()) { |
| 620 | if (Runtime::Current()->IsDexFileFallbackEnabled()) { |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 621 | static constexpr bool kVerifyChecksum = true; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 622 | const ArtDexFileLoader dex_file_loader; |
| 623 | if (!dex_file_loader.Open(dex_location, |
| 624 | dex_location, |
| 625 | Runtime::Current()->IsVerificationEnabled(), |
| 626 | kVerifyChecksum, |
| 627 | /*out*/ &error_msg, |
| 628 | &dex_files)) { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 629 | LOG(WARNING) << error_msg; |
Alex Light | 3045b66 | 2016-04-20 14:26:34 -0700 | [diff] [blame] | 630 | error_msgs->push_back("Failed to open dex files from " + std::string(dex_location) |
| 631 | + " because: " + error_msg); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 632 | } |
| 633 | } else { |
| 634 | error_msgs->push_back("Fallback mode disabled, skipping dex files."); |
| 635 | } |
| 636 | } else { |
| 637 | error_msgs->push_back("No original dex files found for dex location " |
| 638 | + std::string(dex_location)); |
| 639 | } |
| 640 | } |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 641 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 642 | return dex_files; |
| 643 | } |
| 644 | |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 645 | void OatFileManager::SetOnlyUseSystemOatFiles() { |
| 646 | ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
| 647 | CHECK_EQ(oat_files_.size(), GetBootOatFiles().size()); |
| 648 | only_use_system_oat_files_ = true; |
| 649 | } |
| 650 | |
Nicolas Geoffray | 04680f3 | 2016-03-17 11:56:54 +0000 | [diff] [blame] | 651 | void OatFileManager::DumpForSigQuit(std::ostream& os) { |
| 652 | ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
| 653 | std::vector<const OatFile*> boot_oat_files = GetBootOatFiles(); |
| 654 | for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) { |
| 655 | if (ContainsElement(boot_oat_files, oat_file.get())) { |
| 656 | continue; |
| 657 | } |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 658 | os << oat_file->GetLocation() << ": " << oat_file->GetCompilerFilter() << "\n"; |
Nicolas Geoffray | 04680f3 | 2016-03-17 11:56:54 +0000 | [diff] [blame] | 659 | } |
| 660 | } |
| 661 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 662 | } // namespace art |