Path: blob/master/src/hotspot/share/interpreter/linkResolver.hpp
64441 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#ifndef SHARE_INTERPRETER_LINKRESOLVER_HPP25#define SHARE_INTERPRETER_LINKRESOLVER_HPP2627#include "interpreter/bootstrapInfo.hpp"28#include "oops/method.hpp"2930// All the necessary definitions for run-time link resolution.3132// CallInfo provides all the information gathered for a particular33// linked call site after resolving it. A link is any reference34// made from within the bytecodes of a method to an object outside of35// that method. If the info is invalid, the link has not been resolved36// successfully.3738class CallInfo : public StackObj {39public:40// Ways that a method call might be selected (or not) based on receiver type.41// Note that an invokevirtual instruction might be linked with no_dispatch,42// and an invokeinterface instruction might be linked with any of the three options43enum CallKind {44direct_call, // jump into resolved_method (must be concrete)45vtable_call, // select recv.klass.method_at_vtable(index)46itable_call, // select recv.klass.method_at_itable(resolved_method.holder, index)47unknown_kind = -148};49private:50Klass* _resolved_klass; // static receiver klass, resolved from a symbolic reference51methodHandle _resolved_method; // static target method52methodHandle _selected_method; // dynamic (actual) target method53CallKind _call_kind; // kind of call (static(=bytecode static/special +54// others inferred), vtable, itable)55int _call_index; // vtable or itable index of selected class method (if any)56Handle _resolved_appendix; // extra argument in constant pool (if CPCE::has_appendix)57Handle _resolved_method_name; // Object holding the ResolvedMethodName5859void set_static(Klass* resolved_klass, const methodHandle& resolved_method, TRAPS);60void set_interface(Klass* resolved_klass,61const methodHandle& resolved_method,62const methodHandle& selected_method,63int itable_index, TRAPS);64void set_virtual(Klass* resolved_klass,65const methodHandle& resolved_method,66const methodHandle& selected_method,67int vtable_index, TRAPS);68void set_handle(Klass* resolved_klass,69const methodHandle& resolved_method,70Handle resolved_appendix, TRAPS);71void set_common(Klass* resolved_klass,72const methodHandle& resolved_method,73const methodHandle& selected_method,74CallKind kind,75int index, TRAPS);7677friend class BootstrapInfo;78friend class LinkResolver;7980public:81CallInfo() {82#ifndef PRODUCT83_call_kind = CallInfo::unknown_kind;84_call_index = Method::garbage_vtable_index;85#endif //PRODUCT86}8788// utility to extract an effective CallInfo from a method and an optional receiver limit89// does not queue the method for compilation. This also creates a ResolvedMethodName90// object for the resolved_method.91CallInfo(Method* resolved_method, Klass* resolved_klass, TRAPS);9293Klass* resolved_klass() const { return _resolved_klass; }94Method* resolved_method() const { return _resolved_method(); }95Method* selected_method() const { return _selected_method(); }96Handle resolved_appendix() const { return _resolved_appendix; }97Handle resolved_method_name() const { return _resolved_method_name; }98// Materialize a java.lang.invoke.ResolvedMethodName for this resolved_method99void set_resolved_method_name(TRAPS);100101BasicType result_type() const { return selected_method()->result_type(); }102CallKind call_kind() const { return _call_kind; }103int vtable_index() const {104// Even for interface calls the vtable index could be non-negative.105// See CallInfo::set_interface.106assert(has_vtable_index() || is_statically_bound(), "");107assert(call_kind() == vtable_call || call_kind() == direct_call, "");108// The returned value is < 0 if the call is statically bound.109// But, the returned value may be >= 0 even if the kind is direct_call.110// It is up to the caller to decide which way to go.111return _call_index;112}113int itable_index() const {114assert(call_kind() == itable_call, "");115// The returned value is always >= 0, a valid itable index.116return _call_index;117}118119// debugging120#ifdef ASSERT121bool has_vtable_index() const { return _call_index >= 0 && _call_kind != CallInfo::itable_call; }122bool is_statically_bound() const { return _call_index == Method::nonvirtual_vtable_index; }123#endif //ASSERT124void verify() PRODUCT_RETURN;125void print() PRODUCT_RETURN;126};127128129// Condensed information from constant pool to use to resolve the method or field.130// resolved_klass = specified class (i.e., static receiver class)131// current_klass = sending method holder (i.e., class containing the method132// containing the call being resolved)133// current_method = sending method (relevant for field resolution)134class LinkInfo : public StackObj {135Symbol* _name; // extracted from JVM_CONSTANT_NameAndType136Symbol* _signature;137Klass* _resolved_klass; // class that the constant pool entry points to138Klass* _current_klass; // class that owns the constant pool139methodHandle _current_method; // sending method140bool _check_access;141bool _check_loader_constraints;142constantTag _tag;143144public:145enum class AccessCheck { required, skip };146enum class LoaderConstraintCheck { required, skip };147148LinkInfo(const constantPoolHandle& pool, int index, const methodHandle& current_method, TRAPS);149LinkInfo(const constantPoolHandle& pool, int index, TRAPS);150151// Condensed information from other call sites within the vm.152LinkInfo(Klass* resolved_klass, Symbol* name, Symbol* signature, Klass* current_klass,153AccessCheck check_access = AccessCheck::required,154LoaderConstraintCheck check_loader_constraints = LoaderConstraintCheck::required,155constantTag tag = JVM_CONSTANT_Invalid) :156_name(name),157_signature(signature), _resolved_klass(resolved_klass), _current_klass(current_klass), _current_method(methodHandle()),158_check_access(check_access == AccessCheck::required),159_check_loader_constraints(check_loader_constraints == LoaderConstraintCheck::required), _tag(tag) {}160161LinkInfo(Klass* resolved_klass, Symbol* name, Symbol* signature, const methodHandle& current_method,162AccessCheck check_access = AccessCheck::required,163LoaderConstraintCheck check_loader_constraints = LoaderConstraintCheck::required,164constantTag tag = JVM_CONSTANT_Invalid) :165_name(name),166_signature(signature), _resolved_klass(resolved_klass), _current_klass(current_method->method_holder()), _current_method(current_method),167_check_access(check_access == AccessCheck::required),168_check_loader_constraints(check_loader_constraints == LoaderConstraintCheck::required), _tag(tag) {}169170171// Case where we just find the method and don't check access against the current class172LinkInfo(Klass* resolved_klass, Symbol*name, Symbol* signature) :173_name(name),174_signature(signature), _resolved_klass(resolved_klass), _current_klass(NULL), _current_method(methodHandle()),175_check_access(false), _check_loader_constraints(false), _tag(JVM_CONSTANT_Invalid) {}176177// accessors178Symbol* name() const { return _name; }179Symbol* signature() const { return _signature; }180Klass* resolved_klass() const { return _resolved_klass; }181Klass* current_klass() const { return _current_klass; }182Method* current_method() const { return _current_method(); }183constantTag tag() const { return _tag; }184bool check_access() const { return _check_access; }185bool check_loader_constraints() const { return _check_loader_constraints; }186void print() PRODUCT_RETURN;187};188189// Link information for getfield/putfield & getstatic/putstatic bytecodes190// is represented using a fieldDescriptor.191192// The LinkResolver is used to resolve constant-pool references at run-time.193// It does all necessary link-time checks & throws exceptions if necessary.194195class LinkResolver: AllStatic {196friend class klassVtable;197friend class klassItable;198199private:200201static Method* lookup_method_in_klasses(const LinkInfo& link_info,202bool checkpolymorphism,203bool in_imethod_resolve);204static Method* lookup_method_in_interfaces(const LinkInfo& link_info);205206static Method* lookup_polymorphic_method(const LinkInfo& link_info,207Handle *appendix_result_or_null, TRAPS);208JVMCI_ONLY(public:) // Needed for CompilerToVM.resolveMethod()209// Not Linktime so doesn't take LinkInfo210static Method* lookup_instance_method_in_klasses (Klass* klass, Symbol* name, Symbol* signature,211Klass::PrivateLookupMode private_mode);212JVMCI_ONLY(private:)213214// Similar loader constraint checking functions that throw215// LinkageError with descriptive message.216static void check_method_loader_constraints(const LinkInfo& link_info,217const methodHandle& resolved_method,218const char* method_type, TRAPS);219static void check_field_loader_constraints(Symbol* field, Symbol* sig,220Klass* current_klass,221Klass* sel_klass, TRAPS);222223static Method* resolve_interface_method(const LinkInfo& link_info, Bytecodes::Code code, TRAPS);224static Method* resolve_method (const LinkInfo& link_info, Bytecodes::Code code, TRAPS);225226static Method* linktime_resolve_static_method (const LinkInfo& link_info, TRAPS);227static Method* linktime_resolve_special_method (const LinkInfo& link_info, TRAPS);228static Method* linktime_resolve_virtual_method (const LinkInfo& link_info, TRAPS);229static Method* linktime_resolve_interface_method (const LinkInfo& link_info, TRAPS);230231static void runtime_resolve_special_method (CallInfo& result,232const LinkInfo& link_info,233const methodHandle& resolved_method,234Handle recv, TRAPS);235236static void runtime_resolve_virtual_method (CallInfo& result,237const methodHandle& resolved_method,238Klass* resolved_klass,239Handle recv,240Klass* recv_klass,241bool check_null_and_abstract, TRAPS);242static void runtime_resolve_interface_method (CallInfo& result,243const methodHandle& resolved_method,244Klass* resolved_klass,245Handle recv,246Klass* recv_klass,247bool check_null_and_abstract, TRAPS);248249static bool resolve_previously_linked_invokehandle(CallInfo& result,250const LinkInfo& link_info,251const constantPoolHandle& pool,252int index, TRAPS);253254static void check_field_accessability(Klass* ref_klass,255Klass* resolved_klass,256Klass* sel_klass,257const fieldDescriptor& fd, TRAPS);258static void check_method_accessability(Klass* ref_klass,259Klass* resolved_klass,260Klass* sel_klass,261const methodHandle& sel_method, TRAPS);262263// runtime resolving from constant pool264static void resolve_invokestatic (CallInfo& result,265const constantPoolHandle& pool, int index, TRAPS);266static void resolve_invokespecial (CallInfo& result, Handle recv,267const constantPoolHandle& pool, int index, TRAPS);268static void resolve_invokevirtual (CallInfo& result, Handle recv,269const constantPoolHandle& pool, int index, TRAPS);270static void resolve_invokeinterface(CallInfo& result, Handle recv,271const constantPoolHandle& pool, int index, TRAPS);272static void resolve_invokedynamic (CallInfo& result,273const constantPoolHandle& pool, int index, TRAPS);274static void resolve_invokehandle (CallInfo& result,275const constantPoolHandle& pool, int index, TRAPS);276public:277// constant pool resolving278static void check_klass_accessibility(Klass* ref_klass, Klass* sel_klass, TRAPS);279280// static resolving calls (will not run any Java code);281// used only from Bytecode_invoke::static_target282static Method* resolve_method_statically(Bytecodes::Code code,283const constantPoolHandle& pool,284int index, TRAPS);285286static void resolve_field_access(fieldDescriptor& result,287const constantPoolHandle& pool,288int index,289const methodHandle& method,290Bytecodes::Code byte, TRAPS);291static void resolve_field(fieldDescriptor& result, const LinkInfo& link_info,292Bytecodes::Code access_kind,293bool initialize_class, TRAPS);294295static void resolve_static_call (CallInfo& result,296const LinkInfo& link_info,297bool initialize_klass, TRAPS);298static void resolve_special_call (CallInfo& result,299Handle recv,300const LinkInfo& link_info,301TRAPS);302static void resolve_virtual_call (CallInfo& result, Handle recv, Klass* recv_klass,303const LinkInfo& link_info,304bool check_null_and_abstract, TRAPS);305static void resolve_interface_call(CallInfo& result, Handle recv, Klass* recv_klass,306const LinkInfo& link_info,307bool check_null_and_abstract, TRAPS);308static void resolve_handle_call (CallInfo& result,309const LinkInfo& link_info, TRAPS);310static void resolve_dynamic_call (CallInfo& result,311BootstrapInfo& bootstrap_specifier, TRAPS);312313// same as above for compile-time resolution; but returns null handle instead of throwing314// an exception on error also, does not initialize klass (i.e., no side effects)315static Method* resolve_virtual_call_or_null(Klass* receiver_klass,316const LinkInfo& link_info);317static Method* resolve_interface_call_or_null(Klass* receiver_klass,318const LinkInfo& link_info);319static Method* resolve_static_call_or_null(const LinkInfo& link_info);320static Method* resolve_special_call_or_null(const LinkInfo& link_info);321322static int vtable_index_of_interface_method(Klass* klass, const methodHandle& resolved_method);323324// same as above for compile-time resolution; returns vtable_index if current_klass if linked325static int resolve_virtual_vtable_index (Klass* receiver_klass,326const LinkInfo& link_info);327328// static resolving for compiler (does not throw exceptions, returns null handle if unsuccessful)329static Method* linktime_resolve_virtual_method_or_null (const LinkInfo& link_info);330static Method* linktime_resolve_interface_method_or_null(const LinkInfo& link_info);331332// runtime resolving from constant pool333static void resolve_invoke(CallInfo& result, Handle recv,334const constantPoolHandle& pool, int index,335Bytecodes::Code byte, TRAPS);336337// runtime resolving from attached method338static void resolve_invoke(CallInfo& result, Handle& recv,339const methodHandle& attached_method,340Bytecodes::Code byte, TRAPS);341342// Only resolved method known.343static void throw_abstract_method_error(const methodHandle& resolved_method, TRAPS) {344throw_abstract_method_error(resolved_method, methodHandle(), NULL, CHECK);345}346// Resolved method and receiver klass know.347static void throw_abstract_method_error(const methodHandle& resolved_method, Klass *recv_klass, TRAPS) {348throw_abstract_method_error(resolved_method, methodHandle(), recv_klass, CHECK);349}350// Selected method is abstract.351static void throw_abstract_method_error(const methodHandle& resolved_method,352const methodHandle& selected_method,353Klass *recv_klass, TRAPS);354};355#endif // SHARE_INTERPRETER_LINKRESOLVER_HPP356357358