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