< prev index next >

src/hotspot/share/classfile/systemDictionary.cpp

Print this page

   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "cds/cdsConfig.hpp"

  27 #include "cds/heapShared.hpp"
  28 #include "classfile/classFileParser.hpp"
  29 #include "classfile/classFileStream.hpp"
  30 #include "classfile/classLoader.hpp"
  31 #include "classfile/classLoaderData.inline.hpp"
  32 #include "classfile/classLoaderDataGraph.inline.hpp"
  33 #include "classfile/classLoaderExt.hpp"
  34 #include "classfile/classLoadInfo.hpp"
  35 #include "classfile/dictionary.hpp"
  36 #include "classfile/javaClasses.inline.hpp"
  37 #include "classfile/klassFactory.hpp"
  38 #include "classfile/loaderConstraints.hpp"

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

  65 #include "oops/oop.hpp"
  66 #include "oops/oopHandle.hpp"
  67 #include "oops/oopHandle.inline.hpp"
  68 #include "oops/symbol.hpp"
  69 #include "oops/typeArrayKlass.hpp"
  70 #include "prims/jvmtiExport.hpp"
  71 #include "prims/methodHandles.hpp"
  72 #include "runtime/arguments.hpp"
  73 #include "runtime/atomic.hpp"
  74 #include "runtime/handles.inline.hpp"
  75 #include "runtime/java.hpp"
  76 #include "runtime/javaCalls.hpp"
  77 #include "runtime/mutexLocker.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/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;
  97     intptr_t _iid;
  98 
  99   public:
 100     InvokeMethodKey(Symbol* symbol, intptr_t iid) :
 101         _symbol(symbol),
 102         _iid(iid) {}
 103 
 104     static bool key_comparison(InvokeMethodKey const &k1, InvokeMethodKey const &k2){

 117 using InvokeMethodIntrinsicTable = ResourceHashtable<InvokeMethodKey, Method*, 139, AnyObj::C_HEAP, mtClass,
 118                   InvokeMethodKey::compute_hash, InvokeMethodKey::key_comparison>;
 119 static InvokeMethodIntrinsicTable* _invoke_method_intrinsic_table;
 120 using InvokeMethodTypeTable = ResourceHashtable<SymbolHandle, OopHandle, 139, AnyObj::C_HEAP, mtClass, SymbolHandle::compute_hash>;
 121 static InvokeMethodTypeTable* _invoke_method_type_table;
 122 
 123 OopHandle   SystemDictionary::_java_system_loader;
 124 OopHandle   SystemDictionary::_java_platform_loader;
 125 
 126 // ----------------------------------------------------------------------------
 127 // Java-level SystemLoader and PlatformLoader
 128 oop SystemDictionary::java_system_loader() {
 129   return _java_system_loader.resolve();
 130 }
 131 
 132 oop SystemDictionary::java_platform_loader() {
 133   return _java_platform_loader.resolve();
 134 }
 135 
 136 void SystemDictionary::compute_java_loaders(TRAPS) {
 137   if (_java_system_loader.is_empty()) {
 138     oop system_loader = get_system_class_loader_impl(CHECK);
 139     _java_system_loader = OopHandle(Universe::vm_global(), system_loader);

 140   } else {
 141     // It must have been restored from the archived module graph
 142     assert(UseSharedSpaces, "must be");
 143     assert(CDSConfig::is_using_full_module_graph(), "must be");
 144     DEBUG_ONLY(
 145       oop system_loader = get_system_class_loader_impl(CHECK);
 146       assert(_java_system_loader.resolve() == system_loader, "must be");
 147     )
 148  }
 149 
 150   if (_java_platform_loader.is_empty()) {
 151     oop platform_loader = get_platform_class_loader_impl(CHECK);
 152     _java_platform_loader = OopHandle(Universe::vm_global(), platform_loader);

 153   } else {
 154     // It must have been restored from the archived module graph
 155     assert(UseSharedSpaces, "must be");
 156     assert(CDSConfig::is_using_full_module_graph(), "must be");
 157     DEBUG_ONLY(
 158       oop platform_loader = get_platform_class_loader_impl(CHECK);
 159       assert(_java_platform_loader.resolve() == platform_loader, "must be");
 160     )
 161   }
 162 }
 163 
 164 oop SystemDictionary::get_system_class_loader_impl(TRAPS) {
 165   JavaValue result(T_OBJECT);
 166   InstanceKlass* class_loader_klass = vmClasses::ClassLoader_klass();
 167   JavaCalls::call_static(&result,
 168                          class_loader_klass,
 169                          vmSymbols::getSystemClassLoader_name(),
 170                          vmSymbols::void_classloader_signature(),
 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,

 992     assert(scp_entry->is_modules_image() || HeapShared::is_a_test_class_in_unnamed_module(ik),
 993            "only these classes can be loaded before the module system is initialized");
 994     assert(class_loader.is_null(), "sanity");
 995     return true;
 996   }
 997 
 998   if (pkg_entry == nullptr) {
 999     // We might have looked up pkg_entry before the module system was initialized.
1000     // Need to reload it now.
1001     TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);
1002     if (pkg_name != nullptr) {
1003       pkg_entry = class_loader_data(class_loader)->packages()->lookup_only(pkg_name);
1004     }
1005   }
1006 
1007   ModuleEntry* mod_entry = (pkg_entry == nullptr) ? nullptr : pkg_entry->module();
1008   bool should_be_in_named_module = (mod_entry != nullptr && mod_entry->is_named());
1009   bool was_archived_from_named_module = scp_entry->in_named_module();
1010   bool visible;
1011 
1012   if (was_archived_from_named_module) {




1013     if (should_be_in_named_module) {
1014       // Is the module loaded from the same location as during dump time?
1015       visible = mod_entry->shared_path_index() == scp_index;
1016       if (visible) {
1017         assert(!mod_entry->is_patched(), "cannot load archived classes for patched module");
1018       }
1019     } else {
1020       // During dump time, this class was in a named module, but at run time, this class should be
1021       // in an unnamed module.
1022       visible = false;
1023     }
1024   } else {
1025     if (should_be_in_named_module) {
1026       // During dump time, this class was in an unnamed, but at run time, this class should be
1027       // in a named module.
1028       visible = false;
1029     } else {
1030       visible = true;
1031     }
1032   }

1168   {
1169     HandleMark hm(THREAD);
1170     Handle lockObject = get_loader_lock_or_null(class_loader);
1171     ObjectLocker ol(lockObject, THREAD);
1172     // prohibited package check assumes all classes loaded from archive call
1173     // restore_unshareable_info which calls ik->set_package()
1174     ik->restore_unshareable_info(loader_data, protection_domain, pkg_entry, CHECK_NULL);
1175   }
1176 
1177   load_shared_class_misc(ik, loader_data);
1178   return ik;
1179 }
1180 
1181 void SystemDictionary::load_shared_class_misc(InstanceKlass* ik, ClassLoaderData* loader_data) {
1182   ik->print_class_load_logging(loader_data, nullptr, nullptr);
1183 
1184   // For boot loader, ensure that GetSystemPackage knows that a class in this
1185   // package was loaded.
1186   if (loader_data->is_the_null_class_loader_data()) {
1187     s2 path_index = ik->shared_classpath_index();
1188     ik->set_classpath_index(path_index);








1189   }
1190 
1191   // notify a class loaded from shared object
1192   ClassLoadingService::notify_class_loaded(ik, true /* shared class */);









1193 }
1194 
1195 #endif // INCLUDE_CDS
1196 
1197 InstanceKlass* SystemDictionary::load_instance_class_impl(Symbol* class_name, Handle class_loader, TRAPS) {
1198 
1199   if (class_loader.is_null()) {
1200     ResourceMark rm(THREAD);
1201     PackageEntry* pkg_entry = nullptr;
1202     bool search_only_bootloader_append = false;
1203 
1204     // Find the package in the boot loader's package entry table.
1205     TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);
1206     if (pkg_name != nullptr) {
1207       pkg_entry = class_loader_data(class_loader)->packages()->lookup_only(pkg_name);
1208     }
1209 
1210     // Prior to attempting to load the class, enforce the boot loader's
1211     // visibility boundaries.
1212     if (!Universe::is_module_initialized()) {

1247            // If there is no bootclasspath append entry, no need to continue
1248            // searching.
1249            return nullptr;
1250         }
1251         search_only_bootloader_append = true;
1252       }
1253     }
1254 
1255     // Prior to bootstrapping's module initialization, never load a class outside
1256     // of the boot loader's module path
1257     assert(Universe::is_module_initialized() ||
1258            !search_only_bootloader_append,
1259            "Attempt to load a class outside of boot loader's module path");
1260 
1261     // Search for classes in the CDS archive.
1262     InstanceKlass* k = nullptr;
1263 
1264 #if INCLUDE_CDS
1265     if (UseSharedSpaces)
1266     {
1267       PerfTraceTime vmtimer(ClassLoader::perf_shared_classload_time());
1268       InstanceKlass* ik = SystemDictionaryShared::find_builtin_class(class_name);
1269       if (ik != nullptr && ik->is_shared_boot_class() && !ik->shared_loading_failed()) {
1270         SharedClassLoadingMark slm(THREAD, ik);
1271         k = load_shared_class(ik, class_loader, Handle(), nullptr,  pkg_entry, CHECK_NULL);
1272       }
1273     }
1274 #endif
1275 
1276     if (k == nullptr) {
1277       // Use VM class loader
1278       PerfTraceTime vmtimer(ClassLoader::perf_sys_classload_time());
1279       k = ClassLoader::load_class(class_name, pkg_entry, search_only_bootloader_append, CHECK_NULL);
1280     }
1281 
1282     // find_or_define_instance_class may return a different InstanceKlass
1283     if (k != nullptr) {
1284       CDS_ONLY(SharedClassLoadingMark slm(THREAD, k);)
1285       k = find_or_define_instance_class(class_name, class_loader, k, CHECK_NULL);
1286     }
1287     return k;
1288   } else {
1289     // Use user specified class loader to load class. Call loadClass operation on class_loader.
1290     ResourceMark rm(THREAD);
1291 
1292     JavaThread* jt = THREAD;
1293 
1294     PerfClassTraceTime vmtimer(ClassLoader::perf_app_classload_time(),
1295                                ClassLoader::perf_app_classload_selftime(),
1296                                ClassLoader::perf_app_classload_count(),
1297                                jt->get_thread_stat()->perf_recursion_counts_addr(),
1298                                jt->get_thread_stat()->perf_timers_addr(),

1685   }
1686 }
1687 
1688 // Update class loader data dictionary - done after check_constraint and add_to_hierarchy
1689 // have been called.
1690 void SystemDictionary::update_dictionary(JavaThread* current,
1691                                          InstanceKlass* k,
1692                                          ClassLoaderData* loader_data) {
1693   MonitorLocker mu1(SystemDictionary_lock);
1694 
1695   // Make a new dictionary entry.
1696   Symbol* name  = k->name();
1697   Dictionary* dictionary = loader_data->dictionary();
1698   InstanceKlass* sd_check = dictionary->find_class(current, name);
1699   if (sd_check == nullptr) {
1700     dictionary->add_klass(current, name, k);
1701   }
1702   mu1.notify_all();
1703 }
1704 











1705 
1706 // Try to find a class name using the loader constraints.  The
1707 // loader constraints might know about a class that isn't fully loaded
1708 // yet and these will be ignored.
1709 Klass* SystemDictionary::find_constrained_instance_or_array_klass(
1710                     Thread* current, Symbol* class_name, Handle class_loader) {
1711 
1712   // First see if it has been loaded directly.
1713   // Force the protection domain to be null.  (This removes protection checks.)
1714   Handle no_protection_domain;
1715   Klass* klass = find_instance_or_array_klass(current, class_name, class_loader,
1716                                               no_protection_domain);
1717   if (klass != nullptr)
1718     return klass;
1719 
1720   // Now look to see if it has been loaded elsewhere, and is subject to
1721   // a loader constraint that would require this loader to return the
1722   // klass that is already loaded.
1723   if (Signature::is_array(class_name)) {
1724     // For array classes, their Klass*s are not kept in the

2000       ml.notify_all();
2001     } else {
2002       signature->make_permanent(); // The signature is never unloaded.
2003       assert(Arguments::is_interpreter_only() || (m->has_compiled_code() &&
2004              m->code()->entry_point() == m->from_compiled_entry()),
2005              "MH intrinsic invariant");
2006       *met = m(); // insert the element
2007       ml.notify_all();
2008       return m();
2009     }
2010   }
2011 
2012   // Throw OOM or the pending exception in the JavaThread
2013   if (throw_error && !HAS_PENDING_EXCEPTION) {
2014     THROW_MSG_NULL(vmSymbols::java_lang_OutOfMemoryError(),
2015                    "Out of space in CodeCache for method handle intrinsic");
2016   }
2017   return nullptr;
2018 }
2019 








































2020 // Helper for unpacking the return value from linkMethod and linkCallSite.
2021 static Method* unpack_method_and_appendix(Handle mname,
2022                                           Klass* accessing_klass,
2023                                           objArrayHandle appendix_box,
2024                                           Handle* appendix_result,
2025                                           TRAPS) {
2026   if (mname.not_null()) {
2027     Method* m = java_lang_invoke_MemberName::vmtarget(mname());
2028     if (m != nullptr) {
2029       oop appendix = appendix_box->obj_at(0);
2030       LogTarget(Info, methodhandles) lt;
2031       if (lt.develop_is_enabled()) {
2032         ResourceMark rm(THREAD);
2033         LogStream ls(lt);
2034         ls.print("Linked method=" INTPTR_FORMAT ": ", p2i(m));
2035         m->print_on(&ls);
2036         if (appendix != nullptr) { ls.print("appendix = "); appendix->print_on(&ls); }
2037         ls.cr();
2038       }
2039 

   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "cds/cdsConfig.hpp"
  27 #include "cds/classPreloader.hpp"
  28 #include "cds/heapShared.hpp"
  29 #include "classfile/classFileParser.hpp"
  30 #include "classfile/classFileStream.hpp"
  31 #include "classfile/classLoader.hpp"
  32 #include "classfile/classLoaderData.inline.hpp"
  33 #include "classfile/classLoaderDataGraph.inline.hpp"
  34 #include "classfile/classLoaderExt.hpp"
  35 #include "classfile/classLoadInfo.hpp"
  36 #include "classfile/dictionary.hpp"
  37 #include "classfile/javaClasses.inline.hpp"
  38 #include "classfile/klassFactory.hpp"
  39 #include "classfile/loaderConstraints.hpp"
  40 #include "classfile/modules.hpp"
  41 #include "classfile/packageEntry.hpp"
  42 #include "classfile/placeholders.hpp"
  43 #include "classfile/protectionDomainCache.hpp"
  44 #include "classfile/resolutionErrors.hpp"
  45 #include "classfile/stringTable.hpp"
  46 #include "classfile/symbolTable.hpp"
  47 #include "classfile/systemDictionary.hpp"
  48 #include "classfile/vmClasses.hpp"
  49 #include "classfile/vmSymbols.hpp"
  50 #include "gc/shared/gcTraceTime.inline.hpp"
  51 #include "interpreter/bootstrapInfo.hpp"
  52 #include "jfr/jfrEvents.hpp"
  53 #include "jvm.h"
  54 #include "logging/log.hpp"
  55 #include "logging/logStream.hpp"
  56 #include "memory/metaspaceClosure.hpp"
  57 #include "memory/oopFactory.hpp"
  58 #include "memory/resourceArea.hpp"
  59 #include "memory/universe.hpp"
  60 #include "oops/access.inline.hpp"

  67 #include "oops/oop.hpp"
  68 #include "oops/oopHandle.hpp"
  69 #include "oops/oopHandle.inline.hpp"
  70 #include "oops/symbol.hpp"
  71 #include "oops/typeArrayKlass.hpp"
  72 #include "prims/jvmtiExport.hpp"
  73 #include "prims/methodHandles.hpp"
  74 #include "runtime/arguments.hpp"
  75 #include "runtime/atomic.hpp"
  76 #include "runtime/handles.inline.hpp"
  77 #include "runtime/java.hpp"
  78 #include "runtime/javaCalls.hpp"
  79 #include "runtime/mutexLocker.hpp"
  80 #include "runtime/sharedRuntime.hpp"
  81 #include "runtime/signature.hpp"
  82 #include "runtime/synchronizer.hpp"
  83 #include "services/classLoadingService.hpp"
  84 #include "services/diagnosticCommand.hpp"
  85 #include "services/finalizerService.hpp"
  86 #include "services/threadService.hpp"
  87 #include "utilities/growableArray.hpp"
  88 #include "utilities/macros.hpp"
  89 #include "utilities/utf8.hpp"
  90 #if INCLUDE_CDS
  91 #include "classfile/systemDictionaryShared.hpp"
  92 #endif
  93 #if INCLUDE_JFR
  94 #include "jfr/jfr.hpp"
  95 #endif
  96 
  97 class InvokeMethodKey : public StackObj {
  98   private:
  99     Symbol* _symbol;
 100     intptr_t _iid;
 101 
 102   public:
 103     InvokeMethodKey(Symbol* symbol, intptr_t iid) :
 104         _symbol(symbol),
 105         _iid(iid) {}
 106 
 107     static bool key_comparison(InvokeMethodKey const &k1, InvokeMethodKey const &k2){

 120 using InvokeMethodIntrinsicTable = ResourceHashtable<InvokeMethodKey, Method*, 139, AnyObj::C_HEAP, mtClass,
 121                   InvokeMethodKey::compute_hash, InvokeMethodKey::key_comparison>;
 122 static InvokeMethodIntrinsicTable* _invoke_method_intrinsic_table;
 123 using InvokeMethodTypeTable = ResourceHashtable<SymbolHandle, OopHandle, 139, AnyObj::C_HEAP, mtClass, SymbolHandle::compute_hash>;
 124 static InvokeMethodTypeTable* _invoke_method_type_table;
 125 
 126 OopHandle   SystemDictionary::_java_system_loader;
 127 OopHandle   SystemDictionary::_java_platform_loader;
 128 
 129 // ----------------------------------------------------------------------------
 130 // Java-level SystemLoader and PlatformLoader
 131 oop SystemDictionary::java_system_loader() {
 132   return _java_system_loader.resolve();
 133 }
 134 
 135 oop SystemDictionary::java_platform_loader() {
 136   return _java_platform_loader.resolve();
 137 }
 138 
 139 void SystemDictionary::compute_java_loaders(TRAPS) {
 140   if (_java_platform_loader.is_empty()) {
 141     oop platform_loader = get_platform_class_loader_impl(CHECK);
 142     _java_platform_loader = OopHandle(Universe::vm_global(), platform_loader);
 143     ClassPreloader::runtime_preload(THREAD, Handle(THREAD, java_platform_loader()));
 144   } else {
 145     // It must have been restored from the archived module graph
 146     assert(UseSharedSpaces, "must be");
 147     assert(CDSConfig::is_using_full_module_graph(), "must be");
 148     DEBUG_ONLY(
 149       oop platform_loader = get_platform_class_loader_impl(CHECK);
 150       assert(_java_platform_loader.resolve() == platform_loader, "must be");
 151     )
 152   }
 153 
 154   if (_java_system_loader.is_empty()) {
 155     oop system_loader = get_system_class_loader_impl(CHECK);
 156     _java_system_loader = OopHandle(Universe::vm_global(), system_loader);
 157     ClassPreloader::runtime_preload(THREAD, Handle(THREAD, java_system_loader()));
 158   } else {
 159     // It must have been restored from the archived module graph
 160     assert(UseSharedSpaces, "must be");
 161     assert(CDSConfig::is_using_full_module_graph(), "must be");
 162     DEBUG_ONLY(
 163       oop system_loader = get_system_class_loader_impl(CHECK);
 164       assert(_java_system_loader.resolve() == system_loader, "must be");
 165     )
 166   }
 167 }
 168 
 169 oop SystemDictionary::get_system_class_loader_impl(TRAPS) {
 170   JavaValue result(T_OBJECT);
 171   InstanceKlass* class_loader_klass = vmClasses::ClassLoader_klass();
 172   JavaCalls::call_static(&result,
 173                          class_loader_klass,
 174                          vmSymbols::getSystemClassLoader_name(),
 175                          vmSymbols::void_classloader_signature(),
 176                          CHECK_NULL);
 177   return result.get_oop();
 178 }
 179 
 180 oop SystemDictionary::get_platform_class_loader_impl(TRAPS) {
 181   JavaValue result(T_OBJECT);
 182   InstanceKlass* class_loader_klass = vmClasses::ClassLoader_klass();
 183   JavaCalls::call_static(&result,
 184                          class_loader_klass,

 997     assert(scp_entry->is_modules_image() || HeapShared::is_a_test_class_in_unnamed_module(ik),
 998            "only these classes can be loaded before the module system is initialized");
 999     assert(class_loader.is_null(), "sanity");
1000     return true;
1001   }
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 = scp_entry->in_named_module();
1015   bool visible;
1016 
1017   if (mod_entry != nullptr && mod_entry->location() == nullptr && mod_entry->is_named()) {
1018     // Archived module for dynamic proxies. It's always visible.
1019     assert(Modules::is_dynamic_proxy_module(mod_entry), "must be");
1020     visible = true;
1021   } else if (was_archived_from_named_module) {
1022     if (should_be_in_named_module) {
1023       // Is the module loaded from the same location as during dump time?
1024       visible = mod_entry->shared_path_index() == scp_index;
1025       if (visible) {
1026         assert(!mod_entry->is_patched(), "cannot load archived classes for patched module");
1027       }
1028     } else {
1029       // During dump time, this class was in a named module, but at run time, this class should be
1030       // in an unnamed module.
1031       visible = false;
1032     }
1033   } else {
1034     if (should_be_in_named_module) {
1035       // During dump time, this class was in an unnamed, but at run time, this class should be
1036       // in a named module.
1037       visible = false;
1038     } else {
1039       visible = true;
1040     }
1041   }

1177   {
1178     HandleMark hm(THREAD);
1179     Handle lockObject = get_loader_lock_or_null(class_loader);
1180     ObjectLocker ol(lockObject, THREAD);
1181     // prohibited package check assumes all classes loaded from archive call
1182     // restore_unshareable_info which calls ik->set_package()
1183     ik->restore_unshareable_info(loader_data, protection_domain, pkg_entry, CHECK_NULL);
1184   }
1185 
1186   load_shared_class_misc(ik, loader_data);
1187   return ik;
1188 }
1189 
1190 void SystemDictionary::load_shared_class_misc(InstanceKlass* ik, ClassLoaderData* loader_data) {
1191   ik->print_class_load_logging(loader_data, nullptr, nullptr);
1192 
1193   // For boot loader, ensure that GetSystemPackage knows that a class in this
1194   // package was loaded.
1195   if (loader_data->is_the_null_class_loader_data()) {
1196     s2 path_index = ik->shared_classpath_index();
1197     if (path_index >= 0) { // FIXME ... for lambda form classes
1198       ik->set_classpath_index(path_index);
1199 
1200       if (CDSConfig::is_dumping_final_static_archive()) {
1201         if (path_index > ClassLoaderExt::max_used_path_index()) {
1202           ClassLoaderExt::set_max_used_path_index(path_index);
1203         }
1204       }
1205     }
1206   }
1207 
1208   // notify a class loaded from shared object
1209   ClassLoadingService::notify_class_loaded(ik, true /* shared class */);
1210 
1211   if (CDSConfig::is_dumping_final_static_archive()) {
1212     SystemDictionaryShared::init_dumptime_info(ik);
1213     if (SystemDictionary::is_platform_class_loader(loader_data->class_loader())) {
1214       ClassLoaderExt::set_has_platform_classes();
1215     } else if (SystemDictionary::is_system_class_loader(loader_data->class_loader())) {
1216       ClassLoaderExt::set_has_app_classes();
1217     }
1218   }
1219 }
1220 
1221 #endif // INCLUDE_CDS
1222 
1223 InstanceKlass* SystemDictionary::load_instance_class_impl(Symbol* class_name, Handle class_loader, TRAPS) {
1224 
1225   if (class_loader.is_null()) {
1226     ResourceMark rm(THREAD);
1227     PackageEntry* pkg_entry = nullptr;
1228     bool search_only_bootloader_append = false;
1229 
1230     // Find the package in the boot loader's package entry table.
1231     TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);
1232     if (pkg_name != nullptr) {
1233       pkg_entry = class_loader_data(class_loader)->packages()->lookup_only(pkg_name);
1234     }
1235 
1236     // Prior to attempting to load the class, enforce the boot loader's
1237     // visibility boundaries.
1238     if (!Universe::is_module_initialized()) {

1273            // If there is no bootclasspath append entry, no need to continue
1274            // searching.
1275            return nullptr;
1276         }
1277         search_only_bootloader_append = true;
1278       }
1279     }
1280 
1281     // Prior to bootstrapping's module initialization, never load a class outside
1282     // of the boot loader's module path
1283     assert(Universe::is_module_initialized() ||
1284            !search_only_bootloader_append,
1285            "Attempt to load a class outside of boot loader's module path");
1286 
1287     // Search for classes in the CDS archive.
1288     InstanceKlass* k = nullptr;
1289 
1290 #if INCLUDE_CDS
1291     if (UseSharedSpaces)
1292     {
1293       PerfTraceElapsedTime vmtimer(ClassLoader::perf_shared_classload_time());
1294       InstanceKlass* ik = SystemDictionaryShared::find_builtin_class(class_name);
1295       if (ik != nullptr && ik->is_shared_boot_class() && !ik->shared_loading_failed()) {
1296         SharedClassLoadingMark slm(THREAD, ik);
1297         k = load_shared_class(ik, class_loader, Handle(), nullptr,  pkg_entry, CHECK_NULL);
1298       }
1299     }
1300 #endif
1301 
1302     if (k == nullptr) {
1303       // Use VM class loader
1304       PerfTraceElapsedTime vmtimer(ClassLoader::perf_sys_classload_time());
1305       k = ClassLoader::load_class(class_name, pkg_entry, search_only_bootloader_append, CHECK_NULL);
1306     }
1307 
1308     // find_or_define_instance_class may return a different InstanceKlass
1309     if (k != nullptr) {
1310       CDS_ONLY(SharedClassLoadingMark slm(THREAD, k);)
1311       k = find_or_define_instance_class(class_name, class_loader, k, CHECK_NULL);
1312     }
1313     return k;
1314   } else {
1315     // Use user specified class loader to load class. Call loadClass operation on class_loader.
1316     ResourceMark rm(THREAD);
1317 
1318     JavaThread* jt = THREAD;
1319 
1320     PerfClassTraceTime vmtimer(ClassLoader::perf_app_classload_time(),
1321                                ClassLoader::perf_app_classload_selftime(),
1322                                ClassLoader::perf_app_classload_count(),
1323                                jt->get_thread_stat()->perf_recursion_counts_addr(),
1324                                jt->get_thread_stat()->perf_timers_addr(),

1711   }
1712 }
1713 
1714 // Update class loader data dictionary - done after check_constraint and add_to_hierarchy
1715 // have been called.
1716 void SystemDictionary::update_dictionary(JavaThread* current,
1717                                          InstanceKlass* k,
1718                                          ClassLoaderData* loader_data) {
1719   MonitorLocker mu1(SystemDictionary_lock);
1720 
1721   // Make a new dictionary entry.
1722   Symbol* name  = k->name();
1723   Dictionary* dictionary = loader_data->dictionary();
1724   InstanceKlass* sd_check = dictionary->find_class(current, name);
1725   if (sd_check == nullptr) {
1726     dictionary->add_klass(current, name, k);
1727   }
1728   mu1.notify_all();
1729 }
1730 
1731 #if INCLUDE_CDS
1732 void SystemDictionary::preload_class(JavaThread* current,
1733                                      InstanceKlass* k,
1734                                      ClassLoaderData* loader_data) {
1735   assert_locked_or_safepoint(SystemDictionary_lock);
1736   Symbol* name  = k->name();
1737   Dictionary* dictionary = loader_data->dictionary();
1738   assert(dictionary->find_class(current, name) == nullptr, "sanity");
1739   dictionary->add_klass(current, name, k);
1740 }
1741 #endif
1742 
1743 // Try to find a class name using the loader constraints.  The
1744 // loader constraints might know about a class that isn't fully loaded
1745 // yet and these will be ignored.
1746 Klass* SystemDictionary::find_constrained_instance_or_array_klass(
1747                     Thread* current, Symbol* class_name, Handle class_loader) {
1748 
1749   // First see if it has been loaded directly.
1750   // Force the protection domain to be null.  (This removes protection checks.)
1751   Handle no_protection_domain;
1752   Klass* klass = find_instance_or_array_klass(current, class_name, class_loader,
1753                                               no_protection_domain);
1754   if (klass != nullptr)
1755     return klass;
1756 
1757   // Now look to see if it has been loaded elsewhere, and is subject to
1758   // a loader constraint that would require this loader to return the
1759   // klass that is already loaded.
1760   if (Signature::is_array(class_name)) {
1761     // For array classes, their Klass*s are not kept in the

2037       ml.notify_all();
2038     } else {
2039       signature->make_permanent(); // The signature is never unloaded.
2040       assert(Arguments::is_interpreter_only() || (m->has_compiled_code() &&
2041              m->code()->entry_point() == m->from_compiled_entry()),
2042              "MH intrinsic invariant");
2043       *met = m(); // insert the element
2044       ml.notify_all();
2045       return m();
2046     }
2047   }
2048 
2049   // Throw OOM or the pending exception in the JavaThread
2050   if (throw_error && !HAS_PENDING_EXCEPTION) {
2051     THROW_MSG_NULL(vmSymbols::java_lang_OutOfMemoryError(),
2052                    "Out of space in CodeCache for method handle intrinsic");
2053   }
2054   return nullptr;
2055 }
2056 
2057 #if INCLUDE_CDS
2058 void SystemDictionary::get_all_method_handle_intrinsics(GrowableArray<Method*>* methods) {
2059   auto do_method = [&] (InvokeMethodKey& key, Method*& m) {
2060     methods->append(m);
2061   };
2062   _invoke_method_intrinsic_table->iterate_all(do_method);
2063 }
2064 
2065 void SystemDictionary::restore_archived_method_handle_intrinsics() {
2066   if (UseSharedSpaces) {
2067     EXCEPTION_MARK;
2068     restore_archived_method_handle_intrinsics_impl(THREAD);
2069     if (HAS_PENDING_EXCEPTION) {
2070       vm_exit_during_initialization(err_msg("Failed to restore archived method handle intrinsics"));
2071     }
2072   }
2073 }
2074 
2075 void SystemDictionary::restore_archived_method_handle_intrinsics_impl(TRAPS) {
2076   Array<Method*>* list = MetaspaceShared::archived_method_handle_intrinsics();
2077   for (int i = 0; i < list->length(); i++) {
2078     methodHandle m(THREAD, list->at(i));
2079     Method::restore_archived_method_handle_intrinsic(m, CHECK);
2080     m->constants()->restore_unshareable_info(CHECK);
2081     if (!Arguments::is_interpreter_only() || m->intrinsic_id() == vmIntrinsics::_linkToNative) {
2082       AdapterHandlerLibrary::create_native_wrapper(m);
2083       if (!m->has_compiled_code()) {
2084         ResourceMark rm(THREAD);
2085         vm_exit_during_initialization(err_msg("Failed to initialize method %s", m->external_name()));
2086       }
2087     }
2088 
2089     const int iid_as_int = vmIntrinsics::as_int(m->intrinsic_id());
2090     InvokeMethodKey key(m->signature(), iid_as_int);
2091     bool created = _invoke_method_intrinsic_table->put(key, m());
2092     assert(created, "must be");
2093   }
2094 }
2095 #endif
2096 
2097 // Helper for unpacking the return value from linkMethod and linkCallSite.
2098 static Method* unpack_method_and_appendix(Handle mname,
2099                                           Klass* accessing_klass,
2100                                           objArrayHandle appendix_box,
2101                                           Handle* appendix_result,
2102                                           TRAPS) {
2103   if (mname.not_null()) {
2104     Method* m = java_lang_invoke_MemberName::vmtarget(mname());
2105     if (m != nullptr) {
2106       oop appendix = appendix_box->obj_at(0);
2107       LogTarget(Info, methodhandles) lt;
2108       if (lt.develop_is_enabled()) {
2109         ResourceMark rm(THREAD);
2110         LogStream ls(lt);
2111         ls.print("Linked method=" INTPTR_FORMAT ": ", p2i(m));
2112         m->print_on(&ls);
2113         if (appendix != nullptr) { ls.print("appendix = "); appendix->print_on(&ls); }
2114         ls.cr();
2115       }
2116 
< prev index next >