blob: b5fdcbde2d037b2486dcd42214659ace12e4e1fd [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
23#include "base/logging.h"
24#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080025#include "base/systrace.h"
Mathieu Chartierfbc31082016-01-24 11:59:56 -080026#include "class_linker.h"
Mathieu Chartier80b37b72015-10-12 18:13:39 -070027#include "dex_file-inl.h"
Mathieu Chartier61d2b2d2016-02-04 13:31:46 -080028#include "gc/scoped_gc_critical_section.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070029#include "gc/space/image_space.h"
Mathieu Chartierfbc31082016-01-24 11:59:56 -080030#include "handle_scope-inl.h"
31#include "mirror/class_loader.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070032#include "oat_file_assistant.h"
Mathieu Chartierfbc31082016-01-24 11:59:56 -080033#include "scoped_thread_state_change.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070034#include "thread-inl.h"
Mathieu Chartiera9d82fe2016-01-25 20:06:11 -080035#include "thread_list.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070036
37namespace art {
38
Mathieu Chartierfbc31082016-01-24 11:59:56 -080039// If true, then we attempt to load the application image if it exists.
40static constexpr bool kEnableAppImage = true;
41
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070042const OatFile* OatFileManager::RegisterOatFile(std::unique_ptr<const OatFile> oat_file) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -070043 WriterMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070044 DCHECK(oat_file != nullptr);
45 if (kIsDebugBuild) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -070046 CHECK(oat_files_.find(oat_file) == oat_files_.end());
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070047 for (const std::unique_ptr<const OatFile>& existing : oat_files_) {
48 CHECK_NE(oat_file.get(), existing.get()) << oat_file->GetLocation();
49 // Check that we don't have an oat file with the same address. Copies of the same oat file
50 // should be loaded at different addresses.
51 CHECK_NE(oat_file->Begin(), existing->Begin()) << "Oat file already mapped at that location";
52 }
53 }
54 have_non_pic_oat_file_ = have_non_pic_oat_file_ || !oat_file->IsPic();
Mathieu Chartiere58991b2015-10-13 07:59:34 -070055 const OatFile* ret = oat_file.get();
56 oat_files_.insert(std::move(oat_file));
57 return ret;
58}
59
60void OatFileManager::UnRegisterAndDeleteOatFile(const OatFile* oat_file) {
61 WriterMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
62 DCHECK(oat_file != nullptr);
63 std::unique_ptr<const OatFile> compare(oat_file);
64 auto it = oat_files_.find(compare);
65 CHECK(it != oat_files_.end());
66 oat_files_.erase(it);
67 compare.release();
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070068}
69
Calin Juravle0b791272016-04-18 16:38:27 +010070const OatFile* OatFileManager::FindOpenedOatFileFromDexLocation(
71 const std::string& dex_base_location) const {
72 ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
73 for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) {
74 const std::vector<const OatDexFile*>& oat_dex_files = oat_file->GetOatDexFiles();
75 for (const OatDexFile* oat_dex_file : oat_dex_files) {
76 if (DexFile::GetBaseLocation(oat_dex_file->GetDexFileLocation()) == dex_base_location) {
77 return oat_file.get();
78 }
79 }
80 }
81 return nullptr;
82}
83
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070084const OatFile* OatFileManager::FindOpenedOatFileFromOatLocation(const std::string& oat_location)
85 const {
86 ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
Mathieu Chartiere58991b2015-10-13 07:59:34 -070087 return FindOpenedOatFileFromOatLocationLocked(oat_location);
88}
89
90const OatFile* OatFileManager::FindOpenedOatFileFromOatLocationLocked(
91 const std::string& oat_location) const {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070092 for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) {
93 if (oat_file->GetLocation() == oat_location) {
94 return oat_file.get();
95 }
96 }
97 return nullptr;
98}
99
Jeff Haodcdc85b2015-12-04 14:06:18 -0800100std::vector<const OatFile*> OatFileManager::GetBootOatFiles() const {
101 std::vector<const OatFile*> oat_files;
102 std::vector<gc::space::ImageSpace*> image_spaces =
103 Runtime::Current()->GetHeap()->GetBootImageSpaces();
104 for (gc::space::ImageSpace* image_space : image_spaces) {
105 oat_files.push_back(image_space->GetOatFile());
106 }
107 return oat_files;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700108}
109
110const OatFile* OatFileManager::GetPrimaryOatFile() const {
111 ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -0800112 std::vector<const OatFile*> boot_oat_files = GetBootOatFiles();
113 if (!boot_oat_files.empty()) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700114 for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800115 if (std::find(boot_oat_files.begin(), boot_oat_files.end(), oat_file.get()) ==
116 boot_oat_files.end()) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700117 return oat_file.get();
118 }
119 }
120 }
121 return nullptr;
122}
123
124OatFileManager::~OatFileManager() {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700125 // Explicitly clear oat_files_ since the OatFile destructor calls back into OatFileManager for
126 // UnRegisterOatFileLocation.
127 oat_files_.clear();
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700128}
129
Jeff Haodcdc85b2015-12-04 14:06:18 -0800130std::vector<const OatFile*> OatFileManager::RegisterImageOatFiles(
131 std::vector<gc::space::ImageSpace*> spaces) {
132 std::vector<const OatFile*> oat_files;
133 for (gc::space::ImageSpace* space : spaces) {
134 oat_files.push_back(RegisterOatFile(space->ReleaseOatFile()));
135 }
136 return oat_files;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700137}
138
139class DexFileAndClassPair : ValueObject {
140 public:
141 DexFileAndClassPair(const DexFile* dex_file, size_t current_class_index, bool from_loaded_oat)
142 : cached_descriptor_(GetClassDescriptor(dex_file, current_class_index)),
143 dex_file_(dex_file),
144 current_class_index_(current_class_index),
145 from_loaded_oat_(from_loaded_oat) {}
146
Mathieu Chartier80b37b72015-10-12 18:13:39 -0700147 DexFileAndClassPair(const DexFileAndClassPair& rhs) = default;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700148
Mathieu Chartier80b37b72015-10-12 18:13:39 -0700149 DexFileAndClassPair& operator=(const DexFileAndClassPair& rhs) = default;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700150
151 const char* GetCachedDescriptor() const {
152 return cached_descriptor_;
153 }
154
155 bool operator<(const DexFileAndClassPair& rhs) const {
156 const int cmp = strcmp(cached_descriptor_, rhs.cached_descriptor_);
157 if (cmp != 0) {
158 // Note that the order must be reversed. We want to iterate over the classes in dex files.
159 // They are sorted lexicographically. Thus, the priority-queue must be a min-queue.
160 return cmp > 0;
161 }
162 return dex_file_ < rhs.dex_file_;
163 }
164
165 bool DexFileHasMoreClasses() const {
166 return current_class_index_ + 1 < dex_file_->NumClassDefs();
167 }
168
169 void Next() {
170 ++current_class_index_;
Jeff Haof0192c82016-03-28 20:39:50 -0700171 cached_descriptor_ = GetClassDescriptor(dex_file_, current_class_index_);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700172 }
173
174 size_t GetCurrentClassIndex() const {
175 return current_class_index_;
176 }
177
178 bool FromLoadedOat() const {
179 return from_loaded_oat_;
180 }
181
182 const DexFile* GetDexFile() const {
Jeff Haof0192c82016-03-28 20:39:50 -0700183 return dex_file_;
184 }
185
186 void DeleteDexFile() {
187 delete dex_file_;
188 dex_file_ = nullptr;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700189 }
190
191 private:
192 static const char* GetClassDescriptor(const DexFile* dex_file, size_t index) {
193 DCHECK(IsUint<16>(index));
194 const DexFile::ClassDef& class_def = dex_file->GetClassDef(static_cast<uint16_t>(index));
195 return dex_file->StringByTypeIdx(class_def.class_idx_);
196 }
197
198 const char* cached_descriptor_;
Jeff Haof0192c82016-03-28 20:39:50 -0700199 const DexFile* dex_file_;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700200 size_t current_class_index_;
201 bool from_loaded_oat_; // We only need to compare mismatches between what we load now
202 // and what was loaded before. Any old duplicates must have been
203 // OK, and any new "internal" duplicates are as well (they must
204 // be from multidex, which resolves correctly).
205};
206
207static void AddDexFilesFromOat(const OatFile* oat_file,
208 bool already_loaded,
209 /*out*/std::priority_queue<DexFileAndClassPair>* heap) {
210 for (const OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
211 std::string error;
212 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error);
213 if (dex_file == nullptr) {
214 LOG(WARNING) << "Could not create dex file from oat file: " << error;
215 } else if (dex_file->NumClassDefs() > 0U) {
216 heap->emplace(dex_file.release(), /*current_class_index*/0U, already_loaded);
217 }
218 }
219}
220
221static void AddNext(/*inout*/DexFileAndClassPair* original,
Jeff Haof0192c82016-03-28 20:39:50 -0700222 /*inout*/std::priority_queue<DexFileAndClassPair>* heap,
223 bool owning_dex_files) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700224 if (original->DexFileHasMoreClasses()) {
225 original->Next();
226 heap->push(std::move(*original));
Jeff Haof0192c82016-03-28 20:39:50 -0700227 } else if (owning_dex_files) {
228 original->DeleteDexFile();
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700229 }
230}
231
Jeff Haof0192c82016-03-28 20:39:50 -0700232static void FreeDexFilesInHeap(std::priority_queue<DexFileAndClassPair>* heap,
233 bool owning_dex_files) {
234 if (owning_dex_files) {
235 while (!heap->empty()) {
236 delete heap->top().GetDexFile();
237 heap->pop();
238 }
239 }
240}
241
242static void IterateOverJavaDexFile(mirror::Object* dex_file,
243 ArtField* const cookie_field,
244 std::function<bool(const DexFile*)> fn)
245 SHARED_REQUIRES(Locks::mutator_lock_) {
246 if (dex_file != nullptr) {
247 mirror::LongArray* long_array = cookie_field->GetObject(dex_file)->AsLongArray();
248 if (long_array == nullptr) {
249 // This should never happen so log a warning.
250 LOG(WARNING) << "Null DexFile::mCookie";
251 return;
252 }
253 int32_t long_array_size = long_array->GetLength();
254 // Start from 1 to skip the oat file.
255 for (int32_t j = 1; j < long_array_size; ++j) {
256 const DexFile* cp_dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(
257 long_array->GetWithoutChecks(j)));
258 if (!fn(cp_dex_file)) {
259 return;
260 }
261 }
262 }
263}
264
265static void IterateOverPathClassLoader(
266 ScopedObjectAccessAlreadyRunnable& soa,
267 Handle<mirror::ClassLoader> class_loader,
268 MutableHandle<mirror::ObjectArray<mirror::Object>> dex_elements,
269 std::function<bool(const DexFile*)> fn) SHARED_REQUIRES(Locks::mutator_lock_) {
270 // Handle this step.
271 // Handle as if this is the child PathClassLoader.
272 // The class loader is a PathClassLoader which inherits from BaseDexClassLoader.
273 // We need to get the DexPathList and loop through it.
274 ArtField* const cookie_field = soa.DecodeField(WellKnownClasses::dalvik_system_DexFile_cookie);
275 ArtField* const dex_file_field =
276 soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile);
277 mirror::Object* dex_path_list =
278 soa.DecodeField(WellKnownClasses::dalvik_system_PathClassLoader_pathList)->
279 GetObject(class_loader.Get());
280 if (dex_path_list != nullptr && dex_file_field != nullptr && cookie_field != nullptr) {
281 // DexPathList has an array dexElements of Elements[] which each contain a dex file.
282 mirror::Object* dex_elements_obj =
283 soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList_dexElements)->
284 GetObject(dex_path_list);
285 // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look
286 // at the mCookie which is a DexFile vector.
287 if (dex_elements_obj != nullptr) {
288 dex_elements.Assign(dex_elements_obj->AsObjectArray<mirror::Object>());
289 for (int32_t i = 0; i < dex_elements->GetLength(); ++i) {
290 mirror::Object* element = dex_elements->GetWithoutChecks(i);
291 if (element == nullptr) {
292 // Should never happen, fall back to java code to throw a NPE.
293 break;
294 }
295 mirror::Object* dex_file = dex_file_field->GetObject(element);
296 IterateOverJavaDexFile(dex_file, cookie_field, fn);
297 }
298 }
299 }
300}
301
302static bool GetDexFilesFromClassLoader(
303 ScopedObjectAccessAlreadyRunnable& soa,
304 mirror::ClassLoader* class_loader,
305 std::priority_queue<DexFileAndClassPair>* queue) SHARED_REQUIRES(Locks::mutator_lock_) {
306 if (ClassLinker::IsBootClassLoader(soa, class_loader)) {
307 // The boot class loader. We don't load any of these files, as we know we compiled against
308 // them correctly.
309 return true;
310 }
311
312 // Unsupported class-loader?
313 if (class_loader->GetClass() !=
314 soa.Decode<mirror::Class*>(WellKnownClasses::dalvik_system_PathClassLoader)) {
315 VLOG(class_linker) << "Unsupported class-loader " << PrettyClass(class_loader->GetClass());
316 return false;
317 }
318
319 bool recursive_result = GetDexFilesFromClassLoader(soa, class_loader->GetParent(), queue);
320 if (!recursive_result) {
321 // Something wrong up the chain.
322 return false;
323 }
324
325 // Collect all the dex files.
326 auto GetDexFilesFn = [&] (const DexFile* cp_dex_file)
327 SHARED_REQUIRES(Locks::mutator_lock_) {
328 if (cp_dex_file->NumClassDefs() > 0) {
329 queue->emplace(cp_dex_file, 0U, true);
330 }
331 return true; // Continue looking.
332 };
333
334 // Handle for dex-cache-element.
335 StackHandleScope<3> hs(soa.Self());
336 MutableHandle<mirror::ObjectArray<mirror::Object>> dex_elements(
337 hs.NewHandle<mirror::ObjectArray<mirror::Object>>(nullptr));
338 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
339
340 IterateOverPathClassLoader(soa, h_class_loader, dex_elements, GetDexFilesFn);
341
342 return true;
343}
344
345static void GetDexFilesFromDexElementsArray(
346 ScopedObjectAccessAlreadyRunnable& soa,
347 Handle<mirror::ObjectArray<mirror::Object>> dex_elements,
348 std::priority_queue<DexFileAndClassPair>* queue) SHARED_REQUIRES(Locks::mutator_lock_) {
349 if (dex_elements.Get() == nullptr) {
350 // Nothing to do.
351 return;
352 }
353
354 ArtField* const cookie_field = soa.DecodeField(WellKnownClasses::dalvik_system_DexFile_cookie);
355 ArtField* const dex_file_field =
356 soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile);
357 const mirror::Class* const element_class = soa.Decode<mirror::Class*>(
358 WellKnownClasses::dalvik_system_DexPathList__Element);
359 const mirror::Class* const dexfile_class = soa.Decode<mirror::Class*>(
360 WellKnownClasses::dalvik_system_DexFile);
361
362 // Collect all the dex files.
363 auto GetDexFilesFn = [&] (const DexFile* cp_dex_file)
364 SHARED_REQUIRES(Locks::mutator_lock_) {
365 if (cp_dex_file != nullptr && cp_dex_file->NumClassDefs() > 0) {
366 queue->emplace(cp_dex_file, 0U, true);
367 }
368 return true; // Continue looking.
369 };
370
371 for (int32_t i = 0; i < dex_elements->GetLength(); ++i) {
372 mirror::Object* element = dex_elements->GetWithoutChecks(i);
373 if (element == nullptr) {
374 continue;
375 }
376
377 // We support this being dalvik.system.DexPathList$Element and dalvik.system.DexFile.
378
379 mirror::Object* dex_file;
380 if (element->GetClass() == element_class) {
381 dex_file = dex_file_field->GetObject(element);
382 } else if (element->GetClass() == dexfile_class) {
383 dex_file = element;
384 } else {
385 LOG(WARNING) << "Unsupported element in dex_elements: " << PrettyClass(element->GetClass());
386 continue;
387 }
388
389 IterateOverJavaDexFile(dex_file, cookie_field, GetDexFilesFn);
390 }
391}
392
393static bool AreSharedLibrariesOk(const std::string shared_libraries,
394 std::priority_queue<DexFileAndClassPair>& queue) {
395 if (shared_libraries.empty()) {
396 if (queue.empty()) {
397 // No shared libraries or oat files, as expected.
398 return true;
399 }
400 } else {
401 if (shared_libraries.compare(OatFile::kSpecialSharedLibrary) == 0) {
402 // If we find the special shared library, skip the shared libraries check.
403 return true;
404 }
405 // Shared libraries is a series of dex file paths and their checksums, each separated by '*'.
406 std::vector<std::string> shared_libraries_split;
407 Split(shared_libraries, '*', &shared_libraries_split);
408
409 size_t index = 0;
410 std::priority_queue<DexFileAndClassPair> temp = queue;
411 while (!temp.empty() && index < shared_libraries_split.size() - 1) {
412 DexFileAndClassPair pair(temp.top());
413 const DexFile* dex_file = pair.GetDexFile();
414 std::string dex_filename(dex_file->GetLocation());
415 uint32_t dex_checksum = dex_file->GetLocationChecksum();
416 if (dex_filename != shared_libraries_split[index] ||
417 dex_checksum != std::stoul(shared_libraries_split[index + 1])) {
418 break;
419 }
420 temp.pop();
421 index += 2;
422 }
423
424 // Check is successful if it made it through the queue and all the shared libraries.
425 return temp.empty() && index == shared_libraries_split.size();
426 }
427 return false;
428}
429
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700430// Check for class-def collisions in dex files.
431//
Jeff Haof0192c82016-03-28 20:39:50 -0700432// This first walks the class loader chain, getting all the dex files from the class loader. If
433// the class loader is null or one of the class loaders in the chain is unsupported, we collect
434// dex files from all open non-boot oat files to be safe.
435//
436// This first checks whether the shared libraries are in the expected order and the oat files
437// have the expected checksums. If so, we exit early. Otherwise, we do the collision check.
438//
439// The collision check works by maintaining a heap with one class from each dex file, sorted by the
440// class descriptor. Then a dex-file/class pair is continually removed from the heap and compared
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700441// against the following top element. If the descriptor is the same, it is now checked whether
442// the two elements agree on whether their dex file was from an already-loaded oat-file or the
443// new oat file. Any disagreement indicates a collision.
444bool OatFileManager::HasCollisions(const OatFile* oat_file,
Jeff Haof0192c82016-03-28 20:39:50 -0700445 jobject class_loader,
446 jobjectArray dex_elements,
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700447 std::string* error_msg /*out*/) const {
448 DCHECK(oat_file != nullptr);
449 DCHECK(error_msg != nullptr);
Jeff Haof0192c82016-03-28 20:39:50 -0700450
451 std::priority_queue<DexFileAndClassPair> queue;
452 bool owning_dex_files = false;
453
454 // Try to get dex files from the given class loader. If the class loader is null, or we do
455 // not support one of the class loaders in the chain, conservatively compare against all
456 // (non-boot) oat files.
457 bool class_loader_ok = false;
458 {
459 ScopedObjectAccess soa(Thread::Current());
460 StackHandleScope<2> hs(Thread::Current());
461 Handle<mirror::ClassLoader> h_class_loader =
462 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(class_loader));
463 Handle<mirror::ObjectArray<mirror::Object>> h_dex_elements =
464 hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Object>*>(dex_elements));
465 if (h_class_loader.Get() != nullptr &&
466 GetDexFilesFromClassLoader(soa, h_class_loader.Get(), &queue)) {
467 class_loader_ok = true;
468
469 // In this case, also take into account the dex_elements array, if given. We don't need to
470 // read it otherwise, as we'll compare against all open oat files anyways.
471 GetDexFilesFromDexElementsArray(soa, h_dex_elements, &queue);
472 } else if (h_class_loader.Get() != nullptr) {
473 VLOG(class_linker) << "Something unsupported with "
474 << PrettyClass(h_class_loader->GetClass());
475 }
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700476 }
477
478 // Dex files are registered late - once a class is actually being loaded. We have to compare
479 // against the open oat files. Take the oat_file_manager_lock_ that protects oat_files_ accesses.
480 ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
481
Jeff Haof0192c82016-03-28 20:39:50 -0700482 if (!class_loader_ok) {
483 // Add dex files from already loaded oat files, but skip boot.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700484
Jeff Haof0192c82016-03-28 20:39:50 -0700485 // Clean up the queue.
486 while (!queue.empty()) {
487 queue.pop();
488 }
489
490 // Anything we load now is something we own and must be released later.
491 owning_dex_files = true;
492
493 std::vector<const OatFile*> boot_oat_files = GetBootOatFiles();
494 // The same OatFile can be loaded multiple times at different addresses. In this case, we don't
495 // need to check both against each other since they would have resolved the same way at compile
496 // time.
497 std::unordered_set<std::string> unique_locations;
498 for (const std::unique_ptr<const OatFile>& loaded_oat_file : oat_files_) {
499 DCHECK_NE(loaded_oat_file.get(), oat_file);
500 const std::string& location = loaded_oat_file->GetLocation();
501 if (std::find(boot_oat_files.begin(), boot_oat_files.end(), loaded_oat_file.get()) ==
502 boot_oat_files.end() && location != oat_file->GetLocation() &&
503 unique_locations.find(location) == unique_locations.end()) {
504 unique_locations.insert(location);
505 AddDexFilesFromOat(loaded_oat_file.get(), /*already_loaded*/true, &queue);
506 }
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700507 }
508 }
509
Jeff Haof0192c82016-03-28 20:39:50 -0700510 // Exit if shared libraries are ok. Do a full duplicate classes check otherwise.
511 const std::string
512 shared_libraries(oat_file->GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey));
513 if (AreSharedLibrariesOk(shared_libraries, queue)) {
514 FreeDexFilesInHeap(&queue, owning_dex_files);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700515 return false;
516 }
517
518 // Add dex files from the oat file to check.
519 AddDexFilesFromOat(oat_file, /*already_loaded*/false, &queue);
520
521 // Now drain the queue.
522 while (!queue.empty()) {
523 // Modifying the top element is only safe if we pop right after.
Mathieu Chartier80b37b72015-10-12 18:13:39 -0700524 DexFileAndClassPair compare_pop(queue.top());
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700525 queue.pop();
526
527 // Compare against the following elements.
528 while (!queue.empty()) {
Mathieu Chartier80b37b72015-10-12 18:13:39 -0700529 DexFileAndClassPair top(queue.top());
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700530
531 if (strcmp(compare_pop.GetCachedDescriptor(), top.GetCachedDescriptor()) == 0) {
532 // Same descriptor. Check whether it's crossing old-oat-files to new-oat-files.
533 if (compare_pop.FromLoadedOat() != top.FromLoadedOat()) {
534 *error_msg =
535 StringPrintf("Found duplicated class when checking oat files: '%s' in %s and %s",
536 compare_pop.GetCachedDescriptor(),
537 compare_pop.GetDexFile()->GetLocation().c_str(),
538 top.GetDexFile()->GetLocation().c_str());
Jeff Haof0192c82016-03-28 20:39:50 -0700539 FreeDexFilesInHeap(&queue, owning_dex_files);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700540 return true;
541 }
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700542 queue.pop();
Jeff Haof0192c82016-03-28 20:39:50 -0700543 AddNext(&top, &queue, owning_dex_files);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700544 } else {
545 // Something else. Done here.
546 break;
547 }
548 }
Jeff Haof0192c82016-03-28 20:39:50 -0700549 AddNext(&compare_pop, &queue, owning_dex_files);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700550 }
551
552 return false;
553}
554
555std::vector<std::unique_ptr<const DexFile>> OatFileManager::OpenDexFilesFromOat(
556 const char* dex_location,
557 const char* oat_location,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800558 jobject class_loader,
559 jobjectArray dex_elements,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700560 const OatFile** out_oat_file,
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700561 std::vector<std::string>* error_msgs) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800562 ScopedTrace trace(__FUNCTION__);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700563 CHECK(dex_location != nullptr);
564 CHECK(error_msgs != nullptr);
565
566 // Verify we aren't holding the mutator lock, which could starve GC if we
567 // have to generate or relocate an oat file.
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800568 Thread* const self = Thread::Current();
569 Locks::mutator_lock_->AssertNotHeld(self);
570 Runtime* const runtime = Runtime::Current();
Calin Juravleb077e152016-02-18 18:47:37 +0000571
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700572 OatFileAssistant oat_file_assistant(dex_location,
573 oat_location,
574 kRuntimeISA,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000575 /*profile_changed*/false,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800576 !runtime->IsAotCompiler());
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700577
578 // Lock the target oat location to avoid races generating and loading the
579 // oat file.
580 std::string error_msg;
581 if (!oat_file_assistant.Lock(/*out*/&error_msg)) {
582 // Don't worry too much if this fails. If it does fail, it's unlikely we
583 // can generate an oat file anyway.
584 VLOG(class_linker) << "OatFileAssistant::Lock: " << error_msg;
585 }
586
587 const OatFile* source_oat_file = nullptr;
588
Richard Uhlerf4b34872016-04-13 11:03:46 -0700589 // Update the oat file on disk if we can, based on the --compiler-filter
590 // option derived from the current runtime options.
591 // This may fail, but that's okay. Best effort is all that matters here.
592 switch (oat_file_assistant.MakeUpToDate(/*out*/ &error_msg)) {
Richard Uhler1e860612016-03-30 12:17:55 -0700593 case OatFileAssistant::kUpdateFailed:
594 LOG(WARNING) << error_msg;
595 break;
596
597 case OatFileAssistant::kUpdateNotAttempted:
598 // Avoid spamming the logs if we decided not to attempt making the oat
599 // file up to date.
600 VLOG(oat) << error_msg;
601 break;
602
603 case OatFileAssistant::kUpdateSucceeded:
604 // Nothing to do.
605 break;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700606 }
607
608 // Get the oat file on disk.
609 std::unique_ptr<const OatFile> oat_file(oat_file_assistant.GetBestOatFile().release());
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800610
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700611 if (oat_file != nullptr) {
612 // Take the file only if it has no collisions, or we must take it because of preopting.
Jeff Haof0192c82016-03-28 20:39:50 -0700613 bool accept_oat_file =
614 !HasCollisions(oat_file.get(), class_loader, dex_elements, /*out*/ &error_msg);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700615 if (!accept_oat_file) {
616 // Failed the collision check. Print warning.
617 if (Runtime::Current()->IsDexFileFallbackEnabled()) {
618 LOG(WARNING) << "Found duplicate classes, falling back to interpreter mode for "
619 << dex_location;
620 } else {
621 LOG(WARNING) << "Found duplicate classes, dex-file-fallback disabled, will be failing to "
622 " load classes for " << dex_location;
623 }
624 LOG(WARNING) << error_msg;
625
626 // However, if the app was part of /system and preopted, there is no original dex file
627 // available. In that case grudgingly accept the oat file.
Richard Uhler76f5cb62016-04-04 13:30:16 -0700628 if (!oat_file_assistant.HasOriginalDexFiles()) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700629 accept_oat_file = true;
630 LOG(WARNING) << "Dex location " << dex_location << " does not seem to include dex file. "
631 << "Allow oat file use. This is potentially dangerous.";
632 }
633 }
634
635 if (accept_oat_file) {
636 VLOG(class_linker) << "Registering " << oat_file->GetLocation();
637 source_oat_file = RegisterOatFile(std::move(oat_file));
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700638 *out_oat_file = source_oat_file;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700639 }
640 }
641
642 std::vector<std::unique_ptr<const DexFile>> dex_files;
643
644 // Load the dex files from the oat file.
645 if (source_oat_file != nullptr) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800646 bool added_image_space = false;
647 if (source_oat_file->IsExecutable()) {
648 std::unique_ptr<gc::space::ImageSpace> image_space(
649 kEnableAppImage ? oat_file_assistant.OpenImageSpace(source_oat_file) : nullptr);
650 if (image_space != nullptr) {
651 ScopedObjectAccess soa(self);
652 StackHandleScope<1> hs(self);
653 Handle<mirror::ClassLoader> h_loader(
654 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(class_loader)));
655 // Can not load app image without class loader.
656 if (h_loader.Get() != nullptr) {
657 std::string temp_error_msg;
658 // Add image space has a race condition since other threads could be reading from the
659 // spaces array.
Mathieu Chartiera9d82fe2016-01-25 20:06:11 -0800660 {
661 ScopedThreadSuspension sts(self, kSuspended);
Mathieu Chartier61d2b2d2016-02-04 13:31:46 -0800662 gc::ScopedGCCriticalSection gcs(self,
663 gc::kGcCauseAddRemoveAppImageSpace,
664 gc::kCollectorTypeAddRemoveAppImageSpace);
Mathieu Chartiera9d82fe2016-01-25 20:06:11 -0800665 ScopedSuspendAll ssa("Add image space");
666 runtime->GetHeap()->AddSpace(image_space.get());
667 }
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800668 {
669 ScopedTrace trace2(StringPrintf("Adding image space for location %s", dex_location));
670 added_image_space = runtime->GetClassLinker()->AddImageSpace(image_space.get(),
671 h_loader,
672 dex_elements,
673 dex_location,
674 /*out*/&dex_files,
675 /*out*/&temp_error_msg);
676 }
Mathieu Chartiera6e81ed2016-02-25 13:52:10 -0800677 if (added_image_space) {
Mathieu Chartierbd064ea2016-02-11 16:27:18 -0800678 // Successfully added image space to heap, release the map so that it does not get
679 // freed.
680 image_space.release();
681 } else {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800682 LOG(INFO) << "Failed to add image file " << temp_error_msg;
683 dex_files.clear();
Mathieu Chartiera9d82fe2016-01-25 20:06:11 -0800684 {
685 ScopedThreadSuspension sts(self, kSuspended);
Mathieu Chartier61d2b2d2016-02-04 13:31:46 -0800686 gc::ScopedGCCriticalSection gcs(self,
687 gc::kGcCauseAddRemoveAppImageSpace,
688 gc::kCollectorTypeAddRemoveAppImageSpace);
Mathieu Chartiera9d82fe2016-01-25 20:06:11 -0800689 ScopedSuspendAll ssa("Remove image space");
690 runtime->GetHeap()->RemoveSpace(image_space.get());
691 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800692 // Non-fatal, don't update error_msg.
693 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800694 }
695 }
696 }
697 if (!added_image_space) {
698 DCHECK(dex_files.empty());
699 dex_files = oat_file_assistant.LoadDexFiles(*source_oat_file, dex_location);
700 }
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700701 if (dex_files.empty()) {
702 error_msgs->push_back("Failed to open dex files from " + source_oat_file->GetLocation());
703 }
704 }
705
706 // Fall back to running out of the original dex file if we couldn't load any
707 // dex_files from the oat file.
708 if (dex_files.empty()) {
709 if (oat_file_assistant.HasOriginalDexFiles()) {
710 if (Runtime::Current()->IsDexFileFallbackEnabled()) {
711 if (!DexFile::Open(dex_location, dex_location, /*out*/ &error_msg, &dex_files)) {
712 LOG(WARNING) << error_msg;
Alex Light3045b662016-04-20 14:26:34 -0700713 error_msgs->push_back("Failed to open dex files from " + std::string(dex_location)
714 + " because: " + error_msg);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700715 }
716 } else {
717 error_msgs->push_back("Fallback mode disabled, skipping dex files.");
718 }
719 } else {
720 error_msgs->push_back("No original dex files found for dex location "
721 + std::string(dex_location));
722 }
723 }
Calin Juravlec90bc922016-02-24 10:13:09 +0000724
725 // TODO(calin): Consider optimizing this knowing that is useless to record the
726 // use of fully compiled apks.
727 Runtime::Current()->NotifyDexLoaded(dex_location);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700728 return dex_files;
729}
730
Nicolas Geoffray04680f32016-03-17 11:56:54 +0000731void OatFileManager::DumpForSigQuit(std::ostream& os) {
732 ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
733 std::vector<const OatFile*> boot_oat_files = GetBootOatFiles();
734 for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) {
735 if (ContainsElement(boot_oat_files, oat_file.get())) {
736 continue;
737 }
Andreas Gampe29d38e72016-03-23 15:31:51 +0000738 os << oat_file->GetLocation() << ": " << oat_file->GetCompilerFilter() << "\n";
Nicolas Geoffray04680f32016-03-17 11:56:54 +0000739 }
740}
741
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700742} // namespace art