< prev index next >

src/hotspot/share/classfile/systemDictionaryShared.cpp

Print this page

  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(UseSharedSpaces, "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 (!UseSharedSpaces) {
 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 }

 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   }
 334 

 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()) {

 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);

 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() && UseSharedSpaces, "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() && UseSharedSpaces, "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() && UseSharedSpaces, "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 (!UseSharedSpaces || !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 (UseSharedSpaces) {
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 }






















































































































































































































  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/heapShared.hpp"
  35 #include "cds/cdsProtectionDomain.hpp"
  36 #include "cds/dumpTimeClassInfo.inline.hpp"
  37 #include "cds/lambdaFormInvokers.inline.hpp"
  38 #include "cds/metaspaceShared.hpp"
  39 #include "cds/methodDataDictionary.hpp"
  40 #include "cds/runTimeClassInfo.hpp"
  41 #include "classfile/classFileStream.hpp"
  42 #include "classfile/classLoader.hpp"
  43 #include "classfile/classLoaderData.inline.hpp"
  44 #include "classfile/classLoaderDataGraph.hpp"
  45 #include "classfile/classLoaderExt.hpp"
  46 #include "classfile/dictionary.hpp"
  47 #include "classfile/javaClasses.hpp"
  48 #include "classfile/javaClasses.inline.hpp"
  49 #include "classfile/symbolTable.hpp"
  50 #include "classfile/systemDictionary.hpp"
  51 #include "classfile/systemDictionaryShared.hpp"
  52 #include "classfile/verificationType.hpp"
  53 #include "classfile/vmClasses.hpp"
  54 #include "classfile/vmSymbols.hpp"
  55 #include "interpreter/bootstrapInfo.hpp"
  56 #include "jfr/jfrEvents.hpp"
  57 #include "logging/log.hpp"
  58 #include "logging/logStream.hpp"
  59 #include "memory/allocation.hpp"
  60 #include "memory/metadataFactory.hpp"
  61 #include "memory/metaspaceClosure.hpp"
  62 #include "memory/oopFactory.hpp"
  63 #include "memory/resourceArea.hpp"
  64 #include "memory/universe.hpp"
  65 #include "oops/fieldStreams.inline.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 
  97 // Used by NoClassLoadingMark
  98 DEBUG_ONLY(bool SystemDictionaryShared::_class_loading_may_happen = true;)
  99 
 100 InstanceKlass* SystemDictionaryShared::load_shared_class_for_builtin_loader(
 101                  Symbol* class_name, Handle class_loader, TRAPS) {
 102   assert(UseSharedSpaces, "must be");
 103   InstanceKlass* ik = find_builtin_class(class_name);
 104 
 105   if (ik != nullptr && !ik->shared_loading_failed()) {
 106     if ((SystemDictionary::is_system_class_loader(class_loader()) && ik->is_shared_app_class())  ||
 107         (SystemDictionary::is_platform_class_loader(class_loader()) && ik->is_shared_platform_class())) {
 108       SharedClassLoadingMark slm(THREAD, ik);
 109       PackageEntry* pkg_entry = CDSProtectionDomain::get_package_entry_from_class(ik, class_loader);
 110       Handle protection_domain;
 111       if (!CDSConfig::is_dumping_final_static_archive() && // Why this check??
 112           !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 (!UseSharedSpaces) {
 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 bool SystemDictionaryShared::check_for_exclusion(InstanceKlass* k, DumpTimeClassInfo* info) {
 215   if (!CDSConfig::is_dumping_final_static_archive() && MetaspaceShared::is_in_shared_metaspace(k)) {
 216     // We have reached a super type that's already in the base archive. Treat it
 217     // as "not excluded".
 218     assert(CDSConfig::is_dumping_dynamic_archive(), "must be");
 219     return false;
 220   }
 221 
 222   if (info == nullptr) {
 223     info = _dumptime_table->get(k);
 224     assert(info != nullptr, "supertypes of any classes in _dumptime_table must either be shared, or must also be in _dumptime_table");
 225   }
 226 
 227   if (!info->has_checked_exclusion()) {
 228     if (check_for_exclusion_impl(k)) {
 229       info->set_excluded();
 230     }
 231     info->set_has_checked_exclusion();
 232   }
 233 
 234   return info->is_excluded();
 235 }

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

 515     // The VM is not trying to resolve a super type of parser->current_class_name().
 516     // Instead, it's resolving an error class (because parser->current_class_name() has
 517     // failed parsing or verification). Don't do anything here.
 518     return nullptr;
 519   }
 520 }
 521 
 522 void SystemDictionaryShared::set_shared_class_misc_info(InstanceKlass* k, ClassFileStream* cfs) {
 523   assert(CDSConfig::is_dumping_archive(), "sanity");
 524   assert(!is_builtin(k), "must be unregistered class");
 525   DumpTimeClassInfo* info = get_info(k);
 526   info->_clsfile_size  = cfs->length();
 527   info->_clsfile_crc32 = ClassLoader::crc32(0, (const char*)cfs->buffer(), cfs->length());
 528 }
 529 
 530 void SystemDictionaryShared::initialize() {
 531   if (CDSConfig::is_dumping_archive()) {
 532     _dumptime_table = new (mtClass) DumpTimeSharedClassTable;
 533     _dumptime_lambda_proxy_class_dictionary =
 534                       new (mtClass) DumpTimeLambdaProxyClassDictionary;
 535     _dumptime_method_info_dictionary = new (mtClass) DumpTimeMethodInfoDictionary;
 536   }
 537 }
 538 
 539 void SystemDictionaryShared::init_dumptime_info(InstanceKlass* k) {
 540   MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
 541   assert(SystemDictionaryShared::class_loading_may_happen(), "sanity");
 542   _dumptime_table->allocate_info(k);
 543 }
 544 
 545 void SystemDictionaryShared::remove_dumptime_info(InstanceKlass* k) {
 546   MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
 547   _dumptime_table->remove(k);
 548 }
 549 
 550 void SystemDictionaryShared::handle_class_unloading(InstanceKlass* klass) {
 551   if (CDSConfig::is_dumping_archive()) {
 552     remove_dumptime_info(klass);
 553   }
 554 
 555   if (CDSConfig::is_dumping_archive() || ClassListWriter::is_enabled()) {

 584   Array<InstanceKlass*>* interfaces = k->local_interfaces();
 585   int len = interfaces->length();
 586   for (int i = 0; i < len; i++) {
 587     if (has_been_redefined(interfaces->at(i))) {
 588       return true;
 589     }
 590   }
 591   return false;
 592 }
 593 
 594 // k is a class before relocating by ArchiveBuilder
 595 void SystemDictionaryShared::validate_before_archiving(InstanceKlass* k) {
 596   ResourceMark rm;
 597   const char* name = k->name()->as_C_string();
 598   DumpTimeClassInfo* info = _dumptime_table->get(k);
 599   assert(!class_loading_may_happen(), "class loading must be disabled");
 600   guarantee(info != nullptr, "Class %s must be entered into _dumptime_table", name);
 601   guarantee(!info->is_excluded(), "Should not attempt to archive excluded class %s", name);
 602   if (is_builtin(k)) {
 603     if (k->is_hidden()) {
 604       if (ArchiveInvokeDynamic) { // FIXME -- clean up
 605         return;
 606       }
 607       assert(is_registered_lambda_proxy_class(k), "unexpected hidden class %s", name);
 608     }
 609     guarantee(!k->is_shared_unregistered_class(),
 610               "Class loader type must be set for BUILTIN class %s", name);
 611 
 612   } else {
 613     guarantee(k->is_shared_unregistered_class(),
 614               "Class loader type must not be set for UNREGISTERED class %s", name);
 615   }
 616 }
 617 
 618 class UnregisteredClassesDuplicationChecker : StackObj {
 619   GrowableArray<InstanceKlass*> _list;
 620   Thread* _thread;
 621 public:
 622   UnregisteredClassesDuplicationChecker() : _thread(Thread::current()) {}
 623 
 624   void do_entry(InstanceKlass* k, DumpTimeClassInfo& info) {
 625     if (!SystemDictionaryShared::is_builtin(k)) {
 626       _list.append(k);

 659 void SystemDictionaryShared::check_excluded_classes() {
 660   assert(!class_loading_may_happen(), "class loading must be disabled");
 661   assert_lock_strong(DumpTimeTable_lock);
 662 
 663   if (CDSConfig::is_dumping_dynamic_archive()) {
 664     // Do this first -- if a base class is excluded due to duplication,
 665     // all of its subclasses will also be excluded.
 666     ResourceMark rm;
 667     UnregisteredClassesDuplicationChecker dup_checker;
 668     _dumptime_table->iterate_all_live_classes(&dup_checker);
 669     dup_checker.mark_duplicated_classes();
 670   }
 671 
 672   auto check_for_exclusion = [&] (InstanceKlass* k, DumpTimeClassInfo& info) {
 673     SystemDictionaryShared::check_for_exclusion(k, &info);
 674   };
 675   _dumptime_table->iterate_all_live_classes(check_for_exclusion);
 676   _dumptime_table->update_counts();
 677 
 678   cleanup_lambda_proxy_class_dictionary();
 679 
 680   cleanup_method_info_dictionary();
 681 
 682   TrainingData::cleanup_training_data();
 683 }
 684 
 685 bool SystemDictionaryShared::is_excluded_class(InstanceKlass* k) {
 686   assert(!class_loading_may_happen(), "class loading must be disabled");
 687   assert_lock_strong(DumpTimeTable_lock);
 688   assert(CDSConfig::is_dumping_archive(), "sanity");
 689   DumpTimeClassInfo* p = get_info_locked(k);
 690   return p->is_excluded();
 691 }
 692 
 693 void SystemDictionaryShared::set_excluded_locked(InstanceKlass* k) {
 694   assert_lock_strong(DumpTimeTable_lock);
 695   assert(CDSConfig::is_dumping_archive(), "sanity");
 696   DumpTimeClassInfo* info = get_info_locked(k);
 697   info->set_excluded();
 698 }
 699 
 700 void SystemDictionaryShared::set_excluded(InstanceKlass* k) {
 701   assert(CDSConfig::is_dumping_archive(), "sanity");
 702   DumpTimeClassInfo* info = get_info(k);
 703   info->set_excluded();
 704 }
 705 
 706 void SystemDictionaryShared::set_class_has_failed_verification(InstanceKlass* ik) {
 707   assert(CDSConfig::is_dumping_archive(), "sanity");
 708   DumpTimeClassInfo* p = get_info(ik);
 709   p->set_failed_verification();
 710 }
 711 
 712 bool SystemDictionaryShared::has_class_failed_verification(InstanceKlass* ik) {
 713   assert(CDSConfig::is_dumping_archive(), "sanity");
 714   DumpTimeClassInfo* p = _dumptime_table->get(ik);
 715   return (p == nullptr) ? false : p->failed_verification();
 716 }
 717 
 718 void SystemDictionaryShared::dumptime_classes_do(class MetaspaceClosure* it) {
 719   assert_lock_strong(DumpTimeTable_lock);
 720 
 721   auto do_klass = [&] (InstanceKlass* k, DumpTimeClassInfo& info) {
 722     if (CDSConfig::is_dumping_final_static_archive() && !k->is_loaded()) {
 723       assert(k->is_shared_unregistered_class(), "must be");
 724       info.metaspace_pointers_do(it);
 725     } else if (k->is_loader_alive() && !info.is_excluded()) {
 726       info.metaspace_pointers_do(it);
 727     }
 728   };
 729   _dumptime_table->iterate_all_live_classes(do_klass);
 730 
 731   auto do_lambda = [&] (LambdaProxyClassKey& key, DumpTimeLambdaProxyClassInfo& info) {
 732     if (key.caller_ik()->is_loader_alive()) {
 733       info.metaspace_pointers_do(it);
 734       key.metaspace_pointers_do(it);
 735     }
 736   };
 737   _dumptime_lambda_proxy_class_dictionary->iterate_all(do_lambda);
 738 
 739   auto do_method_info = [&] (MethodDataKey& key, DumpTimeMethodDataInfo& info) {
 740     info.metaspace_pointers_do(it);
 741     key.metaspace_pointers_do(it);
 742   };
 743   _dumptime_method_info_dictionary->iterate_all(do_method_info);
 744 }
 745 
 746 bool SystemDictionaryShared::add_verification_constraint(InstanceKlass* k, Symbol* name,
 747          Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object) {
 748   assert(CDSConfig::is_dumping_archive(), "sanity");
 749   if (CDSConfig::is_dumping_dynamic_archive() && k->is_shared()) {
 750     // k is a new class in the static archive, but one of its supertypes is an old class, so k wasn't
 751     // verified during dump time. No need to record constraints as k won't be included in the dynamic archive.
 752     return false;
 753   }
 754   if (PreloadSharedClasses && is_builtin(k)) {
 755     // There's no need to save verification constraints
 756     return false;
 757   }
 758 
 759   DumpTimeClassInfo* info = get_info(k);
 760   info->add_verification_constraint(k, name, from_name, from_field_is_protected,
 761                                     from_is_array, from_is_object);
 762 
 763   if (CDSConfig::is_dumping_dynamic_archive()) {
 764     // For dynamic dumping, we can resolve all the constraint classes for all class loaders during
 765     // the initial run prior to creating the archive before vm exit. We will also perform verification
 766     // check when running with the archive.
 767     return false;
 768   } else {
 769     if (is_builtin(k)) {
 770       // For builtin class loaders, we can try to complete the verification check at dump time,
 771       // because we can resolve all the constraint classes. We will also perform verification check
 772       // when running with the archive.
 773       return false;
 774     } else {
 775       // For non-builtin class loaders, we cannot complete the verification check at dump time,
 776       // because at dump time we don't know how to resolve classes for such loaders.
 777       return true;
 778     }

 788 void SystemDictionaryShared::add_to_dump_time_lambda_proxy_class_dictionary(LambdaProxyClassKey& key,
 789                                                            InstanceKlass* proxy_klass) {
 790   assert_lock_strong(DumpTimeTable_lock);
 791 
 792   bool created;
 793   DumpTimeLambdaProxyClassInfo* info = _dumptime_lambda_proxy_class_dictionary->put_if_absent(key, &created);
 794   info->add_proxy_klass(proxy_klass);
 795   if (created) {
 796     ++_dumptime_lambda_proxy_class_dictionary->_count;
 797   }
 798 }
 799 
 800 void SystemDictionaryShared::add_lambda_proxy_class(InstanceKlass* caller_ik,
 801                                                     InstanceKlass* lambda_ik,
 802                                                     Symbol* invoked_name,
 803                                                     Symbol* invoked_type,
 804                                                     Symbol* method_type,
 805                                                     Method* member_method,
 806                                                     Symbol* instantiated_method_type,
 807                                                     TRAPS) {
 808   if (CDSConfig::is_dumping_static_archive() && ArchiveInvokeDynamic) {
 809     // The proxy classes will be accessible through the archived CP entries.
 810     return;
 811   }
 812   if (CDSConfig::is_dumping_preimage_static_archive() || CDSConfig::is_dumping_final_static_archive()) {
 813     // TODO: not supported in new workflow
 814     return;
 815   }
 816 
 817   assert(caller_ik->class_loader() == lambda_ik->class_loader(), "mismatched class loader");
 818   assert(caller_ik->class_loader_data() == lambda_ik->class_loader_data(), "mismatched class loader data");
 819   assert(java_lang_Class::class_data(lambda_ik->java_mirror()) == nullptr, "must not have class data");
 820 
 821   MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
 822 
 823   lambda_ik->assign_class_loader_type();
 824   lambda_ik->set_shared_classpath_index(caller_ik->shared_classpath_index());
 825   InstanceKlass* nest_host = caller_ik->nest_host(CHECK);
 826   assert(nest_host != nullptr, "unexpected nullptr nest_host");
 827 
 828   DumpTimeClassInfo* info = _dumptime_table->get(lambda_ik);
 829   if (info != nullptr && !lambda_ik->is_non_strong_hidden() && is_builtin(lambda_ik) && is_builtin(caller_ik)
 830       // Don't include the lambda proxy if its nest host is not in the "linked" state.
 831       && nest_host->is_linked()) {
 832     // Set _is_archived_lambda_proxy in DumpTimeClassInfo so that the lambda_ik
 833     // won't be excluded during dumping of shared archive. See ExcludeDumpTimeSharedClasses.
 834     info->_is_archived_lambda_proxy = true;
 835     info->set_nest_host(nest_host);
 836 
 837     LambdaProxyClassKey key(caller_ik,
 838                             invoked_name,
 839                             invoked_type,
 840                             method_type,
 841                             member_method,
 842                             instantiated_method_type);
 843     add_to_dump_time_lambda_proxy_class_dictionary(key, lambda_ik);
 844   }
 845 }
 846 
 847 InstanceKlass* SystemDictionaryShared::get_shared_lambda_proxy_class(InstanceKlass* caller_ik,
 848                                                                      Symbol* invoked_name,
 849                                                                      Symbol* invoked_type,
 850                                                                      Symbol* method_type,
 851                                                                      Method* member_method,
 852                                                                      Symbol* instantiated_method_type) {
 853   if (CDSConfig::is_dumping_final_static_archive()) {
 854     return nullptr;
 855   }
 856   MutexLocker ml(CDSLambda_lock, Mutex::_no_safepoint_check_flag);
 857   LambdaProxyClassKey key(caller_ik, invoked_name, invoked_type,
 858                           method_type, member_method, instantiated_method_type);
 859 
 860   // Try to retrieve the lambda proxy class from static archive.
 861   const RunTimeLambdaProxyClassInfo* info = _static_archive.lookup_lambda_proxy_class(&key);
 862   InstanceKlass* proxy_klass = retrieve_lambda_proxy_class(info);
 863   if (proxy_klass == nullptr) {
 864     if (info != nullptr && log_is_enabled(Debug, cds)) {
 865       ResourceMark rm;
 866       log_debug(cds)("Used all static archived lambda proxy classes for: %s %s%s",
 867                      caller_ik->external_name(), invoked_name->as_C_string(), invoked_type->as_C_string());
 868     }
 869   } else {
 870     return proxy_klass;
 871   }
 872 
 873   // Retrieving from static archive is unsuccessful, try dynamic archive.
 874   info = _dynamic_archive.lookup_lambda_proxy_class(&key);
 875   proxy_klass = retrieve_lambda_proxy_class(info);

 884 }
 885 
 886 InstanceKlass* SystemDictionaryShared::retrieve_lambda_proxy_class(const RunTimeLambdaProxyClassInfo* info) {
 887   InstanceKlass* proxy_klass = nullptr;
 888   if (info != nullptr) {
 889     InstanceKlass* curr_klass = info->proxy_klass_head();
 890     InstanceKlass* prev_klass = curr_klass;
 891     if (curr_klass->lambda_proxy_is_available()) {
 892       while (curr_klass->next_link() != nullptr) {
 893         prev_klass = curr_klass;
 894         curr_klass = InstanceKlass::cast(curr_klass->next_link());
 895       }
 896       assert(curr_klass->is_hidden(), "must be");
 897       assert(curr_klass->lambda_proxy_is_available(), "must be");
 898 
 899       prev_klass->set_next_link(nullptr);
 900       proxy_klass = curr_klass;
 901       proxy_klass->clear_lambda_proxy_is_available();
 902       if (log_is_enabled(Debug, cds)) {
 903         ResourceMark rm;
 904         log_debug(cds)("Loaded lambda proxy: " PTR_FORMAT " %s ", p2i(proxy_klass), proxy_klass->external_name());
 905       }
 906     }
 907   }
 908   return proxy_klass;
 909 }
 910 
 911 InstanceKlass* SystemDictionaryShared::get_shared_nest_host(InstanceKlass* lambda_ik) {
 912   assert(!CDSConfig::is_dumping_static_archive() && UseSharedSpaces, "called at run time with CDS enabled only");
 913   RunTimeClassInfo* record = RunTimeClassInfo::get_for(lambda_ik);
 914   return record->nest_host();
 915 }
 916 
 917 InstanceKlass* SystemDictionaryShared::prepare_shared_lambda_proxy_class(InstanceKlass* lambda_ik,
 918                                                                          InstanceKlass* caller_ik, TRAPS) {
 919   Handle class_loader(THREAD, caller_ik->class_loader());
 920   Handle protection_domain;
 921   PackageEntry* pkg_entry = caller_ik->package();
 922   if (caller_ik->class_loader() != nullptr) {
 923     protection_domain = CDSProtectionDomain::init_security_info(class_loader, caller_ik, pkg_entry, CHECK_NULL);
 924   }

 943   // Add to class hierarchy, and do possible deoptimizations.
 944   loaded_lambda->add_to_hierarchy(THREAD);
 945   // But, do not add to dictionary.
 946 
 947   loaded_lambda->link_class(CHECK_NULL);
 948   // notify jvmti
 949   if (JvmtiExport::should_post_class_load()) {
 950     JvmtiExport::post_class_load(THREAD, loaded_lambda);
 951   }
 952   if (class_load_start_event.should_commit()) {
 953     SystemDictionary::post_class_load_event(&class_load_start_event, loaded_lambda, ClassLoaderData::class_loader_data(class_loader()));
 954   }
 955 
 956   loaded_lambda->initialize(CHECK_NULL);
 957 
 958   return loaded_lambda;
 959 }
 960 
 961 void SystemDictionaryShared::check_verification_constraints(InstanceKlass* klass,
 962                                                             TRAPS) {
 963   //assert(!CDSConfig::is_dumping_static_archive() && UseSharedSpaces, "called at run time with CDS enabled only");
 964   RunTimeClassInfo* record = RunTimeClassInfo::get_for(klass);
 965 
 966   int length = record->_num_verifier_constraints;
 967   if (length > 0) {
 968     for (int i = 0; i < length; i++) {
 969       RunTimeClassInfo::RTVerifierConstraint* vc = record->verifier_constraint_at(i);
 970       Symbol* name      = vc->name();
 971       Symbol* from_name = vc->from_name();
 972       char c            = record->verifier_constraint_flag(i);
 973 
 974       if (log_is_enabled(Trace, cds, verification)) {
 975         ResourceMark rm(THREAD);
 976         log_trace(cds, verification)("check_verification_constraint: %s: %s must be subclass of %s [0x%x]",
 977                                      klass->external_name(), from_name->as_klass_external_name(),
 978                                      name->as_klass_external_name(), c);
 979       }
 980 
 981       bool from_field_is_protected = (c & SystemDictionaryShared::FROM_FIELD_IS_PROTECTED) ? true : false;
 982       bool from_is_array           = (c & SystemDictionaryShared::FROM_IS_ARRAY)           ? true : false;
 983       bool from_is_object          = (c & SystemDictionaryShared::FROM_IS_OBJECT)          ? true : false;

1053   assert(is_builtin(klass), "must be");
1054   assert(klass_loader != nullptr, "should not be called for boot loader");
1055   assert(loader1 != loader2, "must be");
1056 
1057   if (CDSConfig::is_dumping_dynamic_archive() && Thread::current()->is_VM_thread()) {
1058     // We are re-laying out the vtable/itables of the *copy* of
1059     // a class during the final stage of dynamic dumping. The
1060     // linking constraints for this class has already been recorded.
1061     return;
1062   }
1063   assert(!Thread::current()->is_VM_thread(), "must be");
1064 
1065   assert(CDSConfig::is_dumping_archive(), "sanity");
1066   DumpTimeClassInfo* info = get_info(klass);
1067   info->record_linking_constraint(name, loader1, loader2);
1068 }
1069 
1070 // returns true IFF there's no need to re-initialize the i/v-tables for klass for
1071 // the purpose of checking class loader constraints.
1072 bool SystemDictionaryShared::check_linking_constraints(Thread* current, InstanceKlass* klass) {
1073   //assert(!CDSConfig::is_dumping_static_archive() && UseSharedSpaces, "called at run time with CDS enabled only");
1074   LogTarget(Info, class, loader, constraints) log;
1075   if (klass->is_shared_boot_class()) {
1076     // No class loader constraint check performed for boot classes.
1077     return true;
1078   }
1079   if (klass->is_shared_platform_class() || klass->is_shared_app_class()) {
1080     RunTimeClassInfo* info = RunTimeClassInfo::get_for(klass);
1081     assert(info != nullptr, "Sanity");
1082     if (info->_num_loader_constraints > 0) {
1083       HandleMark hm(current);
1084       for (int i = 0; i < info->_num_loader_constraints; i++) {
1085         RunTimeClassInfo::RTLoaderConstraint* lc = info->loader_constraint_at(i);
1086         Symbol* name = lc->constraint_name();
1087         Handle loader1(current, get_class_loader_by(lc->_loader_type1));
1088         Handle loader2(current, get_class_loader_by(lc->_loader_type2));
1089         if (log.is_enabled()) {
1090           ResourceMark rm(current);
1091           log.print("[CDS add loader constraint for class %s symbol %s loader[0] %s loader[1] %s",
1092                     klass->external_name(), name->as_C_string(),
1093                     ClassLoaderData::class_loader_data(loader1())->loader_name_and_id(),

1182     }
1183   }
1184 
1185   size_t total() {
1186     return _shared_class_info_size;
1187   }
1188 };
1189 
1190 size_t SystemDictionaryShared::estimate_size_for_archive() {
1191   EstimateSizeForArchive est;
1192   _dumptime_table->iterate_all_live_classes(&est);
1193   size_t total_size = est.total() +
1194     CompactHashtableWriter::estimate_size(_dumptime_table->count_of(true)) +
1195     CompactHashtableWriter::estimate_size(_dumptime_table->count_of(false));
1196 
1197   size_t bytesize = align_up(sizeof(RunTimeLambdaProxyClassInfo), SharedSpaceObjectAlignment);
1198   total_size +=
1199       (bytesize * _dumptime_lambda_proxy_class_dictionary->_count) +
1200       CompactHashtableWriter::estimate_size(_dumptime_lambda_proxy_class_dictionary->_count);
1201 
1202   size_t method_info_byte_size = align_up(sizeof(RunTimeMethodDataInfo), SharedSpaceObjectAlignment);
1203   total_size +=
1204       (method_info_byte_size * _dumptime_method_info_dictionary->_count) +
1205       CompactHashtableWriter::estimate_size(_dumptime_method_info_dictionary->_count);
1206 
1207   return total_size;
1208 }
1209 
1210 unsigned int SystemDictionaryShared::hash_for_shared_dictionary(address ptr) {
1211   if (ArchiveBuilder::is_active() && ArchiveBuilder::current()->is_in_buffer_space(ptr)) {
1212     uintx offset = ArchiveBuilder::current()->any_to_offset(ptr);
1213     unsigned int hash = primitive_hash<uintx>(offset);
1214     DEBUG_ONLY({
1215         if (MetaspaceObj::is_shared((const MetaspaceObj*)ptr)) {
1216           assert(hash == SystemDictionaryShared::hash_for_shared_dictionary_quick(ptr), "must be");
1217         }
1218       });
1219     return hash;
1220   } else {
1221     return SystemDictionaryShared::hash_for_shared_dictionary_quick(ptr);
1222   }
1223 }
1224 
1225 class CopyLambdaProxyClassInfoToArchive : StackObj {
1226   CompactHashtableWriter* _writer;
1227   ArchiveBuilder* _builder;
1228 public:
1229   CopyLambdaProxyClassInfoToArchive(CompactHashtableWriter* writer)
1230   : _writer(writer), _builder(ArchiveBuilder::current()) {}
1231   bool do_entry(LambdaProxyClassKey& key, DumpTimeLambdaProxyClassInfo& info) {

1304         ResourceMark rm;
1305         log_trace(cds,hashtables)("%s dictionary: %s", (_is_builtin ? "builtin" : "unregistered"), info._klass->external_name());
1306       }
1307 
1308       // Save this for quick runtime lookup of InstanceKlass* -> RunTimeClassInfo*
1309       InstanceKlass* buffered_klass = ArchiveBuilder::current()->get_buffered_addr(info._klass);
1310       RunTimeClassInfo::set_for(buffered_klass, record);
1311     }
1312   }
1313 };
1314 
1315 void SystemDictionaryShared::write_lambda_proxy_class_dictionary(LambdaProxyClassDictionary *dictionary) {
1316   CompactHashtableStats stats;
1317   dictionary->reset();
1318   CompactHashtableWriter writer(_dumptime_lambda_proxy_class_dictionary->_count, &stats);
1319   CopyLambdaProxyClassInfoToArchive copy(&writer);
1320   _dumptime_lambda_proxy_class_dictionary->iterate(&copy);
1321   writer.dump(dictionary, "lambda proxy class dictionary");
1322 }
1323 
1324 class CopyMethodDataInfoToArchive : StackObj {
1325   CompactHashtableWriter* _writer;
1326   ArchiveBuilder* _builder;
1327 public:
1328   CopyMethodDataInfoToArchive(CompactHashtableWriter* writer)
1329       : _writer(writer), _builder(ArchiveBuilder::current()) {}
1330 
1331   bool do_entry(MethodDataKey& key, DumpTimeMethodDataInfo& info) {
1332     Method* holder = key.method();
1333     log_info(cds,dynamic)("Archiving method info for %s", holder->external_name());
1334 
1335     size_t byte_size = sizeof(RunTimeMethodDataInfo);
1336     RunTimeMethodDataInfo* record = (RunTimeMethodDataInfo*)ArchiveBuilder::ro_region_alloc(byte_size);
1337 
1338     DumpTimeMethodDataInfo data(info.method_data(), info.method_counters());
1339     record->init(key, data);
1340 
1341     uint hash = SystemDictionaryShared::hash_for_shared_dictionary((address)holder);
1342     u4 delta = _builder->buffer_to_offset_u4((address)record);
1343     _writer->add(hash, delta);
1344 
1345     return true;
1346   }
1347 };
1348 
1349 void SystemDictionaryShared::write_method_info_dictionary(MethodDataInfoDictionary* dictionary) {
1350   CompactHashtableStats stats;
1351   dictionary->reset();
1352   CompactHashtableWriter writer(_dumptime_method_info_dictionary->_count, &stats);
1353   CopyMethodDataInfoToArchive copy(&writer);
1354   _dumptime_method_info_dictionary->iterate(&copy);
1355   writer.dump(dictionary, "method info dictionary");
1356 }
1357 
1358 void SystemDictionaryShared::write_dictionary(RunTimeSharedDictionary* dictionary,
1359                                               bool is_builtin) {
1360   CompactHashtableStats stats;
1361   dictionary->reset();
1362   CompactHashtableWriter writer(_dumptime_table->count_of(is_builtin), &stats);
1363   CopySharedClassInfoToArchive copy(&writer, is_builtin);
1364   assert_lock_strong(DumpTimeTable_lock);
1365   _dumptime_table->iterate_all_live_classes(&copy);
1366   writer.dump(dictionary, is_builtin ? "builtin dictionary" : "unregistered dictionary");
1367 }
1368 
1369 void SystemDictionaryShared::write_to_archive(bool is_static_archive) {
1370   ArchiveInfo* archive = get_archive(is_static_archive);
1371 
1372   write_dictionary(&archive->_builtin_dictionary, true);
1373   write_dictionary(&archive->_unregistered_dictionary, false);
1374 
1375   write_lambda_proxy_class_dictionary(&archive->_lambda_proxy_class_dictionary);
1376 
1377   write_method_info_dictionary(&archive->_method_info_dictionary);
1378 }
1379 
1380 void SystemDictionaryShared::adjust_lambda_proxy_class_dictionary() {
1381   AdjustLambdaProxyClassInfo adjuster;
1382   _dumptime_lambda_proxy_class_dictionary->iterate(&adjuster);
1383 }
1384 
1385 class AdjustMethodInfo : StackObj {
1386 public:
1387   AdjustMethodInfo() {}
1388   bool do_entry(MethodDataKey& key, DumpTimeMethodDataInfo& info) {
1389     // TODO: is it possible for the data to become stale/invalid?
1390     MethodData*     md = info.method_data();
1391     MethodCounters* mc = info.method_counters();
1392     if (md != nullptr) {
1393       md = ArchiveBuilder::current()->get_buffered_addr(md);
1394     }
1395     if (mc != nullptr) {
1396       mc = ArchiveBuilder::current()->get_buffered_addr(mc);
1397     }
1398     assert(ArchiveBuilder::current()->is_in_buffer_space(md) || md == nullptr, "must be");
1399     assert(ArchiveBuilder::current()->is_in_buffer_space(mc) || mc == nullptr, "must be");
1400     if (md != nullptr) {
1401       md->remove_unshareable_info();
1402     }
1403     if (mc != nullptr) {
1404       mc->remove_unshareable_info();
1405     }
1406     return true;
1407   }
1408 };
1409 
1410 void SystemDictionaryShared::adjust_method_info_dictionary() {
1411   AdjustMethodInfo adjuster;
1412   _dumptime_method_info_dictionary->iterate(&adjuster);
1413 }
1414 
1415 void SystemDictionaryShared::serialize_dictionary_headers(SerializeClosure* soc,
1416                                                           bool is_static_archive) {
1417   ArchiveInfo* archive = get_archive(is_static_archive);
1418 
1419   archive->_builtin_dictionary.serialize_header(soc);
1420   archive->_unregistered_dictionary.serialize_header(soc);
1421   archive->_lambda_proxy_class_dictionary.serialize_header(soc);
1422   archive->_method_info_dictionary.serialize_header(soc);
1423 }
1424 
1425 void SystemDictionaryShared::serialize_vm_classes(SerializeClosure* soc) {
1426   for (auto id : EnumRange<vmClassID>{}) {
1427     soc->do_ptr(vmClasses::klass_addr_at(id));
1428   }
1429   soc->do_ptr((void**)&_archived_lambda_form_classes);
1430   soc->do_ptr((void**)&_archived_lambda_proxy_classes_boot);
1431   soc->do_ptr((void**)&_archived_lambda_proxy_classes_boot2);
1432   soc->do_ptr((void**)&_archived_lambda_proxy_classes_platform);
1433   soc->do_ptr((void**)&_archived_lambda_proxy_classes_app);
1434 }
1435 
1436 const RunTimeClassInfo*
1437 SystemDictionaryShared::find_record(RunTimeSharedDictionary* static_dict, RunTimeSharedDictionary* dynamic_dict, Symbol* name) {
1438   if (!UseSharedSpaces || !name->is_shared()) {
1439     // The names of all shared classes must also be a shared Symbol.
1440     return nullptr;
1441   }
1442 
1443   unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary_quick(name);
1444   const RunTimeClassInfo* record = nullptr;
1445   if (DynamicArchive::is_mapped()) {
1446     // Use the regenerated holder classes in the dynamic archive as they
1447     // have more methods than those in the base archive.
1448     if (LambdaFormInvokers::may_be_regenerated_class(name)) {



1449       record = dynamic_dict->lookup(name, hash, 0);
1450       if (record != nullptr) {
1451         return record;
1452       }
1453     }
1454   }
1455 
1456   if (!MetaspaceShared::is_shared_dynamic(name)) {
1457     // The names of all shared classes in the static dict must also be in the
1458     // static archive
1459     record = static_dict->lookup(name, hash, 0);
1460   }
1461 
1462   if (record == nullptr && DynamicArchive::is_mapped()) {
1463     record = dynamic_dict->lookup(name, hash, 0);
1464   }
1465 
1466   return record;
1467 }
1468 

1473   if (record != nullptr) {
1474     assert(!record->_klass->is_hidden(), "hidden class cannot be looked up by name");
1475     assert(check_alignment(record->_klass), "Address not aligned");
1476     // We did not save the classfile data of the generated LambdaForm invoker classes,
1477     // so we cannot support CLFH for such classes.
1478     if (record->_klass->is_generated_shared_class() && JvmtiExport::should_post_class_file_load_hook()) {
1479        return nullptr;
1480     }
1481     return record->_klass;
1482   } else {
1483     return nullptr;
1484   }
1485 }
1486 
1487 void SystemDictionaryShared::update_shared_entry(InstanceKlass* k, int id) {
1488   assert(CDSConfig::is_dumping_static_archive(), "class ID is used only for static dump (from classlist)");
1489   DumpTimeClassInfo* info = get_info(k);
1490   info->_id = id;
1491 }
1492 
1493 const char* SystemDictionaryShared::class_loader_name_for_shared(Klass* k) {
1494   assert(k != nullptr, "Sanity");
1495   assert(k->is_shared(), "Must be");
1496   assert(k->is_instance_klass(), "Must be");
1497   InstanceKlass* ik = InstanceKlass::cast(k);
1498   if (ik->is_shared_boot_class()) {
1499     return "boot_loader";
1500   } else if (ik->is_shared_platform_class()) {
1501     return "platform_loader";
1502   } else if (ik->is_shared_app_class()) {
1503     return "app_loader";
1504   } else if (ik->is_shared_unregistered_class()) {
1505     return "unregistered_loader";
1506   } else {
1507     return "unknown loader";
1508   }
1509 }
1510 
1511 class SharedDictionaryPrinter : StackObj {
1512   outputStream* _st;
1513   int _index;
1514 public:
1515   SharedDictionaryPrinter(outputStream* st) : _st(st), _index(0) {}
1516 
1517   void do_value(const RunTimeClassInfo* record) {
1518     ResourceMark rm;
1519     _st->print_cr("%4d: %s %s", _index++, record->_klass->external_name(),
1520         SystemDictionaryShared::class_loader_name_for_shared(record->_klass));
1521     if (record->_klass->array_klasses() != nullptr) {
1522       record->_klass->array_klasses()->cds_print_value_on(_st);
1523       _st->cr();
1524     }
1525   }
1526   int index() const { return _index; }
1527 };
1528 
1529 class SharedLambdaDictionaryPrinter : StackObj {
1530   outputStream* _st;
1531   int _index;
1532 public:
1533   SharedLambdaDictionaryPrinter(outputStream* st, int idx) : _st(st), _index(idx) {}
1534 
1535   void do_value(const RunTimeLambdaProxyClassInfo* record) {
1536     if (record->proxy_klass_head()->lambda_proxy_is_available()) {
1537       ResourceMark rm;
1538       Klass* k = record->proxy_klass_head();
1539       while (k != nullptr) {
1540         _st->print_cr("%4d: %s %s", _index++, k->external_name(),
1541                       SystemDictionaryShared::class_loader_name_for_shared(k));
1542         k = k->next_link();
1543       }
1544     }
1545   }
1546 };
1547 
1548 class SharedMethodInfoDictionaryPrinter : StackObj {
1549   outputStream* _st;
1550   int _index;
1551 
1552 private:
1553   static const char* tag(void* p) {
1554     if (p == nullptr) {
1555       return "   ";
1556     } else if (MetaspaceShared::is_shared_dynamic(p)) {
1557       return "<D>";
1558     } else if (MetaspaceShared::is_in_shared_metaspace(p)) {
1559       return "<S>";
1560     } else {
1561       return "???";
1562     }
1563   }
1564 public:
1565   SharedMethodInfoDictionaryPrinter(outputStream* st) : _st(st), _index(0) {}
1566 
1567   void do_value(const RunTimeMethodDataInfo* record) {
1568     ResourceMark rm;
1569     Method*         m  = record->method();
1570     MethodCounters* mc = record->method_counters();
1571     MethodData*     md = record->method_data();
1572 
1573     _st->print_cr("%4d: %s" PTR_FORMAT " %s" PTR_FORMAT " %s" PTR_FORMAT " %s", _index++,
1574                   tag(m), p2i(m),
1575                   tag(mc), p2i(mc),
1576                   tag(md), p2i(md),
1577                   m->external_name());
1578     if (Verbose) {
1579       if (mc != nullptr) {
1580         mc->print_on(_st);
1581       }
1582       if (md != nullptr) {
1583         md->print_on(_st);
1584       }
1585       _st->cr();
1586     }
1587   }
1588 };
1589 
1590 void SystemDictionaryShared::ArchiveInfo::print_on(const char* prefix,
1591                                                    outputStream* st) {
1592   st->print_cr("%sShared Dictionary", prefix);
1593   SharedDictionaryPrinter p(st);
1594   st->print_cr("%sShared Builtin Dictionary", prefix);
1595   _builtin_dictionary.iterate(&p);
1596   st->print_cr("%sShared Unregistered Dictionary", prefix);
1597   _unregistered_dictionary.iterate(&p);
1598   if (!_lambda_proxy_class_dictionary.empty()) {
1599     st->print_cr("%sShared Lambda Dictionary", prefix);
1600     SharedLambdaDictionaryPrinter ldp(st, p.index());
1601     _lambda_proxy_class_dictionary.iterate(&ldp);
1602   }
1603   if (!_method_info_dictionary.empty()) {
1604     st->print_cr("%sShared MethodData Dictionary", prefix);
1605     SharedMethodInfoDictionaryPrinter mdp(st);
1606     _method_info_dictionary.iterate(&mdp);
1607   }
1608   st->print_cr("%sTraining Data", prefix);
1609   TrainingDataPrinter tdp(st);
1610   _builtin_dictionary.iterate(&tdp);
1611   _method_info_dictionary.iterate(&tdp);
1612 }
1613 
1614 void SystemDictionaryShared::ArchiveInfo::print_table_statistics(const char* prefix,
1615                                                                  outputStream* st) {
1616   st->print_cr("%sArchve Statistics", prefix);
1617   _builtin_dictionary.print_table_statistics(st, "Builtin Shared Dictionary");
1618   _unregistered_dictionary.print_table_statistics(st, "Unregistered Shared Dictionary");
1619   _lambda_proxy_class_dictionary.print_table_statistics(st, "Lambda Shared Dictionary");
1620   _method_info_dictionary.print_table_statistics(st, "MethodData Dictionary");
1621 }
1622 
1623 void SystemDictionaryShared::print_shared_archive(outputStream* st, bool is_static) {
1624   if (UseSharedSpaces) {
1625     if (is_static) {
1626       _static_archive.print_on("", st);
1627     } else {
1628       if (DynamicArchive::is_mapped()) {
1629         _dynamic_archive.print_on("Dynamic ", st);
1630       }
1631     }
1632   }
1633 }
1634 
1635 void SystemDictionaryShared::print_on(outputStream* st) {
1636   print_shared_archive(st, true);
1637   print_shared_archive(st, false);
1638 }
1639 
1640 void SystemDictionaryShared::print_table_statistics(outputStream* st) {

1666     // must also be excluded.
1667     bool always_exclude = SystemDictionaryShared::check_for_exclusion(caller_ik, nullptr) ||
1668                           SystemDictionaryShared::check_for_exclusion(nest_host, nullptr);
1669 
1670     for (int i = info._proxy_klasses->length() - 1; i >= 0; i--) {
1671       InstanceKlass* ik = info._proxy_klasses->at(i);
1672       if (always_exclude || SystemDictionaryShared::check_for_exclusion(ik, nullptr)) {
1673         SystemDictionaryShared::reset_registered_lambda_proxy_class(ik);
1674         info._proxy_klasses->remove_at(i);
1675       }
1676     }
1677     return info._proxy_klasses->length() == 0 ? true /* delete the node*/ : false;
1678   }
1679 };
1680 
1681 void SystemDictionaryShared::cleanup_lambda_proxy_class_dictionary() {
1682   assert_lock_strong(DumpTimeTable_lock);
1683   CleanupDumpTimeLambdaProxyClassTable cleanup_proxy_classes;
1684   _dumptime_lambda_proxy_class_dictionary->unlink(&cleanup_proxy_classes);
1685 }
1686 
1687 class CleanupDumpTimeMethodInfoTable : StackObj {
1688 public:
1689   bool do_entry(MethodDataKey& key, DumpTimeMethodDataInfo& info) {
1690     assert_lock_strong(DumpTimeTable_lock);
1691     assert(MetaspaceShared::is_in_shared_metaspace(key.method()), "");
1692     InstanceKlass* holder = key.method()->method_holder();
1693     bool is_excluded = SystemDictionaryShared::check_for_exclusion(holder, nullptr);
1694     return is_excluded;
1695   }
1696 };
1697 
1698 void SystemDictionaryShared::cleanup_method_info_dictionary() {
1699   assert_lock_strong(DumpTimeTable_lock);
1700 
1701   CleanupDumpTimeMethodInfoTable cleanup_method_info;
1702   _dumptime_method_info_dictionary->unlink(&cleanup_method_info);
1703 }
1704 
1705 // SystemDictionaryShared::can_be_preinited() is called in two different phases
1706 //   [1] SystemDictionaryShared::try_init_class()
1707 //   [2] HeapShared::archive_java_mirrors()
1708 // Between the two phases, some Java code may have been executed to contaminate the
1709 // initialized mirror of X. So we call reset_preinit_check() at the beginning of the
1710 // [2] so that we will re-run has_non_default_static_fields() on all the classes.
1711 void SystemDictionaryShared::reset_preinit_check() {
1712   auto iterator = [&] (InstanceKlass* k, DumpTimeClassInfo& info) {
1713     if (info.can_be_preinited()) {
1714       info.reset_preinit_check();
1715     }
1716   };
1717   _dumptime_table->iterate_all_live_classes(iterator);
1718 }
1719 
1720 // Called by ClassPrelinker before we get into VM_PopulateDumpSharedSpace
1721 void SystemDictionaryShared::force_preinit(InstanceKlass* ik) {
1722   MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
1723   DumpTimeClassInfo* info = get_info_locked(ik);
1724   info->force_preinit();
1725 }
1726 
1727 bool SystemDictionaryShared::can_be_preinited(InstanceKlass* ik) {
1728   if (!CDSConfig::is_initing_classes_at_dump_time()) {
1729     return false;
1730   }
1731 
1732   assert_lock_strong(DumpTimeTable_lock);
1733   DumpTimeClassInfo* info = get_info_locked(ik);
1734   if (!info->has_done_preinit_check()) {
1735     info->set_can_be_preinited(check_can_be_preinited(ik, info));
1736   }
1737   return info->can_be_preinited();
1738 }
1739 
1740 bool SystemDictionaryShared::has_non_default_static_fields(InstanceKlass* ik) {
1741   oop mirror = ik->java_mirror();
1742 
1743   for (JavaFieldStream fs(ik); !fs.done(); fs.next()) {
1744     if (fs.access_flags().is_static()) {
1745       fieldDescriptor& fd = fs.field_descriptor();
1746       int offset = fd.offset();
1747       bool is_default = true;
1748       bool has_initval = fd.has_initial_value();
1749       switch (fd.field_type()) {
1750       case T_OBJECT:
1751       case T_ARRAY:
1752         is_default = mirror->obj_field(offset) == nullptr;
1753         break;
1754       case T_BOOLEAN:
1755         is_default = mirror->bool_field(offset) == (has_initval ? fd.int_initial_value() : 0);
1756         break;
1757       case T_BYTE:
1758         is_default = mirror->byte_field(offset) == (has_initval ? fd.int_initial_value() : 0);
1759         break;
1760       case T_SHORT:
1761         is_default = mirror->short_field(offset) == (has_initval ? fd.int_initial_value() : 0);
1762         break;
1763       case T_CHAR:
1764         is_default = mirror->char_field(offset) == (has_initval ? fd.int_initial_value() : 0);
1765         break;
1766       case T_INT:
1767         is_default = mirror->int_field(offset) == (has_initval ? fd.int_initial_value() : 0);
1768         break;
1769       case T_LONG:
1770         is_default = mirror->long_field(offset) == (has_initval ? fd.long_initial_value() : 0);
1771         break;
1772       case T_FLOAT:
1773         is_default = mirror->float_field(offset) == (has_initval ? fd.float_initial_value() : 0);
1774         break;
1775       case T_DOUBLE:
1776         is_default = mirror->double_field(offset) == (has_initval ? fd.double_initial_value() : 0);
1777         break;
1778       default:
1779         ShouldNotReachHere();
1780       }
1781 
1782       if (!is_default) {
1783         log_info(cds, init)("cannot initialize %s (static field %s has non-default value)",
1784                             ik->external_name(), fd.name()->as_C_string());
1785         return false;
1786       }
1787     }
1788   }
1789 
1790   return true;
1791 }
1792 
1793 bool SystemDictionaryShared::check_can_be_preinited(InstanceKlass* ik, DumpTimeClassInfo* info) {
1794   ResourceMark rm;
1795 
1796   if (!is_builtin(ik)) {
1797     log_info(cds, init)("cannot initialize %s (not built-in loader)", ik->external_name());
1798     return false;
1799   }
1800 
1801   InstanceKlass* super = ik->java_super();
1802   if (super != nullptr && !can_be_preinited(super)) {
1803     log_info(cds, init)("cannot initialize %s (super %s not initable)", ik->external_name(), super->external_name());
1804     return false;
1805   }
1806 
1807   Array<InstanceKlass*>* interfaces = ik->local_interfaces();
1808   for (int i = 0; i < interfaces->length(); i++) {
1809     if (!can_be_preinited(interfaces->at(i))) {
1810       log_info(cds, init)("cannot initialize %s (interface %s not initable)",
1811                           ik->external_name(), interfaces->at(i)->external_name());
1812       return false;
1813     }
1814   }
1815 
1816   if (HeapShared::is_lambda_form_klass(ik) || info->is_forced_preinit()) {
1817     // We allow only these to have <clinit> and non-default static fields
1818   } else {
1819     if (ik->class_initializer() != nullptr) {
1820       log_info(cds, init)("cannot initialize %s (has <clinit>)", ik->external_name());
1821       return false;
1822     }
1823     if (ik->is_initialized() && !has_non_default_static_fields(ik)) {
1824       return false;
1825     }
1826   }
1827 
1828   return true;
1829 }
1830 
1831 #if 0
1832 static Array<InstanceKlass*>* copy_klass_array(GrowableArray<InstanceKlass*>* src) {
1833   Array<InstanceKlass*>* dst = ArchiveBuilder::new_ro_array<InstanceKlass*>(src->length());
1834   for (int i = 0; i < src->length(); i++) {
1835     ArchiveBuilder::current()->write_pointer_in_buffer(dst->adr_at(i), src->at(i));
1836   }
1837   return dst;
1838 }
1839 #endif
1840 
1841 class GatherSharedClasses : StackObj {
1842 private:
1843   GrowableArray<InstanceKlass*> _list;
1844 
1845 public:
1846   void do_entry(InstanceKlass* k, DumpTimeClassInfo& info) {
1847     if ((k->is_shared_boot_class() || k->is_shared_platform_class() || k->is_shared_app_class())
1848         && !SystemDictionaryShared::check_for_exclusion(k, &info)) {
1849       _list.append(k);
1850     }
1851   }
1852 
1853   GrowableArray<InstanceKlass*>* list() { return &_list; }
1854   int count() { return _list.length(); }
1855 };
1856 
1857 void SystemDictionaryShared::create_loader_positive_lookup_cache(TRAPS) {
1858   GatherSharedClasses shared_classes;
1859   {
1860     MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
1861     _dumptime_table->iterate_all_live_classes(&shared_classes);
1862   }
1863 
1864   InstanceKlass* ik = vmClasses::Class_klass();
1865   objArrayOop r = oopFactory::new_objArray(ik, shared_classes.count(), CHECK);
1866   objArrayHandle array_h(THREAD, r);
1867 
1868   for (int i = 0; i < shared_classes.count(); i++) {
1869     oop mirror = shared_classes.list()->at(i)->java_mirror();
1870     Handle mirror_h(THREAD, mirror);
1871     array_h->obj_at_put(i, mirror_h());
1872   }
1873 
1874   TempNewSymbol method = SymbolTable::new_symbol("generatePositiveLookupCache");
1875   TempNewSymbol signature = SymbolTable::new_symbol("([Ljava/lang/Class;)V");
1876 
1877   JavaCallArguments args(Handle(THREAD, SystemDictionary::java_system_loader()));
1878   args.push_oop(array_h);
1879   JavaValue result(T_VOID);
1880   JavaCalls::call_virtual(&result,
1881                           vmClasses::jdk_internal_loader_ClassLoaders_AppClassLoader_klass(),
1882                           method,
1883                           signature,
1884                           &args,
1885                           CHECK);
1886 
1887   if (HAS_PENDING_EXCEPTION) {
1888     Handle exc_handle(THREAD, PENDING_EXCEPTION);
1889     CLEAR_PENDING_EXCEPTION;
1890 
1891     log_warning(cds)("Exception during AppClassLoader::generatePositiveLookupCache() call");
1892     LogStreamHandle(Debug, cds) log;
1893     if (log.is_enabled()) {
1894       java_lang_Throwable::print_stack_trace(exc_handle, &log);
1895     }
1896     return;
1897   }
1898 }
< prev index next >