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/aotClassLocation.hpp" 26 #include "cds/archiveBuilder.hpp" 27 #include "cds/archiveHeapLoader.hpp" 28 #include "cds/archiveUtils.hpp" 29 #include "cds/cdsConfig.hpp" 30 #include "cds/cdsProtectionDomain.hpp" 31 #include "cds/classListParser.hpp" 32 #include "cds/classListWriter.hpp" 33 #include "cds/dumpTimeClassInfo.inline.hpp" 34 #include "cds/dynamicArchive.hpp" 35 #include "cds/filemap.hpp" 36 #include "cds/heapShared.hpp" 37 #include "cds/metaspaceShared.hpp" 38 #include "cds/runTimeClassInfo.hpp" 39 #include "cds/unregisteredClasses.hpp" 40 #include "classfile/classFileStream.hpp" 41 #include "classfile/classLoader.hpp" 42 #include "classfile/classLoaderData.inline.hpp" 43 #include "classfile/classLoaderDataGraph.hpp" 44 #include "classfile/classLoaderExt.hpp" 45 #include "classfile/dictionary.hpp" 46 #include "classfile/javaClasses.hpp" 47 #include "classfile/javaClasses.inline.hpp" 48 #include "classfile/symbolTable.hpp" 49 #include "classfile/systemDictionary.hpp" 50 #include "classfile/systemDictionaryShared.hpp" 51 #include "classfile/verificationType.hpp" 52 #include "classfile/vmClasses.hpp" 53 #include "classfile/vmSymbols.hpp" 54 #include "interpreter/bootstrapInfo.hpp" 55 #include "jfr/jfrEvents.hpp" 56 #include "logging/log.hpp" 57 #include "logging/logStream.hpp" 58 #include "memory/allocation.hpp" 59 #include "memory/metadataFactory.hpp" 60 #include "memory/metaspaceClosure.hpp" 61 #include "memory/oopFactory.hpp" 62 #include "memory/resourceArea.hpp" 63 #include "memory/universe.hpp" 64 #include "oops/compressedKlass.inline.hpp" 65 #include "oops/instanceKlass.hpp" 66 #include "oops/klass.inline.hpp" 67 #include "oops/objArrayKlass.hpp" 68 #include "oops/objArrayOop.inline.hpp" 69 #include "oops/oop.inline.hpp" 70 #include "oops/oopHandle.inline.hpp" 71 #include "oops/typeArrayOop.inline.hpp" 72 #include "runtime/arguments.hpp" 73 #include "runtime/handles.inline.hpp" 74 #include "runtime/java.hpp" 75 #include "runtime/javaCalls.hpp" 76 #include "runtime/mutexLocker.hpp" 77 #include "utilities/resourceHash.hpp" 78 #include "utilities/stringUtils.hpp" 79 80 SystemDictionaryShared::ArchiveInfo SystemDictionaryShared::_static_archive; 81 SystemDictionaryShared::ArchiveInfo SystemDictionaryShared::_dynamic_archive; 82 83 DumpTimeSharedClassTable* SystemDictionaryShared::_dumptime_table = nullptr; 84 DumpTimeLambdaProxyClassDictionary* SystemDictionaryShared::_dumptime_lambda_proxy_class_dictionary = nullptr; 85 86 // Used by NoClassLoadingMark 87 DEBUG_ONLY(bool SystemDictionaryShared::_class_loading_may_happen = true;) 88 89 #ifdef ASSERT 90 static void check_klass_after_loading(const Klass* k) { 91 #ifdef _LP64 92 if (k != nullptr && UseCompressedClassPointers && k->needs_narrow_id()) { 93 CompressedKlassPointers::check_encodable(k); 94 } 95 #endif 96 } 97 #endif 98 99 InstanceKlass* SystemDictionaryShared::load_shared_class_for_builtin_loader( 100 Symbol* class_name, Handle class_loader, TRAPS) { 101 assert(CDSConfig::is_using_archive(), "must be"); 102 InstanceKlass* ik = find_builtin_class(class_name); 103 104 if (ik != nullptr && !ik->shared_loading_failed()) { 105 if ((SystemDictionary::is_system_class_loader(class_loader()) && ik->is_shared_app_class()) || 106 (SystemDictionary::is_platform_class_loader(class_loader()) && ik->is_shared_platform_class())) { 107 SharedClassLoadingMark slm(THREAD, ik); 108 PackageEntry* pkg_entry = CDSProtectionDomain::get_package_entry_from_class(ik, class_loader); 109 Handle protection_domain = 110 CDSProtectionDomain::init_security_info(class_loader, ik, pkg_entry, CHECK_NULL); 111 return load_shared_class(ik, class_loader, protection_domain, nullptr, pkg_entry, THREAD); 112 } 113 } 114 return nullptr; 115 } 116 117 // This function is called for loading only UNREGISTERED classes 118 InstanceKlass* SystemDictionaryShared::lookup_from_stream(Symbol* class_name, 119 Handle class_loader, 120 Handle protection_domain, 121 const ClassFileStream* cfs, 122 TRAPS) { 123 if (!CDSConfig::is_using_archive()) { 124 return nullptr; 125 } 126 if (class_name == nullptr) { // don't do this for hidden classes 127 return nullptr; 128 } 129 if (class_loader.is_null() || 130 SystemDictionary::is_system_class_loader(class_loader()) || 131 SystemDictionary::is_platform_class_loader(class_loader())) { 132 // Do nothing for the BUILTIN loaders. 133 return nullptr; 134 } 135 136 const RunTimeClassInfo* record = find_record(&_static_archive._unregistered_dictionary, 137 &_dynamic_archive._unregistered_dictionary, 138 class_name); 139 if (record == nullptr) { 140 return nullptr; 141 } 142 143 int clsfile_size = cfs->length(); 144 int clsfile_crc32 = ClassLoader::crc32(0, (const char*)cfs->buffer(), cfs->length()); 145 146 if (!record->matches(clsfile_size, clsfile_crc32)) { 147 return nullptr; 148 } 149 150 return acquire_class_for_current_thread(record->klass(), class_loader, 151 protection_domain, cfs, 152 THREAD); 153 } 154 155 InstanceKlass* SystemDictionaryShared::acquire_class_for_current_thread( 156 InstanceKlass *ik, 157 Handle class_loader, 158 Handle protection_domain, 159 const ClassFileStream *cfs, 160 TRAPS) { 161 ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(class_loader()); 162 163 { 164 MutexLocker mu(THREAD, SharedDictionary_lock); 165 if (ik->class_loader_data() != nullptr) { 166 // ik is already loaded (by this loader or by a different loader) 167 // or ik is being loaded by a different thread (by this loader or by a different loader) 168 return nullptr; 169 } 170 171 // No other thread has acquired this yet, so give it to *this thread* 172 ik->set_class_loader_data(loader_data); 173 } 174 175 // No longer holding SharedDictionary_lock 176 // No need to lock, as <ik> can be held only by a single thread. 177 loader_data->add_class(ik); 178 179 // Get the package entry. 180 PackageEntry* pkg_entry = CDSProtectionDomain::get_package_entry_from_class(ik, class_loader); 181 182 // Load and check super/interfaces, restore unshareable info 183 InstanceKlass* shared_klass = load_shared_class(ik, class_loader, protection_domain, 184 cfs, pkg_entry, THREAD); 185 if (shared_klass == nullptr || HAS_PENDING_EXCEPTION) { 186 // TODO: clean up <ik> so it can be used again 187 return nullptr; 188 } 189 190 return shared_klass; 191 } 192 193 // Guaranteed to return non-null value for non-shared classes. 194 // k must not be a shared class. 195 DumpTimeClassInfo* SystemDictionaryShared::get_info(InstanceKlass* k) { 196 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag); 197 return get_info_locked(k); 198 } 199 200 DumpTimeClassInfo* SystemDictionaryShared::get_info_locked(InstanceKlass* k) { 201 assert_lock_strong(DumpTimeTable_lock); 202 DumpTimeClassInfo* info = _dumptime_table->get_info(k); 203 assert(info != nullptr, "must be"); 204 return info; 205 } 206 207 bool SystemDictionaryShared::check_for_exclusion(InstanceKlass* k, DumpTimeClassInfo* info) { 208 if (CDSConfig::is_dumping_dynamic_archive() && MetaspaceShared::is_in_shared_metaspace(k)) { 209 // We have reached a super type that's already in the base archive. Treat it 210 // as "not excluded". 211 return false; 212 } 213 214 if (info == nullptr) { 215 info = _dumptime_table->get(k); 216 assert(info != nullptr, "supertypes of any classes in _dumptime_table must either be shared, or must also be in _dumptime_table"); 217 } 218 219 if (!info->has_checked_exclusion()) { 220 if (check_for_exclusion_impl(k)) { 221 info->set_excluded(); 222 } 223 info->set_has_checked_exclusion(); 224 } 225 226 return info->is_excluded(); 227 } 228 229 // Returns true so the caller can do: return warn_excluded("....."); 230 bool SystemDictionaryShared::warn_excluded(InstanceKlass* k, const char* reason) { 231 ResourceMark rm; 232 log_warning(cds)("Skipping %s: %s", k->name()->as_C_string(), reason); 233 return true; 234 } 235 236 bool SystemDictionaryShared::is_jfr_event_class(InstanceKlass *k) { 237 while (k) { 238 if (k->name()->equals("jdk/internal/event/Event")) { 239 return true; 240 } 241 k = k->java_super(); 242 } 243 return false; 244 } 245 246 bool SystemDictionaryShared::is_registered_lambda_proxy_class(InstanceKlass* ik) { 247 DumpTimeClassInfo* info = _dumptime_table->get(ik); 248 bool result = (info != nullptr) ? info->_is_registered_lambda_proxy : false; 249 if (result) { 250 assert(!CDSConfig::is_dumping_invokedynamic(), "only used in legacy lambda proxy support"); 251 } 252 return result; 253 } 254 255 void SystemDictionaryShared::reset_registered_lambda_proxy_class(InstanceKlass* ik) { 256 DumpTimeClassInfo* info = _dumptime_table->get(ik); 257 if (info != nullptr) { 258 info->_is_registered_lambda_proxy = false; 259 info->set_excluded(); 260 } 261 } 262 263 bool SystemDictionaryShared::is_early_klass(InstanceKlass* ik) { 264 DumpTimeClassInfo* info = _dumptime_table->get(ik); 265 return (info != nullptr) ? info->is_early_klass() : false; 266 } 267 268 bool SystemDictionaryShared::is_hidden_lambda_proxy(InstanceKlass* ik) { 269 assert(ik->is_shared(), "applicable to only a shared class"); 270 if (ik->is_hidden()) { 271 return true; 272 } else { 273 return false; 274 } 275 } 276 277 bool SystemDictionaryShared::check_for_exclusion_impl(InstanceKlass* k) { 278 if (CDSConfig::is_dumping_final_static_archive() && k->is_shared_unregistered_class() 279 && k->is_shared()) { 280 return false; // Do not exclude: unregistered classes are passed from preimage to final image. 281 } 282 283 if (k->is_in_error_state()) { 284 return warn_excluded(k, "In error state"); 285 } 286 if (k->is_scratch_class()) { 287 return warn_excluded(k, "A scratch class"); 288 } 289 if (!k->is_loaded()) { 290 return warn_excluded(k, "Not in loaded state"); 291 } 292 if (has_been_redefined(k)) { 293 return warn_excluded(k, "Has been redefined"); 294 } 295 if (!k->is_hidden() && k->shared_classpath_index() < 0 && is_builtin(k)) { 296 if (k->name()->starts_with("java/lang/invoke/BoundMethodHandle$Species_")) { 297 // This class is dynamically generated by the JDK 298 if (CDSConfig::is_dumping_aot_linked_classes()) { 299 k->set_shared_classpath_index(0); 300 } else { 301 ResourceMark rm; 302 log_info(cds)("Skipping %s because it is dynamically generated", k->name()->as_C_string()); 303 return true; // exclude without warning 304 } 305 } else { 306 // These are classes loaded from unsupported locations (such as those loaded by JVMTI native 307 // agent during dump time). 308 return warn_excluded(k, "Unsupported location"); 309 } 310 } 311 if (k->signers() != nullptr) { 312 // We cannot include signed classes in the archive because the certificates 313 // used during dump time may be different than those used during 314 // runtime (due to expiration, etc). 315 return warn_excluded(k, "Signed JAR"); 316 } 317 if (is_jfr_event_class(k)) { 318 // We cannot include JFR event classes because they need runtime-specific 319 // instrumentation in order to work with -XX:FlightRecorderOptions:retransform=false. 320 // There are only a small number of these classes, so it's not worthwhile to 321 // support them and make CDS more complicated. 322 return warn_excluded(k, "JFR event class"); 323 } 324 325 if (!k->is_linked()) { 326 if (has_class_failed_verification(k)) { 327 return warn_excluded(k, "Failed verification"); 328 } else if (CDSConfig::is_dumping_aot_linked_classes()) { 329 // Most loaded classes should have been speculatively linked by MetaspaceShared::link_class_for_cds(). 330 // However, we do not speculatively link old classes, as they are not recorded by 331 // SystemDictionaryShared::record_linking_constraint(). As a result, such an unlinked 332 // class may fail to verify in AOTLinkedClassBulkLoader::init_required_classes_for_loader(), 333 // causing the JVM to fail at bootstrap. 334 return warn_excluded(k, "Unlinked class not supported by AOTClassLinking"); 335 } else if (CDSConfig::is_dumping_preimage_static_archive()) { 336 // When dumping the final static archive, we will unconditionally load and link all 337 // classes from tje preimage. We don't want to get a VerifyError when linking this class. 338 return warn_excluded(k, "Unlinked class not supported by AOTConfiguration"); 339 } 340 } else { 341 if (!k->can_be_verified_at_dumptime()) { 342 // We have an old class that has been linked (e.g., it's been executed during 343 // dump time). This class has been verified using the old verifier, which 344 // doesn't save the verification constraints, so check_verification_constraints() 345 // won't work at runtime. 346 // As a result, we cannot store this class. It must be loaded and fully verified 347 // at runtime. 348 return warn_excluded(k, "Old class has been linked"); 349 } 350 } 351 352 InstanceKlass* super = k->java_super(); 353 if (super != nullptr && check_for_exclusion(super, nullptr)) { 354 ResourceMark rm; 355 log_warning(cds)("Skipping %s: super class %s is excluded", k->name()->as_C_string(), super->name()->as_C_string()); 356 return true; 357 } 358 359 Array<InstanceKlass*>* interfaces = k->local_interfaces(); 360 int len = interfaces->length(); 361 for (int i = 0; i < len; i++) { 362 InstanceKlass* intf = interfaces->at(i); 363 if (check_for_exclusion(intf, nullptr)) { 364 ResourceMark rm; 365 log_warning(cds)("Skipping %s: interface %s is excluded", k->name()->as_C_string(), intf->name()->as_C_string()); 366 return true; 367 } 368 } 369 370 if (k == UnregisteredClasses::UnregisteredClassLoader_klass()) { 371 ResourceMark rm; 372 log_info(cds)("Skipping %s: used only when dumping CDS archive", k->name()->as_C_string()); 373 return true; 374 } 375 376 return false; // false == k should NOT be excluded 377 } 378 379 bool SystemDictionaryShared::is_builtin_loader(ClassLoaderData* loader_data) { 380 oop class_loader = loader_data->class_loader(); 381 return (class_loader == nullptr || 382 SystemDictionary::is_system_class_loader(class_loader) || 383 SystemDictionary::is_platform_class_loader(class_loader)); 384 } 385 386 bool SystemDictionaryShared::has_platform_or_app_classes() { 387 if (FileMapInfo::current_info()->has_platform_or_app_classes()) { 388 return true; 389 } 390 if (DynamicArchive::is_mapped() && 391 FileMapInfo::dynamic_info()->has_platform_or_app_classes()) { 392 return true; 393 } 394 return false; 395 } 396 397 // The following stack shows how this code is reached: 398 // 399 // [0] SystemDictionaryShared::find_or_load_shared_class() 400 // [1] JVM_FindLoadedClass 401 // [2] java.lang.ClassLoader.findLoadedClass0() 402 // [3] java.lang.ClassLoader.findLoadedClass() 403 // [4] jdk.internal.loader.BuiltinClassLoader.loadClassOrNull() 404 // [5] jdk.internal.loader.BuiltinClassLoader.loadClass() 405 // [6] jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(), or 406 // jdk.internal.loader.ClassLoaders$PlatformClassLoader.loadClass() 407 // 408 // AppCDS supports fast class loading for these 2 built-in class loaders: 409 // jdk.internal.loader.ClassLoaders$PlatformClassLoader 410 // jdk.internal.loader.ClassLoaders$AppClassLoader 411 // with the following assumptions (based on the JDK core library source code): 412 // 413 // [a] these two loaders use the BuiltinClassLoader.loadClassOrNull() to 414 // load the named class. 415 // [b] BuiltinClassLoader.loadClassOrNull() first calls findLoadedClass(name). 416 // [c] At this point, if we can find the named class inside the 417 // shared_dictionary, we can perform further checks (see 418 // SystemDictionary::is_shared_class_visible) to ensure that this class 419 // was loaded by the same class loader during dump time. 420 // 421 // Given these assumptions, we intercept the findLoadedClass() call to invoke 422 // SystemDictionaryShared::find_or_load_shared_class() to load the shared class from 423 // the archive for the 2 built-in class loaders. This way, 424 // we can improve start-up because we avoid decoding the classfile, 425 // and avoid delegating to the parent loader. 426 // 427 // NOTE: there's a lot of assumption about the Java code. If any of that change, this 428 // needs to be redesigned. 429 430 InstanceKlass* SystemDictionaryShared::find_or_load_shared_class( 431 Symbol* name, Handle class_loader, TRAPS) { 432 InstanceKlass* k = nullptr; 433 if (CDSConfig::is_using_archive()) { 434 if (!has_platform_or_app_classes()) { 435 return nullptr; 436 } 437 438 if (SystemDictionary::is_system_class_loader(class_loader()) || 439 SystemDictionary::is_platform_class_loader(class_loader())) { 440 ClassLoaderData *loader_data = register_loader(class_loader); 441 Dictionary* dictionary = loader_data->dictionary(); 442 443 // Note: currently, find_or_load_shared_class is called only from 444 // JVM_FindLoadedClass and used for PlatformClassLoader and AppClassLoader, 445 // which are parallel-capable loaders, so a lock here is NOT taken. 446 assert(get_loader_lock_or_null(class_loader) == nullptr, "ObjectLocker not required"); 447 { 448 MutexLocker mu(THREAD, SystemDictionary_lock); 449 InstanceKlass* check = dictionary->find_class(THREAD, name); 450 if (check != nullptr) { 451 return check; 452 } 453 } 454 455 k = load_shared_class_for_builtin_loader(name, class_loader, THREAD); 456 if (k != nullptr) { 457 SharedClassLoadingMark slm(THREAD, k); 458 k = find_or_define_instance_class(name, class_loader, k, CHECK_NULL); 459 } 460 } 461 } 462 463 DEBUG_ONLY(check_klass_after_loading(k);) 464 465 return k; 466 } 467 468 class UnregisteredClassesTable : public ResourceHashtable< 469 Symbol*, InstanceKlass*, 470 15889, // prime number 471 AnyObj::C_HEAP> {}; 472 473 static UnregisteredClassesTable* _unregistered_classes_table = nullptr; 474 475 // true == class was successfully added; false == a duplicated class (with the same name) already exists. 476 bool SystemDictionaryShared::add_unregistered_class(Thread* current, InstanceKlass* klass) { 477 // We don't allow duplicated unregistered classes with the same name. 478 // We only archive the first class with that name that succeeds putting 479 // itself into the table. 480 assert(CDSConfig::is_dumping_archive() || ClassListWriter::is_enabled(), "sanity"); 481 MutexLocker ml(current, UnregisteredClassesTable_lock, Mutex::_no_safepoint_check_flag); 482 Symbol* name = klass->name(); 483 if (_unregistered_classes_table == nullptr) { 484 _unregistered_classes_table = new (mtClass)UnregisteredClassesTable(); 485 } 486 bool created; 487 InstanceKlass** v = _unregistered_classes_table->put_if_absent(name, klass, &created); 488 if (created) { 489 name->increment_refcount(); 490 } 491 return (klass == *v); 492 } 493 494 void SystemDictionaryShared::set_shared_class_misc_info(InstanceKlass* k, ClassFileStream* cfs) { 495 assert(CDSConfig::is_dumping_archive(), "sanity"); 496 assert(!is_builtin(k), "must be unregistered class"); 497 DumpTimeClassInfo* info = get_info(k); 498 info->_clsfile_size = cfs->length(); 499 info->_clsfile_crc32 = ClassLoader::crc32(0, (const char*)cfs->buffer(), cfs->length()); 500 } 501 502 void SystemDictionaryShared::initialize() { 503 if (CDSConfig::is_dumping_archive()) { 504 _dumptime_table = new (mtClass) DumpTimeSharedClassTable; 505 _dumptime_lambda_proxy_class_dictionary = 506 new (mtClass) DumpTimeLambdaProxyClassDictionary; 507 if (CDSConfig::is_dumping_heap()) { 508 HeapShared::init_dumping(); 509 } 510 } 511 } 512 513 void SystemDictionaryShared::init_dumptime_info(InstanceKlass* k) { 514 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag); 515 assert(SystemDictionaryShared::class_loading_may_happen(), "sanity"); 516 _dumptime_table->allocate_info(k); 517 } 518 519 void SystemDictionaryShared::remove_dumptime_info(InstanceKlass* k) { 520 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag); 521 _dumptime_table->remove(k); 522 } 523 524 void SystemDictionaryShared::handle_class_unloading(InstanceKlass* klass) { 525 if (CDSConfig::is_dumping_archive()) { 526 remove_dumptime_info(klass); 527 } 528 529 if (CDSConfig::is_dumping_archive() || ClassListWriter::is_enabled()) { 530 MutexLocker ml(Thread::current(), UnregisteredClassesTable_lock, Mutex::_no_safepoint_check_flag); 531 if (_unregistered_classes_table != nullptr) { 532 // Remove the class from _unregistered_classes_table: keep the entry but 533 // set it to null. This ensure no classes with the same name can be 534 // added again. 535 InstanceKlass** v = _unregistered_classes_table->get(klass->name()); 536 if (v != nullptr) { 537 *v = nullptr; 538 } 539 } 540 } else { 541 assert(_unregistered_classes_table == nullptr, "must not be used"); 542 } 543 544 if (ClassListWriter::is_enabled()) { 545 ClassListWriter cw; 546 cw.handle_class_unloading((const InstanceKlass*)klass); 547 } 548 } 549 550 void SystemDictionaryShared::init_dumptime_info_from_preimage(InstanceKlass* k) { 551 init_dumptime_info(k); 552 copy_verification_constraints_from_preimage(k); 553 copy_linking_constraints_from_preimage(k); 554 555 if (SystemDictionary::is_platform_class_loader(k->class_loader())) { 556 AOTClassLocationConfig::dumptime_set_has_platform_classes(); 557 } else if (SystemDictionary::is_system_class_loader(k->class_loader())) { 558 AOTClassLocationConfig::dumptime_set_has_app_classes(); 559 } 560 } 561 562 // Check if a class or any of its supertypes has been redefined. 563 bool SystemDictionaryShared::has_been_redefined(InstanceKlass* k) { 564 if (k->has_been_redefined()) { 565 return true; 566 } 567 if (k->java_super() != nullptr && has_been_redefined(k->java_super())) { 568 return true; 569 } 570 Array<InstanceKlass*>* interfaces = k->local_interfaces(); 571 int len = interfaces->length(); 572 for (int i = 0; i < len; i++) { 573 if (has_been_redefined(interfaces->at(i))) { 574 return true; 575 } 576 } 577 return false; 578 } 579 580 // k is a class before relocating by ArchiveBuilder 581 void SystemDictionaryShared::validate_before_archiving(InstanceKlass* k) { 582 ResourceMark rm; 583 const char* name = k->name()->as_C_string(); 584 DumpTimeClassInfo* info = _dumptime_table->get(k); 585 assert(!class_loading_may_happen(), "class loading must be disabled"); 586 guarantee(info != nullptr, "Class %s must be entered into _dumptime_table", name); 587 guarantee(!info->is_excluded(), "Should not attempt to archive excluded class %s", name); 588 if (is_builtin(k)) { 589 if (k->is_hidden()) { 590 if (!CDSConfig::is_dumping_invokedynamic()) { 591 assert(is_registered_lambda_proxy_class(k), "unexpected hidden class %s", name); 592 } 593 } 594 guarantee(!k->is_shared_unregistered_class(), 595 "Class loader type must be set for BUILTIN class %s", name); 596 597 } else { 598 guarantee(k->is_shared_unregistered_class(), 599 "Class loader type must not be set for UNREGISTERED class %s", name); 600 } 601 } 602 603 class UnregisteredClassesDuplicationChecker : StackObj { 604 GrowableArray<InstanceKlass*> _list; 605 Thread* _thread; 606 public: 607 UnregisteredClassesDuplicationChecker() : _thread(Thread::current()) {} 608 609 void do_entry(InstanceKlass* k, DumpTimeClassInfo& info) { 610 if (!SystemDictionaryShared::is_builtin(k)) { 611 _list.append(k); 612 } 613 } 614 615 static int compare_by_loader(InstanceKlass** a, InstanceKlass** b) { 616 ClassLoaderData* loader_a = a[0]->class_loader_data(); 617 ClassLoaderData* loader_b = b[0]->class_loader_data(); 618 619 if (loader_a != loader_b) { 620 return primitive_compare(loader_a, loader_b); 621 } else { 622 return primitive_compare(a[0], b[0]); 623 } 624 } 625 626 void mark_duplicated_classes() { 627 // Two loaders may load two identical or similar hierarchies of classes. If we 628 // check for duplication in random order, we may end up excluding important base classes 629 // in both hierarchies, causing most of the classes to be excluded. 630 // We sort the classes by their loaders. This way we're likely to archive 631 // all classes in the one of the two hierarchies. 632 _list.sort(compare_by_loader); 633 for (int i = 0; i < _list.length(); i++) { 634 InstanceKlass* k = _list.at(i); 635 bool i_am_first = SystemDictionaryShared::add_unregistered_class(_thread, k); 636 if (!i_am_first) { 637 SystemDictionaryShared::warn_excluded(k, "Duplicated unregistered class"); 638 SystemDictionaryShared::set_excluded_locked(k); 639 } 640 } 641 } 642 }; 643 644 // Returns true if the class should be excluded. This can be called by 645 // AOTConstantPoolResolver before or after we enter the CDS safepoint. 646 // When called before the safepoint, we need to link the class so that 647 // it can be checked by check_for_exclusion(). 648 bool SystemDictionaryShared::should_be_excluded(Klass* k) { 649 assert(CDSConfig::is_dumping_archive(), "sanity"); 650 assert(CDSConfig::current_thread_is_vm_or_dumper(), "sanity"); 651 652 if (k->is_objArray_klass()) { 653 return should_be_excluded(ObjArrayKlass::cast(k)->bottom_klass()); 654 } 655 656 if (!k->is_instance_klass()) { 657 return false; 658 } else { 659 InstanceKlass* ik = InstanceKlass::cast(k); 660 661 if (!SafepointSynchronize::is_at_safepoint()) { 662 if (!ik->is_linked()) { 663 // check_for_exclusion() below doesn't link unlinked classes. We come 664 // here only when we are trying to aot-link constant pool entries, so 665 // we'd better link the class. 666 JavaThread* THREAD = JavaThread::current(); 667 ik->link_class(THREAD); 668 if (HAS_PENDING_EXCEPTION) { 669 CLEAR_PENDING_EXCEPTION; 670 return true; // linking failed -- let's exclude it 671 } 672 } 673 674 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag); 675 DumpTimeClassInfo* p = get_info_locked(ik); 676 if (p->is_excluded()) { 677 return true; 678 } 679 return check_for_exclusion(ik, p); 680 } else { 681 // No need to check for is_linked() as all eligible classes should have 682 // already been linked in MetaspaceShared::link_class_for_cds(). 683 // Can't take the lock as we are in safepoint. 684 DumpTimeClassInfo* p = _dumptime_table->get(ik); 685 if (p->is_excluded()) { 686 return true; 687 } 688 return check_for_exclusion(ik, p); 689 } 690 } 691 } 692 693 void SystemDictionaryShared::finish_exclusion_checks() { 694 if (CDSConfig::is_dumping_dynamic_archive()) { 695 // Do this first -- if a base class is excluded due to duplication, 696 // all of its subclasses will also be excluded. 697 ResourceMark rm; 698 UnregisteredClassesDuplicationChecker dup_checker; 699 _dumptime_table->iterate_all_live_classes(&dup_checker); 700 dup_checker.mark_duplicated_classes(); 701 } 702 703 _dumptime_table->iterate_all_live_classes([&] (InstanceKlass* k, DumpTimeClassInfo& info) { 704 SystemDictionaryShared::check_for_exclusion(k, &info); 705 }); 706 707 _dumptime_table->update_counts(); 708 cleanup_lambda_proxy_class_dictionary(); 709 } 710 711 bool SystemDictionaryShared::is_excluded_class(InstanceKlass* k) { 712 assert(!class_loading_may_happen(), "class loading must be disabled"); 713 assert_lock_strong(DumpTimeTable_lock); 714 assert(CDSConfig::is_dumping_archive(), "sanity"); 715 DumpTimeClassInfo* p = get_info_locked(k); 716 return p->is_excluded(); 717 } 718 719 void SystemDictionaryShared::set_excluded_locked(InstanceKlass* k) { 720 assert_lock_strong(DumpTimeTable_lock); 721 assert(CDSConfig::is_dumping_archive(), "sanity"); 722 DumpTimeClassInfo* info = get_info_locked(k); 723 info->set_excluded(); 724 } 725 726 void SystemDictionaryShared::set_excluded(InstanceKlass* k) { 727 assert(CDSConfig::is_dumping_archive(), "sanity"); 728 DumpTimeClassInfo* info = get_info(k); 729 info->set_excluded(); 730 } 731 732 void SystemDictionaryShared::set_class_has_failed_verification(InstanceKlass* ik) { 733 assert(CDSConfig::is_dumping_archive(), "sanity"); 734 DumpTimeClassInfo* p = get_info(ik); 735 p->set_failed_verification(); 736 } 737 738 bool SystemDictionaryShared::has_class_failed_verification(InstanceKlass* ik) { 739 assert(CDSConfig::is_dumping_archive(), "sanity"); 740 DumpTimeClassInfo* p = _dumptime_table->get(ik); 741 return (p == nullptr) ? false : p->failed_verification(); 742 } 743 744 void SystemDictionaryShared::dumptime_classes_do(class MetaspaceClosure* it) { 745 assert_lock_strong(DumpTimeTable_lock); 746 747 auto do_klass = [&] (InstanceKlass* k, DumpTimeClassInfo& info) { 748 if (CDSConfig::is_dumping_final_static_archive() && !k->is_loaded()) { 749 assert(k->is_shared_unregistered_class(), "must be"); 750 info.metaspace_pointers_do(it); 751 } else if (k->is_loader_alive() && !info.is_excluded()) { 752 info.metaspace_pointers_do(it); 753 } 754 }; 755 _dumptime_table->iterate_all_live_classes(do_klass); 756 757 auto do_lambda = [&] (LambdaProxyClassKey& key, DumpTimeLambdaProxyClassInfo& info) { 758 if (key.caller_ik()->is_loader_alive()) { 759 info.metaspace_pointers_do(it); 760 key.metaspace_pointers_do(it); 761 } 762 }; 763 _dumptime_lambda_proxy_class_dictionary->iterate_all(do_lambda); 764 } 765 766 bool SystemDictionaryShared::add_verification_constraint(InstanceKlass* k, Symbol* name, 767 Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object) { 768 assert(CDSConfig::is_dumping_archive(), "sanity"); 769 DumpTimeClassInfo* info = get_info(k); 770 info->add_verification_constraint(k, name, from_name, from_field_is_protected, 771 from_is_array, from_is_object); 772 773 if (CDSConfig::is_dumping_dynamic_archive()) { 774 // For dynamic dumping, we can resolve all the constraint classes for all class loaders during 775 // the initial run prior to creating the archive before vm exit. We will also perform verification 776 // check when running with the archive. 777 return false; 778 } else { 779 if (is_builtin(k)) { 780 // For builtin class loaders, we can try to complete the verification check at dump time, 781 // because we can resolve all the constraint classes. We will also perform verification check 782 // when running with the archive. 783 return false; 784 } else { 785 // For non-builtin class loaders, we cannot complete the verification check at dump time, 786 // because at dump time we don't know how to resolve classes for such loaders. 787 return true; 788 } 789 } 790 } 791 792 void SystemDictionaryShared::add_enum_klass_static_field(InstanceKlass* ik, int root_index) { 793 assert(CDSConfig::is_dumping_heap(), "sanity"); 794 DumpTimeClassInfo* info = get_info_locked(ik); 795 info->add_enum_klass_static_field(root_index); 796 } 797 798 void SystemDictionaryShared::add_to_dump_time_lambda_proxy_class_dictionary(LambdaProxyClassKey& key, 799 InstanceKlass* proxy_klass) { 800 assert_lock_strong(DumpTimeTable_lock); 801 802 bool created; 803 DumpTimeLambdaProxyClassInfo* info = _dumptime_lambda_proxy_class_dictionary->put_if_absent(key, &created); 804 info->add_proxy_klass(proxy_klass); 805 if (created) { 806 ++_dumptime_lambda_proxy_class_dictionary->_count; 807 } 808 } 809 810 void SystemDictionaryShared::add_lambda_proxy_class(InstanceKlass* caller_ik, 811 InstanceKlass* lambda_ik, 812 Symbol* invoked_name, 813 Symbol* invoked_type, 814 Symbol* method_type, 815 Method* member_method, 816 Symbol* instantiated_method_type, 817 TRAPS) { 818 if (CDSConfig::is_dumping_invokedynamic()) { 819 // The lambda proxy classes will be stored as part of aot-resolved constant pool entries. 820 // There's no need to remember them in a separate table. 821 return; 822 } 823 if (CDSConfig::is_dumping_preimage_static_archive()) { 824 // Information about lambda proxies are recorded in FinalImageRecipes. 825 return; 826 } 827 828 assert(caller_ik->class_loader() == lambda_ik->class_loader(), "mismatched class loader"); 829 assert(caller_ik->class_loader_data() == lambda_ik->class_loader_data(), "mismatched class loader data"); 830 assert(java_lang_Class::class_data(lambda_ik->java_mirror()) == nullptr, "must not have class data"); 831 832 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag); 833 834 lambda_ik->assign_class_loader_type(); 835 lambda_ik->set_shared_classpath_index(caller_ik->shared_classpath_index()); 836 InstanceKlass* nest_host = caller_ik->nest_host(CHECK); 837 assert(nest_host != nullptr, "unexpected nullptr nest_host"); 838 839 DumpTimeClassInfo* info = _dumptime_table->get(lambda_ik); 840 if (info != nullptr && !lambda_ik->is_non_strong_hidden() && is_builtin(lambda_ik) && is_builtin(caller_ik) 841 // Don't include the lambda proxy if its nest host is not in the "linked" state. 842 && nest_host->is_linked()) { 843 // Set _is_registered_lambda_proxy in DumpTimeClassInfo so that the lambda_ik 844 // won't be excluded during dumping of shared archive. 845 info->_is_registered_lambda_proxy = true; 846 info->set_nest_host(nest_host); 847 848 LambdaProxyClassKey key(caller_ik, 849 invoked_name, 850 invoked_type, 851 method_type, 852 member_method, 853 instantiated_method_type); 854 add_to_dump_time_lambda_proxy_class_dictionary(key, lambda_ik); 855 } 856 } 857 858 InstanceKlass* SystemDictionaryShared::get_shared_lambda_proxy_class(InstanceKlass* caller_ik, 859 Symbol* invoked_name, 860 Symbol* invoked_type, 861 Symbol* method_type, 862 Method* member_method, 863 Symbol* instantiated_method_type) { 864 if (CDSConfig::is_dumping_final_static_archive()) { 865 return nullptr; 866 } 867 868 assert(caller_ik != nullptr, "sanity"); 869 assert(invoked_name != nullptr, "sanity"); 870 assert(invoked_type != nullptr, "sanity"); 871 assert(method_type != nullptr, "sanity"); 872 assert(instantiated_method_type != nullptr, "sanity"); 873 874 if (!caller_ik->is_shared() || 875 !invoked_name->is_shared() || 876 !invoked_type->is_shared() || 877 !method_type->is_shared() || 878 (member_method != nullptr && !member_method->is_shared()) || 879 !instantiated_method_type->is_shared()) { 880 // These can't be represented as u4 offset, but we wouldn't have archived a lambda proxy in this case anyway. 881 return nullptr; 882 } 883 884 MutexLocker ml(CDSLambda_lock, Mutex::_no_safepoint_check_flag); 885 RunTimeLambdaProxyClassKey key = 886 RunTimeLambdaProxyClassKey::init_for_runtime(caller_ik, invoked_name, invoked_type, 887 method_type, member_method, instantiated_method_type); 888 889 // Try to retrieve the lambda proxy class from static archive. 890 const RunTimeLambdaProxyClassInfo* info = _static_archive.lookup_lambda_proxy_class(&key); 891 InstanceKlass* proxy_klass = retrieve_lambda_proxy_class(info); 892 if (proxy_klass == nullptr) { 893 if (info != nullptr && log_is_enabled(Debug, cds)) { 894 ResourceMark rm; 895 log_debug(cds)("Used all static archived lambda proxy classes for: %s %s%s", 896 caller_ik->external_name(), invoked_name->as_C_string(), invoked_type->as_C_string()); 897 } 898 } else { 899 return proxy_klass; 900 } 901 902 // Retrieving from static archive is unsuccessful, try dynamic archive. 903 info = _dynamic_archive.lookup_lambda_proxy_class(&key); 904 proxy_klass = retrieve_lambda_proxy_class(info); 905 if (proxy_klass == nullptr) { 906 if (info != nullptr && log_is_enabled(Debug, cds)) { 907 ResourceMark rm; 908 log_debug(cds)("Used all dynamic archived lambda proxy classes for: %s %s%s", 909 caller_ik->external_name(), invoked_name->as_C_string(), invoked_type->as_C_string()); 910 } 911 } 912 return proxy_klass; 913 } 914 915 InstanceKlass* SystemDictionaryShared::retrieve_lambda_proxy_class(const RunTimeLambdaProxyClassInfo* info) { 916 InstanceKlass* proxy_klass = nullptr; 917 if (info != nullptr) { 918 InstanceKlass* curr_klass = info->proxy_klass_head(); 919 InstanceKlass* prev_klass = curr_klass; 920 if (curr_klass->lambda_proxy_is_available()) { 921 while (curr_klass->next_link() != nullptr) { 922 prev_klass = curr_klass; 923 curr_klass = InstanceKlass::cast(curr_klass->next_link()); 924 } 925 assert(curr_klass->is_hidden(), "must be"); 926 assert(curr_klass->lambda_proxy_is_available(), "must be"); 927 928 prev_klass->set_next_link(nullptr); 929 proxy_klass = curr_klass; 930 proxy_klass->clear_lambda_proxy_is_available(); 931 if (log_is_enabled(Debug, cds)) { 932 ResourceMark rm; 933 log_debug(cds)("Loaded lambda proxy: %s ", proxy_klass->external_name()); 934 } 935 } 936 } 937 return proxy_klass; 938 } 939 940 InstanceKlass* SystemDictionaryShared::get_shared_nest_host(InstanceKlass* lambda_ik) { 941 assert(!CDSConfig::is_dumping_static_archive() && CDSConfig::is_using_archive(), "called at run time with CDS enabled only"); 942 RunTimeClassInfo* record = RunTimeClassInfo::get_for(lambda_ik); 943 return record->nest_host(); 944 } 945 946 InstanceKlass* SystemDictionaryShared::prepare_shared_lambda_proxy_class(InstanceKlass* lambda_ik, 947 InstanceKlass* caller_ik, TRAPS) { 948 Handle class_loader(THREAD, caller_ik->class_loader()); 949 Handle protection_domain; 950 PackageEntry* pkg_entry = caller_ik->package(); 951 if (caller_ik->class_loader() != nullptr) { 952 protection_domain = CDSProtectionDomain::init_security_info(class_loader, caller_ik, pkg_entry, CHECK_NULL); 953 } 954 955 InstanceKlass* shared_nest_host = get_shared_nest_host(lambda_ik); 956 assert(shared_nest_host != nullptr, "unexpected nullptr _nest_host"); 957 958 InstanceKlass* loaded_lambda = 959 SystemDictionary::load_shared_lambda_proxy_class(lambda_ik, class_loader, protection_domain, pkg_entry, CHECK_NULL); 960 961 if (loaded_lambda == nullptr) { 962 return nullptr; 963 } 964 965 // Ensures the nest host is the same as the lambda proxy's 966 // nest host recorded at dump time. 967 InstanceKlass* nest_host = caller_ik->nest_host(THREAD); 968 assert(nest_host == shared_nest_host, "mismatched nest host"); 969 970 EventClassLoad class_load_start_event; 971 972 // Add to class hierarchy, and do possible deoptimizations. 973 loaded_lambda->add_to_hierarchy(THREAD); 974 // But, do not add to dictionary. 975 976 loaded_lambda->link_class(CHECK_NULL); 977 // notify jvmti 978 if (JvmtiExport::should_post_class_load()) { 979 JvmtiExport::post_class_load(THREAD, loaded_lambda); 980 } 981 if (class_load_start_event.should_commit()) { 982 SystemDictionary::post_class_load_event(&class_load_start_event, loaded_lambda, ClassLoaderData::class_loader_data(class_loader())); 983 } 984 985 loaded_lambda->initialize(CHECK_NULL); 986 987 return loaded_lambda; 988 } 989 990 void SystemDictionaryShared::check_verification_constraints(InstanceKlass* klass, 991 TRAPS) { 992 assert(CDSConfig::is_using_archive(), "called at run time with CDS enabled only"); 993 RunTimeClassInfo* record = RunTimeClassInfo::get_for(klass); 994 995 int length = record->num_verifier_constraints(); 996 if (length > 0) { 997 for (int i = 0; i < length; i++) { 998 RunTimeClassInfo::RTVerifierConstraint* vc = record->verifier_constraint_at(i); 999 Symbol* name = vc->name(); 1000 Symbol* from_name = vc->from_name(); 1001 1002 if (log_is_enabled(Trace, cds, verification)) { 1003 ResourceMark rm(THREAD); 1004 log_trace(cds, verification)("check_verification_constraint: %s: %s must be subclass of %s [0x%x]", 1005 klass->external_name(), from_name->as_klass_external_name(), 1006 name->as_klass_external_name(), record->verifier_constraint_flag(i)); 1007 } 1008 1009 bool ok = VerificationType::resolve_and_check_assignability(klass, name, from_name, 1010 record->from_field_is_protected(i), record->from_is_array(i), record->from_is_object(i), CHECK); 1011 if (!ok) { 1012 ResourceMark rm(THREAD); 1013 stringStream ss; 1014 1015 ss.print_cr("Bad type on operand stack"); 1016 ss.print_cr("Exception Details:"); 1017 ss.print_cr(" Location:\n %s", klass->name()->as_C_string()); 1018 ss.print_cr(" Reason:\n Type '%s' is not assignable to '%s'", 1019 from_name->as_quoted_ascii(), name->as_quoted_ascii()); 1020 THROW_MSG(vmSymbols::java_lang_VerifyError(), ss.as_string()); 1021 } 1022 } 1023 } 1024 } 1025 1026 void SystemDictionaryShared::copy_verification_constraints_from_preimage(InstanceKlass* klass) { 1027 assert(CDSConfig::is_using_archive(), "called at run time with CDS enabled only"); 1028 DumpTimeClassInfo* dt_info = get_info(klass); 1029 RunTimeClassInfo* rt_info = RunTimeClassInfo::get_for(klass); // from preimage 1030 1031 int length = rt_info->num_verifier_constraints(); 1032 if (length > 0) { 1033 for (int i = 0; i < length; i++) { 1034 RunTimeClassInfo::RTVerifierConstraint* vc = rt_info->verifier_constraint_at(i); 1035 Symbol* name = vc->name(); 1036 Symbol* from_name = vc->from_name(); 1037 1038 dt_info->add_verification_constraint(klass, name, from_name, 1039 rt_info->from_field_is_protected(i), rt_info->from_is_array(i), rt_info->from_is_object(i)); 1040 } 1041 } 1042 } 1043 1044 static oop get_class_loader_by(char type) { 1045 if (type == (char)ClassLoader::BOOT_LOADER) { 1046 return (oop)nullptr; 1047 } else if (type == (char)ClassLoader::PLATFORM_LOADER) { 1048 return SystemDictionary::java_platform_loader(); 1049 } else { 1050 assert (type == (char)ClassLoader::APP_LOADER, "Sanity"); 1051 return SystemDictionary::java_system_loader(); 1052 } 1053 } 1054 1055 // Record class loader constraints that are checked inside 1056 // InstanceKlass::link_class(), so that these can be checked quickly 1057 // at runtime without laying out the vtable/itables. 1058 void SystemDictionaryShared::record_linking_constraint(Symbol* name, InstanceKlass* klass, 1059 Handle loader1, Handle loader2) { 1060 // A linking constraint check is executed when: 1061 // - klass extends or implements type S 1062 // - klass overrides method S.M(...) with X.M 1063 // - If klass defines the method M, X is 1064 // the same as klass. 1065 // - If klass does not define the method M, 1066 // X must be a supertype of klass and X.M is 1067 // a default method defined by X. 1068 // - loader1 = X->class_loader() 1069 // - loader2 = S->class_loader() 1070 // - loader1 != loader2 1071 // - M's parameter(s) include an object type T 1072 // We require that 1073 // - whenever loader1 and loader2 try to 1074 // resolve the type T, they must always resolve to 1075 // the same InstanceKlass. 1076 // NOTE: type T may or may not be currently resolved in 1077 // either of these two loaders. The check itself does not 1078 // try to resolve T. 1079 oop klass_loader = klass->class_loader(); 1080 1081 if (!is_system_class_loader(klass_loader) && 1082 !is_platform_class_loader(klass_loader)) { 1083 // If klass is loaded by system/platform loaders, we can 1084 // guarantee that klass and S must be loaded by the same 1085 // respective loader between dump time and run time, and 1086 // the exact same check on (name, loader1, loader2) will 1087 // be executed. Hence, we can cache this check and execute 1088 // it at runtime without walking the vtable/itables. 1089 // 1090 // This cannot be guaranteed for classes loaded by other 1091 // loaders, so we bail. 1092 return; 1093 } 1094 1095 assert(is_builtin(klass), "must be"); 1096 assert(klass_loader != nullptr, "should not be called for boot loader"); 1097 assert(loader1 != loader2, "must be"); 1098 1099 if (CDSConfig::is_dumping_dynamic_archive() && Thread::current()->is_VM_thread()) { 1100 // We are re-laying out the vtable/itables of the *copy* of 1101 // a class during the final stage of dynamic dumping. The 1102 // linking constraints for this class has already been recorded. 1103 return; 1104 } 1105 assert(!Thread::current()->is_VM_thread(), "must be"); 1106 1107 assert(CDSConfig::is_dumping_archive(), "sanity"); 1108 DumpTimeClassInfo* info = get_info(klass); 1109 info->record_linking_constraint(name, loader1, loader2); 1110 } 1111 1112 // returns true IFF there's no need to re-initialize the i/v-tables for klass for 1113 // the purpose of checking class loader constraints. 1114 bool SystemDictionaryShared::check_linking_constraints(Thread* current, InstanceKlass* klass) { 1115 assert(CDSConfig::is_using_archive(), "called at run time with CDS enabled only"); 1116 LogTarget(Info, class, loader, constraints) log; 1117 if (klass->is_shared_boot_class()) { 1118 // No class loader constraint check performed for boot classes. 1119 return true; 1120 } 1121 if (klass->is_shared_platform_class() || klass->is_shared_app_class()) { 1122 RunTimeClassInfo* info = RunTimeClassInfo::get_for(klass); 1123 assert(info != nullptr, "Sanity"); 1124 if (info->num_loader_constraints() > 0) { 1125 HandleMark hm(current); 1126 for (int i = 0; i < info->num_loader_constraints(); i++) { 1127 RunTimeClassInfo::RTLoaderConstraint* lc = info->loader_constraint_at(i); 1128 Symbol* name = lc->constraint_name(); 1129 Handle loader1(current, get_class_loader_by(lc->_loader_type1)); 1130 Handle loader2(current, get_class_loader_by(lc->_loader_type2)); 1131 if (log.is_enabled()) { 1132 ResourceMark rm(current); 1133 log.print("[CDS add loader constraint for class %s symbol %s loader[0] %s loader[1] %s", 1134 klass->external_name(), name->as_C_string(), 1135 ClassLoaderData::class_loader_data(loader1())->loader_name_and_id(), 1136 ClassLoaderData::class_loader_data(loader2())->loader_name_and_id()); 1137 } 1138 if (!SystemDictionary::add_loader_constraint(name, klass, loader1, loader2)) { 1139 // Loader constraint violation has been found. The caller 1140 // will re-layout the vtable/itables to produce the correct 1141 // exception. 1142 if (log.is_enabled()) { 1143 log.print(" failed]"); 1144 } 1145 return false; 1146 } 1147 if (log.is_enabled()) { 1148 log.print(" succeeded]"); 1149 } 1150 } 1151 return true; // for all recorded constraints added successfully. 1152 } 1153 } 1154 if (log.is_enabled()) { 1155 ResourceMark rm(current); 1156 log.print("[CDS has not recorded loader constraint for class %s]", klass->external_name()); 1157 } 1158 return false; 1159 } 1160 1161 void SystemDictionaryShared::copy_linking_constraints_from_preimage(InstanceKlass* klass) { 1162 assert(CDSConfig::is_using_archive(), "called at run time with CDS enabled only"); 1163 JavaThread* current = JavaThread::current(); 1164 if (klass->is_shared_platform_class() || klass->is_shared_app_class()) { 1165 RunTimeClassInfo* rt_info = RunTimeClassInfo::get_for(klass); // from preimage 1166 1167 if (rt_info->num_loader_constraints() > 0) { 1168 for (int i = 0; i < rt_info->num_loader_constraints(); i++) { 1169 RunTimeClassInfo::RTLoaderConstraint* lc = rt_info->loader_constraint_at(i); 1170 Symbol* name = lc->constraint_name(); 1171 Handle loader1(current, get_class_loader_by(lc->_loader_type1)); 1172 Handle loader2(current, get_class_loader_by(lc->_loader_type2)); 1173 record_linking_constraint(name, klass, loader1, loader2); 1174 } 1175 } 1176 } 1177 } 1178 1179 bool SystemDictionaryShared::is_supported_invokedynamic(BootstrapInfo* bsi) { 1180 LogTarget(Debug, cds, lambda) log; 1181 if (bsi->arg_values() == nullptr || !bsi->arg_values()->is_objArray()) { 1182 if (log.is_enabled()) { 1183 LogStream log_stream(log); 1184 log.print("bsi check failed"); 1185 log.print(" bsi->arg_values().not_null() %d", bsi->arg_values().not_null()); 1186 if (bsi->arg_values().not_null()) { 1187 log.print(" bsi->arg_values()->is_objArray() %d", bsi->arg_values()->is_objArray()); 1188 bsi->print_msg_on(&log_stream); 1189 } 1190 } 1191 return false; 1192 } 1193 1194 Handle bsm = bsi->bsm(); 1195 if (bsm.is_null() || !java_lang_invoke_DirectMethodHandle::is_instance(bsm())) { 1196 if (log.is_enabled()) { 1197 log.print("bsm check failed"); 1198 log.print(" bsm.is_null() %d", bsm.is_null()); 1199 log.print(" java_lang_invoke_DirectMethodHandle::is_instance(bsm()) %d", 1200 java_lang_invoke_DirectMethodHandle::is_instance(bsm())); 1201 } 1202 return false; 1203 } 1204 1205 oop mn = java_lang_invoke_DirectMethodHandle::member(bsm()); 1206 Method* method = java_lang_invoke_MemberName::vmtarget(mn); 1207 if (method->klass_name()->equals("java/lang/invoke/LambdaMetafactory") && 1208 method->name()->equals("metafactory") && 1209 method->signature()->equals("(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;" 1210 "Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;" 1211 "Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;")) { 1212 return true; 1213 } else { 1214 if (log.is_enabled()) { 1215 ResourceMark rm; 1216 log.print("method check failed"); 1217 log.print(" klass_name() %s", method->klass_name()->as_C_string()); 1218 log.print(" name() %s", method->name()->as_C_string()); 1219 log.print(" signature() %s", method->signature()->as_C_string()); 1220 } 1221 } 1222 1223 return false; 1224 } 1225 1226 class EstimateSizeForArchive : StackObj { 1227 size_t _shared_class_info_size; 1228 int _num_builtin_klasses; 1229 int _num_unregistered_klasses; 1230 1231 public: 1232 EstimateSizeForArchive() { 1233 _shared_class_info_size = 0; 1234 _num_builtin_klasses = 0; 1235 _num_unregistered_klasses = 0; 1236 } 1237 1238 void do_entry(InstanceKlass* k, DumpTimeClassInfo& info) { 1239 if (!info.is_excluded()) { 1240 size_t byte_size = info.runtime_info_bytesize(); 1241 _shared_class_info_size += align_up(byte_size, SharedSpaceObjectAlignment); 1242 } 1243 } 1244 1245 size_t total() { 1246 return _shared_class_info_size; 1247 } 1248 }; 1249 1250 size_t SystemDictionaryShared::estimate_size_for_archive() { 1251 EstimateSizeForArchive est; 1252 _dumptime_table->iterate_all_live_classes(&est); 1253 size_t total_size = est.total() + 1254 CompactHashtableWriter::estimate_size(_dumptime_table->count_of(true)) + 1255 CompactHashtableWriter::estimate_size(_dumptime_table->count_of(false)); 1256 1257 size_t bytesize = align_up(sizeof(RunTimeLambdaProxyClassInfo), SharedSpaceObjectAlignment); 1258 total_size += 1259 (bytesize * _dumptime_lambda_proxy_class_dictionary->_count) + 1260 CompactHashtableWriter::estimate_size(_dumptime_lambda_proxy_class_dictionary->_count); 1261 1262 return total_size; 1263 } 1264 1265 unsigned int SystemDictionaryShared::hash_for_shared_dictionary(address ptr) { 1266 if (ArchiveBuilder::is_active()) { 1267 uintx offset = ArchiveBuilder::current()->any_to_offset(ptr); 1268 unsigned int hash = primitive_hash<uintx>(offset); 1269 DEBUG_ONLY({ 1270 if (MetaspaceObj::is_shared((const MetaspaceObj*)ptr)) { 1271 assert(hash == SystemDictionaryShared::hash_for_shared_dictionary_quick(ptr), "must be"); 1272 } 1273 }); 1274 return hash; 1275 } else { 1276 return SystemDictionaryShared::hash_for_shared_dictionary_quick(ptr); 1277 } 1278 } 1279 1280 class CopyLambdaProxyClassInfoToArchive : StackObj { 1281 CompactHashtableWriter* _writer; 1282 ArchiveBuilder* _builder; 1283 public: 1284 CopyLambdaProxyClassInfoToArchive(CompactHashtableWriter* writer) 1285 : _writer(writer), _builder(ArchiveBuilder::current()) {} 1286 bool do_entry(LambdaProxyClassKey& key, DumpTimeLambdaProxyClassInfo& info) { 1287 // In static dump, info._proxy_klasses->at(0) is already relocated to point to the archived class 1288 // (not the original class). 1289 ResourceMark rm; 1290 log_info(cds,dynamic)("Archiving hidden %s", info._proxy_klasses->at(0)->external_name()); 1291 size_t byte_size = sizeof(RunTimeLambdaProxyClassInfo); 1292 RunTimeLambdaProxyClassInfo* runtime_info = 1293 (RunTimeLambdaProxyClassInfo*)ArchiveBuilder::ro_region_alloc(byte_size); 1294 runtime_info->init(key, info); 1295 unsigned int hash = runtime_info->hash(); 1296 u4 delta = _builder->any_to_offset_u4((void*)runtime_info); 1297 _writer->add(hash, delta); 1298 return true; 1299 } 1300 }; 1301 1302 class AdjustLambdaProxyClassInfo : StackObj { 1303 public: 1304 AdjustLambdaProxyClassInfo() {} 1305 bool do_entry(LambdaProxyClassKey& key, DumpTimeLambdaProxyClassInfo& info) { 1306 int len = info._proxy_klasses->length(); 1307 InstanceKlass* last_buff_k = nullptr; 1308 1309 for (int i = len - 1; i >= 0; i--) { 1310 InstanceKlass* orig_k = info._proxy_klasses->at(i); 1311 InstanceKlass* buff_k = ArchiveBuilder::current()->get_buffered_addr(orig_k); 1312 assert(ArchiveBuilder::current()->is_in_buffer_space(buff_k), "must be"); 1313 buff_k->set_lambda_proxy_is_available(); 1314 buff_k->set_next_link(last_buff_k); 1315 if (last_buff_k != nullptr) { 1316 ArchivePtrMarker::mark_pointer(buff_k->next_link_addr()); 1317 } 1318 last_buff_k = buff_k; 1319 } 1320 1321 return true; 1322 } 1323 }; 1324 1325 class CopySharedClassInfoToArchive : StackObj { 1326 CompactHashtableWriter* _writer; 1327 bool _is_builtin; 1328 ArchiveBuilder *_builder; 1329 public: 1330 CopySharedClassInfoToArchive(CompactHashtableWriter* writer, 1331 bool is_builtin) 1332 : _writer(writer), _is_builtin(is_builtin), _builder(ArchiveBuilder::current()) {} 1333 1334 void do_entry(InstanceKlass* k, DumpTimeClassInfo& info) { 1335 if (!info.is_excluded() && info.is_builtin() == _is_builtin) { 1336 size_t byte_size = info.runtime_info_bytesize(); 1337 RunTimeClassInfo* record; 1338 record = (RunTimeClassInfo*)ArchiveBuilder::ro_region_alloc(byte_size); 1339 record->init(info); 1340 1341 unsigned int hash; 1342 Symbol* name = info._klass->name(); 1343 name = ArchiveBuilder::current()->get_buffered_addr(name); 1344 hash = SystemDictionaryShared::hash_for_shared_dictionary((address)name); 1345 u4 delta = _builder->buffer_to_offset_u4((address)record); 1346 if (_is_builtin && info._klass->is_hidden()) { 1347 // skip 1348 } else { 1349 _writer->add(hash, delta); 1350 } 1351 if (log_is_enabled(Trace, cds, hashtables)) { 1352 ResourceMark rm; 1353 log_trace(cds,hashtables)("%s dictionary: %s", (_is_builtin ? "builtin" : "unregistered"), info._klass->external_name()); 1354 } 1355 1356 // Save this for quick runtime lookup of InstanceKlass* -> RunTimeClassInfo* 1357 InstanceKlass* buffered_klass = ArchiveBuilder::current()->get_buffered_addr(info._klass); 1358 RunTimeClassInfo::set_for(buffered_klass, record); 1359 } 1360 } 1361 }; 1362 1363 void SystemDictionaryShared::write_lambda_proxy_class_dictionary(LambdaProxyClassDictionary *dictionary) { 1364 CompactHashtableStats stats; 1365 dictionary->reset(); 1366 CompactHashtableWriter writer(_dumptime_lambda_proxy_class_dictionary->_count, &stats); 1367 CopyLambdaProxyClassInfoToArchive copy(&writer); 1368 _dumptime_lambda_proxy_class_dictionary->iterate(©); 1369 writer.dump(dictionary, "lambda proxy class dictionary"); 1370 } 1371 1372 void SystemDictionaryShared::write_dictionary(RunTimeSharedDictionary* dictionary, 1373 bool is_builtin) { 1374 CompactHashtableStats stats; 1375 dictionary->reset(); 1376 CompactHashtableWriter writer(_dumptime_table->count_of(is_builtin), &stats); 1377 CopySharedClassInfoToArchive copy(&writer, is_builtin); 1378 assert_lock_strong(DumpTimeTable_lock); 1379 _dumptime_table->iterate_all_live_classes(©); 1380 writer.dump(dictionary, is_builtin ? "builtin dictionary" : "unregistered dictionary"); 1381 } 1382 1383 void SystemDictionaryShared::write_to_archive(bool is_static_archive) { 1384 ArchiveInfo* archive = get_archive(is_static_archive); 1385 1386 write_dictionary(&archive->_builtin_dictionary, true); 1387 write_dictionary(&archive->_unregistered_dictionary, false); 1388 1389 write_lambda_proxy_class_dictionary(&archive->_lambda_proxy_class_dictionary); 1390 } 1391 1392 void SystemDictionaryShared::adjust_lambda_proxy_class_dictionary() { 1393 AdjustLambdaProxyClassInfo adjuster; 1394 _dumptime_lambda_proxy_class_dictionary->iterate(&adjuster); 1395 } 1396 1397 void SystemDictionaryShared::serialize_dictionary_headers(SerializeClosure* soc, 1398 bool is_static_archive) { 1399 ArchiveInfo* archive = get_archive(is_static_archive); 1400 1401 archive->_builtin_dictionary.serialize_header(soc); 1402 archive->_unregistered_dictionary.serialize_header(soc); 1403 archive->_lambda_proxy_class_dictionary.serialize_header(soc); 1404 } 1405 1406 void SystemDictionaryShared::serialize_vm_classes(SerializeClosure* soc) { 1407 for (auto id : EnumRange<vmClassID>{}) { 1408 soc->do_ptr(vmClasses::klass_addr_at(id)); 1409 } 1410 } 1411 1412 const RunTimeClassInfo* 1413 SystemDictionaryShared::find_record(RunTimeSharedDictionary* static_dict, RunTimeSharedDictionary* dynamic_dict, Symbol* name) { 1414 if (!CDSConfig::is_using_archive() || !name->is_shared()) { 1415 // The names of all shared classes must also be a shared Symbol. 1416 return nullptr; 1417 } 1418 1419 unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary_quick(name); 1420 const RunTimeClassInfo* record = nullptr; 1421 if (DynamicArchive::is_mapped()) { 1422 // Use the regenerated holder classes in the dynamic archive as they 1423 // have more methods than those in the base archive. 1424 if (name == vmSymbols::java_lang_invoke_Invokers_Holder() || 1425 name == vmSymbols::java_lang_invoke_DirectMethodHandle_Holder() || 1426 name == vmSymbols::java_lang_invoke_LambdaForm_Holder() || 1427 name == vmSymbols::java_lang_invoke_DelegatingMethodHandle_Holder()) { 1428 record = dynamic_dict->lookup(name, hash, 0); 1429 if (record != nullptr) { 1430 return record; 1431 } 1432 } 1433 } 1434 1435 if (!MetaspaceShared::is_shared_dynamic(name)) { 1436 // The names of all shared classes in the static dict must also be in the 1437 // static archive 1438 record = static_dict->lookup(name, hash, 0); 1439 } 1440 1441 if (record == nullptr && DynamicArchive::is_mapped()) { 1442 record = dynamic_dict->lookup(name, hash, 0); 1443 } 1444 1445 return record; 1446 } 1447 1448 InstanceKlass* SystemDictionaryShared::find_builtin_class(Symbol* name) { 1449 const RunTimeClassInfo* record = find_record(&_static_archive._builtin_dictionary, 1450 &_dynamic_archive._builtin_dictionary, 1451 name); 1452 if (record != nullptr) { 1453 assert(!record->klass()->is_hidden(), "hidden class cannot be looked up by name"); 1454 DEBUG_ONLY(check_klass_after_loading(record->klass());) 1455 // We did not save the classfile data of the generated LambdaForm invoker classes, 1456 // so we cannot support CLFH for such classes. 1457 if (record->klass()->is_generated_shared_class() && JvmtiExport::should_post_class_file_load_hook()) { 1458 return nullptr; 1459 } 1460 return record->klass(); 1461 } else { 1462 return nullptr; 1463 } 1464 } 1465 1466 void SystemDictionaryShared::update_shared_entry(InstanceKlass* k, int id) { 1467 assert(CDSConfig::is_dumping_static_archive(), "class ID is used only for static dump (from classlist)"); 1468 DumpTimeClassInfo* info = get_info(k); 1469 info->_id = id; 1470 } 1471 1472 static const char* class_loader_name_for_shared(Klass* k) { 1473 assert(k != nullptr, "Sanity"); 1474 assert(k->is_shared(), "Must be"); 1475 assert(k->is_instance_klass(), "Must be"); 1476 InstanceKlass* ik = InstanceKlass::cast(k); 1477 if (ik->is_shared_boot_class()) { 1478 return "boot_loader"; 1479 } else if (ik->is_shared_platform_class()) { 1480 return "platform_loader"; 1481 } else if (ik->is_shared_app_class()) { 1482 return "app_loader"; 1483 } else if (ik->is_shared_unregistered_class()) { 1484 return "unregistered_loader"; 1485 } else { 1486 return "unknown loader"; 1487 } 1488 } 1489 1490 class SharedDictionaryPrinter : StackObj { 1491 outputStream* _st; 1492 int _index; 1493 public: 1494 SharedDictionaryPrinter(outputStream* st) : _st(st), _index(0) {} 1495 1496 void do_value(const RunTimeClassInfo* record) { 1497 ResourceMark rm; 1498 _st->print_cr("%4d: %s %s", _index++, record->klass()->external_name(), 1499 class_loader_name_for_shared(record->klass())); 1500 if (record->klass()->array_klasses() != nullptr) { 1501 record->klass()->array_klasses()->cds_print_value_on(_st); 1502 _st->cr(); 1503 } 1504 } 1505 int index() const { return _index; } 1506 }; 1507 1508 class SharedLambdaDictionaryPrinter : StackObj { 1509 outputStream* _st; 1510 int _index; 1511 public: 1512 SharedLambdaDictionaryPrinter(outputStream* st, int idx) : _st(st), _index(idx) {} 1513 1514 void do_value(const RunTimeLambdaProxyClassInfo* record) { 1515 if (record->proxy_klass_head()->lambda_proxy_is_available()) { 1516 ResourceMark rm; 1517 Klass* k = record->proxy_klass_head(); 1518 while (k != nullptr) { 1519 _st->print_cr("%4d: %s %s", _index++, k->external_name(), 1520 class_loader_name_for_shared(k)); 1521 k = k->next_link(); 1522 } 1523 } 1524 } 1525 }; 1526 1527 void SystemDictionaryShared::ArchiveInfo::print_on(const char* prefix, 1528 outputStream* st) { 1529 st->print_cr("%sShared Dictionary", prefix); 1530 SharedDictionaryPrinter p(st); 1531 st->print_cr("%sShared Builtin Dictionary", prefix); 1532 _builtin_dictionary.iterate(&p); 1533 st->print_cr("%sShared Unregistered Dictionary", prefix); 1534 _unregistered_dictionary.iterate(&p); 1535 if (!_lambda_proxy_class_dictionary.empty()) { 1536 st->print_cr("%sShared Lambda Dictionary", prefix); 1537 SharedLambdaDictionaryPrinter ldp(st, p.index()); 1538 _lambda_proxy_class_dictionary.iterate(&ldp); 1539 } 1540 } 1541 1542 void SystemDictionaryShared::ArchiveInfo::print_table_statistics(const char* prefix, 1543 outputStream* st) { 1544 st->print_cr("%sArchve Statistics", prefix); 1545 _builtin_dictionary.print_table_statistics(st, "Builtin Shared Dictionary"); 1546 _unregistered_dictionary.print_table_statistics(st, "Unregistered Shared Dictionary"); 1547 _lambda_proxy_class_dictionary.print_table_statistics(st, "Lambda Shared Dictionary"); 1548 } 1549 1550 void SystemDictionaryShared::print_shared_archive(outputStream* st, bool is_static) { 1551 if (CDSConfig::is_using_archive()) { 1552 if (is_static) { 1553 _static_archive.print_on("", st); 1554 } else { 1555 if (DynamicArchive::is_mapped()) { 1556 _dynamic_archive.print_on("Dynamic ", st); 1557 } 1558 } 1559 } 1560 } 1561 1562 void SystemDictionaryShared::print_on(outputStream* st) { 1563 print_shared_archive(st, true); 1564 print_shared_archive(st, false); 1565 } 1566 1567 void SystemDictionaryShared::print_table_statistics(outputStream* st) { 1568 if (CDSConfig::is_using_archive()) { 1569 _static_archive.print_table_statistics("Static ", st); 1570 if (DynamicArchive::is_mapped()) { 1571 _dynamic_archive.print_table_statistics("Dynamic ", st); 1572 } 1573 } 1574 } 1575 1576 bool SystemDictionaryShared::is_dumptime_table_empty() { 1577 assert_lock_strong(DumpTimeTable_lock); 1578 _dumptime_table->update_counts(); 1579 if (_dumptime_table->count_of(true) == 0 && _dumptime_table->count_of(false) == 0){ 1580 return true; 1581 } 1582 return false; 1583 } 1584 1585 class CleanupDumpTimeLambdaProxyClassTable: StackObj { 1586 public: 1587 bool do_entry(LambdaProxyClassKey& key, DumpTimeLambdaProxyClassInfo& info) { 1588 assert_lock_strong(DumpTimeTable_lock); 1589 InstanceKlass* caller_ik = key.caller_ik(); 1590 InstanceKlass* nest_host = caller_ik->nest_host_not_null(); 1591 1592 // If the caller class and/or nest_host are excluded, the associated lambda proxy 1593 // must also be excluded. 1594 bool always_exclude = SystemDictionaryShared::check_for_exclusion(caller_ik, nullptr) || 1595 SystemDictionaryShared::check_for_exclusion(nest_host, nullptr); 1596 1597 for (int i = info._proxy_klasses->length() - 1; i >= 0; i--) { 1598 InstanceKlass* ik = info._proxy_klasses->at(i); 1599 if (always_exclude || SystemDictionaryShared::check_for_exclusion(ik, nullptr)) { 1600 SystemDictionaryShared::reset_registered_lambda_proxy_class(ik); 1601 info._proxy_klasses->remove_at(i); 1602 } 1603 } 1604 return info._proxy_klasses->length() == 0 ? true /* delete the node*/ : false; 1605 } 1606 }; 1607 1608 void SystemDictionaryShared::cleanup_lambda_proxy_class_dictionary() { 1609 assert_lock_strong(DumpTimeTable_lock); 1610 CleanupDumpTimeLambdaProxyClassTable cleanup_proxy_classes; 1611 _dumptime_lambda_proxy_class_dictionary->unlink(&cleanup_proxy_classes); 1612 }