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