blob: 9552ca33f1740d32805a5ec5e2be676515e483fb [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>
22
Andreas Gampe46ee31b2016-12-14 10:11:49 -080023#include "android-base/stringprintf.h"
Nicolas Geoffray68bf3902017-09-07 14:40:48 +010024#include "android-base/strings.h"
Andreas Gampe46ee31b2016-12-14 10:11:49 -080025
Andreas Gampe90b936d2017-01-31 08:58:55 -080026#include "art_field-inl.h"
Jeff Hao8ec0a202017-03-07 21:56:31 -080027#include "base/bit_vector-inl.h"
David Sehr891a50e2017-10-27 17:01:07 -070028#include "base/file_utils.h"
Andreas Gampe57943812017-12-06 21:39:13 -080029#include "base/logging.h" // For VLOG.
Andreas Gampe7fbc4a52018-11-28 08:26:47 -080030#include "base/mutex-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070031#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080032#include "base/systrace.h"
Mathieu Chartierfbc31082016-01-24 11:59:56 -080033#include "class_linker.h"
Calin Juravle7b0648a2017-07-07 18:40:50 -070034#include "class_loader_context.h"
David Sehr013fd802018-01-11 22:55:24 -080035#include "dex/art_dex_file_loader.h"
David Sehr9e734c72018-01-04 17:56:19 -080036#include "dex/dex_file-inl.h"
37#include "dex/dex_file_loader.h"
38#include "dex/dex_file_tracking_registrar.h"
Mathieu Chartier61d2b2d2016-02-04 13:31:46 -080039#include "gc/scoped_gc_critical_section.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070040#include "gc/space/image_space.h"
Mathieu Chartierfbc31082016-01-24 11:59:56 -080041#include "handle_scope-inl.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010042#include "jni/jni_internal.h"
Mathieu Chartierfbc31082016-01-24 11:59:56 -080043#include "mirror/class_loader.h"
Andreas Gampe90b936d2017-01-31 08:58:55 -080044#include "mirror/object-inl.h"
Alex Light2ce6fc82017-12-18 16:42:36 -080045#include "oat_file.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070046#include "oat_file_assistant.h"
Mathieu Chartier3398c782016-09-30 10:27:43 -070047#include "obj_ptr-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070048#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070049#include "thread-current-inl.h"
Mathieu Chartiera9d82fe2016-01-25 20:06:11 -080050#include "thread_list.h"
Andreas Gampe90b936d2017-01-31 08:58:55 -080051#include "well_known_classes.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070052
53namespace art {
54
Andreas Gampe46ee31b2016-12-14 10:11:49 -080055using android::base::StringPrintf;
56
Mathieu Chartier120aa282017-08-05 16:03:03 -070057// If true, we attempt to load the application image if it exists.
Mathieu Chartierfbc31082016-01-24 11:59:56 -080058static constexpr bool kEnableAppImage = true;
59
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070060const OatFile* OatFileManager::RegisterOatFile(std::unique_ptr<const OatFile> oat_file) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -070061 WriterMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
Nicolas Geoffray29742602017-12-14 10:09:03 +000062 CHECK(!only_use_system_oat_files_ ||
63 LocationIsOnSystem(oat_file->GetLocation().c_str()) ||
64 !oat_file->IsExecutable())
Nicolas Geoffray68bf3902017-09-07 14:40:48 +010065 << "Registering a non /system oat file: " << oat_file->GetLocation();
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070066 DCHECK(oat_file != nullptr);
67 if (kIsDebugBuild) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -070068 CHECK(oat_files_.find(oat_file) == oat_files_.end());
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070069 for (const std::unique_ptr<const OatFile>& existing : oat_files_) {
70 CHECK_NE(oat_file.get(), existing.get()) << oat_file->GetLocation();
71 // Check that we don't have an oat file with the same address. Copies of the same oat file
72 // should be loaded at different addresses.
73 CHECK_NE(oat_file->Begin(), existing->Begin()) << "Oat file already mapped at that location";
74 }
75 }
Mathieu Chartiere58991b2015-10-13 07:59:34 -070076 const OatFile* ret = oat_file.get();
77 oat_files_.insert(std::move(oat_file));
78 return ret;
79}
80
81void OatFileManager::UnRegisterAndDeleteOatFile(const OatFile* oat_file) {
82 WriterMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
83 DCHECK(oat_file != nullptr);
84 std::unique_ptr<const OatFile> compare(oat_file);
85 auto it = oat_files_.find(compare);
86 CHECK(it != oat_files_.end());
87 oat_files_.erase(it);
Andreas Gampeafaf7f82018-10-16 11:32:38 -070088 compare.release(); // NOLINT b/117926937
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070089}
90
Calin Juravle0b791272016-04-18 16:38:27 +010091const OatFile* OatFileManager::FindOpenedOatFileFromDexLocation(
92 const std::string& dex_base_location) const {
93 ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
94 for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) {
95 const std::vector<const OatDexFile*>& oat_dex_files = oat_file->GetOatDexFiles();
96 for (const OatDexFile* oat_dex_file : oat_dex_files) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -070097 if (DexFileLoader::GetBaseLocation(oat_dex_file->GetDexFileLocation()) == dex_base_location) {
Calin Juravle0b791272016-04-18 16:38:27 +010098 return oat_file.get();
99 }
100 }
101 }
102 return nullptr;
103}
104
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700105const OatFile* OatFileManager::FindOpenedOatFileFromOatLocation(const std::string& oat_location)
106 const {
107 ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700108 return FindOpenedOatFileFromOatLocationLocked(oat_location);
109}
110
111const OatFile* OatFileManager::FindOpenedOatFileFromOatLocationLocked(
112 const std::string& oat_location) const {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700113 for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) {
114 if (oat_file->GetLocation() == oat_location) {
115 return oat_file.get();
116 }
117 }
118 return nullptr;
119}
120
Jeff Haodcdc85b2015-12-04 14:06:18 -0800121std::vector<const OatFile*> OatFileManager::GetBootOatFiles() const {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800122 std::vector<gc::space::ImageSpace*> image_spaces =
123 Runtime::Current()->GetHeap()->GetBootImageSpaces();
Andreas Gampe2a487eb2018-11-19 11:41:22 -0800124 std::vector<const OatFile*> oat_files;
125 oat_files.reserve(image_spaces.size());
Jeff Haodcdc85b2015-12-04 14:06:18 -0800126 for (gc::space::ImageSpace* image_space : image_spaces) {
127 oat_files.push_back(image_space->GetOatFile());
128 }
129 return oat_files;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700130}
131
132const OatFile* OatFileManager::GetPrimaryOatFile() const {
133 ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -0800134 std::vector<const OatFile*> boot_oat_files = GetBootOatFiles();
135 if (!boot_oat_files.empty()) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700136 for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800137 if (std::find(boot_oat_files.begin(), boot_oat_files.end(), oat_file.get()) ==
138 boot_oat_files.end()) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700139 return oat_file.get();
140 }
141 }
142 }
143 return nullptr;
144}
145
Vladimir Markob0b68cf2017-11-14 18:11:50 +0000146OatFileManager::OatFileManager()
Vladimir Markoe0669322018-09-03 15:44:54 +0100147 : only_use_system_oat_files_(false) {}
Vladimir Markob0b68cf2017-11-14 18:11:50 +0000148
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700149OatFileManager::~OatFileManager() {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700150 // Explicitly clear oat_files_ since the OatFile destructor calls back into OatFileManager for
151 // UnRegisterOatFileLocation.
152 oat_files_.clear();
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700153}
154
Jeff Haodcdc85b2015-12-04 14:06:18 -0800155std::vector<const OatFile*> OatFileManager::RegisterImageOatFiles(
Stephen Hines48ba1972018-09-24 13:35:54 -0700156 const std::vector<gc::space::ImageSpace*>& spaces) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800157 std::vector<const OatFile*> oat_files;
Andreas Gampe2a487eb2018-11-19 11:41:22 -0800158 oat_files.reserve(spaces.size());
Jeff Haodcdc85b2015-12-04 14:06:18 -0800159 for (gc::space::ImageSpace* space : spaces) {
160 oat_files.push_back(RegisterOatFile(space->ReleaseOatFile()));
161 }
162 return oat_files;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700163}
164
Jeff Hao8ec0a202017-03-07 21:56:31 -0800165class TypeIndexInfo {
166 public:
167 explicit TypeIndexInfo(const DexFile* dex_file)
168 : type_indexes_(GenerateTypeIndexes(dex_file)),
169 iter_(type_indexes_.Indexes().begin()),
170 end_(type_indexes_.Indexes().end()) { }
171
172 BitVector& GetTypeIndexes() {
173 return type_indexes_;
174 }
175 BitVector::IndexIterator& GetIterator() {
176 return iter_;
177 }
178 BitVector::IndexIterator& GetIteratorEnd() {
179 return end_;
180 }
181 void AdvanceIterator() {
182 iter_++;
183 }
184
185 private:
186 static BitVector GenerateTypeIndexes(const DexFile* dex_file) {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700187 BitVector type_indexes(/*start_bits=*/0, /*expandable=*/true, Allocator::GetMallocAllocator());
Jeff Hao8ec0a202017-03-07 21:56:31 -0800188 for (uint16_t i = 0; i < dex_file->NumClassDefs(); ++i) {
189 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
190 uint16_t type_idx = class_def.class_idx_.index_;
191 type_indexes.SetBit(type_idx);
192 }
193 return type_indexes;
194 }
195
196 // BitVector with bits set for the type indexes of all classes in the input dex file.
197 BitVector type_indexes_;
198 BitVector::IndexIterator iter_;
199 BitVector::IndexIterator end_;
200};
201
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700202class DexFileAndClassPair : ValueObject {
203 public:
Jeff Hao8ec0a202017-03-07 21:56:31 -0800204 DexFileAndClassPair(const DexFile* dex_file, TypeIndexInfo* type_info, bool from_loaded_oat)
205 : type_info_(type_info),
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700206 dex_file_(dex_file),
Jeff Hao8ec0a202017-03-07 21:56:31 -0800207 cached_descriptor_(dex_file_->StringByTypeIdx(dex::TypeIndex(*type_info->GetIterator()))),
208 from_loaded_oat_(from_loaded_oat) {
209 type_info_->AdvanceIterator();
210 }
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700211
Mathieu Chartier80b37b72015-10-12 18:13:39 -0700212 DexFileAndClassPair(const DexFileAndClassPair& rhs) = default;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700213
Mathieu Chartier80b37b72015-10-12 18:13:39 -0700214 DexFileAndClassPair& operator=(const DexFileAndClassPair& rhs) = default;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700215
216 const char* GetCachedDescriptor() const {
217 return cached_descriptor_;
218 }
219
220 bool operator<(const DexFileAndClassPair& rhs) const {
221 const int cmp = strcmp(cached_descriptor_, rhs.cached_descriptor_);
222 if (cmp != 0) {
223 // Note that the order must be reversed. We want to iterate over the classes in dex files.
224 // They are sorted lexicographically. Thus, the priority-queue must be a min-queue.
225 return cmp > 0;
226 }
227 return dex_file_ < rhs.dex_file_;
228 }
229
230 bool DexFileHasMoreClasses() const {
Jeff Hao8ec0a202017-03-07 21:56:31 -0800231 return type_info_->GetIterator() != type_info_->GetIteratorEnd();
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700232 }
233
234 void Next() {
Jeff Hao8ec0a202017-03-07 21:56:31 -0800235 cached_descriptor_ = dex_file_->StringByTypeIdx(dex::TypeIndex(*type_info_->GetIterator()));
236 type_info_->AdvanceIterator();
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700237 }
238
239 bool FromLoadedOat() const {
240 return from_loaded_oat_;
241 }
242
243 const DexFile* GetDexFile() const {
Jeff Haof0192c82016-03-28 20:39:50 -0700244 return dex_file_;
245 }
246
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700247 private:
Jeff Hao8ec0a202017-03-07 21:56:31 -0800248 TypeIndexInfo* type_info_;
Jeff Haof0192c82016-03-28 20:39:50 -0700249 const DexFile* dex_file_;
Jeff Hao8ec0a202017-03-07 21:56:31 -0800250 const char* cached_descriptor_;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700251 bool from_loaded_oat_; // We only need to compare mismatches between what we load now
252 // and what was loaded before. Any old duplicates must have been
253 // OK, and any new "internal" duplicates are as well (they must
254 // be from multidex, which resolves correctly).
255};
256
Jeff Hao8ec0a202017-03-07 21:56:31 -0800257static void AddDexFilesFromOat(
258 const OatFile* oat_file,
259 /*out*/std::vector<const DexFile*>* dex_files,
260 std::vector<std::unique_ptr<const DexFile>>* opened_dex_files) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700261 for (const OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
262 std::string error;
263 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error);
264 if (dex_file == nullptr) {
265 LOG(WARNING) << "Could not create dex file from oat file: " << error;
266 } else if (dex_file->NumClassDefs() > 0U) {
Jeff Hao8ec0a202017-03-07 21:56:31 -0800267 dex_files->push_back(dex_file.get());
Mathieu Chartier14d7b3e2016-06-09 16:18:04 -0700268 opened_dex_files->push_back(std::move(dex_file));
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700269 }
270 }
271}
272
Jeff Hao8ec0a202017-03-07 21:56:31 -0800273static void AddNext(/*inout*/DexFileAndClassPair& original,
274 /*inout*/std::priority_queue<DexFileAndClassPair>& heap) {
275 if (original.DexFileHasMoreClasses()) {
276 original.Next();
277 heap.push(std::move(original));
Jeff Haof0192c82016-03-28 20:39:50 -0700278 }
279}
280
Mathieu Chartieradc90862018-05-11 13:03:06 -0700281static bool CheckClassCollision(const OatFile* oat_file,
282 const ClassLoaderContext* context,
283 std::string* error_msg /*out*/) {
284 std::vector<const DexFile*> dex_files_loaded = context->FlattenOpenedDexFiles();
285
286 // Vector that holds the newly opened dex files live, this is done to prevent leaks.
287 std::vector<std::unique_ptr<const DexFile>> opened_dex_files;
288
289 ScopedTrace st("Collision check");
290 // Add dex files from the oat file to check.
291 std::vector<const DexFile*> dex_files_unloaded;
292 AddDexFilesFromOat(oat_file, &dex_files_unloaded, &opened_dex_files);
293
Jeff Hao8ec0a202017-03-07 21:56:31 -0800294 // Generate type index information for each dex file.
295 std::vector<TypeIndexInfo> loaded_types;
Andreas Gampe2a487eb2018-11-19 11:41:22 -0800296 loaded_types.reserve(dex_files_loaded.size());
Jeff Hao8ec0a202017-03-07 21:56:31 -0800297 for (const DexFile* dex_file : dex_files_loaded) {
298 loaded_types.push_back(TypeIndexInfo(dex_file));
299 }
300 std::vector<TypeIndexInfo> unloaded_types;
Andreas Gampe2a487eb2018-11-19 11:41:22 -0800301 unloaded_types.reserve(dex_files_unloaded.size());
Jeff Hao8ec0a202017-03-07 21:56:31 -0800302 for (const DexFile* dex_file : dex_files_unloaded) {
303 unloaded_types.push_back(TypeIndexInfo(dex_file));
304 }
305
306 // Populate the queue of dex file and class pairs with the loaded and unloaded dex files.
307 std::priority_queue<DexFileAndClassPair> queue;
308 for (size_t i = 0; i < dex_files_loaded.size(); ++i) {
309 if (loaded_types[i].GetIterator() != loaded_types[i].GetIteratorEnd()) {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700310 queue.emplace(dex_files_loaded[i], &loaded_types[i], /*from_loaded_oat=*/true);
Jeff Hao8ec0a202017-03-07 21:56:31 -0800311 }
312 }
313 for (size_t i = 0; i < dex_files_unloaded.size(); ++i) {
314 if (unloaded_types[i].GetIterator() != unloaded_types[i].GetIteratorEnd()) {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700315 queue.emplace(dex_files_unloaded[i], &unloaded_types[i], /*from_loaded_oat=*/false);
Jeff Hao8ec0a202017-03-07 21:56:31 -0800316 }
317 }
318
319 // Now drain the queue.
Jeff Hao0471ece2017-04-07 16:28:12 -0700320 bool has_duplicates = false;
321 error_msg->clear();
Jeff Hao8ec0a202017-03-07 21:56:31 -0800322 while (!queue.empty()) {
323 // Modifying the top element is only safe if we pop right after.
324 DexFileAndClassPair compare_pop(queue.top());
325 queue.pop();
326
327 // Compare against the following elements.
328 while (!queue.empty()) {
329 DexFileAndClassPair top(queue.top());
330 if (strcmp(compare_pop.GetCachedDescriptor(), top.GetCachedDescriptor()) == 0) {
331 // Same descriptor. Check whether it's crossing old-oat-files to new-oat-files.
332 if (compare_pop.FromLoadedOat() != top.FromLoadedOat()) {
Jeff Hao0471ece2017-04-07 16:28:12 -0700333 error_msg->append(
334 StringPrintf("Found duplicated class when checking oat files: '%s' in %s and %s\n",
Jeff Hao8ec0a202017-03-07 21:56:31 -0800335 compare_pop.GetCachedDescriptor(),
336 compare_pop.GetDexFile()->GetLocation().c_str(),
Jeff Hao0471ece2017-04-07 16:28:12 -0700337 top.GetDexFile()->GetLocation().c_str()));
338 if (!VLOG_IS_ON(oat)) {
339 return true;
340 }
341 has_duplicates = true;
Jeff Hao8ec0a202017-03-07 21:56:31 -0800342 }
343 queue.pop();
344 AddNext(top, queue);
345 } else {
346 // Something else. Done here.
347 break;
348 }
349 }
350 AddNext(compare_pop, queue);
351 }
352
Jeff Hao0471ece2017-04-07 16:28:12 -0700353 return has_duplicates;
Jeff Haof0192c82016-03-28 20:39:50 -0700354}
355
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700356// Check for class-def collisions in dex files.
357//
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700358// This first walks the class loader chain present in the given context, getting all the dex files
359// from the class loader.
Jeff Haof0192c82016-03-28 20:39:50 -0700360//
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700361// If the context is null (which means the initial class loader was null or unsupported)
362// this returns false. b/37777332.
363//
364// This first checks whether all class loaders in the context have the same type and
365// classpath. If so, we exit early. Otherwise, we do the collision check.
Jeff Haof0192c82016-03-28 20:39:50 -0700366//
367// The collision check works by maintaining a heap with one class from each dex file, sorted by the
368// class descriptor. Then a dex-file/class pair is continually removed from the heap and compared
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700369// against the following top element. If the descriptor is the same, it is now checked whether
370// the two elements agree on whether their dex file was from an already-loaded oat-file or the
371// new oat file. Any disagreement indicates a collision.
Mathieu Chartieradc90862018-05-11 13:03:06 -0700372OatFileManager::CheckCollisionResult OatFileManager::CheckCollision(
373 const OatFile* oat_file,
374 const ClassLoaderContext* context,
375 /*out*/ std::string* error_msg) const {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700376 DCHECK(oat_file != nullptr);
377 DCHECK(error_msg != nullptr);
Jeff Haof0192c82016-03-28 20:39:50 -0700378
Calin Juravle3f918642017-07-11 19:04:20 -0700379 // The context might be null if there are unrecognized class loaders in the chain or they
380 // don't meet sensible sanity conditions. In this case we assume that the app knows what it's
381 // doing and accept the oat file.
382 // Note that this has correctness implications as we cannot guarantee that the class resolution
383 // used during compilation is OK (b/37777332).
384 if (context == nullptr) {
Mathieu Chartieradc90862018-05-11 13:03:06 -0700385 LOG(WARNING) << "Skipping duplicate class check due to unsupported classloader";
386 return CheckCollisionResult::kSkippedUnsupportedClassLoader;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700387 }
388
Mathieu Chartieradc90862018-05-11 13:03:06 -0700389 // If the oat file loading context matches the context used during compilation then we accept
Calin Juravle3f918642017-07-11 19:04:20 -0700390 // the oat file without addition checks
Mathieu Chartieradc90862018-05-11 13:03:06 -0700391 ClassLoaderContext::VerificationResult result = context->VerifyClassLoaderContextMatch(
392 oat_file->GetClassLoaderContext(),
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700393 /*verify_names=*/ true,
394 /*verify_checksums=*/ true);
Mathieu Chartieradc90862018-05-11 13:03:06 -0700395 switch (result) {
396 case ClassLoaderContext::VerificationResult::kForcedToSkipChecks:
397 return CheckCollisionResult::kSkippedClassLoaderContextSharedLibrary;
398 case ClassLoaderContext::VerificationResult::kMismatch:
399 // Mismatched context, do the actual collision check.
400 break;
401 case ClassLoaderContext::VerificationResult::kVerifies:
402 return CheckCollisionResult::kNoCollisions;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700403 }
404
Calin Juravle3f918642017-07-11 19:04:20 -0700405 // The class loader context does not match. Perform a full duplicate classes check.
Mathieu Chartieradc90862018-05-11 13:03:06 -0700406 return CheckClassCollision(oat_file, context, error_msg)
407 ? CheckCollisionResult::kPerformedHasCollisions : CheckCollisionResult::kNoCollisions;
408}
Calin Juravle3f918642017-07-11 19:04:20 -0700409
Mathieu Chartieradc90862018-05-11 13:03:06 -0700410bool OatFileManager::AcceptOatFile(CheckCollisionResult result) const {
411 // Take the file only if it has no collisions, or we must take it because of preopting.
412 // Also accept oat files for shared libraries and unsupported class loaders.
413 return result != CheckCollisionResult::kPerformedHasCollisions;
414}
Calin Juravle3f918642017-07-11 19:04:20 -0700415
Mathieu Chartieradc90862018-05-11 13:03:06 -0700416bool OatFileManager::ShouldLoadAppImage(CheckCollisionResult check_collision_result,
417 const OatFile* source_oat_file,
418 ClassLoaderContext* context,
419 std::string* error_msg) {
420 Runtime* const runtime = Runtime::Current();
421 if (kEnableAppImage && (!runtime->IsJavaDebuggable() || source_oat_file->IsDebuggable())) {
422 // If we verified the class loader context (skipping due to the special marker doesn't
423 // count), then also avoid the collision check.
424 bool load_image = check_collision_result == CheckCollisionResult::kNoCollisions;
425 // If we skipped the collision check, we need to reverify to be sure its OK to load the
426 // image.
427 if (!load_image &&
428 check_collision_result ==
429 CheckCollisionResult::kSkippedClassLoaderContextSharedLibrary) {
430 // We can load the app image only if there are no collisions. If we know the
431 // class loader but didn't do the full collision check in HasCollisions(),
432 // do it now. b/77342775
433 load_image = !CheckClassCollision(source_oat_file, context, error_msg);
434 }
435 return load_image;
436 }
437 return false;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700438}
439
440std::vector<std::unique_ptr<const DexFile>> OatFileManager::OpenDexFilesFromOat(
441 const char* dex_location,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800442 jobject class_loader,
443 jobjectArray dex_elements,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700444 const OatFile** out_oat_file,
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700445 std::vector<std::string>* error_msgs) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800446 ScopedTrace trace(__FUNCTION__);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700447 CHECK(dex_location != nullptr);
448 CHECK(error_msgs != nullptr);
449
450 // Verify we aren't holding the mutator lock, which could starve GC if we
451 // have to generate or relocate an oat file.
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800452 Thread* const self = Thread::Current();
453 Locks::mutator_lock_->AssertNotHeld(self);
454 Runtime* const runtime = Runtime::Current();
Calin Juravleb077e152016-02-18 18:47:37 +0000455
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700456 std::unique_ptr<ClassLoaderContext> context;
457 // If the class_loader is null there's not much we can do. This happens if a dex files is loaded
458 // directly with DexFile APIs instead of using class loaders.
459 if (class_loader == nullptr) {
460 LOG(WARNING) << "Opening an oat file without a class loader. "
461 << "Are you using the deprecated DexFile APIs?";
462 context = nullptr;
463 } else {
464 context = ClassLoaderContext::CreateContextForClassLoader(class_loader, dex_elements);
465 }
466
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700467 OatFileAssistant oat_file_assistant(dex_location,
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700468 kRuntimeISA,
Nicolas Geoffray29742602017-12-14 10:09:03 +0000469 !runtime->IsAotCompiler(),
470 only_use_system_oat_files_);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700471
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700472 // Get the oat file on disk.
473 std::unique_ptr<const OatFile> oat_file(oat_file_assistant.GetBestOatFile().release());
Andreas Gampe9ea84d02018-03-02 09:32:03 -0800474 VLOG(oat) << "OatFileAssistant(" << dex_location << ").GetBestOatFile()="
475 << reinterpret_cast<uintptr_t>(oat_file.get())
476 << " (executable=" << (oat_file != nullptr ? oat_file->IsExecutable() : false) << ")";
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800477
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +0000478 const OatFile* source_oat_file = nullptr;
Mathieu Chartieradc90862018-05-11 13:03:06 -0700479 CheckCollisionResult check_collision_result = CheckCollisionResult::kPerformedHasCollisions;
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +0000480 std::string error_msg;
Nicolas Geoffray29742602017-12-14 10:09:03 +0000481 if ((class_loader != nullptr || dex_elements != nullptr) && oat_file != nullptr) {
Nicolas Geoffray68bf3902017-09-07 14:40:48 +0100482 // Prevent oat files from being loaded if no class_loader or dex_elements are provided.
483 // This can happen when the deprecated DexFile.<init>(String) is called directly, and it
484 // could load oat files without checking the classpath, which would be incorrect.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700485 // Take the file only if it has no collisions, or we must take it because of preopting.
Mathieu Chartieradc90862018-05-11 13:03:06 -0700486 check_collision_result = CheckCollision(oat_file.get(), context.get(), /*out*/ &error_msg);
487 bool accept_oat_file = AcceptOatFile(check_collision_result);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700488 if (!accept_oat_file) {
489 // Failed the collision check. Print warning.
Mathieu Chartieradc90862018-05-11 13:03:06 -0700490 if (runtime->IsDexFileFallbackEnabled()) {
Narayan Kamath5c525742017-04-28 10:19:29 +0100491 if (!oat_file_assistant.HasOriginalDexFiles()) {
492 // We need to fallback but don't have original dex files. We have to
493 // fallback to opening the existing oat file. This is potentially
494 // unsafe so we warn about it.
495 accept_oat_file = true;
496
497 LOG(WARNING) << "Dex location " << dex_location << " does not seem to include dex file. "
498 << "Allow oat file use. This is potentially dangerous.";
499 } else {
500 // We have to fallback and found original dex files - extract them from an APK.
501 // Also warn about this operation because it's potentially wasteful.
502 LOG(WARNING) << "Found duplicate classes, falling back to extracting from APK : "
503 << dex_location;
504 LOG(WARNING) << "NOTE: This wastes RAM and hurts startup performance.";
505 }
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700506 } else {
Narayan Kamath5c525742017-04-28 10:19:29 +0100507 // TODO: We should remove this. The fact that we're here implies -Xno-dex-file-fallback
508 // was set, which means that we should never fallback. If we don't have original dex
509 // files, we should just fail resolution as the flag intended.
510 if (!oat_file_assistant.HasOriginalDexFiles()) {
511 accept_oat_file = true;
512 }
513
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700514 LOG(WARNING) << "Found duplicate classes, dex-file-fallback disabled, will be failing to "
515 " load classes for " << dex_location;
516 }
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700517
Narayan Kamath5c525742017-04-28 10:19:29 +0100518 LOG(WARNING) << error_msg;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700519 }
520
521 if (accept_oat_file) {
522 VLOG(class_linker) << "Registering " << oat_file->GetLocation();
523 source_oat_file = RegisterOatFile(std::move(oat_file));
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700524 *out_oat_file = source_oat_file;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700525 }
526 }
527
528 std::vector<std::unique_ptr<const DexFile>> dex_files;
529
530 // Load the dex files from the oat file.
531 if (source_oat_file != nullptr) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800532 bool added_image_space = false;
533 if (source_oat_file->IsExecutable()) {
Chris Wailes23866362018-08-22 16:16:58 -0700534 ScopedTrace app_image_timing("AppImage:Loading");
535
Alex Light2ce6fc82017-12-18 16:42:36 -0800536 // We need to throw away the image space if we are debuggable but the oat-file source of the
537 // image is not otherwise we might get classes with inlined methods or other such things.
538 std::unique_ptr<gc::space::ImageSpace> image_space;
Mathieu Chartieradc90862018-05-11 13:03:06 -0700539 if (ShouldLoadAppImage(check_collision_result,
540 source_oat_file,
541 context.get(),
542 &error_msg)) {
Alex Light2ce6fc82017-12-18 16:42:36 -0800543 image_space = oat_file_assistant.OpenImageSpace(source_oat_file);
Alex Light2ce6fc82017-12-18 16:42:36 -0800544 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800545 if (image_space != nullptr) {
546 ScopedObjectAccess soa(self);
547 StackHandleScope<1> hs(self);
548 Handle<mirror::ClassLoader> h_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700549 hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader)));
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800550 // Can not load app image without class loader.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800551 if (h_loader != nullptr) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800552 std::string temp_error_msg;
553 // Add image space has a race condition since other threads could be reading from the
554 // spaces array.
Mathieu Chartiera9d82fe2016-01-25 20:06:11 -0800555 {
556 ScopedThreadSuspension sts(self, kSuspended);
Mathieu Chartier61d2b2d2016-02-04 13:31:46 -0800557 gc::ScopedGCCriticalSection gcs(self,
558 gc::kGcCauseAddRemoveAppImageSpace,
559 gc::kCollectorTypeAddRemoveAppImageSpace);
Mathieu Chartiera9d82fe2016-01-25 20:06:11 -0800560 ScopedSuspendAll ssa("Add image space");
561 runtime->GetHeap()->AddSpace(image_space.get());
562 }
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800563 {
564 ScopedTrace trace2(StringPrintf("Adding image space for location %s", dex_location));
565 added_image_space = runtime->GetClassLinker()->AddImageSpace(image_space.get(),
566 h_loader,
567 dex_elements,
568 dex_location,
569 /*out*/&dex_files,
570 /*out*/&temp_error_msg);
571 }
Mathieu Chartiera6e81ed2016-02-25 13:52:10 -0800572 if (added_image_space) {
Mathieu Chartierbd064ea2016-02-11 16:27:18 -0800573 // Successfully added image space to heap, release the map so that it does not get
574 // freed.
Andreas Gampeafaf7f82018-10-16 11:32:38 -0700575 image_space.release(); // NOLINT b/117926937
Bharadwaj Kalandhabhatta0bb40312017-06-01 10:47:00 -0700576
577 // Register for tracking.
578 for (const auto& dex_file : dex_files) {
579 dex::tracking::RegisterDexFile(dex_file.get());
580 }
Mathieu Chartierbd064ea2016-02-11 16:27:18 -0800581 } else {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800582 LOG(INFO) << "Failed to add image file " << temp_error_msg;
583 dex_files.clear();
Mathieu Chartiera9d82fe2016-01-25 20:06:11 -0800584 {
585 ScopedThreadSuspension sts(self, kSuspended);
Mathieu Chartier61d2b2d2016-02-04 13:31:46 -0800586 gc::ScopedGCCriticalSection gcs(self,
587 gc::kGcCauseAddRemoveAppImageSpace,
588 gc::kCollectorTypeAddRemoveAppImageSpace);
Mathieu Chartiera9d82fe2016-01-25 20:06:11 -0800589 ScopedSuspendAll ssa("Remove image space");
590 runtime->GetHeap()->RemoveSpace(image_space.get());
591 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800592 // Non-fatal, don't update error_msg.
593 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800594 }
595 }
596 }
597 if (!added_image_space) {
598 DCHECK(dex_files.empty());
599 dex_files = oat_file_assistant.LoadDexFiles(*source_oat_file, dex_location);
Bharadwaj Kalandhabhatta0bb40312017-06-01 10:47:00 -0700600
601 // Register for tracking.
602 for (const auto& dex_file : dex_files) {
603 dex::tracking::RegisterDexFile(dex_file.get());
604 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800605 }
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700606 if (dex_files.empty()) {
607 error_msgs->push_back("Failed to open dex files from " + source_oat_file->GetLocation());
Mathieu Chartierbe8303d2017-08-17 17:39:39 -0700608 } else {
Mathieu Chartier120aa282017-08-05 16:03:03 -0700609 // Opened dex files from an oat file, madvise them to their loaded state.
610 for (const std::unique_ptr<const DexFile>& dex_file : dex_files) {
611 OatDexFile::MadviseDexFile(*dex_file, MadviseState::kMadviseStateAtLoad);
612 }
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700613 }
614 }
615
616 // Fall back to running out of the original dex file if we couldn't load any
617 // dex_files from the oat file.
618 if (dex_files.empty()) {
619 if (oat_file_assistant.HasOriginalDexFiles()) {
620 if (Runtime::Current()->IsDexFileFallbackEnabled()) {
Aart Bik37d6a3b2016-06-21 18:30:10 -0700621 static constexpr bool kVerifyChecksum = true;
David Sehr013fd802018-01-11 22:55:24 -0800622 const ArtDexFileLoader dex_file_loader;
623 if (!dex_file_loader.Open(dex_location,
624 dex_location,
625 Runtime::Current()->IsVerificationEnabled(),
626 kVerifyChecksum,
627 /*out*/ &error_msg,
628 &dex_files)) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700629 LOG(WARNING) << error_msg;
Alex Light3045b662016-04-20 14:26:34 -0700630 error_msgs->push_back("Failed to open dex files from " + std::string(dex_location)
631 + " because: " + error_msg);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700632 }
633 } else {
634 error_msgs->push_back("Fallback mode disabled, skipping dex files.");
635 }
636 } else {
637 error_msgs->push_back("No original dex files found for dex location "
638 + std::string(dex_location));
639 }
640 }
Calin Juravlec90bc922016-02-24 10:13:09 +0000641
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700642 return dex_files;
643}
644
Nicolas Geoffray68bf3902017-09-07 14:40:48 +0100645void OatFileManager::SetOnlyUseSystemOatFiles() {
646 ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
647 CHECK_EQ(oat_files_.size(), GetBootOatFiles().size());
648 only_use_system_oat_files_ = true;
649}
650
Nicolas Geoffray04680f32016-03-17 11:56:54 +0000651void OatFileManager::DumpForSigQuit(std::ostream& os) {
652 ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
653 std::vector<const OatFile*> boot_oat_files = GetBootOatFiles();
654 for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) {
655 if (ContainsElement(boot_oat_files, oat_file.get())) {
656 continue;
657 }
Andreas Gampe29d38e72016-03-23 15:31:51 +0000658 os << oat_file->GetLocation() << ": " << oat_file->GetCompilerFilter() << "\n";
Nicolas Geoffray04680f32016-03-17 11:56:54 +0000659 }
660}
661
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700662} // namespace art