< prev index next >

src/hotspot/share/classfile/systemDictionary.cpp

Print this page

   1 /*
   2  * Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *

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

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

   1 /*
   2  * Copyright (c) 1997, 2026, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *

  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 
 954   assert(!ModuleEntryTable::javabase_moduleEntry()->is_patched(),
 955          "Cannot use sharing if java.base is patched");
 956 
 957   // (1) Check if we are loading into the same loader as in dump time.
 958 
 959   if (ik->defined_by_boot_loader()) {
 960     if (class_loader() != nullptr) {
 961       return false;
 962     }
 963   } else if (ik->defined_by_platform_loader()) {
 964     if (class_loader() != java_platform_loader()) {
 965       return false;
 966     }
 967   } else if (ik->defined_by_app_loader()) {
 968     if (class_loader() != java_system_loader()) {
 969       return false;
 970     }
 971   } else {
 972     // ik was loaded by a custom loader during dump time
 973     if (class_loader_data(class_loader)->is_builtin_class_loader_data()) {

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

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

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

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

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