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