jdk
1962 строки · 86.6 Кб
1/*
2* Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*
5* This code is free software; you can redistribute it and/or modify it
6* under the terms of the GNU General Public License version 2 only, as
7* published by the Free Software Foundation.
8*
9* This code is distributed in the hope that it will be useful, but WITHOUT
10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12* version 2 for more details (a copy is included in the LICENSE file that
13* accompanied this code).
14*
15* You should have received a copy of the GNU General Public License version
16* 2 along with this work; if not, write to the Free Software Foundation,
17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18*
19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20* or visit www.oracle.com if you need additional information or have any
21* questions.
22*
23*/
24
25#include "precompiled.hpp"
26#include "cds/archiveUtils.hpp"
27#include "classfile/classLoader.hpp"
28#include "classfile/defaultMethods.hpp"
29#include "classfile/javaClasses.hpp"
30#include "classfile/systemDictionary.hpp"
31#include "classfile/vmClasses.hpp"
32#include "classfile/vmSymbols.hpp"
33#include "compiler/compilationPolicy.hpp"
34#include "compiler/compileBroker.hpp"
35#include "gc/shared/collectedHeap.inline.hpp"
36#include "interpreter/bootstrapInfo.hpp"
37#include "interpreter/bytecode.hpp"
38#include "interpreter/interpreterRuntime.hpp"
39#include "interpreter/linkResolver.hpp"
40#include "jvm.h"
41#include "logging/log.hpp"
42#include "logging/logStream.hpp"
43#include "memory/resourceArea.hpp"
44#include "oops/constantPool.inline.hpp"
45#include "oops/cpCache.inline.hpp"
46#include "oops/instanceKlass.inline.hpp"
47#include "oops/klass.inline.hpp"
48#include "oops/method.inline.hpp"
49#include "oops/objArrayKlass.hpp"
50#include "oops/objArrayOop.hpp"
51#include "oops/oop.inline.hpp"
52#include "oops/resolvedIndyEntry.hpp"
53#include "oops/symbolHandle.hpp"
54#include "prims/jvmtiExport.hpp"
55#include "prims/methodHandles.hpp"
56#include "runtime/fieldDescriptor.inline.hpp"
57#include "runtime/frame.inline.hpp"
58#include "runtime/handles.inline.hpp"
59#include "runtime/javaThread.inline.hpp"
60#include "runtime/perfData.hpp"
61#include "runtime/reflection.hpp"
62#include "runtime/safepointVerifiers.hpp"
63#include "runtime/sharedRuntime.hpp"
64#include "runtime/signature.hpp"
65#include "runtime/vmThread.hpp"
66#include "utilities/macros.hpp"
67#if INCLUDE_JFR
68#include "jfr/jfr.hpp"
69#endif
70
71//------------------------------------------------------------------------------------------------------------------------
72// Implementation of CallInfo
73
74
75void CallInfo::set_static(Klass* resolved_klass, const methodHandle& resolved_method, TRAPS) {
76int vtable_index = Method::nonvirtual_vtable_index;
77set_common(resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK);
78}
79
80
81void CallInfo::set_interface(Klass* resolved_klass,
82const methodHandle& resolved_method,
83const methodHandle& selected_method,
84int itable_index, TRAPS) {
85// This is only called for interface methods. If the resolved_method
86// comes from java/lang/Object, it can be the subject of a virtual call, so
87// we should pick the vtable index from the resolved method.
88// In that case, the caller must call set_virtual instead of set_interface.
89assert(resolved_method->method_holder()->is_interface(), "");
90assert(itable_index == resolved_method()->itable_index(), "");
91set_common(resolved_klass, resolved_method, selected_method, CallInfo::itable_call, itable_index, CHECK);
92}
93
94void CallInfo::set_virtual(Klass* resolved_klass,
95const methodHandle& resolved_method,
96const methodHandle& selected_method,
97int vtable_index, TRAPS) {
98assert(vtable_index >= 0 || vtable_index == Method::nonvirtual_vtable_index, "valid index");
99assert(vtable_index < 0 || !resolved_method->has_vtable_index() || vtable_index == resolved_method->vtable_index(), "");
100CallKind kind = (vtable_index >= 0 && !resolved_method->can_be_statically_bound() ? CallInfo::vtable_call : CallInfo::direct_call);
101set_common(resolved_klass, resolved_method, selected_method, kind, vtable_index, CHECK);
102assert(!resolved_method->is_compiled_lambda_form(), "these must be handled via an invokehandle call");
103}
104
105void CallInfo::set_handle(Klass* resolved_klass,
106const methodHandle& resolved_method,
107Handle resolved_appendix, TRAPS) {
108guarantee(resolved_method.not_null(), "resolved method is null");
109assert(resolved_method->intrinsic_id() == vmIntrinsics::_invokeBasic ||
110resolved_method->is_compiled_lambda_form(),
111"linkMethod must return one of these");
112int vtable_index = Method::nonvirtual_vtable_index;
113assert(!resolved_method->has_vtable_index(), "");
114set_common(resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK);
115_resolved_appendix = resolved_appendix;
116}
117
118// Redefinition safepoint may have updated the method. Make sure the new version of the method is returned.
119// Callers are responsible for not safepointing and storing this method somewhere safe where redefinition
120// can replace it if runs again. Safe places are constant pool cache and code cache metadata.
121// The old method is safe in CallInfo since its a methodHandle (it won't get deleted), and accessed with these
122// accessors.
123Method* CallInfo::resolved_method() const {
124if (JvmtiExport::can_hotswap_or_post_breakpoint() && _resolved_method->is_old()) {
125return _resolved_method->get_new_method();
126} else {
127return _resolved_method();
128}
129}
130
131Method* CallInfo::selected_method() const {
132if (JvmtiExport::can_hotswap_or_post_breakpoint() && _selected_method->is_old()) {
133return _selected_method->get_new_method();
134} else {
135return _selected_method();
136}
137}
138
139void CallInfo::set_common(Klass* resolved_klass,
140const methodHandle& resolved_method,
141const methodHandle& selected_method,
142CallKind kind,
143int index,
144TRAPS) {
145if (selected_method.not_null()) {
146assert(resolved_method->signature() == selected_method->signature(), "signatures must correspond");
147}
148_resolved_klass = resolved_klass;
149_resolved_method = resolved_method;
150_selected_method = selected_method;
151_call_kind = kind;
152_call_index = index;
153_resolved_appendix = Handle();
154DEBUG_ONLY(verify()); // verify before making side effects
155
156if (selected_method.not_null()) {
157CompilationPolicy::compile_if_required(selected_method, THREAD);
158}
159}
160
161// utility query for unreflecting a method
162CallInfo::CallInfo(Method* resolved_method, Klass* resolved_klass, TRAPS) {
163Klass* resolved_method_holder = resolved_method->method_holder();
164if (resolved_klass == nullptr) { // 2nd argument defaults to holder of 1st
165resolved_klass = resolved_method_holder;
166}
167_resolved_klass = resolved_klass;
168_resolved_method = methodHandle(THREAD, resolved_method);
169_selected_method = methodHandle(THREAD, resolved_method);
170// classify:
171CallKind kind = CallInfo::unknown_kind;
172int index = resolved_method->vtable_index();
173if (resolved_method->can_be_statically_bound()) {
174kind = CallInfo::direct_call;
175} else if (!resolved_method_holder->is_interface()) {
176// Could be an Object method inherited into an interface, but still a vtable call.
177kind = CallInfo::vtable_call;
178} else if (!resolved_klass->is_interface()) {
179// A default or miranda method. Compute the vtable index.
180index = LinkResolver::vtable_index_of_interface_method(resolved_klass, _resolved_method);
181assert(index >= 0 , "we should have valid vtable index at this point");
182
183kind = CallInfo::vtable_call;
184} else if (resolved_method->has_vtable_index()) {
185// Can occur if an interface redeclares a method of Object.
186
187#ifdef ASSERT
188// Ensure that this is really the case.
189Klass* object_klass = vmClasses::Object_klass();
190Method * object_resolved_method = object_klass->vtable().method_at(index);
191assert(object_resolved_method->name() == resolved_method->name(),
192"Object and interface method names should match at vtable index %d, %s != %s",
193index, object_resolved_method->name()->as_C_string(), resolved_method->name()->as_C_string());
194assert(object_resolved_method->signature() == resolved_method->signature(),
195"Object and interface method signatures should match at vtable index %d, %s != %s",
196index, object_resolved_method->signature()->as_C_string(), resolved_method->signature()->as_C_string());
197#endif // ASSERT
198
199kind = CallInfo::vtable_call;
200} else {
201// A regular interface call.
202kind = CallInfo::itable_call;
203index = resolved_method->itable_index();
204}
205assert(index == Method::nonvirtual_vtable_index || index >= 0, "bad index %d", index);
206_call_kind = kind;
207_call_index = index;
208_resolved_appendix = Handle();
209// Find or create a ResolvedMethod instance for this Method*
210set_resolved_method_name(CHECK);
211
212DEBUG_ONLY(verify());
213}
214
215void CallInfo::set_resolved_method_name(TRAPS) {
216assert(_resolved_method() != nullptr, "Should already have a Method*");
217oop rmethod_name = java_lang_invoke_ResolvedMethodName::find_resolved_method(_resolved_method, CHECK);
218_resolved_method_name = Handle(THREAD, rmethod_name);
219}
220
221#ifdef ASSERT
222void CallInfo::verify() {
223switch (call_kind()) { // the meaning and allowed value of index depends on kind
224case CallInfo::direct_call:
225if (_call_index == Method::nonvirtual_vtable_index) break;
226// else fall through to check vtable index:
227case CallInfo::vtable_call:
228assert(resolved_klass()->verify_vtable_index(_call_index), "");
229break;
230case CallInfo::itable_call:
231assert(resolved_method()->method_holder()->verify_itable_index(_call_index), "");
232break;
233case CallInfo::unknown_kind:
234assert(call_kind() != CallInfo::unknown_kind, "CallInfo must be set");
235break;
236default:
237fatal("Unexpected call kind %d", call_kind());
238}
239}
240#endif // ASSERT
241
242#ifndef PRODUCT
243void CallInfo::print() {
244ResourceMark rm;
245const char* kindstr;
246switch (_call_kind) {
247case direct_call: kindstr = "direct"; break;
248case vtable_call: kindstr = "vtable"; break;
249case itable_call: kindstr = "itable"; break;
250default : kindstr = "unknown"; break;
251}
252tty->print_cr("Call %s@%d %s", kindstr, _call_index,
253_resolved_method.is_null() ? "(none)" : _resolved_method->name_and_sig_as_C_string());
254}
255#endif
256
257//------------------------------------------------------------------------------------------------------------------------
258// Implementation of LinkInfo
259
260LinkInfo::LinkInfo(const constantPoolHandle& pool, int index, const methodHandle& current_method, Bytecodes::Code code, TRAPS) {
261// resolve klass
262_resolved_klass = pool->klass_ref_at(index, code, CHECK);
263
264// Get name, signature, and static klass
265_name = pool->name_ref_at(index, code);
266_signature = pool->signature_ref_at(index, code);
267_tag = pool->tag_ref_at(index, code);
268_current_klass = pool->pool_holder();
269_current_method = current_method;
270
271// Coming from the constant pool always checks access
272_check_access = true;
273_check_loader_constraints = true;
274}
275
276LinkInfo::LinkInfo(const constantPoolHandle& pool, int index, Bytecodes::Code code, TRAPS) {
277// resolve klass
278_resolved_klass = pool->klass_ref_at(index, code, CHECK);
279
280// Get name, signature, and static klass
281_name = pool->name_ref_at(index, code);
282_signature = pool->signature_ref_at(index, code);
283_tag = pool->tag_ref_at(index, code);
284_current_klass = pool->pool_holder();
285_current_method = methodHandle();
286
287// Coming from the constant pool always checks access
288_check_access = true;
289_check_loader_constraints = true;
290}
291
292#ifndef PRODUCT
293void LinkInfo::print() {
294ResourceMark rm;
295tty->print_cr("Link resolved_klass=%s name=%s signature=%s current_klass=%s check_access=%s check_loader_constraints=%s",
296_resolved_klass->name()->as_C_string(),
297_name->as_C_string(),
298_signature->as_C_string(),
299_current_klass == nullptr ? "(none)" : _current_klass->name()->as_C_string(),
300_check_access ? "true" : "false",
301_check_loader_constraints ? "true" : "false");
302
303}
304#endif // PRODUCT
305//------------------------------------------------------------------------------------------------------------------------
306// Klass resolution
307
308void LinkResolver::check_klass_accessibility(Klass* ref_klass, Klass* sel_klass, TRAPS) {
309Klass* base_klass = sel_klass;
310if (sel_klass->is_objArray_klass()) {
311base_klass = ObjArrayKlass::cast(sel_klass)->bottom_klass();
312}
313// The element type could be a typeArray - we only need the access
314// check if it is a reference to another class.
315if (!base_klass->is_instance_klass()) {
316return; // no relevant check to do
317}
318
319Reflection::VerifyClassAccessResults vca_result =
320Reflection::verify_class_access(ref_klass, InstanceKlass::cast(base_klass), true);
321if (vca_result != Reflection::ACCESS_OK) {
322ResourceMark rm(THREAD);
323char* msg = Reflection::verify_class_access_msg(ref_klass,
324InstanceKlass::cast(base_klass),
325vca_result);
326bool same_module = (base_klass->module() == ref_klass->module());
327if (msg == nullptr) {
328Exceptions::fthrow(
329THREAD_AND_LOCATION,
330vmSymbols::java_lang_IllegalAccessError(),
331"failed to access class %s from class %s (%s%s%s)",
332base_klass->external_name(),
333ref_klass->external_name(),
334(same_module) ? base_klass->joint_in_module_of_loader(ref_klass) : base_klass->class_in_module_of_loader(),
335(same_module) ? "" : "; ",
336(same_module) ? "" : ref_klass->class_in_module_of_loader());
337} else {
338// Use module specific message returned by verify_class_access_msg().
339Exceptions::fthrow(
340THREAD_AND_LOCATION,
341vmSymbols::java_lang_IllegalAccessError(),
342"%s", msg);
343}
344}
345}
346
347//------------------------------------------------------------------------------------------------------------------------
348// Method resolution
349//
350// According to JVM spec. $5.4.3c & $5.4.3d
351
352// Look up method in klasses, including static methods
353// Then look up local default methods
354Method* LinkResolver::lookup_method_in_klasses(const LinkInfo& link_info,
355bool checkpolymorphism,
356bool in_imethod_resolve) {
357NoSafepointVerifier nsv; // Method* returned may not be reclaimed
358
359Klass* klass = link_info.resolved_klass();
360Symbol* name = link_info.name();
361Symbol* signature = link_info.signature();
362
363// Ignore overpasses so statics can be found during resolution
364Method* result = klass->uncached_lookup_method(name, signature, Klass::OverpassLookupMode::skip);
365
366if (klass->is_array_klass()) {
367// Only consider klass and super klass for arrays
368return result;
369}
370
371InstanceKlass* ik = InstanceKlass::cast(klass);
372
373// JDK 8, JVMS 5.4.3.4: Interface method resolution should
374// ignore static and non-public methods of java.lang.Object,
375// like clone and finalize.
376if (in_imethod_resolve &&
377result != nullptr &&
378ik->is_interface() &&
379(result->is_static() || !result->is_public()) &&
380result->method_holder() == vmClasses::Object_klass()) {
381result = nullptr;
382}
383
384// Before considering default methods, check for an overpass in the
385// current class if a method has not been found.
386if (result == nullptr) {
387result = ik->find_method(name, signature);
388}
389
390if (result == nullptr) {
391Array<Method*>* default_methods = ik->default_methods();
392if (default_methods != nullptr) {
393result = InstanceKlass::find_method(default_methods, name, signature);
394}
395}
396
397if (checkpolymorphism && result != nullptr) {
398vmIntrinsics::ID iid = result->intrinsic_id();
399if (MethodHandles::is_signature_polymorphic(iid)) {
400// Do not link directly to these. The VM must produce a synthetic one using lookup_polymorphic_method.
401return nullptr;
402}
403}
404return result;
405}
406
407// returns first instance method
408// Looks up method in classes, then looks up local default methods
409Method* LinkResolver::lookup_instance_method_in_klasses(Klass* klass,
410Symbol* name,
411Symbol* signature,
412Klass::PrivateLookupMode private_mode) {
413Method* result = klass->uncached_lookup_method(name, signature, Klass::OverpassLookupMode::find, private_mode);
414
415while (result != nullptr && result->is_static() && result->method_holder()->super() != nullptr) {
416Klass* super_klass = result->method_holder()->super();
417result = super_klass->uncached_lookup_method(name, signature, Klass::OverpassLookupMode::find, private_mode);
418}
419
420if (klass->is_array_klass()) {
421// Only consider klass and super klass for arrays
422return result;
423}
424
425if (result == nullptr) {
426Array<Method*>* default_methods = InstanceKlass::cast(klass)->default_methods();
427if (default_methods != nullptr) {
428result = InstanceKlass::find_method(default_methods, name, signature);
429assert(result == nullptr || !result->is_static(), "static defaults not allowed");
430}
431}
432return result;
433}
434
435int LinkResolver::vtable_index_of_interface_method(Klass* klass, const methodHandle& resolved_method) {
436InstanceKlass* ik = InstanceKlass::cast(klass);
437return ik->vtable_index_of_interface_method(resolved_method());
438}
439
440Method* LinkResolver::lookup_method_in_interfaces(const LinkInfo& cp_info) {
441InstanceKlass *ik = InstanceKlass::cast(cp_info.resolved_klass());
442
443// Specify 'true' in order to skip default methods when searching the
444// interfaces. Function lookup_method_in_klasses() already looked for
445// the method in the default methods table.
446return ik->lookup_method_in_all_interfaces(cp_info.name(), cp_info.signature(), Klass::DefaultsLookupMode::skip);
447}
448
449Method* LinkResolver::lookup_polymorphic_method(const LinkInfo& link_info,
450Handle *appendix_result_or_null,
451TRAPS) {
452ResourceMark rm(THREAD);
453Klass* klass = link_info.resolved_klass();
454Symbol* name = link_info.name();
455Symbol* full_signature = link_info.signature();
456LogTarget(Info, methodhandles) lt_mh;
457
458vmIntrinsics::ID iid = MethodHandles::signature_polymorphic_name_id(name);
459log_info(methodhandles)("lookup_polymorphic_method iid=%s %s.%s%s",
460vmIntrinsics::name_at(iid), klass->external_name(),
461name->as_C_string(), full_signature->as_C_string());
462if ((klass == vmClasses::MethodHandle_klass() ||
463klass == vmClasses::VarHandle_klass()) &&
464iid != vmIntrinsics::_none) {
465if (MethodHandles::is_signature_polymorphic_intrinsic(iid)) {
466// Most of these do not need an up-call to Java to resolve, so can be done anywhere.
467// Do not erase last argument type (MemberName) if it is a static linkTo method.
468bool keep_last_arg = MethodHandles::is_signature_polymorphic_static(iid);
469TempNewSymbol basic_signature =
470MethodHandles::lookup_basic_type_signature(full_signature, keep_last_arg);
471log_info(methodhandles)("lookup_polymorphic_method %s %s => basic %s",
472name->as_C_string(),
473full_signature->as_C_string(),
474basic_signature->as_C_string());
475Method* result = SystemDictionary::find_method_handle_intrinsic(iid,
476basic_signature,
477CHECK_NULL);
478if (result != nullptr) {
479assert(result->is_method_handle_intrinsic(), "MH.invokeBasic or MH.linkTo* intrinsic");
480assert(result->intrinsic_id() != vmIntrinsics::_invokeGeneric, "wrong place to find this");
481assert(basic_signature == result->signature(), "predict the result signature");
482if (lt_mh.is_enabled()) {
483LogStream ls(lt_mh);
484ls.print("lookup_polymorphic_method => intrinsic ");
485result->print_on(&ls);
486}
487}
488return result;
489} else if (iid == vmIntrinsics::_invokeGeneric
490&& THREAD->can_call_java()
491&& appendix_result_or_null != nullptr) {
492// This is a method with type-checking semantics.
493// We will ask Java code to spin an adapter method for it.
494if (!MethodHandles::enabled()) {
495// Make sure the Java part of the runtime has been booted up.
496Klass* natives = vmClasses::MethodHandleNatives_klass();
497if (natives == nullptr || InstanceKlass::cast(natives)->is_not_initialized()) {
498SystemDictionary::resolve_or_fail(vmSymbols::java_lang_invoke_MethodHandleNatives(),
499Handle(),
500Handle(),
501true,
502CHECK_NULL);
503}
504}
505
506Handle appendix;
507Method* result = SystemDictionary::find_method_handle_invoker(klass,
508name,
509full_signature,
510link_info.current_klass(),
511&appendix,
512CHECK_NULL);
513if (lt_mh.is_enabled()) {
514LogStream ls(lt_mh);
515ls.print("lookup_polymorphic_method => (via Java) ");
516result->print_on(&ls);
517ls.print(" lookup_polymorphic_method => appendix = ");
518appendix.is_null() ? ls.print_cr("(none)") : appendix->print_on(&ls);
519}
520if (result != nullptr) {
521#ifdef ASSERT
522ResourceMark rm(THREAD);
523
524TempNewSymbol basic_signature =
525MethodHandles::lookup_basic_type_signature(full_signature);
526int actual_size_of_params = result->size_of_parameters();
527int expected_size_of_params = ArgumentSizeComputer(basic_signature).size();
528// +1 for MethodHandle.this, +1 for trailing MethodType
529if (!MethodHandles::is_signature_polymorphic_static(iid)) expected_size_of_params += 1;
530if (appendix.not_null()) expected_size_of_params += 1;
531if (actual_size_of_params != expected_size_of_params) {
532tty->print_cr("*** basic_signature=%s", basic_signature->as_C_string());
533tty->print_cr("*** result for %s: ", vmIntrinsics::name_at(iid));
534result->print();
535}
536assert(actual_size_of_params == expected_size_of_params,
537"%d != %d", actual_size_of_params, expected_size_of_params);
538#endif //ASSERT
539
540assert(appendix_result_or_null != nullptr, "");
541(*appendix_result_or_null) = appendix;
542}
543return result;
544}
545}
546return nullptr;
547}
548
549static void print_nest_host_error_on(stringStream* ss, Klass* ref_klass, Klass* sel_klass) {
550assert(ref_klass->is_instance_klass(), "must be");
551assert(sel_klass->is_instance_klass(), "must be");
552InstanceKlass* ref_ik = InstanceKlass::cast(ref_klass);
553InstanceKlass* sel_ik = InstanceKlass::cast(sel_klass);
554const char* nest_host_error_1 = ref_ik->nest_host_error();
555const char* nest_host_error_2 = sel_ik->nest_host_error();
556if (nest_host_error_1 != nullptr || nest_host_error_2 != nullptr) {
557ss->print(", (%s%s%s)",
558(nest_host_error_1 != nullptr) ? nest_host_error_1 : "",
559(nest_host_error_1 != nullptr && nest_host_error_2 != nullptr) ? ", " : "",
560(nest_host_error_2 != nullptr) ? nest_host_error_2 : "");
561}
562}
563
564void LinkResolver::check_method_accessability(Klass* ref_klass,
565Klass* resolved_klass,
566Klass* sel_klass,
567const methodHandle& sel_method,
568TRAPS) {
569
570AccessFlags flags = sel_method->access_flags();
571
572// Special case: arrays always override "clone". JVMS 2.15.
573// If the resolved klass is an array class, and the declaring class
574// is java.lang.Object and the method is "clone", set the flags
575// to public.
576//
577// We'll check for the method name first, as that's most likely
578// to be false (so we'll short-circuit out of these tests).
579if (sel_method->name() == vmSymbols::clone_name() &&
580sel_klass == vmClasses::Object_klass() &&
581resolved_klass->is_array_klass()) {
582// We need to change "protected" to "public".
583assert(flags.is_protected(), "clone not protected?");
584jint new_flags = flags.as_int();
585new_flags = new_flags & (~JVM_ACC_PROTECTED);
586new_flags = new_flags | JVM_ACC_PUBLIC;
587flags.set_flags(new_flags);
588}
589// assert(extra_arg_result_or_null != nullptr, "must be able to return extra argument");
590
591bool can_access = Reflection::verify_member_access(ref_klass,
592resolved_klass,
593sel_klass,
594flags,
595true, false, CHECK);
596// Any existing exceptions that may have been thrown
597// have been allowed to propagate.
598if (!can_access) {
599ResourceMark rm(THREAD);
600stringStream ss;
601bool same_module = (sel_klass->module() == ref_klass->module());
602ss.print("class %s tried to access %s%s%smethod '%s' (%s%s%s)",
603ref_klass->external_name(),
604sel_method->is_abstract() ? "abstract " : "",
605sel_method->is_protected() ? "protected " : "",
606sel_method->is_private() ? "private " : "",
607sel_method->external_name(),
608(same_module) ? ref_klass->joint_in_module_of_loader(sel_klass) : ref_klass->class_in_module_of_loader(),
609(same_module) ? "" : "; ",
610(same_module) ? "" : sel_klass->class_in_module_of_loader()
611);
612
613// For private access see if there was a problem with nest host
614// resolution, and if so report that as part of the message.
615if (sel_method->is_private()) {
616print_nest_host_error_on(&ss, ref_klass, sel_klass);
617}
618
619Exceptions::fthrow(THREAD_AND_LOCATION,
620vmSymbols::java_lang_IllegalAccessError(),
621"%s",
622ss.as_string()
623);
624return;
625}
626}
627
628void LinkResolver::resolve_continuation_enter(CallInfo& callinfo, TRAPS) {
629Klass* resolved_klass = vmClasses::Continuation_klass();
630Symbol* method_name = vmSymbols::enter_name();
631Symbol* method_signature = vmSymbols::continuationEnter_signature();
632Klass* current_klass = resolved_klass;
633LinkInfo link_info(resolved_klass, method_name, method_signature, current_klass);
634Method* resolved_method = resolve_method(link_info, Bytecodes::_invokestatic, CHECK);
635callinfo.set_static(resolved_klass, methodHandle(THREAD, resolved_method), CHECK);
636}
637
638Method* LinkResolver::resolve_method_statically(Bytecodes::Code code,
639const constantPoolHandle& pool, int index, TRAPS) {
640// This method is used only
641// (1) in C2 from InlineTree::ok_to_inline (via ciMethod::check_call),
642// and
643// (2) in Bytecode_invoke::static_target
644// It appears to fail when applied to an invokeinterface call site.
645// FIXME: Remove this method and ciMethod::check_call; refactor to use the other LinkResolver entry points.
646// resolve klass
647if (code == Bytecodes::_invokedynamic) {
648Klass* resolved_klass = vmClasses::MethodHandle_klass();
649Symbol* method_name = vmSymbols::invoke_name();
650Symbol* method_signature = pool->signature_ref_at(index, code);
651Klass* current_klass = pool->pool_holder();
652LinkInfo link_info(resolved_klass, method_name, method_signature, current_klass);
653return resolve_method(link_info, code, THREAD);
654}
655
656LinkInfo link_info(pool, index, methodHandle(), code, CHECK_NULL);
657Klass* resolved_klass = link_info.resolved_klass();
658
659if (pool->has_preresolution()
660|| ((resolved_klass == vmClasses::MethodHandle_klass() || resolved_klass == vmClasses::VarHandle_klass()) &&
661MethodHandles::is_signature_polymorphic_name(resolved_klass, link_info.name()))) {
662Method* result = ConstantPool::method_at_if_loaded(pool, index);
663if (result != nullptr) {
664return result;
665}
666}
667
668if (code == Bytecodes::_invokeinterface) {
669return resolve_interface_method(link_info, code, THREAD);
670} else if (code == Bytecodes::_invokevirtual) {
671return resolve_method(link_info, code, THREAD);
672} else if (!resolved_klass->is_interface()) {
673return resolve_method(link_info, code, THREAD);
674} else {
675return resolve_interface_method(link_info, code, THREAD);
676}
677}
678
679// Check and print a loader constraint violation message for method or interface method
680void LinkResolver::check_method_loader_constraints(const LinkInfo& link_info,
681const methodHandle& resolved_method,
682const char* method_type, TRAPS) {
683Handle current_loader(THREAD, link_info.current_klass()->class_loader());
684Handle resolved_loader(THREAD, resolved_method->method_holder()->class_loader());
685
686ResourceMark rm(THREAD);
687Symbol* failed_type_symbol =
688SystemDictionary::check_signature_loaders(link_info.signature(),
689/*klass_being_linked*/ nullptr, // We are not linking class
690current_loader,
691resolved_loader, true);
692if (failed_type_symbol != nullptr) {
693Klass* current_class = link_info.current_klass();
694ClassLoaderData* current_loader_data = current_class->class_loader_data();
695assert(current_loader_data != nullptr, "current class has no class loader data");
696Klass* resolved_method_class = resolved_method->method_holder();
697ClassLoaderData* target_loader_data = resolved_method_class->class_loader_data();
698assert(target_loader_data != nullptr, "resolved method's class has no class loader data");
699
700stringStream ss;
701ss.print("loader constraint violation: when resolving %s '", method_type);
702Method::print_external_name(&ss, link_info.resolved_klass(), link_info.name(), link_info.signature());
703ss.print("' the class loader %s of the current class, %s,"
704" and the class loader %s for the method's defining class, %s, have"
705" different Class objects for the type %s used in the signature (%s; %s)",
706current_loader_data->loader_name_and_id(),
707current_class->name()->as_C_string(),
708target_loader_data->loader_name_and_id(),
709resolved_method_class->name()->as_C_string(),
710failed_type_symbol->as_C_string(),
711current_class->class_in_module_of_loader(false, true),
712resolved_method_class->class_in_module_of_loader(false, true));
713THROW_MSG(vmSymbols::java_lang_LinkageError(), ss.as_string());
714}
715}
716
717void LinkResolver::check_field_loader_constraints(Symbol* field, Symbol* sig,
718Klass* current_klass,
719Klass* sel_klass, TRAPS) {
720Handle ref_loader(THREAD, current_klass->class_loader());
721Handle sel_loader(THREAD, sel_klass->class_loader());
722
723ResourceMark rm(THREAD); // needed for check_signature_loaders
724Symbol* failed_type_symbol =
725SystemDictionary::check_signature_loaders(sig,
726/*klass_being_linked*/ nullptr, // We are not linking class
727ref_loader, sel_loader,
728false);
729if (failed_type_symbol != nullptr) {
730stringStream ss;
731const char* failed_type_name = failed_type_symbol->as_klass_external_name();
732
733ss.print("loader constraint violation: when resolving field \"%s\" of type %s, "
734"the class loader %s of the current class, %s, "
735"and the class loader %s for the field's defining %s, %s, "
736"have different Class objects for type %s (%s; %s)",
737field->as_C_string(),
738failed_type_name,
739current_klass->class_loader_data()->loader_name_and_id(),
740current_klass->external_name(),
741sel_klass->class_loader_data()->loader_name_and_id(),
742sel_klass->external_kind(),
743sel_klass->external_name(),
744failed_type_name,
745current_klass->class_in_module_of_loader(false, true),
746sel_klass->class_in_module_of_loader(false, true));
747THROW_MSG(vmSymbols::java_lang_LinkageError(), ss.as_string());
748}
749}
750
751Method* LinkResolver::resolve_method(const LinkInfo& link_info,
752Bytecodes::Code code, TRAPS) {
753
754Handle nested_exception;
755Klass* resolved_klass = link_info.resolved_klass();
756
757// 1. For invokevirtual, cannot call an interface method
758if (code == Bytecodes::_invokevirtual && resolved_klass->is_interface()) {
759ResourceMark rm(THREAD);
760char buf[200];
761jio_snprintf(buf, sizeof(buf), "Found interface %s, but class was expected",
762resolved_klass->external_name());
763THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
764}
765
766// 2. check constant pool tag for called method - must be JVM_CONSTANT_Methodref
767if (!link_info.tag().is_invalid() && !link_info.tag().is_method()) {
768ResourceMark rm(THREAD);
769stringStream ss;
770ss.print("Method '");
771Method::print_external_name(&ss, link_info.resolved_klass(), link_info.name(), link_info.signature());
772ss.print("' must be Methodref constant");
773THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
774}
775
776// 3. lookup method in resolved klass and its super klasses
777methodHandle resolved_method(THREAD, lookup_method_in_klasses(link_info, true, false));
778
779// 4. lookup method in all the interfaces implemented by the resolved klass
780if (resolved_method.is_null() && !resolved_klass->is_array_klass()) { // not found in the class hierarchy
781resolved_method = methodHandle(THREAD, lookup_method_in_interfaces(link_info));
782
783if (resolved_method.is_null()) {
784// JSR 292: see if this is an implicitly generated method MethodHandle.linkToVirtual(*...), etc
785Method* method = lookup_polymorphic_method(link_info, (Handle*)nullptr, THREAD);
786resolved_method = methodHandle(THREAD, method);
787if (HAS_PENDING_EXCEPTION) {
788nested_exception = Handle(THREAD, PENDING_EXCEPTION);
789CLEAR_PENDING_EXCEPTION;
790}
791}
792}
793
794// 5. method lookup failed
795if (resolved_method.is_null()) {
796ResourceMark rm(THREAD);
797stringStream ss;
798ss.print("'");
799Method::print_external_name(&ss, resolved_klass, link_info.name(), link_info.signature());
800ss.print("'");
801THROW_MSG_CAUSE_(vmSymbols::java_lang_NoSuchMethodError(),
802ss.as_string(), nested_exception, nullptr);
803}
804
805// 6. access checks, access checking may be turned off when calling from within the VM.
806Klass* current_klass = link_info.current_klass();
807if (link_info.check_access()) {
808assert(current_klass != nullptr , "current_klass should not be null");
809
810// check if method can be accessed by the referring class
811check_method_accessability(current_klass,
812resolved_klass,
813resolved_method->method_holder(),
814resolved_method,
815CHECK_NULL);
816}
817if (link_info.check_loader_constraints()) {
818// check loader constraints
819check_method_loader_constraints(link_info, resolved_method, "method", CHECK_NULL);
820}
821
822return resolved_method();
823}
824
825static void trace_method_resolution(const char* prefix,
826Klass* klass,
827Klass* resolved_klass,
828Method* method,
829bool logitables,
830int index = -1) {
831#ifndef PRODUCT
832ResourceMark rm;
833Log(itables) logi;
834LogStream lsi(logi.trace());
835Log(vtables) logv;
836LogStream lsv(logv.trace());
837outputStream* st;
838if (logitables) {
839st = &lsi;
840} else {
841st = &lsv;
842}
843st->print("%s%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ",
844prefix,
845(klass == nullptr ? "<null>" : klass->internal_name()),
846resolved_klass->internal_name(),
847Method::name_and_sig_as_C_string(resolved_klass,
848method->name(),
849method->signature()),
850method->method_holder()->internal_name());
851method->print_linkage_flags(st);
852if (index != -1) {
853st->print("vtable_index:%d", index);
854}
855st->cr();
856#endif // PRODUCT
857}
858
859// Do linktime resolution of a method in the interface within the context of the specified bytecode.
860Method* LinkResolver::resolve_interface_method(const LinkInfo& link_info, Bytecodes::Code code, TRAPS) {
861
862Klass* resolved_klass = link_info.resolved_klass();
863
864// check if klass is interface
865if (!resolved_klass->is_interface()) {
866ResourceMark rm(THREAD);
867char buf[200];
868jio_snprintf(buf, sizeof(buf), "Found class %s, but interface was expected", resolved_klass->external_name());
869THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
870}
871
872// check constant pool tag for called method - must be JVM_CONSTANT_InterfaceMethodref
873if (!link_info.tag().is_invalid() && !link_info.tag().is_interface_method()) {
874ResourceMark rm(THREAD);
875stringStream ss;
876ss.print("Method '");
877Method::print_external_name(&ss, link_info.resolved_klass(), link_info.name(), link_info.signature());
878ss.print("' must be InterfaceMethodref constant");
879THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
880}
881
882// lookup method in this interface or its super, java.lang.Object
883// JDK8: also look for static methods
884methodHandle resolved_method(THREAD, lookup_method_in_klasses(link_info, false, true));
885
886if (resolved_method.is_null() && !resolved_klass->is_array_klass()) {
887// lookup method in all the super-interfaces
888resolved_method = methodHandle(THREAD, lookup_method_in_interfaces(link_info));
889}
890
891if (resolved_method.is_null()) {
892// no method found
893ResourceMark rm(THREAD);
894stringStream ss;
895ss.print("'");
896Method::print_external_name(&ss, resolved_klass, link_info.name(), link_info.signature());
897ss.print("'");
898THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(), ss.as_string());
899}
900
901if (link_info.check_access()) {
902// JDK8 adds non-public interface methods, and accessability check requirement
903Klass* current_klass = link_info.current_klass();
904
905assert(current_klass != nullptr , "current_klass should not be null");
906
907// check if method can be accessed by the referring class
908check_method_accessability(current_klass,
909resolved_klass,
910resolved_method->method_holder(),
911resolved_method,
912CHECK_NULL);
913}
914if (link_info.check_loader_constraints()) {
915check_method_loader_constraints(link_info, resolved_method, "interface method", CHECK_NULL);
916}
917
918if (code != Bytecodes::_invokestatic && resolved_method->is_static()) {
919ResourceMark rm(THREAD);
920stringStream ss;
921ss.print("Expected instance not static method '");
922Method::print_external_name(&ss, resolved_klass,
923resolved_method->name(), resolved_method->signature());
924ss.print("'");
925THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
926}
927
928if (log_develop_is_enabled(Trace, itables)) {
929char buf[200];
930jio_snprintf(buf, sizeof(buf), "%s resolved interface method: caller-class:",
931Bytecodes::name(code));
932trace_method_resolution(buf, link_info.current_klass(), resolved_klass, resolved_method(), true);
933}
934
935return resolved_method();
936}
937
938//------------------------------------------------------------------------------------------------------------------------
939// Field resolution
940
941void LinkResolver::check_field_accessability(Klass* ref_klass,
942Klass* resolved_klass,
943Klass* sel_klass,
944const fieldDescriptor& fd,
945TRAPS) {
946bool can_access = Reflection::verify_member_access(ref_klass,
947resolved_klass,
948sel_klass,
949fd.access_flags(),
950true, false, CHECK);
951// Any existing exceptions that may have been thrown, for example LinkageErrors
952// from nest-host resolution, have been allowed to propagate.
953if (!can_access) {
954bool same_module = (sel_klass->module() == ref_klass->module());
955ResourceMark rm(THREAD);
956stringStream ss;
957ss.print("class %s tried to access %s%sfield %s.%s (%s%s%s)",
958ref_klass->external_name(),
959fd.is_protected() ? "protected " : "",
960fd.is_private() ? "private " : "",
961sel_klass->external_name(),
962fd.name()->as_C_string(),
963(same_module) ? ref_klass->joint_in_module_of_loader(sel_klass) : ref_klass->class_in_module_of_loader(),
964(same_module) ? "" : "; ",
965(same_module) ? "" : sel_klass->class_in_module_of_loader()
966);
967// For private access see if there was a problem with nest host
968// resolution, and if so report that as part of the message.
969if (fd.is_private()) {
970print_nest_host_error_on(&ss, ref_klass, sel_klass);
971}
972Exceptions::fthrow(THREAD_AND_LOCATION,
973vmSymbols::java_lang_IllegalAccessError(),
974"%s",
975ss.as_string()
976);
977return;
978}
979}
980
981void LinkResolver::resolve_field_access(fieldDescriptor& fd,
982const constantPoolHandle& pool,
983int index,
984const methodHandle& method,
985Bytecodes::Code byte,
986bool initialize_class, TRAPS) {
987LinkInfo link_info(pool, index, method, byte, CHECK);
988resolve_field(fd, link_info, byte, initialize_class, CHECK);
989}
990
991void LinkResolver::resolve_field(fieldDescriptor& fd,
992const LinkInfo& link_info,
993Bytecodes::Code byte, bool initialize_class,
994TRAPS) {
995assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic ||
996byte == Bytecodes::_getfield || byte == Bytecodes::_putfield ||
997byte == Bytecodes::_nofast_getfield || byte == Bytecodes::_nofast_putfield ||
998(byte == Bytecodes::_nop && !link_info.check_access()), "bad field access bytecode");
999
1000bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic);
1001bool is_put = (byte == Bytecodes::_putfield || byte == Bytecodes::_putstatic || byte == Bytecodes::_nofast_putfield);
1002// Check if there's a resolved klass containing the field
1003Klass* resolved_klass = link_info.resolved_klass();
1004Symbol* field = link_info.name();
1005Symbol* sig = link_info.signature();
1006
1007// Resolve instance field
1008Klass* sel_klass = resolved_klass->find_field(field, sig, &fd);
1009// check if field exists; i.e., if a klass containing the field def has been selected
1010if (sel_klass == nullptr) {
1011ResourceMark rm(THREAD);
1012stringStream ss;
1013ss.print("Class %s does not have member field '", resolved_klass->external_name());
1014sig->print_as_field_external_type(&ss);
1015ss.print(" %s'", field->as_C_string());
1016THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), ss.as_string());
1017}
1018
1019// Access checking may be turned off when calling from within the VM.
1020Klass* current_klass = link_info.current_klass();
1021if (link_info.check_access()) {
1022
1023// check access
1024check_field_accessability(current_klass, resolved_klass, sel_klass, fd, CHECK);
1025
1026// check for errors
1027if (is_static != fd.is_static()) {
1028ResourceMark rm(THREAD);
1029char msg[200];
1030jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string());
1031THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg);
1032}
1033
1034// A final field can be modified only
1035// (1) by methods declared in the class declaring the field and
1036// (2) by the <clinit> method (in case of a static field)
1037// or by the <init> method (in case of an instance field).
1038if (is_put && fd.access_flags().is_final()) {
1039
1040if (sel_klass != current_klass) {
1041ResourceMark rm(THREAD);
1042stringStream ss;
1043ss.print("Update to %s final field %s.%s attempted from a different class (%s) than the field's declaring class",
1044is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),
1045current_klass->external_name());
1046THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1047}
1048
1049if (fd.constants()->pool_holder()->major_version() >= 53) {
1050Method* m = link_info.current_method();
1051assert(m != nullptr, "information about the current method must be available for 'put' bytecodes");
1052bool is_initialized_static_final_update = (byte == Bytecodes::_putstatic &&
1053fd.is_static() &&
1054!m->is_static_initializer());
1055bool is_initialized_instance_final_update = ((byte == Bytecodes::_putfield || byte == Bytecodes::_nofast_putfield) &&
1056!fd.is_static() &&
1057!m->is_object_initializer());
1058
1059if (is_initialized_static_final_update || is_initialized_instance_final_update) {
1060ResourceMark rm(THREAD);
1061stringStream ss;
1062ss.print("Update to %s final field %s.%s attempted from a different method (%s) than the initializer method %s ",
1063is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),
1064m->name()->as_C_string(),
1065is_static ? "<clinit>" : "<init>");
1066THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1067}
1068}
1069}
1070
1071// initialize resolved_klass if necessary
1072// note 1: the klass which declared the field must be initialized (i.e, sel_klass)
1073// according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99)
1074//
1075// note 2: we don't want to force initialization if we are just checking
1076// if the field access is legal; e.g., during compilation
1077if (is_static && initialize_class) {
1078sel_klass->initialize(CHECK);
1079}
1080}
1081
1082if (link_info.check_loader_constraints() && (sel_klass != current_klass) && (current_klass != nullptr)) {
1083check_field_loader_constraints(field, sig, current_klass, sel_klass, CHECK);
1084}
1085
1086// return information. note that the klass is set to the actual klass containing the
1087// field, otherwise access of static fields in superclasses will not work.
1088}
1089
1090
1091//------------------------------------------------------------------------------------------------------------------------
1092// Invoke resolution
1093//
1094// Naming conventions:
1095//
1096// resolved_method the specified method (i.e., static receiver specified via constant pool index)
1097// sel_method the selected method (selected via run-time lookup; e.g., based on dynamic receiver class)
1098// resolved_klass the specified klass (i.e., specified via constant pool index)
1099// recv_klass the receiver klass
1100
1101
1102void LinkResolver::resolve_static_call(CallInfo& result,
1103const LinkInfo& link_info,
1104bool initialize_class, TRAPS) {
1105Method* resolved_method = linktime_resolve_static_method(link_info, CHECK);
1106
1107// The resolved class can change as a result of this resolution.
1108Klass* resolved_klass = resolved_method->method_holder();
1109
1110// Initialize klass (this should only happen if everything is ok)
1111if (initialize_class && resolved_klass->should_be_initialized()) {
1112resolved_klass->initialize(CHECK);
1113// Use updated LinkInfo to reresolve with resolved method holder
1114LinkInfo new_info(resolved_klass, link_info.name(), link_info.signature(),
1115link_info.current_klass(),
1116link_info.check_access() ? LinkInfo::AccessCheck::required : LinkInfo::AccessCheck::skip,
1117link_info.check_loader_constraints() ? LinkInfo::LoaderConstraintCheck::required : LinkInfo::LoaderConstraintCheck::skip);
1118resolved_method = linktime_resolve_static_method(new_info, CHECK);
1119}
1120
1121// setup result
1122result.set_static(resolved_klass, methodHandle(THREAD, resolved_method), CHECK);
1123JFR_ONLY(Jfr::on_resolution(result, CHECK);)
1124}
1125
1126// throws linktime exceptions
1127Method* LinkResolver::linktime_resolve_static_method(const LinkInfo& link_info, TRAPS) {
1128
1129Klass* resolved_klass = link_info.resolved_klass();
1130Method* resolved_method;
1131if (!resolved_klass->is_interface()) {
1132resolved_method = resolve_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);
1133} else {
1134resolved_method = resolve_interface_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);
1135}
1136assert(resolved_method->name() != vmSymbols::class_initializer_name(), "should have been checked in verifier");
1137
1138// check if static
1139if (!resolved_method->is_static()) {
1140ResourceMark rm(THREAD);
1141stringStream ss;
1142ss.print("Expected static method '");
1143resolved_method->print_external_name(&ss);
1144ss.print("'");
1145THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1146}
1147return resolved_method;
1148}
1149
1150
1151void LinkResolver::resolve_special_call(CallInfo& result,
1152Handle recv,
1153const LinkInfo& link_info,
1154TRAPS) {
1155Method* resolved_method = linktime_resolve_special_method(link_info, CHECK);
1156runtime_resolve_special_method(result, link_info, methodHandle(THREAD, resolved_method), recv, CHECK);
1157}
1158
1159void LinkResolver::cds_resolve_special_call(CallInfo& result, const LinkInfo& link_info, TRAPS) {
1160resolve_special_call(result, Handle(), link_info, CHECK);
1161}
1162
1163// throws linktime exceptions
1164Method* LinkResolver::linktime_resolve_special_method(const LinkInfo& link_info, TRAPS) {
1165
1166// Invokespecial is called for multiple special reasons:
1167// <init>
1168// local private method invocation, for classes and interfaces
1169// superclass.method, which can also resolve to a default method
1170// and the selected method is recalculated relative to the direct superclass
1171// superinterface.method, which explicitly does not check shadowing
1172Klass* resolved_klass = link_info.resolved_klass();
1173Method* resolved_method = nullptr;
1174
1175if (!resolved_klass->is_interface()) {
1176resolved_method = resolve_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1177} else {
1178resolved_method = resolve_interface_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1179}
1180
1181// check if method name is <init>, that it is found in same klass as static type
1182if (resolved_method->name() == vmSymbols::object_initializer_name() &&
1183resolved_method->method_holder() != resolved_klass) {
1184ResourceMark rm(THREAD);
1185stringStream ss;
1186ss.print("%s: method '", resolved_klass->external_name());
1187resolved_method->signature()->print_as_signature_external_return_type(&ss);
1188ss.print(" %s(", resolved_method->name()->as_C_string());
1189resolved_method->signature()->print_as_signature_external_parameters(&ss);
1190ss.print(")' not found");
1191Exceptions::fthrow(
1192THREAD_AND_LOCATION,
1193vmSymbols::java_lang_NoSuchMethodError(),
1194"%s", ss.as_string());
1195return nullptr;
1196}
1197
1198// ensure that invokespecial's interface method reference is in
1199// a direct superinterface, not an indirect superinterface
1200Klass* current_klass = link_info.current_klass();
1201if (current_klass != nullptr && resolved_klass->is_interface()) {
1202InstanceKlass* klass_to_check = InstanceKlass::cast(current_klass);
1203// Disable verification for the dynamically-generated reflection bytecodes
1204// for serialization constructor accessor.
1205bool is_reflect = klass_to_check->is_subclass_of(
1206vmClasses::reflect_SerializationConstructorAccessorImpl_klass());
1207
1208if (!is_reflect &&
1209!klass_to_check->is_same_or_direct_interface(resolved_klass)) {
1210ResourceMark rm(THREAD);
1211stringStream ss;
1212ss.print("Interface method reference: '");
1213resolved_method->print_external_name(&ss);
1214ss.print("', is in an indirect superinterface of %s",
1215current_klass->external_name());
1216THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1217}
1218}
1219
1220// check if not static
1221if (resolved_method->is_static()) {
1222ResourceMark rm(THREAD);
1223stringStream ss;
1224ss.print("Expecting non-static method '");
1225resolved_method->print_external_name(&ss);
1226ss.print("'");
1227THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1228}
1229
1230if (log_develop_is_enabled(Trace, itables)) {
1231trace_method_resolution("invokespecial resolved method: caller-class:",
1232current_klass, resolved_klass, resolved_method, true);
1233}
1234
1235return resolved_method;
1236}
1237
1238// throws runtime exceptions
1239void LinkResolver::runtime_resolve_special_method(CallInfo& result,
1240const LinkInfo& link_info,
1241const methodHandle& resolved_method,
1242Handle recv, TRAPS) {
1243
1244Klass* resolved_klass = link_info.resolved_klass();
1245
1246// resolved method is selected method unless we have an old-style lookup
1247// for a superclass method
1248// Invokespecial for a superinterface, resolved method is selected method,
1249// no checks for shadowing
1250methodHandle sel_method(THREAD, resolved_method());
1251
1252if (link_info.check_access() &&
1253// check if the method is not <init>
1254resolved_method->name() != vmSymbols::object_initializer_name()) {
1255
1256Klass* current_klass = link_info.current_klass();
1257
1258// Check if the class of the resolved_klass is a superclass
1259// (not supertype in order to exclude interface classes) of the current class.
1260// This check is not performed for super.invoke for interface methods
1261// in super interfaces.
1262if (current_klass->is_subclass_of(resolved_klass) &&
1263current_klass != resolved_klass) {
1264// Lookup super method
1265Klass* super_klass = current_klass->super();
1266Method* instance_method = lookup_instance_method_in_klasses(super_klass,
1267resolved_method->name(),
1268resolved_method->signature(),
1269Klass::PrivateLookupMode::find);
1270sel_method = methodHandle(THREAD, instance_method);
1271
1272// check if found
1273if (sel_method.is_null()) {
1274ResourceMark rm(THREAD);
1275stringStream ss;
1276ss.print("'");
1277resolved_method->print_external_name(&ss);
1278ss.print("'");
1279THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), ss.as_string());
1280// check loader constraints if found a different method
1281} else if (link_info.check_loader_constraints() && sel_method() != resolved_method()) {
1282check_method_loader_constraints(link_info, sel_method, "method", CHECK);
1283}
1284}
1285
1286// Check that the class of objectref (the receiver) is the current class or interface,
1287// or a subtype of the current class or interface (the sender), otherwise invokespecial
1288// throws IllegalAccessError.
1289// The verifier checks that the sender is a subtype of the class in the I/MR operand.
1290// The verifier also checks that the receiver is a subtype of the sender, if the sender is
1291// a class. If the sender is an interface, the check has to be performed at runtime.
1292InstanceKlass* sender = InstanceKlass::cast(current_klass);
1293if (sender->is_interface() && recv.not_null()) {
1294Klass* receiver_klass = recv->klass();
1295if (!receiver_klass->is_subtype_of(sender)) {
1296ResourceMark rm(THREAD);
1297char buf[500];
1298jio_snprintf(buf, sizeof(buf),
1299"Receiver class %s must be the current class or a subtype of interface %s",
1300receiver_klass->external_name(),
1301sender->external_name());
1302THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), buf);
1303}
1304}
1305}
1306
1307// check if not static
1308if (sel_method->is_static()) {
1309ResourceMark rm(THREAD);
1310stringStream ss;
1311ss.print("Expecting non-static method '");
1312resolved_method->print_external_name(&ss);
1313ss.print("'");
1314THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1315}
1316
1317// check if abstract
1318if (sel_method->is_abstract()) {
1319ResourceMark rm(THREAD);
1320stringStream ss;
1321ss.print("'");
1322Method::print_external_name(&ss, resolved_klass, sel_method->name(), sel_method->signature());
1323ss.print("'");
1324THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), ss.as_string());
1325}
1326
1327if (log_develop_is_enabled(Trace, itables)) {
1328trace_method_resolution("invokespecial selected method: resolved-class:",
1329resolved_klass, resolved_klass, sel_method(), true);
1330}
1331
1332// setup result
1333result.set_static(resolved_klass, sel_method, CHECK);
1334JFR_ONLY(Jfr::on_resolution(result, CHECK);)
1335}
1336
1337void LinkResolver::resolve_virtual_call(CallInfo& result, Handle recv, Klass* receiver_klass,
1338const LinkInfo& link_info,
1339bool check_null_and_abstract, TRAPS) {
1340Method* resolved_method = linktime_resolve_virtual_method(link_info, CHECK);
1341runtime_resolve_virtual_method(result, methodHandle(THREAD, resolved_method),
1342link_info.resolved_klass(),
1343recv, receiver_klass,
1344check_null_and_abstract,
1345/*is_abstract_interpretation*/ false, CHECK);
1346}
1347
1348void LinkResolver::cds_resolve_virtual_call(CallInfo& result, const LinkInfo& link_info, TRAPS) {
1349Method* resolved_method = linktime_resolve_virtual_method(link_info, CHECK);
1350runtime_resolve_virtual_method(result, methodHandle(THREAD, resolved_method),
1351link_info.resolved_klass(),
1352Handle(), nullptr,
1353/*check_null_and_abstract*/ false,
1354/*is_abstract_interpretation*/ true, CHECK);
1355}
1356
1357// throws linktime exceptions
1358Method* LinkResolver::linktime_resolve_virtual_method(const LinkInfo& link_info,
1359TRAPS) {
1360// normal method resolution
1361Method* resolved_method = resolve_method(link_info, Bytecodes::_invokevirtual, CHECK_NULL);
1362
1363assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1364assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1365
1366// check if private interface method
1367Klass* resolved_klass = link_info.resolved_klass();
1368Klass* current_klass = link_info.current_klass();
1369
1370// This is impossible, if resolve_klass is an interface, we've thrown icce in resolve_method
1371if (resolved_klass->is_interface() && resolved_method->is_private()) {
1372ResourceMark rm(THREAD);
1373stringStream ss;
1374ss.print("private interface method requires invokespecial, not invokevirtual: method '");
1375resolved_method->print_external_name(&ss);
1376ss.print("', caller-class: %s",
1377(current_klass == nullptr ? "<null>" : current_klass->internal_name()));
1378THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1379}
1380
1381// check if not static
1382if (resolved_method->is_static()) {
1383ResourceMark rm(THREAD);
1384stringStream ss;
1385ss.print("Expecting non-static method '");
1386resolved_method->print_external_name(&ss);
1387ss.print("'");
1388THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1389}
1390
1391if (log_develop_is_enabled(Trace, vtables)) {
1392trace_method_resolution("invokevirtual resolved method: caller-class:",
1393current_klass, resolved_klass, resolved_method, false);
1394}
1395
1396return resolved_method;
1397}
1398
1399// throws runtime exceptions
1400void LinkResolver::runtime_resolve_virtual_method(CallInfo& result,
1401const methodHandle& resolved_method,
1402Klass* resolved_klass,
1403Handle recv,
1404Klass* recv_klass,
1405bool check_null_and_abstract,
1406bool is_abstract_interpretation,
1407TRAPS) {
1408// is_abstract_interpretation is true IFF CDS is resolving method references without
1409// running any actual bytecode. Therefore, we don't have an actual recv/recv_klass, so
1410// we cannot check the actual selected_method (which is not needed by CDS anyway).
1411
1412// setup default return values
1413int vtable_index = Method::invalid_vtable_index;
1414methodHandle selected_method;
1415
1416// runtime method resolution
1417if (check_null_and_abstract && recv.is_null()) { // check if receiver exists
1418THROW(vmSymbols::java_lang_NullPointerException());
1419}
1420
1421// Virtual methods cannot be resolved before its klass has been linked, for otherwise the Method*'s
1422// has not been rewritten, and the vtable initialized. Make sure to do this after the nullcheck, since
1423// a missing receiver might result in a bogus lookup.
1424assert(resolved_method->method_holder()->is_linked(), "must be linked");
1425
1426// do lookup based on receiver klass using the vtable index
1427if (resolved_method->method_holder()->is_interface()) { // default or miranda method
1428vtable_index = vtable_index_of_interface_method(resolved_klass, resolved_method);
1429assert(vtable_index >= 0 , "we should have valid vtable index at this point");
1430
1431if (!is_abstract_interpretation) {
1432selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));
1433}
1434} else {
1435// at this point we are sure that resolved_method is virtual and not
1436// a default or miranda method; therefore, it must have a valid vtable index.
1437assert(!resolved_method->has_itable_index(), "");
1438vtable_index = resolved_method->vtable_index();
1439// We could get a negative vtable_index of nonvirtual_vtable_index for private
1440// methods, or for final methods. Private methods never appear in the vtable
1441// and never override other methods. As an optimization, final methods are
1442// never put in the vtable, unless they override an existing method.
1443// So if we do get nonvirtual_vtable_index, it means the selected method is the
1444// resolved method, and it can never be changed by an override.
1445if (vtable_index == Method::nonvirtual_vtable_index) {
1446assert(resolved_method->can_be_statically_bound(), "cannot override this method");
1447if (!is_abstract_interpretation) {
1448selected_method = resolved_method;
1449}
1450} else {
1451if (!is_abstract_interpretation) {
1452selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));
1453}
1454}
1455}
1456
1457if (!is_abstract_interpretation) {
1458// check if method exists
1459if (selected_method.is_null()) {
1460throw_abstract_method_error(resolved_method, recv_klass, CHECK);
1461}
1462
1463// check if abstract
1464if (check_null_and_abstract && selected_method->is_abstract()) {
1465// Pass arguments for generating a verbose error message.
1466throw_abstract_method_error(resolved_method, selected_method, recv_klass, CHECK);
1467}
1468
1469if (log_develop_is_enabled(Trace, vtables)) {
1470trace_method_resolution("invokevirtual selected method: receiver-class:",
1471recv_klass, resolved_klass, selected_method(),
1472false, vtable_index);
1473}
1474}
1475
1476// setup result
1477result.set_virtual(resolved_klass, resolved_method, selected_method, vtable_index, CHECK);
1478if (selected_method.not_null()) {
1479JFR_ONLY(Jfr::on_resolution(result, CHECK);)
1480}
1481}
1482
1483void LinkResolver::resolve_interface_call(CallInfo& result, Handle recv, Klass* recv_klass,
1484const LinkInfo& link_info,
1485bool check_null_and_abstract, TRAPS) {
1486// throws linktime exceptions
1487Method* resolved_method = linktime_resolve_interface_method(link_info, CHECK);
1488methodHandle mh(THREAD, resolved_method);
1489runtime_resolve_interface_method(result, mh, link_info.resolved_klass(),
1490recv, recv_klass, check_null_and_abstract,
1491/*is_abstract_interpretation*/ false, CHECK);
1492}
1493
1494void LinkResolver::cds_resolve_interface_call(CallInfo& result, const LinkInfo& link_info, TRAPS) {
1495Method* resolved_method = linktime_resolve_interface_method(link_info, CHECK);
1496runtime_resolve_interface_method(result, methodHandle(THREAD, resolved_method), link_info.resolved_klass(),
1497Handle(), nullptr,
1498/*check_null_and_abstract*/ false,
1499/*is_abstract_interpretation*/ true, CHECK);
1500}
1501
1502Method* LinkResolver::linktime_resolve_interface_method(const LinkInfo& link_info,
1503TRAPS) {
1504// normal interface method resolution
1505Method* resolved_method = resolve_interface_method(link_info, Bytecodes::_invokeinterface, CHECK_NULL);
1506assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1507assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1508
1509return resolved_method;
1510}
1511
1512// throws runtime exceptions
1513void LinkResolver::runtime_resolve_interface_method(CallInfo& result,
1514const methodHandle& resolved_method,
1515Klass* resolved_klass,
1516Handle recv,
1517Klass* recv_klass,
1518bool check_null_and_abstract,
1519bool is_abstract_interpretation, TRAPS) {
1520// is_abstract_interpretation -- see comments in runtime_resolve_virtual_method()
1521
1522// check if receiver exists
1523if (check_null_and_abstract && recv.is_null()) {
1524THROW(vmSymbols::java_lang_NullPointerException());
1525}
1526
1527// check if receiver klass implements the resolved interface
1528if (!is_abstract_interpretation && !recv_klass->is_subtype_of(resolved_klass)) {
1529ResourceMark rm(THREAD);
1530char buf[200];
1531jio_snprintf(buf, sizeof(buf), "Class %s does not implement the requested interface %s",
1532recv_klass->external_name(),
1533resolved_klass->external_name());
1534THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1535}
1536
1537methodHandle selected_method;
1538
1539if (!is_abstract_interpretation) {
1540selected_method = resolved_method;
1541}
1542
1543// resolve the method in the receiver class, unless it is private
1544if (!is_abstract_interpretation && !resolved_method()->is_private()) {
1545// do lookup based on receiver klass
1546// This search must match the linktime preparation search for itable initialization
1547// to correctly enforce loader constraints for interface method inheritance.
1548// Private methods are skipped as the resolved method was not private.
1549Method* method = lookup_instance_method_in_klasses(recv_klass,
1550resolved_method->name(),
1551resolved_method->signature(),
1552Klass::PrivateLookupMode::skip);
1553selected_method = methodHandle(THREAD, method);
1554
1555if (selected_method.is_null() && !check_null_and_abstract) {
1556// In theory this is a harmless placeholder value, but
1557// in practice leaving in null affects the nsk default method tests.
1558// This needs further study.
1559selected_method = resolved_method;
1560}
1561// check if method exists
1562if (selected_method.is_null()) {
1563// Pass arguments for generating a verbose error message.
1564throw_abstract_method_error(resolved_method, recv_klass, CHECK);
1565}
1566// check access
1567// Throw Illegal Access Error if selected_method is not public.
1568if (!selected_method->is_public()) {
1569ResourceMark rm(THREAD);
1570stringStream ss;
1571ss.print("'");
1572Method::print_external_name(&ss, recv_klass, selected_method->name(), selected_method->signature());
1573ss.print("'");
1574THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1575}
1576// check if abstract
1577if (check_null_and_abstract && selected_method->is_abstract()) {
1578throw_abstract_method_error(resolved_method, selected_method, recv_klass, CHECK);
1579}
1580}
1581
1582if (log_develop_is_enabled(Trace, itables)) {
1583trace_method_resolution("invokeinterface selected method: receiver-class:",
1584recv_klass, resolved_klass, selected_method(), true);
1585}
1586// setup result
1587if (resolved_method->has_vtable_index()) {
1588int vtable_index = resolved_method->vtable_index();
1589log_develop_trace(itables)(" -- vtable index: %d", vtable_index);
1590assert(is_abstract_interpretation || vtable_index == selected_method->vtable_index(), "sanity check");
1591result.set_virtual(resolved_klass, resolved_method, selected_method, vtable_index, CHECK);
1592} else if (resolved_method->has_itable_index()) {
1593int itable_index = resolved_method()->itable_index();
1594log_develop_trace(itables)(" -- itable index: %d", itable_index);
1595result.set_interface(resolved_klass, resolved_method, selected_method, itable_index, CHECK);
1596} else {
1597int index = resolved_method->vtable_index();
1598log_develop_trace(itables)(" -- non itable/vtable index: %d", index);
1599assert(index == Method::nonvirtual_vtable_index, "Oops hit another case!");
1600assert(resolved_method()->is_private() ||
1601(resolved_method()->is_final() && resolved_method->method_holder() == vmClasses::Object_klass()),
1602"Should only have non-virtual invokeinterface for private or final-Object methods!");
1603assert(resolved_method()->can_be_statically_bound(), "Should only have non-virtual invokeinterface for statically bound methods!");
1604// This sets up the nonvirtual form of "virtual" call (as needed for final and private methods)
1605result.set_virtual(resolved_klass, resolved_method, resolved_method, index, CHECK);
1606}
1607if (!is_abstract_interpretation) {
1608JFR_ONLY(Jfr::on_resolution(result, CHECK);)
1609}
1610}
1611
1612
1613Method* LinkResolver::linktime_resolve_interface_method_or_null(
1614const LinkInfo& link_info) {
1615EXCEPTION_MARK;
1616Method* method_result = linktime_resolve_interface_method(link_info, THREAD);
1617if (HAS_PENDING_EXCEPTION) {
1618CLEAR_PENDING_EXCEPTION;
1619return nullptr;
1620} else {
1621return method_result;
1622}
1623}
1624
1625Method* LinkResolver::linktime_resolve_virtual_method_or_null(
1626const LinkInfo& link_info) {
1627EXCEPTION_MARK;
1628Method* method_result = linktime_resolve_virtual_method(link_info, THREAD);
1629if (HAS_PENDING_EXCEPTION) {
1630CLEAR_PENDING_EXCEPTION;
1631return nullptr;
1632} else {
1633return method_result;
1634}
1635}
1636
1637Method* LinkResolver::resolve_virtual_call_or_null(
1638Klass* receiver_klass,
1639const LinkInfo& link_info) {
1640EXCEPTION_MARK;
1641CallInfo info;
1642resolve_virtual_call(info, Handle(), receiver_klass, link_info, false, THREAD);
1643if (HAS_PENDING_EXCEPTION) {
1644CLEAR_PENDING_EXCEPTION;
1645return nullptr;
1646}
1647return info.selected_method();
1648}
1649
1650Method* LinkResolver::resolve_interface_call_or_null(
1651Klass* receiver_klass,
1652const LinkInfo& link_info) {
1653EXCEPTION_MARK;
1654CallInfo info;
1655resolve_interface_call(info, Handle(), receiver_klass, link_info, false, THREAD);
1656if (HAS_PENDING_EXCEPTION) {
1657CLEAR_PENDING_EXCEPTION;
1658return nullptr;
1659}
1660return info.selected_method();
1661}
1662
1663int LinkResolver::resolve_virtual_vtable_index(Klass* receiver_klass,
1664const LinkInfo& link_info) {
1665EXCEPTION_MARK;
1666CallInfo info;
1667resolve_virtual_call(info, Handle(), receiver_klass, link_info,
1668/*check_null_or_abstract*/false, THREAD);
1669if (HAS_PENDING_EXCEPTION) {
1670CLEAR_PENDING_EXCEPTION;
1671return Method::invalid_vtable_index;
1672}
1673return info.vtable_index();
1674}
1675
1676Method* LinkResolver::resolve_static_call_or_null(const LinkInfo& link_info) {
1677EXCEPTION_MARK;
1678CallInfo info;
1679resolve_static_call(info, link_info, /*initialize_class*/false, THREAD);
1680if (HAS_PENDING_EXCEPTION) {
1681CLEAR_PENDING_EXCEPTION;
1682return nullptr;
1683}
1684return info.selected_method();
1685}
1686
1687Method* LinkResolver::resolve_special_call_or_null(const LinkInfo& link_info) {
1688EXCEPTION_MARK;
1689CallInfo info;
1690resolve_special_call(info, Handle(), link_info, THREAD);
1691if (HAS_PENDING_EXCEPTION) {
1692CLEAR_PENDING_EXCEPTION;
1693return nullptr;
1694}
1695return info.selected_method();
1696}
1697
1698
1699
1700//------------------------------------------------------------------------------------------------------------------------
1701// ConstantPool entries
1702
1703void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, Bytecodes::Code byte, TRAPS) {
1704switch (byte) {
1705case Bytecodes::_invokestatic : resolve_invokestatic (result, pool, index, CHECK); break;
1706case Bytecodes::_invokespecial : resolve_invokespecial (result, recv, pool, index, CHECK); break;
1707case Bytecodes::_invokevirtual : resolve_invokevirtual (result, recv, pool, index, CHECK); break;
1708case Bytecodes::_invokehandle : resolve_invokehandle (result, pool, index, CHECK); break;
1709case Bytecodes::_invokedynamic : resolve_invokedynamic (result, pool, index, CHECK); break;
1710case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break;
1711default : break;
1712}
1713return;
1714}
1715
1716void LinkResolver::resolve_invoke(CallInfo& result, Handle& recv,
1717const methodHandle& attached_method,
1718Bytecodes::Code byte, TRAPS) {
1719Klass* defc = attached_method->method_holder();
1720Symbol* name = attached_method->name();
1721Symbol* type = attached_method->signature();
1722LinkInfo link_info(defc, name, type);
1723switch(byte) {
1724case Bytecodes::_invokevirtual:
1725resolve_virtual_call(result, recv, recv->klass(), link_info,
1726/*check_null_and_abstract=*/true, CHECK);
1727break;
1728case Bytecodes::_invokeinterface:
1729resolve_interface_call(result, recv, recv->klass(), link_info,
1730/*check_null_and_abstract=*/true, CHECK);
1731break;
1732case Bytecodes::_invokestatic:
1733resolve_static_call(result, link_info, /*initialize_class=*/false, CHECK);
1734break;
1735case Bytecodes::_invokespecial:
1736resolve_special_call(result, recv, link_info, CHECK);
1737break;
1738default:
1739fatal("bad call: %s", Bytecodes::name(byte));
1740break;
1741}
1742}
1743
1744void LinkResolver::resolve_invokestatic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1745LinkInfo link_info(pool, index, Bytecodes::_invokestatic, CHECK);
1746resolve_static_call(result, link_info, /*initialize_class*/true, CHECK);
1747}
1748
1749
1750void LinkResolver::resolve_invokespecial(CallInfo& result, Handle recv,
1751const constantPoolHandle& pool, int index, TRAPS) {
1752LinkInfo link_info(pool, index, Bytecodes::_invokespecial, CHECK);
1753resolve_special_call(result, recv, link_info, CHECK);
1754}
1755
1756
1757void LinkResolver::resolve_invokevirtual(CallInfo& result, Handle recv,
1758const constantPoolHandle& pool, int index,
1759TRAPS) {
1760
1761LinkInfo link_info(pool, index, Bytecodes::_invokevirtual, CHECK);
1762Klass* recvrKlass = recv.is_null() ? (Klass*)nullptr : recv->klass();
1763resolve_virtual_call(result, recv, recvrKlass, link_info, /*check_null_or_abstract*/true, CHECK);
1764}
1765
1766
1767void LinkResolver::resolve_invokeinterface(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, TRAPS) {
1768LinkInfo link_info(pool, index, Bytecodes::_invokeinterface, CHECK);
1769Klass* recvrKlass = recv.is_null() ? (Klass*)nullptr : recv->klass();
1770resolve_interface_call(result, recv, recvrKlass, link_info, true, CHECK);
1771}
1772
1773bool LinkResolver::resolve_previously_linked_invokehandle(CallInfo& result, const LinkInfo& link_info, const constantPoolHandle& pool, int index, TRAPS) {
1774ResolvedMethodEntry* method_entry = pool->cache()->resolved_method_entry_at(index);
1775if (method_entry->method() != nullptr) {
1776Klass* resolved_klass = link_info.resolved_klass();
1777methodHandle method(THREAD, method_entry->method());
1778Handle appendix(THREAD, pool->cache()->appendix_if_resolved(method_entry));
1779result.set_handle(resolved_klass, method, appendix, CHECK_false);
1780JFR_ONLY(Jfr::on_resolution(result, CHECK_false);)
1781return true;
1782} else {
1783return false;
1784}
1785}
1786
1787void LinkResolver::resolve_invokehandle(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1788
1789PerfTraceTimedEvent timer(ClassLoader::perf_resolve_invokehandle_time(),
1790ClassLoader::perf_resolve_invokehandle_count());
1791
1792LinkInfo link_info(pool, index, Bytecodes::_invokehandle, CHECK);
1793if (log_is_enabled(Info, methodhandles)) {
1794ResourceMark rm(THREAD);
1795log_info(methodhandles)("resolve_invokehandle %s %s", link_info.name()->as_C_string(),
1796link_info.signature()->as_C_string());
1797}
1798{ // Check if the call site has been bound already, and short circuit:
1799bool is_done = resolve_previously_linked_invokehandle(result, link_info, pool, index, CHECK);
1800if (is_done) return;
1801}
1802resolve_handle_call(result, link_info, CHECK);
1803}
1804
1805void LinkResolver::resolve_handle_call(CallInfo& result,
1806const LinkInfo& link_info,
1807TRAPS) {
1808// JSR 292: this must be an implicitly generated method MethodHandle.invokeExact(*...) or similar
1809Klass* resolved_klass = link_info.resolved_klass();
1810assert(resolved_klass == vmClasses::MethodHandle_klass() ||
1811resolved_klass == vmClasses::VarHandle_klass(), "");
1812assert(MethodHandles::is_signature_polymorphic_name(link_info.name()), "");
1813Handle resolved_appendix;
1814Method* m = lookup_polymorphic_method(link_info, &resolved_appendix, CHECK);
1815methodHandle resolved_method(THREAD, m);
1816
1817if (link_info.check_access()) {
1818Symbol* name = link_info.name();
1819vmIntrinsics::ID iid = MethodHandles::signature_polymorphic_name_id(name);
1820if (MethodHandles::is_signature_polymorphic_intrinsic(iid)) {
1821// Check if method can be accessed by the referring class.
1822// MH.linkTo* invocations are not rewritten to invokehandle.
1823assert(iid == vmIntrinsicID::_invokeBasic, "%s", vmIntrinsics::name_at(iid));
1824
1825Klass* current_klass = link_info.current_klass();
1826assert(current_klass != nullptr , "current_klass should not be null");
1827check_method_accessability(current_klass,
1828resolved_klass,
1829resolved_method->method_holder(),
1830resolved_method,
1831CHECK);
1832} else {
1833// Java code is free to arbitrarily link signature-polymorphic invokers.
1834assert(iid == vmIntrinsics::_invokeGeneric, "not an invoker: %s", vmIntrinsics::name_at(iid));
1835assert(MethodHandles::is_signature_polymorphic_public_name(resolved_klass, name), "not public");
1836}
1837}
1838result.set_handle(resolved_klass, resolved_method, resolved_appendix, CHECK);
1839JFR_ONLY(Jfr::on_resolution(result, CHECK);)
1840}
1841
1842void LinkResolver::resolve_invokedynamic(CallInfo& result, const constantPoolHandle& pool, int indy_index, TRAPS) {
1843PerfTraceTimedEvent timer(ClassLoader::perf_resolve_invokedynamic_time(),
1844ClassLoader::perf_resolve_invokedynamic_count());
1845
1846int pool_index = pool->resolved_indy_entry_at(indy_index)->constant_pool_index();
1847
1848// Resolve the bootstrap specifier (BSM + optional arguments).
1849BootstrapInfo bootstrap_specifier(pool, pool_index, indy_index);
1850
1851// Check if CallSite has been bound already or failed already, and short circuit:
1852{
1853bool is_done = bootstrap_specifier.resolve_previously_linked_invokedynamic(result, CHECK);
1854if (is_done) return;
1855}
1856
1857// The initial step in Call Site Specifier Resolution is to resolve the symbolic
1858// reference to a method handle which will be the bootstrap method for a dynamic
1859// call site. If resolution for the java.lang.invoke.MethodHandle for the bootstrap
1860// method fails, then a MethodHandleInError is stored at the corresponding bootstrap
1861// method's CP index for the CONSTANT_MethodHandle_info.
1862// Any subsequent invokedynamic instruction which shares
1863// this bootstrap method will encounter the resolution of MethodHandleInError.
1864
1865resolve_dynamic_call(result, bootstrap_specifier, CHECK);
1866
1867LogTarget(Debug, methodhandles, indy) lt_indy;
1868if (lt_indy.is_enabled()) {
1869LogStream ls(lt_indy);
1870bootstrap_specifier.print_msg_on(&ls, "resolve_invokedynamic");
1871}
1872
1873// The returned linkage result is provisional up to the moment
1874// the interpreter or runtime performs a serialized check of
1875// the relevant ResolvedIndyEntry::method field. This is done by the caller
1876// of this method, via CPC::set_dynamic_call, which uses
1877// an ObjectLocker to do the final serialization of updates
1878// to ResolvedIndyEntry state, including method.
1879
1880// Log dynamic info to CDS classlist.
1881ArchiveUtils::log_to_classlist(&bootstrap_specifier, CHECK);
1882}
1883
1884void LinkResolver::resolve_dynamic_call(CallInfo& result,
1885BootstrapInfo& bootstrap_specifier,
1886TRAPS) {
1887// JSR 292: this must resolve to an implicitly generated method
1888// such as MH.linkToCallSite(*...) or some other call-site shape.
1889// The appendix argument is likely to be a freshly-created CallSite.
1890// It may also be a MethodHandle from an unwrapped ConstantCallSite,
1891// or any other reference. The resolved_method as well as the appendix
1892// are both recorded together via CallInfo::set_handle.
1893SystemDictionary::invoke_bootstrap_method(bootstrap_specifier, THREAD);
1894Exceptions::wrap_dynamic_exception(/* is_indy */ true, THREAD);
1895
1896if (HAS_PENDING_EXCEPTION) {
1897if (!PENDING_EXCEPTION->is_a(vmClasses::LinkageError_klass())) {
1898// Let any random low-level IE or SOE or OOME just bleed through.
1899// Basically we pretend that the bootstrap method was never called,
1900// if it fails this way: We neither record a successful linkage,
1901// nor do we memorize a LE for posterity.
1902return;
1903}
1904// JVMS 5.4.3 says: If an attempt by the Java Virtual Machine to resolve
1905// a symbolic reference fails because an error is thrown that is an
1906// instance of LinkageError (or a subclass), then subsequent attempts to
1907// resolve the reference always fail with the same error that was thrown
1908// as a result of the initial resolution attempt.
1909bool recorded_res_status = bootstrap_specifier.save_and_throw_indy_exc(CHECK);
1910if (!recorded_res_status) {
1911// Another thread got here just before we did. So, either use the method
1912// that it resolved or throw the LinkageError exception that it threw.
1913bool is_done = bootstrap_specifier.resolve_previously_linked_invokedynamic(result, CHECK);
1914if (is_done) return;
1915}
1916assert(bootstrap_specifier.pool()->resolved_indy_entry_at(bootstrap_specifier.indy_index())->resolution_failed(),
1917"Resolution should have failed");
1918}
1919
1920bootstrap_specifier.resolve_newly_linked_invokedynamic(result, CHECK);
1921// Exceptions::wrap_dynamic_exception not used because
1922// set_handle doesn't throw linkage errors
1923JFR_ONLY(Jfr::on_resolution(result, CHECK);)
1924}
1925
1926// Selected method is abstract.
1927void LinkResolver::throw_abstract_method_error(const methodHandle& resolved_method,
1928const methodHandle& selected_method,
1929Klass *recv_klass, TRAPS) {
1930Klass *resolved_klass = resolved_method->method_holder();
1931ResourceMark rm(THREAD);
1932stringStream ss;
1933
1934if (recv_klass != nullptr) {
1935ss.print("Receiver class %s does not define or inherit an "
1936"implementation of the",
1937recv_klass->external_name());
1938} else {
1939ss.print("Missing implementation of");
1940}
1941
1942assert(resolved_method.not_null(), "Sanity");
1943ss.print(" resolved method '%s%s",
1944resolved_method->is_abstract() ? "abstract " : "",
1945resolved_method->is_private() ? "private " : "");
1946resolved_method->signature()->print_as_signature_external_return_type(&ss);
1947ss.print(" %s(", resolved_method->name()->as_C_string());
1948resolved_method->signature()->print_as_signature_external_parameters(&ss);
1949ss.print(")' of %s %s.",
1950resolved_klass->external_kind(),
1951resolved_klass->external_name());
1952
1953if (selected_method.not_null() && !(resolved_method == selected_method)) {
1954ss.print(" Selected method is '%s%s",
1955selected_method->is_abstract() ? "abstract " : "",
1956selected_method->is_private() ? "private " : "");
1957selected_method->print_external_name(&ss);
1958ss.print("'.");
1959}
1960
1961THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), ss.as_string());
1962}
1963