Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/interpreter/linkResolver.cpp
32285 views
/*1* Copyright (c) 1997, 2018, 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 "classfile/defaultMethods.hpp"26#include "classfile/systemDictionary.hpp"27#include "classfile/vmSymbols.hpp"28#include "compiler/compileBroker.hpp"29#include "gc_interface/collectedHeap.inline.hpp"30#include "interpreter/bytecode.hpp"31#include "interpreter/interpreterRuntime.hpp"32#include "interpreter/linkResolver.hpp"33#include "memory/resourceArea.hpp"34#include "memory/universe.inline.hpp"35#include "oops/instanceKlass.hpp"36#include "oops/objArrayOop.hpp"37#include "prims/methodHandles.hpp"38#include "prims/nativeLookup.hpp"39#include "runtime/compilationPolicy.hpp"40#include "runtime/fieldDescriptor.hpp"41#include "runtime/frame.inline.hpp"42#include "runtime/handles.inline.hpp"43#include "runtime/reflection.hpp"44#include "runtime/signature.hpp"45#include "runtime/thread.inline.hpp"46#include "runtime/vmThread.hpp"474849//------------------------------------------------------------------------------------------------------------------------50// Implementation of CallInfo515253void CallInfo::set_static(KlassHandle resolved_klass, methodHandle resolved_method, TRAPS) {54int vtable_index = Method::nonvirtual_vtable_index;55set_common(resolved_klass, resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK);56}575859void CallInfo::set_interface(KlassHandle resolved_klass, KlassHandle selected_klass, methodHandle resolved_method, methodHandle selected_method, int itable_index, TRAPS) {60// This is only called for interface methods. If the resolved_method61// comes from java/lang/Object, it can be the subject of a virtual call, so62// we should pick the vtable index from the resolved method.63// In that case, the caller must call set_virtual instead of set_interface.64assert(resolved_method->method_holder()->is_interface(), "");65assert(itable_index == resolved_method()->itable_index(), "");66set_common(resolved_klass, selected_klass, resolved_method, selected_method, CallInfo::itable_call, itable_index, CHECK);67}6869void CallInfo::set_virtual(KlassHandle resolved_klass, KlassHandle selected_klass, methodHandle resolved_method, methodHandle selected_method, int vtable_index, TRAPS) {70assert(vtable_index >= 0 || vtable_index == Method::nonvirtual_vtable_index, "valid index");71assert(vtable_index < 0 || !resolved_method->has_vtable_index() || vtable_index == resolved_method->vtable_index(), "");72CallKind kind = (vtable_index >= 0 && !resolved_method->can_be_statically_bound() ? CallInfo::vtable_call : CallInfo::direct_call);73set_common(resolved_klass, selected_klass, resolved_method, selected_method, kind, vtable_index, CHECK);74assert(!resolved_method->is_compiled_lambda_form(), "these must be handled via an invokehandle call");75}7677void CallInfo::set_handle(methodHandle resolved_method, Handle resolved_appendix, Handle resolved_method_type, TRAPS) {78if (resolved_method.is_null()) {79THROW_MSG(vmSymbols::java_lang_InternalError(), "resolved method is null");80}81KlassHandle resolved_klass = SystemDictionary::MethodHandle_klass();82assert(resolved_method->intrinsic_id() == vmIntrinsics::_invokeBasic ||83resolved_method->is_compiled_lambda_form(),84"linkMethod must return one of these");85int vtable_index = Method::nonvirtual_vtable_index;86assert(!resolved_method->has_vtable_index(), "");87set_common(resolved_klass, resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK);88_resolved_appendix = resolved_appendix;89_resolved_method_type = resolved_method_type;90}9192void CallInfo::set_common(KlassHandle resolved_klass,93KlassHandle selected_klass,94methodHandle resolved_method,95methodHandle selected_method,96CallKind kind,97int index,98TRAPS) {99assert(resolved_method->signature() == selected_method->signature(), "signatures must correspond");100_resolved_klass = resolved_klass;101_selected_klass = selected_klass;102_resolved_method = resolved_method;103_selected_method = selected_method;104_call_kind = kind;105_call_index = index;106_resolved_appendix = Handle();107DEBUG_ONLY(verify()); // verify before making side effects108109if (CompilationPolicy::must_be_compiled(selected_method)) {110// This path is unusual, mostly used by the '-Xcomp' stress test mode.111112// Note: with several active threads, the must_be_compiled may be true113// while can_be_compiled is false; remove assert114// assert(CompilationPolicy::can_be_compiled(selected_method), "cannot compile");115if (THREAD->is_Compiler_thread()) {116// don't force compilation, resolve was on behalf of compiler117return;118}119if (selected_method->method_holder()->is_not_initialized()) {120// 'is_not_initialized' means not only '!is_initialized', but also that121// initialization has not been started yet ('!being_initialized')122// Do not force compilation of methods in uninitialized classes.123// Note that doing this would throw an assert later,124// in CompileBroker::compile_method.125// We sometimes use the link resolver to do reflective lookups126// even before classes are initialized.127return;128}129CompileBroker::compile_method(selected_method, InvocationEntryBci,130CompilationPolicy::policy()->initial_compile_level(),131methodHandle(), 0, "must_be_compiled", CHECK);132}133}134135// utility query for unreflecting a method136CallInfo::CallInfo(Method* resolved_method, Klass* resolved_klass) {137Klass* resolved_method_holder = resolved_method->method_holder();138if (resolved_klass == NULL) { // 2nd argument defaults to holder of 1st139resolved_klass = resolved_method_holder;140}141_resolved_klass = resolved_klass;142_selected_klass = resolved_klass;143_resolved_method = resolved_method;144_selected_method = resolved_method;145// classify:146CallKind kind = CallInfo::unknown_kind;147int index = resolved_method->vtable_index();148if (resolved_method->can_be_statically_bound()) {149kind = CallInfo::direct_call;150} else if (!resolved_method_holder->is_interface()) {151// Could be an Object method inherited into an interface, but still a vtable call.152kind = CallInfo::vtable_call;153} else if (!resolved_klass->is_interface()) {154// A default or miranda method. Compute the vtable index.155ResourceMark rm;156klassVtable* vt = InstanceKlass::cast(resolved_klass)->vtable();157index = LinkResolver::vtable_index_of_interface_method(resolved_klass,158resolved_method);159assert(index >= 0 , "we should have valid vtable index at this point");160161kind = CallInfo::vtable_call;162} else if (resolved_method->has_vtable_index()) {163// Can occur if an interface redeclares a method of Object.164165#ifdef ASSERT166// Ensure that this is really the case.167KlassHandle object_klass = SystemDictionary::Object_klass();168Method * object_resolved_method = object_klass()->vtable()->method_at(index);169assert(object_resolved_method->name() == resolved_method->name(),170err_msg("Object and interface method names should match at vtable index %d, %s != %s",171index, object_resolved_method->name()->as_C_string(), resolved_method->name()->as_C_string()));172assert(object_resolved_method->signature() == resolved_method->signature(),173err_msg("Object and interface method signatures should match at vtable index %d, %s != %s",174index, object_resolved_method->signature()->as_C_string(), resolved_method->signature()->as_C_string()));175#endif // ASSERT176177kind = CallInfo::vtable_call;178} else {179// A regular interface call.180kind = CallInfo::itable_call;181index = resolved_method->itable_index();182}183assert(index == Method::nonvirtual_vtable_index || index >= 0, err_msg("bad index %d", index));184_call_kind = kind;185_call_index = index;186_resolved_appendix = Handle();187DEBUG_ONLY(verify());188}189190#ifdef ASSERT191void CallInfo::verify() {192switch (call_kind()) { // the meaning and allowed value of index depends on kind193case CallInfo::direct_call:194if (_call_index == Method::nonvirtual_vtable_index) break;195// else fall through to check vtable index:196case CallInfo::vtable_call:197assert(resolved_klass()->verify_vtable_index(_call_index), "");198break;199case CallInfo::itable_call:200assert(resolved_method()->method_holder()->verify_itable_index(_call_index), "");201break;202case CallInfo::unknown_kind:203assert(call_kind() != CallInfo::unknown_kind, "CallInfo must be set");204break;205default:206fatal(err_msg_res("Unexpected call kind %d", call_kind()));207}208}209#endif //ASSERT210211212213//------------------------------------------------------------------------------------------------------------------------214// Klass resolution215216void LinkResolver::check_klass_accessability(KlassHandle ref_klass, KlassHandle sel_klass, TRAPS) {217if (!Reflection::verify_class_access(ref_klass(),218sel_klass(),219true)) {220ResourceMark rm(THREAD);221Exceptions::fthrow(222THREAD_AND_LOCATION,223vmSymbols::java_lang_IllegalAccessError(),224"tried to access class %s from class %s",225sel_klass->external_name(),226ref_klass->external_name()227);228return;229}230}231232void LinkResolver::resolve_klass(KlassHandle& result, constantPoolHandle pool, int index, TRAPS) {233Klass* result_oop = pool->klass_ref_at(index, CHECK);234result = KlassHandle(THREAD, result_oop);235}236237//------------------------------------------------------------------------------------------------------------------------238// Method resolution239//240// According to JVM spec. $5.4.3c & $5.4.3d241242// Look up method in klasses, including static methods243// Then look up local default methods244void LinkResolver::lookup_method_in_klasses(methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, bool checkpolymorphism, bool in_imethod_resolve, TRAPS) {245// Ignore overpasses so statics can be found during resolution246Method* result_oop = klass->uncached_lookup_method(name, signature, Klass::skip_overpass);247248if (klass->oop_is_array()) {249// Only consider klass and super klass for arrays250result = methodHandle(THREAD, result_oop);251return;252}253254// JDK 8, JVMS 5.4.3.4: Interface method resolution should255// ignore static and non-public methods of java.lang.Object,256// like clone, finalize, registerNatives.257if (in_imethod_resolve &&258result_oop != NULL &&259klass->is_interface() &&260(result_oop->is_static() || !result_oop->is_public()) &&261result_oop->method_holder() == SystemDictionary::Object_klass()) {262result_oop = NULL;263}264265// Before considering default methods, check for an overpass in the266// current class if a method has not been found.267if (result_oop == NULL) {268result_oop = InstanceKlass::cast(klass())->find_method(name, signature);269}270271if (result_oop == NULL) {272Array<Method*>* default_methods = InstanceKlass::cast(klass())->default_methods();273if (default_methods != NULL) {274result_oop = InstanceKlass::find_method(default_methods, name, signature);275}276}277278if (checkpolymorphism && EnableInvokeDynamic && result_oop != NULL) {279vmIntrinsics::ID iid = result_oop->intrinsic_id();280if (MethodHandles::is_signature_polymorphic(iid)) {281// Do not link directly to these. The VM must produce a synthetic one using lookup_polymorphic_method.282return;283}284}285result = methodHandle(THREAD, result_oop);286}287288// returns first instance method289// Looks up method in classes, then looks up local default methods290void LinkResolver::lookup_instance_method_in_klasses(methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS) {291Method* result_oop = klass->uncached_lookup_method(name, signature, Klass::find_overpass);292result = methodHandle(THREAD, result_oop);293while (!result.is_null() && result->is_static() && result->method_holder()->super() != NULL) {294KlassHandle super_klass = KlassHandle(THREAD, result->method_holder()->super());295result = methodHandle(THREAD, super_klass->uncached_lookup_method(name, signature, Klass::find_overpass));296}297298if (klass->oop_is_array()) {299// Only consider klass and super klass for arrays300return;301}302303if (result.is_null()) {304Array<Method*>* default_methods = InstanceKlass::cast(klass())->default_methods();305if (default_methods != NULL) {306result = methodHandle(InstanceKlass::find_method(default_methods, name, signature));307assert(result.is_null() || !result->is_static(), "static defaults not allowed");308}309}310}311312int LinkResolver::vtable_index_of_interface_method(KlassHandle klass,313methodHandle resolved_method) {314315int vtable_index = Method::invalid_vtable_index;316Symbol* name = resolved_method->name();317Symbol* signature = resolved_method->signature();318319// First check in default method array320if (!resolved_method->is_abstract() &&321(InstanceKlass::cast(klass())->default_methods() != NULL)) {322int index = InstanceKlass::find_method_index(InstanceKlass::cast(klass())->default_methods(),323name, signature, Klass::find_overpass,324Klass::find_static, Klass::find_private);325if (index >= 0 ) {326vtable_index = InstanceKlass::cast(klass())->default_vtable_indices()->at(index);327}328}329if (vtable_index == Method::invalid_vtable_index) {330// get vtable_index for miranda methods331ResourceMark rm;332klassVtable *vt = InstanceKlass::cast(klass())->vtable();333vtable_index = vt->index_of_miranda(name, signature);334}335return vtable_index;336}337338void LinkResolver::lookup_method_in_interfaces(methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS) {339InstanceKlass *ik = InstanceKlass::cast(klass());340341// Specify 'true' in order to skip default methods when searching the342// interfaces. Function lookup_method_in_klasses() already looked for343// the method in the default methods table.344result = methodHandle(THREAD, ik->lookup_method_in_all_interfaces(name, signature, Klass::skip_defaults));345}346347void LinkResolver::lookup_polymorphic_method(methodHandle& result,348KlassHandle klass, Symbol* name, Symbol* full_signature,349KlassHandle current_klass,350Handle *appendix_result_or_null,351Handle *method_type_result,352TRAPS) {353vmIntrinsics::ID iid = MethodHandles::signature_polymorphic_name_id(name);354if (TraceMethodHandles) {355ResourceMark rm(THREAD);356tty->print_cr("lookup_polymorphic_method iid=%s %s.%s%s",357vmIntrinsics::name_at(iid), klass->external_name(),358name->as_C_string(), full_signature->as_C_string());359}360if (EnableInvokeDynamic &&361klass() == SystemDictionary::MethodHandle_klass() &&362iid != vmIntrinsics::_none) {363if (MethodHandles::is_signature_polymorphic_intrinsic(iid)) {364// Most of these do not need an up-call to Java to resolve, so can be done anywhere.365// Do not erase last argument type (MemberName) if it is a static linkTo method.366bool keep_last_arg = MethodHandles::is_signature_polymorphic_static(iid);367TempNewSymbol basic_signature =368MethodHandles::lookup_basic_type_signature(full_signature, keep_last_arg, CHECK);369if (TraceMethodHandles) {370ResourceMark rm(THREAD);371tty->print_cr("lookup_polymorphic_method %s %s => basic %s",372name->as_C_string(),373full_signature->as_C_string(),374basic_signature->as_C_string());375}376result = SystemDictionary::find_method_handle_intrinsic(iid,377basic_signature,378CHECK);379if (result.not_null()) {380assert(result->is_method_handle_intrinsic(), "MH.invokeBasic or MH.linkTo* intrinsic");381assert(result->intrinsic_id() != vmIntrinsics::_invokeGeneric, "wrong place to find this");382assert(basic_signature == result->signature(), "predict the result signature");383if (TraceMethodHandles) {384tty->print("lookup_polymorphic_method => intrinsic ");385result->print_on(tty);386}387return;388}389} else if (iid == vmIntrinsics::_invokeGeneric390&& !THREAD->is_Compiler_thread()391&& appendix_result_or_null != NULL) {392// This is a method with type-checking semantics.393// We will ask Java code to spin an adapter method for it.394if (!MethodHandles::enabled()) {395// Make sure the Java part of the runtime has been booted up.396Klass* natives = SystemDictionary::MethodHandleNatives_klass();397if (natives == NULL || InstanceKlass::cast(natives)->is_not_initialized()) {398SystemDictionary::resolve_or_fail(vmSymbols::java_lang_invoke_MethodHandleNatives(),399Handle(),400Handle(),401true,402CHECK);403}404}405406Handle appendix;407Handle method_type;408result = SystemDictionary::find_method_handle_invoker(name,409full_signature,410current_klass,411&appendix,412&method_type,413CHECK);414if (TraceMethodHandles) {415tty->print("lookup_polymorphic_method => (via Java) ");416result->print_on(tty);417tty->print(" lookup_polymorphic_method => appendix = ");418if (appendix.is_null()) tty->print_cr("(none)");419else appendix->print_on(tty);420}421if (result.not_null()) {422#ifdef ASSERT423ResourceMark rm(THREAD);424425TempNewSymbol basic_signature =426MethodHandles::lookup_basic_type_signature(full_signature, CHECK);427int actual_size_of_params = result->size_of_parameters();428int expected_size_of_params = ArgumentSizeComputer(basic_signature).size();429// +1 for MethodHandle.this, +1 for trailing MethodType430if (!MethodHandles::is_signature_polymorphic_static(iid)) expected_size_of_params += 1;431if (appendix.not_null()) expected_size_of_params += 1;432if (actual_size_of_params != expected_size_of_params) {433tty->print_cr("*** basic_signature=%s", basic_signature->as_C_string());434tty->print_cr("*** result for %s: ", vmIntrinsics::name_at(iid));435result->print();436}437assert(actual_size_of_params == expected_size_of_params,438err_msg("%d != %d", actual_size_of_params, expected_size_of_params));439#endif //ASSERT440441assert(appendix_result_or_null != NULL, "");442(*appendix_result_or_null) = appendix;443(*method_type_result) = method_type;444return;445}446}447}448}449450void LinkResolver::check_method_accessability(KlassHandle ref_klass,451KlassHandle resolved_klass,452KlassHandle sel_klass,453methodHandle sel_method,454TRAPS) {455456AccessFlags flags = sel_method->access_flags();457458// Special case: arrays always override "clone". JVMS 2.15.459// If the resolved klass is an array class, and the declaring class460// is java.lang.Object and the method is "clone", set the flags461// to public.462//463// We'll check for the method name first, as that's most likely464// to be false (so we'll short-circuit out of these tests).465if (sel_method->name() == vmSymbols::clone_name() &&466sel_klass() == SystemDictionary::Object_klass() &&467resolved_klass->oop_is_array()) {468// We need to change "protected" to "public".469assert(flags.is_protected(), "clone not protected?");470jint new_flags = flags.as_int();471new_flags = new_flags & (~JVM_ACC_PROTECTED);472new_flags = new_flags | JVM_ACC_PUBLIC;473flags.set_flags(new_flags);474}475// assert(extra_arg_result_or_null != NULL, "must be able to return extra argument");476477if (!Reflection::verify_field_access(ref_klass(),478resolved_klass(),479sel_klass(),480flags,481true)) {482ResourceMark rm(THREAD);483Exceptions::fthrow(484THREAD_AND_LOCATION,485vmSymbols::java_lang_IllegalAccessError(),486"tried to access method %s.%s%s from class %s",487sel_klass->external_name(),488sel_method->name()->as_C_string(),489sel_method->signature()->as_C_string(),490ref_klass->external_name()491);492return;493}494}495496void LinkResolver::resolve_method_statically(methodHandle& resolved_method, KlassHandle& resolved_klass,497Bytecodes::Code code, constantPoolHandle pool, int index, TRAPS) {498// This method is used only499// (1) in C2 from InlineTree::ok_to_inline (via ciMethod::check_call),500// and501// (2) in Bytecode_invoke::static_target502// It appears to fail when applied to an invokeinterface call site.503// FIXME: Remove this method and ciMethod::check_call; refactor to use the other LinkResolver entry points.504// resolve klass505if (code == Bytecodes::_invokedynamic) {506resolved_klass = SystemDictionary::MethodHandle_klass();507Symbol* method_name = vmSymbols::invoke_name();508Symbol* method_signature = pool->signature_ref_at(index);509KlassHandle current_klass(THREAD, pool->pool_holder());510resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, false, CHECK);511return;512}513514resolve_klass(resolved_klass, pool, index, CHECK);515516Symbol* method_name = pool->name_ref_at(index);517Symbol* method_signature = pool->signature_ref_at(index);518KlassHandle current_klass(THREAD, pool->pool_holder());519520if (pool->has_preresolution()521|| (resolved_klass() == SystemDictionary::MethodHandle_klass() &&522MethodHandles::is_signature_polymorphic_name(resolved_klass(), method_name))) {523Method* result_oop = ConstantPool::method_at_if_loaded(pool, index);524if (result_oop != NULL) {525resolved_method = methodHandle(THREAD, result_oop);526return;527}528}529530if (code == Bytecodes::_invokeinterface) {531resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK);532} else if (code == Bytecodes::_invokevirtual) {533resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK);534} else if (!resolved_klass->is_interface()) {535resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, false, CHECK);536} else {537bool nostatics = (code == Bytecodes::_invokestatic) ? false : true;538resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, nostatics, CHECK);539}540}541542void LinkResolver::check_method_loader_constraints(methodHandle& resolved_method,543KlassHandle resolved_klass,544Symbol* method_name,545Symbol* method_signature,546KlassHandle current_klass,547const char* method_type, TRAPS) {548Handle loader (THREAD, InstanceKlass::cast(current_klass())->class_loader());549Handle class_loader (THREAD, resolved_method->method_holder()->class_loader());550{551ResourceMark rm(THREAD);552Symbol* failed_type_symbol =553SystemDictionary::check_signature_loaders(method_signature, loader,554class_loader, true, CHECK);555if (failed_type_symbol != NULL) {556const char* msg = "loader constraint violation: when resolving %s"557" \"%s\" the class loader (instance of %s) of the current class, %s,"558" and the class loader (instance of %s) for the method's defining class, %s, have"559" different Class objects for the type %s used in the signature";560char* sig = Method::name_and_sig_as_C_string(resolved_klass(), method_name, method_signature);561const char* loader1 = SystemDictionary::loader_name(loader());562char* current = InstanceKlass::cast(current_klass())->name()->as_C_string();563const char* loader2 = SystemDictionary::loader_name(class_loader());564char* target = InstanceKlass::cast(resolved_method->method_holder())565->name()->as_C_string();566char* failed_type_name = failed_type_symbol->as_C_string();567size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +568strlen(current) + strlen(loader2) + strlen(target) +569strlen(failed_type_name) + strlen(method_type) + 1;570char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);571jio_snprintf(buf, buflen, msg, method_type, sig, loader1, current, loader2,572target, failed_type_name);573THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);574}575}576}577578void LinkResolver::resolve_method(methodHandle& resolved_method, KlassHandle resolved_klass,579Symbol* method_name, Symbol* method_signature,580KlassHandle current_klass, bool check_access,581bool require_methodref, TRAPS) {582583Handle nested_exception;584585// 1. check if methodref required, that resolved_klass is not interfacemethodref586if (require_methodref && resolved_klass->is_interface()) {587ResourceMark rm(THREAD);588char buf[200];589jio_snprintf(buf, sizeof(buf), "Found interface %s, but class was expected",590resolved_klass()->external_name());591THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);592}593594// 2. lookup method in resolved klass and its super klasses595lookup_method_in_klasses(resolved_method, resolved_klass, method_name, method_signature, true, false, CHECK);596597if (resolved_method.is_null() && !resolved_klass->oop_is_array()) { // not found in the class hierarchy598// 3. lookup method in all the interfaces implemented by the resolved klass599lookup_method_in_interfaces(resolved_method, resolved_klass, method_name, method_signature, CHECK);600601if (resolved_method.is_null()) {602// JSR 292: see if this is an implicitly generated method MethodHandle.linkToVirtual(*...), etc603lookup_polymorphic_method(resolved_method, resolved_klass, method_name, method_signature,604current_klass, (Handle*)NULL, (Handle*)NULL, THREAD);605if (HAS_PENDING_EXCEPTION) {606nested_exception = Handle(THREAD, PENDING_EXCEPTION);607CLEAR_PENDING_EXCEPTION;608}609}610}611612if (resolved_method.is_null()) {613// 4. method lookup failed614ResourceMark rm(THREAD);615THROW_MSG_CAUSE(vmSymbols::java_lang_NoSuchMethodError(),616Method::name_and_sig_as_C_string(resolved_klass(),617method_name,618method_signature),619nested_exception);620}621622// 5. access checks, access checking may be turned off when calling from within the VM.623if (check_access) {624assert(current_klass.not_null() , "current_klass should not be null");625626// check if method can be accessed by the referring class627check_method_accessability(current_klass,628resolved_klass,629KlassHandle(THREAD, resolved_method->method_holder()),630resolved_method,631CHECK);632633// check loader constraints634check_method_loader_constraints(resolved_method, resolved_klass, method_name,635method_signature, current_klass, "method", CHECK);636}637}638639void LinkResolver::resolve_interface_method(methodHandle& resolved_method,640KlassHandle resolved_klass,641Symbol* method_name,642Symbol* method_signature,643KlassHandle current_klass,644bool check_access,645bool nostatics, TRAPS) {646647// check if klass is interface648if (!resolved_klass->is_interface()) {649ResourceMark rm(THREAD);650char buf[200];651jio_snprintf(buf, sizeof(buf), "Found class %s, but interface was expected", resolved_klass()->external_name());652THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);653}654655// lookup method in this interface or its super, java.lang.Object656// JDK8: also look for static methods657lookup_method_in_klasses(resolved_method, resolved_klass, method_name, method_signature, false, true, CHECK);658659if (resolved_method.is_null() && !resolved_klass->oop_is_array()) {660// lookup method in all the super-interfaces661lookup_method_in_interfaces(resolved_method, resolved_klass, method_name, method_signature, CHECK);662}663664if (resolved_method.is_null()) {665// no method found666ResourceMark rm(THREAD);667THROW_MSG(vmSymbols::java_lang_NoSuchMethodError(),668Method::name_and_sig_as_C_string(resolved_klass(),669method_name,670method_signature));671}672673if (check_access) {674// JDK8 adds non-public interface methods, and accessability check requirement675assert(current_klass.not_null() , "current_klass should not be null");676677// check if method can be accessed by the referring class678check_method_accessability(current_klass,679resolved_klass,680KlassHandle(THREAD, resolved_method->method_holder()),681resolved_method,682CHECK);683684check_method_loader_constraints(resolved_method, resolved_klass, method_name,685method_signature, current_klass, "interface method", CHECK);686}687688if (nostatics && resolved_method->is_static()) {689ResourceMark rm(THREAD);690char buf[200];691jio_snprintf(buf, sizeof(buf), "Expected instance not static method %s",692Method::name_and_sig_as_C_string(resolved_klass(),693resolved_method->name(), resolved_method->signature()));694THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);695}696697if (TraceItables && Verbose) {698ResourceMark rm(THREAD);699tty->print("invokeinterface resolved method: caller-class:%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ",700(current_klass.is_null() ? "<NULL>" : current_klass->internal_name()),701(resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),702Method::name_and_sig_as_C_string(resolved_klass(),703resolved_method->name(),704resolved_method->signature()),705resolved_method->method_holder()->internal_name()706);707resolved_method->access_flags().print_on(tty);708if (resolved_method->is_default_method()) {709tty->print("default ");710}711if (resolved_method->is_overpass()) {712tty->print("overpass");713}714tty->cr();715}716}717718//------------------------------------------------------------------------------------------------------------------------719// Field resolution720721void LinkResolver::check_field_accessability(KlassHandle ref_klass,722KlassHandle resolved_klass,723KlassHandle sel_klass,724fieldDescriptor& fd,725TRAPS) {726if (!Reflection::verify_field_access(ref_klass(),727resolved_klass(),728sel_klass(),729fd.access_flags(),730true)) {731ResourceMark rm(THREAD);732Exceptions::fthrow(733THREAD_AND_LOCATION,734vmSymbols::java_lang_IllegalAccessError(),735"tried to access field %s.%s from class %s",736sel_klass->external_name(),737fd.name()->as_C_string(),738ref_klass->external_name()739);740return;741}742}743744void LinkResolver::resolve_field_access(fieldDescriptor& result, constantPoolHandle pool, int index, Bytecodes::Code byte, TRAPS) {745// Load these early in case the resolve of the containing klass fails746Symbol* field = pool->name_ref_at(index);747Symbol* sig = pool->signature_ref_at(index);748749// resolve specified klass750KlassHandle resolved_klass;751resolve_klass(resolved_klass, pool, index, CHECK);752753KlassHandle current_klass(THREAD, pool->pool_holder());754resolve_field(result, resolved_klass, field, sig, current_klass, byte, true, true, CHECK);755}756757void LinkResolver::resolve_field(fieldDescriptor& fd, KlassHandle resolved_klass, Symbol* field, Symbol* sig,758KlassHandle current_klass, Bytecodes::Code byte, bool check_access, bool initialize_class,759TRAPS) {760assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic ||761byte == Bytecodes::_getfield || byte == Bytecodes::_putfield ||762(byte == Bytecodes::_nop && !check_access), "bad field access bytecode");763764bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic);765bool is_put = (byte == Bytecodes::_putfield || byte == Bytecodes::_putstatic);766767// Check if there's a resolved klass containing the field768if (resolved_klass.is_null()) {769ResourceMark rm(THREAD);770THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());771}772773// Resolve instance field774KlassHandle sel_klass(THREAD, resolved_klass->find_field(field, sig, &fd));775// check if field exists; i.e., if a klass containing the field def has been selected776if (sel_klass.is_null()) {777ResourceMark rm(THREAD);778THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());779}780781// Access checking may be turned off when calling from within the VM.782if (check_access) {783784// check access785check_field_accessability(current_klass, resolved_klass, sel_klass, fd, CHECK);786787// check for errors788if (is_static != fd.is_static()) {789ResourceMark rm(THREAD);790char msg[200];791jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", resolved_klass()->external_name(), fd.name()->as_C_string());792THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg);793}794795// Final fields can only be accessed from its own class.796if (is_put && fd.access_flags().is_final() && sel_klass() != current_klass()) {797THROW(vmSymbols::java_lang_IllegalAccessError());798}799800// initialize resolved_klass if necessary801// note 1: the klass which declared the field must be initialized (i.e, sel_klass)802// according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99)803//804// note 2: we don't want to force initialization if we are just checking805// if the field access is legal; e.g., during compilation806if (is_static && initialize_class) {807sel_klass->initialize(CHECK);808}809}810811if (sel_klass() != current_klass() && !current_klass.is_null()) {812HandleMark hm(THREAD);813Handle ref_loader (THREAD, InstanceKlass::cast(current_klass())->class_loader());814Handle sel_loader (THREAD, InstanceKlass::cast(sel_klass())->class_loader());815{816ResourceMark rm(THREAD);817Symbol* failed_type_symbol =818SystemDictionary::check_signature_loaders(sig,819ref_loader, sel_loader,820false,821CHECK);822if (failed_type_symbol != NULL) {823const char* msg = "loader constraint violation: when resolving field"824" \"%s\" the class loader (instance of %s) of the referring class, "825"%s, and the class loader (instance of %s) for the field's resolved "826"type, %s, have different Class objects for that type";827char* field_name = field->as_C_string();828const char* loader1 = SystemDictionary::loader_name(ref_loader());829char* sel = InstanceKlass::cast(sel_klass())->name()->as_C_string();830const char* loader2 = SystemDictionary::loader_name(sel_loader());831char* failed_type_name = failed_type_symbol->as_C_string();832size_t buflen = strlen(msg) + strlen(field_name) + strlen(loader1) +833strlen(sel) + strlen(loader2) + strlen(failed_type_name) + 1;834char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);835jio_snprintf(buf, buflen, msg, field_name, loader1, sel, loader2,836failed_type_name);837THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);838}839}840}841842// return information. note that the klass is set to the actual klass containing the843// field, otherwise access of static fields in superclasses will not work.844}845846847//------------------------------------------------------------------------------------------------------------------------848// Invoke resolution849//850// Naming conventions:851//852// resolved_method the specified method (i.e., static receiver specified via constant pool index)853// sel_method the selected method (selected via run-time lookup; e.g., based on dynamic receiver class)854// resolved_klass the specified klass (i.e., specified via constant pool index)855// recv_klass the receiver klass856857858void LinkResolver::resolve_static_call(CallInfo& result, KlassHandle& resolved_klass, Symbol* method_name,859Symbol* method_signature, KlassHandle current_klass,860bool check_access, bool initialize_class, TRAPS) {861methodHandle resolved_method;862linktime_resolve_static_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);863resolved_klass = KlassHandle(THREAD, resolved_method->method_holder());864865// Initialize klass (this should only happen if everything is ok)866if (initialize_class && resolved_klass->should_be_initialized()) {867resolved_klass->initialize(CHECK);868linktime_resolve_static_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);869}870871// setup result872result.set_static(resolved_klass, resolved_method, CHECK);873}874875// throws linktime exceptions876void LinkResolver::linktime_resolve_static_method(methodHandle& resolved_method, KlassHandle resolved_klass,877Symbol* method_name, Symbol* method_signature,878KlassHandle current_klass, bool check_access, TRAPS) {879880if (!resolved_klass->is_interface()) {881resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, false, CHECK);882} else {883resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, false, CHECK);884}885assert(resolved_method->name() != vmSymbols::class_initializer_name(), "should have been checked in verifier");886887// check if static888if (!resolved_method->is_static()) {889ResourceMark rm(THREAD);890char buf[200];891jio_snprintf(buf, sizeof(buf), "Expected static method %s", Method::name_and_sig_as_C_string(resolved_klass(),892resolved_method->name(),893resolved_method->signature()));894THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);895}896}897898899void LinkResolver::resolve_special_call(CallInfo& result, Handle recv, KlassHandle resolved_klass, Symbol* method_name,900Symbol* method_signature, KlassHandle current_klass, bool check_access, TRAPS) {901methodHandle resolved_method;902linktime_resolve_special_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);903runtime_resolve_special_method(result, resolved_method, resolved_klass, current_klass, recv, check_access, CHECK);904}905906// throws linktime exceptions907void LinkResolver::linktime_resolve_special_method(methodHandle& resolved_method, KlassHandle resolved_klass,908Symbol* method_name, Symbol* method_signature,909KlassHandle current_klass, bool check_access, TRAPS) {910911// Invokespecial is called for multiple special reasons:912// <init>913// local private method invocation, for classes and interfaces914// superclass.method, which can also resolve to a default method915// and the selected method is recalculated relative to the direct superclass916// superinterface.method, which explicitly does not check shadowing917918if (!resolved_klass->is_interface()) {919resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, false, CHECK);920} else {921resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, true, CHECK);922}923924// check if method name is <init>, that it is found in same klass as static type925if (resolved_method->name() == vmSymbols::object_initializer_name() &&926resolved_method->method_holder() != resolved_klass()) {927ResourceMark rm(THREAD);928Exceptions::fthrow(929THREAD_AND_LOCATION,930vmSymbols::java_lang_NoSuchMethodError(),931"%s: method %s%s not found",932resolved_klass->external_name(),933resolved_method->name()->as_C_string(),934resolved_method->signature()->as_C_string()935);936return;937}938939// check if invokespecial's interface method reference is in an indirect superinterface940if (!current_klass.is_null() && resolved_klass->is_interface()) {941Klass *klass_to_check = !InstanceKlass::cast(current_klass())->is_anonymous() ?942current_klass() :943InstanceKlass::cast(current_klass())->host_klass();944// As of the fix for 4486457 we disable verification for all of the945// dynamically-generated bytecodes associated with the 1.4946// reflection implementation, not just those associated with947// sun/reflect/SerializationConstructorAccessor.948bool is_reflect = JDK_Version::is_gte_jdk14x_version() &&949UseNewReflection &&950klass_to_check->is_subclass_of(951SystemDictionary::reflect_MagicAccessorImpl_klass());952953if (!is_reflect &&954!InstanceKlass::cast(klass_to_check)->is_same_or_direct_interface(resolved_klass())) {955ResourceMark rm(THREAD);956char buf[200];957jio_snprintf(buf, sizeof(buf),958"Interface method reference: %s, is in an indirect superinterface of %s",959Method::name_and_sig_as_C_string(resolved_klass(),960resolved_method->name(),961resolved_method->signature()),962current_klass->external_name());963THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);964}965}966967// check if not static968if (resolved_method->is_static()) {969ResourceMark rm(THREAD);970char buf[200];971jio_snprintf(buf, sizeof(buf),972"Expecting non-static method %s",973Method::name_and_sig_as_C_string(resolved_klass(),974resolved_method->name(),975resolved_method->signature()));976THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);977}978979if (TraceItables && Verbose) {980ResourceMark rm(THREAD);981tty->print("invokespecial resolved method: caller-class:%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ",982(current_klass.is_null() ? "<NULL>" : current_klass->internal_name()),983(resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),984Method::name_and_sig_as_C_string(resolved_klass(),985resolved_method->name(),986resolved_method->signature()),987resolved_method->method_holder()->internal_name()988);989resolved_method->access_flags().print_on(tty);990if (resolved_method->is_default_method()) {991tty->print("default ");992}993if (resolved_method->is_overpass()) {994tty->print("overpass");995}996tty->cr();997}998}9991000// throws runtime exceptions1001void LinkResolver::runtime_resolve_special_method(CallInfo& result, methodHandle resolved_method, KlassHandle resolved_klass,1002KlassHandle current_klass, Handle recv, bool check_access, TRAPS) {10031004// resolved method is selected method unless we have an old-style lookup1005// for a superclass method1006// Invokespecial for a superinterface, resolved method is selected method,1007// no checks for shadowing1008methodHandle sel_method(THREAD, resolved_method());10091010if (check_access &&1011// check if the method is not <init>1012resolved_method->name() != vmSymbols::object_initializer_name()) {10131014// check if this is an old-style super call and do a new lookup if so1015// a) check if ACC_SUPER flag is set for the current class1016if ((current_klass->is_super() || !AllowNonVirtualCalls) &&1017// b) check if the class of the resolved_klass is a superclass1018// (not supertype in order to exclude interface classes) of the current class.1019// This check is not performed for super.invoke for interface methods1020// in super interfaces.1021current_klass->is_subclass_of(resolved_klass()) &&1022current_klass() != resolved_klass()) {1023// Lookup super method1024KlassHandle super_klass(THREAD, current_klass->super());1025lookup_instance_method_in_klasses(sel_method, super_klass,1026resolved_method->name(),1027resolved_method->signature(), CHECK);1028// check if found1029if (sel_method.is_null()) {1030ResourceMark rm(THREAD);1031THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),1032Method::name_and_sig_as_C_string(resolved_klass(),1033resolved_method->name(),1034resolved_method->signature()));1035} else if (sel_method() != resolved_method()) {1036check_method_loader_constraints(sel_method, resolved_klass,1037sel_method->name(), sel_method->signature(),1038current_klass, "method", CHECK);1039}1040}10411042// Check that the class of objectref (the receiver) is the current class or interface,1043// or a subtype of the current class or interface (the sender), otherwise invokespecial1044// throws IllegalAccessError.1045// The verifier checks that the sender is a subtype of the class in the I/MR operand.1046// The verifier also checks that the receiver is a subtype of the sender, if the sender is1047// a class. If the sender is an interface, the check has to be performed at runtime.1048InstanceKlass* sender = InstanceKlass::cast(current_klass());1049sender = sender->is_anonymous() ? InstanceKlass::cast(sender->host_klass()) : sender;1050if (sender->is_interface() && recv.not_null()) {1051Klass* receiver_klass = recv->klass();1052if (!receiver_klass->is_subtype_of(sender)) {1053ResourceMark rm(THREAD);1054char buf[500];1055jio_snprintf(buf, sizeof(buf),1056"Receiver class %s must be the current class or a subtype of interface %s",1057receiver_klass->name()->as_C_string(),1058sender->name()->as_C_string());1059THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), buf);1060}1061}1062}10631064// check if not static1065if (sel_method->is_static()) {1066ResourceMark rm(THREAD);1067char buf[200];1068jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass(),1069resolved_method->name(),1070resolved_method->signature()));1071THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);1072}10731074// check if abstract1075if (sel_method->is_abstract()) {1076ResourceMark rm(THREAD);1077THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),1078Method::name_and_sig_as_C_string(resolved_klass(),1079sel_method->name(),1080sel_method->signature()));1081}10821083if (TraceItables && Verbose) {1084ResourceMark rm(THREAD);1085tty->print("invokespecial selected method: resolved-class:%s, method:%s, method_holder:%s, access_flags: ",1086(resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),1087Method::name_and_sig_as_C_string(resolved_klass(),1088sel_method->name(),1089sel_method->signature()),1090sel_method->method_holder()->internal_name()1091);1092sel_method->access_flags().print_on(tty);1093if (sel_method->is_default_method()) {1094tty->print("default ");1095}1096if (sel_method->is_overpass()) {1097tty->print("overpass");1098}1099tty->cr();1100}11011102// setup result1103result.set_static(resolved_klass, sel_method, CHECK);1104}11051106void LinkResolver::resolve_virtual_call(CallInfo& result, Handle recv, KlassHandle receiver_klass, KlassHandle resolved_klass,1107Symbol* method_name, Symbol* method_signature, KlassHandle current_klass,1108bool check_access, bool check_null_and_abstract, TRAPS) {1109methodHandle resolved_method;1110linktime_resolve_virtual_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);1111runtime_resolve_virtual_method(result, resolved_method, resolved_klass, recv, receiver_klass, check_null_and_abstract, CHECK);1112}11131114// throws linktime exceptions1115void LinkResolver::linktime_resolve_virtual_method(methodHandle &resolved_method, KlassHandle resolved_klass,1116Symbol* method_name, Symbol* method_signature,1117KlassHandle current_klass, bool check_access, TRAPS) {1118// normal method resolution1119resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, true, CHECK);11201121assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");1122assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");11231124// check if private interface method1125if (resolved_klass->is_interface() && resolved_method->is_private()) {1126ResourceMark rm(THREAD);1127char buf[200];1128jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokevirtual: method %s, caller-class:%s",1129Method::name_and_sig_as_C_string(resolved_klass(),1130resolved_method->name(),1131resolved_method->signature()),1132(current_klass.is_null() ? "<NULL>" : current_klass->internal_name()));1133THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);1134}11351136// check if not static1137if (resolved_method->is_static()) {1138ResourceMark rm(THREAD);1139char buf[200];1140jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass(),1141resolved_method->name(),1142resolved_method->signature()));1143THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);1144}11451146if (PrintVtables && Verbose) {1147ResourceMark rm(THREAD);1148tty->print("invokevirtual resolved method: caller-class:%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ",1149(current_klass.is_null() ? "<NULL>" : current_klass->internal_name()),1150(resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),1151Method::name_and_sig_as_C_string(resolved_klass(),1152resolved_method->name(),1153resolved_method->signature()),1154resolved_method->method_holder()->internal_name()1155);1156resolved_method->access_flags().print_on(tty);1157if (resolved_method->is_default_method()) {1158tty->print("default ");1159}1160if (resolved_method->is_overpass()) {1161tty->print("overpass");1162}1163tty->cr();1164}1165}11661167// throws runtime exceptions1168void LinkResolver::runtime_resolve_virtual_method(CallInfo& result,1169methodHandle resolved_method,1170KlassHandle resolved_klass,1171Handle recv,1172KlassHandle recv_klass,1173bool check_null_and_abstract,1174TRAPS) {11751176// setup default return values1177int vtable_index = Method::invalid_vtable_index;1178methodHandle selected_method;11791180assert(recv.is_null() || recv->is_oop(), "receiver is not an oop");11811182// runtime method resolution1183if (check_null_and_abstract && recv.is_null()) { // check if receiver exists1184THROW(vmSymbols::java_lang_NullPointerException());1185}11861187// Virtual methods cannot be resolved before its klass has been linked, for otherwise the Method*'s1188// has not been rewritten, and the vtable initialized. Make sure to do this after the nullcheck, since1189// a missing receiver might result in a bogus lookup.1190assert(resolved_method->method_holder()->is_linked(), "must be linked");11911192// do lookup based on receiver klass using the vtable index1193if (resolved_method->method_holder()->is_interface()) { // default or miranda method1194vtable_index = vtable_index_of_interface_method(resolved_klass,1195resolved_method);1196assert(vtable_index >= 0 , "we should have valid vtable index at this point");11971198InstanceKlass* inst = InstanceKlass::cast(recv_klass());1199selected_method = methodHandle(THREAD, inst->method_at_vtable(vtable_index));1200} else {1201// at this point we are sure that resolved_method is virtual and not1202// a default or miranda method; therefore, it must have a valid vtable index.1203assert(!resolved_method->has_itable_index(), "");1204vtable_index = resolved_method->vtable_index();1205// We could get a negative vtable_index for final methods,1206// because as an optimization they are they are never put in the vtable,1207// unless they override an existing method.1208// If we do get a negative, it means the resolved method is the the selected1209// method, and it can never be changed by an override.1210if (vtable_index == Method::nonvirtual_vtable_index) {1211assert(resolved_method->can_be_statically_bound(), "cannot override this method");1212selected_method = resolved_method;1213} else {1214// recv_klass might be an arrayKlassOop but all vtables start at1215// the same place. The cast is to avoid virtual call and assertion.1216InstanceKlass* inst = (InstanceKlass*)recv_klass();1217selected_method = methodHandle(THREAD, inst->method_at_vtable(vtable_index));1218}1219}12201221// check if method exists1222if (selected_method.is_null()) {1223ResourceMark rm(THREAD);1224THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),1225Method::name_and_sig_as_C_string(resolved_klass(),1226resolved_method->name(),1227resolved_method->signature()));1228}12291230// check if abstract1231if (check_null_and_abstract && selected_method->is_abstract()) {1232ResourceMark rm(THREAD);1233THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),1234Method::name_and_sig_as_C_string(resolved_klass(),1235selected_method->name(),1236selected_method->signature()));1237}12381239if (PrintVtables && Verbose) {1240ResourceMark rm(THREAD);1241tty->print("invokevirtual selected method: receiver-class:%s, resolved-class:%s, method:%s, method_holder:%s, vtable_index:%d, access_flags: ",1242(recv_klass.is_null() ? "<NULL>" : recv_klass->internal_name()),1243(resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),1244Method::name_and_sig_as_C_string(resolved_klass(),1245resolved_method->name(),1246resolved_method->signature()),1247selected_method->method_holder()->internal_name(),1248vtable_index1249);1250selected_method->access_flags().print_on(tty);1251if (selected_method->is_default_method()) {1252tty->print("default ");1253}1254if (selected_method->is_overpass()) {1255tty->print("overpass");1256}1257tty->cr();1258}1259// setup result1260result.set_virtual(resolved_klass, recv_klass, resolved_method, selected_method, vtable_index, CHECK);1261}12621263void LinkResolver::resolve_interface_call(CallInfo& result, Handle recv, KlassHandle recv_klass, KlassHandle resolved_klass,1264Symbol* method_name, Symbol* method_signature, KlassHandle current_klass,1265bool check_access, bool check_null_and_abstract, TRAPS) {1266methodHandle resolved_method;1267linktime_resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);1268runtime_resolve_interface_method(result, resolved_method, resolved_klass, recv, recv_klass, check_null_and_abstract, CHECK);1269}12701271// throws linktime exceptions1272void LinkResolver::linktime_resolve_interface_method(methodHandle& resolved_method, KlassHandle resolved_klass, Symbol* method_name,1273Symbol* method_signature, KlassHandle current_klass, bool check_access, TRAPS) {1274// normal interface method resolution1275resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, true, CHECK);12761277assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");1278assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");1279}12801281// throws runtime exceptions1282void LinkResolver::runtime_resolve_interface_method(CallInfo& result, methodHandle resolved_method, KlassHandle resolved_klass,1283Handle recv, KlassHandle recv_klass, bool check_null_and_abstract, TRAPS) {1284// check if receiver exists1285if (check_null_and_abstract && recv.is_null()) {1286THROW(vmSymbols::java_lang_NullPointerException());1287}12881289// check if private interface method1290if (resolved_klass->is_interface() && resolved_method->is_private()) {1291ResourceMark rm(THREAD);1292char buf[200];1293jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokeinterface: method %s",1294Method::name_and_sig_as_C_string(resolved_klass(),1295resolved_method->name(),1296resolved_method->signature()));1297THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);1298}12991300// check if receiver klass implements the resolved interface1301if (!recv_klass->is_subtype_of(resolved_klass())) {1302ResourceMark rm(THREAD);1303char buf[200];1304jio_snprintf(buf, sizeof(buf), "Class %s does not implement the requested interface %s",1305recv_klass()->external_name(),1306resolved_klass()->external_name());1307THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);1308}13091310// do lookup based on receiver klass1311methodHandle sel_method;1312// This search must match the linktime preparation search for itable initialization1313// to correctly enforce loader constraints for interface method inheritance1314lookup_instance_method_in_klasses(sel_method, recv_klass,1315resolved_method->name(),1316resolved_method->signature(), CHECK);1317if (sel_method.is_null() && !check_null_and_abstract) {1318// In theory this is a harmless placeholder value, but1319// in practice leaving in null affects the nsk default method tests.1320// This needs further study.1321sel_method = resolved_method;1322}1323// check if method exists1324if (sel_method.is_null()) {1325ResourceMark rm(THREAD);1326THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),1327Method::name_and_sig_as_C_string(recv_klass(),1328resolved_method->name(),1329resolved_method->signature()));1330}1331// check access1332// Throw Illegal Access Error if sel_method is not public.1333if (!sel_method->is_public()) {1334ResourceMark rm(THREAD);1335THROW_MSG(vmSymbols::java_lang_IllegalAccessError(),1336Method::name_and_sig_as_C_string(recv_klass(),1337sel_method->name(),1338sel_method->signature()));1339}1340// check if abstract1341if (check_null_and_abstract && sel_method->is_abstract()) {1342ResourceMark rm(THREAD);1343THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),1344Method::name_and_sig_as_C_string(recv_klass(),1345sel_method->name(),1346sel_method->signature()));1347}13481349if (TraceItables && Verbose) {1350ResourceMark rm(THREAD);1351tty->print("invokeinterface selected method: receiver-class:%s, resolved-class:%s, method:%s, method_holder:%s, access_flags: ",1352(recv_klass.is_null() ? "<NULL>" : recv_klass->internal_name()),1353(resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),1354Method::name_and_sig_as_C_string(resolved_klass(),1355resolved_method->name(),1356resolved_method->signature()),1357sel_method->method_holder()->internal_name()1358);1359sel_method->access_flags().print_on(tty);1360if (sel_method->is_default_method()) {1361tty->print("default ");1362}1363if (sel_method->is_overpass()) {1364tty->print("overpass");1365}1366tty->cr();1367}1368// setup result1369if (!resolved_method->has_itable_index()) {1370int vtable_index = resolved_method->vtable_index();1371assert(vtable_index == sel_method->vtable_index(), "sanity check");1372result.set_virtual(resolved_klass, recv_klass, resolved_method, sel_method, vtable_index, CHECK);1373} else {1374int itable_index = resolved_method()->itable_index();1375result.set_interface(resolved_klass, recv_klass, resolved_method, sel_method, itable_index, CHECK);1376}1377}137813791380methodHandle LinkResolver::linktime_resolve_interface_method_or_null(1381KlassHandle resolved_klass,1382Symbol* method_name,1383Symbol* method_signature,1384KlassHandle current_klass,1385bool check_access) {1386EXCEPTION_MARK;1387methodHandle method_result;1388linktime_resolve_interface_method(method_result, resolved_klass, method_name, method_signature, current_klass, check_access, THREAD);1389if (HAS_PENDING_EXCEPTION) {1390CLEAR_PENDING_EXCEPTION;1391return methodHandle();1392} else {1393return method_result;1394}1395}13961397methodHandle LinkResolver::linktime_resolve_virtual_method_or_null(1398KlassHandle resolved_klass,1399Symbol* method_name,1400Symbol* method_signature,1401KlassHandle current_klass,1402bool check_access) {1403EXCEPTION_MARK;1404methodHandle method_result;1405linktime_resolve_virtual_method(method_result, resolved_klass, method_name, method_signature, current_klass, check_access, THREAD);1406if (HAS_PENDING_EXCEPTION) {1407CLEAR_PENDING_EXCEPTION;1408return methodHandle();1409} else {1410return method_result;1411}1412}14131414methodHandle LinkResolver::resolve_virtual_call_or_null(1415KlassHandle receiver_klass,1416KlassHandle resolved_klass,1417Symbol* name,1418Symbol* signature,1419KlassHandle current_klass,1420bool check_access) {1421EXCEPTION_MARK;1422CallInfo info;1423resolve_virtual_call(info, Handle(), receiver_klass, resolved_klass, name, signature, current_klass, check_access, false, THREAD);1424if (HAS_PENDING_EXCEPTION) {1425CLEAR_PENDING_EXCEPTION;1426return methodHandle();1427}1428return info.selected_method();1429}14301431methodHandle LinkResolver::resolve_interface_call_or_null(1432KlassHandle receiver_klass,1433KlassHandle resolved_klass,1434Symbol* name,1435Symbol* signature,1436KlassHandle current_klass,1437bool check_access) {1438EXCEPTION_MARK;1439CallInfo info;1440resolve_interface_call(info, Handle(), receiver_klass, resolved_klass, name, signature, current_klass, check_access, false, THREAD);1441if (HAS_PENDING_EXCEPTION) {1442CLEAR_PENDING_EXCEPTION;1443return methodHandle();1444}1445return info.selected_method();1446}14471448int LinkResolver::resolve_virtual_vtable_index(1449KlassHandle receiver_klass,1450KlassHandle resolved_klass,1451Symbol* name,1452Symbol* signature,1453KlassHandle current_klass) {1454EXCEPTION_MARK;1455CallInfo info;1456resolve_virtual_call(info, Handle(), receiver_klass, resolved_klass, name, signature, current_klass, true, false, THREAD);1457if (HAS_PENDING_EXCEPTION) {1458CLEAR_PENDING_EXCEPTION;1459return Method::invalid_vtable_index;1460}1461return info.vtable_index();1462}14631464methodHandle LinkResolver::resolve_static_call_or_null(1465KlassHandle resolved_klass,1466Symbol* name,1467Symbol* signature,1468KlassHandle current_klass,1469bool check_access) {1470EXCEPTION_MARK;1471CallInfo info;1472resolve_static_call(info, resolved_klass, name, signature, current_klass, check_access, false, THREAD);1473if (HAS_PENDING_EXCEPTION) {1474CLEAR_PENDING_EXCEPTION;1475return methodHandle();1476}1477return info.selected_method();1478}14791480methodHandle LinkResolver::resolve_special_call_or_null(1481KlassHandle resolved_klass,1482Symbol* name,1483Symbol* signature,1484KlassHandle current_klass,1485bool check_access) {1486EXCEPTION_MARK;1487CallInfo info;1488resolve_special_call(info, Handle(), resolved_klass, name, signature, current_klass, check_access, THREAD);1489if (HAS_PENDING_EXCEPTION) {1490CLEAR_PENDING_EXCEPTION;1491return methodHandle();1492}1493return info.selected_method();1494}1495149614971498//------------------------------------------------------------------------------------------------------------------------1499// ConstantPool entries15001501void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, constantPoolHandle pool, int index, Bytecodes::Code byte, TRAPS) {1502switch (byte) {1503case Bytecodes::_invokestatic : resolve_invokestatic (result, pool, index, CHECK); break;1504case Bytecodes::_invokespecial : resolve_invokespecial (result, recv, pool, index, CHECK); break;1505case Bytecodes::_invokevirtual : resolve_invokevirtual (result, recv, pool, index, CHECK); break;1506case Bytecodes::_invokehandle : resolve_invokehandle (result, pool, index, CHECK); break;1507case Bytecodes::_invokedynamic : resolve_invokedynamic (result, pool, index, CHECK); break;1508case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break;1509}1510return;1511}15121513void LinkResolver::resolve_pool(KlassHandle& resolved_klass, Symbol*& method_name, Symbol*& method_signature,1514KlassHandle& current_klass, constantPoolHandle pool, int index, TRAPS) {1515// resolve klass1516resolve_klass(resolved_klass, pool, index, CHECK);15171518// Get name, signature, and static klass1519method_name = pool->name_ref_at(index);1520method_signature = pool->signature_ref_at(index);1521current_klass = KlassHandle(THREAD, pool->pool_holder());1522}152315241525void LinkResolver::resolve_invokestatic(CallInfo& result, constantPoolHandle pool, int index, TRAPS) {1526KlassHandle resolved_klass;1527Symbol* method_name = NULL;1528Symbol* method_signature = NULL;1529KlassHandle current_klass;1530resolve_pool(resolved_klass, method_name, method_signature, current_klass, pool, index, CHECK);1531resolve_static_call(result, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK);1532}153315341535void LinkResolver::resolve_invokespecial(CallInfo& result, Handle recv, constantPoolHandle pool, int index, TRAPS) {1536KlassHandle resolved_klass;1537Symbol* method_name = NULL;1538Symbol* method_signature = NULL;1539KlassHandle current_klass;1540resolve_pool(resolved_klass, method_name, method_signature, current_klass, pool, index, CHECK);1541resolve_special_call(result, recv, resolved_klass, method_name, method_signature, current_klass, true, CHECK);1542}154315441545void LinkResolver::resolve_invokevirtual(CallInfo& result, Handle recv,1546constantPoolHandle pool, int index,1547TRAPS) {15481549KlassHandle resolved_klass;1550Symbol* method_name = NULL;1551Symbol* method_signature = NULL;1552KlassHandle current_klass;1553resolve_pool(resolved_klass, method_name, method_signature, current_klass, pool, index, CHECK);1554KlassHandle recvrKlass (THREAD, recv.is_null() ? (Klass*)NULL : recv->klass());1555resolve_virtual_call(result, recv, recvrKlass, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK);1556}155715581559void LinkResolver::resolve_invokeinterface(CallInfo& result, Handle recv, constantPoolHandle pool, int index, TRAPS) {1560KlassHandle resolved_klass;1561Symbol* method_name = NULL;1562Symbol* method_signature = NULL;1563KlassHandle current_klass;1564resolve_pool(resolved_klass, method_name, method_signature, current_klass, pool, index, CHECK);1565KlassHandle recvrKlass (THREAD, recv.is_null() ? (Klass*)NULL : recv->klass());1566resolve_interface_call(result, recv, recvrKlass, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK);1567}156815691570void LinkResolver::resolve_invokehandle(CallInfo& result, constantPoolHandle pool, int index, TRAPS) {1571assert(EnableInvokeDynamic, "");1572// This guy is reached from InterpreterRuntime::resolve_invokehandle.1573KlassHandle resolved_klass;1574Symbol* method_name = NULL;1575Symbol* method_signature = NULL;1576KlassHandle current_klass;1577resolve_pool(resolved_klass, method_name, method_signature, current_klass, pool, index, CHECK);1578if (TraceMethodHandles) {1579ResourceMark rm(THREAD);1580tty->print_cr("resolve_invokehandle %s %s", method_name->as_C_string(), method_signature->as_C_string());1581}1582resolve_handle_call(result, resolved_klass, method_name, method_signature, current_klass, CHECK);1583}15841585void LinkResolver::resolve_handle_call(CallInfo& result, KlassHandle resolved_klass,1586Symbol* method_name, Symbol* method_signature,1587KlassHandle current_klass,1588TRAPS) {1589// JSR 292: this must be an implicitly generated method MethodHandle.invokeExact(*...) or similar1590assert(resolved_klass() == SystemDictionary::MethodHandle_klass(), "");1591assert(MethodHandles::is_signature_polymorphic_name(method_name), "");1592methodHandle resolved_method;1593Handle resolved_appendix;1594Handle resolved_method_type;1595lookup_polymorphic_method(resolved_method, resolved_klass,1596method_name, method_signature,1597current_klass, &resolved_appendix, &resolved_method_type, CHECK);1598result.set_handle(resolved_method, resolved_appendix, resolved_method_type, CHECK);1599}16001601static void wrap_invokedynamic_exception(TRAPS) {1602if (HAS_PENDING_EXCEPTION) {1603if (TraceMethodHandles) {1604tty->print_cr("invokedynamic throws BSME for " INTPTR_FORMAT, p2i((void *)PENDING_EXCEPTION));1605PENDING_EXCEPTION->print();1606}1607if (PENDING_EXCEPTION->is_a(SystemDictionary::BootstrapMethodError_klass())) {1608// throw these guys, since they are already wrapped1609return;1610}1611if (!PENDING_EXCEPTION->is_a(SystemDictionary::LinkageError_klass())) {1612// intercept only LinkageErrors which might have failed to wrap1613return;1614}1615// See the "Linking Exceptions" section for the invokedynamic instruction in the JVMS.1616Handle nested_exception(THREAD, PENDING_EXCEPTION);1617CLEAR_PENDING_EXCEPTION;1618THROW_CAUSE(vmSymbols::java_lang_BootstrapMethodError(), nested_exception)1619}1620}16211622void LinkResolver::resolve_invokedynamic(CallInfo& result, constantPoolHandle pool, int index, TRAPS) {1623assert(EnableInvokeDynamic, "");16241625//resolve_pool(<resolved_klass>, method_name, method_signature, current_klass, pool, index, CHECK);1626Symbol* method_name = pool->name_ref_at(index);1627Symbol* method_signature = pool->signature_ref_at(index);1628KlassHandle current_klass = KlassHandle(THREAD, pool->pool_holder());16291630// Resolve the bootstrap specifier (BSM + optional arguments).1631Handle bootstrap_specifier;1632// Check if CallSite has been bound already:1633ConstantPoolCacheEntry* cpce = pool->invokedynamic_cp_cache_entry_at(index);1634if (cpce->is_f1_null()) {1635int pool_index = cpce->constant_pool_index();1636oop bsm_info = pool->resolve_bootstrap_specifier_at(pool_index, THREAD);1637wrap_invokedynamic_exception(CHECK);1638assert(bsm_info != NULL, "");1639// FIXME: Cache this once per BootstrapMethods entry, not once per CONSTANT_InvokeDynamic.1640bootstrap_specifier = Handle(THREAD, bsm_info);1641}1642if (!cpce->is_f1_null()) {1643methodHandle method( THREAD, cpce->f1_as_method());1644Handle appendix( THREAD, cpce->appendix_if_resolved(pool));1645Handle method_type(THREAD, cpce->method_type_if_resolved(pool));1646result.set_handle(method, appendix, method_type, THREAD);1647wrap_invokedynamic_exception(CHECK);1648return;1649}16501651if (TraceMethodHandles) {1652ResourceMark rm(THREAD);1653tty->print_cr("resolve_invokedynamic #%d %s %s",1654ConstantPool::decode_invokedynamic_index(index),1655method_name->as_C_string(), method_signature->as_C_string());1656tty->print(" BSM info: "); bootstrap_specifier->print();1657}16581659resolve_dynamic_call(result, bootstrap_specifier, method_name, method_signature, current_klass, CHECK);1660}16611662void LinkResolver::resolve_dynamic_call(CallInfo& result,1663Handle bootstrap_specifier,1664Symbol* method_name, Symbol* method_signature,1665KlassHandle current_klass,1666TRAPS) {1667// JSR 292: this must resolve to an implicitly generated method MH.linkToCallSite(*...)1668// The appendix argument is likely to be a freshly-created CallSite.1669Handle resolved_appendix;1670Handle resolved_method_type;1671methodHandle resolved_method =1672SystemDictionary::find_dynamic_call_site_invoker(current_klass,1673bootstrap_specifier,1674method_name, method_signature,1675&resolved_appendix,1676&resolved_method_type,1677THREAD);1678wrap_invokedynamic_exception(CHECK);1679result.set_handle(resolved_method, resolved_appendix, resolved_method_type, THREAD);1680wrap_invokedynamic_exception(CHECK);1681}16821683//------------------------------------------------------------------------------------------------------------------------1684#ifndef PRODUCT16851686void CallInfo::print() {1687ResourceMark rm;1688const char* kindstr = "unknown";1689switch (_call_kind) {1690case direct_call: kindstr = "direct"; break;1691case vtable_call: kindstr = "vtable"; break;1692case itable_call: kindstr = "itable"; break;1693}1694tty->print_cr("Call %s@%d %s", kindstr, _call_index,1695_resolved_method.is_null() ? "(none)" : _resolved_method->name_and_sig_as_C_string());1696}16971698#endif169917001701