Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1 | /* |
| 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 Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 23 | #include "android-base/stringprintf.h" |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 24 | #include "android-base/strings.h" |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 25 | |
Andreas Gampe | 90b936d | 2017-01-31 08:58:55 -0800 | [diff] [blame] | 26 | #include "art_field-inl.h" |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 27 | #include "base/bit_vector-inl.h" |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 28 | #include "base/file_utils.h" |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 29 | #include "base/logging.h" // For VLOG. |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 30 | #include "base/stl_util.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 31 | #include "base/systrace.h" |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 32 | #include "class_linker.h" |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 33 | #include "class_loader_context.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 34 | #include "dex/dex_file-inl.h" |
| 35 | #include "dex/dex_file_loader.h" |
| 36 | #include "dex/dex_file_tracking_registrar.h" |
Mathieu Chartier | 61d2b2d | 2016-02-04 13:31:46 -0800 | [diff] [blame] | 37 | #include "gc/scoped_gc_critical_section.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 38 | #include "gc/space/image_space.h" |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 39 | #include "handle_scope-inl.h" |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 40 | #include "jni_internal.h" |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 41 | #include "mirror/class_loader.h" |
Andreas Gampe | 90b936d | 2017-01-31 08:58:55 -0800 | [diff] [blame] | 42 | #include "mirror/object-inl.h" |
Alex Light | 2ce6fc8 | 2017-12-18 16:42:36 -0800 | [diff] [blame] | 43 | #include "oat_file.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 44 | #include "oat_file_assistant.h" |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 45 | #include "obj_ptr-inl.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 46 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 47 | #include "thread-current-inl.h" |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 48 | #include "thread_list.h" |
Andreas Gampe | 90b936d | 2017-01-31 08:58:55 -0800 | [diff] [blame] | 49 | #include "well_known_classes.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 50 | |
| 51 | namespace art { |
| 52 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 53 | using android::base::StringPrintf; |
| 54 | |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 55 | // If true, we attempt to load the application image if it exists. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 56 | static constexpr bool kEnableAppImage = true; |
| 57 | |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 58 | static bool OatFileIsOnSystem(const std::unique_ptr<const OatFile>& oat_file) { |
| 59 | UniqueCPtr<const char[]> path(realpath(oat_file->GetLocation().c_str(), nullptr)); |
Andreas Gampe | 3f09389 | 2017-09-14 15:11:01 -0700 | [diff] [blame] | 60 | return path != nullptr && android::base::StartsWith(oat_file->GetLocation(), |
| 61 | GetAndroidRoot().c_str()); |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 64 | const OatFile* OatFileManager::RegisterOatFile(std::unique_ptr<const OatFile> oat_file) { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 65 | WriterMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 66 | CHECK(!only_use_system_oat_files_ || OatFileIsOnSystem(oat_file)) |
| 67 | << "Registering a non /system oat file: " << oat_file->GetLocation(); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 68 | DCHECK(oat_file != nullptr); |
| 69 | if (kIsDebugBuild) { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 70 | CHECK(oat_files_.find(oat_file) == oat_files_.end()); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 71 | for (const std::unique_ptr<const OatFile>& existing : oat_files_) { |
| 72 | CHECK_NE(oat_file.get(), existing.get()) << oat_file->GetLocation(); |
| 73 | // Check that we don't have an oat file with the same address. Copies of the same oat file |
| 74 | // should be loaded at different addresses. |
| 75 | CHECK_NE(oat_file->Begin(), existing->Begin()) << "Oat file already mapped at that location"; |
| 76 | } |
| 77 | } |
| 78 | have_non_pic_oat_file_ = have_non_pic_oat_file_ || !oat_file->IsPic(); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 79 | const OatFile* ret = oat_file.get(); |
| 80 | oat_files_.insert(std::move(oat_file)); |
| 81 | return ret; |
| 82 | } |
| 83 | |
| 84 | void OatFileManager::UnRegisterAndDeleteOatFile(const OatFile* oat_file) { |
| 85 | WriterMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
| 86 | DCHECK(oat_file != nullptr); |
| 87 | std::unique_ptr<const OatFile> compare(oat_file); |
| 88 | auto it = oat_files_.find(compare); |
| 89 | CHECK(it != oat_files_.end()); |
| 90 | oat_files_.erase(it); |
| 91 | compare.release(); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Calin Juravle | 0b79127 | 2016-04-18 16:38:27 +0100 | [diff] [blame] | 94 | const OatFile* OatFileManager::FindOpenedOatFileFromDexLocation( |
| 95 | const std::string& dex_base_location) const { |
| 96 | ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
| 97 | for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) { |
| 98 | const std::vector<const OatDexFile*>& oat_dex_files = oat_file->GetOatDexFiles(); |
| 99 | for (const OatDexFile* oat_dex_file : oat_dex_files) { |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 100 | if (DexFileLoader::GetBaseLocation(oat_dex_file->GetDexFileLocation()) == dex_base_location) { |
Calin Juravle | 0b79127 | 2016-04-18 16:38:27 +0100 | [diff] [blame] | 101 | return oat_file.get(); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | return nullptr; |
| 106 | } |
| 107 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 108 | const OatFile* OatFileManager::FindOpenedOatFileFromOatLocation(const std::string& oat_location) |
| 109 | const { |
| 110 | ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 111 | return FindOpenedOatFileFromOatLocationLocked(oat_location); |
| 112 | } |
| 113 | |
| 114 | const OatFile* OatFileManager::FindOpenedOatFileFromOatLocationLocked( |
| 115 | const std::string& oat_location) const { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 116 | for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) { |
| 117 | if (oat_file->GetLocation() == oat_location) { |
| 118 | return oat_file.get(); |
| 119 | } |
| 120 | } |
| 121 | return nullptr; |
| 122 | } |
| 123 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 124 | std::vector<const OatFile*> OatFileManager::GetBootOatFiles() const { |
| 125 | std::vector<const OatFile*> oat_files; |
| 126 | std::vector<gc::space::ImageSpace*> image_spaces = |
| 127 | Runtime::Current()->GetHeap()->GetBootImageSpaces(); |
| 128 | for (gc::space::ImageSpace* image_space : image_spaces) { |
| 129 | oat_files.push_back(image_space->GetOatFile()); |
| 130 | } |
| 131 | return oat_files; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | const OatFile* OatFileManager::GetPrimaryOatFile() const { |
| 135 | ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 136 | std::vector<const OatFile*> boot_oat_files = GetBootOatFiles(); |
| 137 | if (!boot_oat_files.empty()) { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 138 | for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 139 | if (std::find(boot_oat_files.begin(), boot_oat_files.end(), oat_file.get()) == |
| 140 | boot_oat_files.end()) { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 141 | return oat_file.get(); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | return nullptr; |
| 146 | } |
| 147 | |
Vladimir Marko | b0b68cf | 2017-11-14 18:11:50 +0000 | [diff] [blame] | 148 | OatFileManager::OatFileManager() |
| 149 | : have_non_pic_oat_file_(false), only_use_system_oat_files_(false) {} |
| 150 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 151 | OatFileManager::~OatFileManager() { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 152 | // Explicitly clear oat_files_ since the OatFile destructor calls back into OatFileManager for |
| 153 | // UnRegisterOatFileLocation. |
| 154 | oat_files_.clear(); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 157 | std::vector<const OatFile*> OatFileManager::RegisterImageOatFiles( |
| 158 | std::vector<gc::space::ImageSpace*> spaces) { |
| 159 | std::vector<const OatFile*> oat_files; |
| 160 | for (gc::space::ImageSpace* space : spaces) { |
| 161 | oat_files.push_back(RegisterOatFile(space->ReleaseOatFile())); |
| 162 | } |
| 163 | return oat_files; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 166 | class TypeIndexInfo { |
| 167 | public: |
| 168 | explicit TypeIndexInfo(const DexFile* dex_file) |
| 169 | : type_indexes_(GenerateTypeIndexes(dex_file)), |
| 170 | iter_(type_indexes_.Indexes().begin()), |
| 171 | end_(type_indexes_.Indexes().end()) { } |
| 172 | |
| 173 | BitVector& GetTypeIndexes() { |
| 174 | return type_indexes_; |
| 175 | } |
| 176 | BitVector::IndexIterator& GetIterator() { |
| 177 | return iter_; |
| 178 | } |
| 179 | BitVector::IndexIterator& GetIteratorEnd() { |
| 180 | return end_; |
| 181 | } |
| 182 | void AdvanceIterator() { |
| 183 | iter_++; |
| 184 | } |
| 185 | |
| 186 | private: |
| 187 | static BitVector GenerateTypeIndexes(const DexFile* dex_file) { |
| 188 | BitVector type_indexes(/*start_bits*/0, /*expandable*/true, Allocator::GetMallocAllocator()); |
| 189 | for (uint16_t i = 0; i < dex_file->NumClassDefs(); ++i) { |
| 190 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(i); |
| 191 | uint16_t type_idx = class_def.class_idx_.index_; |
| 192 | type_indexes.SetBit(type_idx); |
| 193 | } |
| 194 | return type_indexes; |
| 195 | } |
| 196 | |
| 197 | // BitVector with bits set for the type indexes of all classes in the input dex file. |
| 198 | BitVector type_indexes_; |
| 199 | BitVector::IndexIterator iter_; |
| 200 | BitVector::IndexIterator end_; |
| 201 | }; |
| 202 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 203 | class DexFileAndClassPair : ValueObject { |
| 204 | public: |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 205 | DexFileAndClassPair(const DexFile* dex_file, TypeIndexInfo* type_info, bool from_loaded_oat) |
| 206 | : type_info_(type_info), |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 207 | dex_file_(dex_file), |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 208 | cached_descriptor_(dex_file_->StringByTypeIdx(dex::TypeIndex(*type_info->GetIterator()))), |
| 209 | from_loaded_oat_(from_loaded_oat) { |
| 210 | type_info_->AdvanceIterator(); |
| 211 | } |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 212 | |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 213 | DexFileAndClassPair(const DexFileAndClassPair& rhs) = default; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 214 | |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 215 | DexFileAndClassPair& operator=(const DexFileAndClassPair& rhs) = default; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 216 | |
| 217 | const char* GetCachedDescriptor() const { |
| 218 | return cached_descriptor_; |
| 219 | } |
| 220 | |
| 221 | bool operator<(const DexFileAndClassPair& rhs) const { |
| 222 | const int cmp = strcmp(cached_descriptor_, rhs.cached_descriptor_); |
| 223 | if (cmp != 0) { |
| 224 | // Note that the order must be reversed. We want to iterate over the classes in dex files. |
| 225 | // They are sorted lexicographically. Thus, the priority-queue must be a min-queue. |
| 226 | return cmp > 0; |
| 227 | } |
| 228 | return dex_file_ < rhs.dex_file_; |
| 229 | } |
| 230 | |
| 231 | bool DexFileHasMoreClasses() const { |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 232 | return type_info_->GetIterator() != type_info_->GetIteratorEnd(); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | void Next() { |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 236 | cached_descriptor_ = dex_file_->StringByTypeIdx(dex::TypeIndex(*type_info_->GetIterator())); |
| 237 | type_info_->AdvanceIterator(); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | bool FromLoadedOat() const { |
| 241 | return from_loaded_oat_; |
| 242 | } |
| 243 | |
| 244 | const DexFile* GetDexFile() const { |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 245 | return dex_file_; |
| 246 | } |
| 247 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 248 | private: |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 249 | TypeIndexInfo* type_info_; |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 250 | const DexFile* dex_file_; |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 251 | const char* cached_descriptor_; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 252 | bool from_loaded_oat_; // We only need to compare mismatches between what we load now |
| 253 | // and what was loaded before. Any old duplicates must have been |
| 254 | // OK, and any new "internal" duplicates are as well (they must |
| 255 | // be from multidex, which resolves correctly). |
| 256 | }; |
| 257 | |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 258 | static void AddDexFilesFromOat( |
| 259 | const OatFile* oat_file, |
| 260 | /*out*/std::vector<const DexFile*>* dex_files, |
| 261 | std::vector<std::unique_ptr<const DexFile>>* opened_dex_files) { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 262 | for (const OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) { |
| 263 | std::string error; |
| 264 | std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error); |
| 265 | if (dex_file == nullptr) { |
| 266 | LOG(WARNING) << "Could not create dex file from oat file: " << error; |
| 267 | } else if (dex_file->NumClassDefs() > 0U) { |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 268 | dex_files->push_back(dex_file.get()); |
Mathieu Chartier | 14d7b3e | 2016-06-09 16:18:04 -0700 | [diff] [blame] | 269 | opened_dex_files->push_back(std::move(dex_file)); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 274 | static void AddNext(/*inout*/DexFileAndClassPair& original, |
| 275 | /*inout*/std::priority_queue<DexFileAndClassPair>& heap) { |
| 276 | if (original.DexFileHasMoreClasses()) { |
| 277 | original.Next(); |
| 278 | heap.push(std::move(original)); |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 282 | static bool CollisionCheck(std::vector<const DexFile*>& dex_files_loaded, |
| 283 | std::vector<const DexFile*>& dex_files_unloaded, |
| 284 | std::string* error_msg /*out*/) { |
| 285 | // Generate type index information for each dex file. |
| 286 | std::vector<TypeIndexInfo> loaded_types; |
| 287 | for (const DexFile* dex_file : dex_files_loaded) { |
| 288 | loaded_types.push_back(TypeIndexInfo(dex_file)); |
| 289 | } |
| 290 | std::vector<TypeIndexInfo> unloaded_types; |
| 291 | for (const DexFile* dex_file : dex_files_unloaded) { |
| 292 | unloaded_types.push_back(TypeIndexInfo(dex_file)); |
| 293 | } |
| 294 | |
| 295 | // Populate the queue of dex file and class pairs with the loaded and unloaded dex files. |
| 296 | std::priority_queue<DexFileAndClassPair> queue; |
| 297 | for (size_t i = 0; i < dex_files_loaded.size(); ++i) { |
| 298 | if (loaded_types[i].GetIterator() != loaded_types[i].GetIteratorEnd()) { |
| 299 | queue.emplace(dex_files_loaded[i], &loaded_types[i], /*from_loaded_oat*/true); |
| 300 | } |
| 301 | } |
| 302 | for (size_t i = 0; i < dex_files_unloaded.size(); ++i) { |
| 303 | if (unloaded_types[i].GetIterator() != unloaded_types[i].GetIteratorEnd()) { |
| 304 | queue.emplace(dex_files_unloaded[i], &unloaded_types[i], /*from_loaded_oat*/false); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | // Now drain the queue. |
Jeff Hao | 0471ece | 2017-04-07 16:28:12 -0700 | [diff] [blame] | 309 | bool has_duplicates = false; |
| 310 | error_msg->clear(); |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 311 | while (!queue.empty()) { |
| 312 | // Modifying the top element is only safe if we pop right after. |
| 313 | DexFileAndClassPair compare_pop(queue.top()); |
| 314 | queue.pop(); |
| 315 | |
| 316 | // Compare against the following elements. |
| 317 | while (!queue.empty()) { |
| 318 | DexFileAndClassPair top(queue.top()); |
| 319 | if (strcmp(compare_pop.GetCachedDescriptor(), top.GetCachedDescriptor()) == 0) { |
| 320 | // Same descriptor. Check whether it's crossing old-oat-files to new-oat-files. |
| 321 | if (compare_pop.FromLoadedOat() != top.FromLoadedOat()) { |
Jeff Hao | 0471ece | 2017-04-07 16:28:12 -0700 | [diff] [blame] | 322 | error_msg->append( |
| 323 | StringPrintf("Found duplicated class when checking oat files: '%s' in %s and %s\n", |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 324 | compare_pop.GetCachedDescriptor(), |
| 325 | compare_pop.GetDexFile()->GetLocation().c_str(), |
Jeff Hao | 0471ece | 2017-04-07 16:28:12 -0700 | [diff] [blame] | 326 | top.GetDexFile()->GetLocation().c_str())); |
| 327 | if (!VLOG_IS_ON(oat)) { |
| 328 | return true; |
| 329 | } |
| 330 | has_duplicates = true; |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 331 | } |
| 332 | queue.pop(); |
| 333 | AddNext(top, queue); |
| 334 | } else { |
| 335 | // Something else. Done here. |
| 336 | break; |
| 337 | } |
| 338 | } |
| 339 | AddNext(compare_pop, queue); |
| 340 | } |
| 341 | |
Jeff Hao | 0471ece | 2017-04-07 16:28:12 -0700 | [diff] [blame] | 342 | return has_duplicates; |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 345 | // Check for class-def collisions in dex files. |
| 346 | // |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 347 | // This first walks the class loader chain present in the given context, getting all the dex files |
| 348 | // from the class loader. |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 349 | // |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 350 | // If the context is null (which means the initial class loader was null or unsupported) |
| 351 | // this returns false. b/37777332. |
| 352 | // |
| 353 | // This first checks whether all class loaders in the context have the same type and |
| 354 | // classpath. If so, we exit early. Otherwise, we do the collision check. |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 355 | // |
| 356 | // The collision check works by maintaining a heap with one class from each dex file, sorted by the |
| 357 | // class descriptor. Then a dex-file/class pair is continually removed from the heap and compared |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 358 | // against the following top element. If the descriptor is the same, it is now checked whether |
| 359 | // the two elements agree on whether their dex file was from an already-loaded oat-file or the |
| 360 | // new oat file. Any disagreement indicates a collision. |
| 361 | bool OatFileManager::HasCollisions(const OatFile* oat_file, |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 362 | const ClassLoaderContext* context, |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 363 | std::string* error_msg /*out*/) const { |
| 364 | DCHECK(oat_file != nullptr); |
| 365 | DCHECK(error_msg != nullptr); |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 366 | |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 367 | // The context might be null if there are unrecognized class loaders in the chain or they |
| 368 | // don't meet sensible sanity conditions. In this case we assume that the app knows what it's |
| 369 | // doing and accept the oat file. |
| 370 | // Note that this has correctness implications as we cannot guarantee that the class resolution |
| 371 | // used during compilation is OK (b/37777332). |
| 372 | if (context == nullptr) { |
| 373 | LOG(WARNING) << "Skipping duplicate class check due to unsupported classloader"; |
Narayan Kamath | 5c52574 | 2017-04-28 10:19:29 +0100 | [diff] [blame] | 374 | return false; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 377 | // If the pat file loading context matches the context used during compilation then we accept |
| 378 | // the oat file without addition checks |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 379 | if (context->VerifyClassLoaderContextMatch(oat_file->GetClassLoaderContext())) { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 380 | return false; |
| 381 | } |
| 382 | |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 383 | // The class loader context does not match. Perform a full duplicate classes check. |
| 384 | |
| 385 | std::vector<const DexFile*> dex_files_loaded = context->FlattenOpenedDexFiles(); |
| 386 | |
Narayan Kamath | 5c52574 | 2017-04-28 10:19:29 +0100 | [diff] [blame] | 387 | // Vector that holds the newly opened dex files live, this is done to prevent leaks. |
| 388 | std::vector<std::unique_ptr<const DexFile>> opened_dex_files; |
| 389 | |
Andreas Gampe | 1fdbe1b | 2016-06-10 08:36:20 -0700 | [diff] [blame] | 390 | ScopedTrace st("Collision check"); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 391 | // Add dex files from the oat file to check. |
Jeff Hao | 8ec0a20 | 2017-03-07 21:56:31 -0800 | [diff] [blame] | 392 | std::vector<const DexFile*> dex_files_unloaded; |
| 393 | AddDexFilesFromOat(oat_file, &dex_files_unloaded, &opened_dex_files); |
| 394 | return CollisionCheck(dex_files_loaded, dex_files_unloaded, error_msg); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | std::vector<std::unique_ptr<const DexFile>> OatFileManager::OpenDexFilesFromOat( |
| 398 | const char* dex_location, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 399 | jobject class_loader, |
| 400 | jobjectArray dex_elements, |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 401 | const OatFile** out_oat_file, |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 402 | std::vector<std::string>* error_msgs) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 403 | ScopedTrace trace(__FUNCTION__); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 404 | CHECK(dex_location != nullptr); |
| 405 | CHECK(error_msgs != nullptr); |
| 406 | |
| 407 | // Verify we aren't holding the mutator lock, which could starve GC if we |
| 408 | // have to generate or relocate an oat file. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 409 | Thread* const self = Thread::Current(); |
| 410 | Locks::mutator_lock_->AssertNotHeld(self); |
| 411 | Runtime* const runtime = Runtime::Current(); |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 412 | |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 413 | std::unique_ptr<ClassLoaderContext> context; |
| 414 | // If the class_loader is null there's not much we can do. This happens if a dex files is loaded |
| 415 | // directly with DexFile APIs instead of using class loaders. |
| 416 | if (class_loader == nullptr) { |
| 417 | LOG(WARNING) << "Opening an oat file without a class loader. " |
| 418 | << "Are you using the deprecated DexFile APIs?"; |
| 419 | context = nullptr; |
| 420 | } else { |
| 421 | context = ClassLoaderContext::CreateContextForClassLoader(class_loader, dex_elements); |
| 422 | } |
| 423 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 424 | OatFileAssistant oat_file_assistant(dex_location, |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 425 | kRuntimeISA, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 426 | !runtime->IsAotCompiler()); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 427 | |
| 428 | // Lock the target oat location to avoid races generating and loading the |
| 429 | // oat file. |
| 430 | std::string error_msg; |
| 431 | if (!oat_file_assistant.Lock(/*out*/&error_msg)) { |
| 432 | // Don't worry too much if this fails. If it does fail, it's unlikely we |
| 433 | // can generate an oat file anyway. |
| 434 | VLOG(class_linker) << "OatFileAssistant::Lock: " << error_msg; |
| 435 | } |
| 436 | |
| 437 | const OatFile* source_oat_file = nullptr; |
| 438 | |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 439 | // No point in trying to make up-to-date if we can only use system oat files. |
| 440 | if (!only_use_system_oat_files_ && !oat_file_assistant.IsUpToDate()) { |
Richard Uhler | 01be681 | 2016-05-17 10:34:52 -0700 | [diff] [blame] | 441 | // Update the oat file on disk if we can, based on the --compiler-filter |
| 442 | // option derived from the current runtime options. |
| 443 | // This may fail, but that's okay. Best effort is all that matters here. |
Calin Juravle | 8d8d37b | 2017-10-02 14:56:29 -0700 | [diff] [blame] | 444 | // TODO(calin): b/64530081 b/66984396. Pass a null context to verify and compile |
| 445 | // secondary dex files in isolation (and avoid to extract/verify the main apk |
| 446 | // if it's in the class path). Note this trades correctness for performance |
| 447 | // since the resulting slow down is unacceptable in some cases until b/64530081 |
| 448 | // is fixed. |
Nicolas Geoffray | 55f39ed | 2017-11-24 10:52:05 +0000 | [diff] [blame] | 449 | // We still pass the class loader context when the classpath string of the runtime |
| 450 | // is not empty, which is the situation when ART is invoked standalone. |
| 451 | ClassLoaderContext* actual_context = Runtime::Current()->GetClassPathString().empty() |
| 452 | ? nullptr |
| 453 | : context.get(); |
Calin Juravle | 8d8d37b | 2017-10-02 14:56:29 -0700 | [diff] [blame] | 454 | switch (oat_file_assistant.MakeUpToDate(/*profile_changed*/ false, |
Nicolas Geoffray | 55f39ed | 2017-11-24 10:52:05 +0000 | [diff] [blame] | 455 | actual_context, |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 456 | /*out*/ &error_msg)) { |
Richard Uhler | 01be681 | 2016-05-17 10:34:52 -0700 | [diff] [blame] | 457 | case OatFileAssistant::kUpdateFailed: |
| 458 | LOG(WARNING) << error_msg; |
| 459 | break; |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 460 | |
Richard Uhler | 01be681 | 2016-05-17 10:34:52 -0700 | [diff] [blame] | 461 | case OatFileAssistant::kUpdateNotAttempted: |
| 462 | // Avoid spamming the logs if we decided not to attempt making the oat |
| 463 | // file up to date. |
| 464 | VLOG(oat) << error_msg; |
| 465 | break; |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 466 | |
Richard Uhler | 01be681 | 2016-05-17 10:34:52 -0700 | [diff] [blame] | 467 | case OatFileAssistant::kUpdateSucceeded: |
| 468 | // Nothing to do. |
| 469 | break; |
| 470 | } |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | // Get the oat file on disk. |
| 474 | std::unique_ptr<const OatFile> oat_file(oat_file_assistant.GetBestOatFile().release()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 475 | |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 476 | if (oat_file != nullptr && only_use_system_oat_files_ && !OatFileIsOnSystem(oat_file)) { |
| 477 | // If the oat file is not on /system, don't use it. |
| 478 | } else if ((class_loader != nullptr || dex_elements != nullptr) && oat_file != nullptr) { |
| 479 | // Prevent oat files from being loaded if no class_loader or dex_elements are provided. |
| 480 | // This can happen when the deprecated DexFile.<init>(String) is called directly, and it |
| 481 | // could load oat files without checking the classpath, which would be incorrect. |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 482 | // Take the file only if it has no collisions, or we must take it because of preopting. |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 483 | bool accept_oat_file = |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 484 | !HasCollisions(oat_file.get(), context.get(), /*out*/ &error_msg); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 485 | if (!accept_oat_file) { |
| 486 | // Failed the collision check. Print warning. |
| 487 | if (Runtime::Current()->IsDexFileFallbackEnabled()) { |
Narayan Kamath | 5c52574 | 2017-04-28 10:19:29 +0100 | [diff] [blame] | 488 | if (!oat_file_assistant.HasOriginalDexFiles()) { |
| 489 | // We need to fallback but don't have original dex files. We have to |
| 490 | // fallback to opening the existing oat file. This is potentially |
| 491 | // unsafe so we warn about it. |
| 492 | accept_oat_file = true; |
| 493 | |
| 494 | LOG(WARNING) << "Dex location " << dex_location << " does not seem to include dex file. " |
| 495 | << "Allow oat file use. This is potentially dangerous."; |
| 496 | } else { |
| 497 | // We have to fallback and found original dex files - extract them from an APK. |
| 498 | // Also warn about this operation because it's potentially wasteful. |
| 499 | LOG(WARNING) << "Found duplicate classes, falling back to extracting from APK : " |
| 500 | << dex_location; |
| 501 | LOG(WARNING) << "NOTE: This wastes RAM and hurts startup performance."; |
| 502 | } |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 503 | } else { |
Narayan Kamath | 5c52574 | 2017-04-28 10:19:29 +0100 | [diff] [blame] | 504 | // TODO: We should remove this. The fact that we're here implies -Xno-dex-file-fallback |
| 505 | // was set, which means that we should never fallback. If we don't have original dex |
| 506 | // files, we should just fail resolution as the flag intended. |
| 507 | if (!oat_file_assistant.HasOriginalDexFiles()) { |
| 508 | accept_oat_file = true; |
| 509 | } |
| 510 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 511 | LOG(WARNING) << "Found duplicate classes, dex-file-fallback disabled, will be failing to " |
| 512 | " load classes for " << dex_location; |
| 513 | } |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 514 | |
Narayan Kamath | 5c52574 | 2017-04-28 10:19:29 +0100 | [diff] [blame] | 515 | LOG(WARNING) << error_msg; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | if (accept_oat_file) { |
| 519 | VLOG(class_linker) << "Registering " << oat_file->GetLocation(); |
| 520 | source_oat_file = RegisterOatFile(std::move(oat_file)); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 521 | *out_oat_file = source_oat_file; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 522 | } |
| 523 | } |
| 524 | |
| 525 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 526 | |
| 527 | // Load the dex files from the oat file. |
| 528 | if (source_oat_file != nullptr) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 529 | bool added_image_space = false; |
| 530 | if (source_oat_file->IsExecutable()) { |
Alex Light | 2ce6fc8 | 2017-12-18 16:42:36 -0800 | [diff] [blame] | 531 | // We need to throw away the image space if we are debuggable but the oat-file source of the |
| 532 | // image is not otherwise we might get classes with inlined methods or other such things. |
| 533 | std::unique_ptr<gc::space::ImageSpace> image_space; |
| 534 | if (kEnableAppImage && (!runtime->IsJavaDebuggable() || source_oat_file->IsDebuggable())) { |
| 535 | image_space = oat_file_assistant.OpenImageSpace(source_oat_file); |
| 536 | } else { |
| 537 | image_space = nullptr; |
| 538 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 539 | if (image_space != nullptr) { |
| 540 | ScopedObjectAccess soa(self); |
| 541 | StackHandleScope<1> hs(self); |
| 542 | Handle<mirror::ClassLoader> h_loader( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 543 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader))); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 544 | // Can not load app image without class loader. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 545 | if (h_loader != nullptr) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 546 | std::string temp_error_msg; |
| 547 | // Add image space has a race condition since other threads could be reading from the |
| 548 | // spaces array. |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 549 | { |
| 550 | ScopedThreadSuspension sts(self, kSuspended); |
Mathieu Chartier | 61d2b2d | 2016-02-04 13:31:46 -0800 | [diff] [blame] | 551 | gc::ScopedGCCriticalSection gcs(self, |
| 552 | gc::kGcCauseAddRemoveAppImageSpace, |
| 553 | gc::kCollectorTypeAddRemoveAppImageSpace); |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 554 | ScopedSuspendAll ssa("Add image space"); |
| 555 | runtime->GetHeap()->AddSpace(image_space.get()); |
| 556 | } |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 557 | { |
| 558 | ScopedTrace trace2(StringPrintf("Adding image space for location %s", dex_location)); |
| 559 | added_image_space = runtime->GetClassLinker()->AddImageSpace(image_space.get(), |
| 560 | h_loader, |
| 561 | dex_elements, |
| 562 | dex_location, |
| 563 | /*out*/&dex_files, |
| 564 | /*out*/&temp_error_msg); |
| 565 | } |
Mathieu Chartier | a6e81ed | 2016-02-25 13:52:10 -0800 | [diff] [blame] | 566 | if (added_image_space) { |
Mathieu Chartier | bd064ea | 2016-02-11 16:27:18 -0800 | [diff] [blame] | 567 | // Successfully added image space to heap, release the map so that it does not get |
| 568 | // freed. |
| 569 | image_space.release(); |
Bharadwaj Kalandhabhatta | 0bb4031 | 2017-06-01 10:47:00 -0700 | [diff] [blame] | 570 | |
| 571 | // Register for tracking. |
| 572 | for (const auto& dex_file : dex_files) { |
| 573 | dex::tracking::RegisterDexFile(dex_file.get()); |
| 574 | } |
Mathieu Chartier | bd064ea | 2016-02-11 16:27:18 -0800 | [diff] [blame] | 575 | } else { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 576 | LOG(INFO) << "Failed to add image file " << temp_error_msg; |
| 577 | dex_files.clear(); |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 578 | { |
| 579 | ScopedThreadSuspension sts(self, kSuspended); |
Mathieu Chartier | 61d2b2d | 2016-02-04 13:31:46 -0800 | [diff] [blame] | 580 | gc::ScopedGCCriticalSection gcs(self, |
| 581 | gc::kGcCauseAddRemoveAppImageSpace, |
| 582 | gc::kCollectorTypeAddRemoveAppImageSpace); |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 583 | ScopedSuspendAll ssa("Remove image space"); |
| 584 | runtime->GetHeap()->RemoveSpace(image_space.get()); |
| 585 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 586 | // Non-fatal, don't update error_msg. |
| 587 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 588 | } |
| 589 | } |
| 590 | } |
| 591 | if (!added_image_space) { |
| 592 | DCHECK(dex_files.empty()); |
| 593 | dex_files = oat_file_assistant.LoadDexFiles(*source_oat_file, dex_location); |
Bharadwaj Kalandhabhatta | 0bb4031 | 2017-06-01 10:47:00 -0700 | [diff] [blame] | 594 | |
| 595 | // Register for tracking. |
| 596 | for (const auto& dex_file : dex_files) { |
| 597 | dex::tracking::RegisterDexFile(dex_file.get()); |
| 598 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 599 | } |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 600 | if (dex_files.empty()) { |
| 601 | error_msgs->push_back("Failed to open dex files from " + source_oat_file->GetLocation()); |
Mathieu Chartier | be8303d | 2017-08-17 17:39:39 -0700 | [diff] [blame] | 602 | } else { |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 603 | // Opened dex files from an oat file, madvise them to their loaded state. |
| 604 | for (const std::unique_ptr<const DexFile>& dex_file : dex_files) { |
| 605 | OatDexFile::MadviseDexFile(*dex_file, MadviseState::kMadviseStateAtLoad); |
| 606 | } |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | |
| 610 | // Fall back to running out of the original dex file if we couldn't load any |
| 611 | // dex_files from the oat file. |
| 612 | if (dex_files.empty()) { |
| 613 | if (oat_file_assistant.HasOriginalDexFiles()) { |
| 614 | if (Runtime::Current()->IsDexFileFallbackEnabled()) { |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 615 | static constexpr bool kVerifyChecksum = true; |
Nicolas Geoffray | 095c6c9 | 2017-10-19 13:59:55 +0100 | [diff] [blame] | 616 | if (!DexFileLoader::Open(dex_location, |
| 617 | dex_location, |
Nicolas Geoffray | e875f4c | 2017-10-26 12:26:43 +0100 | [diff] [blame] | 618 | Runtime::Current()->IsVerificationEnabled(), |
Nicolas Geoffray | 095c6c9 | 2017-10-19 13:59:55 +0100 | [diff] [blame] | 619 | kVerifyChecksum, |
| 620 | /*out*/ &error_msg, |
| 621 | &dex_files)) { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 622 | LOG(WARNING) << error_msg; |
Alex Light | 3045b66 | 2016-04-20 14:26:34 -0700 | [diff] [blame] | 623 | error_msgs->push_back("Failed to open dex files from " + std::string(dex_location) |
| 624 | + " because: " + error_msg); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 625 | } |
| 626 | } else { |
| 627 | error_msgs->push_back("Fallback mode disabled, skipping dex files."); |
| 628 | } |
| 629 | } else { |
| 630 | error_msgs->push_back("No original dex files found for dex location " |
| 631 | + std::string(dex_location)); |
| 632 | } |
| 633 | } |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 634 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 635 | return dex_files; |
| 636 | } |
| 637 | |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 638 | void OatFileManager::SetOnlyUseSystemOatFiles() { |
| 639 | ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
| 640 | CHECK_EQ(oat_files_.size(), GetBootOatFiles().size()); |
| 641 | only_use_system_oat_files_ = true; |
| 642 | } |
| 643 | |
Nicolas Geoffray | 04680f3 | 2016-03-17 11:56:54 +0000 | [diff] [blame] | 644 | void OatFileManager::DumpForSigQuit(std::ostream& os) { |
| 645 | ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
| 646 | std::vector<const OatFile*> boot_oat_files = GetBootOatFiles(); |
| 647 | for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) { |
| 648 | if (ContainsElement(boot_oat_files, oat_file.get())) { |
| 649 | continue; |
| 650 | } |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 651 | os << oat_file->GetLocation() << ": " << oat_file->GetCompilerFilter() << "\n"; |
Nicolas Geoffray | 04680f3 | 2016-03-17 11:56:54 +0000 | [diff] [blame] | 652 | } |
| 653 | } |
| 654 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 655 | } // namespace art |