38 #include "classfile/loaderConstraints.hpp"
39 #include "classfile/packageEntry.hpp"
40 #include "classfile/placeholders.hpp"
41 #include "classfile/resolutionErrors.hpp"
42 #include "classfile/stringTable.hpp"
43 #include "classfile/symbolTable.hpp"
44 #include "classfile/systemDictionary.hpp"
45 #include "classfile/vmClasses.hpp"
46 #include "classfile/vmSymbols.hpp"
47 #include "gc/shared/gcTraceTime.inline.hpp"
48 #include "interpreter/bootstrapInfo.hpp"
49 #include "jfr/jfrEvents.hpp"
50 #include "jvm.h"
51 #include "logging/log.hpp"
52 #include "logging/logStream.hpp"
53 #include "memory/metaspaceClosure.hpp"
54 #include "memory/oopFactory.hpp"
55 #include "memory/resourceArea.hpp"
56 #include "memory/universe.hpp"
57 #include "oops/access.inline.hpp"
58 #include "oops/instanceKlass.hpp"
59 #include "oops/klass.inline.hpp"
60 #include "oops/method.inline.hpp"
61 #include "oops/objArrayKlass.hpp"
62 #include "oops/objArrayOop.inline.hpp"
63 #include "oops/oop.inline.hpp"
64 #include "oops/oop.hpp"
65 #include "oops/oopHandle.hpp"
66 #include "oops/oopHandle.inline.hpp"
67 #include "oops/symbol.hpp"
68 #include "oops/typeArrayKlass.hpp"
69 #include "prims/jvmtiExport.hpp"
70 #include "prims/methodHandles.hpp"
71 #include "runtime/arguments.hpp"
72 #include "runtime/atomic.hpp"
73 #include "runtime/handles.inline.hpp"
74 #include "runtime/java.hpp"
75 #include "runtime/javaCalls.hpp"
76 #include "runtime/mutexLocker.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 ClassLoaderData* SystemDictionary::register_loader(Handle class_loader, bool create_mirror_cld) {
192 if (create_mirror_cld) {
193 // Add a new class loader data to the graph.
194 return ClassLoaderDataGraph::add(class_loader, true);
195 } else {
196 return (class_loader() == nullptr) ? ClassLoaderData::the_null_class_loader_data() :
197 ClassLoaderDataGraph::find_or_create(class_loader);
198 }
199 }
200
201 void SystemDictionary::set_system_loader(ClassLoaderData *cld) {
202 assert(_java_system_loader.is_empty(), "already set!");
203 _java_system_loader = cld->class_loader_handle();
204
205 }
206
207 void SystemDictionary::set_platform_loader(ClassLoaderData *cld) {
208 assert(_java_platform_loader.is_empty(), "already set!");
209 _java_platform_loader = cld->class_loader_handle();
210 }
211
212 // ----------------------------------------------------------------------------
213 // Parallel class loading check
214
215 static bool is_parallelCapable(Handle class_loader) {
216 if (class_loader.is_null()) return true;
217 return java_lang_ClassLoader::parallelCapable(class_loader());
332
333 Klass* SystemDictionary::resolve_or_fail(Symbol* class_name, Handle class_loader,
334 bool throw_error, TRAPS) {
335 Klass* klass = resolve_or_null(class_name, class_loader, THREAD);
336 // Check for pending exception or null klass, and throw exception
337 if (HAS_PENDING_EXCEPTION || klass == nullptr) {
338 handle_resolution_exception(class_name, throw_error, CHECK_NULL);
339 }
340 return klass;
341 }
342
343 // Forwards to resolve_array_class_or_null or resolve_instance_class_or_null
344
345 Klass* SystemDictionary::resolve_or_null(Symbol* class_name, Handle class_loader, TRAPS) {
346 if (Signature::is_array(class_name)) {
347 return resolve_array_class_or_null(class_name, class_loader, THREAD);
348 } else {
349 assert(class_name != nullptr && !Signature::is_array(class_name), "must be");
350 if (Signature::has_envelope(class_name)) {
351 ResourceMark rm(THREAD);
352 // Ignore wrapping L and ;.
353 TempNewSymbol name = SymbolTable::new_symbol(class_name->as_C_string() + 1,
354 class_name->utf8_length() - 2);
355 return resolve_instance_class_or_null(name, class_loader, THREAD);
356 } else {
357 return resolve_instance_class_or_null(class_name, class_loader, THREAD);
358 }
359 }
360 }
361
362 // Forwards to resolve_instance_class_or_null
363
364 Klass* SystemDictionary::resolve_array_class_or_null(Symbol* class_name,
365 Handle class_loader,
366 TRAPS) {
367 assert(Signature::is_array(class_name), "must be array");
368 ResourceMark rm(THREAD);
369 SignatureStream ss(class_name, false);
370 int ndims = ss.skip_array_prefix(); // skip all '['s
371 Klass* k = nullptr;
372 BasicType t = ss.type();
901
902 InstanceKlass* SystemDictionary::resolve_from_stream(ClassFileStream* st,
903 Symbol* class_name,
904 Handle class_loader,
905 const ClassLoadInfo& cl_info,
906 TRAPS) {
907 if (cl_info.is_hidden()) {
908 return resolve_hidden_class_from_stream(st, class_name, class_loader, cl_info, CHECK_NULL);
909 } else {
910 return resolve_class_from_stream(st, class_name, class_loader, cl_info, CHECK_NULL);
911 }
912 }
913
914
915 #if INCLUDE_CDS
916 // Check if a shared class can be loaded by the specific classloader.
917 bool SystemDictionary::is_shared_class_visible(Symbol* class_name,
918 InstanceKlass* ik,
919 PackageEntry* pkg_entry,
920 Handle class_loader) {
921 assert(!ModuleEntryTable::javabase_moduleEntry()->is_patched(),
922 "Cannot use sharing if java.base is patched");
923
924 // (1) Check if we are loading into the same loader as in dump time.
925
926 if (ik->defined_by_boot_loader()) {
927 if (class_loader() != nullptr) {
928 return false;
929 }
930 } else if (ik->defined_by_platform_loader()) {
931 if (class_loader() != java_platform_loader()) {
932 return false;
933 }
934 } else if (ik->defined_by_app_loader()) {
935 if (class_loader() != java_system_loader()) {
936 return false;
937 }
938 } else {
939 // ik was loaded by a custom loader during dump time
940 if (class_loader_data(class_loader)->is_builtin_class_loader_data()) {
941 return false;
942 } else {
978
979 if (pkg_entry == nullptr) {
980 // We might have looked up pkg_entry before the module system was initialized.
981 // Need to reload it now.
982 TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);
983 if (pkg_name != nullptr) {
984 pkg_entry = class_loader_data(class_loader)->packages()->lookup_only(pkg_name);
985 }
986 }
987
988 ModuleEntry* mod_entry = (pkg_entry == nullptr) ? nullptr : pkg_entry->module();
989 bool should_be_in_named_module = (mod_entry != nullptr && mod_entry->is_named());
990 bool was_archived_from_named_module = !cl->has_unnamed_module();
991 bool visible;
992
993 if (was_archived_from_named_module) {
994 if (should_be_in_named_module) {
995 // Is the module loaded from the same location as during dump time?
996 visible = mod_entry->shared_path_index() == scp_index;
997 if (visible) {
998 assert(!mod_entry->is_patched(), "cannot load archived classes for patched module");
999 }
1000 } else {
1001 // During dump time, this class was in a named module, but at run time, this class should be
1002 // in an unnamed module.
1003 visible = false;
1004 }
1005 } else {
1006 if (should_be_in_named_module) {
1007 // During dump time, this class was in an unnamed, but at run time, this class should be
1008 // in a named module.
1009 visible = false;
1010 } else {
1011 visible = true;
1012 }
1013 }
1014
1015 return visible;
1016 }
1017
1018 bool SystemDictionary::check_shared_class_super_type(InstanceKlass* klass, InstanceKlass* super_type,
1056 class_loader, true,
1057 CHECK_false);
1058 if (!check_super) {
1059 return false;
1060 }
1061 }
1062
1063 Array<InstanceKlass*>* interfaces = ik->local_interfaces();
1064 int num_interfaces = interfaces->length();
1065 for (int index = 0; index < num_interfaces; index++) {
1066 bool check_interface = check_shared_class_super_type(ik, interfaces->at(index), class_loader, false,
1067 CHECK_false);
1068 if (!check_interface) {
1069 return false;
1070 }
1071 }
1072
1073 return true;
1074 }
1075
1076 InstanceKlass* SystemDictionary::load_shared_class(InstanceKlass* ik,
1077 Handle class_loader,
1078 Handle protection_domain,
1079 const ClassFileStream *cfs,
1080 PackageEntry* pkg_entry,
1081 TRAPS) {
1082 assert(ik != nullptr, "sanity");
1083 assert(ik->is_shared(), "sanity");
1084 assert(!ik->is_unshareable_info_restored(), "shared class can be restored only once");
1085 assert(Atomic::add(&ik->_shared_class_load_count, 1) == 1, "shared class loaded more than once");
1086 Symbol* class_name = ik->name();
1087
1088 if (!is_shared_class_visible(class_name, ik, pkg_entry, class_loader)) {
1089 ik->set_shared_loading_failed();
1090 return nullptr;
1091 }
1092
1093 bool check = check_shared_class_super_types(ik, class_loader, CHECK_NULL);
1094 if (!check) {
1095 ik->set_shared_loading_failed();
1096 return nullptr;
1097 }
1098
1099 InstanceKlass* new_ik = nullptr;
1100 // CFLH check is skipped for VM hidden classes (see KlassFactory::create_from_stream).
1101 // It will be skipped for shared VM hidden lambda proxy classes.
1102 if (!ik->is_hidden()) {
1103 new_ik = KlassFactory::check_shared_class_file_load_hook(
1104 ik, class_name, class_loader, protection_domain, cfs, CHECK_NULL);
1105 }
1106 if (new_ik != nullptr) {
1107 // The class is changed by CFLH. Return the new class. The shared class is
1108 // not used.
1109 return new_ik;
1110 }
1111
1112 // Adjust methods to recover missing data. They need addresses for
1113 // interpreter entry points and their default native method address
1114 // must be reset.
1115
1116 // Shared classes are all currently loaded by either the bootstrap or
1117 // internal parallel class loaders, so this will never cause a deadlock
1118 // on a custom class loader lock.
1119 // Since this class is already locked with parallel capable class
1120 // loaders, including the bootstrap loader via the placeholder table,
1121 // this lock is currently a nop.
1122
1123 ClassLoaderData* loader_data = class_loader_data(class_loader);
1124 {
1125 HandleMark hm(THREAD);
1126 Handle lockObject = get_loader_lock_or_null(class_loader);
1127 ObjectLocker ol(lockObject, THREAD);
1128 // prohibited package check assumes all classes loaded from archive call
1129 // restore_unshareable_info which calls ik->set_package()
1130 ik->restore_unshareable_info(loader_data, protection_domain, pkg_entry, CHECK_NULL);
1131 }
1132
1133 load_shared_class_misc(ik, loader_data);
1134 return ik;
1135 }
1136
1137 void SystemDictionary::load_shared_class_misc(InstanceKlass* ik, ClassLoaderData* loader_data) {
1138 ik->print_class_load_logging(loader_data, nullptr, nullptr);
1139
1140 // For boot loader, ensure that GetSystemPackage knows that a class in this
1141 // package was loaded.
1142 if (loader_data->is_the_null_class_loader_data()) {
1143 s2 path_index = ik->shared_classpath_index();
1144 ik->set_classpath_index(path_index);
1145 }
1146
1147 // notify a class loaded from shared object
1148 ClassLoadingService::notify_class_loaded(ik, true /* shared class */);
1149
1150 if (CDSConfig::is_dumping_final_static_archive()) {
1151 SystemDictionaryShared::init_dumptime_info_from_preimage(ik);
1152 }
1153 }
1636 }
1637 }
1638
1639 // Update class loader data dictionary - done after check_constraint and add_to_hierarchy
1640 // have been called.
1641 void SystemDictionary::update_dictionary(JavaThread* current,
1642 InstanceKlass* k,
1643 ClassLoaderData* loader_data) {
1644 MonitorLocker mu1(SystemDictionary_lock);
1645
1646 // Make a new dictionary entry.
1647 Symbol* name = k->name();
1648 Dictionary* dictionary = loader_data->dictionary();
1649 InstanceKlass* sd_check = dictionary->find_class(current, name);
1650 if (sd_check == nullptr) {
1651 dictionary->add_klass(current, name, k);
1652 }
1653 mu1.notify_all();
1654 }
1655
1656 #if INCLUDE_CDS
1657 // Indicate that loader_data has initiated the loading of class k, which
1658 // has already been defined by a parent loader.
1659 // This API should be used only by AOTLinkedClassBulkLoader
1660 void SystemDictionary::add_to_initiating_loader(JavaThread* current,
1661 InstanceKlass* k,
1662 ClassLoaderData* loader_data) {
1663 assert(CDSConfig::is_using_aot_linked_classes(), "must be");
1664 assert_locked_or_safepoint(SystemDictionary_lock);
1665 Symbol* name = k->name();
1666 Dictionary* dictionary = loader_data->dictionary();
1667 assert(k->is_loaded(), "must be");
1668 assert(k->class_loader_data() != loader_data, "only for classes defined by a parent loader");
1669 assert(dictionary->find_class(current, name) == nullptr, "sanity");
1670 dictionary->add_klass(current, name, k);
1671 }
1672 #endif
1673
1674 // Try to find a class name using the loader constraints. The
1675 // loader constraints might know about a class that isn't fully loaded
1676 // yet and these will be ignored.
1677 Klass* SystemDictionary::find_constrained_instance_or_array_klass(
1678 Thread* current, Symbol* class_name, Handle class_loader) {
1679
1680 // First see if it has been loaded directly.
1681 Klass* klass = find_instance_or_array_klass(current, class_name, class_loader);
1682 if (klass != nullptr)
1683 return klass;
1684
1685 // Now look to see if it has been loaded elsewhere, and is subject to
1686 // a loader constraint that would require this loader to return the
1687 // klass that is already loaded.
1688 if (Signature::is_array(class_name)) {
1689 // For array classes, their Klass*s are not kept in the
1690 // constraint table. The element Klass*s are.
1691 SignatureStream ss(class_name, false);
1692 int ndims = ss.skip_array_prefix(); // skip all '['s
|
38 #include "classfile/loaderConstraints.hpp"
39 #include "classfile/packageEntry.hpp"
40 #include "classfile/placeholders.hpp"
41 #include "classfile/resolutionErrors.hpp"
42 #include "classfile/stringTable.hpp"
43 #include "classfile/symbolTable.hpp"
44 #include "classfile/systemDictionary.hpp"
45 #include "classfile/vmClasses.hpp"
46 #include "classfile/vmSymbols.hpp"
47 #include "gc/shared/gcTraceTime.inline.hpp"
48 #include "interpreter/bootstrapInfo.hpp"
49 #include "jfr/jfrEvents.hpp"
50 #include "jvm.h"
51 #include "logging/log.hpp"
52 #include "logging/logStream.hpp"
53 #include "memory/metaspaceClosure.hpp"
54 #include "memory/oopFactory.hpp"
55 #include "memory/resourceArea.hpp"
56 #include "memory/universe.hpp"
57 #include "oops/access.inline.hpp"
58 #include "oops/fieldStreams.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/oop.hpp"
66 #include "oops/oopHandle.hpp"
67 #include "oops/oopHandle.inline.hpp"
68 #include "oops/symbol.hpp"
69 #include "oops/typeArrayKlass.hpp"
70 #include "oops/inlineKlass.inline.hpp"
71 #include "prims/jvmtiExport.hpp"
72 #include "prims/methodHandles.hpp"
73 #include "runtime/arguments.hpp"
74 #include "runtime/atomic.hpp"
75 #include "runtime/handles.inline.hpp"
76 #include "runtime/java.hpp"
77 #include "runtime/javaCalls.hpp"
78 #include "runtime/mutexLocker.hpp"
79 #include "runtime/os.hpp"
80 #include "runtime/sharedRuntime.hpp"
81 #include "runtime/signature.hpp"
82 #include "runtime/synchronizer.hpp"
83 #include "services/classLoadingService.hpp"
84 #include "services/diagnosticCommand.hpp"
85 #include "services/finalizerService.hpp"
86 #include "services/threadService.hpp"
87 #include "utilities/growableArray.hpp"
88 #include "utilities/macros.hpp"
89 #include "utilities/utf8.hpp"
90 #if INCLUDE_CDS
91 #include "classfile/systemDictionaryShared.hpp"
92 #endif
93 #if INCLUDE_JFR
94 #include "jfr/jfr.hpp"
95 #endif
96
97 class InvokeMethodKey : public StackObj {
98 private:
99 Symbol* _symbol;
174 CHECK_NULL);
175 return result.get_oop();
176 }
177
178 oop SystemDictionary::get_platform_class_loader_impl(TRAPS) {
179 JavaValue result(T_OBJECT);
180 InstanceKlass* class_loader_klass = vmClasses::ClassLoader_klass();
181 JavaCalls::call_static(&result,
182 class_loader_klass,
183 vmSymbols::getPlatformClassLoader_name(),
184 vmSymbols::void_classloader_signature(),
185 CHECK_NULL);
186 return result.get_oop();
187 }
188
189 // Helper function
190 inline ClassLoaderData* class_loader_data(Handle class_loader) {
191 return ClassLoaderData::class_loader_data(class_loader());
192 }
193
194 // These migrated value classes are loaded by the bootstrap class loader but are added to the initiating
195 // loaders automatically so that fields of these types can be found and potentially flattened during
196 // field layout.
197 static void add_migrated_value_classes(ClassLoaderData* cld) {
198 JavaThread* current = JavaThread::current();
199 auto add_klass = [&] (Symbol* classname) {
200 InstanceKlass* ik = SystemDictionary::find_instance_klass(current, classname, Handle(current, nullptr));
201 assert(ik != nullptr, "Must exist");
202 SystemDictionary::add_to_initiating_loader(current, ik, cld);
203 };
204
205 MonitorLocker mu1(SystemDictionary_lock);
206 vmSymbols::migrated_class_names_do(add_klass);
207 }
208
209 ClassLoaderData* SystemDictionary::register_loader(Handle class_loader, bool create_mirror_cld) {
210 if (create_mirror_cld) {
211 // Add a new class loader data to the graph.
212 return ClassLoaderDataGraph::add(class_loader, true);
213 } else {
214 if (class_loader() == nullptr) {
215 return ClassLoaderData::the_null_class_loader_data();
216 } else {
217 ClassLoaderData* cld = ClassLoaderDataGraph::find_or_create(class_loader);
218 if (EnableValhalla) {
219 add_migrated_value_classes(cld);
220 }
221 return cld;
222 }
223 }
224 }
225
226 void SystemDictionary::set_system_loader(ClassLoaderData *cld) {
227 assert(_java_system_loader.is_empty(), "already set!");
228 _java_system_loader = cld->class_loader_handle();
229
230 }
231
232 void SystemDictionary::set_platform_loader(ClassLoaderData *cld) {
233 assert(_java_platform_loader.is_empty(), "already set!");
234 _java_platform_loader = cld->class_loader_handle();
235 }
236
237 // ----------------------------------------------------------------------------
238 // Parallel class loading check
239
240 static bool is_parallelCapable(Handle class_loader) {
241 if (class_loader.is_null()) return true;
242 return java_lang_ClassLoader::parallelCapable(class_loader());
357
358 Klass* SystemDictionary::resolve_or_fail(Symbol* class_name, Handle class_loader,
359 bool throw_error, TRAPS) {
360 Klass* klass = resolve_or_null(class_name, class_loader, THREAD);
361 // Check for pending exception or null klass, and throw exception
362 if (HAS_PENDING_EXCEPTION || klass == nullptr) {
363 handle_resolution_exception(class_name, throw_error, CHECK_NULL);
364 }
365 return klass;
366 }
367
368 // Forwards to resolve_array_class_or_null or resolve_instance_class_or_null
369
370 Klass* SystemDictionary::resolve_or_null(Symbol* class_name, Handle class_loader, TRAPS) {
371 if (Signature::is_array(class_name)) {
372 return resolve_array_class_or_null(class_name, class_loader, THREAD);
373 } else {
374 assert(class_name != nullptr && !Signature::is_array(class_name), "must be");
375 if (Signature::has_envelope(class_name)) {
376 ResourceMark rm(THREAD);
377 // Ignore wrapping L and ; (and Q and ; for value types).
378 TempNewSymbol name = SymbolTable::new_symbol(class_name->as_C_string() + 1,
379 class_name->utf8_length() - 2);
380 return resolve_instance_class_or_null(name, class_loader, THREAD);
381 } else {
382 return resolve_instance_class_or_null(class_name, class_loader, THREAD);
383 }
384 }
385 }
386
387 // Forwards to resolve_instance_class_or_null
388
389 Klass* SystemDictionary::resolve_array_class_or_null(Symbol* class_name,
390 Handle class_loader,
391 TRAPS) {
392 assert(Signature::is_array(class_name), "must be array");
393 ResourceMark rm(THREAD);
394 SignatureStream ss(class_name, false);
395 int ndims = ss.skip_array_prefix(); // skip all '['s
396 Klass* k = nullptr;
397 BasicType t = ss.type();
926
927 InstanceKlass* SystemDictionary::resolve_from_stream(ClassFileStream* st,
928 Symbol* class_name,
929 Handle class_loader,
930 const ClassLoadInfo& cl_info,
931 TRAPS) {
932 if (cl_info.is_hidden()) {
933 return resolve_hidden_class_from_stream(st, class_name, class_loader, cl_info, CHECK_NULL);
934 } else {
935 return resolve_class_from_stream(st, class_name, class_loader, cl_info, CHECK_NULL);
936 }
937 }
938
939
940 #if INCLUDE_CDS
941 // Check if a shared class can be loaded by the specific classloader.
942 bool SystemDictionary::is_shared_class_visible(Symbol* class_name,
943 InstanceKlass* ik,
944 PackageEntry* pkg_entry,
945 Handle class_loader) {
946 assert(!CDSConfig::module_patching_disables_cds(), "Cannot use CDS");
947
948 // (1) Check if we are loading into the same loader as in dump time.
949
950 if (ik->defined_by_boot_loader()) {
951 if (class_loader() != nullptr) {
952 return false;
953 }
954 } else if (ik->defined_by_platform_loader()) {
955 if (class_loader() != java_platform_loader()) {
956 return false;
957 }
958 } else if (ik->defined_by_app_loader()) {
959 if (class_loader() != java_system_loader()) {
960 return false;
961 }
962 } else {
963 // ik was loaded by a custom loader during dump time
964 if (class_loader_data(class_loader)->is_builtin_class_loader_data()) {
965 return false;
966 } else {
1002
1003 if (pkg_entry == nullptr) {
1004 // We might have looked up pkg_entry before the module system was initialized.
1005 // Need to reload it now.
1006 TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);
1007 if (pkg_name != nullptr) {
1008 pkg_entry = class_loader_data(class_loader)->packages()->lookup_only(pkg_name);
1009 }
1010 }
1011
1012 ModuleEntry* mod_entry = (pkg_entry == nullptr) ? nullptr : pkg_entry->module();
1013 bool should_be_in_named_module = (mod_entry != nullptr && mod_entry->is_named());
1014 bool was_archived_from_named_module = !cl->has_unnamed_module();
1015 bool visible;
1016
1017 if (was_archived_from_named_module) {
1018 if (should_be_in_named_module) {
1019 // Is the module loaded from the same location as during dump time?
1020 visible = mod_entry->shared_path_index() == scp_index;
1021 if (visible) {
1022 assert(!CDSConfig::module_patching_disables_cds(), "Cannot use CDS");
1023 }
1024 } else {
1025 // During dump time, this class was in a named module, but at run time, this class should be
1026 // in an unnamed module.
1027 visible = false;
1028 }
1029 } else {
1030 if (should_be_in_named_module) {
1031 // During dump time, this class was in an unnamed, but at run time, this class should be
1032 // in a named module.
1033 visible = false;
1034 } else {
1035 visible = true;
1036 }
1037 }
1038
1039 return visible;
1040 }
1041
1042 bool SystemDictionary::check_shared_class_super_type(InstanceKlass* klass, InstanceKlass* super_type,
1080 class_loader, true,
1081 CHECK_false);
1082 if (!check_super) {
1083 return false;
1084 }
1085 }
1086
1087 Array<InstanceKlass*>* interfaces = ik->local_interfaces();
1088 int num_interfaces = interfaces->length();
1089 for (int index = 0; index < num_interfaces; index++) {
1090 bool check_interface = check_shared_class_super_type(ik, interfaces->at(index), class_loader, false,
1091 CHECK_false);
1092 if (!check_interface) {
1093 return false;
1094 }
1095 }
1096
1097 return true;
1098 }
1099
1100 // Pre-load class referred to in non-static null-free instance field. These fields trigger MANDATORY loading.
1101 // Some pre-loading does not fail fatally
1102 bool SystemDictionary::preload_from_null_free_field(InstanceKlass* ik, Handle class_loader, Symbol* sig, int field_index, TRAPS) {
1103 TempNewSymbol name = Signature::strip_envelope(sig);
1104 log_info(class, preload)("Preloading class %s during loading of shared class %s. "
1105 "Cause: a null-free non-static field is declared with this type",
1106 name->as_C_string(), ik->name()->as_C_string());
1107 InstanceKlass* real_k = SystemDictionary::resolve_with_circularity_detection_or_fail(ik->name(), name,
1108 class_loader, false, CHECK_false);
1109 if (HAS_PENDING_EXCEPTION) {
1110 log_warning(class, preload)("Preloading of class %s during loading of class %s "
1111 "(cause: null-free non-static field) failed: %s",
1112 name->as_C_string(), ik->name()->as_C_string(),
1113 PENDING_EXCEPTION->klass()->name()->as_C_string());
1114 return false; // Exception is still pending
1115 }
1116
1117 InstanceKlass* k = ik->get_inline_type_field_klass_or_null(field_index);
1118 if (real_k != k) {
1119 // oops, the app has substituted a different version of k! Does not fail fatally
1120 log_warning(class, preload)("Preloading of class %s during loading of shared class %s "
1121 "(cause: null-free non-static field) failed : "
1122 "app substituted a different version of %s",
1123 name->as_C_string(), ik->name()->as_C_string(),
1124 name->as_C_string());
1125 return false;
1126 }
1127 log_info(class, preload)("Preloading of class %s during loading of shared class %s "
1128 "(cause: null-free non-static field) succeeded",
1129 name->as_C_string(), ik->name()->as_C_string());
1130
1131 assert(real_k != nullptr, "Sanity check");
1132 InstanceKlass::check_can_be_annotated_with_NullRestricted(real_k, ik->name(), CHECK_false);
1133
1134 return true;
1135 }
1136
1137 // Tries to pre-load classes referred to in non-static nullable instance fields if they are found in the
1138 // loadable descriptors attribute. If loading fails, we can fail silently.
1139 void SystemDictionary::try_preload_from_loadable_descriptors(InstanceKlass* ik, Handle class_loader, Symbol* sig, int field_index, TRAPS) {
1140 TempNewSymbol name = Signature::strip_envelope(sig);
1141 if (name != ik->name() && ik->is_class_in_loadable_descriptors_attribute(sig)) {
1142 log_info(class, preload)("Preloading class %s during loading of shared class %s. "
1143 "Cause: field type in LoadableDescriptors attribute",
1144 name->as_C_string(), ik->name()->as_C_string());
1145 InstanceKlass* real_k = SystemDictionary::resolve_with_circularity_detection_or_fail(ik->name(), name,
1146 class_loader, false, THREAD);
1147 if (HAS_PENDING_EXCEPTION) {
1148 CLEAR_PENDING_EXCEPTION;
1149 }
1150
1151 InstanceKlass* k = ik->get_inline_type_field_klass_or_null(field_index);
1152 if (real_k != k) {
1153 // oops, the app has substituted a different version of k!
1154 log_warning(class, preload)("Preloading of class %s during loading of shared class %s "
1155 "(cause: field type in LoadableDescriptors attribute) failed : "
1156 "app substituted a different version of %s",
1157 name->as_C_string(), ik->name()->as_C_string(),
1158 k->name()->as_C_string());
1159 return;
1160 } else if (real_k != nullptr) {
1161 log_info(class, preload)("Preloading of class %s during loading of shared class %s "
1162 "(cause: field type in LoadableDescriptors attribute) succeeded",
1163 name->as_C_string(), ik->name()->as_C_string());
1164 }
1165 }
1166 }
1167
1168
1169 InstanceKlass* SystemDictionary::load_shared_class(InstanceKlass* ik,
1170 Handle class_loader,
1171 Handle protection_domain,
1172 const ClassFileStream *cfs,
1173 PackageEntry* pkg_entry,
1174 TRAPS) {
1175 assert(ik != nullptr, "sanity");
1176 assert(ik->is_shared(), "sanity");
1177 assert(!ik->is_unshareable_info_restored(), "shared class can be restored only once");
1178 assert(Atomic::add(&ik->_shared_class_load_count, 1) == 1, "shared class loaded more than once");
1179 Symbol* class_name = ik->name();
1180
1181 if (!is_shared_class_visible(class_name, ik, pkg_entry, class_loader)) {
1182 ik->set_shared_loading_failed();
1183 return nullptr;
1184 }
1185
1186 bool check = check_shared_class_super_types(ik, class_loader, CHECK_NULL);
1187 if (!check) {
1188 ik->set_shared_loading_failed();
1189 return nullptr;
1190 }
1191
1192 if (ik->has_inline_type_fields()) {
1193 for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
1194 if (fs.access_flags().is_static()) continue;
1195
1196 Symbol* sig = fs.signature();
1197 int field_index = fs.index();
1198
1199 if (fs.is_null_free_inline_type()) {
1200 // A false return means that the class didn't load for other reasons than an exception.
1201 bool check = preload_from_null_free_field(ik, class_loader, sig, field_index, CHECK_NULL);
1202 if (!check) {
1203 ik->set_shared_loading_failed();
1204 return nullptr;
1205 }
1206 } else if (Signature::has_envelope(sig)) {
1207 // Pending exceptions are cleared so we can fail silently
1208 try_preload_from_loadable_descriptors(ik, class_loader, sig, field_index, CHECK_NULL);
1209 }
1210 }
1211 }
1212
1213 InstanceKlass* new_ik = nullptr;
1214 // CFLH check is skipped for VM hidden classes (see KlassFactory::create_from_stream).
1215 // It will be skipped for shared VM hidden lambda proxy classes.
1216 if (!ik->is_hidden()) {
1217 new_ik = KlassFactory::check_shared_class_file_load_hook(
1218 ik, class_name, class_loader, protection_domain, cfs, CHECK_NULL);
1219 }
1220 if (new_ik != nullptr) {
1221 // The class is changed by CFLH. Return the new class. The shared class is
1222 // not used.
1223 return new_ik;
1224 }
1225
1226 // Adjust methods to recover missing data. They need addresses for
1227 // interpreter entry points and their default native method address
1228 // must be reset.
1229
1230 // Shared classes are all currently loaded by either the bootstrap or
1231 // internal parallel class loaders, so this will never cause a deadlock
1232 // on a custom class loader lock.
1233 // Since this class is already locked with parallel capable class
1234 // loaders, including the bootstrap loader via the placeholder table,
1235 // this lock is currently a nop.
1236
1237 ClassLoaderData* loader_data = class_loader_data(class_loader);
1238 {
1239 HandleMark hm(THREAD);
1240 Handle lockObject = get_loader_lock_or_null(class_loader);
1241 ObjectLocker ol(lockObject, THREAD);
1242 // prohibited package check assumes all classes loaded from archive call
1243 // restore_unshareable_info which calls ik->set_package()
1244 ik->restore_unshareable_info(loader_data, protection_domain, pkg_entry, CHECK_NULL);
1245 }
1246
1247 load_shared_class_misc(ik, loader_data);
1248
1249 return ik;
1250 }
1251
1252 void SystemDictionary::load_shared_class_misc(InstanceKlass* ik, ClassLoaderData* loader_data) {
1253 ik->print_class_load_logging(loader_data, nullptr, nullptr);
1254
1255 // For boot loader, ensure that GetSystemPackage knows that a class in this
1256 // package was loaded.
1257 if (loader_data->is_the_null_class_loader_data()) {
1258 s2 path_index = ik->shared_classpath_index();
1259 ik->set_classpath_index(path_index);
1260 }
1261
1262 // notify a class loaded from shared object
1263 ClassLoadingService::notify_class_loaded(ik, true /* shared class */);
1264
1265 if (CDSConfig::is_dumping_final_static_archive()) {
1266 SystemDictionaryShared::init_dumptime_info_from_preimage(ik);
1267 }
1268 }
1751 }
1752 }
1753
1754 // Update class loader data dictionary - done after check_constraint and add_to_hierarchy
1755 // have been called.
1756 void SystemDictionary::update_dictionary(JavaThread* current,
1757 InstanceKlass* k,
1758 ClassLoaderData* loader_data) {
1759 MonitorLocker mu1(SystemDictionary_lock);
1760
1761 // Make a new dictionary entry.
1762 Symbol* name = k->name();
1763 Dictionary* dictionary = loader_data->dictionary();
1764 InstanceKlass* sd_check = dictionary->find_class(current, name);
1765 if (sd_check == nullptr) {
1766 dictionary->add_klass(current, name, k);
1767 }
1768 mu1.notify_all();
1769 }
1770
1771 // Indicate that loader_data has initiated the loading of class k, which
1772 // has already been defined by a parent loader.
1773 // This API is used by AOTLinkedClassBulkLoader and to register boxing
1774 // classes from java.lang in all class loaders to enable more value
1775 // classes optimizations
1776 void SystemDictionary::add_to_initiating_loader(JavaThread* current,
1777 InstanceKlass* k,
1778 ClassLoaderData* loader_data) {
1779 assert_locked_or_safepoint(SystemDictionary_lock);
1780 Symbol* name = k->name();
1781 Dictionary* dictionary = loader_data->dictionary();
1782 assert(k->is_loaded(), "must be");
1783 assert(k->class_loader_data() != loader_data, "only for classes defined by a parent loader");
1784 if (dictionary->find_class(current, name) == nullptr) {
1785 dictionary->add_klass(current, name, k);
1786 }
1787 }
1788
1789 // Try to find a class name using the loader constraints. The
1790 // loader constraints might know about a class that isn't fully loaded
1791 // yet and these will be ignored.
1792 Klass* SystemDictionary::find_constrained_instance_or_array_klass(
1793 Thread* current, Symbol* class_name, Handle class_loader) {
1794
1795 // First see if it has been loaded directly.
1796 Klass* klass = find_instance_or_array_klass(current, class_name, class_loader);
1797 if (klass != nullptr)
1798 return klass;
1799
1800 // Now look to see if it has been loaded elsewhere, and is subject to
1801 // a loader constraint that would require this loader to return the
1802 // klass that is already loaded.
1803 if (Signature::is_array(class_name)) {
1804 // For array classes, their Klass*s are not kept in the
1805 // constraint table. The element Klass*s are.
1806 SignatureStream ss(class_name, false);
1807 int ndims = ss.skip_array_prefix(); // skip all '['s
|