blob: 9ab0072ea9c49cfc70b4ae5db289166fe2ee3637 [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
39// For b/21333911.
Mathieu Chartier80b37b72015-10-12 18:13:39 -070040// Only enabled for debug builds to prevent bit rot. There are too many performance regressions for
41// normal builds.
42static constexpr bool kDuplicateClassesCheck = kIsDebugBuild;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070043
Mathieu Chartierfbc31082016-01-24 11:59:56 -080044// If true, then we attempt to load the application image if it exists.
45static constexpr bool kEnableAppImage = true;
46
Andreas Gampe7bcfcb82016-03-23 15:31:51 +000047CompilerFilter::Filter OatFileManager::filter_ = CompilerFilter::Filter::kSpeed;
48
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070049const OatFile* OatFileManager::RegisterOatFile(std::unique_ptr<const OatFile> oat_file) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -070050 WriterMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070051 DCHECK(oat_file != nullptr);
52 if (kIsDebugBuild) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -070053 CHECK(oat_files_.find(oat_file) == oat_files_.end());
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070054 for (const std::unique_ptr<const OatFile>& existing : oat_files_) {
55 CHECK_NE(oat_file.get(), existing.get()) << oat_file->GetLocation();
56 // Check that we don't have an oat file with the same address. Copies of the same oat file
57 // should be loaded at different addresses.
58 CHECK_NE(oat_file->Begin(), existing->Begin()) << "Oat file already mapped at that location";
59 }
60 }
61 have_non_pic_oat_file_ = have_non_pic_oat_file_ || !oat_file->IsPic();
Mathieu Chartiere58991b2015-10-13 07:59:34 -070062 const OatFile* ret = oat_file.get();
63 oat_files_.insert(std::move(oat_file));
64 return ret;
65}
66
67void OatFileManager::UnRegisterAndDeleteOatFile(const OatFile* oat_file) {
68 WriterMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
69 DCHECK(oat_file != nullptr);
70 std::unique_ptr<const OatFile> compare(oat_file);
71 auto it = oat_files_.find(compare);
72 CHECK(it != oat_files_.end());
73 oat_files_.erase(it);
74 compare.release();
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070075}
76
Calin Juravle75064232016-04-18 16:38:27 +010077const OatFile* OatFileManager::FindOpenedOatFileFromDexLocation(
78 const std::string& dex_base_location) const {
79 ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
80 for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) {
81 const std::vector<const OatDexFile*>& oat_dex_files = oat_file->GetOatDexFiles();
82 for (const OatDexFile* oat_dex_file : oat_dex_files) {
83 if (DexFile::GetBaseLocation(oat_dex_file->GetDexFileLocation()) == dex_base_location) {
84 return oat_file.get();
85 }
86 }
87 }
88 return nullptr;
89}
90
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070091const OatFile* OatFileManager::FindOpenedOatFileFromOatLocation(const std::string& oat_location)
92 const {
93 ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
Mathieu Chartiere58991b2015-10-13 07:59:34 -070094 return FindOpenedOatFileFromOatLocationLocked(oat_location);
95}
96
97const OatFile* OatFileManager::FindOpenedOatFileFromOatLocationLocked(
98 const std::string& oat_location) const {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070099 for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) {
100 if (oat_file->GetLocation() == oat_location) {
101 return oat_file.get();
102 }
103 }
104 return nullptr;
105}
106
Jeff Haodcdc85b2015-12-04 14:06:18 -0800107std::vector<const OatFile*> OatFileManager::GetBootOatFiles() const {
108 std::vector<const OatFile*> oat_files;
109 std::vector<gc::space::ImageSpace*> image_spaces =
110 Runtime::Current()->GetHeap()->GetBootImageSpaces();
111 for (gc::space::ImageSpace* image_space : image_spaces) {
112 oat_files.push_back(image_space->GetOatFile());
113 }
114 return oat_files;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700115}
116
117const OatFile* OatFileManager::GetPrimaryOatFile() const {
118 ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -0800119 std::vector<const OatFile*> boot_oat_files = GetBootOatFiles();
120 if (!boot_oat_files.empty()) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700121 for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800122 if (std::find(boot_oat_files.begin(), boot_oat_files.end(), oat_file.get()) ==
123 boot_oat_files.end()) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700124 return oat_file.get();
125 }
126 }
127 }
128 return nullptr;
129}
130
131OatFileManager::~OatFileManager() {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700132 // Explicitly clear oat_files_ since the OatFile destructor calls back into OatFileManager for
133 // UnRegisterOatFileLocation.
134 oat_files_.clear();
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700135}
136
Jeff Haodcdc85b2015-12-04 14:06:18 -0800137std::vector<const OatFile*> OatFileManager::RegisterImageOatFiles(
138 std::vector<gc::space::ImageSpace*> spaces) {
139 std::vector<const OatFile*> oat_files;
140 for (gc::space::ImageSpace* space : spaces) {
141 oat_files.push_back(RegisterOatFile(space->ReleaseOatFile()));
142 }
143 return oat_files;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700144}
145
146class DexFileAndClassPair : ValueObject {
147 public:
148 DexFileAndClassPair(const DexFile* dex_file, size_t current_class_index, bool from_loaded_oat)
149 : cached_descriptor_(GetClassDescriptor(dex_file, current_class_index)),
150 dex_file_(dex_file),
151 current_class_index_(current_class_index),
152 from_loaded_oat_(from_loaded_oat) {}
153
Mathieu Chartier80b37b72015-10-12 18:13:39 -0700154 DexFileAndClassPair(const DexFileAndClassPair& rhs) = default;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700155
Mathieu Chartier80b37b72015-10-12 18:13:39 -0700156 DexFileAndClassPair& operator=(const DexFileAndClassPair& rhs) = default;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700157
158 const char* GetCachedDescriptor() const {
159 return cached_descriptor_;
160 }
161
162 bool operator<(const DexFileAndClassPair& rhs) const {
163 const int cmp = strcmp(cached_descriptor_, rhs.cached_descriptor_);
164 if (cmp != 0) {
165 // Note that the order must be reversed. We want to iterate over the classes in dex files.
166 // They are sorted lexicographically. Thus, the priority-queue must be a min-queue.
167 return cmp > 0;
168 }
169 return dex_file_ < rhs.dex_file_;
170 }
171
172 bool DexFileHasMoreClasses() const {
173 return current_class_index_ + 1 < dex_file_->NumClassDefs();
174 }
175
176 void Next() {
177 ++current_class_index_;
Mathieu Chartier80b37b72015-10-12 18:13:39 -0700178 cached_descriptor_ = GetClassDescriptor(dex_file_.get(), current_class_index_);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700179 }
180
181 size_t GetCurrentClassIndex() const {
182 return current_class_index_;
183 }
184
185 bool FromLoadedOat() const {
186 return from_loaded_oat_;
187 }
188
189 const DexFile* GetDexFile() const {
190 return dex_file_.get();
191 }
192
193 private:
194 static const char* GetClassDescriptor(const DexFile* dex_file, size_t index) {
195 DCHECK(IsUint<16>(index));
196 const DexFile::ClassDef& class_def = dex_file->GetClassDef(static_cast<uint16_t>(index));
197 return dex_file->StringByTypeIdx(class_def.class_idx_);
198 }
199
200 const char* cached_descriptor_;
Mathieu Chartier80b37b72015-10-12 18:13:39 -0700201 std::shared_ptr<const DexFile> dex_file_;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700202 size_t current_class_index_;
203 bool from_loaded_oat_; // We only need to compare mismatches between what we load now
204 // and what was loaded before. Any old duplicates must have been
205 // OK, and any new "internal" duplicates are as well (they must
206 // be from multidex, which resolves correctly).
207};
208
209static void AddDexFilesFromOat(const OatFile* oat_file,
210 bool already_loaded,
211 /*out*/std::priority_queue<DexFileAndClassPair>* heap) {
212 for (const OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
213 std::string error;
214 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error);
215 if (dex_file == nullptr) {
216 LOG(WARNING) << "Could not create dex file from oat file: " << error;
217 } else if (dex_file->NumClassDefs() > 0U) {
218 heap->emplace(dex_file.release(), /*current_class_index*/0U, already_loaded);
219 }
220 }
221}
222
223static void AddNext(/*inout*/DexFileAndClassPair* original,
224 /*inout*/std::priority_queue<DexFileAndClassPair>* heap) {
225 if (original->DexFileHasMoreClasses()) {
226 original->Next();
227 heap->push(std::move(*original));
228 }
229}
230
231// Check for class-def collisions in dex files.
232//
233// This works by maintaining a heap with one class from each dex file, sorted by the class
234// descriptor. Then a dex-file/class pair is continually removed from the heap and compared
235// against the following top element. If the descriptor is the same, it is now checked whether
236// the two elements agree on whether their dex file was from an already-loaded oat-file or the
237// new oat file. Any disagreement indicates a collision.
238bool OatFileManager::HasCollisions(const OatFile* oat_file,
239 std::string* error_msg /*out*/) const {
240 DCHECK(oat_file != nullptr);
241 DCHECK(error_msg != nullptr);
242 if (!kDuplicateClassesCheck) {
243 return false;
244 }
245
246 // Dex files are registered late - once a class is actually being loaded. We have to compare
247 // against the open oat files. Take the oat_file_manager_lock_ that protects oat_files_ accesses.
248 ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
249
250 std::priority_queue<DexFileAndClassPair> queue;
251
252 // Add dex files from already loaded oat files, but skip boot.
Jeff Haodcdc85b2015-12-04 14:06:18 -0800253 std::vector<const OatFile*> boot_oat_files = GetBootOatFiles();
Mathieu Chartier80b37b72015-10-12 18:13:39 -0700254 // The same OatFile can be loaded multiple times at different addresses. In this case, we don't
255 // need to check both against each other since they would have resolved the same way at compile
256 // time.
257 std::unordered_set<std::string> unique_locations;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700258 for (const std::unique_ptr<const OatFile>& loaded_oat_file : oat_files_) {
Mathieu Chartier80b37b72015-10-12 18:13:39 -0700259 DCHECK_NE(loaded_oat_file.get(), oat_file);
260 const std::string& location = loaded_oat_file->GetLocation();
Jeff Haodcdc85b2015-12-04 14:06:18 -0800261 if (std::find(boot_oat_files.begin(), boot_oat_files.end(), loaded_oat_file.get()) ==
262 boot_oat_files.end() && location != oat_file->GetLocation() &&
Mathieu Chartier80b37b72015-10-12 18:13:39 -0700263 unique_locations.find(location) == unique_locations.end()) {
264 unique_locations.insert(location);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700265 AddDexFilesFromOat(loaded_oat_file.get(), /*already_loaded*/true, &queue);
266 }
267 }
268
269 if (queue.empty()) {
270 // No other oat files, return early.
271 return false;
272 }
273
274 // Add dex files from the oat file to check.
275 AddDexFilesFromOat(oat_file, /*already_loaded*/false, &queue);
276
277 // Now drain the queue.
278 while (!queue.empty()) {
279 // Modifying the top element is only safe if we pop right after.
Mathieu Chartier80b37b72015-10-12 18:13:39 -0700280 DexFileAndClassPair compare_pop(queue.top());
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700281 queue.pop();
282
283 // Compare against the following elements.
284 while (!queue.empty()) {
Mathieu Chartier80b37b72015-10-12 18:13:39 -0700285 DexFileAndClassPair top(queue.top());
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700286
287 if (strcmp(compare_pop.GetCachedDescriptor(), top.GetCachedDescriptor()) == 0) {
288 // Same descriptor. Check whether it's crossing old-oat-files to new-oat-files.
289 if (compare_pop.FromLoadedOat() != top.FromLoadedOat()) {
290 *error_msg =
291 StringPrintf("Found duplicated class when checking oat files: '%s' in %s and %s",
292 compare_pop.GetCachedDescriptor(),
293 compare_pop.GetDexFile()->GetLocation().c_str(),
294 top.GetDexFile()->GetLocation().c_str());
295 return true;
296 }
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700297 queue.pop();
298 AddNext(&top, &queue);
299 } else {
300 // Something else. Done here.
301 break;
302 }
303 }
304 AddNext(&compare_pop, &queue);
305 }
306
307 return false;
308}
309
310std::vector<std::unique_ptr<const DexFile>> OatFileManager::OpenDexFilesFromOat(
311 const char* dex_location,
312 const char* oat_location,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800313 jobject class_loader,
314 jobjectArray dex_elements,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700315 const OatFile** out_oat_file,
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700316 std::vector<std::string>* error_msgs) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800317 ScopedTrace trace(__FUNCTION__);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700318 CHECK(dex_location != nullptr);
319 CHECK(error_msgs != nullptr);
320
321 // Verify we aren't holding the mutator lock, which could starve GC if we
322 // have to generate or relocate an oat file.
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800323 Thread* const self = Thread::Current();
324 Locks::mutator_lock_->AssertNotHeld(self);
325 Runtime* const runtime = Runtime::Current();
Calin Juravleb077e152016-02-18 18:47:37 +0000326
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700327 OatFileAssistant oat_file_assistant(dex_location,
328 oat_location,
329 kRuntimeISA,
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000330 /*profile_changed*/false,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800331 !runtime->IsAotCompiler());
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700332
333 // Lock the target oat location to avoid races generating and loading the
334 // oat file.
335 std::string error_msg;
336 if (!oat_file_assistant.Lock(/*out*/&error_msg)) {
337 // Don't worry too much if this fails. If it does fail, it's unlikely we
338 // can generate an oat file anyway.
339 VLOG(class_linker) << "OatFileAssistant::Lock: " << error_msg;
340 }
341
342 const OatFile* source_oat_file = nullptr;
343
Nicolas Geoffraye722d292015-12-15 11:51:37 +0000344 // Update the oat file on disk if we can. This may fail, but that's okay.
345 // Best effort is all that matters here.
Richard Uhlerff0274b2016-03-30 12:17:55 -0700346 switch (oat_file_assistant.MakeUpToDate(filter_, /*out*/ &error_msg)) {
347 case OatFileAssistant::kUpdateFailed:
348 LOG(WARNING) << error_msg;
349 break;
350
351 case OatFileAssistant::kUpdateNotAttempted:
352 // Avoid spamming the logs if we decided not to attempt making the oat
353 // file up to date.
354 VLOG(oat) << error_msg;
355 break;
356
357 case OatFileAssistant::kUpdateSucceeded:
358 // Nothing to do.
359 break;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700360 }
361
362 // Get the oat file on disk.
363 std::unique_ptr<const OatFile> oat_file(oat_file_assistant.GetBestOatFile().release());
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800364
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700365 if (oat_file != nullptr) {
366 // Take the file only if it has no collisions, or we must take it because of preopting.
367 bool accept_oat_file = !HasCollisions(oat_file.get(), /*out*/ &error_msg);
368 if (!accept_oat_file) {
369 // Failed the collision check. Print warning.
370 if (Runtime::Current()->IsDexFileFallbackEnabled()) {
371 LOG(WARNING) << "Found duplicate classes, falling back to interpreter mode for "
372 << dex_location;
373 } else {
374 LOG(WARNING) << "Found duplicate classes, dex-file-fallback disabled, will be failing to "
375 " load classes for " << dex_location;
376 }
377 LOG(WARNING) << error_msg;
378
379 // However, if the app was part of /system and preopted, there is no original dex file
380 // available. In that case grudgingly accept the oat file.
Richard Uhler1153ae12016-04-04 13:30:16 -0700381 if (!oat_file_assistant.HasOriginalDexFiles()) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700382 accept_oat_file = true;
383 LOG(WARNING) << "Dex location " << dex_location << " does not seem to include dex file. "
384 << "Allow oat file use. This is potentially dangerous.";
385 }
386 }
387
388 if (accept_oat_file) {
389 VLOG(class_linker) << "Registering " << oat_file->GetLocation();
390 source_oat_file = RegisterOatFile(std::move(oat_file));
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700391 *out_oat_file = source_oat_file;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700392 }
393 }
394
395 std::vector<std::unique_ptr<const DexFile>> dex_files;
396
397 // Load the dex files from the oat file.
398 if (source_oat_file != nullptr) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800399 bool added_image_space = false;
400 if (source_oat_file->IsExecutable()) {
401 std::unique_ptr<gc::space::ImageSpace> image_space(
402 kEnableAppImage ? oat_file_assistant.OpenImageSpace(source_oat_file) : nullptr);
403 if (image_space != nullptr) {
404 ScopedObjectAccess soa(self);
405 StackHandleScope<1> hs(self);
406 Handle<mirror::ClassLoader> h_loader(
407 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(class_loader)));
408 // Can not load app image without class loader.
409 if (h_loader.Get() != nullptr) {
410 std::string temp_error_msg;
411 // Add image space has a race condition since other threads could be reading from the
412 // spaces array.
Mathieu Chartiera9d82fe2016-01-25 20:06:11 -0800413 {
414 ScopedThreadSuspension sts(self, kSuspended);
Mathieu Chartier61d2b2d2016-02-04 13:31:46 -0800415 gc::ScopedGCCriticalSection gcs(self,
416 gc::kGcCauseAddRemoveAppImageSpace,
417 gc::kCollectorTypeAddRemoveAppImageSpace);
Mathieu Chartiera9d82fe2016-01-25 20:06:11 -0800418 ScopedSuspendAll ssa("Add image space");
419 runtime->GetHeap()->AddSpace(image_space.get());
420 }
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800421 {
422 ScopedTrace trace2(StringPrintf("Adding image space for location %s", dex_location));
423 added_image_space = runtime->GetClassLinker()->AddImageSpace(image_space.get(),
424 h_loader,
425 dex_elements,
426 dex_location,
427 /*out*/&dex_files,
428 /*out*/&temp_error_msg);
429 }
Mathieu Chartiera6e81ed2016-02-25 13:52:10 -0800430 if (added_image_space) {
Mathieu Chartierbd064ea2016-02-11 16:27:18 -0800431 // Successfully added image space to heap, release the map so that it does not get
432 // freed.
433 image_space.release();
434 } else {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800435 LOG(INFO) << "Failed to add image file " << temp_error_msg;
436 dex_files.clear();
Mathieu Chartiera9d82fe2016-01-25 20:06:11 -0800437 {
438 ScopedThreadSuspension sts(self, kSuspended);
Mathieu Chartier61d2b2d2016-02-04 13:31:46 -0800439 gc::ScopedGCCriticalSection gcs(self,
440 gc::kGcCauseAddRemoveAppImageSpace,
441 gc::kCollectorTypeAddRemoveAppImageSpace);
Mathieu Chartiera9d82fe2016-01-25 20:06:11 -0800442 ScopedSuspendAll ssa("Remove image space");
443 runtime->GetHeap()->RemoveSpace(image_space.get());
444 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800445 // Non-fatal, don't update error_msg.
446 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800447 }
448 }
449 }
450 if (!added_image_space) {
451 DCHECK(dex_files.empty());
452 dex_files = oat_file_assistant.LoadDexFiles(*source_oat_file, dex_location);
453 }
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700454 if (dex_files.empty()) {
455 error_msgs->push_back("Failed to open dex files from " + source_oat_file->GetLocation());
456 }
457 }
458
459 // Fall back to running out of the original dex file if we couldn't load any
460 // dex_files from the oat file.
461 if (dex_files.empty()) {
462 if (oat_file_assistant.HasOriginalDexFiles()) {
463 if (Runtime::Current()->IsDexFileFallbackEnabled()) {
464 if (!DexFile::Open(dex_location, dex_location, /*out*/ &error_msg, &dex_files)) {
465 LOG(WARNING) << error_msg;
Alex Light25792ae2016-04-20 14:26:34 -0700466 error_msgs->push_back("Failed to open dex files from " + std::string(dex_location)
467 + " because: " + error_msg);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700468 }
469 } else {
470 error_msgs->push_back("Fallback mode disabled, skipping dex files.");
471 }
472 } else {
473 error_msgs->push_back("No original dex files found for dex location "
474 + std::string(dex_location));
475 }
476 }
Calin Juravlec90bc922016-02-24 10:13:09 +0000477
478 // TODO(calin): Consider optimizing this knowing that is useless to record the
479 // use of fully compiled apks.
480 Runtime::Current()->NotifyDexLoaded(dex_location);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700481 return dex_files;
482}
483
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700484bool OatFileManager::RegisterOatFileLocation(const std::string& oat_location) {
485 WriterMutexLock mu(Thread::Current(), *Locks::oat_file_count_lock_);
486 auto it = oat_file_count_.find(oat_location);
487 if (it != oat_file_count_.end()) {
488 ++it->second;
489 return false;
490 }
491 oat_file_count_.insert(std::pair<std::string, size_t>(oat_location, 1u));
492 return true;
493}
494
495void OatFileManager::UnRegisterOatFileLocation(const std::string& oat_location) {
496 WriterMutexLock mu(Thread::Current(), *Locks::oat_file_count_lock_);
497 auto it = oat_file_count_.find(oat_location);
498 if (it != oat_file_count_.end()) {
499 --it->second;
500 if (it->second == 0) {
501 oat_file_count_.erase(it);
502 }
503 }
504}
505
Nicolas Geoffray04680f32016-03-17 11:56:54 +0000506void OatFileManager::DumpForSigQuit(std::ostream& os) {
507 ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
508 std::vector<const OatFile*> boot_oat_files = GetBootOatFiles();
509 for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) {
510 if (ContainsElement(boot_oat_files, oat_file.get())) {
511 continue;
512 }
Andreas Gampe7bcfcb82016-03-23 15:31:51 +0000513 os << oat_file->GetLocation() << ": " << oat_file->GetCompilerFilter() << "\n";
Nicolas Geoffray04680f32016-03-17 11:56:54 +0000514 }
515}
516
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700517} // namespace art