Path: blob/master/src/hotspot/share/interpreter/linkResolver.hpp
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#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(const methodHandle& resolved_method,69Handle resolved_appendix, TRAPS);70void set_handle(Klass* resolved_klass,71const methodHandle& resolved_method,72Handle resolved_appendix, TRAPS);73void set_common(Klass* resolved_klass,74const methodHandle& resolved_method,75const methodHandle& selected_method,76CallKind kind,77int index, TRAPS);7879friend class BootstrapInfo;80friend class LinkResolver;8182public:83CallInfo() {84#ifndef PRODUCT85_call_kind = CallInfo::unknown_kind;86_call_index = Method::garbage_vtable_index;87#endif //PRODUCT88}8990// utility to extract an effective CallInfo from a method and an optional receiver limit91// does not queue the method for compilation. This also creates a ResolvedMethodName92// object for the resolved_method.93CallInfo(Method* resolved_method, Klass* resolved_klass, TRAPS);9495Klass* resolved_klass() const { return _resolved_klass; }96Method* resolved_method() const { return _resolved_method(); }97Method* selected_method() const { return _selected_method(); }98Handle resolved_appendix() const { return _resolved_appendix; }99Handle resolved_method_name() const { return _resolved_method_name; }100// Materialize a java.lang.invoke.ResolvedMethodName for this resolved_method101void set_resolved_method_name(TRAPS);102103BasicType result_type() const { return selected_method()->result_type(); }104CallKind call_kind() const { return _call_kind; }105int vtable_index() const {106// Even for interface calls the vtable index could be non-negative.107// See CallInfo::set_interface.108assert(has_vtable_index() || is_statically_bound(), "");109assert(call_kind() == vtable_call || call_kind() == direct_call, "");110// The returned value is < 0 if the call is statically bound.111// But, the returned value may be >= 0 even if the kind is direct_call.112// It is up to the caller to decide which way to go.113return _call_index;114}115int itable_index() const {116assert(call_kind() == itable_call, "");117// The returned value is always >= 0, a valid itable index.118return _call_index;119}120121// debugging122#ifdef ASSERT123bool has_vtable_index() const { return _call_index >= 0 && _call_kind != CallInfo::itable_call; }124bool is_statically_bound() const { return _call_index == Method::nonvirtual_vtable_index; }125#endif //ASSERT126void verify() PRODUCT_RETURN;127void print() PRODUCT_RETURN;128};129130131// Condensed information from constant pool to use to resolve the method or field.132// resolved_klass = specified class (i.e., static receiver class)133// current_klass = sending method holder (i.e., class containing the method134// containing the call being resolved)135// current_method = sending method (relevant for field resolution)136class LinkInfo : public StackObj {137Symbol* _name; // extracted from JVM_CONSTANT_NameAndType138Symbol* _signature;139Klass* _resolved_klass; // class that the constant pool entry points to140Klass* _current_klass; // class that owns the constant pool141methodHandle _current_method; // sending method142bool _check_access;143bool _check_loader_constraints;144constantTag _tag;145146public:147enum class AccessCheck { required, skip };148enum class LoaderConstraintCheck { required, skip };149150LinkInfo(const constantPoolHandle& pool, int index, const methodHandle& current_method, TRAPS);151LinkInfo(const constantPoolHandle& pool, int index, TRAPS);152153// Condensed information from other call sites within the vm.154LinkInfo(Klass* resolved_klass, Symbol* name, Symbol* signature, Klass* current_klass,155AccessCheck check_access = AccessCheck::required,156LoaderConstraintCheck check_loader_constraints = LoaderConstraintCheck::required,157constantTag tag = JVM_CONSTANT_Invalid) :158_name(name),159_signature(signature), _resolved_klass(resolved_klass), _current_klass(current_klass), _current_method(methodHandle()),160_check_access(check_access == AccessCheck::required),161_check_loader_constraints(check_loader_constraints == LoaderConstraintCheck::required), _tag(tag) {}162163LinkInfo(Klass* resolved_klass, Symbol* name, Symbol* signature, const methodHandle& current_method,164AccessCheck check_access = AccessCheck::required,165LoaderConstraintCheck check_loader_constraints = LoaderConstraintCheck::required,166constantTag tag = JVM_CONSTANT_Invalid) :167_name(name),168_signature(signature), _resolved_klass(resolved_klass), _current_klass(current_method->method_holder()), _current_method(current_method),169_check_access(check_access == AccessCheck::required),170_check_loader_constraints(check_loader_constraints == LoaderConstraintCheck::required), _tag(tag) {}171172173// Case where we just find the method and don't check access against the current class174LinkInfo(Klass* resolved_klass, Symbol*name, Symbol* signature) :175_name(name),176_signature(signature), _resolved_klass(resolved_klass), _current_klass(NULL), _current_method(methodHandle()),177_check_access(false), _check_loader_constraints(false), _tag(JVM_CONSTANT_Invalid) {}178179// accessors180Symbol* name() const { return _name; }181Symbol* signature() const { return _signature; }182Klass* resolved_klass() const { return _resolved_klass; }183Klass* current_klass() const { return _current_klass; }184Method* current_method() const { return _current_method(); }185constantTag tag() const { return _tag; }186bool check_access() const { return _check_access; }187bool check_loader_constraints() const { return _check_loader_constraints; }188void print() PRODUCT_RETURN;189};190191// Link information for getfield/putfield & getstatic/putstatic bytecodes192// is represented using a fieldDescriptor.193194// The LinkResolver is used to resolve constant-pool references at run-time.195// It does all necessary link-time checks & throws exceptions if necessary.196197class LinkResolver: AllStatic {198friend class klassVtable;199friend class klassItable;200201private:202203static Method* lookup_method_in_klasses(const LinkInfo& link_info,204bool checkpolymorphism,205bool in_imethod_resolve);206static Method* lookup_method_in_interfaces(const LinkInfo& link_info);207208static Method* lookup_polymorphic_method(const LinkInfo& link_info,209Handle *appendix_result_or_null, TRAPS);210JVMCI_ONLY(public:) // Needed for CompilerToVM.resolveMethod()211// Not Linktime so doesn't take LinkInfo212static Method* lookup_instance_method_in_klasses (Klass* klass, Symbol* name, Symbol* signature,213Klass::PrivateLookupMode private_mode);214JVMCI_ONLY(private:)215216// Similar loader constraint checking functions that throw217// LinkageError with descriptive message.218static void check_method_loader_constraints(const LinkInfo& link_info,219const methodHandle& resolved_method,220const char* method_type, TRAPS);221static void check_field_loader_constraints(Symbol* field, Symbol* sig,222Klass* current_klass,223Klass* sel_klass, TRAPS);224225static Method* resolve_interface_method(const LinkInfo& link_info, Bytecodes::Code code, TRAPS);226static Method* resolve_method (const LinkInfo& link_info, Bytecodes::Code code, TRAPS);227228static Method* linktime_resolve_static_method (const LinkInfo& link_info, TRAPS);229static Method* linktime_resolve_special_method (const LinkInfo& link_info, TRAPS);230static Method* linktime_resolve_virtual_method (const LinkInfo& link_info, TRAPS);231static Method* linktime_resolve_interface_method (const LinkInfo& link_info, TRAPS);232233static void runtime_resolve_special_method (CallInfo& result,234const LinkInfo& link_info,235const methodHandle& resolved_method,236Handle recv, TRAPS);237238static void runtime_resolve_virtual_method (CallInfo& result,239const methodHandle& resolved_method,240Klass* resolved_klass,241Handle recv,242Klass* recv_klass,243bool check_null_and_abstract, TRAPS);244static void runtime_resolve_interface_method (CallInfo& result,245const methodHandle& resolved_method,246Klass* resolved_klass,247Handle recv,248Klass* recv_klass,249bool check_null_and_abstract, TRAPS);250251static void check_field_accessability(Klass* ref_klass,252Klass* resolved_klass,253Klass* sel_klass,254const fieldDescriptor& fd, TRAPS);255static void check_method_accessability(Klass* ref_klass,256Klass* resolved_klass,257Klass* sel_klass,258const methodHandle& sel_method, TRAPS);259260// runtime resolving from constant pool261static void resolve_invokestatic (CallInfo& result,262const constantPoolHandle& pool, int index, TRAPS);263static void resolve_invokespecial (CallInfo& result, Handle recv,264const constantPoolHandle& pool, int index, TRAPS);265static void resolve_invokevirtual (CallInfo& result, Handle recv,266const constantPoolHandle& pool, int index, TRAPS);267static void resolve_invokeinterface(CallInfo& result, Handle recv,268const constantPoolHandle& pool, int index, TRAPS);269static void resolve_invokedynamic (CallInfo& result,270const constantPoolHandle& pool, int index, TRAPS);271static void resolve_invokehandle (CallInfo& result,272const constantPoolHandle& pool, int index, TRAPS);273public:274// constant pool resolving275static void check_klass_accessibility(Klass* ref_klass, Klass* sel_klass, TRAPS);276277// static resolving calls (will not run any Java code);278// used only from Bytecode_invoke::static_target279static Method* resolve_method_statically(Bytecodes::Code code,280const constantPoolHandle& pool,281int index, TRAPS);282283static void resolve_field_access(fieldDescriptor& result,284const constantPoolHandle& pool,285int index,286const methodHandle& method,287Bytecodes::Code byte, TRAPS);288static void resolve_field(fieldDescriptor& result, const LinkInfo& link_info,289Bytecodes::Code access_kind,290bool initialize_class, TRAPS);291292static void resolve_static_call (CallInfo& result,293const LinkInfo& link_info,294bool initialize_klass, TRAPS);295static void resolve_special_call (CallInfo& result,296Handle recv,297const LinkInfo& link_info,298TRAPS);299static void resolve_virtual_call (CallInfo& result, Handle recv, Klass* recv_klass,300const LinkInfo& link_info,301bool check_null_and_abstract, TRAPS);302static void resolve_interface_call(CallInfo& result, Handle recv, Klass* recv_klass,303const LinkInfo& link_info,304bool check_null_and_abstract, TRAPS);305static void resolve_handle_call (CallInfo& result,306const LinkInfo& link_info, TRAPS);307static void resolve_dynamic_call (CallInfo& result,308BootstrapInfo& bootstrap_specifier, TRAPS);309310// same as above for compile-time resolution; but returns null handle instead of throwing311// an exception on error also, does not initialize klass (i.e., no side effects)312static Method* resolve_virtual_call_or_null(Klass* receiver_klass,313const LinkInfo& link_info);314static Method* resolve_interface_call_or_null(Klass* receiver_klass,315const LinkInfo& link_info);316static Method* resolve_static_call_or_null(const LinkInfo& link_info);317static Method* resolve_special_call_or_null(const LinkInfo& link_info);318319static int vtable_index_of_interface_method(Klass* klass, const methodHandle& resolved_method);320321// same as above for compile-time resolution; returns vtable_index if current_klass if linked322static int resolve_virtual_vtable_index (Klass* receiver_klass,323const LinkInfo& link_info);324325// static resolving for compiler (does not throw exceptions, returns null handle if unsuccessful)326static Method* linktime_resolve_virtual_method_or_null (const LinkInfo& link_info);327static Method* linktime_resolve_interface_method_or_null(const LinkInfo& link_info);328329// runtime resolving from constant pool330static void resolve_invoke(CallInfo& result, Handle recv,331const constantPoolHandle& pool, int index,332Bytecodes::Code byte, TRAPS);333334// runtime resolving from attached method335static void resolve_invoke(CallInfo& result, Handle& recv,336const methodHandle& attached_method,337Bytecodes::Code byte, TRAPS);338339public:340// Only resolved method known.341static void throw_abstract_method_error(const methodHandle& resolved_method, TRAPS) {342throw_abstract_method_error(resolved_method, methodHandle(), NULL, CHECK);343}344// Resolved method and receiver klass know.345static void throw_abstract_method_error(const methodHandle& resolved_method, Klass *recv_klass, TRAPS) {346throw_abstract_method_error(resolved_method, methodHandle(), recv_klass, CHECK);347}348// Selected method is abstract.349static void throw_abstract_method_error(const methodHandle& resolved_method,350const methodHandle& selected_method,351Klass *recv_klass, TRAPS);352};353#endif // SHARE_INTERPRETER_LINKRESOLVER_HPP354355356