1 /* 2 * Copyright (c) 2014, 2025, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "cds/aotClassFilter.hpp" 26 #include "cds/aotClassLocation.hpp" 27 #include "cds/aotLogging.hpp" 28 #include "cds/archiveBuilder.hpp" 29 #include "cds/archiveUtils.hpp" 30 #include "cds/cdsConfig.hpp" 31 #include "cds/cdsProtectionDomain.hpp" 32 #include "cds/classListParser.hpp" 33 #include "cds/classListWriter.hpp" 34 #include "cds/dumpTimeClassInfo.inline.hpp" 35 #include "cds/dynamicArchive.hpp" 36 #include "cds/filemap.hpp" 37 #include "cds/heapShared.hpp" 38 #include "cds/lambdaFormInvokers.inline.hpp" 39 #include "cds/lambdaProxyClassDictionary.hpp" 40 #include "cds/lambdaFormInvokers.inline.hpp" 41 #include "cds/metaspaceShared.hpp" 42 #include "cds/runTimeClassInfo.hpp" 43 #include "cds/unregisteredClasses.hpp" 44 #include "classfile/classFileStream.hpp" 45 #include "classfile/classLoader.hpp" 46 #include "classfile/classLoaderData.inline.hpp" 47 #include "classfile/classLoaderDataGraph.hpp" 48 #include "classfile/classLoaderExt.hpp" 49 #include "classfile/dictionary.hpp" 50 #include "classfile/javaClasses.hpp" 51 #include "classfile/javaClasses.inline.hpp" 52 #include "classfile/symbolTable.hpp" 53 #include "classfile/systemDictionary.hpp" 54 #include "classfile/systemDictionaryShared.hpp" 55 #include "classfile/verificationType.hpp" 56 #include "classfile/vmClasses.hpp" 57 #include "classfile/vmSymbols.hpp" 58 #include "jfr/jfrEvents.hpp" 59 #include "logging/log.hpp" 60 #include "logging/logStream.hpp" 61 #include "memory/allocation.hpp" 62 #include "memory/metadataFactory.hpp" 63 #include "memory/metaspaceClosure.hpp" 64 #include "memory/oopFactory.hpp" 65 #include "memory/resourceArea.hpp" 66 #include "memory/universe.hpp" 67 #include "oops/compressedKlass.inline.hpp" 68 #include "oops/instanceKlass.hpp" 69 #include "oops/klass.inline.hpp" 70 #include "oops/methodData.hpp" 71 #include "oops/objArrayKlass.hpp" 72 #include "oops/objArrayOop.inline.hpp" 73 #include "oops/oop.inline.hpp" 74 #include "oops/oopHandle.inline.hpp" 75 #include "oops/typeArrayOop.inline.hpp" 76 #include "runtime/arguments.hpp" 77 #include "runtime/handles.inline.hpp" 78 #include "runtime/java.hpp" 79 #include "runtime/javaCalls.hpp" 80 #include "runtime/mutexLocker.hpp" 81 #include "utilities/resourceHash.hpp" 82 #include "utilities/stringUtils.hpp" 83 84 SystemDictionaryShared::ArchiveInfo SystemDictionaryShared::_static_archive; 85 SystemDictionaryShared::ArchiveInfo SystemDictionaryShared::_dynamic_archive; 86 87 DumpTimeSharedClassTable* SystemDictionaryShared::_dumptime_table = nullptr; 88 89 // Used by NoClassLoadingMark 90 DEBUG_ONLY(bool SystemDictionaryShared::_class_loading_may_happen = true;) 91 92 #ifdef ASSERT 93 static void check_klass_after_loading(const Klass* k) { 94 #ifdef _LP64 95 if (k != nullptr && UseCompressedClassPointers && k->needs_narrow_id()) { 96 CompressedKlassPointers::check_encodable(k); 97 } 98 #endif 99 } 100 #endif 101 102 InstanceKlass* SystemDictionaryShared::load_shared_class_for_builtin_loader( 103 Symbol* class_name, Handle class_loader, TRAPS) { 104 assert(CDSConfig::is_using_archive(), "must be"); 105 InstanceKlass* ik = find_builtin_class(class_name); 106 107 if (ik != nullptr && !ik->shared_loading_failed()) { 108 if ((SystemDictionary::is_system_class_loader(class_loader()) && ik->defined_by_app_loader()) || 109 (SystemDictionary::is_platform_class_loader(class_loader()) && ik->defined_by_platform_loader())) { 110 SharedClassLoadingMark slm(THREAD, ik); 111 PackageEntry* pkg_entry = CDSProtectionDomain::get_package_entry_from_class(ik, class_loader); 112 Handle protection_domain; 113 if (!class_name->starts_with("jdk/proxy")) // java/lang/reflect/Proxy$ProxyBuilder defines the proxy classes with a null protection domain. 114 { 115 protection_domain = CDSProtectionDomain::init_security_info(class_loader, ik, pkg_entry, CHECK_NULL); 116 } 117 return load_shared_class(ik, class_loader, protection_domain, nullptr, pkg_entry, THREAD); 118 } 119 } 120 return nullptr; 121 } 122 123 // This function is called for loading only UNREGISTERED classes 124 InstanceKlass* SystemDictionaryShared::lookup_from_stream(Symbol* class_name, 125 Handle class_loader, 126 Handle protection_domain, 127 const ClassFileStream* cfs, 128 TRAPS) { 129 if (!CDSConfig::is_using_archive()) { 130 return nullptr; 131 } 132 if (class_name == nullptr) { // don't do this for hidden classes 133 return nullptr; 134 } 135 if (class_loader.is_null() || 136 SystemDictionary::is_system_class_loader(class_loader()) || 137 SystemDictionary::is_platform_class_loader(class_loader())) { 138 // Do nothing for the BUILTIN loaders. 139 return nullptr; 140 } 141 142 const RunTimeClassInfo* record = find_record(&_static_archive._unregistered_dictionary, 143 &_dynamic_archive._unregistered_dictionary, 144 class_name); 145 if (record == nullptr) { 146 return nullptr; 147 } 148 149 int clsfile_size = cfs->length(); 150 int clsfile_crc32 = ClassLoader::crc32(0, (const char*)cfs->buffer(), cfs->length()); 151 152 if (!record->matches(clsfile_size, clsfile_crc32)) { 153 return nullptr; 154 } 155 156 return acquire_class_for_current_thread(record->klass(), class_loader, 157 protection_domain, cfs, 158 THREAD); 159 } 160 161 InstanceKlass* SystemDictionaryShared::acquire_class_for_current_thread( 162 InstanceKlass *ik, 163 Handle class_loader, 164 Handle protection_domain, 165 const ClassFileStream *cfs, 166 TRAPS) { 167 ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(class_loader()); 168 169 { 170 MutexLocker mu(THREAD, SharedDictionary_lock); 171 if (ik->class_loader_data() != nullptr) { 172 // ik is already loaded (by this loader or by a different loader) 173 // or ik is being loaded by a different thread (by this loader or by a different loader) 174 return nullptr; 175 } 176 177 // No other thread has acquired this yet, so give it to *this thread* 178 ik->set_class_loader_data(loader_data); 179 } 180 181 // No longer holding SharedDictionary_lock 182 // No need to lock, as <ik> can be held only by a single thread. 183 loader_data->add_class(ik); 184 185 // Get the package entry. 186 PackageEntry* pkg_entry = CDSProtectionDomain::get_package_entry_from_class(ik, class_loader); 187 188 // Load and check super/interfaces, restore unshareable info 189 InstanceKlass* shared_klass = load_shared_class(ik, class_loader, protection_domain, 190 cfs, pkg_entry, THREAD); 191 if (shared_klass == nullptr || HAS_PENDING_EXCEPTION) { 192 // TODO: clean up <ik> so it can be used again 193 return nullptr; 194 } 195 196 return shared_klass; 197 } 198 199 // Guaranteed to return non-null value for non-shared classes. 200 // k must not be a shared class. 201 DumpTimeClassInfo* SystemDictionaryShared::get_info(InstanceKlass* k) { 202 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag); 203 return get_info_locked(k); 204 } 205 206 DumpTimeClassInfo* SystemDictionaryShared::get_info_locked(InstanceKlass* k) { 207 assert_lock_strong(DumpTimeTable_lock); 208 DumpTimeClassInfo* info = _dumptime_table->get_info(k); 209 assert(info != nullptr, "must be"); 210 return info; 211 } 212 213 bool SystemDictionaryShared::check_for_exclusion(InstanceKlass* k, DumpTimeClassInfo* info) { 214 if (CDSConfig::is_dumping_dynamic_archive() && MetaspaceShared::is_in_shared_metaspace(k)) { 215 // We have reached a super type that's already in the base archive. Treat it 216 // as "not excluded". 217 return false; 218 } 219 220 if (info == nullptr) { 221 info = _dumptime_table->get(k); 222 assert(info != nullptr, "supertypes of any classes in _dumptime_table must either be shared, or must also be in _dumptime_table"); 223 } 224 225 if (!info->has_checked_exclusion()) { 226 if (check_for_exclusion_impl(k)) { 227 info->set_excluded(); 228 } 229 info->set_has_checked_exclusion(); 230 } 231 232 return info->is_excluded(); 233 } 234 235 // Returns true so the caller can do: return warn_excluded("....."); 236 bool SystemDictionaryShared::warn_excluded(InstanceKlass* k, const char* reason) { 237 ResourceMark rm; 238 aot_log_warning(aot)("Skipping %s: %s", k->name()->as_C_string(), reason); 239 return true; 240 } 241 242 bool SystemDictionaryShared::is_jfr_event_class(InstanceKlass *k) { 243 while (k) { 244 if (k->name()->equals("jdk/internal/event/Event")) { 245 return true; 246 } 247 k = k->java_super(); 248 } 249 return false; 250 } 251 252 bool SystemDictionaryShared::is_early_klass(InstanceKlass* ik) { 253 DumpTimeClassInfo* info = _dumptime_table->get(ik); 254 return (info != nullptr) ? info->is_early_klass() : false; 255 } 256 257 bool SystemDictionaryShared::check_for_exclusion_impl(InstanceKlass* k) { 258 if (CDSConfig::is_dumping_final_static_archive() && k->defined_by_other_loaders() 259 && k->is_shared()) { 260 return false; // Do not exclude: unregistered classes are passed from preimage to final image. 261 } 262 263 if (k->is_in_error_state()) { 264 return warn_excluded(k, "In error state"); 265 } 266 if (k->is_scratch_class()) { 267 return warn_excluded(k, "A scratch class"); 268 } 269 if (!k->is_loaded()) { 270 return warn_excluded(k, "Not in loaded state"); 271 } 272 if (has_been_redefined(k)) { 273 return warn_excluded(k, "Has been redefined"); 274 } 275 if (!k->is_hidden() && k->shared_classpath_index() < 0 && is_builtin(k)) { 276 if (k->name()->starts_with("java/lang/invoke/BoundMethodHandle$Species_")) { 277 // This class is dynamically generated by the JDK 278 if (CDSConfig::is_dumping_method_handles()) { 279 k->set_shared_classpath_index(0); 280 } else { 281 ResourceMark rm; 282 aot_log_info(aot)("Skipping %s because it is dynamically generated", k->name()->as_C_string()); 283 return true; // exclude without warning 284 } 285 } else { 286 // These are classes loaded from unsupported locations (such as those loaded by JVMTI native 287 // agent during dump time). 288 return warn_excluded(k, "Unsupported location"); 289 } 290 } 291 if (k->signers() != nullptr) { 292 // We cannot include signed classes in the archive because the certificates 293 // used during dump time may be different than those used during 294 // runtime (due to expiration, etc). 295 return warn_excluded(k, "Signed JAR"); 296 } 297 if (is_jfr_event_class(k)) { 298 // We cannot include JFR event classes because they need runtime-specific 299 // instrumentation in order to work with -XX:FlightRecorderOptions:retransform=false. 300 // There are only a small number of these classes, so it's not worthwhile to 301 // support them and make CDS more complicated. 302 if (!CDSConfig::is_dumping_reflection_data()) { // FIXME: !!! HACK !!! 303 return warn_excluded(k, "JFR event class"); 304 } 305 } 306 307 if (!k->is_linked()) { 308 if (has_class_failed_verification(k)) { 309 return warn_excluded(k, "Failed verification"); 310 } else if (CDSConfig::is_dumping_aot_linked_classes()) { 311 // Most loaded classes should have been speculatively linked by MetaspaceShared::link_class_for_cds(). 312 // However, we do not speculatively link old classes, as they are not recorded by 313 // SystemDictionaryShared::record_linking_constraint(). As a result, such an unlinked 314 // class may fail to verify in AOTLinkedClassBulkLoader::init_required_classes_for_loader(), 315 // causing the JVM to fail at bootstrap. 316 return warn_excluded(k, "Unlinked class not supported by AOTClassLinking"); 317 } else if (CDSConfig::is_dumping_preimage_static_archive()) { 318 // When dumping the final static archive, we will unconditionally load and link all 319 // classes from the preimage. We don't want to get a VerifyError when linking this class. 320 return warn_excluded(k, "Unlinked class not supported by AOTConfiguration"); 321 } 322 } else { 323 if (!k->can_be_verified_at_dumptime() && !CDSConfig::preserve_all_dumptime_verification_states(k)) { 324 // We have an old class that has been linked (e.g., it's been executed during 325 // dump time). This class has been verified using the old verifier, which 326 // doesn't save the verification constraints, so check_verification_constraints() 327 // won't work at runtime. 328 // As a result, we cannot store this class. It must be loaded and fully verified 329 // at runtime. 330 return warn_excluded(k, "Old class has been linked"); 331 } 332 } 333 334 if (UnregisteredClasses::check_for_exclusion(k)) { 335 ResourceMark rm; 336 aot_log_info(aot)("Skipping %s: used only when dumping CDS archive", k->name()->as_C_string()); 337 return true; 338 } 339 340 InstanceKlass* super = k->java_super(); 341 if (super != nullptr && check_for_exclusion(super, nullptr)) { 342 ResourceMark rm; 343 aot_log_warning(aot)("Skipping %s: super class %s is excluded", k->name()->as_C_string(), super->name()->as_C_string()); 344 return true; 345 } 346 347 Array<InstanceKlass*>* interfaces = k->local_interfaces(); 348 int len = interfaces->length(); 349 for (int i = 0; i < len; i++) { 350 InstanceKlass* intf = interfaces->at(i); 351 if (check_for_exclusion(intf, nullptr)) { 352 ResourceMark rm; 353 aot_log_warning(aot)("Skipping %s: interface %s is excluded", k->name()->as_C_string(), intf->name()->as_C_string()); 354 return true; 355 } 356 } 357 358 return false; // false == k should NOT be excluded 359 } 360 361 bool SystemDictionaryShared::is_builtin_loader(ClassLoaderData* loader_data) { 362 oop class_loader = loader_data->class_loader(); 363 return (class_loader == nullptr || 364 SystemDictionary::is_system_class_loader(class_loader) || 365 SystemDictionary::is_platform_class_loader(class_loader)); 366 } 367 368 bool SystemDictionaryShared::has_platform_or_app_classes() { 369 if (FileMapInfo::current_info()->has_platform_or_app_classes()) { 370 return true; 371 } 372 if (DynamicArchive::is_mapped() && 373 FileMapInfo::dynamic_info()->has_platform_or_app_classes()) { 374 return true; 375 } 376 return false; 377 } 378 379 // The following stack shows how this code is reached: 380 // 381 // [0] SystemDictionaryShared::find_or_load_shared_class() 382 // [1] JVM_FindLoadedClass 383 // [2] java.lang.ClassLoader.findLoadedClass0() 384 // [3] java.lang.ClassLoader.findLoadedClass() 385 // [4] jdk.internal.loader.BuiltinClassLoader.loadClassOrNull() 386 // [5] jdk.internal.loader.BuiltinClassLoader.loadClass() 387 // [6] jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(), or 388 // jdk.internal.loader.ClassLoaders$PlatformClassLoader.loadClass() 389 // 390 // AppCDS supports fast class loading for these 2 built-in class loaders: 391 // jdk.internal.loader.ClassLoaders$PlatformClassLoader 392 // jdk.internal.loader.ClassLoaders$AppClassLoader 393 // with the following assumptions (based on the JDK core library source code): 394 // 395 // [a] these two loaders use the BuiltinClassLoader.loadClassOrNull() to 396 // load the named class. 397 // [b] BuiltinClassLoader.loadClassOrNull() first calls findLoadedClass(name). 398 // [c] At this point, if we can find the named class inside the 399 // shared_dictionary, we can perform further checks (see 400 // SystemDictionary::is_shared_class_visible) to ensure that this class 401 // was loaded by the same class loader during dump time. 402 // 403 // Given these assumptions, we intercept the findLoadedClass() call to invoke 404 // SystemDictionaryShared::find_or_load_shared_class() to load the shared class from 405 // the archive for the 2 built-in class loaders. This way, 406 // we can improve start-up because we avoid decoding the classfile, 407 // and avoid delegating to the parent loader. 408 // 409 // NOTE: there's a lot of assumption about the Java code. If any of that change, this 410 // needs to be redesigned. 411 412 InstanceKlass* SystemDictionaryShared::find_or_load_shared_class( 413 Symbol* name, Handle class_loader, TRAPS) { 414 InstanceKlass* k = nullptr; 415 if (CDSConfig::is_using_archive()) { 416 if (!has_platform_or_app_classes()) { 417 return nullptr; 418 } 419 420 if (SystemDictionary::is_system_class_loader(class_loader()) || 421 SystemDictionary::is_platform_class_loader(class_loader())) { 422 ClassLoaderData *loader_data = register_loader(class_loader); 423 Dictionary* dictionary = loader_data->dictionary(); 424 425 // Note: currently, find_or_load_shared_class is called only from 426 // JVM_FindLoadedClass and used for PlatformClassLoader and AppClassLoader, 427 // which are parallel-capable loaders, so a lock here is NOT taken. 428 assert(get_loader_lock_or_null(class_loader) == nullptr, "ObjectLocker not required"); 429 { 430 MutexLocker mu(THREAD, SystemDictionary_lock); 431 InstanceKlass* check = dictionary->find_class(THREAD, name); 432 if (check != nullptr) { 433 return check; 434 } 435 } 436 437 k = load_shared_class_for_builtin_loader(name, class_loader, THREAD); 438 if (k != nullptr) { 439 SharedClassLoadingMark slm(THREAD, k); 440 k = find_or_define_instance_class(name, class_loader, k, CHECK_NULL); 441 } 442 } 443 } 444 445 DEBUG_ONLY(check_klass_after_loading(k);) 446 447 return k; 448 } 449 450 class UnregisteredClassesTable : public ResourceHashtable< 451 Symbol*, InstanceKlass*, 452 15889, // prime number 453 AnyObj::C_HEAP> {}; 454 455 static UnregisteredClassesTable* _unregistered_classes_table = nullptr; 456 457 // true == class was successfully added; false == a duplicated class (with the same name) already exists. 458 bool SystemDictionaryShared::add_unregistered_class(Thread* current, InstanceKlass* klass) { 459 // We don't allow duplicated unregistered classes with the same name. 460 // We only archive the first class with that name that succeeds putting 461 // itself into the table. 462 assert(CDSConfig::is_dumping_archive() || ClassListWriter::is_enabled(), "sanity"); 463 MutexLocker ml(current, UnregisteredClassesTable_lock, Mutex::_no_safepoint_check_flag); 464 Symbol* name = klass->name(); 465 if (_unregistered_classes_table == nullptr) { 466 _unregistered_classes_table = new (mtClass)UnregisteredClassesTable(); 467 } 468 bool created; 469 InstanceKlass** v = _unregistered_classes_table->put_if_absent(name, klass, &created); 470 if (created) { 471 name->increment_refcount(); 472 } 473 return (klass == *v); 474 } 475 476 InstanceKlass* SystemDictionaryShared::get_unregistered_class(Symbol* name) { 477 assert(CDSConfig::is_dumping_archive() || ClassListWriter::is_enabled(), "sanity"); 478 if (_unregistered_classes_table == nullptr) { 479 return nullptr; 480 } 481 InstanceKlass** k = _unregistered_classes_table->get(name); 482 return k != nullptr ? *k : nullptr; 483 } 484 485 void SystemDictionaryShared::copy_unregistered_class_size_and_crc32(InstanceKlass* klass) { 486 precond(CDSConfig::is_dumping_final_static_archive()); 487 precond(klass->is_shared()); 488 489 // A shared class must have a RunTimeClassInfo record 490 const RunTimeClassInfo* record = find_record(&_static_archive._unregistered_dictionary, 491 nullptr, klass->name()); 492 precond(record != nullptr); 493 precond(record->klass() == klass); 494 495 DumpTimeClassInfo* info = get_info(klass); 496 info->_clsfile_size = record->crc()->_clsfile_size; 497 info->_clsfile_crc32 = record->crc()->_clsfile_crc32; 498 } 499 500 void SystemDictionaryShared::set_shared_class_misc_info(InstanceKlass* k, ClassFileStream* cfs) { 501 assert(CDSConfig::is_dumping_archive(), "sanity"); 502 assert(!is_builtin(k), "must be unregistered class"); 503 DumpTimeClassInfo* info = get_info(k); 504 info->_clsfile_size = cfs->length(); 505 info->_clsfile_crc32 = ClassLoader::crc32(0, (const char*)cfs->buffer(), cfs->length()); 506 } 507 508 void SystemDictionaryShared::initialize() { 509 if (CDSConfig::is_dumping_archive()) { 510 _dumptime_table = new (mtClass) DumpTimeSharedClassTable; 511 LambdaProxyClassDictionary::dumptime_init(); 512 if (CDSConfig::is_dumping_heap()) { 513 HeapShared::init_dumping(); 514 } 515 } 516 } 517 518 void SystemDictionaryShared::init_dumptime_info(InstanceKlass* k) { 519 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag); 520 assert(SystemDictionaryShared::class_loading_may_happen(), "sanity"); 521 DumpTimeClassInfo* info = _dumptime_table->allocate_info(k); 522 if (AOTClassFilter::is_aot_tooling_class(k)) { 523 info->set_is_aot_tooling_class(); 524 } 525 } 526 527 void SystemDictionaryShared::remove_dumptime_info(InstanceKlass* k) { 528 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag); 529 _dumptime_table->remove(k); 530 } 531 532 void SystemDictionaryShared::handle_class_unloading(InstanceKlass* klass) { 533 if (CDSConfig::is_dumping_archive()) { 534 remove_dumptime_info(klass); 535 } 536 537 if (CDSConfig::is_dumping_archive() || ClassListWriter::is_enabled()) { 538 MutexLocker ml(Thread::current(), UnregisteredClassesTable_lock, Mutex::_no_safepoint_check_flag); 539 if (_unregistered_classes_table != nullptr) { 540 // Remove the class from _unregistered_classes_table: keep the entry but 541 // set it to null. This ensure no classes with the same name can be 542 // added again. 543 InstanceKlass** v = _unregistered_classes_table->get(klass->name()); 544 if (v != nullptr) { 545 *v = nullptr; 546 } 547 } 548 } else { 549 assert(_unregistered_classes_table == nullptr, "must not be used"); 550 } 551 552 if (ClassListWriter::is_enabled()) { 553 ClassListWriter cw; 554 cw.handle_class_unloading((const InstanceKlass*)klass); 555 } 556 } 557 558 void SystemDictionaryShared::init_dumptime_info_from_preimage(InstanceKlass* k) { 559 init_dumptime_info(k); 560 copy_verification_constraints_from_preimage(k); 561 copy_linking_constraints_from_preimage(k); 562 563 if (SystemDictionary::is_platform_class_loader(k->class_loader())) { 564 AOTClassLocationConfig::dumptime_set_has_platform_classes(); 565 } else if (SystemDictionary::is_system_class_loader(k->class_loader())) { 566 AOTClassLocationConfig::dumptime_set_has_app_classes(); 567 } 568 } 569 570 // Check if a class or any of its supertypes has been redefined. 571 bool SystemDictionaryShared::has_been_redefined(InstanceKlass* k) { 572 if (k->has_been_redefined()) { 573 return true; 574 } 575 if (k->java_super() != nullptr && has_been_redefined(k->java_super())) { 576 return true; 577 } 578 Array<InstanceKlass*>* interfaces = k->local_interfaces(); 579 int len = interfaces->length(); 580 for (int i = 0; i < len; i++) { 581 if (has_been_redefined(interfaces->at(i))) { 582 return true; 583 } 584 } 585 return false; 586 } 587 588 // k is a class before relocating by ArchiveBuilder 589 void SystemDictionaryShared::validate_before_archiving(InstanceKlass* k) { 590 ResourceMark rm; 591 const char* name = k->name()->as_C_string(); 592 DumpTimeClassInfo* info = _dumptime_table->get(k); 593 assert(!class_loading_may_happen(), "class loading must be disabled"); 594 guarantee(info != nullptr, "Class %s must be entered into _dumptime_table", name); 595 guarantee(!info->is_excluded(), "Should not attempt to archive excluded class %s", name); 596 if (is_builtin(k)) { 597 if (k->is_hidden()) { 598 if (CDSConfig::is_dumping_lambdas_in_legacy_mode()) { 599 assert(LambdaProxyClassDictionary::is_registered_lambda_proxy_class(k), "unexpected hidden class %s", name); 600 } 601 } 602 guarantee(!k->defined_by_other_loaders(), 603 "Class loader type must be set for BUILTIN class %s", name); 604 605 } else { 606 guarantee(k->defined_by_other_loaders(), 607 "Class loader type must not be set for UNREGISTERED class %s", name); 608 } 609 } 610 611 class UnregisteredClassesDuplicationChecker : StackObj { 612 GrowableArray<InstanceKlass*> _list; 613 Thread* _thread; 614 public: 615 UnregisteredClassesDuplicationChecker() : _thread(Thread::current()) {} 616 617 void do_entry(InstanceKlass* k, DumpTimeClassInfo& info) { 618 if (!SystemDictionaryShared::is_builtin(k)) { 619 _list.append(k); 620 } 621 } 622 623 static int compare_by_loader(InstanceKlass** a, InstanceKlass** b) { 624 ClassLoaderData* loader_a = a[0]->class_loader_data(); 625 ClassLoaderData* loader_b = b[0]->class_loader_data(); 626 627 if (loader_a != loader_b) { 628 return primitive_compare(loader_a, loader_b); 629 } else { 630 return primitive_compare(a[0], b[0]); 631 } 632 } 633 634 void mark_duplicated_classes() { 635 // Two loaders may load two identical or similar hierarchies of classes. If we 636 // check for duplication in random order, we may end up excluding important base classes 637 // in both hierarchies, causing most of the classes to be excluded. 638 // We sort the classes by their loaders. This way we're likely to archive 639 // all classes in the one of the two hierarchies. 640 _list.sort(compare_by_loader); 641 for (int i = 0; i < _list.length(); i++) { 642 InstanceKlass* k = _list.at(i); 643 bool i_am_first = SystemDictionaryShared::add_unregistered_class(_thread, k); 644 if (!i_am_first) { 645 SystemDictionaryShared::warn_excluded(k, "Duplicated unregistered class"); 646 SystemDictionaryShared::set_excluded_locked(k); 647 } 648 } 649 } 650 }; 651 652 // Returns true if the class should be excluded. This can be called by 653 // AOTConstantPoolResolver before or after we enter the CDS safepoint. 654 // When called before the safepoint, we need to link the class so that 655 // it can be checked by check_for_exclusion(). 656 bool SystemDictionaryShared::should_be_excluded(Klass* k) { 657 assert(CDSConfig::is_dumping_archive(), "sanity"); 658 assert(CDSConfig::current_thread_is_vm_or_dumper(), "sanity"); 659 660 if (k->is_objArray_klass()) { 661 return should_be_excluded(ObjArrayKlass::cast(k)->bottom_klass()); 662 } 663 664 if (!k->is_instance_klass()) { 665 return false; 666 } else { 667 InstanceKlass* ik = InstanceKlass::cast(k); 668 669 if (CDSConfig::is_dumping_dynamic_archive() && ik->is_shared()) { 670 // ik is already part of the static archive, so it will never be considered as excluded. 671 return false; 672 } 673 674 if (!SafepointSynchronize::is_at_safepoint()) { 675 if (!ik->is_linked()) { 676 // check_for_exclusion() below doesn't link unlinked classes. We come 677 // here only when we are trying to aot-link constant pool entries, so 678 // we'd better link the class. 679 JavaThread* THREAD = JavaThread::current(); 680 ik->link_class(THREAD); 681 if (HAS_PENDING_EXCEPTION) { 682 CLEAR_PENDING_EXCEPTION; 683 return true; // linking failed -- let's exclude it 684 } 685 } 686 687 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag); 688 DumpTimeClassInfo* p = get_info_locked(ik); 689 if (p->is_excluded()) { 690 return true; 691 } 692 return check_for_exclusion(ik, p); 693 } else { 694 // No need to check for is_linked() as all eligible classes should have 695 // already been linked in MetaspaceShared::link_class_for_cds(). 696 // Can't take the lock as we are in safepoint. 697 DumpTimeClassInfo* p = _dumptime_table->get(ik); 698 if (p->is_excluded()) { 699 return true; 700 } 701 return check_for_exclusion(ik, p); 702 } 703 } 704 } 705 706 void SystemDictionaryShared::finish_exclusion_checks() { 707 if (CDSConfig::is_dumping_dynamic_archive() || CDSConfig::is_dumping_preimage_static_archive()) { 708 // Do this first -- if a base class is excluded due to duplication, 709 // all of its subclasses will also be excluded. 710 ResourceMark rm; 711 UnregisteredClassesDuplicationChecker dup_checker; 712 _dumptime_table->iterate_all_live_classes(&dup_checker); 713 dup_checker.mark_duplicated_classes(); 714 } 715 716 _dumptime_table->iterate_all_live_classes([&] (InstanceKlass* k, DumpTimeClassInfo& info) { 717 SystemDictionaryShared::check_for_exclusion(k, &info); 718 }); 719 720 _dumptime_table->update_counts(); 721 if (CDSConfig::is_dumping_lambdas_in_legacy_mode()) { 722 LambdaProxyClassDictionary::cleanup_dumptime_table(); 723 } 724 } 725 726 bool SystemDictionaryShared::is_excluded_class(InstanceKlass* k) { 727 assert(!class_loading_may_happen(), "class loading must be disabled"); 728 assert_lock_strong(DumpTimeTable_lock); 729 assert(CDSConfig::is_dumping_archive(), "sanity"); 730 DumpTimeClassInfo* p = get_info_locked(k); 731 return p->is_excluded(); 732 } 733 734 void SystemDictionaryShared::set_excluded_locked(InstanceKlass* k) { 735 assert_lock_strong(DumpTimeTable_lock); 736 assert(CDSConfig::is_dumping_archive(), "sanity"); 737 DumpTimeClassInfo* info = get_info_locked(k); 738 info->set_excluded(); 739 } 740 741 void SystemDictionaryShared::set_excluded(InstanceKlass* k) { 742 assert(CDSConfig::is_dumping_archive(), "sanity"); 743 DumpTimeClassInfo* info = get_info(k); 744 info->set_excluded(); 745 } 746 747 void SystemDictionaryShared::set_class_has_failed_verification(InstanceKlass* ik) { 748 assert(CDSConfig::is_dumping_archive(), "sanity"); 749 DumpTimeClassInfo* p = get_info(ik); 750 p->set_failed_verification(); 751 } 752 753 bool SystemDictionaryShared::has_class_failed_verification(InstanceKlass* ik) { 754 assert(CDSConfig::is_dumping_archive(), "sanity"); 755 DumpTimeClassInfo* p = _dumptime_table->get(ik); 756 return (p == nullptr) ? false : p->failed_verification(); 757 } 758 759 void SystemDictionaryShared::set_from_class_file_load_hook(InstanceKlass* ik) { 760 warn_excluded(ik, "From ClassFileLoadHook"); 761 set_excluded(ik); 762 } 763 764 void SystemDictionaryShared::dumptime_classes_do(MetaspaceClosure* it) { 765 assert_lock_strong(DumpTimeTable_lock); 766 767 auto do_klass = [&] (InstanceKlass* k, DumpTimeClassInfo& info) { 768 if (CDSConfig::is_dumping_final_static_archive() && !k->is_loaded()) { 769 assert(k->defined_by_other_loaders(), "must be"); 770 info.metaspace_pointers_do(it); 771 } else if (k->is_loader_alive() && !info.is_excluded()) { 772 info.metaspace_pointers_do(it); 773 } 774 }; 775 _dumptime_table->iterate_all_live_classes(do_klass); 776 777 if (CDSConfig::is_dumping_lambdas_in_legacy_mode()) { 778 LambdaProxyClassDictionary::dumptime_classes_do(it); 779 } 780 } 781 782 // Called from VerificationType::is_reference_assignable_from() before performing the assignability check of 783 // T1 must be assignable from T2 784 // Where: 785 // L is the class loader of <k> 786 // T1 is the type resolved by L using the name <name> 787 // T2 is the type resolved by L using the name <from_name> 788 // 789 // The meaning of (*skip_assignability_check): 790 // true: is_reference_assignable_from() should SKIP the assignability check 791 // false: is_reference_assignable_from() should COMPLETE the assignability check 792 void SystemDictionaryShared::add_verification_constraint(InstanceKlass* k, Symbol* name, 793 Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object, 794 bool* skip_assignability_check) { 795 assert(CDSConfig::is_dumping_archive(), "sanity"); 796 if (CDSConfig::is_dumping_dynamic_archive() && k->is_shared()) { 797 // k is a new class in the static archive, but one of its supertypes is an old class, so k wasn't 798 // verified during dump time. No need to record constraints as k won't be included in the dynamic archive. 799 return; 800 } 801 if (CDSConfig::is_dumping_aot_linked_classes() && is_builtin(k)) { 802 // There's no need to save verification constraints 803 // TODO -- double check the logic before integrating into mainline!! 804 return; 805 } 806 807 DumpTimeClassInfo* info = get_info(k); 808 info->add_verification_constraint(k, name, from_name, from_field_is_protected, 809 from_is_array, from_is_object); 810 811 if (CDSConfig::is_dumping_classic_static_archive() && !is_builtin(k)) { 812 // This applies ONLY to the "classic" CDS static dump, which reads the list of 813 // unregistered classes (those intended for custom class loaders) from the classlist 814 // and loads them using jdk.internal.misc.CDS$UnregisteredClassLoader. 815 // 816 // When the classlist contains an unregistered class k, the supertypes of k are also 817 // recorded in the classlist. However, the classlist does not contain information about 818 // any class X that's not a supertype of k but is needed in the verification of k. 819 // As a result, CDS$UnregisteredClassLoader will not know how to resolve X. 820 // 821 // Therefore, we tell the verifier to refrain from resolving X. Instead, X is recorded 822 // (symbolically) in the verification constraints of k. In the production run, 823 // when k is loaded, we will go through its verification constraints and resolve X to complete 824 // the is_reference_assignable_from() checks. 825 *skip_assignability_check = true; 826 } else { 827 // In all other cases, we are using an *actual* class loader to load k, so it should be able 828 // to resolve any types that are needed for the verification of k. 829 *skip_assignability_check = false; 830 } 831 } 832 833 void SystemDictionaryShared::add_enum_klass_static_field(InstanceKlass* ik, int root_index) { 834 assert(CDSConfig::is_dumping_heap(), "sanity"); 835 DumpTimeClassInfo* info = get_info_locked(ik); 836 info->add_enum_klass_static_field(root_index); 837 } 838 839 void SystemDictionaryShared::check_verification_constraints(InstanceKlass* klass, 840 TRAPS) { 841 assert(CDSConfig::is_using_archive(), "called at run time with CDS enabled only"); 842 RunTimeClassInfo* record = RunTimeClassInfo::get_for(klass); 843 844 int length = record->num_verifier_constraints(); 845 if (length > 0) { 846 for (int i = 0; i < length; i++) { 847 RunTimeClassInfo::RTVerifierConstraint* vc = record->verifier_constraint_at(i); 848 Symbol* name = vc->name(); 849 Symbol* from_name = vc->from_name(); 850 851 if (log_is_enabled(Trace, aot, verification)) { 852 ResourceMark rm(THREAD); 853 log_trace(aot, verification)("check_verification_constraint: %s: %s must be subclass of %s [0x%x]", 854 klass->external_name(), from_name->as_klass_external_name(), 855 name->as_klass_external_name(), record->verifier_constraint_flag(i)); 856 } 857 858 bool ok = VerificationType::resolve_and_check_assignability(klass, name, from_name, 859 record->from_field_is_protected(i), record->from_is_array(i), record->from_is_object(i), CHECK); 860 if (!ok) { 861 ResourceMark rm(THREAD); 862 stringStream ss; 863 864 ss.print_cr("Bad type on operand stack"); 865 ss.print_cr("Exception Details:"); 866 ss.print_cr(" Location:\n %s", klass->name()->as_C_string()); 867 ss.print_cr(" Reason:\n Type '%s' is not assignable to '%s'", 868 from_name->as_quoted_ascii(), name->as_quoted_ascii()); 869 THROW_MSG(vmSymbols::java_lang_VerifyError(), ss.as_string()); 870 } 871 } 872 } 873 } 874 875 void SystemDictionaryShared::copy_verification_constraints_from_preimage(InstanceKlass* klass) { 876 assert(CDSConfig::is_using_archive(), "called at run time with CDS enabled only"); 877 DumpTimeClassInfo* dt_info = get_info(klass); 878 RunTimeClassInfo* rt_info = RunTimeClassInfo::get_for(klass); // from preimage 879 880 int length = rt_info->num_verifier_constraints(); 881 if (length > 0) { 882 for (int i = 0; i < length; i++) { 883 RunTimeClassInfo::RTVerifierConstraint* vc = rt_info->verifier_constraint_at(i); 884 Symbol* name = vc->name(); 885 Symbol* from_name = vc->from_name(); 886 887 dt_info->add_verification_constraint(klass, name, from_name, 888 rt_info->from_field_is_protected(i), rt_info->from_is_array(i), rt_info->from_is_object(i)); 889 } 890 } 891 } 892 893 static oop get_class_loader_by(char type) { 894 if (type == (char)ClassLoader::BOOT_LOADER) { 895 return (oop)nullptr; 896 } else if (type == (char)ClassLoader::PLATFORM_LOADER) { 897 return SystemDictionary::java_platform_loader(); 898 } else { 899 assert (type == (char)ClassLoader::APP_LOADER, "Sanity"); 900 return SystemDictionary::java_system_loader(); 901 } 902 } 903 904 // Record class loader constraints that are checked inside 905 // InstanceKlass::link_class(), so that these can be checked quickly 906 // at runtime without laying out the vtable/itables. 907 void SystemDictionaryShared::record_linking_constraint(Symbol* name, InstanceKlass* klass, 908 Handle loader1, Handle loader2) { 909 // A linking constraint check is executed when: 910 // - klass extends or implements type S 911 // - klass overrides method S.M(...) with X.M 912 // - If klass defines the method M, X is 913 // the same as klass. 914 // - If klass does not define the method M, 915 // X must be a supertype of klass and X.M is 916 // a default method defined by X. 917 // - loader1 = X->class_loader() 918 // - loader2 = S->class_loader() 919 // - loader1 != loader2 920 // - M's parameter(s) include an object type T 921 // We require that 922 // - whenever loader1 and loader2 try to 923 // resolve the type T, they must always resolve to 924 // the same InstanceKlass. 925 // NOTE: type T may or may not be currently resolved in 926 // either of these two loaders. The check itself does not 927 // try to resolve T. 928 oop klass_loader = klass->class_loader(); 929 930 if (!is_system_class_loader(klass_loader) && 931 !is_platform_class_loader(klass_loader)) { 932 // If klass is loaded by system/platform loaders, we can 933 // guarantee that klass and S must be loaded by the same 934 // respective loader between dump time and run time, and 935 // the exact same check on (name, loader1, loader2) will 936 // be executed. Hence, we can cache this check and execute 937 // it at runtime without walking the vtable/itables. 938 // 939 // This cannot be guaranteed for classes loaded by other 940 // loaders, so we bail. 941 return; 942 } 943 944 assert(is_builtin(klass), "must be"); 945 assert(klass_loader != nullptr, "should not be called for boot loader"); 946 assert(loader1 != loader2, "must be"); 947 948 if (CDSConfig::is_dumping_dynamic_archive() && Thread::current()->is_VM_thread()) { 949 // We are re-laying out the vtable/itables of the *copy* of 950 // a class during the final stage of dynamic dumping. The 951 // linking constraints for this class has already been recorded. 952 return; 953 } 954 assert(!Thread::current()->is_VM_thread(), "must be"); 955 956 assert(CDSConfig::is_dumping_archive(), "sanity"); 957 DumpTimeClassInfo* info = get_info(klass); 958 info->record_linking_constraint(name, loader1, loader2); 959 } 960 961 // returns true IFF there's no need to re-initialize the i/v-tables for klass for 962 // the purpose of checking class loader constraints. 963 bool SystemDictionaryShared::check_linking_constraints(Thread* current, InstanceKlass* klass) { 964 assert(CDSConfig::is_using_archive(), "called at run time with CDS enabled only"); 965 LogTarget(Info, class, loader, constraints) log; 966 if (klass->defined_by_boot_loader()) { 967 // No class loader constraint check performed for boot classes. 968 return true; 969 } 970 if (klass->defined_by_platform_loader() || klass->defined_by_app_loader()) { 971 RunTimeClassInfo* info = RunTimeClassInfo::get_for(klass); 972 assert(info != nullptr, "Sanity"); 973 if (info->num_loader_constraints() > 0) { 974 HandleMark hm(current); 975 for (int i = 0; i < info->num_loader_constraints(); i++) { 976 RunTimeClassInfo::RTLoaderConstraint* lc = info->loader_constraint_at(i); 977 Symbol* name = lc->constraint_name(); 978 Handle loader1(current, get_class_loader_by(lc->_loader_type1)); 979 Handle loader2(current, get_class_loader_by(lc->_loader_type2)); 980 if (log.is_enabled()) { 981 ResourceMark rm(current); 982 log.print("[CDS add loader constraint for class %s symbol %s loader[0] %s loader[1] %s", 983 klass->external_name(), name->as_C_string(), 984 ClassLoaderData::class_loader_data(loader1())->loader_name_and_id(), 985 ClassLoaderData::class_loader_data(loader2())->loader_name_and_id()); 986 } 987 if (!SystemDictionary::add_loader_constraint(name, klass, loader1, loader2)) { 988 // Loader constraint violation has been found. The caller 989 // will re-layout the vtable/itables to produce the correct 990 // exception. 991 if (log.is_enabled()) { 992 log.print(" failed]"); 993 } 994 return false; 995 } 996 if (log.is_enabled()) { 997 log.print(" succeeded]"); 998 } 999 } 1000 return true; // for all recorded constraints added successfully. 1001 } 1002 } 1003 if (log.is_enabled()) { 1004 ResourceMark rm(current); 1005 log.print("[CDS has not recorded loader constraint for class %s]", klass->external_name()); 1006 } 1007 return false; 1008 } 1009 1010 void SystemDictionaryShared::copy_linking_constraints_from_preimage(InstanceKlass* klass) { 1011 assert(CDSConfig::is_using_archive(), "called at run time with CDS enabled only"); 1012 JavaThread* current = JavaThread::current(); 1013 if (klass->defined_by_platform_loader() || klass->defined_by_app_loader()) { 1014 RunTimeClassInfo* rt_info = RunTimeClassInfo::get_for(klass); // from preimage 1015 1016 if (rt_info->num_loader_constraints() > 0) { 1017 for (int i = 0; i < rt_info->num_loader_constraints(); i++) { 1018 RunTimeClassInfo::RTLoaderConstraint* lc = rt_info->loader_constraint_at(i); 1019 Symbol* name = lc->constraint_name(); 1020 Handle loader1(current, get_class_loader_by(lc->_loader_type1)); 1021 Handle loader2(current, get_class_loader_by(lc->_loader_type2)); 1022 record_linking_constraint(name, klass, loader1, loader2); 1023 } 1024 } 1025 } 1026 } 1027 1028 unsigned int SystemDictionaryShared::hash_for_shared_dictionary(address ptr) { 1029 if (ArchiveBuilder::is_active() && ArchiveBuilder::current()->is_in_buffer_space(ptr)) { 1030 uintx offset = ArchiveBuilder::current()->any_to_offset(ptr); 1031 unsigned int hash = primitive_hash<uintx>(offset); 1032 DEBUG_ONLY({ 1033 if (MetaspaceObj::is_shared((const MetaspaceObj*)ptr)) { 1034 assert(hash == SystemDictionaryShared::hash_for_shared_dictionary_quick(ptr), "must be"); 1035 } 1036 }); 1037 return hash; 1038 } else { 1039 return SystemDictionaryShared::hash_for_shared_dictionary_quick(ptr); 1040 } 1041 } 1042 1043 class CopySharedClassInfoToArchive : StackObj { 1044 CompactHashtableWriter* _writer; 1045 bool _is_builtin; 1046 ArchiveBuilder *_builder; 1047 public: 1048 CopySharedClassInfoToArchive(CompactHashtableWriter* writer, 1049 bool is_builtin) 1050 : _writer(writer), _is_builtin(is_builtin), _builder(ArchiveBuilder::current()) {} 1051 1052 void do_entry(InstanceKlass* k, DumpTimeClassInfo& info) { 1053 if (!info.is_excluded() && info.is_builtin() == _is_builtin) { 1054 size_t byte_size = info.runtime_info_bytesize(); 1055 RunTimeClassInfo* record; 1056 record = (RunTimeClassInfo*)ArchiveBuilder::ro_region_alloc(byte_size); 1057 record->init(info); 1058 1059 unsigned int hash; 1060 Symbol* name = info._klass->name(); 1061 name = ArchiveBuilder::current()->get_buffered_addr(name); 1062 hash = SystemDictionaryShared::hash_for_shared_dictionary((address)name); 1063 u4 delta = _builder->buffer_to_offset_u4((address)record); 1064 if (_is_builtin && info._klass->is_hidden()) { 1065 // skip 1066 } else { 1067 _writer->add(hash, delta); 1068 } 1069 if (log_is_enabled(Trace, aot, hashtables)) { 1070 ResourceMark rm; 1071 log_trace(aot, hashtables)("%s dictionary: %s", (_is_builtin ? "builtin" : "unregistered"), info._klass->external_name()); 1072 } 1073 1074 // Save this for quick runtime lookup of InstanceKlass* -> RunTimeClassInfo* 1075 InstanceKlass* buffered_klass = ArchiveBuilder::current()->get_buffered_addr(info._klass); 1076 RunTimeClassInfo::set_for(buffered_klass, record); 1077 } 1078 } 1079 }; 1080 1081 void SystemDictionaryShared::write_dictionary(RunTimeSharedDictionary* dictionary, 1082 bool is_builtin) { 1083 CompactHashtableStats stats; 1084 dictionary->reset(); 1085 CompactHashtableWriter writer(_dumptime_table->count_of(is_builtin), &stats); 1086 CopySharedClassInfoToArchive copy(&writer, is_builtin); 1087 assert_lock_strong(DumpTimeTable_lock); 1088 _dumptime_table->iterate_all_live_classes(©); 1089 writer.dump(dictionary, is_builtin ? "builtin dictionary" : "unregistered dictionary"); 1090 } 1091 1092 void SystemDictionaryShared::write_to_archive(bool is_static_archive) { 1093 ArchiveInfo* archive = get_archive(is_static_archive); 1094 1095 write_dictionary(&archive->_builtin_dictionary, true); 1096 write_dictionary(&archive->_unregistered_dictionary, false); 1097 if (CDSConfig::is_dumping_lambdas_in_legacy_mode()) { 1098 LambdaProxyClassDictionary::write_dictionary(is_static_archive); 1099 } else { 1100 LambdaProxyClassDictionary::reset_dictionary(is_static_archive); 1101 } 1102 } 1103 1104 void SystemDictionaryShared::serialize_dictionary_headers(SerializeClosure* soc, 1105 bool is_static_archive) { 1106 ArchiveInfo* archive = get_archive(is_static_archive); 1107 1108 archive->_builtin_dictionary.serialize_header(soc); 1109 archive->_unregistered_dictionary.serialize_header(soc); 1110 LambdaProxyClassDictionary::serialize(soc, is_static_archive); 1111 } 1112 1113 void SystemDictionaryShared::serialize_vm_classes(SerializeClosure* soc) { 1114 for (auto id : EnumRange<vmClassID>{}) { 1115 soc->do_ptr(vmClasses::klass_addr_at(id)); 1116 } 1117 } 1118 1119 const RunTimeClassInfo* 1120 SystemDictionaryShared::find_record(RunTimeSharedDictionary* static_dict, RunTimeSharedDictionary* dynamic_dict, Symbol* name) { 1121 if (!CDSConfig::is_using_archive() || !name->is_shared()) { 1122 // The names of all shared classes must also be a shared Symbol. 1123 return nullptr; 1124 } 1125 1126 unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary_quick(name); 1127 const RunTimeClassInfo* record = nullptr; 1128 if (DynamicArchive::is_mapped()) { 1129 // Use the regenerated holder classes in the dynamic archive as they 1130 // have more methods than those in the base archive. 1131 if (LambdaFormInvokers::may_be_regenerated_class(name)) { 1132 record = dynamic_dict->lookup(name, hash, 0); 1133 if (record != nullptr) { 1134 return record; 1135 } 1136 } 1137 } 1138 1139 if (!MetaspaceShared::is_shared_dynamic(name)) { 1140 // The names of all shared classes in the static dict must also be in the 1141 // static archive 1142 record = static_dict->lookup(name, hash, 0); 1143 } 1144 1145 if (record == nullptr && DynamicArchive::is_mapped()) { 1146 record = dynamic_dict->lookup(name, hash, 0); 1147 } 1148 1149 return record; 1150 } 1151 1152 InstanceKlass* SystemDictionaryShared::find_builtin_class(Symbol* name) { 1153 const RunTimeClassInfo* record = find_record(&_static_archive._builtin_dictionary, 1154 &_dynamic_archive._builtin_dictionary, 1155 name); 1156 if (record != nullptr) { 1157 assert(!record->klass()->is_hidden(), "hidden class cannot be looked up by name"); 1158 DEBUG_ONLY(check_klass_after_loading(record->klass());) 1159 // We did not save the classfile data of the generated LambdaForm invoker classes, 1160 // so we cannot support CLFH for such classes. 1161 if (record->klass()->is_generated_shared_class() && JvmtiExport::should_post_class_file_load_hook()) { 1162 return nullptr; 1163 } 1164 return record->klass(); 1165 } else { 1166 return nullptr; 1167 } 1168 } 1169 1170 void SystemDictionaryShared::update_shared_entry(InstanceKlass* k, int id) { 1171 assert(CDSConfig::is_dumping_static_archive(), "class ID is used only for static dump (from classlist)"); 1172 DumpTimeClassInfo* info = get_info(k); 1173 info->_id = id; 1174 } 1175 1176 const char* SystemDictionaryShared::loader_type_for_shared_class(Klass* k) { 1177 assert(k != nullptr, "Sanity"); 1178 assert(k->is_shared(), "Must be"); 1179 assert(k->is_instance_klass(), "Must be"); 1180 InstanceKlass* ik = InstanceKlass::cast(k); 1181 if (ik->defined_by_boot_loader()) { 1182 return "boot_loader"; 1183 } else if (ik->defined_by_platform_loader()) { 1184 return "platform_loader"; 1185 } else if (ik->defined_by_app_loader()) { 1186 return "app_loader"; 1187 } else if (ik->defined_by_other_loaders()) { 1188 return "unregistered_loader"; 1189 } else { 1190 return "unknown loader"; 1191 } 1192 } 1193 1194 class SharedDictionaryPrinter : StackObj { 1195 outputStream* _st; 1196 int _index; 1197 public: 1198 SharedDictionaryPrinter(outputStream* st) : _st(st), _index(0) {} 1199 1200 void do_value(const RunTimeClassInfo* record) { 1201 ResourceMark rm; 1202 _st->print_cr("%4d: %s %s", _index++, record->klass()->external_name(), 1203 SystemDictionaryShared::loader_type_for_shared_class(record->klass())); 1204 if (record->klass()->array_klasses() != nullptr) { 1205 record->klass()->array_klasses()->cds_print_value_on(_st); 1206 _st->cr(); 1207 } 1208 } 1209 int index() const { return _index; } 1210 }; 1211 1212 void SystemDictionaryShared::ArchiveInfo::print_on(const char* prefix, 1213 outputStream* st, 1214 bool is_static_archive) { 1215 st->print_cr("%sShared Dictionary", prefix); 1216 SharedDictionaryPrinter p(st); 1217 st->print_cr("%sShared Builtin Dictionary", prefix); 1218 _builtin_dictionary.iterate(&p); 1219 st->print_cr("%sShared Unregistered Dictionary", prefix); 1220 _unregistered_dictionary.iterate(&p); 1221 LambdaProxyClassDictionary::print_on(prefix, st, p.index(), is_static_archive); 1222 } 1223 1224 void SystemDictionaryShared::ArchiveInfo::print_table_statistics(const char* prefix, 1225 outputStream* st, 1226 bool is_static_archive) { 1227 st->print_cr("%sArchve Statistics", prefix); 1228 _builtin_dictionary.print_table_statistics(st, "Builtin Shared Dictionary"); 1229 _unregistered_dictionary.print_table_statistics(st, "Unregistered Shared Dictionary"); 1230 LambdaProxyClassDictionary::print_statistics(st, is_static_archive); 1231 } 1232 1233 void SystemDictionaryShared::print_shared_archive(outputStream* st, bool is_static) { 1234 if (CDSConfig::is_using_archive()) { 1235 if (is_static) { 1236 _static_archive.print_on("", st, true); 1237 } else { 1238 if (DynamicArchive::is_mapped()) { 1239 _dynamic_archive.print_on("Dynamic ", st, false); 1240 } 1241 } 1242 } 1243 } 1244 1245 void SystemDictionaryShared::print_on(outputStream* st) { 1246 print_shared_archive(st, true); 1247 print_shared_archive(st, false); 1248 } 1249 1250 void SystemDictionaryShared::print_table_statistics(outputStream* st) { 1251 if (CDSConfig::is_using_archive()) { 1252 _static_archive.print_table_statistics("Static ", st, true); 1253 if (DynamicArchive::is_mapped()) { 1254 _dynamic_archive.print_table_statistics("Dynamic ", st, false); 1255 } 1256 } 1257 } 1258 1259 bool SystemDictionaryShared::is_dumptime_table_empty() { 1260 assert_lock_strong(DumpTimeTable_lock); 1261 _dumptime_table->update_counts(); 1262 if (_dumptime_table->count_of(true) == 0 && _dumptime_table->count_of(false) == 0){ 1263 return true; 1264 } 1265 return false; 1266 } 1267 1268 void SystemDictionaryShared::create_loader_positive_lookup_cache(TRAPS) { 1269 GrowableArray<InstanceKlass*> shared_classes_list; 1270 { 1271 // With static dumping, we have only a single Java thread (see JVM_StartThread) so 1272 // no no other threads should be loading classes. Otherwise, the code below may miss some 1273 // classes that are loaded concurrently. 1274 assert(CDSConfig::is_dumping_static_archive(), "no other threads should be loading classes"); 1275 1276 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag); 1277 _dumptime_table->iterate_all_classes_in_builtin_loaders([&](InstanceKlass* k, DumpTimeClassInfo& info) { 1278 if (!k->is_hidden() && !check_for_exclusion(k, &info)) { 1279 shared_classes_list.append(k); 1280 } 1281 } 1282 ); 1283 } 1284 1285 InstanceKlass* ik = vmClasses::Class_klass(); 1286 objArrayOop r = oopFactory::new_objArray(ik, shared_classes_list.length(), CHECK); 1287 objArrayHandle array_h(THREAD, r); 1288 1289 for (int i = 0; i < shared_classes_list.length(); i++) { 1290 oop mirror = shared_classes_list.at(i)->java_mirror(); 1291 Handle mirror_h(THREAD, mirror); 1292 array_h->obj_at_put(i, mirror_h()); 1293 } 1294 1295 TempNewSymbol method = SymbolTable::new_symbol("generatePositiveLookupCache"); 1296 TempNewSymbol signature = SymbolTable::new_symbol("([Ljava/lang/Class;)V"); 1297 1298 JavaCallArguments args(Handle(THREAD, SystemDictionary::java_system_loader())); 1299 args.push_oop(array_h); 1300 JavaValue result(T_VOID); 1301 JavaCalls::call_virtual(&result, 1302 vmClasses::jdk_internal_loader_ClassLoaders_AppClassLoader_klass(), 1303 method, 1304 signature, 1305 &args, 1306 CHECK); 1307 1308 if (HAS_PENDING_EXCEPTION) { 1309 Handle exc_handle(THREAD, PENDING_EXCEPTION); 1310 CLEAR_PENDING_EXCEPTION; 1311 ResourceMark rm(THREAD); 1312 1313 log_warning(cds)("Exception during AppClassLoader::generatePositiveLookupCache() call"); 1314 LogStreamHandle(Debug, cds) log; 1315 if (log.is_enabled()) { 1316 java_lang_Throwable::print_stack_trace(exc_handle, &log); 1317 } 1318 return; 1319 } 1320 }