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> |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 22 | #include <sys/stat.h> |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 23 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 24 | #include "android-base/stringprintf.h" |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 25 | #include "android-base/strings.h" |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 26 | |
Andreas Gampe | 90b936d | 2017-01-31 08:58:55 -0800 | [diff] [blame] | 27 | #include "art_field-inl.h" |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 28 | #include "base/bit_vector-inl.h" |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 29 | #include "base/file_utils.h" |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 30 | #include "base/logging.h" // For VLOG. |
Andreas Gampe | 7fbc4a5 | 2018-11-28 08:26:47 -0800 | [diff] [blame] | 31 | #include "base/mutex-inl.h" |
David Brazdil | 527072e | 2019-04-03 15:15:40 +0100 | [diff] [blame] | 32 | #include "base/sdk_version.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 33 | #include "base/stl_util.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 34 | #include "base/systrace.h" |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 35 | #include "class_linker.h" |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 36 | #include "class_loader_context.h" |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 37 | #include "dex/art_dex_file_loader.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 38 | #include "dex/dex_file-inl.h" |
| 39 | #include "dex/dex_file_loader.h" |
| 40 | #include "dex/dex_file_tracking_registrar.h" |
Mathieu Chartier | 61d2b2d | 2016-02-04 13:31:46 -0800 | [diff] [blame] | 41 | #include "gc/scoped_gc_critical_section.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 42 | #include "gc/space/image_space.h" |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 43 | #include "handle_scope-inl.h" |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 44 | #include "jni/java_vm_ext.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 45 | #include "jni/jni_internal.h" |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 46 | #include "mirror/class_loader.h" |
Andreas Gampe | 90b936d | 2017-01-31 08:58:55 -0800 | [diff] [blame] | 47 | #include "mirror/object-inl.h" |
Alex Light | 2ce6fc8 | 2017-12-18 16:42:36 -0800 | [diff] [blame] | 48 | #include "oat_file.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 49 | #include "oat_file_assistant.h" |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 50 | #include "obj_ptr-inl.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 51 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 52 | #include "thread-current-inl.h" |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 53 | #include "thread_list.h" |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 54 | #include "thread_pool.h" |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 55 | #include "vdex_file.h" |
| 56 | #include "verifier/verifier_deps.h" |
Andreas Gampe | 90b936d | 2017-01-31 08:58:55 -0800 | [diff] [blame] | 57 | #include "well_known_classes.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 58 | |
| 59 | namespace art { |
| 60 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 61 | using android::base::StringPrintf; |
| 62 | |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 63 | // If true, we attempt to load the application image if it exists. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 64 | static constexpr bool kEnableAppImage = true; |
| 65 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 66 | const OatFile* OatFileManager::RegisterOatFile(std::unique_ptr<const OatFile> oat_file) { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 67 | WriterMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 68 | CHECK(!only_use_system_oat_files_ || |
| 69 | LocationIsOnSystem(oat_file->GetLocation().c_str()) || |
| 70 | !oat_file->IsExecutable()) |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 71 | << "Registering a non /system oat file: " << oat_file->GetLocation(); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 72 | DCHECK(oat_file != nullptr); |
| 73 | if (kIsDebugBuild) { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 74 | CHECK(oat_files_.find(oat_file) == oat_files_.end()); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 75 | for (const std::unique_ptr<const OatFile>& existing : oat_files_) { |
| 76 | CHECK_NE(oat_file.get(), existing.get()) << oat_file->GetLocation(); |
| 77 | // Check that we don't have an oat file with the same address. Copies of the same oat file |
| 78 | // should be loaded at different addresses. |
| 79 | CHECK_NE(oat_file->Begin(), existing->Begin()) << "Oat file already mapped at that location"; |
| 80 | } |
| 81 | } |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 82 | const OatFile* ret = oat_file.get(); |
| 83 | oat_files_.insert(std::move(oat_file)); |
| 84 | return ret; |
| 85 | } |
| 86 | |
| 87 | void OatFileManager::UnRegisterAndDeleteOatFile(const OatFile* oat_file) { |
| 88 | WriterMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
| 89 | DCHECK(oat_file != nullptr); |
| 90 | std::unique_ptr<const OatFile> compare(oat_file); |
| 91 | auto it = oat_files_.find(compare); |
| 92 | CHECK(it != oat_files_.end()); |
| 93 | oat_files_.erase(it); |
Andreas Gampe | afaf7f8 | 2018-10-16 11:32:38 -0700 | [diff] [blame] | 94 | compare.release(); // NOLINT b/117926937 |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Calin Juravle | 0b79127 | 2016-04-18 16:38:27 +0100 | [diff] [blame] | 97 | const OatFile* OatFileManager::FindOpenedOatFileFromDexLocation( |
| 98 | const std::string& dex_base_location) const { |
| 99 | ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
| 100 | for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) { |
| 101 | const std::vector<const OatDexFile*>& oat_dex_files = oat_file->GetOatDexFiles(); |
| 102 | for (const OatDexFile* oat_dex_file : oat_dex_files) { |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 103 | if (DexFileLoader::GetBaseLocation(oat_dex_file->GetDexFileLocation()) == dex_base_location) { |
Calin Juravle | 0b79127 | 2016-04-18 16:38:27 +0100 | [diff] [blame] | 104 | return oat_file.get(); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | return nullptr; |
| 109 | } |
| 110 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 111 | const OatFile* OatFileManager::FindOpenedOatFileFromOatLocation(const std::string& oat_location) |
| 112 | const { |
| 113 | ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 114 | return FindOpenedOatFileFromOatLocationLocked(oat_location); |
| 115 | } |
| 116 | |
| 117 | const OatFile* OatFileManager::FindOpenedOatFileFromOatLocationLocked( |
| 118 | const std::string& oat_location) const { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 119 | for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) { |
| 120 | if (oat_file->GetLocation() == oat_location) { |
| 121 | return oat_file.get(); |
| 122 | } |
| 123 | } |
| 124 | return nullptr; |
| 125 | } |
| 126 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 127 | std::vector<const OatFile*> OatFileManager::GetBootOatFiles() const { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 128 | std::vector<gc::space::ImageSpace*> image_spaces = |
| 129 | Runtime::Current()->GetHeap()->GetBootImageSpaces(); |
Andreas Gampe | 2a487eb | 2018-11-19 11:41:22 -0800 | [diff] [blame] | 130 | std::vector<const OatFile*> oat_files; |
| 131 | oat_files.reserve(image_spaces.size()); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 132 | for (gc::space::ImageSpace* image_space : image_spaces) { |
| 133 | oat_files.push_back(image_space->GetOatFile()); |
| 134 | } |
| 135 | return oat_files; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | const OatFile* OatFileManager::GetPrimaryOatFile() const { |
| 139 | ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 140 | std::vector<const OatFile*> boot_oat_files = GetBootOatFiles(); |
| 141 | if (!boot_oat_files.empty()) { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 142 | for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 143 | if (std::find(boot_oat_files.begin(), boot_oat_files.end(), oat_file.get()) == |
| 144 | boot_oat_files.end()) { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 145 | return oat_file.get(); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | return nullptr; |
| 150 | } |
| 151 | |
Vladimir Marko | b0b68cf | 2017-11-14 18:11:50 +0000 | [diff] [blame] | 152 | OatFileManager::OatFileManager() |
Vladimir Marko | e066932 | 2018-09-03 15:44:54 +0100 | [diff] [blame] | 153 | : only_use_system_oat_files_(false) {} |
Vladimir Marko | b0b68cf | 2017-11-14 18:11:50 +0000 | [diff] [blame] | 154 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 155 | OatFileManager::~OatFileManager() { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 156 | // Explicitly clear oat_files_ since the OatFile destructor calls back into OatFileManager for |
| 157 | // UnRegisterOatFileLocation. |
| 158 | oat_files_.clear(); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 161 | std::vector<const OatFile*> OatFileManager::RegisterImageOatFiles( |
Stephen Hines | 48ba197 | 2018-09-24 13:35:54 -0700 | [diff] [blame] | 162 | const std::vector<gc::space::ImageSpace*>& spaces) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 163 | std::vector<const OatFile*> oat_files; |
Andreas Gampe | 2a487eb | 2018-11-19 11:41:22 -0800 | [diff] [blame] | 164 | oat_files.reserve(spaces.size()); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 165 | for (gc::space::ImageSpace* space : spaces) { |
| 166 | oat_files.push_back(RegisterOatFile(space->ReleaseOatFile())); |
| 167 | } |
| 168 | return oat_files; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 171 | class TypeIndexInfo { |
| 172 | public: |
| 173 | explicit TypeIndexInfo(const DexFile* dex_file) |
| 174 | : type_indexes_(GenerateTypeIndexes(dex_file)), |
| 175 | iter_(type_indexes_.Indexes().begin()), |
| 176 | end_(type_indexes_.Indexes().end()) { } |
| 177 | |
| 178 | BitVector& GetTypeIndexes() { |
| 179 | return type_indexes_; |
| 180 | } |
| 181 | BitVector::IndexIterator& GetIterator() { |
| 182 | return iter_; |
| 183 | } |
| 184 | BitVector::IndexIterator& GetIteratorEnd() { |
| 185 | return end_; |
| 186 | } |
| 187 | void AdvanceIterator() { |
| 188 | iter_++; |
| 189 | } |
| 190 | |
| 191 | private: |
| 192 | static BitVector GenerateTypeIndexes(const DexFile* dex_file) { |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 193 | BitVector type_indexes(/*start_bits=*/0, /*expandable=*/true, Allocator::GetMallocAllocator()); |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 194 | for (uint16_t i = 0; i < dex_file->NumClassDefs(); ++i) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 195 | const dex::ClassDef& class_def = dex_file->GetClassDef(i); |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 196 | uint16_t type_idx = class_def.class_idx_.index_; |
| 197 | type_indexes.SetBit(type_idx); |
| 198 | } |
| 199 | return type_indexes; |
| 200 | } |
| 201 | |
| 202 | // BitVector with bits set for the type indexes of all classes in the input dex file. |
| 203 | BitVector type_indexes_; |
| 204 | BitVector::IndexIterator iter_; |
| 205 | BitVector::IndexIterator end_; |
| 206 | }; |
| 207 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 208 | class DexFileAndClassPair : ValueObject { |
| 209 | public: |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 210 | DexFileAndClassPair(const DexFile* dex_file, TypeIndexInfo* type_info, bool from_loaded_oat) |
| 211 | : type_info_(type_info), |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 212 | dex_file_(dex_file), |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 213 | cached_descriptor_(dex_file_->StringByTypeIdx(dex::TypeIndex(*type_info->GetIterator()))), |
| 214 | from_loaded_oat_(from_loaded_oat) { |
| 215 | type_info_->AdvanceIterator(); |
| 216 | } |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 217 | |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 218 | DexFileAndClassPair(const DexFileAndClassPair& rhs) = default; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 219 | |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 220 | DexFileAndClassPair& operator=(const DexFileAndClassPair& rhs) = default; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 221 | |
| 222 | const char* GetCachedDescriptor() const { |
| 223 | return cached_descriptor_; |
| 224 | } |
| 225 | |
| 226 | bool operator<(const DexFileAndClassPair& rhs) const { |
| 227 | const int cmp = strcmp(cached_descriptor_, rhs.cached_descriptor_); |
| 228 | if (cmp != 0) { |
| 229 | // Note that the order must be reversed. We want to iterate over the classes in dex files. |
| 230 | // They are sorted lexicographically. Thus, the priority-queue must be a min-queue. |
| 231 | return cmp > 0; |
| 232 | } |
| 233 | return dex_file_ < rhs.dex_file_; |
| 234 | } |
| 235 | |
| 236 | bool DexFileHasMoreClasses() const { |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 237 | return type_info_->GetIterator() != type_info_->GetIteratorEnd(); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | void Next() { |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 241 | cached_descriptor_ = dex_file_->StringByTypeIdx(dex::TypeIndex(*type_info_->GetIterator())); |
| 242 | type_info_->AdvanceIterator(); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | bool FromLoadedOat() const { |
| 246 | return from_loaded_oat_; |
| 247 | } |
| 248 | |
| 249 | const DexFile* GetDexFile() const { |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 250 | return dex_file_; |
| 251 | } |
| 252 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 253 | private: |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 254 | TypeIndexInfo* type_info_; |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 255 | const DexFile* dex_file_; |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 256 | const char* cached_descriptor_; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 257 | bool from_loaded_oat_; // We only need to compare mismatches between what we load now |
| 258 | // and what was loaded before. Any old duplicates must have been |
| 259 | // OK, and any new "internal" duplicates are as well (they must |
| 260 | // be from multidex, which resolves correctly). |
| 261 | }; |
| 262 | |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 263 | static void AddDexFilesFromOat( |
| 264 | const OatFile* oat_file, |
| 265 | /*out*/std::vector<const DexFile*>* dex_files, |
| 266 | std::vector<std::unique_ptr<const DexFile>>* opened_dex_files) { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 267 | for (const OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) { |
| 268 | std::string error; |
| 269 | std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error); |
| 270 | if (dex_file == nullptr) { |
| 271 | LOG(WARNING) << "Could not create dex file from oat file: " << error; |
| 272 | } else if (dex_file->NumClassDefs() > 0U) { |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 273 | dex_files->push_back(dex_file.get()); |
Mathieu Chartier | 14d7b3e | 2016-06-09 16:18:04 -0700 | [diff] [blame] | 274 | opened_dex_files->push_back(std::move(dex_file)); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 279 | static void AddNext(/*inout*/DexFileAndClassPair& original, |
| 280 | /*inout*/std::priority_queue<DexFileAndClassPair>& heap) { |
| 281 | if (original.DexFileHasMoreClasses()) { |
| 282 | original.Next(); |
| 283 | heap.push(std::move(original)); |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 287 | static bool CheckClassCollision(const OatFile* oat_file, |
| 288 | const ClassLoaderContext* context, |
| 289 | std::string* error_msg /*out*/) { |
| 290 | std::vector<const DexFile*> dex_files_loaded = context->FlattenOpenedDexFiles(); |
| 291 | |
| 292 | // Vector that holds the newly opened dex files live, this is done to prevent leaks. |
| 293 | std::vector<std::unique_ptr<const DexFile>> opened_dex_files; |
| 294 | |
| 295 | ScopedTrace st("Collision check"); |
| 296 | // Add dex files from the oat file to check. |
| 297 | std::vector<const DexFile*> dex_files_unloaded; |
| 298 | AddDexFilesFromOat(oat_file, &dex_files_unloaded, &opened_dex_files); |
| 299 | |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 300 | // Generate type index information for each dex file. |
| 301 | std::vector<TypeIndexInfo> loaded_types; |
Andreas Gampe | 2a487eb | 2018-11-19 11:41:22 -0800 | [diff] [blame] | 302 | loaded_types.reserve(dex_files_loaded.size()); |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 303 | for (const DexFile* dex_file : dex_files_loaded) { |
| 304 | loaded_types.push_back(TypeIndexInfo(dex_file)); |
| 305 | } |
| 306 | std::vector<TypeIndexInfo> unloaded_types; |
Andreas Gampe | 2a487eb | 2018-11-19 11:41:22 -0800 | [diff] [blame] | 307 | unloaded_types.reserve(dex_files_unloaded.size()); |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 308 | for (const DexFile* dex_file : dex_files_unloaded) { |
| 309 | unloaded_types.push_back(TypeIndexInfo(dex_file)); |
| 310 | } |
| 311 | |
| 312 | // Populate the queue of dex file and class pairs with the loaded and unloaded dex files. |
| 313 | std::priority_queue<DexFileAndClassPair> queue; |
| 314 | for (size_t i = 0; i < dex_files_loaded.size(); ++i) { |
| 315 | if (loaded_types[i].GetIterator() != loaded_types[i].GetIteratorEnd()) { |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 316 | 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] | 317 | } |
| 318 | } |
| 319 | for (size_t i = 0; i < dex_files_unloaded.size(); ++i) { |
| 320 | if (unloaded_types[i].GetIterator() != unloaded_types[i].GetIteratorEnd()) { |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 321 | 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] | 322 | } |
| 323 | } |
| 324 | |
| 325 | // Now drain the queue. |
Jeff Hao | 0471ece | 2017-04-07 16:28:12 -0700 | [diff] [blame] | 326 | bool has_duplicates = false; |
| 327 | error_msg->clear(); |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 328 | while (!queue.empty()) { |
| 329 | // Modifying the top element is only safe if we pop right after. |
| 330 | DexFileAndClassPair compare_pop(queue.top()); |
| 331 | queue.pop(); |
| 332 | |
| 333 | // Compare against the following elements. |
| 334 | while (!queue.empty()) { |
| 335 | DexFileAndClassPair top(queue.top()); |
| 336 | if (strcmp(compare_pop.GetCachedDescriptor(), top.GetCachedDescriptor()) == 0) { |
| 337 | // Same descriptor. Check whether it's crossing old-oat-files to new-oat-files. |
| 338 | if (compare_pop.FromLoadedOat() != top.FromLoadedOat()) { |
Jeff Hao | 0471ece | 2017-04-07 16:28:12 -0700 | [diff] [blame] | 339 | error_msg->append( |
| 340 | 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] | 341 | compare_pop.GetCachedDescriptor(), |
| 342 | compare_pop.GetDexFile()->GetLocation().c_str(), |
Jeff Hao | 0471ece | 2017-04-07 16:28:12 -0700 | [diff] [blame] | 343 | top.GetDexFile()->GetLocation().c_str())); |
| 344 | if (!VLOG_IS_ON(oat)) { |
| 345 | return true; |
| 346 | } |
| 347 | has_duplicates = true; |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 348 | } |
| 349 | queue.pop(); |
| 350 | AddNext(top, queue); |
| 351 | } else { |
| 352 | // Something else. Done here. |
| 353 | break; |
| 354 | } |
| 355 | } |
| 356 | AddNext(compare_pop, queue); |
| 357 | } |
| 358 | |
Jeff Hao | 0471ece | 2017-04-07 16:28:12 -0700 | [diff] [blame] | 359 | return has_duplicates; |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 362 | // Check for class-def collisions in dex files. |
| 363 | // |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 364 | // This first walks the class loader chain present in the given context, getting all the dex files |
| 365 | // from the class loader. |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 366 | // |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 367 | // If the context is null (which means the initial class loader was null or unsupported) |
| 368 | // this returns false. b/37777332. |
| 369 | // |
| 370 | // This first checks whether all class loaders in the context have the same type and |
| 371 | // classpath. If so, we exit early. Otherwise, we do the collision check. |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 372 | // |
| 373 | // The collision check works by maintaining a heap with one class from each dex file, sorted by the |
| 374 | // 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] | 375 | // against the following top element. If the descriptor is the same, it is now checked whether |
| 376 | // the two elements agree on whether their dex file was from an already-loaded oat-file or the |
| 377 | // new oat file. Any disagreement indicates a collision. |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 378 | OatFileManager::CheckCollisionResult OatFileManager::CheckCollision( |
| 379 | const OatFile* oat_file, |
| 380 | const ClassLoaderContext* context, |
| 381 | /*out*/ std::string* error_msg) const { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 382 | DCHECK(oat_file != nullptr); |
| 383 | DCHECK(error_msg != nullptr); |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 384 | |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 385 | // The context might be null if there are unrecognized class loaders in the chain or they |
| 386 | // don't meet sensible sanity conditions. In this case we assume that the app knows what it's |
| 387 | // doing and accept the oat file. |
| 388 | // Note that this has correctness implications as we cannot guarantee that the class resolution |
| 389 | // used during compilation is OK (b/37777332). |
| 390 | if (context == nullptr) { |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 391 | LOG(WARNING) << "Skipping duplicate class check due to unsupported classloader"; |
| 392 | return CheckCollisionResult::kSkippedUnsupportedClassLoader; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 395 | // 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] | 396 | // the oat file without addition checks |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 397 | ClassLoaderContext::VerificationResult result = context->VerifyClassLoaderContextMatch( |
| 398 | oat_file->GetClassLoaderContext(), |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 399 | /*verify_names=*/ true, |
| 400 | /*verify_checksums=*/ true); |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 401 | switch (result) { |
| 402 | case ClassLoaderContext::VerificationResult::kForcedToSkipChecks: |
| 403 | return CheckCollisionResult::kSkippedClassLoaderContextSharedLibrary; |
| 404 | case ClassLoaderContext::VerificationResult::kMismatch: |
| 405 | // Mismatched context, do the actual collision check. |
| 406 | break; |
| 407 | case ClassLoaderContext::VerificationResult::kVerifies: |
| 408 | return CheckCollisionResult::kNoCollisions; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 411 | // 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] | 412 | return CheckClassCollision(oat_file, context, error_msg) |
| 413 | ? CheckCollisionResult::kPerformedHasCollisions : CheckCollisionResult::kNoCollisions; |
| 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::AcceptOatFile(CheckCollisionResult result) const { |
| 417 | // Take the file only if it has no collisions, or we must take it because of preopting. |
| 418 | // Also accept oat files for shared libraries and unsupported class loaders. |
| 419 | return result != CheckCollisionResult::kPerformedHasCollisions; |
| 420 | } |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 421 | |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 422 | bool OatFileManager::ShouldLoadAppImage(CheckCollisionResult check_collision_result, |
| 423 | const OatFile* source_oat_file, |
| 424 | ClassLoaderContext* context, |
| 425 | std::string* error_msg) { |
| 426 | Runtime* const runtime = Runtime::Current(); |
| 427 | if (kEnableAppImage && (!runtime->IsJavaDebuggable() || source_oat_file->IsDebuggable())) { |
| 428 | // If we verified the class loader context (skipping due to the special marker doesn't |
| 429 | // count), then also avoid the collision check. |
| 430 | bool load_image = check_collision_result == CheckCollisionResult::kNoCollisions; |
| 431 | // If we skipped the collision check, we need to reverify to be sure its OK to load the |
| 432 | // image. |
| 433 | if (!load_image && |
| 434 | check_collision_result == |
| 435 | CheckCollisionResult::kSkippedClassLoaderContextSharedLibrary) { |
| 436 | // We can load the app image only if there are no collisions. If we know the |
| 437 | // class loader but didn't do the full collision check in HasCollisions(), |
| 438 | // do it now. b/77342775 |
| 439 | load_image = !CheckClassCollision(source_oat_file, context, error_msg); |
| 440 | } |
| 441 | return load_image; |
| 442 | } |
| 443 | return false; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | std::vector<std::unique_ptr<const DexFile>> OatFileManager::OpenDexFilesFromOat( |
| 447 | const char* dex_location, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 448 | jobject class_loader, |
| 449 | jobjectArray dex_elements, |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 450 | const OatFile** out_oat_file, |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 451 | std::vector<std::string>* error_msgs) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 452 | ScopedTrace trace(__FUNCTION__); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 453 | CHECK(dex_location != nullptr); |
| 454 | CHECK(error_msgs != nullptr); |
| 455 | |
| 456 | // Verify we aren't holding the mutator lock, which could starve GC if we |
| 457 | // have to generate or relocate an oat file. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 458 | Thread* const self = Thread::Current(); |
| 459 | Locks::mutator_lock_->AssertNotHeld(self); |
| 460 | Runtime* const runtime = Runtime::Current(); |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 461 | |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 462 | std::unique_ptr<ClassLoaderContext> context; |
| 463 | // If the class_loader is null there's not much we can do. This happens if a dex files is loaded |
| 464 | // directly with DexFile APIs instead of using class loaders. |
| 465 | if (class_loader == nullptr) { |
| 466 | LOG(WARNING) << "Opening an oat file without a class loader. " |
| 467 | << "Are you using the deprecated DexFile APIs?"; |
| 468 | context = nullptr; |
| 469 | } else { |
| 470 | context = ClassLoaderContext::CreateContextForClassLoader(class_loader, dex_elements); |
| 471 | } |
| 472 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 473 | OatFileAssistant oat_file_assistant(dex_location, |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 474 | kRuntimeISA, |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 475 | !runtime->IsAotCompiler(), |
| 476 | only_use_system_oat_files_); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 477 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 478 | // Get the oat file on disk. |
| 479 | std::unique_ptr<const OatFile> oat_file(oat_file_assistant.GetBestOatFile().release()); |
Andreas Gampe | 9ea84d0 | 2018-03-02 09:32:03 -0800 | [diff] [blame] | 480 | VLOG(oat) << "OatFileAssistant(" << dex_location << ").GetBestOatFile()=" |
| 481 | << reinterpret_cast<uintptr_t>(oat_file.get()) |
| 482 | << " (executable=" << (oat_file != nullptr ? oat_file->IsExecutable() : false) << ")"; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 483 | |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame] | 484 | const OatFile* source_oat_file = nullptr; |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 485 | CheckCollisionResult check_collision_result = CheckCollisionResult::kPerformedHasCollisions; |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame] | 486 | std::string error_msg; |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 487 | if ((class_loader != nullptr || dex_elements != nullptr) && oat_file != nullptr) { |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 488 | // Prevent oat files from being loaded if no class_loader or dex_elements are provided. |
| 489 | // This can happen when the deprecated DexFile.<init>(String) is called directly, and it |
| 490 | // could load oat files without checking the classpath, which would be incorrect. |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 491 | // 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] | 492 | check_collision_result = CheckCollision(oat_file.get(), context.get(), /*out*/ &error_msg); |
| 493 | bool accept_oat_file = AcceptOatFile(check_collision_result); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 494 | if (!accept_oat_file) { |
| 495 | // Failed the collision check. Print warning. |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 496 | if (runtime->IsDexFileFallbackEnabled()) { |
Narayan Kamath | 5c52574 | 2017-04-28 10:19:29 +0100 | [diff] [blame] | 497 | if (!oat_file_assistant.HasOriginalDexFiles()) { |
| 498 | // We need to fallback but don't have original dex files. We have to |
| 499 | // fallback to opening the existing oat file. This is potentially |
| 500 | // unsafe so we warn about it. |
| 501 | accept_oat_file = true; |
| 502 | |
| 503 | LOG(WARNING) << "Dex location " << dex_location << " does not seem to include dex file. " |
| 504 | << "Allow oat file use. This is potentially dangerous."; |
| 505 | } else { |
| 506 | // We have to fallback and found original dex files - extract them from an APK. |
| 507 | // Also warn about this operation because it's potentially wasteful. |
| 508 | LOG(WARNING) << "Found duplicate classes, falling back to extracting from APK : " |
| 509 | << dex_location; |
| 510 | LOG(WARNING) << "NOTE: This wastes RAM and hurts startup performance."; |
| 511 | } |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 512 | } else { |
Narayan Kamath | 5c52574 | 2017-04-28 10:19:29 +0100 | [diff] [blame] | 513 | // TODO: We should remove this. The fact that we're here implies -Xno-dex-file-fallback |
| 514 | // was set, which means that we should never fallback. If we don't have original dex |
| 515 | // files, we should just fail resolution as the flag intended. |
| 516 | if (!oat_file_assistant.HasOriginalDexFiles()) { |
| 517 | accept_oat_file = true; |
| 518 | } |
| 519 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 520 | LOG(WARNING) << "Found duplicate classes, dex-file-fallback disabled, will be failing to " |
| 521 | " load classes for " << dex_location; |
| 522 | } |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 523 | |
Narayan Kamath | 5c52574 | 2017-04-28 10:19:29 +0100 | [diff] [blame] | 524 | LOG(WARNING) << error_msg; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | if (accept_oat_file) { |
| 528 | VLOG(class_linker) << "Registering " << oat_file->GetLocation(); |
| 529 | source_oat_file = RegisterOatFile(std::move(oat_file)); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 530 | *out_oat_file = source_oat_file; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | |
| 534 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 535 | |
| 536 | // Load the dex files from the oat file. |
| 537 | if (source_oat_file != nullptr) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 538 | bool added_image_space = false; |
| 539 | if (source_oat_file->IsExecutable()) { |
Chris Wailes | 2386636 | 2018-08-22 16:16:58 -0700 | [diff] [blame] | 540 | ScopedTrace app_image_timing("AppImage:Loading"); |
| 541 | |
Alex Light | 2ce6fc8 | 2017-12-18 16:42:36 -0800 | [diff] [blame] | 542 | // We need to throw away the image space if we are debuggable but the oat-file source of the |
| 543 | // image is not otherwise we might get classes with inlined methods or other such things. |
| 544 | std::unique_ptr<gc::space::ImageSpace> image_space; |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 545 | if (ShouldLoadAppImage(check_collision_result, |
| 546 | source_oat_file, |
| 547 | context.get(), |
| 548 | &error_msg)) { |
Alex Light | 2ce6fc8 | 2017-12-18 16:42:36 -0800 | [diff] [blame] | 549 | image_space = oat_file_assistant.OpenImageSpace(source_oat_file); |
Alex Light | 2ce6fc8 | 2017-12-18 16:42:36 -0800 | [diff] [blame] | 550 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 551 | if (image_space != nullptr) { |
| 552 | ScopedObjectAccess soa(self); |
| 553 | StackHandleScope<1> hs(self); |
| 554 | Handle<mirror::ClassLoader> h_loader( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 555 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader))); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 556 | // Can not load app image without class loader. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 557 | if (h_loader != nullptr) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 558 | std::string temp_error_msg; |
| 559 | // Add image space has a race condition since other threads could be reading from the |
| 560 | // spaces array. |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 561 | { |
| 562 | ScopedThreadSuspension sts(self, kSuspended); |
Mathieu Chartier | 61d2b2d | 2016-02-04 13:31:46 -0800 | [diff] [blame] | 563 | gc::ScopedGCCriticalSection gcs(self, |
| 564 | gc::kGcCauseAddRemoveAppImageSpace, |
| 565 | gc::kCollectorTypeAddRemoveAppImageSpace); |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 566 | ScopedSuspendAll ssa("Add image space"); |
| 567 | runtime->GetHeap()->AddSpace(image_space.get()); |
| 568 | } |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 569 | { |
| 570 | ScopedTrace trace2(StringPrintf("Adding image space for location %s", dex_location)); |
| 571 | added_image_space = runtime->GetClassLinker()->AddImageSpace(image_space.get(), |
| 572 | h_loader, |
| 573 | dex_elements, |
| 574 | dex_location, |
| 575 | /*out*/&dex_files, |
| 576 | /*out*/&temp_error_msg); |
| 577 | } |
Mathieu Chartier | a6e81ed | 2016-02-25 13:52:10 -0800 | [diff] [blame] | 578 | if (added_image_space) { |
Mathieu Chartier | bd064ea | 2016-02-11 16:27:18 -0800 | [diff] [blame] | 579 | // Successfully added image space to heap, release the map so that it does not get |
| 580 | // freed. |
Andreas Gampe | afaf7f8 | 2018-10-16 11:32:38 -0700 | [diff] [blame] | 581 | image_space.release(); // NOLINT b/117926937 |
Bharadwaj Kalandhabhatta | 0bb4031 | 2017-06-01 10:47:00 -0700 | [diff] [blame] | 582 | |
| 583 | // Register for tracking. |
| 584 | for (const auto& dex_file : dex_files) { |
| 585 | dex::tracking::RegisterDexFile(dex_file.get()); |
| 586 | } |
Mathieu Chartier | bd064ea | 2016-02-11 16:27:18 -0800 | [diff] [blame] | 587 | } else { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 588 | LOG(INFO) << "Failed to add image file " << temp_error_msg; |
| 589 | dex_files.clear(); |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 590 | { |
| 591 | ScopedThreadSuspension sts(self, kSuspended); |
Mathieu Chartier | 61d2b2d | 2016-02-04 13:31:46 -0800 | [diff] [blame] | 592 | gc::ScopedGCCriticalSection gcs(self, |
| 593 | gc::kGcCauseAddRemoveAppImageSpace, |
| 594 | gc::kCollectorTypeAddRemoveAppImageSpace); |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 595 | ScopedSuspendAll ssa("Remove image space"); |
| 596 | runtime->GetHeap()->RemoveSpace(image_space.get()); |
| 597 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 598 | // Non-fatal, don't update error_msg. |
| 599 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | } |
| 603 | if (!added_image_space) { |
| 604 | DCHECK(dex_files.empty()); |
| 605 | dex_files = oat_file_assistant.LoadDexFiles(*source_oat_file, dex_location); |
Bharadwaj Kalandhabhatta | 0bb4031 | 2017-06-01 10:47:00 -0700 | [diff] [blame] | 606 | |
| 607 | // Register for tracking. |
| 608 | for (const auto& dex_file : dex_files) { |
| 609 | dex::tracking::RegisterDexFile(dex_file.get()); |
| 610 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 611 | } |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 612 | if (dex_files.empty()) { |
| 613 | 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] | 614 | } else { |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 615 | // Opened dex files from an oat file, madvise them to their loaded state. |
| 616 | for (const std::unique_ptr<const DexFile>& dex_file : dex_files) { |
| 617 | OatDexFile::MadviseDexFile(*dex_file, MadviseState::kMadviseStateAtLoad); |
| 618 | } |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | |
| 622 | // Fall back to running out of the original dex file if we couldn't load any |
| 623 | // dex_files from the oat file. |
| 624 | if (dex_files.empty()) { |
| 625 | if (oat_file_assistant.HasOriginalDexFiles()) { |
| 626 | if (Runtime::Current()->IsDexFileFallbackEnabled()) { |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 627 | static constexpr bool kVerifyChecksum = true; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 628 | const ArtDexFileLoader dex_file_loader; |
| 629 | if (!dex_file_loader.Open(dex_location, |
| 630 | dex_location, |
| 631 | Runtime::Current()->IsVerificationEnabled(), |
| 632 | kVerifyChecksum, |
| 633 | /*out*/ &error_msg, |
| 634 | &dex_files)) { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 635 | LOG(WARNING) << error_msg; |
Alex Light | 3045b66 | 2016-04-20 14:26:34 -0700 | [diff] [blame] | 636 | error_msgs->push_back("Failed to open dex files from " + std::string(dex_location) |
| 637 | + " because: " + error_msg); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 638 | } |
| 639 | } else { |
| 640 | error_msgs->push_back("Fallback mode disabled, skipping dex files."); |
| 641 | } |
| 642 | } else { |
| 643 | error_msgs->push_back("No original dex files found for dex location " |
| 644 | + std::string(dex_location)); |
| 645 | } |
| 646 | } |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 647 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 648 | return dex_files; |
| 649 | } |
| 650 | |
David Brazdil | 7126c5b | 2019-03-05 00:02:51 +0000 | [diff] [blame] | 651 | static std::vector<const DexFile::Header*> GetDexFileHeaders(const std::vector<MemMap>& maps) { |
| 652 | std::vector<const DexFile::Header*> headers; |
| 653 | headers.reserve(maps.size()); |
| 654 | for (const MemMap& map : maps) { |
| 655 | DCHECK(map.IsValid()); |
| 656 | headers.push_back(reinterpret_cast<const DexFile::Header*>(map.Begin())); |
| 657 | } |
| 658 | return headers; |
| 659 | } |
| 660 | |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 661 | static std::vector<const DexFile::Header*> GetDexFileHeaders( |
| 662 | const std::vector<const DexFile*>& dex_files) { |
| 663 | std::vector<const DexFile::Header*> headers; |
| 664 | headers.reserve(dex_files.size()); |
| 665 | for (const DexFile* dex_file : dex_files) { |
| 666 | headers.push_back(&dex_file->GetHeader()); |
| 667 | } |
| 668 | return headers; |
| 669 | } |
| 670 | |
David Brazdil | 7126c5b | 2019-03-05 00:02:51 +0000 | [diff] [blame] | 671 | std::vector<std::unique_ptr<const DexFile>> OatFileManager::OpenDexFilesFromOat( |
| 672 | std::vector<MemMap>&& dex_mem_maps, |
| 673 | jobject class_loader, |
| 674 | jobjectArray dex_elements, |
| 675 | const OatFile** out_oat_file, |
| 676 | std::vector<std::string>* error_msgs) { |
| 677 | std::vector<std::unique_ptr<const DexFile>> dex_files = OpenDexFilesFromOat_Impl( |
| 678 | std::move(dex_mem_maps), |
| 679 | class_loader, |
| 680 | dex_elements, |
| 681 | out_oat_file, |
| 682 | error_msgs); |
| 683 | |
| 684 | if (error_msgs->empty()) { |
| 685 | // Remove write permission from DexFile pages. We do this at the end because |
| 686 | // OatFile assigns OatDexFile pointer in the DexFile objects. |
| 687 | for (std::unique_ptr<const DexFile>& dex_file : dex_files) { |
| 688 | if (!dex_file->DisableWrite()) { |
| 689 | error_msgs->push_back("Failed to make dex file " + dex_file->GetLocation() + " read-only"); |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | if (!error_msgs->empty()) { |
| 695 | return std::vector<std::unique_ptr<const DexFile>>(); |
| 696 | } |
| 697 | |
| 698 | return dex_files; |
| 699 | } |
| 700 | |
| 701 | std::vector<std::unique_ptr<const DexFile>> OatFileManager::OpenDexFilesFromOat_Impl( |
| 702 | std::vector<MemMap>&& dex_mem_maps, |
| 703 | jobject class_loader, |
| 704 | jobjectArray dex_elements, |
| 705 | const OatFile** out_oat_file, |
| 706 | std::vector<std::string>* error_msgs) { |
| 707 | ScopedTrace trace(__FUNCTION__); |
| 708 | std::string error_msg; |
| 709 | DCHECK(error_msgs != nullptr); |
| 710 | |
| 711 | // Extract dex file headers from `dex_mem_maps`. |
| 712 | const std::vector<const DexFile::Header*> dex_headers = GetDexFileHeaders(dex_mem_maps); |
| 713 | |
| 714 | // Determine dex/vdex locations and the combined location checksum. |
| 715 | uint32_t location_checksum; |
| 716 | std::string dex_location; |
| 717 | std::string vdex_path; |
| 718 | bool has_vdex = OatFileAssistant::AnonymousDexVdexLocation(dex_headers, |
| 719 | kRuntimeISA, |
| 720 | &location_checksum, |
| 721 | &dex_location, |
| 722 | &vdex_path); |
| 723 | |
| 724 | // Attempt to open an existing vdex and check dex file checksums match. |
| 725 | std::unique_ptr<VdexFile> vdex_file = nullptr; |
| 726 | if (has_vdex && OS::FileExists(vdex_path.c_str())) { |
| 727 | vdex_file = VdexFile::Open(vdex_path, |
| 728 | /* writable= */ false, |
| 729 | /* low_4gb= */ false, |
| 730 | /* unquicken= */ false, |
| 731 | &error_msg); |
| 732 | if (vdex_file == nullptr) { |
| 733 | LOG(WARNING) << "Failed to open vdex " << vdex_path << ": " << error_msg; |
| 734 | } else if (!vdex_file->MatchesDexFileChecksums(dex_headers)) { |
| 735 | LOG(WARNING) << "Failed to open vdex " << vdex_path << ": dex file checksum mismatch"; |
| 736 | vdex_file.reset(nullptr); |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | // Load dex files. Skip structural dex file verification if vdex was found |
| 741 | // and dex checksums matched. |
| 742 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 743 | for (size_t i = 0; i < dex_mem_maps.size(); ++i) { |
| 744 | static constexpr bool kVerifyChecksum = true; |
| 745 | const ArtDexFileLoader dex_file_loader; |
| 746 | std::unique_ptr<const DexFile> dex_file(dex_file_loader.Open( |
| 747 | DexFileLoader::GetMultiDexLocation(i, dex_location.c_str()), |
| 748 | location_checksum, |
| 749 | std::move(dex_mem_maps[i]), |
| 750 | /* verify= */ (vdex_file == nullptr) && Runtime::Current()->IsVerificationEnabled(), |
| 751 | kVerifyChecksum, |
| 752 | &error_msg)); |
| 753 | if (dex_file != nullptr) { |
| 754 | dex::tracking::RegisterDexFile(dex_file.get()); // Register for tracking. |
| 755 | dex_files.push_back(std::move(dex_file)); |
| 756 | } else { |
| 757 | error_msgs->push_back("Failed to open dex files from memory: " + error_msg); |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | // Check if we should proceed to creating an OatFile instance backed by the vdex. |
| 762 | // We need: (a) an existing vdex, (b) class loader (can be null if invoked via reflection), |
| 763 | // and (c) no errors during dex file loading. |
| 764 | if (vdex_file == nullptr || class_loader == nullptr || !error_msgs->empty()) { |
| 765 | return dex_files; |
| 766 | } |
| 767 | |
| 768 | // Attempt to create a class loader context, check OpenDexFiles succeeds (prerequisite |
| 769 | // for using the context later). |
| 770 | std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::CreateContextForClassLoader( |
| 771 | class_loader, |
| 772 | dex_elements); |
| 773 | if (context == nullptr) { |
| 774 | LOG(ERROR) << "Could not create class loader context for " << vdex_path; |
| 775 | return dex_files; |
| 776 | } |
| 777 | DCHECK(context->OpenDexFiles(kRuntimeISA, "")) |
| 778 | << "Context created from already opened dex files should not attempt to open again"; |
| 779 | |
| 780 | // Check that we can use the vdex against this boot class path and in this class loader context. |
| 781 | // Note 1: We do not need a class loader collision check because there is no compiled code. |
| 782 | // Note 2: If these checks fail, we cannot fast-verify because the vdex does not contain |
| 783 | // full VerifierDeps. |
| 784 | if (!vdex_file->MatchesBootClassPathChecksums() || |
| 785 | !vdex_file->MatchesClassLoaderContext(*context.get())) { |
| 786 | return dex_files; |
| 787 | } |
| 788 | |
| 789 | // Initialize an OatFile instance backed by the loaded vdex. |
| 790 | std::unique_ptr<OatFile> oat_file(OatFile::OpenFromVdex(MakeNonOwningPointerVector(dex_files), |
| 791 | std::move(vdex_file), |
| 792 | dex_location)); |
| 793 | DCHECK(oat_file != nullptr); |
| 794 | VLOG(class_linker) << "Registering " << oat_file->GetLocation(); |
| 795 | *out_oat_file = RegisterOatFile(std::move(oat_file)); |
| 796 | return dex_files; |
| 797 | } |
| 798 | |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 799 | // Check how many vdex files exist in the same directory as the vdex file we are about |
| 800 | // to write. If more than or equal to kAnonymousVdexCacheSize, unlink the least |
| 801 | // recently used one(s) (according to stat-reported atime). |
| 802 | static bool UnlinkLeastRecentlyUsedVdexIfNeeded(const std::string& vdex_path_to_add, |
| 803 | std::string* error_msg) { |
| 804 | if (OS::FileExists(vdex_path_to_add.c_str())) { |
| 805 | // File already exists and will be overwritten. |
| 806 | // This will not change the number of entries in the cache. |
| 807 | return true; |
| 808 | } |
| 809 | |
| 810 | auto last_slash = vdex_path_to_add.rfind('/'); |
| 811 | CHECK(last_slash != std::string::npos); |
| 812 | std::string vdex_dir = vdex_path_to_add.substr(0, last_slash + 1); |
| 813 | |
| 814 | if (!OS::DirectoryExists(vdex_dir.c_str())) { |
| 815 | // Folder does not exist yet. Cache has zero entries. |
| 816 | return true; |
| 817 | } |
| 818 | |
| 819 | std::vector<std::pair<time_t, std::string>> cache; |
| 820 | |
| 821 | DIR* c_dir = opendir(vdex_dir.c_str()); |
| 822 | if (c_dir == nullptr) { |
| 823 | *error_msg = "Unable to open " + vdex_dir + " to delete unused vdex files"; |
| 824 | return false; |
| 825 | } |
| 826 | for (struct dirent* de = readdir(c_dir); de != nullptr; de = readdir(c_dir)) { |
| 827 | if (de->d_type != DT_REG) { |
| 828 | continue; |
| 829 | } |
| 830 | std::string basename = de->d_name; |
| 831 | if (!OatFileAssistant::IsAnonymousVdexBasename(basename)) { |
| 832 | continue; |
| 833 | } |
| 834 | std::string fullname = vdex_dir + basename; |
| 835 | |
| 836 | struct stat s; |
| 837 | int rc = TEMP_FAILURE_RETRY(stat(fullname.c_str(), &s)); |
| 838 | if (rc == -1) { |
| 839 | *error_msg = "Failed to stat() anonymous vdex file " + fullname; |
| 840 | return false; |
| 841 | } |
| 842 | |
| 843 | cache.push_back(std::make_pair(s.st_atime, fullname)); |
| 844 | } |
| 845 | CHECK_EQ(0, closedir(c_dir)) << "Unable to close directory."; |
| 846 | |
| 847 | if (cache.size() < OatFileManager::kAnonymousVdexCacheSize) { |
| 848 | return true; |
| 849 | } |
| 850 | |
| 851 | std::sort(cache.begin(), |
| 852 | cache.end(), |
| 853 | [](const auto& a, const auto& b) { return a.first < b.first; }); |
| 854 | for (size_t i = OatFileManager::kAnonymousVdexCacheSize - 1; i < cache.size(); ++i) { |
| 855 | if (unlink(cache[i].second.c_str()) != 0) { |
| 856 | *error_msg = "Could not unlink anonymous vdex file " + cache[i].second; |
| 857 | return false; |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | return true; |
| 862 | } |
| 863 | |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 864 | class BackgroundVerificationTask final : public Task { |
| 865 | public: |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 866 | BackgroundVerificationTask(const std::vector<const DexFile*>& dex_files, |
| 867 | jobject class_loader, |
| 868 | const char* class_loader_context, |
| 869 | const std::string& vdex_path) |
| 870 | : dex_files_(dex_files), |
| 871 | class_loader_context_(class_loader_context), |
| 872 | vdex_path_(vdex_path) { |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 873 | Thread* const self = Thread::Current(); |
| 874 | ScopedObjectAccess soa(self); |
| 875 | // Create a global ref for `class_loader` because it will be accessed from a different thread. |
| 876 | class_loader_ = soa.Vm()->AddGlobalRef(self, soa.Decode<mirror::ClassLoader>(class_loader)); |
| 877 | CHECK(class_loader_ != nullptr); |
| 878 | } |
| 879 | |
| 880 | ~BackgroundVerificationTask() { |
| 881 | Thread* const self = Thread::Current(); |
| 882 | ScopedObjectAccess soa(self); |
| 883 | soa.Vm()->DeleteGlobalRef(self, class_loader_); |
| 884 | } |
| 885 | |
| 886 | void Run(Thread* self) override { |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 887 | std::string error_msg; |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 888 | ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 889 | verifier::VerifierDeps verifier_deps(dex_files_); |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 890 | |
| 891 | // Iterate over all classes and verify them. |
| 892 | for (const DexFile* dex_file : dex_files_) { |
| 893 | for (uint32_t cdef_idx = 0; cdef_idx < dex_file->NumClassDefs(); cdef_idx++) { |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 894 | const dex::ClassDef& class_def = dex_file->GetClassDef(cdef_idx); |
| 895 | |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 896 | // Take handles inside the loop. The background verification is low priority |
| 897 | // and we want to minimize the risk of blocking anyone else. |
| 898 | ScopedObjectAccess soa(self); |
| 899 | StackHandleScope<2> hs(self); |
| 900 | Handle<mirror::ClassLoader> h_loader(hs.NewHandle( |
| 901 | soa.Decode<mirror::ClassLoader>(class_loader_))); |
| 902 | Handle<mirror::Class> h_class(hs.NewHandle<mirror::Class>(class_linker->FindClass( |
| 903 | self, |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 904 | dex_file->GetClassDescriptor(class_def), |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 905 | h_loader))); |
| 906 | |
| 907 | if (h_class == nullptr) { |
| 908 | CHECK(self->IsExceptionPending()); |
| 909 | self->ClearException(); |
| 910 | continue; |
| 911 | } |
| 912 | |
| 913 | if (&h_class->GetDexFile() != dex_file) { |
| 914 | // There is a different class in the class path or a parent class loader |
| 915 | // with the same descriptor. This `h_class` is not resolvable, skip it. |
| 916 | continue; |
| 917 | } |
| 918 | |
| 919 | CHECK(h_class->IsResolved()) << h_class->PrettyDescriptor(); |
| 920 | class_linker->VerifyClass(self, h_class); |
| 921 | if (h_class->IsErroneous()) { |
| 922 | // ClassLinker::VerifyClass throws, which isn't useful here. |
| 923 | CHECK(soa.Self()->IsExceptionPending()); |
| 924 | soa.Self()->ClearException(); |
| 925 | } |
| 926 | |
| 927 | CHECK(h_class->IsVerified() || h_class->IsErroneous()) |
| 928 | << h_class->PrettyDescriptor() << ": state=" << h_class->GetStatus(); |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 929 | |
| 930 | if (h_class->IsVerified()) { |
| 931 | verifier_deps.RecordClassVerified(*dex_file, class_def); |
| 932 | } |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 933 | } |
| 934 | } |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 935 | |
| 936 | // Delete old vdex files if there are too many in the folder. |
| 937 | if (!UnlinkLeastRecentlyUsedVdexIfNeeded(vdex_path_, &error_msg)) { |
| 938 | LOG(ERROR) << "Could not unlink old vdex files " << vdex_path_ << ": " << error_msg; |
| 939 | return; |
| 940 | } |
| 941 | |
| 942 | // Construct a vdex file and write `verifier_deps` into it. |
| 943 | if (!VdexFile::WriteToDisk(vdex_path_, |
| 944 | dex_files_, |
| 945 | verifier_deps, |
| 946 | class_loader_context_, |
| 947 | &error_msg)) { |
| 948 | LOG(ERROR) << "Could not write anonymous vdex " << vdex_path_ << ": " << error_msg; |
| 949 | return; |
| 950 | } |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | void Finalize() override { |
| 954 | delete this; |
| 955 | } |
| 956 | |
| 957 | private: |
| 958 | const std::vector<const DexFile*> dex_files_; |
| 959 | jobject class_loader_; |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 960 | const std::string class_loader_context_; |
| 961 | const std::string vdex_path_; |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 962 | |
| 963 | DISALLOW_COPY_AND_ASSIGN(BackgroundVerificationTask); |
| 964 | }; |
| 965 | |
| 966 | void OatFileManager::RunBackgroundVerification(const std::vector<const DexFile*>& dex_files, |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 967 | jobject class_loader, |
| 968 | const char* class_loader_context) { |
David Brazdil | 527072e | 2019-04-03 15:15:40 +0100 | [diff] [blame] | 969 | Runtime* const runtime = Runtime::Current(); |
| 970 | Thread* const self = Thread::Current(); |
| 971 | |
| 972 | if (runtime->IsJavaDebuggable()) { |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 973 | // Threads created by ThreadPool ("runtime threads") are not allowed to load |
| 974 | // classes when debuggable to match class-initialization semantics |
| 975 | // expectations. Do not verify in the background. |
| 976 | return; |
| 977 | } |
| 978 | |
David Brazdil | 527072e | 2019-04-03 15:15:40 +0100 | [diff] [blame] | 979 | if (!IsSdkVersionSetAndAtLeast(runtime->GetTargetSdkVersion(), SdkVersion::kQ)) { |
| 980 | // Do not run for legacy apps as they may depend on the previous class loader behaviour. |
| 981 | return; |
| 982 | } |
| 983 | |
| 984 | if (runtime->IsShuttingDown(self)) { |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 985 | // Not allowed to create new threads during runtime shutdown. |
| 986 | return; |
| 987 | } |
| 988 | |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 989 | uint32_t location_checksum; |
| 990 | std::string dex_location; |
| 991 | std::string vdex_path; |
| 992 | if (OatFileAssistant::AnonymousDexVdexLocation(GetDexFileHeaders(dex_files), |
| 993 | kRuntimeISA, |
| 994 | &location_checksum, |
| 995 | &dex_location, |
| 996 | &vdex_path)) { |
| 997 | if (verification_thread_pool_ == nullptr) { |
| 998 | verification_thread_pool_.reset( |
| 999 | new ThreadPool("Verification thread pool", /* num_threads= */ 1)); |
| 1000 | verification_thread_pool_->StartWorkers(self); |
| 1001 | } |
| 1002 | verification_thread_pool_->AddTask(self, new BackgroundVerificationTask( |
| 1003 | dex_files, |
| 1004 | class_loader, |
| 1005 | class_loader_context, |
| 1006 | vdex_path)); |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 1007 | } |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | void OatFileManager::WaitForWorkersToBeCreated() { |
| 1011 | DCHECK(!Runtime::Current()->IsShuttingDown(Thread::Current())) |
| 1012 | << "Cannot create new threads during runtime shutdown"; |
| 1013 | if (verification_thread_pool_ != nullptr) { |
| 1014 | verification_thread_pool_->WaitForWorkersToBeCreated(); |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | void OatFileManager::DeleteThreadPool() { |
| 1019 | verification_thread_pool_.reset(nullptr); |
| 1020 | } |
| 1021 | |
| 1022 | void OatFileManager::WaitForBackgroundVerificationTasks() { |
| 1023 | if (verification_thread_pool_ != nullptr) { |
| 1024 | Thread* const self = Thread::Current(); |
| 1025 | verification_thread_pool_->WaitForWorkersToBeCreated(); |
| 1026 | verification_thread_pool_->Wait(self, /* do_work= */ true, /* may_hold_locks= */ false); |
| 1027 | } |
| 1028 | } |
| 1029 | |
Andreas Gampe | 9ef308d | 2019-03-28 11:06:26 -0700 | [diff] [blame] | 1030 | void OatFileManager::SetOnlyUseSystemOatFiles(bool enforce, bool assert_no_files_loaded) { |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 1031 | ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
Andreas Gampe | 9ef308d | 2019-03-28 11:06:26 -0700 | [diff] [blame] | 1032 | if (!only_use_system_oat_files_ && enforce && assert_no_files_loaded) { |
Andreas Gampe | 99c0795 | 2019-03-30 17:43:55 +0000 | [diff] [blame] | 1033 | // Make sure all files that were loaded up to this point are on /system. Skip the image |
| 1034 | // files. |
| 1035 | std::vector<const OatFile*> boot_vector = GetBootOatFiles(); |
| 1036 | std::unordered_set<const OatFile*> boot_set(boot_vector.begin(), boot_vector.end()); |
| 1037 | |
| 1038 | for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) { |
| 1039 | if (boot_set.find(oat_file.get()) == boot_set.end()) { |
| 1040 | CHECK(LocationIsOnSystem(oat_file->GetLocation().c_str())) << oat_file->GetLocation(); |
| 1041 | } |
| 1042 | } |
Andreas Gampe | 3383f9c | 2019-03-15 11:46:08 -0700 | [diff] [blame] | 1043 | } |
Andreas Gampe | 9ef308d | 2019-03-28 11:06:26 -0700 | [diff] [blame] | 1044 | only_use_system_oat_files_ = enforce; |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 1045 | } |
| 1046 | |
Nicolas Geoffray | 04680f3 | 2016-03-17 11:56:54 +0000 | [diff] [blame] | 1047 | void OatFileManager::DumpForSigQuit(std::ostream& os) { |
| 1048 | ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
| 1049 | std::vector<const OatFile*> boot_oat_files = GetBootOatFiles(); |
| 1050 | for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) { |
| 1051 | if (ContainsElement(boot_oat_files, oat_file.get())) { |
| 1052 | continue; |
| 1053 | } |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 1054 | os << oat_file->GetLocation() << ": " << oat_file->GetCompilerFilter() << "\n"; |
Nicolas Geoffray | 04680f3 | 2016-03-17 11:56:54 +0000 | [diff] [blame] | 1055 | } |
| 1056 | } |
| 1057 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1058 | } // namespace art |