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"
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
165 oop SystemDictionary::get_system_class_loader_impl(TRAPS) {
166 JavaValue result(T_OBJECT);
167 InstanceKlass* class_loader_klass = vmClasses::ClassLoader_klass();
168 JavaCalls::call_static(&result,
169 class_loader_klass,
170 vmSymbols::getSystemClassLoader_name(),
171 vmSymbols::void_classloader_signature(),
172 CHECK_NULL);
173 return result.get_oop();
174 }
175
176 oop SystemDictionary::get_platform_class_loader_impl(TRAPS) {
177 JavaValue result(T_OBJECT);
178 InstanceKlass* class_loader_klass = vmClasses::ClassLoader_klass();
179 JavaCalls::call_static(&result,
180 class_loader_klass,
181 vmSymbols::getPlatformClassLoader_name(),
182 vmSymbols::void_classloader_signature(),
1001 assert(scp_entry->is_modules_image() || HeapShared::is_a_test_class_in_unnamed_module(ik),
1002 "only these classes can be loaded before the module system is initialized");
1003 assert(class_loader.is_null(), "sanity");
1004 return true;
1005 }
1006
1007 if (pkg_entry == nullptr) {
1008 // We might have looked up pkg_entry before the module system was initialized.
1009 // Need to reload it now.
1010 TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);
1011 if (pkg_name != nullptr) {
1012 pkg_entry = class_loader_data(class_loader)->packages()->lookup_only(pkg_name);
1013 }
1014 }
1015
1016 ModuleEntry* mod_entry = (pkg_entry == nullptr) ? nullptr : pkg_entry->module();
1017 bool should_be_in_named_module = (mod_entry != nullptr && mod_entry->is_named());
1018 bool was_archived_from_named_module = scp_entry->in_named_module();
1019 bool visible;
1020
1021 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 }
1185 {
1186 HandleMark hm(THREAD);
1187 Handle lockObject = get_loader_lock_or_null(class_loader);
1188 ObjectLocker ol(lockObject, THREAD);
1189 // prohibited package check assumes all classes loaded from archive call
1190 // restore_unshareable_info which calls ik->set_package()
1191 ik->restore_unshareable_info(loader_data, protection_domain, pkg_entry, CHECK_NULL);
1192 }
1193
1194 load_shared_class_misc(ik, loader_data);
1195 return ik;
1196 }
1197
1198 void SystemDictionary::load_shared_class_misc(InstanceKlass* ik, ClassLoaderData* loader_data) {
1199 ik->print_class_load_logging(loader_data, nullptr, nullptr);
1200
1201 // For boot loader, ensure that GetSystemPackage knows that a class in this
1202 // package was loaded.
1203 if (loader_data->is_the_null_class_loader_data()) {
1204 s2 path_index = ik->shared_classpath_index();
1205 ik->set_classpath_index(path_index);
1206 }
1207
1208 // notify a class loaded from shared object
1209 ClassLoadingService::notify_class_loaded(ik, true /* shared class */);
1210 }
1211
1212 #endif // INCLUDE_CDS
1213
1214 InstanceKlass* SystemDictionary::load_instance_class_impl(Symbol* class_name, Handle class_loader, TRAPS) {
1215
1216 if (class_loader.is_null()) {
1217 ResourceMark rm(THREAD);
1218 PackageEntry* pkg_entry = nullptr;
1219 bool search_only_bootloader_append = false;
1220
1221 // Find the package in the boot loader's package entry table.
1222 TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);
1223 if (pkg_name != nullptr) {
1224 pkg_entry = class_loader_data(class_loader)->packages()->lookup_only(pkg_name);
1225 }
1226
1227 // Prior to attempting to load the class, enforce the boot loader's
1228 // visibility boundaries.
1229 if (!Universe::is_module_initialized()) {
1264 // If there is no bootclasspath append entry, no need to continue
1265 // searching.
1266 return nullptr;
1267 }
1268 search_only_bootloader_append = true;
1269 }
1270 }
1271
1272 // Prior to bootstrapping's module initialization, never load a class outside
1273 // of the boot loader's module path
1274 assert(Universe::is_module_initialized() ||
1275 !search_only_bootloader_append,
1276 "Attempt to load a class outside of boot loader's module path");
1277
1278 // Search for classes in the CDS archive.
1279 InstanceKlass* k = nullptr;
1280
1281 #if INCLUDE_CDS
1282 if (CDSConfig::is_using_archive())
1283 {
1284 PerfTraceTime vmtimer(ClassLoader::perf_shared_classload_time());
1285 InstanceKlass* ik = SystemDictionaryShared::find_builtin_class(class_name);
1286 if (ik != nullptr && ik->is_shared_boot_class() && !ik->shared_loading_failed()) {
1287 SharedClassLoadingMark slm(THREAD, ik);
1288 k = load_shared_class(ik, class_loader, Handle(), nullptr, pkg_entry, CHECK_NULL);
1289 }
1290 }
1291 #endif
1292
1293 if (k == nullptr) {
1294 // Use VM class loader
1295 PerfTraceTime vmtimer(ClassLoader::perf_sys_classload_time());
1296 k = ClassLoader::load_class(class_name, pkg_entry, search_only_bootloader_append, CHECK_NULL);
1297 }
1298
1299 // find_or_define_instance_class may return a different InstanceKlass
1300 if (k != nullptr) {
1301 CDS_ONLY(SharedClassLoadingMark slm(THREAD, k);)
1302 k = find_or_define_instance_class(class_name, class_loader, k, CHECK_NULL);
1303 }
1304 return k;
1305 } else {
1306 // Use user specified class loader to load class. Call loadClass operation on class_loader.
1307 ResourceMark rm(THREAD);
1308
1309 JavaThread* jt = THREAD;
1310
1311 PerfClassTraceTime vmtimer(ClassLoader::perf_app_classload_time(),
1312 ClassLoader::perf_app_classload_selftime(),
1313 ClassLoader::perf_app_classload_count(),
1314 jt->get_thread_stat()->perf_recursion_counts_addr(),
1315 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/protectionDomainCache.hpp"
43 #include "classfile/resolutionErrors.hpp"
44 #include "classfile/stringTable.hpp"
45 #include "classfile/symbolTable.hpp"
46 #include "classfile/systemDictionary.hpp"
47 #include "classfile/vmClasses.hpp"
48 #include "classfile/vmSymbols.hpp"
49 #include "gc/shared/gcTraceTime.inline.hpp"
50 #include "interpreter/bootstrapInfo.hpp"
51 #include "jfr/jfrEvents.hpp"
52 #include "jvm.h"
53 #include "logging/log.hpp"
54 #include "logging/logStream.hpp"
55 #include "memory/metaspaceClosure.hpp"
56 #include "memory/oopFactory.hpp"
57 #include "memory/resourceArea.hpp"
58 #include "memory/universe.hpp"
59 #include "oops/access.inline.hpp"
144 assert(CDSConfig::is_using_archive(), "must be");
145 assert(CDSConfig::is_using_full_module_graph(), "must be");
146 DEBUG_ONLY(
147 oop platform_loader = get_platform_class_loader_impl(CHECK);
148 assert(_java_platform_loader.resolve() == platform_loader, "must be");
149 )
150 }
151
152 if (_java_system_loader.is_empty()) {
153 oop system_loader = get_system_class_loader_impl(CHECK);
154 _java_system_loader = OopHandle(Universe::vm_global(), system_loader);
155 } else {
156 // It must have been restored from the archived module graph
157 assert(CDSConfig::is_using_archive(), "must be");
158 assert(CDSConfig::is_using_full_module_graph(), "must be");
159 DEBUG_ONLY(
160 oop system_loader = get_system_class_loader_impl(CHECK);
161 assert(_java_system_loader.resolve() == system_loader, "must be");
162 )
163 }
164
165 if (CDSConfig::is_dumping_final_static_archive()) {
166 AOTLinkedClassBulkLoader::load_non_javabase_classes(THREAD);
167 }
168 }
169
170 oop SystemDictionary::get_system_class_loader_impl(TRAPS) {
171 JavaValue result(T_OBJECT);
172 InstanceKlass* class_loader_klass = vmClasses::ClassLoader_klass();
173 JavaCalls::call_static(&result,
174 class_loader_klass,
175 vmSymbols::getSystemClassLoader_name(),
176 vmSymbols::void_classloader_signature(),
177 CHECK_NULL);
178 return result.get_oop();
179 }
180
181 oop SystemDictionary::get_platform_class_loader_impl(TRAPS) {
182 JavaValue result(T_OBJECT);
183 InstanceKlass* class_loader_klass = vmClasses::ClassLoader_klass();
184 JavaCalls::call_static(&result,
185 class_loader_klass,
186 vmSymbols::getPlatformClassLoader_name(),
187 vmSymbols::void_classloader_signature(),
1006 assert(scp_entry->is_modules_image() || HeapShared::is_a_test_class_in_unnamed_module(ik),
1007 "only these classes can be loaded before the module system is initialized");
1008 assert(class_loader.is_null(), "sanity");
1009 return true;
1010 }
1011
1012 if (pkg_entry == nullptr) {
1013 // We might have looked up pkg_entry before the module system was initialized.
1014 // Need to reload it now.
1015 TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);
1016 if (pkg_name != nullptr) {
1017 pkg_entry = class_loader_data(class_loader)->packages()->lookup_only(pkg_name);
1018 }
1019 }
1020
1021 ModuleEntry* mod_entry = (pkg_entry == nullptr) ? nullptr : pkg_entry->module();
1022 bool should_be_in_named_module = (mod_entry != nullptr && mod_entry->is_named());
1023 bool was_archived_from_named_module = scp_entry->in_named_module();
1024 bool visible;
1025
1026 if (mod_entry != nullptr && mod_entry->location() == nullptr && mod_entry->is_named()) {
1027 // Archived module for dynamic proxies. It's always visible.
1028 assert(Modules::is_dynamic_proxy_module(mod_entry), "must be");
1029 visible = true;
1030 } else if (was_archived_from_named_module) {
1031 if (should_be_in_named_module) {
1032 // Is the module loaded from the same location as during dump time?
1033 visible = mod_entry->shared_path_index() == scp_index;
1034 if (visible) {
1035 assert(!mod_entry->is_patched(), "cannot load archived classes for patched module");
1036 }
1037 } else {
1038 // During dump time, this class was in a named module, but at run time, this class should be
1039 // in an unnamed module.
1040 visible = false;
1041 }
1042 } else {
1043 if (should_be_in_named_module) {
1044 // During dump time, this class was in an unnamed, but at run time, this class should be
1045 // in a named module.
1046 visible = false;
1047 } else {
1048 visible = true;
1049 }
1050 }
1194 {
1195 HandleMark hm(THREAD);
1196 Handle lockObject = get_loader_lock_or_null(class_loader);
1197 ObjectLocker ol(lockObject, THREAD);
1198 // prohibited package check assumes all classes loaded from archive call
1199 // restore_unshareable_info which calls ik->set_package()
1200 ik->restore_unshareable_info(loader_data, protection_domain, pkg_entry, CHECK_NULL);
1201 }
1202
1203 load_shared_class_misc(ik, loader_data);
1204 return ik;
1205 }
1206
1207 void SystemDictionary::load_shared_class_misc(InstanceKlass* ik, ClassLoaderData* loader_data) {
1208 ik->print_class_load_logging(loader_data, nullptr, nullptr);
1209
1210 // For boot loader, ensure that GetSystemPackage knows that a class in this
1211 // package was loaded.
1212 if (loader_data->is_the_null_class_loader_data()) {
1213 s2 path_index = ik->shared_classpath_index();
1214 if (path_index >= 0) { // FIXME ... for lambda form classes
1215 ik->set_classpath_index(path_index);
1216
1217 if (CDSConfig::is_dumping_final_static_archive()) {
1218 if (path_index > ClassLoaderExt::max_used_path_index()) {
1219 ClassLoaderExt::set_max_used_path_index(path_index);
1220 }
1221 }
1222 }
1223 }
1224
1225 // notify a class loaded from shared object
1226 ClassLoadingService::notify_class_loaded(ik, true /* shared class */);
1227
1228 if (CDSConfig::is_dumping_final_static_archive()) {
1229 SystemDictionaryShared::init_dumptime_info(ik);
1230 if (SystemDictionary::is_platform_class_loader(loader_data->class_loader())) {
1231 ClassLoaderExt::set_has_platform_classes();
1232 } else if (SystemDictionary::is_system_class_loader(loader_data->class_loader())) {
1233 ClassLoaderExt::set_has_app_classes();
1234 }
1235 }
1236 }
1237
1238 #endif // INCLUDE_CDS
1239
1240 InstanceKlass* SystemDictionary::load_instance_class_impl(Symbol* class_name, Handle class_loader, TRAPS) {
1241
1242 if (class_loader.is_null()) {
1243 ResourceMark rm(THREAD);
1244 PackageEntry* pkg_entry = nullptr;
1245 bool search_only_bootloader_append = false;
1246
1247 // Find the package in the boot loader's package entry table.
1248 TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);
1249 if (pkg_name != nullptr) {
1250 pkg_entry = class_loader_data(class_loader)->packages()->lookup_only(pkg_name);
1251 }
1252
1253 // Prior to attempting to load the class, enforce the boot loader's
1254 // visibility boundaries.
1255 if (!Universe::is_module_initialized()) {
1290 // If there is no bootclasspath append entry, no need to continue
1291 // searching.
1292 return nullptr;
1293 }
1294 search_only_bootloader_append = true;
1295 }
1296 }
1297
1298 // Prior to bootstrapping's module initialization, never load a class outside
1299 // of the boot loader's module path
1300 assert(Universe::is_module_initialized() ||
1301 !search_only_bootloader_append,
1302 "Attempt to load a class outside of boot loader's module path");
1303
1304 // Search for classes in the CDS archive.
1305 InstanceKlass* k = nullptr;
1306
1307 #if INCLUDE_CDS
1308 if (CDSConfig::is_using_archive())
1309 {
1310 PerfTraceElapsedTime vmtimer(ClassLoader::perf_shared_classload_time());
1311 InstanceKlass* ik = SystemDictionaryShared::find_builtin_class(class_name);
1312 if (ik != nullptr && ik->is_shared_boot_class() && !ik->shared_loading_failed()) {
1313 SharedClassLoadingMark slm(THREAD, ik);
1314 k = load_shared_class(ik, class_loader, Handle(), nullptr, pkg_entry, CHECK_NULL);
1315 }
1316 }
1317 #endif
1318
1319 if (k == nullptr) {
1320 // Use VM class loader
1321 PerfTraceElapsedTime vmtimer(ClassLoader::perf_sys_classload_time());
1322 k = ClassLoader::load_class(class_name, pkg_entry, search_only_bootloader_append, CHECK_NULL);
1323 }
1324
1325 // find_or_define_instance_class may return a different InstanceKlass
1326 if (k != nullptr) {
1327 CDS_ONLY(SharedClassLoadingMark slm(THREAD, k);)
1328 k = find_or_define_instance_class(class_name, class_loader, k, CHECK_NULL);
1329 }
1330 return k;
1331 } else {
1332 // Use user specified class loader to load class. Call loadClass operation on class_loader.
1333 ResourceMark rm(THREAD);
1334
1335 JavaThread* jt = THREAD;
1336
1337 PerfClassTraceTime vmtimer(ClassLoader::perf_app_classload_time(),
1338 ClassLoader::perf_app_classload_selftime(),
1339 ClassLoader::perf_app_classload_count(),
1340 jt->get_thread_stat()->perf_recursion_counts_addr(),
1341 jt->get_thread_stat()->perf_timers_addr(),
|