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