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 | |
Nicolas Geoffray | 9bc364b | 2021-03-29 10:41:39 +0100 | [diff] [blame] | 24 | #include "android-base/file.h" |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 25 | #include "android-base/stringprintf.h" |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 26 | #include "android-base/strings.h" |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 27 | |
Andreas Gampe | 90b936d | 2017-01-31 08:58:55 -0800 | [diff] [blame] | 28 | #include "art_field-inl.h" |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 29 | #include "base/bit_vector-inl.h" |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 30 | #include "base/file_utils.h" |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 31 | #include "base/logging.h" // For VLOG. |
Andreas Gampe | 7fbc4a5 | 2018-11-28 08:26:47 -0800 | [diff] [blame] | 32 | #include "base/mutex-inl.h" |
David Brazdil | 527072e | 2019-04-03 15:15:40 +0100 | [diff] [blame] | 33 | #include "base/sdk_version.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 34 | #include "base/stl_util.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 35 | #include "base/systrace.h" |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 36 | #include "class_linker.h" |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 37 | #include "class_loader_context.h" |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 38 | #include "dex/art_dex_file_loader.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 39 | #include "dex/dex_file-inl.h" |
| 40 | #include "dex/dex_file_loader.h" |
| 41 | #include "dex/dex_file_tracking_registrar.h" |
Mathieu Chartier | 61d2b2d | 2016-02-04 13:31:46 -0800 | [diff] [blame] | 42 | #include "gc/scoped_gc_critical_section.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 43 | #include "gc/space/image_space.h" |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 44 | #include "handle_scope-inl.h" |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 45 | #include "jit/jit.h" |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 46 | #include "jni/java_vm_ext.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 47 | #include "jni/jni_internal.h" |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 48 | #include "mirror/class_loader.h" |
Andreas Gampe | 90b936d | 2017-01-31 08:58:55 -0800 | [diff] [blame] | 49 | #include "mirror/object-inl.h" |
Alex Light | 2ce6fc8 | 2017-12-18 16:42:36 -0800 | [diff] [blame] | 50 | #include "oat_file.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 51 | #include "oat_file_assistant.h" |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 52 | #include "obj_ptr-inl.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 53 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 54 | #include "thread-current-inl.h" |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 55 | #include "thread_list.h" |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 56 | #include "thread_pool.h" |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 57 | #include "vdex_file.h" |
| 58 | #include "verifier/verifier_deps.h" |
Andreas Gampe | 90b936d | 2017-01-31 08:58:55 -0800 | [diff] [blame] | 59 | #include "well_known_classes.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 60 | |
| 61 | namespace art { |
| 62 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 63 | using android::base::StringPrintf; |
| 64 | |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 65 | // If true, we attempt to load the application image if it exists. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 66 | static constexpr bool kEnableAppImage = true; |
| 67 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 68 | const OatFile* OatFileManager::RegisterOatFile(std::unique_ptr<const OatFile> oat_file) { |
Nicolas Geoffray | 1d4f009 | 2020-08-07 14:01:05 +0100 | [diff] [blame] | 69 | // Use class_linker vlog to match the log for dex file registration. |
| 70 | VLOG(class_linker) << "Registered oat file " << oat_file->GetLocation(); |
Nicolas Geoffray | 4f6bb44 | 2021-06-02 18:05:51 +0100 | [diff] [blame] | 71 | PaletteNotifyOatFileLoaded(oat_file->GetLocation().c_str()); |
Nicolas Geoffray | 1d4f009 | 2020-08-07 14:01:05 +0100 | [diff] [blame] | 72 | |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 73 | WriterMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 74 | CHECK(!only_use_system_oat_files_ || |
Orion Hodson | 094b1cf | 2021-06-08 09:28:28 +0100 | [diff] [blame] | 75 | LocationIsTrusted(oat_file->GetLocation()) || |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 76 | !oat_file->IsExecutable()) |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 77 | << "Registering a non /system oat file: " << oat_file->GetLocation(); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 78 | DCHECK(oat_file != nullptr); |
| 79 | if (kIsDebugBuild) { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 80 | CHECK(oat_files_.find(oat_file) == oat_files_.end()); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 81 | for (const std::unique_ptr<const OatFile>& existing : oat_files_) { |
| 82 | CHECK_NE(oat_file.get(), existing.get()) << oat_file->GetLocation(); |
| 83 | // Check that we don't have an oat file with the same address. Copies of the same oat file |
| 84 | // should be loaded at different addresses. |
| 85 | CHECK_NE(oat_file->Begin(), existing->Begin()) << "Oat file already mapped at that location"; |
| 86 | } |
| 87 | } |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 88 | const OatFile* ret = oat_file.get(); |
| 89 | oat_files_.insert(std::move(oat_file)); |
| 90 | return ret; |
| 91 | } |
| 92 | |
| 93 | void OatFileManager::UnRegisterAndDeleteOatFile(const OatFile* oat_file) { |
| 94 | WriterMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
| 95 | DCHECK(oat_file != nullptr); |
| 96 | std::unique_ptr<const OatFile> compare(oat_file); |
| 97 | auto it = oat_files_.find(compare); |
| 98 | CHECK(it != oat_files_.end()); |
| 99 | oat_files_.erase(it); |
Andreas Gampe | afaf7f8 | 2018-10-16 11:32:38 -0700 | [diff] [blame] | 100 | compare.release(); // NOLINT b/117926937 |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Calin Juravle | 0b79127 | 2016-04-18 16:38:27 +0100 | [diff] [blame] | 103 | const OatFile* OatFileManager::FindOpenedOatFileFromDexLocation( |
| 104 | const std::string& dex_base_location) const { |
| 105 | ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
| 106 | for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) { |
| 107 | const std::vector<const OatDexFile*>& oat_dex_files = oat_file->GetOatDexFiles(); |
| 108 | for (const OatDexFile* oat_dex_file : oat_dex_files) { |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 109 | if (DexFileLoader::GetBaseLocation(oat_dex_file->GetDexFileLocation()) == dex_base_location) { |
Calin Juravle | 0b79127 | 2016-04-18 16:38:27 +0100 | [diff] [blame] | 110 | return oat_file.get(); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | return nullptr; |
| 115 | } |
| 116 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 117 | const OatFile* OatFileManager::FindOpenedOatFileFromOatLocation(const std::string& oat_location) |
| 118 | const { |
| 119 | ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 120 | return FindOpenedOatFileFromOatLocationLocked(oat_location); |
| 121 | } |
| 122 | |
| 123 | const OatFile* OatFileManager::FindOpenedOatFileFromOatLocationLocked( |
| 124 | const std::string& oat_location) const { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 125 | for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) { |
| 126 | if (oat_file->GetLocation() == oat_location) { |
| 127 | return oat_file.get(); |
| 128 | } |
| 129 | } |
| 130 | return nullptr; |
| 131 | } |
| 132 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 133 | std::vector<const OatFile*> OatFileManager::GetBootOatFiles() const { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 134 | std::vector<gc::space::ImageSpace*> image_spaces = |
| 135 | Runtime::Current()->GetHeap()->GetBootImageSpaces(); |
Andreas Gampe | 2a487eb | 2018-11-19 11:41:22 -0800 | [diff] [blame] | 136 | std::vector<const OatFile*> oat_files; |
| 137 | oat_files.reserve(image_spaces.size()); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 138 | for (gc::space::ImageSpace* image_space : image_spaces) { |
| 139 | oat_files.push_back(image_space->GetOatFile()); |
| 140 | } |
| 141 | return oat_files; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Nicolas Geoffray | 0744d72 | 2021-04-19 08:46:32 +0100 | [diff] [blame] | 144 | bool OatFileManager::GetPrimaryOatFileInfo(std::string* compilation_reason, |
| 145 | CompilerFilter::Filter* compiler_filter) const { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 146 | ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 147 | std::vector<const OatFile*> boot_oat_files = GetBootOatFiles(); |
| 148 | if (!boot_oat_files.empty()) { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 149 | for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 150 | if (std::find(boot_oat_files.begin(), boot_oat_files.end(), oat_file.get()) == |
| 151 | boot_oat_files.end()) { |
Nicolas Geoffray | 0744d72 | 2021-04-19 08:46:32 +0100 | [diff] [blame] | 152 | const char* reason = oat_file->GetCompilationReason(); |
| 153 | if (reason != nullptr) { |
| 154 | *compilation_reason = reason; |
| 155 | } |
| 156 | *compiler_filter = oat_file->GetCompilerFilter(); |
| 157 | return true; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | } |
Nicolas Geoffray | 0744d72 | 2021-04-19 08:46:32 +0100 | [diff] [blame] | 161 | return false; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Vladimir Marko | b0b68cf | 2017-11-14 18:11:50 +0000 | [diff] [blame] | 164 | OatFileManager::OatFileManager() |
Vladimir Marko | e066932 | 2018-09-03 15:44:54 +0100 | [diff] [blame] | 165 | : only_use_system_oat_files_(false) {} |
Vladimir Marko | b0b68cf | 2017-11-14 18:11:50 +0000 | [diff] [blame] | 166 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 167 | OatFileManager::~OatFileManager() { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 168 | // Explicitly clear oat_files_ since the OatFile destructor calls back into OatFileManager for |
| 169 | // UnRegisterOatFileLocation. |
| 170 | oat_files_.clear(); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 173 | std::vector<const OatFile*> OatFileManager::RegisterImageOatFiles( |
Stephen Hines | 48ba197 | 2018-09-24 13:35:54 -0700 | [diff] [blame] | 174 | const std::vector<gc::space::ImageSpace*>& spaces) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 175 | std::vector<const OatFile*> oat_files; |
Andreas Gampe | 2a487eb | 2018-11-19 11:41:22 -0800 | [diff] [blame] | 176 | oat_files.reserve(spaces.size()); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 177 | for (gc::space::ImageSpace* space : spaces) { |
| 178 | oat_files.push_back(RegisterOatFile(space->ReleaseOatFile())); |
| 179 | } |
| 180 | return oat_files; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 183 | bool OatFileManager::ShouldLoadAppImage(const OatFile* source_oat_file) const { |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 184 | Runtime* const runtime = Runtime::Current(); |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 185 | return kEnableAppImage && (!runtime->IsJavaDebuggable() || source_oat_file->IsDebuggable()); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | std::vector<std::unique_ptr<const DexFile>> OatFileManager::OpenDexFilesFromOat( |
| 189 | const char* dex_location, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 190 | jobject class_loader, |
| 191 | jobjectArray dex_elements, |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 192 | const OatFile** out_oat_file, |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 193 | std::vector<std::string>* error_msgs) { |
Calin Juravle | 9807115 | 2021-01-27 18:41:58 -0800 | [diff] [blame] | 194 | ScopedTrace trace(StringPrintf("%s(%s)", __FUNCTION__, dex_location)); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 195 | CHECK(dex_location != nullptr); |
| 196 | CHECK(error_msgs != nullptr); |
| 197 | |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 198 | // Verify we aren't holding the mutator lock, which could starve GC when |
| 199 | // hitting the disk. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 200 | Thread* const self = Thread::Current(); |
| 201 | Locks::mutator_lock_->AssertNotHeld(self); |
| 202 | Runtime* const runtime = Runtime::Current(); |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 203 | |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 204 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
Nicolas Geoffray | 525fa42 | 2021-04-19 07:50:35 +0000 | [diff] [blame] | 205 | std::unique_ptr<ClassLoaderContext> context( |
| 206 | ClassLoaderContext::CreateContextForClassLoader(class_loader, dex_elements)); |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 207 | |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 208 | // If the class_loader is null there's not much we can do. This happens if a dex files is loaded |
| 209 | // directly with DexFile APIs instead of using class loaders. |
| 210 | if (class_loader == nullptr) { |
| 211 | LOG(WARNING) << "Opening an oat file without a class loader. " |
| 212 | << "Are you using the deprecated DexFile APIs?"; |
Nicolas Geoffray | 525fa42 | 2021-04-19 07:50:35 +0000 | [diff] [blame] | 213 | } else if (context != nullptr) { |
Ulya Trafimovich | 5439f05 | 2020-07-29 10:03:46 +0100 | [diff] [blame] | 214 | OatFileAssistant oat_file_assistant(dex_location, |
| 215 | kRuntimeISA, |
Nicolas Geoffray | 525fa42 | 2021-04-19 07:50:35 +0000 | [diff] [blame] | 216 | context.get(), |
Ulya Trafimovich | 5439f05 | 2020-07-29 10:03:46 +0100 | [diff] [blame] | 217 | runtime->GetOatFilesExecutable(), |
| 218 | only_use_system_oat_files_); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 219 | |
Calin Juravle | 9807115 | 2021-01-27 18:41:58 -0800 | [diff] [blame] | 220 | // Get the current optimization status for trace debugging. |
| 221 | // Implementation detail note: GetOptimizationStatus will select the same |
| 222 | // oat file as GetBestOatFile used below, and in doing so it already pre-populates |
| 223 | // some OatFileAssistant internal fields. |
| 224 | std::string odex_location; |
| 225 | std::string compilation_filter; |
| 226 | std::string compilation_reason; |
| 227 | std::string odex_status; |
| 228 | oat_file_assistant.GetOptimizationStatus( |
| 229 | &odex_location, |
| 230 | &compilation_filter, |
| 231 | &compilation_reason, |
| 232 | &odex_status); |
| 233 | |
| 234 | ScopedTrace odex_loading(StringPrintf( |
| 235 | "location=%s status=%s filter=%s reason=%s", |
| 236 | odex_location.c_str(), |
| 237 | odex_status.c_str(), |
| 238 | compilation_filter.c_str(), |
| 239 | compilation_reason.c_str())); |
| 240 | |
| 241 | // Proceed with oat file loading. |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 242 | std::unique_ptr<const OatFile> oat_file(oat_file_assistant.GetBestOatFile().release()); |
| 243 | VLOG(oat) << "OatFileAssistant(" << dex_location << ").GetBestOatFile()=" |
Nicolas Geoffray | 16f60dc | 2021-02-16 15:35:16 +0000 | [diff] [blame] | 244 | << (oat_file != nullptr ? oat_file->GetLocation() : "") |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 245 | << " (executable=" << (oat_file != nullptr ? oat_file->IsExecutable() : false) << ")"; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 246 | |
Calin Juravle | 9807115 | 2021-01-27 18:41:58 -0800 | [diff] [blame] | 247 | CHECK(oat_file == nullptr || odex_location == oat_file->GetLocation()) |
| 248 | << "OatFileAssistant non-determinism in choosing best oat files. " |
| 249 | << "optimization-status-location=" << odex_location |
| 250 | << " best_oat_file-location=" << oat_file->GetLocation(); |
| 251 | |
Nicolas Geoffray | 525fa42 | 2021-04-19 07:50:35 +0000 | [diff] [blame] | 252 | if (oat_file != nullptr) { |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 253 | // Load the dex files from the oat file. |
| 254 | bool added_image_space = false; |
| 255 | if (oat_file->IsExecutable()) { |
| 256 | ScopedTrace app_image_timing("AppImage:Loading"); |
Narayan Kamath | 5c52574 | 2017-04-28 10:19:29 +0100 | [diff] [blame] | 257 | |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 258 | // We need to throw away the image space if we are debuggable but the oat-file source of the |
| 259 | // image is not otherwise we might get classes with inlined methods or other such things. |
| 260 | std::unique_ptr<gc::space::ImageSpace> image_space; |
Nicolas Geoffray | 982eced | 2021-03-29 08:49:38 +0100 | [diff] [blame] | 261 | if (ShouldLoadAppImage(oat_file.get())) { |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 262 | image_space = oat_file_assistant.OpenImageSpace(oat_file.get()); |
Narayan Kamath | 5c52574 | 2017-04-28 10:19:29 +0100 | [diff] [blame] | 263 | } |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 264 | if (image_space != nullptr) { |
| 265 | ScopedObjectAccess soa(self); |
| 266 | StackHandleScope<1> hs(self); |
| 267 | Handle<mirror::ClassLoader> h_loader( |
| 268 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader))); |
| 269 | // Can not load app image without class loader. |
| 270 | if (h_loader != nullptr) { |
| 271 | std::string temp_error_msg; |
| 272 | // Add image space has a race condition since other threads could be reading from the |
| 273 | // spaces array. |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 274 | { |
| 275 | ScopedThreadSuspension sts(self, kSuspended); |
Mathieu Chartier | 61d2b2d | 2016-02-04 13:31:46 -0800 | [diff] [blame] | 276 | gc::ScopedGCCriticalSection gcs(self, |
| 277 | gc::kGcCauseAddRemoveAppImageSpace, |
| 278 | gc::kCollectorTypeAddRemoveAppImageSpace); |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 279 | ScopedSuspendAll ssa("Add image space"); |
| 280 | runtime->GetHeap()->AddSpace(image_space.get()); |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 281 | } |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 282 | { |
Calin Juravle | 9807115 | 2021-01-27 18:41:58 -0800 | [diff] [blame] | 283 | ScopedTrace image_space_timing("Adding image space"); |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 284 | added_image_space = runtime->GetClassLinker()->AddImageSpace(image_space.get(), |
| 285 | h_loader, |
| 286 | /*out*/&dex_files, |
| 287 | /*out*/&temp_error_msg); |
| 288 | } |
| 289 | if (added_image_space) { |
| 290 | // Successfully added image space to heap, release the map so that it does not get |
| 291 | // freed. |
| 292 | image_space.release(); // NOLINT b/117926937 |
| 293 | |
| 294 | // Register for tracking. |
| 295 | for (const auto& dex_file : dex_files) { |
| 296 | dex::tracking::RegisterDexFile(dex_file.get()); |
| 297 | } |
| 298 | } else { |
| 299 | LOG(INFO) << "Failed to add image file " << temp_error_msg; |
| 300 | dex_files.clear(); |
| 301 | { |
| 302 | ScopedThreadSuspension sts(self, kSuspended); |
| 303 | gc::ScopedGCCriticalSection gcs(self, |
| 304 | gc::kGcCauseAddRemoveAppImageSpace, |
| 305 | gc::kCollectorTypeAddRemoveAppImageSpace); |
| 306 | ScopedSuspendAll ssa("Remove image space"); |
| 307 | runtime->GetHeap()->RemoveSpace(image_space.get()); |
| 308 | } |
| 309 | // Non-fatal, don't update error_msg. |
| 310 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 311 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 312 | } |
| 313 | } |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 314 | if (!added_image_space) { |
| 315 | DCHECK(dex_files.empty()); |
Eric Holk | bc89ed4 | 2020-04-29 19:59:24 +0000 | [diff] [blame] | 316 | |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 317 | if (oat_file->RequiresImage()) { |
Nicolas Geoffray | 107d22b | 2021-05-04 10:57:36 +0100 | [diff] [blame] | 318 | LOG(WARNING) << "Loading " |
| 319 | << oat_file->GetLocation() |
| 320 | << "non-executable as it requires an image which we failed to load"; |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 321 | // file as non-executable. |
| 322 | OatFileAssistant nonexecutable_oat_file_assistant(dex_location, |
| 323 | kRuntimeISA, |
Nicolas Geoffray | 525fa42 | 2021-04-19 07:50:35 +0000 | [diff] [blame] | 324 | context.get(), |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 325 | /*load_executable=*/false, |
| 326 | only_use_system_oat_files_); |
| 327 | oat_file.reset(nonexecutable_oat_file_assistant.GetBestOatFile().release()); |
Nicolas Geoffray | 107d22b | 2021-05-04 10:57:36 +0100 | [diff] [blame] | 328 | |
| 329 | // The file could be deleted concurrently (for example background |
| 330 | // dexopt, or secondary oat file being deleted by the app). |
| 331 | if (oat_file == nullptr) { |
| 332 | LOG(WARNING) << "Failed to reload oat file non-executable " << dex_location; |
| 333 | } |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 334 | } |
| 335 | |
Nicolas Geoffray | 107d22b | 2021-05-04 10:57:36 +0100 | [diff] [blame] | 336 | if (oat_file != nullptr) { |
| 337 | dex_files = oat_file_assistant.LoadDexFiles(*oat_file.get(), dex_location); |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 338 | |
Nicolas Geoffray | 107d22b | 2021-05-04 10:57:36 +0100 | [diff] [blame] | 339 | // Register for tracking. |
| 340 | for (const auto& dex_file : dex_files) { |
| 341 | dex::tracking::RegisterDexFile(dex_file.get()); |
| 342 | } |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | if (dex_files.empty()) { |
Calin Juravle | 9807115 | 2021-01-27 18:41:58 -0800 | [diff] [blame] | 346 | ScopedTrace failed_to_open_dex_files("FailedToOpenDexFilesFromOat"); |
Nicolas Geoffray | c07e873 | 2021-05-18 14:26:42 +0100 | [diff] [blame] | 347 | error_msgs->push_back("Failed to open dex files from " + odex_location); |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 348 | } else { |
| 349 | // Opened dex files from an oat file, madvise them to their loaded state. |
| 350 | for (const std::unique_ptr<const DexFile>& dex_file : dex_files) { |
| 351 | OatDexFile::MadviseDexFile(*dex_file, MadviseState::kMadviseStateAtLoad); |
| 352 | } |
Eric Holk | bc89ed4 | 2020-04-29 19:59:24 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Nicolas Geoffray | 107d22b | 2021-05-04 10:57:36 +0100 | [diff] [blame] | 355 | if (oat_file != nullptr) { |
| 356 | VLOG(class_linker) << "Registering " << oat_file->GetLocation(); |
| 357 | *out_oat_file = RegisterOatFile(std::move(oat_file)); |
| 358 | } |
Nicolas Geoffray | 525fa42 | 2021-04-19 07:50:35 +0000 | [diff] [blame] | 359 | } else { |
| 360 | // oat_file == nullptr |
| 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 (oat_file_assistant.GetBestStatus() == OatFileAssistant::kOatContextOutOfDate) { |
| 366 | std::set<const DexFile*> already_exists_in_classpath = |
| 367 | context->CheckForDuplicateDexFiles(MakeNonOwningPointerVector(dex_files)); |
| 368 | if (!already_exists_in_classpath.empty()) { |
| 369 | ScopedTrace duplicate_dex_files("DuplicateDexFilesInContext"); |
| 370 | auto duplicate_it = already_exists_in_classpath.begin(); |
| 371 | std::string duplicates = (*duplicate_it)->GetLocation(); |
| 372 | for (duplicate_it++ ; duplicate_it != already_exists_in_classpath.end(); duplicate_it++) { |
| 373 | duplicates += "," + (*duplicate_it)->GetLocation(); |
| 374 | } |
Bharadwaj Kalandhabhatta | 0bb4031 | 2017-06-01 10:47:00 -0700 | [diff] [blame] | 375 | |
Nicolas Geoffray | 525fa42 | 2021-04-19 07:50:35 +0000 | [diff] [blame] | 376 | std::ostringstream out; |
| 377 | out << "Trying to load dex files which is already loaded in the same ClassLoader " |
| 378 | << "hierarchy.\n" |
| 379 | << "This is a strong indication of bad ClassLoader construct which leads to poor " |
| 380 | << "performance and wastes memory.\n" |
| 381 | << "The list of duplicate dex files is: " << duplicates << "\n" |
| 382 | << "The current class loader context is: " |
| 383 | << context->EncodeContextForOatFile("") << "\n" |
| 384 | << "Java stack trace:\n"; |
| 385 | |
| 386 | { |
| 387 | ScopedObjectAccess soa(self); |
| 388 | self->DumpJavaStack(out); |
| 389 | } |
| 390 | |
| 391 | // We log this as an ERROR to stress the fact that this is most likely unintended. |
| 392 | // Note that ART cannot do anything about it. It is up to the app to fix their logic. |
| 393 | // Here we are trying to give a heads up on why the app might have performance issues. |
| 394 | LOG(ERROR) << out.str(); |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 395 | } |
Bharadwaj Kalandhabhatta | 0bb4031 | 2017-06-01 10:47:00 -0700 | [diff] [blame] | 396 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 397 | } |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 400 | // If we arrive here with an empty dex files list, it means we fail to load |
| 401 | // it/them through an .oat file. |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 402 | if (dex_files.empty()) { |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 403 | std::string error_msg; |
| 404 | static constexpr bool kVerifyChecksum = true; |
| 405 | const ArtDexFileLoader dex_file_loader; |
| 406 | if (!dex_file_loader.Open(dex_location, |
| 407 | dex_location, |
| 408 | Runtime::Current()->IsVerificationEnabled(), |
| 409 | kVerifyChecksum, |
| 410 | /*out*/ &error_msg, |
| 411 | &dex_files)) { |
Calin Juravle | 9807115 | 2021-01-27 18:41:58 -0800 | [diff] [blame] | 412 | ScopedTrace fail_to_open_dex_from_apk("FailedToOpenDexFilesFromApk"); |
Nicolas Geoffray | 90a18cf | 2020-06-25 15:12:59 +0100 | [diff] [blame] | 413 | LOG(WARNING) << error_msg; |
| 414 | error_msgs->push_back("Failed to open dex files from " + std::string(dex_location) |
| 415 | + " because: " + error_msg); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 416 | } |
| 417 | } |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 418 | |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 419 | if (Runtime::Current()->GetJit() != nullptr) { |
Nicolas Geoffray | 741a070 | 2019-06-10 11:18:11 +0100 | [diff] [blame] | 420 | Runtime::Current()->GetJit()->RegisterDexFiles(dex_files, class_loader); |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 421 | } |
| 422 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 423 | return dex_files; |
| 424 | } |
| 425 | |
David Brazdil | 7126c5b | 2019-03-05 00:02:51 +0000 | [diff] [blame] | 426 | static std::vector<const DexFile::Header*> GetDexFileHeaders(const std::vector<MemMap>& maps) { |
| 427 | std::vector<const DexFile::Header*> headers; |
| 428 | headers.reserve(maps.size()); |
| 429 | for (const MemMap& map : maps) { |
| 430 | DCHECK(map.IsValid()); |
| 431 | headers.push_back(reinterpret_cast<const DexFile::Header*>(map.Begin())); |
| 432 | } |
| 433 | return headers; |
| 434 | } |
| 435 | |
David Brazdil | 7126c5b | 2019-03-05 00:02:51 +0000 | [diff] [blame] | 436 | std::vector<std::unique_ptr<const DexFile>> OatFileManager::OpenDexFilesFromOat( |
| 437 | std::vector<MemMap>&& dex_mem_maps, |
| 438 | jobject class_loader, |
| 439 | jobjectArray dex_elements, |
| 440 | const OatFile** out_oat_file, |
| 441 | std::vector<std::string>* error_msgs) { |
| 442 | std::vector<std::unique_ptr<const DexFile>> dex_files = OpenDexFilesFromOat_Impl( |
| 443 | std::move(dex_mem_maps), |
| 444 | class_loader, |
| 445 | dex_elements, |
| 446 | out_oat_file, |
| 447 | error_msgs); |
| 448 | |
| 449 | if (error_msgs->empty()) { |
| 450 | // Remove write permission from DexFile pages. We do this at the end because |
| 451 | // OatFile assigns OatDexFile pointer in the DexFile objects. |
| 452 | for (std::unique_ptr<const DexFile>& dex_file : dex_files) { |
| 453 | if (!dex_file->DisableWrite()) { |
| 454 | error_msgs->push_back("Failed to make dex file " + dex_file->GetLocation() + " read-only"); |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | if (!error_msgs->empty()) { |
| 460 | return std::vector<std::unique_ptr<const DexFile>>(); |
| 461 | } |
| 462 | |
| 463 | return dex_files; |
| 464 | } |
| 465 | |
| 466 | std::vector<std::unique_ptr<const DexFile>> OatFileManager::OpenDexFilesFromOat_Impl( |
| 467 | std::vector<MemMap>&& dex_mem_maps, |
| 468 | jobject class_loader, |
| 469 | jobjectArray dex_elements, |
| 470 | const OatFile** out_oat_file, |
| 471 | std::vector<std::string>* error_msgs) { |
| 472 | ScopedTrace trace(__FUNCTION__); |
| 473 | std::string error_msg; |
| 474 | DCHECK(error_msgs != nullptr); |
| 475 | |
| 476 | // Extract dex file headers from `dex_mem_maps`. |
| 477 | const std::vector<const DexFile::Header*> dex_headers = GetDexFileHeaders(dex_mem_maps); |
| 478 | |
| 479 | // Determine dex/vdex locations and the combined location checksum. |
David Brazdil | 7126c5b | 2019-03-05 00:02:51 +0000 | [diff] [blame] | 480 | std::string dex_location; |
| 481 | std::string vdex_path; |
| 482 | bool has_vdex = OatFileAssistant::AnonymousDexVdexLocation(dex_headers, |
| 483 | kRuntimeISA, |
David Brazdil | 7126c5b | 2019-03-05 00:02:51 +0000 | [diff] [blame] | 484 | &dex_location, |
| 485 | &vdex_path); |
| 486 | |
| 487 | // Attempt to open an existing vdex and check dex file checksums match. |
| 488 | std::unique_ptr<VdexFile> vdex_file = nullptr; |
| 489 | if (has_vdex && OS::FileExists(vdex_path.c_str())) { |
| 490 | vdex_file = VdexFile::Open(vdex_path, |
| 491 | /* writable= */ false, |
| 492 | /* low_4gb= */ false, |
| 493 | /* unquicken= */ false, |
| 494 | &error_msg); |
| 495 | if (vdex_file == nullptr) { |
| 496 | LOG(WARNING) << "Failed to open vdex " << vdex_path << ": " << error_msg; |
| 497 | } else if (!vdex_file->MatchesDexFileChecksums(dex_headers)) { |
| 498 | LOG(WARNING) << "Failed to open vdex " << vdex_path << ": dex file checksum mismatch"; |
| 499 | vdex_file.reset(nullptr); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | // Load dex files. Skip structural dex file verification if vdex was found |
| 504 | // and dex checksums matched. |
| 505 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 506 | for (size_t i = 0; i < dex_mem_maps.size(); ++i) { |
| 507 | static constexpr bool kVerifyChecksum = true; |
| 508 | const ArtDexFileLoader dex_file_loader; |
| 509 | std::unique_ptr<const DexFile> dex_file(dex_file_loader.Open( |
| 510 | DexFileLoader::GetMultiDexLocation(i, dex_location.c_str()), |
Nicolas Geoffray | 8856695 | 2021-02-16 15:51:18 +0000 | [diff] [blame] | 511 | dex_headers[i]->checksum_, |
David Brazdil | 7126c5b | 2019-03-05 00:02:51 +0000 | [diff] [blame] | 512 | std::move(dex_mem_maps[i]), |
| 513 | /* verify= */ (vdex_file == nullptr) && Runtime::Current()->IsVerificationEnabled(), |
| 514 | kVerifyChecksum, |
| 515 | &error_msg)); |
| 516 | if (dex_file != nullptr) { |
| 517 | dex::tracking::RegisterDexFile(dex_file.get()); // Register for tracking. |
| 518 | dex_files.push_back(std::move(dex_file)); |
| 519 | } else { |
| 520 | error_msgs->push_back("Failed to open dex files from memory: " + error_msg); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | // Check if we should proceed to creating an OatFile instance backed by the vdex. |
| 525 | // We need: (a) an existing vdex, (b) class loader (can be null if invoked via reflection), |
| 526 | // and (c) no errors during dex file loading. |
| 527 | if (vdex_file == nullptr || class_loader == nullptr || !error_msgs->empty()) { |
| 528 | return dex_files; |
| 529 | } |
| 530 | |
| 531 | // Attempt to create a class loader context, check OpenDexFiles succeeds (prerequisite |
| 532 | // for using the context later). |
| 533 | std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::CreateContextForClassLoader( |
| 534 | class_loader, |
| 535 | dex_elements); |
| 536 | if (context == nullptr) { |
| 537 | LOG(ERROR) << "Could not create class loader context for " << vdex_path; |
| 538 | return dex_files; |
| 539 | } |
Calin Juravle | 5ff2393 | 2020-12-11 18:26:14 -0800 | [diff] [blame] | 540 | DCHECK(context->OpenDexFiles()) |
David Brazdil | 7126c5b | 2019-03-05 00:02:51 +0000 | [diff] [blame] | 541 | << "Context created from already opened dex files should not attempt to open again"; |
| 542 | |
David Brazdil | 7126c5b | 2019-03-05 00:02:51 +0000 | [diff] [blame] | 543 | // Initialize an OatFile instance backed by the loaded vdex. |
| 544 | std::unique_ptr<OatFile> oat_file(OatFile::OpenFromVdex(MakeNonOwningPointerVector(dex_files), |
| 545 | std::move(vdex_file), |
| 546 | dex_location)); |
Vladimir Marko | c390879 | 2020-04-06 14:52:04 +0100 | [diff] [blame] | 547 | if (oat_file != nullptr) { |
| 548 | VLOG(class_linker) << "Registering " << oat_file->GetLocation(); |
| 549 | *out_oat_file = RegisterOatFile(std::move(oat_file)); |
| 550 | } |
David Brazdil | 7126c5b | 2019-03-05 00:02:51 +0000 | [diff] [blame] | 551 | return dex_files; |
| 552 | } |
| 553 | |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 554 | // Check how many vdex files exist in the same directory as the vdex file we are about |
| 555 | // to write. If more than or equal to kAnonymousVdexCacheSize, unlink the least |
| 556 | // recently used one(s) (according to stat-reported atime). |
| 557 | static bool UnlinkLeastRecentlyUsedVdexIfNeeded(const std::string& vdex_path_to_add, |
| 558 | std::string* error_msg) { |
Nicolas Geoffray | 9bc364b | 2021-03-29 10:41:39 +0100 | [diff] [blame] | 559 | std::string basename = android::base::Basename(vdex_path_to_add); |
| 560 | if (!OatFileAssistant::IsAnonymousVdexBasename(basename)) { |
| 561 | // File is not for in memory dex files. |
| 562 | return true; |
| 563 | } |
| 564 | |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 565 | if (OS::FileExists(vdex_path_to_add.c_str())) { |
| 566 | // File already exists and will be overwritten. |
| 567 | // This will not change the number of entries in the cache. |
| 568 | return true; |
| 569 | } |
| 570 | |
| 571 | auto last_slash = vdex_path_to_add.rfind('/'); |
| 572 | CHECK(last_slash != std::string::npos); |
| 573 | std::string vdex_dir = vdex_path_to_add.substr(0, last_slash + 1); |
| 574 | |
| 575 | if (!OS::DirectoryExists(vdex_dir.c_str())) { |
| 576 | // Folder does not exist yet. Cache has zero entries. |
| 577 | return true; |
| 578 | } |
| 579 | |
| 580 | std::vector<std::pair<time_t, std::string>> cache; |
| 581 | |
| 582 | DIR* c_dir = opendir(vdex_dir.c_str()); |
| 583 | if (c_dir == nullptr) { |
| 584 | *error_msg = "Unable to open " + vdex_dir + " to delete unused vdex files"; |
| 585 | return false; |
| 586 | } |
| 587 | for (struct dirent* de = readdir(c_dir); de != nullptr; de = readdir(c_dir)) { |
| 588 | if (de->d_type != DT_REG) { |
| 589 | continue; |
| 590 | } |
Nicolas Geoffray | 9bc364b | 2021-03-29 10:41:39 +0100 | [diff] [blame] | 591 | basename = de->d_name; |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 592 | if (!OatFileAssistant::IsAnonymousVdexBasename(basename)) { |
| 593 | continue; |
| 594 | } |
| 595 | std::string fullname = vdex_dir + basename; |
| 596 | |
| 597 | struct stat s; |
| 598 | int rc = TEMP_FAILURE_RETRY(stat(fullname.c_str(), &s)); |
| 599 | if (rc == -1) { |
| 600 | *error_msg = "Failed to stat() anonymous vdex file " + fullname; |
| 601 | return false; |
| 602 | } |
| 603 | |
| 604 | cache.push_back(std::make_pair(s.st_atime, fullname)); |
| 605 | } |
| 606 | CHECK_EQ(0, closedir(c_dir)) << "Unable to close directory."; |
| 607 | |
| 608 | if (cache.size() < OatFileManager::kAnonymousVdexCacheSize) { |
| 609 | return true; |
| 610 | } |
| 611 | |
| 612 | std::sort(cache.begin(), |
| 613 | cache.end(), |
| 614 | [](const auto& a, const auto& b) { return a.first < b.first; }); |
| 615 | for (size_t i = OatFileManager::kAnonymousVdexCacheSize - 1; i < cache.size(); ++i) { |
| 616 | if (unlink(cache[i].second.c_str()) != 0) { |
| 617 | *error_msg = "Could not unlink anonymous vdex file " + cache[i].second; |
| 618 | return false; |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | return true; |
| 623 | } |
| 624 | |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 625 | class BackgroundVerificationTask final : public Task { |
| 626 | public: |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 627 | BackgroundVerificationTask(const std::vector<const DexFile*>& dex_files, |
| 628 | jobject class_loader, |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 629 | const std::string& vdex_path) |
| 630 | : dex_files_(dex_files), |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 631 | vdex_path_(vdex_path) { |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 632 | Thread* const self = Thread::Current(); |
| 633 | ScopedObjectAccess soa(self); |
| 634 | // Create a global ref for `class_loader` because it will be accessed from a different thread. |
| 635 | class_loader_ = soa.Vm()->AddGlobalRef(self, soa.Decode<mirror::ClassLoader>(class_loader)); |
| 636 | CHECK(class_loader_ != nullptr); |
| 637 | } |
| 638 | |
| 639 | ~BackgroundVerificationTask() { |
| 640 | Thread* const self = Thread::Current(); |
| 641 | ScopedObjectAccess soa(self); |
| 642 | soa.Vm()->DeleteGlobalRef(self, class_loader_); |
| 643 | } |
| 644 | |
| 645 | void Run(Thread* self) override { |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 646 | std::string error_msg; |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 647 | ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 648 | verifier::VerifierDeps verifier_deps(dex_files_); |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 649 | |
| 650 | // Iterate over all classes and verify them. |
| 651 | for (const DexFile* dex_file : dex_files_) { |
| 652 | 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] | 653 | const dex::ClassDef& class_def = dex_file->GetClassDef(cdef_idx); |
| 654 | |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 655 | // Take handles inside the loop. The background verification is low priority |
| 656 | // and we want to minimize the risk of blocking anyone else. |
| 657 | ScopedObjectAccess soa(self); |
| 658 | StackHandleScope<2> hs(self); |
| 659 | Handle<mirror::ClassLoader> h_loader(hs.NewHandle( |
| 660 | soa.Decode<mirror::ClassLoader>(class_loader_))); |
| 661 | Handle<mirror::Class> h_class(hs.NewHandle<mirror::Class>(class_linker->FindClass( |
| 662 | self, |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 663 | dex_file->GetClassDescriptor(class_def), |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 664 | h_loader))); |
| 665 | |
| 666 | if (h_class == nullptr) { |
| 667 | CHECK(self->IsExceptionPending()); |
| 668 | self->ClearException(); |
| 669 | continue; |
| 670 | } |
| 671 | |
| 672 | if (&h_class->GetDexFile() != dex_file) { |
| 673 | // There is a different class in the class path or a parent class loader |
| 674 | // with the same descriptor. This `h_class` is not resolvable, skip it. |
| 675 | continue; |
| 676 | } |
| 677 | |
| 678 | CHECK(h_class->IsResolved()) << h_class->PrettyDescriptor(); |
Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 679 | class_linker->VerifyClass(self, &verifier_deps, h_class); |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 680 | if (h_class->IsErroneous()) { |
| 681 | // ClassLinker::VerifyClass throws, which isn't useful here. |
| 682 | CHECK(soa.Self()->IsExceptionPending()); |
| 683 | soa.Self()->ClearException(); |
| 684 | } |
| 685 | |
| 686 | CHECK(h_class->IsVerified() || h_class->IsErroneous()) |
| 687 | << h_class->PrettyDescriptor() << ": state=" << h_class->GetStatus(); |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 688 | |
| 689 | if (h_class->IsVerified()) { |
| 690 | verifier_deps.RecordClassVerified(*dex_file, class_def); |
| 691 | } |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 692 | } |
| 693 | } |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 694 | |
| 695 | // Delete old vdex files if there are too many in the folder. |
| 696 | if (!UnlinkLeastRecentlyUsedVdexIfNeeded(vdex_path_, &error_msg)) { |
| 697 | LOG(ERROR) << "Could not unlink old vdex files " << vdex_path_ << ": " << error_msg; |
| 698 | return; |
| 699 | } |
| 700 | |
| 701 | // Construct a vdex file and write `verifier_deps` into it. |
| 702 | if (!VdexFile::WriteToDisk(vdex_path_, |
| 703 | dex_files_, |
| 704 | verifier_deps, |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 705 | &error_msg)) { |
| 706 | LOG(ERROR) << "Could not write anonymous vdex " << vdex_path_ << ": " << error_msg; |
| 707 | return; |
| 708 | } |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | void Finalize() override { |
| 712 | delete this; |
| 713 | } |
| 714 | |
| 715 | private: |
| 716 | const std::vector<const DexFile*> dex_files_; |
| 717 | jobject class_loader_; |
David Brazdil | 35a3f6a | 2019-03-04 15:59:06 +0000 | [diff] [blame] | 718 | const std::string vdex_path_; |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 719 | |
| 720 | DISALLOW_COPY_AND_ASSIGN(BackgroundVerificationTask); |
| 721 | }; |
| 722 | |
| 723 | void OatFileManager::RunBackgroundVerification(const std::vector<const DexFile*>& dex_files, |
Nicolas Geoffray | 9bc364b | 2021-03-29 10:41:39 +0100 | [diff] [blame] | 724 | jobject class_loader) { |
David Brazdil | 527072e | 2019-04-03 15:15:40 +0100 | [diff] [blame] | 725 | Runtime* const runtime = Runtime::Current(); |
| 726 | Thread* const self = Thread::Current(); |
| 727 | |
| 728 | if (runtime->IsJavaDebuggable()) { |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 729 | // Threads created by ThreadPool ("runtime threads") are not allowed to load |
| 730 | // classes when debuggable to match class-initialization semantics |
| 731 | // expectations. Do not verify in the background. |
| 732 | return; |
| 733 | } |
| 734 | |
David Brazdil | 527072e | 2019-04-03 15:15:40 +0100 | [diff] [blame] | 735 | if (!IsSdkVersionSetAndAtLeast(runtime->GetTargetSdkVersion(), SdkVersion::kQ)) { |
| 736 | // Do not run for legacy apps as they may depend on the previous class loader behaviour. |
| 737 | return; |
| 738 | } |
| 739 | |
| 740 | if (runtime->IsShuttingDown(self)) { |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 741 | // Not allowed to create new threads during runtime shutdown. |
| 742 | return; |
| 743 | } |
| 744 | |
Nicolas Geoffray | 9bc364b | 2021-03-29 10:41:39 +0100 | [diff] [blame] | 745 | if (dex_files.size() < 1) { |
| 746 | // Nothing to verify. |
| 747 | return; |
David Brazdil | 331a5e1 | 2019-04-01 22:46:16 +0000 | [diff] [blame] | 748 | } |
Nicolas Geoffray | 9bc364b | 2021-03-29 10:41:39 +0100 | [diff] [blame] | 749 | |
| 750 | std::string dex_location = dex_files[0]->GetLocation(); |
| 751 | const std::string& data_dir = Runtime::Current()->GetProcessDataDirectory(); |
| 752 | if (!android::base::StartsWith(dex_location, data_dir)) { |
| 753 | // For now, we only run background verification for secondary dex files. |
| 754 | // Running it for primary or split APKs could have some undesirable |
| 755 | // side-effects, like overloading the device on app startup. |
| 756 | return; |
| 757 | } |
| 758 | |
| 759 | std::string error_msg; |
| 760 | std::string odex_filename; |
| 761 | if (!OatFileAssistant::DexLocationToOdexFilename(dex_location, |
| 762 | kRuntimeISA, |
| 763 | &odex_filename, |
| 764 | &error_msg)) { |
| 765 | LOG(WARNING) << "Could not get odex filename for " << dex_location << ": " << error_msg; |
| 766 | return; |
| 767 | } |
| 768 | |
Nicolas Geoffray | 30ba3a0 | 2021-03-31 14:11:58 +0100 | [diff] [blame] | 769 | { |
| 770 | WriterMutexLock mu(self, *Locks::oat_file_manager_lock_); |
| 771 | if (verification_thread_pool_ == nullptr) { |
| 772 | verification_thread_pool_.reset( |
| 773 | new ThreadPool("Verification thread pool", /* num_threads= */ 1)); |
| 774 | verification_thread_pool_->StartWorkers(self); |
| 775 | } |
Nicolas Geoffray | 9bc364b | 2021-03-29 10:41:39 +0100 | [diff] [blame] | 776 | } |
| 777 | verification_thread_pool_->AddTask(self, new BackgroundVerificationTask( |
| 778 | dex_files, |
| 779 | class_loader, |
Nicolas Geoffray | 30ba3a0 | 2021-03-31 14:11:58 +0100 | [diff] [blame] | 780 | GetVdexFilename(odex_filename))); |
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 | |
Orion Hodson | 094b1cf | 2021-06-08 09:28:28 +0100 | [diff] [blame] | 803 | void OatFileManager::SetOnlyUseTrustedOatFiles() { |
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()) { |
Orion Hodson | 094b1cf | 2021-06-08 09:28:28 +0100 | [diff] [blame] | 813 | if (!LocationIsTrusted(oat_file->GetLocation())) { |
| 814 | // When the file is not in a trusted location, we check whether the oat file has any |
Nicolas Geoffray | efe9a3d | 2021-03-30 16:59:17 +0100 | [diff] [blame] | 815 | // AOT or DEX code. It is a fatal error if it has. |
| 816 | if (CompilerFilter::IsAotCompilationEnabled(oat_file->GetCompilerFilter()) || |
| 817 | oat_file->ContainsDexCode()) { |
| 818 | LOG(FATAL) << "Executing untrusted code from " << oat_file->GetLocation(); |
| 819 | } |
| 820 | } |
Andreas Gampe | 99c0795 | 2019-03-30 17:43:55 +0000 | [diff] [blame] | 821 | } |
Andreas Gampe | 3383f9c | 2019-03-15 11:46:08 -0700 | [diff] [blame] | 822 | } |
Nicolas Geoffray | e5c48f7 | 2020-03-11 11:28:43 +0000 | [diff] [blame] | 823 | only_use_system_oat_files_ = true; |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 824 | } |
| 825 | |
Nicolas Geoffray | 04680f3 | 2016-03-17 11:56:54 +0000 | [diff] [blame] | 826 | void OatFileManager::DumpForSigQuit(std::ostream& os) { |
| 827 | ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
| 828 | std::vector<const OatFile*> boot_oat_files = GetBootOatFiles(); |
| 829 | for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) { |
| 830 | if (ContainsElement(boot_oat_files, oat_file.get())) { |
| 831 | continue; |
| 832 | } |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 833 | os << oat_file->GetLocation() << ": " << oat_file->GetCompilerFilter() << "\n"; |
Nicolas Geoffray | 04680f3 | 2016-03-17 11:56:54 +0000 | [diff] [blame] | 834 | } |
| 835 | } |
| 836 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 837 | } // namespace art |