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 | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 25 | #include "class_linker.h" |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 26 | #include "dex_file-inl.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 27 | #include "gc/space/image_space.h" |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 28 | #include "handle_scope-inl.h" |
| 29 | #include "mirror/class_loader.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 30 | #include "oat_file_assistant.h" |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 31 | #include "scoped_thread_state_change.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 32 | #include "thread-inl.h" |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame^] | 33 | #include "thread_list.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 34 | |
| 35 | namespace art { |
| 36 | |
| 37 | // For b/21333911. |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 38 | // Only enabled for debug builds to prevent bit rot. There are too many performance regressions for |
| 39 | // normal builds. |
| 40 | static constexpr bool kDuplicateClassesCheck = kIsDebugBuild; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 41 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 42 | // If true, then we attempt to load the application image if it exists. |
| 43 | static constexpr bool kEnableAppImage = true; |
| 44 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 45 | const OatFile* OatFileManager::RegisterOatFile(std::unique_ptr<const OatFile> oat_file) { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 46 | WriterMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 47 | DCHECK(oat_file != nullptr); |
| 48 | if (kIsDebugBuild) { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 49 | CHECK(oat_files_.find(oat_file) == oat_files_.end()); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 50 | for (const std::unique_ptr<const OatFile>& existing : oat_files_) { |
| 51 | CHECK_NE(oat_file.get(), existing.get()) << oat_file->GetLocation(); |
| 52 | // Check that we don't have an oat file with the same address. Copies of the same oat file |
| 53 | // should be loaded at different addresses. |
| 54 | CHECK_NE(oat_file->Begin(), existing->Begin()) << "Oat file already mapped at that location"; |
| 55 | } |
| 56 | } |
| 57 | 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] | 58 | const OatFile* ret = oat_file.get(); |
| 59 | oat_files_.insert(std::move(oat_file)); |
| 60 | return ret; |
| 61 | } |
| 62 | |
| 63 | void OatFileManager::UnRegisterAndDeleteOatFile(const OatFile* oat_file) { |
| 64 | WriterMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
| 65 | DCHECK(oat_file != nullptr); |
| 66 | std::unique_ptr<const OatFile> compare(oat_file); |
| 67 | auto it = oat_files_.find(compare); |
| 68 | CHECK(it != oat_files_.end()); |
| 69 | oat_files_.erase(it); |
| 70 | compare.release(); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | const OatFile* OatFileManager::FindOpenedOatFileFromOatLocation(const std::string& oat_location) |
| 74 | const { |
| 75 | ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 76 | return FindOpenedOatFileFromOatLocationLocked(oat_location); |
| 77 | } |
| 78 | |
| 79 | const OatFile* OatFileManager::FindOpenedOatFileFromOatLocationLocked( |
| 80 | const std::string& oat_location) const { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 81 | for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) { |
| 82 | if (oat_file->GetLocation() == oat_location) { |
| 83 | return oat_file.get(); |
| 84 | } |
| 85 | } |
| 86 | return nullptr; |
| 87 | } |
| 88 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 89 | std::vector<const OatFile*> OatFileManager::GetBootOatFiles() const { |
| 90 | std::vector<const OatFile*> oat_files; |
| 91 | std::vector<gc::space::ImageSpace*> image_spaces = |
| 92 | Runtime::Current()->GetHeap()->GetBootImageSpaces(); |
| 93 | for (gc::space::ImageSpace* image_space : image_spaces) { |
| 94 | oat_files.push_back(image_space->GetOatFile()); |
| 95 | } |
| 96 | return oat_files; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | const OatFile* OatFileManager::GetPrimaryOatFile() const { |
| 100 | ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 101 | std::vector<const OatFile*> boot_oat_files = GetBootOatFiles(); |
| 102 | if (!boot_oat_files.empty()) { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 103 | for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 104 | if (std::find(boot_oat_files.begin(), boot_oat_files.end(), oat_file.get()) == |
| 105 | boot_oat_files.end()) { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 106 | return oat_file.get(); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | return nullptr; |
| 111 | } |
| 112 | |
| 113 | OatFileManager::~OatFileManager() { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 114 | // Explicitly clear oat_files_ since the OatFile destructor calls back into OatFileManager for |
| 115 | // UnRegisterOatFileLocation. |
| 116 | oat_files_.clear(); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 119 | std::vector<const OatFile*> OatFileManager::RegisterImageOatFiles( |
| 120 | std::vector<gc::space::ImageSpace*> spaces) { |
| 121 | std::vector<const OatFile*> oat_files; |
| 122 | for (gc::space::ImageSpace* space : spaces) { |
| 123 | oat_files.push_back(RegisterOatFile(space->ReleaseOatFile())); |
| 124 | } |
| 125 | return oat_files; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | class DexFileAndClassPair : ValueObject { |
| 129 | public: |
| 130 | DexFileAndClassPair(const DexFile* dex_file, size_t current_class_index, bool from_loaded_oat) |
| 131 | : cached_descriptor_(GetClassDescriptor(dex_file, current_class_index)), |
| 132 | dex_file_(dex_file), |
| 133 | current_class_index_(current_class_index), |
| 134 | from_loaded_oat_(from_loaded_oat) {} |
| 135 | |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 136 | DexFileAndClassPair(const DexFileAndClassPair& rhs) = default; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 137 | |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 138 | DexFileAndClassPair& operator=(const DexFileAndClassPair& rhs) = default; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 139 | |
| 140 | const char* GetCachedDescriptor() const { |
| 141 | return cached_descriptor_; |
| 142 | } |
| 143 | |
| 144 | bool operator<(const DexFileAndClassPair& rhs) const { |
| 145 | const int cmp = strcmp(cached_descriptor_, rhs.cached_descriptor_); |
| 146 | if (cmp != 0) { |
| 147 | // Note that the order must be reversed. We want to iterate over the classes in dex files. |
| 148 | // They are sorted lexicographically. Thus, the priority-queue must be a min-queue. |
| 149 | return cmp > 0; |
| 150 | } |
| 151 | return dex_file_ < rhs.dex_file_; |
| 152 | } |
| 153 | |
| 154 | bool DexFileHasMoreClasses() const { |
| 155 | return current_class_index_ + 1 < dex_file_->NumClassDefs(); |
| 156 | } |
| 157 | |
| 158 | void Next() { |
| 159 | ++current_class_index_; |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 160 | cached_descriptor_ = GetClassDescriptor(dex_file_.get(), current_class_index_); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | size_t GetCurrentClassIndex() const { |
| 164 | return current_class_index_; |
| 165 | } |
| 166 | |
| 167 | bool FromLoadedOat() const { |
| 168 | return from_loaded_oat_; |
| 169 | } |
| 170 | |
| 171 | const DexFile* GetDexFile() const { |
| 172 | return dex_file_.get(); |
| 173 | } |
| 174 | |
| 175 | private: |
| 176 | static const char* GetClassDescriptor(const DexFile* dex_file, size_t index) { |
| 177 | DCHECK(IsUint<16>(index)); |
| 178 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(static_cast<uint16_t>(index)); |
| 179 | return dex_file->StringByTypeIdx(class_def.class_idx_); |
| 180 | } |
| 181 | |
| 182 | const char* cached_descriptor_; |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 183 | std::shared_ptr<const DexFile> dex_file_; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 184 | size_t current_class_index_; |
| 185 | bool from_loaded_oat_; // We only need to compare mismatches between what we load now |
| 186 | // and what was loaded before. Any old duplicates must have been |
| 187 | // OK, and any new "internal" duplicates are as well (they must |
| 188 | // be from multidex, which resolves correctly). |
| 189 | }; |
| 190 | |
| 191 | static void AddDexFilesFromOat(const OatFile* oat_file, |
| 192 | bool already_loaded, |
| 193 | /*out*/std::priority_queue<DexFileAndClassPair>* heap) { |
| 194 | for (const OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) { |
| 195 | std::string error; |
| 196 | std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error); |
| 197 | if (dex_file == nullptr) { |
| 198 | LOG(WARNING) << "Could not create dex file from oat file: " << error; |
| 199 | } else if (dex_file->NumClassDefs() > 0U) { |
| 200 | heap->emplace(dex_file.release(), /*current_class_index*/0U, already_loaded); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | static void AddNext(/*inout*/DexFileAndClassPair* original, |
| 206 | /*inout*/std::priority_queue<DexFileAndClassPair>* heap) { |
| 207 | if (original->DexFileHasMoreClasses()) { |
| 208 | original->Next(); |
| 209 | heap->push(std::move(*original)); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | // Check for class-def collisions in dex files. |
| 214 | // |
| 215 | // This works by maintaining a heap with one class from each dex file, sorted by the class |
| 216 | // descriptor. Then a dex-file/class pair is continually removed from the heap and compared |
| 217 | // against the following top element. If the descriptor is the same, it is now checked whether |
| 218 | // the two elements agree on whether their dex file was from an already-loaded oat-file or the |
| 219 | // new oat file. Any disagreement indicates a collision. |
| 220 | bool OatFileManager::HasCollisions(const OatFile* oat_file, |
| 221 | std::string* error_msg /*out*/) const { |
| 222 | DCHECK(oat_file != nullptr); |
| 223 | DCHECK(error_msg != nullptr); |
| 224 | if (!kDuplicateClassesCheck) { |
| 225 | return false; |
| 226 | } |
| 227 | |
| 228 | // Dex files are registered late - once a class is actually being loaded. We have to compare |
| 229 | // against the open oat files. Take the oat_file_manager_lock_ that protects oat_files_ accesses. |
| 230 | ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
| 231 | |
| 232 | std::priority_queue<DexFileAndClassPair> queue; |
| 233 | |
| 234 | // Add dex files from already loaded oat files, but skip boot. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 235 | std::vector<const OatFile*> boot_oat_files = GetBootOatFiles(); |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 236 | // The same OatFile can be loaded multiple times at different addresses. In this case, we don't |
| 237 | // need to check both against each other since they would have resolved the same way at compile |
| 238 | // time. |
| 239 | std::unordered_set<std::string> unique_locations; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 240 | for (const std::unique_ptr<const OatFile>& loaded_oat_file : oat_files_) { |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 241 | DCHECK_NE(loaded_oat_file.get(), oat_file); |
| 242 | const std::string& location = loaded_oat_file->GetLocation(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 243 | if (std::find(boot_oat_files.begin(), boot_oat_files.end(), loaded_oat_file.get()) == |
| 244 | boot_oat_files.end() && location != oat_file->GetLocation() && |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 245 | unique_locations.find(location) == unique_locations.end()) { |
| 246 | unique_locations.insert(location); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 247 | AddDexFilesFromOat(loaded_oat_file.get(), /*already_loaded*/true, &queue); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | if (queue.empty()) { |
| 252 | // No other oat files, return early. |
| 253 | return false; |
| 254 | } |
| 255 | |
| 256 | // Add dex files from the oat file to check. |
| 257 | AddDexFilesFromOat(oat_file, /*already_loaded*/false, &queue); |
| 258 | |
| 259 | // Now drain the queue. |
| 260 | while (!queue.empty()) { |
| 261 | // Modifying the top element is only safe if we pop right after. |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 262 | DexFileAndClassPair compare_pop(queue.top()); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 263 | queue.pop(); |
| 264 | |
| 265 | // Compare against the following elements. |
| 266 | while (!queue.empty()) { |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 267 | DexFileAndClassPair top(queue.top()); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 268 | |
| 269 | if (strcmp(compare_pop.GetCachedDescriptor(), top.GetCachedDescriptor()) == 0) { |
| 270 | // Same descriptor. Check whether it's crossing old-oat-files to new-oat-files. |
| 271 | if (compare_pop.FromLoadedOat() != top.FromLoadedOat()) { |
| 272 | *error_msg = |
| 273 | StringPrintf("Found duplicated class when checking oat files: '%s' in %s and %s", |
| 274 | compare_pop.GetCachedDescriptor(), |
| 275 | compare_pop.GetDexFile()->GetLocation().c_str(), |
| 276 | top.GetDexFile()->GetLocation().c_str()); |
| 277 | return true; |
| 278 | } |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 279 | queue.pop(); |
| 280 | AddNext(&top, &queue); |
| 281 | } else { |
| 282 | // Something else. Done here. |
| 283 | break; |
| 284 | } |
| 285 | } |
| 286 | AddNext(&compare_pop, &queue); |
| 287 | } |
| 288 | |
| 289 | return false; |
| 290 | } |
| 291 | |
| 292 | std::vector<std::unique_ptr<const DexFile>> OatFileManager::OpenDexFilesFromOat( |
| 293 | const char* dex_location, |
| 294 | const char* oat_location, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 295 | jobject class_loader, |
| 296 | jobjectArray dex_elements, |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 297 | const OatFile** out_oat_file, |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 298 | std::vector<std::string>* error_msgs) { |
| 299 | CHECK(dex_location != nullptr); |
| 300 | CHECK(error_msgs != nullptr); |
| 301 | |
| 302 | // Verify we aren't holding the mutator lock, which could starve GC if we |
| 303 | // have to generate or relocate an oat file. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 304 | Thread* const self = Thread::Current(); |
| 305 | Locks::mutator_lock_->AssertNotHeld(self); |
| 306 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 307 | OatFileAssistant oat_file_assistant(dex_location, |
| 308 | oat_location, |
| 309 | kRuntimeISA, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 310 | !runtime->IsAotCompiler()); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 311 | |
| 312 | // Lock the target oat location to avoid races generating and loading the |
| 313 | // oat file. |
| 314 | std::string error_msg; |
| 315 | if (!oat_file_assistant.Lock(/*out*/&error_msg)) { |
| 316 | // Don't worry too much if this fails. If it does fail, it's unlikely we |
| 317 | // can generate an oat file anyway. |
| 318 | VLOG(class_linker) << "OatFileAssistant::Lock: " << error_msg; |
| 319 | } |
| 320 | |
| 321 | const OatFile* source_oat_file = nullptr; |
| 322 | |
Nicolas Geoffray | e722d29 | 2015-12-15 11:51:37 +0000 | [diff] [blame] | 323 | // Update the oat file on disk if we can. This may fail, but that's okay. |
| 324 | // Best effort is all that matters here. |
| 325 | if (!oat_file_assistant.MakeUpToDate(/*out*/&error_msg)) { |
Nicolas Geoffray | a28267f | 2015-12-16 12:34:50 +0000 | [diff] [blame] | 326 | LOG(INFO) << error_msg; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | // Get the oat file on disk. |
| 330 | std::unique_ptr<const OatFile> oat_file(oat_file_assistant.GetBestOatFile().release()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 331 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 332 | if (oat_file != nullptr) { |
| 333 | // Take the file only if it has no collisions, or we must take it because of preopting. |
| 334 | bool accept_oat_file = !HasCollisions(oat_file.get(), /*out*/ &error_msg); |
| 335 | if (!accept_oat_file) { |
| 336 | // Failed the collision check. Print warning. |
| 337 | if (Runtime::Current()->IsDexFileFallbackEnabled()) { |
| 338 | LOG(WARNING) << "Found duplicate classes, falling back to interpreter mode for " |
| 339 | << dex_location; |
| 340 | } else { |
| 341 | LOG(WARNING) << "Found duplicate classes, dex-file-fallback disabled, will be failing to " |
| 342 | " load classes for " << dex_location; |
| 343 | } |
| 344 | LOG(WARNING) << error_msg; |
| 345 | |
| 346 | // However, if the app was part of /system and preopted, there is no original dex file |
| 347 | // available. In that case grudgingly accept the oat file. |
| 348 | if (!DexFile::MaybeDex(dex_location)) { |
| 349 | accept_oat_file = true; |
| 350 | LOG(WARNING) << "Dex location " << dex_location << " does not seem to include dex file. " |
| 351 | << "Allow oat file use. This is potentially dangerous."; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | if (accept_oat_file) { |
| 356 | VLOG(class_linker) << "Registering " << oat_file->GetLocation(); |
| 357 | source_oat_file = RegisterOatFile(std::move(oat_file)); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 358 | *out_oat_file = source_oat_file; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | |
| 362 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 363 | |
| 364 | // Load the dex files from the oat file. |
| 365 | if (source_oat_file != nullptr) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 366 | bool added_image_space = false; |
| 367 | if (source_oat_file->IsExecutable()) { |
| 368 | std::unique_ptr<gc::space::ImageSpace> image_space( |
| 369 | kEnableAppImage ? oat_file_assistant.OpenImageSpace(source_oat_file) : nullptr); |
| 370 | if (image_space != nullptr) { |
| 371 | ScopedObjectAccess soa(self); |
| 372 | StackHandleScope<1> hs(self); |
| 373 | Handle<mirror::ClassLoader> h_loader( |
| 374 | hs.NewHandle(soa.Decode<mirror::ClassLoader*>(class_loader))); |
| 375 | // Can not load app image without class loader. |
| 376 | if (h_loader.Get() != nullptr) { |
| 377 | std::string temp_error_msg; |
| 378 | // Add image space has a race condition since other threads could be reading from the |
| 379 | // spaces array. |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame^] | 380 | { |
| 381 | ScopedThreadSuspension sts(self, kSuspended); |
| 382 | ScopedSuspendAll ssa("Add image space"); |
| 383 | runtime->GetHeap()->AddSpace(image_space.get()); |
| 384 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 385 | added_image_space = true; |
| 386 | if (!runtime->GetClassLinker()->AddImageSpace(image_space.get(), |
| 387 | h_loader, |
| 388 | dex_elements, |
| 389 | dex_location, |
| 390 | /*out*/&dex_files, |
| 391 | /*out*/&temp_error_msg)) { |
| 392 | LOG(INFO) << "Failed to add image file " << temp_error_msg; |
| 393 | dex_files.clear(); |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame^] | 394 | { |
| 395 | ScopedThreadSuspension sts(self, kSuspended); |
| 396 | ScopedSuspendAll ssa("Remove image space"); |
| 397 | runtime->GetHeap()->RemoveSpace(image_space.get()); |
| 398 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 399 | added_image_space = false; |
| 400 | // Non-fatal, don't update error_msg. |
| 401 | } |
| 402 | image_space.release(); |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | if (!added_image_space) { |
| 407 | DCHECK(dex_files.empty()); |
| 408 | dex_files = oat_file_assistant.LoadDexFiles(*source_oat_file, dex_location); |
| 409 | } |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 410 | if (dex_files.empty()) { |
| 411 | error_msgs->push_back("Failed to open dex files from " + source_oat_file->GetLocation()); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | // Fall back to running out of the original dex file if we couldn't load any |
| 416 | // dex_files from the oat file. |
| 417 | if (dex_files.empty()) { |
| 418 | if (oat_file_assistant.HasOriginalDexFiles()) { |
| 419 | if (Runtime::Current()->IsDexFileFallbackEnabled()) { |
| 420 | if (!DexFile::Open(dex_location, dex_location, /*out*/ &error_msg, &dex_files)) { |
| 421 | LOG(WARNING) << error_msg; |
| 422 | error_msgs->push_back("Failed to open dex files from " + std::string(dex_location)); |
| 423 | } |
| 424 | } else { |
| 425 | error_msgs->push_back("Fallback mode disabled, skipping dex files."); |
| 426 | } |
| 427 | } else { |
| 428 | error_msgs->push_back("No original dex files found for dex location " |
| 429 | + std::string(dex_location)); |
| 430 | } |
| 431 | } |
| 432 | return dex_files; |
| 433 | } |
| 434 | |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 435 | bool OatFileManager::RegisterOatFileLocation(const std::string& oat_location) { |
| 436 | WriterMutexLock mu(Thread::Current(), *Locks::oat_file_count_lock_); |
| 437 | auto it = oat_file_count_.find(oat_location); |
| 438 | if (it != oat_file_count_.end()) { |
| 439 | ++it->second; |
| 440 | return false; |
| 441 | } |
| 442 | oat_file_count_.insert(std::pair<std::string, size_t>(oat_location, 1u)); |
| 443 | return true; |
| 444 | } |
| 445 | |
| 446 | void OatFileManager::UnRegisterOatFileLocation(const std::string& oat_location) { |
| 447 | WriterMutexLock mu(Thread::Current(), *Locks::oat_file_count_lock_); |
| 448 | auto it = oat_file_count_.find(oat_location); |
| 449 | if (it != oat_file_count_.end()) { |
| 450 | --it->second; |
| 451 | if (it->second == 0) { |
| 452 | oat_file_count_.erase(it); |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 457 | } // namespace art |