1 /* 2 * Copyright (C) 2017 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 "class_loader_context.h" 18 19 #include "art_field-inl.h" 20 #include "base/dchecked_vector.h" 21 #include "base/stl_util.h" 22 #include "class_linker.h" 23 #include "class_loader_utils.h" 24 #include "dex/art_dex_file_loader.h" 25 #include "dex/dex_file.h" 26 #include "dex/dex_file_loader.h" 27 #include "handle_scope-inl.h" 28 #include "jni_internal.h" 29 #include "oat_file_assistant.h" 30 #include "obj_ptr-inl.h" 31 #include "runtime.h" 32 #include "scoped_thread_state_change-inl.h" 33 #include "thread.h" 34 #include "well_known_classes.h" 35 36 namespace art { 37 38 static constexpr char kPathClassLoaderString[] = "PCL"; 39 static constexpr char kDelegateLastClassLoaderString[] = "DLC"; 40 static constexpr char kClassLoaderOpeningMark = '['; 41 static constexpr char kClassLoaderClosingMark = ']'; 42 static constexpr char kClassLoaderSeparator = ';'; 43 static constexpr char kClasspathSeparator = ':'; 44 static constexpr char kDexFileChecksumSeparator = '*'; 45 46 ClassLoaderContext::ClassLoaderContext() 47 : special_shared_library_(false), 48 dex_files_open_attempted_(false), 49 dex_files_open_result_(false), 50 owns_the_dex_files_(true) {} 51 52 ClassLoaderContext::ClassLoaderContext(bool owns_the_dex_files) 53 : special_shared_library_(false), 54 dex_files_open_attempted_(true), 55 dex_files_open_result_(true), 56 owns_the_dex_files_(owns_the_dex_files) {} 57 58 ClassLoaderContext::~ClassLoaderContext() { 59 if (!owns_the_dex_files_) { 60 // If the context does not own the dex/oat files release the unique pointers to 61 // make sure we do not de-allocate them. 62 for (ClassLoaderInfo& info : class_loader_chain_) { 63 for (std::unique_ptr<OatFile>& oat_file : info.opened_oat_files) { 64 oat_file.release(); 65 } 66 for (std::unique_ptr<const DexFile>& dex_file : info.opened_dex_files) { 67 dex_file.release(); 68 } 69 } 70 } 71 } 72 73 std::unique_ptr<ClassLoaderContext> ClassLoaderContext::Default() { 74 return Create(""); 75 } 76 77 std::unique_ptr<ClassLoaderContext> ClassLoaderContext::Create(const std::string& spec) { 78 std::unique_ptr<ClassLoaderContext> result(new ClassLoaderContext()); 79 if (result->Parse(spec)) { 80 return result; 81 } else { 82 return nullptr; 83 } 84 } 85 86 // The expected format is: "ClassLoaderType1[ClasspathElem1*Checksum1:ClasspathElem2*Checksum2...]". 87 // The checksum part of the format is expected only if parse_cheksums is true. 88 bool ClassLoaderContext::ParseClassLoaderSpec(const std::string& class_loader_spec, 89 ClassLoaderType class_loader_type, 90 bool parse_checksums) { 91 const char* class_loader_type_str = GetClassLoaderTypeName(class_loader_type); 92 size_t type_str_size = strlen(class_loader_type_str); 93 94 CHECK_EQ(0, class_loader_spec.compare(0, type_str_size, class_loader_type_str)); 95 96 // Check the opening and closing markers. 97 if (class_loader_spec[type_str_size] != kClassLoaderOpeningMark) { 98 return false; 99 } 100 if (class_loader_spec[class_loader_spec.length() - 1] != kClassLoaderClosingMark) { 101 return false; 102 } 103 104 // At this point we know the format is ok; continue and extract the classpath. 105 // Note that class loaders with an empty class path are allowed. 106 std::string classpath = class_loader_spec.substr(type_str_size + 1, 107 class_loader_spec.length() - type_str_size - 2); 108 109 class_loader_chain_.push_back(ClassLoaderInfo(class_loader_type)); 110 111 if (!parse_checksums) { 112 Split(classpath, kClasspathSeparator, &class_loader_chain_.back().classpath); 113 } else { 114 std::vector<std::string> classpath_elements; 115 Split(classpath, kClasspathSeparator, &classpath_elements); 116 for (const std::string& element : classpath_elements) { 117 std::vector<std::string> dex_file_with_checksum; 118 Split(element, kDexFileChecksumSeparator, &dex_file_with_checksum); 119 if (dex_file_with_checksum.size() != 2) { 120 return false; 121 } 122 uint32_t checksum = 0; 123 if (!ParseInt(dex_file_with_checksum[1].c_str(), &checksum)) { 124 return false; 125 } 126 class_loader_chain_.back().classpath.push_back(dex_file_with_checksum[0]); 127 class_loader_chain_.back().checksums.push_back(checksum); 128 } 129 } 130 131 return true; 132 } 133 134 // Extracts the class loader type from the given spec. 135 // Return ClassLoaderContext::kInvalidClassLoader if the class loader type is not 136 // recognized. 137 ClassLoaderContext::ClassLoaderType 138 ClassLoaderContext::ExtractClassLoaderType(const std::string& class_loader_spec) { 139 const ClassLoaderType kValidTypes[] = {kPathClassLoader, kDelegateLastClassLoader}; 140 for (const ClassLoaderType& type : kValidTypes) { 141 const char* type_str = GetClassLoaderTypeName(type); 142 if (class_loader_spec.compare(0, strlen(type_str), type_str) == 0) { 143 return type; 144 } 145 } 146 return kInvalidClassLoader; 147 } 148 149 // The format: ClassLoaderType1[ClasspathElem1:ClasspathElem2...];ClassLoaderType2[...]... 150 // ClassLoaderType is either "PCL" (PathClassLoader) or "DLC" (DelegateLastClassLoader). 151 // ClasspathElem is the path of dex/jar/apk file. 152 bool ClassLoaderContext::Parse(const std::string& spec, bool parse_checksums) { 153 if (spec.empty()) { 154 // By default we load the dex files in a PathClassLoader. 155 // So an empty spec is equivalent to an empty PathClassLoader (this happens when running 156 // tests) 157 class_loader_chain_.push_back(ClassLoaderInfo(kPathClassLoader)); 158 return true; 159 } 160 161 // Stop early if we detect the special shared library, which may be passed as the classpath 162 // for dex2oat when we want to skip the shared libraries check. 163 if (spec == OatFile::kSpecialSharedLibrary) { 164 LOG(INFO) << "The ClassLoaderContext is a special shared library."; 165 special_shared_library_ = true; 166 return true; 167 } 168 169 std::vector<std::string> class_loaders; 170 Split(spec, kClassLoaderSeparator, &class_loaders); 171 172 for (const std::string& class_loader : class_loaders) { 173 ClassLoaderType type = ExtractClassLoaderType(class_loader); 174 if (type == kInvalidClassLoader) { 175 LOG(ERROR) << "Invalid class loader type: " << class_loader; 176 return false; 177 } 178 if (!ParseClassLoaderSpec(class_loader, type, parse_checksums)) { 179 LOG(ERROR) << "Invalid class loader spec: " << class_loader; 180 return false; 181 } 182 } 183 return true; 184 } 185 186 // Opens requested class path files and appends them to opened_dex_files. If the dex files have 187 // been stripped, this opens them from their oat files (which get added to opened_oat_files). 188 bool ClassLoaderContext::OpenDexFiles(InstructionSet isa, const std::string& classpath_dir) { 189 if (dex_files_open_attempted_) { 190 // Do not attempt to re-open the files if we already tried. 191 return dex_files_open_result_; 192 } 193 194 dex_files_open_attempted_ = true; 195 // Assume we can open all dex files. If not, we will set this to false as we go. 196 dex_files_open_result_ = true; 197 198 if (special_shared_library_) { 199 // Nothing to open if the context is a special shared library. 200 return true; 201 } 202 203 // Note that we try to open all dex files even if some fail. 204 // We may get resource-only apks which we cannot load. 205 // TODO(calin): Refine the dex opening interface to be able to tell if an archive contains 206 // no dex files. So that we can distinguish the real failures... 207 const ArtDexFileLoader dex_file_loader; 208 for (ClassLoaderInfo& info : class_loader_chain_) { 209 size_t opened_dex_files_index = info.opened_dex_files.size(); 210 for (const std::string& cp_elem : info.classpath) { 211 // If path is relative, append it to the provided base directory. 212 std::string location = cp_elem; 213 if (location[0] != '/' && !classpath_dir.empty()) { 214 location = classpath_dir + (classpath_dir.back() == '/' ? "" : "/") + location; 215 } 216 217 std::string error_msg; 218 // When opening the dex files from the context we expect their checksum to match their 219 // contents. So pass true to verify_checksum. 220 if (!dex_file_loader.Open(location.c_str(), 221 location.c_str(), 222 Runtime::Current()->IsVerificationEnabled(), 223 /*verify_checksum*/ true, 224 &error_msg, 225 &info.opened_dex_files)) { 226 // If we fail to open the dex file because it's been stripped, try to open the dex file 227 // from its corresponding oat file. 228 // This could happen when we need to recompile a pre-build whose dex code has been stripped. 229 // (for example, if the pre-build is only quicken and we want to re-compile it 230 // speed-profile). 231 // TODO(calin): Use the vdex directly instead of going through the oat file. 232 OatFileAssistant oat_file_assistant(location.c_str(), isa, false); 233 std::unique_ptr<OatFile> oat_file(oat_file_assistant.GetBestOatFile()); 234 std::vector<std::unique_ptr<const DexFile>> oat_dex_files; 235 if (oat_file != nullptr && 236 OatFileAssistant::LoadDexFiles(*oat_file, location, &oat_dex_files)) { 237 info.opened_oat_files.push_back(std::move(oat_file)); 238 info.opened_dex_files.insert(info.opened_dex_files.end(), 239 std::make_move_iterator(oat_dex_files.begin()), 240 std::make_move_iterator(oat_dex_files.end())); 241 } else { 242 LOG(WARNING) << "Could not open dex files from location: " << location; 243 dex_files_open_result_ = false; 244 } 245 } 246 } 247 248 // We finished opening the dex files from the classpath. 249 // Now update the classpath and the checksum with the locations of the dex files. 250 // 251 // We do this because initially the classpath contains the paths of the dex files; and 252 // some of them might be multi-dexes. So in order to have a consistent view we replace all the 253 // file paths with the actual dex locations being loaded. 254 // This will allow the context to VerifyClassLoaderContextMatch which expects or multidex 255 // location in the class paths. 256 // Note that this will also remove the paths that could not be opened. 257 info.original_classpath = std::move(info.classpath); 258 info.classpath.clear(); 259 info.checksums.clear(); 260 for (size_t k = opened_dex_files_index; k < info.opened_dex_files.size(); k++) { 261 std::unique_ptr<const DexFile>& dex = info.opened_dex_files[k]; 262 info.classpath.push_back(dex->GetLocation()); 263 info.checksums.push_back(dex->GetLocationChecksum()); 264 } 265 } 266 267 return dex_files_open_result_; 268 } 269 270 bool ClassLoaderContext::RemoveLocationsFromClassPaths( 271 const dchecked_vector<std::string>& locations) { 272 CHECK(!dex_files_open_attempted_) 273 << "RemoveLocationsFromClasspaths cannot be call after OpenDexFiles"; 274 275 std::set<std::string> canonical_locations; 276 for (const std::string& location : locations) { 277 canonical_locations.insert(DexFileLoader::GetDexCanonicalLocation(location.c_str())); 278 } 279 bool removed_locations = false; 280 for (ClassLoaderInfo& info : class_loader_chain_) { 281 size_t initial_size = info.classpath.size(); 282 auto kept_it = std::remove_if( 283 info.classpath.begin(), 284 info.classpath.end(), 285 [canonical_locations](const std::string& location) { 286 return ContainsElement(canonical_locations, 287 DexFileLoader::GetDexCanonicalLocation(location.c_str())); 288 }); 289 info.classpath.erase(kept_it, info.classpath.end()); 290 if (initial_size != info.classpath.size()) { 291 removed_locations = true; 292 } 293 } 294 return removed_locations; 295 } 296 297 std::string ClassLoaderContext::EncodeContextForDex2oat(const std::string& base_dir) const { 298 return EncodeContext(base_dir, /*for_dex2oat*/ true, /*stored_context*/ nullptr); 299 } 300 301 std::string ClassLoaderContext::EncodeContextForOatFile(const std::string& base_dir, 302 ClassLoaderContext* stored_context) const { 303 return EncodeContext(base_dir, /*for_dex2oat*/ false, stored_context); 304 } 305 306 std::string ClassLoaderContext::EncodeContext(const std::string& base_dir, 307 bool for_dex2oat, 308 ClassLoaderContext* stored_context) const { 309 CheckDexFilesOpened("EncodeContextForOatFile"); 310 if (special_shared_library_) { 311 return OatFile::kSpecialSharedLibrary; 312 } 313 314 if (stored_context != nullptr) { 315 DCHECK_EQ(class_loader_chain_.size(), stored_context->class_loader_chain_.size()); 316 } 317 318 std::ostringstream out; 319 if (class_loader_chain_.empty()) { 320 // We can get in this situation if the context was created with a class path containing the 321 // source dex files which were later removed (happens during run-tests). 322 out << GetClassLoaderTypeName(kPathClassLoader) 323 << kClassLoaderOpeningMark 324 << kClassLoaderClosingMark; 325 return out.str(); 326 } 327 328 for (size_t i = 0; i < class_loader_chain_.size(); i++) { 329 const ClassLoaderInfo& info = class_loader_chain_[i]; 330 if (i > 0) { 331 out << kClassLoaderSeparator; 332 } 333 out << GetClassLoaderTypeName(info.type); 334 out << kClassLoaderOpeningMark; 335 std::set<std::string> seen_locations; 336 SafeMap<std::string, std::string> remap; 337 if (stored_context != nullptr) { 338 DCHECK_EQ(info.original_classpath.size(), 339 stored_context->class_loader_chain_[i].classpath.size()); 340 for (size_t k = 0; k < info.original_classpath.size(); ++k) { 341 // Note that we don't care if the same name appears twice. 342 remap.Put(info.original_classpath[k], stored_context->class_loader_chain_[i].classpath[k]); 343 } 344 } 345 for (size_t k = 0; k < info.opened_dex_files.size(); k++) { 346 const std::unique_ptr<const DexFile>& dex_file = info.opened_dex_files[k]; 347 if (for_dex2oat) { 348 // dex2oat only needs the base location. It cannot accept multidex locations. 349 // So ensure we only add each file once. 350 bool new_insert = seen_locations.insert( 351 DexFileLoader::GetBaseLocation(dex_file->GetLocation())).second; 352 if (!new_insert) { 353 continue; 354 } 355 } 356 std::string location = dex_file->GetLocation(); 357 // If there is a stored class loader remap, fix up the multidex strings. 358 if (!remap.empty()) { 359 std::string base_dex_location = DexFileLoader::GetBaseLocation(location); 360 auto it = remap.find(base_dex_location); 361 CHECK(it != remap.end()) << base_dex_location; 362 location = it->second + DexFileLoader::GetMultiDexSuffix(location); 363 } 364 if (k > 0) { 365 out << kClasspathSeparator; 366 } 367 // Find paths that were relative and convert them back from absolute. 368 if (!base_dir.empty() && location.substr(0, base_dir.length()) == base_dir) { 369 out << location.substr(base_dir.length() + 1).c_str(); 370 } else { 371 out << location.c_str(); 372 } 373 // dex2oat does not need the checksums. 374 if (!for_dex2oat) { 375 out << kDexFileChecksumSeparator; 376 out << dex_file->GetLocationChecksum(); 377 } 378 } 379 out << kClassLoaderClosingMark; 380 } 381 return out.str(); 382 } 383 384 jobject ClassLoaderContext::CreateClassLoader( 385 const std::vector<const DexFile*>& compilation_sources) const { 386 CheckDexFilesOpened("CreateClassLoader"); 387 388 Thread* self = Thread::Current(); 389 ScopedObjectAccess soa(self); 390 391 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); 392 393 if (class_loader_chain_.empty()) { 394 return class_linker->CreatePathClassLoader(self, compilation_sources); 395 } 396 397 // Create the class loaders starting from the top most parent (the one on the last position 398 // in the chain) but omit the first class loader which will contain the compilation_sources and 399 // needs special handling. 400 jobject current_parent = nullptr; // the starting parent is the BootClassLoader. 401 for (size_t i = class_loader_chain_.size() - 1; i > 0; i--) { 402 std::vector<const DexFile*> class_path_files = MakeNonOwningPointerVector( 403 class_loader_chain_[i].opened_dex_files); 404 current_parent = class_linker->CreateWellKnownClassLoader( 405 self, 406 class_path_files, 407 GetClassLoaderClass(class_loader_chain_[i].type), 408 current_parent); 409 } 410 411 // We set up all the parents. Move on to create the first class loader. 412 // Its classpath comes first, followed by compilation sources. This ensures that whenever 413 // we need to resolve classes from it the classpath elements come first. 414 415 std::vector<const DexFile*> first_class_loader_classpath = MakeNonOwningPointerVector( 416 class_loader_chain_[0].opened_dex_files); 417 first_class_loader_classpath.insert(first_class_loader_classpath.end(), 418 compilation_sources.begin(), 419 compilation_sources.end()); 420 421 return class_linker->CreateWellKnownClassLoader( 422 self, 423 first_class_loader_classpath, 424 GetClassLoaderClass(class_loader_chain_[0].type), 425 current_parent); 426 } 427 428 std::vector<const DexFile*> ClassLoaderContext::FlattenOpenedDexFiles() const { 429 CheckDexFilesOpened("FlattenOpenedDexFiles"); 430 431 std::vector<const DexFile*> result; 432 for (const ClassLoaderInfo& info : class_loader_chain_) { 433 for (const std::unique_ptr<const DexFile>& dex_file : info.opened_dex_files) { 434 result.push_back(dex_file.get()); 435 } 436 } 437 return result; 438 } 439 440 const char* ClassLoaderContext::GetClassLoaderTypeName(ClassLoaderType type) { 441 switch (type) { 442 case kPathClassLoader: return kPathClassLoaderString; 443 case kDelegateLastClassLoader: return kDelegateLastClassLoaderString; 444 default: 445 LOG(FATAL) << "Invalid class loader type " << type; 446 UNREACHABLE(); 447 } 448 } 449 450 void ClassLoaderContext::CheckDexFilesOpened(const std::string& calling_method) const { 451 CHECK(dex_files_open_attempted_) 452 << "Dex files were not successfully opened before the call to " << calling_method 453 << "attempt=" << dex_files_open_attempted_ << ", result=" << dex_files_open_result_; 454 } 455 456 // Collects the dex files from the give Java dex_file object. Only the dex files with 457 // at least 1 class are collected. If a null java_dex_file is passed this method does nothing. 458 static bool CollectDexFilesFromJavaDexFile(ObjPtr<mirror::Object> java_dex_file, 459 ArtField* const cookie_field, 460 std::vector<const DexFile*>* out_dex_files) 461 REQUIRES_SHARED(Locks::mutator_lock_) { 462 if (java_dex_file == nullptr) { 463 return true; 464 } 465 // On the Java side, the dex files are stored in the cookie field. 466 mirror::LongArray* long_array = cookie_field->GetObject(java_dex_file)->AsLongArray(); 467 if (long_array == nullptr) { 468 // This should never happen so log a warning. 469 LOG(ERROR) << "Unexpected null cookie"; 470 return false; 471 } 472 int32_t long_array_size = long_array->GetLength(); 473 // Index 0 from the long array stores the oat file. The dex files start at index 1. 474 for (int32_t j = 1; j < long_array_size; ++j) { 475 const DexFile* cp_dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>( 476 long_array->GetWithoutChecks(j))); 477 if (cp_dex_file != nullptr && cp_dex_file->NumClassDefs() > 0) { 478 // TODO(calin): It's unclear why the dex files with no classes are skipped here and when 479 // cp_dex_file can be null. 480 out_dex_files->push_back(cp_dex_file); 481 } 482 } 483 return true; 484 } 485 486 // Collects all the dex files loaded by the given class loader. 487 // Returns true for success or false if an unexpected state is discovered (e.g. a null dex cookie, 488 // a null list of dex elements or a null dex element). 489 static bool CollectDexFilesFromSupportedClassLoader(ScopedObjectAccessAlreadyRunnable& soa, 490 Handle<mirror::ClassLoader> class_loader, 491 std::vector<const DexFile*>* out_dex_files) 492 REQUIRES_SHARED(Locks::mutator_lock_) { 493 CHECK(IsPathOrDexClassLoader(soa, class_loader) || IsDelegateLastClassLoader(soa, class_loader)); 494 495 // All supported class loaders inherit from BaseDexClassLoader. 496 // We need to get the DexPathList and loop through it. 497 ArtField* const cookie_field = 498 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_cookie); 499 ArtField* const dex_file_field = 500 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); 501 ObjPtr<mirror::Object> dex_path_list = 502 jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList)-> 503 GetObject(class_loader.Get()); 504 CHECK(cookie_field != nullptr); 505 CHECK(dex_file_field != nullptr); 506 if (dex_path_list == nullptr) { 507 // This may be null if the current class loader is under construction and it does not 508 // have its fields setup yet. 509 return true; 510 } 511 // DexPathList has an array dexElements of Elements[] which each contain a dex file. 512 ObjPtr<mirror::Object> dex_elements_obj = 513 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList_dexElements)-> 514 GetObject(dex_path_list); 515 // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look 516 // at the mCookie which is a DexFile vector. 517 if (dex_elements_obj == nullptr) { 518 // TODO(calin): It's unclear if we should just assert here. For now be prepared for the worse 519 // and assume we have no elements. 520 return true; 521 } else { 522 StackHandleScope<1> hs(soa.Self()); 523 Handle<mirror::ObjectArray<mirror::Object>> dex_elements( 524 hs.NewHandle(dex_elements_obj->AsObjectArray<mirror::Object>())); 525 for (int32_t i = 0; i < dex_elements->GetLength(); ++i) { 526 mirror::Object* element = dex_elements->GetWithoutChecks(i); 527 if (element == nullptr) { 528 // Should never happen, log an error and break. 529 // TODO(calin): It's unclear if we should just assert here. 530 // This code was propagated to oat_file_manager from the class linker where it would 531 // throw a NPE. For now, return false which will mark this class loader as unsupported. 532 LOG(ERROR) << "Unexpected null in the dex element list"; 533 return false; 534 } 535 ObjPtr<mirror::Object> dex_file = dex_file_field->GetObject(element); 536 if (!CollectDexFilesFromJavaDexFile(dex_file, cookie_field, out_dex_files)) { 537 return false; 538 } 539 } 540 } 541 542 return true; 543 } 544 545 static bool GetDexFilesFromDexElementsArray( 546 ScopedObjectAccessAlreadyRunnable& soa, 547 Handle<mirror::ObjectArray<mirror::Object>> dex_elements, 548 std::vector<const DexFile*>* out_dex_files) REQUIRES_SHARED(Locks::mutator_lock_) { 549 DCHECK(dex_elements != nullptr); 550 551 ArtField* const cookie_field = 552 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_cookie); 553 ArtField* const dex_file_field = 554 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); 555 ObjPtr<mirror::Class> const element_class = soa.Decode<mirror::Class>( 556 WellKnownClasses::dalvik_system_DexPathList__Element); 557 ObjPtr<mirror::Class> const dexfile_class = soa.Decode<mirror::Class>( 558 WellKnownClasses::dalvik_system_DexFile); 559 560 for (int32_t i = 0; i < dex_elements->GetLength(); ++i) { 561 mirror::Object* element = dex_elements->GetWithoutChecks(i); 562 // We can hit a null element here because this is invoked with a partially filled dex_elements 563 // array from DexPathList. DexPathList will open each dex sequentially, each time passing the 564 // list of dex files which were opened before. 565 if (element == nullptr) { 566 continue; 567 } 568 569 // We support this being dalvik.system.DexPathList$Element and dalvik.system.DexFile. 570 // TODO(calin): Code caried over oat_file_manager: supporting both classes seem to be 571 // a historical glitch. All the java code opens dex files using an array of Elements. 572 ObjPtr<mirror::Object> dex_file; 573 if (element_class == element->GetClass()) { 574 dex_file = dex_file_field->GetObject(element); 575 } else if (dexfile_class == element->GetClass()) { 576 dex_file = element; 577 } else { 578 LOG(ERROR) << "Unsupported element in dex_elements: " 579 << mirror::Class::PrettyClass(element->GetClass()); 580 return false; 581 } 582 583 if (!CollectDexFilesFromJavaDexFile(dex_file, cookie_field, out_dex_files)) { 584 return false; 585 } 586 } 587 return true; 588 } 589 590 // Adds the `class_loader` info to the `context`. 591 // The dex file present in `dex_elements` array (if not null) will be added at the end of 592 // the classpath. 593 // This method is recursive (w.r.t. the class loader parent) and will stop once it reaches the 594 // BootClassLoader. Note that the class loader chain is expected to be short. 595 bool ClassLoaderContext::AddInfoToContextFromClassLoader( 596 ScopedObjectAccessAlreadyRunnable& soa, 597 Handle<mirror::ClassLoader> class_loader, 598 Handle<mirror::ObjectArray<mirror::Object>> dex_elements) 599 REQUIRES_SHARED(Locks::mutator_lock_) { 600 if (ClassLinker::IsBootClassLoader(soa, class_loader.Get())) { 601 // Nothing to do for the boot class loader as we don't add its dex files to the context. 602 return true; 603 } 604 605 ClassLoaderContext::ClassLoaderType type; 606 if (IsPathOrDexClassLoader(soa, class_loader)) { 607 type = kPathClassLoader; 608 } else if (IsDelegateLastClassLoader(soa, class_loader)) { 609 type = kDelegateLastClassLoader; 610 } else { 611 LOG(WARNING) << "Unsupported class loader"; 612 return false; 613 } 614 615 // Inspect the class loader for its dex files. 616 std::vector<const DexFile*> dex_files_loaded; 617 CollectDexFilesFromSupportedClassLoader(soa, class_loader, &dex_files_loaded); 618 619 // If we have a dex_elements array extract its dex elements now. 620 // This is used in two situations: 621 // 1) when a new ClassLoader is created DexPathList will open each dex file sequentially 622 // passing the list of already open dex files each time. This ensures that we see the 623 // correct context even if the ClassLoader under construction is not fully build. 624 // 2) when apk splits are loaded on the fly, the framework will load their dex files by 625 // appending them to the current class loader. When the new code paths are loaded in 626 // BaseDexClassLoader, the paths already present in the class loader will be passed 627 // in the dex_elements array. 628 if (dex_elements != nullptr) { 629 GetDexFilesFromDexElementsArray(soa, dex_elements, &dex_files_loaded); 630 } 631 632 class_loader_chain_.push_back(ClassLoaderContext::ClassLoaderInfo(type)); 633 ClassLoaderInfo& info = class_loader_chain_.back(); 634 for (const DexFile* dex_file : dex_files_loaded) { 635 info.classpath.push_back(dex_file->GetLocation()); 636 info.checksums.push_back(dex_file->GetLocationChecksum()); 637 info.opened_dex_files.emplace_back(dex_file); 638 } 639 640 // We created the ClassLoaderInfo for the current loader. Move on to its parent. 641 642 StackHandleScope<1> hs(Thread::Current()); 643 Handle<mirror::ClassLoader> parent = hs.NewHandle(class_loader->GetParent()); 644 645 // Note that dex_elements array is null here. The elements are considered to be part of the 646 // current class loader and are not passed to the parents. 647 ScopedNullHandle<mirror::ObjectArray<mirror::Object>> null_dex_elements; 648 return AddInfoToContextFromClassLoader(soa, parent, null_dex_elements); 649 } 650 651 std::unique_ptr<ClassLoaderContext> ClassLoaderContext::CreateContextForClassLoader( 652 jobject class_loader, 653 jobjectArray dex_elements) { 654 CHECK(class_loader != nullptr); 655 656 ScopedObjectAccess soa(Thread::Current()); 657 StackHandleScope<2> hs(soa.Self()); 658 Handle<mirror::ClassLoader> h_class_loader = 659 hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader)); 660 Handle<mirror::ObjectArray<mirror::Object>> h_dex_elements = 661 hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Object>>(dex_elements)); 662 663 std::unique_ptr<ClassLoaderContext> result(new ClassLoaderContext(/*owns_the_dex_files*/ false)); 664 if (result->AddInfoToContextFromClassLoader(soa, h_class_loader, h_dex_elements)) { 665 return result; 666 } else { 667 return nullptr; 668 } 669 } 670 671 static bool IsAbsoluteLocation(const std::string& location) { 672 return !location.empty() && location[0] == '/'; 673 } 674 675 bool ClassLoaderContext::VerifyClassLoaderContextMatch(const std::string& context_spec, 676 bool verify_names, 677 bool verify_checksums) const { 678 if (verify_names || verify_checksums) { 679 DCHECK(dex_files_open_attempted_); 680 DCHECK(dex_files_open_result_); 681 } 682 683 ClassLoaderContext expected_context; 684 if (!expected_context.Parse(context_spec, verify_checksums)) { 685 LOG(WARNING) << "Invalid class loader context: " << context_spec; 686 return false; 687 } 688 689 // Special shared library contexts always match. They essentially instruct the runtime 690 // to ignore the class path check because the oat file is known to be loaded in different 691 // contexts. OatFileManager will further verify if the oat file can be loaded based on the 692 // collision check. 693 if (special_shared_library_ || expected_context.special_shared_library_) { 694 return true; 695 } 696 697 if (expected_context.class_loader_chain_.size() != class_loader_chain_.size()) { 698 LOG(WARNING) << "ClassLoaderContext size mismatch. expected=" 699 << expected_context.class_loader_chain_.size() 700 << ", actual=" << class_loader_chain_.size() 701 << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")"; 702 return false; 703 } 704 705 for (size_t i = 0; i < class_loader_chain_.size(); i++) { 706 const ClassLoaderInfo& info = class_loader_chain_[i]; 707 const ClassLoaderInfo& expected_info = expected_context.class_loader_chain_[i]; 708 if (info.type != expected_info.type) { 709 LOG(WARNING) << "ClassLoaderContext type mismatch for position " << i 710 << ". expected=" << GetClassLoaderTypeName(expected_info.type) 711 << ", found=" << GetClassLoaderTypeName(info.type) 712 << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")"; 713 return false; 714 } 715 if (info.classpath.size() != expected_info.classpath.size()) { 716 LOG(WARNING) << "ClassLoaderContext classpath size mismatch for position " << i 717 << ". expected=" << expected_info.classpath.size() 718 << ", found=" << info.classpath.size() 719 << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")"; 720 return false; 721 } 722 723 if (verify_checksums) { 724 DCHECK_EQ(info.classpath.size(), info.checksums.size()); 725 DCHECK_EQ(expected_info.classpath.size(), expected_info.checksums.size()); 726 } 727 728 if (!verify_names) { 729 continue; 730 } 731 732 for (size_t k = 0; k < info.classpath.size(); k++) { 733 // Compute the dex location that must be compared. 734 // We shouldn't do a naive comparison `info.classpath[k] == expected_info.classpath[k]` 735 // because even if they refer to the same file, one could be encoded as a relative location 736 // and the other as an absolute one. 737 bool is_dex_name_absolute = IsAbsoluteLocation(info.classpath[k]); 738 bool is_expected_dex_name_absolute = IsAbsoluteLocation(expected_info.classpath[k]); 739 std::string dex_name; 740 std::string expected_dex_name; 741 742 if (is_dex_name_absolute == is_expected_dex_name_absolute) { 743 // If both locations are absolute or relative then compare them as they are. 744 // This is usually the case for: shared libraries and secondary dex files. 745 dex_name = info.classpath[k]; 746 expected_dex_name = expected_info.classpath[k]; 747 } else if (is_dex_name_absolute) { 748 // The runtime name is absolute but the compiled name (the expected one) is relative. 749 // This is the case for split apks which depend on base or on other splits. 750 dex_name = info.classpath[k]; 751 expected_dex_name = OatFile::ResolveRelativeEncodedDexLocation( 752 info.classpath[k].c_str(), expected_info.classpath[k]); 753 } else if (is_expected_dex_name_absolute) { 754 // The runtime name is relative but the compiled name is absolute. 755 // There is no expected use case that would end up here as dex files are always loaded 756 // with their absolute location. However, be tolerant and do the best effort (in case 757 // there are unexpected new use case...). 758 dex_name = OatFile::ResolveRelativeEncodedDexLocation( 759 expected_info.classpath[k].c_str(), info.classpath[k]); 760 expected_dex_name = expected_info.classpath[k]; 761 } else { 762 // Both locations are relative. In this case there's not much we can be sure about 763 // except that the names are the same. The checksum will ensure that the files are 764 // are same. This should not happen outside testing and manual invocations. 765 dex_name = info.classpath[k]; 766 expected_dex_name = expected_info.classpath[k]; 767 } 768 769 // Compare the locations. 770 if (dex_name != expected_dex_name) { 771 LOG(WARNING) << "ClassLoaderContext classpath element mismatch for position " << i 772 << ". expected=" << expected_info.classpath[k] 773 << ", found=" << info.classpath[k] 774 << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")"; 775 return false; 776 } 777 778 // Compare the checksums. 779 if (info.checksums[k] != expected_info.checksums[k]) { 780 LOG(WARNING) << "ClassLoaderContext classpath element checksum mismatch for position " << i 781 << ". expected=" << expected_info.checksums[k] 782 << ", found=" << info.checksums[k] 783 << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")"; 784 return false; 785 } 786 } 787 } 788 return true; 789 } 790 791 jclass ClassLoaderContext::GetClassLoaderClass(ClassLoaderType type) { 792 switch (type) { 793 case kPathClassLoader: return WellKnownClasses::dalvik_system_PathClassLoader; 794 case kDelegateLastClassLoader: return WellKnownClasses::dalvik_system_DelegateLastClassLoader; 795 case kInvalidClassLoader: break; // will fail after the switch. 796 } 797 LOG(FATAL) << "Invalid class loader type " << type; 798 UNREACHABLE(); 799 } 800 801 } // namespace art 802