6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "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(CDSConfig::is_using_archive(), "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(CDSConfig::is_using_archive(), "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,
1008 assert(scp_entry->is_modules_image() || HeapShared::is_a_test_class_in_unnamed_module(ik),
1009 "only these classes can be loaded before the module system is initialized");
1010 assert(class_loader.is_null(), "sanity");
1011 return true;
1012 }
1013
1014 if (pkg_entry == nullptr) {
1015 // We might have looked up pkg_entry before the module system was initialized.
1016 // Need to reload it now.
1017 TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);
1018 if (pkg_name != nullptr) {
1019 pkg_entry = class_loader_data(class_loader)->packages()->lookup_only(pkg_name);
1020 }
1021 }
1022
1023 ModuleEntry* mod_entry = (pkg_entry == nullptr) ? nullptr : pkg_entry->module();
1024 bool should_be_in_named_module = (mod_entry != nullptr && mod_entry->is_named());
1025 bool was_archived_from_named_module = scp_entry->in_named_module();
1026 bool visible;
1027
1028 if (was_archived_from_named_module) {
1029 if (should_be_in_named_module) {
1030 // Is the module loaded from the same location as during dump time?
1031 visible = mod_entry->shared_path_index() == scp_index;
1032 if (visible) {
1033 assert(!mod_entry->is_patched(), "cannot load archived classes for patched module");
1034 }
1035 } else {
1036 // During dump time, this class was in a named module, but at run time, this class should be
1037 // in an unnamed module.
1038 visible = false;
1039 }
1040 } else {
1041 if (should_be_in_named_module) {
1042 // During dump time, this class was in an unnamed, but at run time, this class should be
1043 // in a named module.
1044 visible = false;
1045 } else {
1046 visible = true;
1047 }
1048 }
1192 {
1193 HandleMark hm(THREAD);
1194 Handle lockObject = get_loader_lock_or_null(class_loader);
1195 ObjectLocker ol(lockObject, THREAD);
1196 // prohibited package check assumes all classes loaded from archive call
1197 // restore_unshareable_info which calls ik->set_package()
1198 ik->restore_unshareable_info(loader_data, protection_domain, pkg_entry, CHECK_NULL);
1199 }
1200
1201 load_shared_class_misc(ik, loader_data);
1202 return ik;
1203 }
1204
1205 void SystemDictionary::load_shared_class_misc(InstanceKlass* ik, ClassLoaderData* loader_data) {
1206 ik->print_class_load_logging(loader_data, nullptr, nullptr);
1207
1208 // For boot loader, ensure that GetSystemPackage knows that a class in this
1209 // package was loaded.
1210 if (loader_data->is_the_null_class_loader_data()) {
1211 s2 path_index = ik->shared_classpath_index();
1212 ik->set_classpath_index(path_index);
1213 }
1214
1215 // notify a class loaded from shared object
1216 ClassLoadingService::notify_class_loaded(ik, true /* shared class */);
1217 }
1218
1219 #endif // INCLUDE_CDS
1220
1221 InstanceKlass* SystemDictionary::load_instance_class_impl(Symbol* class_name, Handle class_loader, TRAPS) {
1222
1223 if (class_loader.is_null()) {
1224 ResourceMark rm(THREAD);
1225 PackageEntry* pkg_entry = nullptr;
1226 bool search_only_bootloader_append = false;
1227
1228 // Find the package in the boot loader's package entry table.
1229 TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);
1230 if (pkg_name != nullptr) {
1231 pkg_entry = class_loader_data(class_loader)->packages()->lookup_only(pkg_name);
1232 }
1233
1234 // Prior to attempting to load the class, enforce the boot loader's
1235 // visibility boundaries.
1236 if (!Universe::is_module_initialized()) {
1271 // If there is no bootclasspath append entry, no need to continue
1272 // searching.
1273 return nullptr;
1274 }
1275 search_only_bootloader_append = true;
1276 }
1277 }
1278
1279 // Prior to bootstrapping's module initialization, never load a class outside
1280 // of the boot loader's module path
1281 assert(Universe::is_module_initialized() ||
1282 !search_only_bootloader_append,
1283 "Attempt to load a class outside of boot loader's module path");
1284
1285 // Search for classes in the CDS archive.
1286 InstanceKlass* k = nullptr;
1287
1288 #if INCLUDE_CDS
1289 if (CDSConfig::is_using_archive())
1290 {
1291 PerfTraceTime vmtimer(ClassLoader::perf_shared_classload_time());
1292 InstanceKlass* ik = SystemDictionaryShared::find_builtin_class(class_name);
1293 if (ik != nullptr && ik->is_shared_boot_class() && !ik->shared_loading_failed()) {
1294 SharedClassLoadingMark slm(THREAD, ik);
1295 k = load_shared_class(ik, class_loader, Handle(), nullptr, pkg_entry, CHECK_NULL);
1296 }
1297 }
1298 #endif
1299
1300 if (k == nullptr) {
1301 // Use VM class loader
1302 PerfTraceTime vmtimer(ClassLoader::perf_sys_classload_time());
1303 k = ClassLoader::load_class(class_name, pkg_entry, search_only_bootloader_append, CHECK_NULL);
1304 }
1305
1306 // find_or_define_instance_class may return a different InstanceKlass
1307 if (k != nullptr) {
1308 CDS_ONLY(SharedClassLoadingMark slm(THREAD, k);)
1309 k = find_or_define_instance_class(class_name, class_loader, k, CHECK_NULL);
1310 }
1311 return k;
1312 } else {
1313 // Use user specified class loader to load class. Call loadClass operation on class_loader.
1314 ResourceMark rm(THREAD);
1315
1316 JavaThread* jt = THREAD;
1317
1318 PerfClassTraceTime vmtimer(ClassLoader::perf_app_classload_time(),
1319 ClassLoader::perf_app_classload_selftime(),
1320 ClassLoader::perf_app_classload_count(),
1321 jt->get_thread_stat()->perf_recursion_counts_addr(),
1322 jt->get_thread_stat()->perf_timers_addr(),
1709 }
1710 }
1711
1712 // Update class loader data dictionary - done after check_constraint and add_to_hierarchy
1713 // have been called.
1714 void SystemDictionary::update_dictionary(JavaThread* current,
1715 InstanceKlass* k,
1716 ClassLoaderData* loader_data) {
1717 MonitorLocker mu1(SystemDictionary_lock);
1718
1719 // Make a new dictionary entry.
1720 Symbol* name = k->name();
1721 Dictionary* dictionary = loader_data->dictionary();
1722 InstanceKlass* sd_check = dictionary->find_class(current, name);
1723 if (sd_check == nullptr) {
1724 dictionary->add_klass(current, name, k);
1725 }
1726 mu1.notify_all();
1727 }
1728
1729
1730 // Try to find a class name using the loader constraints. The
1731 // loader constraints might know about a class that isn't fully loaded
1732 // yet and these will be ignored.
1733 Klass* SystemDictionary::find_constrained_instance_or_array_klass(
1734 Thread* current, Symbol* class_name, Handle class_loader) {
1735
1736 // First see if it has been loaded directly.
1737 // Force the protection domain to be null. (This removes protection checks.)
1738 Handle no_protection_domain;
1739 Klass* klass = find_instance_or_array_klass(current, class_name, class_loader,
1740 no_protection_domain);
1741 if (klass != nullptr)
1742 return klass;
1743
1744 // Now look to see if it has been loaded elsewhere, and is subject to
1745 // a loader constraint that would require this loader to return the
1746 // klass that is already loaded.
1747 if (Signature::is_array(class_name)) {
1748 // For array classes, their Klass*s are not kept in the
2024 ml.notify_all();
2025 } else {
2026 signature->make_permanent(); // The signature is never unloaded.
2027 assert(Arguments::is_interpreter_only() || (m->has_compiled_code() &&
2028 m->code()->entry_point() == m->from_compiled_entry()),
2029 "MH intrinsic invariant");
2030 *met = m(); // insert the element
2031 ml.notify_all();
2032 return m();
2033 }
2034 }
2035
2036 // Throw OOM or the pending exception in the JavaThread
2037 if (throw_error && !HAS_PENDING_EXCEPTION) {
2038 THROW_MSG_NULL(vmSymbols::java_lang_OutOfMemoryError(),
2039 "Out of space in CodeCache for method handle intrinsic");
2040 }
2041 return nullptr;
2042 }
2043
2044 // Helper for unpacking the return value from linkMethod and linkCallSite.
2045 static Method* unpack_method_and_appendix(Handle mname,
2046 Klass* accessing_klass,
2047 objArrayHandle appendix_box,
2048 Handle* appendix_result,
2049 TRAPS) {
2050 if (mname.not_null()) {
2051 Method* m = java_lang_invoke_MemberName::vmtarget(mname());
2052 if (m != nullptr) {
2053 oop appendix = appendix_box->obj_at(0);
2054 LogTarget(Info, methodhandles) lt;
2055 if (lt.develop_is_enabled()) {
2056 ResourceMark rm(THREAD);
2057 LogStream ls(lt);
2058 ls.print("Linked method=" INTPTR_FORMAT ": ", p2i(m));
2059 m->print_on(&ls);
2060 if (appendix != nullptr) { ls.print("appendix = "); appendix->print_on(&ls); }
2061 ls.cr();
2062 }
2063
|
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "cds/aotLinkedClassBulkLoader.hpp"
27 #include "cds/cdsConfig.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 if (CDSConfig::is_dumping_final_static_archive()) {
144 AOTLinkedClassBulkLoader::load_platform_classes(THREAD);
145 }
146 } else {
147 // It must have been restored from the archived module graph
148 assert(CDSConfig::is_using_archive(), "must be");
149 assert(CDSConfig::is_using_full_module_graph(), "must be");
150 DEBUG_ONLY(
151 oop platform_loader = get_platform_class_loader_impl(CHECK);
152 assert(_java_platform_loader.resolve() == platform_loader, "must be");
153 )
154 }
155
156 if (_java_system_loader.is_empty()) {
157 oop system_loader = get_system_class_loader_impl(CHECK);
158 _java_system_loader = OopHandle(Universe::vm_global(), system_loader);
159 if (CDSConfig::is_dumping_final_static_archive()) {
160 AOTLinkedClassBulkLoader::load_app_classes(THREAD);
161 }
162 } else {
163 // It must have been restored from the archived module graph
164 assert(CDSConfig::is_using_archive(), "must be");
165 assert(CDSConfig::is_using_full_module_graph(), "must be");
166 DEBUG_ONLY(
167 oop system_loader = get_system_class_loader_impl(CHECK);
168 assert(_java_system_loader.resolve() == system_loader, "must be");
169 )
170 }
171 }
172
173 oop SystemDictionary::get_system_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::getSystemClassLoader_name(),
179 vmSymbols::void_classloader_signature(),
180 CHECK_NULL);
181 return result.get_oop();
182 }
183
184 oop SystemDictionary::get_platform_class_loader_impl(TRAPS) {
185 JavaValue result(T_OBJECT);
186 InstanceKlass* class_loader_klass = vmClasses::ClassLoader_klass();
187 JavaCalls::call_static(&result,
188 class_loader_klass,
1017 assert(scp_entry->is_modules_image() || HeapShared::is_a_test_class_in_unnamed_module(ik),
1018 "only these classes can be loaded before the module system is initialized");
1019 assert(class_loader.is_null(), "sanity");
1020 return true;
1021 }
1022
1023 if (pkg_entry == nullptr) {
1024 // We might have looked up pkg_entry before the module system was initialized.
1025 // Need to reload it now.
1026 TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);
1027 if (pkg_name != nullptr) {
1028 pkg_entry = class_loader_data(class_loader)->packages()->lookup_only(pkg_name);
1029 }
1030 }
1031
1032 ModuleEntry* mod_entry = (pkg_entry == nullptr) ? nullptr : pkg_entry->module();
1033 bool should_be_in_named_module = (mod_entry != nullptr && mod_entry->is_named());
1034 bool was_archived_from_named_module = scp_entry->in_named_module();
1035 bool visible;
1036
1037 if (mod_entry != nullptr && mod_entry->location() == nullptr && mod_entry->is_named()) {
1038 // Archived module for dynamic proxies. It's always visible.
1039 assert(Modules::is_dynamic_proxy_module(mod_entry), "must be");
1040 visible = true;
1041 } else if (was_archived_from_named_module) {
1042 if (should_be_in_named_module) {
1043 // Is the module loaded from the same location as during dump time?
1044 visible = mod_entry->shared_path_index() == scp_index;
1045 if (visible) {
1046 assert(!mod_entry->is_patched(), "cannot load archived classes for patched module");
1047 }
1048 } else {
1049 // During dump time, this class was in a named module, but at run time, this class should be
1050 // in an unnamed module.
1051 visible = false;
1052 }
1053 } else {
1054 if (should_be_in_named_module) {
1055 // During dump time, this class was in an unnamed, but at run time, this class should be
1056 // in a named module.
1057 visible = false;
1058 } else {
1059 visible = true;
1060 }
1061 }
1205 {
1206 HandleMark hm(THREAD);
1207 Handle lockObject = get_loader_lock_or_null(class_loader);
1208 ObjectLocker ol(lockObject, THREAD);
1209 // prohibited package check assumes all classes loaded from archive call
1210 // restore_unshareable_info which calls ik->set_package()
1211 ik->restore_unshareable_info(loader_data, protection_domain, pkg_entry, CHECK_NULL);
1212 }
1213
1214 load_shared_class_misc(ik, loader_data);
1215 return ik;
1216 }
1217
1218 void SystemDictionary::load_shared_class_misc(InstanceKlass* ik, ClassLoaderData* loader_data) {
1219 ik->print_class_load_logging(loader_data, nullptr, nullptr);
1220
1221 // For boot loader, ensure that GetSystemPackage knows that a class in this
1222 // package was loaded.
1223 if (loader_data->is_the_null_class_loader_data()) {
1224 s2 path_index = ik->shared_classpath_index();
1225 if (path_index >= 0) { // FIXME ... for lambda form classes
1226 ik->set_classpath_index(path_index);
1227
1228 if (CDSConfig::is_dumping_final_static_archive()) {
1229 if (path_index > ClassLoaderExt::max_used_path_index()) {
1230 ClassLoaderExt::set_max_used_path_index(path_index);
1231 }
1232 }
1233 }
1234 }
1235
1236 // notify a class loaded from shared object
1237 ClassLoadingService::notify_class_loaded(ik, true /* shared class */);
1238
1239 if (CDSConfig::is_dumping_final_static_archive()) {
1240 SystemDictionaryShared::init_dumptime_info(ik);
1241 if (SystemDictionary::is_platform_class_loader(loader_data->class_loader())) {
1242 ClassLoaderExt::set_has_platform_classes();
1243 } else if (SystemDictionary::is_system_class_loader(loader_data->class_loader())) {
1244 ClassLoaderExt::set_has_app_classes();
1245 }
1246 }
1247 }
1248
1249 #endif // INCLUDE_CDS
1250
1251 InstanceKlass* SystemDictionary::load_instance_class_impl(Symbol* class_name, Handle class_loader, TRAPS) {
1252
1253 if (class_loader.is_null()) {
1254 ResourceMark rm(THREAD);
1255 PackageEntry* pkg_entry = nullptr;
1256 bool search_only_bootloader_append = false;
1257
1258 // Find the package in the boot loader's package entry table.
1259 TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);
1260 if (pkg_name != nullptr) {
1261 pkg_entry = class_loader_data(class_loader)->packages()->lookup_only(pkg_name);
1262 }
1263
1264 // Prior to attempting to load the class, enforce the boot loader's
1265 // visibility boundaries.
1266 if (!Universe::is_module_initialized()) {
1301 // If there is no bootclasspath append entry, no need to continue
1302 // searching.
1303 return nullptr;
1304 }
1305 search_only_bootloader_append = true;
1306 }
1307 }
1308
1309 // Prior to bootstrapping's module initialization, never load a class outside
1310 // of the boot loader's module path
1311 assert(Universe::is_module_initialized() ||
1312 !search_only_bootloader_append,
1313 "Attempt to load a class outside of boot loader's module path");
1314
1315 // Search for classes in the CDS archive.
1316 InstanceKlass* k = nullptr;
1317
1318 #if INCLUDE_CDS
1319 if (CDSConfig::is_using_archive())
1320 {
1321 PerfTraceElapsedTime vmtimer(ClassLoader::perf_shared_classload_time());
1322 InstanceKlass* ik = SystemDictionaryShared::find_builtin_class(class_name);
1323 if (ik != nullptr && ik->is_shared_boot_class() && !ik->shared_loading_failed()) {
1324 SharedClassLoadingMark slm(THREAD, ik);
1325 k = load_shared_class(ik, class_loader, Handle(), nullptr, pkg_entry, CHECK_NULL);
1326 }
1327 }
1328 #endif
1329
1330 if (k == nullptr) {
1331 // Use VM class loader
1332 PerfTraceElapsedTime vmtimer(ClassLoader::perf_sys_classload_time());
1333 k = ClassLoader::load_class(class_name, pkg_entry, search_only_bootloader_append, CHECK_NULL);
1334 }
1335
1336 // find_or_define_instance_class may return a different InstanceKlass
1337 if (k != nullptr) {
1338 CDS_ONLY(SharedClassLoadingMark slm(THREAD, k);)
1339 k = find_or_define_instance_class(class_name, class_loader, k, CHECK_NULL);
1340 }
1341 return k;
1342 } else {
1343 // Use user specified class loader to load class. Call loadClass operation on class_loader.
1344 ResourceMark rm(THREAD);
1345
1346 JavaThread* jt = THREAD;
1347
1348 PerfClassTraceTime vmtimer(ClassLoader::perf_app_classload_time(),
1349 ClassLoader::perf_app_classload_selftime(),
1350 ClassLoader::perf_app_classload_count(),
1351 jt->get_thread_stat()->perf_recursion_counts_addr(),
1352 jt->get_thread_stat()->perf_timers_addr(),
1739 }
1740 }
1741
1742 // Update class loader data dictionary - done after check_constraint and add_to_hierarchy
1743 // have been called.
1744 void SystemDictionary::update_dictionary(JavaThread* current,
1745 InstanceKlass* k,
1746 ClassLoaderData* loader_data) {
1747 MonitorLocker mu1(SystemDictionary_lock);
1748
1749 // Make a new dictionary entry.
1750 Symbol* name = k->name();
1751 Dictionary* dictionary = loader_data->dictionary();
1752 InstanceKlass* sd_check = dictionary->find_class(current, name);
1753 if (sd_check == nullptr) {
1754 dictionary->add_klass(current, name, k);
1755 }
1756 mu1.notify_all();
1757 }
1758
1759 #if INCLUDE_CDS
1760 // Indicate that loader_data has initiated the loading of class k, which
1761 // has already been defined by a parent loader.
1762 // This API should be used only by AOTLinkedClassBulkLoader
1763 void SystemDictionary::add_to_initiating_loader(JavaThread* current,
1764 InstanceKlass* k,
1765 ClassLoaderData* loader_data) {
1766 assert(CDSConfig::is_using_aot_linked_classes(), "must be");
1767 assert_locked_or_safepoint(SystemDictionary_lock);
1768 Symbol* name = k->name();
1769 Dictionary* dictionary = loader_data->dictionary();
1770 assert(k->is_loaded(), "must be");
1771 assert(k->class_loader_data() != loader_data, "only for classes defined by a parent loader");
1772 assert(dictionary->find_class(current, name) == nullptr, "sanity");
1773 dictionary->add_klass(current, name, k);
1774 }
1775 #endif
1776
1777 // Try to find a class name using the loader constraints. The
1778 // loader constraints might know about a class that isn't fully loaded
1779 // yet and these will be ignored.
1780 Klass* SystemDictionary::find_constrained_instance_or_array_klass(
1781 Thread* current, Symbol* class_name, Handle class_loader) {
1782
1783 // First see if it has been loaded directly.
1784 // Force the protection domain to be null. (This removes protection checks.)
1785 Handle no_protection_domain;
1786 Klass* klass = find_instance_or_array_klass(current, class_name, class_loader,
1787 no_protection_domain);
1788 if (klass != nullptr)
1789 return klass;
1790
1791 // Now look to see if it has been loaded elsewhere, and is subject to
1792 // a loader constraint that would require this loader to return the
1793 // klass that is already loaded.
1794 if (Signature::is_array(class_name)) {
1795 // For array classes, their Klass*s are not kept in the
2071 ml.notify_all();
2072 } else {
2073 signature->make_permanent(); // The signature is never unloaded.
2074 assert(Arguments::is_interpreter_only() || (m->has_compiled_code() &&
2075 m->code()->entry_point() == m->from_compiled_entry()),
2076 "MH intrinsic invariant");
2077 *met = m(); // insert the element
2078 ml.notify_all();
2079 return m();
2080 }
2081 }
2082
2083 // Throw OOM or the pending exception in the JavaThread
2084 if (throw_error && !HAS_PENDING_EXCEPTION) {
2085 THROW_MSG_NULL(vmSymbols::java_lang_OutOfMemoryError(),
2086 "Out of space in CodeCache for method handle intrinsic");
2087 }
2088 return nullptr;
2089 }
2090
2091 #if INCLUDE_CDS
2092 void SystemDictionary::get_all_method_handle_intrinsics(GrowableArray<Method*>* methods) {
2093 auto do_method = [&] (InvokeMethodKey& key, Method*& m) {
2094 methods->append(m);
2095 };
2096 _invoke_method_intrinsic_table->iterate_all(do_method);
2097 }
2098
2099 void SystemDictionary::restore_archived_method_handle_intrinsics() {
2100 if (UseSharedSpaces) {
2101 EXCEPTION_MARK;
2102 restore_archived_method_handle_intrinsics_impl(THREAD);
2103 if (HAS_PENDING_EXCEPTION) {
2104 vm_exit_during_initialization(err_msg("Failed to restore archived method handle intrinsics"));
2105 }
2106 }
2107 }
2108
2109 void SystemDictionary::restore_archived_method_handle_intrinsics_impl(TRAPS) {
2110 Array<Method*>* list = MetaspaceShared::archived_method_handle_intrinsics();
2111 for (int i = 0; i < list->length(); i++) {
2112 methodHandle m(THREAD, list->at(i));
2113 Method::restore_archived_method_handle_intrinsic(m, CHECK);
2114 m->constants()->restore_unshareable_info(CHECK);
2115 if (!Arguments::is_interpreter_only() || m->intrinsic_id() == vmIntrinsics::_linkToNative) {
2116 AdapterHandlerLibrary::create_native_wrapper(m);
2117 if (!m->has_compiled_code()) {
2118 ResourceMark rm(THREAD);
2119 vm_exit_during_initialization(err_msg("Failed to initialize method %s", m->external_name()));
2120 }
2121 }
2122
2123 const int iid_as_int = vmIntrinsics::as_int(m->intrinsic_id());
2124 InvokeMethodKey key(m->signature(), iid_as_int);
2125 bool created = _invoke_method_intrinsic_table->put(key, m());
2126 assert(created, "must be");
2127 }
2128 }
2129 #endif
2130
2131 // Helper for unpacking the return value from linkMethod and linkCallSite.
2132 static Method* unpack_method_and_appendix(Handle mname,
2133 Klass* accessing_klass,
2134 objArrayHandle appendix_box,
2135 Handle* appendix_result,
2136 TRAPS) {
2137 if (mname.not_null()) {
2138 Method* m = java_lang_invoke_MemberName::vmtarget(mname());
2139 if (m != nullptr) {
2140 oop appendix = appendix_box->obj_at(0);
2141 LogTarget(Info, methodhandles) lt;
2142 if (lt.develop_is_enabled()) {
2143 ResourceMark rm(THREAD);
2144 LogStream ls(lt);
2145 ls.print("Linked method=" INTPTR_FORMAT ": ", p2i(m));
2146 m->print_on(&ls);
2147 if (appendix != nullptr) { ls.print("appendix = "); appendix->print_on(&ls); }
2148 ls.cr();
2149 }
2150
|