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/instanceKlass.hpp"
58 #include "oops/klass.inline.hpp"
59 #include "oops/method.inline.hpp"
60 #include "oops/objArrayKlass.hpp"
61 #include "oops/objArrayOop.inline.hpp"
62 #include "oops/oop.inline.hpp"
63 #include "oops/oopHandle.inline.hpp"
64 #include "oops/symbol.hpp"
65 #include "oops/typeArrayKlass.hpp"
66 #include "prims/jvmtiExport.hpp"
67 #include "prims/methodHandles.hpp"
68 #include "runtime/arguments.hpp"
69 #include "runtime/atomicAccess.hpp"
70 #include "runtime/handles.inline.hpp"
71 #include "runtime/java.hpp"
72 #include "runtime/javaCalls.hpp"
73 #include "runtime/mutexLocker.hpp"
74 #include "runtime/sharedRuntime.hpp"
75 #include "runtime/signature.hpp"
76 #include "runtime/synchronizer.hpp"
77 #include "services/classLoadingService.hpp"
78 #include "services/diagnosticCommand.hpp"
79 #include "services/finalizerService.hpp"
80 #include "services/threadService.hpp"
81 #include "utilities/growableArray.hpp"
82 #include "utilities/macros.hpp"
83 #include "utilities/utf8.hpp"
84 #if INCLUDE_CDS
85 #include "classfile/systemDictionaryShared.hpp"
86 #endif
87 #if INCLUDE_JFR
88 #include "jfr/jfr.hpp"
89 #endif
90
91 class InvokeMethodKey : public StackObj {
92 private:
93 Symbol* _symbol;
168 CHECK_NULL);
169 return result.get_oop();
170 }
171
172 oop SystemDictionary::get_platform_class_loader_impl(TRAPS) {
173 JavaValue result(T_OBJECT);
174 InstanceKlass* class_loader_klass = vmClasses::ClassLoader_klass();
175 JavaCalls::call_static(&result,
176 class_loader_klass,
177 vmSymbols::getPlatformClassLoader_name(),
178 vmSymbols::void_classloader_signature(),
179 CHECK_NULL);
180 return result.get_oop();
181 }
182
183 // Helper function
184 inline ClassLoaderData* class_loader_data(Handle class_loader) {
185 return ClassLoaderData::class_loader_data(class_loader());
186 }
187
188 ClassLoaderData* SystemDictionary::register_loader(Handle class_loader, bool create_mirror_cld) {
189 if (create_mirror_cld) {
190 // Add a new class loader data to the graph.
191 return ClassLoaderDataGraph::add(class_loader, true);
192 } else {
193 return (class_loader() == nullptr) ? ClassLoaderData::the_null_class_loader_data() :
194 ClassLoaderDataGraph::find_or_create(class_loader);
195 }
196 }
197
198 void SystemDictionary::set_system_loader(ClassLoaderData *cld) {
199 if (_java_system_loader.is_empty()) {
200 _java_system_loader = cld->class_loader_handle();
201 } else {
202 assert(_java_system_loader.resolve() == cld->class_loader(), "sanity");
203 }
204 }
205
206 void SystemDictionary::set_platform_loader(ClassLoaderData *cld) {
207 if (_java_platform_loader.is_empty()) {
208 _java_platform_loader = cld->class_loader_handle();
209 } else {
210 assert(_java_platform_loader.resolve() == cld->class_loader(), "sanity");
211 }
212 }
213
214 // ----------------------------------------------------------------------------
382 }
383 } else {
384 k = Universe::typeArrayKlass(t);
385 k = k->array_klass(ndims, CHECK_NULL);
386 }
387 return k;
388 }
389
390 static inline void log_circularity_error(Symbol* name, PlaceholderEntry* probe) {
391 LogTarget(Debug, class, load, placeholders) lt;
392 if (lt.is_enabled()) {
393 ResourceMark rm;
394 LogStream ls(lt);
395 ls.print("ClassCircularityError detected for placeholder entry %s", name->as_C_string());
396 probe->print_on(&ls);
397 ls.cr();
398 }
399 }
400
401 // Must be called for any superclass or superinterface resolution
402 // during class definition to allow class circularity checking
403 // superinterface callers:
404 // parse_interfaces - from defineClass
405 // superclass callers:
406 // ClassFileParser - from defineClass
407 // load_shared_class - while loading a class from shared archive
408 // resolve_instance_class_or_null:
409 // via: handle_parallel_super_load
410 // when resolving a class that has an existing placeholder with
411 // a saved superclass [i.e. a defineClass is currently in progress]
412 // If another thread is trying to resolve the class, it must do
413 // superclass checks on its own thread to catch class circularity and
414 // to avoid deadlock.
415 //
416 // resolve_with_circularity_detection adds a DETECT_CIRCULARITY placeholder to the placeholder table before calling
417 // resolve_instance_class_or_null. ClassCircularityError is detected when a DETECT_CIRCULARITY or LOAD_INSTANCE
418 // placeholder for the same thread, class, classloader is found.
419 // This can be seen with logging option: -Xlog:class+load+placeholders=debug.
420 //
421 InstanceKlass* SystemDictionary::resolve_with_circularity_detection(Symbol* class_name,
422 Symbol* next_name,
423 Handle class_loader,
424 bool is_superclass,
425 TRAPS) {
426
427 assert(next_name != nullptr, "null superclass for resolving");
428 assert(!Signature::is_array(next_name), "invalid superclass name");
429
430 ClassLoaderData* loader_data = class_loader_data(class_loader);
431
432 if (is_superclass) {
433 InstanceKlass* klassk = loader_data->dictionary()->find_class(THREAD, class_name);
434 if (klassk != nullptr) {
435 // We can come here for two reasons:
436 // (a) RedefineClasses -- the class is already loaded
437 // (b) Rarely, the class might have been loaded by a parallel thread
438 // We can do a quick check against the already assigned superclass's name and loader.
439 InstanceKlass* superk = klassk->super();
440 if (superk != nullptr &&
441 superk->name() == next_name &&
442 superk->class_loader() == class_loader()) {
443 return superk;
444 }
445 }
446 }
447
448 // can't throw error holding a lock
449 bool throw_circularity_error = false;
450 {
451 MutexLocker mu(THREAD, SystemDictionary_lock);
452
453 // Must check ClassCircularity before resolving next_name (superclass or interface).
454 PlaceholderEntry* probe = PlaceholderTable::get_entry(class_name, loader_data);
455 if (probe != nullptr && probe->check_seen_thread(THREAD, PlaceholderTable::DETECT_CIRCULARITY)) {
456 log_circularity_error(class_name, probe);
457 throw_circularity_error = true;
458 }
459
460 // Make sure there's a placeholder for the class_name before resolving.
461 // This is used as a claim that this thread is currently loading superclass/classloader
462 // and for ClassCircularity checks.
463 if (!throw_circularity_error) {
464 // Be careful not to exit resolve_with_circularity_detection without removing this placeholder.
465 PlaceholderEntry* newprobe = PlaceholderTable::find_and_add(class_name,
466 loader_data,
467 PlaceholderTable::DETECT_CIRCULARITY,
468 next_name, THREAD);
469 }
470 }
471
472 if (throw_circularity_error) {
473 ResourceMark rm(THREAD);
474 THROW_MSG_NULL(vmSymbols::java_lang_ClassCircularityError(), class_name->as_C_string());
475 }
476
477 // Resolve the superclass or superinterface, check results on return
478 InstanceKlass* superk =
479 SystemDictionary::resolve_instance_class_or_null(next_name,
480 class_loader,
481 THREAD);
482
483 // Clean up placeholder entry.
484 {
485 MutexLocker mu(THREAD, SystemDictionary_lock);
486 PlaceholderTable::find_and_remove(class_name, loader_data, PlaceholderTable::DETECT_CIRCULARITY, THREAD);
487 SystemDictionary_lock->notify_all();
488 }
489
490 // Check for pending exception or null superk, and throw exception
491 if (HAS_PENDING_EXCEPTION || superk == nullptr) {
492 handle_resolution_exception(next_name, true, CHECK_NULL);
493 }
494
495 return superk;
496 }
497
903
904 InstanceKlass* SystemDictionary::resolve_from_stream(ClassFileStream* st,
905 Symbol* class_name,
906 Handle class_loader,
907 const ClassLoadInfo& cl_info,
908 TRAPS) {
909 if (cl_info.is_hidden()) {
910 return resolve_hidden_class_from_stream(st, class_name, class_loader, cl_info, CHECK_NULL);
911 } else {
912 return resolve_class_from_stream(st, class_name, class_loader, cl_info, CHECK_NULL);
913 }
914 }
915
916
917 #if INCLUDE_CDS
918 // Check if a shared class can be loaded by the specific classloader.
919 bool SystemDictionary::is_shared_class_visible(Symbol* class_name,
920 InstanceKlass* ik,
921 PackageEntry* pkg_entry,
922 Handle class_loader) {
923 assert(!ModuleEntryTable::javabase_moduleEntry()->is_patched(),
924 "Cannot use sharing if java.base is patched");
925
926 // (1) Check if we are loading into the same loader as in dump time.
927
928 if (ik->defined_by_boot_loader()) {
929 if (class_loader() != nullptr) {
930 return false;
931 }
932 } else if (ik->defined_by_platform_loader()) {
933 if (class_loader() != java_platform_loader()) {
934 return false;
935 }
936 } else if (ik->defined_by_app_loader()) {
937 if (class_loader() != java_system_loader()) {
938 return false;
939 }
940 } else {
941 // ik was loaded by a custom loader during dump time
942 if (class_loader_data(class_loader)->is_builtin_class_loader_data()) {
943 return false;
944 } else {
980
981 if (pkg_entry == nullptr) {
982 // We might have looked up pkg_entry before the module system was initialized.
983 // Need to reload it now.
984 TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);
985 if (pkg_name != nullptr) {
986 pkg_entry = class_loader_data(class_loader)->packages()->lookup_only(pkg_name);
987 }
988 }
989
990 ModuleEntry* mod_entry = (pkg_entry == nullptr) ? nullptr : pkg_entry->module();
991 bool should_be_in_named_module = (mod_entry != nullptr && mod_entry->is_named());
992 bool was_archived_from_named_module = !cl->has_unnamed_module();
993 bool visible;
994
995 if (was_archived_from_named_module) {
996 if (should_be_in_named_module) {
997 // Is the module loaded from the same location as during dump time?
998 visible = mod_entry->shared_path_index() == scp_index;
999 if (visible) {
1000 assert(!mod_entry->is_patched(), "cannot load archived classes for patched module");
1001 }
1002 } else {
1003 // During dump time, this class was in a named module, but at run time, this class should be
1004 // in an unnamed module.
1005 visible = false;
1006 }
1007 } else {
1008 if (should_be_in_named_module) {
1009 // During dump time, this class was in an unnamed, but at run time, this class should be
1010 // in a named module.
1011 visible = false;
1012 } else {
1013 visible = true;
1014 }
1015 }
1016
1017 return visible;
1018 }
1019
1020 bool SystemDictionary::check_shared_class_super_type(InstanceKlass* klass, InstanceKlass* super_type,
1058 class_loader, true,
1059 CHECK_false);
1060 if (!check_super) {
1061 return false;
1062 }
1063 }
1064
1065 Array<InstanceKlass*>* interfaces = ik->local_interfaces();
1066 int num_interfaces = interfaces->length();
1067 for (int index = 0; index < num_interfaces; index++) {
1068 bool check_interface = check_shared_class_super_type(ik, interfaces->at(index), class_loader, false,
1069 CHECK_false);
1070 if (!check_interface) {
1071 return false;
1072 }
1073 }
1074
1075 return true;
1076 }
1077
1078 InstanceKlass* SystemDictionary::load_shared_class(InstanceKlass* ik,
1079 Handle class_loader,
1080 Handle protection_domain,
1081 const ClassFileStream *cfs,
1082 PackageEntry* pkg_entry,
1083 TRAPS) {
1084 assert(ik != nullptr, "sanity");
1085 assert(ik->in_aot_cache(), "sanity");
1086 assert(!ik->is_unshareable_info_restored(), "shared class can be restored only once");
1087 assert(AtomicAccess::add(&ik->_shared_class_load_count, 1) == 1, "shared class loaded more than once");
1088 Symbol* class_name = ik->name();
1089
1090 if (!is_shared_class_visible(class_name, ik, pkg_entry, class_loader)) {
1091 ik->set_shared_loading_failed();
1092 return nullptr;
1093 }
1094
1095 bool check = check_shared_class_super_types(ik, class_loader, CHECK_NULL);
1096 if (!check) {
1097 ik->set_shared_loading_failed();
1098 return nullptr;
1099 }
1100
1101 InstanceKlass* new_ik = nullptr;
1102 // CFLH check is skipped for VM hidden classes (see KlassFactory::create_from_stream).
1103 // It will be skipped for shared VM hidden lambda proxy classes.
1104 if (!ik->is_hidden()) {
1105 new_ik = KlassFactory::check_shared_class_file_load_hook(
1106 ik, class_name, class_loader, protection_domain, cfs, CHECK_NULL);
1107 }
1108 if (new_ik != nullptr) {
1109 // The class is changed by CFLH. Return the new class. The shared class is
1110 // not used.
1111 return new_ik;
1112 }
1113
1114 // Adjust methods to recover missing data. They need addresses for
1115 // interpreter entry points and their default native method address
1116 // must be reset.
1117
1118 // Shared classes are all currently loaded by either the bootstrap or
1119 // internal parallel class loaders, so this will never cause a deadlock
1120 // on a custom class loader lock.
1121 // Since this class is already locked with parallel capable class
1122 // loaders, including the bootstrap loader via the placeholder table,
1123 // this lock is currently a nop.
1124
1125 ClassLoaderData* loader_data = class_loader_data(class_loader);
1126 {
1127 HandleMark hm(THREAD);
1128 Handle lockObject = get_loader_lock_or_null(class_loader);
1129 ObjectLocker ol(lockObject, THREAD);
1130 // prohibited package check assumes all classes loaded from archive call
1131 // restore_unshareable_info which calls ik->set_package()
1132 ik->restore_unshareable_info(loader_data, protection_domain, pkg_entry, CHECK_NULL);
1133 }
1134
1135 load_shared_class_misc(ik, loader_data);
1136 return ik;
1137 }
1138
1139 void SystemDictionary::load_shared_class_misc(InstanceKlass* ik, ClassLoaderData* loader_data) {
1140 ik->print_class_load_logging(loader_data, nullptr, nullptr);
1141
1142 // For boot loader, ensure that GetSystemPackage knows that a class in this
1143 // package was loaded.
1144 if (loader_data->is_the_null_class_loader_data()) {
1145 s2 path_index = ik->shared_classpath_index();
1146 ik->set_classpath_index(path_index);
1147 }
1148
1149 // notify a class loaded from shared object
1150 ClassLoadingService::notify_class_loaded(ik, true /* shared class */);
1151
1152 if (CDSConfig::is_dumping_final_static_archive()) {
1153 SystemDictionaryShared::init_dumptime_info_from_preimage(ik);
1154 }
1155 }
1690 }
1691 }
1692
1693 // Update class loader data dictionary - done after check_constraint and add_to_hierarchy
1694 // have been called.
1695 void SystemDictionary::update_dictionary(JavaThread* current,
1696 InstanceKlass* k,
1697 ClassLoaderData* loader_data) {
1698 MonitorLocker mu1(SystemDictionary_lock);
1699
1700 // Make a new dictionary entry.
1701 Symbol* name = k->name();
1702 Dictionary* dictionary = loader_data->dictionary();
1703 InstanceKlass* sd_check = dictionary->find_class(current, name);
1704 if (sd_check == nullptr) {
1705 dictionary->add_klass(current, name, k);
1706 }
1707 mu1.notify_all();
1708 }
1709
1710 #if INCLUDE_CDS
1711 // Indicate that loader_data has initiated the loading of class k, which
1712 // has already been defined by a parent loader.
1713 // This API should be used only by AOTLinkedClassBulkLoader
1714 void SystemDictionary::add_to_initiating_loader(JavaThread* current,
1715 InstanceKlass* k,
1716 ClassLoaderData* loader_data) {
1717 assert(CDSConfig::is_using_aot_linked_classes(), "must be");
1718 assert_locked_or_safepoint(SystemDictionary_lock);
1719 Symbol* name = k->name();
1720 Dictionary* dictionary = loader_data->dictionary();
1721 assert(k->is_loaded(), "must be");
1722 assert(k->class_loader_data() != loader_data, "only for classes defined by a parent loader");
1723 assert(dictionary->find_class(current, name) == nullptr, "sanity");
1724 dictionary->add_klass(current, name, k);
1725 }
1726 #endif
1727
1728 // Try to find a class name using the loader constraints. The
1729 // loader constraints might know about a class that isn't fully loaded
1730 // yet and these will be ignored.
1731 Klass* SystemDictionary::find_constrained_instance_or_array_klass(
1732 Thread* current, Symbol* class_name, Handle class_loader) {
1733
1734 // First see if it has been loaded directly.
1735 Klass* klass = find_instance_or_array_klass(current, class_name, class_loader);
1736 if (klass != nullptr)
1737 return klass;
1738
1739 // Now look to see if it has been loaded elsewhere, and is subject to
1740 // a loader constraint that would require this loader to return the
1741 // klass that is already loaded.
1742 if (Signature::is_array(class_name)) {
1743 // For array classes, their Klass*s are not kept in the
1744 // constraint table. The element Klass*s are.
1745 SignatureStream ss(class_name, false);
1746 int ndims = ss.skip_array_prefix(); // skip all '['s
|
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/fieldStreams.inline.hpp"
58 #include "oops/inlineKlass.inline.hpp"
59 #include "oops/instanceKlass.hpp"
60 #include "oops/klass.inline.hpp"
61 #include "oops/method.inline.hpp"
62 #include "oops/objArrayKlass.hpp"
63 #include "oops/objArrayOop.inline.hpp"
64 #include "oops/oop.inline.hpp"
65 #include "oops/oopHandle.inline.hpp"
66 #include "oops/symbol.hpp"
67 #include "oops/typeArrayKlass.hpp"
68 #include "prims/jvmtiExport.hpp"
69 #include "prims/methodHandles.hpp"
70 #include "runtime/arguments.hpp"
71 #include "runtime/atomicAccess.hpp"
72 #include "runtime/handles.inline.hpp"
73 #include "runtime/java.hpp"
74 #include "runtime/javaCalls.hpp"
75 #include "runtime/mutexLocker.hpp"
76 #include "runtime/os.hpp"
77 #include "runtime/sharedRuntime.hpp"
78 #include "runtime/signature.hpp"
79 #include "runtime/synchronizer.hpp"
80 #include "services/classLoadingService.hpp"
81 #include "services/diagnosticCommand.hpp"
82 #include "services/finalizerService.hpp"
83 #include "services/threadService.hpp"
84 #include "utilities/growableArray.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;
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(),
182 CHECK_NULL);
183 return result.get_oop();
184 }
185
186 // Helper function
187 inline ClassLoaderData* class_loader_data(Handle class_loader) {
188 return ClassLoaderData::class_loader_data(class_loader());
189 }
190
191 // These migrated value classes are loaded by the bootstrap class loader but are added to the initiating
192 // loaders automatically so that fields of these types can be found and potentially flattened during
193 // field layout.
194 static void add_migrated_value_classes(ClassLoaderData* cld) {
195 JavaThread* current = JavaThread::current();
196 auto add_klass = [&] (Symbol* classname) {
197 InstanceKlass* ik = SystemDictionary::find_instance_klass(current, classname, Handle(current, nullptr));
198 assert(ik != nullptr, "Must exist");
199 SystemDictionary::add_to_initiating_loader(current, ik, cld);
200 };
201
202 MonitorLocker mu1(SystemDictionary_lock);
203 vmSymbols::migrated_class_names_do(add_klass);
204 }
205
206 ClassLoaderData* SystemDictionary::register_loader(Handle class_loader, bool create_mirror_cld) {
207 if (create_mirror_cld) {
208 // Add a new class loader data to the graph.
209 return ClassLoaderDataGraph::add(class_loader, true);
210 } else {
211 if (class_loader() == nullptr) {
212 return ClassLoaderData::the_null_class_loader_data();
213 } else {
214 ClassLoaderData* cld = ClassLoaderDataGraph::find_or_create(class_loader);
215 if (Arguments::enable_preview() && EnableValhalla) {
216 add_migrated_value_classes(cld);
217 }
218 return cld;
219 }
220 }
221 }
222
223 void SystemDictionary::set_system_loader(ClassLoaderData *cld) {
224 if (_java_system_loader.is_empty()) {
225 _java_system_loader = cld->class_loader_handle();
226 } else {
227 assert(_java_system_loader.resolve() == cld->class_loader(), "sanity");
228 }
229 }
230
231 void SystemDictionary::set_platform_loader(ClassLoaderData *cld) {
232 if (_java_platform_loader.is_empty()) {
233 _java_platform_loader = cld->class_loader_handle();
234 } else {
235 assert(_java_platform_loader.resolve() == cld->class_loader(), "sanity");
236 }
237 }
238
239 // ----------------------------------------------------------------------------
407 }
408 } else {
409 k = Universe::typeArrayKlass(t);
410 k = k->array_klass(ndims, CHECK_NULL);
411 }
412 return k;
413 }
414
415 static inline void log_circularity_error(Symbol* name, PlaceholderEntry* probe) {
416 LogTarget(Debug, class, load, placeholders) lt;
417 if (lt.is_enabled()) {
418 ResourceMark rm;
419 LogStream ls(lt);
420 ls.print("ClassCircularityError detected for placeholder entry %s", name->as_C_string());
421 probe->print_on(&ls);
422 ls.cr();
423 }
424 }
425
426 // Must be called for any superclass or superinterface resolution
427 // during class definition, or may be called for inline field layout processing
428 // to detect class circularity errors.
429 // superinterface callers:
430 // parse_interfaces - from defineClass
431 // superclass callers:
432 // ClassFileParser - from defineClass
433 // load_shared_class - while loading a class from shared archive
434 // resolve_instance_class_or_null:
435 // via: handle_parallel_super_load
436 // when resolving a class that has an existing placeholder with
437 // a saved superclass [i.e. a defineClass is currently in progress]
438 // If another thread is trying to resolve the class, it must do
439 // superclass checks on its own thread to catch class circularity and
440 // to avoid deadlock.
441 // inline field layout callers:
442 // The field's class must be loaded to determine layout.
443 //
444 // resolve_with_circularity_detection adds a DETECT_CIRCULARITY placeholder to the placeholder table before calling
445 // resolve_instance_class_or_null. ClassCircularityError is detected when a DETECT_CIRCULARITY or LOAD_INSTANCE
446 // placeholder for the same thread, class, and classloader is found.
447 // This can be seen with logging option: -Xlog:class+load+placeholders=debug.
448 //
449 InstanceKlass* SystemDictionary::resolve_with_circularity_detection(Symbol* class_name,
450 Symbol* next_name,
451 Handle class_loader,
452 bool is_superclass,
453 TRAPS) {
454
455 assert(next_name != nullptr, "null superclass for resolving");
456 assert(!Signature::is_array(next_name), "invalid superclass name");
457
458 ClassLoaderData* loader_data = class_loader_data(class_loader);
459
460 if (is_superclass) {
461 InstanceKlass* klassk = loader_data->dictionary()->find_class(THREAD, class_name);
462 if (klassk != nullptr) {
463 // We can come here for two reasons:
464 // (a) RedefineClasses -- the class is already loaded
465 // (b) Rarely, the class might have been loaded by a parallel thread
466 // We can do a quick check against the already assigned superclass's name and loader.
467 InstanceKlass* superk = klassk->super();
468 if (superk != nullptr &&
469 superk->name() == next_name &&
470 superk->class_loader() == class_loader()) {
471 return superk;
472 }
473 }
474 }
475
476 // can't throw error holding a lock
477 bool throw_circularity_error = false;
478 {
479 MutexLocker mu(THREAD, SystemDictionary_lock);
480
481 // Must check ClassCircularity before resolving next_name (superclass, interface, field types or speculatively preloaded argument types).
482 PlaceholderEntry* probe = PlaceholderTable::get_entry(class_name, loader_data);
483 if (probe != nullptr && probe->check_seen_thread(THREAD, PlaceholderTable::DETECT_CIRCULARITY)) {
484 log_circularity_error(class_name, probe);
485 throw_circularity_error = true;
486 }
487
488 // Make sure there's a placeholder for the class_name before resolving.
489 // This is used as a claim that this thread is currently loading superclass/classloader
490 // and for ClassCircularity checks.
491 if (!throw_circularity_error) {
492 // Be careful not to exit resolve_with_circularity_detection without removing this placeholder.
493 PlaceholderEntry* newprobe = PlaceholderTable::find_and_add(class_name,
494 loader_data,
495 PlaceholderTable::DETECT_CIRCULARITY,
496 next_name, THREAD);
497 }
498 }
499
500 if (throw_circularity_error) {
501 ResourceMark rm(THREAD);
502 THROW_MSG_NULL(vmSymbols::java_lang_ClassCircularityError(), class_name->as_C_string());
503 }
504
505 // Resolve the superclass, superinterface, field type or speculatively preloaded argument types and check results on return.
506 InstanceKlass* superk =
507 SystemDictionary::resolve_instance_class_or_null(next_name,
508 class_loader,
509 THREAD);
510
511 // Clean up placeholder entry.
512 {
513 MutexLocker mu(THREAD, SystemDictionary_lock);
514 PlaceholderTable::find_and_remove(class_name, loader_data, PlaceholderTable::DETECT_CIRCULARITY, THREAD);
515 SystemDictionary_lock->notify_all();
516 }
517
518 // Check for pending exception or null superk, and throw exception
519 if (HAS_PENDING_EXCEPTION || superk == nullptr) {
520 handle_resolution_exception(next_name, true, CHECK_NULL);
521 }
522
523 return superk;
524 }
525
931
932 InstanceKlass* SystemDictionary::resolve_from_stream(ClassFileStream* st,
933 Symbol* class_name,
934 Handle class_loader,
935 const ClassLoadInfo& cl_info,
936 TRAPS) {
937 if (cl_info.is_hidden()) {
938 return resolve_hidden_class_from_stream(st, class_name, class_loader, cl_info, CHECK_NULL);
939 } else {
940 return resolve_class_from_stream(st, class_name, class_loader, cl_info, CHECK_NULL);
941 }
942 }
943
944
945 #if INCLUDE_CDS
946 // Check if a shared class can be loaded by the specific classloader.
947 bool SystemDictionary::is_shared_class_visible(Symbol* class_name,
948 InstanceKlass* ik,
949 PackageEntry* pkg_entry,
950 Handle class_loader) {
951 assert(!CDSConfig::module_patching_disables_cds(), "Cannot use CDS");
952
953 // (1) Check if we are loading into the same loader as in dump time.
954
955 if (ik->defined_by_boot_loader()) {
956 if (class_loader() != nullptr) {
957 return false;
958 }
959 } else if (ik->defined_by_platform_loader()) {
960 if (class_loader() != java_platform_loader()) {
961 return false;
962 }
963 } else if (ik->defined_by_app_loader()) {
964 if (class_loader() != java_system_loader()) {
965 return false;
966 }
967 } else {
968 // ik was loaded by a custom loader during dump time
969 if (class_loader_data(class_loader)->is_builtin_class_loader_data()) {
970 return false;
971 } else {
1007
1008 if (pkg_entry == nullptr) {
1009 // We might have looked up pkg_entry before the module system was initialized.
1010 // Need to reload it now.
1011 TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);
1012 if (pkg_name != nullptr) {
1013 pkg_entry = class_loader_data(class_loader)->packages()->lookup_only(pkg_name);
1014 }
1015 }
1016
1017 ModuleEntry* mod_entry = (pkg_entry == nullptr) ? nullptr : pkg_entry->module();
1018 bool should_be_in_named_module = (mod_entry != nullptr && mod_entry->is_named());
1019 bool was_archived_from_named_module = !cl->has_unnamed_module();
1020 bool visible;
1021
1022 if (was_archived_from_named_module) {
1023 if (should_be_in_named_module) {
1024 // Is the module loaded from the same location as during dump time?
1025 visible = mod_entry->shared_path_index() == scp_index;
1026 if (visible) {
1027 assert(!CDSConfig::module_patching_disables_cds(), "Cannot use CDS");
1028 }
1029 } else {
1030 // During dump time, this class was in a named module, but at run time, this class should be
1031 // in an unnamed module.
1032 visible = false;
1033 }
1034 } else {
1035 if (should_be_in_named_module) {
1036 // During dump time, this class was in an unnamed, but at run time, this class should be
1037 // in a named module.
1038 visible = false;
1039 } else {
1040 visible = true;
1041 }
1042 }
1043
1044 return visible;
1045 }
1046
1047 bool SystemDictionary::check_shared_class_super_type(InstanceKlass* klass, InstanceKlass* super_type,
1085 class_loader, true,
1086 CHECK_false);
1087 if (!check_super) {
1088 return false;
1089 }
1090 }
1091
1092 Array<InstanceKlass*>* interfaces = ik->local_interfaces();
1093 int num_interfaces = interfaces->length();
1094 for (int index = 0; index < num_interfaces; index++) {
1095 bool check_interface = check_shared_class_super_type(ik, interfaces->at(index), class_loader, false,
1096 CHECK_false);
1097 if (!check_interface) {
1098 return false;
1099 }
1100 }
1101
1102 return true;
1103 }
1104
1105 // Pre-load class referred to in non-static null-free instance field. These fields trigger MANDATORY loading.
1106 // Some pre-loading does not fail fatally
1107 bool SystemDictionary::preload_from_null_free_field(InstanceKlass* ik, Handle class_loader, Symbol* sig, int field_index, TRAPS) {
1108 TempNewSymbol name = Signature::strip_envelope(sig);
1109 log_info(class, preload)("Preloading of class %s during loading of shared class %s. "
1110 "Cause: a null-free non-static field is declared with this type",
1111 name->as_C_string(), ik->name()->as_C_string());
1112 InstanceKlass* real_k = SystemDictionary::resolve_with_circularity_detection(ik->name(), name,
1113 class_loader, false, CHECK_false);
1114 if (HAS_PENDING_EXCEPTION) {
1115 log_info(class, preload)("Preloading of class %s during loading of class %s "
1116 "(cause: null-free non-static field) failed: %s",
1117 name->as_C_string(), ik->name()->as_C_string(),
1118 PENDING_EXCEPTION->klass()->name()->as_C_string());
1119 return false; // Exception is still pending
1120 }
1121
1122 InstanceKlass* k = ik->get_inline_type_field_klass_or_null(field_index);
1123 if (real_k != k) {
1124 // oops, the app has substituted a different version of k! Does not fail fatally
1125 log_info(class, preload)("Preloading of class %s during loading of shared class %s "
1126 "(cause: null-free non-static field) failed : "
1127 "app substituted a different version of %s",
1128 name->as_C_string(), ik->name()->as_C_string(),
1129 name->as_C_string());
1130 return false;
1131 }
1132 log_info(class, preload)("Preloading of class %s during loading of shared class %s "
1133 "(cause: null-free non-static field) succeeded",
1134 name->as_C_string(), ik->name()->as_C_string());
1135
1136 assert(real_k != nullptr, "Sanity check");
1137 InstanceKlass::check_can_be_annotated_with_NullRestricted(real_k, ik->name(), CHECK_false);
1138
1139 return true;
1140 }
1141
1142 // Tries to pre-load classes referred to in non-static nullable instance fields if they are found in the
1143 // loadable descriptors attribute. If loading fails, we can fail silently.
1144 void SystemDictionary::try_preload_from_loadable_descriptors(InstanceKlass* ik, Handle class_loader, Symbol* sig, int field_index, TRAPS) {
1145 TempNewSymbol name = Signature::strip_envelope(sig);
1146 if (name != ik->name() && ik->is_class_in_loadable_descriptors_attribute(sig)) {
1147 log_info(class, preload)("Preloading of class %s during loading of shared class %s. "
1148 "Cause: field type in LoadableDescriptors attribute",
1149 name->as_C_string(), ik->name()->as_C_string());
1150 InstanceKlass* real_k = SystemDictionary::resolve_with_circularity_detection(ik->name(), name,
1151 class_loader, false, THREAD);
1152 if (HAS_PENDING_EXCEPTION) {
1153 CLEAR_PENDING_EXCEPTION;
1154 }
1155
1156 InstanceKlass* k = ik->get_inline_type_field_klass_or_null(field_index);
1157 if (real_k != k) {
1158 // oops, the app has substituted a different version of k!
1159 log_info(class, preload)("Preloading of class %s during loading of shared class %s "
1160 "(cause: field type in LoadableDescriptors attribute) failed : "
1161 "app substituted a different version of %s",
1162 name->as_C_string(), ik->name()->as_C_string(),
1163 k->name()->as_C_string());
1164 return;
1165 } else if (real_k != nullptr) {
1166 log_info(class, preload)("Preloading of class %s during loading of shared class %s "
1167 "(cause: field type in LoadableDescriptors attribute) succeeded",
1168 name->as_C_string(), ik->name()->as_C_string());
1169 }
1170 }
1171 }
1172
1173
1174 InstanceKlass* SystemDictionary::load_shared_class(InstanceKlass* ik,
1175 Handle class_loader,
1176 Handle protection_domain,
1177 const ClassFileStream *cfs,
1178 PackageEntry* pkg_entry,
1179 TRAPS) {
1180 assert(ik != nullptr, "sanity");
1181 assert(ik->in_aot_cache(), "sanity");
1182 assert(!ik->is_unshareable_info_restored(), "shared class can be restored only once");
1183 assert(AtomicAccess::add(&ik->_shared_class_load_count, 1) == 1, "shared class loaded more than once");
1184 Symbol* class_name = ik->name();
1185
1186 if (!is_shared_class_visible(class_name, ik, pkg_entry, class_loader)) {
1187 ik->set_shared_loading_failed();
1188 return nullptr;
1189 }
1190
1191 bool check = check_shared_class_super_types(ik, class_loader, CHECK_NULL);
1192 if (!check) {
1193 ik->set_shared_loading_failed();
1194 return nullptr;
1195 }
1196
1197 if (ik->has_inline_type_fields()) {
1198 for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
1199 if (fs.access_flags().is_static()) continue;
1200
1201 Symbol* sig = fs.signature();
1202 int field_index = fs.index();
1203
1204 if (fs.is_null_free_inline_type()) {
1205 // A false return means that the class didn't load for other reasons than an exception.
1206 bool check = preload_from_null_free_field(ik, class_loader, sig, field_index, CHECK_NULL);
1207 if (!check) {
1208 ik->set_shared_loading_failed();
1209 return nullptr;
1210 }
1211 } else if (Signature::has_envelope(sig)) {
1212 // Pending exceptions are cleared so we can fail silently
1213 try_preload_from_loadable_descriptors(ik, class_loader, sig, field_index, CHECK_NULL);
1214 }
1215 }
1216 }
1217
1218 InstanceKlass* new_ik = nullptr;
1219 // CFLH check is skipped for VM hidden classes (see KlassFactory::create_from_stream).
1220 // It will be skipped for shared VM hidden lambda proxy classes.
1221 if (!ik->is_hidden()) {
1222 new_ik = KlassFactory::check_shared_class_file_load_hook(
1223 ik, class_name, class_loader, protection_domain, cfs, CHECK_NULL);
1224 }
1225 if (new_ik != nullptr) {
1226 // The class is changed by CFLH. Return the new class. The shared class is
1227 // not used.
1228 return new_ik;
1229 }
1230
1231 // Adjust methods to recover missing data. They need addresses for
1232 // interpreter entry points and their default native method address
1233 // must be reset.
1234
1235 // Shared classes are all currently loaded by either the bootstrap or
1236 // internal parallel class loaders, so this will never cause a deadlock
1237 // on a custom class loader lock.
1238 // Since this class is already locked with parallel capable class
1239 // loaders, including the bootstrap loader via the placeholder table,
1240 // this lock is currently a nop.
1241
1242 ClassLoaderData* loader_data = class_loader_data(class_loader);
1243 {
1244 HandleMark hm(THREAD);
1245 Handle lockObject = get_loader_lock_or_null(class_loader);
1246 ObjectLocker ol(lockObject, THREAD);
1247 // prohibited package check assumes all classes loaded from archive call
1248 // restore_unshareable_info which calls ik->set_package()
1249 ik->restore_unshareable_info(loader_data, protection_domain, pkg_entry, CHECK_NULL);
1250 }
1251
1252 load_shared_class_misc(ik, loader_data);
1253
1254 return ik;
1255 }
1256
1257 void SystemDictionary::load_shared_class_misc(InstanceKlass* ik, ClassLoaderData* loader_data) {
1258 ik->print_class_load_logging(loader_data, nullptr, nullptr);
1259
1260 // For boot loader, ensure that GetSystemPackage knows that a class in this
1261 // package was loaded.
1262 if (loader_data->is_the_null_class_loader_data()) {
1263 s2 path_index = ik->shared_classpath_index();
1264 ik->set_classpath_index(path_index);
1265 }
1266
1267 // notify a class loaded from shared object
1268 ClassLoadingService::notify_class_loaded(ik, true /* shared class */);
1269
1270 if (CDSConfig::is_dumping_final_static_archive()) {
1271 SystemDictionaryShared::init_dumptime_info_from_preimage(ik);
1272 }
1273 }
1808 }
1809 }
1810
1811 // Update class loader data dictionary - done after check_constraint and add_to_hierarchy
1812 // have been called.
1813 void SystemDictionary::update_dictionary(JavaThread* current,
1814 InstanceKlass* k,
1815 ClassLoaderData* loader_data) {
1816 MonitorLocker mu1(SystemDictionary_lock);
1817
1818 // Make a new dictionary entry.
1819 Symbol* name = k->name();
1820 Dictionary* dictionary = loader_data->dictionary();
1821 InstanceKlass* sd_check = dictionary->find_class(current, name);
1822 if (sd_check == nullptr) {
1823 dictionary->add_klass(current, name, k);
1824 }
1825 mu1.notify_all();
1826 }
1827
1828 // Indicate that loader_data has initiated the loading of class k, which
1829 // has already been defined by a parent loader.
1830 // This API is used by AOTLinkedClassBulkLoader and to register boxing
1831 // classes from java.lang in all class loaders to enable more value
1832 // classes optimizations
1833 void SystemDictionary::add_to_initiating_loader(JavaThread* current,
1834 InstanceKlass* k,
1835 ClassLoaderData* loader_data) {
1836 assert_locked_or_safepoint(SystemDictionary_lock);
1837 Symbol* name = k->name();
1838 Dictionary* dictionary = loader_data->dictionary();
1839 assert(k->is_loaded(), "must be");
1840 assert(k->class_loader_data() != loader_data, "only for classes defined by a parent loader");
1841 if (dictionary->find_class(current, name) == nullptr) {
1842 dictionary->add_klass(current, name, k);
1843 }
1844 }
1845
1846 // Try to find a class name using the loader constraints. The
1847 // loader constraints might know about a class that isn't fully loaded
1848 // yet and these will be ignored.
1849 Klass* SystemDictionary::find_constrained_instance_or_array_klass(
1850 Thread* current, Symbol* class_name, Handle class_loader) {
1851
1852 // First see if it has been loaded directly.
1853 Klass* klass = find_instance_or_array_klass(current, class_name, class_loader);
1854 if (klass != nullptr)
1855 return klass;
1856
1857 // Now look to see if it has been loaded elsewhere, and is subject to
1858 // a loader constraint that would require this loader to return the
1859 // klass that is already loaded.
1860 if (Signature::is_array(class_name)) {
1861 // For array classes, their Klass*s are not kept in the
1862 // constraint table. The element Klass*s are.
1863 SignatureStream ss(class_name, false);
1864 int ndims = ss.skip_array_prefix(); // skip all '['s
|