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 659 if (k->is_objArray_klass()) { 660 return should_be_excluded(ObjArrayKlass::cast(k)->bottom_klass()); 661 } 662 663 if (!k->is_instance_klass()) { 664 return false; 665 } else { 666 InstanceKlass* ik = InstanceKlass::cast(k); 667 668 if (CDSConfig::is_dumping_dynamic_archive() && ik->is_shared()) { 669 // ik is already part of the static archive, so it will never be considered as excluded. 670 return false; 671 } 672 673 if (!SafepointSynchronize::is_at_safepoint() && !Thread::current()->is_Compiler_thread()) { 674 if (!ik->is_linked()) { 675 // check_for_exclusion() below doesn't link unlinked classes. We come 676 // here only when we are trying to aot-link constant pool entries, so 677 // we'd better link the class. 678 JavaThread* THREAD = JavaThread::current(); 679 ik->link_class(THREAD); 680 if (HAS_PENDING_EXCEPTION) { 681 CLEAR_PENDING_EXCEPTION; 682 return true; // linking failed -- let's exclude it 683 } 684 } 685 686 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag); 687 DumpTimeClassInfo* p = get_info_locked(ik); 688 if (p->is_excluded()) { 689 return true; 690 } 691 return check_for_exclusion(ik, p); 692 } else { 693 // No need to check for is_linked() as all eligible classes should have 694 // already been linked in MetaspaceShared::link_class_for_cds(). 695 // Can't take the lock as we are in safepoint. 696 DumpTimeClassInfo* p = _dumptime_table->get(ik); 697 if (p->is_excluded()) { 698 return true; 699 } 700 return check_for_exclusion(ik, p); 701 } 702 } 703 } 704 705 void SystemDictionaryShared::finish_exclusion_checks() { 706 if (CDSConfig::is_dumping_dynamic_archive() || CDSConfig::is_dumping_preimage_static_archive()) { 707 // Do this first -- if a base class is excluded due to duplication, 708 // all of its subclasses will also be excluded. 709 ResourceMark rm; 710 UnregisteredClassesDuplicationChecker dup_checker; 711 _dumptime_table->iterate_all_live_classes(&dup_checker); 712 dup_checker.mark_duplicated_classes(); 713 } 714 715 _dumptime_table->iterate_all_live_classes([&] (InstanceKlass* k, DumpTimeClassInfo& info) { 716 SystemDictionaryShared::check_for_exclusion(k, &info); 717 }); 718 719 _dumptime_table->update_counts(); 720 if (CDSConfig::is_dumping_lambdas_in_legacy_mode()) { 721 LambdaProxyClassDictionary::cleanup_dumptime_table(); 722 } 723 } 724 725 bool SystemDictionaryShared::is_excluded_class(InstanceKlass* k) { 726 assert(!class_loading_may_happen(), "class loading must be disabled"); 727 assert_lock_strong(DumpTimeTable_lock); 728 assert(CDSConfig::is_dumping_archive(), "sanity"); 729 DumpTimeClassInfo* p = get_info_locked(k); 730 return p->is_excluded(); 731 } 732 733 void SystemDictionaryShared::set_excluded_locked(InstanceKlass* k) { 734 assert_lock_strong(DumpTimeTable_lock); 735 assert(CDSConfig::is_dumping_archive(), "sanity"); 736 DumpTimeClassInfo* info = get_info_locked(k); 737 info->set_excluded(); 738 } 739 740 void SystemDictionaryShared::set_excluded(InstanceKlass* k) { 741 assert(CDSConfig::is_dumping_archive(), "sanity"); 742 DumpTimeClassInfo* info = get_info(k); 743 info->set_excluded(); 744 } 745 746 void SystemDictionaryShared::set_class_has_failed_verification(InstanceKlass* ik) { 747 assert(CDSConfig::is_dumping_archive(), "sanity"); 748 DumpTimeClassInfo* p = get_info(ik); 749 p->set_failed_verification(); 750 } 751 752 bool SystemDictionaryShared::has_class_failed_verification(InstanceKlass* ik) { 753 assert(CDSConfig::is_dumping_archive(), "sanity"); 754 DumpTimeClassInfo* p = _dumptime_table->get(ik); 755 return (p == nullptr) ? false : p->failed_verification(); 756 } 757 758 void SystemDictionaryShared::set_from_class_file_load_hook(InstanceKlass* ik) { 759 warn_excluded(ik, "From ClassFileLoadHook"); 760 set_excluded(ik); 761 } 762 763 void SystemDictionaryShared::dumptime_classes_do(MetaspaceClosure* it) { 764 assert_lock_strong(DumpTimeTable_lock); 765 766 auto do_klass = [&] (InstanceKlass* k, DumpTimeClassInfo& info) { 767 if (CDSConfig::is_dumping_final_static_archive() && !k->is_loaded()) { 768 assert(k->defined_by_other_loaders(), "must be"); 769 info.metaspace_pointers_do(it); 770 } else if (k->is_loader_alive() && !info.is_excluded()) { 771 info.metaspace_pointers_do(it); 772 } 773 }; 774 _dumptime_table->iterate_all_live_classes(do_klass); 775 776 if (CDSConfig::is_dumping_lambdas_in_legacy_mode()) { 777 LambdaProxyClassDictionary::dumptime_classes_do(it); 778 } 779 } 780 781 // Called from VerificationType::is_reference_assignable_from() before performing the assignability check of 782 // T1 must be assignable from T2 783 // Where: 784 // L is the class loader of <k> 785 // T1 is the type resolved by L using the name <name> 786 // T2 is the type resolved by L using the name <from_name> 787 // 788 // The meaning of (*skip_assignability_check): 789 // true: is_reference_assignable_from() should SKIP the assignability check 790 // false: is_reference_assignable_from() should COMPLETE the assignability check 791 void SystemDictionaryShared::add_verification_constraint(InstanceKlass* k, Symbol* name, 792 Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object, 793 bool* skip_assignability_check) { 794 assert(CDSConfig::is_dumping_archive(), "sanity"); 795 if (CDSConfig::is_dumping_dynamic_archive() && k->is_shared()) { 796 // k is a new class in the static archive, but one of its supertypes is an old class, so k wasn't 797 // verified during dump time. No need to record constraints as k won't be included in the dynamic archive. 798 return; 799 } 800 if (CDSConfig::is_dumping_aot_linked_classes() && is_builtin(k)) { 801 // There's no need to save verification constraints 802 // TODO -- double check the logic before integrating into mainline!! 803 return; 804 } 805 806 DumpTimeClassInfo* info = get_info(k); 807 info->add_verification_constraint(k, name, from_name, from_field_is_protected, 808 from_is_array, from_is_object); 809 810 if (CDSConfig::is_dumping_classic_static_archive() && !is_builtin(k)) { 811 // This applies ONLY to the "classic" CDS static dump, which reads the list of 812 // unregistered classes (those intended for custom class loaders) from the classlist 813 // and loads them using jdk.internal.misc.CDS$UnregisteredClassLoader. 814 // 815 // When the classlist contains an unregistered class k, the supertypes of k are also 816 // recorded in the classlist. However, the classlist does not contain information about 817 // any class X that's not a supertype of k but is needed in the verification of k. 818 // As a result, CDS$UnregisteredClassLoader will not know how to resolve X. 819 // 820 // Therefore, we tell the verifier to refrain from resolving X. Instead, X is recorded 821 // (symbolically) in the verification constraints of k. In the production run, 822 // when k is loaded, we will go through its verification constraints and resolve X to complete 823 // the is_reference_assignable_from() checks. 824 *skip_assignability_check = true; 825 } else { 826 // In all other cases, we are using an *actual* class loader to load k, so it should be able 827 // to resolve any types that are needed for the verification of k. 828 *skip_assignability_check = false; 829 } 830 } 831 832 void SystemDictionaryShared::add_enum_klass_static_field(InstanceKlass* ik, int root_index) { 833 assert(CDSConfig::is_dumping_heap(), "sanity"); 834 DumpTimeClassInfo* info = get_info_locked(ik); 835 info->add_enum_klass_static_field(root_index); 836 } 837 838 void SystemDictionaryShared::check_verification_constraints(InstanceKlass* klass, 839 TRAPS) { 840 assert(CDSConfig::is_using_archive(), "called at run time with CDS enabled only"); 841 RunTimeClassInfo* record = RunTimeClassInfo::get_for(klass); 842 843 int length = record->num_verifier_constraints(); 844 if (length > 0) { 845 for (int i = 0; i < length; i++) { 846 RunTimeClassInfo::RTVerifierConstraint* vc = record->verifier_constraint_at(i); 847 Symbol* name = vc->name(); 848 Symbol* from_name = vc->from_name(); 849 850 if (log_is_enabled(Trace, aot, verification)) { 851 ResourceMark rm(THREAD); 852 log_trace(aot, verification)("check_verification_constraint: %s: %s must be subclass of %s [0x%x]", 853 klass->external_name(), from_name->as_klass_external_name(), 854 name->as_klass_external_name(), record->verifier_constraint_flag(i)); 855 } 856 857 bool ok = VerificationType::resolve_and_check_assignability(klass, name, from_name, 858 record->from_field_is_protected(i), record->from_is_array(i), record->from_is_object(i), CHECK); 859 if (!ok) { 860 ResourceMark rm(THREAD); 861 stringStream ss; 862 863 ss.print_cr("Bad type on operand stack"); 864 ss.print_cr("Exception Details:"); 865 ss.print_cr(" Location:\n %s", klass->name()->as_C_string()); 866 ss.print_cr(" Reason:\n Type '%s' is not assignable to '%s'", 867 from_name->as_quoted_ascii(), name->as_quoted_ascii()); 868 THROW_MSG(vmSymbols::java_lang_VerifyError(), ss.as_string()); 869 } 870 } 871 } 872 } 873 874 void SystemDictionaryShared::copy_verification_constraints_from_preimage(InstanceKlass* klass) { 875 assert(CDSConfig::is_using_archive(), "called at run time with CDS enabled only"); 876 DumpTimeClassInfo* dt_info = get_info(klass); 877 RunTimeClassInfo* rt_info = RunTimeClassInfo::get_for(klass); // from preimage 878 879 int length = rt_info->num_verifier_constraints(); 880 if (length > 0) { 881 for (int i = 0; i < length; i++) { 882 RunTimeClassInfo::RTVerifierConstraint* vc = rt_info->verifier_constraint_at(i); 883 Symbol* name = vc->name(); 884 Symbol* from_name = vc->from_name(); 885 886 dt_info->add_verification_constraint(klass, name, from_name, 887 rt_info->from_field_is_protected(i), rt_info->from_is_array(i), rt_info->from_is_object(i)); 888 } 889 } 890 } 891 892 static oop get_class_loader_by(char type) { 893 if (type == (char)ClassLoader::BOOT_LOADER) { 894 return (oop)nullptr; 895 } else if (type == (char)ClassLoader::PLATFORM_LOADER) { 896 return SystemDictionary::java_platform_loader(); 897 } else { 898 assert (type == (char)ClassLoader::APP_LOADER, "Sanity"); 899 return SystemDictionary::java_system_loader(); 900 } 901 } 902 903 // Record class loader constraints that are checked inside 904 // InstanceKlass::link_class(), so that these can be checked quickly 905 // at runtime without laying out the vtable/itables. 906 void SystemDictionaryShared::record_linking_constraint(Symbol* name, InstanceKlass* klass, 907 Handle loader1, Handle loader2) { 908 // A linking constraint check is executed when: 909 // - klass extends or implements type S 910 // - klass overrides method S.M(...) with X.M 911 // - If klass defines the method M, X is 912 // the same as klass. 913 // - If klass does not define the method M, 914 // X must be a supertype of klass and X.M is 915 // a default method defined by X. 916 // - loader1 = X->class_loader() 917 // - loader2 = S->class_loader() 918 // - loader1 != loader2 919 // - M's parameter(s) include an object type T 920 // We require that 921 // - whenever loader1 and loader2 try to 922 // resolve the type T, they must always resolve to 923 // the same InstanceKlass. 924 // NOTE: type T may or may not be currently resolved in 925 // either of these two loaders. The check itself does not 926 // try to resolve T. 927 oop klass_loader = klass->class_loader(); 928 929 if (!is_system_class_loader(klass_loader) && 930 !is_platform_class_loader(klass_loader)) { 931 // If klass is loaded by system/platform loaders, we can 932 // guarantee that klass and S must be loaded by the same 933 // respective loader between dump time and run time, and 934 // the exact same check on (name, loader1, loader2) will 935 // be executed. Hence, we can cache this check and execute 936 // it at runtime without walking the vtable/itables. 937 // 938 // This cannot be guaranteed for classes loaded by other 939 // loaders, so we bail. 940 return; 941 } 942 943 assert(is_builtin(klass), "must be"); 944 assert(klass_loader != nullptr, "should not be called for boot loader"); 945 assert(loader1 != loader2, "must be"); 946 947 if (CDSConfig::is_dumping_dynamic_archive() && Thread::current()->is_VM_thread()) { 948 // We are re-laying out the vtable/itables of the *copy* of 949 // a class during the final stage of dynamic dumping. The 950 // linking constraints for this class has already been recorded. 951 return; 952 } 953 assert(!Thread::current()->is_VM_thread(), "must be"); 954 955 assert(CDSConfig::is_dumping_archive(), "sanity"); 956 DumpTimeClassInfo* info = get_info(klass); 957 info->record_linking_constraint(name, loader1, loader2); 958 } 959 960 // returns true IFF there's no need to re-initialize the i/v-tables for klass for 961 // the purpose of checking class loader constraints. 962 bool SystemDictionaryShared::check_linking_constraints(Thread* current, InstanceKlass* klass) { 963 assert(CDSConfig::is_using_archive(), "called at run time with CDS enabled only"); 964 LogTarget(Info, class, loader, constraints) log; 965 if (klass->defined_by_boot_loader()) { 966 // No class loader constraint check performed for boot classes. 967 return true; 968 } 969 if (klass->defined_by_platform_loader() || klass->defined_by_app_loader()) { 970 RunTimeClassInfo* info = RunTimeClassInfo::get_for(klass); 971 assert(info != nullptr, "Sanity"); 972 if (info->num_loader_constraints() > 0) { 973 HandleMark hm(current); 974 for (int i = 0; i < info->num_loader_constraints(); i++) { 975 RunTimeClassInfo::RTLoaderConstraint* lc = info->loader_constraint_at(i); 976 Symbol* name = lc->constraint_name(); 977 Handle loader1(current, get_class_loader_by(lc->_loader_type1)); 978 Handle loader2(current, get_class_loader_by(lc->_loader_type2)); 979 if (log.is_enabled()) { 980 ResourceMark rm(current); 981 log.print("[CDS add loader constraint for class %s symbol %s loader[0] %s loader[1] %s", 982 klass->external_name(), name->as_C_string(), 983 ClassLoaderData::class_loader_data(loader1())->loader_name_and_id(), 984 ClassLoaderData::class_loader_data(loader2())->loader_name_and_id()); 985 } 986 if (!SystemDictionary::add_loader_constraint(name, klass, loader1, loader2)) { 987 // Loader constraint violation has been found. The caller 988 // will re-layout the vtable/itables to produce the correct 989 // exception. 990 if (log.is_enabled()) { 991 log.print(" failed]"); 992 } 993 return false; 994 } 995 if (log.is_enabled()) { 996 log.print(" succeeded]"); 997 } 998 } 999 return true; // for all recorded constraints added successfully. 1000 } 1001 } 1002 if (log.is_enabled()) { 1003 ResourceMark rm(current); 1004 log.print("[CDS has not recorded loader constraint for class %s]", klass->external_name()); 1005 } 1006 return false; 1007 } 1008 1009 void SystemDictionaryShared::copy_linking_constraints_from_preimage(InstanceKlass* klass) { 1010 assert(CDSConfig::is_using_archive(), "called at run time with CDS enabled only"); 1011 JavaThread* current = JavaThread::current(); 1012 if (klass->defined_by_platform_loader() || klass->defined_by_app_loader()) { 1013 RunTimeClassInfo* rt_info = RunTimeClassInfo::get_for(klass); // from preimage 1014 1015 if (rt_info->num_loader_constraints() > 0) { 1016 for (int i = 0; i < rt_info->num_loader_constraints(); i++) { 1017 RunTimeClassInfo::RTLoaderConstraint* lc = rt_info->loader_constraint_at(i); 1018 Symbol* name = lc->constraint_name(); 1019 Handle loader1(current, get_class_loader_by(lc->_loader_type1)); 1020 Handle loader2(current, get_class_loader_by(lc->_loader_type2)); 1021 record_linking_constraint(name, klass, loader1, loader2); 1022 } 1023 } 1024 } 1025 } 1026 1027 unsigned int SystemDictionaryShared::hash_for_shared_dictionary(address ptr) { 1028 if (ArchiveBuilder::is_active() && ArchiveBuilder::current()->is_in_buffer_space(ptr)) { 1029 uintx offset = ArchiveBuilder::current()->any_to_offset(ptr); 1030 unsigned int hash = primitive_hash<uintx>(offset); 1031 DEBUG_ONLY({ 1032 if (MetaspaceObj::is_shared((const MetaspaceObj*)ptr)) { 1033 assert(hash == SystemDictionaryShared::hash_for_shared_dictionary_quick(ptr), "must be"); 1034 } 1035 }); 1036 return hash; 1037 } else { 1038 return SystemDictionaryShared::hash_for_shared_dictionary_quick(ptr); 1039 } 1040 } 1041 1042 class CopySharedClassInfoToArchive : StackObj { 1043 CompactHashtableWriter* _writer; 1044 bool _is_builtin; 1045 ArchiveBuilder *_builder; 1046 public: 1047 CopySharedClassInfoToArchive(CompactHashtableWriter* writer, 1048 bool is_builtin) 1049 : _writer(writer), _is_builtin(is_builtin), _builder(ArchiveBuilder::current()) {} 1050 1051 void do_entry(InstanceKlass* k, DumpTimeClassInfo& info) { 1052 if (!info.is_excluded() && info.is_builtin() == _is_builtin) { 1053 size_t byte_size = info.runtime_info_bytesize(); 1054 RunTimeClassInfo* record; 1055 record = (RunTimeClassInfo*)ArchiveBuilder::ro_region_alloc(byte_size); 1056 record->init(info); 1057 1058 unsigned int hash; 1059 Symbol* name = info._klass->name(); 1060 name = ArchiveBuilder::current()->get_buffered_addr(name); 1061 hash = SystemDictionaryShared::hash_for_shared_dictionary((address)name); 1062 u4 delta = _builder->buffer_to_offset_u4((address)record); 1063 if (_is_builtin && info._klass->is_hidden()) { 1064 // skip 1065 } else { 1066 _writer->add(hash, delta); 1067 } 1068 if (log_is_enabled(Trace, aot, hashtables)) { 1069 ResourceMark rm; 1070 log_trace(aot, hashtables)("%s dictionary: %s", (_is_builtin ? "builtin" : "unregistered"), info._klass->external_name()); 1071 } 1072 1073 // Save this for quick runtime lookup of InstanceKlass* -> RunTimeClassInfo* 1074 InstanceKlass* buffered_klass = ArchiveBuilder::current()->get_buffered_addr(info._klass); 1075 RunTimeClassInfo::set_for(buffered_klass, record); 1076 } 1077 } 1078 }; 1079 1080 void SystemDictionaryShared::write_dictionary(RunTimeSharedDictionary* dictionary, 1081 bool is_builtin) { 1082 CompactHashtableStats stats; 1083 dictionary->reset(); 1084 CompactHashtableWriter writer(_dumptime_table->count_of(is_builtin), &stats); 1085 CopySharedClassInfoToArchive copy(&writer, is_builtin); 1086 assert_lock_strong(DumpTimeTable_lock); 1087 _dumptime_table->iterate_all_live_classes(©); 1088 writer.dump(dictionary, is_builtin ? "builtin dictionary" : "unregistered dictionary"); 1089 } 1090 1091 void SystemDictionaryShared::write_to_archive(bool is_static_archive) { 1092 ArchiveInfo* archive = get_archive(is_static_archive); 1093 1094 write_dictionary(&archive->_builtin_dictionary, true); 1095 write_dictionary(&archive->_unregistered_dictionary, false); 1096 if (CDSConfig::is_dumping_lambdas_in_legacy_mode()) { 1097 LambdaProxyClassDictionary::write_dictionary(is_static_archive); 1098 } else { 1099 LambdaProxyClassDictionary::reset_dictionary(is_static_archive); 1100 } 1101 } 1102 1103 void SystemDictionaryShared::serialize_dictionary_headers(SerializeClosure* soc, 1104 bool is_static_archive) { 1105 ArchiveInfo* archive = get_archive(is_static_archive); 1106 1107 archive->_builtin_dictionary.serialize_header(soc); 1108 archive->_unregistered_dictionary.serialize_header(soc); 1109 LambdaProxyClassDictionary::serialize(soc, is_static_archive); 1110 } 1111 1112 void SystemDictionaryShared::serialize_vm_classes(SerializeClosure* soc) { 1113 for (auto id : EnumRange<vmClassID>{}) { 1114 soc->do_ptr(vmClasses::klass_addr_at(id)); 1115 } 1116 } 1117 1118 const RunTimeClassInfo* 1119 SystemDictionaryShared::find_record(RunTimeSharedDictionary* static_dict, RunTimeSharedDictionary* dynamic_dict, Symbol* name) { 1120 if (!CDSConfig::is_using_archive() || !name->is_shared()) { 1121 // The names of all shared classes must also be a shared Symbol. 1122 return nullptr; 1123 } 1124 1125 unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary_quick(name); 1126 const RunTimeClassInfo* record = nullptr; 1127 if (DynamicArchive::is_mapped()) { 1128 // Use the regenerated holder classes in the dynamic archive as they 1129 // have more methods than those in the base archive. 1130 if (LambdaFormInvokers::may_be_regenerated_class(name)) { 1131 record = dynamic_dict->lookup(name, hash, 0); 1132 if (record != nullptr) { 1133 return record; 1134 } 1135 } 1136 } 1137 1138 if (!MetaspaceShared::is_shared_dynamic(name)) { 1139 // The names of all shared classes in the static dict must also be in the 1140 // static archive 1141 record = static_dict->lookup(name, hash, 0); 1142 } 1143 1144 if (record == nullptr && DynamicArchive::is_mapped()) { 1145 record = dynamic_dict->lookup(name, hash, 0); 1146 } 1147 1148 return record; 1149 } 1150 1151 InstanceKlass* SystemDictionaryShared::find_builtin_class(Symbol* name) { 1152 const RunTimeClassInfo* record = find_record(&_static_archive._builtin_dictionary, 1153 &_dynamic_archive._builtin_dictionary, 1154 name); 1155 if (record != nullptr) { 1156 assert(!record->klass()->is_hidden(), "hidden class cannot be looked up by name"); 1157 DEBUG_ONLY(check_klass_after_loading(record->klass());) 1158 // We did not save the classfile data of the generated LambdaForm invoker classes, 1159 // so we cannot support CLFH for such classes. 1160 if (record->klass()->is_generated_shared_class() && JvmtiExport::should_post_class_file_load_hook()) { 1161 return nullptr; 1162 } 1163 return record->klass(); 1164 } else { 1165 return nullptr; 1166 } 1167 } 1168 1169 void SystemDictionaryShared::update_shared_entry(InstanceKlass* k, int id) { 1170 assert(CDSConfig::is_dumping_static_archive(), "class ID is used only for static dump (from classlist)"); 1171 DumpTimeClassInfo* info = get_info(k); 1172 info->_id = id; 1173 } 1174 1175 const char* SystemDictionaryShared::loader_type_for_shared_class(Klass* k) { 1176 assert(k != nullptr, "Sanity"); 1177 assert(k->is_shared(), "Must be"); 1178 assert(k->is_instance_klass(), "Must be"); 1179 InstanceKlass* ik = InstanceKlass::cast(k); 1180 if (ik->defined_by_boot_loader()) { 1181 return "boot_loader"; 1182 } else if (ik->defined_by_platform_loader()) { 1183 return "platform_loader"; 1184 } else if (ik->defined_by_app_loader()) { 1185 return "app_loader"; 1186 } else if (ik->defined_by_other_loaders()) { 1187 return "unregistered_loader"; 1188 } else { 1189 return "unknown loader"; 1190 } 1191 } 1192 1193 class SharedDictionaryPrinter : StackObj { 1194 outputStream* _st; 1195 int _index; 1196 public: 1197 SharedDictionaryPrinter(outputStream* st) : _st(st), _index(0) {} 1198 1199 void do_value(const RunTimeClassInfo* record) { 1200 ResourceMark rm; 1201 _st->print_cr("%4d: %s %s", _index++, record->klass()->external_name(), 1202 SystemDictionaryShared::loader_type_for_shared_class(record->klass())); 1203 if (record->klass()->array_klasses() != nullptr) { 1204 record->klass()->array_klasses()->cds_print_value_on(_st); 1205 _st->cr(); 1206 } 1207 } 1208 int index() const { return _index; } 1209 }; 1210 1211 void SystemDictionaryShared::ArchiveInfo::print_on(const char* prefix, 1212 outputStream* st, 1213 bool is_static_archive) { 1214 st->print_cr("%sShared Dictionary", prefix); 1215 SharedDictionaryPrinter p(st); 1216 st->print_cr("%sShared Builtin Dictionary", prefix); 1217 _builtin_dictionary.iterate(&p); 1218 st->print_cr("%sShared Unregistered Dictionary", prefix); 1219 _unregistered_dictionary.iterate(&p); 1220 LambdaProxyClassDictionary::print_on(prefix, st, p.index(), is_static_archive); 1221 } 1222 1223 void SystemDictionaryShared::ArchiveInfo::print_table_statistics(const char* prefix, 1224 outputStream* st, 1225 bool is_static_archive) { 1226 st->print_cr("%sArchve Statistics", prefix); 1227 _builtin_dictionary.print_table_statistics(st, "Builtin Shared Dictionary"); 1228 _unregistered_dictionary.print_table_statistics(st, "Unregistered Shared Dictionary"); 1229 LambdaProxyClassDictionary::print_statistics(st, is_static_archive); 1230 } 1231 1232 void SystemDictionaryShared::print_shared_archive(outputStream* st, bool is_static) { 1233 if (CDSConfig::is_using_archive()) { 1234 if (is_static) { 1235 _static_archive.print_on("", st, true); 1236 } else { 1237 if (DynamicArchive::is_mapped()) { 1238 _dynamic_archive.print_on("Dynamic ", st, false); 1239 } 1240 } 1241 } 1242 } 1243 1244 void SystemDictionaryShared::print_on(outputStream* st) { 1245 print_shared_archive(st, true); 1246 print_shared_archive(st, false); 1247 } 1248 1249 void SystemDictionaryShared::print_table_statistics(outputStream* st) { 1250 if (CDSConfig::is_using_archive()) { 1251 _static_archive.print_table_statistics("Static ", st, true); 1252 if (DynamicArchive::is_mapped()) { 1253 _dynamic_archive.print_table_statistics("Dynamic ", st, false); 1254 } 1255 } 1256 } 1257 1258 bool SystemDictionaryShared::is_dumptime_table_empty() { 1259 assert_lock_strong(DumpTimeTable_lock); 1260 _dumptime_table->update_counts(); 1261 if (_dumptime_table->count_of(true) == 0 && _dumptime_table->count_of(false) == 0){ 1262 return true; 1263 } 1264 return false; 1265 } 1266 1267 void SystemDictionaryShared::create_loader_positive_lookup_cache(TRAPS) { 1268 GrowableArray<InstanceKlass*> shared_classes_list; 1269 { 1270 // With static dumping, we have only a single Java thread (see JVM_StartThread) so 1271 // no no other threads should be loading classes. Otherwise, the code below may miss some 1272 // classes that are loaded concurrently. 1273 assert(CDSConfig::is_dumping_static_archive(), "no other threads should be loading classes"); 1274 1275 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag); 1276 _dumptime_table->iterate_all_classes_in_builtin_loaders([&](InstanceKlass* k, DumpTimeClassInfo& info) { 1277 if (!k->is_hidden() && !check_for_exclusion(k, &info)) { 1278 shared_classes_list.append(k); 1279 } 1280 } 1281 ); 1282 } 1283 1284 InstanceKlass* ik = vmClasses::Class_klass(); 1285 objArrayOop r = oopFactory::new_objArray(ik, shared_classes_list.length(), CHECK); 1286 objArrayHandle array_h(THREAD, r); 1287 1288 for (int i = 0; i < shared_classes_list.length(); i++) { 1289 oop mirror = shared_classes_list.at(i)->java_mirror(); 1290 Handle mirror_h(THREAD, mirror); 1291 array_h->obj_at_put(i, mirror_h()); 1292 } 1293 1294 TempNewSymbol method = SymbolTable::new_symbol("generatePositiveLookupCache"); 1295 TempNewSymbol signature = SymbolTable::new_symbol("([Ljava/lang/Class;)V"); 1296 1297 JavaCallArguments args(Handle(THREAD, SystemDictionary::java_system_loader())); 1298 args.push_oop(array_h); 1299 JavaValue result(T_VOID); 1300 JavaCalls::call_virtual(&result, 1301 vmClasses::jdk_internal_loader_ClassLoaders_AppClassLoader_klass(), 1302 method, 1303 signature, 1304 &args, 1305 CHECK); 1306 1307 if (HAS_PENDING_EXCEPTION) { 1308 Handle exc_handle(THREAD, PENDING_EXCEPTION); 1309 CLEAR_PENDING_EXCEPTION; 1310 ResourceMark rm(THREAD); 1311 1312 log_warning(cds)("Exception during AppClassLoader::generatePositiveLookupCache() call"); 1313 LogStreamHandle(Debug, cds) log; 1314 if (log.is_enabled()) { 1315 java_lang_Throwable::print_stack_trace(exc_handle, &log); 1316 } 1317 return; 1318 } 1319 }