< prev index next >

src/hotspot/share/classfile/systemDictionary.cpp

Print this page

  37 #include "classfile/loaderConstraints.hpp"
  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/instanceKlass.hpp"
  58 #include "oops/klass.inline.hpp"
  59 #include "oops/method.inline.hpp"
  60 #include "oops/objArrayKlass.hpp"
  61 #include "oops/objArrayOop.inline.hpp"
  62 #include "oops/oop.inline.hpp"
  63 #include "oops/oopHandle.inline.hpp"
  64 #include "oops/symbol.hpp"
  65 #include "oops/typeArrayKlass.hpp"
  66 #include "prims/jvmtiExport.hpp"
  67 #include "prims/methodHandles.hpp"
  68 #include "runtime/arguments.hpp"
  69 #include "runtime/atomic.hpp"
  70 #include "runtime/handles.inline.hpp"
  71 #include "runtime/java.hpp"
  72 #include "runtime/javaCalls.hpp"
  73 #include "runtime/mutexLocker.hpp"

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

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















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







 195   }
 196 }
 197 
 198 void SystemDictionary::set_system_loader(ClassLoaderData *cld) {
 199   assert(_java_system_loader.is_empty(), "already set!");
 200   _java_system_loader = cld->class_loader_handle();
 201 
 202 }
 203 
 204 void SystemDictionary::set_platform_loader(ClassLoaderData *cld) {
 205   assert(_java_platform_loader.is_empty(), "already set!");
 206   _java_platform_loader = cld->class_loader_handle();
 207 }
 208 
 209 // ----------------------------------------------------------------------------
 210 // Parallel class loading check
 211 
 212 static bool is_parallelCapable(Handle class_loader) {
 213   if (class_loader.is_null()) return true;
 214   return java_lang_ClassLoader::parallelCapable(class_loader());

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

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


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

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

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

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





































































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





















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

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

1633   }
1634 }
1635 
1636 // Update class loader data dictionary - done after check_constraint and add_to_hierarchy
1637 // have been called.
1638 void SystemDictionary::update_dictionary(JavaThread* current,
1639                                          InstanceKlass* k,
1640                                          ClassLoaderData* loader_data) {
1641   MonitorLocker mu1(SystemDictionary_lock);
1642 
1643   // Make a new dictionary entry.
1644   Symbol* name  = k->name();
1645   Dictionary* dictionary = loader_data->dictionary();
1646   InstanceKlass* sd_check = dictionary->find_class(current, name);
1647   if (sd_check == nullptr) {
1648     dictionary->add_klass(current, name, k);
1649   }
1650   mu1.notify_all();
1651 }
1652 
1653 #if INCLUDE_CDS
1654 // Indicate that loader_data has initiated the loading of class k, which
1655 // has already been defined by a parent loader.
1656 // This API should be used only by AOTLinkedClassBulkLoader


1657 void SystemDictionary::add_to_initiating_loader(JavaThread* current,
1658                                                 InstanceKlass* k,
1659                                                 ClassLoaderData* loader_data) {
1660   assert(CDSConfig::is_using_aot_linked_classes(), "must be");
1661   assert_locked_or_safepoint(SystemDictionary_lock);
1662   Symbol* name  = k->name();
1663   Dictionary* dictionary = loader_data->dictionary();
1664   assert(k->is_loaded(), "must be");
1665   assert(k->class_loader_data() != loader_data, "only for classes defined by a parent loader");
1666   assert(dictionary->find_class(current, name) == nullptr, "sanity");
1667   dictionary->add_klass(current, name, k);

1668 }
1669 #endif
1670 
1671 // Try to find a class name using the loader constraints.  The
1672 // loader constraints might know about a class that isn't fully loaded
1673 // yet and these will be ignored.
1674 Klass* SystemDictionary::find_constrained_instance_or_array_klass(
1675                     Thread* current, Symbol* class_name, Handle class_loader) {
1676 
1677   // First see if it has been loaded directly.
1678   Klass* klass = find_instance_or_array_klass(current, class_name, class_loader);
1679   if (klass != nullptr)
1680     return klass;
1681 
1682   // Now look to see if it has been loaded elsewhere, and is subject to
1683   // a loader constraint that would require this loader to return the
1684   // klass that is already loaded.
1685   if (Signature::is_array(class_name)) {
1686     // For array classes, their Klass*s are not kept in the
1687     // constraint table. The element Klass*s are.
1688     SignatureStream ss(class_name, false);
1689     int ndims = ss.skip_array_prefix();  // skip all '['s

  37 #include "classfile/loaderConstraints.hpp"
  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/fieldStreams.inline.hpp"
  58 #include "oops/inlineKlass.inline.hpp"
  59 #include "oops/instanceKlass.hpp"
  60 #include "oops/klass.inline.hpp"
  61 #include "oops/method.inline.hpp"
  62 #include "oops/objArrayKlass.hpp"
  63 #include "oops/objArrayOop.inline.hpp"
  64 #include "oops/oop.inline.hpp"
  65 #include "oops/oopHandle.inline.hpp"
  66 #include "oops/symbol.hpp"
  67 #include "oops/typeArrayKlass.hpp"
  68 #include "prims/jvmtiExport.hpp"
  69 #include "prims/methodHandles.hpp"
  70 #include "runtime/arguments.hpp"
  71 #include "runtime/atomic.hpp"
  72 #include "runtime/handles.inline.hpp"
  73 #include "runtime/java.hpp"
  74 #include "runtime/javaCalls.hpp"
  75 #include "runtime/mutexLocker.hpp"
  76 #include "runtime/os.hpp"
  77 #include "runtime/sharedRuntime.hpp"
  78 #include "runtime/signature.hpp"
  79 #include "runtime/synchronizer.hpp"
  80 #include "services/classLoadingService.hpp"
  81 #include "services/diagnosticCommand.hpp"
  82 #include "services/finalizerService.hpp"
  83 #include "services/threadService.hpp"
  84 #include "utilities/growableArray.hpp"
  85 #include "utilities/macros.hpp"
  86 #include "utilities/utf8.hpp"
  87 #if INCLUDE_CDS
  88 #include "classfile/systemDictionaryShared.hpp"
  89 #endif
  90 #if INCLUDE_JFR
  91 #include "jfr/jfr.hpp"
  92 #endif
  93 
  94 class InvokeMethodKey : public StackObj {
  95   private:
  96     Symbol* _symbol;

 171                          CHECK_NULL);
 172   return result.get_oop();
 173 }
 174 
 175 oop SystemDictionary::get_platform_class_loader_impl(TRAPS) {
 176   JavaValue result(T_OBJECT);
 177   InstanceKlass* class_loader_klass = vmClasses::ClassLoader_klass();
 178   JavaCalls::call_static(&result,
 179                          class_loader_klass,
 180                          vmSymbols::getPlatformClassLoader_name(),
 181                          vmSymbols::void_classloader_signature(),
 182                          CHECK_NULL);
 183   return result.get_oop();
 184 }
 185 
 186 // Helper function
 187 inline ClassLoaderData* class_loader_data(Handle class_loader) {
 188   return ClassLoaderData::class_loader_data(class_loader());
 189 }
 190 
 191 // These migrated value classes are loaded by the bootstrap class loader but are added to the initiating
 192 // loaders automatically so that fields of these types can be found and potentially flattened during
 193 // field layout.
 194 static void add_migrated_value_classes(ClassLoaderData* cld) {
 195   JavaThread* current = JavaThread::current();
 196   auto add_klass = [&] (Symbol* classname) {
 197     InstanceKlass* ik = SystemDictionary::find_instance_klass(current, classname, Handle(current, nullptr));
 198     assert(ik != nullptr, "Must exist");
 199     SystemDictionary::add_to_initiating_loader(current, ik, cld);
 200   };
 201 
 202   MonitorLocker mu1(SystemDictionary_lock);
 203   vmSymbols::migrated_class_names_do(add_klass);
 204 }
 205 
 206 ClassLoaderData* SystemDictionary::register_loader(Handle class_loader, bool create_mirror_cld) {
 207   if (create_mirror_cld) {
 208     // Add a new class loader data to the graph.
 209     return ClassLoaderDataGraph::add(class_loader, true);
 210   } else {
 211     if (class_loader() == nullptr) {
 212       return ClassLoaderData::the_null_class_loader_data();
 213     } else {
 214       ClassLoaderData* cld = ClassLoaderDataGraph::find_or_create(class_loader);
 215       if (EnableValhalla) {
 216         add_migrated_value_classes(cld);
 217       }
 218       return cld;
 219     }
 220   }
 221 }
 222 
 223 void SystemDictionary::set_system_loader(ClassLoaderData *cld) {
 224   assert(_java_system_loader.is_empty(), "already set!");
 225   _java_system_loader = cld->class_loader_handle();
 226 
 227 }
 228 
 229 void SystemDictionary::set_platform_loader(ClassLoaderData *cld) {
 230   assert(_java_platform_loader.is_empty(), "already set!");
 231   _java_platform_loader = cld->class_loader_handle();
 232 }
 233 
 234 // ----------------------------------------------------------------------------
 235 // Parallel class loading check
 236 
 237 static bool is_parallelCapable(Handle class_loader) {
 238   if (class_loader.is_null()) return true;
 239   return java_lang_ClassLoader::parallelCapable(class_loader());

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

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

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

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

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

1751   }
1752 }
1753 
1754 // Update class loader data dictionary - done after check_constraint and add_to_hierarchy
1755 // have been called.
1756 void SystemDictionary::update_dictionary(JavaThread* current,
1757                                          InstanceKlass* k,
1758                                          ClassLoaderData* loader_data) {
1759   MonitorLocker mu1(SystemDictionary_lock);
1760 
1761   // Make a new dictionary entry.
1762   Symbol* name  = k->name();
1763   Dictionary* dictionary = loader_data->dictionary();
1764   InstanceKlass* sd_check = dictionary->find_class(current, name);
1765   if (sd_check == nullptr) {
1766     dictionary->add_klass(current, name, k);
1767   }
1768   mu1.notify_all();
1769 }
1770 

1771 // Indicate that loader_data has initiated the loading of class k, which
1772 // has already been defined by a parent loader.
1773 // This API is used by AOTLinkedClassBulkLoader and to register boxing
1774 // classes from java.lang in all class loaders to enable more value
1775 // classes optimizations
1776 void SystemDictionary::add_to_initiating_loader(JavaThread* current,
1777                                                 InstanceKlass* k,
1778                                                 ClassLoaderData* loader_data) {

1779   assert_locked_or_safepoint(SystemDictionary_lock);
1780   Symbol* name  = k->name();
1781   Dictionary* dictionary = loader_data->dictionary();
1782   assert(k->is_loaded(), "must be");
1783   assert(k->class_loader_data() != loader_data, "only for classes defined by a parent loader");
1784   if (dictionary->find_class(current, name) == nullptr) {
1785     dictionary->add_klass(current, name, k);
1786   }
1787 }

1788 
1789 // Try to find a class name using the loader constraints.  The
1790 // loader constraints might know about a class that isn't fully loaded
1791 // yet and these will be ignored.
1792 Klass* SystemDictionary::find_constrained_instance_or_array_klass(
1793                     Thread* current, Symbol* class_name, Handle class_loader) {
1794 
1795   // First see if it has been loaded directly.
1796   Klass* klass = find_instance_or_array_klass(current, class_name, class_loader);
1797   if (klass != nullptr)
1798     return klass;
1799 
1800   // Now look to see if it has been loaded elsewhere, and is subject to
1801   // a loader constraint that would require this loader to return the
1802   // klass that is already loaded.
1803   if (Signature::is_array(class_name)) {
1804     // For array classes, their Klass*s are not kept in the
1805     // constraint table. The element Klass*s are.
1806     SignatureStream ss(class_name, false);
1807     int ndims = ss.skip_array_prefix();  // skip all '['s
< prev index next >