< prev index next >

src/hotspot/share/classfile/systemDictionaryShared.cpp

Print this page

  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 "precompiled.hpp"
  26 #include "cds/archiveBuilder.hpp"
  27 #include "cds/archiveHeapLoader.hpp"
  28 #include "cds/archiveUtils.hpp"
  29 #include "cds/cdsConfig.hpp"
  30 #include "cds/classListParser.hpp"
  31 #include "cds/classListWriter.hpp"

  32 #include "cds/dynamicArchive.hpp"
  33 #include "cds/filemap.hpp"

  34 #include "cds/cdsProtectionDomain.hpp"
  35 #include "cds/dumpTimeClassInfo.inline.hpp"

  36 #include "cds/metaspaceShared.hpp"

  37 #include "cds/runTimeClassInfo.hpp"
  38 #include "classfile/classFileStream.hpp"
  39 #include "classfile/classLoader.hpp"
  40 #include "classfile/classLoaderData.inline.hpp"
  41 #include "classfile/classLoaderDataGraph.hpp"
  42 #include "classfile/classLoaderExt.hpp"
  43 #include "classfile/dictionary.hpp"
  44 #include "classfile/javaClasses.hpp"
  45 #include "classfile/javaClasses.inline.hpp"
  46 #include "classfile/symbolTable.hpp"
  47 #include "classfile/systemDictionary.hpp"
  48 #include "classfile/systemDictionaryShared.hpp"
  49 #include "classfile/verificationType.hpp"
  50 #include "classfile/vmClasses.hpp"
  51 #include "classfile/vmSymbols.hpp"
  52 #include "interpreter/bootstrapInfo.hpp"
  53 #include "jfr/jfrEvents.hpp"
  54 #include "logging/log.hpp"
  55 #include "logging/logStream.hpp"
  56 #include "memory/allocation.hpp"
  57 #include "memory/metadataFactory.hpp"
  58 #include "memory/metaspaceClosure.hpp"
  59 #include "memory/oopFactory.hpp"
  60 #include "memory/resourceArea.hpp"
  61 #include "memory/universe.hpp"
  62 #include "oops/instanceKlass.hpp"
  63 #include "oops/klass.inline.hpp"


  64 #include "oops/objArrayKlass.hpp"
  65 #include "oops/objArrayOop.inline.hpp"
  66 #include "oops/oop.inline.hpp"
  67 #include "oops/oopHandle.inline.hpp"
  68 #include "oops/typeArrayOop.inline.hpp"
  69 #include "runtime/arguments.hpp"
  70 #include "runtime/handles.inline.hpp"
  71 #include "runtime/java.hpp"
  72 #include "runtime/javaCalls.hpp"
  73 #include "runtime/mutexLocker.hpp"
  74 #include "utilities/resourceHash.hpp"
  75 #include "utilities/stringUtils.hpp"
  76 
  77 SystemDictionaryShared::ArchiveInfo SystemDictionaryShared::_static_archive;
  78 SystemDictionaryShared::ArchiveInfo SystemDictionaryShared::_dynamic_archive;
  79 
  80 DumpTimeSharedClassTable* SystemDictionaryShared::_dumptime_table = nullptr;
  81 DumpTimeLambdaProxyClassDictionary* SystemDictionaryShared::_dumptime_lambda_proxy_class_dictionary = nullptr;
  82 









  83 // Used by NoClassLoadingMark
  84 DEBUG_ONLY(bool SystemDictionaryShared::_class_loading_may_happen = true;)
  85 
  86 InstanceKlass* SystemDictionaryShared::load_shared_class_for_builtin_loader(
  87                  Symbol* class_name, Handle class_loader, TRAPS) {
  88   assert(CDSConfig::is_using_archive(), "must be");
  89   InstanceKlass* ik = find_builtin_class(class_name);
  90 
  91   if (ik != nullptr && !ik->shared_loading_failed()) {
  92     if ((SystemDictionary::is_system_class_loader(class_loader()) && ik->is_shared_app_class())  ||
  93         (SystemDictionary::is_platform_class_loader(class_loader()) && ik->is_shared_platform_class())) {
  94       SharedClassLoadingMark slm(THREAD, ik);
  95       PackageEntry* pkg_entry = CDSProtectionDomain::get_package_entry_from_class(ik, class_loader);
  96       Handle protection_domain =
  97         CDSProtectionDomain::init_security_info(class_loader, ik, pkg_entry, CHECK_NULL);



  98       return load_shared_class(ik, class_loader, protection_domain, nullptr, pkg_entry, THREAD);
  99     }
 100   }
 101   return nullptr;
 102 }
 103 
 104 // This function is called for loading only UNREGISTERED classes
 105 InstanceKlass* SystemDictionaryShared::lookup_from_stream(Symbol* class_name,
 106                                                           Handle class_loader,
 107                                                           Handle protection_domain,
 108                                                           const ClassFileStream* cfs,
 109                                                           TRAPS) {
 110   if (!CDSConfig::is_using_archive()) {
 111     return nullptr;
 112   }
 113   if (class_name == nullptr) {  // don't do this for hidden classes
 114     return nullptr;
 115   }
 116   if (class_loader.is_null() ||
 117       SystemDictionary::is_system_class_loader(class_loader()) ||

 164   loader_data->add_class(ik);
 165 
 166   // Get the package entry.
 167   PackageEntry* pkg_entry = CDSProtectionDomain::get_package_entry_from_class(ik, class_loader);
 168 
 169   // Load and check super/interfaces, restore unshareable info
 170   InstanceKlass* shared_klass = load_shared_class(ik, class_loader, protection_domain,
 171                                                   cfs, pkg_entry, THREAD);
 172   if (shared_klass == nullptr || HAS_PENDING_EXCEPTION) {
 173     // TODO: clean up <ik> so it can be used again
 174     return nullptr;
 175   }
 176 
 177   return shared_klass;
 178 }
 179 
 180 // Guaranteed to return non-null value for non-shared classes.
 181 // k must not be a shared class.
 182 DumpTimeClassInfo* SystemDictionaryShared::get_info(InstanceKlass* k) {
 183   MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
 184   assert(!k->is_shared(), "sanity");
 185   return get_info_locked(k);
 186 }
 187 
 188 DumpTimeClassInfo* SystemDictionaryShared::get_info_locked(InstanceKlass* k) {
 189   assert_lock_strong(DumpTimeTable_lock);
 190   assert(!k->is_shared(), "sanity");
 191   DumpTimeClassInfo* info = _dumptime_table->get_info(k);
 192   assert(info != nullptr, "must be");
 193   return info;
 194 }
 195 








 196 bool SystemDictionaryShared::check_for_exclusion(InstanceKlass* k, DumpTimeClassInfo* info) {
 197   if (MetaspaceShared::is_in_shared_metaspace(k)) {
 198     // We have reached a super type that's already in the base archive. Treat it
 199     // as "not excluded".
 200     assert(CDSConfig::is_dumping_dynamic_archive(), "must be");
 201     return false;
 202   }
 203 
 204   if (info == nullptr) {
 205     info = _dumptime_table->get(k);
 206     assert(info != nullptr, "supertypes of any classes in _dumptime_table must either be shared, or must also be in _dumptime_table");
 207   }
 208 
 209   if (!info->has_checked_exclusion()) {
 210     if (check_for_exclusion_impl(k)) {
 211       info->set_excluded();
 212     }
 213     info->set_has_checked_exclusion();
 214   }
 215 
 216   return info->is_excluded();
 217 }

 243   if (info != nullptr) {
 244     info->_is_archived_lambda_proxy = false;
 245     info->set_excluded();
 246   }
 247 }
 248 
 249 bool SystemDictionaryShared::is_early_klass(InstanceKlass* ik) {
 250   DumpTimeClassInfo* info = _dumptime_table->get(ik);
 251   return (info != nullptr) ? info->is_early_klass() : false;
 252 }
 253 
 254 bool SystemDictionaryShared::is_hidden_lambda_proxy(InstanceKlass* ik) {
 255   assert(ik->is_shared(), "applicable to only a shared class");
 256   if (ik->is_hidden()) {
 257     return true;
 258   } else {
 259     return false;
 260   }
 261 }
 262 





 263 bool SystemDictionaryShared::check_for_exclusion_impl(InstanceKlass* k) {





 264   if (k->is_in_error_state()) {
 265     return warn_excluded(k, "In error state");
 266   }
 267   if (k->is_scratch_class()) {
 268     return warn_excluded(k, "A scratch class");
 269   }
 270   if (!k->is_loaded()) {
 271     return warn_excluded(k, "Not in loaded state");
 272   }
 273   if (has_been_redefined(k)) {
 274     return warn_excluded(k, "Has been redefined");
 275   }
 276   if (!k->is_hidden() && k->shared_classpath_index() < 0 && is_builtin(k)) {
 277     // These are classes loaded from unsupported locations (such as those loaded by JVMTI native
 278     // agent during dump time).
 279     return warn_excluded(k, "Unsupported location");
 280   }
 281   if (k->signers() != nullptr) {
 282     // We cannot include signed classes in the archive because the certificates
 283     // used during dump time may be different than those used during
 284     // runtime (due to expiration, etc).
 285     return warn_excluded(k, "Signed JAR");
 286   }
 287   if (is_jfr_event_class(k)) {
 288     // We cannot include JFR event classes because they need runtime-specific
 289     // instrumentation in order to work with -XX:FlightRecorderOptions:retransform=false.
 290     // There are only a small number of these classes, so it's not worthwhile to
 291     // support them and make CDS more complicated.
 292     return warn_excluded(k, "JFR event class");


 293   }
 294 
 295   if (!k->is_linked()) {
 296     if (has_class_failed_verification(k)) {
 297       return warn_excluded(k, "Failed verification");
 298     }
 299   } else {
 300     if (!k->can_be_verified_at_dumptime()) {
 301       // We have an old class that has been linked (e.g., it's been executed during
 302       // dump time). This class has been verified using the old verifier, which
 303       // doesn't save the verification constraints, so check_verification_constraints()
 304       // won't work at runtime.
 305       // As a result, we cannot store this class. It must be loaded and fully verified
 306       // at runtime.
 307       return warn_excluded(k, "Old class has been linked");


 308     }
 309   }
 310 
 311   if (k->is_hidden() && !is_registered_lambda_proxy_class(k)) {
 312     ResourceMark rm;
 313     log_debug(cds)("Skipping %s: Hidden class", k->name()->as_C_string());
 314     return true;
 315   }
 316 
 317   InstanceKlass* super = k->java_super();
 318   if (super != nullptr && check_for_exclusion(super, nullptr)) {
 319     ResourceMark rm;
 320     log_warning(cds)("Skipping %s: super class %s is excluded", k->name()->as_C_string(), super->name()->as_C_string());
 321     return true;
 322   }
 323 
 324   Array<InstanceKlass*>* interfaces = k->local_interfaces();
 325   int len = interfaces->length();
 326   for (int i = 0; i < len; i++) {
 327     InstanceKlass* intf = interfaces->at(i);
 328     if (check_for_exclusion(intf, nullptr)) {
 329       ResourceMark rm;
 330       log_warning(cds)("Skipping %s: interface %s is excluded", k->name()->as_C_string(), intf->name()->as_C_string());
 331       return true;
 332     }
 333   }

 485     // The VM is not trying to resolve a super type of parser->current_class_name().
 486     // Instead, it's resolving an error class (because parser->current_class_name() has
 487     // failed parsing or verification). Don't do anything here.
 488     return nullptr;
 489   }
 490 }
 491 
 492 void SystemDictionaryShared::set_shared_class_misc_info(InstanceKlass* k, ClassFileStream* cfs) {
 493   assert(CDSConfig::is_dumping_archive(), "sanity");
 494   assert(!is_builtin(k), "must be unregistered class");
 495   DumpTimeClassInfo* info = get_info(k);
 496   info->_clsfile_size  = cfs->length();
 497   info->_clsfile_crc32 = ClassLoader::crc32(0, (const char*)cfs->buffer(), cfs->length());
 498 }
 499 
 500 void SystemDictionaryShared::initialize() {
 501   if (CDSConfig::is_dumping_archive()) {
 502     _dumptime_table = new (mtClass) DumpTimeSharedClassTable;
 503     _dumptime_lambda_proxy_class_dictionary =
 504                       new (mtClass) DumpTimeLambdaProxyClassDictionary;

 505   }
 506 }
 507 
 508 void SystemDictionaryShared::init_dumptime_info(InstanceKlass* k) {
 509   MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
 510   assert(SystemDictionaryShared::class_loading_may_happen(), "sanity");
 511   _dumptime_table->allocate_info(k);







 512 }
 513 
 514 void SystemDictionaryShared::remove_dumptime_info(InstanceKlass* k) {
 515   MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
 516   _dumptime_table->remove(k);
 517 }
 518 
 519 void SystemDictionaryShared::handle_class_unloading(InstanceKlass* klass) {
 520   if (CDSConfig::is_dumping_archive()) {
 521     remove_dumptime_info(klass);
 522   }
 523 
 524   if (CDSConfig::is_dumping_archive() || ClassListWriter::is_enabled()) {
 525     MutexLocker ml(Thread::current(), UnregisteredClassesTable_lock, Mutex::_no_safepoint_check_flag);
 526     if (_unregistered_classes_table != nullptr) {
 527       // Remove the class from _unregistered_classes_table: keep the entry but
 528       // set it to null. This ensure no classes with the same name can be
 529       // added again.
 530       InstanceKlass** v = _unregistered_classes_table->get(klass->name());
 531       if (v != nullptr) {

 553   Array<InstanceKlass*>* interfaces = k->local_interfaces();
 554   int len = interfaces->length();
 555   for (int i = 0; i < len; i++) {
 556     if (has_been_redefined(interfaces->at(i))) {
 557       return true;
 558     }
 559   }
 560   return false;
 561 }
 562 
 563 // k is a class before relocating by ArchiveBuilder
 564 void SystemDictionaryShared::validate_before_archiving(InstanceKlass* k) {
 565   ResourceMark rm;
 566   const char* name = k->name()->as_C_string();
 567   DumpTimeClassInfo* info = _dumptime_table->get(k);
 568   assert(!class_loading_may_happen(), "class loading must be disabled");
 569   guarantee(info != nullptr, "Class %s must be entered into _dumptime_table", name);
 570   guarantee(!info->is_excluded(), "Should not attempt to archive excluded class %s", name);
 571   if (is_builtin(k)) {
 572     if (k->is_hidden()) {
 573       assert(is_registered_lambda_proxy_class(k), "unexpected hidden class %s", name);



 574     }
 575     guarantee(!k->is_shared_unregistered_class(),
 576               "Class loader type must be set for BUILTIN class %s", name);
 577 
 578   } else {
 579     guarantee(k->is_shared_unregistered_class(),
 580               "Class loader type must not be set for UNREGISTERED class %s", name);
 581   }
 582 }
 583 
 584 class UnregisteredClassesDuplicationChecker : StackObj {
 585   GrowableArray<InstanceKlass*> _list;
 586   Thread* _thread;
 587 public:
 588   UnregisteredClassesDuplicationChecker() : _thread(Thread::current()) {}
 589 
 590   void do_entry(InstanceKlass* k, DumpTimeClassInfo& info) {
 591     if (!SystemDictionaryShared::is_builtin(k)) {
 592       _list.append(k);
 593     }

 605   }
 606 
 607   void mark_duplicated_classes() {
 608     // Two loaders may load two identical or similar hierarchies of classes. If we
 609     // check for duplication in random order, we may end up excluding important base classes
 610     // in both hierarchies, causing most of the classes to be excluded.
 611     // We sort the classes by their loaders. This way we're likely to archive
 612     // all classes in the one of the two hierarchies.
 613     _list.sort(compare_by_loader);
 614     for (int i = 0; i < _list.length(); i++) {
 615       InstanceKlass* k = _list.at(i);
 616       bool i_am_first = SystemDictionaryShared::add_unregistered_class(_thread, k);
 617       if (!i_am_first) {
 618         SystemDictionaryShared::warn_excluded(k, "Duplicated unregistered class");
 619         SystemDictionaryShared::set_excluded_locked(k);
 620       }
 621     }
 622   }
 623 };
 624 

























 625 void SystemDictionaryShared::check_excluded_classes() {
 626   assert(!class_loading_may_happen(), "class loading must be disabled");
 627   assert_lock_strong(DumpTimeTable_lock);
 628 
 629   if (CDSConfig::is_dumping_dynamic_archive()) {
 630     // Do this first -- if a base class is excluded due to duplication,
 631     // all of its subclasses will also be excluded.
 632     ResourceMark rm;
 633     UnregisteredClassesDuplicationChecker dup_checker;
 634     _dumptime_table->iterate_all_live_classes(&dup_checker);
 635     dup_checker.mark_duplicated_classes();
 636   }
 637 
 638   auto check_for_exclusion = [&] (InstanceKlass* k, DumpTimeClassInfo& info) {
 639     SystemDictionaryShared::check_for_exclusion(k, &info);









 640   };
 641   _dumptime_table->iterate_all_live_classes(check_for_exclusion);































 642   _dumptime_table->update_counts();
 643 
 644   cleanup_lambda_proxy_class_dictionary();




 645 }
 646 
 647 bool SystemDictionaryShared::is_excluded_class(InstanceKlass* k) {
 648   assert(!class_loading_may_happen(), "class loading must be disabled");
 649   assert_lock_strong(DumpTimeTable_lock);
 650   assert(CDSConfig::is_dumping_archive(), "sanity");
 651   DumpTimeClassInfo* p = get_info_locked(k);
 652   return p->is_excluded();
 653 }
 654 
 655 void SystemDictionaryShared::set_excluded_locked(InstanceKlass* k) {
 656   assert_lock_strong(DumpTimeTable_lock);
 657   assert(CDSConfig::is_dumping_archive(), "sanity");
 658   DumpTimeClassInfo* info = get_info_locked(k);
 659   info->set_excluded();
 660 }
 661 
 662 void SystemDictionaryShared::set_excluded(InstanceKlass* k) {
 663   assert(CDSConfig::is_dumping_archive(), "sanity");
 664   DumpTimeClassInfo* info = get_info(k);
 665   info->set_excluded();
 666 }
 667 
 668 void SystemDictionaryShared::set_class_has_failed_verification(InstanceKlass* ik) {
 669   assert(CDSConfig::is_dumping_archive(), "sanity");
 670   DumpTimeClassInfo* p = get_info(ik);
 671   p->set_failed_verification();
 672 }
 673 
 674 bool SystemDictionaryShared::has_class_failed_verification(InstanceKlass* ik) {
 675   assert(CDSConfig::is_dumping_archive(), "sanity");
 676   DumpTimeClassInfo* p = _dumptime_table->get(ik);
 677   return (p == nullptr) ? false : p->failed_verification();
 678 }
 679 
 680 void SystemDictionaryShared::dumptime_classes_do(class MetaspaceClosure* it) {
 681   assert_lock_strong(DumpTimeTable_lock);
 682 
 683   auto do_klass = [&] (InstanceKlass* k, DumpTimeClassInfo& info) {
 684     if (k->is_loader_alive() && !info.is_excluded()) {



 685       info.metaspace_pointers_do(it);
 686     }
 687   };
 688   _dumptime_table->iterate_all_live_classes(do_klass);
 689 
 690   auto do_lambda = [&] (LambdaProxyClassKey& key, DumpTimeLambdaProxyClassInfo& info) {
 691     if (key.caller_ik()->is_loader_alive()) {
 692       info.metaspace_pointers_do(it);
 693       key.metaspace_pointers_do(it);
 694     }
 695   };
 696   _dumptime_lambda_proxy_class_dictionary->iterate_all(do_lambda);






 697 }
 698 
 699 bool SystemDictionaryShared::add_verification_constraint(InstanceKlass* k, Symbol* name,
 700          Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object) {
 701   assert(CDSConfig::is_dumping_archive(), "sanity");










 702   DumpTimeClassInfo* info = get_info(k);
 703   info->add_verification_constraint(k, name, from_name, from_field_is_protected,
 704                                     from_is_array, from_is_object);
 705 
 706   if (CDSConfig::is_dumping_dynamic_archive()) {
 707     // For dynamic dumping, we can resolve all the constraint classes for all class loaders during
 708     // the initial run prior to creating the archive before vm exit. We will also perform verification
 709     // check when running with the archive.
 710     return false;
 711   } else {
 712     if (is_builtin(k)) {
 713       // For builtin class loaders, we can try to complete the verification check at dump time,
 714       // because we can resolve all the constraint classes. We will also perform verification check
 715       // when running with the archive.
 716       return false;
 717     } else {
 718       // For non-builtin class loaders, we cannot complete the verification check at dump time,
 719       // because at dump time we don't know how to resolve classes for such loaders.
 720       return true;
 721     }

 731 void SystemDictionaryShared::add_to_dump_time_lambda_proxy_class_dictionary(LambdaProxyClassKey& key,
 732                                                            InstanceKlass* proxy_klass) {
 733   assert_lock_strong(DumpTimeTable_lock);
 734 
 735   bool created;
 736   DumpTimeLambdaProxyClassInfo* info = _dumptime_lambda_proxy_class_dictionary->put_if_absent(key, &created);
 737   info->add_proxy_klass(proxy_klass);
 738   if (created) {
 739     ++_dumptime_lambda_proxy_class_dictionary->_count;
 740   }
 741 }
 742 
 743 void SystemDictionaryShared::add_lambda_proxy_class(InstanceKlass* caller_ik,
 744                                                     InstanceKlass* lambda_ik,
 745                                                     Symbol* invoked_name,
 746                                                     Symbol* invoked_type,
 747                                                     Symbol* method_type,
 748                                                     Method* member_method,
 749                                                     Symbol* instantiated_method_type,
 750                                                     TRAPS) {








 751 
 752   assert(caller_ik->class_loader() == lambda_ik->class_loader(), "mismatched class loader");
 753   assert(caller_ik->class_loader_data() == lambda_ik->class_loader_data(), "mismatched class loader data");
 754   assert(java_lang_Class::class_data(lambda_ik->java_mirror()) == nullptr, "must not have class data");
 755 
 756   MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
 757 
 758   lambda_ik->assign_class_loader_type();
 759   lambda_ik->set_shared_classpath_index(caller_ik->shared_classpath_index());
 760   InstanceKlass* nest_host = caller_ik->nest_host(CHECK);
 761   assert(nest_host != nullptr, "unexpected nullptr nest_host");
 762 
 763   DumpTimeClassInfo* info = _dumptime_table->get(lambda_ik);
 764   if (info != nullptr && !lambda_ik->is_non_strong_hidden() && is_builtin(lambda_ik) && is_builtin(caller_ik)
 765       // Don't include the lambda proxy if its nest host is not in the "linked" state.
 766       && nest_host->is_linked()) {
 767     // Set _is_archived_lambda_proxy in DumpTimeClassInfo so that the lambda_ik
 768     // won't be excluded during dumping of shared archive. See ExcludeDumpTimeSharedClasses.
 769     info->_is_archived_lambda_proxy = true;
 770     info->set_nest_host(nest_host);
 771 
 772     LambdaProxyClassKey key(caller_ik,
 773                             invoked_name,
 774                             invoked_type,
 775                             method_type,
 776                             member_method,
 777                             instantiated_method_type);
 778     add_to_dump_time_lambda_proxy_class_dictionary(key, lambda_ik);
 779   }
 780 }
 781 
 782 InstanceKlass* SystemDictionaryShared::get_shared_lambda_proxy_class(InstanceKlass* caller_ik,
 783                                                                      Symbol* invoked_name,
 784                                                                      Symbol* invoked_type,
 785                                                                      Symbol* method_type,
 786                                                                      Method* member_method,
 787                                                                      Symbol* instantiated_method_type) {



 788   MutexLocker ml(CDSLambda_lock, Mutex::_no_safepoint_check_flag);
 789   LambdaProxyClassKey key(caller_ik, invoked_name, invoked_type,
 790                           method_type, member_method, instantiated_method_type);
 791 
 792   // Try to retrieve the lambda proxy class from static archive.
 793   const RunTimeLambdaProxyClassInfo* info = _static_archive.lookup_lambda_proxy_class(&key);
 794   InstanceKlass* proxy_klass = retrieve_lambda_proxy_class(info);
 795   if (proxy_klass == nullptr) {
 796     if (info != nullptr && log_is_enabled(Debug, cds)) {
 797       ResourceMark rm;
 798       log_debug(cds)("Used all static archived lambda proxy classes for: %s %s%s",
 799                      caller_ik->external_name(), invoked_name->as_C_string(), invoked_type->as_C_string());
 800     }
 801   } else {
 802     return proxy_klass;
 803   }
 804 
 805   // Retrieving from static archive is unsuccessful, try dynamic archive.
 806   info = _dynamic_archive.lookup_lambda_proxy_class(&key);
 807   proxy_klass = retrieve_lambda_proxy_class(info);

 816 }
 817 
 818 InstanceKlass* SystemDictionaryShared::retrieve_lambda_proxy_class(const RunTimeLambdaProxyClassInfo* info) {
 819   InstanceKlass* proxy_klass = nullptr;
 820   if (info != nullptr) {
 821     InstanceKlass* curr_klass = info->proxy_klass_head();
 822     InstanceKlass* prev_klass = curr_klass;
 823     if (curr_klass->lambda_proxy_is_available()) {
 824       while (curr_klass->next_link() != nullptr) {
 825         prev_klass = curr_klass;
 826         curr_klass = InstanceKlass::cast(curr_klass->next_link());
 827       }
 828       assert(curr_klass->is_hidden(), "must be");
 829       assert(curr_klass->lambda_proxy_is_available(), "must be");
 830 
 831       prev_klass->set_next_link(nullptr);
 832       proxy_klass = curr_klass;
 833       proxy_klass->clear_lambda_proxy_is_available();
 834       if (log_is_enabled(Debug, cds)) {
 835         ResourceMark rm;
 836         log_debug(cds)("Loaded lambda proxy: %s ", proxy_klass->external_name());
 837       }
 838     }
 839   }
 840   return proxy_klass;
 841 }
 842 
 843 InstanceKlass* SystemDictionaryShared::get_shared_nest_host(InstanceKlass* lambda_ik) {
 844   assert(!CDSConfig::is_dumping_static_archive() && CDSConfig::is_using_archive(), "called at run time with CDS enabled only");
 845   RunTimeClassInfo* record = RunTimeClassInfo::get_for(lambda_ik);
 846   return record->nest_host();
 847 }
 848 
 849 InstanceKlass* SystemDictionaryShared::prepare_shared_lambda_proxy_class(InstanceKlass* lambda_ik,
 850                                                                          InstanceKlass* caller_ik, TRAPS) {
 851   Handle class_loader(THREAD, caller_ik->class_loader());
 852   Handle protection_domain;
 853   PackageEntry* pkg_entry = caller_ik->package();
 854   if (caller_ik->class_loader() != nullptr) {
 855     protection_domain = CDSProtectionDomain::init_security_info(class_loader, caller_ik, pkg_entry, CHECK_NULL);
 856   }

 875   // Add to class hierarchy, and do possible deoptimizations.
 876   loaded_lambda->add_to_hierarchy(THREAD);
 877   // But, do not add to dictionary.
 878 
 879   loaded_lambda->link_class(CHECK_NULL);
 880   // notify jvmti
 881   if (JvmtiExport::should_post_class_load()) {
 882     JvmtiExport::post_class_load(THREAD, loaded_lambda);
 883   }
 884   if (class_load_start_event.should_commit()) {
 885     SystemDictionary::post_class_load_event(&class_load_start_event, loaded_lambda, ClassLoaderData::class_loader_data(class_loader()));
 886   }
 887 
 888   loaded_lambda->initialize(CHECK_NULL);
 889 
 890   return loaded_lambda;
 891 }
 892 
 893 void SystemDictionaryShared::check_verification_constraints(InstanceKlass* klass,
 894                                                             TRAPS) {
 895   assert(!CDSConfig::is_dumping_static_archive() && CDSConfig::is_using_archive(), "called at run time with CDS enabled only");
 896   RunTimeClassInfo* record = RunTimeClassInfo::get_for(klass);
 897 
 898   int length = record->_num_verifier_constraints;
 899   if (length > 0) {
 900     for (int i = 0; i < length; i++) {
 901       RunTimeClassInfo::RTVerifierConstraint* vc = record->verifier_constraint_at(i);
 902       Symbol* name      = vc->name();
 903       Symbol* from_name = vc->from_name();
 904       char c            = record->verifier_constraint_flag(i);
 905 
 906       if (log_is_enabled(Trace, cds, verification)) {
 907         ResourceMark rm(THREAD);
 908         log_trace(cds, verification)("check_verification_constraint: %s: %s must be subclass of %s [0x%x]",
 909                                      klass->external_name(), from_name->as_klass_external_name(),
 910                                      name->as_klass_external_name(), c);
 911       }
 912 
 913       bool from_field_is_protected = (c & SystemDictionaryShared::FROM_FIELD_IS_PROTECTED) ? true : false;
 914       bool from_is_array           = (c & SystemDictionaryShared::FROM_IS_ARRAY)           ? true : false;
 915       bool from_is_object          = (c & SystemDictionaryShared::FROM_IS_OBJECT)          ? true : false;

 985   assert(is_builtin(klass), "must be");
 986   assert(klass_loader != nullptr, "should not be called for boot loader");
 987   assert(loader1 != loader2, "must be");
 988 
 989   if (CDSConfig::is_dumping_dynamic_archive() && Thread::current()->is_VM_thread()) {
 990     // We are re-laying out the vtable/itables of the *copy* of
 991     // a class during the final stage of dynamic dumping. The
 992     // linking constraints for this class has already been recorded.
 993     return;
 994   }
 995   assert(!Thread::current()->is_VM_thread(), "must be");
 996 
 997   assert(CDSConfig::is_dumping_archive(), "sanity");
 998   DumpTimeClassInfo* info = get_info(klass);
 999   info->record_linking_constraint(name, loader1, loader2);
1000 }
1001 
1002 // returns true IFF there's no need to re-initialize the i/v-tables for klass for
1003 // the purpose of checking class loader constraints.
1004 bool SystemDictionaryShared::check_linking_constraints(Thread* current, InstanceKlass* klass) {
1005   assert(!CDSConfig::is_dumping_static_archive() && CDSConfig::is_using_archive(), "called at run time with CDS enabled only");
1006   LogTarget(Info, class, loader, constraints) log;
1007   if (klass->is_shared_boot_class()) {
1008     // No class loader constraint check performed for boot classes.
1009     return true;
1010   }
1011   if (klass->is_shared_platform_class() || klass->is_shared_app_class()) {
1012     RunTimeClassInfo* info = RunTimeClassInfo::get_for(klass);
1013     assert(info != nullptr, "Sanity");
1014     if (info->_num_loader_constraints > 0) {
1015       HandleMark hm(current);
1016       for (int i = 0; i < info->_num_loader_constraints; i++) {
1017         RunTimeClassInfo::RTLoaderConstraint* lc = info->loader_constraint_at(i);
1018         Symbol* name = lc->constraint_name();
1019         Handle loader1(current, get_class_loader_by(lc->_loader_type1));
1020         Handle loader2(current, get_class_loader_by(lc->_loader_type2));
1021         if (log.is_enabled()) {
1022           ResourceMark rm(current);
1023           log.print("[CDS add loader constraint for class %s symbol %s loader[0] %s loader[1] %s",
1024                     klass->external_name(), name->as_C_string(),
1025                     ClassLoaderData::class_loader_data(loader1())->loader_name_and_id(),

1114     }
1115   }
1116 
1117   size_t total() {
1118     return _shared_class_info_size;
1119   }
1120 };
1121 
1122 size_t SystemDictionaryShared::estimate_size_for_archive() {
1123   EstimateSizeForArchive est;
1124   _dumptime_table->iterate_all_live_classes(&est);
1125   size_t total_size = est.total() +
1126     CompactHashtableWriter::estimate_size(_dumptime_table->count_of(true)) +
1127     CompactHashtableWriter::estimate_size(_dumptime_table->count_of(false));
1128 
1129   size_t bytesize = align_up(sizeof(RunTimeLambdaProxyClassInfo), SharedSpaceObjectAlignment);
1130   total_size +=
1131       (bytesize * _dumptime_lambda_proxy_class_dictionary->_count) +
1132       CompactHashtableWriter::estimate_size(_dumptime_lambda_proxy_class_dictionary->_count);
1133 





1134   return total_size;
1135 }
1136 
1137 unsigned int SystemDictionaryShared::hash_for_shared_dictionary(address ptr) {
1138   if (ArchiveBuilder::is_active()) {
1139     uintx offset = ArchiveBuilder::current()->any_to_offset(ptr);
1140     unsigned int hash = primitive_hash<uintx>(offset);
1141     DEBUG_ONLY({
1142         if (MetaspaceObj::is_shared((const MetaspaceObj*)ptr)) {
1143           assert(hash == SystemDictionaryShared::hash_for_shared_dictionary_quick(ptr), "must be");
1144         }
1145       });
1146     return hash;
1147   } else {
1148     return SystemDictionaryShared::hash_for_shared_dictionary_quick(ptr);
1149   }
1150 }
1151 
1152 class CopyLambdaProxyClassInfoToArchive : StackObj {
1153   CompactHashtableWriter* _writer;
1154   ArchiveBuilder* _builder;
1155 public:
1156   CopyLambdaProxyClassInfoToArchive(CompactHashtableWriter* writer)
1157   : _writer(writer), _builder(ArchiveBuilder::current()) {}
1158   bool do_entry(LambdaProxyClassKey& key, DumpTimeLambdaProxyClassInfo& info) {

1231         ResourceMark rm;
1232         log_trace(cds,hashtables)("%s dictionary: %s", (_is_builtin ? "builtin" : "unregistered"), info._klass->external_name());
1233       }
1234 
1235       // Save this for quick runtime lookup of InstanceKlass* -> RunTimeClassInfo*
1236       InstanceKlass* buffered_klass = ArchiveBuilder::current()->get_buffered_addr(info._klass);
1237       RunTimeClassInfo::set_for(buffered_klass, record);
1238     }
1239   }
1240 };
1241 
1242 void SystemDictionaryShared::write_lambda_proxy_class_dictionary(LambdaProxyClassDictionary *dictionary) {
1243   CompactHashtableStats stats;
1244   dictionary->reset();
1245   CompactHashtableWriter writer(_dumptime_lambda_proxy_class_dictionary->_count, &stats);
1246   CopyLambdaProxyClassInfoToArchive copy(&writer);
1247   _dumptime_lambda_proxy_class_dictionary->iterate(&copy);
1248   writer.dump(dictionary, "lambda proxy class dictionary");
1249 }
1250 


































1251 void SystemDictionaryShared::write_dictionary(RunTimeSharedDictionary* dictionary,
1252                                               bool is_builtin) {
1253   CompactHashtableStats stats;
1254   dictionary->reset();
1255   CompactHashtableWriter writer(_dumptime_table->count_of(is_builtin), &stats);
1256   CopySharedClassInfoToArchive copy(&writer, is_builtin);
1257   assert_lock_strong(DumpTimeTable_lock);
1258   _dumptime_table->iterate_all_live_classes(&copy);
1259   writer.dump(dictionary, is_builtin ? "builtin dictionary" : "unregistered dictionary");
1260 }
1261 
1262 void SystemDictionaryShared::write_to_archive(bool is_static_archive) {
1263   ArchiveInfo* archive = get_archive(is_static_archive);
1264 
1265   write_dictionary(&archive->_builtin_dictionary, true);
1266   write_dictionary(&archive->_unregistered_dictionary, false);
1267 
1268   write_lambda_proxy_class_dictionary(&archive->_lambda_proxy_class_dictionary);


1269 }
1270 
1271 void SystemDictionaryShared::adjust_lambda_proxy_class_dictionary() {
1272   AdjustLambdaProxyClassInfo adjuster;
1273   _dumptime_lambda_proxy_class_dictionary->iterate(&adjuster);
1274 }
1275 






























1276 void SystemDictionaryShared::serialize_dictionary_headers(SerializeClosure* soc,
1277                                                           bool is_static_archive) {
1278   ArchiveInfo* archive = get_archive(is_static_archive);
1279 
1280   archive->_builtin_dictionary.serialize_header(soc);
1281   archive->_unregistered_dictionary.serialize_header(soc);
1282   archive->_lambda_proxy_class_dictionary.serialize_header(soc);

1283 }
1284 
1285 void SystemDictionaryShared::serialize_vm_classes(SerializeClosure* soc) {
1286   for (auto id : EnumRange<vmClassID>{}) {
1287     soc->do_ptr(vmClasses::klass_addr_at(id));
1288   }





1289 }
1290 
1291 const RunTimeClassInfo*
1292 SystemDictionaryShared::find_record(RunTimeSharedDictionary* static_dict, RunTimeSharedDictionary* dynamic_dict, Symbol* name) {
1293   if (!CDSConfig::is_using_archive() || !name->is_shared()) {
1294     // The names of all shared classes must also be a shared Symbol.
1295     return nullptr;
1296   }
1297 
1298   unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary_quick(name);
1299   const RunTimeClassInfo* record = nullptr;
1300   if (DynamicArchive::is_mapped()) {
1301     // Use the regenerated holder classes in the dynamic archive as they
1302     // have more methods than those in the base archive.
1303     if (name == vmSymbols::java_lang_invoke_Invokers_Holder() ||
1304         name == vmSymbols::java_lang_invoke_DirectMethodHandle_Holder() ||
1305         name == vmSymbols::java_lang_invoke_LambdaForm_Holder() ||
1306         name == vmSymbols::java_lang_invoke_DelegatingMethodHandle_Holder()) {
1307       record = dynamic_dict->lookup(name, hash, 0);
1308       if (record != nullptr) {
1309         return record;
1310       }
1311     }
1312   }
1313 
1314   if (!MetaspaceShared::is_shared_dynamic(name)) {
1315     // The names of all shared classes in the static dict must also be in the
1316     // static archive
1317     record = static_dict->lookup(name, hash, 0);
1318   }
1319 
1320   if (record == nullptr && DynamicArchive::is_mapped()) {
1321     record = dynamic_dict->lookup(name, hash, 0);
1322   }
1323 
1324   return record;
1325 }
1326 

1331   if (record != nullptr) {
1332     assert(!record->_klass->is_hidden(), "hidden class cannot be looked up by name");
1333     assert(check_alignment(record->_klass), "Address not aligned");
1334     // We did not save the classfile data of the generated LambdaForm invoker classes,
1335     // so we cannot support CLFH for such classes.
1336     if (record->_klass->is_generated_shared_class() && JvmtiExport::should_post_class_file_load_hook()) {
1337        return nullptr;
1338     }
1339     return record->_klass;
1340   } else {
1341     return nullptr;
1342   }
1343 }
1344 
1345 void SystemDictionaryShared::update_shared_entry(InstanceKlass* k, int id) {
1346   assert(CDSConfig::is_dumping_static_archive(), "class ID is used only for static dump (from classlist)");
1347   DumpTimeClassInfo* info = get_info(k);
1348   info->_id = id;
1349 }
1350 
1351 static const char* class_loader_name_for_shared(Klass* k) {
1352   assert(k != nullptr, "Sanity");
1353   assert(k->is_shared(), "Must be");
1354   assert(k->is_instance_klass(), "Must be");
1355   InstanceKlass* ik = InstanceKlass::cast(k);
1356   if (ik->is_shared_boot_class()) {
1357     return "boot_loader";
1358   } else if (ik->is_shared_platform_class()) {
1359     return "platform_loader";
1360   } else if (ik->is_shared_app_class()) {
1361     return "app_loader";
1362   } else if (ik->is_shared_unregistered_class()) {
1363     return "unregistered_loader";
1364   } else {
1365     return "unknown loader";
1366   }
1367 }
1368 
1369 class SharedDictionaryPrinter : StackObj {
1370   outputStream* _st;
1371   int _index;
1372 public:
1373   SharedDictionaryPrinter(outputStream* st) : _st(st), _index(0) {}
1374 
1375   void do_value(const RunTimeClassInfo* record) {
1376     ResourceMark rm;
1377     _st->print_cr("%4d: %s %s", _index++, record->_klass->external_name(),
1378         class_loader_name_for_shared(record->_klass));
1379     if (record->_klass->array_klasses() != nullptr) {
1380       record->_klass->array_klasses()->cds_print_value_on(_st);
1381       _st->cr();
1382     }
1383   }
1384   int index() const { return _index; }
1385 };
1386 
1387 class SharedLambdaDictionaryPrinter : StackObj {
1388   outputStream* _st;
1389   int _index;
1390 public:
1391   SharedLambdaDictionaryPrinter(outputStream* st, int idx) : _st(st), _index(idx) {}
1392 
1393   void do_value(const RunTimeLambdaProxyClassInfo* record) {
1394     if (record->proxy_klass_head()->lambda_proxy_is_available()) {
1395       ResourceMark rm;
1396       Klass* k = record->proxy_klass_head();
1397       while (k != nullptr) {
1398         _st->print_cr("%4d: %s %s", _index++, k->external_name(),
1399                       class_loader_name_for_shared(k));
1400         k = k->next_link();
1401       }
1402     }
1403   }
1404 };
1405 










































1406 void SystemDictionaryShared::ArchiveInfo::print_on(const char* prefix,
1407                                                    outputStream* st) {
1408   st->print_cr("%sShared Dictionary", prefix);
1409   SharedDictionaryPrinter p(st);
1410   st->print_cr("%sShared Builtin Dictionary", prefix);
1411   _builtin_dictionary.iterate(&p);
1412   st->print_cr("%sShared Unregistered Dictionary", prefix);
1413   _unregistered_dictionary.iterate(&p);
1414   if (!_lambda_proxy_class_dictionary.empty()) {
1415     st->print_cr("%sShared Lambda Dictionary", prefix);
1416     SharedLambdaDictionaryPrinter ldp(st, p.index());
1417     _lambda_proxy_class_dictionary.iterate(&ldp);
1418   }





1419 }
1420 
1421 void SystemDictionaryShared::ArchiveInfo::print_table_statistics(const char* prefix,
1422                                                                  outputStream* st) {
1423   st->print_cr("%sArchve Statistics", prefix);
1424   _builtin_dictionary.print_table_statistics(st, "Builtin Shared Dictionary");
1425   _unregistered_dictionary.print_table_statistics(st, "Unregistered Shared Dictionary");
1426   _lambda_proxy_class_dictionary.print_table_statistics(st, "Lambda Shared Dictionary");

1427 }
1428 
1429 void SystemDictionaryShared::print_shared_archive(outputStream* st, bool is_static) {
1430   if (CDSConfig::is_using_archive()) {
1431     if (is_static) {
1432       _static_archive.print_on("", st);
1433     } else {
1434       if (DynamicArchive::is_mapped()) {
1435         _dynamic_archive.print_on("Dynamic ", st);
1436       }
1437     }
1438   }
1439 }
1440 
1441 void SystemDictionaryShared::print_on(outputStream* st) {
1442   print_shared_archive(st, true);
1443   print_shared_archive(st, false);
1444 }
1445 
1446 void SystemDictionaryShared::print_table_statistics(outputStream* st) {

1472     // must also be excluded.
1473     bool always_exclude = SystemDictionaryShared::check_for_exclusion(caller_ik, nullptr) ||
1474                           SystemDictionaryShared::check_for_exclusion(nest_host, nullptr);
1475 
1476     for (int i = info._proxy_klasses->length() - 1; i >= 0; i--) {
1477       InstanceKlass* ik = info._proxy_klasses->at(i);
1478       if (always_exclude || SystemDictionaryShared::check_for_exclusion(ik, nullptr)) {
1479         SystemDictionaryShared::reset_registered_lambda_proxy_class(ik);
1480         info._proxy_klasses->remove_at(i);
1481       }
1482     }
1483     return info._proxy_klasses->length() == 0 ? true /* delete the node*/ : false;
1484   }
1485 };
1486 
1487 void SystemDictionaryShared::cleanup_lambda_proxy_class_dictionary() {
1488   assert_lock_strong(DumpTimeTable_lock);
1489   CleanupDumpTimeLambdaProxyClassTable cleanup_proxy_classes;
1490   _dumptime_lambda_proxy_class_dictionary->unlink(&cleanup_proxy_classes);
1491 }









































































  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 "precompiled.hpp"
  26 #include "cds/archiveBuilder.hpp"
  27 #include "cds/archiveHeapLoader.hpp"
  28 #include "cds/archiveUtils.hpp"
  29 #include "cds/cdsConfig.hpp"
  30 #include "cds/classListParser.hpp"
  31 #include "cds/classListWriter.hpp"
  32 #include "cds/classPreinitializer.hpp"
  33 #include "cds/dynamicArchive.hpp"
  34 #include "cds/filemap.hpp"
  35 #include "cds/heapShared.hpp"
  36 #include "cds/cdsProtectionDomain.hpp"
  37 #include "cds/dumpTimeClassInfo.inline.hpp"
  38 #include "cds/lambdaFormInvokers.inline.hpp"
  39 #include "cds/metaspaceShared.hpp"
  40 #include "cds/methodDataDictionary.hpp"
  41 #include "cds/runTimeClassInfo.hpp"
  42 #include "classfile/classFileStream.hpp"
  43 #include "classfile/classLoader.hpp"
  44 #include "classfile/classLoaderData.inline.hpp"
  45 #include "classfile/classLoaderDataGraph.hpp"
  46 #include "classfile/classLoaderExt.hpp"
  47 #include "classfile/dictionary.hpp"
  48 #include "classfile/javaClasses.hpp"
  49 #include "classfile/javaClasses.inline.hpp"
  50 #include "classfile/symbolTable.hpp"
  51 #include "classfile/systemDictionary.hpp"
  52 #include "classfile/systemDictionaryShared.hpp"
  53 #include "classfile/verificationType.hpp"
  54 #include "classfile/vmClasses.hpp"
  55 #include "classfile/vmSymbols.hpp"
  56 #include "interpreter/bootstrapInfo.hpp"
  57 #include "jfr/jfrEvents.hpp"
  58 #include "logging/log.hpp"
  59 #include "logging/logStream.hpp"
  60 #include "memory/allocation.hpp"
  61 #include "memory/metadataFactory.hpp"
  62 #include "memory/metaspaceClosure.hpp"
  63 #include "memory/oopFactory.hpp"
  64 #include "memory/resourceArea.hpp"
  65 #include "memory/universe.hpp"
  66 #include "oops/instanceKlass.hpp"
  67 #include "oops/klass.inline.hpp"
  68 #include "oops/methodData.hpp"
  69 #include "oops/trainingData.hpp"
  70 #include "oops/objArrayKlass.hpp"
  71 #include "oops/objArrayOop.inline.hpp"
  72 #include "oops/oop.inline.hpp"
  73 #include "oops/oopHandle.inline.hpp"
  74 #include "oops/typeArrayOop.inline.hpp"
  75 #include "runtime/arguments.hpp"
  76 #include "runtime/handles.inline.hpp"
  77 #include "runtime/java.hpp"
  78 #include "runtime/javaCalls.hpp"
  79 #include "runtime/mutexLocker.hpp"
  80 #include "utilities/resourceHash.hpp"
  81 #include "utilities/stringUtils.hpp"
  82 
  83 SystemDictionaryShared::ArchiveInfo SystemDictionaryShared::_static_archive;
  84 SystemDictionaryShared::ArchiveInfo SystemDictionaryShared::_dynamic_archive;
  85 
  86 DumpTimeSharedClassTable* SystemDictionaryShared::_dumptime_table = nullptr;
  87 DumpTimeLambdaProxyClassDictionary* SystemDictionaryShared::_dumptime_lambda_proxy_class_dictionary = nullptr;
  88 
  89 DumpTimeMethodInfoDictionary* SystemDictionaryShared::_dumptime_method_info_dictionary = nullptr;
  90 DumpTimeMethodInfoDictionary* SystemDictionaryShared::_cloned_dumptime_method_info_dictionary = nullptr;
  91 static Array<InstanceKlass*>* _archived_lambda_form_classes = nullptr;
  92 static Array<InstanceKlass*>* _archived_lambda_proxy_classes_boot = nullptr;
  93 static Array<InstanceKlass*>* _archived_lambda_proxy_classes_boot2 = nullptr;
  94 static Array<InstanceKlass*>* _archived_lambda_proxy_classes_platform = nullptr;
  95 static Array<InstanceKlass*>* _archived_lambda_proxy_classes_app = nullptr;
  96 static bool _ignore_new_classes = false;
  97 
  98 // Used by NoClassLoadingMark
  99 DEBUG_ONLY(bool SystemDictionaryShared::_class_loading_may_happen = true;)
 100 
 101 InstanceKlass* SystemDictionaryShared::load_shared_class_for_builtin_loader(
 102                  Symbol* class_name, Handle class_loader, TRAPS) {
 103   assert(CDSConfig::is_using_archive(), "must be");
 104   InstanceKlass* ik = find_builtin_class(class_name);
 105 
 106   if (ik != nullptr && !ik->shared_loading_failed()) {
 107     if ((SystemDictionary::is_system_class_loader(class_loader()) && ik->is_shared_app_class())  ||
 108         (SystemDictionary::is_platform_class_loader(class_loader()) && ik->is_shared_platform_class())) {
 109       SharedClassLoadingMark slm(THREAD, ik);
 110       PackageEntry* pkg_entry = CDSProtectionDomain::get_package_entry_from_class(ik, class_loader);
 111       Handle protection_domain;
 112       if (!class_name->starts_with("jdk/proxy")) // java/lang/reflect/Proxy$ProxyBuilder defines the proxy classes with a null protection domain.
 113       {
 114         protection_domain = CDSProtectionDomain::init_security_info(class_loader, ik, pkg_entry, CHECK_NULL);
 115       }
 116       return load_shared_class(ik, class_loader, protection_domain, nullptr, pkg_entry, THREAD);
 117     }
 118   }
 119   return nullptr;
 120 }
 121 
 122 // This function is called for loading only UNREGISTERED classes
 123 InstanceKlass* SystemDictionaryShared::lookup_from_stream(Symbol* class_name,
 124                                                           Handle class_loader,
 125                                                           Handle protection_domain,
 126                                                           const ClassFileStream* cfs,
 127                                                           TRAPS) {
 128   if (!CDSConfig::is_using_archive()) {
 129     return nullptr;
 130   }
 131   if (class_name == nullptr) {  // don't do this for hidden classes
 132     return nullptr;
 133   }
 134   if (class_loader.is_null() ||
 135       SystemDictionary::is_system_class_loader(class_loader()) ||

 182   loader_data->add_class(ik);
 183 
 184   // Get the package entry.
 185   PackageEntry* pkg_entry = CDSProtectionDomain::get_package_entry_from_class(ik, class_loader);
 186 
 187   // Load and check super/interfaces, restore unshareable info
 188   InstanceKlass* shared_klass = load_shared_class(ik, class_loader, protection_domain,
 189                                                   cfs, pkg_entry, THREAD);
 190   if (shared_klass == nullptr || HAS_PENDING_EXCEPTION) {
 191     // TODO: clean up <ik> so it can be used again
 192     return nullptr;
 193   }
 194 
 195   return shared_klass;
 196 }
 197 
 198 // Guaranteed to return non-null value for non-shared classes.
 199 // k must not be a shared class.
 200 DumpTimeClassInfo* SystemDictionaryShared::get_info(InstanceKlass* k) {
 201   MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
 202 //assert(!k->is_shared(), "sanity"); // FIXME new workflow
 203   return get_info_locked(k);
 204 }
 205 
 206 DumpTimeClassInfo* SystemDictionaryShared::get_info_locked(InstanceKlass* k) {
 207   assert_lock_strong(DumpTimeTable_lock);
 208 //assert(!k->is_shared(), "sanity"); // FIXME new workflow
 209   DumpTimeClassInfo* info = _dumptime_table->get_info(k);
 210   assert(info != nullptr, "must be");
 211   return info;
 212 }
 213 
 214 void SystemDictionaryShared::mark_required_class(InstanceKlass* k) {
 215   DumpTimeClassInfo* info = _dumptime_table->get(k);
 216   ResourceMark rm;
 217   if (info != nullptr) {
 218     info->set_is_required();
 219   }
 220 }
 221 
 222 bool SystemDictionaryShared::check_for_exclusion(InstanceKlass* k, DumpTimeClassInfo* info) {
 223   if (!CDSConfig::is_dumping_final_static_archive() && MetaspaceShared::is_in_shared_metaspace(k)) {
 224     // We have reached a super type that's already in the base archive. Treat it
 225     // as "not excluded".
 226     assert(CDSConfig::is_dumping_dynamic_archive(), "must be");
 227     return false;
 228   }
 229 
 230   if (info == nullptr) {
 231     info = _dumptime_table->get(k);
 232     assert(info != nullptr, "supertypes of any classes in _dumptime_table must either be shared, or must also be in _dumptime_table");
 233   }
 234 
 235   if (!info->has_checked_exclusion()) {
 236     if (check_for_exclusion_impl(k)) {
 237       info->set_excluded();
 238     }
 239     info->set_has_checked_exclusion();
 240   }
 241 
 242   return info->is_excluded();
 243 }

 269   if (info != nullptr) {
 270     info->_is_archived_lambda_proxy = false;
 271     info->set_excluded();
 272   }
 273 }
 274 
 275 bool SystemDictionaryShared::is_early_klass(InstanceKlass* ik) {
 276   DumpTimeClassInfo* info = _dumptime_table->get(ik);
 277   return (info != nullptr) ? info->is_early_klass() : false;
 278 }
 279 
 280 bool SystemDictionaryShared::is_hidden_lambda_proxy(InstanceKlass* ik) {
 281   assert(ik->is_shared(), "applicable to only a shared class");
 282   if (ik->is_hidden()) {
 283     return true;
 284   } else {
 285     return false;
 286   }
 287 }
 288 
 289 void SystemDictionaryShared::ignore_new_classes() {
 290   _ignore_new_classes = true;
 291 }
 292 
 293 
 294 bool SystemDictionaryShared::check_for_exclusion_impl(InstanceKlass* k) {
 295   if (CDSConfig::is_dumping_final_static_archive() && k->is_shared_unregistered_class()
 296       && k->is_shared()) {
 297     return false; // Do not exclude: unregistered classes are passed from preimage to final image.
 298   }
 299 
 300   if (k->is_in_error_state()) {
 301     return warn_excluded(k, "In error state");
 302   }
 303   if (k->is_scratch_class()) {
 304     return warn_excluded(k, "A scratch class");
 305   }
 306   if (!k->is_loaded()) {
 307     return warn_excluded(k, "Not in loaded state");
 308   }
 309   if (has_been_redefined(k)) {
 310     return warn_excluded(k, "Has been redefined");
 311   }
 312   if (!k->is_hidden() && k->shared_classpath_index() < 0 && is_builtin(k)) {
 313     // These are classes loaded from unsupported locations (such as those loaded by JVMTI native
 314     // agent during dump time).
 315     return warn_excluded(k, "Unsupported location");
 316   }
 317   if (k->signers() != nullptr) {
 318     // We cannot include signed classes in the archive because the certificates
 319     // used during dump time may be different than those used during
 320     // runtime (due to expiration, etc).
 321     return warn_excluded(k, "Signed JAR");
 322   }
 323   if (is_jfr_event_class(k)) {
 324     // We cannot include JFR event classes because they need runtime-specific
 325     // instrumentation in order to work with -XX:FlightRecorderOptions:retransform=false.
 326     // There are only a small number of these classes, so it's not worthwhile to
 327     // support them and make CDS more complicated.
 328     if (!CDSConfig::is_dumping_reflection_data()) { // FIXME: !!! HACK !!!
 329       return warn_excluded(k, "JFR event class");
 330     }
 331   }
 332 
 333   if (!CDSConfig::preserve_all_dumptime_verification_states(k)) {
 334     if (!k->is_linked()) {
 335       if (has_class_failed_verification(k)) {
 336         return warn_excluded(k, "Failed verification");
 337       }
 338     } else {
 339       if (!k->can_be_verified_at_dumptime()) {
 340         // We have an old class that has been linked (e.g., it's been executed during
 341         // dump time). This class has been verified using the old verifier, which
 342         // doesn't save the verification constraints, so check_verification_constraints()
 343         // won't work at runtime.
 344         // As a result, we cannot store this class. It must be loaded and fully verified
 345         // at runtime.
 346         return warn_excluded(k, "Old class has been linked");
 347       }
 348     }
 349   }
 350 
 351   if (k->is_hidden() && !should_hidden_class_be_archived(k)) {
 352     log_info(cds)("Skipping %s: Hidden class", k->name()->as_C_string());

 353     return true;
 354   }
 355 
 356   InstanceKlass* super = k->java_super();
 357   if (super != nullptr && check_for_exclusion(super, nullptr)) {
 358     ResourceMark rm;
 359     log_warning(cds)("Skipping %s: super class %s is excluded", k->name()->as_C_string(), super->name()->as_C_string());
 360     return true;
 361   }
 362 
 363   Array<InstanceKlass*>* interfaces = k->local_interfaces();
 364   int len = interfaces->length();
 365   for (int i = 0; i < len; i++) {
 366     InstanceKlass* intf = interfaces->at(i);
 367     if (check_for_exclusion(intf, nullptr)) {
 368       ResourceMark rm;
 369       log_warning(cds)("Skipping %s: interface %s is excluded", k->name()->as_C_string(), intf->name()->as_C_string());
 370       return true;
 371     }
 372   }

 524     // The VM is not trying to resolve a super type of parser->current_class_name().
 525     // Instead, it's resolving an error class (because parser->current_class_name() has
 526     // failed parsing or verification). Don't do anything here.
 527     return nullptr;
 528   }
 529 }
 530 
 531 void SystemDictionaryShared::set_shared_class_misc_info(InstanceKlass* k, ClassFileStream* cfs) {
 532   assert(CDSConfig::is_dumping_archive(), "sanity");
 533   assert(!is_builtin(k), "must be unregistered class");
 534   DumpTimeClassInfo* info = get_info(k);
 535   info->_clsfile_size  = cfs->length();
 536   info->_clsfile_crc32 = ClassLoader::crc32(0, (const char*)cfs->buffer(), cfs->length());
 537 }
 538 
 539 void SystemDictionaryShared::initialize() {
 540   if (CDSConfig::is_dumping_archive()) {
 541     _dumptime_table = new (mtClass) DumpTimeSharedClassTable;
 542     _dumptime_lambda_proxy_class_dictionary =
 543                       new (mtClass) DumpTimeLambdaProxyClassDictionary;
 544     _dumptime_method_info_dictionary = new (mtClass) DumpTimeMethodInfoDictionary;
 545   }
 546 }
 547 
 548 void SystemDictionaryShared::init_dumptime_info(InstanceKlass* k) {
 549   MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
 550   assert(SystemDictionaryShared::class_loading_may_happen(), "sanity");
 551   DumpTimeClassInfo* info = _dumptime_table->allocate_info(k);
 552   if (_ignore_new_classes) {
 553     if (!LambdaFormInvokers::may_be_regenerated_class(k->name())) {
 554       ResourceMark rm;
 555       log_debug(cds)("Skipping %s: Class loaded for lambda form invoker regeneration", k->name()->as_C_string());
 556       info->set_excluded();
 557     }
 558   }
 559 }
 560 
 561 void SystemDictionaryShared::remove_dumptime_info(InstanceKlass* k) {
 562   MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
 563   _dumptime_table->remove(k);
 564 }
 565 
 566 void SystemDictionaryShared::handle_class_unloading(InstanceKlass* klass) {
 567   if (CDSConfig::is_dumping_archive()) {
 568     remove_dumptime_info(klass);
 569   }
 570 
 571   if (CDSConfig::is_dumping_archive() || ClassListWriter::is_enabled()) {
 572     MutexLocker ml(Thread::current(), UnregisteredClassesTable_lock, Mutex::_no_safepoint_check_flag);
 573     if (_unregistered_classes_table != nullptr) {
 574       // Remove the class from _unregistered_classes_table: keep the entry but
 575       // set it to null. This ensure no classes with the same name can be
 576       // added again.
 577       InstanceKlass** v = _unregistered_classes_table->get(klass->name());
 578       if (v != nullptr) {

 600   Array<InstanceKlass*>* interfaces = k->local_interfaces();
 601   int len = interfaces->length();
 602   for (int i = 0; i < len; i++) {
 603     if (has_been_redefined(interfaces->at(i))) {
 604       return true;
 605     }
 606   }
 607   return false;
 608 }
 609 
 610 // k is a class before relocating by ArchiveBuilder
 611 void SystemDictionaryShared::validate_before_archiving(InstanceKlass* k) {
 612   ResourceMark rm;
 613   const char* name = k->name()->as_C_string();
 614   DumpTimeClassInfo* info = _dumptime_table->get(k);
 615   assert(!class_loading_may_happen(), "class loading must be disabled");
 616   guarantee(info != nullptr, "Class %s must be entered into _dumptime_table", name);
 617   guarantee(!info->is_excluded(), "Should not attempt to archive excluded class %s", name);
 618   if (is_builtin(k)) {
 619     if (k->is_hidden()) {
 620       if (CDSConfig::is_dumping_invokedynamic()) { // FIXME -- clean up
 621         return;
 622       }
 623       assert(should_hidden_class_be_archived(k), "unexpected hidden class %s", name);
 624     }
 625     guarantee(!k->is_shared_unregistered_class(),
 626               "Class loader type must be set for BUILTIN class %s", name);
 627 
 628   } else {
 629     guarantee(k->is_shared_unregistered_class(),
 630               "Class loader type must not be set for UNREGISTERED class %s", name);
 631   }
 632 }
 633 
 634 class UnregisteredClassesDuplicationChecker : StackObj {
 635   GrowableArray<InstanceKlass*> _list;
 636   Thread* _thread;
 637 public:
 638   UnregisteredClassesDuplicationChecker() : _thread(Thread::current()) {}
 639 
 640   void do_entry(InstanceKlass* k, DumpTimeClassInfo& info) {
 641     if (!SystemDictionaryShared::is_builtin(k)) {
 642       _list.append(k);
 643     }

 655   }
 656 
 657   void mark_duplicated_classes() {
 658     // Two loaders may load two identical or similar hierarchies of classes. If we
 659     // check for duplication in random order, we may end up excluding important base classes
 660     // in both hierarchies, causing most of the classes to be excluded.
 661     // We sort the classes by their loaders. This way we're likely to archive
 662     // all classes in the one of the two hierarchies.
 663     _list.sort(compare_by_loader);
 664     for (int i = 0; i < _list.length(); i++) {
 665       InstanceKlass* k = _list.at(i);
 666       bool i_am_first = SystemDictionaryShared::add_unregistered_class(_thread, k);
 667       if (!i_am_first) {
 668         SystemDictionaryShared::warn_excluded(k, "Duplicated unregistered class");
 669         SystemDictionaryShared::set_excluded_locked(k);
 670       }
 671     }
 672   }
 673 };
 674 
 675 void SystemDictionaryShared::scan_constant_pool(InstanceKlass* k) {
 676   k->constants()->find_archivable_hidden_classes();
 677 }
 678 
 679 bool SystemDictionaryShared::should_hidden_class_be_archived(InstanceKlass* k) {
 680   assert(k->is_hidden(), "sanity");
 681   if (is_registered_lambda_proxy_class(k)) {
 682     return true;
 683   }
 684 
 685   if (CDSConfig::is_dumping_invokedynamic()) {
 686     if (HeapShared::is_archivable_hidden_klass(k)) {
 687       return true;
 688     }
 689 
 690     // TODO: merge the following with HeapShared::is_archivable_hidden_klass()
 691     DumpTimeClassInfo* info = _dumptime_table->get(k);
 692     if (info != nullptr && info->is_required()) {
 693       return true;
 694     }
 695   }
 696 
 697   return false;
 698 }
 699 
 700 void SystemDictionaryShared::check_excluded_classes() {
 701   assert(!class_loading_may_happen(), "class loading must be disabled");
 702   assert_lock_strong(DumpTimeTable_lock);
 703 
 704   if (CDSConfig::is_dumping_dynamic_archive()) {
 705     // Do this first -- if a base class is excluded due to duplication,
 706     // all of its subclasses will also be excluded.
 707     ResourceMark rm;
 708     UnregisteredClassesDuplicationChecker dup_checker;
 709     _dumptime_table->iterate_all_live_classes(&dup_checker);
 710     dup_checker.mark_duplicated_classes();
 711   }
 712 
 713   ResourceMark rm;
 714 
 715   // First, scan all non-hidden classes
 716   auto check_non_hidden = [&] (InstanceKlass* k, DumpTimeClassInfo& info) {
 717     if (!k->is_hidden()) {
 718       SystemDictionaryShared::check_for_exclusion(k, &info);
 719       if (!info.is_excluded() && !info.has_scanned_constant_pool()) {
 720         scan_constant_pool(k);
 721         info.set_has_scanned_constant_pool();
 722       }
 723     }
 724   };
 725   _dumptime_table->iterate_all_live_classes(check_non_hidden);
 726 
 727   // Then, scan all that hidden classes that have been marked as required, until
 728   // we reach a stable state.
 729   bool made_progress;
 730   do {
 731     made_progress = false;
 732     auto check_hidden = [&] (InstanceKlass* k, DumpTimeClassInfo& info) {
 733       if (k->is_hidden() && should_hidden_class_be_archived(k)) {
 734         SystemDictionaryShared::check_for_exclusion(k, &info);
 735         if (info.is_excluded()) {
 736           assert(!info.is_required(), "A required hidden class cannot be marked as excluded");
 737         } else if (!info.has_scanned_constant_pool()) {
 738           scan_constant_pool(k);
 739           info.set_has_scanned_constant_pool();
 740           // The CP entries in k *MAY* refer to other hidden classes, so scan
 741           // every hidden class again.
 742           made_progress = true;
 743         }
 744       }
 745     };
 746     _dumptime_table->iterate_all_live_classes(check_hidden);
 747   } while (made_progress);
 748 
 749   // Now, all hidden classes that have not yet been scanned should be excluded
 750   auto exclude_remaining_hidden = [&] (InstanceKlass* k, DumpTimeClassInfo& info) {
 751     if (k->is_hidden() && !info.has_checked_exclusion()) {
 752       SystemDictionaryShared::check_for_exclusion(k, &info);
 753       assert(info.is_excluded(), "Must be");
 754     }
 755   };
 756   _dumptime_table->iterate_all_live_classes(exclude_remaining_hidden);
 757   _dumptime_table->update_counts();
 758 
 759   cleanup_lambda_proxy_class_dictionary();
 760 
 761   cleanup_method_info_dictionary();
 762 
 763   TrainingData::cleanup_training_data();
 764 }
 765 
 766 bool SystemDictionaryShared::is_excluded_class(InstanceKlass* k) {
 767   assert(!class_loading_may_happen(), "class loading must be disabled");
 768   assert_lock_strong(DumpTimeTable_lock);
 769   assert(CDSConfig::is_dumping_archive(), "sanity");
 770   DumpTimeClassInfo* p = get_info_locked(k);
 771   return p->is_excluded();
 772 }
 773 
 774 void SystemDictionaryShared::set_excluded_locked(InstanceKlass* k) {
 775   assert_lock_strong(DumpTimeTable_lock);
 776   assert(CDSConfig::is_dumping_archive(), "sanity");
 777   DumpTimeClassInfo* info = get_info_locked(k);
 778   info->set_excluded();
 779 }
 780 
 781 void SystemDictionaryShared::set_excluded(InstanceKlass* k) {
 782   assert(CDSConfig::is_dumping_archive(), "sanity");
 783   DumpTimeClassInfo* info = get_info(k);
 784   info->set_excluded();
 785 }
 786 
 787 void SystemDictionaryShared::set_class_has_failed_verification(InstanceKlass* ik) {
 788   assert(CDSConfig::is_dumping_archive(), "sanity");
 789   DumpTimeClassInfo* p = get_info(ik);
 790   p->set_failed_verification();
 791 }
 792 
 793 bool SystemDictionaryShared::has_class_failed_verification(InstanceKlass* ik) {
 794   assert(CDSConfig::is_dumping_archive(), "sanity");
 795   DumpTimeClassInfo* p = _dumptime_table->get(ik);
 796   return (p == nullptr) ? false : p->failed_verification();
 797 }
 798 
 799 void SystemDictionaryShared::dumptime_classes_do(class MetaspaceClosure* it) {
 800   assert_lock_strong(DumpTimeTable_lock);
 801 
 802   auto do_klass = [&] (InstanceKlass* k, DumpTimeClassInfo& info) {
 803     if (CDSConfig::is_dumping_final_static_archive() && !k->is_loaded()) {
 804       assert(k->is_shared_unregistered_class(), "must be");
 805       info.metaspace_pointers_do(it);
 806     } else if (k->is_loader_alive() && !info.is_excluded()) {
 807       info.metaspace_pointers_do(it);
 808     }
 809   };
 810   _dumptime_table->iterate_all_live_classes(do_klass);
 811 
 812   auto do_lambda = [&] (LambdaProxyClassKey& key, DumpTimeLambdaProxyClassInfo& info) {
 813     if (key.caller_ik()->is_loader_alive()) {
 814       info.metaspace_pointers_do(it);
 815       key.metaspace_pointers_do(it);
 816     }
 817   };
 818   _dumptime_lambda_proxy_class_dictionary->iterate_all(do_lambda);
 819 
 820   auto do_method_info = [&] (MethodDataKey& key, DumpTimeMethodDataInfo& info) {
 821     info.metaspace_pointers_do(it);
 822     key.metaspace_pointers_do(it);
 823   };
 824   _dumptime_method_info_dictionary->iterate_all(do_method_info);
 825 }
 826 
 827 bool SystemDictionaryShared::add_verification_constraint(InstanceKlass* k, Symbol* name,
 828          Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object) {
 829   assert(CDSConfig::is_dumping_archive(), "sanity");
 830   if (CDSConfig::is_dumping_dynamic_archive() && k->is_shared()) {
 831     // k is a new class in the static archive, but one of its supertypes is an old class, so k wasn't
 832     // verified during dump time. No need to record constraints as k won't be included in the dynamic archive.
 833     return false;
 834   }
 835   if (PreloadSharedClasses && is_builtin(k)) {
 836     // There's no need to save verification constraints
 837     return false;
 838   }
 839 
 840   DumpTimeClassInfo* info = get_info(k);
 841   info->add_verification_constraint(k, name, from_name, from_field_is_protected,
 842                                     from_is_array, from_is_object);
 843 
 844   if (CDSConfig::is_dumping_dynamic_archive()) {
 845     // For dynamic dumping, we can resolve all the constraint classes for all class loaders during
 846     // the initial run prior to creating the archive before vm exit. We will also perform verification
 847     // check when running with the archive.
 848     return false;
 849   } else {
 850     if (is_builtin(k)) {
 851       // For builtin class loaders, we can try to complete the verification check at dump time,
 852       // because we can resolve all the constraint classes. We will also perform verification check
 853       // when running with the archive.
 854       return false;
 855     } else {
 856       // For non-builtin class loaders, we cannot complete the verification check at dump time,
 857       // because at dump time we don't know how to resolve classes for such loaders.
 858       return true;
 859     }

 869 void SystemDictionaryShared::add_to_dump_time_lambda_proxy_class_dictionary(LambdaProxyClassKey& key,
 870                                                            InstanceKlass* proxy_klass) {
 871   assert_lock_strong(DumpTimeTable_lock);
 872 
 873   bool created;
 874   DumpTimeLambdaProxyClassInfo* info = _dumptime_lambda_proxy_class_dictionary->put_if_absent(key, &created);
 875   info->add_proxy_klass(proxy_klass);
 876   if (created) {
 877     ++_dumptime_lambda_proxy_class_dictionary->_count;
 878   }
 879 }
 880 
 881 void SystemDictionaryShared::add_lambda_proxy_class(InstanceKlass* caller_ik,
 882                                                     InstanceKlass* lambda_ik,
 883                                                     Symbol* invoked_name,
 884                                                     Symbol* invoked_type,
 885                                                     Symbol* method_type,
 886                                                     Method* member_method,
 887                                                     Symbol* instantiated_method_type,
 888                                                     TRAPS) {
 889   if (CDSConfig::is_dumping_invokedynamic()) {
 890     // The proxy classes will be accessible through the archived CP entries.
 891     return;
 892   }
 893   if (CDSConfig::is_dumping_preimage_static_archive() || CDSConfig::is_dumping_final_static_archive()) {
 894     // TODO: not supported in new workflow
 895     return;
 896   }
 897 
 898   assert(caller_ik->class_loader() == lambda_ik->class_loader(), "mismatched class loader");
 899   assert(caller_ik->class_loader_data() == lambda_ik->class_loader_data(), "mismatched class loader data");
 900   assert(java_lang_Class::class_data(lambda_ik->java_mirror()) == nullptr, "must not have class data");
 901 
 902   MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
 903 
 904   lambda_ik->assign_class_loader_type();
 905   lambda_ik->set_shared_classpath_index(caller_ik->shared_classpath_index());
 906   InstanceKlass* nest_host = caller_ik->nest_host(CHECK);
 907   assert(nest_host != nullptr, "unexpected nullptr nest_host");
 908 
 909   DumpTimeClassInfo* info = _dumptime_table->get(lambda_ik);
 910   if (info != nullptr && !lambda_ik->is_non_strong_hidden() && is_builtin(lambda_ik) && is_builtin(caller_ik)
 911       // Don't include the lambda proxy if its nest host is not in the "linked" state.
 912       && nest_host->is_linked()) {
 913     // Set _is_archived_lambda_proxy in DumpTimeClassInfo so that the lambda_ik
 914     // won't be excluded during dumping of shared archive. See ExcludeDumpTimeSharedClasses.
 915     info->_is_archived_lambda_proxy = true;
 916     info->set_nest_host(nest_host);
 917 
 918     LambdaProxyClassKey key(caller_ik,
 919                             invoked_name,
 920                             invoked_type,
 921                             method_type,
 922                             member_method,
 923                             instantiated_method_type);
 924     add_to_dump_time_lambda_proxy_class_dictionary(key, lambda_ik);
 925   }
 926 }
 927 
 928 InstanceKlass* SystemDictionaryShared::get_shared_lambda_proxy_class(InstanceKlass* caller_ik,
 929                                                                      Symbol* invoked_name,
 930                                                                      Symbol* invoked_type,
 931                                                                      Symbol* method_type,
 932                                                                      Method* member_method,
 933                                                                      Symbol* instantiated_method_type) {
 934   if (CDSConfig::is_dumping_final_static_archive()) {
 935     return nullptr;
 936   }
 937   MutexLocker ml(CDSLambda_lock, Mutex::_no_safepoint_check_flag);
 938   LambdaProxyClassKey key(caller_ik, invoked_name, invoked_type,
 939                           method_type, member_method, instantiated_method_type);
 940 
 941   // Try to retrieve the lambda proxy class from static archive.
 942   const RunTimeLambdaProxyClassInfo* info = _static_archive.lookup_lambda_proxy_class(&key);
 943   InstanceKlass* proxy_klass = retrieve_lambda_proxy_class(info);
 944   if (proxy_klass == nullptr) {
 945     if (info != nullptr && log_is_enabled(Debug, cds)) {
 946       ResourceMark rm;
 947       log_debug(cds)("Used all static archived lambda proxy classes for: %s %s%s",
 948                      caller_ik->external_name(), invoked_name->as_C_string(), invoked_type->as_C_string());
 949     }
 950   } else {
 951     return proxy_klass;
 952   }
 953 
 954   // Retrieving from static archive is unsuccessful, try dynamic archive.
 955   info = _dynamic_archive.lookup_lambda_proxy_class(&key);
 956   proxy_klass = retrieve_lambda_proxy_class(info);

 965 }
 966 
 967 InstanceKlass* SystemDictionaryShared::retrieve_lambda_proxy_class(const RunTimeLambdaProxyClassInfo* info) {
 968   InstanceKlass* proxy_klass = nullptr;
 969   if (info != nullptr) {
 970     InstanceKlass* curr_klass = info->proxy_klass_head();
 971     InstanceKlass* prev_klass = curr_klass;
 972     if (curr_klass->lambda_proxy_is_available()) {
 973       while (curr_klass->next_link() != nullptr) {
 974         prev_klass = curr_klass;
 975         curr_klass = InstanceKlass::cast(curr_klass->next_link());
 976       }
 977       assert(curr_klass->is_hidden(), "must be");
 978       assert(curr_klass->lambda_proxy_is_available(), "must be");
 979 
 980       prev_klass->set_next_link(nullptr);
 981       proxy_klass = curr_klass;
 982       proxy_klass->clear_lambda_proxy_is_available();
 983       if (log_is_enabled(Debug, cds)) {
 984         ResourceMark rm;
 985         log_debug(cds)("Loaded lambda proxy: " PTR_FORMAT " %s ", p2i(proxy_klass), proxy_klass->external_name());
 986       }
 987     }
 988   }
 989   return proxy_klass;
 990 }
 991 
 992 InstanceKlass* SystemDictionaryShared::get_shared_nest_host(InstanceKlass* lambda_ik) {
 993   assert(!CDSConfig::is_dumping_static_archive() && CDSConfig::is_using_archive(), "called at run time with CDS enabled only");
 994   RunTimeClassInfo* record = RunTimeClassInfo::get_for(lambda_ik);
 995   return record->nest_host();
 996 }
 997 
 998 InstanceKlass* SystemDictionaryShared::prepare_shared_lambda_proxy_class(InstanceKlass* lambda_ik,
 999                                                                          InstanceKlass* caller_ik, TRAPS) {
1000   Handle class_loader(THREAD, caller_ik->class_loader());
1001   Handle protection_domain;
1002   PackageEntry* pkg_entry = caller_ik->package();
1003   if (caller_ik->class_loader() != nullptr) {
1004     protection_domain = CDSProtectionDomain::init_security_info(class_loader, caller_ik, pkg_entry, CHECK_NULL);
1005   }

1024   // Add to class hierarchy, and do possible deoptimizations.
1025   loaded_lambda->add_to_hierarchy(THREAD);
1026   // But, do not add to dictionary.
1027 
1028   loaded_lambda->link_class(CHECK_NULL);
1029   // notify jvmti
1030   if (JvmtiExport::should_post_class_load()) {
1031     JvmtiExport::post_class_load(THREAD, loaded_lambda);
1032   }
1033   if (class_load_start_event.should_commit()) {
1034     SystemDictionary::post_class_load_event(&class_load_start_event, loaded_lambda, ClassLoaderData::class_loader_data(class_loader()));
1035   }
1036 
1037   loaded_lambda->initialize(CHECK_NULL);
1038 
1039   return loaded_lambda;
1040 }
1041 
1042 void SystemDictionaryShared::check_verification_constraints(InstanceKlass* klass,
1043                                                             TRAPS) {
1044   //assert(!CDSConfig::is_dumping_static_archive() && CDSConfig::is_using_archive(), "called at run time with CDS enabled only");
1045   RunTimeClassInfo* record = RunTimeClassInfo::get_for(klass);
1046 
1047   int length = record->_num_verifier_constraints;
1048   if (length > 0) {
1049     for (int i = 0; i < length; i++) {
1050       RunTimeClassInfo::RTVerifierConstraint* vc = record->verifier_constraint_at(i);
1051       Symbol* name      = vc->name();
1052       Symbol* from_name = vc->from_name();
1053       char c            = record->verifier_constraint_flag(i);
1054 
1055       if (log_is_enabled(Trace, cds, verification)) {
1056         ResourceMark rm(THREAD);
1057         log_trace(cds, verification)("check_verification_constraint: %s: %s must be subclass of %s [0x%x]",
1058                                      klass->external_name(), from_name->as_klass_external_name(),
1059                                      name->as_klass_external_name(), c);
1060       }
1061 
1062       bool from_field_is_protected = (c & SystemDictionaryShared::FROM_FIELD_IS_PROTECTED) ? true : false;
1063       bool from_is_array           = (c & SystemDictionaryShared::FROM_IS_ARRAY)           ? true : false;
1064       bool from_is_object          = (c & SystemDictionaryShared::FROM_IS_OBJECT)          ? true : false;

1134   assert(is_builtin(klass), "must be");
1135   assert(klass_loader != nullptr, "should not be called for boot loader");
1136   assert(loader1 != loader2, "must be");
1137 
1138   if (CDSConfig::is_dumping_dynamic_archive() && Thread::current()->is_VM_thread()) {
1139     // We are re-laying out the vtable/itables of the *copy* of
1140     // a class during the final stage of dynamic dumping. The
1141     // linking constraints for this class has already been recorded.
1142     return;
1143   }
1144   assert(!Thread::current()->is_VM_thread(), "must be");
1145 
1146   assert(CDSConfig::is_dumping_archive(), "sanity");
1147   DumpTimeClassInfo* info = get_info(klass);
1148   info->record_linking_constraint(name, loader1, loader2);
1149 }
1150 
1151 // returns true IFF there's no need to re-initialize the i/v-tables for klass for
1152 // the purpose of checking class loader constraints.
1153 bool SystemDictionaryShared::check_linking_constraints(Thread* current, InstanceKlass* klass) {
1154   //assert(!CDSConfig::is_dumping_static_archive() && CDSConfig::is_using_archive(), "called at run time with CDS enabled only");
1155   LogTarget(Info, class, loader, constraints) log;
1156   if (klass->is_shared_boot_class()) {
1157     // No class loader constraint check performed for boot classes.
1158     return true;
1159   }
1160   if (klass->is_shared_platform_class() || klass->is_shared_app_class()) {
1161     RunTimeClassInfo* info = RunTimeClassInfo::get_for(klass);
1162     assert(info != nullptr, "Sanity");
1163     if (info->_num_loader_constraints > 0) {
1164       HandleMark hm(current);
1165       for (int i = 0; i < info->_num_loader_constraints; i++) {
1166         RunTimeClassInfo::RTLoaderConstraint* lc = info->loader_constraint_at(i);
1167         Symbol* name = lc->constraint_name();
1168         Handle loader1(current, get_class_loader_by(lc->_loader_type1));
1169         Handle loader2(current, get_class_loader_by(lc->_loader_type2));
1170         if (log.is_enabled()) {
1171           ResourceMark rm(current);
1172           log.print("[CDS add loader constraint for class %s symbol %s loader[0] %s loader[1] %s",
1173                     klass->external_name(), name->as_C_string(),
1174                     ClassLoaderData::class_loader_data(loader1())->loader_name_and_id(),

1263     }
1264   }
1265 
1266   size_t total() {
1267     return _shared_class_info_size;
1268   }
1269 };
1270 
1271 size_t SystemDictionaryShared::estimate_size_for_archive() {
1272   EstimateSizeForArchive est;
1273   _dumptime_table->iterate_all_live_classes(&est);
1274   size_t total_size = est.total() +
1275     CompactHashtableWriter::estimate_size(_dumptime_table->count_of(true)) +
1276     CompactHashtableWriter::estimate_size(_dumptime_table->count_of(false));
1277 
1278   size_t bytesize = align_up(sizeof(RunTimeLambdaProxyClassInfo), SharedSpaceObjectAlignment);
1279   total_size +=
1280       (bytesize * _dumptime_lambda_proxy_class_dictionary->_count) +
1281       CompactHashtableWriter::estimate_size(_dumptime_lambda_proxy_class_dictionary->_count);
1282 
1283   size_t method_info_byte_size = align_up(sizeof(RunTimeMethodDataInfo), SharedSpaceObjectAlignment);
1284   total_size +=
1285       (method_info_byte_size * _dumptime_method_info_dictionary->_count) +
1286       CompactHashtableWriter::estimate_size(_dumptime_method_info_dictionary->_count);
1287 
1288   return total_size;
1289 }
1290 
1291 unsigned int SystemDictionaryShared::hash_for_shared_dictionary(address ptr) {
1292   if (ArchiveBuilder::is_active() && ArchiveBuilder::current()->is_in_buffer_space(ptr)) {
1293     uintx offset = ArchiveBuilder::current()->any_to_offset(ptr);
1294     unsigned int hash = primitive_hash<uintx>(offset);
1295     DEBUG_ONLY({
1296         if (MetaspaceObj::is_shared((const MetaspaceObj*)ptr)) {
1297           assert(hash == SystemDictionaryShared::hash_for_shared_dictionary_quick(ptr), "must be");
1298         }
1299       });
1300     return hash;
1301   } else {
1302     return SystemDictionaryShared::hash_for_shared_dictionary_quick(ptr);
1303   }
1304 }
1305 
1306 class CopyLambdaProxyClassInfoToArchive : StackObj {
1307   CompactHashtableWriter* _writer;
1308   ArchiveBuilder* _builder;
1309 public:
1310   CopyLambdaProxyClassInfoToArchive(CompactHashtableWriter* writer)
1311   : _writer(writer), _builder(ArchiveBuilder::current()) {}
1312   bool do_entry(LambdaProxyClassKey& key, DumpTimeLambdaProxyClassInfo& info) {

1385         ResourceMark rm;
1386         log_trace(cds,hashtables)("%s dictionary: %s", (_is_builtin ? "builtin" : "unregistered"), info._klass->external_name());
1387       }
1388 
1389       // Save this for quick runtime lookup of InstanceKlass* -> RunTimeClassInfo*
1390       InstanceKlass* buffered_klass = ArchiveBuilder::current()->get_buffered_addr(info._klass);
1391       RunTimeClassInfo::set_for(buffered_klass, record);
1392     }
1393   }
1394 };
1395 
1396 void SystemDictionaryShared::write_lambda_proxy_class_dictionary(LambdaProxyClassDictionary *dictionary) {
1397   CompactHashtableStats stats;
1398   dictionary->reset();
1399   CompactHashtableWriter writer(_dumptime_lambda_proxy_class_dictionary->_count, &stats);
1400   CopyLambdaProxyClassInfoToArchive copy(&writer);
1401   _dumptime_lambda_proxy_class_dictionary->iterate(&copy);
1402   writer.dump(dictionary, "lambda proxy class dictionary");
1403 }
1404 
1405 class CopyMethodDataInfoToArchive : StackObj {
1406   CompactHashtableWriter* _writer;
1407   ArchiveBuilder* _builder;
1408 public:
1409   CopyMethodDataInfoToArchive(CompactHashtableWriter* writer)
1410       : _writer(writer), _builder(ArchiveBuilder::current()) {}
1411 
1412   bool do_entry(MethodDataKey& key, DumpTimeMethodDataInfo& info) {
1413     Method* holder = key.method();
1414     log_info(cds,dynamic)("Archiving method info for %s", holder->external_name());
1415 
1416     size_t byte_size = sizeof(RunTimeMethodDataInfo);
1417     RunTimeMethodDataInfo* record = (RunTimeMethodDataInfo*)ArchiveBuilder::ro_region_alloc(byte_size);
1418 
1419     DumpTimeMethodDataInfo data(info.method_data(), info.method_counters());
1420     record->init(key, data);
1421 
1422     uint hash = SystemDictionaryShared::hash_for_shared_dictionary((address)holder);
1423     u4 delta = _builder->buffer_to_offset_u4((address)record);
1424     _writer->add(hash, delta);
1425 
1426     return true;
1427   }
1428 };
1429 
1430 void SystemDictionaryShared::write_method_info_dictionary(MethodDataInfoDictionary* dictionary) {
1431   CompactHashtableStats stats;
1432   dictionary->reset();
1433   CompactHashtableWriter writer(_dumptime_method_info_dictionary->_count, &stats);
1434   CopyMethodDataInfoToArchive copy(&writer);
1435   _dumptime_method_info_dictionary->iterate(&copy);
1436   writer.dump(dictionary, "method info dictionary");
1437 }
1438 
1439 void SystemDictionaryShared::write_dictionary(RunTimeSharedDictionary* dictionary,
1440                                               bool is_builtin) {
1441   CompactHashtableStats stats;
1442   dictionary->reset();
1443   CompactHashtableWriter writer(_dumptime_table->count_of(is_builtin), &stats);
1444   CopySharedClassInfoToArchive copy(&writer, is_builtin);
1445   assert_lock_strong(DumpTimeTable_lock);
1446   _dumptime_table->iterate_all_live_classes(&copy);
1447   writer.dump(dictionary, is_builtin ? "builtin dictionary" : "unregistered dictionary");
1448 }
1449 
1450 void SystemDictionaryShared::write_to_archive(bool is_static_archive) {
1451   ArchiveInfo* archive = get_archive(is_static_archive);
1452 
1453   write_dictionary(&archive->_builtin_dictionary, true);
1454   write_dictionary(&archive->_unregistered_dictionary, false);
1455 
1456   write_lambda_proxy_class_dictionary(&archive->_lambda_proxy_class_dictionary);
1457 
1458   write_method_info_dictionary(&archive->_method_info_dictionary);
1459 }
1460 
1461 void SystemDictionaryShared::adjust_lambda_proxy_class_dictionary() {
1462   AdjustLambdaProxyClassInfo adjuster;
1463   _dumptime_lambda_proxy_class_dictionary->iterate(&adjuster);
1464 }
1465 
1466 class AdjustMethodInfo : StackObj {
1467 public:
1468   AdjustMethodInfo() {}
1469   bool do_entry(MethodDataKey& key, DumpTimeMethodDataInfo& info) {
1470     // TODO: is it possible for the data to become stale/invalid?
1471     MethodData*     md = info.method_data();
1472     MethodCounters* mc = info.method_counters();
1473     if (md != nullptr) {
1474       md = ArchiveBuilder::current()->get_buffered_addr(md);
1475     }
1476     if (mc != nullptr) {
1477       mc = ArchiveBuilder::current()->get_buffered_addr(mc);
1478     }
1479     assert(ArchiveBuilder::current()->is_in_buffer_space(md) || md == nullptr, "must be");
1480     assert(ArchiveBuilder::current()->is_in_buffer_space(mc) || mc == nullptr, "must be");
1481     if (md != nullptr) {
1482       md->remove_unshareable_info();
1483     }
1484     if (mc != nullptr) {
1485       mc->remove_unshareable_info();
1486     }
1487     return true;
1488   }
1489 };
1490 
1491 void SystemDictionaryShared::adjust_method_info_dictionary() {
1492   AdjustMethodInfo adjuster;
1493   _dumptime_method_info_dictionary->iterate(&adjuster);
1494 }
1495 
1496 void SystemDictionaryShared::serialize_dictionary_headers(SerializeClosure* soc,
1497                                                           bool is_static_archive) {
1498   ArchiveInfo* archive = get_archive(is_static_archive);
1499 
1500   archive->_builtin_dictionary.serialize_header(soc);
1501   archive->_unregistered_dictionary.serialize_header(soc);
1502   archive->_lambda_proxy_class_dictionary.serialize_header(soc);
1503   archive->_method_info_dictionary.serialize_header(soc);
1504 }
1505 
1506 void SystemDictionaryShared::serialize_vm_classes(SerializeClosure* soc) {
1507   for (auto id : EnumRange<vmClassID>{}) {
1508     soc->do_ptr(vmClasses::klass_addr_at(id));
1509   }
1510   soc->do_ptr((void**)&_archived_lambda_form_classes);
1511   soc->do_ptr((void**)&_archived_lambda_proxy_classes_boot);
1512   soc->do_ptr((void**)&_archived_lambda_proxy_classes_boot2);
1513   soc->do_ptr((void**)&_archived_lambda_proxy_classes_platform);
1514   soc->do_ptr((void**)&_archived_lambda_proxy_classes_app);
1515 }
1516 
1517 const RunTimeClassInfo*
1518 SystemDictionaryShared::find_record(RunTimeSharedDictionary* static_dict, RunTimeSharedDictionary* dynamic_dict, Symbol* name) {
1519   if (!CDSConfig::is_using_archive() || !name->is_shared()) {
1520     // The names of all shared classes must also be a shared Symbol.
1521     return nullptr;
1522   }
1523 
1524   unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary_quick(name);
1525   const RunTimeClassInfo* record = nullptr;
1526   if (DynamicArchive::is_mapped()) {
1527     // Use the regenerated holder classes in the dynamic archive as they
1528     // have more methods than those in the base archive.
1529     if (LambdaFormInvokers::may_be_regenerated_class(name)) {



1530       record = dynamic_dict->lookup(name, hash, 0);
1531       if (record != nullptr) {
1532         return record;
1533       }
1534     }
1535   }
1536 
1537   if (!MetaspaceShared::is_shared_dynamic(name)) {
1538     // The names of all shared classes in the static dict must also be in the
1539     // static archive
1540     record = static_dict->lookup(name, hash, 0);
1541   }
1542 
1543   if (record == nullptr && DynamicArchive::is_mapped()) {
1544     record = dynamic_dict->lookup(name, hash, 0);
1545   }
1546 
1547   return record;
1548 }
1549 

1554   if (record != nullptr) {
1555     assert(!record->_klass->is_hidden(), "hidden class cannot be looked up by name");
1556     assert(check_alignment(record->_klass), "Address not aligned");
1557     // We did not save the classfile data of the generated LambdaForm invoker classes,
1558     // so we cannot support CLFH for such classes.
1559     if (record->_klass->is_generated_shared_class() && JvmtiExport::should_post_class_file_load_hook()) {
1560        return nullptr;
1561     }
1562     return record->_klass;
1563   } else {
1564     return nullptr;
1565   }
1566 }
1567 
1568 void SystemDictionaryShared::update_shared_entry(InstanceKlass* k, int id) {
1569   assert(CDSConfig::is_dumping_static_archive(), "class ID is used only for static dump (from classlist)");
1570   DumpTimeClassInfo* info = get_info(k);
1571   info->_id = id;
1572 }
1573 
1574 const char* SystemDictionaryShared::class_loader_name_for_shared(Klass* k) {
1575   assert(k != nullptr, "Sanity");
1576   assert(k->is_shared(), "Must be");
1577   assert(k->is_instance_klass(), "Must be");
1578   InstanceKlass* ik = InstanceKlass::cast(k);
1579   if (ik->is_shared_boot_class()) {
1580     return "boot_loader";
1581   } else if (ik->is_shared_platform_class()) {
1582     return "platform_loader";
1583   } else if (ik->is_shared_app_class()) {
1584     return "app_loader";
1585   } else if (ik->is_shared_unregistered_class()) {
1586     return "unregistered_loader";
1587   } else {
1588     return "unknown loader";
1589   }
1590 }
1591 
1592 class SharedDictionaryPrinter : StackObj {
1593   outputStream* _st;
1594   int _index;
1595 public:
1596   SharedDictionaryPrinter(outputStream* st) : _st(st), _index(0) {}
1597 
1598   void do_value(const RunTimeClassInfo* record) {
1599     ResourceMark rm;
1600     _st->print_cr("%4d: %s %s", _index++, record->_klass->external_name(),
1601         SystemDictionaryShared::class_loader_name_for_shared(record->_klass));
1602     if (record->_klass->array_klasses() != nullptr) {
1603       record->_klass->array_klasses()->cds_print_value_on(_st);
1604       _st->cr();
1605     }
1606   }
1607   int index() const { return _index; }
1608 };
1609 
1610 class SharedLambdaDictionaryPrinter : StackObj {
1611   outputStream* _st;
1612   int _index;
1613 public:
1614   SharedLambdaDictionaryPrinter(outputStream* st, int idx) : _st(st), _index(idx) {}
1615 
1616   void do_value(const RunTimeLambdaProxyClassInfo* record) {
1617     if (record->proxy_klass_head()->lambda_proxy_is_available()) {
1618       ResourceMark rm;
1619       Klass* k = record->proxy_klass_head();
1620       while (k != nullptr) {
1621         _st->print_cr("%4d: %s %s", _index++, k->external_name(),
1622                       SystemDictionaryShared::class_loader_name_for_shared(k));
1623         k = k->next_link();
1624       }
1625     }
1626   }
1627 };
1628 
1629 class SharedMethodInfoDictionaryPrinter : StackObj {
1630   outputStream* _st;
1631   int _index;
1632 
1633 private:
1634   static const char* tag(void* p) {
1635     if (p == nullptr) {
1636       return "   ";
1637     } else if (MetaspaceShared::is_shared_dynamic(p)) {
1638       return "<D>";
1639     } else if (MetaspaceShared::is_in_shared_metaspace(p)) {
1640       return "<S>";
1641     } else {
1642       return "???";
1643     }
1644   }
1645 public:
1646   SharedMethodInfoDictionaryPrinter(outputStream* st) : _st(st), _index(0) {}
1647 
1648   void do_value(const RunTimeMethodDataInfo* record) {
1649     ResourceMark rm;
1650     Method*         m  = record->method();
1651     MethodCounters* mc = record->method_counters();
1652     MethodData*     md = record->method_data();
1653 
1654     _st->print_cr("%4d: %s" PTR_FORMAT " %s" PTR_FORMAT " %s" PTR_FORMAT " %s", _index++,
1655                   tag(m), p2i(m),
1656                   tag(mc), p2i(mc),
1657                   tag(md), p2i(md),
1658                   m->external_name());
1659     if (Verbose) {
1660       if (mc != nullptr) {
1661         mc->print_on(_st);
1662       }
1663       if (md != nullptr) {
1664         md->print_on(_st);
1665       }
1666       _st->cr();
1667     }
1668   }
1669 };
1670 
1671 void SystemDictionaryShared::ArchiveInfo::print_on(const char* prefix,
1672                                                    outputStream* st) {
1673   st->print_cr("%sShared Dictionary", prefix);
1674   SharedDictionaryPrinter p(st);
1675   st->print_cr("%sShared Builtin Dictionary", prefix);
1676   _builtin_dictionary.iterate(&p);
1677   st->print_cr("%sShared Unregistered Dictionary", prefix);
1678   _unregistered_dictionary.iterate(&p);
1679   if (!_lambda_proxy_class_dictionary.empty()) {
1680     st->print_cr("%sShared Lambda Dictionary", prefix);
1681     SharedLambdaDictionaryPrinter ldp(st, p.index());
1682     _lambda_proxy_class_dictionary.iterate(&ldp);
1683   }
1684   if (!_method_info_dictionary.empty()) {
1685     st->print_cr("%sShared MethodData Dictionary", prefix);
1686     SharedMethodInfoDictionaryPrinter mdp(st);
1687     _method_info_dictionary.iterate(&mdp);
1688   }
1689 }
1690 
1691 void SystemDictionaryShared::ArchiveInfo::print_table_statistics(const char* prefix,
1692                                                                  outputStream* st) {
1693   st->print_cr("%sArchve Statistics", prefix);
1694   _builtin_dictionary.print_table_statistics(st, "Builtin Shared Dictionary");
1695   _unregistered_dictionary.print_table_statistics(st, "Unregistered Shared Dictionary");
1696   _lambda_proxy_class_dictionary.print_table_statistics(st, "Lambda Shared Dictionary");
1697   _method_info_dictionary.print_table_statistics(st, "MethodData Dictionary");
1698 }
1699 
1700 void SystemDictionaryShared::print_shared_archive(outputStream* st, bool is_static) {
1701   if (CDSConfig::is_using_archive()) {
1702     if (is_static) {
1703       _static_archive.print_on("", st);
1704     } else {
1705       if (DynamicArchive::is_mapped()) {
1706         _dynamic_archive.print_on("Dynamic ", st);
1707       }
1708     }
1709   }
1710 }
1711 
1712 void SystemDictionaryShared::print_on(outputStream* st) {
1713   print_shared_archive(st, true);
1714   print_shared_archive(st, false);
1715 }
1716 
1717 void SystemDictionaryShared::print_table_statistics(outputStream* st) {

1743     // must also be excluded.
1744     bool always_exclude = SystemDictionaryShared::check_for_exclusion(caller_ik, nullptr) ||
1745                           SystemDictionaryShared::check_for_exclusion(nest_host, nullptr);
1746 
1747     for (int i = info._proxy_klasses->length() - 1; i >= 0; i--) {
1748       InstanceKlass* ik = info._proxy_klasses->at(i);
1749       if (always_exclude || SystemDictionaryShared::check_for_exclusion(ik, nullptr)) {
1750         SystemDictionaryShared::reset_registered_lambda_proxy_class(ik);
1751         info._proxy_klasses->remove_at(i);
1752       }
1753     }
1754     return info._proxy_klasses->length() == 0 ? true /* delete the node*/ : false;
1755   }
1756 };
1757 
1758 void SystemDictionaryShared::cleanup_lambda_proxy_class_dictionary() {
1759   assert_lock_strong(DumpTimeTable_lock);
1760   CleanupDumpTimeLambdaProxyClassTable cleanup_proxy_classes;
1761   _dumptime_lambda_proxy_class_dictionary->unlink(&cleanup_proxy_classes);
1762 }
1763 
1764 class CleanupDumpTimeMethodInfoTable : StackObj {
1765 public:
1766   bool do_entry(MethodDataKey& key, DumpTimeMethodDataInfo& info) {
1767     assert_lock_strong(DumpTimeTable_lock);
1768     assert(MetaspaceShared::is_in_shared_metaspace(key.method()), "");
1769     InstanceKlass* holder = key.method()->method_holder();
1770     bool is_excluded = SystemDictionaryShared::check_for_exclusion(holder, nullptr);
1771     return is_excluded;
1772   }
1773 };
1774 
1775 void SystemDictionaryShared::cleanup_method_info_dictionary() {
1776   assert_lock_strong(DumpTimeTable_lock);
1777 
1778   CleanupDumpTimeMethodInfoTable cleanup_method_info;
1779   _dumptime_method_info_dictionary->unlink(&cleanup_method_info);
1780 }
1781 
1782 void SystemDictionaryShared::create_loader_positive_lookup_cache(TRAPS) {
1783   GrowableArray<InstanceKlass*> shared_classes_list;
1784   {
1785     // With static dumping, we have only a single Java thread (see JVM_StartThread) so
1786     // no no other threads should be loading classes. Otherwise, the code below may miss some
1787     // classes that are loaded concurrently.
1788     assert(CDSConfig::is_dumping_static_archive(), "no other threads should be loading classes");
1789 
1790     MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
1791     _dumptime_table->iterate_all_classes_in_builtin_loaders([&](InstanceKlass* k, DumpTimeClassInfo& info) {
1792         if (!k->is_hidden() && !check_for_exclusion(k, &info)) {
1793           shared_classes_list.append(k);
1794         }
1795       }
1796     );
1797   }
1798 
1799   InstanceKlass* ik = vmClasses::Class_klass();
1800   objArrayOop r = oopFactory::new_objArray(ik, shared_classes_list.length(), CHECK);
1801   objArrayHandle array_h(THREAD, r);
1802 
1803   for (int i = 0; i < shared_classes_list.length(); i++) {
1804     oop mirror = shared_classes_list.at(i)->java_mirror();
1805     Handle mirror_h(THREAD, mirror);
1806     array_h->obj_at_put(i, mirror_h());
1807   }
1808 
1809   TempNewSymbol method = SymbolTable::new_symbol("generatePositiveLookupCache");
1810   TempNewSymbol signature = SymbolTable::new_symbol("([Ljava/lang/Class;)V");
1811 
1812   JavaCallArguments args(Handle(THREAD, SystemDictionary::java_system_loader()));
1813   args.push_oop(array_h);
1814   JavaValue result(T_VOID);
1815   JavaCalls::call_virtual(&result,
1816                           vmClasses::jdk_internal_loader_ClassLoaders_AppClassLoader_klass(),
1817                           method,
1818                           signature,
1819                           &args,
1820                           CHECK);
1821 
1822   if (HAS_PENDING_EXCEPTION) {
1823     Handle exc_handle(THREAD, PENDING_EXCEPTION);
1824     CLEAR_PENDING_EXCEPTION;
1825     ResourceMark rm(THREAD);
1826 
1827     log_warning(cds)("Exception during AppClassLoader::generatePositiveLookupCache() call");
1828     LogStreamHandle(Debug, cds) log;
1829     if (log.is_enabled()) {
1830       java_lang_Throwable::print_stack_trace(exc_handle, &log);
1831     }
1832     return;
1833   }
1834 }
< prev index next >