Path: blob/master/src/hotspot/share/interpreter/linkResolver.cpp
40949 views
/*1* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "jvm.h"26#include "cds/archiveUtils.hpp"27#include "classfile/defaultMethods.hpp"28#include "classfile/javaClasses.hpp"29#include "classfile/resolutionErrors.hpp"30#include "classfile/symbolTable.hpp"31#include "classfile/systemDictionary.hpp"32#include "classfile/vmClasses.hpp"33#include "classfile/vmSymbols.hpp"34#include "compiler/compilationPolicy.hpp"35#include "compiler/compileBroker.hpp"36#include "gc/shared/collectedHeap.inline.hpp"37#include "interpreter/bootstrapInfo.hpp"38#include "interpreter/bytecode.hpp"39#include "interpreter/interpreterRuntime.hpp"40#include "interpreter/linkResolver.hpp"41#include "logging/log.hpp"42#include "logging/logStream.hpp"43#include "memory/resourceArea.hpp"44#include "oops/constantPool.hpp"45#include "oops/cpCache.inline.hpp"46#include "oops/instanceKlass.inline.hpp"47#include "oops/klass.inline.hpp"48#include "oops/method.hpp"49#include "oops/objArrayKlass.hpp"50#include "oops/objArrayOop.hpp"51#include "oops/oop.inline.hpp"52#include "prims/methodHandles.hpp"53#include "runtime/fieldDescriptor.inline.hpp"54#include "runtime/frame.inline.hpp"55#include "runtime/handles.inline.hpp"56#include "runtime/reflection.hpp"57#include "runtime/safepointVerifiers.hpp"58#include "runtime/signature.hpp"59#include "runtime/thread.inline.hpp"60#include "runtime/vmThread.hpp"6162//------------------------------------------------------------------------------------------------------------------------63// Implementation of CallInfo646566void CallInfo::set_static(Klass* resolved_klass, const methodHandle& resolved_method, TRAPS) {67int vtable_index = Method::nonvirtual_vtable_index;68set_common(resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK);69}707172void CallInfo::set_interface(Klass* resolved_klass,73const methodHandle& resolved_method,74const methodHandle& selected_method,75int itable_index, TRAPS) {76// This is only called for interface methods. If the resolved_method77// comes from java/lang/Object, it can be the subject of a virtual call, so78// we should pick the vtable index from the resolved method.79// In that case, the caller must call set_virtual instead of set_interface.80assert(resolved_method->method_holder()->is_interface(), "");81assert(itable_index == resolved_method()->itable_index(), "");82set_common(resolved_klass, resolved_method, selected_method, CallInfo::itable_call, itable_index, CHECK);83}8485void CallInfo::set_virtual(Klass* resolved_klass,86const methodHandle& resolved_method,87const methodHandle& selected_method,88int vtable_index, TRAPS) {89assert(vtable_index >= 0 || vtable_index == Method::nonvirtual_vtable_index, "valid index");90assert(vtable_index < 0 || !resolved_method->has_vtable_index() || vtable_index == resolved_method->vtable_index(), "");91CallKind kind = (vtable_index >= 0 && !resolved_method->can_be_statically_bound() ? CallInfo::vtable_call : CallInfo::direct_call);92set_common(resolved_klass, resolved_method, selected_method, kind, vtable_index, CHECK);93assert(!resolved_method->is_compiled_lambda_form(), "these must be handled via an invokehandle call");94}9596void CallInfo::set_handle(const methodHandle& resolved_method,97Handle resolved_appendix, TRAPS) {98set_handle(vmClasses::MethodHandle_klass(), resolved_method, resolved_appendix, CHECK);99}100101void CallInfo::set_handle(Klass* resolved_klass,102const methodHandle& resolved_method,103Handle resolved_appendix, TRAPS) {104guarantee(resolved_method.not_null(), "resolved method is null");105assert(resolved_method->intrinsic_id() == vmIntrinsics::_invokeBasic ||106resolved_method->is_compiled_lambda_form(),107"linkMethod must return one of these");108int vtable_index = Method::nonvirtual_vtable_index;109assert(!resolved_method->has_vtable_index(), "");110set_common(resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK);111_resolved_appendix = resolved_appendix;112}113114void CallInfo::set_common(Klass* resolved_klass,115const methodHandle& resolved_method,116const methodHandle& selected_method,117CallKind kind,118int index,119TRAPS) {120assert(resolved_method->signature() == selected_method->signature(), "signatures must correspond");121_resolved_klass = resolved_klass;122_resolved_method = resolved_method;123_selected_method = selected_method;124_call_kind = kind;125_call_index = index;126_resolved_appendix = Handle();127DEBUG_ONLY(verify()); // verify before making side effects128129CompilationPolicy::compile_if_required(selected_method, THREAD);130}131132// utility query for unreflecting a method133CallInfo::CallInfo(Method* resolved_method, Klass* resolved_klass, TRAPS) {134Klass* resolved_method_holder = resolved_method->method_holder();135if (resolved_klass == NULL) { // 2nd argument defaults to holder of 1st136resolved_klass = resolved_method_holder;137}138_resolved_klass = resolved_klass;139_resolved_method = methodHandle(THREAD, resolved_method);140_selected_method = methodHandle(THREAD, resolved_method);141// classify:142CallKind kind = CallInfo::unknown_kind;143int index = resolved_method->vtable_index();144if (resolved_method->can_be_statically_bound()) {145kind = CallInfo::direct_call;146} else if (!resolved_method_holder->is_interface()) {147// Could be an Object method inherited into an interface, but still a vtable call.148kind = CallInfo::vtable_call;149} else if (!resolved_klass->is_interface()) {150// A default or miranda method. Compute the vtable index.151index = LinkResolver::vtable_index_of_interface_method(resolved_klass, _resolved_method);152assert(index >= 0 , "we should have valid vtable index at this point");153154kind = CallInfo::vtable_call;155} else if (resolved_method->has_vtable_index()) {156// Can occur if an interface redeclares a method of Object.157158#ifdef ASSERT159// Ensure that this is really the case.160Klass* object_klass = vmClasses::Object_klass();161Method * object_resolved_method = object_klass->vtable().method_at(index);162assert(object_resolved_method->name() == resolved_method->name(),163"Object and interface method names should match at vtable index %d, %s != %s",164index, object_resolved_method->name()->as_C_string(), resolved_method->name()->as_C_string());165assert(object_resolved_method->signature() == resolved_method->signature(),166"Object and interface method signatures should match at vtable index %d, %s != %s",167index, object_resolved_method->signature()->as_C_string(), resolved_method->signature()->as_C_string());168#endif // ASSERT169170kind = CallInfo::vtable_call;171} else {172// A regular interface call.173kind = CallInfo::itable_call;174index = resolved_method->itable_index();175}176assert(index == Method::nonvirtual_vtable_index || index >= 0, "bad index %d", index);177_call_kind = kind;178_call_index = index;179_resolved_appendix = Handle();180// Find or create a ResolvedMethod instance for this Method*181set_resolved_method_name(CHECK);182183DEBUG_ONLY(verify());184}185186void CallInfo::set_resolved_method_name(TRAPS) {187assert(_resolved_method() != NULL, "Should already have a Method*");188oop rmethod_name = java_lang_invoke_ResolvedMethodName::find_resolved_method(_resolved_method, CHECK);189_resolved_method_name = Handle(THREAD, rmethod_name);190}191192#ifdef ASSERT193void CallInfo::verify() {194switch (call_kind()) { // the meaning and allowed value of index depends on kind195case CallInfo::direct_call:196if (_call_index == Method::nonvirtual_vtable_index) break;197// else fall through to check vtable index:198case CallInfo::vtable_call:199assert(resolved_klass()->verify_vtable_index(_call_index), "");200break;201case CallInfo::itable_call:202assert(resolved_method()->method_holder()->verify_itable_index(_call_index), "");203break;204case CallInfo::unknown_kind:205assert(call_kind() != CallInfo::unknown_kind, "CallInfo must be set");206break;207default:208fatal("Unexpected call kind %d", call_kind());209}210}211#endif // ASSERT212213#ifndef PRODUCT214void CallInfo::print() {215ResourceMark rm;216const char* kindstr;217switch (_call_kind) {218case direct_call: kindstr = "direct"; break;219case vtable_call: kindstr = "vtable"; break;220case itable_call: kindstr = "itable"; break;221default : kindstr = "unknown"; break;222}223tty->print_cr("Call %s@%d %s", kindstr, _call_index,224_resolved_method.is_null() ? "(none)" : _resolved_method->name_and_sig_as_C_string());225}226#endif227228//------------------------------------------------------------------------------------------------------------------------229// Implementation of LinkInfo230231LinkInfo::LinkInfo(const constantPoolHandle& pool, int index, const methodHandle& current_method, TRAPS) {232// resolve klass233_resolved_klass = pool->klass_ref_at(index, CHECK);234235// Get name, signature, and static klass236_name = pool->name_ref_at(index);237_signature = pool->signature_ref_at(index);238_tag = pool->tag_ref_at(index);239_current_klass = pool->pool_holder();240_current_method = current_method;241242// Coming from the constant pool always checks access243_check_access = true;244_check_loader_constraints = true;245}246247LinkInfo::LinkInfo(const constantPoolHandle& pool, int index, TRAPS) {248// resolve klass249_resolved_klass = pool->klass_ref_at(index, CHECK);250251// Get name, signature, and static klass252_name = pool->name_ref_at(index);253_signature = pool->signature_ref_at(index);254_tag = pool->tag_ref_at(index);255_current_klass = pool->pool_holder();256_current_method = methodHandle();257258// Coming from the constant pool always checks access259_check_access = true;260_check_loader_constraints = true;261}262263#ifndef PRODUCT264void LinkInfo::print() {265ResourceMark rm;266tty->print_cr("Link resolved_klass=%s name=%s signature=%s current_klass=%s check_access=%s check_loader_constraints=%s",267_resolved_klass->name()->as_C_string(),268_name->as_C_string(),269_signature->as_C_string(),270_current_klass == NULL ? "(none)" : _current_klass->name()->as_C_string(),271_check_access ? "true" : "false",272_check_loader_constraints ? "true" : "false");273274}275#endif // PRODUCT276//------------------------------------------------------------------------------------------------------------------------277// Klass resolution278279void LinkResolver::check_klass_accessibility(Klass* ref_klass, Klass* sel_klass, TRAPS) {280Klass* base_klass = sel_klass;281if (sel_klass->is_objArray_klass()) {282base_klass = ObjArrayKlass::cast(sel_klass)->bottom_klass();283}284// The element type could be a typeArray - we only need the access285// check if it is a reference to another class.286if (!base_klass->is_instance_klass()) {287return; // no relevant check to do288}289290Reflection::VerifyClassAccessResults vca_result =291Reflection::verify_class_access(ref_klass, InstanceKlass::cast(base_klass), true);292if (vca_result != Reflection::ACCESS_OK) {293ResourceMark rm(THREAD);294char* msg = Reflection::verify_class_access_msg(ref_klass,295InstanceKlass::cast(base_klass),296vca_result);297bool same_module = (base_klass->module() == ref_klass->module());298if (msg == NULL) {299Exceptions::fthrow(300THREAD_AND_LOCATION,301vmSymbols::java_lang_IllegalAccessError(),302"failed to access class %s from class %s (%s%s%s)",303base_klass->external_name(),304ref_klass->external_name(),305(same_module) ? base_klass->joint_in_module_of_loader(ref_klass) : base_klass->class_in_module_of_loader(),306(same_module) ? "" : "; ",307(same_module) ? "" : ref_klass->class_in_module_of_loader());308} else {309// Use module specific message returned by verify_class_access_msg().310Exceptions::fthrow(311THREAD_AND_LOCATION,312vmSymbols::java_lang_IllegalAccessError(),313"%s", msg);314}315}316}317318//------------------------------------------------------------------------------------------------------------------------319// Method resolution320//321// According to JVM spec. $5.4.3c & $5.4.3d322323// Look up method in klasses, including static methods324// Then look up local default methods325Method* LinkResolver::lookup_method_in_klasses(const LinkInfo& link_info,326bool checkpolymorphism,327bool in_imethod_resolve) {328NoSafepointVerifier nsv; // Method* returned may not be reclaimed329330Klass* klass = link_info.resolved_klass();331Symbol* name = link_info.name();332Symbol* signature = link_info.signature();333334// Ignore overpasses so statics can be found during resolution335Method* result = klass->uncached_lookup_method(name, signature, Klass::OverpassLookupMode::skip);336337if (klass->is_array_klass()) {338// Only consider klass and super klass for arrays339return result;340}341342InstanceKlass* ik = InstanceKlass::cast(klass);343344// JDK 8, JVMS 5.4.3.4: Interface method resolution should345// ignore static and non-public methods of java.lang.Object,346// like clone and finalize.347if (in_imethod_resolve &&348result != NULL &&349ik->is_interface() &&350(result->is_static() || !result->is_public()) &&351result->method_holder() == vmClasses::Object_klass()) {352result = NULL;353}354355// Before considering default methods, check for an overpass in the356// current class if a method has not been found.357if (result == NULL) {358result = ik->find_method(name, signature);359}360361if (result == NULL) {362Array<Method*>* default_methods = ik->default_methods();363if (default_methods != NULL) {364result = InstanceKlass::find_method(default_methods, name, signature);365}366}367368if (checkpolymorphism && result != NULL) {369vmIntrinsics::ID iid = result->intrinsic_id();370if (MethodHandles::is_signature_polymorphic(iid)) {371// Do not link directly to these. The VM must produce a synthetic one using lookup_polymorphic_method.372return NULL;373}374}375return result;376}377378// returns first instance method379// Looks up method in classes, then looks up local default methods380Method* LinkResolver::lookup_instance_method_in_klasses(Klass* klass,381Symbol* name,382Symbol* signature,383Klass::PrivateLookupMode private_mode) {384Method* result = klass->uncached_lookup_method(name, signature, Klass::OverpassLookupMode::find, private_mode);385386while (result != NULL && result->is_static() && result->method_holder()->super() != NULL) {387Klass* super_klass = result->method_holder()->super();388result = super_klass->uncached_lookup_method(name, signature, Klass::OverpassLookupMode::find, private_mode);389}390391if (klass->is_array_klass()) {392// Only consider klass and super klass for arrays393return result;394}395396if (result == NULL) {397Array<Method*>* default_methods = InstanceKlass::cast(klass)->default_methods();398if (default_methods != NULL) {399result = InstanceKlass::find_method(default_methods, name, signature);400assert(result == NULL || !result->is_static(), "static defaults not allowed");401}402}403return result;404}405406int LinkResolver::vtable_index_of_interface_method(Klass* klass, const methodHandle& resolved_method) {407InstanceKlass* ik = InstanceKlass::cast(klass);408return ik->vtable_index_of_interface_method(resolved_method());409}410411Method* LinkResolver::lookup_method_in_interfaces(const LinkInfo& cp_info) {412InstanceKlass *ik = InstanceKlass::cast(cp_info.resolved_klass());413414// Specify 'true' in order to skip default methods when searching the415// interfaces. Function lookup_method_in_klasses() already looked for416// the method in the default methods table.417return ik->lookup_method_in_all_interfaces(cp_info.name(), cp_info.signature(), Klass::DefaultsLookupMode::skip);418}419420Method* LinkResolver::lookup_polymorphic_method(const LinkInfo& link_info,421Handle *appendix_result_or_null,422TRAPS) {423ResourceMark rm(THREAD);424Klass* klass = link_info.resolved_klass();425Symbol* name = link_info.name();426Symbol* full_signature = link_info.signature();427LogTarget(Info, methodhandles) lt_mh;428429vmIntrinsics::ID iid = MethodHandles::signature_polymorphic_name_id(name);430log_info(methodhandles)("lookup_polymorphic_method iid=%s %s.%s%s",431vmIntrinsics::name_at(iid), klass->external_name(),432name->as_C_string(), full_signature->as_C_string());433if ((klass == vmClasses::MethodHandle_klass() ||434klass == vmClasses::VarHandle_klass()) &&435iid != vmIntrinsics::_none) {436if (MethodHandles::is_signature_polymorphic_intrinsic(iid)) {437// Most of these do not need an up-call to Java to resolve, so can be done anywhere.438// Do not erase last argument type (MemberName) if it is a static linkTo method.439bool keep_last_arg = MethodHandles::is_signature_polymorphic_static(iid);440TempNewSymbol basic_signature =441MethodHandles::lookup_basic_type_signature(full_signature, keep_last_arg);442log_info(methodhandles)("lookup_polymorphic_method %s %s => basic %s",443name->as_C_string(),444full_signature->as_C_string(),445basic_signature->as_C_string());446Method* result = SystemDictionary::find_method_handle_intrinsic(iid,447basic_signature,448CHECK_NULL);449if (result != NULL) {450assert(result->is_method_handle_intrinsic(), "MH.invokeBasic or MH.linkTo* intrinsic");451assert(result->intrinsic_id() != vmIntrinsics::_invokeGeneric, "wrong place to find this");452assert(basic_signature == result->signature(), "predict the result signature");453if (lt_mh.is_enabled()) {454LogStream ls(lt_mh);455ls.print("lookup_polymorphic_method => intrinsic ");456result->print_on(&ls);457}458}459return result;460} else if (iid == vmIntrinsics::_invokeGeneric461&& THREAD->can_call_java()462&& appendix_result_or_null != NULL) {463// This is a method with type-checking semantics.464// We will ask Java code to spin an adapter method for it.465if (!MethodHandles::enabled()) {466// Make sure the Java part of the runtime has been booted up.467Klass* natives = vmClasses::MethodHandleNatives_klass();468if (natives == NULL || InstanceKlass::cast(natives)->is_not_initialized()) {469SystemDictionary::resolve_or_fail(vmSymbols::java_lang_invoke_MethodHandleNatives(),470Handle(),471Handle(),472true,473CHECK_NULL);474}475}476477Handle appendix;478Handle method_type;479Method* result = SystemDictionary::find_method_handle_invoker(480klass,481name,482full_signature,483link_info.current_klass(),484&appendix,485CHECK_NULL);486if (lt_mh.is_enabled()) {487LogStream ls(lt_mh);488ls.print("lookup_polymorphic_method => (via Java) ");489result->print_on(&ls);490ls.print(" lookup_polymorphic_method => appendix = ");491appendix.is_null() ? ls.print_cr("(none)") : appendix->print_on(&ls);492}493if (result != NULL) {494#ifdef ASSERT495ResourceMark rm(THREAD);496497TempNewSymbol basic_signature =498MethodHandles::lookup_basic_type_signature(full_signature);499int actual_size_of_params = result->size_of_parameters();500int expected_size_of_params = ArgumentSizeComputer(basic_signature).size();501// +1 for MethodHandle.this, +1 for trailing MethodType502if (!MethodHandles::is_signature_polymorphic_static(iid)) expected_size_of_params += 1;503if (appendix.not_null()) expected_size_of_params += 1;504if (actual_size_of_params != expected_size_of_params) {505tty->print_cr("*** basic_signature=%s", basic_signature->as_C_string());506tty->print_cr("*** result for %s: ", vmIntrinsics::name_at(iid));507result->print();508}509assert(actual_size_of_params == expected_size_of_params,510"%d != %d", actual_size_of_params, expected_size_of_params);511#endif //ASSERT512513assert(appendix_result_or_null != NULL, "");514(*appendix_result_or_null) = appendix;515}516return result;517}518}519return NULL;520}521522static void print_nest_host_error_on(stringStream* ss, Klass* ref_klass, Klass* sel_klass) {523assert(ref_klass->is_instance_klass(), "must be");524assert(sel_klass->is_instance_klass(), "must be");525InstanceKlass* ref_ik = InstanceKlass::cast(ref_klass);526InstanceKlass* sel_ik = InstanceKlass::cast(sel_klass);527const char* nest_host_error_1 = ref_ik->nest_host_error();528const char* nest_host_error_2 = sel_ik->nest_host_error();529if (nest_host_error_1 != NULL || nest_host_error_2 != NULL) {530ss->print(", (%s%s%s)",531(nest_host_error_1 != NULL) ? nest_host_error_1 : "",532(nest_host_error_1 != NULL && nest_host_error_2 != NULL) ? ", " : "",533(nest_host_error_2 != NULL) ? nest_host_error_2 : "");534}535}536537void LinkResolver::check_method_accessability(Klass* ref_klass,538Klass* resolved_klass,539Klass* sel_klass,540const methodHandle& sel_method,541TRAPS) {542543AccessFlags flags = sel_method->access_flags();544545// Special case: arrays always override "clone". JVMS 2.15.546// If the resolved klass is an array class, and the declaring class547// is java.lang.Object and the method is "clone", set the flags548// to public.549//550// We'll check for the method name first, as that's most likely551// to be false (so we'll short-circuit out of these tests).552if (sel_method->name() == vmSymbols::clone_name() &&553sel_klass == vmClasses::Object_klass() &&554resolved_klass->is_array_klass()) {555// We need to change "protected" to "public".556assert(flags.is_protected(), "clone not protected?");557jint new_flags = flags.as_int();558new_flags = new_flags & (~JVM_ACC_PROTECTED);559new_flags = new_flags | JVM_ACC_PUBLIC;560flags.set_flags(new_flags);561}562// assert(extra_arg_result_or_null != NULL, "must be able to return extra argument");563564bool can_access = Reflection::verify_member_access(ref_klass,565resolved_klass,566sel_klass,567flags,568true, false, CHECK);569// Any existing exceptions that may have been thrown570// have been allowed to propagate.571if (!can_access) {572ResourceMark rm(THREAD);573stringStream ss;574bool same_module = (sel_klass->module() == ref_klass->module());575ss.print("class %s tried to access %s%s%smethod '%s' (%s%s%s)",576ref_klass->external_name(),577sel_method->is_abstract() ? "abstract " : "",578sel_method->is_protected() ? "protected " : "",579sel_method->is_private() ? "private " : "",580sel_method->external_name(),581(same_module) ? ref_klass->joint_in_module_of_loader(sel_klass) : ref_klass->class_in_module_of_loader(),582(same_module) ? "" : "; ",583(same_module) ? "" : sel_klass->class_in_module_of_loader()584);585586// For private access see if there was a problem with nest host587// resolution, and if so report that as part of the message.588if (sel_method->is_private()) {589print_nest_host_error_on(&ss, ref_klass, sel_klass);590}591592Exceptions::fthrow(THREAD_AND_LOCATION,593vmSymbols::java_lang_IllegalAccessError(),594"%s",595ss.as_string()596);597return;598}599}600601Method* LinkResolver::resolve_method_statically(Bytecodes::Code code,602const constantPoolHandle& pool, int index, TRAPS) {603// This method is used only604// (1) in C2 from InlineTree::ok_to_inline (via ciMethod::check_call),605// and606// (2) in Bytecode_invoke::static_target607// It appears to fail when applied to an invokeinterface call site.608// FIXME: Remove this method and ciMethod::check_call; refactor to use the other LinkResolver entry points.609// resolve klass610if (code == Bytecodes::_invokedynamic) {611Klass* resolved_klass = vmClasses::MethodHandle_klass();612Symbol* method_name = vmSymbols::invoke_name();613Symbol* method_signature = pool->signature_ref_at(index);614Klass* current_klass = pool->pool_holder();615LinkInfo link_info(resolved_klass, method_name, method_signature, current_klass);616return resolve_method(link_info, code, THREAD);617}618619LinkInfo link_info(pool, index, methodHandle(), CHECK_NULL);620Klass* resolved_klass = link_info.resolved_klass();621622if (pool->has_preresolution()623|| (resolved_klass == vmClasses::MethodHandle_klass() &&624MethodHandles::is_signature_polymorphic_name(resolved_klass, link_info.name()))) {625Method* result = ConstantPool::method_at_if_loaded(pool, index);626if (result != NULL) {627return result;628}629}630631if (code == Bytecodes::_invokeinterface) {632return resolve_interface_method(link_info, code, THREAD);633} else if (code == Bytecodes::_invokevirtual) {634return resolve_method(link_info, code, THREAD);635} else if (!resolved_klass->is_interface()) {636return resolve_method(link_info, code, THREAD);637} else {638return resolve_interface_method(link_info, code, THREAD);639}640}641642// Check and print a loader constraint violation message for method or interface method643void LinkResolver::check_method_loader_constraints(const LinkInfo& link_info,644const methodHandle& resolved_method,645const char* method_type, TRAPS) {646Handle current_loader(THREAD, link_info.current_klass()->class_loader());647Handle resolved_loader(THREAD, resolved_method->method_holder()->class_loader());648649ResourceMark rm(THREAD);650Symbol* failed_type_symbol =651SystemDictionary::check_signature_loaders(link_info.signature(),652/*klass_being_linked*/ NULL, // We are not linking class653current_loader,654resolved_loader, true);655if (failed_type_symbol != NULL) {656Klass* current_class = link_info.current_klass();657ClassLoaderData* current_loader_data = current_class->class_loader_data();658assert(current_loader_data != NULL, "current class has no class loader data");659Klass* resolved_method_class = resolved_method->method_holder();660ClassLoaderData* target_loader_data = resolved_method_class->class_loader_data();661assert(target_loader_data != NULL, "resolved method's class has no class loader data");662663stringStream ss;664ss.print("loader constraint violation: when resolving %s '", method_type);665Method::print_external_name(&ss, link_info.resolved_klass(), link_info.name(), link_info.signature());666ss.print("' the class loader %s of the current class, %s,"667" and the class loader %s for the method's defining class, %s, have"668" different Class objects for the type %s used in the signature (%s; %s)",669current_loader_data->loader_name_and_id(),670current_class->name()->as_C_string(),671target_loader_data->loader_name_and_id(),672resolved_method_class->name()->as_C_string(),673failed_type_symbol->as_C_string(),674current_class->class_in_module_of_loader(false, true),675resolved_method_class->class_in_module_of_loader(false, true));676THROW_MSG(vmSymbols::java_lang_LinkageError(), ss.as_string());677}678}679680void LinkResolver::check_field_loader_constraints(Symbol* field, Symbol* sig,681Klass* current_klass,682Klass* sel_klass, TRAPS) {683Handle ref_loader(THREAD, current_klass->class_loader());684Handle sel_loader(THREAD, sel_klass->class_loader());685686ResourceMark rm(THREAD); // needed for check_signature_loaders687Symbol* failed_type_symbol =688SystemDictionary::check_signature_loaders(sig,689/*klass_being_linked*/ NULL, // We are not linking class690ref_loader, sel_loader,691false);692if (failed_type_symbol != NULL) {693stringStream ss;694const char* failed_type_name = failed_type_symbol->as_klass_external_name();695696ss.print("loader constraint violation: when resolving field \"%s\" of type %s, "697"the class loader %s of the current class, %s, "698"and the class loader %s for the field's defining %s, %s, "699"have different Class objects for type %s (%s; %s)",700field->as_C_string(),701failed_type_name,702current_klass->class_loader_data()->loader_name_and_id(),703current_klass->external_name(),704sel_klass->class_loader_data()->loader_name_and_id(),705sel_klass->external_kind(),706sel_klass->external_name(),707failed_type_name,708current_klass->class_in_module_of_loader(false, true),709sel_klass->class_in_module_of_loader(false, true));710THROW_MSG(vmSymbols::java_lang_LinkageError(), ss.as_string());711}712}713714Method* LinkResolver::resolve_method(const LinkInfo& link_info,715Bytecodes::Code code, TRAPS) {716717Handle nested_exception;718Klass* resolved_klass = link_info.resolved_klass();719720// 1. For invokevirtual, cannot call an interface method721if (code == Bytecodes::_invokevirtual && resolved_klass->is_interface()) {722ResourceMark rm(THREAD);723char buf[200];724jio_snprintf(buf, sizeof(buf), "Found interface %s, but class was expected",725resolved_klass->external_name());726THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);727}728729// 2. check constant pool tag for called method - must be JVM_CONSTANT_Methodref730if (!link_info.tag().is_invalid() && !link_info.tag().is_method()) {731ResourceMark rm(THREAD);732stringStream ss;733ss.print("Method '");734Method::print_external_name(&ss, link_info.resolved_klass(), link_info.name(), link_info.signature());735ss.print("' must be Methodref constant");736THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());737}738739// 3. lookup method in resolved klass and its super klasses740methodHandle resolved_method(THREAD, lookup_method_in_klasses(link_info, true, false));741742// 4. lookup method in all the interfaces implemented by the resolved klass743if (resolved_method.is_null() && !resolved_klass->is_array_klass()) { // not found in the class hierarchy744resolved_method = methodHandle(THREAD, lookup_method_in_interfaces(link_info));745746if (resolved_method.is_null()) {747// JSR 292: see if this is an implicitly generated method MethodHandle.linkToVirtual(*...), etc748Method* method = lookup_polymorphic_method(link_info, (Handle*)NULL, THREAD);749resolved_method = methodHandle(THREAD, method);750if (HAS_PENDING_EXCEPTION) {751nested_exception = Handle(THREAD, PENDING_EXCEPTION);752CLEAR_PENDING_EXCEPTION;753}754}755}756757// 5. method lookup failed758if (resolved_method.is_null()) {759ResourceMark rm(THREAD);760stringStream ss;761ss.print("'");762Method::print_external_name(&ss, resolved_klass, link_info.name(), link_info.signature());763ss.print("'");764THROW_MSG_CAUSE_(vmSymbols::java_lang_NoSuchMethodError(),765ss.as_string(), nested_exception, NULL);766}767768// 6. access checks, access checking may be turned off when calling from within the VM.769Klass* current_klass = link_info.current_klass();770if (link_info.check_access()) {771assert(current_klass != NULL , "current_klass should not be null");772773// check if method can be accessed by the referring class774check_method_accessability(current_klass,775resolved_klass,776resolved_method->method_holder(),777resolved_method,778CHECK_NULL);779}780if (link_info.check_loader_constraints()) {781// check loader constraints782check_method_loader_constraints(link_info, resolved_method, "method", CHECK_NULL);783}784785return resolved_method();786}787788static void trace_method_resolution(const char* prefix,789Klass* klass,790Klass* resolved_klass,791Method* method,792bool logitables,793int index = -1) {794#ifndef PRODUCT795ResourceMark rm;796Log(itables) logi;797LogStream lsi(logi.trace());798Log(vtables) logv;799LogStream lsv(logv.trace());800outputStream* st;801if (logitables) {802st = &lsi;803} else {804st = &lsv;805}806st->print("%s%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ",807prefix,808(klass == NULL ? "<NULL>" : klass->internal_name()),809(resolved_klass == NULL ? "<NULL>" : resolved_klass->internal_name()),810Method::name_and_sig_as_C_string(resolved_klass,811method->name(),812method->signature()),813method->method_holder()->internal_name());814method->print_linkage_flags(st);815if (index != -1) {816st->print("vtable_index:%d", index);817}818st->cr();819#endif // PRODUCT820}821822// Do linktime resolution of a method in the interface within the context of the specied bytecode.823Method* LinkResolver::resolve_interface_method(const LinkInfo& link_info, Bytecodes::Code code, TRAPS) {824825Klass* resolved_klass = link_info.resolved_klass();826827// check if klass is interface828if (!resolved_klass->is_interface()) {829ResourceMark rm(THREAD);830char buf[200];831jio_snprintf(buf, sizeof(buf), "Found class %s, but interface was expected", resolved_klass->external_name());832THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);833}834835// check constant pool tag for called method - must be JVM_CONSTANT_InterfaceMethodref836if (!link_info.tag().is_invalid() && !link_info.tag().is_interface_method()) {837ResourceMark rm(THREAD);838stringStream ss;839ss.print("Method '");840Method::print_external_name(&ss, link_info.resolved_klass(), link_info.name(), link_info.signature());841ss.print("' must be InterfaceMethodref constant");842THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());843}844845// lookup method in this interface or its super, java.lang.Object846// JDK8: also look for static methods847methodHandle resolved_method(THREAD, lookup_method_in_klasses(link_info, false, true));848849if (resolved_method.is_null() && !resolved_klass->is_array_klass()) {850// lookup method in all the super-interfaces851resolved_method = methodHandle(THREAD, lookup_method_in_interfaces(link_info));852}853854if (resolved_method.is_null()) {855// no method found856ResourceMark rm(THREAD);857stringStream ss;858ss.print("'");859Method::print_external_name(&ss, resolved_klass, link_info.name(), link_info.signature());860ss.print("'");861THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(), ss.as_string());862}863864if (link_info.check_access()) {865// JDK8 adds non-public interface methods, and accessability check requirement866Klass* current_klass = link_info.current_klass();867868assert(current_klass != NULL , "current_klass should not be null");869870// check if method can be accessed by the referring class871check_method_accessability(current_klass,872resolved_klass,873resolved_method->method_holder(),874resolved_method,875CHECK_NULL);876}877if (link_info.check_loader_constraints()) {878check_method_loader_constraints(link_info, resolved_method, "interface method", CHECK_NULL);879}880881if (code != Bytecodes::_invokestatic && resolved_method->is_static()) {882ResourceMark rm(THREAD);883stringStream ss;884ss.print("Expected instance not static method '");885Method::print_external_name(&ss, resolved_klass,886resolved_method->name(), resolved_method->signature());887ss.print("'");888THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());889}890891if (log_develop_is_enabled(Trace, itables)) {892char buf[200];893jio_snprintf(buf, sizeof(buf), "%s resolved interface method: caller-class:",894Bytecodes::name(code));895trace_method_resolution(buf, link_info.current_klass(), resolved_klass, resolved_method(), true);896}897898return resolved_method();899}900901//------------------------------------------------------------------------------------------------------------------------902// Field resolution903904void LinkResolver::check_field_accessability(Klass* ref_klass,905Klass* resolved_klass,906Klass* sel_klass,907const fieldDescriptor& fd,908TRAPS) {909bool can_access = Reflection::verify_member_access(ref_klass,910resolved_klass,911sel_klass,912fd.access_flags(),913true, false, CHECK);914// Any existing exceptions that may have been thrown, for example LinkageErrors915// from nest-host resolution, have been allowed to propagate.916if (!can_access) {917bool same_module = (sel_klass->module() == ref_klass->module());918ResourceMark rm(THREAD);919stringStream ss;920ss.print("class %s tried to access %s%sfield %s.%s (%s%s%s)",921ref_klass->external_name(),922fd.is_protected() ? "protected " : "",923fd.is_private() ? "private " : "",924sel_klass->external_name(),925fd.name()->as_C_string(),926(same_module) ? ref_klass->joint_in_module_of_loader(sel_klass) : ref_klass->class_in_module_of_loader(),927(same_module) ? "" : "; ",928(same_module) ? "" : sel_klass->class_in_module_of_loader()929);930// For private access see if there was a problem with nest host931// resolution, and if so report that as part of the message.932if (fd.is_private()) {933print_nest_host_error_on(&ss, ref_klass, sel_klass);934}935Exceptions::fthrow(THREAD_AND_LOCATION,936vmSymbols::java_lang_IllegalAccessError(),937"%s",938ss.as_string()939);940return;941}942}943944void LinkResolver::resolve_field_access(fieldDescriptor& fd, const constantPoolHandle& pool, int index, const methodHandle& method, Bytecodes::Code byte, TRAPS) {945LinkInfo link_info(pool, index, method, CHECK);946resolve_field(fd, link_info, byte, true, CHECK);947}948949void LinkResolver::resolve_field(fieldDescriptor& fd,950const LinkInfo& link_info,951Bytecodes::Code byte, bool initialize_class,952TRAPS) {953assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic ||954byte == Bytecodes::_getfield || byte == Bytecodes::_putfield ||955byte == Bytecodes::_nofast_getfield || byte == Bytecodes::_nofast_putfield ||956(byte == Bytecodes::_nop && !link_info.check_access()), "bad field access bytecode");957958bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic);959bool is_put = (byte == Bytecodes::_putfield || byte == Bytecodes::_putstatic || byte == Bytecodes::_nofast_putfield);960// Check if there's a resolved klass containing the field961Klass* resolved_klass = link_info.resolved_klass();962Symbol* field = link_info.name();963Symbol* sig = link_info.signature();964965if (resolved_klass == NULL) {966ResourceMark rm(THREAD);967THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());968}969970// Resolve instance field971Klass* sel_klass = resolved_klass->find_field(field, sig, &fd);972// check if field exists; i.e., if a klass containing the field def has been selected973if (sel_klass == NULL) {974ResourceMark rm(THREAD);975THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());976}977978// Access checking may be turned off when calling from within the VM.979Klass* current_klass = link_info.current_klass();980if (link_info.check_access()) {981982// check access983check_field_accessability(current_klass, resolved_klass, sel_klass, fd, CHECK);984985// check for errors986if (is_static != fd.is_static()) {987ResourceMark rm(THREAD);988char msg[200];989jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string());990THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg);991}992993// A final field can be modified only994// (1) by methods declared in the class declaring the field and995// (2) by the <clinit> method (in case of a static field)996// or by the <init> method (in case of an instance field).997if (is_put && fd.access_flags().is_final()) {998999if (sel_klass != current_klass) {1000ResourceMark rm(THREAD);1001stringStream ss;1002ss.print("Update to %s final field %s.%s attempted from a different class (%s) than the field's declaring class",1003is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),1004current_klass->external_name());1005THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());1006}10071008if (fd.constants()->pool_holder()->major_version() >= 53) {1009Method* m = link_info.current_method();1010assert(m != NULL, "information about the current method must be available for 'put' bytecodes");1011bool is_initialized_static_final_update = (byte == Bytecodes::_putstatic &&1012fd.is_static() &&1013!m->is_static_initializer());1014bool is_initialized_instance_final_update = ((byte == Bytecodes::_putfield || byte == Bytecodes::_nofast_putfield) &&1015!fd.is_static() &&1016!m->is_object_initializer());10171018if (is_initialized_static_final_update || is_initialized_instance_final_update) {1019ResourceMark rm(THREAD);1020stringStream ss;1021ss.print("Update to %s final field %s.%s attempted from a different method (%s) than the initializer method %s ",1022is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),1023m->name()->as_C_string(),1024is_static ? "<clinit>" : "<init>");1025THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());1026}1027}1028}10291030// initialize resolved_klass if necessary1031// note 1: the klass which declared the field must be initialized (i.e, sel_klass)1032// according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99)1033//1034// note 2: we don't want to force initialization if we are just checking1035// if the field access is legal; e.g., during compilation1036if (is_static && initialize_class) {1037sel_klass->initialize(CHECK);1038}1039}10401041if (link_info.check_loader_constraints() && (sel_klass != current_klass) && (current_klass != NULL)) {1042check_field_loader_constraints(field, sig, current_klass, sel_klass, CHECK);1043}10441045// return information. note that the klass is set to the actual klass containing the1046// field, otherwise access of static fields in superclasses will not work.1047}104810491050//------------------------------------------------------------------------------------------------------------------------1051// Invoke resolution1052//1053// Naming conventions:1054//1055// resolved_method the specified method (i.e., static receiver specified via constant pool index)1056// sel_method the selected method (selected via run-time lookup; e.g., based on dynamic receiver class)1057// resolved_klass the specified klass (i.e., specified via constant pool index)1058// recv_klass the receiver klass105910601061void LinkResolver::resolve_static_call(CallInfo& result,1062const LinkInfo& link_info,1063bool initialize_class, TRAPS) {1064Method* resolved_method = linktime_resolve_static_method(link_info, CHECK);10651066// The resolved class can change as a result of this resolution.1067Klass* resolved_klass = resolved_method->method_holder();10681069// Initialize klass (this should only happen if everything is ok)1070if (initialize_class && resolved_klass->should_be_initialized()) {1071resolved_klass->initialize(CHECK);1072// Use updated LinkInfo to reresolve with resolved method holder1073LinkInfo new_info(resolved_klass, link_info.name(), link_info.signature(),1074link_info.current_klass(),1075link_info.check_access() ? LinkInfo::AccessCheck::required : LinkInfo::AccessCheck::skip,1076link_info.check_loader_constraints() ? LinkInfo::LoaderConstraintCheck::required : LinkInfo::LoaderConstraintCheck::skip);1077resolved_method = linktime_resolve_static_method(new_info, CHECK);1078}10791080// setup result1081result.set_static(resolved_klass, methodHandle(THREAD, resolved_method), CHECK);1082}10831084// throws linktime exceptions1085Method* LinkResolver::linktime_resolve_static_method(const LinkInfo& link_info, TRAPS) {10861087Klass* resolved_klass = link_info.resolved_klass();1088Method* resolved_method;1089if (!resolved_klass->is_interface()) {1090resolved_method = resolve_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);1091} else {1092resolved_method = resolve_interface_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);1093}1094assert(resolved_method->name() != vmSymbols::class_initializer_name(), "should have been checked in verifier");10951096// check if static1097if (!resolved_method->is_static()) {1098ResourceMark rm(THREAD);1099stringStream ss;1100ss.print("Expected static method '");1101resolved_method->print_external_name(&ss);1102ss.print("'");1103THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());1104}1105return resolved_method;1106}110711081109void LinkResolver::resolve_special_call(CallInfo& result,1110Handle recv,1111const LinkInfo& link_info,1112TRAPS) {1113Method* resolved_method = linktime_resolve_special_method(link_info, CHECK);1114runtime_resolve_special_method(result, link_info, methodHandle(THREAD, resolved_method), recv, CHECK);1115}11161117// throws linktime exceptions1118Method* LinkResolver::linktime_resolve_special_method(const LinkInfo& link_info, TRAPS) {11191120// Invokespecial is called for multiple special reasons:1121// <init>1122// local private method invocation, for classes and interfaces1123// superclass.method, which can also resolve to a default method1124// and the selected method is recalculated relative to the direct superclass1125// superinterface.method, which explicitly does not check shadowing1126Klass* resolved_klass = link_info.resolved_klass();1127Method* resolved_method = NULL;11281129if (!resolved_klass->is_interface()) {1130resolved_method = resolve_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);1131} else {1132resolved_method = resolve_interface_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);1133}11341135// check if method name is <init>, that it is found in same klass as static type1136if (resolved_method->name() == vmSymbols::object_initializer_name() &&1137resolved_method->method_holder() != resolved_klass) {1138ResourceMark rm(THREAD);1139stringStream ss;1140ss.print("%s: method '", resolved_klass->external_name());1141resolved_method->signature()->print_as_signature_external_return_type(&ss);1142ss.print(" %s(", resolved_method->name()->as_C_string());1143resolved_method->signature()->print_as_signature_external_parameters(&ss);1144ss.print(")' not found");1145Exceptions::fthrow(1146THREAD_AND_LOCATION,1147vmSymbols::java_lang_NoSuchMethodError(),1148"%s", ss.as_string());1149return NULL;1150}11511152// ensure that invokespecial's interface method reference is in1153// a direct superinterface, not an indirect superinterface1154Klass* current_klass = link_info.current_klass();1155if (current_klass != NULL && resolved_klass->is_interface()) {1156InstanceKlass* klass_to_check = InstanceKlass::cast(current_klass);1157// Disable verification for the dynamically-generated reflection bytecodes.1158bool is_reflect = klass_to_check->is_subclass_of(1159vmClasses::reflect_MagicAccessorImpl_klass());11601161if (!is_reflect &&1162!klass_to_check->is_same_or_direct_interface(resolved_klass)) {1163ResourceMark rm(THREAD);1164stringStream ss;1165ss.print("Interface method reference: '");1166resolved_method->print_external_name(&ss);1167ss.print("', is in an indirect superinterface of %s",1168current_klass->external_name());1169THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());1170}1171}11721173// check if not static1174if (resolved_method->is_static()) {1175ResourceMark rm(THREAD);1176stringStream ss;1177ss.print("Expecting non-static method '");1178resolved_method->print_external_name(&ss);1179ss.print("'");1180THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());1181}11821183if (log_develop_is_enabled(Trace, itables)) {1184trace_method_resolution("invokespecial resolved method: caller-class:",1185current_klass, resolved_klass, resolved_method, true);1186}11871188return resolved_method;1189}11901191// throws runtime exceptions1192void LinkResolver::runtime_resolve_special_method(CallInfo& result,1193const LinkInfo& link_info,1194const methodHandle& resolved_method,1195Handle recv, TRAPS) {11961197Klass* resolved_klass = link_info.resolved_klass();11981199// resolved method is selected method unless we have an old-style lookup1200// for a superclass method1201// Invokespecial for a superinterface, resolved method is selected method,1202// no checks for shadowing1203methodHandle sel_method(THREAD, resolved_method());12041205if (link_info.check_access() &&1206// check if the method is not <init>1207resolved_method->name() != vmSymbols::object_initializer_name()) {12081209Klass* current_klass = link_info.current_klass();12101211// Check if the class of the resolved_klass is a superclass1212// (not supertype in order to exclude interface classes) of the current class.1213// This check is not performed for super.invoke for interface methods1214// in super interfaces.1215if (current_klass->is_subclass_of(resolved_klass) &&1216current_klass != resolved_klass) {1217// Lookup super method1218Klass* super_klass = current_klass->super();1219Method* instance_method = lookup_instance_method_in_klasses(super_klass,1220resolved_method->name(),1221resolved_method->signature(),1222Klass::PrivateLookupMode::find);1223sel_method = methodHandle(THREAD, instance_method);12241225// check if found1226if (sel_method.is_null()) {1227ResourceMark rm(THREAD);1228stringStream ss;1229ss.print("'");1230resolved_method->print_external_name(&ss);1231ss.print("'");1232THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), ss.as_string());1233// check loader constraints if found a different method1234} else if (link_info.check_loader_constraints() && sel_method() != resolved_method()) {1235check_method_loader_constraints(link_info, sel_method, "method", CHECK);1236}1237}12381239// Check that the class of objectref (the receiver) is the current class or interface,1240// or a subtype of the current class or interface (the sender), otherwise invokespecial1241// throws IllegalAccessError.1242// The verifier checks that the sender is a subtype of the class in the I/MR operand.1243// The verifier also checks that the receiver is a subtype of the sender, if the sender is1244// a class. If the sender is an interface, the check has to be performed at runtime.1245InstanceKlass* sender = InstanceKlass::cast(current_klass);1246if (sender->is_interface() && recv.not_null()) {1247Klass* receiver_klass = recv->klass();1248if (!receiver_klass->is_subtype_of(sender)) {1249ResourceMark rm(THREAD);1250char buf[500];1251jio_snprintf(buf, sizeof(buf),1252"Receiver class %s must be the current class or a subtype of interface %s",1253receiver_klass->external_name(),1254sender->external_name());1255THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), buf);1256}1257}1258}12591260// check if not static1261if (sel_method->is_static()) {1262ResourceMark rm(THREAD);1263stringStream ss;1264ss.print("Expecting non-static method '");1265resolved_method->print_external_name(&ss);1266ss.print("'");1267THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());1268}12691270// check if abstract1271if (sel_method->is_abstract()) {1272ResourceMark rm(THREAD);1273stringStream ss;1274ss.print("'");1275Method::print_external_name(&ss, resolved_klass, sel_method->name(), sel_method->signature());1276ss.print("'");1277THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), ss.as_string());1278}12791280if (log_develop_is_enabled(Trace, itables)) {1281trace_method_resolution("invokespecial selected method: resolved-class:",1282resolved_klass, resolved_klass, sel_method(), true);1283}12841285// setup result1286result.set_static(resolved_klass, sel_method, CHECK);1287}12881289void LinkResolver::resolve_virtual_call(CallInfo& result, Handle recv, Klass* receiver_klass,1290const LinkInfo& link_info,1291bool check_null_and_abstract, TRAPS) {1292Method* resolved_method = linktime_resolve_virtual_method(link_info, CHECK);1293runtime_resolve_virtual_method(result, methodHandle(THREAD, resolved_method),1294link_info.resolved_klass(),1295recv, receiver_klass,1296check_null_and_abstract, CHECK);1297}12981299// throws linktime exceptions1300Method* LinkResolver::linktime_resolve_virtual_method(const LinkInfo& link_info,1301TRAPS) {1302// normal method resolution1303Method* resolved_method = resolve_method(link_info, Bytecodes::_invokevirtual, CHECK_NULL);13041305assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");1306assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");13071308// check if private interface method1309Klass* resolved_klass = link_info.resolved_klass();1310Klass* current_klass = link_info.current_klass();13111312// This is impossible, if resolve_klass is an interface, we've thrown icce in resolve_method1313if (resolved_klass->is_interface() && resolved_method->is_private()) {1314ResourceMark rm(THREAD);1315stringStream ss;1316ss.print("private interface method requires invokespecial, not invokevirtual: method '");1317resolved_method->print_external_name(&ss);1318ss.print("', caller-class: %s",1319(current_klass == NULL ? "<null>" : current_klass->internal_name()));1320THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());1321}13221323// check if not static1324if (resolved_method->is_static()) {1325ResourceMark rm(THREAD);1326stringStream ss;1327ss.print("Expecting non-static method '");1328resolved_method->print_external_name(&ss);1329ss.print("'");1330THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());1331}13321333if (log_develop_is_enabled(Trace, vtables)) {1334trace_method_resolution("invokevirtual resolved method: caller-class:",1335current_klass, resolved_klass, resolved_method, false);1336}13371338return resolved_method;1339}13401341// throws runtime exceptions1342void LinkResolver::runtime_resolve_virtual_method(CallInfo& result,1343const methodHandle& resolved_method,1344Klass* resolved_klass,1345Handle recv,1346Klass* recv_klass,1347bool check_null_and_abstract,1348TRAPS) {13491350// setup default return values1351int vtable_index = Method::invalid_vtable_index;1352methodHandle selected_method;13531354// runtime method resolution1355if (check_null_and_abstract && recv.is_null()) { // check if receiver exists1356THROW(vmSymbols::java_lang_NullPointerException());1357}13581359// Virtual methods cannot be resolved before its klass has been linked, for otherwise the Method*'s1360// has not been rewritten, and the vtable initialized. Make sure to do this after the nullcheck, since1361// a missing receiver might result in a bogus lookup.1362assert(resolved_method->method_holder()->is_linked(), "must be linked");13631364// do lookup based on receiver klass using the vtable index1365if (resolved_method->method_holder()->is_interface()) { // default or miranda method1366vtable_index = vtable_index_of_interface_method(resolved_klass, resolved_method);1367assert(vtable_index >= 0 , "we should have valid vtable index at this point");13681369selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));1370} else {1371// at this point we are sure that resolved_method is virtual and not1372// a default or miranda method; therefore, it must have a valid vtable index.1373assert(!resolved_method->has_itable_index(), "");1374vtable_index = resolved_method->vtable_index();1375// We could get a negative vtable_index of nonvirtual_vtable_index for private1376// methods, or for final methods. Private methods never appear in the vtable1377// and never override other methods. As an optimization, final methods are1378// never put in the vtable, unless they override an existing method.1379// So if we do get nonvirtual_vtable_index, it means the selected method is the1380// resolved method, and it can never be changed by an override.1381if (vtable_index == Method::nonvirtual_vtable_index) {1382assert(resolved_method->can_be_statically_bound(), "cannot override this method");1383selected_method = resolved_method;1384} else {1385selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));1386}1387}13881389// check if method exists1390if (selected_method.is_null()) {1391throw_abstract_method_error(resolved_method, recv_klass, CHECK);1392}13931394// check if abstract1395if (check_null_and_abstract && selected_method->is_abstract()) {1396// Pass arguments for generating a verbose error message.1397throw_abstract_method_error(resolved_method, selected_method, recv_klass, CHECK);1398}13991400if (log_develop_is_enabled(Trace, vtables)) {1401trace_method_resolution("invokevirtual selected method: receiver-class:",1402recv_klass, resolved_klass, selected_method(),1403false, vtable_index);1404}1405// setup result1406result.set_virtual(resolved_klass, resolved_method, selected_method, vtable_index, CHECK);1407}14081409void LinkResolver::resolve_interface_call(CallInfo& result, Handle recv, Klass* recv_klass,1410const LinkInfo& link_info,1411bool check_null_and_abstract, TRAPS) {1412// throws linktime exceptions1413Method* resolved_method = linktime_resolve_interface_method(link_info, CHECK);1414methodHandle mh(THREAD, resolved_method);1415runtime_resolve_interface_method(result, mh, link_info.resolved_klass(),1416recv, recv_klass, check_null_and_abstract, CHECK);1417}14181419Method* LinkResolver::linktime_resolve_interface_method(const LinkInfo& link_info,1420TRAPS) {1421// normal interface method resolution1422Method* resolved_method = resolve_interface_method(link_info, Bytecodes::_invokeinterface, CHECK_NULL);1423assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");1424assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");14251426return resolved_method;1427}14281429// throws runtime exceptions1430void LinkResolver::runtime_resolve_interface_method(CallInfo& result,1431const methodHandle& resolved_method,1432Klass* resolved_klass,1433Handle recv,1434Klass* recv_klass,1435bool check_null_and_abstract, TRAPS) {14361437// check if receiver exists1438if (check_null_and_abstract && recv.is_null()) {1439THROW(vmSymbols::java_lang_NullPointerException());1440}14411442// check if receiver klass implements the resolved interface1443if (!recv_klass->is_subtype_of(resolved_klass)) {1444ResourceMark rm(THREAD);1445char buf[200];1446jio_snprintf(buf, sizeof(buf), "Class %s does not implement the requested interface %s",1447recv_klass->external_name(),1448resolved_klass->external_name());1449THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);1450}14511452methodHandle selected_method = resolved_method;14531454// resolve the method in the receiver class, unless it is private1455if (!resolved_method()->is_private()) {1456// do lookup based on receiver klass1457// This search must match the linktime preparation search for itable initialization1458// to correctly enforce loader constraints for interface method inheritance.1459// Private methods are skipped as the resolved method was not private.1460Method* method = lookup_instance_method_in_klasses(recv_klass,1461resolved_method->name(),1462resolved_method->signature(),1463Klass::PrivateLookupMode::skip);1464selected_method = methodHandle(THREAD, method);14651466if (selected_method.is_null() && !check_null_and_abstract) {1467// In theory this is a harmless placeholder value, but1468// in practice leaving in null affects the nsk default method tests.1469// This needs further study.1470selected_method = resolved_method;1471}1472// check if method exists1473if (selected_method.is_null()) {1474// Pass arguments for generating a verbose error message.1475throw_abstract_method_error(resolved_method, recv_klass, CHECK);1476}1477// check access1478// Throw Illegal Access Error if selected_method is not public.1479if (!selected_method->is_public()) {1480ResourceMark rm(THREAD);1481stringStream ss;1482ss.print("'");1483Method::print_external_name(&ss, recv_klass, selected_method->name(), selected_method->signature());1484ss.print("'");1485THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());1486}1487// check if abstract1488if (check_null_and_abstract && selected_method->is_abstract()) {1489throw_abstract_method_error(resolved_method, selected_method, recv_klass, CHECK);1490}1491}14921493if (log_develop_is_enabled(Trace, itables)) {1494trace_method_resolution("invokeinterface selected method: receiver-class:",1495recv_klass, resolved_klass, selected_method(), true);1496}1497// setup result1498if (resolved_method->has_vtable_index()) {1499int vtable_index = resolved_method->vtable_index();1500log_develop_trace(itables)(" -- vtable index: %d", vtable_index);1501assert(vtable_index == selected_method->vtable_index(), "sanity check");1502result.set_virtual(resolved_klass, resolved_method, selected_method, vtable_index, CHECK);1503} else if (resolved_method->has_itable_index()) {1504int itable_index = resolved_method()->itable_index();1505log_develop_trace(itables)(" -- itable index: %d", itable_index);1506result.set_interface(resolved_klass, resolved_method, selected_method, itable_index, CHECK);1507} else {1508int index = resolved_method->vtable_index();1509log_develop_trace(itables)(" -- non itable/vtable index: %d", index);1510assert(index == Method::nonvirtual_vtable_index, "Oops hit another case!");1511assert(resolved_method()->is_private() ||1512(resolved_method()->is_final() && resolved_method->method_holder() == vmClasses::Object_klass()),1513"Should only have non-virtual invokeinterface for private or final-Object methods!");1514assert(resolved_method()->can_be_statically_bound(), "Should only have non-virtual invokeinterface for statically bound methods!");1515// This sets up the nonvirtual form of "virtual" call (as needed for final and private methods)1516result.set_virtual(resolved_klass, resolved_method, resolved_method, index, CHECK);1517}1518}151915201521Method* LinkResolver::linktime_resolve_interface_method_or_null(1522const LinkInfo& link_info) {1523EXCEPTION_MARK;1524Method* method_result = linktime_resolve_interface_method(link_info, THREAD);1525if (HAS_PENDING_EXCEPTION) {1526CLEAR_PENDING_EXCEPTION;1527return NULL;1528} else {1529return method_result;1530}1531}15321533Method* LinkResolver::linktime_resolve_virtual_method_or_null(1534const LinkInfo& link_info) {1535EXCEPTION_MARK;1536Method* method_result = linktime_resolve_virtual_method(link_info, THREAD);1537if (HAS_PENDING_EXCEPTION) {1538CLEAR_PENDING_EXCEPTION;1539return NULL;1540} else {1541return method_result;1542}1543}15441545Method* LinkResolver::resolve_virtual_call_or_null(1546Klass* receiver_klass,1547const LinkInfo& link_info) {1548EXCEPTION_MARK;1549CallInfo info;1550resolve_virtual_call(info, Handle(), receiver_klass, link_info, false, THREAD);1551if (HAS_PENDING_EXCEPTION) {1552CLEAR_PENDING_EXCEPTION;1553return NULL;1554}1555return info.selected_method();1556}15571558Method* LinkResolver::resolve_interface_call_or_null(1559Klass* receiver_klass,1560const LinkInfo& link_info) {1561EXCEPTION_MARK;1562CallInfo info;1563resolve_interface_call(info, Handle(), receiver_klass, link_info, false, THREAD);1564if (HAS_PENDING_EXCEPTION) {1565CLEAR_PENDING_EXCEPTION;1566return NULL;1567}1568return info.selected_method();1569}15701571int LinkResolver::resolve_virtual_vtable_index(Klass* receiver_klass,1572const LinkInfo& link_info) {1573EXCEPTION_MARK;1574CallInfo info;1575resolve_virtual_call(info, Handle(), receiver_klass, link_info,1576/*check_null_or_abstract*/false, THREAD);1577if (HAS_PENDING_EXCEPTION) {1578CLEAR_PENDING_EXCEPTION;1579return Method::invalid_vtable_index;1580}1581return info.vtable_index();1582}15831584Method* LinkResolver::resolve_static_call_or_null(const LinkInfo& link_info) {1585EXCEPTION_MARK;1586CallInfo info;1587resolve_static_call(info, link_info, /*initialize_class*/false, THREAD);1588if (HAS_PENDING_EXCEPTION) {1589CLEAR_PENDING_EXCEPTION;1590return NULL;1591}1592return info.selected_method();1593}15941595Method* LinkResolver::resolve_special_call_or_null(const LinkInfo& link_info) {1596EXCEPTION_MARK;1597CallInfo info;1598resolve_special_call(info, Handle(), link_info, THREAD);1599if (HAS_PENDING_EXCEPTION) {1600CLEAR_PENDING_EXCEPTION;1601return NULL;1602}1603return info.selected_method();1604}1605160616071608//------------------------------------------------------------------------------------------------------------------------1609// ConstantPool entries16101611void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, Bytecodes::Code byte, TRAPS) {1612switch (byte) {1613case Bytecodes::_invokestatic : resolve_invokestatic (result, pool, index, CHECK); break;1614case Bytecodes::_invokespecial : resolve_invokespecial (result, recv, pool, index, CHECK); break;1615case Bytecodes::_invokevirtual : resolve_invokevirtual (result, recv, pool, index, CHECK); break;1616case Bytecodes::_invokehandle : resolve_invokehandle (result, pool, index, CHECK); break;1617case Bytecodes::_invokedynamic : resolve_invokedynamic (result, pool, index, CHECK); break;1618case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break;1619default : break;1620}1621return;1622}16231624void LinkResolver::resolve_invoke(CallInfo& result, Handle& recv,1625const methodHandle& attached_method,1626Bytecodes::Code byte, TRAPS) {1627Klass* defc = attached_method->method_holder();1628Symbol* name = attached_method->name();1629Symbol* type = attached_method->signature();1630LinkInfo link_info(defc, name, type);1631switch(byte) {1632case Bytecodes::_invokevirtual:1633resolve_virtual_call(result, recv, recv->klass(), link_info,1634/*check_null_and_abstract=*/true, CHECK);1635break;1636case Bytecodes::_invokeinterface:1637resolve_interface_call(result, recv, recv->klass(), link_info,1638/*check_null_and_abstract=*/true, CHECK);1639break;1640case Bytecodes::_invokestatic:1641resolve_static_call(result, link_info, /*initialize_class=*/false, CHECK);1642break;1643case Bytecodes::_invokespecial:1644resolve_special_call(result, recv, link_info, CHECK);1645break;1646default:1647fatal("bad call: %s", Bytecodes::name(byte));1648break;1649}1650}16511652void LinkResolver::resolve_invokestatic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {1653LinkInfo link_info(pool, index, CHECK);1654resolve_static_call(result, link_info, /*initialize_class*/true, CHECK);1655}165616571658void LinkResolver::resolve_invokespecial(CallInfo& result, Handle recv,1659const constantPoolHandle& pool, int index, TRAPS) {1660LinkInfo link_info(pool, index, CHECK);1661resolve_special_call(result, recv, link_info, CHECK);1662}166316641665void LinkResolver::resolve_invokevirtual(CallInfo& result, Handle recv,1666const constantPoolHandle& pool, int index,1667TRAPS) {16681669LinkInfo link_info(pool, index, CHECK);1670Klass* recvrKlass = recv.is_null() ? (Klass*)NULL : recv->klass();1671resolve_virtual_call(result, recv, recvrKlass, link_info, /*check_null_or_abstract*/true, CHECK);1672}167316741675void LinkResolver::resolve_invokeinterface(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, TRAPS) {1676LinkInfo link_info(pool, index, CHECK);1677Klass* recvrKlass = recv.is_null() ? (Klass*)NULL : recv->klass();1678resolve_interface_call(result, recv, recvrKlass, link_info, true, CHECK);1679}168016811682void LinkResolver::resolve_invokehandle(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {1683// This guy is reached from InterpreterRuntime::resolve_invokehandle.1684LinkInfo link_info(pool, index, CHECK);1685if (log_is_enabled(Info, methodhandles)) {1686ResourceMark rm(THREAD);1687log_info(methodhandles)("resolve_invokehandle %s %s", link_info.name()->as_C_string(),1688link_info.signature()->as_C_string());1689}1690resolve_handle_call(result, link_info, CHECK);1691}16921693void LinkResolver::resolve_handle_call(CallInfo& result,1694const LinkInfo& link_info,1695TRAPS) {1696// JSR 292: this must be an implicitly generated method MethodHandle.invokeExact(*...) or similar1697Klass* resolved_klass = link_info.resolved_klass();1698assert(resolved_klass == vmClasses::MethodHandle_klass() ||1699resolved_klass == vmClasses::VarHandle_klass(), "");1700assert(MethodHandles::is_signature_polymorphic_name(link_info.name()), "");1701Handle resolved_appendix;1702Method* resolved_method = lookup_polymorphic_method(link_info, &resolved_appendix, CHECK);1703result.set_handle(resolved_klass, methodHandle(THREAD, resolved_method), resolved_appendix, CHECK);1704}17051706void LinkResolver::resolve_invokedynamic(CallInfo& result, const constantPoolHandle& pool, int indy_index, TRAPS) {1707ConstantPoolCacheEntry* cpce = pool->invokedynamic_cp_cache_entry_at(indy_index);1708int pool_index = cpce->constant_pool_index();17091710// Resolve the bootstrap specifier (BSM + optional arguments).1711BootstrapInfo bootstrap_specifier(pool, pool_index, indy_index);17121713// Check if CallSite has been bound already or failed already, and short circuit:1714{1715bool is_done = bootstrap_specifier.resolve_previously_linked_invokedynamic(result, CHECK);1716if (is_done) return;1717}17181719// The initial step in Call Site Specifier Resolution is to resolve the symbolic1720// reference to a method handle which will be the bootstrap method for a dynamic1721// call site. If resolution for the java.lang.invoke.MethodHandle for the bootstrap1722// method fails, then a MethodHandleInError is stored at the corresponding bootstrap1723// method's CP index for the CONSTANT_MethodHandle_info. So, there is no need to1724// set the indy_rf flag since any subsequent invokedynamic instruction which shares1725// this bootstrap method will encounter the resolution of MethodHandleInError.17261727resolve_dynamic_call(result, bootstrap_specifier, CHECK);17281729LogTarget(Debug, methodhandles, indy) lt_indy;1730if (lt_indy.is_enabled()) {1731LogStream ls(lt_indy);1732bootstrap_specifier.print_msg_on(&ls, "resolve_invokedynamic");1733}17341735// The returned linkage result is provisional up to the moment1736// the interpreter or runtime performs a serialized check of1737// the relevant CPCE::f1 field. This is done by the caller1738// of this method, via CPCE::set_dynamic_call, which uses1739// an ObjectLocker to do the final serialization of updates1740// to CPCE state, including f1.17411742// Log dynamic info to CDS classlist.1743ArchiveUtils::log_to_classlist(&bootstrap_specifier, CHECK);1744}17451746void LinkResolver::resolve_dynamic_call(CallInfo& result,1747BootstrapInfo& bootstrap_specifier,1748TRAPS) {1749// JSR 292: this must resolve to an implicitly generated method1750// such as MH.linkToCallSite(*...) or some other call-site shape.1751// The appendix argument is likely to be a freshly-created CallSite.1752// It may also be a MethodHandle from an unwrapped ConstantCallSite,1753// or any other reference. The resolved_method as well as the appendix1754// are both recorded together via CallInfo::set_handle.1755SystemDictionary::invoke_bootstrap_method(bootstrap_specifier, THREAD);1756Exceptions::wrap_dynamic_exception(/* is_indy */ true, THREAD);17571758if (HAS_PENDING_EXCEPTION) {1759if (!PENDING_EXCEPTION->is_a(vmClasses::LinkageError_klass())) {1760// Let any random low-level IE or SOE or OOME just bleed through.1761// Basically we pretend that the bootstrap method was never called,1762// if it fails this way: We neither record a successful linkage,1763// nor do we memorize a LE for posterity.1764return;1765}1766// JVMS 5.4.3 says: If an attempt by the Java Virtual Machine to resolve1767// a symbolic reference fails because an error is thrown that is an1768// instance of LinkageError (or a subclass), then subsequent attempts to1769// resolve the reference always fail with the same error that was thrown1770// as a result of the initial resolution attempt.1771bool recorded_res_status = bootstrap_specifier.save_and_throw_indy_exc(CHECK);1772if (!recorded_res_status) {1773// Another thread got here just before we did. So, either use the method1774// that it resolved or throw the LinkageError exception that it threw.1775bool is_done = bootstrap_specifier.resolve_previously_linked_invokedynamic(result, CHECK);1776if (is_done) return;1777}1778assert(bootstrap_specifier.invokedynamic_cp_cache_entry()->indy_resolution_failed(),1779"Resolution failure flag wasn't set");1780}17811782bootstrap_specifier.resolve_newly_linked_invokedynamic(result, CHECK);1783// Exceptions::wrap_dynamic_exception not used because1784// set_handle doesn't throw linkage errors1785}17861787// Selected method is abstract.1788void LinkResolver::throw_abstract_method_error(const methodHandle& resolved_method,1789const methodHandle& selected_method,1790Klass *recv_klass, TRAPS) {1791Klass *resolved_klass = resolved_method->method_holder();1792ResourceMark rm(THREAD);1793stringStream ss;17941795if (recv_klass != NULL) {1796ss.print("Receiver class %s does not define or inherit an "1797"implementation of the",1798recv_klass->external_name());1799} else {1800ss.print("Missing implementation of");1801}18021803assert(resolved_method.not_null(), "Sanity");1804ss.print(" resolved method '%s%s",1805resolved_method->is_abstract() ? "abstract " : "",1806resolved_method->is_private() ? "private " : "");1807resolved_method->signature()->print_as_signature_external_return_type(&ss);1808ss.print(" %s(", resolved_method->name()->as_C_string());1809resolved_method->signature()->print_as_signature_external_parameters(&ss);1810ss.print(")' of %s %s.",1811resolved_klass->external_kind(),1812resolved_klass->external_name());18131814if (selected_method.not_null() && !(resolved_method == selected_method)) {1815ss.print(" Selected method is '%s%s",1816selected_method->is_abstract() ? "abstract " : "",1817selected_method->is_private() ? "private " : "");1818selected_method->print_external_name(&ss);1819ss.print("'.");1820}18211822THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), ss.as_string());1823}182418251826