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/constantPool.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/oopHandle.inline.hpp"
65 #include "oops/symbol.hpp"
66 #include "oops/typeArrayKlass.hpp"
67 #include "prims/jvmtiExport.hpp"
68 #include "prims/methodHandles.hpp"
69 #include "runtime/arguments.hpp"
70 #include "runtime/atomicAccess.hpp"
71 #include "runtime/handles.inline.hpp"
72 #include "runtime/java.hpp"
73 #include "runtime/javaCalls.hpp"
74 #include "runtime/mutexLocker.hpp"
75 #include "runtime/sharedRuntime.hpp"
76 #include "runtime/signature.hpp"
77 #include "runtime/synchronizer.hpp"
78 #include "services/classLoadingService.hpp"
79 #include "services/diagnosticCommand.hpp"
80 #include "services/finalizerService.hpp"
81 #include "services/threadService.hpp"
82 #include "utilities/growableArray.hpp"
83 #include "utilities/macros.hpp"
84 #include "utilities/utf8.hpp"
85 #if INCLUDE_CDS
86 #include "classfile/systemDictionaryShared.hpp"
87 #endif
88 #if INCLUDE_JFR
89 #include "jfr/jfr.hpp"
90 #endif
91
92 class InvokeMethodKey : public StackObj {
93 private:
94 Symbol* _symbol;
169 CHECK_NULL);
170 return result.get_oop();
171 }
172
173 oop SystemDictionary::get_platform_class_loader_impl(TRAPS) {
174 JavaValue result(T_OBJECT);
175 InstanceKlass* class_loader_klass = vmClasses::ClassLoader_klass();
176 JavaCalls::call_static(&result,
177 class_loader_klass,
178 vmSymbols::getPlatformClassLoader_name(),
179 vmSymbols::void_classloader_signature(),
180 CHECK_NULL);
181 return result.get_oop();
182 }
183
184 // Helper function
185 inline ClassLoaderData* class_loader_data(Handle class_loader) {
186 return ClassLoaderData::class_loader_data(class_loader());
187 }
188
189 ClassLoaderData* SystemDictionary::register_loader(Handle class_loader, bool create_mirror_cld) {
190 if (create_mirror_cld) {
191 // Add a new class loader data to the graph.
192 return ClassLoaderDataGraph::add(class_loader, true);
193 } else {
194 return (class_loader() == nullptr) ? ClassLoaderData::the_null_class_loader_data() :
195 ClassLoaderDataGraph::find_or_create(class_loader);
196 }
197 }
198
199 void SystemDictionary::set_system_loader(ClassLoaderData *cld) {
200 if (_java_system_loader.is_empty()) {
201 _java_system_loader = cld->class_loader_handle();
202 } else {
203 assert(_java_system_loader.resolve() == cld->class_loader(), "sanity");
204 }
205 }
206
207 void SystemDictionary::set_platform_loader(ClassLoaderData *cld) {
208 if (_java_platform_loader.is_empty()) {
209 _java_platform_loader = cld->class_loader_handle();
210 } else {
211 assert(_java_platform_loader.resolve() == cld->class_loader(), "sanity");
212 }
213 }
214
215 // ----------------------------------------------------------------------------
383 }
384 } else {
385 k = Universe::typeArrayKlass(t);
386 k = k->array_klass(ndims, CHECK_NULL);
387 }
388 return k;
389 }
390
391 static inline void log_circularity_error(Symbol* name, PlaceholderEntry* probe) {
392 LogTarget(Debug, class, load, placeholders) lt;
393 if (lt.is_enabled()) {
394 ResourceMark rm;
395 LogStream ls(lt);
396 ls.print("ClassCircularityError detected for placeholder entry %s", name->as_C_string());
397 probe->print_on(&ls);
398 ls.cr();
399 }
400 }
401
402 // Must be called for any superclass or superinterface resolution
403 // during class definition to allow class circularity checking
404 // superinterface callers:
405 // parse_interfaces - from defineClass
406 // superclass callers:
407 // ClassFileParser - from defineClass
408 // load_shared_class - while loading a class from shared archive
409 // resolve_instance_class_or_null:
410 // via: handle_parallel_super_load
411 // when resolving a class that has an existing placeholder with
412 // a saved superclass [i.e. a defineClass is currently in progress]
413 // If another thread is trying to resolve the class, it must do
414 // superclass checks on its own thread to catch class circularity and
415 // to avoid deadlock.
416 //
417 // resolve_with_circularity_detection adds a DETECT_CIRCULARITY placeholder to the placeholder table before calling
418 // resolve_instance_class_or_null. ClassCircularityError is detected when a DETECT_CIRCULARITY or LOAD_INSTANCE
419 // placeholder for the same thread, class, and classloader is found.
420 // This can be seen with logging option: -Xlog:class+load+placeholders=debug.
421 //
422 InstanceKlass* SystemDictionary::resolve_with_circularity_detection(Symbol* class_name,
423 Symbol* next_name,
424 Handle class_loader,
425 bool is_superclass,
426 TRAPS) {
427
428 assert(next_name != nullptr, "null superclass for resolving");
429 assert(!Signature::is_array(next_name), "invalid superclass name");
430
431 ClassLoaderData* loader_data = class_loader_data(class_loader);
432
433 if (is_superclass) {
434 InstanceKlass* klassk = loader_data->dictionary()->find_class(THREAD, class_name);
435 if (klassk != nullptr) {
436 // We can come here for two reasons:
437 // (a) RedefineClasses -- the class is already loaded
438 // (b) Rarely, the class might have been loaded by a parallel thread
439 // We can do a quick check against the already assigned superclass's name and loader.
440 InstanceKlass* superk = klassk->super();
441 if (superk != nullptr &&
442 superk->name() == next_name &&
443 superk->class_loader() == class_loader()) {
444 return superk;
445 }
446 }
447 }
448
449 // can't throw error holding a lock
450 bool throw_circularity_error = false;
451 {
452 MutexLocker mu(THREAD, SystemDictionary_lock);
453
454 // Must check ClassCircularity before resolving next_name (superclass or interface).
455 PlaceholderEntry* probe = PlaceholderTable::get_entry(class_name, loader_data);
456 if (probe != nullptr && probe->check_seen_thread(THREAD, PlaceholderTable::DETECT_CIRCULARITY)) {
457 log_circularity_error(class_name, probe);
458 throw_circularity_error = true;
459 }
460
461 // Make sure there's a placeholder for the class_name before resolving.
462 // This is used as a claim that this thread is currently loading superclass/classloader
463 // and for ClassCircularity checks.
464 if (!throw_circularity_error) {
465 // Be careful not to exit resolve_with_circularity_detection without removing this placeholder.
466 PlaceholderEntry* newprobe = PlaceholderTable::find_and_add(class_name,
467 loader_data,
468 PlaceholderTable::DETECT_CIRCULARITY,
469 next_name, THREAD);
470 }
471 }
472
473 if (throw_circularity_error) {
474 ResourceMark rm(THREAD);
475 THROW_MSG_NULL(vmSymbols::java_lang_ClassCircularityError(), class_name->as_C_string());
476 }
477
478 // Resolve the superclass or superinterface, check results on return
479 InstanceKlass* superk =
480 SystemDictionary::resolve_instance_class_or_null(next_name,
481 class_loader,
482 THREAD);
483
484 // Clean up placeholder entry.
485 {
486 MutexLocker mu(THREAD, SystemDictionary_lock);
487 PlaceholderTable::find_and_remove(class_name, loader_data, PlaceholderTable::DETECT_CIRCULARITY, THREAD);
488 SystemDictionary_lock->notify_all();
489 }
490
491 // Check for pending exception or null superk, and throw exception
492 if (HAS_PENDING_EXCEPTION || superk == nullptr) {
493 handle_resolution_exception(next_name, true, CHECK_NULL);
494 }
495
496 return superk;
497 }
498
896
897 InstanceKlass* SystemDictionary::resolve_from_stream(ClassFileStream* st,
898 Symbol* class_name,
899 Handle class_loader,
900 const ClassLoadInfo& cl_info,
901 TRAPS) {
902 if (cl_info.is_hidden()) {
903 return resolve_hidden_class_from_stream(st, class_name, class_loader, cl_info, CHECK_NULL);
904 } else {
905 return resolve_class_from_stream(st, class_name, class_loader, cl_info, CHECK_NULL);
906 }
907 }
908
909
910 #if INCLUDE_CDS
911 // Check if a shared class can be loaded by the specific classloader.
912 bool SystemDictionary::is_shared_class_visible(Symbol* class_name,
913 InstanceKlass* ik,
914 PackageEntry* pkg_entry,
915 Handle class_loader) {
916 assert(!ModuleEntryTable::javabase_moduleEntry()->is_patched(),
917 "Cannot use sharing if java.base is patched");
918
919 // (1) Check if we are loading into the same loader as in dump time.
920
921 if (ik->defined_by_boot_loader()) {
922 if (class_loader() != nullptr) {
923 return false;
924 }
925 } else if (ik->defined_by_platform_loader()) {
926 if (class_loader() != java_platform_loader()) {
927 return false;
928 }
929 } else if (ik->defined_by_app_loader()) {
930 if (class_loader() != java_system_loader()) {
931 return false;
932 }
933 } else {
934 // ik was loaded by a custom loader during dump time
935 if (class_loader_data(class_loader)->is_builtin_class_loader_data()) {
993 assert(!mod_entry->is_patched(), "cannot load archived classes for patched module");
994 }
995 } else {
996 // During dump time, this class was in a named module, but at run time, this class should be
997 // in an unnamed module.
998 visible = false;
999 }
1000 } else {
1001 if (should_be_in_named_module) {
1002 // During dump time, this class was in an unnamed, but at run time, this class should be
1003 // in a named module.
1004 visible = false;
1005 } else {
1006 visible = true;
1007 }
1008 }
1009
1010 return visible;
1011 }
1012
1013 bool SystemDictionary::check_shared_class_super_type(InstanceKlass* klass, InstanceKlass* super_type,
1014 Handle class_loader, bool is_superclass, TRAPS) {
1015 assert(super_type->in_aot_cache(), "must be");
1016
1017 // Quick check if the super type has been already loaded.
1018 // + Don't do it for unregistered classes -- they can be unloaded so
1019 // super_type->class_loader_data() could be stale.
1020 // + Don't check if loader data is null, ie. the super_type isn't fully loaded.
1021 if (!super_type->defined_by_other_loaders() && super_type->class_loader_data() != nullptr) {
1022 // Check if the superclass is loaded by the current class_loader
1023 Symbol* name = super_type->name();
1024 InstanceKlass* check = find_instance_klass(THREAD, name, class_loader);
1025 if (check == super_type) {
1026 return true;
1027 }
1028 }
1029
1030 Klass *found = resolve_with_circularity_detection(klass->name(), super_type->name(),
1031 class_loader, is_superclass, CHECK_false);
1032 if (found == super_type) {
1033 return true;
1034 } else {
1035 // The dynamically resolved super type is not the same as the one we used during dump time,
1036 // so we cannot use the class.
1037 return false;
1038 }
1039 }
1040
1041 bool SystemDictionary::check_shared_class_super_types(InstanceKlass* ik, Handle class_loader, TRAPS) {
1042 // Check the superclass and interfaces. They must be the same
1043 // as in dump time, because the layout of <ik> depends on
1044 // the specific layout of ik->super() and ik->local_interfaces().
1045 //
1046 // If unexpected superclass or interfaces are found, we cannot
1047 // load <ik> from the shared archive.
1048
1049 if (ik->super() != nullptr) {
1050 bool check_super = check_shared_class_super_type(ik, ik->super(),
1051 class_loader, true,
1052 CHECK_false);
1053 if (!check_super) {
1054 return false;
1055 }
1056 }
1057
1058 Array<InstanceKlass*>* interfaces = ik->local_interfaces();
1059 int num_interfaces = interfaces->length();
1060 for (int index = 0; index < num_interfaces; index++) {
1061 bool check_interface = check_shared_class_super_type(ik, interfaces->at(index), class_loader, false,
1062 CHECK_false);
1063 if (!check_interface) {
1064 return false;
1065 }
1066 }
1067
1068 return true;
1069 }
1070
1071 InstanceKlass* SystemDictionary::load_shared_class(InstanceKlass* ik,
1072 Handle class_loader,
1073 Handle protection_domain,
1074 const ClassFileStream *cfs,
1075 PackageEntry* pkg_entry,
1076 TRAPS) {
1077 assert(ik != nullptr, "sanity");
1078 assert(ik->in_aot_cache(), "sanity");
1079 assert(!ik->is_unshareable_info_restored(), "shared class can be restored only once");
1080 assert(AtomicAccess::add(&ik->_shared_class_load_count, 1) == 1, "shared class loaded more than once");
1081 Symbol* class_name = ik->name();
1082
1083 if (!is_shared_class_visible(class_name, ik, pkg_entry, class_loader)) {
1084 ik->set_shared_loading_failed();
1085 return nullptr;
1086 }
1087
1088 bool check = check_shared_class_super_types(ik, class_loader, CHECK_NULL);
1089 if (!check) {
1090 ik->set_shared_loading_failed();
1091 return nullptr;
1092 }
1093
1094 InstanceKlass* new_ik = nullptr;
1095 // CFLH check is skipped for VM hidden classes (see KlassFactory::create_from_stream).
1096 // It will be skipped for shared VM hidden lambda proxy classes.
1097 if (!ik->is_hidden()) {
1098 new_ik = KlassFactory::check_shared_class_file_load_hook(
1099 ik, class_name, class_loader, protection_domain, cfs, CHECK_NULL);
1100 }
1101 if (new_ik != nullptr) {
1102 // The class is changed by CFLH. Return the new class. The shared class is
1103 // not used.
1104 return new_ik;
1105 }
1106
1107 // Adjust methods to recover missing data. They need addresses for
1108 // interpreter entry points and their default native method address
1109 // must be reset.
1110
1111 // Shared classes are all currently loaded by either the bootstrap or
1112 // internal parallel class loaders, so this will never cause a deadlock
1113 // on a custom class loader lock.
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
2078 if (!m->has_compiled_code()) {
2079 ResourceMark rm(THREAD);
2080 vm_exit_during_initialization(err_msg("Failed to initialize method %s", m->external_name()));
2081 }
2082 }
2083
2084 // There's no need to grab the InvokeMethodIntrinsicTable_lock, as we are still very early in
2085 // VM start-up -- in init_globals2() -- so we are still running a single Java thread. It's not
2086 // possible to have a contention.
2087 const int iid_as_int = vmIntrinsics::as_int(m->intrinsic_id());
2088 InvokeMethodKey key(m->signature(), iid_as_int);
2089 bool created = _invoke_method_intrinsic_table->put(key, m());
2090 assert(created, "unexpected contention");
2091 }
2092 }
2093 #endif // INCLUDE_CDS
2094
2095 // Helper for unpacking the return value from linkMethod and linkCallSite.
2096 static Method* unpack_method_and_appendix(Handle mname,
2097 Klass* accessing_klass,
2098 objArrayHandle appendix_box,
2099 Handle* appendix_result,
2100 TRAPS) {
2101 if (mname.not_null()) {
2102 Method* m = java_lang_invoke_MemberName::vmtarget(mname());
2103 if (m != nullptr) {
2104 oop appendix = appendix_box->obj_at(0);
2105 LogTarget(Info, methodhandles) lt;
2106 if (lt.develop_is_enabled()) {
2107 ResourceMark rm(THREAD);
2108 LogStream ls(lt);
2109 ls.print("Linked method=" INTPTR_FORMAT ": ", p2i(m));
2110 m->print_on(&ls);
2111 if (appendix != nullptr) { ls.print("appendix = "); appendix->print_on(&ls); }
2112 ls.cr();
2113 }
2114
2115 (*appendix_result) = Handle(THREAD, appendix);
2116 // the target is stored in the cpCache and if a reference to this
2117 // MemberName is dropped we need a way to make sure the
2118 // class_loader containing this method is kept alive.
2121 this_key->record_dependency(m->method_holder());
2122 return mh();
2123 }
2124 }
2125 THROW_MSG_NULL(vmSymbols::java_lang_LinkageError(), "bad value from MethodHandleNatives");
2126 }
2127
2128 Method* SystemDictionary::find_method_handle_invoker(Klass* klass,
2129 Symbol* name,
2130 Symbol* signature,
2131 Klass* accessing_klass,
2132 Handle* appendix_result,
2133 TRAPS) {
2134 guarantee(THREAD->can_call_java(), "");
2135 Handle method_type =
2136 SystemDictionary::find_method_handle_type(signature, accessing_klass, CHECK_NULL);
2137
2138 int ref_kind = JVM_REF_invokeVirtual;
2139 oop name_oop = StringTable::intern(name, CHECK_NULL);
2140 Handle name_str (THREAD, name_oop);
2141 objArrayHandle appendix_box = oopFactory::new_objArray_handle(vmClasses::Object_klass(), 1, CHECK_NULL);
2142 assert(appendix_box->obj_at(0) == nullptr, "");
2143
2144 // This should not happen. JDK code should take care of that.
2145 if (accessing_klass == nullptr || method_type.is_null()) {
2146 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "bad invokehandle");
2147 }
2148
2149 // call java.lang.invoke.MethodHandleNatives::linkMethod(... String, MethodType) -> MemberName
2150 JavaCallArguments args;
2151 args.push_oop(Handle(THREAD, accessing_klass->java_mirror()));
2152 args.push_int(ref_kind);
2153 args.push_oop(Handle(THREAD, klass->java_mirror()));
2154 args.push_oop(name_str);
2155 args.push_oop(method_type);
2156 args.push_oop(appendix_box);
2157 JavaValue result(T_OBJECT);
2158 JavaCalls::call_static(&result,
2159 vmClasses::MethodHandleNatives_klass(),
2160 vmSymbols::linkMethod_name(),
2161 vmSymbols::linkMethod_signature(),
2232 {
2233 MutexLocker ml(THREAD, InvokeMethodTypeTable_lock);
2234 o = _invoke_method_type_table->get(signature);
2235 }
2236
2237 if (o != nullptr) {
2238 oop mt = o->resolve();
2239 assert(java_lang_invoke_MethodType::is_instance(mt), "");
2240 return Handle(THREAD, mt);
2241 } else if (!THREAD->can_call_java()) {
2242 warning("SystemDictionary::find_method_handle_type called from compiler thread"); // FIXME
2243 return Handle(); // do not attempt from within compiler, unless it was cached
2244 }
2245
2246 Handle class_loader;
2247 if (accessing_klass != nullptr) {
2248 class_loader = Handle(THREAD, accessing_klass->class_loader());
2249 }
2250 bool can_be_cached = true;
2251 int npts = ArgumentCount(signature).size();
2252 objArrayHandle pts = oopFactory::new_objArray_handle(vmClasses::Class_klass(), npts, CHECK_(empty));
2253 int arg = 0;
2254 Handle rt; // the return type from the signature
2255 ResourceMark rm(THREAD);
2256 for (SignatureStream ss(signature); !ss.is_done(); ss.next()) {
2257 oop mirror = nullptr;
2258 if (can_be_cached) {
2259 // Use neutral class loader to lookup candidate classes to be placed in the cache.
2260 mirror = ss.as_java_mirror(Handle(), SignatureStream::ReturnNull, CHECK_(empty));
2261 if (mirror == nullptr || (ss.is_reference() && !is_always_visible_class(mirror))) {
2262 // Fall back to accessing_klass context.
2263 can_be_cached = false;
2264 }
2265 }
2266 if (!can_be_cached) {
2267 // Resolve, throwing a real error if it doesn't work.
2268 mirror = ss.as_java_mirror(class_loader, SignatureStream::NCDFError, CHECK_(empty));
2269 }
2270 assert(mirror != nullptr, "%s", ss.as_symbol()->as_C_string());
2271 if (ss.at_return_type())
2272 rt = Handle(THREAD, mirror);
2378 vmClasses::MethodHandleNatives_klass(),
2379 vmSymbols::linkMethodHandleConstant_name(),
2380 vmSymbols::linkMethodHandleConstant_signature(),
2381 &args, CHECK_(empty));
2382 return Handle(THREAD, result.get_oop());
2383 }
2384
2385 // Ask Java to run a bootstrap method, in order to create a dynamic call site
2386 // while linking an invokedynamic op, or compute a constant for Dynamic_info CP entry
2387 // with linkage results being stored back into the bootstrap specifier.
2388 void SystemDictionary::invoke_bootstrap_method(BootstrapInfo& bootstrap_specifier, TRAPS) {
2389 // Resolve the bootstrap specifier, its name, type, and static arguments
2390 bootstrap_specifier.resolve_bsm(CHECK);
2391
2392 // This should not happen. JDK code should take care of that.
2393 if (bootstrap_specifier.caller() == nullptr || bootstrap_specifier.type_arg().is_null()) {
2394 THROW_MSG(vmSymbols::java_lang_InternalError(), "Invalid bootstrap method invocation with no caller or type argument");
2395 }
2396
2397 bool is_indy = bootstrap_specifier.is_method_call();
2398 objArrayHandle appendix_box;
2399 if (is_indy) {
2400 // Some method calls may require an appendix argument. Arrange to receive it.
2401 appendix_box = oopFactory::new_objArray_handle(vmClasses::Object_klass(), 1, CHECK);
2402 assert(appendix_box->obj_at(0) == nullptr, "");
2403 }
2404
2405 // call condy: java.lang.invoke.MethodHandleNatives::linkDynamicConstant(caller, bsm, type, info)
2406 // indy: java.lang.invoke.MethodHandleNatives::linkCallSite(caller, bsm, name, mtype, info, &appendix)
2407 JavaCallArguments args;
2408 args.push_oop(Handle(THREAD, bootstrap_specifier.caller_mirror()));
2409 args.push_oop(bootstrap_specifier.bsm());
2410 args.push_oop(bootstrap_specifier.name_arg());
2411 args.push_oop(bootstrap_specifier.type_arg());
2412 args.push_oop(bootstrap_specifier.arg_values());
2413 if (is_indy) {
2414 args.push_oop(appendix_box);
2415 }
2416 JavaValue result(T_OBJECT);
2417 JavaCalls::call_static(&result,
2418 vmClasses::MethodHandleNatives_klass(),
2419 is_indy ? vmSymbols::linkCallSite_name() : vmSymbols::linkDynamicConstant_name(),
2420 is_indy ? vmSymbols::linkCallSite_signature() : vmSymbols::linkDynamicConstant_signature(),
2421 &args, CHECK);
|
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/constantPool.inline.hpp"
58 #include "oops/fieldStreams.inline.hpp"
59 #include "oops/inlineKlass.inline.hpp"
60 #include "oops/instanceKlass.hpp"
61 #include "oops/klass.inline.hpp"
62 #include "oops/method.inline.hpp"
63 #include "oops/objArrayKlass.hpp"
64 #include "oops/objArrayOop.inline.hpp"
65 #include "oops/oop.inline.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/atomicAccess.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/os.hpp"
78 #include "runtime/sharedRuntime.hpp"
79 #include "runtime/signature.hpp"
80 #include "runtime/synchronizer.hpp"
81 #include "services/classLoadingService.hpp"
82 #include "services/diagnosticCommand.hpp"
83 #include "services/finalizerService.hpp"
84 #include "services/threadService.hpp"
85 #include "utilities/growableArray.hpp"
86 #include "utilities/macros.hpp"
87 #include "utilities/utf8.hpp"
88 #if INCLUDE_CDS
89 #include "classfile/systemDictionaryShared.hpp"
90 #endif
91 #if INCLUDE_JFR
92 #include "jfr/jfr.hpp"
93 #endif
94
95 class InvokeMethodKey : public StackObj {
96 private:
97 Symbol* _symbol;
172 CHECK_NULL);
173 return result.get_oop();
174 }
175
176 oop SystemDictionary::get_platform_class_loader_impl(TRAPS) {
177 JavaValue result(T_OBJECT);
178 InstanceKlass* class_loader_klass = vmClasses::ClassLoader_klass();
179 JavaCalls::call_static(&result,
180 class_loader_klass,
181 vmSymbols::getPlatformClassLoader_name(),
182 vmSymbols::void_classloader_signature(),
183 CHECK_NULL);
184 return result.get_oop();
185 }
186
187 // Helper function
188 inline ClassLoaderData* class_loader_data(Handle class_loader) {
189 return ClassLoaderData::class_loader_data(class_loader());
190 }
191
192 // These migrated value classes are loaded by the bootstrap class loader but are added to the initiating
193 // loaders automatically so that fields of these types can be found and potentially flattened during
194 // field layout.
195 static void add_migrated_value_classes(ClassLoaderData* cld) {
196 JavaThread* current = JavaThread::current();
197 auto add_klass = [&] (Symbol* classname) {
198 InstanceKlass* ik = SystemDictionary::find_instance_klass(current, classname, Handle(current, nullptr));
199 assert(ik != nullptr, "Must exist");
200 SystemDictionary::add_to_initiating_loader(current, ik, cld);
201 };
202
203 MonitorLocker mu1(SystemDictionary_lock);
204 vmSymbols::migrated_class_names_do(add_klass);
205 }
206
207 ClassLoaderData* SystemDictionary::register_loader(Handle class_loader, bool create_mirror_cld) {
208 if (create_mirror_cld) {
209 // Add a new class loader data to the graph.
210 return ClassLoaderDataGraph::add(class_loader, true);
211 } else {
212 if (class_loader() == nullptr) {
213 return ClassLoaderData::the_null_class_loader_data();
214 } else {
215 bool created = false;
216 ClassLoaderData* cld = ClassLoaderDataGraph::find_or_create(class_loader, created);
217 if (created && Arguments::enable_preview()) {
218 if (CDSConfig::is_using_aot_linked_classes() && java_system_loader() == nullptr) {
219 // We are inside AOTLinkedClassBulkLoader::preload_classes().
220 //
221 // AOTLinkedClassBulkLoader will automatically initiate the loading of all archived
222 // public classes from the boot loader into platform/system loaders, so there's
223 // no need to call add_migrated_value_classes().
224 } else {
225 add_migrated_value_classes(cld);
226 }
227 }
228 return cld;
229 }
230 }
231 }
232
233 void SystemDictionary::set_system_loader(ClassLoaderData *cld) {
234 if (_java_system_loader.is_empty()) {
235 _java_system_loader = cld->class_loader_handle();
236 } else {
237 assert(_java_system_loader.resolve() == cld->class_loader(), "sanity");
238 }
239 }
240
241 void SystemDictionary::set_platform_loader(ClassLoaderData *cld) {
242 if (_java_platform_loader.is_empty()) {
243 _java_platform_loader = cld->class_loader_handle();
244 } else {
245 assert(_java_platform_loader.resolve() == cld->class_loader(), "sanity");
246 }
247 }
248
249 // ----------------------------------------------------------------------------
417 }
418 } else {
419 k = Universe::typeArrayKlass(t);
420 k = k->array_klass(ndims, CHECK_NULL);
421 }
422 return k;
423 }
424
425 static inline void log_circularity_error(Symbol* name, PlaceholderEntry* probe) {
426 LogTarget(Debug, class, load, placeholders) lt;
427 if (lt.is_enabled()) {
428 ResourceMark rm;
429 LogStream ls(lt);
430 ls.print("ClassCircularityError detected for placeholder entry %s", name->as_C_string());
431 probe->print_on(&ls);
432 ls.cr();
433 }
434 }
435
436 // Must be called for any superclass or superinterface resolution
437 // during class definition, or may be called for inline field layout processing
438 // to detect class circularity errors.
439 // superinterface callers:
440 // parse_interfaces - from defineClass
441 // superclass callers:
442 // ClassFileParser - from defineClass
443 // load_shared_class - while loading a class from shared archive
444 // resolve_instance_class_or_null:
445 // via: handle_parallel_super_load
446 // when resolving a class that has an existing placeholder with
447 // a saved superclass [i.e. a defineClass is currently in progress]
448 // If another thread is trying to resolve the class, it must do
449 // superclass checks on its own thread to catch class circularity and
450 // to avoid deadlock.
451 // inline field layout callers:
452 // The field's class must be loaded to determine layout.
453 //
454 // resolve_with_circularity_detection adds a DETECT_CIRCULARITY placeholder to the placeholder table before calling
455 // resolve_instance_class_or_null. ClassCircularityError is detected when a DETECT_CIRCULARITY or LOAD_INSTANCE
456 // placeholder for the same thread, class, and classloader is found.
457 // This can be seen with logging option: -Xlog:class+load+placeholders=debug.
458 //
459 InstanceKlass* SystemDictionary::resolve_with_circularity_detection(Symbol* class_name,
460 Symbol* next_name,
461 Handle class_loader,
462 bool is_superclass,
463 TRAPS) {
464
465 assert(next_name != nullptr, "null superclass for resolving");
466 assert(!Signature::is_array(next_name), "invalid superclass name");
467
468 ClassLoaderData* loader_data = class_loader_data(class_loader);
469
470 if (is_superclass) {
471 InstanceKlass* klassk = loader_data->dictionary()->find_class(THREAD, class_name);
472 if (klassk != nullptr) {
473 // We can come here for two reasons:
474 // (a) RedefineClasses -- the class is already loaded
475 // (b) Rarely, the class might have been loaded by a parallel thread
476 // We can do a quick check against the already assigned superclass's name and loader.
477 InstanceKlass* superk = klassk->super();
478 if (superk != nullptr &&
479 superk->name() == next_name &&
480 superk->class_loader() == class_loader()) {
481 return superk;
482 }
483 }
484 }
485
486 // can't throw error holding a lock
487 bool throw_circularity_error = false;
488 {
489 MutexLocker mu(THREAD, SystemDictionary_lock);
490
491 // Must check ClassCircularity before resolving next_name (superclass, interface, field types or speculatively preloaded argument types).
492 PlaceholderEntry* probe = PlaceholderTable::get_entry(class_name, loader_data);
493 if (probe != nullptr && probe->check_seen_thread(THREAD, PlaceholderTable::DETECT_CIRCULARITY)) {
494 log_circularity_error(class_name, probe);
495 throw_circularity_error = true;
496 }
497
498 // Make sure there's a placeholder for the class_name before resolving.
499 // This is used as a claim that this thread is currently loading superclass/classloader
500 // and for ClassCircularity checks.
501 if (!throw_circularity_error) {
502 // Be careful not to exit resolve_with_circularity_detection without removing this placeholder.
503 PlaceholderEntry* newprobe = PlaceholderTable::find_and_add(class_name,
504 loader_data,
505 PlaceholderTable::DETECT_CIRCULARITY,
506 next_name, THREAD);
507 }
508 }
509
510 if (throw_circularity_error) {
511 ResourceMark rm(THREAD);
512 THROW_MSG_NULL(vmSymbols::java_lang_ClassCircularityError(), class_name->as_C_string());
513 }
514
515 // Resolve the superclass, superinterface, field type or speculatively preloaded argument types and check results on return.
516 InstanceKlass* superk =
517 SystemDictionary::resolve_instance_class_or_null(next_name,
518 class_loader,
519 THREAD);
520
521 // Clean up placeholder entry.
522 {
523 MutexLocker mu(THREAD, SystemDictionary_lock);
524 PlaceholderTable::find_and_remove(class_name, loader_data, PlaceholderTable::DETECT_CIRCULARITY, THREAD);
525 SystemDictionary_lock->notify_all();
526 }
527
528 // Check for pending exception or null superk, and throw exception
529 if (HAS_PENDING_EXCEPTION || superk == nullptr) {
530 handle_resolution_exception(next_name, true, CHECK_NULL);
531 }
532
533 return superk;
534 }
535
933
934 InstanceKlass* SystemDictionary::resolve_from_stream(ClassFileStream* st,
935 Symbol* class_name,
936 Handle class_loader,
937 const ClassLoadInfo& cl_info,
938 TRAPS) {
939 if (cl_info.is_hidden()) {
940 return resolve_hidden_class_from_stream(st, class_name, class_loader, cl_info, CHECK_NULL);
941 } else {
942 return resolve_class_from_stream(st, class_name, class_loader, cl_info, CHECK_NULL);
943 }
944 }
945
946
947 #if INCLUDE_CDS
948 // Check if a shared class can be loaded by the specific classloader.
949 bool SystemDictionary::is_shared_class_visible(Symbol* class_name,
950 InstanceKlass* ik,
951 PackageEntry* pkg_entry,
952 Handle class_loader) {
953
954 assert(!ModuleEntryTable::javabase_moduleEntry()->is_patched(),
955 "Cannot use sharing if java.base is patched");
956
957 // (1) Check if we are loading into the same loader as in dump time.
958
959 if (ik->defined_by_boot_loader()) {
960 if (class_loader() != nullptr) {
961 return false;
962 }
963 } else if (ik->defined_by_platform_loader()) {
964 if (class_loader() != java_platform_loader()) {
965 return false;
966 }
967 } else if (ik->defined_by_app_loader()) {
968 if (class_loader() != java_system_loader()) {
969 return false;
970 }
971 } else {
972 // ik was loaded by a custom loader during dump time
973 if (class_loader_data(class_loader)->is_builtin_class_loader_data()) {
1031 assert(!mod_entry->is_patched(), "cannot load archived classes for patched module");
1032 }
1033 } else {
1034 // During dump time, this class was in a named module, but at run time, this class should be
1035 // in an unnamed module.
1036 visible = false;
1037 }
1038 } else {
1039 if (should_be_in_named_module) {
1040 // During dump time, this class was in an unnamed, but at run time, this class should be
1041 // in a named module.
1042 visible = false;
1043 } else {
1044 visible = true;
1045 }
1046 }
1047
1048 return visible;
1049 }
1050
1051 bool SystemDictionary::check_shared_class_dependency(InstanceKlass* klass, InstanceKlass* dependency,
1052 Handle class_loader, bool is_superclass, TRAPS) {
1053 assert(dependency->in_aot_cache(), "must be");
1054
1055 // Quick check if the dependency has been already loaded.
1056 // + Don't do it for unregistered classes -- they can be unloaded so
1057 // dependency->class_loader_data() could be stale.
1058 // + Don't check if loader data is null, ie. the dependency isn't fully loaded.
1059 if (!dependency->defined_by_other_loaders() && dependency->class_loader_data() != nullptr) {
1060 // Check if the dependency is loaded by the current class_loader.
1061 Symbol* name = dependency->name();
1062 InstanceKlass* check = find_instance_klass(THREAD, name, class_loader);
1063 if (check == dependency) {
1064 return true;
1065 }
1066 }
1067
1068 Klass* found = resolve_with_circularity_detection(klass->name(), dependency->name(),
1069 class_loader, is_superclass, CHECK_false);
1070 if (found == dependency) {
1071 return true;
1072 } else {
1073 // The dynamically resolved dependency is not the same as the one we used during dump time,
1074 // so we cannot use the class.
1075 return false;
1076 }
1077 }
1078
1079 bool SystemDictionary::check_shared_class_super_types(InstanceKlass* ik, Handle class_loader, TRAPS) {
1080 // Check the superclass and interfaces. They must be the same
1081 // as in dump time, because the layout of <ik> depends on
1082 // the specific layout of ik->super() and ik->local_interfaces().
1083 //
1084 // If unexpected superclass or interfaces are found, we cannot
1085 // load <ik> from the shared archive.
1086
1087 if (ik->super() != nullptr) {
1088 bool check_super = check_shared_class_dependency(ik, ik->super(),
1089 class_loader, true,
1090 CHECK_false);
1091 if (!check_super) {
1092 return false;
1093 }
1094 }
1095
1096 Array<InstanceKlass*>* interfaces = ik->local_interfaces();
1097 int num_interfaces = interfaces->length();
1098 for (int index = 0; index < num_interfaces; index++) {
1099 bool check_interface = check_shared_class_dependency(ik, interfaces->at(index), class_loader, false,
1100 CHECK_false);
1101 if (!check_interface) {
1102 return false;
1103 }
1104 }
1105
1106 return true;
1107 }
1108
1109 // Pre-load class referred to in fields with archived inline field metadata. These fields
1110 // must be checked against the resolved runtime class before the shared class can be used.
1111 bool SystemDictionary::preload_from_required_inline_field(InstanceKlass* ik, Handle class_loader, Symbol* sig, int field_index, TRAPS) {
1112 if (log_is_enabled(Info, class, preload)) {
1113 TempNewSymbol name = Signature::strip_envelope(sig);
1114 log_info(class, preload)("Preloading of class %s during loading of shared class %s. "
1115 "Cause: archived flat/null-restricted field metadata",
1116 name->as_C_string(), ik->name()->as_C_string());
1117 }
1118
1119 InstanceKlass* k = ik->get_inline_type_field_klass_or_null(field_index);
1120 bool check = check_shared_class_dependency(ik, k, class_loader, false, THREAD);
1121 if (!check) {
1122 const bool has_pending_exception = HAS_PENDING_EXCEPTION;
1123 if (log_is_enabled(Info, class, preload)) {
1124 TempNewSymbol name = Signature::strip_envelope(sig);
1125 const char* reason = has_pending_exception ?
1126 PENDING_EXCEPTION->klass()->name()->as_C_string() :
1127 "app substituted a different version";
1128 log_info(class, preload)("Preloading of class %s during loading of shared class %s "
1129 "(cause: archived flat/null-restricted field metadata) failed : %s",
1130 name->as_C_string(), ik->name()->as_C_string(), reason);
1131 }
1132 if (has_pending_exception) {
1133 CLEAR_PENDING_EXCEPTION;
1134 }
1135 return false;
1136 }
1137
1138 assert(k != nullptr, "Sanity check");
1139 if (log_is_enabled(Info, class, preload)) {
1140 TempNewSymbol name = Signature::strip_envelope(sig);
1141 log_info(class, preload)("Preloading of class %s during loading of shared class %s "
1142 "(cause: archived flat/null-restricted field metadata) succeeded",
1143 name->as_C_string(), ik->name()->as_C_string());
1144 }
1145 return true;
1146 }
1147
1148 // Optionally pre-load classes referred to in instance fields if they are found in the
1149 // LoadableDescriptors attribute. This mirrors the speculative preloading in
1150 // ClassFileParser::fetch_field_classes() when loading a class outside the archive.
1151 // Failures are ignored and do not fail shared class loading.
1152 void SystemDictionary::try_preload_from_loadable_descriptors(InstanceKlass* ik, Handle class_loader, Symbol* sig, int field_index, TRAPS) {
1153 TempNewSymbol name = Signature::strip_envelope(sig);
1154 if (name == ik->name() || !ik->is_class_in_loadable_descriptors_attribute(sig)) {
1155 return;
1156 }
1157
1158 log_info(class, preload)("Preloading of class %s during loading of shared class %s. "
1159 "Cause: field type in LoadableDescriptors attribute",
1160 name->as_C_string(), ik->name()->as_C_string());
1161 InstanceKlass* k = ik->get_inline_type_field_klass_or_null(field_index);
1162 if (k == nullptr) {
1163 SystemDictionary::resolve_with_circularity_detection(ik->name(), name, class_loader, false, THREAD);
1164 if (HAS_PENDING_EXCEPTION) {
1165 CLEAR_PENDING_EXCEPTION;
1166 }
1167 return;
1168 }
1169
1170 bool check = check_shared_class_dependency(ik, k, class_loader, false, THREAD);
1171 if (HAS_PENDING_EXCEPTION) {
1172 CLEAR_PENDING_EXCEPTION;
1173 }
1174 if (check) {
1175 log_info(class, preload)("Preloading of class %s during loading of shared class %s "
1176 "(cause: field type in LoadableDescriptors attribute) succeeded",
1177 name->as_C_string(), ik->name()->as_C_string());
1178 } else {
1179 log_info(class, preload)("Preloading of class %s during loading of shared class %s "
1180 "(cause: field type in LoadableDescriptors attribute) failed : "
1181 "app substituted a different version",
1182 name->as_C_string(), ik->name()->as_C_string());
1183 }
1184 }
1185
1186
1187 InstanceKlass* SystemDictionary::load_shared_class(InstanceKlass* ik,
1188 Handle class_loader,
1189 Handle protection_domain,
1190 const ClassFileStream *cfs,
1191 PackageEntry* pkg_entry,
1192 TRAPS) {
1193 assert(ik != nullptr, "sanity");
1194 assert(ik->in_aot_cache(), "sanity");
1195 assert(!ik->is_unshareable_info_restored(), "shared class can be restored only once");
1196 assert(AtomicAccess::add(&ik->_shared_class_load_count, 1) == 1, "shared class loaded more than once");
1197 Symbol* class_name = ik->name();
1198
1199 if (!is_shared_class_visible(class_name, ik, pkg_entry, class_loader)) {
1200 ik->set_shared_loading_failed();
1201 return nullptr;
1202 }
1203
1204 bool check = check_shared_class_super_types(ik, class_loader, CHECK_NULL);
1205 if (!check) {
1206 ik->set_shared_loading_failed();
1207 return nullptr;
1208 }
1209
1210 if (ik->has_inlined_fields() || ik->has_null_restricted_static_fields()) {
1211 for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
1212 if (fs.access_flags().is_static() && !fs.is_null_free_inline_type()) {
1213 continue;
1214 }
1215
1216 Symbol* sig = fs.signature();
1217 int field_index = fs.index();
1218
1219 if (!Signature::has_envelope(sig)) {
1220 continue;
1221 }
1222
1223 if (fs.is_flat() || fs.is_null_free_inline_type()) {
1224 bool check = preload_from_required_inline_field(ik, class_loader, sig, field_index, CHECK_NULL);
1225 if (!check) {
1226 ik->set_shared_loading_failed();
1227 return nullptr;
1228 }
1229 } else {
1230 // Optional LoadableDescriptors preloading. Failures are ignored.
1231 try_preload_from_loadable_descriptors(ik, class_loader, sig, field_index, CHECK_NULL);
1232 }
1233 }
1234 }
1235
1236 InstanceKlass* new_ik = nullptr;
1237 // CFLH check is skipped for VM hidden classes (see KlassFactory::create_from_stream).
1238 // It will be skipped for shared VM hidden lambda proxy classes.
1239 if (!ik->is_hidden()) {
1240 new_ik = KlassFactory::check_shared_class_file_load_hook(
1241 ik, class_name, class_loader, protection_domain, cfs, CHECK_NULL);
1242 }
1243 if (new_ik != nullptr) {
1244 // The class is changed by CFLH. Return the new class. The shared class is
1245 // not used.
1246 return new_ik;
1247 }
1248
1249 // Adjust methods to recover missing data. They need addresses for
1250 // interpreter entry points and their default native method address
1251 // must be reset.
1252
1253 // Shared classes are all currently loaded by either the bootstrap or
1254 // internal parallel class loaders, so this will never cause a deadlock
1255 // on a custom class loader lock.
1832 }
1833 }
1834
1835 // Update class loader data dictionary - done after check_constraint and add_to_hierarchy
1836 // have been called.
1837 void SystemDictionary::update_dictionary(JavaThread* current,
1838 InstanceKlass* k,
1839 ClassLoaderData* loader_data) {
1840 MonitorLocker mu1(SystemDictionary_lock);
1841
1842 // Make a new dictionary entry.
1843 Symbol* name = k->name();
1844 Dictionary* dictionary = loader_data->dictionary();
1845 InstanceKlass* sd_check = dictionary->find_class(current, name);
1846 if (sd_check == nullptr) {
1847 dictionary->add_klass(current, name, k);
1848 }
1849 mu1.notify_all();
1850 }
1851
1852 // Indicate that loader_data has initiated the loading of class k, which
1853 // has already been defined by a parent loader.
1854 // This API is used by AOTLinkedClassBulkLoader and to register boxing
1855 // classes from java.lang in all class loaders to enable more value
1856 // classes optimizations.
1857 void SystemDictionary::add_to_initiating_loader(JavaThread* current,
1858 InstanceKlass* k,
1859 ClassLoaderData* loader_data) {
1860 assert_locked_or_safepoint(SystemDictionary_lock);
1861 Symbol* name = k->name();
1862 Dictionary* dictionary = loader_data->dictionary();
1863 assert(k->is_loaded(), "must be");
1864 assert(k->class_loader_data() != loader_data, "only for classes defined by a parent loader");
1865 if (dictionary->find_class(current, name) == nullptr) {
1866 dictionary->add_klass(current, name, k);
1867 }
1868 }
1869
1870 // Try to find a class name using the loader constraints. The
1871 // loader constraints might know about a class that isn't fully loaded
1872 // yet and these will be ignored.
1873 Klass* SystemDictionary::find_constrained_instance_or_array_klass(
1874 Thread* current, Symbol* class_name, Handle class_loader) {
1875
1876 // First see if it has been loaded directly.
1877 Klass* klass = find_instance_or_array_klass(current, class_name, class_loader);
1878 if (klass != nullptr)
1879 return klass;
1880
1881 // Now look to see if it has been loaded elsewhere, and is subject to
1882 // a loader constraint that would require this loader to return the
1883 // klass that is already loaded.
1884 if (Signature::is_array(class_name)) {
1885 // For array classes, their Klass*s are not kept in the
1886 // constraint table. The element Klass*s are.
1887 SignatureStream ss(class_name, false);
1888 int ndims = ss.skip_array_prefix(); // skip all '['s
2220 if (!m->has_compiled_code()) {
2221 ResourceMark rm(THREAD);
2222 vm_exit_during_initialization(err_msg("Failed to initialize method %s", m->external_name()));
2223 }
2224 }
2225
2226 // There's no need to grab the InvokeMethodIntrinsicTable_lock, as we are still very early in
2227 // VM start-up -- in init_globals2() -- so we are still running a single Java thread. It's not
2228 // possible to have a contention.
2229 const int iid_as_int = vmIntrinsics::as_int(m->intrinsic_id());
2230 InvokeMethodKey key(m->signature(), iid_as_int);
2231 bool created = _invoke_method_intrinsic_table->put(key, m());
2232 assert(created, "unexpected contention");
2233 }
2234 }
2235 #endif // INCLUDE_CDS
2236
2237 // Helper for unpacking the return value from linkMethod and linkCallSite.
2238 static Method* unpack_method_and_appendix(Handle mname,
2239 Klass* accessing_klass,
2240 refArrayHandle appendix_box,
2241 Handle* appendix_result,
2242 TRAPS) {
2243 if (mname.not_null()) {
2244 Method* m = java_lang_invoke_MemberName::vmtarget(mname());
2245 if (m != nullptr) {
2246 oop appendix = appendix_box->obj_at(0);
2247 LogTarget(Info, methodhandles) lt;
2248 if (lt.develop_is_enabled()) {
2249 ResourceMark rm(THREAD);
2250 LogStream ls(lt);
2251 ls.print("Linked method=" INTPTR_FORMAT ": ", p2i(m));
2252 m->print_on(&ls);
2253 if (appendix != nullptr) { ls.print("appendix = "); appendix->print_on(&ls); }
2254 ls.cr();
2255 }
2256
2257 (*appendix_result) = Handle(THREAD, appendix);
2258 // the target is stored in the cpCache and if a reference to this
2259 // MemberName is dropped we need a way to make sure the
2260 // class_loader containing this method is kept alive.
2263 this_key->record_dependency(m->method_holder());
2264 return mh();
2265 }
2266 }
2267 THROW_MSG_NULL(vmSymbols::java_lang_LinkageError(), "bad value from MethodHandleNatives");
2268 }
2269
2270 Method* SystemDictionary::find_method_handle_invoker(Klass* klass,
2271 Symbol* name,
2272 Symbol* signature,
2273 Klass* accessing_klass,
2274 Handle* appendix_result,
2275 TRAPS) {
2276 guarantee(THREAD->can_call_java(), "");
2277 Handle method_type =
2278 SystemDictionary::find_method_handle_type(signature, accessing_klass, CHECK_NULL);
2279
2280 int ref_kind = JVM_REF_invokeVirtual;
2281 oop name_oop = StringTable::intern(name, CHECK_NULL);
2282 Handle name_str (THREAD, name_oop);
2283 refArrayHandle appendix_box = oopFactory::new_refArray_handle(vmClasses::Object_klass(), 1, CHECK_NULL);
2284 assert(appendix_box->obj_at(0) == nullptr, "");
2285
2286 // This should not happen. JDK code should take care of that.
2287 if (accessing_klass == nullptr || method_type.is_null()) {
2288 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "bad invokehandle");
2289 }
2290
2291 // call java.lang.invoke.MethodHandleNatives::linkMethod(... String, MethodType) -> MemberName
2292 JavaCallArguments args;
2293 args.push_oop(Handle(THREAD, accessing_klass->java_mirror()));
2294 args.push_int(ref_kind);
2295 args.push_oop(Handle(THREAD, klass->java_mirror()));
2296 args.push_oop(name_str);
2297 args.push_oop(method_type);
2298 args.push_oop(appendix_box);
2299 JavaValue result(T_OBJECT);
2300 JavaCalls::call_static(&result,
2301 vmClasses::MethodHandleNatives_klass(),
2302 vmSymbols::linkMethod_name(),
2303 vmSymbols::linkMethod_signature(),
2374 {
2375 MutexLocker ml(THREAD, InvokeMethodTypeTable_lock);
2376 o = _invoke_method_type_table->get(signature);
2377 }
2378
2379 if (o != nullptr) {
2380 oop mt = o->resolve();
2381 assert(java_lang_invoke_MethodType::is_instance(mt), "");
2382 return Handle(THREAD, mt);
2383 } else if (!THREAD->can_call_java()) {
2384 warning("SystemDictionary::find_method_handle_type called from compiler thread"); // FIXME
2385 return Handle(); // do not attempt from within compiler, unless it was cached
2386 }
2387
2388 Handle class_loader;
2389 if (accessing_klass != nullptr) {
2390 class_loader = Handle(THREAD, accessing_klass->class_loader());
2391 }
2392 bool can_be_cached = true;
2393 int npts = ArgumentCount(signature).size();
2394 refArrayHandle pts = oopFactory::new_refArray_handle(vmClasses::Class_klass(), npts, CHECK_(empty));
2395 int arg = 0;
2396 Handle rt; // the return type from the signature
2397 ResourceMark rm(THREAD);
2398 for (SignatureStream ss(signature); !ss.is_done(); ss.next()) {
2399 oop mirror = nullptr;
2400 if (can_be_cached) {
2401 // Use neutral class loader to lookup candidate classes to be placed in the cache.
2402 mirror = ss.as_java_mirror(Handle(), SignatureStream::ReturnNull, CHECK_(empty));
2403 if (mirror == nullptr || (ss.is_reference() && !is_always_visible_class(mirror))) {
2404 // Fall back to accessing_klass context.
2405 can_be_cached = false;
2406 }
2407 }
2408 if (!can_be_cached) {
2409 // Resolve, throwing a real error if it doesn't work.
2410 mirror = ss.as_java_mirror(class_loader, SignatureStream::NCDFError, CHECK_(empty));
2411 }
2412 assert(mirror != nullptr, "%s", ss.as_symbol()->as_C_string());
2413 if (ss.at_return_type())
2414 rt = Handle(THREAD, mirror);
2520 vmClasses::MethodHandleNatives_klass(),
2521 vmSymbols::linkMethodHandleConstant_name(),
2522 vmSymbols::linkMethodHandleConstant_signature(),
2523 &args, CHECK_(empty));
2524 return Handle(THREAD, result.get_oop());
2525 }
2526
2527 // Ask Java to run a bootstrap method, in order to create a dynamic call site
2528 // while linking an invokedynamic op, or compute a constant for Dynamic_info CP entry
2529 // with linkage results being stored back into the bootstrap specifier.
2530 void SystemDictionary::invoke_bootstrap_method(BootstrapInfo& bootstrap_specifier, TRAPS) {
2531 // Resolve the bootstrap specifier, its name, type, and static arguments
2532 bootstrap_specifier.resolve_bsm(CHECK);
2533
2534 // This should not happen. JDK code should take care of that.
2535 if (bootstrap_specifier.caller() == nullptr || bootstrap_specifier.type_arg().is_null()) {
2536 THROW_MSG(vmSymbols::java_lang_InternalError(), "Invalid bootstrap method invocation with no caller or type argument");
2537 }
2538
2539 bool is_indy = bootstrap_specifier.is_method_call();
2540 refArrayHandle appendix_box;
2541 if (is_indy) {
2542 // Some method calls may require an appendix argument. Arrange to receive it.
2543 appendix_box = oopFactory::new_refArray_handle(vmClasses::Object_klass(), 1, CHECK);
2544 assert(appendix_box->obj_at(0) == nullptr, "");
2545 }
2546
2547 // call condy: java.lang.invoke.MethodHandleNatives::linkDynamicConstant(caller, bsm, type, info)
2548 // indy: java.lang.invoke.MethodHandleNatives::linkCallSite(caller, bsm, name, mtype, info, &appendix)
2549 JavaCallArguments args;
2550 args.push_oop(Handle(THREAD, bootstrap_specifier.caller_mirror()));
2551 args.push_oop(bootstrap_specifier.bsm());
2552 args.push_oop(bootstrap_specifier.name_arg());
2553 args.push_oop(bootstrap_specifier.type_arg());
2554 args.push_oop(bootstrap_specifier.arg_values());
2555 if (is_indy) {
2556 args.push_oop(appendix_box);
2557 }
2558 JavaValue result(T_OBJECT);
2559 JavaCalls::call_static(&result,
2560 vmClasses::MethodHandleNatives_klass(),
2561 is_indy ? vmSymbols::linkCallSite_name() : vmSymbols::linkDynamicConstant_name(),
2562 is_indy ? vmSymbols::linkCallSite_signature() : vmSymbols::linkDynamicConstant_signature(),
2563 &args, CHECK);
|