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