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 | |
| 23 | #include "base/logging.h" |
| 24 | #include "base/stl_util.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 25 | #include "base/systrace.h" |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 26 | #include "class_linker.h" |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 27 | #include "dex_file-inl.h" |
Mathieu Chartier | 61d2b2d | 2016-02-04 13:31:46 -0800 | [diff] [blame] | 28 | #include "gc/scoped_gc_critical_section.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 29 | #include "gc/space/image_space.h" |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 30 | #include "handle_scope-inl.h" |
| 31 | #include "mirror/class_loader.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 32 | #include "oat_file_assistant.h" |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 33 | #include "scoped_thread_state_change.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 34 | #include "thread-inl.h" |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 35 | #include "thread_list.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 36 | |
| 37 | namespace art { |
| 38 | |
| 39 | // For b/21333911. |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 40 | // Only enabled for debug builds to prevent bit rot. There are too many performance regressions for |
| 41 | // normal builds. |
| 42 | static constexpr bool kDuplicateClassesCheck = kIsDebugBuild; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 43 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 44 | // If true, then we attempt to load the application image if it exists. |
| 45 | static constexpr bool kEnableAppImage = true; |
| 46 | |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 47 | CompilerFilter::Filter OatFileManager::filter_ = CompilerFilter::Filter::kSpeed; |
| 48 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 49 | const OatFile* OatFileManager::RegisterOatFile(std::unique_ptr<const OatFile> oat_file) { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 50 | WriterMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 51 | DCHECK(oat_file != nullptr); |
| 52 | if (kIsDebugBuild) { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 53 | CHECK(oat_files_.find(oat_file) == oat_files_.end()); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 54 | 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 Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 62 | const OatFile* ret = oat_file.get(); |
| 63 | oat_files_.insert(std::move(oat_file)); |
| 64 | return ret; |
| 65 | } |
| 66 | |
| 67 | void 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 Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Calin Juravle | 7506423 | 2016-04-18 16:38:27 +0100 | [diff] [blame] | 77 | const 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 Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 91 | const OatFile* OatFileManager::FindOpenedOatFileFromOatLocation(const std::string& oat_location) |
| 92 | const { |
| 93 | ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 94 | return FindOpenedOatFileFromOatLocationLocked(oat_location); |
| 95 | } |
| 96 | |
| 97 | const OatFile* OatFileManager::FindOpenedOatFileFromOatLocationLocked( |
| 98 | const std::string& oat_location) const { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 99 | 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 Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 107 | std::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 Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | const OatFile* OatFileManager::GetPrimaryOatFile() const { |
| 118 | ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 119 | std::vector<const OatFile*> boot_oat_files = GetBootOatFiles(); |
| 120 | if (!boot_oat_files.empty()) { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 121 | for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 122 | if (std::find(boot_oat_files.begin(), boot_oat_files.end(), oat_file.get()) == |
| 123 | boot_oat_files.end()) { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 124 | return oat_file.get(); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | return nullptr; |
| 129 | } |
| 130 | |
| 131 | OatFileManager::~OatFileManager() { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 132 | // Explicitly clear oat_files_ since the OatFile destructor calls back into OatFileManager for |
| 133 | // UnRegisterOatFileLocation. |
| 134 | oat_files_.clear(); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 137 | std::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 Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | class 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 Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 154 | DexFileAndClassPair(const DexFileAndClassPair& rhs) = default; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 155 | |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 156 | DexFileAndClassPair& operator=(const DexFileAndClassPair& rhs) = default; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 157 | |
| 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 Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 178 | cached_descriptor_ = GetClassDescriptor(dex_file_.get(), current_class_index_); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 179 | } |
| 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 Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 201 | std::shared_ptr<const DexFile> dex_file_; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 202 | 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 | |
| 209 | static 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 | |
| 223 | static 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. |
| 238 | bool 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 Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 253 | std::vector<const OatFile*> boot_oat_files = GetBootOatFiles(); |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 254 | // 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 Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 258 | for (const std::unique_ptr<const OatFile>& loaded_oat_file : oat_files_) { |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 259 | DCHECK_NE(loaded_oat_file.get(), oat_file); |
| 260 | const std::string& location = loaded_oat_file->GetLocation(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 261 | 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 Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 263 | unique_locations.find(location) == unique_locations.end()) { |
| 264 | unique_locations.insert(location); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 265 | 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 Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 280 | DexFileAndClassPair compare_pop(queue.top()); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 281 | queue.pop(); |
| 282 | |
| 283 | // Compare against the following elements. |
| 284 | while (!queue.empty()) { |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 285 | DexFileAndClassPair top(queue.top()); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 286 | |
| 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 Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 297 | 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 | |
| 310 | std::vector<std::unique_ptr<const DexFile>> OatFileManager::OpenDexFilesFromOat( |
| 311 | const char* dex_location, |
| 312 | const char* oat_location, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 313 | jobject class_loader, |
| 314 | jobjectArray dex_elements, |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 315 | const OatFile** out_oat_file, |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 316 | std::vector<std::string>* error_msgs) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 317 | ScopedTrace trace(__FUNCTION__); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 318 | 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 Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 323 | Thread* const self = Thread::Current(); |
| 324 | Locks::mutator_lock_->AssertNotHeld(self); |
| 325 | Runtime* const runtime = Runtime::Current(); |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 326 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 327 | OatFileAssistant oat_file_assistant(dex_location, |
| 328 | oat_location, |
| 329 | kRuntimeISA, |
Andreas Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 330 | /*profile_changed*/false, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 331 | !runtime->IsAotCompiler()); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 332 | |
| 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 Geoffray | e722d29 | 2015-12-15 11:51:37 +0000 | [diff] [blame] | 344 | // 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 Uhler | ff0274b | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 346 | 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 Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | // Get the oat file on disk. |
| 363 | std::unique_ptr<const OatFile> oat_file(oat_file_assistant.GetBestOatFile().release()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 364 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 365 | 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 Uhler | 1153ae1 | 2016-04-04 13:30:16 -0700 | [diff] [blame] | 381 | if (!oat_file_assistant.HasOriginalDexFiles()) { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 382 | 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 Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 391 | *out_oat_file = source_oat_file; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 392 | } |
| 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 Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 399 | 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 Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 413 | { |
| 414 | ScopedThreadSuspension sts(self, kSuspended); |
Mathieu Chartier | 61d2b2d | 2016-02-04 13:31:46 -0800 | [diff] [blame] | 415 | gc::ScopedGCCriticalSection gcs(self, |
| 416 | gc::kGcCauseAddRemoveAppImageSpace, |
| 417 | gc::kCollectorTypeAddRemoveAppImageSpace); |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 418 | ScopedSuspendAll ssa("Add image space"); |
| 419 | runtime->GetHeap()->AddSpace(image_space.get()); |
| 420 | } |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 421 | { |
| 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 Chartier | a6e81ed | 2016-02-25 13:52:10 -0800 | [diff] [blame] | 430 | if (added_image_space) { |
Mathieu Chartier | bd064ea | 2016-02-11 16:27:18 -0800 | [diff] [blame] | 431 | // Successfully added image space to heap, release the map so that it does not get |
| 432 | // freed. |
| 433 | image_space.release(); |
| 434 | } else { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 435 | LOG(INFO) << "Failed to add image file " << temp_error_msg; |
| 436 | dex_files.clear(); |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 437 | { |
| 438 | ScopedThreadSuspension sts(self, kSuspended); |
Mathieu Chartier | 61d2b2d | 2016-02-04 13:31:46 -0800 | [diff] [blame] | 439 | gc::ScopedGCCriticalSection gcs(self, |
| 440 | gc::kGcCauseAddRemoveAppImageSpace, |
| 441 | gc::kCollectorTypeAddRemoveAppImageSpace); |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 442 | ScopedSuspendAll ssa("Remove image space"); |
| 443 | runtime->GetHeap()->RemoveSpace(image_space.get()); |
| 444 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 445 | // Non-fatal, don't update error_msg. |
| 446 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 447 | } |
| 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 Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 454 | 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 Light | 25792ae | 2016-04-20 14:26:34 -0700 | [diff] [blame] | 466 | error_msgs->push_back("Failed to open dex files from " + std::string(dex_location) |
| 467 | + " because: " + error_msg); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 468 | } |
| 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 Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 477 | |
| 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 Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 481 | return dex_files; |
| 482 | } |
| 483 | |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 484 | bool 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 | |
| 495 | void 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 Geoffray | 04680f3 | 2016-03-17 11:56:54 +0000 | [diff] [blame] | 506 | void 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 Gampe | 7bcfcb8 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 513 | os << oat_file->GetLocation() << ": " << oat_file->GetCompilerFilter() << "\n"; |
Nicolas Geoffray | 04680f3 | 2016-03-17 11:56:54 +0000 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 517 | } // namespace art |