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 "cds/aotClassLocation.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/classLoadInfo.hpp"
34 #include "classfile/dictionary.hpp"
35 #include "classfile/javaClasses.inline.hpp"
36 #include "classfile/klassFactory.hpp"
37 #include "classfile/loaderConstraints.hpp"
38 #include "classfile/packageEntry.hpp"
39 #include "classfile/placeholders.hpp"
40 #include "classfile/resolutionErrors.hpp"
41 #include "classfile/stringTable.hpp"
42 #include "classfile/symbolTable.hpp"
43 #include "classfile/systemDictionary.hpp"
44 #include "classfile/vmClasses.hpp"
45 #include "classfile/vmSymbols.hpp"
46 #include "gc/shared/gcTraceTime.inline.hpp"
47 #include "interpreter/bootstrapInfo.hpp"
48 #include "jfr/jfrEvents.hpp"
49 #include "jvm.h"
50 #include "logging/log.hpp"
51 #include "logging/logStream.hpp"
52 #include "memory/metaspaceClosure.hpp"
53 #include "memory/oopFactory.hpp"
54 #include "memory/resourceArea.hpp"
55 #include "memory/universe.hpp"
56 #include "oops/access.inline.hpp"
57 #include "oops/constantPool.inline.hpp"
968 assert(cl->is_modules_image() || HeapShared::is_a_test_class_in_unnamed_module(ik),
969 "only these classes can be loaded before the module system is initialized");
970 assert(class_loader.is_null(), "sanity");
971 return true;
972 }
973
974 if (pkg_entry == nullptr) {
975 // We might have looked up pkg_entry before the module system was initialized.
976 // Need to reload it now.
977 TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);
978 if (pkg_name != nullptr) {
979 pkg_entry = class_loader_data(class_loader)->packages()->lookup_only(pkg_name);
980 }
981 }
982
983 ModuleEntry* mod_entry = (pkg_entry == nullptr) ? nullptr : pkg_entry->module();
984 bool should_be_in_named_module = (mod_entry != nullptr && mod_entry->is_named());
985 bool was_archived_from_named_module = !cl->has_unnamed_module();
986 bool visible;
987
988 if (was_archived_from_named_module) {
989 if (should_be_in_named_module) {
990 // Is the module loaded from the same location as during dump time?
991 visible = mod_entry->shared_path_index() == scp_index;
992 if (visible) {
993 assert(!mod_entry->is_patched(), "cannot load archived classes for patched module");
994 }
995 } else {
996 // During dump time, this class was in a named module, but at run time, this class should be
997 // in an unnamed module.
998 visible = false;
999 }
1000 } else {
1001 if (should_be_in_named_module) {
1002 // During dump time, this class was in an unnamed, but at run time, this class should be
1003 // in a named module.
1004 visible = false;
1005 } else {
1006 visible = true;
1007 }
1008 }
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 }
1149
1150 // This is much more lightweight than SystemDictionary::resolve_or_null
1151 // - There's only a single Java thread at this point. No need for placeholder.
1152 // - All supertypes of ik have been loaded
1153 // - There's no circularity (checked in AOT assembly phase)
1154 // - There's no need to call java.lang.ClassLoader::load_class() because the boot/platform/app
1155 // loaders are well-behaved
1156 void SystemDictionary::preload_class(Handle class_loader, InstanceKlass* ik, TRAPS) {
1157 precond(Universe::is_bootstrapping());
1158 precond(java_platform_loader() != nullptr && java_system_loader() != nullptr);
1159 precond(class_loader() == nullptr || class_loader() == java_platform_loader() ||class_loader() == java_system_loader());
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->defined_by_boot_loader() && !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(),
|
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 "cds/aotClassLocation.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/classLoadInfo.hpp"
34 #include "classfile/dictionary.hpp"
35 #include "classfile/javaClasses.inline.hpp"
36 #include "classfile/klassFactory.hpp"
37 #include "classfile/loaderConstraints.hpp"
38 #include "classfile/modules.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/constantPool.inline.hpp"
969 assert(cl->is_modules_image() || HeapShared::is_a_test_class_in_unnamed_module(ik),
970 "only these classes can be loaded before the module system is initialized");
971 assert(class_loader.is_null(), "sanity");
972 return true;
973 }
974
975 if (pkg_entry == nullptr) {
976 // We might have looked up pkg_entry before the module system was initialized.
977 // Need to reload it now.
978 TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);
979 if (pkg_name != nullptr) {
980 pkg_entry = class_loader_data(class_loader)->packages()->lookup_only(pkg_name);
981 }
982 }
983
984 ModuleEntry* mod_entry = (pkg_entry == nullptr) ? nullptr : pkg_entry->module();
985 bool should_be_in_named_module = (mod_entry != nullptr && mod_entry->is_named());
986 bool was_archived_from_named_module = !cl->has_unnamed_module();
987 bool visible;
988
989 if (mod_entry != nullptr && mod_entry->location() == nullptr && mod_entry->is_named()) {
990 // Archived module for dynamic proxies. It's always visible.
991 assert(Modules::is_dynamic_proxy_module(mod_entry), "must be");
992 visible = true;
993 } else if (was_archived_from_named_module) {
994 if (should_be_in_named_module) {
995 // Is the module loaded from the same location as during dump time?
996 visible = mod_entry->shared_path_index() == scp_index;
997 if (visible) {
998 assert(!mod_entry->is_patched(), "cannot load archived classes for patched module");
999 }
1000 } else {
1001 // During dump time, this class was in a named module, but at run time, this class should be
1002 // in an unnamed module.
1003 visible = false;
1004 }
1005 } else {
1006 if (should_be_in_named_module) {
1007 // During dump time, this class was in an unnamed, but at run time, this class should be
1008 // in a named module.
1009 visible = false;
1010 } else {
1011 visible = true;
1012 }
1013 }
1124 {
1125 HandleMark hm(THREAD);
1126 Handle lockObject = get_loader_lock_or_null(class_loader);
1127 ObjectLocker ol(lockObject, THREAD);
1128 // prohibited package check assumes all classes loaded from archive call
1129 // restore_unshareable_info which calls ik->set_package()
1130 ik->restore_unshareable_info(loader_data, protection_domain, pkg_entry, CHECK_NULL);
1131 }
1132
1133 load_shared_class_misc(ik, loader_data);
1134 return ik;
1135 }
1136
1137 void SystemDictionary::load_shared_class_misc(InstanceKlass* ik, ClassLoaderData* loader_data) {
1138 ik->print_class_load_logging(loader_data, nullptr, nullptr);
1139
1140 // For boot loader, ensure that GetSystemPackage knows that a class in this
1141 // package was loaded.
1142 if (loader_data->is_the_null_class_loader_data()) {
1143 s2 path_index = ik->shared_classpath_index();
1144 if (path_index >= 0) { // FIXME ... for lambda form classes
1145 ik->set_classpath_index(path_index);
1146
1147 if (CDSConfig::is_dumping_final_static_archive()) {
1148 AOTClassLocationConfig::dumptime_update_max_used_index(path_index);
1149 }
1150 }
1151 }
1152
1153 // notify a class loaded from shared object
1154 ClassLoadingService::notify_class_loaded(ik, true /* shared class */);
1155
1156 if (CDSConfig::is_dumping_final_static_archive()) {
1157 SystemDictionaryShared::init_dumptime_info_from_preimage(ik);
1158 }
1159 }
1160
1161 // This is much more lightweight than SystemDictionary::resolve_or_null
1162 // - There's only a single Java thread at this point. No need for placeholder.
1163 // - All supertypes of ik have been loaded
1164 // - There's no circularity (checked in AOT assembly phase)
1165 // - There's no need to call java.lang.ClassLoader::load_class() because the boot/platform/app
1166 // loaders are well-behaved
1167 void SystemDictionary::preload_class(Handle class_loader, InstanceKlass* ik, TRAPS) {
1168 precond(Universe::is_bootstrapping());
1169 precond(java_platform_loader() != nullptr && java_system_loader() != nullptr);
1170 precond(class_loader() == nullptr || class_loader() == java_platform_loader() ||class_loader() == java_system_loader());
1282 // If there is no bootclasspath append entry, no need to continue
1283 // searching.
1284 return nullptr;
1285 }
1286 search_only_bootloader_append = true;
1287 }
1288 }
1289
1290 // Prior to bootstrapping's module initialization, never load a class outside
1291 // of the boot loader's module path
1292 assert(Universe::is_module_initialized() ||
1293 !search_only_bootloader_append,
1294 "Attempt to load a class outside of boot loader's module path");
1295
1296 // Search for classes in the CDS archive.
1297 InstanceKlass* k = nullptr;
1298
1299 #if INCLUDE_CDS
1300 if (CDSConfig::is_using_archive())
1301 {
1302 PerfTraceElapsedTime vmtimer(ClassLoader::perf_shared_classload_time());
1303 InstanceKlass* ik = SystemDictionaryShared::find_builtin_class(class_name);
1304 if (ik != nullptr && ik->defined_by_boot_loader() && !ik->shared_loading_failed()) {
1305 SharedClassLoadingMark slm(THREAD, ik);
1306 k = load_shared_class(ik, class_loader, Handle(), nullptr, pkg_entry, CHECK_NULL);
1307 }
1308 }
1309 #endif
1310
1311 if (k == nullptr) {
1312 // Use VM class loader
1313 PerfTraceElapsedTime vmtimer(ClassLoader::perf_sys_classload_time());
1314 k = ClassLoader::load_class(class_name, pkg_entry, search_only_bootloader_append, CHECK_NULL);
1315 }
1316
1317 // find_or_define_instance_class may return a different InstanceKlass
1318 if (k != nullptr) {
1319 CDS_ONLY(SharedClassLoadingMark slm(THREAD, k);)
1320 k = find_or_define_instance_class(class_name, class_loader, k, CHECK_NULL);
1321 }
1322 return k;
1323 } else {
1324 // Use user specified class loader to load class. Call loadClass operation on class_loader.
1325 ResourceMark rm(THREAD);
1326
1327 JavaThread* jt = THREAD;
1328
1329 PerfClassTraceTime vmtimer(ClassLoader::perf_app_classload_time(),
1330 ClassLoader::perf_app_classload_selftime(),
1331 ClassLoader::perf_app_classload_count(),
1332 jt->get_thread_stat()->perf_recursion_counts_addr(),
1333 jt->get_thread_stat()->perf_timers_addr(),
|