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 | |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 172 | static bool ClassLoaderContextMatches( |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 173 | const OatFile* oat_file, |
| 174 | const ClassLoaderContext* context, |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 175 | bool* is_special_shared_library, |
| 176 | /*out*/ std::string* error_msg) { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 177 | DCHECK(oat_file != nullptr); |
| 178 | DCHECK(error_msg != nullptr); |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 179 | DCHECK(context != nullptr); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 180 | |
Calin Juravle | af32242 | 2020-02-11 13:45:53 -0800 | [diff] [blame] | 181 | if (!CompilerFilter::IsVerificationEnabled(oat_file->GetCompilerFilter())) { |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 182 | // If verification is not enabled we don't need to check if class loader context matches |
| 183 | // as the oat file is either extracted or assumed verified. |
| 184 | return true; |
Calin Juravle | af32242 | 2020-02-11 13:45:53 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 187 | // 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] | 188 | // the oat file without addition checks |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 189 | ClassLoaderContext::VerificationResult result = context->VerifyClassLoaderContextMatch( |
| 190 | oat_file->GetClassLoaderContext(), |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 191 | /*verify_names=*/ true, |
| 192 | /*verify_checksums=*/ true); |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 193 | switch (result) { |
| 194 | case ClassLoaderContext::VerificationResult::kForcedToSkipChecks: |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 195 | *is_special_shared_library = true; |
| 196 | return true; |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 197 | case ClassLoaderContext::VerificationResult::kMismatch: |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 198 | *is_special_shared_library = false; |
| 199 | return false; |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 200 | case ClassLoaderContext::VerificationResult::kVerifies: |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 201 | *is_special_shared_library = false; |
| 202 | return true; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 203 | } |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 204 | LOG(FATAL) << "Unreachable"; |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 205 | } |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 206 | |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 207 | bool OatFileManager::ShouldLoadAppImage(const OatFile* source_oat_file) const { |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 208 | Runtime* const runtime = Runtime::Current(); |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 209 | return kEnableAppImage && (!runtime->IsJavaDebuggable() || source_oat_file->IsDebuggable()); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | std::vector<std::unique_ptr<const DexFile>> OatFileManager::OpenDexFilesFromOat( |
| 213 | const char* dex_location, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 214 | jobject class_loader, |
| 215 | jobjectArray dex_elements, |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 216 | const OatFile** out_oat_file, |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 217 | std::vector<std::string>* error_msgs) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 218 | ScopedTrace trace(__FUNCTION__); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 219 | CHECK(dex_location != nullptr); |
| 220 | CHECK(error_msgs != nullptr); |
| 221 | |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 222 | // Verify we aren't holding the mutator lock, which could starve GC when |
| 223 | // hitting the disk. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 224 | Thread* const self = Thread::Current(); |
| 225 | Locks::mutator_lock_->AssertNotHeld(self); |
| 226 | Runtime* const runtime = Runtime::Current(); |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 227 | |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 228 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 229 | |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 230 | // If the class_loader is null there's not much we can do. This happens if a dex files is loaded |
| 231 | // directly with DexFile APIs instead of using class loaders. |
| 232 | if (class_loader == nullptr) { |
| 233 | LOG(WARNING) << "Opening an oat file without a class loader. " |
| 234 | << "Are you using the deprecated DexFile APIs?"; |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 235 | } else { |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 236 | std::unique_ptr<ClassLoaderContext> context( |
| 237 | ClassLoaderContext::CreateContextForClassLoader(class_loader, dex_elements)); |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 238 | |
Ulyana Trafimovich | 3060bb9 | 2020-07-16 14:17:11 +0000 | [diff] [blame^] | 239 | OatFileAssistant oat_file_assistant(dex_location, |
| 240 | kRuntimeISA, |
| 241 | runtime->GetOatFilesExecutable(), |
| 242 | only_use_system_oat_files_); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 243 | |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 244 | // Get the oat file on disk. |
| 245 | std::unique_ptr<const OatFile> oat_file(oat_file_assistant.GetBestOatFile().release()); |
| 246 | VLOG(oat) << "OatFileAssistant(" << dex_location << ").GetBestOatFile()=" |
| 247 | << reinterpret_cast<uintptr_t>(oat_file.get()) |
| 248 | << " (executable=" << (oat_file != nullptr ? oat_file->IsExecutable() : false) << ")"; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 249 | |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 250 | const OatFile* source_oat_file = nullptr; |
| 251 | std::string error_msg; |
| 252 | bool is_special_shared_library = false; |
| 253 | bool class_loader_context_matches = false; |
| 254 | if (oat_file != nullptr && |
| 255 | context != nullptr && |
| 256 | ClassLoaderContextMatches(oat_file.get(), |
| 257 | context.get(), |
| 258 | /*out*/ &is_special_shared_library, |
| 259 | /*out*/ &error_msg)) { |
| 260 | class_loader_context_matches = true; |
| 261 | // Load the dex files from the oat file. |
| 262 | bool added_image_space = false; |
| 263 | if (oat_file->IsExecutable()) { |
| 264 | ScopedTrace app_image_timing("AppImage:Loading"); |
Narayan Kamath | 5c52574 | 2017-04-28 10:19:29 +0100 | [diff] [blame] | 265 | |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 266 | // We need to throw away the image space if we are debuggable but the oat-file source of the |
| 267 | // image is not otherwise we might get classes with inlined methods or other such things. |
| 268 | std::unique_ptr<gc::space::ImageSpace> image_space; |
| 269 | if (!is_special_shared_library && ShouldLoadAppImage(oat_file.get())) { |
| 270 | image_space = oat_file_assistant.OpenImageSpace(oat_file.get()); |
Narayan Kamath | 5c52574 | 2017-04-28 10:19:29 +0100 | [diff] [blame] | 271 | } |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 272 | if (image_space != nullptr) { |
| 273 | ScopedObjectAccess soa(self); |
| 274 | StackHandleScope<1> hs(self); |
| 275 | Handle<mirror::ClassLoader> h_loader( |
| 276 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader))); |
| 277 | // Can not load app image without class loader. |
| 278 | if (h_loader != nullptr) { |
| 279 | std::string temp_error_msg; |
| 280 | // Add image space has a race condition since other threads could be reading from the |
| 281 | // spaces array. |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 282 | { |
| 283 | ScopedThreadSuspension sts(self, kSuspended); |
Mathieu Chartier | 61d2b2d | 2016-02-04 13:31:46 -0800 | [diff] [blame] | 284 | gc::ScopedGCCriticalSection gcs(self, |
| 285 | gc::kGcCauseAddRemoveAppImageSpace, |
| 286 | gc::kCollectorTypeAddRemoveAppImageSpace); |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 287 | ScopedSuspendAll ssa("Add image space"); |
| 288 | runtime->GetHeap()->AddSpace(image_space.get()); |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 289 | } |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 290 | { |
| 291 | ScopedTrace image_space_timing( |
| 292 | StringPrintf("Adding image space for location %s", dex_location)); |
| 293 | added_image_space = runtime->GetClassLinker()->AddImageSpace(image_space.get(), |
| 294 | h_loader, |
| 295 | /*out*/&dex_files, |
| 296 | /*out*/&temp_error_msg); |
| 297 | } |
| 298 | if (added_image_space) { |
| 299 | // Successfully added image space to heap, release the map so that it does not get |
| 300 | // freed. |
| 301 | image_space.release(); // NOLINT b/117926937 |
| 302 | |
| 303 | // Register for tracking. |
| 304 | for (const auto& dex_file : dex_files) { |
| 305 | dex::tracking::RegisterDexFile(dex_file.get()); |
| 306 | } |
| 307 | } else { |
| 308 | LOG(INFO) << "Failed to add image file " << temp_error_msg; |
| 309 | dex_files.clear(); |
| 310 | { |
| 311 | ScopedThreadSuspension sts(self, kSuspended); |
| 312 | gc::ScopedGCCriticalSection gcs(self, |
| 313 | gc::kGcCauseAddRemoveAppImageSpace, |
| 314 | gc::kCollectorTypeAddRemoveAppImageSpace); |
| 315 | ScopedSuspendAll ssa("Remove image space"); |
| 316 | runtime->GetHeap()->RemoveSpace(image_space.get()); |
| 317 | } |
| 318 | // Non-fatal, don't update error_msg. |
| 319 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 320 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 321 | } |
| 322 | } |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 323 | if (!added_image_space) { |
| 324 | DCHECK(dex_files.empty()); |
Eric Holk | bc89ed4 | 2020-04-29 19:59:24 +0000 | [diff] [blame] | 325 | |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 326 | if (oat_file->RequiresImage()) { |
| 327 | VLOG(oat) << "Loading " |
| 328 | << oat_file->GetLocation() |
| 329 | << "non-executable as it requires an image which we failed to load"; |
| 330 | // file as non-executable. |
| 331 | OatFileAssistant nonexecutable_oat_file_assistant(dex_location, |
| 332 | kRuntimeISA, |
| 333 | /*load_executable=*/false, |
| 334 | only_use_system_oat_files_); |
| 335 | oat_file.reset(nonexecutable_oat_file_assistant.GetBestOatFile().release()); |
| 336 | } |
| 337 | |
| 338 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file.get(), dex_location); |
| 339 | |
| 340 | // Register for tracking. |
| 341 | for (const auto& dex_file : dex_files) { |
| 342 | dex::tracking::RegisterDexFile(dex_file.get()); |
| 343 | } |
| 344 | } |
| 345 | if (dex_files.empty()) { |
| 346 | error_msgs->push_back("Failed to open dex files from " + oat_file->GetLocation()); |
| 347 | } else { |
| 348 | // Opened dex files from an oat file, madvise them to their loaded state. |
| 349 | for (const std::unique_ptr<const DexFile>& dex_file : dex_files) { |
| 350 | OatDexFile::MadviseDexFile(*dex_file, MadviseState::kMadviseStateAtLoad); |
| 351 | } |
Eric Holk | bc89ed4 | 2020-04-29 19:59:24 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 354 | VLOG(class_linker) << "Registering " << oat_file->GetLocation(); |
| 355 | source_oat_file = RegisterOatFile(std::move(oat_file)); |
| 356 | *out_oat_file = source_oat_file; |
| 357 | } else if (!error_msg.empty()) { |
| 358 | LOG(WARNING) << error_msg; |
| 359 | } |
Bharadwaj Kalandhabhatta | 0bb4031 | 2017-06-01 10:47:00 -0700 | [diff] [blame] | 360 | |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 361 | // Verify if any of the dex files being loaded is already in the class path. |
| 362 | // If so, report an error with the current stack trace. |
| 363 | // Most likely the developer didn't intend to do this because it will waste |
| 364 | // performance and memory. |
| 365 | if (context != nullptr && !class_loader_context_matches) { |
| 366 | std::set<const DexFile*> already_exists_in_classpath = |
| 367 | context->CheckForDuplicateDexFiles(MakeNonOwningPointerVector(dex_files)); |
| 368 | if (!already_exists_in_classpath.empty()) { |
| 369 | auto duplicate_it = already_exists_in_classpath.begin(); |
| 370 | std::string duplicates = (*duplicate_it)->GetLocation(); |
| 371 | for (duplicate_it++ ; duplicate_it != already_exists_in_classpath.end(); duplicate_it++) { |
| 372 | duplicates += "," + (*duplicate_it)->GetLocation(); |
| 373 | } |
| 374 | |
| 375 | std::ostringstream out; |
| 376 | out << "Trying to load dex files which is already loaded in the same ClassLoader " |
| 377 | << "hierarchy.\n" |
| 378 | << "This is a strong indication of bad ClassLoader construct which leads to poor " |
| 379 | << "performance and wastes memory.\n" |
| 380 | << "The list of duplicate dex files is: " << duplicates << "\n" |
| 381 | << "The current class loader context is: " |
| 382 | << context->EncodeContextForOatFile("") << "\n" |
| 383 | << "Java stack trace:\n"; |
| 384 | |
| 385 | { |
| 386 | ScopedObjectAccess soa(self); |
| 387 | self->DumpJavaStack(out); |
| 388 | } |
| 389 | |
| 390 | // We log this as an ERROR to stress the fact that this is most likely unintended. |
| 391 | // Note that ART cannot do anything about it. It is up to the app to fix their logic. |
| 392 | // Here we are trying to give a heads up on why the app might have performance issues. |
| 393 | LOG(ERROR) << out.str(); |
Bharadwaj Kalandhabhatta | 0bb4031 | 2017-06-01 10:47:00 -0700 | [diff] [blame] | 394 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 395 | } |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 398 | // If we arrive here with an empty dex files list, it means we fail to load |
| 399 | // it/them through an .oat file. |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 400 | if (dex_files.empty()) { |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 401 | std::string error_msg; |
| 402 | static constexpr bool kVerifyChecksum = true; |
| 403 | const ArtDexFileLoader dex_file_loader; |
| 404 | if (!dex_file_loader.Open(dex_location, |
| 405 | dex_location, |
| 406 | Runtime::Current()->IsVerificationEnabled(), |
| 407 | kVerifyChecksum, |
| 408 | /*out*/ &error_msg, |
| 409 | &dex_files)) { |
| 410 | LOG(WARNING) << error_msg; |
| 411 | error_msgs->push_back("Failed to open dex files from " + std::string(dex_location) |
| 412 | + " because: " + error_msg); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 413 | } |
| 414 | } |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 415 | |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 416 | if (Runtime::Current()->GetJit() != nullptr) { |
Nicolas Geoffray | 741a070 | 2019-06-10 11:18:11 +0100 | [diff] [blame] | 417 | Runtime::Current()->GetJit()->RegisterDexFiles(dex_files, class_loader); |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 418 | } |
| 419 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 420 | return dex_files; |
| 421 | } |
| 422 | |
David Brazdil | 7126c5b | 2019-03-05 00:02:51 +0000 | [diff] [blame] | 423 | static std::vector<const DexFile::Header*> GetDexFileHeaders(const std::vector<MemMap>& maps) { |
| 424 | std::vector<const DexFile::Header*> headers; |
| 425 | headers.reserve(maps.size()); |
| 426 | for (const MemMap& map : maps) { |
| 427 | DCHECK(map.IsValid()); |
| 428 | headers.push_back(reinterpret_cast<const DexFile::Header*>(map.Begin())); |
| 429 | } |
| 430 | return headers; |
| 431 | } |
| 432 | |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 433 | static std::vector<const DexFile::Header*> GetDexFileHeaders( |
| 434 | const std::vector<const DexFile*>& dex_files) { |
| 435 | std::vector<const DexFile::Header*> headers; |
| 436 | headers.reserve(dex_files.size()); |
| 437 | for (const DexFile* dex_file : dex_files) { |
| 438 | headers.push_back(&dex_file->GetHeader()); |
| 439 | } |
| 440 | return headers; |
| 441 | } |
| 442 | |
David Brazdil | 7126c5b | 2019-03-05 00:02:51 +0000 | [diff] [blame] | 443 | std::vector<std::unique_ptr<const DexFile>> OatFileManager::OpenDexFilesFromOat( |
| 444 | std::vector<MemMap>&& dex_mem_maps, |
| 445 | jobject class_loader, |
| 446 | jobjectArray dex_elements, |
| 447 | const OatFile** out_oat_file, |
| 448 | std::vector<std::string>* error_msgs) { |
| 449 | std::vector<std::unique_ptr<const DexFile>> dex_files = OpenDexFilesFromOat_Impl( |
| 450 | std::move(dex_mem_maps), |
| 451 | class_loader, |
| 452 | dex_elements, |
| 453 | out_oat_file, |
| 454 | error_msgs); |
| 455 | |
| 456 | if (error_msgs->empty()) { |
| 457 | // Remove write permission from DexFile pages. We do this at the end because |
| 458 | // OatFile assigns OatDexFile pointer in the DexFile objects. |
| 459 | for (std::unique_ptr<const DexFile>& dex_file : dex_files) { |
| 460 | if (!dex_file->DisableWrite()) { |
| 461 | error_msgs->push_back("Failed to make dex file " + dex_file->GetLocation() + " read-only"); |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | if (!error_msgs->empty()) { |
| 467 | return std::vector<std::unique_ptr<const DexFile>>(); |
| 468 | } |
| 469 | |
| 470 | return dex_files; |
| 471 | } |
| 472 | |
| 473 | std::vector<std::unique_ptr<const DexFile>> OatFileManager::OpenDexFilesFromOat_Impl( |
| 474 | std::vector<MemMap>&& dex_mem_maps, |
| 475 | jobject class_loader, |
| 476 | jobjectArray dex_elements, |
| 477 | const OatFile** out_oat_file, |
| 478 | std::vector<std::string>* error_msgs) { |
| 479 | ScopedTrace trace(__FUNCTION__); |
| 480 | std::string error_msg; |
| 481 | DCHECK(error_msgs != nullptr); |
| 482 | |
| 483 | // Extract dex file headers from `dex_mem_maps`. |
| 484 | const std::vector<const DexFile::Header*> dex_headers = GetDexFileHeaders(dex_mem_maps); |
| 485 | |
| 486 | // Determine dex/vdex locations and the combined location checksum. |
| 487 | uint32_t location_checksum; |
| 488 | std::string dex_location; |
| 489 | std::string vdex_path; |
| 490 | bool has_vdex = OatFileAssistant::AnonymousDexVdexLocation(dex_headers, |
| 491 | kRuntimeISA, |
| 492 | &location_checksum, |
| 493 | &dex_location, |
| 494 | &vdex_path); |
| 495 | |
| 496 | // Attempt to open an existing vdex and check dex file checksums match. |
| 497 | std::unique_ptr<VdexFile> vdex_file = nullptr; |
| 498 | if (has_vdex && OS::FileExists(vdex_path.c_str())) { |
| 499 | vdex_file = VdexFile::Open(vdex_path, |
| 500 | /* writable= */ false, |
| 501 | /* low_4gb= */ false, |
| 502 | /* unquicken= */ false, |
| 503 | &error_msg); |
| 504 | if (vdex_file == nullptr) { |
| 505 | LOG(WARNING) << "Failed to open vdex " << vdex_path << ": " << error_msg; |
| 506 | } else if (!vdex_file->MatchesDexFileChecksums(dex_headers)) { |
| 507 | LOG(WARNING) << "Failed to open vdex " << vdex_path << ": dex file checksum mismatch"; |
| 508 | vdex_file.reset(nullptr); |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | // Load dex files. Skip structural dex file verification if vdex was found |
| 513 | // and dex checksums matched. |
| 514 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 515 | for (size_t i = 0; i < dex_mem_maps.size(); ++i) { |
| 516 | static constexpr bool kVerifyChecksum = true; |
| 517 | const ArtDexFileLoader dex_file_loader; |
| 518 | std::unique_ptr<const DexFile> dex_file(dex_file_loader.Open( |
| 519 | DexFileLoader::GetMultiDexLocation(i, dex_location.c_str()), |
| 520 | location_checksum, |
| 521 | std::move(dex_mem_maps[i]), |
| 522 | /* verify= */ (vdex_file == nullptr) && Runtime::Current()->IsVerificationEnabled(), |
| 523 | kVerifyChecksum, |
| 524 | &error_msg)); |
| 525 | if (dex_file != nullptr) { |
| 526 | dex::tracking::RegisterDexFile(dex_file.get()); // Register for tracking. |
| 527 | dex_files.push_back(std::move(dex_file)); |
| 528 | } else { |
| 529 | error_msgs->push_back("Failed to open dex files from memory: " + error_msg); |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | // Check if we should proceed to creating an OatFile instance backed by the vdex. |
| 534 | // We need: (a) an existing vdex, (b) class loader (can be null if invoked via reflection), |
| 535 | // and (c) no errors during dex file loading. |
| 536 | if (vdex_file == nullptr || class_loader == nullptr || !error_msgs->empty()) { |
| 537 | return dex_files; |
| 538 | } |
| 539 | |
| 540 | // Attempt to create a class loader context, check OpenDexFiles succeeds (prerequisite |
| 541 | // for using the context later). |
| 542 | std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::CreateContextForClassLoader( |
| 543 | class_loader, |
| 544 | dex_elements); |
| 545 | if (context == nullptr) { |
| 546 | LOG(ERROR) << "Could not create class loader context for " << vdex_path; |
| 547 | return dex_files; |
| 548 | } |
| 549 | DCHECK(context->OpenDexFiles(kRuntimeISA, "")) |
| 550 | << "Context created from already opened dex files should not attempt to open again"; |
| 551 | |
| 552 | // Check that we can use the vdex against this boot class path and in this class loader context. |
| 553 | // Note 1: We do not need a class loader collision check because there is no compiled code. |
| 554 | // Note 2: If these checks fail, we cannot fast-verify because the vdex does not contain |
| 555 | // full VerifierDeps. |
| 556 | if (!vdex_file->MatchesBootClassPathChecksums() || |
| 557 | !vdex_file->MatchesClassLoaderContext(*context.get())) { |
| 558 | return dex_files; |
| 559 | } |
| 560 | |
| 561 | // Initialize an OatFile instance backed by the loaded vdex. |
| 562 | std::unique_ptr<OatFile> oat_file(OatFile::OpenFromVdex(MakeNonOwningPointerVector(dex_files), |
| 563 | std::move(vdex_file), |
| 564 | dex_location)); |
Vladimir Marko | c390879 | 2020-04-06 14:52:04 +0100 | [diff] [blame] | 565 | if (oat_file != nullptr) { |
| 566 | VLOG(class_linker) << "Registering " << oat_file->GetLocation(); |
| 567 | *out_oat_file = RegisterOatFile(std::move(oat_file)); |
| 568 | } |
David Brazdil | 7126c5b | 2019-03-05 00:02:51 +0000 | [diff] [blame] | 569 | return dex_files; |
| 570 | } |
| 571 | |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 572 | // Check how many vdex files exist in the same directory as the vdex file we are about |
| 573 | // to write. If more than or equal to kAnonymousVdexCacheSize, unlink the least |
| 574 | // recently used one(s) (according to stat-reported atime). |
| 575 | static bool UnlinkLeastRecentlyUsedVdexIfNeeded(const std::string& vdex_path_to_add, |
| 576 | std::string* error_msg) { |
| 577 | if (OS::FileExists(vdex_path_to_add.c_str())) { |
| 578 | // File already exists and will be overwritten. |
| 579 | // This will not change the number of entries in the cache. |
| 580 | return true; |
| 581 | } |
| 582 | |
| 583 | auto last_slash = vdex_path_to_add.rfind('/'); |
| 584 | CHECK(last_slash != std::string::npos); |
| 585 | std::string vdex_dir = vdex_path_to_add.substr(0, last_slash + 1); |
| 586 | |
| 587 | if (!OS::DirectoryExists(vdex_dir.c_str())) { |
| 588 | // Folder does not exist yet. Cache has zero entries. |
| 589 | return true; |
| 590 | } |
| 591 | |
| 592 | std::vector<std::pair<time_t, std::string>> cache; |
| 593 | |
| 594 | DIR* c_dir = opendir(vdex_dir.c_str()); |
| 595 | if (c_dir == nullptr) { |
| 596 | *error_msg = "Unable to open " + vdex_dir + " to delete unused vdex files"; |
| 597 | return false; |
| 598 | } |
| 599 | for (struct dirent* de = readdir(c_dir); de != nullptr; de = readdir(c_dir)) { |
| 600 | if (de->d_type != DT_REG) { |
| 601 | continue; |
| 602 | } |
| 603 | std::string basename = de->d_name; |
| 604 | if (!OatFileAssistant::IsAnonymousVdexBasename(basename)) { |
| 605 | continue; |
| 606 | } |
| 607 | std::string fullname = vdex_dir + basename; |
| 608 | |
| 609 | struct stat s; |
| 610 | int rc = TEMP_FAILURE_RETRY(stat(fullname.c_str(), &s)); |
| 611 | if (rc == -1) { |
| 612 | *error_msg = "Failed to stat() anonymous vdex file " + fullname; |
| 613 | return false; |
| 614 | } |
| 615 | |
| 616 | cache.push_back(std::make_pair(s.st_atime, fullname)); |
| 617 | } |
| 618 | CHECK_EQ(0, closedir(c_dir)) << "Unable to close directory."; |
| 619 | |
| 620 | if (cache.size() < OatFileManager::kAnonymousVdexCacheSize) { |
| 621 | return true; |
| 622 | } |
| 623 | |
| 624 | std::sort(cache.begin(), |
| 625 | cache.end(), |
| 626 | [](const auto& a, const auto& b) { return a.first < b.first; }); |
| 627 | for (size_t i = OatFileManager::kAnonymousVdexCacheSize - 1; i < cache.size(); ++i) { |
| 628 | if (unlink(cache[i].second.c_str()) != 0) { |
| 629 | *error_msg = "Could not unlink anonymous vdex file " + cache[i].second; |
| 630 | return false; |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | return true; |
| 635 | } |
| 636 | |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 637 | class BackgroundVerificationTask final : public Task { |
| 638 | public: |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 639 | BackgroundVerificationTask(const std::vector<const DexFile*>& dex_files, |
| 640 | jobject class_loader, |
| 641 | const char* class_loader_context, |
| 642 | const std::string& vdex_path) |
| 643 | : dex_files_(dex_files), |
| 644 | class_loader_context_(class_loader_context), |
| 645 | vdex_path_(vdex_path) { |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 646 | Thread* const self = Thread::Current(); |
| 647 | ScopedObjectAccess soa(self); |
| 648 | // Create a global ref for `class_loader` because it will be accessed from a different thread. |
| 649 | class_loader_ = soa.Vm()->AddGlobalRef(self, soa.Decode<mirror::ClassLoader>(class_loader)); |
| 650 | CHECK(class_loader_ != nullptr); |
| 651 | } |
| 652 | |
| 653 | ~BackgroundVerificationTask() { |
| 654 | Thread* const self = Thread::Current(); |
| 655 | ScopedObjectAccess soa(self); |
| 656 | soa.Vm()->DeleteGlobalRef(self, class_loader_); |
| 657 | } |
| 658 | |
| 659 | void Run(Thread* self) override { |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 660 | std::string error_msg; |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 661 | ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 662 | verifier::VerifierDeps verifier_deps(dex_files_); |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 663 | |
| 664 | // Iterate over all classes and verify them. |
| 665 | for (const DexFile* dex_file : dex_files_) { |
| 666 | 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] | 667 | const dex::ClassDef& class_def = dex_file->GetClassDef(cdef_idx); |
| 668 | |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 669 | // Take handles inside the loop. The background verification is low priority |
| 670 | // and we want to minimize the risk of blocking anyone else. |
| 671 | ScopedObjectAccess soa(self); |
| 672 | StackHandleScope<2> hs(self); |
| 673 | Handle<mirror::ClassLoader> h_loader(hs.NewHandle( |
| 674 | soa.Decode<mirror::ClassLoader>(class_loader_))); |
| 675 | Handle<mirror::Class> h_class(hs.NewHandle<mirror::Class>(class_linker->FindClass( |
| 676 | self, |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 677 | dex_file->GetClassDescriptor(class_def), |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 678 | h_loader))); |
| 679 | |
| 680 | if (h_class == nullptr) { |
| 681 | CHECK(self->IsExceptionPending()); |
| 682 | self->ClearException(); |
| 683 | continue; |
| 684 | } |
| 685 | |
| 686 | if (&h_class->GetDexFile() != dex_file) { |
| 687 | // There is a different class in the class path or a parent class loader |
| 688 | // with the same descriptor. This `h_class` is not resolvable, skip it. |
| 689 | continue; |
| 690 | } |
| 691 | |
| 692 | CHECK(h_class->IsResolved()) << h_class->PrettyDescriptor(); |
| 693 | class_linker->VerifyClass(self, h_class); |
| 694 | if (h_class->IsErroneous()) { |
| 695 | // ClassLinker::VerifyClass throws, which isn't useful here. |
| 696 | CHECK(soa.Self()->IsExceptionPending()); |
| 697 | soa.Self()->ClearException(); |
| 698 | } |
| 699 | |
| 700 | CHECK(h_class->IsVerified() || h_class->IsErroneous()) |
| 701 | << h_class->PrettyDescriptor() << ": state=" << h_class->GetStatus(); |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 702 | |
| 703 | if (h_class->IsVerified()) { |
| 704 | verifier_deps.RecordClassVerified(*dex_file, class_def); |
| 705 | } |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 706 | } |
| 707 | } |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 708 | |
| 709 | // Delete old vdex files if there are too many in the folder. |
| 710 | if (!UnlinkLeastRecentlyUsedVdexIfNeeded(vdex_path_, &error_msg)) { |
| 711 | LOG(ERROR) << "Could not unlink old vdex files " << vdex_path_ << ": " << error_msg; |
| 712 | return; |
| 713 | } |
| 714 | |
| 715 | // Construct a vdex file and write `verifier_deps` into it. |
| 716 | if (!VdexFile::WriteToDisk(vdex_path_, |
| 717 | dex_files_, |
| 718 | verifier_deps, |
| 719 | class_loader_context_, |
| 720 | &error_msg)) { |
| 721 | LOG(ERROR) << "Could not write anonymous vdex " << vdex_path_ << ": " << error_msg; |
| 722 | return; |
| 723 | } |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | void Finalize() override { |
| 727 | delete this; |
| 728 | } |
| 729 | |
| 730 | private: |
| 731 | const std::vector<const DexFile*> dex_files_; |
| 732 | jobject class_loader_; |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 733 | const std::string class_loader_context_; |
| 734 | const std::string vdex_path_; |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 735 | |
| 736 | DISALLOW_COPY_AND_ASSIGN(BackgroundVerificationTask); |
| 737 | }; |
| 738 | |
| 739 | void OatFileManager::RunBackgroundVerification(const std::vector<const DexFile*>& dex_files, |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 740 | jobject class_loader, |
| 741 | const char* class_loader_context) { |
David Brazdil | 527072e | 2019-04-03 15:15:40 +0100 | [diff] [blame] | 742 | Runtime* const runtime = Runtime::Current(); |
| 743 | Thread* const self = Thread::Current(); |
| 744 | |
| 745 | if (runtime->IsJavaDebuggable()) { |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 746 | // Threads created by ThreadPool ("runtime threads") are not allowed to load |
| 747 | // classes when debuggable to match class-initialization semantics |
| 748 | // expectations. Do not verify in the background. |
| 749 | return; |
| 750 | } |
| 751 | |
David Brazdil | 527072e | 2019-04-03 15:15:40 +0100 | [diff] [blame] | 752 | if (!IsSdkVersionSetAndAtLeast(runtime->GetTargetSdkVersion(), SdkVersion::kQ)) { |
| 753 | // Do not run for legacy apps as they may depend on the previous class loader behaviour. |
| 754 | return; |
| 755 | } |
| 756 | |
| 757 | if (runtime->IsShuttingDown(self)) { |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 758 | // Not allowed to create new threads during runtime shutdown. |
| 759 | return; |
| 760 | } |
| 761 | |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 762 | uint32_t location_checksum; |
| 763 | std::string dex_location; |
| 764 | std::string vdex_path; |
| 765 | if (OatFileAssistant::AnonymousDexVdexLocation(GetDexFileHeaders(dex_files), |
| 766 | kRuntimeISA, |
| 767 | &location_checksum, |
| 768 | &dex_location, |
| 769 | &vdex_path)) { |
| 770 | if (verification_thread_pool_ == nullptr) { |
| 771 | verification_thread_pool_.reset( |
| 772 | new ThreadPool("Verification thread pool", /* num_threads= */ 1)); |
| 773 | verification_thread_pool_->StartWorkers(self); |
| 774 | } |
| 775 | verification_thread_pool_->AddTask(self, new BackgroundVerificationTask( |
| 776 | dex_files, |
| 777 | class_loader, |
| 778 | class_loader_context, |
| 779 | vdex_path)); |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 780 | } |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | void OatFileManager::WaitForWorkersToBeCreated() { |
| 784 | DCHECK(!Runtime::Current()->IsShuttingDown(Thread::Current())) |
| 785 | << "Cannot create new threads during runtime shutdown"; |
| 786 | if (verification_thread_pool_ != nullptr) { |
| 787 | verification_thread_pool_->WaitForWorkersToBeCreated(); |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | void OatFileManager::DeleteThreadPool() { |
| 792 | verification_thread_pool_.reset(nullptr); |
| 793 | } |
| 794 | |
| 795 | void OatFileManager::WaitForBackgroundVerificationTasks() { |
| 796 | if (verification_thread_pool_ != nullptr) { |
| 797 | Thread* const self = Thread::Current(); |
| 798 | verification_thread_pool_->WaitForWorkersToBeCreated(); |
| 799 | verification_thread_pool_->Wait(self, /* do_work= */ true, /* may_hold_locks= */ false); |
| 800 | } |
| 801 | } |
| 802 | |
Nicolas Geoffray | 2e17cf4 | 2020-03-12 21:19:46 +0000 | [diff] [blame] | 803 | void OatFileManager::SetOnlyUseSystemOatFiles() { |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 804 | ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
Nicolas Geoffray | 2e17cf4 | 2020-03-12 21:19:46 +0000 | [diff] [blame] | 805 | // Make sure all files that were loaded up to this point are on /system. |
| 806 | // Skip the image files as they can encode locations that don't exist (eg not |
| 807 | // containing the arch in the path, or for JIT zygote /nonx/existent). |
| 808 | std::vector<const OatFile*> boot_vector = GetBootOatFiles(); |
| 809 | std::unordered_set<const OatFile*> boot_set(boot_vector.begin(), boot_vector.end()); |
Andreas Gampe | 99c0795 | 2019-03-30 17:43:55 +0000 | [diff] [blame] | 810 | |
Nicolas Geoffray | 2e17cf4 | 2020-03-12 21:19:46 +0000 | [diff] [blame] | 811 | for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) { |
| 812 | if (boot_set.find(oat_file.get()) == boot_set.end()) { |
| 813 | CHECK(LocationIsOnSystem(oat_file->GetLocation().c_str())) << oat_file->GetLocation(); |
Andreas Gampe | 99c0795 | 2019-03-30 17:43:55 +0000 | [diff] [blame] | 814 | } |
Andreas Gampe | 3383f9c | 2019-03-15 11:46:08 -0700 | [diff] [blame] | 815 | } |
Nicolas Geoffray | e5c48f7 | 2020-03-11 11:28:43 +0000 | [diff] [blame] | 816 | only_use_system_oat_files_ = true; |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 817 | } |
| 818 | |
Nicolas Geoffray | 04680f3 | 2016-03-17 11:56:54 +0000 | [diff] [blame] | 819 | void OatFileManager::DumpForSigQuit(std::ostream& os) { |
| 820 | ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
| 821 | std::vector<const OatFile*> boot_oat_files = GetBootOatFiles(); |
| 822 | for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) { |
| 823 | if (ContainsElement(boot_oat_files, oat_file.get())) { |
| 824 | continue; |
| 825 | } |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 826 | os << oat_file->GetLocation() << ": " << oat_file->GetCompilerFilter() << "\n"; |
Nicolas Geoffray | 04680f3 | 2016-03-17 11:56:54 +0000 | [diff] [blame] | 827 | } |
| 828 | } |
| 829 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 830 | } // namespace art |