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;
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->is_shared_boot_class()) {
927 if (class_loader() != nullptr) {
928 return false;
929 }
930 } else if (ik->is_shared_platform_class()) {
931 if (class_loader() != java_platform_loader()) {
932 return false;
933 }
934 } else if (ik->is_shared_app_class()) {
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,
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 }
|
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;
335
336 Klass* SystemDictionary::resolve_or_fail(Symbol* class_name, Handle class_loader,
337 bool throw_error, TRAPS) {
338 Klass* klass = resolve_or_null(class_name, class_loader, THREAD);
339 // Check for pending exception or null klass, and throw exception
340 if (HAS_PENDING_EXCEPTION || klass == nullptr) {
341 handle_resolution_exception(class_name, throw_error, CHECK_NULL);
342 }
343 return klass;
344 }
345
346 // Forwards to resolve_array_class_or_null or resolve_instance_class_or_null
347
348 Klass* SystemDictionary::resolve_or_null(Symbol* class_name, Handle class_loader, TRAPS) {
349 if (Signature::is_array(class_name)) {
350 return resolve_array_class_or_null(class_name, class_loader, THREAD);
351 } else {
352 assert(class_name != nullptr && !Signature::is_array(class_name), "must be");
353 if (Signature::has_envelope(class_name)) {
354 ResourceMark rm(THREAD);
355 // Ignore wrapping L and ; (and Q and ; for value types).
356 TempNewSymbol name = SymbolTable::new_symbol(class_name->as_C_string() + 1,
357 class_name->utf8_length() - 2);
358 return resolve_instance_class_or_null(name, class_loader, THREAD);
359 } else {
360 return resolve_instance_class_or_null(class_name, class_loader, THREAD);
361 }
362 }
363 }
364
365 // Forwards to resolve_instance_class_or_null
366
367 Klass* SystemDictionary::resolve_array_class_or_null(Symbol* class_name,
368 Handle class_loader,
369 TRAPS) {
370 assert(Signature::is_array(class_name), "must be array");
371 ResourceMark rm(THREAD);
372 SignatureStream ss(class_name, false);
373 int ndims = ss.skip_array_prefix(); // skip all '['s
374 Klass* k = nullptr;
375 BasicType t = ss.type();
904
905 InstanceKlass* SystemDictionary::resolve_from_stream(ClassFileStream* st,
906 Symbol* class_name,
907 Handle class_loader,
908 const ClassLoadInfo& cl_info,
909 TRAPS) {
910 if (cl_info.is_hidden()) {
911 return resolve_hidden_class_from_stream(st, class_name, class_loader, cl_info, CHECK_NULL);
912 } else {
913 return resolve_class_from_stream(st, class_name, class_loader, cl_info, CHECK_NULL);
914 }
915 }
916
917
918 #if INCLUDE_CDS
919 // Check if a shared class can be loaded by the specific classloader.
920 bool SystemDictionary::is_shared_class_visible(Symbol* class_name,
921 InstanceKlass* ik,
922 PackageEntry* pkg_entry,
923 Handle class_loader) {
924 assert(!CDSConfig::module_patching_disables_cds(), "Cannot use CDS");
925
926 // (1) Check if we are loading into the same loader as in dump time.
927
928 if (ik->is_shared_boot_class()) {
929 if (class_loader() != nullptr) {
930 return false;
931 }
932 } else if (ik->is_shared_platform_class()) {
933 if (class_loader() != java_platform_loader()) {
934 return false;
935 }
936 } else if (ik->is_shared_app_class()) {
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(!CDSConfig::module_patching_disables_cds(), "Cannot use CDS");
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,
1081 const ClassFileStream *cfs,
1082 PackageEntry* pkg_entry,
1083 TRAPS) {
1084 assert(ik != nullptr, "sanity");
1085 assert(ik->is_shared(), "sanity");
1086 assert(!ik->is_unshareable_info_restored(), "shared class can be restored only once");
1087 assert(Atomic::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 if (ik->has_inline_type_fields()) {
1102 for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
1103 if (fs.access_flags().is_static()) continue;
1104 Symbol* sig = fs.signature();
1105 if (fs.is_null_free_inline_type()) {
1106 // Pre-load inline class
1107 TempNewSymbol name = Signature::strip_envelope(sig);
1108 Klass* real_k = SystemDictionary::resolve_with_circularity_detection_or_fail(ik->name(), name,
1109 class_loader, false, CHECK_NULL);
1110 Klass* k = ik->get_inline_type_field_klass_or_null(fs.index());
1111 if (real_k != k) {
1112 // oops, the app has substituted a different version of k!
1113 return nullptr;
1114 }
1115 } else if (Signature::has_envelope(sig)) {
1116 TempNewSymbol name = Signature::strip_envelope(sig);
1117 if (name != ik->name() && ik->is_class_in_loadable_descriptors_attribute(name)) {
1118 Klass* real_k = SystemDictionary::resolve_with_circularity_detection_or_fail(ik->name(), name,
1119 class_loader, false, THREAD);
1120 if (HAS_PENDING_EXCEPTION) {
1121 CLEAR_PENDING_EXCEPTION;
1122 }
1123 Klass* k = ik->get_inline_type_field_klass_or_null(fs.index());
1124 if (real_k != k) {
1125 // oops, the app has substituted a different version of k!
1126 return nullptr;
1127 }
1128 }
1129 }
1130 }
1131 }
1132
1133 InstanceKlass* new_ik = nullptr;
1134 // CFLH check is skipped for VM hidden classes (see KlassFactory::create_from_stream).
1135 // It will be skipped for shared VM hidden lambda proxy classes.
1136 if (!ik->is_hidden()) {
1137 new_ik = KlassFactory::check_shared_class_file_load_hook(
1138 ik, class_name, class_loader, protection_domain, cfs, CHECK_NULL);
1139 }
1140 if (new_ik != nullptr) {
1141 // The class is changed by CFLH. Return the new class. The shared class is
1142 // not used.
1143 return new_ik;
1144 }
1145
1146 // Adjust methods to recover missing data. They need addresses for
1147 // interpreter entry points and their default native method address
1148 // must be reset.
1149
1150 // Shared classes are all currently loaded by either the bootstrap or
1151 // internal parallel class loaders, so this will never cause a deadlock
1152 // on a custom class loader lock.
1153 // Since this class is already locked with parallel capable class
1154 // loaders, including the bootstrap loader via the placeholder table,
1155 // this lock is currently a nop.
1156
1157 ClassLoaderData* loader_data = class_loader_data(class_loader);
1158 {
1159 HandleMark hm(THREAD);
1160 Handle lockObject = get_loader_lock_or_null(class_loader);
1161 ObjectLocker ol(lockObject, THREAD);
1162 // prohibited package check assumes all classes loaded from archive call
1163 // restore_unshareable_info which calls ik->set_package()
1164 ik->restore_unshareable_info(loader_data, protection_domain, pkg_entry, CHECK_NULL);
1165 }
1166
1167 load_shared_class_misc(ik, loader_data);
1168
1169 return ik;
1170 }
1171
1172 void SystemDictionary::load_shared_class_misc(InstanceKlass* ik, ClassLoaderData* loader_data) {
1173 ik->print_class_load_logging(loader_data, nullptr, nullptr);
1174
1175 // For boot loader, ensure that GetSystemPackage knows that a class in this
1176 // package was loaded.
1177 if (loader_data->is_the_null_class_loader_data()) {
1178 s2 path_index = ik->shared_classpath_index();
1179 ik->set_classpath_index(path_index);
1180 }
1181
1182 // notify a class loaded from shared object
1183 ClassLoadingService::notify_class_loaded(ik, true /* shared class */);
1184
1185 if (CDSConfig::is_dumping_final_static_archive()) {
1186 SystemDictionaryShared::init_dumptime_info_from_preimage(ik);
1187 }
1188 }
|