blob: 85992ea19167b99f1b281781ab8d1b378fdf3d05 [file] [log] [blame]
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001/*
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 Brazdil35a3f6a2019-03-04 15:59:06 +000022#include <sys/stat.h>
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070023
Andreas Gampe46ee31b2016-12-14 10:11:49 -080024#include "android-base/stringprintf.h"
Nicolas Geoffray68bf3902017-09-07 14:40:48 +010025#include "android-base/strings.h"
Andreas Gampe46ee31b2016-12-14 10:11:49 -080026
Andreas Gampe90b936d2017-01-31 08:58:55 -080027#include "art_field-inl.h"
Jeff Hao8ec0a202017-03-07 21:56:31 -080028#include "base/bit_vector-inl.h"
David Sehr891a50e2017-10-27 17:01:07 -070029#include "base/file_utils.h"
Andreas Gampe57943812017-12-06 21:39:13 -080030#include "base/logging.h" // For VLOG.
Andreas Gampe7fbc4a52018-11-28 08:26:47 -080031#include "base/mutex-inl.h"
David Brazdil527072e2019-04-03 15:15:40 +010032#include "base/sdk_version.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070033#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080034#include "base/systrace.h"
Mathieu Chartierfbc31082016-01-24 11:59:56 -080035#include "class_linker.h"
Calin Juravle7b0648a2017-07-07 18:40:50 -070036#include "class_loader_context.h"
David Sehr013fd802018-01-11 22:55:24 -080037#include "dex/art_dex_file_loader.h"
David Sehr9e734c72018-01-04 17:56:19 -080038#include "dex/dex_file-inl.h"
39#include "dex/dex_file_loader.h"
40#include "dex/dex_file_tracking_registrar.h"
Mathieu Chartier61d2b2d2016-02-04 13:31:46 -080041#include "gc/scoped_gc_critical_section.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070042#include "gc/space/image_space.h"
Mathieu Chartierfbc31082016-01-24 11:59:56 -080043#include "handle_scope-inl.h"
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +010044#include "jit/jit.h"
David Brazdil331a5e12019-04-01 22:46:16 +000045#include "jni/java_vm_ext.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010046#include "jni/jni_internal.h"
Mathieu Chartierfbc31082016-01-24 11:59:56 -080047#include "mirror/class_loader.h"
Andreas Gampe90b936d2017-01-31 08:58:55 -080048#include "mirror/object-inl.h"
Alex Light2ce6fc82017-12-18 16:42:36 -080049#include "oat_file.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070050#include "oat_file_assistant.h"
Mathieu Chartier3398c782016-09-30 10:27:43 -070051#include "obj_ptr-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070052#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070053#include "thread-current-inl.h"
Mathieu Chartiera9d82fe2016-01-25 20:06:11 -080054#include "thread_list.h"
David Brazdil331a5e12019-04-01 22:46:16 +000055#include "thread_pool.h"
David Brazdil35a3f6a2019-03-04 15:59:06 +000056#include "vdex_file.h"
57#include "verifier/verifier_deps.h"
Andreas Gampe90b936d2017-01-31 08:58:55 -080058#include "well_known_classes.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070059
60namespace art {
61
Andreas Gampe46ee31b2016-12-14 10:11:49 -080062using android::base::StringPrintf;
63
Mathieu Chartier120aa282017-08-05 16:03:03 -070064// If true, we attempt to load the application image if it exists.
Mathieu Chartierfbc31082016-01-24 11:59:56 -080065static constexpr bool kEnableAppImage = true;
66
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070067const OatFile* OatFileManager::RegisterOatFile(std::unique_ptr<const OatFile> oat_file) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -070068 WriterMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
Nicolas Geoffray29742602017-12-14 10:09:03 +000069 CHECK(!only_use_system_oat_files_ ||
70 LocationIsOnSystem(oat_file->GetLocation().c_str()) ||
71 !oat_file->IsExecutable())
Nicolas Geoffray68bf3902017-09-07 14:40:48 +010072 << "Registering a non /system oat file: " << oat_file->GetLocation();
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070073 DCHECK(oat_file != nullptr);
74 if (kIsDebugBuild) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -070075 CHECK(oat_files_.find(oat_file) == oat_files_.end());
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070076 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 Chartiere58991b2015-10-13 07:59:34 -070083 const OatFile* ret = oat_file.get();
84 oat_files_.insert(std::move(oat_file));
85 return ret;
86}
87
88void 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 Gampeafaf7f82018-10-16 11:32:38 -070095 compare.release(); // NOLINT b/117926937
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070096}
97
Calin Juravle0b791272016-04-18 16:38:27 +010098const 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 Chartier79c87da2017-10-10 11:54:29 -0700104 if (DexFileLoader::GetBaseLocation(oat_dex_file->GetDexFileLocation()) == dex_base_location) {
Calin Juravle0b791272016-04-18 16:38:27 +0100105 return oat_file.get();
106 }
107 }
108 }
109 return nullptr;
110}
111
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700112const OatFile* OatFileManager::FindOpenedOatFileFromOatLocation(const std::string& oat_location)
113 const {
114 ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700115 return FindOpenedOatFileFromOatLocationLocked(oat_location);
116}
117
118const OatFile* OatFileManager::FindOpenedOatFileFromOatLocationLocked(
119 const std::string& oat_location) const {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700120 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 Haodcdc85b2015-12-04 14:06:18 -0800128std::vector<const OatFile*> OatFileManager::GetBootOatFiles() const {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800129 std::vector<gc::space::ImageSpace*> image_spaces =
130 Runtime::Current()->GetHeap()->GetBootImageSpaces();
Andreas Gampe2a487eb2018-11-19 11:41:22 -0800131 std::vector<const OatFile*> oat_files;
132 oat_files.reserve(image_spaces.size());
Jeff Haodcdc85b2015-12-04 14:06:18 -0800133 for (gc::space::ImageSpace* image_space : image_spaces) {
134 oat_files.push_back(image_space->GetOatFile());
135 }
136 return oat_files;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700137}
138
139const OatFile* OatFileManager::GetPrimaryOatFile() const {
140 ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -0800141 std::vector<const OatFile*> boot_oat_files = GetBootOatFiles();
142 if (!boot_oat_files.empty()) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700143 for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800144 if (std::find(boot_oat_files.begin(), boot_oat_files.end(), oat_file.get()) ==
145 boot_oat_files.end()) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700146 return oat_file.get();
147 }
148 }
149 }
150 return nullptr;
151}
152
Vladimir Markob0b68cf2017-11-14 18:11:50 +0000153OatFileManager::OatFileManager()
Vladimir Markoe0669322018-09-03 15:44:54 +0100154 : only_use_system_oat_files_(false) {}
Vladimir Markob0b68cf2017-11-14 18:11:50 +0000155
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700156OatFileManager::~OatFileManager() {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700157 // Explicitly clear oat_files_ since the OatFile destructor calls back into OatFileManager for
158 // UnRegisterOatFileLocation.
159 oat_files_.clear();
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700160}
161
Jeff Haodcdc85b2015-12-04 14:06:18 -0800162std::vector<const OatFile*> OatFileManager::RegisterImageOatFiles(
Stephen Hines48ba1972018-09-24 13:35:54 -0700163 const std::vector<gc::space::ImageSpace*>& spaces) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800164 std::vector<const OatFile*> oat_files;
Andreas Gampe2a487eb2018-11-19 11:41:22 -0800165 oat_files.reserve(spaces.size());
Jeff Haodcdc85b2015-12-04 14:06:18 -0800166 for (gc::space::ImageSpace* space : spaces) {
167 oat_files.push_back(RegisterOatFile(space->ReleaseOatFile()));
168 }
169 return oat_files;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700170}
171
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100172static bool ClassLoaderContextMatches(
Mathieu Chartieradc90862018-05-11 13:03:06 -0700173 const OatFile* oat_file,
174 const ClassLoaderContext* context,
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100175 bool* is_special_shared_library,
176 /*out*/ std::string* error_msg) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700177 DCHECK(oat_file != nullptr);
178 DCHECK(error_msg != nullptr);
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100179 DCHECK(context != nullptr);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700180
Calin Juravleaf322422020-02-11 13:45:53 -0800181 if (!CompilerFilter::IsVerificationEnabled(oat_file->GetCompilerFilter())) {
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100182 // 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 Juravleaf322422020-02-11 13:45:53 -0800185 }
186
Mathieu Chartieradc90862018-05-11 13:03:06 -0700187 // If the oat file loading context matches the context used during compilation then we accept
Calin Juravle3f918642017-07-11 19:04:20 -0700188 // the oat file without addition checks
Mathieu Chartieradc90862018-05-11 13:03:06 -0700189 ClassLoaderContext::VerificationResult result = context->VerifyClassLoaderContextMatch(
190 oat_file->GetClassLoaderContext(),
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700191 /*verify_names=*/ true,
192 /*verify_checksums=*/ true);
Mathieu Chartieradc90862018-05-11 13:03:06 -0700193 switch (result) {
194 case ClassLoaderContext::VerificationResult::kForcedToSkipChecks:
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100195 *is_special_shared_library = true;
196 return true;
Mathieu Chartieradc90862018-05-11 13:03:06 -0700197 case ClassLoaderContext::VerificationResult::kMismatch:
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100198 *is_special_shared_library = false;
199 return false;
Mathieu Chartieradc90862018-05-11 13:03:06 -0700200 case ClassLoaderContext::VerificationResult::kVerifies:
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100201 *is_special_shared_library = false;
202 return true;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700203 }
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100204 LOG(FATAL) << "Unreachable";
Mathieu Chartieradc90862018-05-11 13:03:06 -0700205}
Calin Juravle3f918642017-07-11 19:04:20 -0700206
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100207bool OatFileManager::ShouldLoadAppImage(const OatFile* source_oat_file) const {
Mathieu Chartieradc90862018-05-11 13:03:06 -0700208 Runtime* const runtime = Runtime::Current();
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100209 return kEnableAppImage && (!runtime->IsJavaDebuggable() || source_oat_file->IsDebuggable());
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700210}
211
212std::vector<std::unique_ptr<const DexFile>> OatFileManager::OpenDexFilesFromOat(
213 const char* dex_location,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800214 jobject class_loader,
215 jobjectArray dex_elements,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700216 const OatFile** out_oat_file,
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700217 std::vector<std::string>* error_msgs) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800218 ScopedTrace trace(__FUNCTION__);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700219 CHECK(dex_location != nullptr);
220 CHECK(error_msgs != nullptr);
221
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100222 // Verify we aren't holding the mutator lock, which could starve GC when
223 // hitting the disk.
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800224 Thread* const self = Thread::Current();
225 Locks::mutator_lock_->AssertNotHeld(self);
226 Runtime* const runtime = Runtime::Current();
Calin Juravleb077e152016-02-18 18:47:37 +0000227
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100228 std::vector<std::unique_ptr<const DexFile>> dex_files;
229
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700230 // 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 Juravle27e0d1f2017-07-26 00:16:07 -0700235 } else {
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100236 std::unique_ptr<ClassLoaderContext> context(
237 ClassLoaderContext::CreateContextForClassLoader(class_loader, dex_elements));
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700238
Ulyana Trafimovich3060bb92020-07-16 14:17:11 +0000239 OatFileAssistant oat_file_assistant(dex_location,
240 kRuntimeISA,
241 runtime->GetOatFilesExecutable(),
242 only_use_system_oat_files_);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700243
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100244 // 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 Chartierfbc31082016-01-24 11:59:56 -0800249
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100250 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 Kamath5c525742017-04-28 10:19:29 +0100265
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100266 // 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 Kamath5c525742017-04-28 10:19:29 +0100271 }
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100272 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 Chartiera9d82fe2016-01-25 20:06:11 -0800282 {
283 ScopedThreadSuspension sts(self, kSuspended);
Mathieu Chartier61d2b2d2016-02-04 13:31:46 -0800284 gc::ScopedGCCriticalSection gcs(self,
285 gc::kGcCauseAddRemoveAppImageSpace,
286 gc::kCollectorTypeAddRemoveAppImageSpace);
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100287 ScopedSuspendAll ssa("Add image space");
288 runtime->GetHeap()->AddSpace(image_space.get());
Mathieu Chartiera9d82fe2016-01-25 20:06:11 -0800289 }
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100290 {
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 Chartierfbc31082016-01-24 11:59:56 -0800320 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800321 }
322 }
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100323 if (!added_image_space) {
324 DCHECK(dex_files.empty());
Eric Holkbc89ed42020-04-29 19:59:24 +0000325
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100326 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 Holkbc89ed42020-04-29 19:59:24 +0000352 }
353
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100354 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 Kalandhabhatta0bb40312017-06-01 10:47:00 -0700360
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100361 // 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 Kalandhabhatta0bb40312017-06-01 10:47:00 -0700394 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800395 }
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700396 }
397
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100398 // If we arrive here with an empty dex files list, it means we fail to load
399 // it/them through an .oat file.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700400 if (dex_files.empty()) {
Nicolas Geoffray90a18cf2020-06-25 15:12:59 +0100401 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 Chartierf9c6fc62015-10-07 11:44:05 -0700413 }
414 }
Calin Juravlec90bc922016-02-24 10:13:09 +0000415
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100416 if (Runtime::Current()->GetJit() != nullptr) {
Nicolas Geoffray741a0702019-06-10 11:18:11 +0100417 Runtime::Current()->GetJit()->RegisterDexFiles(dex_files, class_loader);
Nicolas Geoffray9ac09ee2019-05-08 23:38:27 +0100418 }
419
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700420 return dex_files;
421}
422
David Brazdil7126c5b2019-03-05 00:02:51 +0000423static 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 Brazdil35a3f6a2019-03-04 15:59:06 +0000433static 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 Brazdil7126c5b2019-03-05 00:02:51 +0000443std::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
473std::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 Markoc3908792020-04-06 14:52:04 +0100565 if (oat_file != nullptr) {
566 VLOG(class_linker) << "Registering " << oat_file->GetLocation();
567 *out_oat_file = RegisterOatFile(std::move(oat_file));
568 }
David Brazdil7126c5b2019-03-05 00:02:51 +0000569 return dex_files;
570}
571
David Brazdil35a3f6a2019-03-04 15:59:06 +0000572// 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).
575static 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 Brazdil331a5e12019-04-01 22:46:16 +0000637class BackgroundVerificationTask final : public Task {
638 public:
David Brazdil35a3f6a2019-03-04 15:59:06 +0000639 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 Brazdil331a5e12019-04-01 22:46:16 +0000646 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 Brazdil35a3f6a2019-03-04 15:59:06 +0000660 std::string error_msg;
David Brazdil331a5e12019-04-01 22:46:16 +0000661 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
David Brazdil35a3f6a2019-03-04 15:59:06 +0000662 verifier::VerifierDeps verifier_deps(dex_files_);
David Brazdil331a5e12019-04-01 22:46:16 +0000663
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 Brazdil35a3f6a2019-03-04 15:59:06 +0000667 const dex::ClassDef& class_def = dex_file->GetClassDef(cdef_idx);
668
David Brazdil331a5e12019-04-01 22:46:16 +0000669 // 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 Brazdil35a3f6a2019-03-04 15:59:06 +0000677 dex_file->GetClassDescriptor(class_def),
David Brazdil331a5e12019-04-01 22:46:16 +0000678 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 Brazdil35a3f6a2019-03-04 15:59:06 +0000702
703 if (h_class->IsVerified()) {
704 verifier_deps.RecordClassVerified(*dex_file, class_def);
705 }
David Brazdil331a5e12019-04-01 22:46:16 +0000706 }
707 }
David Brazdil35a3f6a2019-03-04 15:59:06 +0000708
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 Brazdil331a5e12019-04-01 22:46:16 +0000724 }
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 Brazdil35a3f6a2019-03-04 15:59:06 +0000733 const std::string class_loader_context_;
734 const std::string vdex_path_;
David Brazdil331a5e12019-04-01 22:46:16 +0000735
736 DISALLOW_COPY_AND_ASSIGN(BackgroundVerificationTask);
737};
738
739void OatFileManager::RunBackgroundVerification(const std::vector<const DexFile*>& dex_files,
David Brazdil35a3f6a2019-03-04 15:59:06 +0000740 jobject class_loader,
741 const char* class_loader_context) {
David Brazdil527072e2019-04-03 15:15:40 +0100742 Runtime* const runtime = Runtime::Current();
743 Thread* const self = Thread::Current();
744
745 if (runtime->IsJavaDebuggable()) {
David Brazdil331a5e12019-04-01 22:46:16 +0000746 // 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 Brazdil527072e2019-04-03 15:15:40 +0100752 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 Brazdil331a5e12019-04-01 22:46:16 +0000758 // Not allowed to create new threads during runtime shutdown.
759 return;
760 }
761
David Brazdil35a3f6a2019-03-04 15:59:06 +0000762 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 Brazdil331a5e12019-04-01 22:46:16 +0000780 }
David Brazdil331a5e12019-04-01 22:46:16 +0000781}
782
783void 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
791void OatFileManager::DeleteThreadPool() {
792 verification_thread_pool_.reset(nullptr);
793}
794
795void 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 Geoffray2e17cf42020-03-12 21:19:46 +0000803void OatFileManager::SetOnlyUseSystemOatFiles() {
Nicolas Geoffray68bf3902017-09-07 14:40:48 +0100804 ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
Nicolas Geoffray2e17cf42020-03-12 21:19:46 +0000805 // 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 Gampe99c07952019-03-30 17:43:55 +0000810
Nicolas Geoffray2e17cf42020-03-12 21:19:46 +0000811 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 Gampe99c07952019-03-30 17:43:55 +0000814 }
Andreas Gampe3383f9c2019-03-15 11:46:08 -0700815 }
Nicolas Geoffraye5c48f72020-03-11 11:28:43 +0000816 only_use_system_oat_files_ = true;
Nicolas Geoffray68bf3902017-09-07 14:40:48 +0100817}
818
Nicolas Geoffray04680f32016-03-17 11:56:54 +0000819void 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 Gampe29d38e72016-03-23 15:31:51 +0000826 os << oat_file->GetLocation() << ": " << oat_file->GetCompilerFilter() << "\n";
Nicolas Geoffray04680f32016-03-17 11:56:54 +0000827 }
828}
829
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700830} // namespace art