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 bool Bug8370217_FIXED = false;
215 ClassLoaderData* cld = ClassLoaderDataGraph::find_or_create(class_loader);
216 if (EnableValhalla && Bug8370217_FIXED) {
217 add_migrated_value_classes(cld);
218 }
219 return cld;
220 }
221 }
222 }
223
224 void SystemDictionary::set_system_loader(ClassLoaderData *cld) {
225 if (_java_system_loader.is_empty()) {
226 _java_system_loader = cld->class_loader_handle();
227 } else {
228 assert(_java_system_loader.resolve() == cld->class_loader(), "sanity");
229 }
230 }
231
232 void SystemDictionary::set_platform_loader(ClassLoaderData *cld) {
233 if (_java_platform_loader.is_empty()) {
234 _java_platform_loader = cld->class_loader_handle();
235 } else {
236 assert(_java_platform_loader.resolve() == cld->class_loader(), "sanity");
237 }
238 }
239
240 // ----------------------------------------------------------------------------
408 }
409 } else {
410 k = Universe::typeArrayKlass(t);
411 k = k->array_klass(ndims, CHECK_NULL);
412 }
413 return k;
414 }
415
416 static inline void log_circularity_error(Symbol* name, PlaceholderEntry* probe) {
417 LogTarget(Debug, class, load, placeholders) lt;
418 if (lt.is_enabled()) {
419 ResourceMark rm;
420 LogStream ls(lt);
421 ls.print("ClassCircularityError detected for placeholder entry %s", name->as_C_string());
422 probe->print_on(&ls);
423 ls.cr();
424 }
425 }
426
427 // Must be called for any superclass or superinterface resolution
428 // during class definition, or may be called for inline field layout processing
429 // to detect class circularity errors.
430 // superinterface callers:
431 // parse_interfaces - from defineClass
432 // superclass callers:
433 // ClassFileParser - from defineClass
434 // load_shared_class - while loading a class from shared archive
435 // resolve_instance_class_or_null:
436 // via: handle_parallel_super_load
437 // when resolving a class that has an existing placeholder with
438 // a saved superclass [i.e. a defineClass is currently in progress]
439 // If another thread is trying to resolve the class, it must do
440 // superclass checks on its own thread to catch class circularity and
441 // to avoid deadlock.
442 // inline field layout callers:
443 // The field's class must be loaded to determine layout.
444 //
445 // resolve_with_circularity_detection adds a DETECT_CIRCULARITY placeholder to the placeholder table before calling
446 // resolve_instance_class_or_null. ClassCircularityError is detected when a DETECT_CIRCULARITY or LOAD_INSTANCE
447 // placeholder for the same thread, class, and classloader is found.
448 // This can be seen with logging option: -Xlog:class+load+placeholders=debug.
449 //
450 InstanceKlass* SystemDictionary::resolve_with_circularity_detection(Symbol* class_name,
451 Symbol* next_name,
452 Handle class_loader,
453 bool is_superclass,
454 TRAPS) {
455
456 assert(next_name != nullptr, "null superclass for resolving");
457 assert(!Signature::is_array(next_name), "invalid superclass name");
458
459 ClassLoaderData* loader_data = class_loader_data(class_loader);
460
461 if (is_superclass) {
462 InstanceKlass* klassk = loader_data->dictionary()->find_class(THREAD, class_name);
463 if (klassk != nullptr) {
464 // We can come here for two reasons:
465 // (a) RedefineClasses -- the class is already loaded
466 // (b) Rarely, the class might have been loaded by a parallel thread
467 // We can do a quick check against the already assigned superclass's name and loader.
468 InstanceKlass* superk = klassk->super();
469 if (superk != nullptr &&
470 superk->name() == next_name &&
471 superk->class_loader() == class_loader()) {
472 return superk;
473 }
474 }
475 }
476
477 // can't throw error holding a lock
478 bool throw_circularity_error = false;
479 {
480 MutexLocker mu(THREAD, SystemDictionary_lock);
481
482 // Must check ClassCircularity before resolving next_name (superclass, interface, field types or speculatively preloaded argument types).
483 PlaceholderEntry* probe = PlaceholderTable::get_entry(class_name, loader_data);
484 if (probe != nullptr && probe->check_seen_thread(THREAD, PlaceholderTable::DETECT_CIRCULARITY)) {
485 log_circularity_error(class_name, probe);
486 throw_circularity_error = true;
487 }
488
489 // Make sure there's a placeholder for the class_name before resolving.
490 // This is used as a claim that this thread is currently loading superclass/classloader
491 // and for ClassCircularity checks.
492 if (!throw_circularity_error) {
493 // Be careful not to exit resolve_with_circularity_detection without removing this placeholder.
494 PlaceholderEntry* newprobe = PlaceholderTable::find_and_add(class_name,
495 loader_data,
496 PlaceholderTable::DETECT_CIRCULARITY,
497 next_name, THREAD);
498 }
499 }
500
501 if (throw_circularity_error) {
502 ResourceMark rm(THREAD);
503 THROW_MSG_NULL(vmSymbols::java_lang_ClassCircularityError(), class_name->as_C_string());
504 }
505
506 // Resolve the superclass, superinterface, field type or speculatively preloaded argument types and check results on return.
507 InstanceKlass* superk =
508 SystemDictionary::resolve_instance_class_or_null(next_name,
509 class_loader,
510 THREAD);
511
512 // Clean up placeholder entry.
513 {
514 MutexLocker mu(THREAD, SystemDictionary_lock);
515 PlaceholderTable::find_and_remove(class_name, loader_data, PlaceholderTable::DETECT_CIRCULARITY, THREAD);
516 SystemDictionary_lock->notify_all();
517 }
518
519 // Check for pending exception or null superk, and throw exception
520 if (HAS_PENDING_EXCEPTION || superk == nullptr) {
521 handle_resolution_exception(next_name, true, CHECK_NULL);
522 }
523
524 return superk;
525 }
526
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_warning(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_warning(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_warning(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 }
1809 }
1810 }
1811
1812 // Update class loader data dictionary - done after check_constraint and add_to_hierarchy
1813 // have been called.
1814 void SystemDictionary::update_dictionary(JavaThread* current,
1815 InstanceKlass* k,
1816 ClassLoaderData* loader_data) {
1817 MonitorLocker mu1(SystemDictionary_lock);
1818
1819 // Make a new dictionary entry.
1820 Symbol* name = k->name();
1821 Dictionary* dictionary = loader_data->dictionary();
1822 InstanceKlass* sd_check = dictionary->find_class(current, name);
1823 if (sd_check == nullptr) {
1824 dictionary->add_klass(current, name, k);
1825 }
1826 mu1.notify_all();
1827 }
1828
1829 // Indicate that loader_data has initiated the loading of class k, which
1830 // has already been defined by a parent loader.
1831 // This API is used by AOTLinkedClassBulkLoader and to register boxing
1832 // classes from java.lang in all class loaders to enable more value
1833 // classes optimizations
1834 void SystemDictionary::add_to_initiating_loader(JavaThread* current,
1835 InstanceKlass* k,
1836 ClassLoaderData* loader_data) {
1837 assert_locked_or_safepoint(SystemDictionary_lock);
1838 Symbol* name = k->name();
1839 Dictionary* dictionary = loader_data->dictionary();
1840 assert(k->is_loaded(), "must be");
1841 assert(k->class_loader_data() != loader_data, "only for classes defined by a parent loader");
1842 if (dictionary->find_class(current, name) == nullptr) {
1843 dictionary->add_klass(current, name, k);
1844 }
1845 }
1846
1847 // Try to find a class name using the loader constraints. The
1848 // loader constraints might know about a class that isn't fully loaded
1849 // yet and these will be ignored.
1850 Klass* SystemDictionary::find_constrained_instance_or_array_klass(
1851 Thread* current, Symbol* class_name, Handle class_loader) {
1852
1853 // First see if it has been loaded directly.
1854 Klass* klass = find_instance_or_array_klass(current, class_name, class_loader);
1855 if (klass != nullptr)
1856 return klass;
1857
1858 // Now look to see if it has been loaded elsewhere, and is subject to
1859 // a loader constraint that would require this loader to return the
1860 // klass that is already loaded.
1861 if (Signature::is_array(class_name)) {
1862 // For array classes, their Klass*s are not kept in the
1863 // constraint table. The element Klass*s are.
1864 SignatureStream ss(class_name, false);
1865 int ndims = ss.skip_array_prefix(); // skip all '['s
|