< prev index next >

src/hotspot/share/classfile/systemDictionary.cpp

Print this page

  38 #include "classfile/packageEntry.hpp"
  39 #include "classfile/placeholders.hpp"
  40 #include "classfile/resolutionErrors.hpp"
  41 #include "classfile/stringTable.hpp"
  42 #include "classfile/symbolTable.hpp"
  43 #include "classfile/systemDictionary.hpp"
  44 #include "classfile/vmClasses.hpp"
  45 #include "classfile/vmSymbols.hpp"
  46 #include "gc/shared/gcTraceTime.inline.hpp"
  47 #include "interpreter/bootstrapInfo.hpp"
  48 #include "jfr/jfrEvents.hpp"
  49 #include "jvm.h"
  50 #include "logging/log.hpp"
  51 #include "logging/logStream.hpp"
  52 #include "memory/metaspaceClosure.hpp"
  53 #include "memory/oopFactory.hpp"
  54 #include "memory/resourceArea.hpp"
  55 #include "memory/universe.hpp"
  56 #include "oops/access.inline.hpp"
  57 #include "oops/constantPool.inline.hpp"


  58 #include "oops/instanceKlass.hpp"
  59 #include "oops/klass.inline.hpp"
  60 #include "oops/method.inline.hpp"
  61 #include "oops/objArrayKlass.hpp"
  62 #include "oops/objArrayOop.inline.hpp"
  63 #include "oops/oop.inline.hpp"
  64 #include "oops/oopHandle.inline.hpp"
  65 #include "oops/symbol.hpp"
  66 #include "oops/typeArrayKlass.hpp"
  67 #include "prims/jvmtiExport.hpp"
  68 #include "prims/methodHandles.hpp"
  69 #include "runtime/arguments.hpp"
  70 #include "runtime/atomicAccess.hpp"
  71 #include "runtime/handles.inline.hpp"
  72 #include "runtime/java.hpp"
  73 #include "runtime/javaCalls.hpp"
  74 #include "runtime/mutexLocker.hpp"

  75 #include "runtime/sharedRuntime.hpp"
  76 #include "runtime/signature.hpp"
  77 #include "runtime/synchronizer.hpp"
  78 #include "services/classLoadingService.hpp"
  79 #include "services/diagnosticCommand.hpp"
  80 #include "services/finalizerService.hpp"
  81 #include "services/threadService.hpp"
  82 #include "utilities/growableArray.hpp"
  83 #include "utilities/macros.hpp"
  84 #include "utilities/utf8.hpp"
  85 #if INCLUDE_CDS
  86 #include "classfile/systemDictionaryShared.hpp"
  87 #endif
  88 #if INCLUDE_JFR
  89 #include "jfr/jfr.hpp"
  90 #endif
  91 
  92 class InvokeMethodKey : public StackObj {
  93   private:
  94     Symbol* _symbol;

 169                          CHECK_NULL);
 170   return result.get_oop();
 171 }
 172 
 173 oop SystemDictionary::get_platform_class_loader_impl(TRAPS) {
 174   JavaValue result(T_OBJECT);
 175   InstanceKlass* class_loader_klass = vmClasses::ClassLoader_klass();
 176   JavaCalls::call_static(&result,
 177                          class_loader_klass,
 178                          vmSymbols::getPlatformClassLoader_name(),
 179                          vmSymbols::void_classloader_signature(),
 180                          CHECK_NULL);
 181   return result.get_oop();
 182 }
 183 
 184 // Helper function
 185 inline ClassLoaderData* class_loader_data(Handle class_loader) {
 186   return ClassLoaderData::class_loader_data(class_loader());
 187 }
 188 















 189 ClassLoaderData* SystemDictionary::register_loader(Handle class_loader, bool create_mirror_cld) {
 190   if (create_mirror_cld) {
 191     // Add a new class loader data to the graph.
 192     return ClassLoaderDataGraph::add(class_loader, true);
 193   } else {
 194     return (class_loader() == nullptr) ? ClassLoaderData::the_null_class_loader_data() :
 195                                       ClassLoaderDataGraph::find_or_create(class_loader);
















 196   }
 197 }
 198 
 199 void SystemDictionary::set_system_loader(ClassLoaderData *cld) {
 200   if (_java_system_loader.is_empty()) {
 201     _java_system_loader = cld->class_loader_handle();
 202   } else {
 203     assert(_java_system_loader.resolve() == cld->class_loader(), "sanity");
 204   }
 205 }
 206 
 207 void SystemDictionary::set_platform_loader(ClassLoaderData *cld) {
 208   if (_java_platform_loader.is_empty()) {
 209     _java_platform_loader = cld->class_loader_handle();
 210   } else {
 211     assert(_java_platform_loader.resolve() == cld->class_loader(), "sanity");
 212   }
 213 }
 214 
 215 // ----------------------------------------------------------------------------

 383     }
 384   } else {
 385     k = Universe::typeArrayKlass(t);
 386     k = k->array_klass(ndims, CHECK_NULL);
 387   }
 388   return k;
 389 }
 390 
 391 static inline void log_circularity_error(Symbol* name, PlaceholderEntry* probe) {
 392   LogTarget(Debug, class, load, placeholders) lt;
 393   if (lt.is_enabled()) {
 394     ResourceMark rm;
 395     LogStream ls(lt);
 396     ls.print("ClassCircularityError detected for placeholder entry %s", name->as_C_string());
 397     probe->print_on(&ls);
 398     ls.cr();
 399   }
 400 }
 401 
 402 // Must be called for any superclass or superinterface resolution
 403 // during class definition to allow class circularity checking

 404 // superinterface callers:
 405 //    parse_interfaces - from defineClass
 406 // superclass callers:
 407 //   ClassFileParser - from defineClass
 408 //   load_shared_class - while loading a class from shared archive
 409 //   resolve_instance_class_or_null:
 410 //     via: handle_parallel_super_load
 411 //      when resolving a class that has an existing placeholder with
 412 //      a saved superclass [i.e. a defineClass is currently in progress]
 413 //      If another thread is trying to resolve the class, it must do
 414 //      superclass checks on its own thread to catch class circularity and
 415 //      to avoid deadlock.


 416 //
 417 // resolve_with_circularity_detection adds a DETECT_CIRCULARITY placeholder to the placeholder table before calling
 418 // resolve_instance_class_or_null. ClassCircularityError is detected when a DETECT_CIRCULARITY or LOAD_INSTANCE
 419 // placeholder for the same thread, class, classloader is found.
 420 // This can be seen with logging option: -Xlog:class+load+placeholders=debug.
 421 //
 422 InstanceKlass* SystemDictionary::resolve_with_circularity_detection(Symbol* class_name,
 423                                                                     Symbol* next_name,
 424                                                                     Handle class_loader,
 425                                                                     bool is_superclass,
 426                                                                     TRAPS) {
 427 
 428   assert(next_name != nullptr, "null superclass for resolving");
 429   assert(!Signature::is_array(next_name), "invalid superclass name");
 430 
 431   ClassLoaderData* loader_data = class_loader_data(class_loader);
 432 
 433   if (is_superclass) {
 434     InstanceKlass* klassk = loader_data->dictionary()->find_class(THREAD, class_name);
 435     if (klassk != nullptr) {
 436       // We can come here for two reasons:
 437       // (a) RedefineClasses -- the class is already loaded
 438       // (b) Rarely, the class might have been loaded by a parallel thread
 439       // We can do a quick check against the already assigned superclass's name and loader.
 440       InstanceKlass* superk = klassk->super();
 441       if (superk != nullptr &&
 442           superk->name() == next_name &&
 443           superk->class_loader() == class_loader()) {
 444         return superk;
 445       }
 446     }
 447   }
 448 
 449   // can't throw error holding a lock
 450   bool throw_circularity_error = false;
 451   {
 452     MutexLocker mu(THREAD, SystemDictionary_lock);
 453 
 454     // Must check ClassCircularity before resolving next_name (superclass or interface).
 455     PlaceholderEntry* probe = PlaceholderTable::get_entry(class_name, loader_data);
 456     if (probe != nullptr && probe->check_seen_thread(THREAD, PlaceholderTable::DETECT_CIRCULARITY)) {
 457         log_circularity_error(class_name, probe);
 458         throw_circularity_error = true;
 459     }
 460 
 461     // Make sure there's a placeholder for the class_name before resolving.
 462     // This is used as a claim that this thread is currently loading superclass/classloader
 463     // and for ClassCircularity checks.
 464     if (!throw_circularity_error) {
 465       // Be careful not to exit resolve_with_circularity_detection without removing this placeholder.
 466       PlaceholderEntry* newprobe = PlaceholderTable::find_and_add(class_name,
 467                                                                   loader_data,
 468                                                                   PlaceholderTable::DETECT_CIRCULARITY,
 469                                                                   next_name, THREAD);
 470     }
 471   }
 472 
 473   if (throw_circularity_error) {
 474       ResourceMark rm(THREAD);
 475       THROW_MSG_NULL(vmSymbols::java_lang_ClassCircularityError(), class_name->as_C_string());
 476   }
 477 
 478   // Resolve the superclass or superinterface, check results on return
 479   InstanceKlass* superk =
 480     SystemDictionary::resolve_instance_class_or_null(next_name,
 481                                                      class_loader,
 482                                                      THREAD);
 483 
 484   // Clean up placeholder entry.
 485   {
 486     MutexLocker mu(THREAD, SystemDictionary_lock);
 487     PlaceholderTable::find_and_remove(class_name, loader_data, PlaceholderTable::DETECT_CIRCULARITY, THREAD);
 488     SystemDictionary_lock->notify_all();
 489   }
 490 
 491   // Check for pending exception or null superk, and throw exception
 492   if (HAS_PENDING_EXCEPTION || superk == nullptr) {
 493     handle_resolution_exception(next_name, true, CHECK_NULL);
 494   }
 495 
 496   return superk;
 497 }
 498 

 896 
 897 InstanceKlass* SystemDictionary::resolve_from_stream(ClassFileStream* st,
 898                                                      Symbol* class_name,
 899                                                      Handle class_loader,
 900                                                      const ClassLoadInfo& cl_info,
 901                                                      TRAPS) {
 902   if (cl_info.is_hidden()) {
 903     return resolve_hidden_class_from_stream(st, class_name, class_loader, cl_info, CHECK_NULL);
 904   } else {
 905     return resolve_class_from_stream(st, class_name, class_loader, cl_info, CHECK_NULL);
 906   }
 907 }
 908 
 909 
 910 #if INCLUDE_CDS
 911 // Check if a shared class can be loaded by the specific classloader.
 912 bool SystemDictionary::is_shared_class_visible(Symbol* class_name,
 913                                                InstanceKlass* ik,
 914                                                PackageEntry* pkg_entry,
 915                                                Handle class_loader) {
 916   assert(!ModuleEntryTable::javabase_moduleEntry()->is_patched(),
 917          "Cannot use sharing if java.base is patched");
 918 
 919   // (1) Check if we are loading into the same loader as in dump time.
 920 
 921   if (ik->defined_by_boot_loader()) {
 922     if (class_loader() != nullptr) {
 923       return false;
 924     }
 925   } else if (ik->defined_by_platform_loader()) {
 926     if (class_loader() != java_platform_loader()) {
 927       return false;
 928     }
 929   } else if (ik->defined_by_app_loader()) {
 930     if (class_loader() != java_system_loader()) {
 931       return false;
 932     }
 933   } else {
 934     // ik was loaded by a custom loader during dump time
 935     if (class_loader_data(class_loader)->is_builtin_class_loader_data()) {
 936       return false;
 937     } else {

 973 
 974   if (pkg_entry == nullptr) {
 975     // We might have looked up pkg_entry before the module system was initialized.
 976     // Need to reload it now.
 977     TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);
 978     if (pkg_name != nullptr) {
 979       pkg_entry = class_loader_data(class_loader)->packages()->lookup_only(pkg_name);
 980     }
 981   }
 982 
 983   ModuleEntry* mod_entry = (pkg_entry == nullptr) ? nullptr : pkg_entry->module();
 984   bool should_be_in_named_module = (mod_entry != nullptr && mod_entry->is_named());
 985   bool was_archived_from_named_module = !cl->has_unnamed_module();
 986   bool visible;
 987 
 988   if (was_archived_from_named_module) {
 989     if (should_be_in_named_module) {
 990       // Is the module loaded from the same location as during dump time?
 991       visible = mod_entry->shared_path_index() == scp_index;
 992       if (visible) {
 993         assert(!mod_entry->is_patched(), "cannot load archived classes for patched module");
 994       }
 995     } else {
 996       // During dump time, this class was in a named module, but at run time, this class should be
 997       // in an unnamed module.
 998       visible = false;
 999     }
1000   } else {
1001     if (should_be_in_named_module) {
1002       // During dump time, this class was in an unnamed, but at run time, this class should be
1003       // in a named module.
1004       visible = false;
1005     } else {
1006       visible = true;
1007     }
1008   }
1009 
1010   return visible;
1011 }
1012 
1013 bool SystemDictionary::check_shared_class_super_type(InstanceKlass* klass, InstanceKlass* super_type,

1051                                                      class_loader, true,
1052                                                      CHECK_false);
1053     if (!check_super) {
1054       return false;
1055     }
1056   }
1057 
1058   Array<InstanceKlass*>* interfaces = ik->local_interfaces();
1059   int num_interfaces = interfaces->length();
1060   for (int index = 0; index < num_interfaces; index++) {
1061     bool check_interface = check_shared_class_super_type(ik, interfaces->at(index), class_loader, false,
1062                                                          CHECK_false);
1063     if (!check_interface) {
1064       return false;
1065     }
1066   }
1067 
1068   return true;
1069 }
1070 





































































1071 InstanceKlass* SystemDictionary::load_shared_class(InstanceKlass* ik,
1072                                                    Handle class_loader,
1073                                                    Handle protection_domain,
1074                                                    const ClassFileStream *cfs,
1075                                                    PackageEntry* pkg_entry,
1076                                                    TRAPS) {
1077   assert(ik != nullptr, "sanity");
1078   assert(ik->in_aot_cache(), "sanity");
1079   assert(!ik->is_unshareable_info_restored(), "shared class can be restored only once");
1080   assert(AtomicAccess::add(&ik->_shared_class_load_count, 1) == 1, "shared class loaded more than once");
1081   Symbol* class_name = ik->name();
1082 
1083   if (!is_shared_class_visible(class_name, ik, pkg_entry, class_loader)) {
1084     ik->set_shared_loading_failed();
1085     return nullptr;
1086   }
1087 
1088   bool check = check_shared_class_super_types(ik, class_loader, CHECK_NULL);
1089   if (!check) {
1090     ik->set_shared_loading_failed();
1091     return nullptr;
1092   }
1093 





















1094   InstanceKlass* new_ik = nullptr;
1095   // CFLH check is skipped for VM hidden classes (see KlassFactory::create_from_stream).
1096   // It will be skipped for shared VM hidden lambda proxy classes.
1097   if (!ik->is_hidden()) {
1098     new_ik = KlassFactory::check_shared_class_file_load_hook(
1099       ik, class_name, class_loader, protection_domain, cfs, CHECK_NULL);
1100   }
1101   if (new_ik != nullptr) {
1102     // The class is changed by CFLH. Return the new class. The shared class is
1103     // not used.
1104     return new_ik;
1105   }
1106 
1107   // Adjust methods to recover missing data.  They need addresses for
1108   // interpreter entry points and their default native method address
1109   // must be reset.
1110 
1111   // Shared classes are all currently loaded by either the bootstrap or
1112   // internal parallel class loaders, so this will never cause a deadlock
1113   // on a custom class loader lock.
1114   // Since this class is already locked with parallel capable class
1115   // loaders, including the bootstrap loader via the placeholder table,
1116   // this lock is currently a nop.
1117 
1118   ClassLoaderData* loader_data = class_loader_data(class_loader);
1119   {
1120     HandleMark hm(THREAD);
1121     Handle lockObject = get_loader_lock_or_null(class_loader);
1122     ObjectLocker ol(lockObject, THREAD);
1123     // prohibited package check assumes all classes loaded from archive call
1124     // restore_unshareable_info which calls ik->set_package()
1125     ik->restore_unshareable_info(loader_data, protection_domain, pkg_entry, CHECK_NULL);
1126   }
1127 
1128   load_shared_class_misc(ik, loader_data);

1129   return ik;
1130 }
1131 
1132 void SystemDictionary::load_shared_class_misc(InstanceKlass* ik, ClassLoaderData* loader_data) {
1133   ik->print_class_load_logging(loader_data, nullptr, nullptr);
1134 
1135   // For boot loader, ensure that GetSystemPackage knows that a class in this
1136   // package was loaded.
1137   if (loader_data->is_the_null_class_loader_data()) {
1138     s2 path_index = ik->shared_classpath_index();
1139     ik->set_classpath_index(path_index);
1140   }
1141 
1142   // notify a class loaded from shared object
1143   ClassLoadingService::notify_class_loaded(ik, true /* shared class */);
1144 
1145   if (CDSConfig::is_dumping_final_static_archive()) {
1146     SystemDictionaryShared::init_dumptime_info_from_preimage(ik);
1147   }
1148 }

1690   }
1691 }
1692 
1693 // Update class loader data dictionary - done after check_constraint and add_to_hierarchy
1694 // have been called.
1695 void SystemDictionary::update_dictionary(JavaThread* current,
1696                                          InstanceKlass* k,
1697                                          ClassLoaderData* loader_data) {
1698   MonitorLocker mu1(SystemDictionary_lock);
1699 
1700   // Make a new dictionary entry.
1701   Symbol* name  = k->name();
1702   Dictionary* dictionary = loader_data->dictionary();
1703   InstanceKlass* sd_check = dictionary->find_class(current, name);
1704   if (sd_check == nullptr) {
1705     dictionary->add_klass(current, name, k);
1706   }
1707   mu1.notify_all();
1708 }
1709 
1710 #if INCLUDE_CDS
1711 // Indicate that loader_data has initiated the loading of class k, which
1712 // has already been defined by a parent loader.
1713 // This API should be used only by AOTLinkedClassBulkLoader


1714 void SystemDictionary::add_to_initiating_loader(JavaThread* current,
1715                                                 InstanceKlass* k,
1716                                                 ClassLoaderData* loader_data) {
1717   assert(CDSConfig::is_using_aot_linked_classes(), "must be");
1718   assert_locked_or_safepoint(SystemDictionary_lock);
1719   Symbol* name  = k->name();
1720   Dictionary* dictionary = loader_data->dictionary();
1721   assert(k->is_loaded(), "must be");
1722   assert(k->class_loader_data() != loader_data, "only for classes defined by a parent loader");
1723   assert(dictionary->find_class(current, name) == nullptr, "sanity");
1724   dictionary->add_klass(current, name, k);

1725 }
1726 #endif
1727 
1728 // Try to find a class name using the loader constraints.  The
1729 // loader constraints might know about a class that isn't fully loaded
1730 // yet and these will be ignored.
1731 Klass* SystemDictionary::find_constrained_instance_or_array_klass(
1732                     Thread* current, Symbol* class_name, Handle class_loader) {
1733 
1734   // First see if it has been loaded directly.
1735   Klass* klass = find_instance_or_array_klass(current, class_name, class_loader);
1736   if (klass != nullptr)
1737     return klass;
1738 
1739   // Now look to see if it has been loaded elsewhere, and is subject to
1740   // a loader constraint that would require this loader to return the
1741   // klass that is already loaded.
1742   if (Signature::is_array(class_name)) {
1743     // For array classes, their Klass*s are not kept in the
1744     // constraint table. The element Klass*s are.
1745     SignatureStream ss(class_name, false);
1746     int ndims = ss.skip_array_prefix();  // skip all '['s

  38 #include "classfile/packageEntry.hpp"
  39 #include "classfile/placeholders.hpp"
  40 #include "classfile/resolutionErrors.hpp"
  41 #include "classfile/stringTable.hpp"
  42 #include "classfile/symbolTable.hpp"
  43 #include "classfile/systemDictionary.hpp"
  44 #include "classfile/vmClasses.hpp"
  45 #include "classfile/vmSymbols.hpp"
  46 #include "gc/shared/gcTraceTime.inline.hpp"
  47 #include "interpreter/bootstrapInfo.hpp"
  48 #include "jfr/jfrEvents.hpp"
  49 #include "jvm.h"
  50 #include "logging/log.hpp"
  51 #include "logging/logStream.hpp"
  52 #include "memory/metaspaceClosure.hpp"
  53 #include "memory/oopFactory.hpp"
  54 #include "memory/resourceArea.hpp"
  55 #include "memory/universe.hpp"
  56 #include "oops/access.inline.hpp"
  57 #include "oops/constantPool.inline.hpp"
  58 #include "oops/fieldStreams.inline.hpp"
  59 #include "oops/inlineKlass.inline.hpp"
  60 #include "oops/instanceKlass.hpp"
  61 #include "oops/klass.inline.hpp"
  62 #include "oops/method.inline.hpp"
  63 #include "oops/objArrayKlass.hpp"
  64 #include "oops/objArrayOop.inline.hpp"
  65 #include "oops/oop.inline.hpp"
  66 #include "oops/oopHandle.inline.hpp"
  67 #include "oops/symbol.hpp"
  68 #include "oops/typeArrayKlass.hpp"
  69 #include "prims/jvmtiExport.hpp"
  70 #include "prims/methodHandles.hpp"
  71 #include "runtime/arguments.hpp"
  72 #include "runtime/atomicAccess.hpp"
  73 #include "runtime/handles.inline.hpp"
  74 #include "runtime/java.hpp"
  75 #include "runtime/javaCalls.hpp"
  76 #include "runtime/mutexLocker.hpp"
  77 #include "runtime/os.hpp"
  78 #include "runtime/sharedRuntime.hpp"
  79 #include "runtime/signature.hpp"
  80 #include "runtime/synchronizer.hpp"
  81 #include "services/classLoadingService.hpp"
  82 #include "services/diagnosticCommand.hpp"
  83 #include "services/finalizerService.hpp"
  84 #include "services/threadService.hpp"
  85 #include "utilities/growableArray.hpp"
  86 #include "utilities/macros.hpp"
  87 #include "utilities/utf8.hpp"
  88 #if INCLUDE_CDS
  89 #include "classfile/systemDictionaryShared.hpp"
  90 #endif
  91 #if INCLUDE_JFR
  92 #include "jfr/jfr.hpp"
  93 #endif
  94 
  95 class InvokeMethodKey : public StackObj {
  96   private:
  97     Symbol* _symbol;

 172                          CHECK_NULL);
 173   return result.get_oop();
 174 }
 175 
 176 oop SystemDictionary::get_platform_class_loader_impl(TRAPS) {
 177   JavaValue result(T_OBJECT);
 178   InstanceKlass* class_loader_klass = vmClasses::ClassLoader_klass();
 179   JavaCalls::call_static(&result,
 180                          class_loader_klass,
 181                          vmSymbols::getPlatformClassLoader_name(),
 182                          vmSymbols::void_classloader_signature(),
 183                          CHECK_NULL);
 184   return result.get_oop();
 185 }
 186 
 187 // Helper function
 188 inline ClassLoaderData* class_loader_data(Handle class_loader) {
 189   return ClassLoaderData::class_loader_data(class_loader());
 190 }
 191 
 192 // These migrated value classes are loaded by the bootstrap class loader but are added to the initiating
 193 // loaders automatically so that fields of these types can be found and potentially flattened during
 194 // field layout.
 195 static void add_migrated_value_classes(ClassLoaderData* cld) {
 196   JavaThread* current = JavaThread::current();
 197   auto add_klass = [&] (Symbol* classname) {
 198     InstanceKlass* ik = SystemDictionary::find_instance_klass(current, classname, Handle(current, nullptr));
 199     assert(ik != nullptr, "Must exist");
 200     SystemDictionary::add_to_initiating_loader(current, ik, cld);
 201   };
 202 
 203   MonitorLocker mu1(SystemDictionary_lock);
 204   vmSymbols::migrated_class_names_do(add_klass);
 205 }
 206 
 207 ClassLoaderData* SystemDictionary::register_loader(Handle class_loader, bool create_mirror_cld) {
 208   if (create_mirror_cld) {
 209     // Add a new class loader data to the graph.
 210     return ClassLoaderDataGraph::add(class_loader, true);
 211   } else {
 212     if (class_loader() == nullptr) {
 213       return ClassLoaderData::the_null_class_loader_data();
 214     } else {
 215       bool created = false;
 216       ClassLoaderData* cld = ClassLoaderDataGraph::find_or_create(class_loader, created);
 217       if (created && Arguments::enable_preview()) {
 218         if (CDSConfig::is_using_aot_linked_classes() && java_system_loader() == nullptr) {
 219           // We are inside AOTLinkedClassBulkLoader::preload_classes().
 220           //
 221           // AOTLinkedClassBulkLoader will automatically initiate the loading of all archived
 222           // public classes from the boot loader into platform/system loaders, so there's
 223           // no need to call add_migrated_value_classes().
 224         } else {
 225           add_migrated_value_classes(cld);
 226         }
 227       }
 228       return cld;
 229     }
 230   }
 231 }
 232 
 233 void SystemDictionary::set_system_loader(ClassLoaderData *cld) {
 234   if (_java_system_loader.is_empty()) {
 235     _java_system_loader = cld->class_loader_handle();
 236   } else {
 237     assert(_java_system_loader.resolve() == cld->class_loader(), "sanity");
 238   }
 239 }
 240 
 241 void SystemDictionary::set_platform_loader(ClassLoaderData *cld) {
 242   if (_java_platform_loader.is_empty()) {
 243     _java_platform_loader = cld->class_loader_handle();
 244   } else {
 245     assert(_java_platform_loader.resolve() == cld->class_loader(), "sanity");
 246   }
 247 }
 248 
 249 // ----------------------------------------------------------------------------

 417     }
 418   } else {
 419     k = Universe::typeArrayKlass(t);
 420     k = k->array_klass(ndims, CHECK_NULL);
 421   }
 422   return k;
 423 }
 424 
 425 static inline void log_circularity_error(Symbol* name, PlaceholderEntry* probe) {
 426   LogTarget(Debug, class, load, placeholders) lt;
 427   if (lt.is_enabled()) {
 428     ResourceMark rm;
 429     LogStream ls(lt);
 430     ls.print("ClassCircularityError detected for placeholder entry %s", name->as_C_string());
 431     probe->print_on(&ls);
 432     ls.cr();
 433   }
 434 }
 435 
 436 // Must be called for any superclass or superinterface resolution
 437 // during class definition, or may be called for inline field layout processing
 438 // to detect class circularity errors.
 439 // superinterface callers:
 440 //    parse_interfaces - from defineClass
 441 // superclass callers:
 442 //   ClassFileParser - from defineClass
 443 //   load_shared_class - while loading a class from shared archive
 444 //   resolve_instance_class_or_null:
 445 //     via: handle_parallel_super_load
 446 //      when resolving a class that has an existing placeholder with
 447 //      a saved superclass [i.e. a defineClass is currently in progress]
 448 //      If another thread is trying to resolve the class, it must do
 449 //      superclass checks on its own thread to catch class circularity and
 450 //      to avoid deadlock.
 451 // inline field layout callers:
 452 //    The field's class must be loaded to determine layout.
 453 //
 454 // resolve_with_circularity_detection adds a DETECT_CIRCULARITY placeholder to the placeholder table before calling
 455 // resolve_instance_class_or_null. ClassCircularityError is detected when a DETECT_CIRCULARITY or LOAD_INSTANCE
 456 // placeholder for the same thread, class, and classloader is found.
 457 // This can be seen with logging option: -Xlog:class+load+placeholders=debug.
 458 //
 459 InstanceKlass* SystemDictionary::resolve_with_circularity_detection(Symbol* class_name,
 460                                                                     Symbol* next_name,
 461                                                                     Handle class_loader,
 462                                                                     bool is_superclass,
 463                                                                     TRAPS) {
 464 
 465   assert(next_name != nullptr, "null superclass for resolving");
 466   assert(!Signature::is_array(next_name), "invalid superclass name");
 467 
 468   ClassLoaderData* loader_data = class_loader_data(class_loader);
 469 
 470   if (is_superclass) {
 471     InstanceKlass* klassk = loader_data->dictionary()->find_class(THREAD, class_name);
 472     if (klassk != nullptr) {
 473       // We can come here for two reasons:
 474       // (a) RedefineClasses -- the class is already loaded
 475       // (b) Rarely, the class might have been loaded by a parallel thread
 476       // We can do a quick check against the already assigned superclass's name and loader.
 477       InstanceKlass* superk = klassk->super();
 478       if (superk != nullptr &&
 479           superk->name() == next_name &&
 480           superk->class_loader() == class_loader()) {
 481         return superk;
 482       }
 483     }
 484   }
 485 
 486   // can't throw error holding a lock
 487   bool throw_circularity_error = false;
 488   {
 489     MutexLocker mu(THREAD, SystemDictionary_lock);
 490 
 491     // Must check ClassCircularity before resolving next_name (superclass, interface, field types or speculatively preloaded argument types).
 492     PlaceholderEntry* probe = PlaceholderTable::get_entry(class_name, loader_data);
 493     if (probe != nullptr && probe->check_seen_thread(THREAD, PlaceholderTable::DETECT_CIRCULARITY)) {
 494         log_circularity_error(class_name, probe);
 495         throw_circularity_error = true;
 496     }
 497 
 498     // Make sure there's a placeholder for the class_name before resolving.
 499     // This is used as a claim that this thread is currently loading superclass/classloader
 500     // and for ClassCircularity checks.
 501     if (!throw_circularity_error) {
 502       // Be careful not to exit resolve_with_circularity_detection without removing this placeholder.
 503       PlaceholderEntry* newprobe = PlaceholderTable::find_and_add(class_name,
 504                                                                   loader_data,
 505                                                                   PlaceholderTable::DETECT_CIRCULARITY,
 506                                                                   next_name, THREAD);
 507     }
 508   }
 509 
 510   if (throw_circularity_error) {
 511       ResourceMark rm(THREAD);
 512       THROW_MSG_NULL(vmSymbols::java_lang_ClassCircularityError(), class_name->as_C_string());
 513   }
 514 
 515   // Resolve the superclass, superinterface, field type or speculatively preloaded argument types and check results on return.
 516   InstanceKlass* superk =
 517     SystemDictionary::resolve_instance_class_or_null(next_name,
 518                                                      class_loader,
 519                                                      THREAD);
 520 
 521   // Clean up placeholder entry.
 522   {
 523     MutexLocker mu(THREAD, SystemDictionary_lock);
 524     PlaceholderTable::find_and_remove(class_name, loader_data, PlaceholderTable::DETECT_CIRCULARITY, THREAD);
 525     SystemDictionary_lock->notify_all();
 526   }
 527 
 528   // Check for pending exception or null superk, and throw exception
 529   if (HAS_PENDING_EXCEPTION || superk == nullptr) {
 530     handle_resolution_exception(next_name, true, CHECK_NULL);
 531   }
 532 
 533   return superk;
 534 }
 535 

 933 
 934 InstanceKlass* SystemDictionary::resolve_from_stream(ClassFileStream* st,
 935                                                      Symbol* class_name,
 936                                                      Handle class_loader,
 937                                                      const ClassLoadInfo& cl_info,
 938                                                      TRAPS) {
 939   if (cl_info.is_hidden()) {
 940     return resolve_hidden_class_from_stream(st, class_name, class_loader, cl_info, CHECK_NULL);
 941   } else {
 942     return resolve_class_from_stream(st, class_name, class_loader, cl_info, CHECK_NULL);
 943   }
 944 }
 945 
 946 
 947 #if INCLUDE_CDS
 948 // Check if a shared class can be loaded by the specific classloader.
 949 bool SystemDictionary::is_shared_class_visible(Symbol* class_name,
 950                                                InstanceKlass* ik,
 951                                                PackageEntry* pkg_entry,
 952                                                Handle class_loader) {
 953   assert(!CDSConfig::module_patching_disables_cds(), "Cannot use CDS");

 954 
 955   // (1) Check if we are loading into the same loader as in dump time.
 956 
 957   if (ik->defined_by_boot_loader()) {
 958     if (class_loader() != nullptr) {
 959       return false;
 960     }
 961   } else if (ik->defined_by_platform_loader()) {
 962     if (class_loader() != java_platform_loader()) {
 963       return false;
 964     }
 965   } else if (ik->defined_by_app_loader()) {
 966     if (class_loader() != java_system_loader()) {
 967       return false;
 968     }
 969   } else {
 970     // ik was loaded by a custom loader during dump time
 971     if (class_loader_data(class_loader)->is_builtin_class_loader_data()) {
 972       return false;
 973     } else {

1009 
1010   if (pkg_entry == nullptr) {
1011     // We might have looked up pkg_entry before the module system was initialized.
1012     // Need to reload it now.
1013     TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);
1014     if (pkg_name != nullptr) {
1015       pkg_entry = class_loader_data(class_loader)->packages()->lookup_only(pkg_name);
1016     }
1017   }
1018 
1019   ModuleEntry* mod_entry = (pkg_entry == nullptr) ? nullptr : pkg_entry->module();
1020   bool should_be_in_named_module = (mod_entry != nullptr && mod_entry->is_named());
1021   bool was_archived_from_named_module = !cl->has_unnamed_module();
1022   bool visible;
1023 
1024   if (was_archived_from_named_module) {
1025     if (should_be_in_named_module) {
1026       // Is the module loaded from the same location as during dump time?
1027       visible = mod_entry->shared_path_index() == scp_index;
1028       if (visible) {
1029         assert(!CDSConfig::module_patching_disables_cds(), "Cannot use CDS");
1030       }
1031     } else {
1032       // During dump time, this class was in a named module, but at run time, this class should be
1033       // in an unnamed module.
1034       visible = false;
1035     }
1036   } else {
1037     if (should_be_in_named_module) {
1038       // During dump time, this class was in an unnamed, but at run time, this class should be
1039       // in a named module.
1040       visible = false;
1041     } else {
1042       visible = true;
1043     }
1044   }
1045 
1046   return visible;
1047 }
1048 
1049 bool SystemDictionary::check_shared_class_super_type(InstanceKlass* klass, InstanceKlass* super_type,

1087                                                      class_loader, true,
1088                                                      CHECK_false);
1089     if (!check_super) {
1090       return false;
1091     }
1092   }
1093 
1094   Array<InstanceKlass*>* interfaces = ik->local_interfaces();
1095   int num_interfaces = interfaces->length();
1096   for (int index = 0; index < num_interfaces; index++) {
1097     bool check_interface = check_shared_class_super_type(ik, interfaces->at(index), class_loader, false,
1098                                                          CHECK_false);
1099     if (!check_interface) {
1100       return false;
1101     }
1102   }
1103 
1104   return true;
1105 }
1106 
1107 // Pre-load class referred to in non-static null-free instance field. These fields trigger MANDATORY loading.
1108 // Some pre-loading does not fail fatally
1109 bool SystemDictionary::preload_from_null_free_field(InstanceKlass* ik, Handle class_loader, Symbol* sig, int field_index, TRAPS) {
1110   TempNewSymbol name = Signature::strip_envelope(sig);
1111   log_info(class, preload)("Preloading of class %s during loading of shared class %s. "
1112                            "Cause: a null-free non-static field is declared with this type",
1113                            name->as_C_string(), ik->name()->as_C_string());
1114   InstanceKlass* real_k = SystemDictionary::resolve_with_circularity_detection(ik->name(), name,
1115                                                                                class_loader, false, CHECK_false);
1116   if (HAS_PENDING_EXCEPTION) {
1117     log_info(class, preload)("Preloading of class %s during loading of class %s "
1118                                 "(cause: null-free non-static field) failed: %s",
1119                                 name->as_C_string(), ik->name()->as_C_string(),
1120                                 PENDING_EXCEPTION->klass()->name()->as_C_string());
1121     return false; // Exception is still pending
1122   }
1123 
1124   InstanceKlass* k = ik->get_inline_type_field_klass_or_null(field_index);
1125   if (real_k != k) {
1126     // oops, the app has substituted a different version of k! Does not fail fatally
1127     log_info(class, preload)("Preloading of class %s during loading of shared class %s "
1128                                 "(cause: null-free non-static field) failed : "
1129                                 "app substituted a different version of %s",
1130                                 name->as_C_string(), ik->name()->as_C_string(),
1131                                 name->as_C_string());
1132     return false;
1133   }
1134   log_info(class, preload)("Preloading of class %s during loading of shared class %s "
1135                            "(cause: null-free non-static field) succeeded",
1136                            name->as_C_string(), ik->name()->as_C_string());
1137 
1138   assert(real_k != nullptr, "Sanity check");
1139   InstanceKlass::check_can_be_annotated_with_NullRestricted(real_k, ik->name(), CHECK_false);
1140 
1141   return true;
1142 }
1143 
1144 // Tries to pre-load classes referred to in non-static nullable instance fields if they are found in the
1145 // loadable descriptors attribute. If loading fails, we can fail silently.
1146 void SystemDictionary::try_preload_from_loadable_descriptors(InstanceKlass* ik, Handle class_loader, Symbol* sig, int field_index, TRAPS) {
1147   TempNewSymbol name = Signature::strip_envelope(sig);
1148   if (name != ik->name() && ik->is_class_in_loadable_descriptors_attribute(sig)) {
1149     log_info(class, preload)("Preloading of class %s during loading of shared class %s. "
1150                              "Cause: field type in LoadableDescriptors attribute",
1151                              name->as_C_string(), ik->name()->as_C_string());
1152     InstanceKlass* real_k = SystemDictionary::resolve_with_circularity_detection(ik->name(), name,
1153                                                                                  class_loader, false, THREAD);
1154     if (HAS_PENDING_EXCEPTION) {
1155       CLEAR_PENDING_EXCEPTION;
1156     }
1157 
1158     InstanceKlass* k = ik->get_inline_type_field_klass_or_null(field_index);
1159     if (real_k != k) {
1160       // oops, the app has substituted a different version of k!
1161       log_info(class, preload)("Preloading of class %s during loading of shared class %s "
1162                                   "(cause: field type in LoadableDescriptors attribute) failed : "
1163                                   "app substituted a different version of %s",
1164                                   name->as_C_string(), ik->name()->as_C_string(),
1165                                   k->name()->as_C_string());
1166       return;
1167     } else if (real_k != nullptr) {
1168       log_info(class, preload)("Preloading of class %s during loading of shared class %s "
1169                                "(cause: field type in LoadableDescriptors attribute) succeeded",
1170                                 name->as_C_string(), ik->name()->as_C_string());
1171     }
1172   }
1173 }
1174 
1175 
1176 InstanceKlass* SystemDictionary::load_shared_class(InstanceKlass* ik,
1177                                                    Handle class_loader,
1178                                                    Handle protection_domain,
1179                                                    const ClassFileStream *cfs,
1180                                                    PackageEntry* pkg_entry,
1181                                                    TRAPS) {
1182   assert(ik != nullptr, "sanity");
1183   assert(ik->in_aot_cache(), "sanity");
1184   assert(!ik->is_unshareable_info_restored(), "shared class can be restored only once");
1185   assert(AtomicAccess::add(&ik->_shared_class_load_count, 1) == 1, "shared class loaded more than once");
1186   Symbol* class_name = ik->name();
1187 
1188   if (!is_shared_class_visible(class_name, ik, pkg_entry, class_loader)) {
1189     ik->set_shared_loading_failed();
1190     return nullptr;
1191   }
1192 
1193   bool check = check_shared_class_super_types(ik, class_loader, CHECK_NULL);
1194   if (!check) {
1195     ik->set_shared_loading_failed();
1196     return nullptr;
1197   }
1198 
1199   if (ik->has_inline_type_fields()) {
1200     for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
1201       if (fs.access_flags().is_static()) continue;
1202 
1203       Symbol* sig = fs.signature();
1204       int field_index = fs.index();
1205 
1206       if (fs.is_null_free_inline_type()) {
1207         // A false return means that the class didn't load for other reasons than an exception.
1208         bool check = preload_from_null_free_field(ik, class_loader, sig, field_index, CHECK_NULL);
1209         if (!check) {
1210           ik->set_shared_loading_failed();
1211           return nullptr;
1212         }
1213       } else if (Signature::has_envelope(sig)) {
1214           // Pending exceptions are cleared so we can fail silently
1215           try_preload_from_loadable_descriptors(ik, class_loader, sig, field_index, CHECK_NULL);
1216       }
1217     }
1218   }
1219 
1220   InstanceKlass* new_ik = nullptr;
1221   // CFLH check is skipped for VM hidden classes (see KlassFactory::create_from_stream).
1222   // It will be skipped for shared VM hidden lambda proxy classes.
1223   if (!ik->is_hidden()) {
1224     new_ik = KlassFactory::check_shared_class_file_load_hook(
1225       ik, class_name, class_loader, protection_domain, cfs, CHECK_NULL);
1226   }
1227   if (new_ik != nullptr) {
1228     // The class is changed by CFLH. Return the new class. The shared class is
1229     // not used.
1230     return new_ik;
1231   }
1232 
1233   // Adjust methods to recover missing data.  They need addresses for
1234   // interpreter entry points and their default native method address
1235   // must be reset.
1236 
1237   // Shared classes are all currently loaded by either the bootstrap or
1238   // internal parallel class loaders, so this will never cause a deadlock
1239   // on a custom class loader lock.
1240   // Since this class is already locked with parallel capable class
1241   // loaders, including the bootstrap loader via the placeholder table,
1242   // this lock is currently a nop.
1243 
1244   ClassLoaderData* loader_data = class_loader_data(class_loader);
1245   {
1246     HandleMark hm(THREAD);
1247     Handle lockObject = get_loader_lock_or_null(class_loader);
1248     ObjectLocker ol(lockObject, THREAD);
1249     // prohibited package check assumes all classes loaded from archive call
1250     // restore_unshareable_info which calls ik->set_package()
1251     ik->restore_unshareable_info(loader_data, protection_domain, pkg_entry, CHECK_NULL);
1252   }
1253 
1254   load_shared_class_misc(ik, loader_data);
1255 
1256   return ik;
1257 }
1258 
1259 void SystemDictionary::load_shared_class_misc(InstanceKlass* ik, ClassLoaderData* loader_data) {
1260   ik->print_class_load_logging(loader_data, nullptr, nullptr);
1261 
1262   // For boot loader, ensure that GetSystemPackage knows that a class in this
1263   // package was loaded.
1264   if (loader_data->is_the_null_class_loader_data()) {
1265     s2 path_index = ik->shared_classpath_index();
1266     ik->set_classpath_index(path_index);
1267   }
1268 
1269   // notify a class loaded from shared object
1270   ClassLoadingService::notify_class_loaded(ik, true /* shared class */);
1271 
1272   if (CDSConfig::is_dumping_final_static_archive()) {
1273     SystemDictionaryShared::init_dumptime_info_from_preimage(ik);
1274   }
1275 }

1817   }
1818 }
1819 
1820 // Update class loader data dictionary - done after check_constraint and add_to_hierarchy
1821 // have been called.
1822 void SystemDictionary::update_dictionary(JavaThread* current,
1823                                          InstanceKlass* k,
1824                                          ClassLoaderData* loader_data) {
1825   MonitorLocker mu1(SystemDictionary_lock);
1826 
1827   // Make a new dictionary entry.
1828   Symbol* name  = k->name();
1829   Dictionary* dictionary = loader_data->dictionary();
1830   InstanceKlass* sd_check = dictionary->find_class(current, name);
1831   if (sd_check == nullptr) {
1832     dictionary->add_klass(current, name, k);
1833   }
1834   mu1.notify_all();
1835 }
1836 

1837 // Indicate that loader_data has initiated the loading of class k, which
1838 // has already been defined by a parent loader.
1839 // This API is used by AOTLinkedClassBulkLoader and to register boxing
1840 // classes from java.lang in all class loaders to enable more value
1841 // classes optimizations
1842 void SystemDictionary::add_to_initiating_loader(JavaThread* current,
1843                                                 InstanceKlass* k,
1844                                                 ClassLoaderData* loader_data) {

1845   assert_locked_or_safepoint(SystemDictionary_lock);
1846   Symbol* name  = k->name();
1847   Dictionary* dictionary = loader_data->dictionary();
1848   assert(k->is_loaded(), "must be");
1849   assert(k->class_loader_data() != loader_data, "only for classes defined by a parent loader");
1850   if (dictionary->find_class(current, name) == nullptr) {
1851     dictionary->add_klass(current, name, k);
1852   }
1853 }

1854 
1855 // Try to find a class name using the loader constraints.  The
1856 // loader constraints might know about a class that isn't fully loaded
1857 // yet and these will be ignored.
1858 Klass* SystemDictionary::find_constrained_instance_or_array_klass(
1859                     Thread* current, Symbol* class_name, Handle class_loader) {
1860 
1861   // First see if it has been loaded directly.
1862   Klass* klass = find_instance_or_array_klass(current, class_name, class_loader);
1863   if (klass != nullptr)
1864     return klass;
1865 
1866   // Now look to see if it has been loaded elsewhere, and is subject to
1867   // a loader constraint that would require this loader to return the
1868   // klass that is already loaded.
1869   if (Signature::is_array(class_name)) {
1870     // For array classes, their Klass*s are not kept in the
1871     // constraint table. The element Klass*s are.
1872     SignatureStream ss(class_name, false);
1873     int ndims = ss.skip_array_prefix();  // skip all '['s
< prev index next >