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 | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame^] | 33 | #include "scoped_thread_state_change-inl.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 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 39 | // If true, then we attempt to load the application image if it exists. |
| 40 | static constexpr bool kEnableAppImage = true; |
| 41 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 42 | const OatFile* OatFileManager::RegisterOatFile(std::unique_ptr<const OatFile> oat_file) { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 43 | WriterMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 44 | DCHECK(oat_file != nullptr); |
| 45 | if (kIsDebugBuild) { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 46 | CHECK(oat_files_.find(oat_file) == oat_files_.end()); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 47 | for (const std::unique_ptr<const OatFile>& existing : oat_files_) { |
| 48 | CHECK_NE(oat_file.get(), existing.get()) << oat_file->GetLocation(); |
| 49 | // Check that we don't have an oat file with the same address. Copies of the same oat file |
| 50 | // should be loaded at different addresses. |
| 51 | CHECK_NE(oat_file->Begin(), existing->Begin()) << "Oat file already mapped at that location"; |
| 52 | } |
| 53 | } |
| 54 | 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] | 55 | const OatFile* ret = oat_file.get(); |
| 56 | oat_files_.insert(std::move(oat_file)); |
| 57 | return ret; |
| 58 | } |
| 59 | |
| 60 | void OatFileManager::UnRegisterAndDeleteOatFile(const OatFile* oat_file) { |
| 61 | WriterMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
| 62 | DCHECK(oat_file != nullptr); |
| 63 | std::unique_ptr<const OatFile> compare(oat_file); |
| 64 | auto it = oat_files_.find(compare); |
| 65 | CHECK(it != oat_files_.end()); |
| 66 | oat_files_.erase(it); |
| 67 | compare.release(); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Calin Juravle | 0b79127 | 2016-04-18 16:38:27 +0100 | [diff] [blame] | 70 | const OatFile* OatFileManager::FindOpenedOatFileFromDexLocation( |
| 71 | const std::string& dex_base_location) const { |
| 72 | ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
| 73 | for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) { |
| 74 | const std::vector<const OatDexFile*>& oat_dex_files = oat_file->GetOatDexFiles(); |
| 75 | for (const OatDexFile* oat_dex_file : oat_dex_files) { |
| 76 | if (DexFile::GetBaseLocation(oat_dex_file->GetDexFileLocation()) == dex_base_location) { |
| 77 | return oat_file.get(); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | return nullptr; |
| 82 | } |
| 83 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 84 | const OatFile* OatFileManager::FindOpenedOatFileFromOatLocation(const std::string& oat_location) |
| 85 | const { |
| 86 | ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 87 | return FindOpenedOatFileFromOatLocationLocked(oat_location); |
| 88 | } |
| 89 | |
| 90 | const OatFile* OatFileManager::FindOpenedOatFileFromOatLocationLocked( |
| 91 | const std::string& oat_location) const { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 92 | for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) { |
| 93 | if (oat_file->GetLocation() == oat_location) { |
| 94 | return oat_file.get(); |
| 95 | } |
| 96 | } |
| 97 | return nullptr; |
| 98 | } |
| 99 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 100 | std::vector<const OatFile*> OatFileManager::GetBootOatFiles() const { |
| 101 | std::vector<const OatFile*> oat_files; |
| 102 | std::vector<gc::space::ImageSpace*> image_spaces = |
| 103 | Runtime::Current()->GetHeap()->GetBootImageSpaces(); |
| 104 | for (gc::space::ImageSpace* image_space : image_spaces) { |
| 105 | oat_files.push_back(image_space->GetOatFile()); |
| 106 | } |
| 107 | return oat_files; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | const OatFile* OatFileManager::GetPrimaryOatFile() const { |
| 111 | ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 112 | std::vector<const OatFile*> boot_oat_files = GetBootOatFiles(); |
| 113 | if (!boot_oat_files.empty()) { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 114 | for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 115 | if (std::find(boot_oat_files.begin(), boot_oat_files.end(), oat_file.get()) == |
| 116 | boot_oat_files.end()) { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 117 | return oat_file.get(); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | return nullptr; |
| 122 | } |
| 123 | |
| 124 | OatFileManager::~OatFileManager() { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 125 | // Explicitly clear oat_files_ since the OatFile destructor calls back into OatFileManager for |
| 126 | // UnRegisterOatFileLocation. |
| 127 | oat_files_.clear(); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 130 | std::vector<const OatFile*> OatFileManager::RegisterImageOatFiles( |
| 131 | std::vector<gc::space::ImageSpace*> spaces) { |
| 132 | std::vector<const OatFile*> oat_files; |
| 133 | for (gc::space::ImageSpace* space : spaces) { |
| 134 | oat_files.push_back(RegisterOatFile(space->ReleaseOatFile())); |
| 135 | } |
| 136 | return oat_files; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | class DexFileAndClassPair : ValueObject { |
| 140 | public: |
| 141 | DexFileAndClassPair(const DexFile* dex_file, size_t current_class_index, bool from_loaded_oat) |
| 142 | : cached_descriptor_(GetClassDescriptor(dex_file, current_class_index)), |
| 143 | dex_file_(dex_file), |
| 144 | current_class_index_(current_class_index), |
| 145 | from_loaded_oat_(from_loaded_oat) {} |
| 146 | |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 147 | DexFileAndClassPair(const DexFileAndClassPair& rhs) = default; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 148 | |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 149 | DexFileAndClassPair& operator=(const DexFileAndClassPair& rhs) = default; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 150 | |
| 151 | const char* GetCachedDescriptor() const { |
| 152 | return cached_descriptor_; |
| 153 | } |
| 154 | |
| 155 | bool operator<(const DexFileAndClassPair& rhs) const { |
| 156 | const int cmp = strcmp(cached_descriptor_, rhs.cached_descriptor_); |
| 157 | if (cmp != 0) { |
| 158 | // Note that the order must be reversed. We want to iterate over the classes in dex files. |
| 159 | // They are sorted lexicographically. Thus, the priority-queue must be a min-queue. |
| 160 | return cmp > 0; |
| 161 | } |
| 162 | return dex_file_ < rhs.dex_file_; |
| 163 | } |
| 164 | |
| 165 | bool DexFileHasMoreClasses() const { |
| 166 | return current_class_index_ + 1 < dex_file_->NumClassDefs(); |
| 167 | } |
| 168 | |
| 169 | void Next() { |
| 170 | ++current_class_index_; |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 171 | cached_descriptor_ = GetClassDescriptor(dex_file_, current_class_index_); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | size_t GetCurrentClassIndex() const { |
| 175 | return current_class_index_; |
| 176 | } |
| 177 | |
| 178 | bool FromLoadedOat() const { |
| 179 | return from_loaded_oat_; |
| 180 | } |
| 181 | |
| 182 | const DexFile* GetDexFile() const { |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 183 | return dex_file_; |
| 184 | } |
| 185 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 186 | private: |
| 187 | static const char* GetClassDescriptor(const DexFile* dex_file, size_t index) { |
| 188 | DCHECK(IsUint<16>(index)); |
| 189 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(static_cast<uint16_t>(index)); |
| 190 | return dex_file->StringByTypeIdx(class_def.class_idx_); |
| 191 | } |
| 192 | |
| 193 | const char* cached_descriptor_; |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 194 | const DexFile* dex_file_; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 195 | size_t current_class_index_; |
| 196 | bool from_loaded_oat_; // We only need to compare mismatches between what we load now |
| 197 | // and what was loaded before. Any old duplicates must have been |
| 198 | // OK, and any new "internal" duplicates are as well (they must |
| 199 | // be from multidex, which resolves correctly). |
| 200 | }; |
| 201 | |
| 202 | static void AddDexFilesFromOat(const OatFile* oat_file, |
| 203 | bool already_loaded, |
Mathieu Chartier | 14d7b3e | 2016-06-09 16:18:04 -0700 | [diff] [blame] | 204 | /*out*/std::priority_queue<DexFileAndClassPair>* heap, |
| 205 | std::vector<std::unique_ptr<const DexFile>>* opened_dex_files) { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 206 | for (const OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) { |
| 207 | std::string error; |
| 208 | std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error); |
| 209 | if (dex_file == nullptr) { |
| 210 | LOG(WARNING) << "Could not create dex file from oat file: " << error; |
| 211 | } else if (dex_file->NumClassDefs() > 0U) { |
Mathieu Chartier | 14d7b3e | 2016-06-09 16:18:04 -0700 | [diff] [blame] | 212 | heap->emplace(dex_file.get(), /*current_class_index*/0U, already_loaded); |
| 213 | opened_dex_files->push_back(std::move(dex_file)); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | static void AddNext(/*inout*/DexFileAndClassPair* original, |
Mathieu Chartier | 14d7b3e | 2016-06-09 16:18:04 -0700 | [diff] [blame] | 219 | /*inout*/std::priority_queue<DexFileAndClassPair>* heap) { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 220 | if (original->DexFileHasMoreClasses()) { |
| 221 | original->Next(); |
| 222 | heap->push(std::move(*original)); |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
| 226 | static void IterateOverJavaDexFile(mirror::Object* dex_file, |
| 227 | ArtField* const cookie_field, |
| 228 | std::function<bool(const DexFile*)> fn) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 229 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 230 | if (dex_file != nullptr) { |
| 231 | mirror::LongArray* long_array = cookie_field->GetObject(dex_file)->AsLongArray(); |
| 232 | if (long_array == nullptr) { |
| 233 | // This should never happen so log a warning. |
| 234 | LOG(WARNING) << "Null DexFile::mCookie"; |
| 235 | return; |
| 236 | } |
| 237 | int32_t long_array_size = long_array->GetLength(); |
| 238 | // Start from 1 to skip the oat file. |
| 239 | for (int32_t j = 1; j < long_array_size; ++j) { |
| 240 | const DexFile* cp_dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>( |
| 241 | long_array->GetWithoutChecks(j))); |
| 242 | if (!fn(cp_dex_file)) { |
| 243 | return; |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | static void IterateOverPathClassLoader( |
| 250 | ScopedObjectAccessAlreadyRunnable& soa, |
| 251 | Handle<mirror::ClassLoader> class_loader, |
| 252 | MutableHandle<mirror::ObjectArray<mirror::Object>> dex_elements, |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 253 | std::function<bool(const DexFile*)> fn) REQUIRES_SHARED(Locks::mutator_lock_) { |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 254 | // Handle this step. |
| 255 | // Handle as if this is the child PathClassLoader. |
| 256 | // The class loader is a PathClassLoader which inherits from BaseDexClassLoader. |
| 257 | // We need to get the DexPathList and loop through it. |
| 258 | ArtField* const cookie_field = soa.DecodeField(WellKnownClasses::dalvik_system_DexFile_cookie); |
| 259 | ArtField* const dex_file_field = |
| 260 | soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); |
| 261 | mirror::Object* dex_path_list = |
| 262 | soa.DecodeField(WellKnownClasses::dalvik_system_PathClassLoader_pathList)-> |
| 263 | GetObject(class_loader.Get()); |
| 264 | if (dex_path_list != nullptr && dex_file_field != nullptr && cookie_field != nullptr) { |
| 265 | // DexPathList has an array dexElements of Elements[] which each contain a dex file. |
| 266 | mirror::Object* dex_elements_obj = |
| 267 | soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList_dexElements)-> |
| 268 | GetObject(dex_path_list); |
| 269 | // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look |
| 270 | // at the mCookie which is a DexFile vector. |
| 271 | if (dex_elements_obj != nullptr) { |
| 272 | dex_elements.Assign(dex_elements_obj->AsObjectArray<mirror::Object>()); |
| 273 | for (int32_t i = 0; i < dex_elements->GetLength(); ++i) { |
| 274 | mirror::Object* element = dex_elements->GetWithoutChecks(i); |
| 275 | if (element == nullptr) { |
| 276 | // Should never happen, fall back to java code to throw a NPE. |
| 277 | break; |
| 278 | } |
| 279 | mirror::Object* dex_file = dex_file_field->GetObject(element); |
| 280 | IterateOverJavaDexFile(dex_file, cookie_field, fn); |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | static bool GetDexFilesFromClassLoader( |
| 287 | ScopedObjectAccessAlreadyRunnable& soa, |
| 288 | mirror::ClassLoader* class_loader, |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 289 | std::priority_queue<DexFileAndClassPair>* queue) REQUIRES_SHARED(Locks::mutator_lock_) { |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 290 | if (ClassLinker::IsBootClassLoader(soa, class_loader)) { |
| 291 | // The boot class loader. We don't load any of these files, as we know we compiled against |
| 292 | // them correctly. |
| 293 | return true; |
| 294 | } |
| 295 | |
| 296 | // Unsupported class-loader? |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame^] | 297 | if (soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_PathClassLoader) != |
| 298 | class_loader->GetClass()) { |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 299 | VLOG(class_linker) << "Unsupported class-loader " << PrettyClass(class_loader->GetClass()); |
| 300 | return false; |
| 301 | } |
| 302 | |
| 303 | bool recursive_result = GetDexFilesFromClassLoader(soa, class_loader->GetParent(), queue); |
| 304 | if (!recursive_result) { |
| 305 | // Something wrong up the chain. |
| 306 | return false; |
| 307 | } |
| 308 | |
| 309 | // Collect all the dex files. |
| 310 | auto GetDexFilesFn = [&] (const DexFile* cp_dex_file) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 311 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 312 | if (cp_dex_file->NumClassDefs() > 0) { |
| 313 | queue->emplace(cp_dex_file, 0U, true); |
| 314 | } |
| 315 | return true; // Continue looking. |
| 316 | }; |
| 317 | |
| 318 | // Handle for dex-cache-element. |
| 319 | StackHandleScope<3> hs(soa.Self()); |
| 320 | MutableHandle<mirror::ObjectArray<mirror::Object>> dex_elements( |
| 321 | hs.NewHandle<mirror::ObjectArray<mirror::Object>>(nullptr)); |
| 322 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
| 323 | |
| 324 | IterateOverPathClassLoader(soa, h_class_loader, dex_elements, GetDexFilesFn); |
| 325 | |
| 326 | return true; |
| 327 | } |
| 328 | |
| 329 | static void GetDexFilesFromDexElementsArray( |
| 330 | ScopedObjectAccessAlreadyRunnable& soa, |
| 331 | Handle<mirror::ObjectArray<mirror::Object>> dex_elements, |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 332 | std::priority_queue<DexFileAndClassPair>* queue) REQUIRES_SHARED(Locks::mutator_lock_) { |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 333 | if (dex_elements.Get() == nullptr) { |
| 334 | // Nothing to do. |
| 335 | return; |
| 336 | } |
| 337 | |
| 338 | ArtField* const cookie_field = soa.DecodeField(WellKnownClasses::dalvik_system_DexFile_cookie); |
| 339 | ArtField* const dex_file_field = |
| 340 | soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame^] | 341 | ObjPtr<mirror::Class> const element_class = soa.Decode<mirror::Class>( |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 342 | WellKnownClasses::dalvik_system_DexPathList__Element); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame^] | 343 | ObjPtr<mirror::Class> const dexfile_class = soa.Decode<mirror::Class>( |
| 344 | WellKnownClasses::dalvik_system_DexFile); |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 345 | |
| 346 | // Collect all the dex files. |
| 347 | auto GetDexFilesFn = [&] (const DexFile* cp_dex_file) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 348 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 349 | if (cp_dex_file != nullptr && cp_dex_file->NumClassDefs() > 0) { |
| 350 | queue->emplace(cp_dex_file, 0U, true); |
| 351 | } |
| 352 | return true; // Continue looking. |
| 353 | }; |
| 354 | |
| 355 | for (int32_t i = 0; i < dex_elements->GetLength(); ++i) { |
| 356 | mirror::Object* element = dex_elements->GetWithoutChecks(i); |
| 357 | if (element == nullptr) { |
| 358 | continue; |
| 359 | } |
| 360 | |
| 361 | // We support this being dalvik.system.DexPathList$Element and dalvik.system.DexFile. |
| 362 | |
| 363 | mirror::Object* dex_file; |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame^] | 364 | if (element_class == element->GetClass()) { |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 365 | dex_file = dex_file_field->GetObject(element); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame^] | 366 | } else if (dexfile_class == element->GetClass()) { |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 367 | dex_file = element; |
| 368 | } else { |
| 369 | LOG(WARNING) << "Unsupported element in dex_elements: " << PrettyClass(element->GetClass()); |
| 370 | continue; |
| 371 | } |
| 372 | |
| 373 | IterateOverJavaDexFile(dex_file, cookie_field, GetDexFilesFn); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | static bool AreSharedLibrariesOk(const std::string shared_libraries, |
| 378 | std::priority_queue<DexFileAndClassPair>& queue) { |
| 379 | if (shared_libraries.empty()) { |
| 380 | if (queue.empty()) { |
| 381 | // No shared libraries or oat files, as expected. |
| 382 | return true; |
| 383 | } |
| 384 | } else { |
| 385 | if (shared_libraries.compare(OatFile::kSpecialSharedLibrary) == 0) { |
| 386 | // If we find the special shared library, skip the shared libraries check. |
| 387 | return true; |
| 388 | } |
| 389 | // Shared libraries is a series of dex file paths and their checksums, each separated by '*'. |
| 390 | std::vector<std::string> shared_libraries_split; |
| 391 | Split(shared_libraries, '*', &shared_libraries_split); |
| 392 | |
| 393 | size_t index = 0; |
| 394 | std::priority_queue<DexFileAndClassPair> temp = queue; |
| 395 | while (!temp.empty() && index < shared_libraries_split.size() - 1) { |
| 396 | DexFileAndClassPair pair(temp.top()); |
| 397 | const DexFile* dex_file = pair.GetDexFile(); |
| 398 | std::string dex_filename(dex_file->GetLocation()); |
| 399 | uint32_t dex_checksum = dex_file->GetLocationChecksum(); |
| 400 | if (dex_filename != shared_libraries_split[index] || |
| 401 | dex_checksum != std::stoul(shared_libraries_split[index + 1])) { |
| 402 | break; |
| 403 | } |
| 404 | temp.pop(); |
| 405 | index += 2; |
| 406 | } |
| 407 | |
| 408 | // Check is successful if it made it through the queue and all the shared libraries. |
| 409 | return temp.empty() && index == shared_libraries_split.size(); |
| 410 | } |
| 411 | return false; |
| 412 | } |
| 413 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 414 | // Check for class-def collisions in dex files. |
| 415 | // |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 416 | // This first walks the class loader chain, getting all the dex files from the class loader. If |
| 417 | // the class loader is null or one of the class loaders in the chain is unsupported, we collect |
| 418 | // dex files from all open non-boot oat files to be safe. |
| 419 | // |
| 420 | // This first checks whether the shared libraries are in the expected order and the oat files |
| 421 | // have the expected checksums. If so, we exit early. Otherwise, we do the collision check. |
| 422 | // |
| 423 | // The collision check works by maintaining a heap with one class from each dex file, sorted by the |
| 424 | // 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] | 425 | // against the following top element. If the descriptor is the same, it is now checked whether |
| 426 | // the two elements agree on whether their dex file was from an already-loaded oat-file or the |
| 427 | // new oat file. Any disagreement indicates a collision. |
| 428 | bool OatFileManager::HasCollisions(const OatFile* oat_file, |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 429 | jobject class_loader, |
| 430 | jobjectArray dex_elements, |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 431 | std::string* error_msg /*out*/) const { |
| 432 | DCHECK(oat_file != nullptr); |
| 433 | DCHECK(error_msg != nullptr); |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 434 | |
| 435 | std::priority_queue<DexFileAndClassPair> queue; |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 436 | |
| 437 | // Try to get dex files from the given class loader. If the class loader is null, or we do |
| 438 | // not support one of the class loaders in the chain, conservatively compare against all |
| 439 | // (non-boot) oat files. |
| 440 | bool class_loader_ok = false; |
| 441 | { |
| 442 | ScopedObjectAccess soa(Thread::Current()); |
| 443 | StackHandleScope<2> hs(Thread::Current()); |
| 444 | Handle<mirror::ClassLoader> h_class_loader = |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame^] | 445 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader)); |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 446 | Handle<mirror::ObjectArray<mirror::Object>> h_dex_elements = |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame^] | 447 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Object>>(dex_elements)); |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 448 | if (h_class_loader.Get() != nullptr && |
| 449 | GetDexFilesFromClassLoader(soa, h_class_loader.Get(), &queue)) { |
| 450 | class_loader_ok = true; |
| 451 | |
| 452 | // In this case, also take into account the dex_elements array, if given. We don't need to |
| 453 | // read it otherwise, as we'll compare against all open oat files anyways. |
| 454 | GetDexFilesFromDexElementsArray(soa, h_dex_elements, &queue); |
| 455 | } else if (h_class_loader.Get() != nullptr) { |
| 456 | VLOG(class_linker) << "Something unsupported with " |
| 457 | << PrettyClass(h_class_loader->GetClass()); |
| 458 | } |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | // Dex files are registered late - once a class is actually being loaded. We have to compare |
| 462 | // against the open oat files. Take the oat_file_manager_lock_ that protects oat_files_ accesses. |
| 463 | ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
| 464 | |
Mathieu Chartier | 14d7b3e | 2016-06-09 16:18:04 -0700 | [diff] [blame] | 465 | // Vector that holds the newly opened dex files live, this is done to prevent leaks. |
| 466 | std::vector<std::unique_ptr<const DexFile>> opened_dex_files; |
| 467 | |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 468 | if (!class_loader_ok) { |
| 469 | // Add dex files from already loaded oat files, but skip boot. |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 470 | |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 471 | // Clean up the queue. |
| 472 | while (!queue.empty()) { |
| 473 | queue.pop(); |
| 474 | } |
| 475 | |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 476 | std::vector<const OatFile*> boot_oat_files = GetBootOatFiles(); |
| 477 | // The same OatFile can be loaded multiple times at different addresses. In this case, we don't |
| 478 | // need to check both against each other since they would have resolved the same way at compile |
| 479 | // time. |
| 480 | std::unordered_set<std::string> unique_locations; |
| 481 | for (const std::unique_ptr<const OatFile>& loaded_oat_file : oat_files_) { |
| 482 | DCHECK_NE(loaded_oat_file.get(), oat_file); |
| 483 | const std::string& location = loaded_oat_file->GetLocation(); |
| 484 | if (std::find(boot_oat_files.begin(), boot_oat_files.end(), loaded_oat_file.get()) == |
| 485 | boot_oat_files.end() && location != oat_file->GetLocation() && |
| 486 | unique_locations.find(location) == unique_locations.end()) { |
| 487 | unique_locations.insert(location); |
Mathieu Chartier | 14d7b3e | 2016-06-09 16:18:04 -0700 | [diff] [blame] | 488 | AddDexFilesFromOat(loaded_oat_file.get(), |
| 489 | /*already_loaded*/true, |
| 490 | &queue, |
| 491 | /*out*/&opened_dex_files); |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 492 | } |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 496 | // Exit if shared libraries are ok. Do a full duplicate classes check otherwise. |
| 497 | const std::string |
| 498 | shared_libraries(oat_file->GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey)); |
| 499 | if (AreSharedLibrariesOk(shared_libraries, queue)) { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 500 | return false; |
| 501 | } |
| 502 | |
Andreas Gampe | 1fdbe1b | 2016-06-10 08:36:20 -0700 | [diff] [blame] | 503 | ScopedTrace st("Collision check"); |
| 504 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 505 | // Add dex files from the oat file to check. |
Mathieu Chartier | 14d7b3e | 2016-06-09 16:18:04 -0700 | [diff] [blame] | 506 | AddDexFilesFromOat(oat_file, /*already_loaded*/false, &queue, &opened_dex_files); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 507 | |
| 508 | // Now drain the queue. |
| 509 | while (!queue.empty()) { |
| 510 | // Modifying the top element is only safe if we pop right after. |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 511 | DexFileAndClassPair compare_pop(queue.top()); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 512 | queue.pop(); |
| 513 | |
| 514 | // Compare against the following elements. |
| 515 | while (!queue.empty()) { |
Mathieu Chartier | 80b37b7 | 2015-10-12 18:13:39 -0700 | [diff] [blame] | 516 | DexFileAndClassPair top(queue.top()); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 517 | |
| 518 | if (strcmp(compare_pop.GetCachedDescriptor(), top.GetCachedDescriptor()) == 0) { |
| 519 | // Same descriptor. Check whether it's crossing old-oat-files to new-oat-files. |
| 520 | if (compare_pop.FromLoadedOat() != top.FromLoadedOat()) { |
| 521 | *error_msg = |
| 522 | StringPrintf("Found duplicated class when checking oat files: '%s' in %s and %s", |
| 523 | compare_pop.GetCachedDescriptor(), |
| 524 | compare_pop.GetDexFile()->GetLocation().c_str(), |
| 525 | top.GetDexFile()->GetLocation().c_str()); |
| 526 | return true; |
| 527 | } |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 528 | queue.pop(); |
Mathieu Chartier | 14d7b3e | 2016-06-09 16:18:04 -0700 | [diff] [blame] | 529 | AddNext(&top, &queue); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 530 | } else { |
| 531 | // Something else. Done here. |
| 532 | break; |
| 533 | } |
| 534 | } |
Mathieu Chartier | 14d7b3e | 2016-06-09 16:18:04 -0700 | [diff] [blame] | 535 | AddNext(&compare_pop, &queue); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | return false; |
| 539 | } |
| 540 | |
| 541 | std::vector<std::unique_ptr<const DexFile>> OatFileManager::OpenDexFilesFromOat( |
| 542 | const char* dex_location, |
| 543 | const char* oat_location, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 544 | jobject class_loader, |
| 545 | jobjectArray dex_elements, |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 546 | const OatFile** out_oat_file, |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 547 | std::vector<std::string>* error_msgs) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 548 | ScopedTrace trace(__FUNCTION__); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 549 | CHECK(dex_location != nullptr); |
| 550 | CHECK(error_msgs != nullptr); |
| 551 | |
| 552 | // Verify we aren't holding the mutator lock, which could starve GC if we |
| 553 | // have to generate or relocate an oat file. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 554 | Thread* const self = Thread::Current(); |
| 555 | Locks::mutator_lock_->AssertNotHeld(self); |
| 556 | Runtime* const runtime = Runtime::Current(); |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 557 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 558 | OatFileAssistant oat_file_assistant(dex_location, |
| 559 | oat_location, |
| 560 | kRuntimeISA, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 561 | !runtime->IsAotCompiler()); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 562 | |
| 563 | // Lock the target oat location to avoid races generating and loading the |
| 564 | // oat file. |
| 565 | std::string error_msg; |
| 566 | if (!oat_file_assistant.Lock(/*out*/&error_msg)) { |
| 567 | // Don't worry too much if this fails. If it does fail, it's unlikely we |
| 568 | // can generate an oat file anyway. |
| 569 | VLOG(class_linker) << "OatFileAssistant::Lock: " << error_msg; |
| 570 | } |
| 571 | |
| 572 | const OatFile* source_oat_file = nullptr; |
| 573 | |
Richard Uhler | 01be681 | 2016-05-17 10:34:52 -0700 | [diff] [blame] | 574 | if (!oat_file_assistant.IsUpToDate()) { |
| 575 | // Update the oat file on disk if we can, based on the --compiler-filter |
| 576 | // option derived from the current runtime options. |
| 577 | // This may fail, but that's okay. Best effort is all that matters here. |
Richard Uhler | d1472a2 | 2016-04-15 15:18:56 -0700 | [diff] [blame] | 578 | switch (oat_file_assistant.MakeUpToDate(/*profile_changed*/false, /*out*/ &error_msg)) { |
Richard Uhler | 01be681 | 2016-05-17 10:34:52 -0700 | [diff] [blame] | 579 | case OatFileAssistant::kUpdateFailed: |
| 580 | LOG(WARNING) << error_msg; |
| 581 | break; |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 582 | |
Richard Uhler | 01be681 | 2016-05-17 10:34:52 -0700 | [diff] [blame] | 583 | case OatFileAssistant::kUpdateNotAttempted: |
| 584 | // Avoid spamming the logs if we decided not to attempt making the oat |
| 585 | // file up to date. |
| 586 | VLOG(oat) << error_msg; |
| 587 | break; |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 588 | |
Richard Uhler | 01be681 | 2016-05-17 10:34:52 -0700 | [diff] [blame] | 589 | case OatFileAssistant::kUpdateSucceeded: |
| 590 | // Nothing to do. |
| 591 | break; |
| 592 | } |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | // Get the oat file on disk. |
| 596 | std::unique_ptr<const OatFile> oat_file(oat_file_assistant.GetBestOatFile().release()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 597 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 598 | if (oat_file != nullptr) { |
| 599 | // Take the file only if it has no collisions, or we must take it because of preopting. |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 600 | bool accept_oat_file = |
| 601 | !HasCollisions(oat_file.get(), class_loader, dex_elements, /*out*/ &error_msg); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 602 | if (!accept_oat_file) { |
| 603 | // Failed the collision check. Print warning. |
| 604 | if (Runtime::Current()->IsDexFileFallbackEnabled()) { |
| 605 | LOG(WARNING) << "Found duplicate classes, falling back to interpreter mode for " |
| 606 | << dex_location; |
| 607 | } else { |
| 608 | LOG(WARNING) << "Found duplicate classes, dex-file-fallback disabled, will be failing to " |
| 609 | " load classes for " << dex_location; |
| 610 | } |
| 611 | LOG(WARNING) << error_msg; |
| 612 | |
| 613 | // However, if the app was part of /system and preopted, there is no original dex file |
| 614 | // available. In that case grudgingly accept the oat file. |
Richard Uhler | 76f5cb6 | 2016-04-04 13:30:16 -0700 | [diff] [blame] | 615 | if (!oat_file_assistant.HasOriginalDexFiles()) { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 616 | accept_oat_file = true; |
| 617 | LOG(WARNING) << "Dex location " << dex_location << " does not seem to include dex file. " |
| 618 | << "Allow oat file use. This is potentially dangerous."; |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | if (accept_oat_file) { |
| 623 | VLOG(class_linker) << "Registering " << oat_file->GetLocation(); |
| 624 | source_oat_file = RegisterOatFile(std::move(oat_file)); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 625 | *out_oat_file = source_oat_file; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 626 | } |
| 627 | } |
| 628 | |
| 629 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 630 | |
| 631 | // Load the dex files from the oat file. |
| 632 | if (source_oat_file != nullptr) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 633 | bool added_image_space = false; |
| 634 | if (source_oat_file->IsExecutable()) { |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 635 | std::unique_ptr<gc::space::ImageSpace> image_space = |
| 636 | kEnableAppImage ? oat_file_assistant.OpenImageSpace(source_oat_file) : nullptr; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 637 | if (image_space != nullptr) { |
| 638 | ScopedObjectAccess soa(self); |
| 639 | StackHandleScope<1> hs(self); |
| 640 | Handle<mirror::ClassLoader> h_loader( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame^] | 641 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader))); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 642 | // Can not load app image without class loader. |
| 643 | if (h_loader.Get() != nullptr) { |
| 644 | std::string temp_error_msg; |
| 645 | // Add image space has a race condition since other threads could be reading from the |
| 646 | // spaces array. |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 647 | { |
| 648 | ScopedThreadSuspension sts(self, kSuspended); |
Mathieu Chartier | 61d2b2d | 2016-02-04 13:31:46 -0800 | [diff] [blame] | 649 | gc::ScopedGCCriticalSection gcs(self, |
| 650 | gc::kGcCauseAddRemoveAppImageSpace, |
| 651 | gc::kCollectorTypeAddRemoveAppImageSpace); |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 652 | ScopedSuspendAll ssa("Add image space"); |
| 653 | runtime->GetHeap()->AddSpace(image_space.get()); |
| 654 | } |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 655 | { |
| 656 | ScopedTrace trace2(StringPrintf("Adding image space for location %s", dex_location)); |
| 657 | added_image_space = runtime->GetClassLinker()->AddImageSpace(image_space.get(), |
| 658 | h_loader, |
| 659 | dex_elements, |
| 660 | dex_location, |
| 661 | /*out*/&dex_files, |
| 662 | /*out*/&temp_error_msg); |
| 663 | } |
Mathieu Chartier | a6e81ed | 2016-02-25 13:52:10 -0800 | [diff] [blame] | 664 | if (added_image_space) { |
Mathieu Chartier | bd064ea | 2016-02-11 16:27:18 -0800 | [diff] [blame] | 665 | // Successfully added image space to heap, release the map so that it does not get |
| 666 | // freed. |
| 667 | image_space.release(); |
| 668 | } else { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 669 | LOG(INFO) << "Failed to add image file " << temp_error_msg; |
| 670 | dex_files.clear(); |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 671 | { |
| 672 | ScopedThreadSuspension sts(self, kSuspended); |
Mathieu Chartier | 61d2b2d | 2016-02-04 13:31:46 -0800 | [diff] [blame] | 673 | gc::ScopedGCCriticalSection gcs(self, |
| 674 | gc::kGcCauseAddRemoveAppImageSpace, |
| 675 | gc::kCollectorTypeAddRemoveAppImageSpace); |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 676 | ScopedSuspendAll ssa("Remove image space"); |
| 677 | runtime->GetHeap()->RemoveSpace(image_space.get()); |
| 678 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 679 | // Non-fatal, don't update error_msg. |
| 680 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 681 | } |
| 682 | } |
| 683 | } |
| 684 | if (!added_image_space) { |
| 685 | DCHECK(dex_files.empty()); |
| 686 | dex_files = oat_file_assistant.LoadDexFiles(*source_oat_file, dex_location); |
| 687 | } |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 688 | if (dex_files.empty()) { |
| 689 | error_msgs->push_back("Failed to open dex files from " + source_oat_file->GetLocation()); |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | // Fall back to running out of the original dex file if we couldn't load any |
| 694 | // dex_files from the oat file. |
| 695 | if (dex_files.empty()) { |
| 696 | if (oat_file_assistant.HasOriginalDexFiles()) { |
| 697 | if (Runtime::Current()->IsDexFileFallbackEnabled()) { |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 698 | static constexpr bool kVerifyChecksum = true; |
| 699 | if (!DexFile::Open( |
| 700 | dex_location, dex_location, kVerifyChecksum, /*out*/ &error_msg, &dex_files)) { |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 701 | LOG(WARNING) << error_msg; |
Alex Light | 3045b66 | 2016-04-20 14:26:34 -0700 | [diff] [blame] | 702 | error_msgs->push_back("Failed to open dex files from " + std::string(dex_location) |
| 703 | + " because: " + error_msg); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 704 | } |
| 705 | } else { |
| 706 | error_msgs->push_back("Fallback mode disabled, skipping dex files."); |
| 707 | } |
| 708 | } else { |
| 709 | error_msgs->push_back("No original dex files found for dex location " |
| 710 | + std::string(dex_location)); |
| 711 | } |
| 712 | } |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 713 | |
| 714 | // TODO(calin): Consider optimizing this knowing that is useless to record the |
| 715 | // use of fully compiled apks. |
| 716 | Runtime::Current()->NotifyDexLoaded(dex_location); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 717 | return dex_files; |
| 718 | } |
| 719 | |
Nicolas Geoffray | 04680f3 | 2016-03-17 11:56:54 +0000 | [diff] [blame] | 720 | void OatFileManager::DumpForSigQuit(std::ostream& os) { |
| 721 | ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_); |
| 722 | std::vector<const OatFile*> boot_oat_files = GetBootOatFiles(); |
| 723 | for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) { |
| 724 | if (ContainsElement(boot_oat_files, oat_file.get())) { |
| 725 | continue; |
| 726 | } |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 727 | os << oat_file->GetLocation() << ": " << oat_file->GetCompilerFilter() << "\n"; |
Nicolas Geoffray | 04680f3 | 2016-03-17 11:56:54 +0000 | [diff] [blame] | 728 | } |
| 729 | } |
| 730 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 731 | } // namespace art |