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