blob: e950fca862654b04997f7fc4224a9dd02037b351 [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"
24
Andreas Gampe90b936d2017-01-31 08:58:55 -080025#include "art_field-inl.h"
Jeff Hao8ec0a202017-03-07 21:56:31 -080026#include "base/bit_vector-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070027#include "base/logging.h"
28#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080029#include "base/systrace.h"
Mathieu Chartierfbc31082016-01-24 11:59:56 -080030#include "class_linker.h"
Calin Juravle7b0648a2017-07-07 18:40:50 -070031#include "class_loader_context.h"
Mathieu Chartier80b37b72015-10-12 18:13:39 -070032#include "dex_file-inl.h"
Bharadwaj Kalandhabhatta0bb40312017-06-01 10:47:00 -070033#include "dex_file_tracking_registrar.h"
Mathieu Chartier61d2b2d2016-02-04 13:31:46 -080034#include "gc/scoped_gc_critical_section.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070035#include "gc/space/image_space.h"
Mathieu Chartierfbc31082016-01-24 11:59:56 -080036#include "handle_scope-inl.h"
Andreas Gampe08883de2016-11-08 13:20:52 -080037#include "jni_internal.h"
Mathieu Chartierfbc31082016-01-24 11:59:56 -080038#include "mirror/class_loader.h"
Andreas Gampe90b936d2017-01-31 08:58:55 -080039#include "mirror/object-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070040#include "oat_file_assistant.h"
Mathieu Chartier3398c782016-09-30 10:27:43 -070041#include "obj_ptr-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070042#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070043#include "thread-current-inl.h"
Mathieu Chartiera9d82fe2016-01-25 20:06:11 -080044#include "thread_list.h"
Andreas Gampe90b936d2017-01-31 08:58:55 -080045#include "well_known_classes.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070046
47namespace art {
48
Andreas Gampe46ee31b2016-12-14 10:11:49 -080049using android::base::StringPrintf;
50
Mathieu Chartierfbc31082016-01-24 11:59:56 -080051// If true, then we attempt to load the application image if it exists.
52static constexpr bool kEnableAppImage = true;
53
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070054const OatFile* OatFileManager::RegisterOatFile(std::unique_ptr<const OatFile> oat_file) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -070055 WriterMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070056 DCHECK(oat_file != nullptr);
57 if (kIsDebugBuild) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -070058 CHECK(oat_files_.find(oat_file) == oat_files_.end());
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070059 for (const std::unique_ptr<const OatFile>& existing : oat_files_) {
60 CHECK_NE(oat_file.get(), existing.get()) << oat_file->GetLocation();
61 // Check that we don't have an oat file with the same address. Copies of the same oat file
62 // should be loaded at different addresses.
63 CHECK_NE(oat_file->Begin(), existing->Begin()) << "Oat file already mapped at that location";
64 }
65 }
66 have_non_pic_oat_file_ = have_non_pic_oat_file_ || !oat_file->IsPic();
Mathieu Chartiere58991b2015-10-13 07:59:34 -070067 const OatFile* ret = oat_file.get();
68 oat_files_.insert(std::move(oat_file));
69 return ret;
70}
71
72void OatFileManager::UnRegisterAndDeleteOatFile(const OatFile* oat_file) {
73 WriterMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
74 DCHECK(oat_file != nullptr);
75 std::unique_ptr<const OatFile> compare(oat_file);
76 auto it = oat_files_.find(compare);
77 CHECK(it != oat_files_.end());
78 oat_files_.erase(it);
79 compare.release();
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070080}
81
Calin Juravle0b791272016-04-18 16:38:27 +010082const OatFile* OatFileManager::FindOpenedOatFileFromDexLocation(
83 const std::string& dex_base_location) const {
84 ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
85 for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) {
86 const std::vector<const OatDexFile*>& oat_dex_files = oat_file->GetOatDexFiles();
87 for (const OatDexFile* oat_dex_file : oat_dex_files) {
88 if (DexFile::GetBaseLocation(oat_dex_file->GetDexFileLocation()) == dex_base_location) {
89 return oat_file.get();
90 }
91 }
92 }
93 return nullptr;
94}
95
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070096const OatFile* OatFileManager::FindOpenedOatFileFromOatLocation(const std::string& oat_location)
97 const {
98 ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
Mathieu Chartiere58991b2015-10-13 07:59:34 -070099 return FindOpenedOatFileFromOatLocationLocked(oat_location);
100}
101
102const OatFile* OatFileManager::FindOpenedOatFileFromOatLocationLocked(
103 const std::string& oat_location) const {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700104 for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) {
105 if (oat_file->GetLocation() == oat_location) {
106 return oat_file.get();
107 }
108 }
109 return nullptr;
110}
111
Jeff Haodcdc85b2015-12-04 14:06:18 -0800112std::vector<const OatFile*> OatFileManager::GetBootOatFiles() const {
113 std::vector<const OatFile*> oat_files;
114 std::vector<gc::space::ImageSpace*> image_spaces =
115 Runtime::Current()->GetHeap()->GetBootImageSpaces();
116 for (gc::space::ImageSpace* image_space : image_spaces) {
117 oat_files.push_back(image_space->GetOatFile());
118 }
119 return oat_files;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700120}
121
122const OatFile* OatFileManager::GetPrimaryOatFile() const {
123 ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -0800124 std::vector<const OatFile*> boot_oat_files = GetBootOatFiles();
125 if (!boot_oat_files.empty()) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700126 for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800127 if (std::find(boot_oat_files.begin(), boot_oat_files.end(), oat_file.get()) ==
128 boot_oat_files.end()) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700129 return oat_file.get();
130 }
131 }
132 }
133 return nullptr;
134}
135
136OatFileManager::~OatFileManager() {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700137 // Explicitly clear oat_files_ since the OatFile destructor calls back into OatFileManager for
138 // UnRegisterOatFileLocation.
139 oat_files_.clear();
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700140}
141
Jeff Haodcdc85b2015-12-04 14:06:18 -0800142std::vector<const OatFile*> OatFileManager::RegisterImageOatFiles(
143 std::vector<gc::space::ImageSpace*> spaces) {
144 std::vector<const OatFile*> oat_files;
145 for (gc::space::ImageSpace* space : spaces) {
146 oat_files.push_back(RegisterOatFile(space->ReleaseOatFile()));
147 }
148 return oat_files;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700149}
150
Jeff Hao8ec0a202017-03-07 21:56:31 -0800151class TypeIndexInfo {
152 public:
153 explicit TypeIndexInfo(const DexFile* dex_file)
154 : type_indexes_(GenerateTypeIndexes(dex_file)),
155 iter_(type_indexes_.Indexes().begin()),
156 end_(type_indexes_.Indexes().end()) { }
157
158 BitVector& GetTypeIndexes() {
159 return type_indexes_;
160 }
161 BitVector::IndexIterator& GetIterator() {
162 return iter_;
163 }
164 BitVector::IndexIterator& GetIteratorEnd() {
165 return end_;
166 }
167 void AdvanceIterator() {
168 iter_++;
169 }
170
171 private:
172 static BitVector GenerateTypeIndexes(const DexFile* dex_file) {
173 BitVector type_indexes(/*start_bits*/0, /*expandable*/true, Allocator::GetMallocAllocator());
174 for (uint16_t i = 0; i < dex_file->NumClassDefs(); ++i) {
175 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
176 uint16_t type_idx = class_def.class_idx_.index_;
177 type_indexes.SetBit(type_idx);
178 }
179 return type_indexes;
180 }
181
182 // BitVector with bits set for the type indexes of all classes in the input dex file.
183 BitVector type_indexes_;
184 BitVector::IndexIterator iter_;
185 BitVector::IndexIterator end_;
186};
187
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700188class DexFileAndClassPair : ValueObject {
189 public:
Jeff Hao8ec0a202017-03-07 21:56:31 -0800190 DexFileAndClassPair(const DexFile* dex_file, TypeIndexInfo* type_info, bool from_loaded_oat)
191 : type_info_(type_info),
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700192 dex_file_(dex_file),
Jeff Hao8ec0a202017-03-07 21:56:31 -0800193 cached_descriptor_(dex_file_->StringByTypeIdx(dex::TypeIndex(*type_info->GetIterator()))),
194 from_loaded_oat_(from_loaded_oat) {
195 type_info_->AdvanceIterator();
196 }
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700197
Mathieu Chartier80b37b72015-10-12 18:13:39 -0700198 DexFileAndClassPair(const DexFileAndClassPair& rhs) = default;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700199
Mathieu Chartier80b37b72015-10-12 18:13:39 -0700200 DexFileAndClassPair& operator=(const DexFileAndClassPair& rhs) = default;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700201
202 const char* GetCachedDescriptor() const {
203 return cached_descriptor_;
204 }
205
206 bool operator<(const DexFileAndClassPair& rhs) const {
207 const int cmp = strcmp(cached_descriptor_, rhs.cached_descriptor_);
208 if (cmp != 0) {
209 // Note that the order must be reversed. We want to iterate over the classes in dex files.
210 // They are sorted lexicographically. Thus, the priority-queue must be a min-queue.
211 return cmp > 0;
212 }
213 return dex_file_ < rhs.dex_file_;
214 }
215
216 bool DexFileHasMoreClasses() const {
Jeff Hao8ec0a202017-03-07 21:56:31 -0800217 return type_info_->GetIterator() != type_info_->GetIteratorEnd();
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700218 }
219
220 void Next() {
Jeff Hao8ec0a202017-03-07 21:56:31 -0800221 cached_descriptor_ = dex_file_->StringByTypeIdx(dex::TypeIndex(*type_info_->GetIterator()));
222 type_info_->AdvanceIterator();
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700223 }
224
225 bool FromLoadedOat() const {
226 return from_loaded_oat_;
227 }
228
229 const DexFile* GetDexFile() const {
Jeff Haof0192c82016-03-28 20:39:50 -0700230 return dex_file_;
231 }
232
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700233 private:
Jeff Hao8ec0a202017-03-07 21:56:31 -0800234 TypeIndexInfo* type_info_;
Jeff Haof0192c82016-03-28 20:39:50 -0700235 const DexFile* dex_file_;
Jeff Hao8ec0a202017-03-07 21:56:31 -0800236 const char* cached_descriptor_;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700237 bool from_loaded_oat_; // We only need to compare mismatches between what we load now
238 // and what was loaded before. Any old duplicates must have been
239 // OK, and any new "internal" duplicates are as well (they must
240 // be from multidex, which resolves correctly).
241};
242
Jeff Hao8ec0a202017-03-07 21:56:31 -0800243static void AddDexFilesFromOat(
244 const OatFile* oat_file,
245 /*out*/std::vector<const DexFile*>* dex_files,
246 std::vector<std::unique_ptr<const DexFile>>* opened_dex_files) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700247 for (const OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
248 std::string error;
249 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error);
250 if (dex_file == nullptr) {
251 LOG(WARNING) << "Could not create dex file from oat file: " << error;
252 } else if (dex_file->NumClassDefs() > 0U) {
Jeff Hao8ec0a202017-03-07 21:56:31 -0800253 dex_files->push_back(dex_file.get());
Mathieu Chartier14d7b3e2016-06-09 16:18:04 -0700254 opened_dex_files->push_back(std::move(dex_file));
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700255 }
256 }
257}
258
Jeff Hao8ec0a202017-03-07 21:56:31 -0800259static void AddNext(/*inout*/DexFileAndClassPair& original,
260 /*inout*/std::priority_queue<DexFileAndClassPair>& heap) {
261 if (original.DexFileHasMoreClasses()) {
262 original.Next();
263 heap.push(std::move(original));
Jeff Haof0192c82016-03-28 20:39:50 -0700264 }
265}
266
Jeff Hao8ec0a202017-03-07 21:56:31 -0800267static bool CollisionCheck(std::vector<const DexFile*>& dex_files_loaded,
268 std::vector<const DexFile*>& dex_files_unloaded,
269 std::string* error_msg /*out*/) {
270 // Generate type index information for each dex file.
271 std::vector<TypeIndexInfo> loaded_types;
272 for (const DexFile* dex_file : dex_files_loaded) {
273 loaded_types.push_back(TypeIndexInfo(dex_file));
274 }
275 std::vector<TypeIndexInfo> unloaded_types;
276 for (const DexFile* dex_file : dex_files_unloaded) {
277 unloaded_types.push_back(TypeIndexInfo(dex_file));
278 }
279
280 // Populate the queue of dex file and class pairs with the loaded and unloaded dex files.
281 std::priority_queue<DexFileAndClassPair> queue;
282 for (size_t i = 0; i < dex_files_loaded.size(); ++i) {
283 if (loaded_types[i].GetIterator() != loaded_types[i].GetIteratorEnd()) {
284 queue.emplace(dex_files_loaded[i], &loaded_types[i], /*from_loaded_oat*/true);
285 }
286 }
287 for (size_t i = 0; i < dex_files_unloaded.size(); ++i) {
288 if (unloaded_types[i].GetIterator() != unloaded_types[i].GetIteratorEnd()) {
289 queue.emplace(dex_files_unloaded[i], &unloaded_types[i], /*from_loaded_oat*/false);
290 }
291 }
292
293 // Now drain the queue.
Jeff Hao0471ece2017-04-07 16:28:12 -0700294 bool has_duplicates = false;
295 error_msg->clear();
Jeff Hao8ec0a202017-03-07 21:56:31 -0800296 while (!queue.empty()) {
297 // Modifying the top element is only safe if we pop right after.
298 DexFileAndClassPair compare_pop(queue.top());
299 queue.pop();
300
301 // Compare against the following elements.
302 while (!queue.empty()) {
303 DexFileAndClassPair top(queue.top());
304 if (strcmp(compare_pop.GetCachedDescriptor(), top.GetCachedDescriptor()) == 0) {
305 // Same descriptor. Check whether it's crossing old-oat-files to new-oat-files.
306 if (compare_pop.FromLoadedOat() != top.FromLoadedOat()) {
Jeff Hao0471ece2017-04-07 16:28:12 -0700307 error_msg->append(
308 StringPrintf("Found duplicated class when checking oat files: '%s' in %s and %s\n",
Jeff Hao8ec0a202017-03-07 21:56:31 -0800309 compare_pop.GetCachedDescriptor(),
310 compare_pop.GetDexFile()->GetLocation().c_str(),
Jeff Hao0471ece2017-04-07 16:28:12 -0700311 top.GetDexFile()->GetLocation().c_str()));
312 if (!VLOG_IS_ON(oat)) {
313 return true;
314 }
315 has_duplicates = true;
Jeff Hao8ec0a202017-03-07 21:56:31 -0800316 }
317 queue.pop();
318 AddNext(top, queue);
319 } else {
320 // Something else. Done here.
321 break;
322 }
323 }
324 AddNext(compare_pop, queue);
325 }
326
Jeff Hao0471ece2017-04-07 16:28:12 -0700327 return has_duplicates;
Jeff Haof0192c82016-03-28 20:39:50 -0700328}
329
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700330// Check for class-def collisions in dex files.
331//
Jeff Haof0192c82016-03-28 20:39:50 -0700332// This first walks the class loader chain, getting all the dex files from the class loader. If
333// the class loader is null or one of the class loaders in the chain is unsupported, we collect
334// dex files from all open non-boot oat files to be safe.
335//
336// This first checks whether the shared libraries are in the expected order and the oat files
337// have the expected checksums. If so, we exit early. Otherwise, we do the collision check.
338//
339// The collision check works by maintaining a heap with one class from each dex file, sorted by the
340// class descriptor. Then a dex-file/class pair is continually removed from the heap and compared
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700341// against the following top element. If the descriptor is the same, it is now checked whether
342// the two elements agree on whether their dex file was from an already-loaded oat-file or the
343// new oat file. Any disagreement indicates a collision.
344bool OatFileManager::HasCollisions(const OatFile* oat_file,
Jeff Haof0192c82016-03-28 20:39:50 -0700345 jobject class_loader,
346 jobjectArray dex_elements,
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700347 std::string* error_msg /*out*/) const {
348 DCHECK(oat_file != nullptr);
349 DCHECK(error_msg != nullptr);
Jeff Haof0192c82016-03-28 20:39:50 -0700350
Calin Juravle3f918642017-07-11 19:04:20 -0700351 // If the class_loader is null there's not much we can do. This happens if a dex files is loaded
352 // directly with DexFile APIs instead of using class loaders.
353 if (class_loader == nullptr) {
354 LOG(WARNING) << "Opening an oat file without a class loader. "
355 << "Are you using the deprecated DexFile APIs?";
356 return false;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700357 }
358
Calin Juravle3f918642017-07-11 19:04:20 -0700359 std::unique_ptr<ClassLoaderContext> context =
360 ClassLoaderContext::CreateContextForClassLoader(class_loader, dex_elements);
361
362 // The context might be null if there are unrecognized class loaders in the chain or they
363 // don't meet sensible sanity conditions. In this case we assume that the app knows what it's
364 // doing and accept the oat file.
365 // Note that this has correctness implications as we cannot guarantee that the class resolution
366 // used during compilation is OK (b/37777332).
367 if (context == nullptr) {
368 LOG(WARNING) << "Skipping duplicate class check due to unsupported classloader";
Narayan Kamath5c525742017-04-28 10:19:29 +0100369 return false;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700370 }
371
Calin Juravle3f918642017-07-11 19:04:20 -0700372 // If the pat file loading context matches the context used during compilation then we accept
373 // the oat file without addition checks
374 if (context->VerifyClassLoaderContextMatch(
375 oat_file->GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey))) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700376 return false;
377 }
378
Calin Juravle3f918642017-07-11 19:04:20 -0700379 // The class loader context does not match. Perform a full duplicate classes check.
380
381 std::vector<const DexFile*> dex_files_loaded = context->FlattenOpenedDexFiles();
382
Narayan Kamath5c525742017-04-28 10:19:29 +0100383 // Vector that holds the newly opened dex files live, this is done to prevent leaks.
384 std::vector<std::unique_ptr<const DexFile>> opened_dex_files;
385
Andreas Gampe1fdbe1b2016-06-10 08:36:20 -0700386 ScopedTrace st("Collision check");
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700387 // Add dex files from the oat file to check.
Jeff Hao8ec0a202017-03-07 21:56:31 -0800388 std::vector<const DexFile*> dex_files_unloaded;
389 AddDexFilesFromOat(oat_file, &dex_files_unloaded, &opened_dex_files);
390 return CollisionCheck(dex_files_loaded, dex_files_unloaded, error_msg);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700391}
392
393std::vector<std::unique_ptr<const DexFile>> OatFileManager::OpenDexFilesFromOat(
394 const char* dex_location,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800395 jobject class_loader,
396 jobjectArray dex_elements,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700397 const OatFile** out_oat_file,
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700398 std::vector<std::string>* error_msgs) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800399 ScopedTrace trace(__FUNCTION__);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700400 CHECK(dex_location != nullptr);
401 CHECK(error_msgs != nullptr);
402
403 // Verify we aren't holding the mutator lock, which could starve GC if we
404 // have to generate or relocate an oat file.
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800405 Thread* const self = Thread::Current();
406 Locks::mutator_lock_->AssertNotHeld(self);
407 Runtime* const runtime = Runtime::Current();
Calin Juravleb077e152016-02-18 18:47:37 +0000408
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700409 OatFileAssistant oat_file_assistant(dex_location,
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700410 kRuntimeISA,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800411 !runtime->IsAotCompiler());
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700412
413 // Lock the target oat location to avoid races generating and loading the
414 // oat file.
415 std::string error_msg;
416 if (!oat_file_assistant.Lock(/*out*/&error_msg)) {
417 // Don't worry too much if this fails. If it does fail, it's unlikely we
418 // can generate an oat file anyway.
419 VLOG(class_linker) << "OatFileAssistant::Lock: " << error_msg;
420 }
421
422 const OatFile* source_oat_file = nullptr;
423
Richard Uhler01be6812016-05-17 10:34:52 -0700424 if (!oat_file_assistant.IsUpToDate()) {
425 // Update the oat file on disk if we can, based on the --compiler-filter
426 // option derived from the current runtime options.
427 // This may fail, but that's okay. Best effort is all that matters here.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700428 switch (oat_file_assistant.MakeUpToDate(/*profile_changed*/false, /*out*/ &error_msg)) {
Richard Uhler01be6812016-05-17 10:34:52 -0700429 case OatFileAssistant::kUpdateFailed:
430 LOG(WARNING) << error_msg;
431 break;
Richard Uhler1e860612016-03-30 12:17:55 -0700432
Richard Uhler01be6812016-05-17 10:34:52 -0700433 case OatFileAssistant::kUpdateNotAttempted:
434 // Avoid spamming the logs if we decided not to attempt making the oat
435 // file up to date.
436 VLOG(oat) << error_msg;
437 break;
Richard Uhler1e860612016-03-30 12:17:55 -0700438
Richard Uhler01be6812016-05-17 10:34:52 -0700439 case OatFileAssistant::kUpdateSucceeded:
440 // Nothing to do.
441 break;
442 }
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700443 }
444
445 // Get the oat file on disk.
446 std::unique_ptr<const OatFile> oat_file(oat_file_assistant.GetBestOatFile().release());
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800447
Jeff Hao0cb17282017-07-12 14:51:49 -0700448 // Prevent oat files from being loaded if no class_loader or dex_elements are provided.
449 // This can happen when the deprecated DexFile.<init>(String) is called directly, and it
450 // could load oat files without checking the classpath, which would be incorrect.
451 if ((class_loader != nullptr || dex_elements != nullptr) && oat_file != nullptr) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700452 // Take the file only if it has no collisions, or we must take it because of preopting.
Jeff Haof0192c82016-03-28 20:39:50 -0700453 bool accept_oat_file =
454 !HasCollisions(oat_file.get(), class_loader, dex_elements, /*out*/ &error_msg);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700455 if (!accept_oat_file) {
456 // Failed the collision check. Print warning.
457 if (Runtime::Current()->IsDexFileFallbackEnabled()) {
Narayan Kamath5c525742017-04-28 10:19:29 +0100458 if (!oat_file_assistant.HasOriginalDexFiles()) {
459 // We need to fallback but don't have original dex files. We have to
460 // fallback to opening the existing oat file. This is potentially
461 // unsafe so we warn about it.
462 accept_oat_file = true;
463
464 LOG(WARNING) << "Dex location " << dex_location << " does not seem to include dex file. "
465 << "Allow oat file use. This is potentially dangerous.";
466 } else {
467 // We have to fallback and found original dex files - extract them from an APK.
468 // Also warn about this operation because it's potentially wasteful.
469 LOG(WARNING) << "Found duplicate classes, falling back to extracting from APK : "
470 << dex_location;
471 LOG(WARNING) << "NOTE: This wastes RAM and hurts startup performance.";
472 }
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700473 } else {
Narayan Kamath5c525742017-04-28 10:19:29 +0100474 // TODO: We should remove this. The fact that we're here implies -Xno-dex-file-fallback
475 // was set, which means that we should never fallback. If we don't have original dex
476 // files, we should just fail resolution as the flag intended.
477 if (!oat_file_assistant.HasOriginalDexFiles()) {
478 accept_oat_file = true;
479 }
480
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700481 LOG(WARNING) << "Found duplicate classes, dex-file-fallback disabled, will be failing to "
482 " load classes for " << dex_location;
483 }
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700484
Narayan Kamath5c525742017-04-28 10:19:29 +0100485 LOG(WARNING) << error_msg;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700486 }
487
488 if (accept_oat_file) {
489 VLOG(class_linker) << "Registering " << oat_file->GetLocation();
490 source_oat_file = RegisterOatFile(std::move(oat_file));
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700491 *out_oat_file = source_oat_file;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700492 }
493 }
494
495 std::vector<std::unique_ptr<const DexFile>> dex_files;
496
497 // Load the dex files from the oat file.
498 if (source_oat_file != nullptr) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800499 bool added_image_space = false;
500 if (source_oat_file->IsExecutable()) {
Andreas Gampea463b6a2016-08-12 21:53:32 -0700501 std::unique_ptr<gc::space::ImageSpace> image_space =
502 kEnableAppImage ? oat_file_assistant.OpenImageSpace(source_oat_file) : nullptr;
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800503 if (image_space != nullptr) {
504 ScopedObjectAccess soa(self);
505 StackHandleScope<1> hs(self);
506 Handle<mirror::ClassLoader> h_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700507 hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader)));
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800508 // Can not load app image without class loader.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800509 if (h_loader != nullptr) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800510 std::string temp_error_msg;
511 // Add image space has a race condition since other threads could be reading from the
512 // spaces array.
Mathieu Chartiera9d82fe2016-01-25 20:06:11 -0800513 {
514 ScopedThreadSuspension sts(self, kSuspended);
Mathieu Chartier61d2b2d2016-02-04 13:31:46 -0800515 gc::ScopedGCCriticalSection gcs(self,
516 gc::kGcCauseAddRemoveAppImageSpace,
517 gc::kCollectorTypeAddRemoveAppImageSpace);
Mathieu Chartiera9d82fe2016-01-25 20:06:11 -0800518 ScopedSuspendAll ssa("Add image space");
519 runtime->GetHeap()->AddSpace(image_space.get());
520 }
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800521 {
522 ScopedTrace trace2(StringPrintf("Adding image space for location %s", dex_location));
523 added_image_space = runtime->GetClassLinker()->AddImageSpace(image_space.get(),
524 h_loader,
525 dex_elements,
526 dex_location,
527 /*out*/&dex_files,
528 /*out*/&temp_error_msg);
529 }
Mathieu Chartiera6e81ed2016-02-25 13:52:10 -0800530 if (added_image_space) {
Mathieu Chartierbd064ea2016-02-11 16:27:18 -0800531 // Successfully added image space to heap, release the map so that it does not get
532 // freed.
533 image_space.release();
Bharadwaj Kalandhabhatta0bb40312017-06-01 10:47:00 -0700534
535 // Register for tracking.
536 for (const auto& dex_file : dex_files) {
537 dex::tracking::RegisterDexFile(dex_file.get());
538 }
Mathieu Chartierbd064ea2016-02-11 16:27:18 -0800539 } else {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800540 LOG(INFO) << "Failed to add image file " << temp_error_msg;
541 dex_files.clear();
Mathieu Chartiera9d82fe2016-01-25 20:06:11 -0800542 {
543 ScopedThreadSuspension sts(self, kSuspended);
Mathieu Chartier61d2b2d2016-02-04 13:31:46 -0800544 gc::ScopedGCCriticalSection gcs(self,
545 gc::kGcCauseAddRemoveAppImageSpace,
546 gc::kCollectorTypeAddRemoveAppImageSpace);
Mathieu Chartiera9d82fe2016-01-25 20:06:11 -0800547 ScopedSuspendAll ssa("Remove image space");
548 runtime->GetHeap()->RemoveSpace(image_space.get());
549 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800550 // Non-fatal, don't update error_msg.
551 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800552 }
553 }
554 }
555 if (!added_image_space) {
556 DCHECK(dex_files.empty());
557 dex_files = oat_file_assistant.LoadDexFiles(*source_oat_file, dex_location);
Bharadwaj Kalandhabhatta0bb40312017-06-01 10:47:00 -0700558
559 // Register for tracking.
560 for (const auto& dex_file : dex_files) {
561 dex::tracking::RegisterDexFile(dex_file.get());
562 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800563 }
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700564 if (dex_files.empty()) {
565 error_msgs->push_back("Failed to open dex files from " + source_oat_file->GetLocation());
566 }
567 }
568
569 // Fall back to running out of the original dex file if we couldn't load any
570 // dex_files from the oat file.
571 if (dex_files.empty()) {
572 if (oat_file_assistant.HasOriginalDexFiles()) {
573 if (Runtime::Current()->IsDexFileFallbackEnabled()) {
Aart Bik37d6a3b2016-06-21 18:30:10 -0700574 static constexpr bool kVerifyChecksum = true;
575 if (!DexFile::Open(
576 dex_location, dex_location, kVerifyChecksum, /*out*/ &error_msg, &dex_files)) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700577 LOG(WARNING) << error_msg;
Alex Light3045b662016-04-20 14:26:34 -0700578 error_msgs->push_back("Failed to open dex files from " + std::string(dex_location)
579 + " because: " + error_msg);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700580 }
581 } else {
582 error_msgs->push_back("Fallback mode disabled, skipping dex files.");
583 }
584 } else {
585 error_msgs->push_back("No original dex files found for dex location "
586 + std::string(dex_location));
587 }
588 }
Calin Juravlec90bc922016-02-24 10:13:09 +0000589
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700590 return dex_files;
591}
592
Nicolas Geoffray04680f32016-03-17 11:56:54 +0000593void OatFileManager::DumpForSigQuit(std::ostream& os) {
594 ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
595 std::vector<const OatFile*> boot_oat_files = GetBootOatFiles();
596 for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) {
597 if (ContainsElement(boot_oat_files, oat_file.get())) {
598 continue;
599 }
Andreas Gampe29d38e72016-03-23 15:31:51 +0000600 os << oat_file->GetLocation() << ": " << oat_file->GetCompilerFilter() << "\n";
Nicolas Geoffray04680f32016-03-17 11:56:54 +0000601 }
602}
603
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700604} // namespace art