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/resolutionErrors.hpp"
42 #include "classfile/stringTable.hpp"
43 #include "classfile/symbolTable.hpp"
44 #include "classfile/systemDictionary.hpp"
45 #include "classfile/vmClasses.hpp"
46 #include "classfile/vmSymbols.hpp"
47 #include "gc/shared/gcTraceTime.inline.hpp"
48 #include "interpreter/bootstrapInfo.hpp"
49 #include "jfr/jfrEvents.hpp"
50 #include "jvm.h"
51 #include "logging/log.hpp"
52 #include "logging/logStream.hpp"
53 #include "memory/metaspaceClosure.hpp"
54 #include "memory/oopFactory.hpp"
55 #include "memory/resourceArea.hpp"
56 #include "memory/universe.hpp"
57 #include "oops/access.inline.hpp"
58 #include "oops/instanceKlass.hpp"
142 assert(CDSConfig::is_using_archive(), "must be");
143 assert(CDSConfig::is_using_full_module_graph(), "must be");
144 DEBUG_ONLY(
145 oop platform_loader = get_platform_class_loader_impl(CHECK);
146 assert(_java_platform_loader.resolve() == platform_loader, "must be");
147 )
148 }
149
150 if (_java_system_loader.is_empty()) {
151 oop system_loader = get_system_class_loader_impl(CHECK);
152 _java_system_loader = OopHandle(Universe::vm_global(), system_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 system_loader = get_system_class_loader_impl(CHECK);
159 assert(_java_system_loader.resolve() == system_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,
180 vmSymbols::getPlatformClassLoader_name(),
181 vmSymbols::void_classloader_signature(),
981 assert(scp_entry->is_modules_image() || HeapShared::is_a_test_class_in_unnamed_module(ik),
982 "only these classes can be loaded before the module system is initialized");
983 assert(class_loader.is_null(), "sanity");
984 return true;
985 }
986
987 if (pkg_entry == nullptr) {
988 // We might have looked up pkg_entry before the module system was initialized.
989 // Need to reload it now.
990 TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);
991 if (pkg_name != nullptr) {
992 pkg_entry = class_loader_data(class_loader)->packages()->lookup_only(pkg_name);
993 }
994 }
995
996 ModuleEntry* mod_entry = (pkg_entry == nullptr) ? nullptr : pkg_entry->module();
997 bool should_be_in_named_module = (mod_entry != nullptr && mod_entry->is_named());
998 bool was_archived_from_named_module = scp_entry->in_named_module();
999 bool visible;
1000
1001 if (was_archived_from_named_module) {
1002 if (should_be_in_named_module) {
1003 // Is the module loaded from the same location as during dump time?
1004 visible = mod_entry->shared_path_index() == scp_index;
1005 if (visible) {
1006 assert(!mod_entry->is_patched(), "cannot load archived classes for patched module");
1007 }
1008 } else {
1009 // During dump time, this class was in a named module, but at run time, this class should be
1010 // in an unnamed module.
1011 visible = false;
1012 }
1013 } else {
1014 if (should_be_in_named_module) {
1015 // During dump time, this class was in an unnamed, but at run time, this class should be
1016 // in a named module.
1017 visible = false;
1018 } else {
1019 visible = true;
1020 }
1021 }
1163 {
1164 HandleMark hm(THREAD);
1165 Handle lockObject = get_loader_lock_or_null(class_loader);
1166 ObjectLocker ol(lockObject, THREAD);
1167 // prohibited package check assumes all classes loaded from archive call
1168 // restore_unshareable_info which calls ik->set_package()
1169 ik->restore_unshareable_info(loader_data, protection_domain, pkg_entry, CHECK_NULL);
1170 }
1171
1172 load_shared_class_misc(ik, loader_data);
1173 return ik;
1174 }
1175
1176 void SystemDictionary::load_shared_class_misc(InstanceKlass* ik, ClassLoaderData* loader_data) {
1177 ik->print_class_load_logging(loader_data, nullptr, nullptr);
1178
1179 // For boot loader, ensure that GetSystemPackage knows that a class in this
1180 // package was loaded.
1181 if (loader_data->is_the_null_class_loader_data()) {
1182 s2 path_index = ik->shared_classpath_index();
1183 ik->set_classpath_index(path_index);
1184 }
1185
1186 // notify a class loaded from shared object
1187 ClassLoadingService::notify_class_loaded(ik, true /* shared class */);
1188 }
1189
1190 #endif // INCLUDE_CDS
1191
1192 InstanceKlass* SystemDictionary::load_instance_class_impl(Symbol* class_name, Handle class_loader, TRAPS) {
1193
1194 if (class_loader.is_null()) {
1195 ResourceMark rm(THREAD);
1196 PackageEntry* pkg_entry = nullptr;
1197 bool search_only_bootloader_append = false;
1198
1199 // Find the package in the boot loader's package entry table.
1200 TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);
1201 if (pkg_name != nullptr) {
1202 pkg_entry = class_loader_data(class_loader)->packages()->lookup_only(pkg_name);
1203 }
1204
1205 // Prior to attempting to load the class, enforce the boot loader's
1206 // visibility boundaries.
1207 if (!Universe::is_module_initialized()) {
1242 // If there is no bootclasspath append entry, no need to continue
1243 // searching.
1244 return nullptr;
1245 }
1246 search_only_bootloader_append = true;
1247 }
1248 }
1249
1250 // Prior to bootstrapping's module initialization, never load a class outside
1251 // of the boot loader's module path
1252 assert(Universe::is_module_initialized() ||
1253 !search_only_bootloader_append,
1254 "Attempt to load a class outside of boot loader's module path");
1255
1256 // Search for classes in the CDS archive.
1257 InstanceKlass* k = nullptr;
1258
1259 #if INCLUDE_CDS
1260 if (CDSConfig::is_using_archive())
1261 {
1262 PerfTraceTime vmtimer(ClassLoader::perf_shared_classload_time());
1263 InstanceKlass* ik = SystemDictionaryShared::find_builtin_class(class_name);
1264 if (ik != nullptr && ik->is_shared_boot_class() && !ik->shared_loading_failed()) {
1265 SharedClassLoadingMark slm(THREAD, ik);
1266 k = load_shared_class(ik, class_loader, Handle(), nullptr, pkg_entry, CHECK_NULL);
1267 }
1268 }
1269 #endif
1270
1271 if (k == nullptr) {
1272 // Use VM class loader
1273 PerfTraceTime vmtimer(ClassLoader::perf_sys_classload_time());
1274 k = ClassLoader::load_class(class_name, pkg_entry, search_only_bootloader_append, CHECK_NULL);
1275 }
1276
1277 // find_or_define_instance_class may return a different InstanceKlass
1278 if (k != nullptr) {
1279 CDS_ONLY(SharedClassLoadingMark slm(THREAD, k);)
1280 k = find_or_define_instance_class(class_name, class_loader, k, CHECK_NULL);
1281 }
1282 return k;
1283 } else {
1284 // Use user specified class loader to load class. Call loadClass operation on class_loader.
1285 ResourceMark rm(THREAD);
1286
1287 JavaThread* jt = THREAD;
1288
1289 PerfClassTraceTime vmtimer(ClassLoader::perf_app_classload_time(),
1290 ClassLoader::perf_app_classload_selftime(),
1291 ClassLoader::perf_app_classload_count(),
1292 jt->get_thread_stat()->perf_recursion_counts_addr(),
1293 jt->get_thread_stat()->perf_timers_addr(),
|
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/modules.hpp"
40 #include "classfile/packageEntry.hpp"
41 #include "classfile/placeholders.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"
59 #include "oops/instanceKlass.hpp"
143 assert(CDSConfig::is_using_archive(), "must be");
144 assert(CDSConfig::is_using_full_module_graph(), "must be");
145 DEBUG_ONLY(
146 oop platform_loader = get_platform_class_loader_impl(CHECK);
147 assert(_java_platform_loader.resolve() == platform_loader, "must be");
148 )
149 }
150
151 if (_java_system_loader.is_empty()) {
152 oop system_loader = get_system_class_loader_impl(CHECK);
153 _java_system_loader = OopHandle(Universe::vm_global(), system_loader);
154 } else {
155 // It must have been restored from the archived module graph
156 assert(CDSConfig::is_using_archive(), "must be");
157 assert(CDSConfig::is_using_full_module_graph(), "must be");
158 DEBUG_ONLY(
159 oop system_loader = get_system_class_loader_impl(CHECK);
160 assert(_java_system_loader.resolve() == system_loader, "must be");
161 )
162 }
163
164 if (CDSConfig::is_dumping_final_static_archive()) {
165 AOTLinkedClassBulkLoader::load_non_javabase_classes(THREAD);
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,
185 vmSymbols::getPlatformClassLoader_name(),
186 vmSymbols::void_classloader_signature(),
986 assert(scp_entry->is_modules_image() || HeapShared::is_a_test_class_in_unnamed_module(ik),
987 "only these classes can be loaded before the module system is initialized");
988 assert(class_loader.is_null(), "sanity");
989 return true;
990 }
991
992 if (pkg_entry == nullptr) {
993 // We might have looked up pkg_entry before the module system was initialized.
994 // Need to reload it now.
995 TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);
996 if (pkg_name != nullptr) {
997 pkg_entry = class_loader_data(class_loader)->packages()->lookup_only(pkg_name);
998 }
999 }
1000
1001 ModuleEntry* mod_entry = (pkg_entry == nullptr) ? nullptr : pkg_entry->module();
1002 bool should_be_in_named_module = (mod_entry != nullptr && mod_entry->is_named());
1003 bool was_archived_from_named_module = scp_entry->in_named_module();
1004 bool visible;
1005
1006 if (mod_entry != nullptr && mod_entry->location() == nullptr && mod_entry->is_named()) {
1007 // Archived module for dynamic proxies. It's always visible.
1008 assert(Modules::is_dynamic_proxy_module(mod_entry), "must be");
1009 visible = true;
1010 } else if (was_archived_from_named_module) {
1011 if (should_be_in_named_module) {
1012 // Is the module loaded from the same location as during dump time?
1013 visible = mod_entry->shared_path_index() == scp_index;
1014 if (visible) {
1015 assert(!mod_entry->is_patched(), "cannot load archived classes for patched module");
1016 }
1017 } else {
1018 // During dump time, this class was in a named module, but at run time, this class should be
1019 // in an unnamed module.
1020 visible = false;
1021 }
1022 } else {
1023 if (should_be_in_named_module) {
1024 // During dump time, this class was in an unnamed, but at run time, this class should be
1025 // in a named module.
1026 visible = false;
1027 } else {
1028 visible = true;
1029 }
1030 }
1172 {
1173 HandleMark hm(THREAD);
1174 Handle lockObject = get_loader_lock_or_null(class_loader);
1175 ObjectLocker ol(lockObject, THREAD);
1176 // prohibited package check assumes all classes loaded from archive call
1177 // restore_unshareable_info which calls ik->set_package()
1178 ik->restore_unshareable_info(loader_data, protection_domain, pkg_entry, CHECK_NULL);
1179 }
1180
1181 load_shared_class_misc(ik, loader_data);
1182 return ik;
1183 }
1184
1185 void SystemDictionary::load_shared_class_misc(InstanceKlass* ik, ClassLoaderData* loader_data) {
1186 ik->print_class_load_logging(loader_data, nullptr, nullptr);
1187
1188 // For boot loader, ensure that GetSystemPackage knows that a class in this
1189 // package was loaded.
1190 if (loader_data->is_the_null_class_loader_data()) {
1191 s2 path_index = ik->shared_classpath_index();
1192 if (path_index >= 0) { // FIXME ... for lambda form classes
1193 ik->set_classpath_index(path_index);
1194
1195 if (CDSConfig::is_dumping_final_static_archive()) {
1196 if (path_index > ClassLoaderExt::max_used_path_index()) {
1197 ClassLoaderExt::set_max_used_path_index(path_index);
1198 }
1199 }
1200 }
1201 }
1202
1203 // notify a class loaded from shared object
1204 ClassLoadingService::notify_class_loaded(ik, true /* shared class */);
1205
1206 if (CDSConfig::is_dumping_final_static_archive()) {
1207 SystemDictionaryShared::init_dumptime_info(ik);
1208 if (SystemDictionary::is_platform_class_loader(loader_data->class_loader())) {
1209 ClassLoaderExt::set_has_platform_classes();
1210 } else if (SystemDictionary::is_system_class_loader(loader_data->class_loader())) {
1211 ClassLoaderExt::set_has_app_classes();
1212 }
1213 }
1214 }
1215
1216 #endif // INCLUDE_CDS
1217
1218 InstanceKlass* SystemDictionary::load_instance_class_impl(Symbol* class_name, Handle class_loader, TRAPS) {
1219
1220 if (class_loader.is_null()) {
1221 ResourceMark rm(THREAD);
1222 PackageEntry* pkg_entry = nullptr;
1223 bool search_only_bootloader_append = false;
1224
1225 // Find the package in the boot loader's package entry table.
1226 TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);
1227 if (pkg_name != nullptr) {
1228 pkg_entry = class_loader_data(class_loader)->packages()->lookup_only(pkg_name);
1229 }
1230
1231 // Prior to attempting to load the class, enforce the boot loader's
1232 // visibility boundaries.
1233 if (!Universe::is_module_initialized()) {
1268 // If there is no bootclasspath append entry, no need to continue
1269 // searching.
1270 return nullptr;
1271 }
1272 search_only_bootloader_append = true;
1273 }
1274 }
1275
1276 // Prior to bootstrapping's module initialization, never load a class outside
1277 // of the boot loader's module path
1278 assert(Universe::is_module_initialized() ||
1279 !search_only_bootloader_append,
1280 "Attempt to load a class outside of boot loader's module path");
1281
1282 // Search for classes in the CDS archive.
1283 InstanceKlass* k = nullptr;
1284
1285 #if INCLUDE_CDS
1286 if (CDSConfig::is_using_archive())
1287 {
1288 PerfTraceElapsedTime vmtimer(ClassLoader::perf_shared_classload_time());
1289 InstanceKlass* ik = SystemDictionaryShared::find_builtin_class(class_name);
1290 if (ik != nullptr && ik->is_shared_boot_class() && !ik->shared_loading_failed()) {
1291 SharedClassLoadingMark slm(THREAD, ik);
1292 k = load_shared_class(ik, class_loader, Handle(), nullptr, pkg_entry, CHECK_NULL);
1293 }
1294 }
1295 #endif
1296
1297 if (k == nullptr) {
1298 // Use VM class loader
1299 PerfTraceElapsedTime vmtimer(ClassLoader::perf_sys_classload_time());
1300 k = ClassLoader::load_class(class_name, pkg_entry, search_only_bootloader_append, CHECK_NULL);
1301 }
1302
1303 // find_or_define_instance_class may return a different InstanceKlass
1304 if (k != nullptr) {
1305 CDS_ONLY(SharedClassLoadingMark slm(THREAD, k);)
1306 k = find_or_define_instance_class(class_name, class_loader, k, CHECK_NULL);
1307 }
1308 return k;
1309 } else {
1310 // Use user specified class loader to load class. Call loadClass operation on class_loader.
1311 ResourceMark rm(THREAD);
1312
1313 JavaThread* jt = THREAD;
1314
1315 PerfClassTraceTime vmtimer(ClassLoader::perf_app_classload_time(),
1316 ClassLoader::perf_app_classload_selftime(),
1317 ClassLoader::perf_app_classload_count(),
1318 jt->get_thread_stat()->perf_recursion_counts_addr(),
1319 jt->get_thread_stat()->perf_timers_addr(),
|