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